m5stack / M5Unified

Unified library for M5Stack series
MIT License
302 stars 54 forks source link

IDF 5.1/Arduino 3.0 support: legacy adc driver migration missing causing a boot crash if an ADC input is used #89

Open mhaberler opened 11 months ago

mhaberler commented 11 months ago

I observed this:

Compiling .pio/build/coreS3/lib561/Wire/Wire.cpp.o In file included from .pio/libdeps/coreS3/M5Unified/src/utility/Power_Class.hpp:17, from .pio/libdeps/coreS3/M5Unified/src/M5Unified.hpp:54, from .pio/libdeps/coreS3/M5Unified/src/M5Unified.h:5, from src/main.cpp:1: /Users/mah/.platformio/packages/framework-arduinoespressif32/tools/esp32-arduino-libs/esp32s3/include/driver/deprecated/driver/adc.h:19:2: warning: #warning "legacy adc driver is deprecated, please migrate to use esp_adc/adc_oneshot.h and esp_adc/adc_continuous.h for oneshot mode and continuous mode drivers respectively" [-Wcpp] 19 | #warning "legacy adc driver is deprecated, please migrate to use esp_adc/adc_oneshot.h and esp_adc/adc_continuous.h for oneshot mode and continuous mode drivers respectively" | ^~~

normally a deprecation warning might not be much of an issue

however, if the user code initializes an analog input pin, a crash during boot happens:

abort() was called at PC 0x42030203 on core 0 => 0x42030203: check_adc_oneshot_driver_conflict at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/driver/deprecated/adc_legacy.c:930 (discriminator 3)

...

the reason is that Power_Class.hpp initiates the legacy adc then Arduino 3.0 user code initiates the newer esp_adc/adc_oneshot.h (or esp_adc/adc_continuous.h) API's and you cant have both causing the abort

looks like this code needs to be migrated to the adc_oneshot API similar to https://github.com/espressif/arduino-esp32/pull/7827/commits/74f4582aacfc790ef3c09be8ccb3224cf95ba25c#diff-d2f30d30397442b48494b2372391850aaa539f77977cb1095f5f1d96855598a3

I've tried fixing the headers and disabling the legacy code and the boot crash goes away confirming my theory

this should take care of both Arduino3 and pure IDF5.1 builds using ADC pins

Michael

ps: I think I remember a remaining warning about ADC migration from I2S code

mhaberler commented 11 months ago

you can use this example to verify: https://github.com/mhaberler/arduino3-playground/commit/54b1258a69ed37fdb16b71594010cea9bc1a8ade

lovyan03 commented 10 months ago

@mhaberler Thank you for pointing this out. I am willing to proceed with the work to address this. I apologize that I am currently overwhelmed with a large number of tasks and will not be able to begin work on this for some time...

mhaberler commented 10 months ago

no hurry, Arduino 3 is not out yet for good and I can fall back to Arduino 2 for now

mhaberler commented 2 months ago

this is now a showstopper with Arduino 3.0.1:

ELF file SHA256: d304cc5c40f9ff37

Rebooting...
ESP-ROM:esp32s3-20210327
Build:Mar 27 2021
rst:0xc (RTC_SW_CPU_RST),boot:0x2b (SPI_FAST_FLASH_BOOT)
Saved PC:0x4037806d
  #0  0x4037806d in esp_restart_noos at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/esp_system/port/soc/esp32s3/system_internal.c:158 (discriminator 1)

SPIWP:0xee
mode:DIO, clock div:1
load:0x3fce3818,len:0x508
load:0x403c9700,len:0x4
load:0x403c9704,len:0xad0
load:0x403cc700,len:0x29e4
entry 0x403c9880
E (372) ADC: CONFLICT! driver_ng is not allowed to be used with the legacy driver

abort() was called at PC 0x4207e90b on core 0

Backtrace: 0x40378402:0x3fceb200 0x40381455:0x3fceb220 0x403873dd:0x3fceb240 0x4207e90b:0x3fceb2c0 0x4208a85e:0x3fceb2e0 0x40377d2b:0x3fceb310 0x403cd852:0x3fceb340 0x403cdafe:0x3fceb380 0x403c98d5:0x3fceb4b0 0x40045c01:0x3fceb570 0x40043ab6:0x3fceb6f0 0x40034c45:0x3fceb710
  #0  0x40378402 in panic_abort at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/esp_system/panic.c:466
  #1  0x40381455 in esp_system_abort at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/esp_system/port/esp_system_chip.c:84
  #2  0x403873dd in abort at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/newlib/abort.c:38
  #3  0x4207e90b in check_adc_oneshot_driver_conflict at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/driver/deprecated/adc_legacy.c:930 (discriminator 3)
  #4  0x4208a85e in do_global_ctors at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/esp_system/startup.c:205
      (inlined by) start_cpu0_default at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/esp_system/startup.c:486
  #5  0x40377d2b in call_start_cpu0 at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/esp_system/port/cpu_start.c:706

platformio.ini fragment:

[env]
; see https://github.com/platformio/platform-espressif32/issues/1225#issuecomment-2134975344
platform = espressif32@6.7.0
platform_packages=
  framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#3.0.1
  framework-arduinoespressif32-libs @ https://github.com/espressif/arduino-esp32/releases/download/3.0.1/esp32-arduino-libs-3.0.1.zip
framework = arduino