Hi Fly -
It took me some time to figure out the Maxim datasheet, but I believe that their addresses are "pre-shifted."
Slave Addresses
Charger: 0xD2/D3h
Clogic, GTEST and Safeout LDOs: 0xCCh/0xCDh
Fuel Gauge: 0x6C/0x6D. See the Fuel Gauge I2C Protocol for details in the Fuel Gauge section.
I believe that, in the example of the fuel gauge, the address is really 0x6c >> 1. When you want to write, you use 0x6c; when you want to read, you use 0x6d (I hope that made sense).
I've managed to get this much working (and by "working" I mean not producing a timeout error):
Code: Select all
esp_err_t PowerMgr::i2cRead(uint8_t devAddr, uint8_t regAddr, uint8_t *data)
{
esp_err_t err;
uint8_t addrRead, addrWrite;
// note about data (address) field: the ESP32 docs say to shift addresses one bit to the left,
// but it appears that the addresses supplied in the MAX77818 data sheet already have been shifted.
// keep this in mind when setting addresses from the datasheet.
addrRead = static_cast<uint8_t>(((devAddr) | I2C_MASTER_READ));
addrWrite = static_cast<uint8_t>(((devAddr) | I2C_MASTER_WRITE));
m_i2c_cmd = i2c_cmd_link_create();
ESP_ERROR_CHECK(i2c_master_start(m_i2c_cmd));
ESP_ERROR_CHECK(i2c_master_write_byte(m_i2c_cmd, addrWrite, true));
ESP_ERROR_CHECK(i2c_master_write_byte(m_i2c_cmd, regAddr, true));
//ESP_ERROR_CHECK(i2c_master_read(m_i2c_cmd, data, 1, I2C_MASTER_LAST_NACK))
ESP_ERROR_CHECK(i2c_master_stop(m_i2c_cmd));
err = (i2c_master_cmd_begin(I2C_PORT_NBR, m_i2c_cmd, 100));
if (err == ESP_OK)
{
ESP_LOGI(TAG, "i2cRead(): i2c_master_cmd_begin() successful.");
}
else
{
ESP_LOGE(TAG, "i2cRead(): error %x on i2c_master_cmd_begin.", err);
}
i2c_cmd_link_delete(m_i2c_cmd);
return err;
}
If I uncomment the i2c_master_read line, then I get a time out. I'm sure it's my doing; I'm still trying to understand the protocol of the Maxim device. I am curious, though, how I'm supposed to introduce the read address into my code.