Battery (TechoBoard.h/cpp): Added PIN_VBAT_MEAS_EN (P0.31) — the T-Echo Lite has a gated voltage divider that must be enabled before reading. Also added pinMode(PIN_VBAT_READ, INPUT) before each ADC read to reclaim P0.02 from other peripherals. Pin definitions hardcoded from LilyGo's t_echo_lite_config.h. I2C (variant.h): Corrected SDA/SCL from P0.04/P0.02 to P1.04/P1.02 per LilyGo's IIC_1_SDA/IIC_1_SCL. The old P0.02 mapping conflicted with the battery ADC pin. GPS (variant.h): Corrected all five GPS pin assignments to match LilyGo's config — UART TX/RX, wake, PPS, and power enable were all scrambled. SPI (variant.h): Fixed SPI_INTERFACES_COUNT from _PINNUM(0, 2) to (2). Tested on T-Echo Lite Non-Shell (USB-C, no display). Battery readings match Heltec V4 reference within 10mV.
54 lines
1.4 KiB
C++
54 lines
1.4 KiB
C++
#include <Arduino.h>
|
|
#include <Wire.h>
|
|
|
|
#include "TechoBoard.h"
|
|
|
|
#ifdef LILYGO_TECHO
|
|
|
|
void TechoBoard::begin() {
|
|
NRF52Board::begin();
|
|
|
|
// Configure battery measurement control BEFORE Wire.begin()
|
|
// to ensure P0.02 is not claimed by another peripheral
|
|
pinMode(PIN_VBAT_MEAS_EN, OUTPUT);
|
|
digitalWrite(PIN_VBAT_MEAS_EN, LOW);
|
|
pinMode(PIN_VBAT_READ, INPUT);
|
|
|
|
Wire.begin();
|
|
|
|
pinMode(SX126X_POWER_EN, OUTPUT);
|
|
digitalWrite(SX126X_POWER_EN, HIGH);
|
|
delay(10);
|
|
}
|
|
|
|
uint16_t TechoBoard::getBattMilliVolts() {
|
|
// Use LilyGo's exact ADC configuration
|
|
analogReference(AR_INTERNAL_3_0);
|
|
analogReadResolution(12);
|
|
|
|
// Enable battery voltage divider (MOSFET gate on P0.31)
|
|
pinMode(PIN_VBAT_MEAS_EN, OUTPUT);
|
|
digitalWrite(PIN_VBAT_MEAS_EN, HIGH);
|
|
|
|
// Reclaim P0.02 for analog input (in case another peripheral touched it)
|
|
pinMode(PIN_VBAT_READ, INPUT);
|
|
delay(10); // let divider + ADC settle
|
|
|
|
// Read and average (matching LilyGo's approach)
|
|
uint32_t sum = 0;
|
|
for (int i = 0; i < 8; i++) {
|
|
sum += analogRead(PIN_VBAT_READ);
|
|
delayMicroseconds(100);
|
|
}
|
|
uint16_t adc = sum / 8;
|
|
|
|
// Disable divider to save power
|
|
digitalWrite(PIN_VBAT_MEAS_EN, LOW);
|
|
|
|
// LilyGo's exact formula: adc * (3000.0 / 4096.0) * 2.0
|
|
// = adc * 0.73242188 * 2.0 = adc * 1.46484375
|
|
uint16_t millivolts = (uint16_t)((float)adc * (3000.0f / 4096.0f) * 2.0f);
|
|
|
|
return millivolts;
|
|
}
|
|
#endif |