LPCOpen Platform for LPC18XX/43XX microcontrollers: C:/w/lpcopen_docs/software/lpc_core/lpc_chip/chip_18xx_43xx/i2c_18xx_43xx.c File Reference

| LPCOpen Platform for LPC18XX/43XX microcontrollers 18XX43XX LPCOpen Platform for the NXP LPC18XX/43XX family of Microcontrollers

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

Data Structures | Macros | Functions | Variables
i2c_18xx_43xx.c File Reference
`#include "chip.h"`

Go to the source code of this file.

## Data Structures
struct | i2c_interface
struct | i2c_slave_interface

## Macros
#define | I2C_CON_FLAGS (I2C_CON_AA | I2C_CON_SI | I2C_CON_STO | I2C_CON_STA)
#define | LPC_I2Cx(id) ((i2c[id].ip))
#define | SLAVE_ACTIVE(iic) (((iic)->flags & 0xFF00) != 0)

## Functions
STATIC INLINE void | enableClk (I2C_ID_T id)
STATIC INLINE void | disableClk (I2C_ID_T id)
STATIC INLINE uint32_t | getClkRate (I2C_ID_T id)
STATIC INLINE void | startMasterXfer (LPC_I2C_T *pI2C)
STATIC INLINE void | startSlaverXfer (LPC_I2C_T *pI2C)
STATIC INLINE int | isI2CBusFree (LPC_I2C_T *pI2C)
STATIC INLINE int | getCurState (LPC_I2C_T *pI2C)
STATIC INLINE int | isMasterState (LPC_I2C_T *pI2C)
STATIC void | setSlaveAddr (LPC_I2C_T *pI2C, I2C_SLAVE_ID sid, uint8_t addr, uint8_t mask)
STATIC int | isSlaveAddrMatching (uint8_t addr1, uint8_t addr2, uint8_t mask)
STATIC I2C_SLAVE_ID | lookupSlaveIndex (LPC_I2C_T *pI2C, uint8_t slaveAddr)
int | handleMasterXferState (LPC_I2C_T *pI2C, I2C_XFER_T *xfer)
I2C_SLAVE_ID | getSlaveIndex (LPC_I2C_T *pI2C)
int | handleSlaveXferState (LPC_I2C_T *pI2C, I2C_XFER_T *xfer)
void | Chip_I2C_EventHandler (I2C_ID_T id, I2C_EVENT_T event)
| Default event handler for interrupt base operation. More...
void | Chip_I2C_EventHandlerPolling (I2C_ID_T id, I2C_EVENT_T event)
| Default event handler for polling operation. More...
void | Chip_I2C_Init (I2C_ID_T id)
| Initializes the LPC_I2C peripheral with specified parameter. More...
void | Chip_I2C_DeInit (I2C_ID_T id)
| De-initializes the I2C peripheral registers to their default reset values. More...
void | Chip_I2C_SetClockRate (I2C_ID_T id, uint32_t clockrate)
| Set up clock rate for LPC_I2C peripheral. More...
uint32_t | Chip_I2C_GetClockRate (I2C_ID_T id)
| Get current clock rate for LPC_I2C peripheral. More...
int | Chip_I2C_SetMasterEventHandler (I2C_ID_T id, I2C_EVENTHANDLER_T event)
| Set function that must handle I2C events. More...
I2C_EVENTHANDLER_T | Chip_I2C_GetMasterEventHandler (I2C_ID_T id)
| Get pointer to current function handling the events. More...
int | Chip_I2C_MasterTransfer (I2C_ID_T id, I2C_XFER_T *xfer)
| Transmit and Receive data in master mode. More...
int | Chip_I2C_MasterSend (I2C_ID_T id, uint8_t slaveAddr, const uint8_t *buff, uint8_t len)
| Transmit data to I2C slave using I2C Master mode. More...
int | Chip_I2C_MasterCmdRead (I2C_ID_T id, uint8_t slaveAddr, uint8_t cmd, uint8_t *buff, int len)
| Transfer a command to slave and receive data from slave after a repeated start. More...
int | Chip_I2C_MasterRead (I2C_ID_T id, uint8_t slaveAddr, uint8_t *buff, int len)
| Set function that must handle I2C events. More...
int | Chip_I2C_IsMasterActive (I2C_ID_T id)
| Checks if master xfer in progress. More...
void | Chip_I2C_MasterStateHandler (I2C_ID_T id)
| I2C Master transfer state change handler. More...
void | Chip_I2C_SlaveSetup (I2C_ID_T id, I2C_SLAVE_ID sid, I2C_XFER_T *xfer, I2C_EVENTHANDLER_T event, uint8_t addrMask)
| Setup a slave I2C device. More...
void | Chip_I2C_SlaveStateHandler (I2C_ID_T id)
| I2C Slave event handler. More...
void | Chip_I2C_Disable (I2C_ID_T id)
| Disable I2C peripheral's operation. More...
int | Chip_I2C_IsStateChanged (I2C_ID_T id)
| I2C peripheral state change checking. More...

