mirror of
https://github.com/daveallie/crosspoint-reader.git
synced 2026-02-05 15:17:37 +03:00
fix: refactor wakeup detection logic and improve logging for reset reasons
This commit is contained in:
parent
75319cb807
commit
5f4de8d6c8
@ -24,12 +24,13 @@ bool HalGPIO::wasAnyReleased() const { return inputMgr.wasAnyReleased(); }
|
|||||||
unsigned long HalGPIO::getHeldTime() const { return inputMgr.getHeldTime(); }
|
unsigned long HalGPIO::getHeldTime() const { return inputMgr.getHeldTime(); }
|
||||||
|
|
||||||
void HalGPIO::startDeepSleep() {
|
void HalGPIO::startDeepSleep() {
|
||||||
esp_deep_sleep_enable_gpio_wakeup(1ULL << InputManager::POWER_BUTTON_PIN, ESP_GPIO_WAKEUP_GPIO_LOW);
|
|
||||||
// Ensure that the power button has been released to avoid immediately turning back on if you're holding it
|
// Ensure that the power button has been released to avoid immediately turning back on if you're holding it
|
||||||
while (inputMgr.isPressed(BTN_POWER)) {
|
while (inputMgr.isPressed(BTN_POWER)) {
|
||||||
delay(50);
|
delay(50);
|
||||||
inputMgr.update();
|
inputMgr.update();
|
||||||
}
|
}
|
||||||
|
// Arm the wakeup trigger *after* the button is released
|
||||||
|
esp_deep_sleep_enable_gpio_wakeup(1ULL << InputManager::POWER_BUTTON_PIN, ESP_GPIO_WAKEUP_GPIO_LOW);
|
||||||
// Enter Deep Sleep
|
// Enter Deep Sleep
|
||||||
esp_deep_sleep_start();
|
esp_deep_sleep_start();
|
||||||
}
|
}
|
||||||
@ -44,16 +45,24 @@ bool HalGPIO::isUsbConnected() const {
|
|||||||
return digitalRead(UART0_RXD) == HIGH;
|
return digitalRead(UART0_RXD) == HIGH;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool HalGPIO::isWakeupByPowerButton() const {
|
HalGPIO::WakeupReason HalGPIO::getWakeupReason() const {
|
||||||
const bool usbConnected = isUsbConnected();
|
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();
|
||||||
return ((wakeupCause == ESP_SLEEP_WAKEUP_UNDEFINED && resetReason == ESP_RST_POWERON && !usbConnected) ||
|
|
||||||
(wakeupCause == ESP_SLEEP_WAKEUP_GPIO && resetReason == ESP_RST_DEEPSLEEP && usbConnected));
|
|
||||||
}
|
|
||||||
|
|
||||||
bool HalGPIO::isWakeUpAfterFlash() const {
|
if ((wakeupCause == ESP_SLEEP_WAKEUP_UNDEFINED && resetReason == ESP_RST_POWERON && !usbConnected) ||
|
||||||
return esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_UNDEFINED &&
|
(wakeupCause == ESP_SLEEP_WAKEUP_GPIO && resetReason == ESP_RST_DEEPSLEEP && usbConnected)) {
|
||||||
esp_reset_reason() == ESP_RST_UNKNOWN &&
|
return WakeupReason::PowerButton;
|
||||||
isUsbConnected();
|
}
|
||||||
|
if (wakeupCause == ESP_SLEEP_WAKEUP_UNDEFINED &&
|
||||||
|
resetReason == ESP_RST_UNKNOWN &&
|
||||||
|
usbConnected) {
|
||||||
|
return WakeupReason::AfterFlash;
|
||||||
|
}
|
||||||
|
if (wakeupCause == ESP_SLEEP_WAKEUP_UNDEFINED &&
|
||||||
|
resetReason == ESP_RST_POWERON &&
|
||||||
|
usbConnected) {
|
||||||
|
return WakeupReason::AfterUSBPower;
|
||||||
|
}
|
||||||
|
return WakeupReason::Other;
|
||||||
}
|
}
|
||||||
@ -47,11 +47,14 @@ class HalGPIO {
|
|||||||
// Check if USB is connected
|
// Check if USB is connected
|
||||||
bool isUsbConnected() const;
|
bool isUsbConnected() const;
|
||||||
|
|
||||||
// Check if wakeup was caused by power button press
|
enum class WakeupReason {
|
||||||
bool isWakeupByPowerButton() const;
|
PowerButton,
|
||||||
|
AfterFlash,
|
||||||
|
AfterUSBPower,
|
||||||
|
Other
|
||||||
|
};
|
||||||
|
|
||||||
// Check if wakeup was caused by flashing (hard reset via RTS pin)
|
WakeupReason getWakeupReason() const;
|
||||||
bool isWakeUpAfterFlash() const;
|
|
||||||
|
|
||||||
// Button indices
|
// Button indices
|
||||||
static constexpr uint8_t BTN_BACK = 0;
|
static constexpr uint8_t BTN_BACK = 0;
|
||||||
|
|||||||
68
src/main.cpp
68
src/main.cpp
@ -294,18 +294,22 @@ void setup() {
|
|||||||
SETTINGS.loadFromFile();
|
SETTINGS.loadFromFile();
|
||||||
KOREADER_STORE.loadFromFile();
|
KOREADER_STORE.loadFromFile();
|
||||||
|
|
||||||
if (gpio.isWakeupByPowerButton()) {
|
switch (gpio.getWakeupReason()) {
|
||||||
// For normal wakeups, verify power button press duration
|
case HalGPIO::WakeupReason::PowerButton:
|
||||||
Serial.printf("[%lu] [ ] Verifying power button press duration\n", millis());
|
// For normal wakeups, verify power button press duration
|
||||||
verifyPowerButtonDuration();
|
Serial.printf("[%lu] [ ] Verifying power button press duration\n", millis());
|
||||||
} else if (gpio.isWakeUpAfterFlash()) {
|
verifyPowerButtonDuration();
|
||||||
// After flashing, just proceed to boot
|
break;
|
||||||
Serial.printf("[%lu] [ ] Wake up after flash detected, proceeding to boot\n", millis());
|
case HalGPIO::WakeupReason::AfterUSBPower:
|
||||||
} 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] [ ] Wakeup reason: After USB Power\n", millis());
|
||||||
Serial.printf("[%lu] [ ] No valid wakeup detected, entering deep sleep\n", millis());
|
gpio.startDeepSleep();
|
||||||
gpio.startDeepSleep();
|
break;
|
||||||
// This should never be hit as `startDeepSleep` calls esp_deep_sleep_start
|
case HalGPIO::WakeupReason::AfterFlash:
|
||||||
|
// After flashing, just proceed to boot
|
||||||
|
case HalGPIO::WakeupReason::Other:
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
@ -313,6 +317,46 @@ void setup() {
|
|||||||
|
|
||||||
setupDisplayAndFonts();
|
setupDisplayAndFonts();
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
|
||||||
exitActivity();
|
exitActivity();
|
||||||
enterNewActivity(new BootActivity(renderer, mappedInputManager));
|
enterNewActivity(new BootActivity(renderer, mappedInputManager));
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user