Skip to content

Commit

Permalink
fix(adc): Always use default read resolution in __analogReadMilliVolt…
Browse files Browse the repository at this point in the history
…s to obtain correct milliVolts value. (#9006)

Add new __analogReadRaw function and move code from __analogRead without mapResolution part to __analogReadRaw.

Refactor __anlogRead to use analogReadRaw (and mapResolution).

Refactor __analogReadMilliVolts to always use default read resolution when reading adc value, as expected input by esp_adc_cal_raw_to_voltage is in default resolution (means replacing all calls of __analogRead with __analogReadRaw).
  • Loading branch information
bkari02 authored Dec 15, 2023
1 parent 04d9e33 commit b87d525
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
15 changes: 11 additions & 4 deletions cores/esp32/esp32-hal-adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ void __analogReadResolution(uint8_t bits)
#endif
}

uint16_t __analogRead(uint8_t pin)
uint16_t __analogReadRaw(uint8_t pin)
{
int8_t channel = digitalPinToAnalogChannel(pin);
int value = 0;
Expand All @@ -173,8 +173,14 @@ uint16_t __analogRead(uint8_t pin)
}
} else {
value = adc1_get_raw(channel);
return mapResolution(value);
return value;
}
return value;
}

uint16_t __analogRead(uint8_t pin)
{
uint16_t value = __analogReadRaw(pin);
return mapResolution(value);
}

Expand All @@ -201,7 +207,7 @@ uint32_t __analogReadMilliVolts(uint8_t pin){
if(__analogVRefPin){
esp_adc_cal_characteristics_t chars;
if(adc_vref_to_gpio(ADC_UNIT_2, __analogVRefPin) == ESP_OK){
__analogVRef = __analogRead(__analogVRefPin);
__analogVRef = __analogReadRaw(__analogVRefPin);
esp_adc_cal_characterize(1, __analogAttenuation, __analogWidth, DEFAULT_VREF, &chars);
__analogVRef = esp_adc_cal_raw_to_voltage(__analogVRef, &chars);
log_d("Vref to GPIO%u: %u", __analogVRefPin, __analogVRef);
Expand All @@ -215,7 +221,7 @@ uint32_t __analogReadMilliVolts(uint8_t pin){
unit = 2;
}

uint16_t adc_reading = __analogRead(pin);
uint16_t adc_reading = __analogReadRaw(pin);

uint8_t atten = __analogAttenuation;
if (__pin_attenuation[pin] != ADC_ATTENDB_MAX){
Expand Down Expand Up @@ -266,6 +272,7 @@ int __hallRead() //hall sensor using idf read
#endif

extern uint16_t analogRead(uint8_t pin) __attribute__ ((weak, alias("__analogRead")));
extern uint16_t analogReadRaw(uint8_t pin) __attribute__ ((weak, alias("__analogReadRaw")));
extern uint32_t analogReadMilliVolts(uint8_t pin) __attribute__ ((weak, alias("__analogReadMilliVolts")));
extern void analogReadResolution(uint8_t bits) __attribute__ ((weak, alias("__analogReadResolution")));
extern void analogSetClockDiv(uint8_t clockDiv) __attribute__ ((weak, alias("__analogSetClockDiv")));
Expand Down
5 changes: 5 additions & 0 deletions cores/esp32/esp32-hal-adc.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ typedef enum {
* */
uint16_t analogRead(uint8_t pin);

/*
* Get ADC value in default resolution for pin
* */
uint16_t analogReadRaw(uint8_t pin);

/*
* Get MilliVolts value for pin
* */
Expand Down

0 comments on commit b87d525

Please sign in to comment.