Skip to content

Commit dc54857

Browse files
authored
Merge pull request meshcore-dev#2287 from pelgraine/fix/techo-lite-battery
Fixes meshcore-dev#1183 — T-Echo Lite incorrect battery voltage
2 parents d3b4576 + d516cd8 commit dc54857

3 files changed

Lines changed: 54 additions & 31 deletions

File tree

variants/lilygo_techo_lite/TechoBoard.cpp

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,47 @@
88
void TechoBoard::begin() {
99
NRF52Board::begin();
1010

11+
// Configure battery measurement control BEFORE Wire.begin()
12+
// to ensure P0.02 is not claimed by another peripheral
13+
pinMode(PIN_VBAT_MEAS_EN, OUTPUT);
14+
digitalWrite(PIN_VBAT_MEAS_EN, LOW);
15+
pinMode(PIN_VBAT_READ, INPUT);
16+
1117
Wire.begin();
1218

1319
pinMode(SX126X_POWER_EN, OUTPUT);
1420
digitalWrite(SX126X_POWER_EN, HIGH);
15-
delay(10); // give sx1262 some time to power up
21+
delay(10);
1622
}
1723

1824
uint16_t TechoBoard::getBattMilliVolts() {
19-
int adcvalue = 0;
20-
25+
// Use LilyGo's exact ADC configuration
2126
analogReference(AR_INTERNAL_3_0);
2227
analogReadResolution(12);
23-
delay(10);
2428

25-
// ADC range is 0..3000mV and resolution is 12-bit (0..4095)
26-
adcvalue = analogRead(PIN_VBAT_READ);
27-
// Convert the raw value to compensated mv, taking the resistor-
28-
// divider into account (providing the actual LIPO voltage)
29-
return (uint16_t)((float)adcvalue * REAL_VBAT_MV_PER_LSB);
29+
// Enable battery voltage divider (MOSFET gate on P0.31)
30+
pinMode(PIN_VBAT_MEAS_EN, OUTPUT);
31+
digitalWrite(PIN_VBAT_MEAS_EN, HIGH);
32+
33+
// Reclaim P0.02 for analog input (in case another peripheral touched it)
34+
pinMode(PIN_VBAT_READ, INPUT);
35+
delay(10); // let divider + ADC settle
36+
37+
// Read and average (matching LilyGo's approach)
38+
uint32_t sum = 0;
39+
for (int i = 0; i < 8; i++) {
40+
sum += analogRead(PIN_VBAT_READ);
41+
delayMicroseconds(100);
42+
}
43+
uint16_t adc = sum / 8;
44+
45+
// Disable divider to save power
46+
digitalWrite(PIN_VBAT_MEAS_EN, LOW);
47+
48+
// LilyGo's exact formula: adc * (3000.0 / 4096.0) * 2.0
49+
// = adc * 0.73242188 * 2.0 = adc * 1.46484375
50+
uint16_t millivolts = (uint16_t)((float)adc * (3000.0f / 4096.0f) * 2.0f);
51+
52+
return millivolts;
3053
}
31-
#endif
54+
#endif

variants/lilygo_techo_lite/TechoBoard.h

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44
#include <Arduino.h>
55
#include <helpers/NRF52Board.h>
66

7-
// built-ins
8-
#define VBAT_MV_PER_LSB (0.73242188F) // 3.0V ADC range and 12-bit ADC resolution = 3000mV/4096
9-
10-
#define VBAT_DIVIDER (0.5F) // 150K + 150K voltage divider on VBAT
11-
#define VBAT_DIVIDER_COMP (2.0F) // Compensation factor for the VBAT divider
12-
13-
#define PIN_VBAT_READ (4)
14-
#define REAL_VBAT_MV_PER_LSB (VBAT_DIVIDER_COMP * VBAT_MV_PER_LSB)
7+
// ============================================================
8+
// T-Echo Lite battery pins — hardcoded from LilyGo t_echo_lite_config.h
9+
// NOT using any defines from variant.h for battery measurement
10+
// ============================================================
11+
#define PIN_VBAT_READ _PINNUM(0, 2) // BATTERY_ADC_DATA
12+
#define PIN_VBAT_MEAS_EN _PINNUM(0, 31) // BATTERY_MEASUREMENT_CONTROL
1513

