Parent directory

split_util.c

8967 bytes
  1/* Copyright 2021 QMK
  2 *
  3 * This program is free software: you can redistribute it and/or modify
  4 * it under the terms of the GNU General Public License as published by
  5 * the Free Software Foundation, either version 3 of the License, or
  6 * (at your option) any later version.
  7 *
  8 * This program is distributed in the hope that it will be useful,
  9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 11 * GNU General Public License for more details.
 12 *
 13 * You should have received a copy of the GNU General Public License
 14 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 15 */
 16#include "split_util.h"
 17#include "matrix.h"
 18#include "keyboard.h"
 19#include "timer.h"
 20#include "transport.h"
 21#include "wait.h"
 22#include "debug.h"
 23#include "usb_util.h"
 24#include "bootloader.h"
 25
 26#ifdef EE_HANDS
 27#    include "eeconfig.h"
 28#endif
 29
 30#if defined(RGBLIGHT_ENABLE) && defined(RGBLED_SPLIT)
 31#    include "rgblight.h"
 32#endif
 33
 34#ifndef SPLIT_USB_TIMEOUT
 35#    define SPLIT_USB_TIMEOUT 2000
 36#endif
 37
 38#ifndef SPLIT_USB_TIMEOUT_POLL
 39#    define SPLIT_USB_TIMEOUT_POLL 10
 40#endif
 41
 42// Max number of consecutive failed communications (one per scan cycle) before the communication is seen as disconnected.
 43// Set to 0 to disable the disconnection check altogether.
 44#ifndef SPLIT_MAX_CONNECTION_ERRORS
 45#    define SPLIT_MAX_CONNECTION_ERRORS 10
 46#endif // SPLIT_MAX_CONNECTION_ERRORS
 47
 48// How long (in milliseconds) to block all connection attempts after the communication has been flagged as disconnected.
 49// One communication attempt will be allowed everytime this amount of time has passed since the last attempt. If that attempt succeeds, the communication is seen as working again.
 50// Set to 0 to disable communication throttling while disconnected
 51#ifndef SPLIT_CONNECTION_CHECK_TIMEOUT
 52#    define SPLIT_CONNECTION_CHECK_TIMEOUT 500
 53#endif // SPLIT_CONNECTION_CHECK_TIMEOUT
 54
 55static uint8_t connection_errors = 0;
 56
 57volatile bool isLeftHand = true;
 58
 59static struct {
 60    bool master;
 61    bool left;
 62} split_config;
 63
 64#if defined(SPLIT_USB_DETECT)
 65_Static_assert((SPLIT_USB_TIMEOUT / SPLIT_USB_TIMEOUT_POLL) <= UINT16_MAX, "Please lower SPLIT_USB_TIMEOUT and/or increase SPLIT_USB_TIMEOUT_POLL.");
 66static bool usb_bus_detected(void) {
 67    for (uint16_t i = 0; i < (SPLIT_USB_TIMEOUT / SPLIT_USB_TIMEOUT_POLL); i++) {
 68        // This will return true if a USB connection has been established
 69        if (usb_connected_state()) {
 70            return true;
 71        }
 72        wait_ms(SPLIT_USB_TIMEOUT_POLL);
 73    }
 74    return false;
 75}
 76#else
 77static inline bool usb_bus_detected(void) {
 78    return usb_vbus_state();
 79}
 80#endif
 81
 82#if defined(SPLIT_WATCHDOG_ENABLE)
 83#    if !defined(SPLIT_WATCHDOG_TIMEOUT)
 84#        if defined(SPLIT_USB_TIMEOUT)
 85#            define SPLIT_WATCHDOG_TIMEOUT (SPLIT_USB_TIMEOUT + 100)
 86#        else
 87#            define SPLIT_WATCHDOG_TIMEOUT 3000
 88#        endif
 89#    endif
 90#    if defined(SPLIT_USB_DETECT)
 91_Static_assert(SPLIT_USB_TIMEOUT < SPLIT_WATCHDOG_TIMEOUT, "SPLIT_WATCHDOG_TIMEOUT should not be below SPLIT_USB_TIMEOUT.");
 92#    endif
 93_Static_assert(SPLIT_MAX_CONNECTION_ERRORS > 0, "SPLIT_WATCHDOG_ENABLE requires SPLIT_MAX_CONNECTION_ERRORS be above 0 for a functioning disconnection check.");
 94
 95static uint32_t split_watchdog_started = 0;
 96static bool     split_watchdog_done    = false;
 97
 98void split_watchdog_init(void) {
 99    split_watchdog_started = timer_read32();
100}
101
102void split_watchdog_update(bool done) {
103    split_watchdog_done = done;
104}
105
106bool split_watchdog_check(void) {
107    if (!is_transport_connected()) {
108        split_watchdog_done = false;
109    }
110    return split_watchdog_done;
111}
112
113void split_watchdog_task(void) {
114    if (!split_watchdog_done && !is_keyboard_master()) {
115        if (timer_elapsed32(split_watchdog_started) > SPLIT_WATCHDOG_TIMEOUT) {
116            mcu_reset();
117        }
118    }
119}
120#endif // defined(SPLIT_WATCHDOG_ENABLE)
121
122#ifdef SPLIT_HAND_MATRIX_GRID
123void matrix_io_delay(void);
124
125static uint8_t peek_matrix_intersection(pin_t out_pin, pin_t in_pin) {
126    gpio_set_pin_input_high(in_pin);
127    gpio_set_pin_output(out_pin);
128    gpio_write_pin_low(out_pin);
129    // It's almost unnecessary, but wait until it's down to low, just in case.
130    wait_us(1);
131    uint8_t pin_state = gpio_read_pin(in_pin);
132    // Set out_pin to a setting that is less susceptible to noise.
133    gpio_set_pin_input_high(out_pin);
134    matrix_io_delay(); // Wait for the pull-up to go HIGH.
135    return pin_state;
136}
137#endif
138
139__attribute__((weak)) bool is_keyboard_left_impl(void) {
140#if defined(SPLIT_HAND_PIN)
141    gpio_set_pin_input(SPLIT_HAND_PIN);
142    wait_us(100);
143    // Test pin SPLIT_HAND_PIN for High/Low, if low it's right hand
144#    ifdef SPLIT_HAND_PIN_LOW_IS_LEFT
145    return !gpio_read_pin(SPLIT_HAND_PIN);
146#    else
147    return gpio_read_pin(SPLIT_HAND_PIN);
148#    endif
149#elif defined(SPLIT_HAND_MATRIX_GRID)
150#    ifdef SPLIT_HAND_MATRIX_GRID_LOW_IS_LEFT
151    return !peek_matrix_intersection(SPLIT_HAND_MATRIX_GRID);
152#    else
153    return peek_matrix_intersection(SPLIT_HAND_MATRIX_GRID);
154#    endif
155#elif defined(EE_HANDS)
156    if (!eeconfig_is_enabled()) {
157        eeconfig_init();
158    }
159    // TODO: Remove once ARM has a way to configure EECONFIG_HANDEDNESS within the emulated eeprom via dfu-util or another tool
160#    if defined(INIT_EE_HANDS_LEFT) || defined(INIT_EE_HANDS_RIGHT)
161#        if defined(INIT_EE_HANDS_LEFT)
162#            pragma message "Faking EE_HANDS for left hand"
163    const bool should_be_left = true;
164#        else
165#            pragma message "Faking EE_HANDS for right hand"
166    const bool should_be_left = false;
167#        endif
168    bool       is_left        = eeconfig_read_handedness();
169    if (is_left != should_be_left) {
170        eeconfig_update_handedness(should_be_left);
171    }
172#    endif // defined(INIT_EE_HANDS_LEFT) || defined(INIT_EE_HANDS_RIGHT)
173    return eeconfig_read_handedness();
174#elif defined(MASTER_RIGHT)
175    return !is_keyboard_master();
176#else
177    return is_keyboard_master();
178#endif
179}
180
181__attribute__((weak)) bool is_keyboard_master_impl(void) {
182    bool is_master = usb_bus_detected();
183
184    // Avoid NO_USB_STARTUP_CHECK - Disable USB as the previous checks seem to enable it somehow
185    if (!is_master) {
186        usb_disconnect();
187    }
188    return is_master;
189}
190
191__attribute__((weak)) bool is_keyboard_left(void) {
192    return split_config.left;
193}
194
195__attribute__((weak)) bool is_keyboard_master(void) {
196    return split_config.master;
197}
198
199// this code runs before the keyboard is fully initialized
200void split_pre_init(void) {
201    split_config.master = is_keyboard_master_impl();
202    split_config.left   = is_keyboard_left_impl();
203
204    isLeftHand = is_keyboard_left(); // TODO: Remove isLeftHand
205
206#if defined(RGBLIGHT_ENABLE) && defined(RGBLED_SPLIT)
207    uint8_t num_rgb_leds_split[2] = RGBLED_SPLIT;
208    if (is_keyboard_left()) {
209        rgblight_set_clipping_range(0, num_rgb_leds_split[0]);
210    } else {
211        rgblight_set_clipping_range(num_rgb_leds_split[0], num_rgb_leds_split[1]);
212    }
213#endif
214
215    if (is_keyboard_master()) {
216        transport_master_init();
217    }
218}
219
220// this code runs after the keyboard is fully initialized
221//   - avoids race condition during matrix_init_quantum where slave can start
222//     receiving before the init process has completed
223void split_post_init(void) {
224    if (!is_keyboard_master()) {
225        transport_slave_init();
226#if defined(SPLIT_WATCHDOG_ENABLE)
227        split_watchdog_init();
228#endif
229    }
230}
231
232bool is_transport_connected(void) {
233    return connection_errors < SPLIT_MAX_CONNECTION_ERRORS;
234}
235
236bool transport_master_if_connected(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
237#if SPLIT_MAX_CONNECTION_ERRORS > 0 && SPLIT_CONNECTION_CHECK_TIMEOUT > 0
238    // Throttle transaction attempts if target doesn't seem to be connected
239    // Without this, a solo half becomes unusable due to constant read timeouts
240    static uint16_t connection_check_timer = 0;
241    const bool      is_disconnected        = !is_transport_connected();
242    if (is_disconnected && timer_elapsed(connection_check_timer) < SPLIT_CONNECTION_CHECK_TIMEOUT) {
243        return false;
244    }
245#endif // SPLIT_MAX_CONNECTION_ERRORS > 0 && SPLIT_CONNECTION_CHECK_TIMEOUT > 0
246
247    __attribute__((unused)) bool okay = transport_master(master_matrix, slave_matrix);
248#if SPLIT_MAX_CONNECTION_ERRORS > 0
249    if (!okay) {
250        if (connection_errors < UINT8_MAX) {
251            connection_errors++;
252        }
253#    if SPLIT_CONNECTION_CHECK_TIMEOUT > 0
254        bool connected = is_transport_connected();
255        if (!connected) {
256            connection_check_timer = timer_read();
257            dprintln("Target disconnected, throttling connection attempts");
258        }
259        return connected;
260    } else if (is_disconnected) {
261        dprintln("Target connected");
262#    endif // SPLIT_CONNECTION_CHECK_TIMEOUT > 0
263    }
264
265    connection_errors = 0;
266#endif // SPLIT_MAX_CONNECTION_ERRORS > 0
267    return true;
268}