Second, the I2C config code using esp32-idf(version:3.1.3) is as shown below:
Code: Select all
i2c_config_t conf;
conf.mode = I2C_MODE_MASTER;
conf.sda_io_num = sda_pin; //sda_pin is 21
conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
conf.scl_io_num = scl_pin; //scl_pin is 22
conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
conf.master.clk_speed = clk_frq; //clk_frq is 100000
i2c_param_config(i2c_port, &conf); //i2c_port=I2C_NUM_1
i2c_driver_install(i2c_port, conf.mode, I2C_NUM1_MASTER_RX_BUF_DISABLE, I2C_NUM1_MASTER_TX_BUF_DISABLE, 0); //i2c_port=I2C_NUM_1
Code: Select all
int32_t ret_test = 0;
uint8_t BNO055_ADDR = 0x28;
uint8_t BNO055_CHIP_ID = 0x00;
uint8_t tmp_data = 0x00;
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, BNO055_ADDR << 1, MASTER_EN_CHECK_ACK); //MASTER_EN_CHECK_ACK=1
i2c_master_write_byte(cmd, BNO055_CHIP_ID, MASTER_EN_CHECK_ACK); //MASTER_EN_CHECK_ACK=1
i2c_master_stop(cmd);
ret_test = i2c_master_cmd_begin(I2C_NUM1_MASTER_PORT_GPIO, cmd, 1000 / portTICK_RATE_MS); //I2C_NUM1_MASTER_PORT_GPIO=I2C_NUM_1
cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (BNO055_ADDR<<1) + 1, MASTER_EN_CHECK_ACK); //MASTER_EN_CHECK_ACK=1
i2c_master_read_byte(cmd, &tmp_data, 0x01);
i2c_master_stop(cmd);
ret_test = i2c_master_cmd_begin(I2C_NUM1_MASTER_PORT_GPIO, cmd, 1000 / portTICK_RATE_MS); //I2C_NUM1_MASTER_PORT_GPIO=I2C_NUM_1
i2c_cmd_link_delete(cmd);
if(ret_test == ESP_OK)
{
printf("the BNO055 id is %#X\n", tmp_data);
}
else
{
printf("the BNO055 timeout!!!\n");
}