iLLD_TC27xC  1.0
How to use the I2c driver?
Collaboration diagram for How to use the I2c driver?:

The I2c driver provides a default configuration for 8bit wide data transfers in Master mode.

NOTE: Interrupts are disabled during data transfers as long as the driver operates on the I2C hardware FIFO, except for reading 32 bytes or less. This is due to imperfections of the I2c Module.

In the following sections it will be described, how to integrate the driver into the application framework.

Preparation Preparation

Include Files

Include following header file into your C code:

Variables

Declare the I2c handle, the I2c device handle and the data buffer in your C code:

// used globally
static IfxI2c_I2c i2c; // i2c handle
static IfxI2c_I2c_Device i2cDev; // slave device handle
uint8 data[128]; // data buffer

Module Initialisation

// create config structure
// fill structure with default values and Module address
IfxI2c_I2c_initConfig(&coinfig, &MODULE_I2C0);
// configure pins
const IfxI2c_Pins pins = {
};
config.pins = &pins;
config.baudrate = 400000; // 400 kHz
// initialize module
IfxI2c_I2c_initModule(&i2c, &config);

Device Initialisation

Here the i2c device handle is initialized.

// create device config
IfxI2c_I2c_deviceConfig i2cDeviceConfig;
// fill structure with default values and i2c Handler
IfxI2c_I2c_initDeviceConfig(&i2cDeviceConfig, &i2c);
// set device specifig values
i2cDeviceConfig.deviceAdddress = 0xa0;
// initialize the i2c device handle
IfxI2c_I2c_initDevice(&i2cDeviceConfig, &i2cDev);

Data Transfers

Example for an i2c EEPROM.

Write

uint16 addr = 0x0000;
// setup the device's internal address
data[0] = addr >> 8; // High byte
data[1] = (uint8)addr; // Low byte
// setup data to be written
data[2] = 0x01;
data[3] = 0x02;
data[4] = 0x03;
uint8 size = 5; // 5 bytes to transmit to i2cDev (data and internal address)
// write data to device as soon as it is ready
while(IfxI2c_I2c_write(&i2cDev, data, size) == IfxI2c_I2c_Status_nak);

Read

Ifx_SizeT size;
uint16 addr = 0x0000;
// setup internal address to be read from
data[0] = addr >> 8; // High byte
data[1] = (uint8)addr; // Low byte
size = 2;
while(IfxI2c_I2c_write(&i2cDev, data, size) == IfxI2c_I2c_Status_nak);
size = 8; // 8 bytes to read
// read device data to data array
while(IfxI2c_I2c_read(&i2cDev, data, size) == IfxI2c_I2c_Status_nak);

Acknowledge Polling

It is also possible to poll explicitly for NAK.

By using write operations:

// size = 0;
while(IfxI2c_I2c_write(&i2cDev, data, 0) == IfxI2c_I2c_Status_nak)); // where data is just a dummy pointer

By using read operations:

// size = 0;
while(IfxI2c_I2c_read(&i2cDev, data, 0) == IfxI2c_I2c_Status_nak)); // where data is just a dummy pointer