13
13
// limitations under the License.
14
14
15
15
#include "esp32-hal-adc.h"
16
- #include "freertos/FreeRTOS.h"
17
- #include "freertos/task.h"
18
- #include "esp_attr.h"
19
- #include "soc/rtc_cntl_reg.h"
20
16
#include "driver/adc.h"
21
-
22
- #include "esp_system.h"
23
- #ifdef ESP_IDF_VERSION_MAJOR // IDF 4+
24
- #if CONFIG_IDF_TARGET_ESP32 // ESP32/PICO-D4
25
17
#include "esp_adc_cal.h"
26
- #include "soc/sens_reg.h"
27
- #include "soc/rtc_io_reg.h"
28
- #include "esp32/rom/ets_sys.h"
29
- #include "esp_intr_alloc.h"
30
- #include "soc/dac_channel.h"
31
- #define DEFAULT_VREF 1100
32
- static esp_adc_cal_characteristics_t * __analogCharacteristics [2 ] = {NULL , NULL };
33
- static uint16_t __analogVRef = 0 ;
34
- static uint8_t __analogVRefPin = 0 ;
35
- #elif CONFIG_IDF_TARGET_ESP32S2
36
- #include "esp32s2/rom/ets_sys.h"
37
- #include "soc/sens_reg.h"
38
- #include "soc/rtc_io_reg.h"
18
+
19
+ #if SOC_DAC_SUPPORTED //ESP32, ESP32S2
39
20
#include "soc/dac_channel.h"
40
- #elif CONFIG_IDF_TARGET_ESP32S3
41
- #include "esp32s3/rom/ets_sys.h"
42
21
#include "soc/sens_reg.h"
43
22
#include "soc/rtc_io_reg.h"
44
- #elif CONFIG_IDF_TARGET_ESP32C3
45
- #include "esp32c3/rom/ets_sys.h"
46
- #else
47
- #error Target CONFIG_IDF_TARGET is not supported
48
- #endif
49
- #else // ESP32 Before IDF 4.0
50
- #include "rom/ets_sys.h"
51
- #include "esp_intr.h"
52
23
#endif
53
24
25
+ #define DEFAULT_VREF 1100
26
+
54
27
static uint8_t __analogAttenuation = 3 ;//11db
55
28
static uint8_t __analogWidth = ADC_WIDTH_MAX - 1 ; //3 for ESP32/ESP32C3; 4 for ESP32S2
56
29
static uint8_t __analogReturnedWidth = SOC_ADC_MAX_BITWIDTH ; //12 for ESP32/ESP32C3; 13 for ESP32S2
57
30
static uint8_t __analogClockDiv = 1 ;
58
31
static adc_attenuation_t __pin_attenuation [SOC_GPIO_PIN_COUNT ];
59
32
33
+ static uint16_t __analogVRef = 0 ;
34
+ #if CONFIG_IDF_TARGET_ESP32
35
+ static uint8_t __analogVRefPin = 0 ;
36
+ #endif
37
+
60
38
static inline uint16_t mapResolution (uint16_t value )
61
39
{
62
40
uint8_t from = __analogWidth + 9 ;
@@ -117,8 +95,8 @@ void __analogSetPinAttenuation(uint8_t pin, adc_attenuation_t attenuation)
117
95
if (channel < 0 || attenuation > 3 ){
118
96
return ;
119
97
}
120
- if (channel > 9 ){
121
- adc2_config_channel_atten (channel - 10 , attenuation );
98
+ if (channel > ( SOC_ADC_MAX_CHANNEL_NUM - 1 ) ){
99
+ adc2_config_channel_atten (channel - SOC_ADC_MAX_CHANNEL_NUM , attenuation );
122
100
} else {
123
101
adc1_config_channel_atten (channel , attenuation );
124
102
}
@@ -181,8 +159,8 @@ uint16_t __analogRead(uint8_t pin)
181
159
return value ;
182
160
}
183
161
__adcAttachPin (pin );
184
- if (channel > 9 ){
185
- channel -= 10 ;
162
+ if (channel > ( SOC_ADC_MAX_CHANNEL_NUM - 1 ) ){
163
+ channel -= SOC_ADC_MAX_CHANNEL_NUM ;
186
164
r = adc2_get_raw ( channel , __analogWidth , & value );
187
165
if ( r == ESP_OK ) {
188
166
return mapResolution (value );
@@ -206,7 +184,7 @@ uint32_t __analogReadMilliVolts(uint8_t pin){
206
184
log_e ("Pin %u is not ADC pin!" , pin );
207
185
return 0 ;
208
186
}
209
- #if CONFIG_IDF_TARGET_ESP32
187
+
210
188
if (!__analogVRef ){
211
189
if (esp_adc_cal_check_efuse (ESP_ADC_CAL_VAL_EFUSE_TP ) == ESP_OK ) {
212
190
log_d ("eFuse Two Point: Supported" );
@@ -218,6 +196,8 @@ uint32_t __analogReadMilliVolts(uint8_t pin){
218
196
}
219
197
if (!__analogVRef ){
220
198
__analogVRef = DEFAULT_VREF ;
199
+
200
+ #if CONFIG_IDF_TARGET_ESP32
221
201
if (__analogVRefPin ){
222
202
esp_adc_cal_characteristics_t chars ;
223
203
if (adc_vref_to_gpio (ADC_UNIT_2 , __analogVRefPin ) == ESP_OK ){
@@ -227,42 +207,44 @@ uint32_t __analogReadMilliVolts(uint8_t pin){
227
207
log_d ("Vref to GPIO%u: %u" , __analogVRefPin , __analogVRef );
228
208
}
229
209
}
210
+ #endif
230
211
}
231
212
}
232
213
uint8_t unit = 1 ;
233
- if (channel > 9 ){
214
+ if (channel > ( SOC_ADC_MAX_CHANNEL_NUM - 1 ) ){
234
215
unit = 2 ;
235
216
}
217
+
236
218
uint16_t adc_reading = __analogRead (pin );
237
- if (__analogCharacteristics [unit - 1 ] == NULL ){
238
- __analogCharacteristics [unit - 1 ] = calloc (1 , sizeof (esp_adc_cal_characteristics_t ));
239
- if (__analogCharacteristics [unit - 1 ] == NULL ){
240
- return 0 ;
241
- }
242
- esp_adc_cal_value_t val_type = esp_adc_cal_characterize (unit , __analogAttenuation , __analogWidth , __analogVRef , __analogCharacteristics [unit - 1 ]);
219
+
220
+ uint8_t atten = __analogAttenuation ;
221
+ if (__pin_attenuation [pin ] != ADC_ATTENDB_MAX ){
222
+ atten = __pin_attenuation [pin ];
223
+ }
224
+
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
+ {
243
231
if (val_type == ESP_ADC_CAL_VAL_EFUSE_TP ) {
244
- log_i ("ADC%u: Characterized using Two Point Value: %u\n" , unit , __analogCharacteristics [unit - 1 ]-> vref );
245
- } else if (val_type == ESP_ADC_CAL_VAL_EFUSE_VREF ) {
246
- log_i ("ADC%u: Characterized using eFuse Vref: %u\n" , unit , __analogCharacteristics [unit - 1 ]-> vref );
247
- } else if (__analogVRef != DEFAULT_VREF ){
248
- log_i ("ADC%u: Characterized using Vref to GPIO%u: %u\n" , unit , __analogVRefPin , __analogCharacteristics [unit - 1 ]-> vref );
249
- } else {
250
- log_i ("ADC%u: Characterized using Default Vref: %u\n" , unit , __analogCharacteristics [unit - 1 ]-> vref );
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 );
251
240
}
241
+ #endif
242
+ else {
243
+ log_i ("ADC%u: Characterized using Default Vref: %u\n" , unit , chars .vref );
244
+ }
245
+ print_chars_info = false;
252
246
}
253
- return esp_adc_cal_raw_to_voltage (adc_reading , __analogCharacteristics [unit - 1 ]);
254
- #else
255
- uint16_t adc_reading = __analogRead (pin );
256
- uint16_t max_reading = 8191 ;
257
- uint16_t max_mv = 1100 ;
258
- switch (__analogAttenuation ){
259
- case 3 : max_mv = 3900 ; break ;
260
- case 2 : max_mv = 2200 ; break ;
261
- case 1 : max_mv = 1500 ; break ;
262
- default : break ;
263
- }
264
- return (adc_reading * max_mv ) / max_reading ;
265
- #endif
247
+ return esp_adc_cal_raw_to_voltage ((uint32_t )adc_reading , & chars );
266
248
}
267
249
268
250
#if CONFIG_IDF_TARGET_ESP32
@@ -297,4 +279,3 @@ extern void analogSetVRefPin(uint8_t pin) __attribute__ ((weak, alias("__analogS
297
279
extern void analogSetWidth (uint8_t bits ) __attribute__ ((weak , alias ("__analogSetWidth" )));
298
280
extern int hallRead () __attribute__ ((weak , alias ("__hallRead" )));
299
281
#endif
300
-
0 commit comments