Merge pull request #135 from recrof/dev

LilyGo T-ECHO: report correct battery voltage
This commit is contained in:
ripplebiz
2025-03-24 11:48:52 +11:00
committed by GitHub
3 changed files with 44 additions and 27 deletions

View File

@@ -22,11 +22,18 @@ void TechoBoard::begin() {
// for future use, sub-classes SHOULD call this from their begin() // for future use, sub-classes SHOULD call this from their begin()
startup_reason = BD_STARTUP_NORMAL; startup_reason = BD_STARTUP_NORMAL;
pinMode(PIN_VBAT_READ, INPUT); delay(200);
pinMode(PIN_PWR_EN, OUTPUT);
digitalWrite(PIN_PWR_EN, HIGH);
pinMode(PIN_BUTTON1, INPUT_PULLUP);
pinMode(PIN_BUTTON2, INPUT_PULLUP);
pinMode(LED_RED, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_BLUE, OUTPUT);
delay(200);
#if defined(PIN_BOARD_SDA) && defined(PIN_BOARD_SCL) pinMode(PIN_TXCO, OUTPUT);
Wire.setPins(PIN_BOARD_SDA, PIN_BOARD_SCL) digitalWrite(PIN_TXCO, HIGH);
#endif
Wire.begin(); Wire.begin();
@@ -35,6 +42,20 @@ void TechoBoard::begin() {
delay(10); // give sx1262 some time to power up delay(10); // give sx1262 some time to power up
} }
uint16_t TechoBoard::getBattMilliVolts() {
int adcvalue = 0;
analogReference(AR_INTERNAL_3_0);
analogReadResolution(12);
delay(10);
// ADC range is 0..3000mV and resolution is 12-bit (0..4095)
adcvalue = analogRead(PIN_VBAT_READ);
// Convert the raw value to compensated mv, taking the resistor-
// divider into account (providing the actual LIPO voltage)
return (uint16_t)((float)adcvalue * REAL_VBAT_MV_PER_LSB);
}
bool TechoBoard::startOTAUpdate(const char* id, char reply[]) { bool TechoBoard::startOTAUpdate(const char* id, char reply[]) {
// Config the peripheral connection with maximum bandwidth // Config the peripheral connection with maximum bandwidth
// more SRAM required by SoftDevice // more SRAM required by SoftDevice

View File

@@ -17,28 +17,26 @@
#define SX126X_DIO3_TCXO_VOLTAGE 1.8 #define SX126X_DIO3_TCXO_VOLTAGE 1.8
// built-ins // built-ins
#define PIN_VBAT_READ 4 #define VBAT_MV_PER_LSB (0.73242188F) // 3.0V ADC range and 12-bit ADC resolution = 3000mV/4096
#define ADC_MULTIPLIER (2.0)
#define VBAT_DIVIDER (0.5F) // 150K + 150K voltage divider on VBAT
#define VBAT_DIVIDER_COMP (2.0F) // Compensation factor for the VBAT divider
#define PIN_VBAT_READ (4)
#define REAL_VBAT_MV_PER_LSB (VBAT_DIVIDER_COMP * VBAT_MV_PER_LSB)
class TechoBoard : public mesh::MainBoard { class TechoBoard : public mesh::MainBoard {
protected: protected:
uint8_t startup_reason; uint8_t startup_reason;
public: public:
void begin(); void begin();
uint8_t getStartupReason() const override { return startup_reason; } uint16_t getBattMilliVolts() override;
bool startOTAUpdate(const char* id, char reply[]) override;
#define BATTERY_SAMPLES 8 uint8_t getStartupReason() const override {
return startup_reason;
uint16_t getBattMilliVolts() override {
analogReadResolution(12);
uint32_t raw = 0;
for (int i = 0; i < BATTERY_SAMPLES; i++) {
raw += analogRead(PIN_VBAT_READ);
}
raw = raw / BATTERY_SAMPLES;
return (ADC_MULTIPLIER * raw) / 4096;
} }
const char* getManufacturerName() const override { const char* getManufacturerName() const override {
@@ -48,6 +46,4 @@ public:
void reboot() override { void reboot() override {
NVIC_SystemReset(); NVIC_SystemReset();
} }
bool startOTAUpdate(const char* id, char reply[]) override;
}; };

View File

@@ -14,12 +14,12 @@
#define USE_LFXO // 32.768 kHz crystal oscillator #define USE_LFXO // 32.768 kHz crystal oscillator
#define VARIANT_MCK (64000000ul) #define VARIANT_MCK (64000000ul)
#define WIRE_INTERFACES_COUNT (1) #define WIRE_INTERFACES_COUNT (1)
#define PIN_TXCO (21)
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Power // Power
#define PIN_PWR_EN (6) #define PIN_PWR_EN (12)
#define BATTERY_PIN (4) #define BATTERY_PIN (4)
#define ADC_MULTIPLIER (4.90F) #define ADC_MULTIPLIER (4.90F)
@@ -62,11 +62,11 @@
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Builtin LEDs // Builtin LEDs
#define LED_RED (13) #define LED_RED (34)
#define LED_GREEN (33)
#define LED_BLUE (14) #define LED_BLUE (14)
#define LED_GREEN (15)
#define LED_BUILTIN (15) #define LED_BUILTIN LED_GREEN
#define PIN_LED LED_BUILTIN #define PIN_LED LED_BUILTIN
#define LED_PIN LED_BUILTIN #define LED_PIN LED_BUILTIN
#define LED_STATE_ON LOW #define LED_STATE_ON LOW
@@ -80,7 +80,7 @@
#define PIN_BUTTON1 (42) #define PIN_BUTTON1 (42)
#define BUTTON_PIN PIN_BUTTON1 #define BUTTON_PIN PIN_BUTTON1
#define PIN_BUTTON2 (18) #define PIN_BUTTON2 (11)
#define BUTTON_PIN2 PIN_BUTTON2 #define BUTTON_PIN2 PIN_BUTTON2
#define EXTERNAL_FLASH_DEVICES MX25R1635F #define EXTERNAL_FLASH_DEVICES MX25R1635F