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+
1316HRESULT 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 ();
0 commit comments