UM10850
All information provided in this document is subject to legal disclaimers.
© NXP B.V. 2016. All rights reserved.
User manual
Rev. 2.4 — 13 September 2016
305 of 464
NXP Semiconductors
UM10850
Chapter 23: LPC5410x I2C-bus interfaces (I2C0/1/2)
23.4.1.2 Master read from slave
Configure I2C0 as a master: Set the MSTEN bit to 1 in the CFG register. See
Read data from the slave:
1. Write the slave address with the RW bit set to 1 to the Master data register MSTDAT.
See
2. Start the transmission by setting the MSTSTART bit to 1 in the Master control register.
See
. The following happens:
–
The pending status is cleared and the I
2
C-bus is busy.
–
The I
2
C master sends the start bit and address with the RW bit to the slave.
–
The slave sends 8 bit of data.
3. Wait for the pending status to be set (MSTPENDING = 1) by polling the STAT register.
4. Read 8 bits of data from the MSTDAT register.
5. Stop the transmission by setting the MSTSTOP bit to 1 in the Master control register.
See
23.4.2 I
2
C receive/transmit in slave mode
In this example, I2C0 is configured as the slave. The slave receives 8 bits from the master
and then sends 8 bits to the master. The system clock is set to 30 MHz and the bit rate is
approximately 400 KHz. The I2C0_SCL and I2C0_SDA functions must be enabled on
pins PIO0_22 and PIO0_23 through IOCON. See
.
The pins should be configured as required for the I
2
C-bus mode that will be used (SM,
FM, FM+, HS) via the IOCON block. See
The transmission of the address and data bits is controlled by the state of the
SLVPENDING status bit. Whenever the status is Slave pending, the slave can
acknowledge (“ack”) or send or receive an address and data. The received data or the
data to be sent to the master are available in the SLVDAT register. After sending and
receiving data, continue to the next step of the transmission protocol by writing to the
SLVCTL register.
Table 336. Code example
Master read from slave
// Master read 1 byte from slave. Address 0x23. Polling mode. No error checking.
uint8_t data;
I2C->CFG = I2C_CFG_MSTEN;
while(!(I2C->STAT & I2C_STAT_MSTPENDING));
if((I2C->STAT & I2C_STAT_MSTSTATE) != I2C_STAT_MSTST_IDLE) abort();
I2C->MSTDAT = (0x23 << 1) | 1; // address and 1 for RWn bit
I2C->MSTCTL = I2C_MSTCTL_MSTSTART; // send start
while(!(I2C->STAT & I2C_STAT_MSTPENDING));
if((I2C->STAT & I2C_STAT_MSTSTATE) != I2C_STAT_MSTST_RX) abort();
data = I2C->MSTDAT; // read data
if(data != 0xdd) abort();
I2C->MSTCTL = I2C_MSTCTL_MSTSTOP; // send stop
while(!(I2C->STAT & I2C_STAT_MSTPENDING));
if((I2C->STAT & I2C_STAT_MSTSTATE) != I2C_STAT_MSTST_IDLE) abort();