We're experimenting with a fuel gauge that we'd connect via I2C. I'm trying to follow the docs, but my initial attempts are returning an error. Here's the code:
Code: Select all
const uint8_t FUEL_CELL_ADDR = 0x6c; // from MAX77818 data sheet.
const gpio_num_t GPIO_I2C_SDA = GPIO_NUM_18;
const gpio_num_t GPIO_I2C_SCL = GPIO_NUM_19;
const i2c_port_t I2C_PORT_NBR = I2C_NUM_0;
...
// i2cInit(): configures and installs the i2c driver.
void PowerMgr::i2cInit()
{
m_i2c.mode = I2C_MODE_MASTER;
m_i2c.sda_io_num = GPIO_I2C_SDA;
m_i2c.sda_pullup_en = GPIO_PULLUP_DISABLE;
m_i2c.scl_io_num = GPIO_I2C_SCL;
m_i2c.scl_pullup_en = GPIO_PULLUP_DISABLE;
m_i2c.master.clk_speed = CONFIG_I2C_MASTER_FREQUENCY;
ESP_ERROR_CHECK(i2c_driver_install(I2C_PORT_NBR, I2C_MODE_MASTER, 16, 16, 0));
}
void PowerMgr::i2cWrite(uint8_t *buffOut, uint32_t len)
{
esp_err_t err;
// 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.
uint8_t data = (uint8_t) ((FUEL_CELL_ADDR) | 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, data, true));
ESP_ERROR_CHECK(i2c_master_write(m_i2c_cmd, buffOut, len, true));
ESP_ERROR_CHECK(i2c_master_stop(m_i2c_cmd));
err = (i2c_master_cmd_begin(I2C_PORT_NBR, m_i2c_cmd, 1));
if (err != ESP_OK)
{
ESP_LOGE(TAG, "i2cWrite(): error %x on i2c_master_cmd_begin.", err);
}
i2c_cmd_link_delete(m_i2c_cmd);
Code: Select all
E (1765) i2c: C:/esp-idf/components/driver/i2c.c:537 (i2c_master_clear_bus):scl gpio number error
E (1765) PowerMgr: i2cWrite(): error 107 on i2c_master_cmd_begin.