Files
MeshCore/variants/heltec_v4_r8/HeltecV4R8Board.cpp
T
agessaman 11d7551ccd fix(display): correct R8 touch decode, display wake and power-off
Four defects found by hardware testing of the Expansion Kit V2.

Touch never registered. The panel's controller does not use the point-count
encoding the reference CHSC6X drivers document: byte 0 reads 0x00 idle and
0x1F while a finger is down, so testing for a count of 1 never fired. A
partly-failed read leaves 0xFF, which must not count as a press either, so
the test is now != 0x00 && != 0xFF.

Touch polling could stall the UI loop for ~1 s at a time. The controller
NACKs its address whenever it has nothing to report, and calling requestFrom()
unconditionally logged a bus error on every 50 ms poll and, once the bus
wedged, burned a full ESP_ERR_TIMEOUT inside loop(). Probe the address first,
which reports the same NACK quietly, and bound the read with setTimeOut().

The display could not be woken once it blanked; only RST brought it back.
turnOn() re-ran the whole display.init(), which re-enters SPI setup, spends
~500 ms in Adafruit's reset delays and pulses GPIO 21 - the line shared with
TP_RST, so it reset the touch controller on every wake. Since turnOff() no
longer parks that line low, the panel stays configured while dark and waking
is just the backlight. Toggling also clears the refresh deadline so the
current frame is drawn immediately instead of the stale one.

Power-off rebooted instead of staying off. powerOff() went through
enterDeepSleep(), which always arms an ext1 wake on P_LORA_DIO_1; a deep-sleep
wake is a full reboot, so a node in live traffic restarted within seconds of
showing "Turning OFF". It now disables every wake source, so the node stays
down until RST or a power cycle.

Note that the display off/on cycle had never been exercised on this board
before: observer builds pinned AUTO_OFF_MILLIS=0, so the panel never blanked
until display.timeout made it a runtime setting.
2026-08-28 14:26:52 -07:00

126 lines
3.6 KiB
C++

#include "HeltecV4R8Board.h"
#if defined(HELTEC_V4_R8_TFT) && defined(DISPLAY_CLASS)
#include <target.h>
#endif
void HeltecV4R8Board::begin() {
ESP32Board::begin();
periph_power.begin();
periph_power.claim(); // R8 VEXT also feeds the LoRa antenna boost rail.
loRaFEMControl.init();
// Expansion Kit V2 display/touch pins, verified against Heltec's
// Expansion_board_V2.03 schematic and the V4-R8 datasheet pinout:
// GPIO 17/18 TP_SDA / TP_SCL - the module's OLED_SDA/OLED_SCL I2C bus
// GPIO 21 LCD_RST *and* TP_RST on one net (the module's OLED_RST)
// GPIO 43 TP_INT, optional via R13, and also U0TXD
// GPIO 44 LCD_LEDK backlight, also U0RXD
// ST7789LCDDisplay owns GPIO 21, so there is no separate touch reset to do
// here - and because that net is shared, it must not be parked low while the
// display is off or the touch controller is held in reset with it.
esp_reset_reason_t reason = esp_reset_reason();
if (reason == ESP_RST_DEEPSLEEP) {
long wakeup_source = esp_sleep_get_ext1_wakeup_status();
if (wakeup_source & (1 << P_LORA_DIO_1)) {
startup_reason = BD_STARTUP_RX_PACKET;
}
rtc_gpio_hold_dis((gpio_num_t)P_LORA_NSS);
rtc_gpio_deinit((gpio_num_t)P_LORA_DIO_1);
}
}
void HeltecV4R8Board::onBeforeTransmit(void) {
digitalWrite(P_LORA_TX_LED, HIGH);
loRaFEMControl.setTxModeEnable();
}
void HeltecV4R8Board::onAfterTransmit(void) {
digitalWrite(P_LORA_TX_LED, LOW);
loRaFEMControl.setRxModeEnable();
}
void HeltecV4R8Board::enterDeepSleep(uint32_t secs, int pin_wake_btn) {
#if defined(HELTEC_V4_R8_TFT) && defined(DISPLAY_CLASS)
display.turnOff();
#endif
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
rtc_gpio_set_direction((gpio_num_t)P_LORA_DIO_1, RTC_GPIO_MODE_INPUT_ONLY);
rtc_gpio_pulldown_en((gpio_num_t)P_LORA_DIO_1);
rtc_gpio_hold_en((gpio_num_t)P_LORA_NSS);
loRaFEMControl.setRxModeEnableWhenMCUSleep();
if (pin_wake_btn < 0) {
esp_sleep_enable_ext1_wakeup((1L << P_LORA_DIO_1), ESP_EXT1_WAKEUP_ANY_HIGH);
} else {
esp_sleep_enable_ext1_wakeup((1L << P_LORA_DIO_1) | (1L << pin_wake_btn), ESP_EXT1_WAKEUP_ANY_HIGH);
}
if (secs > 0) {
esp_sleep_enable_timer_wakeup(secs * 1000000);
}
esp_deep_sleep_start();
}
void HeltecV4R8Board::powerOff() {
#if defined(HELTEC_V4_R8_TFT) && defined(DISPLAY_CLASS)
display.turnOff();
#endif
// Deliberately NOT enterDeepSleep(): that always arms an ext1 wake on
// P_LORA_DIO_1, so a node sitting in live traffic woke - and a deep-sleep
// wake is a full reboot - within seconds of showing "Turning OFF". With every
// wake source disabled the node stays down until RST or a power cycle, which
// is what asking for power-off means.
esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_ALL);
loRaFEMControl.setRxModeEnableWhenMCUSleep();
esp_deep_sleep_start();
}
uint16_t HeltecV4R8Board::getBattMilliVolts() {
analogReadResolution(12);
uint32_t raw = 0;
for (int i = 0; i < 8; i++) {
raw += analogReadMilliVolts(PIN_VBAT_READ);
}
raw = raw / 8;
return (adc_mult * raw);
}
const char* HeltecV4R8Board::getManufacturerName() const {
#ifdef HELTEC_V4_R8_TFT
return "Heltec V4 R8 TFT";
#else
return "Heltec V4 R8 OLED";
#endif
}
bool HeltecV4R8Board::setLoRaFemLnaEnabled(bool enable) {
if (!loRaFEMControl.isLnaCanControl()) {
return false;
}
loRaFEMControl.setLNAEnable(enable);
loRaFEMControl.setRxModeEnable();
return true;
}
bool HeltecV4R8Board::canControlLoRaFemLna() const {
return loRaFEMControl.isLnaCanControl();
}
bool HeltecV4R8Board::isLoRaFemLnaEnabled() const {
return loRaFEMControl.isLNAEnabled();
}