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.
This commit is contained in:
Joel Spadin 2023-04-16 15:13:50 -05:00
parent 0239f18b61
commit 4b57588884
6 changed files with 60 additions and 13 deletions

View file

@ -24,6 +24,7 @@ target_sources(app PRIVATE src/stdlib.c)
target_sources(app PRIVATE src/activity.c) target_sources(app PRIVATE src/activity.c)
target_sources(app PRIVATE src/kscan.c) target_sources(app PRIVATE src/kscan.c)
target_sources(app PRIVATE src/matrix_transform.c) target_sources(app PRIVATE src/matrix_transform.c)
target_sources(app PRIVATE src/reset.c)
target_sources(app PRIVATE src/sensors.c) target_sources(app PRIVATE src/sensors.c)
target_sources_ifdef(CONFIG_ZMK_WPM app PRIVATE src/wpm.c) target_sources_ifdef(CONFIG_ZMK_WPM app PRIVATE src/wpm.c)
target_sources(app PRIVATE src/event_manager.c) target_sources(app PRIVATE src/event_manager.c)

View file

@ -17,7 +17,7 @@
bootloader: behavior_reset_dfu { bootloader: behavior_reset_dfu {
compatible = "zmk,behavior-reset"; compatible = "zmk,behavior-reset";
label = "BOOTLOAD"; label = "BOOTLOAD";
type = <RST_UF2>; type = <ZMK_RESET_BOOTLOADER>;
#binding-cells = <0>; #binding-cells = <0>;
}; };
}; };

View file

@ -4,10 +4,6 @@
* SPDX-License-Identifier: MIT * SPDX-License-Identifier: MIT
*/ */
#define RST_WARM 0x00 #define ZMK_RESET_WARM 0
#define RST_COLD 0x01 #define ZMK_RESET_COLD 1
#define ZMK_RESET_BOOTLOADER 2
// AdaFruit nrf52 Bootloader Specific. See
// https://github.com/adafruit/Adafruit_nRF52_Bootloader/blob/d6b28e66053eea467166f44875e3c7ec741cb471/src/main.c#L107
#define RST_UF2 0x57

16
app/include/zmk/reset.h Normal file
View file

@ -0,0 +1,16 @@
/*
* Copyright (c) 2023 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/
#pragma once
#include <zephyr/toolchain.h>
#include <dt-bindings/zmk/reset.h>
/**
* Reboot the system.
* @param type A ZMK_RESET_* value indicating how to reboot.
*/
FUNC_NORETURN void zmk_reset(int type);

View file

@ -7,12 +7,12 @@
#define DT_DRV_COMPAT zmk_behavior_reset #define DT_DRV_COMPAT zmk_behavior_reset
#include <zephyr/device.h> #include <zephyr/device.h>
#include <zephyr/sys/reboot.h>
#include <zephyr/logging/log.h> #include <zephyr/logging/log.h>
#include <drivers/behavior.h> #include <drivers/behavior.h>
#include <zmk/behavior.h> #include <zmk/behavior.h>
#include <zmk/reset.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
@ -28,10 +28,7 @@ static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding,
const struct device *dev = device_get_binding(binding->behavior_dev); const struct device *dev = device_get_binding(binding->behavior_dev);
const struct behavior_reset_config *cfg = dev->config; const struct behavior_reset_config *cfg = dev->config;
// TODO: Correct magic code for going into DFU? zmk_reset(cfg->type);
// See
// https://github.com/adafruit/Adafruit_nRF52_Bootloader/blob/d6b28e66053eea467166f44875e3c7ec741cb471/src/main.c#L107
sys_reboot(cfg->type);
return ZMK_BEHAVIOR_OPAQUE; return ZMK_BEHAVIOR_OPAQUE;
} }

37
app/src/reset.c Normal file
View file

@ -0,0 +1,37 @@
/*
* Copyright (c) 2023 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/
#include <zephyr/sys/reboot.h>
#include <zephyr/logging/log.h>
#include <zmk/reset.h>
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;
}
}