LPCOpen Platform  v1.03
LPCOpen Platform for NXP LPC Microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
LPC18xx/43xx I2C example

Detailed Description

Example description
The I2C example shows how to use I2C interface in master and slave mode using POLLING/INTERRUPT method.

To use the example, connect a serial cable to the board's RS232/UART port and start a terminal program to monitor the port. The terminal program on the host PC should be setup for 115K8N1.

For boards that does not have default UART, the user can enable debugging and connect FTDI UART cable to the board and use the example or see the section below for using the example without uart, others can skip the following paragraph.

For boards like NGX Xplorer where there exist not default UART, do the connections as specified in the "Special connection requirements" section. Press the button in one board and see the LED in the other board glow (Button press is sensed by the board and it will act as the master and will send data to the slave board).

After a Main menu is displayed via UART, I2C0 and I2C1 are configured with 100KHz speed.

User can access any I2C slave device (probe all devices by selecting option 3 from main menu) connected to the bus using this example, i.e, operations like WRITE/READ/WRITE&READ can be carried out by using any I2C I2C0/I2C1 as master. The input from UART could be decimal, hexadecimal (prefixed with 0x), or octal (prefixed with 0) and can be edited using backspace. User must press enter key to complete the input.

This example simulates two slave devices (default by I2C0), an EEPROM (0x5A) and an IO Extension device (0x5B). The EEPROM simulation is just to show how the I2C can be used as a device (it will not retain contents across power cycles). IO extention device have 2 ports, any single byte read will read 8 bit data from IN port (8 bit input reg0), any single byte write will write to OUT port (8 bit output reg1). Multibyte reads will read (IN REG, OUT REG) repeatedly, multi byte writes will write to OUT REG repeatedly. OUT REG represent LEDS in the board set/reset them will turn LED0-LED7 (If present) to ON/OFF, IN REG represent key press (KEY0 to KEY7).

To turn on LED1 and LED2 using IOX Slave, follow the steps given below

  1. Select I2C1 as default device by selecting option 1 and 1
  2. Select option 5 to write data to slave
  3. Enter 0x5B as the slave device address
  4. Enter 1 as the number of bytes to write
  5. Enter 6 as the data [Turn ON LED1 and LED2]

The EEPROM slave (simulation) device will work as any standard I2C EEPROM. To write data 0xDE, 0xAD, 0xBE, 0xEF to address 0x10, 0x11, 0x12 & 0x13 respectively and to read back the full content of the EEPROM follow the steps

  1. Select I2C1 as default device by choosing option 1 and give input 1
  2. Select option 5 to write data to slave
  3. Enter 0x5A as the slave device address
  4. Enter 5 as the number of bytes to write
  5. Enter 0x10 as the address to write

    First byte written to device is always considered as the address from/to which read/write to be performed, the EEPROM uses 64 byte memory hence BIT0 to BIT5 will have address A0-A5 and BIT6 & BIT7 are don't cares. For every read/write the address will auto increment upon reaching the end of the device the address will automatically roll back to location 0.

  6. Enter data 0xDE,0xAD,0xBE,0xEF one by one
  7. Select option 6 to write/read the data from EEPROM
  8. Enter 0x5A as the slave address
  9. Enter 64 as the number of bytes to read
  10. Enter 1 as the number of bytes to write
  11. Enter 0 as the address from which read must start
  12. Full content of EEPROM will be displayed on UART terminal with address 0x10 to 0x13 showing the data written in previous operation.

Special connection requirements

Build procedures:
LPC18xx/43xx example quickstart for Keil uVision4
LPC18xx/43xx example quickstart for IAR EWARM
LPC18xx/43xx example quickstart for Code Red Xpresso

Supported boards and board setup:
Hitex 1850 evaluation boards
Hitex 4350 evaluation boards
Keil 1857 evaluation boards
Keil 4357 evaluation boards
NGX 1830 Xplorer boards
NGX 4330 Xplorer boards

Submitting LPCOpen issues:
Community support for LPCOpen

Macros

#define DEFAULT_I2C   I2C1
 
#define I2C_EEPROM_BUS   DEFAULT_I2C
 
#define I2C_IOX_BUS   DEFAULT_I2C
 
#define SPEED_100KHZ   100000
 
#define SPEED_400KHZ   400000
 
#define I2C_SLAVE_EEPROM_SIZE   64
 
#define I2C_SLAVE_EEPROM_ADDR   0x5A
 
#define I2C_SLAVE_IOX_ADDR   0x5B
 

Functions

static void i2c_state_handling (I2C_ID_T id)
 
static void con_print_data (const uint8_t *dat, int sz)
 
static int con_get_input (const char *str)
 
static void i2c_rw_input (I2C_XFER_T *xfer, int ops)
 
static void i2c_set_mode (I2C_ID_T id, int polling)
 
static void i2c_app_init (I2C_ID_T id, int speed)
 
static void i2c_probe_slaves (I2C_ID_T i2c)
 
