I2C Read problem
Posted: Fri Mar 13, 2020 10:42 am
I want to read/write bno055
And I check
But I have a wrong reading from the chip. May be I do something wrong in I2C read/write routines?
- s8 BNO055_I2C_bus_write(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt)
- {
- i2c_cmd_handle_t cmd = i2c_cmd_link_create();
- i2c_master_start(cmd);
- i2c_master_write_byte(cmd, (dev_addr << 1) | WRITE_BIT, ACK_CHECK_EN);
- i2c_master_write_byte(cmd, reg_addr, ACK_CHECK_EN);
- i2c_master_write_byte(cmd, *reg_data, ACK_CHECK_EN);
- i2c_master_stop(cmd);
- esp_err_t ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000 / portTICK_RATE_MS); //1000
- i2c_cmd_link_delete(cmd);
- ets_delay_us(10000);
- return ret;
- }
- s8 BNO055_I2C_bus_read(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt)
- {
- int ret;
- int i;
- i2c_cmd_handle_t cmd = i2c_cmd_link_create();
- //first - write a pointer to a register
- i2c_master_start(cmd);
- i2c_master_write_byte(cmd, (dev_addr << 1) | WRITE_BIT, ACK_CHECK_EN);
- i2c_master_write_byte(cmd, reg_addr, ACK_CHECK_EN);
- i2c_master_stop(cmd);
- //second - read from the pointer
- ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000 / portTICK_RATE_MS);
- i2c_cmd_link_delete(cmd);
- //if (ret != ESP_OK)
- //return ret;
- cmd = i2c_cmd_link_create();
- i2c_master_start(cmd);
- i2c_master_write_byte(cmd, dev_addr << 1 | READ_BIT, ACK_CHECK_EN);
- if (cnt > 1)
- {
- for (i = 1; i < cnt; i++)
- {
- i2c_master_read_byte(cmd, reg_data, ACK_VAL);
- reg_data++;
- }
- i2c_master_read_byte(cmd, reg_data, NACK_VAL);
- }
- else
- i2c_master_read_byte(cmd, reg_data, NACK_VAL);
- i2c_master_stop(cmd);
- ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000 / portTICK_RATE_MS); //1000
- i2c_cmd_link_delete(cmd);
- return ret;
- }
- BNO055_I2C_bus_read(BNO055_I2C_ADDR1, BNO055_CHIP_ID_REG, &val, 1);
- printf("CHIP ID %X\n", val);