Unverified Commit b87d5257 authored by Benjamin Karic's avatar Benjamin Karic Committed by GitHub

fix(adc): Always use default read resolution in __analogReadMilliVolts to...

fix(adc): Always use default read resolution in __analogReadMilliVolts 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).
parent 04d9e33b
......@@ -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;
......@@ -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);
}
......@@ -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);
......@@ -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){
......@@ -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")));
......
......@@ -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
* */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment