Skip to content

Commit 4ffe33c

Browse files
authored
Update ADC to latest ESP32 api and enable ADC for esp32_H2 & C6 (#3216)
1 parent 5a03562 commit 4ffe33c

10 files changed

+324
-50
lines changed

CMake/Modules/FindSystem.Device.Adc.cmake

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ list(APPEND System.Device.Adc_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/src/System.Device
1717

1818
# source files
1919
set(System.Device.Adc_SRCS
20-
2120
sys_dev_adc_native.cpp
2221

2322
sys_dev_adc_native_System_Device_Adc_AdcChannel.cpp

CMake/binutils.ESP32.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,7 @@ macro(nf_add_idf_as_library)
687687
esp_netif
688688
esp_eth
689689
esp_psram
690+
esp_adc
690691
littlefs
691692
)
692693

@@ -702,6 +703,7 @@ macro(nf_add_idf_as_library)
702703
idf::esp_netif
703704
idf::esp_eth
704705
idf::esp_psram
706+
idf::esp_adc
705707
idf::littlefs
706708
)
707709

CMake/riscv-esp32c6.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"SUPPORT_ANY_BASE_CONVERSION": "ON",
2626
"API_System.Net": "ON",
2727
"API_System.Math": "ON",
28-
"API_System.Device.Adc": "OFF",
28+
"API_System.Device.Adc": "ON",
2929
"API_System.Device.Gpio": "ON",
3030
"API_System.Device.I2c": "ON",
3131
"API_System.Device.I2c.Slave": "ON",

CMake/riscv-esp32h2.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"SUPPORT_ANY_BASE_CONVERSION": "ON",
2626
"API_System.Net": "ON",
2727
"API_System.Math": "ON",
28-
"API_System.Device.Adc": "OFF",
28+
"API_System.Device.Adc": "ON",
2929
"API_System.Device.Gpio": "ON",
3030
"API_System.Device.I2c": "ON",
3131
"API_System.Device.I2c.Slave": "ON",

targets/ESP32/_common/ESP32_C6_DeviceMapping.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ int8_t Esp32_SPI_DevicePinMap[MAX_SPI_DEVICES][Esp32SpiPin_Max] = {
2323
// others assign as NONE because the default pins can be shared with serial flash and PSRAM
2424
int8_t Esp32_SERIAL_DevicePinMap[UART_NUM_MAX][Esp32SerialPin_Max] = {
2525
// COM 1 - pins 21, 20
26-
{UART_NUM_0_TXD_DIRECT_GPIO_NUM,
27-
UART_NUM_0_RXD_DIRECT_GPIO_NUM,
28-
UART_PIN_NO_CHANGE,
29-
UART_PIN_NO_CHANGE},
26+
{UART_NUM_0_TXD_DIRECT_GPIO_NUM, UART_NUM_0_RXD_DIRECT_GPIO_NUM, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE},
3027

3128
#if defined(UART_NUM_2)
3229
// COM 2 - all set to UART_PIN_NO_CHANGE
@@ -56,15 +53,18 @@ int8_t Esp32_LED_DevicePinMap[6] = {
5653
-1, // 6
5754
};
5855

59-
// We use "ADC1" for 20 logical channels
60-
// Mapped to ESP32 controllers
56+
// We use "ADC1" for 7 logical channels
57+
// Mapped to ESP32_C6 controllers
6158
// ESP32 ADC1 channels 0 - 6
62-
// " ADC1 channel 7 - Internal Temperature sensor ?
63-
// " ADC1 channel 8 - vdd33 ?
64-
// TODO review ADC channels for ESP32_C3 , Internal Temperature sensor (VP) & Vdd33
6559
int8_t Esp32_ADC_DevicePinMap[7] = {
66-
// 0 1 2 3 4 5
67-
0, 1, 2, 3, 4, 5, 6};
60+
// 0 1 2 3 4 5
61+
0,
62+
1,
63+
2,
64+
3,
65+
4,
66+
5,
67+
6};
6868

6969
// I2S
7070
// 1 device I2S1

targets/ESP32/_common/ESP32_H2_DeviceMapping.cpp

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ int8_t Esp32_SPI_DevicePinMap[MAX_SPI_DEVICES][Esp32SpiPin_Max] = {
2323
// others assign as NONE because the default pins can be shared with serial flash and PSRAM
2424
int8_t Esp32_SERIAL_DevicePinMap[UART_NUM_MAX][Esp32SerialPin_Max] = {
2525
// COM 1 - pins 21, 20
26-
{UART_NUM_0_TXD_DIRECT_GPIO_NUM,
27-
UART_NUM_0_RXD_DIRECT_GPIO_NUM,
28-
UART_PIN_NO_CHANGE,
29-
UART_PIN_NO_CHANGE},
26+
{UART_NUM_0_TXD_DIRECT_GPIO_NUM, UART_NUM_0_RXD_DIRECT_GPIO_NUM, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE},
3027

3128
#if defined(UART_NUM_2)
3229
// COM 2 - all set to UART_PIN_NO_CHANGE
@@ -56,18 +53,12 @@ int8_t Esp32_LED_DevicePinMap[TARGET_LED_NUM_PINS] = {
5653
-1, // 6
5754
-1, // 7
5855
-1, // 8
59-
};
56+
};
6057

61-
// We use "ADC1" for 20 logical channels
62-
// Mapped to ESP32 controllers
63-
// ESP32 ADC1 channels 0 - 7
64-
// " ADC1 channel 8 - Internal Temperature sensor (VP)
65-
// " ADC1 channel 9 - Internal Hall Sensor (VN)
66-
// " ADC2 channels 10 - 19
67-
// TODO review ADC channels for ESP32_C3
68-
int8_t Esp32_ADC_DevicePinMap[TARGET_ADC_NUM_PINS] = {
69-
// 0 1 2 3 4 5
70-
};
58+
// We use "ADC1" for 5 logical channels
59+
// Mapped to ESP32_H2 controllers
60+
// ESP32_H2 ADC1 channels 1 - 5
61+
int8_t Esp32_ADC_DevicePinMap[TARGET_ADC_NUM_PINS] = {1, 2, 3, 4, 5};
7162

7263
// I2S
7364
// 1 device I2S1

targets/ESP32/_common/ESP32_P4_DeviceMapping.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@ int8_t Esp32_SPI_DevicePinMap[MAX_SPI_DEVICES][Esp32SpiPin_Max] = {
2424
// others assign as NONE because the default pins can be shared with serial flash and PSRAM
2525
int8_t Esp32_SERIAL_DevicePinMap[UART_NUM_MAX][Esp32SerialPin_Max] = {
2626
// COM 1 - pins 21, 20
27-
{UART_NUM_0_TXD_DIRECT_GPIO_NUM,
28-
UART_NUM_0_RXD_DIRECT_GPIO_NUM,
29-
UART_PIN_NO_CHANGE,
30-
UART_PIN_NO_CHANGE},
27+
{UART_NUM_0_TXD_DIRECT_GPIO_NUM, UART_NUM_0_RXD_DIRECT_GPIO_NUM, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE},
3128

3229
#if defined(UART_NUM_2)
3330
// COM 2 - all set to UART_PIN_NO_CHANGE
@@ -59,15 +56,30 @@ int8_t Esp32_LED_DevicePinMap[8] = {
5956
-1 // 8
6057
};
6158

59+
// Under nanoframework we treat all ADC channels as 1 logical ADC unit (ADC1)
60+
// ESP32_P4 ADC1 with 8 channels (GPIO 16,17,18,19,20,21,22,23)
61+
// plus 6 channels (GPIO 49,50,51,52,53,54)
62+
63+
// From IDF point of view there is ADC1 and ADC2
6264
// ESP32P4 ADC1 channels 0 - 9
6365
// ADC2 channels 10 - 19
6466
int8_t Esp32_ADC_DevicePinMap[TARGET_ADC_NUM_PINS] = {
6567
// ADC1
66-
ADC1_CHANNEL_0_GPIO_NUM, ADC1_CHANNEL_1_GPIO_NUM, ADC1_CHANNEL_2_GPIO_NUM, ADC1_CHANNEL_3_GPIO_NUM,
67-
ADC1_CHANNEL_4_GPIO_NUM, ADC1_CHANNEL_5_GPIO_NUM, ADC1_CHANNEL_6_GPIO_NUM, ADC1_CHANNEL_7_GPIO_NUM,
68+
16,
69+
17,
70+
18,
71+
19,
72+
20,
73+
21,
74+
22,
75+
23,
6876
// ADC2
69-
ADC2_CHANNEL_0_GPIO_NUM, ADC2_CHANNEL_1_GPIO_NUM, ADC2_CHANNEL_2_GPIO_NUM, ADC2_CHANNEL_3_GPIO_NUM,
70-
ADC2_CHANNEL_4_GPIO_NUM, ADC2_CHANNEL_5_GPIO_NUM};
77+
49,
78+
50,
79+
51,
80+
52,
81+
53,
82+
54};
7183

7284
// I2S
7385
// 1 device I2S1

targets/ESP32/_include/esp32_idf.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,13 @@
6363
#include <driver/uart.h>
6464
#include <driver/ledc.h>
6565

66+
#if defined(CONFIG_IDF_TARGET_ESP32)
67+
// Use legacy ADC driver for ESP32 for now as the new one also requires the new I2S driver due to dependency because of
68+
// internal DAC other ESP32 variants don't have DAC so use the new ADC driver
6669
#include <driver/adc.h>
67-
// #include <esp_adc/adc_oneshot.h>
70+
#else
71+
#include <esp_adc/adc_oneshot.h>
72+
#endif
6873

6974
#if defined(CONFIG_IDF_TARGET_ESP32) || defined(CONFIG_IDF_TARGET_ESP32S2)
7075
#include <driver/dac_oneshot.h>

targets/ESP32/_nanoCLR/System.Device.Adc/sys_dev_adc_native_System_Device_Adc_AdcChannel.cpp

Lines changed: 86 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
//
66

77
#include <sys_dev_adc_native_target.h>
8+
#include <Esp32_DeviceMapping.h>
9+
10+
// The ESP32 still uses the legacy ADC driver for now as dependency between ADC and I2S due to internal DAC etc
11+
#if defined(CONFIG_IDF_TARGET_ESP32)
812

9-
#if defined(IDF_TARGET_ESP32)
1013
extern "C" uint8_t temprature_sens_read();
11-
#endif
1214

1315
HRESULT Library_sys_dev_adc_native_System_Device_Adc_AdcChannel::NativeReadValue___I4(CLR_RT_StackFrame &stack)
1416
{
@@ -33,13 +35,14 @@ HRESULT Library_sys_dev_adc_native_System_Device_Adc_AdcChannel::NativeReadValue
3335
switch (channelNumber)
3436
{
3537

36-
#if defined(IDF_TARGET_ESP32)
38+
#if defined(CONFIG_IDF_TARGET_ESP32)
3739
case 8:
38-
reading = temperature_sens_read();
40+
reading = temprature_sens_read();
3941
break;
4042

4143
case 9:
42-
reading = hall_sensor_read();
44+
// Hall sensor no longer available in IDF 5.x
45+
NANOCLR_SET_AND_LEAVE(CLR_E_INVALID_PARAMETER);
4346
break;
4447
#endif
4548

@@ -53,7 +56,8 @@ HRESULT Library_sys_dev_adc_native_System_Device_Adc_AdcChannel::NativeReadValue
5356
{
5457
// Adjust channel number for ADC2
5558
channelNumber -= CONFIG_SOC_ADC_MAX_CHANNEL_NUM;
56-
esp_err_t result = adc2_get_raw((adc2_channel_t)channelNumber, (adc_bits_width_t)SOC_ADC_RTC_MAX_BITWIDTH, &reading);
59+
esp_err_t result =
60+
adc2_get_raw((adc2_channel_t)channelNumber, (adc_bits_width_t)SOC_ADC_RTC_MAX_BITWIDTH, &reading);
5761

5862
if (result != ESP_OK)
5963
{
@@ -81,3 +85,79 @@ HRESULT Library_sys_dev_adc_native_System_Device_Adc_AdcChannel::NativeDisposeCh
8185

8286
NANOCLR_NOCLEANUP_NOLABEL();
8387
}
88+
89+
#else // !defined(CONFIG_IDF_TARGET_ESP32)
90+
91+
adc_oneshot_unit_handle_t GetAdcHandle(adc_unit_t unit);
92+
93+
HRESULT Library_sys_dev_adc_native_System_Device_Adc_AdcChannel::NativeReadValue___I4(CLR_RT_StackFrame &stack)
94+
{
95+
NANOCLR_HEADER();
96+
97+
int channelNumber;
98+
adc_unit_t adcNumber;
99+
int reading = 0;
100+
101+
// get a pointer to the managed object instance and check that it's not NULL
102+
CLR_RT_HeapBlock *pThis = stack.This();
103+
FAULT_ON_NULL(pThis);
104+
105+
// Get channel from _channelNumber field
106+
channelNumber = pThis[FIELD___channelNumber].NumericByRef().s4;
107+
if (channelNumber < 0 || channelNumber > TARGET_ADC_NUM_PINS)
108+
{
109+
NANOCLR_SET_AND_LEAVE(CLR_E_INVALID_PARAMETER);
110+
}
111+
112+
// Calculate internal ADC number based on channel number, 0->(CONFIG_SOC_ADC_MAX_CHANNEL_NUM - 1)
113+
adcNumber = channelNumber < CONFIG_SOC_ADC_MAX_CHANNEL_NUM ? ADC_UNIT_1 : ADC_UNIT_2;
114+
115+
#if defined(CONFIG_IDF_TARGET_ESP32)
116+
// Handle internal channels for ESP32 only
117+
if (adcNumber == ADC_UNIT_1 && (channelNumber == 8 || channelNumber == 9))
118+
{
119+
if (channelNumber == 8)
120+
{
121+
// reading = temperature_sens_read();
122+
reading = 0;
123+
}
124+
else
125+
{
126+
// Hall sensor no longer available
127+
NANOCLR_SET_AND_LEAVE(CLR_E_INVALID_PARAMETER);
128+
}
129+
}
130+
else
131+
#endif
132+
{
133+
if (adcNumber == ADC_UNIT_2)
134+
{
135+
// Adjust channel number for ADC2
136+
channelNumber -= CONFIG_SOC_ADC_MAX_CHANNEL_NUM;
137+
}
138+
139+
// Read the value
140+
if (adc_oneshot_read(GetAdcHandle(adcNumber), (adc_channel_t)channelNumber, &reading) != ESP_OK)
141+
{
142+
NANOCLR_SET_AND_LEAVE(CLR_E_PIN_UNAVAILABLE);
143+
}
144+
}
145+
146+
// Return value to the managed application
147+
stack.SetResult_I4(reading);
148+
149+
NANOCLR_NOCLEANUP();
150+
}
151+
152+
HRESULT Library_sys_dev_adc_native_System_Device_Adc_AdcChannel::NativeDisposeChannel___VOID(CLR_RT_StackFrame &stack)
153+
{
154+
(void)stack;
155+
156+
NANOCLR_HEADER();
157+
158+
// left empty on purpose, nothing to do here
159+
160+
NANOCLR_NOCLEANUP_NOLABEL();
161+
}
162+
163+
#endif // !defined(CONFIG_IDF_TARGET_ESP32)

0 commit comments

Comments
 (0)