First of all, I'm a bit of a newbie when it comes to embedded programming. I'm working on my final degree project using an ESP32, and I'm having trouble configuring an external ADC (ADS1115).
According to the ADS1115 datasheet, I need to write 4 bytes to configure the register in the following order: START - ADDRESS/RW - 00000001 - 10000100 - 10000011 - STOP
I tested whether the ADS1115 is correctly connected using the i2c_master_probe function, and it seems to be working properly. However, the problem arises when I try to send all four bytes in the same transmit function. I get this error:
Code: Select all
D (326) i2c.common: bus clock source frequency: 80000000hz
Device found on I2C bus
E (334) i2c.master: s_i2c_synchronous_transaction(830): I2C transaction failed
I (341) gpio: GPIO[21]| InputEn: 1| OutputEn: 1| OpenDrain: 1| Pullup: 0| Pulldown: 0| Intr:0
I (351) gpio: GPIO[22]| InputEn: 1| OutputEn: 1| OpenDrain: 1| Pullup: 0| Pulldown: 0| Intr:0
E (360) i2c.master: i2c_master_transmit(1033): I2C transaction failed
ESP_ERROR_CHECK failed: esp_err_t 0x103 (ESP_ERR_INVALID_STATE) at 0x400d5768
0x400d5768: i2c_master_config at /home/xae/Projects/TFG_ECG/codes/ecg_b_i2c/main/main.c:121 (discriminator 1)
file: "./main/main.c" line 121
func: i2c_master_config
expression: i2c_master_transmit(i2c_handle, config_register_data, sizeof(config_register_data), -1)
abort() was called at PC 0x40085adf on core 0
0x40085adf: _esp_error_check_failed at /home/xae/esp/v5.2.2/esp-idf/components/esp_system/esp_err.c:50
Backtrace: 0x4008160e:0x3ffb4d00 0x40085ae9:0x3ffb4d20 0x4008b6bd:0x3ffb4d40 0x40085adf:0x3ffb4db0 0x400d5768:0x3ffb4de0 0x400d5886:0x3ffb4e10 0x400e6a2c:0x3ffb4e30 0x400865d9:0x3ffb4e60
0x4008160e: panic_abort at /home/xae/esp/v5.2.2/esp-idf/components/esp_system/panic.c:466
0x40085ae9: esp_system_abort at /home/xae/esp/v5.2.2/esp-idf/components/esp_system/port/esp_system_chip.c:93
0x4008b6bd: abort at /home/xae/esp/v5.2.2/esp-idf/components/newlib/abort.c:38
0x40085adf: _esp_error_check_failed at /home/xae/esp/v5.2.2/esp-idf/components/esp_system/esp_err.c:50
0x400d5768: i2c_master_config at /home/xae/Projects/TFG_ECG/codes/ecg_b_i2c/main/main.c:121 (discriminator 1)
0x400d5886: app_main at /home/xae/Projects/TFG_ECG/codes/ecg_b_i2c/main/main.c:332
0x400e6a2c: main_task at /home/xae/esp/v5.2.2/esp-idf/components/freertos/app_startup.c:208
0x400865d9: vPortTaskWrapper at /home/xae/esp/v5.2.2/esp-idf/components/freertos/FreeRTOS-Kernel/portable/xtensa/port.c:134
Here’s the relevant portion of my code:
Code: Select all
void i2c_master_init(void)
{
i2c_master_bus_config_t i2c_mst_config = {
.clk_source = I2C_CLK_SRC_DEFAULT,
.i2c_port = I2C_PORT_NUMB,
.scl_io_num = I2C_MASTER_SCL_IO,
.sda_io_num = I2C_MASTER_SDA_IO,
.glitch_ignore_cnt = 7,
.flags.enable_internal_pullup = false,
};
i2c_master_bus_handle_t bus_handle;
ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_mst_config, &bus_handle));
esp_err_t ret = i2c_master_probe(bus_handle, ADS1115_ADDRESS, -1);
if (ret != ESP_OK) {
printf("Device not found on I2C bus: %s\n", esp_err_to_name(ret));
} else {
printf("Device found on I2C bus\n");
}
i2c_device_config_t dev_cfg = {
.dev_addr_length = I2C_ADDR_BIT_LEN_7,
.device_address = ADS1115_ADDRESS,
.scl_speed_hz = IC2_MASTER_FREQ_HZ,
};
ESP_ERROR_CHECK(i2c_master_bus_add_device(bus_handle, &dev_cfg, &i2c_handle));
}
void i2c_master_config(void)
{
uint8_t config_register_data[] = {0x90, 0x01,0x84, 0x83};
ESP_ERROR_CHECK(i2c_master_transmit(i2c_handle, config_register_data, sizeof(config_register_data), -1));
// Apuntar al registro de conversión
uint8_t conversion_register_pointer[] = {0x90, 0x00};
ESP_ERROR_CHECK(i2c_master_transmit(i2c_handle, conversion_register_pointer, sizeof(conversion_register_pointer), -1));
}
Any advice would be greatly appreciated.
Thanks!