ESP32-S3 ESP-IDF I2C read function with BME280

Viewed 20

I'm currently trying to write an adaptation to the BME280 driver provided by Bosch in Esp-idf for an ESP32-S3-DevKitC-1. I Based my code on the examples in the repo, where you have to provide I2C read and write functions, however my I2C read function does not seem to properly work. The provided instruction on how the i2c read function should be is as follows:

 Data on the bus should be like
 |------------+---------------------|
 | I2C action | Data                |
 |------------+---------------------|
 | Start      | -                   |
 | Write      | (reg_addr)          |
 | Stop       | -                   |
 | Start      | -                   |
 | Read       | (reg_data[0])       |
 | Read       | (....)              |
 | Read       | (reg_data[len - 1]) |
 | Stop       | -                   |
 |------------+---------------------|
 

My read implementation looks like this:

int8_t user_i2c_read(uint8_t reg_addr, uint8_t *data, uint32_t len, void *intf_ptr){

esp_err_t rslt = 0;

i2c_cmd_handle_t cmd = i2c_cmd_link_create();

ESP_ERROR_CHECK(i2c_master_start(cmd));
printf("%d", reg_addr);

ESP_ERROR_CHECK(i2c_master_write_byte(cmd, (BME280_I2C_ADDR_SEC << 1) | I2C_MASTER_READ, I2C_MASTER_ACK));
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, reg_addr, I2C_MASTER_ACK));

ESP_ERROR_CHECK(i2c_master_start(cmd));

ESP_ERROR_CHECK(i2c_master_write_byte(cmd, (reg_addr << 1) | I2C_MASTER_READ, I2C_MASTER_ACK));

if (len > 1) 
{
    ESP_ERROR_CHECK(i2c_master_read(cmd, data, (size_t) len - 1, I2C_MASTER_ACK));
}

ESP_ERROR_CHECK(i2c_master_read_byte(cmd, data + len - 1, I2C_MASTER_NACK));

ESP_ERROR_CHECK(i2c_master_stop(cmd));

ESP_ERROR_CHECK(rslt = i2c_master_cmd_begin(I2C_NUM_0, cmd, 10 / portTICK_PERIOD_MS));

i2c_cmd_link_delete(cmd);

return rslt;
}

This seems to always read 255 when its called from the driver, which results in the application failing on initialization of the sensor.

0 Answers
Related