LPCOpen Platform for LPC112X microcontrollers  112X
LPCOpen Platform for the NXP LPC112X family of Microcontrollers
Data Structures | Functions
CHIP: LPC11xx I2C master-only driver

Detailed Description

This driver only works in master mode. To describe the I2C transactions following symbols are used in driver documentation.

Key to symbols

S (1 bit) : Start bit P (1 bit) : Stop bit Rd/Wr (1 bit) : Read/Write bit. Rd equals 1, Wr equals 0. A, NA (1 bit) : Acknowledge and Not-Acknowledge bit. Addr (7 bits): I2C 7 bit address. Note that this can be expanded as usual to get a 10 bit I2C address. Data (8 bits): A plain data byte. Sometimes, I write DataLow, DataHigh for 16 bit data. [..]: Data sent by I2C device, as opposed to data sent by the host adapter.

Data Structures

struct  I2CM_XFER_T
 Master transfer data structure definitions. More...
 

Functions

void Chip_I2CM_Init (LPC_I2C_T *pI2C)
 Initialize I2C Interface. More...
 
void Chip_I2CM_DeInit (LPC_I2C_T *pI2C)
 Shutdown I2C Interface. More...
 
static INLINE void Chip_I2CM_SetDutyCycle (LPC_I2C_T *pI2C, uint16_t sclH, uint16_t sclL)
 Sets HIGH and LOW duty cycle registers. More...
 
void Chip_I2CM_SetBusSpeed (LPC_I2C_T *pI2C, uint32_t busSpeed)
 Set up bus speed for LPC_I2C controller. More...
 
static INLINE void Chip_I2CM_SendStart (LPC_I2C_T *pI2C)
 Transmit START or Repeat-START signal on I2C bus. More...
 
static INLINE void Chip_I2CM_ResetControl (LPC_I2C_T *pI2C)
 Reset I2C controller state. More...
 
static INLINE void Chip_I2CM_WriteByte (LPC_I2C_T *pI2C, uint8_t data)
 Transmit a single data byte through the I2C peripheral. More...
 
static INLINE uint8_t Chip_I2CM_ReadByte (LPC_I2C_T *pI2C)
 Read a single byte data from the I2C peripheral. More...
 
static INLINE void Chip_I2CM_NackNextByte (LPC_I2C_T *pI2C)
 Generate NACK after receiving next byte. More...
 
static INLINE void Chip_I2CM_SendStop (LPC_I2C_T *pI2C)
 Transmit STOP signal on I2C bus. More...
 
static INLINE void Chip_I2CM_ForceStart (LPC_I2C_T *pI2C)
 Force start I2C transmit. More...
 
static INLINE void Chip_I2CM_SendStartAfterStop (LPC_I2C_T *pI2C)
 Transmit STOP+START signal on I2C bus. More...
 
static INLINE uint32_t Chip_I2CM_StateChanged (LPC_I2C_T *pI2C)
 Check if I2C controller state changed. More...
 
static INLINE void Chip_I2CM_ClearSI (LPC_I2C_T *pI2C)
 Clear state change interrupt flag. More...
 
static INLINE uint32_t Chip_I2CM_BusFree (LPC_I2C_T *pI2C)
 Check if I2C bus is free per our controller. More...
 
static INLINE uint32_t Chip_I2CM_GetCurState (LPC_I2C_T *pI2C)
 Get current state of the I2C controller. More...
 
static INLINE void Chip_I2CM_Disable (LPC_I2C_T *pI2C)
 Disable I2C interface. More...
 
uint32_t Chip_I2CM_XferHandler (LPC_I2C_T *pI2C, I2CM_XFER_T *xfer)
 Transfer state change handler handler. More...
 
void Chip_I2CM_Xfer (LPC_I2C_T *pI2C, I2CM_XFER_T *xfer)
 Transmit and Receive data in master mode. More...
 
uint32_t Chip_I2CM_XferBlocking (LPC_I2C_T *pI2C, I2CM_XFER_T *xfer)
 Transmit and Receive data in master mode. More...
 
uint32_t Chip_I2CM_Write (LPC_I2C_T *pI2C, const uint8_t *buff, uint32_t len)
 Write given buffer of data to I2C interface. More...
 
