~ruther/qmk_firmware

c255174cf3e55483f14351a69689e24e849445a0 — Dasky 2 years ago a9414f4
Implement split comms watchdog (#18599)

M data/mappings/info_config.json => data/mappings/info_config.json +2 -0
@@ 100,6 100,8 @@
    "SPLIT_USB_DETECT": {"info_key": "split.usb_detect.enabled", "value_type": "bool"},
    "SPLIT_USB_TIMEOUT": {"info_key": "split.usb_detect.timeout", "value_type": "int"},
    "SPLIT_USB_TIMEOUT_POLL": {"info_key": "split.usb_detect.polling_interval", "value_type": "int"},
    "SPLIT_WATCHDOG_ENABLE": {"info_key": "split.transport.watchdog", "value_type": "bool"},
    "SPLIT_WATCHDOG_TIMEOUT": {"info_key": "split.transport.watchdog_timeout", "value_type": "int"},
    "SOFT_SERIAL_PIN": {"info_key": "split.soft_serial_pin"},
    "SOFT_SERIAL_SPEED": {"info_key": "split.soft_serial_speed"},
    "TAP_CODE_DELAY": {"info_key": "qmk.tap_keycode_delay", "value_type": "int"},

M data/schemas/keyboard.jsonschema => data/schemas/keyboard.jsonschema +3 -1
@@ 508,7 508,9 @@
                            "enum": ["custom", "i2c", "serial", "serial_usart"]
                        },
                        "sync_matrix_state": {"type": "boolean"},
                        "sync_modifiers": {"type": "boolean"}
                        "sync_modifiers": {"type": "boolean"},
                        "watchdog": {"type": "boolean"},
                        "watchdog_timeout": {"$ref": "qmk.definitions.v1#/unsigned_int"}
                    }
                },
                "usb_detect": {

M docs/config_options.md => docs/config_options.md +7 -0
@@ 325,6 325,13 @@ There are a few different ways to set handedness for split keyboards (listed in 

* `#define SPLIT_USB_TIMEOUT_POLL 10`
  * Poll frequency when detecting master/slave when using `SPLIT_USB_DETECT`
  
* `#define SPLIT_WATCHDOG_ENABLE`
  * Reboot slave if no communication from master within timeout.
  * Helps resolve issue where both sides detect as slave using `SPLIT_USB_DETECT`

* `#define SPLIT_WATCHDOG_TIMEOUT 3000`
  * Maximum slave timeout when waiting for communication from master when using `SPLIT_WATCHDOG_ENABLE`

* `#define FORCED_SYNC_THROTTLE_MS 100`
  * Deadline for synchronizing data from master to slave when using the QMK-provided split transport.

M docs/feature_split_keyboard.md => docs/feature_split_keyboard.md +11 -0
@@ 422,6 422,17 @@ This sets the maximum timeout when detecting master/slave when using `SPLIT_USB_
```
This sets the poll frequency when detecting master/slave when using `SPLIT_USB_DETECT`

```c
#define SPLIT_WATCHDOG_ENABLE
```

This will enable a software watchdog on any side delegated as slave and will reboot the keyboard if no successful communication occurs within `SPLIT_WATCHDOG_TIMEOUT`. This can be particularly helpful when `SPLIT_USB_DETECT` delegates both sides as slave in some circumstances.

```c
#define SPLIT_WATCHDOG_TIMEOUT 3000
```
This set the maximum slave timeout when waiting for communication from master when using `SPLIT_WATCHDOG_ENABLE`

## Hardware Considerations and Mods

Master/slave delegation is made either by detecting voltage on VBUS connection or waiting for USB communication (`SPLIT_USB_DETECT`). Pro Micro boards can use VBUS detection out of the box and be used with or without `SPLIT_USB_DETECT`.

M quantum/keyboard.c => quantum/keyboard.c +4 -0
@@ 586,6 586,10 @@ void keyboard_task(void) {

    quantum_task();

#if defined(SPLIT_WATCHDOG_ENABLE)
    split_watchdog_task();
#endif

#if defined(RGBLIGHT_ENABLE)
    rgblight_task();
#endif

M quantum/split_common/split_util.c => quantum/split_common/split_util.c +43 -0
@@ 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
    }
}


M quantum/split_common/split_util.h => quantum/split_common/split_util.h +4 -0
@@ 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

M quantum/split_common/transaction_id_define.h => quantum/split_common/transaction_id_define.h +4 -0
@@ 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,

M quantum/split_common/transactions.c => quantum/split_common/transactions.c +33 -0
@@ 719,6 719,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] = {
    // Set defaults


@@ 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)

M quantum/split_common/transport.h => quantum/split_common/transport.h +4 -0
@@ 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];