From c255174cf3e55483f14351a69689e24e849445a0 Mon Sep 17 00:00:00 2001 From: Dasky <32983009+daskygit@users.noreply.github.com> Date: Thu, 6 Oct 2022 10:52:42 +0100 Subject: Implement split comms watchdog (#18599) --- quantum/split_common/split_util.c | 43 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'quantum/split_common/split_util.c') diff --git a/quantum/split_common/split_util.c b/quantum/split_common/split_util.c index 0b3338ed6f..9f57c7b9fc 100644 --- a/quantum/split_common/split_util.c +++ b/quantum/split_common/split_util.c @@ -74,6 +74,46 @@ static inline bool usbIsActive(void) { } #endif +#if defined(SPLIT_WATCHDOG_ENABLE) +# if !defined(SPLIT_WATCHDOG_TIMEOUT) +# if defined(SPLIT_USB_TIMEOUT) +# define SPLIT_WATCHDOG_TIMEOUT (SPLIT_USB_TIMEOUT + 100) +# else +# define SPLIT_WATCHDOG_TIMEOUT 3000 +# endif +# endif +# if defined(SPLIT_USB_DETECT) +_Static_assert(SPLIT_USB_TIMEOUT < SPLIT_WATCHDOG_TIMEOUT, "SPLIT_WATCHDOG_TIMEOUT should not be below SPLIT_USB_TIMEOUT."); +# endif +_Static_assert(SPLIT_MAX_CONNECTION_ERRORS > 0, "SPLIT_WATCHDOG_ENABLE requires SPLIT_MAX_CONNECTION_ERRORS be above 0 for a functioning disconnection check."); + +static uint32_t split_watchdog_started = 0; +static bool split_watchdog_done = false; + +void split_watchdog_init(void) { + split_watchdog_started = timer_read32(); +} + +void split_watchdog_update(bool done) { + split_watchdog_done = done; +} + +bool split_watchdog_check(void) { + if (!is_transport_connected()) { + split_watchdog_done = false; + } + return split_watchdog_done; +} + +void split_watchdog_task(void) { + if (!split_watchdog_done && !is_keyboard_master()) { + if (timer_elapsed32(split_watchdog_started) > SPLIT_WATCHDOG_TIMEOUT) { + mcu_reset(); + } + } +} +#endif // defined(SPLIT_WATCHDOG_ENABLE) + #ifdef SPLIT_HAND_MATRIX_GRID void matrix_io_delay(void); @@ -179,6 +219,9 @@ void split_pre_init(void) { void split_post_init(void) { if (!is_keyboard_master()) { transport_slave_init(); +#if defined(SPLIT_WATCHDOG_ENABLE) + split_watchdog_init(); +#endif } } -- cgit v1.2.3