1614
class TechoBoard : public NRF52BoardDCDC {
1715
public:
@@ -20,10 +18,11 @@ class TechoBoard : public NRF52BoardDCDC {
2018
uint16_t getBattMilliVolts() override;
2119

2220
const char* getManufacturerName() const override {
23-
return "LilyGo T-Echo";
21+
return "LilyGo T-Echo Lite";
2422
}
2523

2624
void powerOff() override {
25+
digitalWrite(PIN_VBAT_MEAS_EN, LOW);
2726
#ifdef LED_RED
2827
digitalWrite(LED_RED, LOW);
2928
#endif
@@ -41,4 +40,4 @@ class TechoBoard : public NRF52BoardDCDC {
4140
#endif
4241
sd_power_system_off();
4342
}
44-
};
43+
};

variants/lilygo_techo_lite/variant.h

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#define PIN_PWR_EN _PINNUM(0, 30) // RT9080_EN
2525

2626
#define BATTERY_PIN _PINNUM(0, 2)
27-
#define ADC_MULTIPLIER (4.90F)
27+
#define ADC_MULTIPLIER (2.0F)
2828

2929
#define ADC_RESOLUTION (14)
3030
#define BATTERY_SENSE_RES (12)
@@ -47,13 +47,13 @@
4747
////////////////////////////////////////////////////////////////////////////////
4848
// I2C pin definition
4949

50-
#define PIN_WIRE_SDA _PINNUM(0, 4) // (SDA)
51-
#define PIN_WIRE_SCL _PINNUM(0, 2) // (SCL)
50+
#define PIN_WIRE_SDA _PINNUM(1, 4) // (SDA) - per LilyGo IIC_1_SDA
51+
#define PIN_WIRE_SCL _PINNUM(1, 2) // (SCL) - per LilyGo IIC_1_SCL
5252

5353
////////////////////////////////////////////////////////////////////////////////
5454
// SPI pin definition
5555

56-
#define SPI_INTERFACES_COUNT _PINNUM(0, 2)
56+
#define SPI_INTERFACES_COUNT (2)
5757

5858
#define PIN_SPI_MISO _PINNUM(0, 17) // (MISO)
5959
#define PIN_SPI_MOSI _PINNUM(0, 15) // (MOSI)
@@ -149,10 +149,11 @@ extern const int SCK;
149149
#define PIN_DISPLAY_BUSY DISP_BUSY
150150

151151
////////////////////////////////////////////////////////////////////////////////
152-
// GPS
153-
154-
#define PIN_GPS_RX _PINNUM(1, 13) // RXD
155-
#define PIN_GPS_TX _PINNUM(1, 15) // TXD
156-
#define GPS_EN _PINNUM(1, 11) // POWER_RT9080_EN
157-
#define PIN_GPS_STANDBY _PINNUM(1, 10)
158-
#define PIN_GPS_PPS _PINNUM(0, 29) // 1PPS
152+
// GPS — per LilyGo t_echo_lite_config.h
153+
// PIN_GPS_TX/RX named from GPS module's perspective
154+
155+
#define PIN_GPS_TX _PINNUM(0, 29) // GPS UART TX → MCU RX
156+
#define PIN_GPS_RX _PINNUM(1, 10) // GPS UART RX ← MCU TX
157+
#define GPS_EN _PINNUM(1, 11) // GPS RT9080 power enable
158+
#define PIN_GPS_STANDBY _PINNUM(1, 13) // GPS wake-up
159+
#define PIN_GPS_PPS _PINNUM(1, 15) // GPS 1PPS

0 commit comments

Comments
 (0)