I2C Master Repeat Read

This sample code guide is written to help users easily and completely understand I2C sample. This sample demonstrates I2C master write and then read data function in polling mode. In this sample, I2C is configured as the master. The chip writes some data to I2C slave.

Requirements

For hardware requirements, please refer to the Requirements.

Wiring

Connect P0_0 to the SCL of the I2C slave, connect P0_1 to the SDA of the I2C slave, need to connect 2.2k pull-up resistor.

The hardware connection of I2C sample code is shown in the figure below.

../../../_images/I2C_Master_Sample_Code_Hardware_Connection_Diagram.png

I2C Sample Code Hardware Connection Diagram

Configurations

  1. The following macros can be configured to modify pin definitions. Connect P0_0 to the SCL of the I2C slave, connect P0_1 to the SDA of the I2C slave.

    • #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_polling_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 2 bytes of data to slave, the I2C slave will receive data 0xaa and 0xbb.

  2. After sending, the master device sends a read request command to the slave device.

  3. When the slave device receives a read request, it sends 4 bytes to the master.

  4. The master reads the data sent by the slave back into I2C_ReadBuf.

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\polling\i2c_polling_demo.c.

Initialization

The initialization flow for peripherals can refer to Initialization Flow.

I2C master mode initialization flow is shown in the following figure.

../../../_images/I2C_Master_Repeat_Read_Initialization_Flow_Chart.png

I2C Master Mode Initialization Flow Chart

  1. Call hal_i2c_init_pin() to initialize the pin.

    hal_i2c_init_pin(I2C_ID, PIN_I2C1_SCL, PIN_I2C1_SDA);
    
  2. Initialize the I2C peripheral:

    1. Define the T_I2C_MASTER_CONFIG type i2c_config.

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

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

    I2C Initialization Parameters

    I2C Hardware Parameters

    Setting in the i2c_config

    I2C

    Clock

    T_I2C_MASTER_CONFIG::i2c_speed

    400000

    Address Mode (7bits/10bits Mode)

    T_I2C_MASTER_CONFIG::addr_mode

    T_I2C_ADDR_MODE::I2C_ADDR_MODE_7_BITS

    Slave Address

    T_I2C_MASTER_CONFIG::target_slave_addr

    0x08

    Lock Type

    T_I2C_MASTER_CONFIG::lock_type

    T_I2C_LOCK_TYPE::I2C_LOCK_DISABLE

Functional Implementation

Master Repeat Read

The master call hal_i2c_master_write_to_read(), sends the data in I2C_WriteBuf to the slave, and reads back the data sent by the slave into I2C_ReadBuf.

uint8_t I2C_WriteBuf[16] = {0xaa, 0xbb, 0x66, 0x68, 0x77, 0x88};
uint8_t I2C_ReadBuf[16] = {0, 0, 0, 0};

T_I2C_STATUS ret = hal_i2c_master_write_to_read(I2C_ID, ADDR, I2C_WriteBuf, 2, I2C_ReadBuf, 4);

if (ret != I2C_STATUS_OK)
{
   IO_PRINT_ERROR1("i2c_polling_demo: i2c write to read failed %d", ret);
}
else
{
   for (uint8_t i = 0; i < 4; i++)
   {
      IO_PRINT_INFO2("i2c_polling_demo: readbuf i %d buf 0x%x", i, I2C_ReadBuf[i]);
   }
}