split_util.c 8.7 KB

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