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 ++++++++++++++++++++++++++++ quantum/split_common/split_util.h | 4 +++ quantum/split_common/transaction_id_define.h | 4 +++ quantum/split_common/transactions.c | 33 +++++++++++++++++++++ quantum/split_common/transport.h | 4 +++ 5 files changed, 88 insertions(+) (limited to 'quantum/split_common') 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 } } diff --git a/quantum/split_common/split_util.h b/quantum/split_common/split_util.h index c7eabea233..5c9a260a14 100644 --- a/quantum/split_common/split_util.h +++ b/quantum/split_common/split_util.h @@ -14,3 +14,7 @@ void split_post_init(void); bool transport_master_if_connected(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]); bool is_transport_connected(void); + +void split_watchdog_update(bool done); +void split_watchdog_task(void); +bool split_watchdog_check(void); \ No newline at end of file diff --git a/quantum/split_common/transaction_id_define.h b/quantum/split_common/transaction_id_define.h index 761a8884f4..8c19948107 100644 --- a/quantum/split_common/transaction_id_define.h +++ b/quantum/split_common/transaction_id_define.h @@ -84,6 +84,10 @@ enum serial_transaction_id { PUT_POINTING_CPI, #endif // defined(POINTING_DEVICE_ENABLE) && defined(SPLIT_POINTING_ENABLE) +#if defined(SPLIT_WATCHDOG_ENABLE) + PUT_WATCHDOG, +#endif // defined(SPLIT_WATCHDOG_ENABLE) + #if defined(SPLIT_TRANSACTION_IDS_KB) || defined(SPLIT_TRANSACTION_IDS_USER) PUT_RPC_INFO, PUT_RPC_REQ_DATA, diff --git a/quantum/split_common/transactions.c b/quantum/split_common/transactions.c index 67986e4340..527b2f4caf 100644 --- a/quantum/split_common/transactions.c +++ b/quantum/split_common/transactions.c @@ -718,6 +718,36 @@ static void pointing_handlers_slave(matrix_row_t master_matrix[], matrix_row_t s #endif // defined(POINTING_DEVICE_ENABLE) && defined(SPLIT_POINTING_ENABLE) +//////////////////////////////////////////////////// +// WATCHDOG + +#if defined(SPLIT_WATCHDOG_ENABLE) + +static bool watchdog_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) { + bool okay = true; + if (!split_watchdog_check()) { + okay = transport_write(PUT_WATCHDOG, &okay, sizeof(okay)); + split_watchdog_update(okay); + } + return okay; +} + +static void watchdog_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) { + split_watchdog_update(split_shmem->watchdog_pinged); +} + +# define TRANSACTIONS_WATCHDOG_MASTER() TRANSACTION_HANDLER_MASTER(watchdog) +# define TRANSACTIONS_WATCHDOG_SLAVE() TRANSACTION_HANDLER_SLAVE_AUTOLOCK(watchdog) +# define TRANSACTIONS_WATCHDOG_REGISTRATIONS [PUT_WATCHDOG] = trans_initiator2target_initializer(watchdog_pinged), + +#else // defined(SPLIT_WATCHDOG_ENABLE) + +# define TRANSACTIONS_WATCHDOG_MASTER() +# define TRANSACTIONS_WATCHDOG_SLAVE() +# define TRANSACTIONS_WATCHDOG_REGISTRATIONS + +#endif // defined(SPLIT_WATCHDOG_ENABLE) + //////////////////////////////////////////////////// split_transaction_desc_t split_transaction_table[NUM_TOTAL_TRANSACTIONS] = { @@ -744,6 +774,7 @@ split_transaction_desc_t split_transaction_table[NUM_TOTAL_TRANSACTIONS] = { TRANSACTIONS_OLED_REGISTRATIONS TRANSACTIONS_ST7565_REGISTRATIONS TRANSACTIONS_POINTING_REGISTRATIONS + TRANSACTIONS_WATCHDOG_REGISTRATIONS // clang-format on #if defined(SPLIT_TRANSACTION_IDS_KB) || defined(SPLIT_TRANSACTION_IDS_USER) @@ -770,6 +801,7 @@ bool transactions_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix TRANSACTIONS_OLED_MASTER(); TRANSACTIONS_ST7565_MASTER(); TRANSACTIONS_POINTING_MASTER(); + TRANSACTIONS_WATCHDOG_MASTER(); return true; } @@ -789,6 +821,7 @@ void transactions_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[ TRANSACTIONS_OLED_SLAVE(); TRANSACTIONS_ST7565_SLAVE(); TRANSACTIONS_POINTING_SLAVE(); + TRANSACTIONS_WATCHDOG_SLAVE(); } #if defined(SPLIT_TRANSACTION_IDS_KB) || defined(SPLIT_TRANSACTION_IDS_USER) diff --git a/quantum/split_common/transport.h b/quantum/split_common/transport.h index 06778ad14a..833633edc2 100644 --- a/quantum/split_common/transport.h +++ b/quantum/split_common/transport.h @@ -188,6 +188,10 @@ typedef struct _split_shared_memory_t { split_slave_pointing_sync_t pointing; #endif // defined(POINTING_DEVICE_ENABLE) && defined(SPLIT_POINTING_ENABLE) +#if defined(SPLIT_WATCHDOG_ENABLE) + bool watchdog_pinged; +#endif // defined(SPLIT_WATCHDOG_ENABLE) + #if defined(SPLIT_TRANSACTION_IDS_KB) || defined(SPLIT_TRANSACTION_IDS_USER) rpc_sync_info_t rpc_info; uint8_t rpc_m2s_buffer[RPC_M2S_BUFFER_SIZE]; -- cgit v1.2.3