Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b87d525

Browse filesBrowse files
authored
fix(adc): Always use default read resolution in __analogReadMilliVolts to obtain correct milliVolts value. (espressif#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).
1 parent 04d9e33 commit b87d525
Copy full SHA for b87d525

File tree

Expand file treeCollapse file tree

2 files changed

+16
-4
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+16
-4
lines changed

‎cores/esp32/esp32-hal-adc.c

Copy file name to clipboardExpand all lines: cores/esp32/esp32-hal-adc.c
+11-4Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ void __analogReadResolution(uint8_t bits)
149149
#endif
150150
}
151151

152-
uint16_t __analogRead(uint8_t pin)
152+
uint16_t __analogReadRaw(uint8_t pin)
153153
{
154154
int8_t channel = digitalPinToAnalogChannel(pin);
155155
int value = 0;
@@ -173,8 +173,14 @@ uint16_t __analogRead(uint8_t pin)
173173
}
174174
} else {
175175
value = adc1_get_raw(channel);
176-
return mapResolution(value);
176+
return value;
177177
}
178+
return value;
179+
}
180+
181+
uint16_t __analogRead(uint8_t pin)
182+
{
183+
uint16_t value = __analogReadRaw(pin);
178184
return mapResolution(value);
179185
}
180186

@@ -201,7 +207,7 @@ uint32_t __analogReadMilliVolts(uint8_t pin){
201207
if(__analogVRefPin){
202208
esp_adc_cal_characteristics_t chars;
203209
if(adc_vref_to_gpio(ADC_UNIT_2, __analogVRefPin) == ESP_OK){
204-
__analogVRef = __analogRead(__analogVRefPin);
210+
__analogVRef = __analogReadRaw(__analogVRefPin);
205211
esp_adc_cal_characterize(1, __analogAttenuation, __analogWidth, DEFAULT_VREF, &chars);
206212
__analogVRef = esp_adc_cal_raw_to_voltage(__analogVRef, &chars);
207213
log_d("Vref to GPIO%u: %u", __analogVRefPin, __analogVRef);
@@ -215,7 +221,7 @@ uint32_t __analogReadMilliVolts(uint8_t pin){
215221
unit = 2;
216222
}
217223

218-
uint16_t adc_reading = __analogRead(pin);
224+
uint16_t adc_reading = __analogReadRaw(pin);
219225

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

268274
extern uint16_t analogRead(uint8_t pin) __attribute__ ((weak, alias("__analogRead")));
275+
extern uint16_t analogReadRaw(uint8_t pin) __attribute__ ((weak, alias("__analogReadRaw")));
269276
extern uint32_t analogReadMilliVolts(uint8_t pin) __attribute__ ((weak, alias("__analogReadMilliVolts")));
270277
extern void analogReadResolution(uint8_t bits) __attribute__ ((weak, alias("__analogReadResolution")));
271278
extern void analogSetClockDiv(uint8_t clockDiv) __attribute__ ((weak, alias("__analogSetClockDiv")));

‎cores/esp32/esp32-hal-adc.h

Copy file name to clipboardExpand all lines: cores/esp32/esp32-hal-adc.h
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ typedef enum {
3939
* */
4040
uint16_t analogRead(uint8_t pin);
4141

42+
/*
43+
* Get ADC value in default resolution for pin
44+
* */
45+
uint16_t analogReadRaw(uint8_t pin);
46+
4247
/*
4348
* Get MilliVolts value for pin
4449
* */

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.