Files
mailboxcheckerMQTT/src/power.cpp
2021-01-13 13:00:28 +01:00

195 lines
6.6 KiB
C++

#include "arduino.h"
#include "power.h"
#include "hal.h"
#include <esp_adc_cal.h>
#include <driver/adc.h>
#include "display.h"
#if (ENVIRONMENT == TTGO_T18)
#define VOLTAGE_DIVIDER 2 // ttgo has 100/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 VOLTAGEREF 3787 // measured 2,0474 / 2214 ticks (mv)
#define VOLTAGEADC 0.9245
#else
#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())
#endif
#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);
#elif (ENVIRONMENT == TTGO_T18)
//nothing
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_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");
displayWriteLine("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()
{
//Serial.println("start read batt");
//int reading = 666;
uint16_t rawVoltage;
#if (defined(HELTEC_V2_1))
digitalWrite(VEXT, LOW); // ESP32 Lora v2.1 reads on GPIO37 when GPIO21 is low
delay(ADC_READ_STABILIZE); // let GPIO stabilize
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)
uint16_t rawVoltage = esp_adc_cal_raw_to_voltage(reading, adc_chars);
#elif (ENVIRONMENT == TTGO_T18)
pinMode(VBATT, ANALOG); // ADC GPIO13
rawVoltage = analogRead(35) * VOLTAGEADC;
//Serial.printf("battery analogread = %i\n", reading);
//Serial.printf("raw voltage = %i\n", rawVoltage);
pinMode(VBATT, INPUT); // Disconnect ADC before GPIO goes back high so we protect ADC from direct connect to VBATT (i.e. no divider
#elif (ENVIRONMENT == LOLIN32)
digitalWrite(VEXT, LOW); // ESP32 Lora v2.1 reads on GPIO37 when GPIO21 is low
delay(ADC_READ_STABILIZE); // let GPIO stabilize
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
rawVoltage = esp_adc_cal_raw_to_voltage(reading, adc_chars);
#endif
//Serial.printf("battery rawvoltage = %i\n", rawVoltage);
rawVoltage *= VOLTAGE_DIVIDER;
//Serial.printf("battery voltage = %4.2f\n", rawVoltage/1000);
//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();
//Serial.printf("ADC Raw Reading[%d]: %d", i, voltage);
t += samp[i];
if (++i >= VBATT_SMOOTH)
{
i = 0;
}
uint16_t s = round(((float)t / (float)VBATT_SMOOTH));
Serial.printf("Vbatt = %4.3f Volt\n", (double(s)/1000));
return s;
}