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