Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(adc): Always use default read resolution in __analogReadMilliVolts #9006

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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