intermediate commit

This commit is contained in:
2021-01-12 11:16:33 +01:00
parent 6398ff3460
commit b6dabcd16f
22 changed files with 999 additions and 6 deletions

171
src/power.cpp Normal file
View File

@@ -0,0 +1,171 @@
#include "arduino.h"
#include "power.h"
#include "hal.h"
#include <esp_adc_cal.h>
#include <driver/adc.h>
#include "display.h"
#define VOLTAGE_DIVIDER 2.08 // Lora has 220k/100k voltage divider so need to reverse that reduction via (220k+100k)/100k on vbat GPIO37 or ADC1_1 (early revs were GPIO13 or ADC2_4 but do NOT use with WiFi.begin())
#define DEFAULT_VREF 1100 // Default VREF use if no e-fuse calibration
#define VBATT_SAMPLE 500 // Battery sample rate in ms
#define VBATT_SMOOTH 20 // Number of averages in sample
#define ADC_READ_STABILIZE 50 // in ms (delay from GPIO control and ADC connections times)
#define LO_BATT_SLEEP_TIME 10 * 60 * 1000 * 1000 // How long when low batt to stay in sleep (us)
#define __DEBUG 0 // DEBUG Serial output
uint16_t Sample();
esp_adc_cal_characteristics_t *adc_chars;
uint16_t voltage = 666;
void powerInit(void)
{
#if (ENVIRONMENT == HELTECv21)
adc_chars = (esp_adc_cal_characteristics_t *)calloc(1, sizeof(esp_adc_cal_characteristics_t));
esp_adc_cal_value_t val_type = esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_6, ADC_WIDTH_BIT_12, DEFAULT_VREF, adc_chars);
adc1_config_width(ADC_WIDTH_BIT_12);
adc1_config_channel_atten(VBATT, ADC_ATTEN_DB_6);
#else
// Use this for older V2.0 with VBatt reading wired to GPIO13
adc_chars = (esp_adc_cal_characteristics_t *)calloc(1, sizeof(esp_adc_cal_characteristics_t));
esp_adc_cal_value_t val_type = esp_adc_cal_characterize(ADC_UNIT_2, ADC_ATTEN_DB_6, ADC_WIDTH_BIT_12, DEFAULT_VREF, adc_chars);
adc2_config_channel_atten(VBATT, ADC_ATTEN_DB_6);
#endif
if (val_type)
; // Suppress warning
Serial.printf("ADC Calibration: ");
if (val_type == ESP_ADC_CAL_VAL_EFUSE_VREF)
{
Serial.printf("eFuse Vref\n");
}
else if (val_type == ESP_ADC_CAL_VAL_EFUSE_TP)
{
Serial.printf("Two Point\n");
}
else
{
Serial.printf("Default[%dmV]\n", DEFAULT_VREF);
}
Serial.println("Power init: ADC done");
// Prime the Sample register
for (uint8_t i = 0; i < VBATT_SMOOTH; i++)
{
Sample();
}
Serial.println("Power init: buffer done");
pinMode(VEXT, OUTPUT);
//digitalWrite(VEXT, LOW); // ESP32 Lora v2.1 reads on GPIO37 when GPIO21 is low
//delay(ADC_READ_STABILIZE); // let GPIO stabilize
Serial.println("Power init done");
}
uint16_t powerGetVbatt(void)
{
return voltage;
}
void powerHandler(void)
{
voltage = Sample();
//displayDrawBatt(voltage, voltage < LIGHT_SLEEP_VOLTAGE);
if (voltage < MINBATT)
{ // Low Voltage cut off shut down to protect battery as long as possible
displayDrawShutdown();
delay(2000);
#if defined(__DEBUG) && __DEBUG > 0
Serial.printf(" !! Shutting down...low battery volotage: %dmV.\n", voltage);
delay(10);
#endif
esp_sleep_enable_timer_wakeup(LO_BATT_SLEEP_TIME);
esp_deep_sleep_start();
}
else if (voltage < LIGHT_SLEEP_VOLTAGE)
{ // Use light sleep once on battery
uint64_t s = VBATT_SAMPLE;
#if defined(__DEBUG) && __DEBUG > 0
Serial.printf(" - Light Sleep (%dms)...battery volotage: %dmV.\n", (int)s, voltage);
delay(20);
#endif
esp_sleep_enable_timer_wakeup(s * 1000); // Light Sleep does not flush buffer
esp_light_sleep_start();
}
delay(ADC_READ_STABILIZE);
}
// Heltec WiFi LoRa V2 battery read example
// by Jeff McClain jeff@themcclains.net
//
// Poll the proper ADC for VBatt on Heltec Lora 32 with GPIO21 toggled
uint16_t ReadVBatt()
{
int reading = 666;
digitalWrite(VEXT, LOW); // ESP32 Lora v2.1 reads on GPIO37 when GPIO21 is low
delay(ADC_READ_STABILIZE); // let GPIO stabilize
#if (defined(HELTEC_V2_1))
pinMode(VBATT, OPEN_DRAIN); // ADC GPIO37
reading = adc1_get_raw(VBATT);
pinMode(VBATT, INPUT); // Disconnect ADC before GPIO goes back high so we protect ADC from direct connect to VBATT (i.e. no divider)
#else
pinMode(VBATT, OPEN_DRAIN); // ADC GPIO13
adc2_get_raw(VBATT, ADC_WIDTH_BIT_12, &reading);
pinMode(VBATT, INPUT); // Disconnect ADC before GPIO goes back high so we protect ADC from direct connect to VBATT (i.e. no divider
#endif
uint16_t rawVoltage = esp_adc_cal_raw_to_voltage(reading, adc_chars);
//Serial.printf("battery rawvoltage = %i\n", rawVoltage);
rawVoltage *= VOLTAGE_DIVIDER;
//Serial.printf("battery sample = %i\n", rawVoltage);
//digitalWrite(VEXT, HIGH); // ESP32 Lora v2.1 reads on GPIO37 when GPIO21 is low
return rawVoltage;
}
// Use a buffer to average/sample ADC
uint16_t Sample()
{
static uint8_t i = 0;
static uint16_t samp[VBATT_SMOOTH];
static int32_t t = 0;
static bool f = true;
if (f)
{
for (uint8_t c = 0; c < VBATT_SMOOTH; c++)
{
samp[c] = 0;
}
f = false;
} // Initialize the sample array first time
t -= samp[i]; // doing a rolling recording, so remove the old rolled around value out of total and get ready to put new one in.
if (t < 0)
{
t = 0;
}
// ADC read
samp[i] = ReadVBatt();
#if defined(__DEBUG) && __DEBUG > 0
Serial.printf("ADC Raw Reading[%d]: %d", i, voltage);
#endif
t += samp[i];
if (++i >= VBATT_SMOOTH)
{
i = 0;
}
uint16_t s = round(((float)t / (float)VBATT_SMOOTH));
#if defined(__DEBUG) && __DEBUG > 0
Serial.printf(" Smoothed of %d/%d = %d\n", t, VBATT_S MOOTH, s);
#endif
return s;
}