split_util.c 8.7 KB

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