I2C module provides an interface to any I2C bus compatible device accessible via I2C serial bus. External components attached to I2C bus can serially transmit/receive data to/from the CPU through two wire interface. I2C driver provides API to perform transmit/receive to any of the I2C peripherals on the board, with the multiple modes of operation.
Features Supported
- Controller and Target mode of operation
- Interrupt, Polled Mode
- Blocking and Non-blocking (callback) transfers
- Queueing of I2C transactions
- I2C Bus Recovery
SysConfig Features
- Note
- It is strongly recommend to use SysConfig where it is available instead of using direct SW API calls. This will help simplify the SW application and also catch common mistakes early in the development cycle.
SysConfig can be used to configure below parameters apart from common configuration like Clock,MPU and others.
- I2C module configuration parmaters like bitrate, target addresses to probe.
- I2C instances and pin configurations.
- Interrupt mode enable option.If you disable it, configures to polling mode.
- Based on above parameters, the SysConfig generated code does below as part of Drivers_open and Drivers_close functions
- Set I2C instance parameter configuration.
- Driver ISR registration if Interrupt Mode is enabled.
Usage
Each I2C device on the bus is recognized by a unique address and can operate as either a transmitter or a receiver, depending on the function of the device. In addition to being a transmitter or receiver, a device connected to the I2C bus can also be considered a Controller or a Target when performing data transfers. A Controller initiates the data transfer on a bus and generates the clock signal that permits the transfer. During the transmission, any device addressed by the Controller is considered the Target.
Target Mode
- In the Target Receiver Mode, I2C module is a Target and receives data from a Controller. All Target devices begin in this mode. Serial data bits received on the SDA pin are shifted in with the clock pulses that are generated by the Controller device. The Target device does not generate the clock, but it can hold the SCL pin low while intervention of the device is required (RSFULL = 1) after a byte has been received.
- In the Target Transmitter Mode, I2C module can only be entered from the Target receiver mode (The I2C module must first receive a command from the Controller). This mode is entered only if the Target address byte is the same as its own address and the R/ W bit has been transmitted.
Features NOT Supported
- Target mode is not supported in polling mode.
Example Usage
Controller Mode
Include the below file to access the APIs
Instance Open Example
gI2cHandle =
I2C_open(CONFIG_I2C0, ¶ms);
if (!gI2cHandle) {
}
Instance Close Example
I2c Transfer Example
Target Mode
I2C Target Mode Example
int32_t status;
/* Initialize new transaction struct; controller mode enabled by default */
I2C_Transaction i2cTransaction;
I2C_Transaction_init(&i2cTransaction);
/* Configure write buffer */
uint8_t txBuffer[1];
txBuffer[0] = 0x6A;
i2cTransaction.writeBuf = txBuffer;
i2cTransaction.writeCount = 1;
/* Configure read buffer */
uint8_t rxBuffer[1];
i2cTransaction.readBuf = rxBuffer;
i2cTransaction.readCount = 1;
/* Configure device to target mode */
i2cTransaction.controllerMode = 0;
/* Blocking Mode: Halt program until message received from controller
Callback Mode: Trigger interrupt callback upon message received from controller */
status = I2C_transfer(i2cHandle, &i2cTransaction);
if(status != SystemP_SUCCESS)
{
DebugP_assert(FALSE);
}
API
APIs for I2C