## Variables
static struct i2c_interface | i2c [I2C_NUM_INTERFACE]
static struct i2c_slave_interface | i2c_slave [I2C_NUM_INTERFACE][I2C_SLAVE_NUM_INTERFACE]

## Macro Definition Documentation

#define I2C_CON_FLAGS (I2C_CON_AA | I2C_CON_SI | I2C_CON_STO | I2C_CON_STA)

Definition at line 39 of file i2c_18xx_43xx.c.

#define LPC_I2Cx | ( |  | id | ) | ((i2c[id].ip))

Definition at line 40 of file i2c_18xx_43xx.c.

#define SLAVE_ACTIVE | ( |  | iic | ) | (((iic)->flags & 0xFF00) != 0)

Definition at line 41 of file i2c_18xx_43xx.c.

## Function Documentation

void Chip_I2C_DeInit | ( | I2C_ID_T | id | ) |

De-initializes the I2C peripheral registers to their default reset values.

Parameters

  id | : I2C peripheral ID (I2C0, I2C1 ... etc)

Returns
  Nothing

Definition at line 375 of file i2c_18xx_43xx.c.

void Chip_I2C_Disable | ( | I2C_ID_T | id | ) |

Disable I2C peripheral's operation.

Parameters

  id | : I2C peripheral ID (I2C0, I2C1 ... etc)

Returns
  Nothing

Definition at line 544 of file i2c_18xx_43xx.c.

void Chip_I2C_EventHandler | ( | I2C_ID_T | id,
|  | I2C_EVENT_T | event
| ) |  |

Default event handler for interrupt base operation.

Parameters

  id | : I2C peripheral ID (I2C0, I2C1 ... etc)
  event | : Event ID of the event that called the function

Returns
  Nothing

Definition at line 330 of file i2c_18xx_43xx.c.

void Chip_I2C_EventHandlerPolling | ( | I2C_ID_T | id,
|  | I2C_EVENT_T | event
| ) |  |

Default event handler for polling operation.

Parameters

  id | : I2C peripheral ID (I2C0, I2C1 ... etc)
  event | : Event ID of the event that called the function

Returns
  Nothing

Definition at line 346 of file i2c_18xx_43xx.c.

uint32_t Chip_I2C_GetClockRate | ( | I2C_ID_T | id | ) |

Get current clock rate for LPC_I2C peripheral.

Parameters

  id | : I2C peripheral ID (I2C0, I2C1 ... etc)

Returns
  The current I2C peripheral clock rate

Definition at line 394 of file i2c_18xx_43xx.c.

I2C_EVENTHANDLER_T Chip_I2C_GetMasterEventHandler | ( | I2C_ID_T | id | ) |

Get pointer to current function handling the events.

Parameters

  id | : I2C peripheral ID (I2C0, I2C1 ... etc)

Returns
  Pointer to function handing events of I2C

Definition at line 410 of file i2c_18xx_43xx.c.

void Chip_I2C_Init | ( | I2C_ID_T | id | ) |

Initializes the LPC_I2C peripheral with specified parameter.

Parameters

  id | : I2C peripheral ID (I2C0, I2C1 ... etc)

Returns
  Nothing

Definition at line 366 of file i2c_18xx_43xx.c.

int Chip_I2C_IsMasterActive | ( | I2C_ID_T | id | ) |

Checks if master xfer in progress.

