Skip to content

Commit a9f6e5d

Browse files
committed
Reset ADC cmake logic and add legacy and new to same file
1 parent 6ad2c3c commit a9f6e5d

File tree

5 files changed

+217
-263
lines changed

5 files changed

+217
-263
lines changed

CMake/Modules/FindSystem.Device.Adc.cmake

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,6 @@ 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(DEFINED TARGET_SERIES_SHORT AND ${TARGET_SERIES_SHORT} STREQUAL "esp32")
21-
set(System.Device.Adc_SRCS
22-
23-
sys_dev_adc_native.cpp
24-
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()
3119
set(System.Device.Adc_SRCS
3220
sys_dev_adc_native.cpp
3321

@@ -36,7 +24,6 @@ set(System.Device.Adc_SRCS
3624

3725
target_system_device_adc_config.cpp
3826
)
39-
endif()
4027

4128
foreach(SRC_FILE ${System.Device.Adc_SRCS})
4229

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

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,86 @@
77
#include <sys_dev_adc_native_target.h>
88
#include <Esp32_DeviceMapping.h>
99

10+
// The ESP32 still uses the legacy ADC driver for now as dependency between ADC and I2S due to internal DAC etc
1011
#if defined(CONFIG_IDF_TARGET_ESP32)
11-
extern "C" uint8_t temperature_sens_read();
12+
13+
extern "C" uint8_t temprature_sens_read();
14+
15+
HRESULT Library_sys_dev_adc_native_System_Device_Adc_AdcChannel::NativeReadValue___I4(CLR_RT_StackFrame &stack)
16+
{
17+
NANOCLR_HEADER();
18+
19+
int channelNumber;
20+
int adcNumber;
21+
int reading = 0;
22+
23+
// get a pointer to the managed object instance and check that it's not NULL
24+
CLR_RT_HeapBlock *pThis = stack.This();
25+
FAULT_ON_NULL(pThis);
26+
27+
// Get channel from _channelNumber field
28+
channelNumber = pThis[FIELD___channelNumber].NumericByRef().s4;
29+
30+
// Calculate internal ADC number based on channel number, 0->(CONFIG_SOC_ADC_MAX_CHANNEL_NUM - 1)
31+
adcNumber = channelNumber < CONFIG_SOC_ADC_MAX_CHANNEL_NUM ? 1 : 2;
32+
33+
if (adcNumber == 1)
34+
{
35+
switch (channelNumber)
36+
{
37+
38+
#if defined(CONFIG_IDF_TARGET_ESP32)
39+
case 8:
40+
reading = temprature_sens_read();
41+
break;
42+
43+
case 9:
44+
// Hall sensor no longer available in IDF 5.x
45+
NANOCLR_SET_AND_LEAVE(CLR_E_INVALID_PARAMETER);
46+
break;
47+
#endif
48+
49+
default:
50+
reading = adc1_get_raw((adc1_channel_t)channelNumber);
51+
break;
52+
}
53+
}
54+
#if (CONFIG_SOC_ADC_PERIPH_NUM >= 2)
55+
else if (adcNumber == 2)
56+
{
57+
// Adjust channel number for ADC2
58+
channelNumber -= CONFIG_SOC_ADC_MAX_CHANNEL_NUM;
59+
esp_err_t result =
60+
adc2_get_raw((adc2_channel_t)channelNumber, (adc_bits_width_t)SOC_ADC_RTC_MAX_BITWIDTH, &reading);
61+
62+
if (result != ESP_OK)
63+
{
64+
NANOCLR_SET_AND_LEAVE(CLR_E_PIN_UNAVAILABLE);
65+
}
66+
}
1267
#endif
68+
else
69+
{
70+
NANOCLR_SET_AND_LEAVE(CLR_E_INVALID_PARAMETER);
71+
}
72+
73+
stack.SetResult_I4(reading);
74+
75+
NANOCLR_NOCLEANUP();
76+
}
77+
78+
HRESULT Library_sys_dev_adc_native_System_Device_Adc_AdcChannel::NativeDisposeChannel___VOID(CLR_RT_StackFrame &stack)
79+
{
80+
(void)stack;
81+
82+
NANOCLR_HEADER();
83+
84+
// left empty on purpose, nothing to do here
85+
86+
NANOCLR_NOCLEANUP_NOLABEL();
87+
}
88+
89+
#else // !defined(CONFIG_IDF_TARGET_ESP32)
1390

1491
adc_oneshot_unit_handle_t GetAdcHandle(adc_unit_t unit);
1592

@@ -82,3 +159,5 @@ HRESULT Library_sys_dev_adc_native_System_Device_Adc_AdcChannel::NativeDisposeCh
82159

83160
NANOCLR_NOCLEANUP_NOLABEL();
84161
}
162+
163+
#endif // !defined(CONFIG_IDF_TARGET_ESP32)

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

Lines changed: 0 additions & 85 deletions
This file was deleted.

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

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,142 @@
4444
// CONFIG_SOC_ADC_PERIPH_NUM
4545
// CONFIG_SOC_ADC_MAX_CHANNEL_NUM
4646

47+
// The ESP32 still uses the legacy ADC driver for now as dependency between ADC and I2S due to internal DAC etc
48+
#if defined(CONFIG_IDF_TARGET_ESP32)
49+
50+
HRESULT Library_sys_dev_adc_native_System_Device_Adc_AdcController::NativeOpenChannel___VOID__I4(
51+
CLR_RT_StackFrame &stack)
52+
{
53+
NANOCLR_HEADER();
54+
55+
int channelNumber;
56+
int adcUnit;
57+
esp_err_t result;
58+
// default to MAX bit width for SoC
59+
adc_bits_width_t width_bit = (adc_bits_width_t)SOC_ADC_RTC_MAX_BITWIDTH;
60+
adc_atten_t atten = ADC_ATTEN_DB_12;
61+
62+
// get a pointer to the managed object instance and check that it's not NULL
63+
CLR_RT_HeapBlock *pThis = stack.This();
64+
FAULT_ON_NULL(pThis);
65+
66+
// Get channel from argument
67+
channelNumber = stack.Arg1().NumericByRef().s4;
68+
69+
if (channelNumber < ADC_CHANNEL_0 || channelNumber > TARGET_ADC_NUM_PINS)
70+
{
71+
NANOCLR_SET_AND_LEAVE(CLR_E_INVALID_PARAMETER);
72+
}
73+
74+
// Get ADC device number from channel
75+
adcUnit = channelNumber < CONFIG_SOC_ADC_MAX_CHANNEL_NUM ? 1 : 2;
76+
77+
switch (adcUnit)
78+
{
79+
#if (CONFIG_SOC_ADC_PERIPH_NUM >= 1)
80+
case 1:
81+
// Normal channel 0-7 ?
82+
if (channelNumber <= 7)
83+
{
84+
adc1_config_width(width_bit);
85+
86+
result = adc1_config_channel_atten((adc1_channel_t)channelNumber, atten);
87+
if (result != ESP_OK)
88+
{
89+
NANOCLR_SET_AND_LEAVE(CLR_E_PIN_UNAVAILABLE);
90+
}
91+
}
92+
break;
93+
#endif
94+
95+
#if (CONFIG_SOC_ADC_PERIPH_NUM >= 2)
96+
case 2:
97+
// Adjust for ADC2
98+
channelNumber -= CONFIG_SOC_ADC_MAX_CHANNEL_NUM;
99+
result = adc2_config_channel_atten((adc2_channel_t)channelNumber, atten);
100+
101+
if (result != ESP_OK)
102+
{
103+
NANOCLR_SET_AND_LEAVE(CLR_E_PIN_UNAVAILABLE);
104+
}
105+
break;
106+
#endif
107+
default:
108+
NANOCLR_SET_AND_LEAVE(CLR_E_PIN_UNAVAILABLE);
109+
}
110+
111+
NANOCLR_NOCLEANUP();
112+
}
113+
114+
HRESULT Library_sys_dev_adc_native_System_Device_Adc_AdcController::NativeGetChannelCount___I4(CLR_RT_StackFrame &stack)
115+
{
116+
NANOCLR_HEADER();
117+
118+
int channelCount = 20;
119+
120+
// Return value to the managed application
121+
stack.SetResult_I4(channelCount);
122+
123+
NANOCLR_NOCLEANUP_NOLABEL();
124+
}
125+
126+
HRESULT Library_sys_dev_adc_native_System_Device_Adc_AdcController::NativeGetMaxValue___I4(CLR_RT_StackFrame &stack)
127+
{
128+
NANOCLR_HEADER();
129+
130+
// Currently fixed 12 bit so return 4095
131+
stack.SetResult_I4(4095);
132+
133+
NANOCLR_NOCLEANUP_NOLABEL();
134+
}
135+
136+
HRESULT Library_sys_dev_adc_native_System_Device_Adc_AdcController::NativeGetMinValue___I4(CLR_RT_StackFrame &stack)
137+
{
138+
NANOCLR_HEADER();
139+
140+
// Return 0 for now, is this signed ?
141+
stack.SetResult_I4(0);
142+
143+
NANOCLR_NOCLEANUP_NOLABEL();
144+
}
145+
146+
HRESULT Library_sys_dev_adc_native_System_Device_Adc_AdcController::NativeIsChannelModeSupported___BOOLEAN__I4(
147+
CLR_RT_StackFrame &stack)
148+
{
149+
NANOCLR_HEADER();
150+
151+
int mode = stack.Arg1().NumericByRef().s4;
152+
153+
// Only support Single ended mode for now
154+
stack.SetResult_Boolean((mode == (int)AdcChannelMode_SingleEnded));
155+
156+
NANOCLR_NOCLEANUP_NOLABEL();
157+
}
158+
159+
HRESULT Library_sys_dev_adc_native_System_Device_Adc_AdcController::NativeGetResolutionInBits___I4(
160+
CLR_RT_StackFrame &stack)
161+
{
162+
NANOCLR_HEADER();
163+
164+
// Fixed at 12 bit for now
165+
stack.SetResult_I4(12);
166+
167+
NANOCLR_NOCLEANUP_NOLABEL();
168+
}
169+
170+
HRESULT Library_sys_dev_adc_native_System_Device_Adc_AdcController::NativeInit___VOID(CLR_RT_StackFrame &stack)
171+
{
172+
(void)stack;
173+
174+
NANOCLR_HEADER();
175+
176+
// all required initialization for ADC are already handled
177+
178+
NANOCLR_NOCLEANUP_NOLABEL();
179+
}
180+
181+
#else
182+
47183
adc_oneshot_unit_handle_t adc_handles[SOC_ADC_PERIPH_NUM] = {NULL};
48184

49185
adc_oneshot_unit_handle_t GetAdcHandle(adc_unit_t unit)
@@ -210,3 +346,4 @@ HRESULT Library_sys_dev_adc_native_System_Device_Adc_AdcController::NativeInit__
210346

211347
NANOCLR_NOCLEANUP_NOLABEL();
212348
}
349+
#endif // #if defined(CONFIG_IDF_TARGET_ESP32)

0 commit comments

Comments
 (0)