From 0f49f224cebbcfe278c6cb92c2bfd0117f155029 Mon Sep 17 00:00:00 2001 From: Xudong Zheng <7pkvm5aw@slicealias.com> Date: Fri, 2 Jun 2023 16:47:06 -0400 Subject: [PATCH] slicemk: implement &sys_off (EXPERIMENTAL 2023-06-14) --- app/dts/behaviors/reset.dtsi | 9 ++++++++- app/include/dt-bindings/zmk/reset.h | 5 ++++- app/src/behaviors/behavior_reset.c | 12 ++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/app/dts/behaviors/reset.dtsi b/app/dts/behaviors/reset.dtsi index 2e775269aff1..cc315e494172 100644 --- a/app/dts/behaviors/reset.dtsi +++ b/app/dts/behaviors/reset.dtsi @@ -20,5 +20,12 @@ type = ; #binding-cells = <0>; }; - }; + + sys_off: behavior_reset_sysoff { + compatible = "zmk,behavior-reset"; + label = "SYSOFF"; + type = ; + #binding-cells = <0>; + }; + }; }; diff --git a/app/include/dt-bindings/zmk/reset.h b/app/include/dt-bindings/zmk/reset.h index 2b3d8760d5c2..c34a38b8d070 100644 --- a/app/include/dt-bindings/zmk/reset.h +++ b/app/include/dt-bindings/zmk/reset.h @@ -7,7 +7,10 @@ #define RST_WARM 0x00 #define RST_COLD 0x01 +// Custom SliceMK magic token for SYSOFF. +#define RST_SYSOFF 0x56 + // 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 RST_UF2 0x57 diff --git a/app/src/behaviors/behavior_reset.c b/app/src/behaviors/behavior_reset.c index 7c93cbbfe44a..8c066ab997ed 100644 --- a/app/src/behaviors/behavior_reset.c +++ b/app/src/behaviors/behavior_reset.c @@ -13,6 +13,7 @@ #include #include +#include LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); @@ -50,3 +51,14 @@ static const struct behavior_driver_api behavior_reset_driver_api = { DT_INST_FOREACH_STATUS_OKAY(RST_INST) #endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */ + +static int sysoff_init_prekernel(const struct device *dev) { + uint32_t const gpregret = NRF_POWER->GPREGRET; + if (gpregret == RST_SYSOFF) { + NRF_POWER->GPREGRET = 0; + NRF_POWER->SYSTEMOFF = 1; + } + return 0; +} + +SYS_INIT(sysoff_init_prekernel, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);