I2C Slave Mode
This sample code guide is written to help users easily and completely understand I2C sample. This sample demonstrates how I2C slave sends and receives data in interrupt mode. In this sample, I2C is configured as the slave. The chip read data from the I2C master and write data to the I2C master.
Requirements
For hardware requirements, please refer to the Requirements.
Wiring
Connect P0_0 to the SCL of the I2C master and connect P0_1 to the SDA of the I2C master, need to connect 2.2k pull-up resistor.
The hardware connection of I2C sample code is shown in the figure below.

I2C Sample Code Hardware Connection Diagram
Configurations
The following macros can be configured to modify pin definitions.
#define PIN_I2C1_SCL P0_0
#define PIN_I2C1_SDA P0_1
The entry function is as follows, call this function in
main()
to run this sample code. For more details, please refer to the Initialization.i2c_slave_demo();
Building and Downloading
For building and downloading, please refer to the Building and Downloading.
Experimental Verification
Press the Reset button on the EVB, the master device sends 1 byte of data to the slave device, which contains the number 0x55.
The slave triggers the RX full interrupt and prints log in Debug Analyzer.
i2c1_handler: rx_count 1, read_buf 170
After sending, the master device sends a read request command to the slave device.
The slave receives the read request from the master, the slave triggers read request interrupt and sends 1 byte of data to the master device, which contains the number 0x66.
When the slave detects the stop signal, the slave triggers stop signal detection interrupt and prints log in Debug Analyzer.
i2c1_handler: I2C1 stop detect
Code Overview
This section introduces the code and process description for initialization and corresponding function implementation in the sample.
Source Code Directory
For project directory, please refer to Source Code Directory.
Source code directory:
sdk\src\sample\io_demo\i2c\slave\i2c_slave_demo.c
.
Initialization
The initialization flow for peripherals can refer to Initialization Flow.
I2C slave mode initialization flow is shown in the following figure.

I2C Slave Mode Initialization Flow Chart
Call
Pad_Config()
andPinmux_Config()
to initialize the pin.static void board_i2c1_init(void) { Pinmux_Config(PIN_I2C1_SDA, I2C1_DAT); Pinmux_Config(PIN_I2C1_SCL, I2C1_CLK); Pad_Config(PIN_I2C1_SDA, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_NONE, PAD_OUT_DISABLE, PAD_OUT_HIGH); Pad_Config(PIN_I2C1_SCL, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_NONE, PAD_OUT_DISABLE, PAD_OUT_HIGH); }
Call
RCC_PeriphClockCmd()
to enable the I2C clock and function.Initialize the I2C peripheral:
Define the
I2C_InitTypeDef
typeI2C_InitStructure
, and callI2C_StructInit()
to pre-fillI2C_InitStructure
with default values.Modify the
I2C_InitStructure
parameters as needed. The I2C initialization parameter configuration is shown in the table below.Call
I2C_Init()
to initialize the I2C peripheral.
I2C Initialization Parameters I2C Hardware Parameters
Setting in the
I2C_InitStructure
I2C
Clock
100000
Device Role (I2C Master or I2C Slave)
Address Mode (7bits/10bits Mode)
Slave Address
0x50
Auto ACK Enable
Receive FIFO Threshold Level
0
Call
I2C_INTConfig()
to enable read request interruptI2C_INT_RD_REQ
and RX full interruptI2C_INT_RX_FULL
and stop signal detection interruptI2C_INT_STOP_DET
.Call
NVIC_Init()
to enable NVIC of I2C.Call
I2C_Cmd()
to enable I2C.
Functional Implementation
Interrupt Handle
When the receive buffer reaches or goes above the receive FIFO threshold level (
I2C_InitTypeDef::I2C_RxThresholdLevel
+ 1), RX full interrupt will be triggered and enters the interrupt handler:Call
I2C_GetINTStatus()
to checkI2C_INT_RX_FULL
interrupt status.Call
I2C_GetRxFIFOLen()
to get the data length in RX FIFO.Call
I2C_ReceiveData()
to receive data from RX FIFO.Call
I2C_ClearINTPendingBit()
to clearI2C_INT_RX_FULL
interrupt.
When I2C master is attempting to read data from I2C slave, read request interrupt will be triggered and enters the interrupt handler:
Call
I2C_GetINTStatus()
to checkI2C_INT_RD_REQ
interrupt status.Call
I2C_SendCmd()
to write data to master.Call
I2C_ClearINTPendingBit()
to clearI2C_INT_RD_REQ
interrupt.
When the master detects a stop signal, stop signal detection interrupt will be triggered and enters the interrupt handler:
Call
I2C_GetINTStatus()
to checkI2C_INT_STOP_DET
interrupt status.Call
I2C_ClearINTPendingBit()
to clearI2C_INT_STOP_DET
interrupt.