1313// limitations under the License.
1414
1515#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"
2016#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
2517#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
3920#include "soc/dac_channel.h"
40- #elif CONFIG_IDF_TARGET_ESP32S3
41- #include "esp32s3/rom/ets_sys.h"
4221#include "soc/sens_reg.h"
4322#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"
5223#endif
5324
25+ #define DEFAULT_VREF 1100
26+
5427static uint8_t __analogAttenuation = 3 ;//11db
5528static uint8_t __analogWidth = ADC_WIDTH_MAX - 1 ; //3 for ESP32/ESP32C3; 4 for ESP32S2
5629static uint8_t __analogReturnedWidth = SOC_ADC_MAX_BITWIDTH ; //12 for ESP32/ESP32C3; 13 for ESP32S2
5730static uint8_t __analogClockDiv = 1 ;
5831static adc_attenuation_t __pin_attenuation [SOC_GPIO_PIN_COUNT ];
5932
33+ static uint16_t __analogVRef = 0 ;
34+ #if CONFIG_IDF_TARGET_ESP32
35+ static uint8_t __analogVRefPin = 0 ;
36+ #endif
37+
6038static inline uint16_t mapResolution (uint16_t value )
6139{
6240 uint8_t from = __analogWidth + 9 ;
@@ -117,8 +95,8 @@ void __analogSetPinAttenuation(uint8_t pin, adc_attenuation_t attenuation)
11795 if (channel < 0 || attenuation > 3 ){
11896 return ;
11997 }
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 );
122100 } else {
123101 adc1_config_channel_atten (channel , attenuation );
124102 }
@@ -181,8 +159,8 @@ uint16_t __analogRead(uint8_t pin)
181159 return value ;
182160 }
183161 __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 ;
186164 r = adc2_get_raw ( channel , __analogWidth , & value );
187165 if ( r == ESP_OK ) {
188166 return mapResolution (value );
@@ -206,7 +184,7 @@ uint32_t __analogReadMilliVolts(uint8_t pin){
206184 log_e ("Pin %u is not ADC pin!" , pin );
207185 return 0 ;
208186 }
209- #if CONFIG_IDF_TARGET_ESP32
187+
210188 if (!__analogVRef ){
211189 if (esp_adc_cal_check_efuse (ESP_ADC_CAL_VAL_EFUSE_TP ) == ESP_OK ) {
212190 log_d ("eFuse Two Point: Supported" );
@@ -218,6 +196,8 @@ uint32_t __analogReadMilliVolts(uint8_t pin){
218196 }
219197 if (!__analogVRef ){
220198 __analogVRef = DEFAULT_VREF ;
199+
200+ #if CONFIG_IDF_TARGET_ESP32
221201 if (__analogVRefPin ){
222202 esp_adc_cal_characteristics_t chars ;
223203 if (adc_vref_to_gpio (ADC_UNIT_2 , __analogVRefPin ) == ESP_OK ){
@@ -227,42 +207,44 @@ uint32_t __analogReadMilliVolts(uint8_t pin){
227207 log_d ("Vref to GPIO%u: %u" , __analogVRefPin , __analogVRef );
228208 }
229209 }
210+ #endif
230211 }
231212 }
232213 uint8_t unit = 1 ;
233- if (channel > 9 ){
214+ if (channel > ( SOC_ADC_MAX_CHANNEL_NUM - 1 ) ){
234215 unit = 2 ;
235216 }
217+
236218 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+ {
243231 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 );
251240 }
241+ #endif
242+ else {
243+ log_i ("ADC%u: Characterized using Default Vref: %u\n" , unit , chars .vref );
244+ }
245+ print_chars_info = false;
252246 }
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 );
266248}
267249
268250#if CONFIG_IDF_TARGET_ESP32
@@ -297,4 +279,3 @@ extern void analogSetVRefPin(uint8_t pin) __attribute__ ((weak, alias("__analogS
297279extern void analogSetWidth (uint8_t bits ) __attribute__ ((weak , alias ("__analogSetWidth" )));
298280extern int hallRead () __attribute__ ((weak , alias ("__hallRead" )));
299281#endif
300-
0 commit comments