In order to use the I2C driver we need to include the apropiate header file. For example in the case of LPC1114 this is i2c_11xx.h.
Once that is completed, we need to set the GPIO pins to alternate I2C function, then reset the I2C peripheral, initialize it and set the clock speed:
In case of LPC1114 the I2C is connected to P0.4 and P0.5:
Chip_IOCON_PinMuxSet(LPC_IOCON, IOCON_PIO0_4, IOCON_FUNC1 | IOCON_DIGMODE_EN ); Chip_IOCON_PinMuxSet(LPC_IOCON, IOCON_PIO0_5, IOCON_FUNC1 | IOCON_DIGMODE_EN ); Chip_SYSCTL_PeriphReset(RESET_I2C0); Chip_I2C_Init(I2C0); Chip_I2C_SetClockRate(I2C0, SPEED_400KHZ);
And finally we need to provide a state handling function by defining I2C_IRQHandler() function, set the master event handler and enable I2C interrupts:
void I2C_IRQHandler(void) { if (Chip_I2C_IsMasterActive(I2C0)) { Chip_I2C_MasterStateHandler(I2C0); } else { Chip_I2C_SlaveStateHandler(I2C0); } } Chip_I2C_SetMasterEventHandler(I2C0, Chip_I2C_EventHandler); NVIC_ClearPendingIRQ(I2C0_IRQn); NVIC_EnableIRQ(I2C0_IRQn);
For sending and receiving data, first we define some buffers, to work with and a I2C transfer structure:
static I2C_XFER_T xfer; /* Transfer and Receive buffers */ static uint8_t tx[10], rx[10];
When sending data, we fill the structure variable with appropiate values by setting slave address, transfer buffer and transmit transfer size then call Chip_I2C_MasterSend():
xfer.slaveAddr = slave-addr; tx[0] = byte1; tx[1] = byte2; xfer.txBuff = &tx[0]; xfer.txSz = 2; Chip_I2C_MasterSend(I2C0, xfer.slaveAddr, xfer.txBuff, xfer.txSz); // i2c bytes sent
On receiving data, we do mostly the same thing, except we fill the receive buffer instead of transmit one, and the receive transfer size, and call Chip_I2C_MasterRead():
// then read it's value xfer.slaveAddr = slave-addr; xfer.rxBuff = &rx[0]; xfer.rxSz = 8; // we read 8 bytes Chip_I2C_MasterRead(I2C0, xfer.slaveAddr, xfer.rxBuff, xfer.rxSz);
These are compiled from the provided sample file, which seems a bit too complicated for quickly figuring out how one should use this driver.