enable battery monitoring on HeltecV2

This commit is contained in:
cyberman54 2021-02-13 15:58:56 +01:00
parent e7370e1d3c
commit a2f94ee0bd
3 changed files with 17 additions and 8 deletions

View File

@ -2,8 +2,8 @@
#define _POWER_H #define _POWER_H
#include <Arduino.h> #include <Arduino.h>
#include <driver/adc.h>
#include <esp_adc_cal.h> #include <esp_adc_cal.h>
//#include <esp32-hal-adc.h>
#include "i2c.h" #include "i2c.h"
#include "reset.h" #include "reset.h"
@ -43,6 +43,11 @@
#endif #endif
#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); typedef uint8_t (*mapFn_t)(uint16_t, uint16_t, uint16_t);
uint16_t read_voltage(void); uint16_t read_voltage(void);

View File

@ -23,12 +23,9 @@
#define HAS_LED LED_BUILTIN // white LED on board #define HAS_LED LED_BUILTIN // white LED on board
#define HAS_BUTTON KEY_BUILTIN // button "PROG" on board #define HAS_BUTTON KEY_BUILTIN // button "PROG" on board
// caveat: activating ADC2 conflicts with Wifi in current arduino-esp32 #define BAT_MEASURE_ADC ADC2_GPIO13_CHANNEL // battery probe GPIO pin
// see https://github.com/espressif/arduino-esp32/issues/102 #define BAT_MEASURE_ADC_UNIT 2 // ADC 2
// thus we must waiver of battery monitoring #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 // switches battery power and Vext, switch logic 0 = on / 1 = off
#define EXT_POWER_SW Vext #define EXT_POWER_SW Vext

View File

@ -14,6 +14,7 @@ esp_adc_cal_characteristics_t *adc_characs =
static const adc1_channel_t adc_channel = BAT_MEASURE_ADC; static const adc1_channel_t adc_channel = BAT_MEASURE_ADC;
#else // ADC2 #else // ADC2
static const adc2_channel_t adc_channel = BAT_MEASURE_ADC; static const adc2_channel_t adc_channel = BAT_MEASURE_ADC;
RTC_NOINIT_ATTR uint64_t RTC_reg_b;
#endif #endif
static const adc_atten_t atten = ADC_ATTEN_DB_11; static const adc_atten_t atten = ADC_ATTEN_DB_11;
static const adc_unit_t unit = ADC_UNIT_1; 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_width(ADC_WIDTH_BIT_12);
adc1_config_channel_atten(adc_channel, atten); adc1_config_channel_atten(adc_channel, atten);
#else // ADC2 #else // ADC2
// adc2_config_width(ADC_WIDTH_BIT_12);
adc2_config_channel_atten(adc_channel, atten); 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 #endif
// calibrate ADC // calibrate ADC
esp_adc_cal_value_t val_type = esp_adc_cal_characterize( esp_adc_cal_value_t val_type = esp_adc_cal_characterize(
@ -229,6 +232,10 @@ uint16_t read_voltage(void) {
#else // ADC2 #else // ADC2
int adc_buf = 0; int adc_buf = 0;
for (int i = 0; i < NO_OF_SAMPLES; i++) { 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); adc2_get_raw(adc_channel, ADC_WIDTH_BIT_12, &adc_buf);
adc_reading += adc_buf; adc_reading += adc_buf;
} }