Skip to main content

I2C Lib API

danger

this is a legacy library and thus not supported by Toradex anymore. We recommend that you use the new libraries for all Toradex modules. Please see the Toradex CE Libraries and Code Samples for up-to-date information.


I2C Library I2c uses system interrupt #18 The following flags can be set to act as interrupt sources: . More...

Macros

##define I2CLIB_VER_MAJ   4 Main Version of this library. More...
 #define I2CLIB_VER_MIN   4 Subversion of this library. More...
 #define I2CLIB_VER_BUILD   0 Build Number of this library. More...
 #define I2C_OFFSET_NONE   (-1) use this value in the I2CBurstRead/Write functions to avoid sending an offset byte More...
 

Enumerations

enum  I2CSpeed I2C_UNDEF_KBPS
 Specification of possible i2c data rates. More...
 

Functions

void I2cIntGetLibVersion (DWORD *pVerMaj, DWORD *pVerMin, DWORD *pBuild)
 
void I2CGetLibVersion (DWORD *pVerMaj, DWORD *pVerMin, DWORD *pBuild)
 
BOOL GetI2CLock (DWORD timeout)
 
BOOL ReleaseI2CLock ()
 
BOOL I2CInitEx (DWORD I2CRegAddr)
 
BOOL I2CInit (void)
 
void I2CDeInit (void)
 
void I2CSetSpeed (I2CSpeed Speed)
 
BOOL I2CBurstWrite (unsigned char uSlaveAddress, unsigned char *pBuffer, short ucOffset, unsigned iNumberBytes)
 
BOOL I2CBurstRead (unsigned char uSlaveAddress, unsigned char *pBuffer, short ucOffset, unsigned iNumberBytes)
 

Detailed Description

I2C Library
I2c uses system interrupt #18
The following flags can be set to act as interrupt sources:
.
Author
andy.kiser
Rev
2860
Date
2015-06-26 16:57:15 +0200 (Fr, 26 Jun 2015)
  • ICR:SAD Slave Address Detected
  • ICR:GCAD Global Call Address Detected (interrupt enabled by SADIE flag)
  • ICR:ALD Arbitration Loss Detected while in master mode
  • ICR:SSD Slave Stop Detected while in slave mode
  • ICR:BED Bus Error Detected (NAK received on transmitting a byte in master mode)
  • ICR:IRF IDBR Receive Full
  • ICR:ITE IDBR Transmit Empty
Test:
PXA320, WinCE 5.0
Target Platforms:
PXAxxx, T20

Macro Definition Documentation

#define I2C_OFFSET_NONE   (-1)
use this value in the I2CBurstRead/Write functions to avoid sending an offset byte
#define I2CLIB_VER_BUILD   0
Build Number of this library.
#define I2CLIB_VER_MAJ   4
Main Version of this library.
#define I2CLIB_VER_MIN   4
Subversion of this library.

Enumeration Type Documentation

Specification of possible i2c data rates.
Enumerator
I2C_100KBPS  
I2C_400KBPS  
I2C_UNDEF_KBPS  

Function Documentation

BOOL GetI2CLock(DWORD timeout) 
Syncronisation function if I2C bus is used in multiple threads (e.g. RTCSync which is loaded by default)
Parameters
[in]timeoutMaximum time [ms] to wait for the I2C Mutex
Return values
TRUESuccess
FALSESee GetLastError().
possible Errors: WAIT_FAILED
BOOL I2CBurstRead(unsigned char uSlaveAddress,
  unsigned char * pBuffer,
  short ucOffset,
  unsigned iNumberBytes 
 )  
