logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

split_util.c (8995B)


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