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.

../../../_images/I2C_Slave_Sample_Code_Hardware_Connection_Diagram.png

I2C Sample Code Hardware Connection Diagram

Configurations

  1. The following macros can be configured to modify pin definitions.

    • #define PIN_I2C1_SCL P0_0

    • #define PIN_I2C1_SDA P0_1

  2. 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

  1. Press the Reset button on the EVB, the master device sends 1 byte of data to the slave device, which contains the number 0x55.

  2. The slave triggers the RX full interrupt and prints log in Debug Analyzer.

    i2c1_handler: rx_count 1, read_buf 170
    
  3. After sending, the master device sends a read request command to the slave device.

  4. 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.

  5. 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.

../../../_images/I2C_Slave_Mode_Initialization_Flow_Chart.png

I2C Slave Mode Initialization Flow Chart

  1. Call Pad_Config() and Pinmux_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);
    }
    
  2. Call RCC_PeriphClockCmd() to enable the I2C clock and function.

  3. Initialize the I2C peripheral:

    1. Define the I2C_InitTypeDef type I2C_InitStructure, and call I2C_StructInit() to pre-fill I2C_InitStructure with default values.

    2. Modify the I2C_InitStructure parameters as needed. The I2C initialization parameter configuration is shown in the table below.

    3. Call I2C_Init() to initialize the I2C peripheral.

    I2C Initialization Parameters

    I2C Hardware Parameters

    Setting in the I2C_InitStructure

    I2C

    Clock

    I2C_InitTypeDef::I2C_ClockSpeed

    100000

    Device Role (I2C Master or I2C Slave)

    I2C_InitTypeDef::I2C_DeviveMode

    I2C_DeviveMode_Slave

    Address Mode (7bits/10bits Mode)

    I2C_InitTypeDef::I2C_AddressMode

    I2CAddressMode_TypeDef::I2C_AddressMode_7BIT

    Slave Address

    I2C_InitTypeDef::I2C_SlaveAddress

    0x50

    Auto ACK Enable

    I2C_InitTypeDef::I2C_Ack

    I2C_Ack_Enable

    Receive FIFO Threshold Level

    I2C_InitTypeDef::I2C_RxThresholdLevel

    0

  4. Call I2C_INTConfig() to enable read request interrupt I2C_INT_RD_REQ and RX full interrupt I2C_INT_RX_FULL and stop signal detection interrupt I2C_INT_STOP_DET.

  5. Call NVIC_Init() to enable NVIC of I2C.

  6. Call I2C_Cmd() to enable I2C.

Functional Implementation

Interrupt Handle

  1. 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:

    1. Call I2C_GetINTStatus() to check I2C_INT_RX_FULL interrupt status.

    2. Call I2C_GetRxFIFOLen() to get the data length in RX FIFO.

    3. Call I2C_ReceiveData() to receive data from RX FIFO.

    4. Call I2C_ClearINTPendingBit() to clear I2C_INT_RX_FULL interrupt.

  2. When I2C master is attempting to read data from I2C slave, read request interrupt will be triggered and enters the interrupt handler:

    1. Call I2C_GetINTStatus() to check I2C_INT_RD_REQ interrupt status.

    2. Call I2C_SendCmd() to write data to master.

    3. Call I2C_ClearINTPendingBit() to clear I2C_INT_RD_REQ interrupt.

  3. When the master detects a stop signal, stop signal detection interrupt will be triggered and enters the interrupt handler:

    1. Call I2C_GetINTStatus() to check I2C_INT_STOP_DET interrupt status.

    2. Call I2C_ClearINTPendingBit() to clear I2C_INT_STOP_DET interrupt.