added ttgo board en pins
This commit is contained in:
@@ -5,7 +5,15 @@
|
||||
#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())
|
||||
#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)
|
||||
#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
|
||||
@@ -15,7 +23,6 @@
|
||||
|
||||
uint16_t Sample();
|
||||
|
||||
|
||||
esp_adc_cal_characteristics_t *adc_chars;
|
||||
|
||||
uint16_t voltage = 666;
|
||||
@@ -27,6 +34,11 @@ void powerInit(void)
|
||||
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));
|
||||
@@ -64,7 +76,6 @@ void powerInit(void)
|
||||
Serial.println("Power init done");
|
||||
}
|
||||
|
||||
|
||||
uint16_t powerGetVbatt(void)
|
||||
{
|
||||
return voltage;
|
||||
@@ -75,31 +86,30 @@ 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();
|
||||
}
|
||||
// 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
|
||||
//
|
||||
@@ -107,27 +117,40 @@ void powerHandler(void)
|
||||
// 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
|
||||
#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
|
||||
uint16_t rawVoltage = esp_adc_cal_raw_to_voltage(reading, adc_chars);
|
||||
|
||||
#elif (ENVIRONMENT == TTGO_T18)
|
||||
pinMode(VBATT, ANALOG); // ADC GPIO13
|
||||
reading = analogRead(35);
|
||||
Serial.printf("battery analogread = %i\n", reading);
|
||||
rawVoltage = float(VOLTAGEREF / 4096) * 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
|
||||
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);
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user