Skip to content

Commit 0b6a922

Browse files
committed
Update ADC to latest ESP32 api
1 parent 53360e2 commit 0b6a922

12 files changed

+405
-94
lines changed

CMake/Modules/FindSystem.Device.Adc.cmake

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,27 @@ list(APPEND System.Device.Adc_INCLUDE_DIRS ${BASE_PATH_FOR_THIS_MODULE})
1616
list(APPEND System.Device.Adc_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/src/System.Device.Adc)
1717

1818
# source files
19+
# ESP32 uses legacy ADC driver for now as there is a dependency to legacy I2S due to internal DAC on ESP32
20+
if(${TARGET_SERIES_SHORT} STREQUAL "esp32")
1921
set(System.Device.Adc_SRCS
2022

2123
sys_dev_adc_native.cpp
2224

25+
sys_dev_adc_native_System_Device_Adc_AdcChannel_legacy.cpp
26+
sys_dev_adc_native_System_Device_Adc_AdcController_legacy.cpp
27+
28+
target_system_device_adc_config.cpp
29+
)
30+
else()
31+
set(System.Device.Adc_SRCS
32+
sys_dev_adc_native.cpp
33+
2334
sys_dev_adc_native_System_Device_Adc_AdcChannel.cpp
2435
sys_dev_adc_native_System_Device_Adc_AdcController.cpp
2536

2637
target_system_device_adc_config.cpp
2738
)
39+
endif()
2840

2941
foreach(SRC_FILE ${System.Device.Adc_SRCS})
3042

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: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,13 @@ int8_t Esp32_LED_DevicePinMap[6] = {
5656
-1, // 6
5757
};
5858

59-
// We use "ADC1" for 20 logical channels
60-
// Mapped to ESP32 controllers
59+
// We use "ADC1" for 7 logical channels
60+
// Mapped to ESP32_C6 controllers
6161
// 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
6562
int8_t Esp32_ADC_DevicePinMap[7] = {
6663
// 0 1 2 3 4 5
67-
0, 1, 2, 3, 4, 5, 6};
64+
0, 1, 2, 3, 4, 5, 6
65+
};
6866

6967
// I2S
7068
// 1 device I2S1

targets/ESP32/_common/ESP32_H2_DeviceMapping.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,12 @@ int8_t Esp32_LED_DevicePinMap[TARGET_LED_NUM_PINS] = {
5858
-1, // 8
5959
};
6060

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
61+
// We use "ADC1" for 5 logical channels
62+
// Mapped to ESP32_H2 controllers
63+
// ESP32_H2 ADC1 channels 1 - 5
6864
int8_t Esp32_ADC_DevicePinMap[TARGET_ADC_NUM_PINS] = {
69-
// 0 1 2 3 4 5
70-
};
65+
1, 2, 3, 4, 5
66+
};
7167

7268
// I2S
7369
// 1 device I2S1

targets/ESP32/_common/ESP32_P4_DeviceMapping.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,17 @@ int8_t Esp32_LED_DevicePinMap[8] = {
5959
-1 // 8
6060
};
6161

62+
// ESP32_P4 ADC1 with 8 channels (GPIO 16,17,18,19,20,21,22,23)
63+
// plus 6 channels (GPIO 49,50,51,52,53,54)
64+
6265
// ESP32P4 ADC1 channels 0 - 9
6366
// ADC2 channels 10 - 19
6467
int8_t Esp32_ADC_DevicePinMap[TARGET_ADC_NUM_PINS] = {
6568
// 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,
69+
16,17,18,19,20,21,22,23
6870
// 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};
71+
49,50,51,52,53,54
72+
};
7173

7274
// I2S
7375
// 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 dependcy because of internal DAC
68+
// 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: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,20 @@
55
//
66

77
#include <sys_dev_adc_native_target.h>
8+
#include <Esp32_DeviceMapping.h>
89

9-
#if defined(IDF_TARGET_ESP32)
10-
extern "C" uint8_t temprature_sens_read();
10+
#if defined(CONFIG_IDF_TARGET_ESP32)
11+
extern "C" uint8_t temperature_sens_read();
1112
#endif
1213

