7.1. I2C - AT24CXX read and write

7.1.1. Hardware Connection

This demo is based on BL706_IOT, add AT24CXX circuit by yourself, the connection method is as follows

   GPIO function         GPIO pin
----------------------------------
    I2C_SCL      <-->     GPIO11
    I2C_SDA      <-->     GPIO16

7.1.2. Software Implementation

  • See examples/i2c/i2c_at24cxx for the software code

1#define BSP_I2C_CLOCK_SOURCE  ROOT_CLOCK_SOURCE_BCLK
2#define BSP_I2C_CLOCK_DIV  0
  • Configure the I2C device clock source, see bsp/board/bl706_iot/clock_config.h

1#define CONFIG_GPIO11_FUNC GPIO_FUN_I2C
2#define CONFIG_GPIO16_FUNC GPIO_FUN_I2C
  • Configure I2C device multiplexing pins, see bsp/board/bl706_iot/peripheral_config.h

 1#define BSP_USING_I2C0
 2
 3#if defined(BSP_USING_I2C0)
 4#ifndef I2C0_CONFIG
 5#define I2C0_CONFIG \
 6{   \
 7.id = 0, \
 8.mode = I2C_HW_MODE,\
 9.phase = 15, \
10}
11#endif
12#endif
  • Enable BSP_USING_I2C0 and configure I2C device, see bsp/board/bl706_iot/peripheral_config.h

1i2c_register(I2C0_INDEX, "i2c", DEVICE_OFLAG_RDWR);
2struct device *i2c0 = device_find("i2c");
3
4if (i2c0)
5{
6    MSG("device find success\r\n");
7    device_open(i2c0, 0);
8}
  • First call the i2c_register function to register the I2C device, currently register I2C0

  • Then use the find function to find the handle corresponding to the device and save it in the i2c0 handle

  • Finally use device_open to open the I2C0 device in the default mode

 1i2c_msg_t msg[2];
 2uint8_t buf[8] = {0};
 3
 4msg[0].buf = buf;
 5msg[0].flags = SUB_ADDR_1BYTE | I2C_WR;
 6msg[0].len = 8;
 7msg[0].slaveaddr = 0x50;
 8msg[0].subaddr = 0x00;
 9
10msg[1].buf = buf;
11msg[1].flags = SUB_ADDR_1BYTE | I2C_RD;
12msg[1].len = 8;
13msg[1].slaveaddr = 0x50;
14msg[1].subaddr = 0x00;
15if (i2c_transfer(i2c0, &msg[0], 2) == 0)
16    MSG("\r\n read:%0x\r\n", msg[1].buf[0] << 8 | msg[1].buf[1]);
  • Call i2c_transfer to transfer two msg, one msg represents writing 8-byte data to eeprom, and one msg represents reading 8-byte data from eeprom

7.1.3. Compile and Program

1 $ cd <sdk_path>/bl_mcu_sdk
2 $ make BOARD=bl706_iot APP=i2c_at24cxx

7.1.4. Experimental Phenomena