From 83dbb58bbdcfa77a18e47f14516bb16e9948084e Mon Sep 17 00:00:00 2001 From: Joel Spadin Date: Sun, 16 Apr 2023 15:13:50 -0500 Subject: [PATCH] refactor: Move reset logic to a new function Moved the logic to select the type of reboot from behavior_reset.c to a new zmk_reset() function. This allows rebooting to bootloader from other code, and it gives us a starting point for future work to support other bootloaders aside from the Adafruit nrf52 bootloader. --- app/CMakeLists.txt | 1 + app/dts/behaviors/reset.dtsi | 2 +- app/include/dt-bindings/zmk/reset.h | 10 +++----- app/include/zmk/reset.h | 16 +++++++++++++ app/src/behaviors/behavior_reset.c | 6 ++++- app/src/reset.c | 37 +++++++++++++++++++++++++++++ 6 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 app/include/zmk/reset.h create mode 100644 app/src/reset.c diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index cc38244a4..2eee25a3f 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -30,6 +30,7 @@ target_sources(app PRIVATE src/behavior.c) target_sources_ifdef(CONFIG_ZMK_KSCAN_SIDEBAND_BEHAVIORS app PRIVATE src/kscan_sideband_behaviors.c) target_sources(app PRIVATE src/matrix_transform.c) target_sources(app PRIVATE src/physical_layouts.c) +target_sources(app PRIVATE src/reset.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) diff --git a/app/dts/behaviors/reset.dtsi b/app/dts/behaviors/reset.dtsi index 1b29f9d19..a6adadeac 100644 --- a/app/dts/behaviors/reset.dtsi +++ b/app/dts/behaviors/reset.dtsi @@ -18,7 +18,7 @@ // Behavior can be invoked on peripherals, so name must be <= 8 characters. bootloader: bootload { compatible = "zmk,behavior-reset"; - type = ; + type = ; bootloader; #binding-cells = <0>; display-name = "Bootloader"; diff --git a/app/include/dt-bindings/zmk/reset.h b/app/include/dt-bindings/zmk/reset.h index 2b3d8760d..63f3428e2 100644 --- a/app/include/dt-bindings/zmk/reset.h +++ b/app/include/dt-bindings/zmk/reset.h @@ -4,10 +4,6 @@ * SPDX-License-Identifier: MIT */ -#define RST_WARM 0x00 -#define RST_COLD 0x01 - -// AdaFruit nrf52 Bootloader Specific. See -// https://github.com/adafruit/Adafruit_nRF52_Bootloader/blob/d6b28e66053eea467166f44875e3c7ec741cb471/src/main.c#L107 - -#define RST_UF2 0x57 \ No newline at end of file +#define ZMK_RESET_WARM 0 +#define ZMK_RESET_COLD 1 +#define ZMK_RESET_BOOTLOADER 2 diff --git a/app/include/zmk/reset.h b/app/include/zmk/reset.h new file mode 100644 index 000000000..d23d1d8e6 --- /dev/null +++ b/app/include/zmk/reset.h @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2023 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#pragma once + +#include +#include + +/** + * Reboot the system. + * @param type A ZMK_RESET_* value indicating how to reboot. + */ +FUNC_NORETURN void zmk_reset(int type); diff --git a/app/src/behaviors/behavior_reset.c b/app/src/behaviors/behavior_reset.c index 67f33aaed..f9e4d50ad 100644 --- a/app/src/behaviors/behavior_reset.c +++ b/app/src/behaviors/behavior_reset.c @@ -7,12 +7,12 @@ #define DT_DRV_COMPAT zmk_behavior_reset #include -#include #include #include #include +#include #if IS_ENABLED(CONFIG_RETENTION_BOOT_MODE) @@ -36,6 +36,7 @@ static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding, const struct device *dev = zmk_behavior_get_binding(binding->behavior_dev); const struct behavior_reset_config *cfg = dev->config; +<<<<<<< HEAD #if IS_ENABLED(CONFIG_RETENTION_BOOT_MODE) int ret = bootmode_set(cfg->boot_mode); if (ret < 0) { @@ -50,6 +51,9 @@ static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding, sys_reboot(cfg->type); #endif /* IS_ENABLED(CONFIG_RETENTION_BOOT_MODE) */ +======= + zmk_reset(cfg->type); +>>>>>>> 1d73fc26 (refactor: Move reset logic to a new function) return ZMK_BEHAVIOR_OPAQUE; } diff --git a/app/src/reset.c b/app/src/reset.c new file mode 100644 index 000000000..0470f519d --- /dev/null +++ b/app/src/reset.c @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#include +#include + +#include + +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + +// AdaFruit nrf52 Bootloader Specific. See +// https://github.com/adafruit/Adafruit_nRF52_Bootloader/blob/d6b28e66053eea467166f44875e3c7ec741cb471/src/main.c#L107 +#define ADAFRUIT_MAGIC_UF2 0x57 + +FUNC_NORETURN void zmk_reset(int type) { + switch (type) { + case ZMK_RESET_WARM: + sys_reboot(SYS_REBOOT_WARM); + break; + + case ZMK_RESET_COLD: + sys_reboot(SYS_REBOOT_COLD); + break; + + case ZMK_RESET_BOOTLOADER: + // TODO: Add support for other types of bootloaders. + sys_reboot(ADAFRUIT_MAGIC_UF2); + break; + + default: + LOG_ERR("Unknown reset type %d", type); + break; + } +}