fix: enhance USB wakeup detection and add flash wakeup check

This commit is contained in:
Arthur Tazhitdinov 2026-01-28 00:40:31 +05:00
parent af3f66bbcb
commit ca6598f2c2
3 changed files with 14 additions and 46 deletions

View File

@ -45,11 +45,15 @@ bool HalGPIO::isUsbConnected() const {
} }
bool HalGPIO::isWakeupByPowerButton() const { bool HalGPIO::isWakeupByPowerButton() const {
const bool usbConnected = isUsbConnected();
const auto wakeupCause = esp_sleep_get_wakeup_cause(); const auto wakeupCause = esp_sleep_get_wakeup_cause();
const auto resetReason = esp_reset_reason(); const auto resetReason = esp_reset_reason();
if (isUsbConnected()) { return ((wakeupCause == ESP_SLEEP_WAKEUP_UNDEFINED && resetReason == ESP_RST_POWERON && !usbConnected) ||
return wakeupCause == ESP_SLEEP_WAKEUP_GPIO; (wakeupCause == ESP_SLEEP_WAKEUP_GPIO && resetReason == ESP_RST_DEEPSLEEP && usbConnected));
} else {
return (wakeupCause == ESP_SLEEP_WAKEUP_UNDEFINED) && (resetReason == ESP_RST_POWERON);
}
} }
bool HalGPIO::isWakeUpAfterFlash() const {
return esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_UNDEFINED &&
esp_reset_reason() == ESP_RST_UNKNOWN &&
isUsbConnected();
}

View File

@ -50,6 +50,9 @@ class HalGPIO {
// Check if wakeup was caused by power button press // Check if wakeup was caused by power button press
bool isWakeupByPowerButton() const; bool isWakeupByPowerButton() const;
// Check if wakeup was caused by flashing (hard reset via RTS pin)
bool isWakeUpAfterFlash() const;
// Button indices // Button indices
static constexpr uint8_t BTN_BACK = 0; static constexpr uint8_t BTN_BACK = 0;
static constexpr uint8_t BTN_CONFIRM = 1; static constexpr uint8_t BTN_CONFIRM = 1;

View File

@ -304,7 +304,8 @@ void setup() {
} else { } else {
// If USB power caused a cold boot, go back to sleep // If USB power caused a cold boot, go back to sleep
Serial.printf("[%lu] [ ] No valid wakeup detected, entering deep sleep\n", millis()); Serial.printf("[%lu] [ ] No valid wakeup detected, entering deep sleep\n", millis());
gpio.startDeepSleep() gpio.startDeepSleep();
// This should never be hit as `startDeepSleep` calls esp_deep_sleep_start
} }
// First serial output only here to avoid timing inconsistencies for power button press duration verification // First serial output only here to avoid timing inconsistencies for power button press duration verification
@ -314,46 +315,6 @@ void setup() {
exitActivity(); exitActivity();
// // log reset reason and wakeup cause
// // log enum names as strings for easier reading in logs
// // Convert enum values to readable strings for logs
// auto resetReasonStr = [resetReason]() {
// switch (resetReason) {
// case ESP_RST_UNKNOWN: return "UNKNOWN";
// case ESP_RST_POWERON: return "POWERON";
// case ESP_RST_EXT: return "EXT";
// case ESP_RST_SW: return "SW";
// case ESP_RST_PANIC: return "PANIC";
// case ESP_RST_INT_WDT: return "INT_WDT";
// case ESP_RST_TASK_WDT:return "TASK_WDT";
// case ESP_RST_WDT: return "WDT";
// case ESP_RST_DEEPSLEEP: return "DEEPSLEEP";
// case ESP_RST_BROWNOUT: return "BROWNOUT";
// case ESP_RST_SDIO: return "SDIO";
// default: return "OTHER";
// }
// }();
// auto wakeupCauseStr = [wakeupCause]() {
// switch (wakeupCause) {
// case ESP_SLEEP_WAKEUP_UNDEFINED: return "UNDEFINED";
// case ESP_SLEEP_WAKEUP_EXT0: return "EXT0";
// case ESP_SLEEP_WAKEUP_EXT1: return "EXT1";
// case ESP_SLEEP_WAKEUP_TIMER: return "TIMER";
// case ESP_SLEEP_WAKEUP_TOUCHPAD: return "TOUCHPAD";
// case ESP_SLEEP_WAKEUP_ULP: return "ULP";
// case ESP_SLEEP_WAKEUP_GPIO: return "GPIO";
// case ESP_SLEEP_WAKEUP_UART: return "UART";
// default: return "OTHER";
// }
// }();
// const std::string resetInfo =
// std::string("Reset: ") + resetReasonStr + " Wakeup: " + wakeupCauseStr + " USB: " + (usbConnected ? "Yes" : "No");
// enterNewActivity(
// new FullScreenMessageActivity(renderer, mappedInputManager, resetInfo, EpdFontFamily::REGULAR));
// delay(10000);
enterNewActivity(new BootActivity(renderer, mappedInputManager)); enterNewActivity(new BootActivity(renderer, mappedInputManager));
APP_STATE.loadFromFile(); APP_STATE.loadFromFile();