Parameters

  id | : I2C peripheral ID (I2C0, I2C1 ... etc)

Returns
  1 if master xfer in progress 0 otherwise

Note
  This API is generally used in interrupt handler of the application to decide whether to call master state handler or to call slave state handler

Definition at line 481 of file i2c_18xx_43xx.c.

int Chip_I2C_IsStateChanged | ( | I2C_ID_T | id | ) |

I2C peripheral state change checking.

Parameters

  id | : I2C peripheral ID (I2C0, I2C1 ... etc)

Returns
  1 if I2C peripheral id has changed its state, 0 if there is no state change

Note
  This function must be used by the application when the polling has to be done based on state change.

Definition at line 550 of file i2c_18xx_43xx.c.

int Chip_I2C_MasterCmdRead | ( | I2C_ID_T | id,
|  | uint8_t | slaveAddr,
|  | uint8_t | cmd,
|  | uint8_t * | buff,
|  | int | len
| ) |  |

Transfer a command to slave and receive data from slave after a repeated start.

Parameters

  id | : I2C peripheral ID (I2C0, I2C1 ... etc)
  slaveAddr | : Slave address of the I2C device
  cmd | : Command (Address/Register) to be written
  buff | : Pointer to memory that will hold the data received
  len | : Number of bytes to receive

Returns
  Number of bytes successfully received

Definition at line 457 of file i2c_18xx_43xx.c.

int Chip_I2C_MasterRead | ( | I2C_ID_T | id,
|  | uint8_t | slaveAddr,
|  | uint8_t * | buff,
|  | int | len
| ) |  |

Set function that must handle I2C events.

Parameters

  id | : I2C peripheral ID (I2C0, I2C1 ... etc)
  slaveAddr | : Slave address from which data be read
  buff | : Pointer to memory where data read be stored
  len | : Number of bytes to read from slave

Returns
  Number of bytes read successfully

Definition at line 470 of file i2c_18xx_43xx.c.

int Chip_I2C_MasterSend | ( | I2C_ID_T | id,
|  | uint8_t | slaveAddr,
|  | const uint8_t * | buff,
|  | uint8_t | len
| ) |  |

Transmit data to I2C slave using I2C Master mode.

Parameters

  id | : I2C peripheral ID (I2C0, I2C1 .. etc)
  slaveAddr | : Slave address to which the data be written
  buff | : Pointer to buffer having the array of data
  len | : Number of bytes to be transfered from buff

Returns
  Number of bytes successfully transfered

Definition at line 444 of file i2c_18xx_43xx.c.

void Chip_I2C_MasterStateHandler | ( | I2C_ID_T | id | ) |

I2C Master transfer state change handler.

Parameters

  id | : I2C peripheral ID (I2C0, I2C1 ... etc)

Returns
  Nothing

Note
  Usually called from the appropriate Interrupt handler

Definition at line 487 of file i2c_18xx_43xx.c.

int Chip_I2C_MasterTransfer | ( | I2C_ID_T | id,
|  | I2C_XFER_T * | xfer
| ) |  |

Transmit and Receive data in master mode.

Parameters

  id | : I2C peripheral selected (I2C0, I2C1 etc)
  xfer | : Pointer to a I2C_XFER_T structure see notes below

Returns
  Any of I2C_STATUS_T values, xfer->txSz will have number of bytes not sent due to error, xfer->rxSz will have the number of bytes yet to be received.

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.

Definition at line 416 of file i2c_18xx_43xx.c.

void Chip_I2C_SetClockRate | ( | I2C_ID_T | id,
|  | uint32_t | clockrate
| ) |  |

Set up clock rate for LPC_I2C peripheral.

Parameters

  id | : I2C peripheral ID (I2C0, I2C1 ... etc)
  clockrate | : Target clock rate value to initialized I2C peripheral (Hz)

Returns
  Nothing

Note
  Parameter clockrate for I2C0 should be from 1000 up to 1000000 (1 KHz to 1 MHz), as I2C0 support Fast Mode Plus. If the clockrate is more than 400 KHz (Fast Plus Mode) Board_I2C_EnableFastPlus() must be called prior to calling this function.

Definition at line 384 of file i2c_18xx_43xx.c.

