espressif / esp-idf

Espressif IoT Development Framework. Official development framework for Espressif SoCs.
Apache License 2.0
13.84k stars 7.32k forks source link

Allocate and initialize an I2C master bus - Exception was unhandled. (IDFGH-13993) #14819

Closed RDobrinov closed 1 week ago

RDobrinov commented 3 weeks ago

Answers checklist.

IDF version.

v5.4-dev-3951-g9106c43acc-dirty / v5.3.1 release version

Espressif SoC revision.

ESP32-D0WD (revision v1.0)

Operating System used.

Linux

How did you build your project?

VS Code IDE

If you are using Windows, please specify command line type.

None

Development Kit.

ESP32-Wrover-B

Power Supply used.

USB

What is the expected behavior?

To work correctly when no more free I2C controllers available for i2c_new_master_bus()

What is the actual behavior?

Guru Meditation Error: Core 0 panic'ed (LoadProhibited). Exception was unhandled. when no free controller available

Steps to reproduce.

` i2c_master_bus_handle_t bus_handle_a; i2c_master_bus_handle_t bus_handle_b; i2c_master_bus_handle_t bus_handle_c;

i2c_master_bus_config_t i2c_mst_config = { .clk_source = I2C_CLK_SRC_DEFAULT, .i2c_port = -1, .scl_io_num = GPIO_NUM_18, .sda_io_num = GPIO_NUM_19, .glitch_ignore_cnt = 7, .flags.enable_internal_pullup = true, };

esp_err_t err = i2c_new_master_bus(&i2c_mst_config, &bus_handle_a));

i2c_mst_config.scl_io_num = GPIO_NUM_21; i2c_mst_config.sda_io_num = GPIO_NUM_22; esp_err_t err = i2c_new_master_bus(&i2c_mst_config, &bus_handle_b));

i2c_mst_config.scl_io_num = GPIO_NUM_23; i2c_mst_config.sda_io_num = GPIO_NUM_25; esp_err_t err = i2c_new_master_bus(&i2c_mst_config, &bus_handle_c)); `

Debug Logs.

I (265) spi_flash: flash io: qout
I (269) main_task: Started on CPU0
I (279) main_task: Calling app_main()
E (279) i2c.common: i2c_acquire_bus_handle(173): acquire bus failed, no free bus
E (289) i2c.master: i2c_new_master_bus(946): I2C bus acquire failed
Guru Meditation Error: Core  0 panic'ed (LoadProhibited). Exception was unhandled.

Core  0 register dump:
PC      : 0x400d9c51  PS      : 0x00060330  A0      : 0x800d928c  A1      : 0x3ffb3e30  
--- 0x400d9c51: i2c_common_deinit_pins at /home/vbox/esp/master/esp-idf/components/esp_driver_i2c/i2c_common.c:412

A2      : 0x00000000  A3      : 0x3f4067e8  A4      : 0x3f406ae8  A5      : 0x3ffb3e70  
A6      : 0x3ffb3e50  A7      : 0x00000000  A8      : 0x800e53cc  A9      : 0x3ffb3de0  
A10     : 0x00000044  A11     : 0x3ffb3e70  A12     : 0x3ffb3e50  A13     : 0x0000000c  
A14     : 0x3ffb3e20  A15     : 0x0000000c  SAR     : 0x00000004  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x00000000  LBEG    : 0x400014fd  LEND    : 0x4000150d  LCOUNT  : 0xfffffffb  
--- 0x400014fd: strlen in ROM
0x4000150d: strlen in ROM

More Information.

esp-idf/components/esp_driver_i2c/i2c_master.c Allocate memory for bus handle. i2c_master->base pointer is NULL. i2c_acquire_bus_handle() fail with no more more free bus and jump to error processing and try to destroy master bus handle. Because i2c_master is valid i2c_common_deinit_pins() and i2c_release_bus_handle() will be executed with NULL pointer _(Checked by changing L787 to if(i2c_master->base) i2c_common_deinit_pins(i2c_master->base);)_

RDobrinov commented 2 weeks ago

In addition. When no free buses left i2c_acquire_bus_handle() do not release s_i2c_platform.mutex. ESP_RETURN_ON_FALSE is executed and lock never released