diff --git a/include/power.h b/include/power.h index 24ec9e28..af69e44c 100644 --- a/include/power.h +++ b/include/power.h @@ -2,8 +2,8 @@ #define _POWER_H #include -#include #include +//#include #include "i2c.h" #include "reset.h" @@ -43,6 +43,11 @@ #endif #endif +#ifdef BAT_MEASURE_ADC_UNIT // ADC2 wifi bug workaround +extern RTC_NOINIT_ATTR uint64_t RTC_reg_b; +#include "soc/sens_reg.h" // needed for adc pin reset +#endif + typedef uint8_t (*mapFn_t)(uint16_t, uint16_t, uint16_t); uint16_t read_voltage(void); diff --git a/src/hal/heltecv2.h b/src/hal/heltecv2.h index dce2c717..b33863ff 100644 --- a/src/hal/heltecv2.h +++ b/src/hal/heltecv2.h @@ -23,12 +23,9 @@ #define HAS_LED LED_BUILTIN // white LED on board #define HAS_BUTTON KEY_BUILTIN // button "PROG" on board -// caveat: activating ADC2 conflicts with Wifi in current arduino-esp32 -// see https://github.com/espressif/arduino-esp32/issues/102 -// thus we must waiver of battery monitoring -//#define BAT_MEASURE_ADC ADC2_GPIO13_CHANNEL // battery probe GPIO pin -//#define BAT_MEASURE_ADC_UNIT 2 // ADC 2 -//#define BAT_VOLTAGE_DIVIDER 2 // voltage divider 220k/100k on board +#define BAT_MEASURE_ADC ADC2_GPIO13_CHANNEL // battery probe GPIO pin +#define BAT_MEASURE_ADC_UNIT 2 // ADC 2 +#define BAT_VOLTAGE_DIVIDER 2 // voltage divider 220k/100k on board // switches battery power and Vext, switch logic 0 = on / 1 = off #define EXT_POWER_SW Vext diff --git a/src/power.cpp b/src/power.cpp index 048a4385..6453f1b2 100644 --- a/src/power.cpp +++ b/src/power.cpp @@ -14,6 +14,7 @@ esp_adc_cal_characteristics_t *adc_characs = static const adc1_channel_t adc_channel = BAT_MEASURE_ADC; #else // ADC2 static const adc2_channel_t adc_channel = BAT_MEASURE_ADC; +RTC_NOINIT_ATTR uint64_t RTC_reg_b; #endif static const adc_atten_t atten = ADC_ATTEN_DB_11; static const adc_unit_t unit = ADC_UNIT_1; @@ -193,8 +194,10 @@ void calibrate_voltage(void) { adc1_config_width(ADC_WIDTH_BIT_12); adc1_config_channel_atten(adc_channel, atten); #else // ADC2 - // adc2_config_width(ADC_WIDTH_BIT_12); adc2_config_channel_atten(adc_channel, atten); + // ADC2 wifi bug workaround, see + // https://github.com/espressif/arduino-esp32/issues/102 + RTC_reg_b = READ_PERI_REG(SENS_SAR_READ_CTRL2_REG); #endif // calibrate ADC esp_adc_cal_value_t val_type = esp_adc_cal_characterize( @@ -229,6 +232,10 @@ uint16_t read_voltage(void) { #else // ADC2 int adc_buf = 0; for (int i = 0; i < NO_OF_SAMPLES; i++) { + // ADC2 wifi bug workaround, see + // https://github.com/espressif/arduino-esp32/issues/102 + WRITE_PERI_REG(SENS_SAR_READ_CTRL2_REG, RTC_reg_b); + SET_PERI_REG_MASK(SENS_SAR_READ_CTRL2_REG, SENS_SAR2_DATA_INV); adc2_get_raw(adc_channel, ADC_WIDTH_BIT_12, &adc_buf); adc_reading += adc_buf; }