uint32_t Chip_I2CM_Read (LPC_I2C_T *pI2C, uint8_t *buff, uint32_t len)
 Read data from I2C slave to given buffer. More...
 

Macro Definition Documentation

#define I2CM_STATUS_ARBLOST   0x05

Arbitration lost.

Definition at line 82 of file i2cm_112x.h.

#define I2CM_STATUS_BUS_ERROR   0x03

I2C bus error

Definition at line 80 of file i2cm_112x.h.

#define I2CM_STATUS_BUSY   0xFF

I2C transmistter is busy.

Definition at line 83 of file i2cm_112x.h.

#define I2CM_STATUS_ERROR   0x01

Unknown error condition.

Definition at line 78 of file i2cm_112x.h.

#define I2CM_STATUS_NAK   0x02

No acknowledgement received from slave.

Definition at line 79 of file i2cm_112x.h.

#define I2CM_STATUS_OK   0x00

I2CM_1125_STATUS_TYPES I2C master transfer status typesRequested Request was executed successfully.

Definition at line 77 of file i2cm_112x.h.

#define I2CM_STATUS_SLAVE_NAK   0x04

No device responded for given slave address during SLA+W or SLA+R

Definition at line 81 of file i2cm_112x.h.

#define I2CM_XFER_OPTION_IGNORE_NACK   0x01

I2CM_1125_OPTIONS_TYPES I2C master transfer optionsIgnore NACK during data transfer. By default transfer is aborted.

Definition at line 65 of file i2cm_112x.h.

#define I2CM_XFER_OPTION_LAST_RX_ACK   0x02

ACK last byte received. By default we NACK last byte we receive per I2C spec.

Definition at line 67 of file i2cm_112x.h.

Function Documentation

static INLINE uint32_t Chip_I2CM_BusFree ( LPC_I2C_T pI2C)
static

Check if I2C bus is free per our controller.

Parameters
pI2C: Pointer to selected I2C peripheral
Returns
Returns 0 if busy else a non-zero value.
Note
I2C controller clears STO bit when it sees STOP condition after a START condition on the bus.

Definition at line 289 of file i2cm_112x.h.

static INLINE void Chip_I2CM_ClearSI ( LPC_I2C_T pI2C)
static

Clear state change interrupt flag.

Parameters
pI2C: Pointer to selected I2C peripheral
Returns
Nothing
Note

Definition at line 277 of file i2cm_112x.h.

void Chip_I2CM_DeInit ( LPC_I2C_T pI2C)

Shutdown I2C Interface.

Parameters
pI2C: Pointer to selected I2C peripheral
Returns
Nothing
Note
This function disables the I2C clock.

Definition at line 64 of file i2cm_112x.c.

static INLINE void Chip_I2CM_Disable ( LPC_I2C_T pI2C)
static

Disable I2C interface.

Parameters
pI2C: Pointer to selected I2C peripheral
Returns
Nothing
Note

Definition at line 312 of file i2cm_112x.h.

static INLINE void Chip_I2CM_ForceStart ( LPC_I2C_T pI2C)
static

Force start I2C transmit.

Parameters
pI2C: Pointer to selected I2C peripheral
Returns
Nothing
Note
This function force I2C state machine to start transmitting. If an uncontrolled source generates a superfluous START or masks a STOP condition, then the I2C-bus stays busy indefinitely. If the STA flag is set and bus access is not obtained within a reasonable amount of time, then a forced access to the I2C-bus is possible. This is achieved by setting the STO flag while the STA flag is still set. No STOP condition is transmitted.

Definition at line 237 of file i2cm_112x.h.

static INLINE uint32_t Chip_I2CM_GetCurState ( LPC_I2C_T pI2C)
static

Get current state of the I2C controller.

Parameters
pI2C: Pointer to selected I2C peripheral
Returns
Returns 0 if busy else a non-zero value.
Note
I2C controller clears STO bit when it sees STOP condition after a START condition on the bus.

Definition at line 301 of file i2cm_112x.h.

void Chip_I2CM_Init ( LPC_I2C_T pI2C)

Initialize I2C Interface.

Parameters
pI2C: Pointer to selected I2C peripheral
Returns
Nothing
Note
This function enables the I2C clock.

