I2C Master Write

This sample code guide is written to help users easily and completely understand I2C sample. This sample demonstrates I2C master write 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.

    • #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_mw_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 6 bytes of data to slave, the I2C slave will receive data 0xaa, 0xbb, 0x66, 0x68, 0x77, 0x88.

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\master_write\i2c_mw_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_Mode_Initialization_Flow_Chart.png

I2C Master Mode Initialization Flow Chart

  1. Call Pad_Config() and Pinmux_Config() to initialize the pin.

    static void board_i2c_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_ENABLE,
                   PAD_OUT_HIGH);
       Pad_Config(PIN_I2C1_SCL, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_NONE, PAD_OUT_ENABLE,
                   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

    400000

    Device Role (I2C Master or I2C Slave)

    I2C_InitTypeDef::I2C_DeviveMode

    I2C_DeviveMode_Master

    Address Mode (7bits/10bits Mode)

    I2C_InitTypeDef::I2C_AddressMode

    I2CAddressMode_TypeDef::I2C_AddressMode_7BIT

    Slave Address

    I2C_InitTypeDef::I2C_SlaveAddress

    0x08

    Auto ACK Enable

    I2C_InitTypeDef::I2C_Ack

    I2C_Ack_Enable

  4. Call I2C_Cmd() to enable I2C.

Functional Implementation

Master Write

The master call I2C_MasterWrite(), sends the data in I2C_WriteBuf to the slave.

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

if (I2C_Success != I2C_MasterWrite(I2C1, I2C_WriteBuf, 6))
{
   IO_PRINT_ERROR0("i2c_mw_demo: Send failed");

   //Check Event
   if (I2C_CheckEvent(I2C1, ABRT_7B_ADDR_NOACK) == SET)
   {
      IO_PRINT_ERROR0("i2c_mw_demo: Wrong addr");
   }
   if (I2C_CheckEvent(I2C1, ABRT_GCALL_NOACK) == SET)
   {
      IO_PRINT_ERROR0("i2c_mw_demo: General call nack");
   }
   I2C_SendCmd(I2C1, I2C_WRITE_CMD, 0, I2C_STOP_ENABLE);
   I2C_Cmd(I2C1, DISABLE);
   I2C_Cmd(I2C1, ENABLE);
   if (I2C_Success != I2C_MasterWrite(I2C1, I2C_WriteBuf, 6))
   {
      IO_PRINT_ERROR0("i2c_mw_demo: Send failed again");
   }
}