diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 5f8dace8..b760389f 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -22,7 +22,6 @@ zephyr_linker_sources(RODATA include/linker/zmk-events.ld) # Add your source file to the "app" target. This must come after # find_package(Zephyr) which defines the target. target_include_directories(app PRIVATE include) -target_sources_ifdef(CONFIG_ZMK_SLEEP app PRIVATE src/power.c) target_sources(app PRIVATE src/stdlib.c) target_sources(app PRIVATE src/activity.c) target_sources(app PRIVATE src/kscan.c) diff --git a/app/Kconfig b/app/Kconfig index 948eaeff..8be741ba 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -372,10 +372,6 @@ config ZMK_SLEEP if ZMK_SLEEP -choice SYS_PM_POLICY - default PM_POLICY_APP -endchoice - config PM_DEVICE default y diff --git a/app/src/activity.c b/app/src/activity.c index f31e608d..1fa75eb5 100644 --- a/app/src/activity.c +++ b/app/src/activity.c @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include @@ -20,6 +20,18 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #include +#if IS_ENABLED(CONFIG_USB_DEVICE_STACK) +#include +#endif + +bool is_usb_power_present() { +#if IS_ENABLED(CONFIG_USB_DEVICE_STACK) + return zmk_usb_is_powered(); +#else + return false; +#endif /* IS_ENABLED(CONFIG_USB_DEVICE_STACK) */ +} + static enum zmk_activity_state activity_state; static uint32_t activity_last_uptime; @@ -55,7 +67,7 @@ void activity_work_handler(struct k_work *work) { int32_t current = k_uptime_get(); int32_t inactive_time = current - activity_last_uptime; #if IS_ENABLED(CONFIG_ZMK_SLEEP) - if (inactive_time > MAX_SLEEP_MS) { + if (inactive_time > MAX_SLEEP_MS && !is_usb_power_present()) { // Put devices in low power mode before sleeping pm_power_state_force((struct pm_state_info){PM_STATE_STANDBY, 0, 0}); set_state(ZMK_ACTIVITY_SLEEP); diff --git a/app/src/power.c b/app/src/power.c deleted file mode 100644 index 1803f62c..00000000 --- a/app/src/power.c +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2020 The ZMK Contributors - * - * SPDX-License-Identifier: MIT - */ - -#include -#include -#include - -#include - -LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); - -#include -#include - -bool is_usb_power_present() { -#if IS_ENABLED(CONFIG_USB_DEVICE_STACK) - return zmk_usb_is_powered(); -#else - return false; -#endif /* IS_ENABLED(CONFIG_USB_DEVICE_STACK) */ -} - -struct pm_state_info pm_policy_next_state(int32_t ticks) { - if (zmk_activity_get_state() == ZMK_ACTIVITY_SLEEP && !is_usb_power_present()) { - return (struct pm_state_info){PM_STATE_SOFT_OFF, 0, 0}; - } - - return (struct pm_state_info){PM_STATE_ACTIVE, 0, 0}; -} - -__weak bool pm_policy_low_power_devices(enum pm_state state) { return pm_is_sleep_state(state); }