|
13 | 13 | // limitations under the License.
|
14 | 14 |
|
15 | 15 | #include "esp32-hal-adc.h"
|
16 |
| -#include "driver/adc.h" |
17 |
| -#include "esp_adc_cal.h" |
18 | 16 |
|
19 |
| -#if SOC_DAC_SUPPORTED //ESP32, ESP32S2 |
20 |
| -#include "soc/dac_channel.h" |
21 |
| -#include "soc/sens_reg.h" |
22 |
| -#include "soc/rtc_io_reg.h" |
23 |
| -#endif |
| 17 | +#if SOC_ADC_SUPPORTED |
| 18 | +#include "esp32-hal.h" |
| 19 | +#include "esp32-hal-periman.h" |
| 20 | +#include "esp_adc/adc_oneshot.h" |
| 21 | +#include "esp_adc/adc_cali_scheme.h" |
24 | 22 |
|
25 |
| -#define DEFAULT_VREF 1100 |
| 23 | +static uint8_t __analogAttenuation = ADC_11db; |
| 24 | +static uint8_t __analogWidth = SOC_ADC_RTC_MAX_BITWIDTH; |
| 25 | +static uint8_t __analogReturnedWidth = SOC_ADC_RTC_MAX_BITWIDTH; |
26 | 26 |
|
27 |
| -static uint8_t __analogAttenuation = 3;//11db |
28 |
| -static uint8_t __analogWidth = ADC_WIDTH_MAX - 1; //3 for ESP32/ESP32C3; 4 for ESP32S2 |
29 |
| -static uint8_t __analogReturnedWidth = SOC_ADC_RTC_MAX_BITWIDTH; //12 for ESP32/ESP32C3; 13 for ESP32S2 |
30 |
| -static uint8_t __analogClockDiv = 1; |
31 |
| -static adc_attenuation_t __pin_attenuation[SOC_GPIO_PIN_COUNT]; |
| 27 | +adc_oneshot_unit_handle_t adc_handle[SOC_ADC_PERIPH_NUM]; |
| 28 | +adc_cali_handle_t adc_cali_handle[SOC_ADC_PERIPH_NUM]; |
32 | 29 |
|
33 |
| -static uint16_t __analogVRef = 0; |
34 |
| -#if CONFIG_IDF_TARGET_ESP32 |
35 |
| -static uint8_t __analogVRefPin = 0; |
36 |
| -#endif |
| 30 | +static bool adcDetachBus(void * pin){ |
| 31 | + adc_channel_t adc_channel; |
| 32 | + adc_unit_t adc_unit; |
| 33 | + uint8_t used_channels = 0; |
| 34 | + |
| 35 | + adc_oneshot_io_to_channel((int)(pin-1), &adc_unit, &adc_channel); |
| 36 | + for (uint8_t channel = 0; channel < SOC_ADC_CHANNEL_NUM(adc_unit); channel++){ |
| 37 | + int io_pin; |
| 38 | + adc_oneshot_channel_to_io(adc_unit, channel, &io_pin); |
| 39 | + if(perimanGetPinBusType(io_pin) == ESP32_BUS_TYPE_ADC_ONESHOT){ |
| 40 | + used_channels++; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + if(used_channels == 1){ //only 1 channel is used |
| 45 | + esp_err_t err = adc_oneshot_del_unit(adc_handle[adc_unit]); |
| 46 | + if(err != ESP_OK){ |
| 47 | + return false; |
| 48 | + } |
| 49 | + adc_handle[adc_unit] = NULL; |
| 50 | + } |
| 51 | + return true; |
| 52 | +} |
| 53 | + |
| 54 | +esp_err_t __analogChannelConfig(adc_bitwidth_t width, adc_attenuation_t atten, int8_t pin){ |
| 55 | + esp_err_t err = ESP_OK; |
| 56 | + adc_oneshot_chan_cfg_t config = { |
| 57 | + .bitwidth = width, |
| 58 | + .atten = (atten & 3), |
| 59 | + }; |
| 60 | + if(pin == -1){ //Reconfigure all used analog pins/channels |
| 61 | + for(int adc_unit = 0 ; adc_unit < SOC_ADC_PERIPH_NUM; adc_unit++){ |
| 62 | + if(adc_handle[adc_unit] != NULL){ |
| 63 | + for (uint8_t channel = 0; channel < SOC_ADC_CHANNEL_NUM(adc_unit); channel++){ |
| 64 | + int io_pin; |
| 65 | + adc_oneshot_channel_to_io( adc_unit, channel, &io_pin); |
| 66 | + if(perimanGetPinBusType(io_pin) == ESP32_BUS_TYPE_ADC_ONESHOT){ |
| 67 | + err = adc_oneshot_config_channel(adc_handle[adc_unit], channel, &config); |
| 68 | + if(err != ESP_OK){ |
| 69 | + log_e("adc_oneshot_config_channel failed with error: %d", err); |
| 70 | + return err; |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + //ADC calibration reconfig only if all channels are updated |
| 75 | + if(adc_cali_handle[adc_unit] != NULL){ |
| 76 | + #if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED |
| 77 | + log_d("Deleting ADC_UNIT_%d cali handle",adc_unit); |
| 78 | + err = adc_cali_delete_scheme_curve_fitting(adc_cali_handle[adc_unit]); |
| 79 | + if(err != ESP_OK){ |
| 80 | + log_e("adc_cali_delete_scheme_curve_fitting failed with error: %d", err); |
| 81 | + return err; |
| 82 | + } |
| 83 | + adc_cali_curve_fitting_config_t cali_config = { |
| 84 | + .unit_id = adc_unit, |
| 85 | + .atten = atten, |
| 86 | + .bitwidth = width, |
| 87 | + }; |
| 88 | + log_d("Creating ADC_UNIT_%d curve cali handle",adc_unit); |
| 89 | + err = adc_cali_create_scheme_curve_fitting(&cali_config, &adc_cali_handle[adc_unit]); |
| 90 | + if(err != ESP_OK){ |
| 91 | + log_e("adc_cali_create_scheme_curve_fitting failed with error: %d", err); |
| 92 | + return err; |
| 93 | + } |
| 94 | + #else //ADC_CALI_SCHEME_LINE_FITTING_SUPPORTED |
| 95 | + log_d("Deleting ADC_UNIT_%d line cali handle",adc_unit); |
| 96 | + err = adc_cali_delete_scheme_line_fitting(adc_cali_handle[adc_unit]); |
| 97 | + if(err != ESP_OK){ |
| 98 | + log_e("adc_cali_delete_scheme_line_fitting failed with error: %d", err); |
| 99 | + return err; |
| 100 | + } |
| 101 | + adc_cali_line_fitting_config_t cali_config = { |
| 102 | + .unit_id = adc_unit, |
| 103 | + .atten = atten, |
| 104 | + .bitwidth = width, |
| 105 | + }; |
| 106 | + log_d("Creating ADC_UNIT_%d line cali handle",adc_unit); |
| 107 | + err = adc_cali_create_scheme_line_fitting(&cali_config, &adc_cali_handle[adc_unit]); |
| 108 | + if(err != ESP_OK){ |
| 109 | + log_e("adc_cali_create_scheme_line_fitting failed with error: %d", err); |
| 110 | + return err; |
| 111 | + } |
| 112 | + #endif |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + //make it default for next channels |
| 118 | + __analogWidth = width; |
| 119 | + __analogAttenuation = atten; |
| 120 | + } |
| 121 | + else{ //Reconfigure single channel |
| 122 | + if(perimanGetPinBusType(pin) == ESP32_BUS_TYPE_ADC_ONESHOT){ |
| 123 | + adc_channel_t channel; |
| 124 | + adc_unit_t adc_unit; |
37 | 125 |
|
38 |
| -static inline uint16_t mapResolution(uint16_t value) |
39 |
| -{ |
40 |
| - uint8_t from = __analogWidth + 9; |
41 |
| - if (from == __analogReturnedWidth) { |
| 126 | + adc_oneshot_io_to_channel(pin, &adc_unit, &channel); |
| 127 | + if(err != ESP_OK){ |
| 128 | + log_e("Pin %u is not ADC pin!", pin); |
| 129 | + return err; |
| 130 | + } |
| 131 | + err = adc_oneshot_config_channel(adc_handle[adc_unit], channel, &config); |
| 132 | + if(err != ESP_OK){ |
| 133 | + log_e("adc_oneshot_config_channel failed with error: %d", err); |
| 134 | + return err; |
| 135 | + } |
| 136 | + } |
| 137 | + else { |
| 138 | + log_e("Pin is not configured as analog channel"); |
| 139 | + } |
| 140 | + } |
| 141 | + return ESP_OK; |
| 142 | +} |
| 143 | + |
| 144 | +static inline uint16_t mapResolution(uint16_t value){ |
| 145 | + uint8_t from = __analogWidth; |
| 146 | + if (from == __analogReturnedWidth){ |
42 | 147 | return value;
|
43 | 148 | }
|
44 |
| - if (from > __analogReturnedWidth) { |
| 149 | + if (from > __analogReturnedWidth){ |
45 | 150 | return value >> (from - __analogReturnedWidth);
|
46 | 151 | }
|
47 | 152 | return value << (__analogReturnedWidth - from);
|
48 | 153 | }
|
49 | 154 |
|
50 |
| -void __analogSetClockDiv(uint8_t clockDiv){ |
51 |
| - if(!clockDiv){ |
52 |
| - clockDiv = 1; |
| 155 | +void __analogSetAttenuation(adc_attenuation_t attenuation){ |
| 156 | + if(__analogChannelConfig(__analogWidth, attenuation, -1) != ESP_OK){ |
| 157 | + log_e("__analogChannelConfig failed!"); |
53 | 158 | }
|
54 |
| - __analogClockDiv = clockDiv; |
55 |
| -#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 |
56 |
| - adc_set_clk_div(__analogClockDiv); |
57 |
| -#endif |
58 |
| -} |
59 |
| - |
60 |
| -void __analogSetAttenuation(adc_attenuation_t attenuation) |
61 |
| -{ |
62 |
| - __analogAttenuation = attenuation & 3; |
63 | 159 | }
|
64 | 160 |
|
65 | 161 | #if CONFIG_IDF_TARGET_ESP32
|
66 | 162 | void __analogSetWidth(uint8_t bits){
|
67 |
| - if(bits < 9){ |
68 |
| - bits = 9; |
69 |
| - } else if(bits > 12){ |
70 |
| - bits = 12; |
| 163 | + if(bits < SOC_ADC_RTC_MIN_BITWIDTH){ |
| 164 | + bits = SOC_ADC_RTC_MIN_BITWIDTH; |
| 165 | + } |
| 166 | + else if(bits > SOC_ADC_RTC_MAX_BITWIDTH){ |
| 167 | + bits = SOC_ADC_RTC_MAX_BITWIDTH; |
| 168 | + } |
| 169 | + if(__analogChannelConfig(bits, __analogAttenuation, -1) != ESP_OK){ |
| 170 | + log_e("__analogChannelConfig failed!"); |
71 | 171 | }
|
72 |
| - __analogWidth = bits - 9; |
73 |
| - adc1_config_width(__analogWidth); |
74 | 172 | }
|
75 | 173 | #endif
|
76 | 174 |
|
77 |
| -void __analogInit(){ |
78 |
| - static bool initialized = false; |
79 |
| - if(initialized){ |
80 |
| - return; |
81 |
| - } |
82 |
| - initialized = true; |
83 |
| - __analogSetClockDiv(__analogClockDiv); |
84 |
| -#if CONFIG_IDF_TARGET_ESP32 |
85 |
| - __analogSetWidth(__analogWidth + 9);//in bits |
86 |
| -#endif |
87 |
| - for(int i=0; i<SOC_GPIO_PIN_COUNT; i++){ |
88 |
| - __pin_attenuation[i] = ADC_ATTENDB_MAX; |
89 |
| - } |
90 |
| -} |
| 175 | +esp_err_t __analogInit(uint8_t pin, adc_channel_t channel, adc_unit_t adc_unit){ |
| 176 | + esp_err_t err = ESP_OK; |
| 177 | + if(adc_handle[adc_unit] == NULL) { |
| 178 | + adc_oneshot_unit_init_cfg_t init_config1 = { |
| 179 | + .unit_id = adc_unit, |
| 180 | + .ulp_mode = ADC_ULP_MODE_DISABLE, |
| 181 | + }; |
| 182 | + err = adc_oneshot_new_unit(&init_config1, &adc_handle[adc_unit]); |
91 | 183 |
|
92 |
| -void __analogSetPinAttenuation(uint8_t pin, adc_attenuation_t attenuation) |
93 |
| -{ |
94 |
| - int8_t channel = digitalPinToAnalogChannel(pin); |
95 |
| - if(channel < 0 || attenuation > 3){ |
96 |
| - return ; |
| 184 | + if(err != ESP_OK){ |
| 185 | + log_e("adc_oneshot_new_unit failed with error: %d", err); |
| 186 | + return err; |
| 187 | + } |
97 | 188 | }
|
98 |
| - if(channel > (SOC_ADC_MAX_CHANNEL_NUM - 1)){ |
99 |
| - adc2_config_channel_atten(channel - SOC_ADC_MAX_CHANNEL_NUM, attenuation); |
100 |
| - } else { |
101 |
| - adc1_config_channel_atten(channel, attenuation); |
| 189 | + |
| 190 | + if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_ADC_ONESHOT, (void *)(pin+1))){ |
| 191 | + adcDetachBus((void *)(pin+1)); |
| 192 | + return err; |
102 | 193 | }
|
103 |
| - __analogInit(); |
104 |
| - if((__pin_attenuation[pin] != ADC_ATTENDB_MAX) || (attenuation != __analogAttenuation)){ |
105 |
| - __pin_attenuation[pin] = attenuation; |
| 194 | + |
| 195 | + adc_oneshot_chan_cfg_t config = { |
| 196 | + .bitwidth = __analogWidth, |
| 197 | + .atten = __analogAttenuation, |
| 198 | + }; |
| 199 | + |
| 200 | + err = adc_oneshot_config_channel(adc_handle[adc_unit], channel, &config); |
| 201 | + if(err != ESP_OK){ |
| 202 | + log_e("adc_oneshot_config_channel failed with error: %d", err); |
| 203 | + return err; |
106 | 204 | }
|
| 205 | + perimanSetBusDeinit(ESP32_BUS_TYPE_ADC_ONESHOT, adcDetachBus); |
| 206 | + return ESP_OK; |
107 | 207 | }
|
108 | 208 |
|
109 |
| -bool __adcAttachPin(uint8_t pin){ |
110 |
| - int8_t channel = digitalPinToAnalogChannel(pin); |
111 |
| - if(channel < 0){ |
112 |
| - log_e("Pin %u is not ADC pin!", pin); |
113 |
| - return false; |
114 |
| - } |
115 |
| - __analogInit(); |
116 |
| - int8_t pad = digitalPinToTouchChannel(pin); |
117 |
| - if(pad >= 0){ |
118 |
| -#if CONFIG_IDF_TARGET_ESP32 |
119 |
| - uint32_t touch = READ_PERI_REG(SENS_SAR_TOUCH_ENABLE_REG); |
120 |
| - if(touch & (1 << pad)){ |
121 |
| - touch &= ~((1 << (pad + SENS_TOUCH_PAD_OUTEN2_S)) |
122 |
| - | (1 << (pad + SENS_TOUCH_PAD_OUTEN1_S)) |
123 |
| - | (1 << (pad + SENS_TOUCH_PAD_WORKEN_S))); |
124 |
| - WRITE_PERI_REG(SENS_SAR_TOUCH_ENABLE_REG, touch); |
125 |
| - } |
126 |
| -#endif |
127 |
| - } |
128 |
| -#if SOC_DAC_SUPPORTED |
129 |
| - else if(pin == DAC_CHANNEL_1_GPIO_NUM){ |
130 |
| - CLEAR_PERI_REG_MASK(RTC_IO_PAD_DAC1_REG, RTC_IO_PDAC1_XPD_DAC | RTC_IO_PDAC1_DAC_XPD_FORCE);//stop dac1 |
131 |
| - } else if(pin == DAC_CHANNEL_2_GPIO_NUM){ |
132 |
| - CLEAR_PERI_REG_MASK(RTC_IO_PAD_DAC2_REG, RTC_IO_PDAC2_XPD_DAC | RTC_IO_PDAC2_DAC_XPD_FORCE);//stop dac2 |
| 209 | +void __analogSetPinAttenuation(uint8_t pin, adc_attenuation_t attenuation){ |
| 210 | + if(__analogChannelConfig(__analogWidth, attenuation, pin) != ESP_OK) |
| 211 | + { |
| 212 | + log_e("__analogChannelConfig failed!"); |
133 | 213 | }
|
134 |
| -#endif |
135 |
| - |
136 |
| - pinMode(pin, ANALOG); |
137 |
| - __analogSetPinAttenuation(pin, (__pin_attenuation[pin] != ADC_ATTENDB_MAX)?__pin_attenuation[pin]:__analogAttenuation); |
138 |
| - return true; |
139 | 214 | }
|
140 | 215 |
|
141 |
| -void __analogReadResolution(uint8_t bits) |
142 |
| -{ |
| 216 | +void __analogReadResolution(uint8_t bits){ |
143 | 217 | if(!bits || bits > 16){
|
144 | 218 | return;
|
145 | 219 | }
|
146 | 220 | __analogReturnedWidth = bits;
|
| 221 | + |
147 | 222 | #if CONFIG_IDF_TARGET_ESP32
|
148 |
| - __analogSetWidth(bits); // hadware from 9 to 12 |
| 223 | + __analogSetWidth(bits); // hardware analog resolution from 9 to 12 |
149 | 224 | #endif
|
150 | 225 | }
|
151 | 226 |
|
152 |
| -uint16_t __analogRead(uint8_t pin) |
153 |
| -{ |
154 |
| - int8_t channel = digitalPinToAnalogChannel(pin); |
| 227 | +uint16_t __analogRead(uint8_t pin){ |
155 | 228 | int value = 0;
|
156 |
| - esp_err_t r = ESP_OK; |
157 |
| - if(channel < 0){ |
| 229 | + adc_channel_t channel; |
| 230 | + adc_unit_t adc_unit; |
| 231 | + |
| 232 | + esp_err_t err = ESP_OK; |
| 233 | + err = adc_oneshot_io_to_channel(pin, &adc_unit, &channel); |
| 234 | + if(err != ESP_OK){ |
158 | 235 | log_e("Pin %u is not ADC pin!", pin);
|
159 | 236 | return value;
|
160 | 237 | }
|
161 |
| - __adcAttachPin(pin); |
162 |
| - if(channel > (SOC_ADC_MAX_CHANNEL_NUM - 1)){ |
163 |
| - channel -= SOC_ADC_MAX_CHANNEL_NUM; |
164 |
| - r = adc2_get_raw( channel, __analogWidth, &value); |
165 |
| - if ( r == ESP_OK ) { |
166 |
| - return mapResolution(value); |
167 |
| - } else if ( r == ESP_ERR_INVALID_STATE ) { |
168 |
| - log_e("GPIO%u: %s: ADC2 not initialized yet.", pin, esp_err_to_name(r)); |
169 |
| - } else if ( r == ESP_ERR_TIMEOUT ) { |
170 |
| - log_e("GPIO%u: %s: ADC2 is in use by Wi-Fi. Please see https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/adc.html#adc-limitations for more info", pin, esp_err_to_name(r)); |
171 |
| - } else { |
172 |
| - log_e("GPIO%u: %s", pin, esp_err_to_name(r)); |
| 238 | + |
| 239 | + if(perimanGetPinBus(pin, ESP32_BUS_TYPE_ADC_ONESHOT) == NULL){ |
| 240 | + log_d("Calling __analogInit! pin = %d", pin); |
| 241 | + err = __analogInit(pin, channel, adc_unit); |
| 242 | + if(err != ESP_OK){ |
| 243 | + log_e("Analog initialization failed!"); |
| 244 | + return value; |
173 | 245 | }
|
174 |
| - } else { |
175 |
| - value = adc1_get_raw(channel); |
176 |
| - return mapResolution(value); |
177 | 246 | }
|
| 247 | + |
| 248 | + adc_oneshot_read(adc_handle[adc_unit], channel, &value); |
178 | 249 | return mapResolution(value);
|
179 | 250 | }
|
180 | 251 |
|
181 | 252 | uint32_t __analogReadMilliVolts(uint8_t pin){
|
182 |
| - int8_t channel = digitalPinToAnalogChannel(pin); |
183 |
| - if(channel < 0){ |
| 253 | + int value = 0; |
| 254 | + adc_channel_t channel; |
| 255 | + adc_unit_t adc_unit; |
| 256 | + esp_err_t err = ESP_OK; |
| 257 | + |
| 258 | + adc_oneshot_io_to_channel(pin, &adc_unit, &channel); |
| 259 | + if(err != ESP_OK){ |
184 | 260 | log_e("Pin %u is not ADC pin!", pin);
|
185 |
| - return 0; |
| 261 | + return value; |
186 | 262 | }
|
187 | 263 |
|
188 |
| - if(!__analogVRef){ |
189 |
| - if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_TP) == ESP_OK) { |
190 |
| - log_d("eFuse Two Point: Supported"); |
191 |
| - __analogVRef = DEFAULT_VREF; |
| 264 | + if(perimanGetPinBus(pin, ESP32_BUS_TYPE_ADC_ONESHOT) == NULL){ |
| 265 | + err = __analogInit(pin, channel, adc_unit); |
| 266 | + if(err != ESP_OK){ |
| 267 | + log_e("Analog initialization failed!"); |
| 268 | + return value; |
192 | 269 | }
|
193 |
| - if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_VREF) == ESP_OK) { |
194 |
| - log_d("eFuse Vref: Supported"); |
195 |
| - __analogVRef = DEFAULT_VREF; |
196 |
| - } |
197 |
| - if(!__analogVRef){ |
198 |
| - __analogVRef = DEFAULT_VREF; |
199 |
| - |
200 |
| - #if CONFIG_IDF_TARGET_ESP32 |
201 |
| - if(__analogVRefPin){ |
202 |
| - esp_adc_cal_characteristics_t chars; |
203 |
| - if(adc_vref_to_gpio(ADC_UNIT_2, __analogVRefPin) == ESP_OK){ |
204 |
| - __analogVRef = __analogRead(__analogVRefPin); |
205 |
| - esp_adc_cal_characterize(1, __analogAttenuation, __analogWidth, DEFAULT_VREF, &chars); |
206 |
| - __analogVRef = esp_adc_cal_raw_to_voltage(__analogVRef, &chars); |
207 |
| - log_d("Vref to GPIO%u: %u", __analogVRefPin, __analogVRef); |
208 |
| - } |
209 |
| - } |
210 |
| - #endif |
211 |
| - } |
212 |
| - } |
213 |
| - uint8_t unit = 1; |
214 |
| - if(channel > (SOC_ADC_MAX_CHANNEL_NUM - 1)){ |
215 |
| - unit = 2; |
216 |
| - } |
217 |
| - |
218 |
| - uint16_t adc_reading = __analogRead(pin); |
219 |
| - |
220 |
| - uint8_t atten = __analogAttenuation; |
221 |
| - if (__pin_attenuation[pin] != ADC_ATTENDB_MAX){ |
222 |
| - atten = __pin_attenuation[pin]; |
223 | 270 | }
|
224 | 271 |
|
225 |
| - esp_adc_cal_characteristics_t chars = {}; |
226 |
| - esp_adc_cal_value_t val_type = esp_adc_cal_characterize(unit, atten, __analogWidth, __analogVRef, &chars); |
227 |
| - |
228 |
| - static bool print_chars_info = true; |
229 |
| - if(print_chars_info) |
230 |
| - { |
231 |
| - if (val_type == ESP_ADC_CAL_VAL_EFUSE_TP) { |
232 |
| - log_i("ADC%u: Characterized using Two Point Value: %u\n", unit, chars.vref); |
233 |
| - } |
234 |
| - else if (val_type == ESP_ADC_CAL_VAL_EFUSE_VREF) { |
235 |
| - log_i("ADC%u: Characterized using eFuse Vref: %u\n", unit, chars.vref); |
236 |
| - } |
237 |
| - #if CONFIG_IDF_TARGET_ESP32 |
238 |
| - else if(__analogVRef != DEFAULT_VREF){ |
239 |
| - log_i("ADC%u: Characterized using Vref to GPIO%u: %u\n", unit, __analogVRefPin, chars.vref); |
240 |
| - } |
| 272 | + if(adc_cali_handle[adc_unit] == NULL){ |
| 273 | + log_d("Creating cali handle for ADC_%d", adc_unit); |
| 274 | + #if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED |
| 275 | + adc_cali_curve_fitting_config_t cali_config = { |
| 276 | + .unit_id = adc_unit, |
| 277 | + .atten = __analogAttenuation, |
| 278 | + .bitwidth = __analogWidth, |
| 279 | + }; |
| 280 | + err = adc_cali_create_scheme_curve_fitting(&cali_config, &adc_cali_handle[adc_unit]); |
| 281 | + #else //ADC_CALI_SCHEME_LINE_FITTING_SUPPORTED |
| 282 | + adc_cali_line_fitting_config_t cali_config = { |
| 283 | + .unit_id = adc_unit, |
| 284 | + .bitwidth = __analogWidth, |
| 285 | + .atten = __analogAttenuation, |
| 286 | + }; |
| 287 | + err = adc_cali_create_scheme_line_fitting(&cali_config, &adc_cali_handle[adc_unit]); |
241 | 288 | #endif
|
242 |
| - else { |
243 |
| - log_i("ADC%u: Characterized using Default Vref: %u\n", unit, chars.vref); |
| 289 | + if(err != ESP_OK){ |
| 290 | + log_e("adc_cali_create_scheme_x failed!"); |
| 291 | + return value; |
244 | 292 | }
|
245 |
| - print_chars_info = false; |
246 | 293 | }
|
247 |
| - return esp_adc_cal_raw_to_voltage((uint32_t)adc_reading, &chars); |
248 |
| -} |
249 |
| - |
250 |
| -#if CONFIG_IDF_TARGET_ESP32 |
251 | 294 |
|
252 |
| -void __analogSetVRefPin(uint8_t pin){ |
253 |
| - if(pin <25 || pin > 27){ |
254 |
| - pin = 0; |
| 295 | + err = adc_oneshot_get_calibrated_result(adc_handle[adc_unit], adc_cali_handle[adc_unit], channel, &value); |
| 296 | + if(err != ESP_OK){ |
| 297 | + log_e("adc_oneshot_get_calibrated_result failed!"); |
| 298 | + return 0; |
255 | 299 | }
|
256 |
| - __analogVRefPin = pin; |
| 300 | + return value; |
257 | 301 | }
|
258 | 302 |
|
259 |
| -#endif |
260 |
| - |
261 | 303 | extern uint16_t analogRead(uint8_t pin) __attribute__ ((weak, alias("__analogRead")));
|
262 | 304 | extern uint32_t analogReadMilliVolts(uint8_t pin) __attribute__ ((weak, alias("__analogReadMilliVolts")));
|
263 | 305 | extern void analogReadResolution(uint8_t bits) __attribute__ ((weak, alias("__analogReadResolution")));
|
264 |
| -extern void analogSetClockDiv(uint8_t clockDiv) __attribute__ ((weak, alias("__analogSetClockDiv"))); |
265 | 306 | extern void analogSetAttenuation(adc_attenuation_t attenuation) __attribute__ ((weak, alias("__analogSetAttenuation")));
|
266 | 307 | extern void analogSetPinAttenuation(uint8_t pin, adc_attenuation_t attenuation) __attribute__ ((weak, alias("__analogSetPinAttenuation")));
|
267 | 308 |
|
268 |
| -extern bool adcAttachPin(uint8_t pin) __attribute__ ((weak, alias("__adcAttachPin"))); |
269 |
| - |
270 | 309 | #if CONFIG_IDF_TARGET_ESP32
|
271 |
| -extern void analogSetVRefPin(uint8_t pin) __attribute__ ((weak, alias("__analogSetVRefPin"))); |
272 | 310 | extern void analogSetWidth(uint8_t bits) __attribute__ ((weak, alias("__analogSetWidth")));
|
273 | 311 | #endif
|
| 312 | + |
| 313 | +#endif |
0 commit comments