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)
1013extern " C" uint8_t temprature_sens_read ();
11- #endif
1214
1315HRESULT 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