split_util.c 8.6 KB

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