A fork of https://github.com/crosspoint-reader/crosspoint-reader
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix: don't wake up after USB connect (#644)

## Summary

* fixes problem that if short power button press is enabled, connecting
device to usb leads to waking up

authored by

Arthur Tazhitdinov and committed by
GitHub
0d82b039 5a97334a

+35 -13
+16 -7
lib/hal/HalGPIO.cpp
··· 24 24 unsigned long HalGPIO::getHeldTime() const { return inputMgr.getHeldTime(); } 25 25 26 26 void HalGPIO::startDeepSleep() { 27 - esp_deep_sleep_enable_gpio_wakeup(1ULL << InputManager::POWER_BUTTON_PIN, ESP_GPIO_WAKEUP_GPIO_LOW); 28 27 // Ensure that the power button has been released to avoid immediately turning back on if you're holding it 29 28 while (inputMgr.isPressed(BTN_POWER)) { 30 29 delay(50); 31 30 inputMgr.update(); 32 31 } 32 + // Arm the wakeup trigger *after* the button is released 33 + esp_deep_sleep_enable_gpio_wakeup(1ULL << InputManager::POWER_BUTTON_PIN, ESP_GPIO_WAKEUP_GPIO_LOW); 33 34 // Enter Deep Sleep 34 35 esp_deep_sleep_start(); 35 36 } ··· 44 45 return digitalRead(UART0_RXD) == HIGH; 45 46 } 46 47 47 - bool HalGPIO::isWakeupByPowerButton() const { 48 + HalGPIO::WakeupReason HalGPIO::getWakeupReason() const { 49 + const bool usbConnected = isUsbConnected(); 48 50 const auto wakeupCause = esp_sleep_get_wakeup_cause(); 49 51 const auto resetReason = esp_reset_reason(); 50 - if (isUsbConnected()) { 51 - return wakeupCause == ESP_SLEEP_WAKEUP_GPIO; 52 - } else { 53 - return (wakeupCause == ESP_SLEEP_WAKEUP_UNDEFINED) && (resetReason == ESP_RST_POWERON); 52 + 53 + if ((wakeupCause == ESP_SLEEP_WAKEUP_UNDEFINED && resetReason == ESP_RST_POWERON && !usbConnected) || 54 + (wakeupCause == ESP_SLEEP_WAKEUP_GPIO && resetReason == ESP_RST_DEEPSLEEP && usbConnected)) { 55 + return WakeupReason::PowerButton; 56 + } 57 + if (wakeupCause == ESP_SLEEP_WAKEUP_UNDEFINED && resetReason == ESP_RST_UNKNOWN && usbConnected) { 58 + return WakeupReason::AfterFlash; 59 + } 60 + if (wakeupCause == ESP_SLEEP_WAKEUP_UNDEFINED && resetReason == ESP_RST_POWERON && usbConnected) { 61 + return WakeupReason::AfterUSBPower; 54 62 } 55 - } 63 + return WakeupReason::Other; 64 + }
+3 -2
lib/hal/HalGPIO.h
··· 47 47 // Check if USB is connected 48 48 bool isUsbConnected() const; 49 49 50 - // Check if wakeup was caused by power button press 51 - bool isWakeupByPowerButton() const; 50 + enum class WakeupReason { PowerButton, AfterFlash, AfterUSBPower, Other }; 51 + 52 + WakeupReason getWakeupReason() const; 52 53 53 54 // Button indices 54 55 static constexpr uint8_t BTN_BACK = 0;
+16 -4
src/main.cpp
··· 294 294 SETTINGS.loadFromFile(); 295 295 KOREADER_STORE.loadFromFile(); 296 296 297 - if (gpio.isWakeupByPowerButton()) { 298 - // For normal wakeups, verify power button press duration 299 - Serial.printf("[%lu] [ ] Verifying power button press duration\n", millis()); 300 - verifyPowerButtonDuration(); 297 + switch (gpio.getWakeupReason()) { 298 + case HalGPIO::WakeupReason::PowerButton: 299 + // For normal wakeups, verify power button press duration 300 + Serial.printf("[%lu] [ ] Verifying power button press duration\n", millis()); 301 + verifyPowerButtonDuration(); 302 + break; 303 + case HalGPIO::WakeupReason::AfterUSBPower: 304 + // If USB power caused a cold boot, go back to sleep 305 + Serial.printf("[%lu] [ ] Wakeup reason: After USB Power\n", millis()); 306 + gpio.startDeepSleep(); 307 + break; 308 + case HalGPIO::WakeupReason::AfterFlash: 309 + // After flashing, just proceed to boot 310 + case HalGPIO::WakeupReason::Other: 311 + default: 312 + break; 301 313 } 302 314 303 315 // First serial output only here to avoid timing inconsistencies for power button press duration verification