-
-
Notifications
You must be signed in to change notification settings - Fork 186
Update ADC to latest ESP32 api and enable ADC for esp32_H2 & C6 #3216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Update ADC to latest ESP32 api and enable ADC for esp32_H2 & C6 #3216
Conversation
WalkthroughEnables and wires ESP32 ADC support: adds esp_adc to build, toggles ADC feature flags for C6/H2, updates ESP32 device pin mappings, conditions ADC header inclusion per target, and refactors native ADC controller/channel code to a per-unit adc_oneshot flow with per-unit handles and new lifecycle/channel APIs. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
actor App as Managed App
participant Ctl as AdcController (native)
participant Map as DeviceMapping
participant IDF as ESP-IDF (adc_oneshot)
rect rgb(238,247,255)
note right of Ctl: New per-unit adc_oneshot flow (non-classic ESP32)
end
App->>Ctl: NativeInit / NativeOpenChannel(channel)
Ctl->>Map: Resolve unit, gpio, channel
Ctl->>Ctl: AdcController_Initialize(unit) [lazy init]
Ctl->>IDF: adc_oneshot_new_unit(unit)
Ctl->>IDF: adc_oneshot_config_channel(unit, channel, cfg)
App->>Ctl: NativeReadValue
Ctl->>IDF: adc_oneshot_read(unit, channel)
IDF-->>Ctl: value or error
Ctl-->>App: value or mapped error
sequenceDiagram
autonumber
actor App as Managed App
participant Ctl as AdcChannel (classic ESP32)
participant IDF as ESP-IDF legacy ADC
rect rgb(255,249,240)
note right of Ctl: Classic ESP32 path (driver/adc.h)
end
App->>Ctl: Read channel N
alt ADC1
Ctl->>IDF: adc1_get_raw(N)
else ADC2
Ctl->>IDF: adc2_get_raw(N, &value)
else Internal Temp (channel 8)
Ctl->>IDF: temprature_sens_read()
else Removed Hall (channel 9)
Ctl-->>App: CLR_E_INVALID_PARAMETER
end
IDF-->>Ctl: value or status
Ctl-->>App: value or mapped error
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🧰 Additional context used🧬 Code graph analysis (1)targets/ESP32/_nanoCLR/System.Device.Adc/sys_dev_adc_native_System_Device_Adc_AdcController.cpp (2)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (20)
🔇 Additional comments (8)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (12)
CMake/Modules/FindSystem.Device.Adc.cmake
(1 hunks)CMake/binutils.ESP32.cmake
(2 hunks)CMake/riscv-esp32c6.json
(1 hunks)CMake/riscv-esp32h2.json
(1 hunks)targets/ESP32/_common/ESP32_C6_DeviceMapping.cpp
(1 hunks)targets/ESP32/_common/ESP32_H2_DeviceMapping.cpp
(1 hunks)targets/ESP32/_common/ESP32_P4_DeviceMapping.cpp
(1 hunks)targets/ESP32/_include/esp32_idf.h
(1 hunks)targets/ESP32/_nanoCLR/System.Device.Adc/sys_dev_adc_native_System_Device_Adc_AdcChannel.cpp
(2 hunks)targets/ESP32/_nanoCLR/System.Device.Adc/sys_dev_adc_native_System_Device_Adc_AdcChannel_legacy.cpp
(1 hunks)targets/ESP32/_nanoCLR/System.Device.Adc/sys_dev_adc_native_System_Device_Adc_AdcController.cpp
(3 hunks)targets/ESP32/_nanoCLR/System.Device.Adc/sys_dev_adc_native_System_Device_Adc_AdcController_legacy.cpp
(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (4)
targets/ESP32/_nanoCLR/System.Device.Adc/sys_dev_adc_native_System_Device_Adc_AdcController_legacy.cpp (1)
targets/ESP32/_nanoCLR/System.Device.Adc/sys_dev_adc_native_System_Device_Adc_AdcController.cpp (4)
NativeOpenChannel___VOID__I4
(83-145)NativeOpenChannel___VOID__I4
(83-84)NativeGetChannelCount___I4
(147-157)NativeGetChannelCount___I4
(147-147)
targets/ESP32/_nanoCLR/System.Device.Adc/sys_dev_adc_native_System_Device_Adc_AdcChannel.cpp (2)
targets/ESP32/_nanoCLR/System.Device.Adc/sys_dev_adc_native_System_Device_Adc_AdcChannel_legacy.cpp (2)
NativeReadValue___I4
(13-67)NativeReadValue___I4
(13-13)targets/ESP32/_nanoCLR/System.Device.Adc/sys_dev_adc_native_System_Device_Adc_AdcController.cpp (2)
GetAdcHandle
(49-57)GetAdcHandle
(49-49)
targets/ESP32/_nanoCLR/System.Device.Adc/sys_dev_adc_native_System_Device_Adc_AdcController.cpp (1)
targets/ESP32/_nanoCLR/System.Device.Adc/sys_dev_adc_native_System_Device_Adc_AdcController_legacy.cpp (2)
NativeOpenChannel___VOID__I4
(35-97)NativeOpenChannel___VOID__I4
(35-36)
targets/ESP32/_nanoCLR/System.Device.Adc/sys_dev_adc_native_System_Device_Adc_AdcChannel_legacy.cpp (1)
targets/ESP32/_nanoCLR/System.Device.Adc/sys_dev_adc_native_System_Device_Adc_AdcChannel.cpp (5)
NativeReadValue___I4
(16-73)NativeReadValue___I4
(16-16)temperature_sens_read
(11-11)NativeDisposeChannel___VOID
(75-84)NativeDisposeChannel___VOID
(75-75)
🔇 Additional comments (8)
targets/ESP32/_common/ESP32_P4_DeviceMapping.cpp (1)
62-72
: LGTM: ADC pin mappings updated for ESP32_P4.The explicit GPIO mappings for ADC1 (8 channels) and ADC2 (6 channels) are correctly specified and align with the PR objectives to update ESP32 ADC integration.
Note: The comment on lines 65-66 ("ESP32P4 ADC1 channels 0 - 9, ADC2 channels 10 - 19") is inconsistent with the actual mapping (8 + 6 = 14 channels). Consider removing or updating this comment to match the new description on lines 62-64.
CMake/riscv-esp32h2.json (1)
28-28
: LGTM: ADC API enabled for ESP32-H2.The change enables
API_System.Device.Adc
for the RISCV ESP32-H2 preset, aligning with the PR objectives to enable ADC support for ESP32_H2 targets.targets/ESP32/_common/ESP32_H2_DeviceMapping.cpp (1)
61-66
: LGTM: ADC mapping updated for ESP32_H2.The explicit channel mapping (1-5) for ESP32_H2 ADC1 is correctly specified and aligns with the PR objectives to enable ADC support for ESP32_H2 targets using the oneshot API.
targets/ESP32/_common/ESP32_C6_DeviceMapping.cpp (1)
59-65
: LGTM: ADC mapping updated for ESP32_C6.The explicit channel mapping (0-6) for ESP32_C6 ADC1 is correctly specified and aligns with the PR objectives to enable ADC support for ESP32_C6 targets using the oneshot API.
CMake/riscv-esp32c6.json (1)
28-28
: LGTM: ADC API enabled for ESP32-C6.The change enables
API_System.Device.Adc
for the RISCV ESP32-C6 preset, aligning with the PR objectives to enable ADC support for ESP32_C6 targets.targets/ESP32/_include/esp32_idf.h (1)
66-72
: LGTM: Conditional ADC header inclusion for ESP32 targets.The conditional inclusion routes ESP32 through the legacy ADC driver (
driver/adc.h
) due to the dependency with the legacy I2S driver and internal DAC, while other ESP32 variants use the oneshot ADC driver (esp_adc/adc_oneshot.h
). This aligns with the PR objectives to update ADC to the latest ESP32 API while preserving legacy support for ESP32.The comment clearly explains the rationale for the conditional inclusion.
CMake/binutils.ESP32.cmake (1)
690-690
: LGTM: esp_adc component integrated into ESP32 IDF build.The additions of
esp_adc
toIDF_COMPONENTS_TO_ADD
(line 690) andidf::esp_adc
toIDF_LIBRARIES_TO_ADD
(line 706) correctly integrate the ESP32 ADC component into the IDF build and link stages, aligning with the PR objectives to update ADC to the latest ESP32 API.Also applies to: 706-706
CMake/Modules/FindSystem.Device.Adc.cmake (1)
19-39
: LGTM: Conditional ADC source selection for ESP32 targets.The conditional logic correctly routes ESP32 targets to legacy ADC source files (
sys_dev_adc_native_System_Device_Adc_AdcChannel_legacy.cpp
,sys_dev_adc_native_System_Device_Adc_AdcController_legacy.cpp
) due to the dependency with the legacy I2S driver and internal DAC, while preserving the standard non-legacy sources for other targets. This aligns with the PR objectives to update ADC to the latest ESP32 API while maintaining backward compatibility for ESP32.The comment on line 19 clearly explains the rationale for the conditional logic.
.../ESP32/_nanoCLR/System.Device.Adc/sys_dev_adc_native_System_Device_Adc_AdcChannel_legacy.cpp
Outdated
Show resolved
Hide resolved
Automated fixes for code style.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
targets/ESP32/_nanoCLR/System.Device.Adc/sys_dev_adc_native_System_Device_Adc_AdcChannel_legacy.cpp
(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
targets/ESP32/_nanoCLR/System.Device.Adc/sys_dev_adc_native_System_Device_Adc_AdcChannel_legacy.cpp (1)
targets/ESP32/_nanoCLR/System.Device.Adc/sys_dev_adc_native_System_Device_Adc_AdcChannel.cpp (4)
NativeReadValue___I4
(16-73)NativeReadValue___I4
(16-16)NativeDisposeChannel___VOID
(75-84)NativeDisposeChannel___VOID
(75-75)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (6)
- GitHub Check: nf-interpreter (Check_Code_Style)
- GitHub Check: nf-interpreter (Nightly build) (Build_STM32_targets ST_NUCLEO64_F091RC)
- GitHub Check: nf-interpreter (Nightly build) (Build_STM32_targets ORGPAL_PALX)
- GitHub Check: nf-interpreter (Nightly build) (Build_STM32_targets ORGPAL_PALTHREE)
- GitHub Check: nf-interpreter (Nightly build) (Check_Build_Options)
- GitHub Check: nf-interpreter (Check_Build_Options)
🔇 Additional comments (5)
targets/ESP32/_nanoCLR/System.Device.Adc/sys_dev_adc_native_System_Device_Adc_AdcChannel_legacy.cpp (5)
9-11
: Legacy SDK symbol correctly declared.The misspelled
temprature_sens_read()
matches the legacy ESP32 SDK's actual symbol name. The call site at line 38 correctly uses this name.
36-45
: ESP32 internal sensor handling is correct.The temperature sensor correctly calls the legacy SDK's
temprature_sens_read()
, and the Hall sensor appropriately returns an error since it's no longer available in IDF 5.x.
52-68
: ADC2 handling is correct.The ADC2 channel adjustment, legacy API call with proper bitwidth, error checking, and invalid ADC number handling are all implemented correctly.
70-72
: Result handling is correct.The reading value is properly returned to managed code using the standard stack result mechanism.
75-84
: Dispose implementation is appropriate.The no-op implementation matches the non-legacy version and is correct for the legacy ADC API, which doesn't require explicit channel cleanup.
.../ESP32/_nanoCLR/System.Device.Adc/sys_dev_adc_native_System_Device_Adc_AdcChannel_legacy.cpp
Outdated
Show resolved
Hide resolved
…be14-d2c2-4941-ac40-0d7cd5432a77 Code style fixes for nanoframework/nf-interpreter PR#3216
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
CMake/Modules/FindSystem.Device.Adc.cmake
(0 hunks)targets/ESP32/_nanoCLR/System.Device.Adc/sys_dev_adc_native_System_Device_Adc_AdcChannel.cpp
(4 hunks)targets/ESP32/_nanoCLR/System.Device.Adc/sys_dev_adc_native_System_Device_Adc_AdcController.cpp
(2 hunks)
💤 Files with no reviewable changes (1)
- CMake/Modules/FindSystem.Device.Adc.cmake
🧰 Additional context used
🧬 Code graph analysis (2)
targets/ESP32/_nanoCLR/System.Device.Adc/sys_dev_adc_native_System_Device_Adc_AdcChannel.cpp (2)
src/System.Device.Adc/sys_dev_adc_native_System_Device_Adc_AdcChannel_stubs.cpp (2)
NativeReadValue___I4
(9-16)NativeReadValue___I4
(9-9)targets/ESP32/_nanoCLR/System.Device.Adc/sys_dev_adc_native_System_Device_Adc_AdcController.cpp (2)
GetAdcHandle
(185-193)GetAdcHandle
(185-185)
targets/ESP32/_nanoCLR/System.Device.Adc/sys_dev_adc_native_System_Device_Adc_AdcController.cpp (3)
src/System.Device.Adc/sys_dev_adc_native_System_Device_Adc_AdcController_stubs.cpp (14)
NativeOpenChannel___VOID__I4
(9-17)NativeOpenChannel___VOID__I4
(9-10)NativeGetChannelCount___I4
(19-26)NativeGetChannelCount___I4
(19-19)NativeGetMaxValue___I4
(28-35)NativeGetMaxValue___I4
(28-28)NativeGetMinValue___I4
(37-44)NativeGetMinValue___I4
(37-37)NativeIsChannelModeSupported___BOOLEAN__I4
(46-54)NativeIsChannelModeSupported___BOOLEAN__I4
(46-47)NativeGetResolutionInBits___I4
(56-64)NativeGetResolutionInBits___I4
(56-57)NativeInit___VOID
(66-73)NativeInit___VOID
(66-66)targets/ChibiOS/_nanoCLR/System.Device.Adc/sys_dev_adc_native_System_Device_Adc_AdcController.cpp (14)
NativeOpenChannel___VOID__I4
(9-74)NativeOpenChannel___VOID__I4
(9-10)NativeGetChannelCount___I4
(76-84)NativeGetChannelCount___I4
(76-76)NativeGetMaxValue___I4
(86-94)NativeGetMaxValue___I4
(86-86)NativeGetMinValue___I4
(96-103)NativeGetMinValue___I4
(96-96)NativeIsChannelModeSupported___BOOLEAN__I4
(105-116)NativeIsChannelModeSupported___BOOLEAN__I4
(105-106)NativeGetResolutionInBits___I4
(118-127)NativeGetResolutionInBits___I4
(118-119)NativeInit___VOID
(129-138)NativeInit___VOID
(129-129)targets/ESP32/_nanoCLR/System.Device.Adc/sys_dev_adc_native_System_Device_Adc_AdcChannel.cpp (1)
GetAdcHandle
(91-91)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (6)
- GitHub Check: nf-interpreter (Nightly build) (Build_STM32_targets ST_NUCLEO64_F091RC)
- GitHub Check: nf-interpreter (Nightly build) (Build_STM32_targets ORGPAL_PALX)
- GitHub Check: nf-interpreter (Nightly build) (Build_STM32_targets ORGPAL_PALTHREE)
- GitHub Check: nf-interpreter (Check_Code_Style)
- GitHub Check: nf-interpreter (Nightly build) (Check_Build_Options)
- GitHub Check: nf-interpreter (Check_Build_Options)
🔇 Additional comments (8)
targets/ESP32/_nanoCLR/System.Device.Adc/sys_dev_adc_native_System_Device_Adc_AdcChannel.cpp (4)
8-11
: LGTM! Good addition of device mapping and target-specific guard.The inclusion of
Esp32_DeviceMapping.h
and theCONFIG_IDF_TARGET_ESP32
guard properly separate legacy from new ADC API paths.
38-46
: LGTM! Correct handling of deprecated internal channels.The temperature sensor read and the hall sensor deprecation (IDF 5.x removal) are properly handled for the ESP32 legacy path.
59-60
: LGTM! Improved readability.The multi-line formatting of the
adc2_get_raw
call improves readability without changing functionality.
107-110
: TARGET_ADC_NUM_PINS defined in all ESP32 device mappings Verified the macro is present in every mapping file under targets/ESP32/_common.targets/ESP32/_nanoCLR/System.Device.Adc/sys_dev_adc_native_System_Device_Adc_AdcController.cpp (4)
28-46
: LGTM! Comprehensive documentation of ADC channel layouts.The updated comments clearly document the ADC channel configurations for all ESP32 variants (ESP32, S2, S3, P4, C3, C6, H2), including the note about IDF 5.x deprecating internal sensors.
283-293
: LGTM! Dynamic channel count calculation improves maintainability.The dynamic calculation using
CONFIG_SOC_ADC_MAX_CHANNEL_NUM * CONFIG_SOC_ADC_PERIPH_NUM
correctly determines the total channel count for each target without hardcoding, making it more maintainable than the legacy path's hardcoded value of 20.
295-348
: LGTM! Native methods correctly implement ADC properties.All native methods (GetMaxValue, GetMinValue, IsChannelModeSupported, GetResolutionInBits, Init) are correctly implemented and consistent with the 12-bit ADC configuration and legacy behavior.
185-193
: Verifyadc_unit_t
signedness: confirm in the ESP-IDF headers whetheradc_unit_t
is signed; if it’s unsigned, theunit < 0
check is always false and can be removed.
targets/ESP32/_nanoCLR/System.Device.Adc/sys_dev_adc_native_System_Device_Adc_AdcChannel.cpp
Show resolved
Hide resolved
targets/ESP32/_nanoCLR/System.Device.Adc/sys_dev_adc_native_System_Device_Adc_AdcController.cpp
Show resolved
Hide resolved
…a5ff-9201-440e-ba6c-cd7e890bfd23 Code style fixes for nanoframework/nf-interpreter PR#3216
Description
This updates the IDF api for ADC from legacy to latest API before it gets removed from IDF.
Because there is a dependency between the legacy ADC and the legacy I2S was not able to upgrade the ESP32 target until the I2S driver has been updated. The reason for this is the ESP32 has an internal DAC & ADC which can be used with I2S. The other ESP32 series don't have these. It also looks like the latest API for I2S doesn't support the internal DAC and once upgraded ww will have to remove that support from managed I2S driver.
The ADC driver is now re-enabled for Esp32_C6 and ESP32_H2 as it no longer hangs on start with new API.
Motivation and Context
Start of change to update all legacy IDF api drivers and fix missing ADC from C6 & H2 targets
Part of change for nanoframework/Home#1580
How Has This Been Tested?
Tested with ESP32_S3 and ESP32_C6 targets
Types of changes
Checklist
Summary by CodeRabbit
New Features
Refactor
Chores
Style