Read data from one or multiple registers of an i2c device.
The function assumes that the i2c protocol defines the first transmitted byte to be the device's register offset.
Parameters
[in]uSlaveAddressThe device's i2c address (0..127) if uSlaveAddress=0xff, no slaveAddress byte will be transmitted.
[in]pBufferPointer to the data bytes to Receive
[in]ucOffsetThe device's register address (offset). This is one byte transmitted after the slave address.
Set to any negative value if no offset byte should be transmitted
[in]iNumberBytesNumber of Bytes to Receive
Return values
TRUEsuccess
FALSEError during Transmission See GetLastError(). Possible error codes:
ERROR_BUSY
ERROR_INVALID_PARAMETER
ERROR_TIMEOUT
ERROR_IO_DEVICE
ERROR_INVALID_ADDRESS
ERROR_OPERATION_ABORTED
BOOL I2CBurstWrite(unsigned char uSlaveAddress,
  unsigned char * pBuffer,
  short ucOffset,
  unsigned iNumberBytes 
 )  
Write data to one or multiple registers of an i2c device.
The function assumes that the i2c protocol defines the first transmitted byte to be the device's register offset.
Parameters
[in]uSlaveAddressThe device's i2c address (0..127) On Tegra T20 and T30 modules: if uSlaveAddress > 0x7f, then uSlaveAddress - 0x80 is used, and no stop bit is sent.
[in]pBufferPointer to the data bytes to Transmit
[in]ucOffsetThe device's register address (offset). This is one byte transmitted after the slave address.
Set to any negative value if no offset byte should be transmitted
[in]iNumberBytesNumber of Bytes to Transmit
Return values
TRUEsuccess
FALSESee GetLastError(). Possible error codes:
ERROR_BUSY
ERROR_INVALID_PARAMETER
ERROR_TIMEOUT
ERROR_IO_DEVICE
ERROR_INVALID_ADDRESS
ERROR_OPERATION_ABORTED
On Tegra 3, a byte not acknowledged will be reported with a Timeout Error rather than
an Operation Aborted Error
void I2CDeInit(void  ) 
DeInit I2C bus
Release I2C interrupt, unmap I2C registers and deinit Clock
void I2CGetLibVersion(DWORD * pVerMaj,
  DWORD * pVerMin,
  DWORD * pBuild 
 )  
Returns the library Version - for compatibility
Parameters
[out]pVerMajReturns the major version number. Set this parameter to NULL if not required.
[out]pVerMinReturns the minor version number. Set this parameter to NULL if not required.
[out]pBuildReturns the build number. Set this parameter to NULL if not required.
BOOL I2CInit(void  ) 
Initialization to use I2C bus
Enable Clock, configure GPIOs, map registers, setup I2C interrupt
Return values
TRUEsuccess
FALSESee GetLastError(). Possible error codes:
ERROR_INVALID_PARAMETER
ERROR_NO_SYSTEM_RESOURCES
ERROR_INVALID_COMPUTERNAME
BOOL I2CInitEx(DWORD I2CRegAddr) 
Initialization to use I2C bus
Use this function instead of I2CInit() if you need to use another i2c bus than the default bus.
Parameters
[in]I2CRegAddrBase address of the CPU's i2c controller.
Return values
TRUESuccess
FALSEUnspecified Error. See GetLastError().
void I2cIntGetLibVersion(DWORD * pVerMaj,
  DWORD * pVerMin,
  DWORD * pBuild 
 )  
Returns the library Version.
Parameters
[out]pVerMajReturns the major version number. Set this parameter to NULL if not required.
[out]pVerMinReturns the minor version number. Set this parameter to NULL if not required.
[out]pBuildReturns the build number. Set this parameter to NULL if not required.
void I2CSetSpeed(I2CSpeed Speed) 
Set I2C bus speed.
Attention:
If the same i2c bus is used by multiple processes, the speed is preserved for each process individually. This is handled by the GetI2CLock() and ReleaseI2CLock() functions.
However if multiple threads within the same process use the same i2c bus, they all share a global speed setting.
Parameters
[in]SpeedI2C_100KBPS or I2C_400KBPS
BOOL ReleaseI2CLock( ) 
Release I2C bus for other threads (e.g. RTCSync which is loaded by default)
Return values
TRUESuccess
FALSEUnspecified Error. See GetLastError().


Send Feedback!