Definition at line 54 of file i2cm_112x.c.

static INLINE void Chip_I2CM_NackNextByte ( LPC_I2C_T pI2C)
static

Generate NACK after receiving next byte.

Parameters
pI2C: Pointer to selected I2C peripheral
Returns
Nothing
Note
This function sets the controller to NACK after receiving next byte from slave transmitter. Used before receiving last byte.

Definition at line 209 of file i2cm_112x.h.

uint32_t Chip_I2CM_Read ( LPC_I2C_T pI2C,
uint8_t *  buff,
uint32_t  len 
)

Read data from I2C slave to given buffer.

Parameters
pI2C: Pointer to selected I2C peripheral
buff: Pointer to buffer for data received from I2C slave
len: Length of the buffer
Returns
Returns number of bytes read.
Note
This function is a blocking call. The function generates START/repeat-START condition on bus and starts reading data untill requested number of bytes are read. No STOP condition is transmitted on the bus.

S [Data0] A [Data1] A ... [DataN] A

Definition at line 225 of file i2cm_112x.c.

static INLINE uint8_t Chip_I2CM_ReadByte ( LPC_I2C_T pI2C)
static

Read a single byte data from the I2C peripheral.

Parameters
pI2C: Pointer to selected I2C peripheral
Returns
A single byte of data read
Note
This function reads a byte from the I2C receive hold register regardless of I2C state. The I2C status should be read first prior to using this function.

Definition at line 197 of file i2cm_112x.h.

static INLINE void Chip_I2CM_ResetControl ( LPC_I2C_T pI2C)
static

Reset I2C controller state.

Parameters
pI2C: Pointer to selected I2C peripheral
Returns
Nothing
Note
This function clears all control/status flags.

Definition at line 168 of file i2cm_112x.h.

static INLINE void Chip_I2CM_SendStart ( LPC_I2C_T pI2C)
static

Transmit START or Repeat-START signal on I2C bus.

Parameters
pI2C: Pointer to selected I2C peripheral
Returns
Nothing
Note
This function sets the controller to transmit START condition when the bus becomes free.

Definition at line 157 of file i2cm_112x.h.

static INLINE void Chip_I2CM_SendStartAfterStop ( LPC_I2C_T pI2C)
static

Transmit STOP+START signal on I2C bus.

Parameters
pI2C: Pointer to selected I2C peripheral
Returns
Nothing
Note
This function sets the controller to transmit STOP condition followed by a START condition.

Definition at line 255 of file i2cm_112x.h.

static INLINE void Chip_I2CM_SendStop ( LPC_I2C_T pI2C)
static

Transmit STOP signal on I2C bus.

Parameters
pI2C: Pointer to selected I2C peripheral
Returns
Nothing
Note
This function sets the controller to transmit STOP condition.

Definition at line 220 of file i2cm_112x.h.

void Chip_I2CM_SetBusSpeed ( LPC_I2C_T pI2C,
uint32_t  busSpeed 
)

Set up bus speed for LPC_I2C controller.

Parameters
pI2C: Pointer to selected I2C peripheral
busSpeed: I2C bus clock rate
Returns
Nothing
Note
Per I2C specification the busSpeed should be
  • 100000 for Standard mode
  • 400000 for Fast mode
  • 1000000 for Fast mode plus IOCON registers corresponding to I2C pads should be updated according to the bus mode.

Definition at line 71 of file i2cm_112x.c.

static INLINE void Chip_I2CM_SetDutyCycle ( LPC_I2C_T pI2C,
uint16_t  sclH,
uint16_t  sclL 
)
static

Sets HIGH and LOW duty cycle registers.

Parameters
pI2C: Pointer to selected I2C peripheral
sclH: Number of I2C_PCLK cycles for the SCL HIGH time.
sclL: Number of I2C_PCLK cycles for the SCL LOW time.
Returns
Nothing
Note
The frequency is determined by the following formula (I2C_PCLK is the frequency of the peripheral I2C clock):
I2C_bitFrequency = (I2C_PCLK)/(sclH + sclL);

Definition at line 130 of file i2cm_112x.h.

static INLINE uint32_t Chip_I2CM_StateChanged ( LPC_I2C_T pI2C)
static

Check if I2C controller state changed.