int Chip_I2C_SetMasterEventHandler | ( | I2C_ID_T | id,
|  | I2C_EVENTHANDLER_T | event
| ) |  |

Set function that must handle I2C events.

Parameters

  id | : I2C peripheral ID (I2C0, I2C1 ... etc)
  event | : Pointer to function that will handle the event (Should not be NULL)

Returns
  1 when successful, 0 when a transfer is on going with its own event handler

Definition at line 400 of file i2c_18xx_43xx.c.

void Chip_I2C_SlaveSetup | ( | I2C_ID_T | id,
|  | I2C_SLAVE_ID | sid,
|  | I2C_XFER_T * | xfer,
|  | I2C_EVENTHANDLER_T | event,
|  | uint8_t | addrMask
| ) |  |

Setup a slave I2C device.

Parameters

  id | : I2C peripheral ID (I2C0, I2C1 ... etc)
  sid | : I2C Slave peripheral ID (I2C_SLAVE_0, I2C_SLAVE_1 etc)
  xfer | : Pointer to transfer structure (see note below for more info)
  event | : Event handler for slave transfers
  addrMask | : Address mask to use along with slave address (see notes below for more info)

Returns
  Nothing

Note
  Parameter xfer should point to a valid I2C_XFER_T structure object and must have slaveAddr initialized with 7bit Slave address (From Bit1 to Bit7), Bit0 when set enables general call handling, slaveAddr along with addrMask will be used to match the slave address. rxBuff and txBuff must point to valid buffers where slave can receive or send the data from, size of which will be provided by rxSz and txSz respectively. Function pointed to by event will be called for the following events I2C_EVENT_SLAVE_RX (One byte of data received successfully from the master and stored inside memory pointed by xfer->rxBuff, incremented the pointer and decremented the xfer->rxSz), I2C_EVENT_SLAVE_TX (One byte of data from xfer->txBuff was sent to master successfully, incremented the pointer and decremented xfer->txSz), I2C_EVENT_DONE (Master is done doing its transfers with the slave).
  Bit-0 of the parameter addrMask is reserved and should always be 0. Any bit (BIT1 to BIT7) set in addrMask will make the corresponding bit in xfer->slaveAddr as don't care. Thit is, if xfer->slaveAddr is (0x10 << 1) and addrMask is (0x03 << 1) then 0x10, 0x11, 0x12, 0x13 will all be considered as valid slave addresses for the registered slave. Upon receving any event xfer->slaveAddr (BIT1 to BIT7) will hold the actual address which was received from master.
  General Call Handling
  Slave can receive data from master using general call address (0x00). General call handling must be setup as given below
  - Call Chip_I2C_SlaveSetup() with argument sid as I2C_SLAVE_GENERAL
    - xfer->slaveAddr ignored, argument addrMask ignored
    - function provided by event will registered to be called when slave received data using addr 0x00
    - xfer->rxBuff and xfer->rxSz should be valid in argument xfer
  - To handle General Call only (No other slaves are configured)
    - Call Chip_I2C_SlaveSetup() with sid as I2C_SLAVE_X (X=0,1,2,3)
    - setup xfer with slaveAddr member set to 0, event is ignored hence can be NULL
    - provide addrMask (typically 0, if not you better be knowing what you are doing)
  - To handler General Call when other slave is active
    - Call Chip_I2C_SlaveSetup() with sid as I2C_SLAVE_X (X=0,1,2,3)
    - setup xfer with slaveAddr member set to 7-Bit Slave address [from Bit1 to 7]
    - Set Bit0 of xfer->slaveAddr as 1
    - Provide appropriate addrMask
    - Argument event must point to function, that handles events from actual slaveAddress and not the GC