14+
adc_oneshot_unit_handle_t GetAdcHandle(adc_unit_t unit);
15+
1316
HRESULT Library_sys_dev_adc_native_System_Device_Adc_AdcChannel::NativeReadValue___I4(CLR_RT_StackFrame &stack)
1417
{
1518
NANOCLR_HEADER();
1619

1720
int channelNumber;
18-
int adcNumber;
21+
adc_unit_t adcNumber;
1922
int reading = 0;
2023

2124
// get a pointer to the managed object instance and check that it's not NULL
@@ -24,48 +27,46 @@ HRESULT Library_sys_dev_adc_native_System_Device_Adc_AdcChannel::NativeReadValue
2427

2528
// Get channel from _channelNumber field
2629
channelNumber = pThis[FIELD___channelNumber].NumericByRef().s4;
30+
if (channelNumber < 0 || channelNumber > TARGET_ADC_NUM_PINS)
31+
{
32+
NANOCLR_SET_AND_LEAVE(CLR_E_INVALID_PARAMETER);
33+
}
2734

2835
// Calculate internal ADC number based on channel number, 0->(CONFIG_SOC_ADC_MAX_CHANNEL_NUM - 1)
29-
adcNumber = channelNumber < CONFIG_SOC_ADC_MAX_CHANNEL_NUM ? 1 : 2;
36+
adcNumber = channelNumber < CONFIG_SOC_ADC_MAX_CHANNEL_NUM ? ADC_UNIT_1 : ADC_UNIT_2;
3037

31-
if (adcNumber == 1)
38+
#if defined(CONFIG_IDF_TARGET_ESP32)
39+
// Handle internal channels for ESP32 only
40+
if (adcNumber == ADC_UNIT_1 && (channelNumber == 8 || channelNumber == 9))
3241
{
33-
switch (channelNumber)
42+
if (channelNumber == 8)
3443
{
35-
36-
#if defined(IDF_TARGET_ESP32)
37-
case 8:
38-
reading = temperature_sens_read();
39-
break;
40-
41-
case 9:
42-
reading = hall_sensor_read();
43-
break;
44-
#endif
45-
46-
default:
47-
reading = adc1_get_raw((adc1_channel_t)channelNumber);
48-
break;
44+
//reading = temperature_sens_read();
45+
reading = 0;
46+
}
47+
else
48+
{
49+
// Hall sensor no longer available
50+
NANOCLR_SET_AND_LEAVE(CLR_E_INVALID_PARAMETER);
4951
}
5052
}
51-
#if (CONFIG_SOC_ADC_PERIPH_NUM >= 2)
52-
else if (adcNumber == 2)
53-
{
54-
// Adjust channel number for ADC2
55-
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);
53+
else
54+
#endif
55+
{
56+
if (adcNumber == ADC_UNIT_2)
57+
{
58+
// Adjust channel number for ADC2
59+
channelNumber -= CONFIG_SOC_ADC_MAX_CHANNEL_NUM;
60+
}
5761

58-
if (result != ESP_OK)
62+
// Read the value
63+
if (adc_oneshot_read(GetAdcHandle(adcNumber), (adc_channel_t)channelNumber, &reading) != ESP_OK)
5964
{
6065
NANOCLR_SET_AND_LEAVE(CLR_E_PIN_UNAVAILABLE);
6166
}
6267
}
63-
#endif
64-
else
65-
{
66-
NANOCLR_SET_AND_LEAVE(CLR_E_INVALID_PARAMETER);
67-
}
6868

69+
// Return value to the managed application
6970
stack.SetResult_I4(reading);
7071

7172
NANOCLR_NOCLEANUP();
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
4+
// See LICENSE file in the project root for full license information.
5+
//
6+
7+
#include <sys_dev_adc_native_target.h>
8+
9+
#if defined(IDF_TARGET_ESP32)
10+
extern "C" uint8_t temprature_sens_read();
11+
#endif
12+
13+
HRESULT Library_sys_dev_adc_native_System_Device_Adc_AdcChannel::NativeReadValue___I4(CLR_RT_StackFrame &stack)
14+
{
15+
NANOCLR_HEADER();
16+
17+
int channelNumber;
18+
int adcNumber;
19+
int reading = 0;
20+
21+
// get a pointer to the managed object instance and check that it's not NULL
22+
CLR_RT_HeapBlock *pThis = stack.This();
23+
FAULT_ON_NULL(pThis);
24+
25+
// Get channel from _channelNumber field
26+
channelNumber = pThis[FIELD___channelNumber].NumericByRef().s4;
27+
28+
// Calculate internal ADC number based on channel number, 0->(CONFIG_SOC_ADC_MAX_CHANNEL_NUM - 1)
29+
adcNumber = channelNumber < CONFIG_SOC_ADC_MAX_CHANNEL_NUM ? 1 : 2;
30+
31+
if (adcNumber == 1)
32+
{
33+
switch (channelNumber)
34+
{
35+
36+
#if defined(IDF_TARGET_ESP32)
37+
case 8:
38+
reading = temperature_sens_read();
39+
break;
40+
41+
case 9:
42+
reading = hall_sensor_read();
43+
break;
44+
#endif
45+
46+
default:
47+
reading = adc1_get_raw((adc1_channel_t)channelNumber);
48+
break;
49+
}
50+
}
51+
#if (CONFIG_SOC_ADC_PERIPH_NUM >= 2)
52+
else if (adcNumber == 2)
53+
{
54+
// Adjust channel number for ADC2
55+
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);
57+
58+
if (result != ESP_OK)
59+
{
60+
NANOCLR_SET_AND_LEAVE(CLR_E_PIN_UNAVAILABLE);
61+
}
62+
}
63+
#endif
64+
else
65+
{
66+
NANOCLR_SET_AND_LEAVE(CLR_E_INVALID_PARAMETER);
67+
}
68+
69+
stack.SetResult_I4(reading);
70+
71+
NANOCLR_NOCLEANUP();
72+
}
73+
74+
HRESULT Library_sys_dev_adc_native_System_Device_Adc_AdcChannel::NativeDisposeChannel___VOID(CLR_RT_StackFrame &stack)
75+
{
76+
(void)stack;
77+
78+
NANOCLR_HEADER();
79+
80+
// left empty on purpose, nothing to do here
81+
82+
NANOCLR_NOCLEANUP_NOLABEL();
83+
}

0 commit comments

Comments
 (0)