Parameters
pI2C: Pointer to selected I2C peripheral
Returns
Returns 0 if state didn't change
Note

Definition at line 266 of file i2cm_112x.h.

uint32_t Chip_I2CM_Write ( LPC_I2C_T pI2C,
const uint8_t *  buff,
uint32_t  len 
)

Write given buffer of data to I2C interface.

Parameters
pI2C: Pointer to selected I2C peripheral
buff: Pointer to buffer to be transmitted
len: Length of the buffer
Returns
Returns number of bytes written.
Note
This function is a blocking call. The function generates START/repeat-START condition on bus and starts transmitting data untill transfer finishes or a NACK is received. No STOP condition is transmitted on the bus.

S Data0 [A] Data1 [A] ... DataN [A]

Definition at line 188 of file i2cm_112x.c.

static INLINE void Chip_I2CM_WriteByte ( LPC_I2C_T pI2C,
uint8_t  data 
)
static

Transmit a single data byte through the I2C peripheral.

Parameters
pI2C: Pointer to selected I2C peripheral
data: Byte to transmit
Returns
Nothing
Note
This function attempts to place a byte into the UART transmit FIFO or transmit hold register regard regardless of UART state

Definition at line 184 of file i2cm_112x.h.

void Chip_I2CM_Xfer ( LPC_I2C_T pI2C,
I2CM_XFER_T xfer 
)

Transmit and Receive data in master mode.

Parameters
pI2C: Pointer to selected I2C peripheral
xfer: Pointer to a I2CM_XFER_T structure see notes below
Returns
Nothing.
Note
The parameter xfer should have its member slaveAddr initialized to the 7-Bit slave address to which the master will do the xfer, Bit0 to bit6 should have the address and Bit8 is ignored. During the transfer no code (like event handler) must change the content of the memory pointed to by xfer. The member of xfer, txBuff and txSz be initialized to the memory from which the I2C must pick the data to be transfered to slave and the number of bytes to send respectively, similarly rxBuff and rxSz must have pointer to memroy where data received from slave be stored and the number of data to get from slave respectilvely. Following types of transfers are possible:
  • Write-only transfer: When rxSz member of xfer is set to 0.

       S Addr Wr [A] txBuff0 [A] txBuff1 [A] ... txBuffN [A] P
    
    • If I2CM_XFER_OPTION_IGNORE_NACK is set in options memeber

      S Addr Wr [A] txBuff0 [A or NA] ... txBuffN [A or NA] P

  • Read-only transfer: When txSz member of xfer is set to 0.

       S Addr Rd [A] [rxBuff0] A [rxBuff1] A ... [rxBuffN] NA P
    
    • If I2CM_XFER_OPTION_LAST_RX_ACK is set in options memeber

      S Addr Rd [A] [rxBuff0] A [rxBuff1] A ... [rxBuffN] A P

  • Read-Write transfer: When rxSz and @ txSz members of xfer are non-zero.
       S Addr Wr [A] txBuff0 [A] txBuff1 [A] ... txBuffN [A]
           S Addr Rd [A] [rxBuff0] A [rxBuff1] A ... [rxBuffN] NA P

Definition at line 161 of file i2cm_112x.c.

uint32_t Chip_I2CM_XferBlocking ( LPC_I2C_T pI2C,
I2CM_XFER_T xfer 
)

Transmit and Receive data in master mode.

Parameters
pI2C: Pointer to selected I2C peripheral
xfer: Pointer to a I2CM_XFER_T structure see notes below
Returns
Returns non-zero value on succesful completion of transfer.
Note
This function operates same as Chip_I2CM_Xfer(), but is a blocking call.

Definition at line 172 of file i2cm_112x.c.

uint32_t Chip_I2CM_XferHandler ( LPC_I2C_T pI2C,
I2CM_XFER_T xfer 
)

Transfer state change handler handler.

Parameters
pI2C: Pointer to selected I2C peripheral
xfer: Pointer to a I2CM_XFER_T structure see notes below
Returns
Returns non-zero value on completion of transfer. The status member of xfer structure contains the current status of the transfer at the end of the call.
Note
The parameter xfer should be same as the one passed to Chip_I2CM_Xfer() routine.

Definition at line 79 of file i2cm_112x.c.