Page 1 of 1

Read data with I2C communication

Posted: Mon Oct 02, 2023 10:25 am
by DJesusCry
Hello everyone.

I'm trying to read data from a component via I2C

From the documentation of said component, the address of the device is 0x6B and the register address is 0x30.

The data i want to read is the voltage of a battery but it's spread across two bytes with bit 15,14,13,0 being reserved (see attachment)

Thing is, I can't read the value i want (i get 0 for both the data bytes). I'm not sure what I'm doing wrong. I just want to make sure it's not the PCB that is faulty (i'm working with custom work in progress PCBs.

I'm using esp-idf 4.2 and I can not change to another version...

Here my driver setup :
  1. i2c_config_t conf;
  2. conf.mode = I2C_MODE_MASTER;
  3. conf.sda_io_num = SDA;
  4. conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
  5. conf.scl_io_num = SCL;
  6. conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
  7. conf.master.clk_speed = 500000;
  8. i2c_param_config(I2C_NUM_0, &conf);
  9. i2c_driver_install(I2C_NUM_0, conf.mode, 0, 0, 0);
And here is my reading function :
  1. void CPowerManagement::BatteryCheckI2C()
  2. {
  3.     uint8_t data1;
  4.     uint8_t data2;
  5.    
  6.     uint8_t reg= 0x30;
  7.     uint8_t address = 0x6B;
  8.     uint16_t extracted_data;
  9.    
  10.     i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  11.     i2c_master_start(cmd);
  12.     i2c_master_write_byte(cmd, (address << 1) | I2C_MASTER_WRITE, 1);
  13.     i2c_master_write_byte(cmd, reg, 1);
  14.     i2c_master_start(cmd);
  15.     i2c_master_write_byte(cmd, (address << 1) | I2C_MASTER_READ, 1);
  16.     i2c_master_read_byte(cmd, &data1, I2C_MASTER_ACK);
  17.     i2c_master_read_byte(cmd, &data2, I2C_MASTER_LAST_NACK);
  18.     i2c_master_stop(cmd);
  19.     i2c_master_cmd_begin(0, cmd, pdMS_TO_TICKS(3000));
  20.     i2c_cmd_link_delete(cmd);
  21.    
  22.    
  23.     // extracted_data = (data1 << 3) | (data2 >> 1);
  24.     ESP_LOGI(TAG, "Value read from I2C battery : %d et %d", data1, data2); // i get 0 and 0
  25. }