static int i2c_menu (void)
 
static void i2c_eeprom_update_state (I2C_XFER_T *xfer, uint8_t *buff, int sz)
 
static void i2c_eeprom_events (I2C_ID_T id, I2C_EVENT_T event)
 
static void i2c_eeprom_init (I2C_ID_T id)
 
void i2c_iox_update_regs (int ops)
 
static void i2c_iox_events (I2C_ID_T id, I2C_EVENT_T event)
 
static void i2c_iox_init (I2C_ID_T id)
 
void SysTick_Handler (void)
 SysTick Interrupt Handler.
 
void I2C1_IRQHandler (void)
 I2C Interrupt Handler.
 
void I2C0_IRQHandler (void)
 I2C0 Interrupt handler.
 
int main (void)
 Main program body.
 

Variables

static int mode_poll
 
static I2C_ID_T i2cDev = DEFAULT_I2C
 
static I2C_XFER_T seep_xfer
 
static I2C_XFER_T iox_xfer
 
static uint8_t seep_data [I2C_SLAVE_EEPROM_SIZE+1]
 
static uint8_t buffer [2][256]
 
static uint8_t iox_data [2]
 

Macro Definition Documentation

#define DEFAULT_I2C   I2C1

Definition at line 142 of file i2c.c.

#define I2C_EEPROM_BUS   DEFAULT_I2C

Definition at line 145 of file i2c.c.

#define I2C_IOX_BUS   DEFAULT_I2C

Definition at line 146 of file i2c.c.

#define I2C_SLAVE_EEPROM_ADDR   0x5A

Definition at line 168 of file i2c.c.

#define I2C_SLAVE_EEPROM_SIZE   64

Definition at line 167 of file i2c.c.

#define I2C_SLAVE_IOX_ADDR   0x5B

Definition at line 169 of file i2c.c.

#define SPEED_100KHZ   100000

Definition at line 148 of file i2c.c.

#define SPEED_400KHZ   400000

Definition at line 149 of file i2c.c.

Function Documentation

static int con_get_input ( const char *  str)
static

Definition at line 216 of file i2c.c.

static void con_print_data ( const uint8_t *  dat,
int  sz 
)
static

Definition at line 200 of file i2c.c.

void I2C0_IRQHandler ( void  )

I2C0 Interrupt handler.

Returns
None

Definition at line 520 of file i2c.c.

void I2C1_IRQHandler ( void  )

I2C Interrupt Handler.

Returns
None

Definition at line 511 of file i2c.c.

static void i2c_app_init ( I2C_ID_T  id,
int  speed 
)
static

Definition at line 337 of file i2c.c.

static void i2c_eeprom_events ( I2C_ID_T  id,
I2C_EVENT_T  event 
)
static

Definition at line 391 of file i2c.c.

static void i2c_eeprom_init ( I2C_ID_T  id)
static

Definition at line 428 of file i2c.c.

static void i2c_eeprom_update_state ( I2C_XFER_T xfer,
uint8_t *  buff,
int  sz 
)
static

Definition at line 384 of file i2c.c.

static void i2c_iox_events ( I2C_ID_T  id,
I2C_EVENT_T  event 
)
static

Definition at line 455 of file i2c.c.

static void i2c_iox_init ( I2C_ID_T  id)
static

Definition at line 481 of file i2c.c.

void i2c_iox_update_regs ( int  ops)

Definition at line 440 of file i2c.c.

static int i2c_menu ( void  )
static

Definition at line 377 of file i2c.c.

static void i2c_probe_slaves ( I2C_ID_T  i2c)
static

Definition at line 350 of file i2c.c.

static void i2c_rw_input ( I2C_XFER_T xfer,
int  ops 
)
static

Definition at line 290 of file i2c.c.

static void i2c_set_mode ( I2C_ID_T  id,
int  polling 
)
static

Definition at line 322 of file i2c.c.

static void i2c_state_handling ( I2C_ID_T  id)
static

Definition at line 189 of file i2c.c.

int main ( void  )

Main program body.

Returns
int

Definition at line 529 of file i2c.c.

void SysTick_Handler ( void  )

SysTick Interrupt Handler.

SysTick 1mS tick handler.

Returns
Nothing
Note
Systick interrupt handler updates the button status

Definition at line 502 of file i2c.c.

Variable Documentation

uint8_t buffer[2][256]
static

Definition at line 177 of file i2c.c.

I2C_ID_T i2cDev = DEFAULT_I2C
static

Definition at line 164 of file i2c.c.

uint8_t iox_data[2]
static

Definition at line 178 of file i2c.c.

I2C_XFER_T iox_xfer
static

Definition at line 173 of file i2c.c.

int mode_poll
static

Definition at line 163 of file i2c.c.

uint8_t seep_data[I2C_SLAVE_EEPROM_SIZE+1]
static

Definition at line 176 of file i2c.c.

I2C_XFER_T seep_xfer
static

Definition at line 172 of file i2c.c.