From e5d352850c1bafca18bef1d0080a8eceb13390a0 Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Wed, 15 Mar 2023 21:48:30 -0400 Subject: [PATCH 01/10] feat: Add soft on/off support. Initial work on a soft on/off support for ZMK. Triggering soft off puts the device into deep sleep with only a specific GPIO pin configured to wake the device, avoiding waking from other key presses in the matrix like the normal deep sleep. Co-authored-by: Cem Aksoylar --- app/CMakeLists.txt | 4 + app/Kconfig | 9 + app/Kconfig.behaviors | 10 + app/dts/behaviors.dtsi | 1 + app/dts/behaviors/soft_off.dtsi | 15 ++ .../bindings/zmk,behavior-key-scanned.yaml | 31 +++ app/dts/bindings/zmk,behavior-key.yaml | 31 +++ .../bindings/zmk,soft-off-wakeup-sources.yaml | 14 ++ app/dts/bindings/zmk,wakeup-trigger-key.yaml | 18 ++ app/include/zmk/pm.h | 9 + app/src/behavior_key.c | 159 ++++++++++++++ app/src/behavior_key_scanned.c | 194 ++++++++++++++++++ app/src/kscan.c | 6 + app/src/pm.c | 59 ++++++ app/src/wakeup_trigger_key.c | 87 ++++++++ docs/docs/behaviors/soft-off.md | 38 ++++ docs/docs/development/new-shield.md | 1 + docs/docs/features/soft-off.md | 164 +++++++++++++++ docs/sidebars.js | 2 + 19 files changed, 852 insertions(+) create mode 100644 app/dts/behaviors/soft_off.dtsi create mode 100644 app/dts/bindings/zmk,behavior-key-scanned.yaml create mode 100644 app/dts/bindings/zmk,behavior-key.yaml create mode 100644 app/dts/bindings/zmk,soft-off-wakeup-sources.yaml create mode 100644 app/dts/bindings/zmk,wakeup-trigger-key.yaml create mode 100644 app/include/zmk/pm.h create mode 100644 app/src/behavior_key.c create mode 100644 app/src/behavior_key_scanned.c create mode 100644 app/src/pm.c create mode 100644 app/src/wakeup_trigger_key.c create mode 100644 docs/docs/behaviors/soft-off.md create mode 100644 docs/docs/features/soft-off.md diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index e5f489765e8..d14614fc484 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -22,7 +22,11 @@ target_sources(app PRIVATE src/matrix_transform.c) target_sources(app PRIVATE src/sensors.c) target_sources_ifdef(CONFIG_ZMK_WPM app PRIVATE src/wpm.c) target_sources(app PRIVATE src/event_manager.c) +target_sources_ifdef(CONFIG_ZMK_BEHAVIOR_KEY app PRIVATE src/behavior_key.c) +target_sources_ifdef(CONFIG_ZMK_BEHAVIOR_KEY_SCANNED app PRIVATE src/behavior_key_scanned.c) +target_sources_ifdef(CONFIG_ZMK_PM_SOFT_OFF app PRIVATE src/pm.c) target_sources_ifdef(CONFIG_ZMK_EXT_POWER app PRIVATE src/ext_power_generic.c) +target_sources_ifdef(CONFIG_ZMK_WAKEUP_TRIGGER_KEY app PRIVATE src/wakeup_trigger_key.c) target_sources(app PRIVATE src/events/activity_state_changed.c) target_sources(app PRIVATE src/events/position_state_changed.c) target_sources(app PRIVATE src/events/sensor_event.c) diff --git a/app/Kconfig b/app/Kconfig index a5fa54f614b..eda83a33ca9 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -406,6 +406,15 @@ config ZMK_EXT_POWER bool "Enable support to control external power output" default y +config ZMK_PM_SOFT_OFF + bool "Soft-off support" + select PM_DEVICE + +config ZMK_WAKEUP_TRIGGER_KEY + bool "Hardware supported wakeup (GPIO)" + default y + depends on DT_HAS_ZMK_WAKEUP_TRIGGER_KEY_ENABLED && ZMK_PM_SOFT_OFF + #Power Management endmenu diff --git a/app/Kconfig.behaviors b/app/Kconfig.behaviors index 11bc8c5900f..9d1773c3d1f 100644 --- a/app/Kconfig.behaviors +++ b/app/Kconfig.behaviors @@ -1,6 +1,16 @@ # Copyright (c) 2023 The ZMK Contributors # SPDX-License-Identifier: MIT +config ZMK_BEHAVIOR_KEY + bool + default y + depends on DT_HAS_ZMK_BEHAVIOR_KEY_ENABLED + +config ZMK_BEHAVIOR_KEY_SCANNED + bool + default y + depends on DT_HAS_ZMK_BEHAVIOR_KEY_SCANNED_ENABLED + config ZMK_BEHAVIOR_KEY_TOGGLE bool default y diff --git a/app/dts/behaviors.dtsi b/app/dts/behaviors.dtsi index 23f2fee2806..fde75271891 100644 --- a/app/dts/behaviors.dtsi +++ b/app/dts/behaviors.dtsi @@ -20,3 +20,4 @@ #include #include #include +#include diff --git a/app/dts/behaviors/soft_off.dtsi b/app/dts/behaviors/soft_off.dtsi new file mode 100644 index 00000000000..fa6571a1117 --- /dev/null +++ b/app/dts/behaviors/soft_off.dtsi @@ -0,0 +1,15 @@ +/* + * Copyright (c) 2023 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +/ { + behaviors { + /omit-if-no-ref/ soft_off: behavior_soft_off { + compatible = "zmk,behavior-soft-off"; + label = "SOFTOFF"; + #binding-cells = <0>; + }; + }; +}; diff --git a/app/dts/bindings/zmk,behavior-key-scanned.yaml b/app/dts/bindings/zmk,behavior-key-scanned.yaml new file mode 100644 index 00000000000..bdb3abaff0e --- /dev/null +++ b/app/dts/bindings/zmk,behavior-key-scanned.yaml @@ -0,0 +1,31 @@ +# Copyright (c) 2023 The ZMK Contributors +# SPDX-License-Identifier: MIT + +description: | + Driver for a dedicated key triggered by matrix scanning for invoking a connected behavior. + +compatible: "zmk,behavior-key-scanned" + +include: base.yaml + +properties: + key: + type: phandle + required: true + description: The GPIO key that triggers wake via interrupt + bindings: + type: phandle + required: true + description: The GPIO key that triggers wake via interrupt + debounce-press-ms: + type: int + default: 5 + description: Debounce time for key press in milliseconds. Use 0 for eager debouncing. + debounce-release-ms: + type: int + default: 5 + description: Debounce time for key release in milliseconds. + debounce-scan-period-ms: + type: int + default: 1 + description: Time between reads in milliseconds when any key is pressed. diff --git a/app/dts/bindings/zmk,behavior-key.yaml b/app/dts/bindings/zmk,behavior-key.yaml new file mode 100644 index 00000000000..ff7a585eafa --- /dev/null +++ b/app/dts/bindings/zmk,behavior-key.yaml @@ -0,0 +1,31 @@ +# Copyright (c) 2023 The ZMK Contributors +# SPDX-License-Identifier: MIT + +description: | + Driver for a dedicated key for invoking a connected behavior. + +compatible: "zmk,behavior-key" + +include: base.yaml + +properties: + key: + type: phandle + required: true + description: The GPIO key that triggers wake via interrupt + bindings: + type: phandle + required: true + description: The GPIO key that triggers wake via interrupt + debounce-press-ms: + type: int + default: 5 + description: Debounce time for key press in milliseconds. Use 0 for eager debouncing. + debounce-release-ms: + type: int + default: 5 + description: Debounce time for key release in milliseconds. + debounce-scan-period-ms: + type: int + default: 1 + description: Time between reads in milliseconds when any key is pressed. diff --git a/app/dts/bindings/zmk,soft-off-wakeup-sources.yaml b/app/dts/bindings/zmk,soft-off-wakeup-sources.yaml new file mode 100644 index 00000000000..f98039a0ca9 --- /dev/null +++ b/app/dts/bindings/zmk,soft-off-wakeup-sources.yaml @@ -0,0 +1,14 @@ +# Copyright (c) 2023 The ZMK Contributors +# SPDX-License-Identifier: MIT + +description: | + Description of all possible wakeup-sources from a forces + soft-off state. + +compatible: "zmk,soft-off-wakeup-sources" + +properties: + wakeup-sources: + type: phandles + required: true + description: List of wakeup-sources that should be enabled to wake the system from forces soft-off state. diff --git a/app/dts/bindings/zmk,wakeup-trigger-key.yaml b/app/dts/bindings/zmk,wakeup-trigger-key.yaml new file mode 100644 index 00000000000..fa7636d1f32 --- /dev/null +++ b/app/dts/bindings/zmk,wakeup-trigger-key.yaml @@ -0,0 +1,18 @@ +# Copyright (c) 2023 The ZMK Contributors +# SPDX-License-Identifier: MIT + +description: | + Driver for a dedicated key for waking the device from sleep + +compatible: "zmk,wakeup-trigger-key" + +include: base.yaml + +properties: + trigger: + type: phandle + required: true + description: The GPIO key that triggers wake via interrupt + extra-gpios: + type: phandle-array + description: Optional set of pins that should be set active before sleeping. diff --git a/app/include/zmk/pm.h b/app/include/zmk/pm.h new file mode 100644 index 00000000000..dff217afd29 --- /dev/null +++ b/app/include/zmk/pm.h @@ -0,0 +1,9 @@ +/* + * Copyright (c) 2023 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#pragma once + +int zmk_pm_soft_off(void); \ No newline at end of file diff --git a/app/src/behavior_key.c b/app/src/behavior_key.c new file mode 100644 index 00000000000..3633ce39a40 --- /dev/null +++ b/app/src/behavior_key.c @@ -0,0 +1,159 @@ +/* + * Copyright (c) 2023 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#define DT_DRV_COMPAT zmk_behavior_key + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + +struct behavior_key_config { + struct zmk_debounce_config debounce_config; + int32_t debounce_scan_period_ms; + struct gpio_dt_spec key; +}; + +struct behavior_key_data { + struct zmk_behavior_binding binding; + struct zmk_debounce_state debounce_state; + struct gpio_callback key_callback; + const struct device *dev; + struct k_work_delayable update_work; + uint32_t read_time; +}; + +static void bk_enable_interrupt(const struct device *dev) { + const struct behavior_key_config *config = dev->config; + + gpio_pin_interrupt_configure_dt(&config->key, GPIO_INT_LEVEL_ACTIVE); +} + +static void bk_disable_interrupt(const struct device *dev) { + const struct behavior_key_config *config = dev->config; + + gpio_pin_interrupt_configure_dt(&config->key, GPIO_INT_DISABLE); +} + +static void bk_read(const struct device *dev) { + const struct behavior_key_config *config = dev->config; + struct behavior_key_data *data = dev->data; + + zmk_debounce_update(&data->debounce_state, gpio_pin_get_dt(&config->key), + config->debounce_scan_period_ms, &config->debounce_config); + + if (zmk_debounce_get_changed(&data->debounce_state)) { + const bool pressed = zmk_debounce_is_pressed(&data->debounce_state); + + struct zmk_behavior_binding_event event = {.position = INT32_MAX, + .timestamp = k_uptime_get()}; + + if (pressed) { + behavior_keymap_binding_pressed(&data->binding, event); + } else { + behavior_keymap_binding_released(&data->binding, event); + } + } + + if (zmk_debounce_is_active(&data->debounce_state)) { + data->read_time += config->debounce_scan_period_ms; + + k_work_reschedule(&data->update_work, K_TIMEOUT_ABS_MS(data->read_time)); + } else { + bk_enable_interrupt(dev); + } +} + +static void bk_update_work(struct k_work *work) { + struct k_work_delayable *dwork = CONTAINER_OF(work, struct k_work_delayable, work); + struct behavior_key_data *data = CONTAINER_OF(dwork, struct behavior_key_data, update_work); + bk_read(data->dev); +} + +static void bk_gpio_irq_callback(const struct device *port, struct gpio_callback *cb, + const gpio_port_pins_t pin) { + struct behavior_key_data *data = CONTAINER_OF(cb, struct behavior_key_data, key_callback); + + bk_disable_interrupt(data->dev); + + data->read_time = k_uptime_get(); + k_work_reschedule(&data->update_work, K_NO_WAIT); +} + +static int behavior_key_init(const struct device *dev) { + const struct behavior_key_config *config = dev->config; + struct behavior_key_data *data = dev->data; + + if (!device_is_ready(config->key.port)) { + LOG_ERR("GPIO port is not ready"); + return -ENODEV; + } + + k_work_init_delayable(&data->update_work, bk_update_work); + data->dev = dev; + + gpio_pin_configure_dt(&config->key, GPIO_INPUT); + gpio_init_callback(&data->key_callback, bk_gpio_irq_callback, BIT(config->key.pin)); + gpio_add_callback(config->key.port, &data->key_callback); + + while (gpio_pin_get_dt(&config->key)) { + k_sleep(K_MSEC(100)); + } + + bk_enable_interrupt(dev); + + return 0; +} + +static int behavior_key_pm_action(const struct device *dev, enum pm_device_action action) { + const struct behavior_key_config *config = dev->config; + struct behavior_key_data *data = dev->data; + + int ret; + + switch (action) { + case PM_DEVICE_ACTION_SUSPEND: + bk_disable_interrupt(dev); + ret = gpio_remove_callback(config->key.port, &data->key_callback); + break; + case PM_DEVICE_ACTION_RESUME: + ret = gpio_add_callback(config->key.port, &data->key_callback); + bk_enable_interrupt(dev); + break; + default: + ret = -ENOTSUP; + break; + } + + return ret; +} + +#define BK_INST(n) \ + const struct behavior_key_config bk_config_##n = { \ + .key = GPIO_DT_SPEC_GET(DT_INST_PHANDLE(n, key), gpios), \ + .debounce_config = \ + { \ + .debounce_press_ms = DT_INST_PROP(n, debounce_press_ms), \ + .debounce_release_ms = DT_INST_PROP(n, debounce_release_ms), \ + }, \ + .debounce_scan_period_ms = DT_INST_PROP(n, debounce_scan_period_ms), \ + }; \ + struct behavior_key_data bk_data_##n = { \ + .binding = ZMK_KEYMAP_EXTRACT_BINDING(0, DT_DRV_INST(n)), \ + }; \ + PM_DEVICE_DT_INST_DEFINE(n, behavior_key_pm_action); \ + DEVICE_DT_INST_DEFINE(n, behavior_key_init, PM_DEVICE_DT_INST_GET(n), &bk_data_##n, \ + &bk_config_##n, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, NULL); + +DT_INST_FOREACH_STATUS_OKAY(BK_INST) diff --git a/app/src/behavior_key_scanned.c b/app/src/behavior_key_scanned.c new file mode 100644 index 00000000000..c961b292622 --- /dev/null +++ b/app/src/behavior_key_scanned.c @@ -0,0 +1,194 @@ +/* + * Copyright (c) 2023 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#define DT_DRV_COMPAT zmk_behavior_key_scanned + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + +struct behavior_key_scanned_config { + struct zmk_debounce_config debounce_config; + int32_t debounce_scan_period_ms; + struct gpio_dt_spec key; +}; + +struct behavior_key_scanned_data { + struct zmk_behavior_binding binding; + struct zmk_debounce_state debounce_state; + struct gpio_callback key_callback; + const struct device *dev; + struct k_work_delayable update_work; + uint32_t read_time; + bool pin_active; + bool active_scan_detected; + struct k_sem sem; +}; + +static void bks_enable_interrupt(const struct device *dev, bool active_scanning) { + const struct behavior_key_scanned_config *config = dev->config; + + gpio_pin_interrupt_configure_dt(&config->key, active_scanning ? GPIO_INT_EDGE_TO_ACTIVE + : GPIO_INT_LEVEL_ACTIVE); +} + +static void bks_disable_interrupt(const struct device *dev) { + const struct behavior_key_scanned_config *config = dev->config; + + gpio_pin_interrupt_configure_dt(&config->key, GPIO_INT_DISABLE); +} + +static void bks_read(const struct device *dev) { + const struct behavior_key_scanned_config *config = dev->config; + struct behavior_key_scanned_data *data = dev->data; + + if (k_sem_take(&data->sem, K_NO_WAIT) < 0) { + // k_work_reschedule(&data->update_work, K_NO_WAIT); + return; + } + + zmk_debounce_update(&data->debounce_state, data->active_scan_detected, + config->debounce_scan_period_ms, &config->debounce_config); + + if (zmk_debounce_get_changed(&data->debounce_state)) { + const bool pressed = zmk_debounce_is_pressed(&data->debounce_state); + + struct zmk_behavior_binding_event event = {.position = INT32_MAX, + .timestamp = k_uptime_get()}; + + if (pressed) { + behavior_keymap_binding_pressed(&data->binding, event); + } else { + behavior_keymap_binding_released(&data->binding, event); + } + } + + if (zmk_debounce_is_active(&data->debounce_state)) { + data->active_scan_detected = false; + data->read_time += config->debounce_scan_period_ms; + + k_work_schedule(&data->update_work, K_TIMEOUT_ABS_MS(data->read_time)); + } else { + bks_enable_interrupt(dev, false); + } + + k_sem_give(&data->sem); +} + +static void bks_update_work(struct k_work *work) { + struct k_work_delayable *dwork = CONTAINER_OF(work, struct k_work_delayable, work); + struct behavior_key_scanned_data *data = + CONTAINER_OF(dwork, struct behavior_key_scanned_data, update_work); + bks_read(data->dev); +} + +static void bks_gpio_irq_callback(const struct device *port, struct gpio_callback *cb, + const gpio_port_pins_t pin) { + struct behavior_key_scanned_data *data = + CONTAINER_OF(cb, struct behavior_key_scanned_data, key_callback); + const struct behavior_key_scanned_config *config = data->dev->config; + + uint32_t time = k_uptime_get(); + + if (k_sem_take(&data->sem, K_MSEC(10)) < 0) { + LOG_ERR("FAILED TO TAKE THE SEMAPHORE"); + // Do more? + return; + } + + data->active_scan_detected = true; + data->read_time = time; + + if (!zmk_debounce_is_active(&data->debounce_state)) { + // When we get that very first interrupt, we need to schedule the update checks to fall in + // between each of the real scans, so we can do our checks for state *after* each scan has + // occurred. + k_work_reschedule(&data->update_work, + K_TIMEOUT_ABS_MS(time + (config->debounce_scan_period_ms / 2))); + + bks_enable_interrupt(data->dev, true); + } + + k_sem_give(&data->sem); +} + +static int behavior_key_scanned_init(const struct device *dev) { + const struct behavior_key_scanned_config *config = dev->config; + struct behavior_key_scanned_data *data = dev->data; + + if (!device_is_ready(config->key.port)) { + LOG_ERR("GPIO port is not ready"); + return -ENODEV; + } + + k_work_init_delayable(&data->update_work, bks_update_work); + k_sem_init(&data->sem, 1, 1); + data->dev = dev; + + gpio_pin_configure_dt(&config->key, GPIO_INPUT); + gpio_init_callback(&data->key_callback, bks_gpio_irq_callback, BIT(config->key.pin)); + gpio_add_callback(config->key.port, &data->key_callback); + + while (gpio_pin_get_dt(&config->key)) { + k_sleep(K_MSEC(100)); + } + + bks_enable_interrupt(dev, false); + + return 0; +} + +static int behavior_key_scanned_pm_action(const struct device *dev, enum pm_device_action action) { + const struct behavior_key_scanned_config *config = dev->config; + struct behavior_key_scanned_data *data = dev->data; + + int ret; + + switch (action) { + case PM_DEVICE_ACTION_SUSPEND: + bks_disable_interrupt(dev); + ret = gpio_remove_callback(config->key.port, &data->key_callback); + break; + case PM_DEVICE_ACTION_RESUME: + ret = gpio_add_callback(config->key.port, &data->key_callback); + bks_enable_interrupt(dev, false); + break; + default: + ret = -ENOTSUP; + break; + } + + return ret; +} + +#define BK_INST(n) \ + const struct behavior_key_scanned_config bks_config_##n = { \ + .key = GPIO_DT_SPEC_GET(DT_INST_PHANDLE(n, key), gpios), \ + .debounce_config = \ + { \ + .debounce_press_ms = DT_INST_PROP(n, debounce_press_ms), \ + .debounce_release_ms = DT_INST_PROP(n, debounce_release_ms), \ + }, \ + .debounce_scan_period_ms = DT_INST_PROP(n, debounce_scan_period_ms), \ + }; \ + struct behavior_key_scanned_data bks_data_##n = { \ + .binding = ZMK_KEYMAP_EXTRACT_BINDING(0, DT_DRV_INST(n)), \ + }; \ + PM_DEVICE_DT_INST_DEFINE(n, behavior_key_scanned_pm_action); \ + DEVICE_DT_INST_DEFINE(n, behavior_key_scanned_init, PM_DEVICE_DT_INST_GET(n), &bks_data_##n, \ + &bks_config_##n, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, \ + NULL); + +DT_INST_FOREACH_STATUS_OKAY(BK_INST) diff --git a/app/src/kscan.c b/app/src/kscan.c index 62d0cf0756e..409093c79f4 100644 --- a/app/src/kscan.c +++ b/app/src/kscan.c @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -75,6 +76,11 @@ int zmk_kscan_init(const struct device *dev) { kscan_config(dev, zmk_kscan_callback); kscan_enable_callback(dev); +#if IS_ENABLED(CONFIG_PM_DEVICE) + if (pm_device_wakeup_is_capable(dev)) { + pm_device_wakeup_enable(dev, true); + } +#endif // IS_ENABLED(CONFIG_PM_DEVICE) return 0; } diff --git a/app/src/pm.c b/app/src/pm.c new file mode 100644 index 00000000000..af12623918f --- /dev/null +++ b/app/src/pm.c @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2023 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#include +#include +#include +#include +#include + +#include +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + +#define HAS_WAKERS DT_HAS_COMPAT_STATUS_OKAY(zmk_soft_off_wakeup_sources) + +#if HAS_WAKERS + +#define DEVICE_WITH_SEP(node_id, prop, idx) DEVICE_DT_GET(DT_PROP_BY_IDX(node_id, prop, idx)), + +const struct device *soft_off_wakeup_sources[] = { + DT_FOREACH_PROP_ELEM(DT_INST(0, zmk_soft_off_wakeup_sources), wakeup_sources, DEVICE_WITH_SEP)}; + +#endif + +int zmk_pm_soft_off(void) { +#if IS_ENABLED(CONFIG_PM_DEVICE) + size_t device_count; + const struct device *devs; + + device_count = z_device_get_all_static(&devs); + + // There may be some matrix/direct kscan devices that would be used for wakeup + // from normal "inactive goes to sleep" behavior, so disable them as wakeup devices + // and then suspend them so we're ready to take over setting up our system + // and then putting it into an off state. + for (int i = 0; i < device_count; i++) { + const struct device *dev = &devs[i]; + + LOG_DBG("soft-on-off pressed cb: suspend device"); + if (pm_device_wakeup_is_enabled(dev)) { + pm_device_wakeup_enable(dev, false); + } + pm_device_action_run(dev, PM_DEVICE_ACTION_SUSPEND); + } +#endif // IS_ENABLED(CONFIG_PM_DEVICE) + +#if HAS_WAKERS + for (int i = 0; i < ARRAY_SIZE(soft_off_wakeup_sources); i++) { + const struct device *dev = soft_off_wakeup_sources[i]; + pm_device_wakeup_enable(dev, true); + pm_device_action_run(dev, PM_DEVICE_ACTION_RESUME); + } +#endif // HAS_WAKERS + + LOG_DBG("soft-on-off interrupt: go to sleep"); + return pm_state_force(0U, &(struct pm_state_info){PM_STATE_SOFT_OFF, 0, 0}); +} diff --git a/app/src/wakeup_trigger_key.c b/app/src/wakeup_trigger_key.c new file mode 100644 index 00000000000..0cc4f250707 --- /dev/null +++ b/app/src/wakeup_trigger_key.c @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2023 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#include +#include +#include +#include +#include + +#include + +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + +#define DT_DRV_COMPAT zmk_wakeup_trigger_key + +struct wakeup_trigger_key_config { + struct gpio_dt_spec trigger; + size_t extra_gpios_count; + struct gpio_dt_spec extra_gpios[]; +}; + +static int zmk_wakeup_trigger_key_init(const struct device *dev) { +#if IS_ENABLED(CONFIG_PM_DEVICE) + pm_device_init_suspended(dev); + pm_device_wakeup_enable(dev, true); +#endif + + return 0; +} + +#if IS_ENABLED(CONFIG_PM_DEVICE) + +static int wakeup_trigger_key_pm_action(const struct device *dev, enum pm_device_action action) { + const struct wakeup_trigger_key_config *config = dev->config; + int ret = 0; + + switch (action) { + case PM_DEVICE_ACTION_RESUME: + ret = gpio_pin_interrupt_configure_dt(&config->trigger, GPIO_INT_LEVEL_ACTIVE); + if (ret < 0) { + LOG_ERR("Failed to configure wakeup trigger key GPIO pin interrupt (%d)", ret); + return ret; + } + + for (int i = 0; i < config->extra_gpios_count; i++) { + ret = gpio_pin_configure_dt(&config->extra_gpios[i], GPIO_OUTPUT_ACTIVE); + if (ret < 0) { + LOG_WRN("Failed to set extra GPIO pin active for waker (%d)", ret); + } + } + break; + case PM_DEVICE_ACTION_SUSPEND: + + ret = gpio_pin_interrupt_configure_dt(&config->trigger, GPIO_INT_DISABLE); + if (ret < 0) { + LOG_ERR("Failed to configure wakeup trigger key GPIO pin interrupt (%d)", ret); + return ret; + } + break; + default: + ret = -ENOTSUP; + break; + } + + return ret; +} + +#endif // IS_ENABLED(CONFIG_PM_DEVICE) + +#define WAKEUP_TRIGGER_EXTRA_GPIO_SPEC(idx, n) \ + GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(n), extra_gpios, idx) + +#define WAKEUP_TRIGGER_KEY_INST(n) \ + const struct wakeup_trigger_key_config wtk_cfg_##n = { \ + .trigger = GPIO_DT_SPEC_GET(DT_INST_PROP(n, trigger), gpios), \ + .extra_gpios = {LISTIFY(DT_PROP_LEN_OR(DT_DRV_INST(n), extra_gpios, 0), \ + WAKEUP_TRIGGER_EXTRA_GPIO_SPEC, (, ), n)}, \ + .extra_gpios_count = DT_PROP_LEN_OR(DT_DRV_INST(n), extra_gpios, 0), \ + }; \ + PM_DEVICE_DT_INST_DEFINE(n, wakeup_trigger_key_pm_action); \ + DEVICE_DT_INST_DEFINE(n, zmk_wakeup_trigger_key_init, PM_DEVICE_DT_INST_GET(n), NULL, \ + &wtk_cfg_##n, PRE_KERNEL_2, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, NULL); + +DT_INST_FOREACH_STATUS_OKAY(WAKEUP_TRIGGER_KEY_INST) diff --git a/docs/docs/behaviors/soft-off.md b/docs/docs/behaviors/soft-off.md new file mode 100644 index 00000000000..05374004955 --- /dev/null +++ b/docs/docs/behaviors/soft-off.md @@ -0,0 +1,38 @@ +--- +title: Soft Off Behavior +sidebar_label: Soft Off +--- + +## Summary + +The soft off behavior is used to force the keyboard into an off state. Depending on the specific keyboard hardware, the keyboard can be turned back on again either with a dedicated on/off button that is available, or using the reset button found on the device. + +For more information, see the [Soft Off Feature](../features/soft-off.md) page. + +### Behavior Binding + +- Reference: `&soft_off` + +Example: + +``` +&soft_off +``` + +### Configuration + +#### Hold Time + +By default, the keyboard will be turned off as soon as the key bound to the behavior is released, even if the key is only tapped briefly. If you would prefer that the key need be held a certain amount of time before releasing, you can set the `hold-time-ms` to a non-zero value in your keymap: + +``` +&soft_off { + hold-time-ms = <5000>; // Only turn off it the key is held for 5 seconds or longer. +}; + +/ { + keymap { + ... + }; +}; +``` diff --git a/docs/docs/development/new-shield.md b/docs/docs/development/new-shield.md index dd63fa9b880..853f21bcfe0 100644 --- a/docs/docs/development/new-shield.md +++ b/docs/docs/development/new-shield.md @@ -167,6 +167,7 @@ this might look something like: kscan0: kscan_0 { compatible = "zmk,kscan-gpio-matrix"; diode-direction = "col2row"; + wakeup-source; col-gpios = <&pro_micro 15 GPIO_ACTIVE_HIGH> diff --git a/docs/docs/features/soft-off.md b/docs/docs/features/soft-off.md new file mode 100644 index 00000000000..b0206825cbc --- /dev/null +++ b/docs/docs/features/soft-off.md @@ -0,0 +1,164 @@ +--- +title: Soft Off Feature +sidebar_label: Soft Off +--- + +Similar to the deep sleep feature that sends the keyboard into a low power state after a certain period of inactivity, the soft off feature is used to turn the keyboard on and off explicitly. Depending on the keyboard, this may be through a dedicated on/off push button, or merely through an additional binding in the keymap to turn the device off and the existing reset button to turn the device back on. + +The feature is intended as an alternative to using a hardware switch to physically cut power from the battery to the keyboard. This can be useful for existing PCBs not designed for wireless that don't have a power switch, or for new designs that favor a push button on/off like found on other devices. + +:::note + +The power off is accomplished by putting the MCU into a "soft off" state. Power is _not_ technically removed from the entire system, but the device will only be woken from the state by a few possible events. + +::: + +Once powered off, the keyboard will only wake up when: + +- You press the same button/sequence that you pressed to power off the keyboard, or +- You press a reset button found on the keyboard. + +## Soft Off With Existing Designs + +For existing designs, using soft off is as simple as placing the [Soft Off Behavior](../behaviors/soft-off.md) in your keymap and then invoking it. For splits, at least for now, you'll need to place it somewhere on each side of your keymap and trigger on both sides, starting from the peripheral side first. + +You can then wake up the keyboard by pressing the reset button once, and repeating this for each side for split keyboards. + +## Adding Soft On/Off To New Designs + +### Hardware Design + +ZMK's soft on/off requires a dedicated GPIO pin to be used to trigger powering off, and to wake the core from the +soft off state when it goes active again later. + +#### Simple Direct Pin + +The simplest way to achieve this is with a push button between a GPIO pin and ground. + +#### Matrix-Integrated Hardware Combo + +Another, more complicated option is to tie two of the switch outputs in the matrix together through an AND gate and connect that to the dedicated GPIO pin. This way you can use a key combination in your existing keyboard matrix to trigger soft on/off. To make this work best, the two switches used should both be driven by the same matrix input pin so that both will be active simultaneously on the AND gate inputs. The alternative is to use a combination of diodes and capacitors to ensure both pins are active/high at the same time even if scanning sets them high at different times. + +### Firmware Changes + +Several items work together to make both triggering soft off properly, and setting up the device to _wake_ from soft off work as expected. + +#### GPIO Key + +Zephyr's basic GPIO Key concept is used to configure the GPIO pin that will be used for both triggering soft off and waking the device later. Here is an example for a keyboard with a dedicated on/off push button that is a direct wire between the GPIO pin and ground: + +``` +/ { + keys { + compatible = "gpio-keys"; + wakeup_key: wakeup_key { + gpios = <&gpio0 2 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)>; + }; + }; +}; +``` + +GPIO keys are defined using child nodes under the `gpio-keys` compatible node. Each child needs just one property defined: + +- The `gpios` property should be a phandle-array with a fully defined GPIO pin and with the correct pull up/down and active high/low flags set. In the above example the soft on/off would be triggered by pulling the specified pin low, typically by pressing a switch that has the other leg connected to ground. + +#### Behavior Key + +Next, we will create a new "behavior key". Behavior keys are an easy way to tie a keymap behavior to a GPIO key outside of the normal keymap processing. They do _not_ do the normal keymap processing, so they are only suitable for use with basic behaviors, not complicated macros, hold-taps, etc. + +In this case, we will be creating a dedicated instance of the [Soft Off Behavior](../behaviors/soft-off.md) that will be used only for our hardware on/off button, then binding it to our key: + +``` +/ { + behaviors { + hw_soft_off: behavior_hw_soft_off { + compatible = "zmk,behavior-soft-off"; + #binding-cells = <0>; + label = "HW_SO"; + hold-time-ms = <5000>; + }; + }; + + soft_off_behavior_key { + compatible = "zmk,behavior-key"; + bindings = <&hw_soft_off>; + key = <&wakeup_key>; + }; +}; +``` + +Here are the properties for the behavior key node: + +- The `compatible` property for the node must be `zmk,behavior-key`. +- The `bindings` property is a phandle to the soft off behavior defined above. +- The `key` property is a phandle to the GPIO key defined earlier. + +If you have set up your on/off to be controlled by a matrix-integrated combo, the behavior key needs use a different driver that will handle detecting the pressed state when the pin is toggled by the other matrix kscan driver: + +``` +/ { + soft_off_behavior_key { + compatible = "zmk,behavior-key-scanned"; + status = "okay"; + bindings = <&hw_soft_off>; + key = <&wakeup_key>; + }; +}; +``` + +Note that the only difference from the `soft_off_behavior_key` definition for GPIO keys above is the `compatible` value of `zmk,behavior-key-scanned`. + +#### Wakeup Sources + +Zephyr has general support for the concept of a device as a "wakeup source", which ZMK has not previously used. Adding soft off requires properly updating the existing `kscan` devices with the `wakeup-source` property, e.g.: + +``` +/ { + kscan0: kscan_0 { + compatible = "zmk,kscan-gpio-matrix"; + label = "KSCAN"; + diode-direction = "col2row"; + wakeup-source; + + ... + }; +}; +``` + +#### Soft Off Waker + +Next, we need to add another device which will be enabled only when the keyboard is going into soft off state, and will configure the previously declared GPIO key with the correct interrupt configuration to wake the device from soft off once it is pressed. + +``` +/ { + wakeup_source: wakeup_source { + compatible = "zmk,wakeup-trigger-key"; + + trigger = <&wakeup_key>; + wakeup-source; + }; +}; +``` + +Here are the properties for the node: + +- The `compatible` property for the node must be `zmk,wakeup-trigger-key`. +- The `trigger` property is a phandle to the GPIO key defined earlier. +- The `wakeup-source` property signals to Zephyr this device should not be suspended during the shutdown procedure. +- An optional `output-gpios` property contains a list of GPIO pins (including the appropriate flags) to set active before going into power off, if needed to ensure the GPIO pin will trigger properly to wake the keyboard. This is only needed for matrix integrated combos. For those keyboards, the list should include the matrix output needs needed so the combo hardware is properly "driven" when the keyboard is off. + +Once that is declared, we will list it in an additional configuration section so that the ZMK soft off process knows it needs to enable this device as part of the soft off processing: + +``` +/ { + soft_off_wakers { + compatible = "zmk,soft-off-wakeup-sources"; + wakeup-sources = <&wakeup_source>; + }; +}; +``` + +Here are the properties for the node: + +- The `compatible` property for the node must be `zmk,soft-off-wakeup-sources`. +- The `wakeup-sources` property is a [phandle array](../config/index.md#devicetree-property-types) pointing to all the devices that should be enabled during the shutdown process to be sure they can later wake the keyboard. diff --git a/docs/sidebars.js b/docs/sidebars.js index 19b0ad7e68c..4827c8fb1d7 100644 --- a/docs/sidebars.js +++ b/docs/sidebars.js @@ -19,6 +19,7 @@ module.exports = { "features/underglow", "features/backlight", "features/battery", + "features/soft-off", "features/beta-testing", ], Behaviors: [ @@ -43,6 +44,7 @@ module.exports = { "behaviors/underglow", "behaviors/backlight", "behaviors/power", + "behaviors/soft-off", ], Codes: [ "codes/index", From bdea7b107a59f4ddca40a3b0d7a42d8cb108de69 Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Wed, 13 Sep 2023 19:58:53 +0000 Subject: [PATCH 02/10] feat(kscan): Add PM support to GPIO kscan drivers. * Add PM device hook to the kscan direct & matrix drivers. --- app/module/drivers/kscan/kscan_gpio_direct.c | 27 +++++++++++++++++++- app/module/drivers/kscan/kscan_gpio_matrix.c | 27 +++++++++++++++++++- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/app/module/drivers/kscan/kscan_gpio_direct.c b/app/module/drivers/kscan/kscan_gpio_direct.c index 5b227784dad..c44debe54de 100644 --- a/app/module/drivers/kscan/kscan_gpio_direct.c +++ b/app/module/drivers/kscan/kscan_gpio_direct.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -318,6 +319,28 @@ static int kscan_direct_init(const struct device *dev) { return 0; } +#if IS_ENABLED(CONFIG_PM_DEVICE) + +static int kscan_direct_pm_action(const struct device *dev, enum pm_device_action action) { + int ret = 0; + + switch (action) { + case PM_DEVICE_ACTION_SUSPEND: + kscan_direct_disable(dev); + break; + case PM_DEVICE_ACTION_RESUME: + kscan_direct_enable(dev); + break; + default: + ret = -ENOTSUP; + break; + } + + return ret; +} + +#endif // IS_ENABLED(CONFIG_PM_DEVICE) + static const struct kscan_driver_api kscan_direct_api = { .config = kscan_direct_configure, .enable_callback = kscan_direct_enable, @@ -354,7 +377,9 @@ static const struct kscan_driver_api kscan_direct_api = { .toggle_mode = DT_INST_PROP(n, toggle_mode), \ }; \ \ - DEVICE_DT_INST_DEFINE(n, &kscan_direct_init, NULL, &kscan_direct_data_##n, \ + PM_DEVICE_DT_INST_DEFINE(n, kscan_direct_pm_action); \ + \ + DEVICE_DT_INST_DEFINE(n, &kscan_direct_init, PM_DEVICE_DT_INST_GET(n), &kscan_direct_data_##n, \ &kscan_direct_config_##n, POST_KERNEL, CONFIG_KSCAN_INIT_PRIORITY, \ &kscan_direct_api); diff --git a/app/module/drivers/kscan/kscan_gpio_matrix.c b/app/module/drivers/kscan/kscan_gpio_matrix.c index 0d8a3190659..de2e1127920 100644 --- a/app/module/drivers/kscan/kscan_gpio_matrix.c +++ b/app/module/drivers/kscan/kscan_gpio_matrix.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -421,6 +422,28 @@ static int kscan_matrix_init(const struct device *dev) { return 0; } +#if IS_ENABLED(CONFIG_PM_DEVICE) + +static int kscan_matrix_pm_action(const struct device *dev, enum pm_device_action action) { + int ret = 0; + + switch (action) { + case PM_DEVICE_ACTION_SUSPEND: + kscan_matrix_disable(dev); + break; + case PM_DEVICE_ACTION_RESUME: + kscan_matrix_enable(dev); + break; + default: + ret = -ENOTSUP; + break; + } + + return ret; +} + +#endif // IS_ENABLED(CONFIG_PM_DEVICE) + static const struct kscan_driver_api kscan_matrix_api = { .config = kscan_matrix_configure, .enable_callback = kscan_matrix_enable, @@ -465,7 +488,9 @@ static const struct kscan_driver_api kscan_matrix_api = { .diode_direction = INST_DIODE_DIR(n), \ }; \ \ - DEVICE_DT_INST_DEFINE(n, &kscan_matrix_init, NULL, &kscan_matrix_data_##n, \ + PM_DEVICE_DT_INST_DEFINE(n, kscan_matrix_pm_action); \ + \ + DEVICE_DT_INST_DEFINE(n, &kscan_matrix_init, PM_DEVICE_DT_INST_GET(n), &kscan_matrix_data_##n, \ &kscan_matrix_config_##n, POST_KERNEL, CONFIG_KSCAN_INIT_PRIORITY, \ &kscan_matrix_api); From a4fd551c6b3d1f9ad3fc8c7af1752c2cb0f0601f Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Wed, 13 Sep 2023 20:01:02 +0000 Subject: [PATCH 03/10] feat(behaviors): Add soft off behavior. * New soft-off behavior that can be used to force the device into soft-off state with only certain configured wakeup devices. --- app/CMakeLists.txt | 1 + app/Kconfig.behaviors | 6 ++ app/dts/behaviors/soft_off.dtsi | 3 +- .../behaviors/zmk,behavior-soft-off.yaml | 14 ++++ app/src/behaviors/behavior_soft_off.c | 70 +++++++++++++++++++ 5 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 app/dts/bindings/behaviors/zmk,behavior-soft-off.yaml create mode 100644 app/src/behaviors/behavior_soft_off.c diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index d14614fc484..dddb52268da 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -35,6 +35,7 @@ target_sources_ifdef(CONFIG_ZMK_WPM app PRIVATE src/events/wpm_state_changed.c) target_sources_ifdef(CONFIG_USB_DEVICE_STACK app PRIVATE src/events/usb_conn_state_changed.c) target_sources(app PRIVATE src/behaviors/behavior_reset.c) target_sources_ifdef(CONFIG_ZMK_EXT_POWER app PRIVATE src/behaviors/behavior_ext_power.c) +target_sources_ifdef(CONFIG_ZMK_BEHAVIOR_SOFT_OFF app PRIVATE src/behaviors/behavior_soft_off.c) if ((NOT CONFIG_ZMK_SPLIT) OR CONFIG_ZMK_SPLIT_ROLE_CENTRAL) target_sources(app PRIVATE src/hid.c) target_sources_ifdef(CONFIG_ZMK_MOUSE app PRIVATE src/mouse.c) diff --git a/app/Kconfig.behaviors b/app/Kconfig.behaviors index 9d1773c3d1f..2b1304b2770 100644 --- a/app/Kconfig.behaviors +++ b/app/Kconfig.behaviors @@ -22,6 +22,12 @@ config ZMK_BEHAVIOR_MOUSE_KEY_PRESS depends on DT_HAS_ZMK_BEHAVIOR_MOUSE_KEY_PRESS_ENABLED imply ZMK_MOUSE +config ZMK_BEHAVIOR_SOFT_OFF + bool + default y + select ZMK_PM_SOFT_OFF + depends on DT_HAS_ZMK_BEHAVIOR_SOFT_OFF_ENABLED + config ZMK_BEHAVIOR_SENSOR_ROTATE_COMMON bool default n diff --git a/app/dts/behaviors/soft_off.dtsi b/app/dts/behaviors/soft_off.dtsi index fa6571a1117..c68230f3231 100644 --- a/app/dts/behaviors/soft_off.dtsi +++ b/app/dts/behaviors/soft_off.dtsi @@ -6,9 +6,8 @@ / { behaviors { - /omit-if-no-ref/ soft_off: behavior_soft_off { + soft_off: soft_off { compatible = "zmk,behavior-soft-off"; - label = "SOFTOFF"; #binding-cells = <0>; }; }; diff --git a/app/dts/bindings/behaviors/zmk,behavior-soft-off.yaml b/app/dts/bindings/behaviors/zmk,behavior-soft-off.yaml new file mode 100644 index 00000000000..1467ede4772 --- /dev/null +++ b/app/dts/bindings/behaviors/zmk,behavior-soft-off.yaml @@ -0,0 +1,14 @@ +# Copyright (c) 2023 The ZMK Contributors +# SPDX-License-Identifier: MIT + +description: Soft-Off Behavior + +compatible: "zmk,behavior-soft-off" + +include: zero_param.yaml + +properties: + hold-time-ms: + type: int + required: false + description: Number of milliseconds the behavior must be held before releasing will actually trigger a soft-off. diff --git a/app/src/behaviors/behavior_soft_off.c b/app/src/behaviors/behavior_soft_off.c new file mode 100644 index 00000000000..0f24a644d9a --- /dev/null +++ b/app/src/behaviors/behavior_soft_off.c @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#define DT_DRV_COMPAT zmk_behavior_soft_off + +#include +#include +#include + +#include +#include + +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + +struct behavior_soft_off_config { + uint32_t hold_time_ms; +}; + +struct behavior_soft_off_data { + uint32_t press_start; +}; + +static int behavior_soft_off_init(const struct device *dev) { return 0; }; + +static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding, + struct zmk_behavior_binding_event event) { + const struct device *dev = zmk_behavior_get_binding(binding->behavior_dev); + struct behavior_soft_off_data *data = dev->data; + +#if IS_ENABLED(CONFIG_ZMK_SPLIT) && !IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL) + zmk_pm_soft_off(); +#else + data->press_start = k_uptime_get(); +#endif + + return ZMK_BEHAVIOR_OPAQUE; +} + +static int on_keymap_binding_released(struct zmk_behavior_binding *binding, + struct zmk_behavior_binding_event event) { + const struct device *dev = zmk_behavior_get_binding(binding->behavior_dev); + struct behavior_soft_off_data *data = dev->data; + const struct behavior_soft_off_config *config = dev->config; + + if (config->hold_time_ms == 0 || (k_uptime_get() - data->press_start) >= config->hold_time_ms) { + zmk_pm_soft_off(); + } + + return ZMK_BEHAVIOR_OPAQUE; +} + +static const struct behavior_driver_api behavior_soft_off_driver_api = { + .binding_pressed = on_keymap_binding_pressed, + .binding_released = on_keymap_binding_released, + .locality = BEHAVIOR_LOCALITY_GLOBAL, +}; + +#define BSO_INST(n) \ + static const struct behavior_soft_off_config bso_config_##n = { \ + .hold_time_ms = DT_INST_PROP_OR(n, hold_time_ms, 0), \ + }; \ + static struct behavior_soft_off_data bso_data_##n = {}; \ + BEHAVIOR_DT_INST_DEFINE(0, behavior_soft_off_init, NULL, &bso_data_##n, &bso_config_##n, \ + APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, \ + &behavior_soft_off_driver_api); + +DT_INST_FOREACH_STATUS_OKAY(BSO_INST) From 8614a9c1012b964bed1ae99ce9d59cd4fde7d295 Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Sat, 16 Sep 2023 11:50:12 -0700 Subject: [PATCH 04/10] feat(shields): Add soft-off to the nrf52840dk ZMK Uno * Use Button 1 for soft off on the nrf52840 when using the ZMK Uno shield. --- .../boards/nrf52840dk_nrf52840.overlay | 44 ++++++++++++++++++- app/boards/shields/zmk_uno/zmk_uno.dtsi | 2 + 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/app/boards/shields/zmk_uno/boards/nrf52840dk_nrf52840.overlay b/app/boards/shields/zmk_uno/boards/nrf52840dk_nrf52840.overlay index 5ac7af7c532..d08105c6889 100644 --- a/app/boards/shields/zmk_uno/boards/nrf52840dk_nrf52840.overlay +++ b/app/boards/shields/zmk_uno/boards/nrf52840dk_nrf52840.overlay @@ -12,6 +12,15 @@ bias-pull-up; }; }; + + qdec_sleep: qdec_sleep { + group1 { + psels = , + ; + bias-pull-up; + low-power-enable; + }; + }; }; // Set up the QDEC hardware based driver and give it the same label as the deleted node. @@ -20,5 +29,38 @@ encoder: &qdec0 { led-pre = <0>; steps = <80>; pinctrl-0 = <&qdec_default>; - pinctrl-names = "default"; + pinctrl-1 = <&qdec_sleep>; + pinctrl-names = "default", "sleep"; }; + +/ { + behaviors { + soft_off: soft_off { + compatible = "zmk,behavior-soft-off"; + #binding-cells = <0>; + status = "okay"; + }; + }; + + wakeup_source: wakeup_source { + compatible = "zmk,wakeup-trigger-key"; + status = "okay"; + + trigger = <&button0>; + wakeup-source; + }; + + soft_off_wakers { + compatible = "zmk,soft-off-wakeup-sources"; + status = "okay"; + + wakeup-sources = <&wakeup_source>; + }; + + soft_off_behavior_key { + compatible = "zmk,behavior-key"; + status = "okay"; + bindings = <&soft_off>; + key = <&button0>; + }; +}; \ No newline at end of file diff --git a/app/boards/shields/zmk_uno/zmk_uno.dtsi b/app/boards/shields/zmk_uno/zmk_uno.dtsi index 63deb06a4d1..196ac8b5002 100644 --- a/app/boards/shields/zmk_uno/zmk_uno.dtsi +++ b/app/boards/shields/zmk_uno/zmk_uno.dtsi @@ -124,6 +124,7 @@ nice_view_spi: &arduino_spi { kscan_matrix: kscan_matrix { compatible = "zmk,kscan-gpio-matrix"; + wakeup-source; diode-direction = "col2row"; @@ -141,6 +142,7 @@ nice_view_spi: &arduino_spi { kscan_direct: kscan_direct { compatible = "zmk,kscan-gpio-direct"; + wakeup-source; status = "disabled"; input-gpios From 5ef5208e4aafad57ade2c23ef3e53243e5ab7edc Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Thu, 7 Dec 2023 00:36:02 +0000 Subject: [PATCH 05/10] refactor: Promote new endpoints API * Add ability for external callers to clear the current endpoint. --- app/include/zmk/endpoints.h | 2 ++ app/src/endpoints.c | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/include/zmk/endpoints.h b/app/include/zmk/endpoints.h index 70240183e36..f2aff2bcc2d 100644 --- a/app/include/zmk/endpoints.h +++ b/app/include/zmk/endpoints.h @@ -73,3 +73,5 @@ int zmk_endpoints_send_report(uint16_t usage_page); #if IS_ENABLED(CONFIG_ZMK_MOUSE) int zmk_endpoints_send_mouse_report(); #endif // IS_ENABLE(CONFIG_ZMK_MOUSE) + +void zmk_endpoints_clear_current(void); diff --git a/app/src/endpoints.c b/app/src/endpoints.c index 098e04e2776..3deb3be26b7 100644 --- a/app/src/endpoints.c +++ b/app/src/endpoints.c @@ -322,7 +322,7 @@ static int zmk_endpoints_init(const struct device *_arg) { return 0; } -static void disconnect_current_endpoint() { +void zmk_endpoints_clear_current(void) { zmk_hid_keyboard_clear(); zmk_hid_consumer_clear(); #if IS_ENABLED(CONFIG_ZMK_MOUSE) @@ -338,7 +338,7 @@ static void update_current_endpoint(void) { if (!zmk_endpoint_instance_eq(new_instance, current_instance)) { // Cancel all current keypresses so keys don't stay held on the old endpoint. - disconnect_current_endpoint(); + zmk_endpoints_clear_current(); current_instance = new_instance; From ea6473522185bb49c02fce477e7b52f6bfe77032 Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Thu, 7 Dec 2023 00:37:31 +0000 Subject: [PATCH 06/10] feat(pm): Clear HID data before soft off. * Make sure the connected host has no held HID usages before we sleep. --- app/src/pm.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app/src/pm.c b/app/src/pm.c index af12623918f..a78b6ae53cb 100644 --- a/app/src/pm.c +++ b/app/src/pm.c @@ -13,6 +13,8 @@ #include LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); +#include + #define HAS_WAKERS DT_HAS_COMPAT_STATUS_OKAY(zmk_soft_off_wakeup_sources) #if HAS_WAKERS @@ -29,6 +31,10 @@ int zmk_pm_soft_off(void) { size_t device_count; const struct device *devs; +#if !IS_ENABLED(CONFIG_ZMK_SPLIT) || IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL) + zmk_endpoints_clear_current(); +#endif + device_count = z_device_get_all_static(&devs); // There may be some matrix/direct kscan devices that would be used for wakeup From ae0f0d866ad9554aea6e0e085c7b15bc535df4cd Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Thu, 7 Dec 2023 04:04:13 +0000 Subject: [PATCH 07/10] fix(bt): Fix BT tests after soft off work. * Move to explicit enable of `ZMK_PM_SOFT_OFF` to turn on the feature and use the behaviors, which matches how other features work, and helps with split and testing schemes. --- app/Kconfig.behaviors | 3 +-- .../shields/zmk_uno/boards/nrf52840dk_nrf52840.conf | 1 + app/dts/behaviors/soft_off.dtsi | 3 ++- docs/docs/config/power.md | 12 ++++++++++++ 4 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 app/boards/shields/zmk_uno/boards/nrf52840dk_nrf52840.conf diff --git a/app/Kconfig.behaviors b/app/Kconfig.behaviors index 2b1304b2770..6c1981bea8c 100644 --- a/app/Kconfig.behaviors +++ b/app/Kconfig.behaviors @@ -25,8 +25,7 @@ config ZMK_BEHAVIOR_MOUSE_KEY_PRESS config ZMK_BEHAVIOR_SOFT_OFF bool default y - select ZMK_PM_SOFT_OFF - depends on DT_HAS_ZMK_BEHAVIOR_SOFT_OFF_ENABLED + depends on DT_HAS_ZMK_BEHAVIOR_SOFT_OFF_ENABLED && ZMK_PM_SOFT_OFF config ZMK_BEHAVIOR_SENSOR_ROTATE_COMMON bool diff --git a/app/boards/shields/zmk_uno/boards/nrf52840dk_nrf52840.conf b/app/boards/shields/zmk_uno/boards/nrf52840dk_nrf52840.conf new file mode 100644 index 00000000000..ac92c4d8f00 --- /dev/null +++ b/app/boards/shields/zmk_uno/boards/nrf52840dk_nrf52840.conf @@ -0,0 +1 @@ +CONFIG_ZMK_PM_SOFT_OFF=y \ No newline at end of file diff --git a/app/dts/behaviors/soft_off.dtsi b/app/dts/behaviors/soft_off.dtsi index c68230f3231..c88e1b51f12 100644 --- a/app/dts/behaviors/soft_off.dtsi +++ b/app/dts/behaviors/soft_off.dtsi @@ -6,8 +6,9 @@ / { behaviors { - soft_off: soft_off { + /omit-if-no-ref/ soft_off: soft_off { compatible = "zmk,behavior-soft-off"; + label = "SOFTOFF"; #binding-cells = <0>; }; }; diff --git a/docs/docs/config/power.md b/docs/docs/config/power.md index 75e1b26ab8b..396456b2589 100644 --- a/docs/docs/config/power.md +++ b/docs/docs/config/power.md @@ -24,6 +24,18 @@ Definition file: [zmk/app/Kconfig](https://github.com/zmkfirmware/zmk/blob/main/ | `CONFIG_ZMK_SLEEP` | bool | Enable deep sleep support | n | | `CONFIG_ZMK_IDLE_SLEEP_TIMEOUT` | int | Milliseconds of inactivity before entering deep sleep | 900000 | +## Soft Off + +The [soft off feature](../features/soft-off.md) allows turning the keyboard on/off from either dedicated hardware of using the [`&soft_off` behavior](../behaviors/soft-off.md) to turn off and a reset button to turn back on again. + +### Kconfig + +Definition file: [zmk/app/Kconfig](https://github.com/zmkfirmware/zmk/blob/main/app/Kconfig) + +| Config | Type | Description | Default | +| ------------------------ | ---- | ------------------------------------------------------------------- | ------- | +| `CONFIG_ZMK_PM_SOFT_OFF` | bool | Enable soft off functionality from the keymap of dedicated hardware | n | + ## External Power Control Driver for enabling or disabling power to peripherals such as displays and lighting. This driver must be configured to use [power management behaviors](../behaviors/power.md). From a7851d01ee5c610c3e5d40d0c3e4c04f0fbee34c Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Sat, 9 Dec 2023 17:18:04 -0500 Subject: [PATCH 08/10] fix(docs): Apply suggestions from code review Co-authored-by: Cem Aksoylar --- docs/docs/config/kscan.md | 8 +++++++- docs/docs/config/power.md | 4 ++-- docs/docs/features/soft-off.md | 9 ++++----- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/docs/docs/config/kscan.md b/docs/docs/config/kscan.md index 65ea63ec8b7..052d64ce270 100644 --- a/docs/docs/config/kscan.md +++ b/docs/docs/config/kscan.md @@ -79,6 +79,7 @@ Definition file: [zmk/app/module/dts/bindings/kscan/zmk,kscan-gpio-direct.yaml]( | `debounce-scan-period-ms` | int | Time between reads in milliseconds when any key is pressed. | 1 | | `poll-period-ms` | int | Time between reads in milliseconds when no key is pressed and `CONFIG_ZMK_KSCAN_DIRECT_POLLING` is enabled. | 10 | | `toggle-mode` | bool | Use toggle switch mode. | n | +| `wakeup-source` | bool | Mark this kscan instance as able to wake the keyboard from deep sleep | n | By default, a switch will drain current through the internal pull up/down resistor whenever it is pressed. This is not ideal for a toggle switch, where the switch may be left in the "pressed" state for a long time. Enabling `toggle-mode` will make the driver flip between pull up and down as the switch is toggled to optimize for power. @@ -89,6 +90,7 @@ Assuming the switches connect each GPIO pin to the ground, the [GPIO flags](http ```dts kscan0: kscan { compatible = "zmk,kscan-gpio-direct"; + wakeup-source; input-gpios = <&pro_micro 4 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)> , <&pro_micro 5 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)> @@ -123,6 +125,7 @@ Definition file: [zmk/app/module/dts/bindings/kscan/zmk,kscan-gpio-matrix.yaml]( | `debounce-scan-period-ms` | int | Time between reads in milliseconds when any key is pressed. | 1 | | `diode-direction` | string | The direction of the matrix diodes | `"row2col"` | | `poll-period-ms` | int | Time between reads in milliseconds when no key is pressed and `CONFIG_ZMK_KSCAN_MATRIX_POLLING` is enabled. | 10 | +| `wakeup-source` | bool | Mark this kscan instance as able to wake the keyboard from deep sleep | n | The `diode-direction` property must be one of: @@ -137,6 +140,7 @@ The output pins (e.g. columns for `col2row`) should have the flag `GPIO_ACTIVE_H ```dts kscan0: kscan { compatible = "zmk,kscan-gpio-matrix"; + wakeup-source; diode-direction = "col2row"; col-gpios = <&pro_micro 4 GPIO_ACTIVE_HIGH> @@ -177,6 +181,7 @@ Definition file: [zmk/app/module/dts/bindings/kscan/zmk,kscan-gpio-charlieplex.y | `debounce-release-ms` | int | Debounce time for key release in milliseconds. | 5 | | `debounce-scan-period-ms` | int | Time between reads in milliseconds when any key is pressed. | 1 | | `poll-period-ms` | int | Time between reads in milliseconds when no key is pressed and `interrupt-gpois` is not set. | 10 | +| `wakeup-source` | bool | Mark this kscan instance as able to wake the keyboard from deep sleep | n | Define the transform with a [matrix transform](#matrix-transform). The row is always the driven pin, and the column always the receiving pin (input to the controller). For example, in `RC(5,0)` power flows from the 6th pin in `gpios` to the 1st pin in `gpios`. @@ -450,7 +455,8 @@ Note that the entire addressable space does not need to be mapped. }; kscan0: kscan { - compatible = "zmk,kscan-gpio-charlieplex"; + compatible = "zmk,kscan-gpio-charlieplex";k + wakeup-source; interrupt-gpios = <&pro_micro 21 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN) >; gpios diff --git a/docs/docs/config/power.md b/docs/docs/config/power.md index 396456b2589..1a142eb2e4b 100644 --- a/docs/docs/config/power.md +++ b/docs/docs/config/power.md @@ -26,7 +26,7 @@ Definition file: [zmk/app/Kconfig](https://github.com/zmkfirmware/zmk/blob/main/ ## Soft Off -The [soft off feature](../features/soft-off.md) allows turning the keyboard on/off from either dedicated hardware of using the [`&soft_off` behavior](../behaviors/soft-off.md) to turn off and a reset button to turn back on again. +The [soft off feature](../features/soft-off.md) allows turning the keyboard on/off from either dedicated hardware, or using the [`&soft_off` behavior](../behaviors/soft-off.md) to turn off and a reset button to turn back on again. ### Kconfig @@ -34,7 +34,7 @@ Definition file: [zmk/app/Kconfig](https://github.com/zmkfirmware/zmk/blob/main/ | Config | Type | Description | Default | | ------------------------ | ---- | ------------------------------------------------------------------- | ------- | -| `CONFIG_ZMK_PM_SOFT_OFF` | bool | Enable soft off functionality from the keymap of dedicated hardware | n | +| `CONFIG_ZMK_PM_SOFT_OFF` | bool | Enable soft off functionality from the keymap or dedicated hardware | n | ## External Power Control diff --git a/docs/docs/features/soft-off.md b/docs/docs/features/soft-off.md index b0206825cbc..a3a5d7a1cb7 100644 --- a/docs/docs/features/soft-off.md +++ b/docs/docs/features/soft-off.md @@ -20,15 +20,15 @@ Once powered off, the keyboard will only wake up when: ## Soft Off With Existing Designs -For existing designs, using soft off is as simple as placing the [Soft Off Behavior](../behaviors/soft-off.md) in your keymap and then invoking it. For splits, at least for now, you'll need to place it somewhere on each side of your keymap and trigger on both sides, starting from the peripheral side first. +For existing designs, using soft off is as simple as placing the [Soft Off Behavior](../behaviors/soft-off.md) in your keymap and then invoking it. You can then wake up the keyboard by pressing the reset button once, and repeating this for each side for split keyboards. -## Adding Soft On/Off To New Designs +## Adding Dedicated Soft On/Off GPIO Pin To New Designs ### Hardware Design -ZMK's soft on/off requires a dedicated GPIO pin to be used to trigger powering off, and to wake the core from the +ZMK's dedicated soft on/off pin feature requires a dedicated GPIO pin to be used to trigger powering off, and to wake the core from the soft off state when it goes active again later. #### Simple Direct Pin @@ -71,10 +71,9 @@ In this case, we will be creating a dedicated instance of the [Soft Off Behavior ``` / { behaviors { - hw_soft_off: behavior_hw_soft_off { + hw_soft_off: hw_soft_off { compatible = "zmk,behavior-soft-off"; #binding-cells = <0>; - label = "HW_SO"; hold-time-ms = <5000>; }; }; From b2a9990f89348051242f3f613a9a8138499c5682 Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Wed, 20 Dec 2023 18:08:40 -0800 Subject: [PATCH 09/10] refactor: Fixes for soft-off based on review. * Better naming for gpio-key behavior triggers. * Tweaks to scanned behavior trigger to avoid bad semaphore use, and reduce chance of issues with slowly scanned matrixes. * Various code cleanups of style issues. --- app/CMakeLists.txt | 6 +- app/Kconfig | 4 +- app/Kconfig.behaviors | 8 +- app/boards/shields/zmk_uno/Kconfig.defconfig | 3 + .../zmk_uno/boards/nrf52840dk_nrf52840.conf | 1 - .../boards/nrf52840dk_nrf52840.overlay | 4 +- app/dts/behaviors/soft_off.dtsi | 1 - ...aml => zmk,gpio-key-behavior-trigger.yaml} | 4 +- ....yaml => zmk,gpio-key-wakeup-trigger.yaml} | 2 +- ...mk,gpio-scanned-key-behavior-trigger.yaml} | 4 +- .../bindings/zmk,soft-off-wakeup-sources.yaml | 4 +- app/module/drivers/kscan/kscan_gpio_direct.c | 7 +- app/module/drivers/kscan/kscan_gpio_matrix.c | 7 +- app/src/behaviors/behavior_soft_off.c | 2 +- ...vior_key.c => gpio_key_behavior_trigger.c} | 88 +++++++------ app/src/gpio_key_wakeup_trigger.c | 96 +++++++++++++++ ....c => gpio_scanned_key_behavior_trigger.c} | 116 ++++++++---------- app/src/pm.c | 4 +- app/src/wakeup_trigger_key.c | 87 ------------- docs/docs/config/kscan.md | 2 +- docs/docs/features/soft-off.md | 12 +- 21 files changed, 230 insertions(+), 232 deletions(-) delete mode 100644 app/boards/shields/zmk_uno/boards/nrf52840dk_nrf52840.conf rename app/dts/bindings/{zmk,behavior-key.yaml => zmk,gpio-key-behavior-trigger.yaml} (86%) rename app/dts/bindings/{zmk,wakeup-trigger-key.yaml => zmk,gpio-key-wakeup-trigger.yaml} (90%) rename app/dts/bindings/{zmk,behavior-key-scanned.yaml => zmk,gpio-scanned-key-behavior-trigger.yaml} (86%) rename app/src/{behavior_key.c => gpio_key_behavior_trigger.c} (61%) create mode 100644 app/src/gpio_key_wakeup_trigger.c rename app/src/{behavior_key_scanned.c => gpio_scanned_key_behavior_trigger.c} (57%) delete mode 100644 app/src/wakeup_trigger_key.c diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index dddb52268da..23b0398f407 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -22,11 +22,11 @@ target_sources(app PRIVATE src/matrix_transform.c) target_sources(app PRIVATE src/sensors.c) target_sources_ifdef(CONFIG_ZMK_WPM app PRIVATE src/wpm.c) target_sources(app PRIVATE src/event_manager.c) -target_sources_ifdef(CONFIG_ZMK_BEHAVIOR_KEY app PRIVATE src/behavior_key.c) -target_sources_ifdef(CONFIG_ZMK_BEHAVIOR_KEY_SCANNED app PRIVATE src/behavior_key_scanned.c) +target_sources_ifdef(CONFIG_ZMK_GPIO_KEY_BEHAVIOR_TRIGGER app PRIVATE src/gpio_key_behavior_trigger.c) +target_sources_ifdef(CONFIG_ZMK_GPIO_SCANNED_KEY_BEHAVIOR_TRIGGER app PRIVATE src/gpio_scanned_key_behavior_trigger.c) target_sources_ifdef(CONFIG_ZMK_PM_SOFT_OFF app PRIVATE src/pm.c) target_sources_ifdef(CONFIG_ZMK_EXT_POWER app PRIVATE src/ext_power_generic.c) -target_sources_ifdef(CONFIG_ZMK_WAKEUP_TRIGGER_KEY app PRIVATE src/wakeup_trigger_key.c) +target_sources_ifdef(CONFIG_ZMK_GPIO_KEY_WAKEUP_TRIGGER app PRIVATE src/gpio_key_wakeup_trigger.c) target_sources(app PRIVATE src/events/activity_state_changed.c) target_sources(app PRIVATE src/events/position_state_changed.c) target_sources(app PRIVATE src/events/sensor_event.c) diff --git a/app/Kconfig b/app/Kconfig index eda83a33ca9..5f3ccf45b8b 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -410,10 +410,10 @@ config ZMK_PM_SOFT_OFF bool "Soft-off support" select PM_DEVICE -config ZMK_WAKEUP_TRIGGER_KEY +config ZMK_GPIO_KEY_WAKEUP_TRIGGER bool "Hardware supported wakeup (GPIO)" default y - depends on DT_HAS_ZMK_WAKEUP_TRIGGER_KEY_ENABLED && ZMK_PM_SOFT_OFF + depends on DT_HAS_ZMK_GPIO_KEY_WAKEUP_TRIGGER_ENABLED && ZMK_PM_SOFT_OFF #Power Management endmenu diff --git a/app/Kconfig.behaviors b/app/Kconfig.behaviors index 6c1981bea8c..0b7d8fdc574 100644 --- a/app/Kconfig.behaviors +++ b/app/Kconfig.behaviors @@ -1,15 +1,15 @@ # Copyright (c) 2023 The ZMK Contributors # SPDX-License-Identifier: MIT -config ZMK_BEHAVIOR_KEY +config ZMK_GPIO_KEY_BEHAVIOR_TRIGGER bool default y - depends on DT_HAS_ZMK_BEHAVIOR_KEY_ENABLED + depends on DT_HAS_ZMK_GPIO_KEY_BEHAVIOR_TRIGGER_ENABLED -config ZMK_BEHAVIOR_KEY_SCANNED +config ZMK_GPIO_SCANNED_KEY_BEHAVIOR_TRIGGER bool default y - depends on DT_HAS_ZMK_BEHAVIOR_KEY_SCANNED_ENABLED + depends on DT_HAS_ZMK_GPIO_SCANNED_KEY_BEHAVIOR_TRIGGER_ENABLED config ZMK_BEHAVIOR_KEY_TOGGLE bool diff --git a/app/boards/shields/zmk_uno/Kconfig.defconfig b/app/boards/shields/zmk_uno/Kconfig.defconfig index cccca1d2dee..95602ca73f9 100644 --- a/app/boards/shields/zmk_uno/Kconfig.defconfig +++ b/app/boards/shields/zmk_uno/Kconfig.defconfig @@ -20,4 +20,7 @@ config ZMK_RGB_UNDERGLOW select WS2812_STRIP select SPI +config ZMK_PM_SOFT_OFF + default y if BOARD_NRF52840DK_NRF52840 + endif diff --git a/app/boards/shields/zmk_uno/boards/nrf52840dk_nrf52840.conf b/app/boards/shields/zmk_uno/boards/nrf52840dk_nrf52840.conf deleted file mode 100644 index ac92c4d8f00..00000000000 --- a/app/boards/shields/zmk_uno/boards/nrf52840dk_nrf52840.conf +++ /dev/null @@ -1 +0,0 @@ -CONFIG_ZMK_PM_SOFT_OFF=y \ No newline at end of file diff --git a/app/boards/shields/zmk_uno/boards/nrf52840dk_nrf52840.overlay b/app/boards/shields/zmk_uno/boards/nrf52840dk_nrf52840.overlay index d08105c6889..b068b431e25 100644 --- a/app/boards/shields/zmk_uno/boards/nrf52840dk_nrf52840.overlay +++ b/app/boards/shields/zmk_uno/boards/nrf52840dk_nrf52840.overlay @@ -43,7 +43,7 @@ encoder: &qdec0 { }; wakeup_source: wakeup_source { - compatible = "zmk,wakeup-trigger-key"; + compatible = "zmk,gpio-key-wakeup-trigger"; status = "okay"; trigger = <&button0>; @@ -58,7 +58,7 @@ encoder: &qdec0 { }; soft_off_behavior_key { - compatible = "zmk,behavior-key"; + compatible = "zmk,gpio-key-behavior-trigger"; status = "okay"; bindings = <&soft_off>; key = <&button0>; diff --git a/app/dts/behaviors/soft_off.dtsi b/app/dts/behaviors/soft_off.dtsi index c88e1b51f12..1e58c7711d0 100644 --- a/app/dts/behaviors/soft_off.dtsi +++ b/app/dts/behaviors/soft_off.dtsi @@ -8,7 +8,6 @@ behaviors { /omit-if-no-ref/ soft_off: soft_off { compatible = "zmk,behavior-soft-off"; - label = "SOFTOFF"; #binding-cells = <0>; }; }; diff --git a/app/dts/bindings/zmk,behavior-key.yaml b/app/dts/bindings/zmk,gpio-key-behavior-trigger.yaml similarity index 86% rename from app/dts/bindings/zmk,behavior-key.yaml rename to app/dts/bindings/zmk,gpio-key-behavior-trigger.yaml index ff7a585eafa..2a1387f0220 100644 --- a/app/dts/bindings/zmk,behavior-key.yaml +++ b/app/dts/bindings/zmk,gpio-key-behavior-trigger.yaml @@ -4,7 +4,7 @@ description: | Driver for a dedicated key for invoking a connected behavior. -compatible: "zmk,behavior-key" +compatible: "zmk,gpio-key-behavior-trigger" include: base.yaml @@ -16,7 +16,7 @@ properties: bindings: type: phandle required: true - description: The GPIO key that triggers wake via interrupt + description: The behavior to invoke when the GPIO key is pressed debounce-press-ms: type: int default: 5 diff --git a/app/dts/bindings/zmk,wakeup-trigger-key.yaml b/app/dts/bindings/zmk,gpio-key-wakeup-trigger.yaml similarity index 90% rename from app/dts/bindings/zmk,wakeup-trigger-key.yaml rename to app/dts/bindings/zmk,gpio-key-wakeup-trigger.yaml index fa7636d1f32..4e16ff3307d 100644 --- a/app/dts/bindings/zmk,wakeup-trigger-key.yaml +++ b/app/dts/bindings/zmk,gpio-key-wakeup-trigger.yaml @@ -4,7 +4,7 @@ description: | Driver for a dedicated key for waking the device from sleep -compatible: "zmk,wakeup-trigger-key" +compatible: "zmk,gpio-key-wakeup-trigger" include: base.yaml diff --git a/app/dts/bindings/zmk,behavior-key-scanned.yaml b/app/dts/bindings/zmk,gpio-scanned-key-behavior-trigger.yaml similarity index 86% rename from app/dts/bindings/zmk,behavior-key-scanned.yaml rename to app/dts/bindings/zmk,gpio-scanned-key-behavior-trigger.yaml index bdb3abaff0e..860155dda72 100644 --- a/app/dts/bindings/zmk,behavior-key-scanned.yaml +++ b/app/dts/bindings/zmk,gpio-scanned-key-behavior-trigger.yaml @@ -4,7 +4,7 @@ description: | Driver for a dedicated key triggered by matrix scanning for invoking a connected behavior. -compatible: "zmk,behavior-key-scanned" +compatible: "zmk,gpio-scanned-key-behavior-trigger" include: base.yaml @@ -16,7 +16,7 @@ properties: bindings: type: phandle required: true - description: The GPIO key that triggers wake via interrupt + description: The behavior to invoke when the GPIO key is pressed debounce-press-ms: type: int default: 5 diff --git a/app/dts/bindings/zmk,soft-off-wakeup-sources.yaml b/app/dts/bindings/zmk,soft-off-wakeup-sources.yaml index f98039a0ca9..6b55d5d265a 100644 --- a/app/dts/bindings/zmk,soft-off-wakeup-sources.yaml +++ b/app/dts/bindings/zmk,soft-off-wakeup-sources.yaml @@ -2,7 +2,7 @@ # SPDX-License-Identifier: MIT description: | - Description of all possible wakeup-sources from a forces + Description of all possible wakeup-sources from a forced soft-off state. compatible: "zmk,soft-off-wakeup-sources" @@ -11,4 +11,4 @@ properties: wakeup-sources: type: phandles required: true - description: List of wakeup-sources that should be enabled to wake the system from forces soft-off state. + description: List of wakeup-sources that should be enabled to wake the system from forced soft-off state. diff --git a/app/module/drivers/kscan/kscan_gpio_direct.c b/app/module/drivers/kscan/kscan_gpio_direct.c index c44debe54de..87c8f716d50 100644 --- a/app/module/drivers/kscan/kscan_gpio_direct.c +++ b/app/module/drivers/kscan/kscan_gpio_direct.c @@ -322,8 +322,6 @@ static int kscan_direct_init(const struct device *dev) { #if IS_ENABLED(CONFIG_PM_DEVICE) static int kscan_direct_pm_action(const struct device *dev, enum pm_device_action action) { - int ret = 0; - switch (action) { case PM_DEVICE_ACTION_SUSPEND: kscan_direct_disable(dev); @@ -332,11 +330,8 @@ static int kscan_direct_pm_action(const struct device *dev, enum pm_device_actio kscan_direct_enable(dev); break; default: - ret = -ENOTSUP; - break; + return -ENOTSUP; } - - return ret; } #endif // IS_ENABLED(CONFIG_PM_DEVICE) diff --git a/app/module/drivers/kscan/kscan_gpio_matrix.c b/app/module/drivers/kscan/kscan_gpio_matrix.c index de2e1127920..d1d6a496980 100644 --- a/app/module/drivers/kscan/kscan_gpio_matrix.c +++ b/app/module/drivers/kscan/kscan_gpio_matrix.c @@ -425,8 +425,6 @@ static int kscan_matrix_init(const struct device *dev) { #if IS_ENABLED(CONFIG_PM_DEVICE) static int kscan_matrix_pm_action(const struct device *dev, enum pm_device_action action) { - int ret = 0; - switch (action) { case PM_DEVICE_ACTION_SUSPEND: kscan_matrix_disable(dev); @@ -435,11 +433,8 @@ static int kscan_matrix_pm_action(const struct device *dev, enum pm_device_actio kscan_matrix_enable(dev); break; default: - ret = -ENOTSUP; - break; + return -ENOTSUP; } - - return ret; } #endif // IS_ENABLED(CONFIG_PM_DEVICE) diff --git a/app/src/behaviors/behavior_soft_off.c b/app/src/behaviors/behavior_soft_off.c index 0f24a644d9a..e6096bb4841 100644 --- a/app/src/behaviors/behavior_soft_off.c +++ b/app/src/behaviors/behavior_soft_off.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 The ZMK Contributors + * Copyright (c) 2023 The ZMK Contributors * * SPDX-License-Identifier: MIT */ diff --git a/app/src/behavior_key.c b/app/src/gpio_key_behavior_trigger.c similarity index 61% rename from app/src/behavior_key.c rename to app/src/gpio_key_behavior_trigger.c index 3633ce39a40..a72f8e48925 100644 --- a/app/src/behavior_key.c +++ b/app/src/gpio_key_behavior_trigger.c @@ -4,7 +4,7 @@ * SPDX-License-Identifier: MIT */ -#define DT_DRV_COMPAT zmk_behavior_key +#define DT_DRV_COMPAT zmk_gpio_key_behavior_trigger #include #include @@ -19,13 +19,13 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); -struct behavior_key_config { +struct gkbt_config { struct zmk_debounce_config debounce_config; int32_t debounce_scan_period_ms; struct gpio_dt_spec key; }; -struct behavior_key_data { +struct gkbt_data { struct zmk_behavior_binding binding; struct zmk_debounce_state debounce_state; struct gpio_callback key_callback; @@ -34,21 +34,21 @@ struct behavior_key_data { uint32_t read_time; }; -static void bk_enable_interrupt(const struct device *dev) { - const struct behavior_key_config *config = dev->config; +static void gkbt_enable_interrupt(const struct device *dev) { + const struct gkbt_config *config = dev->config; gpio_pin_interrupt_configure_dt(&config->key, GPIO_INT_LEVEL_ACTIVE); } -static void bk_disable_interrupt(const struct device *dev) { - const struct behavior_key_config *config = dev->config; +static void gkbt_disable_interrupt(const struct device *dev) { + const struct gkbt_config *config = dev->config; gpio_pin_interrupt_configure_dt(&config->key, GPIO_INT_DISABLE); } -static void bk_read(const struct device *dev) { - const struct behavior_key_config *config = dev->config; - struct behavior_key_data *data = dev->data; +static void gkbt_read(const struct device *dev) { + const struct gkbt_config *config = dev->config; + struct gkbt_data *data = dev->data; zmk_debounce_update(&data->debounce_state, gpio_pin_get_dt(&config->key), config->debounce_scan_period_ms, &config->debounce_config); @@ -71,65 +71,72 @@ static void bk_read(const struct device *dev) { k_work_reschedule(&data->update_work, K_TIMEOUT_ABS_MS(data->read_time)); } else { - bk_enable_interrupt(dev); + gkbt_enable_interrupt(dev); } } -static void bk_update_work(struct k_work *work) { +static void gkbt_update_work(struct k_work *work) { struct k_work_delayable *dwork = CONTAINER_OF(work, struct k_work_delayable, work); - struct behavior_key_data *data = CONTAINER_OF(dwork, struct behavior_key_data, update_work); - bk_read(data->dev); + struct gkbt_data *data = CONTAINER_OF(dwork, struct gkbt_data, update_work); + gkbt_read(data->dev); } -static void bk_gpio_irq_callback(const struct device *port, struct gpio_callback *cb, - const gpio_port_pins_t pin) { - struct behavior_key_data *data = CONTAINER_OF(cb, struct behavior_key_data, key_callback); +static void gkbt_gpio_irq_callback(const struct device *port, struct gpio_callback *cb, + const gpio_port_pins_t pin) { + struct gkbt_data *data = CONTAINER_OF(cb, struct gkbt_data, key_callback); - bk_disable_interrupt(data->dev); + gkbt_disable_interrupt(data->dev); data->read_time = k_uptime_get(); k_work_reschedule(&data->update_work, K_NO_WAIT); } -static int behavior_key_init(const struct device *dev) { - const struct behavior_key_config *config = dev->config; - struct behavior_key_data *data = dev->data; +static void gkbt_wait_for_key_release(const struct device *dev) { + const struct gkbt_config *config = dev->config; + + while (gpio_pin_get_dt(&config->key)) { + k_sleep(K_MSEC(100)); + } +} + +static int gkbt_init(const struct device *dev) { + const struct gkbt_config *config = dev->config; + struct gkbt_data *data = dev->data; if (!device_is_ready(config->key.port)) { - LOG_ERR("GPIO port is not ready"); + LOG_ERR("GPIO port %s is not ready", config->key.port->name); return -ENODEV; } - k_work_init_delayable(&data->update_work, bk_update_work); + k_work_init_delayable(&data->update_work, gkbt_update_work); data->dev = dev; gpio_pin_configure_dt(&config->key, GPIO_INPUT); - gpio_init_callback(&data->key_callback, bk_gpio_irq_callback, BIT(config->key.pin)); + gpio_init_callback(&data->key_callback, gkbt_gpio_irq_callback, BIT(config->key.pin)); gpio_add_callback(config->key.port, &data->key_callback); - while (gpio_pin_get_dt(&config->key)) { - k_sleep(K_MSEC(100)); - } + // Be sure our wakeup key is released before startup continues to avoid wake/sleep loop. + gkbt_wait_for_key_release(dev); - bk_enable_interrupt(dev); + gkbt_enable_interrupt(dev); return 0; } -static int behavior_key_pm_action(const struct device *dev, enum pm_device_action action) { - const struct behavior_key_config *config = dev->config; - struct behavior_key_data *data = dev->data; +static int gkbt_pm_action(const struct device *dev, enum pm_device_action action) { + const struct gkbt_config *config = dev->config; + struct gkbt_data *data = dev->data; int ret; switch (action) { case PM_DEVICE_ACTION_SUSPEND: - bk_disable_interrupt(dev); + gkbt_disable_interrupt(dev); ret = gpio_remove_callback(config->key.port, &data->key_callback); break; case PM_DEVICE_ACTION_RESUME: ret = gpio_add_callback(config->key.port, &data->key_callback); - bk_enable_interrupt(dev); + gkbt_enable_interrupt(dev); break; default: ret = -ENOTSUP; @@ -139,8 +146,8 @@ static int behavior_key_pm_action(const struct device *dev, enum pm_device_actio return ret; } -#define BK_INST(n) \ - const struct behavior_key_config bk_config_##n = { \ +#define GKBT_INST(n) \ + const struct gkbt_config gkbt_config_##n = { \ .key = GPIO_DT_SPEC_GET(DT_INST_PHANDLE(n, key), gpios), \ .debounce_config = \ { \ @@ -149,11 +156,12 @@ static int behavior_key_pm_action(const struct device *dev, enum pm_device_actio }, \ .debounce_scan_period_ms = DT_INST_PROP(n, debounce_scan_period_ms), \ }; \ - struct behavior_key_data bk_data_##n = { \ + struct gkbt_data gkbt_data_##n = { \ .binding = ZMK_KEYMAP_EXTRACT_BINDING(0, DT_DRV_INST(n)), \ }; \ - PM_DEVICE_DT_INST_DEFINE(n, behavior_key_pm_action); \ - DEVICE_DT_INST_DEFINE(n, behavior_key_init, PM_DEVICE_DT_INST_GET(n), &bk_data_##n, \ - &bk_config_##n, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, NULL); + PM_DEVICE_DT_INST_DEFINE(n, gkbt_pm_action); \ + DEVICE_DT_INST_DEFINE(n, gkbt_init, PM_DEVICE_DT_INST_GET(n), &gkbt_data_##n, \ + &gkbt_config_##n, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, \ + NULL); -DT_INST_FOREACH_STATUS_OKAY(BK_INST) +DT_INST_FOREACH_STATUS_OKAY(GKBT_INST) diff --git a/app/src/gpio_key_wakeup_trigger.c b/app/src/gpio_key_wakeup_trigger.c new file mode 100644 index 00000000000..ac0c6b228db --- /dev/null +++ b/app/src/gpio_key_wakeup_trigger.c @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2023 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#include +#include +#include +#include +#include + +#include + +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + +#define DT_DRV_COMPAT zmk_gpio_key_wakeup_trigger + +struct gpio_key_wakeup_trigger_config { + struct gpio_dt_spec trigger; + size_t extra_gpios_count; + struct gpio_dt_spec extra_gpios[]; +}; + +static int zmk_gpio_key_wakeup_trigger_init(const struct device *dev) { +#if IS_ENABLED(CONFIG_PM_DEVICE) + pm_device_init_suspended(dev); + pm_device_wakeup_enable(dev, true); +#endif + + return 0; +} + +#if IS_ENABLED(CONFIG_PM_DEVICE) + +static int gpio_key_wakeup_trigger_pm_resume(const struct device *dev) { + const struct gpio_key_wakeup_trigger_config *config = dev->config; + + int ret = gpio_pin_interrupt_configure_dt(&config->trigger, GPIO_INT_LEVEL_ACTIVE); + if (ret < 0) { + LOG_ERR("Failed to configure wakeup trigger key GPIO pin interrupt (%d)", ret); + goto exit; + } + + for (int i = 0; i < config->extra_gpios_count; i++) { + ret = gpio_pin_configure_dt(&config->extra_gpios[i], GPIO_OUTPUT_ACTIVE); + if (ret < 0) { + LOG_WRN("Failed to set extra GPIO pin active for waker (%d)", ret); + goto exit; + } + } + +exit: + return ret; +} + +static int gpio_key_wakeup_trigger_pm_suspend(const struct device *dev) { + const struct gpio_key_wakeup_trigger_config *config = dev->config; + + int ret = gpio_pin_interrupt_configure_dt(&config->trigger, GPIO_INT_DISABLE); + if (ret < 0) { + LOG_ERR("Failed to configure wakeup trigger key GPIO pin interrupt (%d)", ret); + } + + return ret; +} + +static int gpio_key_wakeup_trigger_pm_action(const struct device *dev, + enum pm_device_action action) { + switch (action) { + case PM_DEVICE_ACTION_RESUME: + return gpio_key_wakeup_trigger_pm_resume(dev); + case PM_DEVICE_ACTION_SUSPEND: + return gpio_key_wakeup_trigger_pm_suspend(dev); + default: + return -ENOTSUP; + } +} + +#endif // IS_ENABLED(CONFIG_PM_DEVICE) + +#define WAKEUP_TRIGGER_EXTRA_GPIO_SPEC(idx, n) \ + GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(n), extra_gpios, idx) + +#define GPIO_KEY_WAKEUP_TRIGGER_INST(n) \ + const struct gpio_key_wakeup_trigger_config wtk_cfg_##n = { \ + .trigger = GPIO_DT_SPEC_GET(DT_INST_PROP(n, trigger), gpios), \ + .extra_gpios = {LISTIFY(DT_PROP_LEN_OR(DT_DRV_INST(n), extra_gpios, 0), \ + WAKEUP_TRIGGER_EXTRA_GPIO_SPEC, (, ), n)}, \ + .extra_gpios_count = DT_PROP_LEN_OR(DT_DRV_INST(n), extra_gpios, 0), \ + }; \ + PM_DEVICE_DT_INST_DEFINE(n, gpio_key_wakeup_trigger_pm_action); \ + DEVICE_DT_INST_DEFINE(n, zmk_gpio_key_wakeup_trigger_init, PM_DEVICE_DT_INST_GET(n), NULL, \ + &wtk_cfg_##n, PRE_KERNEL_2, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, NULL); + +DT_INST_FOREACH_STATUS_OKAY(GPIO_KEY_WAKEUP_TRIGGER_INST) diff --git a/app/src/behavior_key_scanned.c b/app/src/gpio_scanned_key_behavior_trigger.c similarity index 57% rename from app/src/behavior_key_scanned.c rename to app/src/gpio_scanned_key_behavior_trigger.c index c961b292622..d27b162b191 100644 --- a/app/src/behavior_key_scanned.c +++ b/app/src/gpio_scanned_key_behavior_trigger.c @@ -4,7 +4,7 @@ * SPDX-License-Identifier: MIT */ -#define DT_DRV_COMPAT zmk_behavior_key_scanned +#define DT_DRV_COMPAT zmk_gpio_scanned_key_behavior_trigger #include #include @@ -19,45 +19,41 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); -struct behavior_key_scanned_config { +struct gskbt_config { struct zmk_debounce_config debounce_config; int32_t debounce_scan_period_ms; struct gpio_dt_spec key; }; -struct behavior_key_scanned_data { +struct gskbt_data { struct zmk_behavior_binding binding; struct zmk_debounce_state debounce_state; struct gpio_callback key_callback; const struct device *dev; struct k_work_delayable update_work; + struct k_work gpio_trigger_work; uint32_t read_time; + uint32_t trigger_time; bool pin_active; bool active_scan_detected; - struct k_sem sem; }; -static void bks_enable_interrupt(const struct device *dev, bool active_scanning) { - const struct behavior_key_scanned_config *config = dev->config; +static void gskbt_enable_interrupt(const struct device *dev, bool active_scanning) { + const struct gskbt_config *config = dev->config; gpio_pin_interrupt_configure_dt(&config->key, active_scanning ? GPIO_INT_EDGE_TO_ACTIVE : GPIO_INT_LEVEL_ACTIVE); } -static void bks_disable_interrupt(const struct device *dev) { - const struct behavior_key_scanned_config *config = dev->config; +static void gskbt_disable_interrupt(const struct device *dev) { + const struct gskbt_config *config = dev->config; gpio_pin_interrupt_configure_dt(&config->key, GPIO_INT_DISABLE); } -static void bks_read(const struct device *dev) { - const struct behavior_key_scanned_config *config = dev->config; - struct behavior_key_scanned_data *data = dev->data; - - if (k_sem_take(&data->sem, K_NO_WAIT) < 0) { - // k_work_reschedule(&data->update_work, K_NO_WAIT); - return; - } +static void gskbt_read(const struct device *dev) { + const struct gskbt_config *config = dev->config; + struct gskbt_data *data = dev->data; zmk_debounce_update(&data->debounce_state, data->active_scan_detected, config->debounce_scan_period_ms, &config->debounce_config); @@ -81,89 +77,83 @@ static void bks_read(const struct device *dev) { k_work_schedule(&data->update_work, K_TIMEOUT_ABS_MS(data->read_time)); } else { - bks_enable_interrupt(dev, false); + gskbt_enable_interrupt(dev, false); } - - k_sem_give(&data->sem); } -static void bks_update_work(struct k_work *work) { +static void gskbt_update_work(struct k_work *work) { struct k_work_delayable *dwork = CONTAINER_OF(work, struct k_work_delayable, work); - struct behavior_key_scanned_data *data = - CONTAINER_OF(dwork, struct behavior_key_scanned_data, update_work); - bks_read(data->dev); + struct gskbt_data *data = CONTAINER_OF(dwork, struct gskbt_data, update_work); + gskbt_read(data->dev); } -static void bks_gpio_irq_callback(const struct device *port, struct gpio_callback *cb, - const gpio_port_pins_t pin) { - struct behavior_key_scanned_data *data = - CONTAINER_OF(cb, struct behavior_key_scanned_data, key_callback); - const struct behavior_key_scanned_config *config = data->dev->config; - - uint32_t time = k_uptime_get(); - - if (k_sem_take(&data->sem, K_MSEC(10)) < 0) { - LOG_ERR("FAILED TO TAKE THE SEMAPHORE"); - // Do more? - return; - } +static void gskbt_gpio_interrupt_work(struct k_work *work) { + struct gskbt_data *data = CONTAINER_OF(work, struct gskbt_data, gpio_trigger_work); - data->active_scan_detected = true; - data->read_time = time; + const struct gskbt_config *config = data->dev->config; if (!zmk_debounce_is_active(&data->debounce_state)) { - // When we get that very first interrupt, we need to schedule the update checks to fall in - // between each of the real scans, so we can do our checks for state *after* each scan has + // When we get that very first interrupt, we need to schedule the update checks right before + // the next real scan, so we can do our checks for state *after* each scan has // occurred. + data->read_time = data->trigger_time; k_work_reschedule(&data->update_work, - K_TIMEOUT_ABS_MS(time + (config->debounce_scan_period_ms / 2))); - - bks_enable_interrupt(data->dev, true); + K_TIMEOUT_ABS_MS(data->read_time + config->debounce_scan_period_ms - 1)); } +} - k_sem_give(&data->sem); +static void gskbt_gpio_irq_callback(const struct device *port, struct gpio_callback *cb, + const gpio_port_pins_t pin) { + struct gskbt_data *data = CONTAINER_OF(cb, struct gskbt_data, key_callback); + + // LOG_DBG("IRQ"); + data->active_scan_detected = true; + data->trigger_time = k_uptime_get(); + gskbt_enable_interrupt(data->dev, true); + k_work_submit(&data->gpio_trigger_work); } -static int behavior_key_scanned_init(const struct device *dev) { - const struct behavior_key_scanned_config *config = dev->config; - struct behavior_key_scanned_data *data = dev->data; +static int gskbt_init(const struct device *dev) { + const struct gskbt_config *config = dev->config; + struct gskbt_data *data = dev->data; if (!device_is_ready(config->key.port)) { LOG_ERR("GPIO port is not ready"); return -ENODEV; } - k_work_init_delayable(&data->update_work, bks_update_work); - k_sem_init(&data->sem, 1, 1); + k_work_init_delayable(&data->update_work, gskbt_update_work); + k_work_init(&data->gpio_trigger_work, gskbt_gpio_interrupt_work); + data->dev = dev; gpio_pin_configure_dt(&config->key, GPIO_INPUT); - gpio_init_callback(&data->key_callback, bks_gpio_irq_callback, BIT(config->key.pin)); + gpio_init_callback(&data->key_callback, gskbt_gpio_irq_callback, BIT(config->key.pin)); gpio_add_callback(config->key.port, &data->key_callback); while (gpio_pin_get_dt(&config->key)) { k_sleep(K_MSEC(100)); } - bks_enable_interrupt(dev, false); + gskbt_enable_interrupt(dev, false); return 0; } -static int behavior_key_scanned_pm_action(const struct device *dev, enum pm_device_action action) { - const struct behavior_key_scanned_config *config = dev->config; - struct behavior_key_scanned_data *data = dev->data; +static int gskbt_pm_action(const struct device *dev, enum pm_device_action action) { + const struct gskbt_config *config = dev->config; + struct gskbt_data *data = dev->data; int ret; switch (action) { case PM_DEVICE_ACTION_SUSPEND: - bks_disable_interrupt(dev); + gskbt_disable_interrupt(dev); ret = gpio_remove_callback(config->key.port, &data->key_callback); break; case PM_DEVICE_ACTION_RESUME: ret = gpio_add_callback(config->key.port, &data->key_callback); - bks_enable_interrupt(dev, false); + gskbt_enable_interrupt(dev, false); break; default: ret = -ENOTSUP; @@ -173,8 +163,8 @@ static int behavior_key_scanned_pm_action(const struct device *dev, enum pm_devi return ret; } -#define BK_INST(n) \ - const struct behavior_key_scanned_config bks_config_##n = { \ +#define GSKBT_INST(n) \ + const struct gskbt_config gskbt_config_##n = { \ .key = GPIO_DT_SPEC_GET(DT_INST_PHANDLE(n, key), gpios), \ .debounce_config = \ { \ @@ -183,12 +173,12 @@ static int behavior_key_scanned_pm_action(const struct device *dev, enum pm_devi }, \ .debounce_scan_period_ms = DT_INST_PROP(n, debounce_scan_period_ms), \ }; \ - struct behavior_key_scanned_data bks_data_##n = { \ + struct gskbt_data gskbt_data_##n = { \ .binding = ZMK_KEYMAP_EXTRACT_BINDING(0, DT_DRV_INST(n)), \ }; \ - PM_DEVICE_DT_INST_DEFINE(n, behavior_key_scanned_pm_action); \ - DEVICE_DT_INST_DEFINE(n, behavior_key_scanned_init, PM_DEVICE_DT_INST_GET(n), &bks_data_##n, \ - &bks_config_##n, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, \ + PM_DEVICE_DT_INST_DEFINE(n, gskbt_pm_action); \ + DEVICE_DT_INST_DEFINE(n, gskbt_init, PM_DEVICE_DT_INST_GET(n), &gskbt_data_##n, \ + &gskbt_config_##n, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, \ NULL); -DT_INST_FOREACH_STATUS_OKAY(BK_INST) +DT_INST_FOREACH_STATUS_OKAY(GSKBT_INST) diff --git a/app/src/pm.c b/app/src/pm.c index a78b6ae53cb..41d72eeed30 100644 --- a/app/src/pm.c +++ b/app/src/pm.c @@ -41,10 +41,10 @@ int zmk_pm_soft_off(void) { // from normal "inactive goes to sleep" behavior, so disable them as wakeup devices // and then suspend them so we're ready to take over setting up our system // and then putting it into an off state. + LOG_DBG("soft-on-off pressed cb: suspend devices"); for (int i = 0; i < device_count; i++) { const struct device *dev = &devs[i]; - LOG_DBG("soft-on-off pressed cb: suspend device"); if (pm_device_wakeup_is_enabled(dev)) { pm_device_wakeup_enable(dev, false); } @@ -60,6 +60,6 @@ int zmk_pm_soft_off(void) { } #endif // HAS_WAKERS - LOG_DBG("soft-on-off interrupt: go to sleep"); + LOG_DBG("soft-off: go to sleep"); return pm_state_force(0U, &(struct pm_state_info){PM_STATE_SOFT_OFF, 0, 0}); } diff --git a/app/src/wakeup_trigger_key.c b/app/src/wakeup_trigger_key.c deleted file mode 100644 index 0cc4f250707..00000000000 --- a/app/src/wakeup_trigger_key.c +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright (c) 2023 The ZMK Contributors - * - * SPDX-License-Identifier: MIT - */ - -#include -#include -#include -#include -#include - -#include - -LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); - -#define DT_DRV_COMPAT zmk_wakeup_trigger_key - -struct wakeup_trigger_key_config { - struct gpio_dt_spec trigger; - size_t extra_gpios_count; - struct gpio_dt_spec extra_gpios[]; -}; - -static int zmk_wakeup_trigger_key_init(const struct device *dev) { -#if IS_ENABLED(CONFIG_PM_DEVICE) - pm_device_init_suspended(dev); - pm_device_wakeup_enable(dev, true); -#endif - - return 0; -} - -#if IS_ENABLED(CONFIG_PM_DEVICE) - -static int wakeup_trigger_key_pm_action(const struct device *dev, enum pm_device_action action) { - const struct wakeup_trigger_key_config *config = dev->config; - int ret = 0; - - switch (action) { - case PM_DEVICE_ACTION_RESUME: - ret = gpio_pin_interrupt_configure_dt(&config->trigger, GPIO_INT_LEVEL_ACTIVE); - if (ret < 0) { - LOG_ERR("Failed to configure wakeup trigger key GPIO pin interrupt (%d)", ret); - return ret; - } - - for (int i = 0; i < config->extra_gpios_count; i++) { - ret = gpio_pin_configure_dt(&config->extra_gpios[i], GPIO_OUTPUT_ACTIVE); - if (ret < 0) { - LOG_WRN("Failed to set extra GPIO pin active for waker (%d)", ret); - } - } - break; - case PM_DEVICE_ACTION_SUSPEND: - - ret = gpio_pin_interrupt_configure_dt(&config->trigger, GPIO_INT_DISABLE); - if (ret < 0) { - LOG_ERR("Failed to configure wakeup trigger key GPIO pin interrupt (%d)", ret); - return ret; - } - break; - default: - ret = -ENOTSUP; - break; - } - - return ret; -} - -#endif // IS_ENABLED(CONFIG_PM_DEVICE) - -#define WAKEUP_TRIGGER_EXTRA_GPIO_SPEC(idx, n) \ - GPIO_DT_SPEC_GET_BY_IDX(DT_DRV_INST(n), extra_gpios, idx) - -#define WAKEUP_TRIGGER_KEY_INST(n) \ - const struct wakeup_trigger_key_config wtk_cfg_##n = { \ - .trigger = GPIO_DT_SPEC_GET(DT_INST_PROP(n, trigger), gpios), \ - .extra_gpios = {LISTIFY(DT_PROP_LEN_OR(DT_DRV_INST(n), extra_gpios, 0), \ - WAKEUP_TRIGGER_EXTRA_GPIO_SPEC, (, ), n)}, \ - .extra_gpios_count = DT_PROP_LEN_OR(DT_DRV_INST(n), extra_gpios, 0), \ - }; \ - PM_DEVICE_DT_INST_DEFINE(n, wakeup_trigger_key_pm_action); \ - DEVICE_DT_INST_DEFINE(n, zmk_wakeup_trigger_key_init, PM_DEVICE_DT_INST_GET(n), NULL, \ - &wtk_cfg_##n, PRE_KERNEL_2, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, NULL); - -DT_INST_FOREACH_STATUS_OKAY(WAKEUP_TRIGGER_KEY_INST) diff --git a/docs/docs/config/kscan.md b/docs/docs/config/kscan.md index 052d64ce270..051622495c2 100644 --- a/docs/docs/config/kscan.md +++ b/docs/docs/config/kscan.md @@ -455,7 +455,7 @@ Note that the entire addressable space does not need to be mapped. }; kscan0: kscan { - compatible = "zmk,kscan-gpio-charlieplex";k + compatible = "zmk,kscan-gpio-charlieplex"; wakeup-source; interrupt-gpios = <&pro_micro 21 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN) >; diff --git a/docs/docs/features/soft-off.md b/docs/docs/features/soft-off.md index a3a5d7a1cb7..6b6b5cf4b33 100644 --- a/docs/docs/features/soft-off.md +++ b/docs/docs/features/soft-off.md @@ -79,7 +79,7 @@ In this case, we will be creating a dedicated instance of the [Soft Off Behavior }; soft_off_behavior_key { - compatible = "zmk,behavior-key"; + compatible = "zmk,gpio-key-behavior-trigger"; bindings = <&hw_soft_off>; key = <&wakeup_key>; }; @@ -88,7 +88,7 @@ In this case, we will be creating a dedicated instance of the [Soft Off Behavior Here are the properties for the behavior key node: -- The `compatible` property for the node must be `zmk,behavior-key`. +- The `compatible` property for the node must be `zmk,gpio-key-behavior-trigger`. - The `bindings` property is a phandle to the soft off behavior defined above. - The `key` property is a phandle to the GPIO key defined earlier. @@ -97,7 +97,7 @@ If you have set up your on/off to be controlled by a matrix-integrated combo, th ``` / { soft_off_behavior_key { - compatible = "zmk,behavior-key-scanned"; + compatible = "zmk,gpio-scanned-key-behavior-trigger"; status = "okay"; bindings = <&hw_soft_off>; key = <&wakeup_key>; @@ -105,7 +105,7 @@ If you have set up your on/off to be controlled by a matrix-integrated combo, th }; ``` -Note that the only difference from the `soft_off_behavior_key` definition for GPIO keys above is the `compatible` value of `zmk,behavior-key-scanned`. +Note that the only difference from the `soft_off_behavior_key` definition for GPIO keys above is the `compatible` value of `zmk,gpio-scanned-key-behavior-trigger`. #### Wakeup Sources @@ -131,7 +131,7 @@ Next, we need to add another device which will be enabled only when the keyboard ``` / { wakeup_source: wakeup_source { - compatible = "zmk,wakeup-trigger-key"; + compatible = "zmk,gpio-key-wakeup-trigger"; trigger = <&wakeup_key>; wakeup-source; @@ -141,7 +141,7 @@ Next, we need to add another device which will be enabled only when the keyboard Here are the properties for the node: -- The `compatible` property for the node must be `zmk,wakeup-trigger-key`. +- The `compatible` property for the node must be `zmk,gpio-key-wakeup-trigger`. - The `trigger` property is a phandle to the GPIO key defined earlier. - The `wakeup-source` property signals to Zephyr this device should not be suspended during the shutdown procedure. - An optional `output-gpios` property contains a list of GPIO pins (including the appropriate flags) to set active before going into power off, if needed to ensure the GPIO pin will trigger properly to wake the keyboard. This is only needed for matrix integrated combos. For those keyboards, the list should include the matrix output needs needed so the combo hardware is properly "driven" when the keyboard is off. From bf87543b012df45b57435662a3406c85d203fcb2 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Fri, 29 Dec 2023 15:03:14 -0800 Subject: [PATCH 10/10] chore: Various soft-off review fixes * Code style to avoid goto. * Enable pm.c compilation via dedicated Kconfig flag. * Comment wakeup trigger PM behavior. --- app/CMakeLists.txt | 2 +- app/Kconfig | 4 ++++ app/module/drivers/kscan/kscan_gpio_matrix.c | 6 ++---- app/src/gpio_key_wakeup_trigger.c | 16 +++++++++++++--- app/src/pm.c | 4 ++++ 5 files changed, 24 insertions(+), 8 deletions(-) diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 23b0398f407..79136630b38 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -24,7 +24,7 @@ target_sources_ifdef(CONFIG_ZMK_WPM app PRIVATE src/wpm.c) target_sources(app PRIVATE src/event_manager.c) target_sources_ifdef(CONFIG_ZMK_GPIO_KEY_BEHAVIOR_TRIGGER app PRIVATE src/gpio_key_behavior_trigger.c) target_sources_ifdef(CONFIG_ZMK_GPIO_SCANNED_KEY_BEHAVIOR_TRIGGER app PRIVATE src/gpio_scanned_key_behavior_trigger.c) -target_sources_ifdef(CONFIG_ZMK_PM_SOFT_OFF app PRIVATE src/pm.c) +target_sources_ifdef(CONFIG_ZMK_PM app PRIVATE src/pm.c) target_sources_ifdef(CONFIG_ZMK_EXT_POWER app PRIVATE src/ext_power_generic.c) target_sources_ifdef(CONFIG_ZMK_GPIO_KEY_WAKEUP_TRIGGER app PRIVATE src/gpio_key_wakeup_trigger.c) target_sources(app PRIVATE src/events/activity_state_changed.c) diff --git a/app/Kconfig b/app/Kconfig index 5f3ccf45b8b..e9c7a00df2e 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -406,8 +406,12 @@ config ZMK_EXT_POWER bool "Enable support to control external power output" default y +config ZMK_PM + bool + config ZMK_PM_SOFT_OFF bool "Soft-off support" + select ZMK_PM select PM_DEVICE config ZMK_GPIO_KEY_WAKEUP_TRIGGER diff --git a/app/module/drivers/kscan/kscan_gpio_matrix.c b/app/module/drivers/kscan/kscan_gpio_matrix.c index d1d6a496980..f2f03976cad 100644 --- a/app/module/drivers/kscan/kscan_gpio_matrix.c +++ b/app/module/drivers/kscan/kscan_gpio_matrix.c @@ -427,11 +427,9 @@ static int kscan_matrix_init(const struct device *dev) { static int kscan_matrix_pm_action(const struct device *dev, enum pm_device_action action) { switch (action) { case PM_DEVICE_ACTION_SUSPEND: - kscan_matrix_disable(dev); - break; + return kscan_matrix_disable(dev); case PM_DEVICE_ACTION_RESUME: - kscan_matrix_enable(dev); - break; + return kscan_matrix_enable(dev); default: return -ENOTSUP; } diff --git a/app/src/gpio_key_wakeup_trigger.c b/app/src/gpio_key_wakeup_trigger.c index ac0c6b228db..308c4973d67 100644 --- a/app/src/gpio_key_wakeup_trigger.c +++ b/app/src/gpio_key_wakeup_trigger.c @@ -39,18 +39,17 @@ static int gpio_key_wakeup_trigger_pm_resume(const struct device *dev) { int ret = gpio_pin_interrupt_configure_dt(&config->trigger, GPIO_INT_LEVEL_ACTIVE); if (ret < 0) { LOG_ERR("Failed to configure wakeup trigger key GPIO pin interrupt (%d)", ret); - goto exit; + return ret; } for (int i = 0; i < config->extra_gpios_count; i++) { ret = gpio_pin_configure_dt(&config->extra_gpios[i], GPIO_OUTPUT_ACTIVE); if (ret < 0) { LOG_WRN("Failed to set extra GPIO pin active for waker (%d)", ret); - goto exit; + return ret; } } -exit: return ret; } @@ -62,9 +61,20 @@ static int gpio_key_wakeup_trigger_pm_suspend(const struct device *dev) { LOG_ERR("Failed to configure wakeup trigger key GPIO pin interrupt (%d)", ret); } + for (int i = 0; i < config->extra_gpios_count; i++) { + ret = gpio_pin_configure_dt(&config->extra_gpios[i], GPIO_DISCONNECTED); + if (ret < 0) { + LOG_WRN("Failed to set extra GPIO pin disconnected for waker (%d)", ret); + return ret; + } + } + return ret; } +// The waker is "backwards", in as much as it is designed to be resumed/enabled immediately +// before a soft-off state is entered, so it can wake the device from that state later. +// So this waker correctly resumes and is ready to wake the device later. static int gpio_key_wakeup_trigger_pm_action(const struct device *dev, enum pm_device_action action) { switch (action) { diff --git a/app/src/pm.c b/app/src/pm.c index 41d72eeed30..a4599ac8fce 100644 --- a/app/src/pm.c +++ b/app/src/pm.c @@ -15,6 +15,8 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #include +#if IS_ENABLED(CONFIG_ZMK_PM_SOFT_OFF) + #define HAS_WAKERS DT_HAS_COMPAT_STATUS_OKAY(zmk_soft_off_wakeup_sources) #if HAS_WAKERS @@ -63,3 +65,5 @@ int zmk_pm_soft_off(void) { LOG_DBG("soft-off: go to sleep"); return pm_state_force(0U, &(struct pm_state_info){PM_STATE_SOFT_OFF, 0, 0}); } + +#endif // IS_ENABLED(CONFIG_ZMK_PM_SOFT_OFF) \ No newline at end of file