Warning
  If the slave has only one byte in its txBuff, once that byte is transfered to master the event handler will be called for event I2C_EVENT_DONE. If the master attempts to read more bytes in the same transfer then the slave hardware will send 0xFF to master till the end of transfer, event handler will not be called to notify this. For more info see section below
  Last data handling in slave
  If the user wants to implement a slave which will read a byte from a specific location over and over again whenever master reads the slave. If the user initializes the xfer->txBuff as the location to read the byte from and xfer->txSz as 1, then say, if master reads one byte; slave will send the byte read from xfer->txBuff and will call the event handler with I2C_EVENT_DONE. If the master attempts to read another byte instead of sending the byte read from xfer->txBuff the slave hardware will send 0xFF and no event will occur. To handle this issue, slave should set xfer->txSz to 2, in which case when master reads the byte event handler will be called with I2C_EVENT_SLAVE_TX, in which the slave implementation can reset the buffer and size back to original location (i.e, xfer->txBuff–, xfer->txSz++), if the master reads another byte in the same transfer, byte read from xfer->txBuff will be sent and I2C_EVENT_SLAVE_TX will be called again, and the process repeats.

Definition at line 495 of file i2c_18xx_43xx.c.

void Chip_I2C_SlaveStateHandler | ( | I2C_ID_T | id | ) |

I2C Slave event handler.

Parameters

  id | : I2C peripheral ID (I2C0, I2C1 ... etc)

Returns
  Nothing

Definition at line 518 of file i2c_18xx_43xx.c.

STATIC INLINE void disableClk | ( | I2C_ID_T | id | ) |

Definition at line 81 of file i2c_18xx_43xx.c.

STATIC INLINE void enableClk | ( | I2C_ID_T | id | ) |

Definition at line 76 of file i2c_18xx_43xx.c.

STATIC INLINE uint32_t getClkRate | ( | I2C_ID_T | id | ) |

Definition at line 87 of file i2c_18xx_43xx.c.

STATIC INLINE int getCurState | ( | LPC_I2C_T * | pI2C | ) |

Definition at line 119 of file i2c_18xx_43xx.c.

I2C_SLAVE_ID getSlaveIndex | ( | LPC_I2C_T * | pI2C | ) |

Definition at line 248 of file i2c_18xx_43xx.c.

int handleMasterXferState | ( | LPC_I2C_T * | pI2C,
|  | I2C_XFER_T * | xfer
| ) |  |

Definition at line 175 of file i2c_18xx_43xx.c.

int handleSlaveXferState | ( | LPC_I2C_T * | pI2C,
|  | I2C_XFER_T * | xfer
| ) |  |

Definition at line 265 of file i2c_18xx_43xx.c.

STATIC INLINE int isI2CBusFree | ( | LPC_I2C_T * | pI2C | ) |

Definition at line 113 of file i2c_18xx_43xx.c.

STATIC INLINE int isMasterState | ( | LPC_I2C_T * | pI2C | ) |

Definition at line 125 of file i2c_18xx_43xx.c.

STATIC int isSlaveAddrMatching | ( | uint8_t | addr1,
|  | uint8_t | addr2,
|  | uint8_t | mask
| ) |  |

Definition at line 145 of file i2c_18xx_43xx.c.

STATIC I2C_SLAVE_ID lookupSlaveIndex | ( | LPC_I2C_T * | pI2C,
|  | uint8_t | slaveAddr
| ) |  |

Definition at line 152 of file i2c_18xx_43xx.c.

STATIC void setSlaveAddr | ( | LPC_I2C_T * | pI2C,
|  | I2C_SLAVE_ID | sid,
|  | uint8_t | addr,
|  | uint8_t | mask
| ) |  |

Definition at line 131 of file i2c_18xx_43xx.c.

STATIC INLINE void startMasterXfer | ( | LPC_I2C_T * | pI2C | ) |

Definition at line 93 of file i2c_18xx_43xx.c.

STATIC INLINE void startSlaverXfer | ( | LPC_I2C_T * | pI2C | ) |

Definition at line 103 of file i2c_18xx_43xx.c.

## Variable Documentation

struct i2c_interface i2c[I2C_NUM_INTERFACE]
| static

Initial value:
= {
{LPC_I2C0, CLK_APB1_I2C0, Chip_I2C_EventHandler, NULL, NULL, NULL, 0},
{LPC_I2C1, CLK_APB3_I2C1, Chip_I2C_EventHandler, NULL, NULL, NULL, 0}
}

Definition at line 61 of file i2c_18xx_43xx.c.

struct i2c_slave_interface i2c_slave[I2C_NUM_INTERFACE][I2C_SLAVE_NUM_INTERFACE]
| static

Definition at line 66 of file i2c_18xx_43xx.c.