From 81f1bc5c7048e8c821267868632e52f886c132e7 Mon Sep 17 00:00:00 2001 From: Verkehrsrot Date: Sun, 8 Sep 2019 12:55:27 +0200 Subject: [PATCH] Heltecv2 battery monitor --- src/hal/heltecv2.h | 5 +++-- src/power.cpp | 28 +++++++++++++++++++++------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/hal/heltecv2.h b/src/hal/heltecv2.h index 3f6bf743..cc7de4d1 100644 --- a/src/hal/heltecv2.h +++ b/src/hal/heltecv2.h @@ -20,8 +20,9 @@ #define HAS_LED LED_BUILTIN // white LED on board #define HAS_BUTTON KEY_BUILTIN // button "PROG" on board -//#define BAT_MEASURE_ADC ADC2_GPIO13_CHANNEL // battery probe GPIO pin -//#define BAT_VOLTAGE_DIVIDER 4 // 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 #define EXT_POWER_SW Vext // switches battery power, Vext control 0 = on / 1 = off #define EXT_POWER_ON 0 //#define EXT_POWER_OFF 1 diff --git a/src/power.cpp b/src/power.cpp index 5797da26..ad366d8b 100644 --- a/src/power.cpp +++ b/src/power.cpp @@ -10,7 +10,7 @@ static const char TAG[] = __FILE__; AXP20X_Class axp; void AXP192_init(void) { - + if (axp.begin(Wire, AXP192_PRIMARY_ADDRESS)) ESP_LOGI(TAG, "AXP192 PMU initialization failed"); else { @@ -49,16 +49,26 @@ esp_adc_cal_characteristics_t *adc_characs = (esp_adc_cal_characteristics_t *)calloc( 1, sizeof(esp_adc_cal_characteristics_t)); +#ifndef BAT_MEASURE_ADC_UNIT // ADC1 static const adc1_channel_t adc_channel = BAT_MEASURE_ADC; +#else // ADC2 +static const adc2_channel_t adc_channel = BAT_MEASURE_ADC; +#endif static const adc_atten_t atten = ADC_ATTEN_DB_11; static const adc_unit_t unit = ADC_UNIT_1; -#endif + +#endif // BAT_MEASURE_ADC void calibrate_voltage(void) { #ifdef BAT_MEASURE_ADC - // configure ADC +// configure ADC +#ifndef BAT_MEASURE_ADC_UNIT // ADC1 ESP_ERROR_CHECK(adc1_config_width(ADC_WIDTH_BIT_12)); ESP_ERROR_CHECK(adc1_config_channel_atten(adc_channel, atten)); +#else // ADC2 + // ESP_ERROR_CHECK(adc2_config_width(ADC_WIDTH_BIT_12)); + ESP_ERROR_CHECK(adc2_config_channel_atten(adc_channel, atten)); +#endif // calibrate ADC esp_adc_cal_value_t val_type = esp_adc_cal_characterize( unit, atten, ADC_WIDTH_BIT_12, DEFAULT_VREF, adc_characs); @@ -75,7 +85,6 @@ void calibrate_voltage(void) { #endif } - uint8_t getBattLevel() { /* return values: @@ -102,7 +111,6 @@ uint8_t getBattLevel() { // u1_t os_getBattLevel(void) { return getBattLevel(); }; - uint16_t read_voltage() { uint16_t voltage = 0; @@ -114,17 +122,23 @@ uint16_t read_voltage() { #ifdef BAT_MEASURE_ADC // multisample ADC uint32_t adc_reading = 0; + int adc_buf = 0; for (int i = 0; i < NO_OF_SAMPLES; i++) { +#ifndef BAT_MEASURE_ADC_UNIT // ADC1 adc_reading += adc1_get_raw(adc_channel); +#else // ADC2 + ESP_ERROR_CHECK(adc2_get_raw(adc_channel, ADC_WIDTH_BIT_12, &adc_buf)); + adc_reading += adc_buf; +#endif } adc_reading /= NO_OF_SAMPLES; // Convert ADC reading to voltage in mV voltage = esp_adc_cal_raw_to_voltage(adc_reading, adc_characs); -#endif +#endif // BAT_MEASURE_ADC #ifdef BAT_VOLTAGE_DIVIDER voltage *= BAT_VOLTAGE_DIVIDER; -#endif +#endif // BAT_VOLTAGE_DIVIDER #endif // HAS_PMU