You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

389 lines
17 KiB

Make solo half of split keyboards (more) usable. (#13523) * Make solo half of split keyboards (more) usable. Using only one half of a split keyboard (that's using the split_common framework to communicate) is not a great experience, since several read timeouts per scan cycle cause an unusably slow scan rate. This change blocks all split communication attempts for 500 ms (configurable) after an error occurs, causing the scan rate to become at least _more_ usable, but might need some tweaking to work fully on most keyboards. One read timeout still needs to occur after the 500 ms has passed, and if that timeout isn't low enough, some scan cycles may still be too slow. * Fix lint complaint. * Require 25 consecutive comm errors to see comms as disconnected. The number of max errors can be overridden by defining `SPLIT_MAX_CONNECTION_ERRORS`. * Add comments to new defines, and ability to disable disconnection check. Also increase `SPLIT_MAX_CONNECTION_ERRORS` to 40, since it's divisible by most relevant numbers for the description. * Make lint happy ...again * Only update `connection_check_timer` when needed. * Add new defines to split keyboard documentation. * Move connection timeout logic to transport.c, add `is_transport_connected`. * Use split_common disconnection logic in matrix.c. Instead of doing more or less the same thing twice. * Move disconnection logic to `transport_master`. Is a cleaner implementation, and causes the scan rate while disconnected to increase instead of decrease. * Lint fixes. * Lower default `SERIAL_USART_TIMEOUT` to 20 ms. The read timeout must be low enough to not cause exessively long scan cycles when using a solo split half. 10 ms was determined from testing to work fine even with the slowest defined baudrate of 19200 (5 ms was too low for that case), so 20 ms should be fine for most cases. * Remove `SERIAL_USART_TIMEOUT` from ergodox_infinity/config.h Was somewhat mistakenly included in an earlier PR. * Fix building with `USE_I2C`. * Reduce built firmware size. Not really sure why this works, the idea was taken from tzarc's work on split disconnection. * Tweak and improve opt-out for split disconnection logic. There are now two ways to opt out from this feature: * Set `SPLIT_MAX_CONNECTION_ERRORS` to 0. This will completely disable the connection status checks (also affects the slave matrix reset logic in matrix.c, though). * Set `SPLIT_CONNECTION_CHECK_TIMEOUT` to 0. This will only disable the communication throttling while disconnected. Will make the firmware smaller. * Make split disconnection logic work with custom transports. Includes a fallback implementation for keyboards using a custom split_util.c but not a custom matrix.c (currently no such keyboard seems to be merged, though). * Remove unnecessary include of timer.h Co-authored-by: Joel Challis <git@zvecr.com> Co-authored-by: Joel Challis <git@zvecr.com>
2 years ago
  1. # 'serial' Driver
  2. The Serial driver powers the [Split Keyboard](feature_split_keyboard.md) feature. Several implementations are available that cater to the platform and capabilites of MCU in use. Note that none of the drivers support split keyboards with more than two halves.
  3. | Driver | AVR | ARM | Connection between halves |
  4. | --------------------------------------- | ------------------ | ------------------ | --------------------------------------------------------------------------------------------- |
  5. | [Bitbang](#bitbang) | :heavy_check_mark: | :heavy_check_mark: | Single wire communication. One wire is used for reception and transmission. |
  6. | [USART Half-duplex](#usart-half-duplex) | | :heavy_check_mark: | Efficient single wire communication. One wire is used for reception and transmission. |
  7. | [USART Full-duplex](#usart-full-duplex) | | :heavy_check_mark: | Efficient two wire communication. Two distinct wires are used for reception and transmission. |
  8. ?> Serial in this context should be read as **sending information one bit at a time**, rather than implementing UART/USART/RS485/RS232 standards.
  9. <hr>
  10. ## Bitbang
  11. This is the Default driver, absence of configuration assumes this driver. It works by [bit banging](https://en.wikipedia.org/wiki/Bit_banging) a GPIO pin using the CPU. It is therefore not as efficient as a dedicated hardware peripheral, which the Half-duplex and Full-duplex drivers use.
  12. !> On ARM platforms the bitbang driver causes connection issues when using it together with the bitbang WS2812 driver. Choosing alternate drivers for both serial and WS2812 (instead of bitbang) is strongly recommended.
  13. ### Pin configuration
  14. ```
  15. LEFT RIGHT
  16. +-------+ SERIAL +-------+
  17. | SSP |-----------------| SSP |
  18. | | VDD | |
  19. | |-----------------| |
  20. | | GND | |
  21. | |-----------------| |
  22. +-------+ +-------+
  23. ```
  24. One GPIO pin is needed for the bitbang driver, as only one wire is used for receiving and transmitting data. This pin is referred to as the `SOFT_SERIAL_PIN` (SSP) in the configuration. A TRS or USB cable provides enough conductors for this driver to function.
  25. ### Setup
  26. To use the bitbang driver follow these steps to activate it.
  27. 1. Change the `SERIAL_DRIVER` to `bitbang` in your keyboards `rules.mk` file:
  28. ```make
  29. SERIAL_DRIVER = bitbang
  30. ```
  31. 2. Configure the GPIO pin of your keyboard via the `config.h` file:
  32. ```c
  33. #define SOFT_SERIAL_PIN D0 // or D1, D2, D3, E6
  34. ```
  35. 3. On ARM platforms you must turn on ChibiOS `PAL_USE_CALLBACKS` feature:
  36. * In `halconf.h` add the line `#define PAL_USE_CALLBACKS TRUE`.
  37. <hr>
  38. ## USART Half-duplex
  39. Targeting ARM boards based on ChibiOS, where communication is offloaded to a USART hardware device that supports Half-duplex operation. The advantages over bitbanging are fast, accurate timings and reduced CPU usage. Therefore it is advised to choose Half-duplex over Bitbang if MCU is capable of utilising Half-duplex, and Full-duplex can't be used instead (e.g. lack of available GPIO pins, or imcompatible PCB design).
  40. ### Pin configuration
  41. ```
  42. LEFT RIGHT
  43. +-------+ | | +-------+
  44. | | R R | |
  45. | | | SERIAL | | |
  46. | TX |-----------------| TX |
  47. | | VDD | |
  48. | |-----------------| |
  49. | | GND | |
  50. | |-----------------| |
  51. +-------+ +-------+
  52. ```
  53. Only one GPIO pin is needed for the Half-duplex driver, as only one wire is used for receiving and transmitting data. This pin is referred to as the `SERIAL_USART_TX_PIN` in the configuration. Ensure that the pin chosen for split communication can operate as the TX pin of the contoller's USART peripheral. A TRS or USB cable provides enough conductors for this driver to function. As the split connection is configured to operate in open-drain mode, an **external pull-up resistor is needed to keep the line high**. Resistor values of 1.5kΩ to 8.2kΩ are known to work.
  54. !> ***Note:*** A pull-up resistor isn't required for RP2040 controllers configured with PIO subsystem.
  55. ### Setup
  56. To use the Half-duplex driver follow these steps to activate it. If you target the Raspberry Pi RP2040 PIO implementation, start at step 2.
  57. 1. Change the `SERIAL_DRIVER` to `usart` in your keyboards `rules.mk` file:
  58. ```make
  59. SERIAL_DRIVER = usart
  60. ```
  61. Skip to step 3.
  62. 2. (RP2040 + PIO only!) Change the `SERIAL_DRIVER` to `vendor` in your keyboards `rules.mk` file:
  63. ```make
  64. SERIAL_DRIVER = vendor
  65. ```
  66. 3. Configure the hardware of your keyboard via the `config.h` file:
  67. ```c
  68. #define SERIAL_USART_TX_PIN B6 // The GPIO pin that is used split communication.
  69. ```
  70. For STM32 MCUs several GPIO configuration options can be changed as well. See the section ["Alternate Functions for selected STM32 MCUs"](alternate-functions-for-selected-stm32-mcus).
  71. ```c
  72. #define USART1_REMAP // Remap USART TX and RX pins on STM32F103 MCUs, see table below.
  73. #define SERIAL_USART_TX_PAL_MODE 7 // Pin "alternate function", see the respective datasheet for the appropriate values for your MCU. default: 7
  74. ```
  75. 4. Decide either for `SERIAL`, `SIO`, or `PIO` subsystem. See section ["Choosing a driver subsystem"](#choosing-a-driver-subsystem).
  76. <hr>
  77. ## USART Full-duplex
  78. Targeting ARM boards based on ChibiOS where communication is offloaded to an USART hardware device. The advantages over bitbanging are fast, accurate timings and reduced CPU usage; therefore it is advised to choose this driver over all others where possible. Due to its internal design Full-duplex is slightly more efficient than the Half-duplex driver, but Full-duplex should be primarily chosen if Half-duplex operation is not supported by the controller's USART peripheral.
  79. ### Pin configuration
  80. ```
  81. LEFT RIGHT
  82. +-------+ +-------+
  83. | | SERIAL | |
  84. | TX |-----------------| RX |
  85. | | SERIAL | |
  86. | RX |-----------------| TX |
  87. | | VDD | |
  88. | |-----------------| |
  89. | | GND | |
  90. | |-----------------| |
  91. +-------+ +-------+
  92. ```
  93. Two GPIO pins are needed for the Full-duplex driver, as two distinct wires are used for receiving and transmitting data. The pin transmitting data is the `TX` pin and refereed to as the `SERIAL_USART_TX_PIN`, the pin receiving data is the `RX` pin and refereed to as the `SERIAL_USART_RX_PIN` in this configuration. Please note that `TX` pin of the master half has to be connected with the `RX` pin of the slave half and the `RX` pin of the master half has to be connected with the `TX` pin of the slave half! Usually this pin swap has to be done outside of the MCU e.g. with cables or on the PCB. Some MCUs like the STM32F303 used on the Proton-C allow this pin swap directly inside the MCU. A TRRS or USB cable provides enough conductors for this driver to function.
  94. To use this driver the USART peripherals `TX` and `RX` pins must be configured with the correct Alternate-functions. If you are using a Proton-C development board everything is already setup, same is true for STM32F103 MCUs. For MCUs which are using a modern flexible GPIO configuration you have to specify these by setting `SERIAL_USART_TX_PAL_MODE` and `SERIAL_USART_RX_PAL_MODE`. Refer to the corresponding datasheets of your MCU or find those settings in the section ["Alternate Functions for selected STM32 MCUs"](#alternate-functions-for-selected-stm32-mcus).
  95. ### Setup
  96. To use the Full-duplex driver follow these steps to activate it. If you target the Raspberry Pi RP2040 PIO implementation, start at step 2
  97. 1. Change the `SERIAL_DRIVER` to `usart` in your keyboards `rules.mk` file:
  98. ```make
  99. SERIAL_DRIVER = usart
  100. ```
  101. Skip to step 3
  102. 2. (RP2040 + PIO only!) Change the `SERIAL_DRIVER` to `vendor` in your keyboards `rules.mk` file:
  103. ```make
  104. SERIAL_DRIVER = vendor
  105. ```
  106. 3. Configure the hardware of your keyboard via the `config.h` file:
  107. ```c
  108. #define SERIAL_USART_FULL_DUPLEX // Enable full duplex operation mode.
  109. #define SERIAL_USART_TX_PIN B6 // USART TX pin
  110. #define SERIAL_USART_RX_PIN B7 // USART RX pin
  111. ```
  112. For STM32 MCUs several GPIO configuration options, including the ability for `TX` to `RX` pin swapping, can be changed as well. See the section ["Alternate Functions for selected STM32 MCUs"](alternate-functions-for-selected-stm32-mcus).
  113. ```c
  114. #define SERIAL_USART_PIN_SWAP // Swap TX and RX pins if keyboard is master halve. (Only available on some MCUs)
  115. #define USART1_REMAP // Remap USART TX and RX pins on STM32F103 MCUs, see table below.
  116. #define SERIAL_USART_TX_PAL_MODE 7 // Pin "alternate function", see the respective datasheet for the appropriate values for your MCU. default: 7
  117. ```
  118. 4. Decide either for `SERIAL`, `SIO`, or `PIO` subsystem. See section ["Choosing a driver subsystem"](#choosing-a-driver-subsystem).
  119. <hr>
  120. ## Choosing a driver subsystem
  121. ### The `SERIAL` driver
  122. The `SERIAL` Subsystem is supported for the majority of ChibiOS MCUs and should be used whenever supported. Follow these steps in order to activate it:
  123. 1. In your keyboards `halconf.h` add:
  124. ```c
  125. #define HAL_USE_SERIAL TRUE
  126. ```
  127. 2. In your keyboards `mcuconf.h`: activate the USART peripheral that is used on your MCU. The shown example is for an STM32 MCU, so this will not work on MCUs by other manufacturers. You can find the correct names in the `mcuconf.h` files of your MCU that ship with ChibiOS.
  128. Just below `#include_next <mcuconf.h>` add:
  129. ```c
  130. #include_next <mcuconf.h>
  131. #undef STM32_SERIAL_USE_USARTn
  132. #define STM32_SERIAL_USE_USARTn TRUE
  133. ```
  134. Where 'n' matches the peripheral number of your selected USART on the MCU.
  135. 3. In you keyboards `config.h`: override the default USART `SERIAL` driver if you use a USART peripheral that does not belong to the default selected `SD1` driver. For instance, if you selected `STM32_SERIAL_USE_USART3` the matching driver would be `SD3`.
  136. ```c
  137. #define SERIAL_USART_DRIVER SD3
  138. ```
  139. ### The `SIO` driver
  140. The `SIO` Subsystem was added to ChibiOS with the 21.11 release and is only supported on selected MCUs. It should only be chosen when the `SERIAL` subsystem is not supported by your MCU.
  141. Follow these steps in order to activate it:
  142. 1. In your keyboards `halconf.h` add:
  143. ```c
  144. #define HAL_USE_SIO TRUE
  145. ```
  146. 2. In your keyboards `mcuconf.h:` activate the USART peripheral that is used on your MCU. The shown example is for an STM32 MCU, so this will not work on MCUs by other manufacturers. You can find the correct names in the `mcuconf.h` files of your MCU that ship with ChibiOS.
  147. Just below `#include_next <mcuconf.h>` add:
  148. ```c
  149. #include_next <mcuconf.h>
  150. #undef STM32_SIO_USE_USARTn
  151. #define STM32_SIO_USE_USARTn TRUE
  152. ```
  153. Where 'n' matches the peripheral number of your selected USART on the MCU.
  154. 3. In the keyboard's `config.h` file: override the default USART `SIO` driver if you use a USART peripheral that does not belong to the default selected `SIOD1` driver. For instance, if you selected `STM32_SERIAL_USE_USART3` the matching driver would be `SIOD3`.
  155. ```c
  156. #define SERIAL_USART_DRIVER SIOD3
  157. ```
  158. ### The `PIO` driver
  159. The `PIO` subsystem is a Raspberry Pi RP2040 specific implementation, using an integrated PIO peripheral and is therefore only available on this MCU. Because of the flexible nature of PIO peripherals, **any** GPIO pin can be used as a `TX` or `RX` pin. Half-duplex and Full-duplex operation modes are fully supported with this driver. Half-duplex uses the built-in pull-ups and GPIO manipulation of the RP2040 to drive the line high by default, thus an external pull-up resistor **is not required**.
  160. Optionally, the PIO peripheral utilized for split communication can be changed with the following define in config.h:
  161. ```c
  162. #define SERIAL_PIO_USE_PIO1 // Force the usage of PIO1 peripheral, by default the Serial implementation uses the PIO0 peripheral
  163. ```
  164. The Serial PIO program uses 2 state machines, 13 instructions and the complete interrupt handler of the PIO peripheral it is running on.
  165. <hr>
  166. ## Advanced Configuration
  167. There are several advanced configuration options that can be defined in your keyboards `config.h` file:
  168. ### Baudrate
  169. If you're having issues or need a higher baudrate with serial communication, you can change the baudrate which in turn controls the communication speed for serial. You want to lower the baudrate if you experience failed transactions.
  170. ```c
  171. #define SELECT_SOFT_SERIAL_SPEED {#}
  172. ```
  173. | Speed | Bitbang | Half-duplex and Full-duplex |
  174. | ----- | -------------------------- | --------------------------- |
  175. | `0` | 189000 baud (experimental) | 460800 baud |
  176. | `1` | 137000 baud (default) | 230400 baud (default) |
  177. | `2` | 75000 baud | 115200 baud |
  178. | `3` | 39000 baud | 57600 baud |
  179. | `4` | 26000 baud | 38400 baud |
  180. | `5` | 20000 baud | 19200 baud |
  181. Alternatively you can specify the baudrate directly by defining `SERIAL_USART_SPEED`.
  182. ### Timeout
  183. This is the default time window in milliseconds in which a successful communication has to complete. Usually you don't want to change this value. But you can do so anyways by defining an alternate one in your keyboards `config.h` file:
  184. ```c
  185. #define SERIAL_USART_TIMEOUT 20 // USART driver timeout. default 20
  186. ```
  187. <hr>
  188. ## Troubleshooting
  189. If you're having issues withe serial communication, you can enable debug messages that will give you insights which part of the communication failed. The enable these messages add to your keyboards `config.h` file:
  190. ```c
  191. #define SERIAL_DEBUG
  192. ```
  193. ?> The messages will be printed out to the `CONSOLE` output. For additional information, refer to [Debugging/Troubleshooting QMK](faq_debug.md).
  194. ## Alternate Functions for selected STM32 MCUs
  195. Pins for USART Peripherals with
  196. ### STM32F303 / Proton-C [Datasheet](https://www.st.com/resource/en/datasheet/stm32f303cc.pdf)
  197. Pin Swap available: :heavy_check_mark:
  198. | Pin | Function | Mode |
  199. | ---------- | -------- | ---- |
  200. | **USART1** | | |
  201. | PA9 | TX | AF7 |
  202. | PA10 | RX | AF7 |
  203. | PB6 | TX | AF7 |
  204. | PB7 | RX | AF7 |
  205. | PC4 | TX | AF7 |
  206. | PC5 | RX | AF7 |
  207. | PE0 | TX | AF7 |
  208. | PE1 | RX | AF7 |
  209. | **USART2** | | |
  210. | PA2 | TX | AF7 |
  211. | PA3 | RX | AF7 |
  212. | PA14 | TX | AF7 |
  213. | PA15 | RX | AF7 |
  214. | PB3 | TX | AF7 |
  215. | PB4 | RX | AF7 |
  216. | PD5 | TX | AF7 |
  217. | PD6 | RX | AF7 |
  218. | **USART3** | | |
  219. | PB10 | TX | AF7 |
  220. | PB11 | RX | AF7 |
  221. | PC10 | TX | AF7 |
  222. | PC11 | RX | AF7 |
  223. | PD8 | TX | AF7 |
  224. | PD9 | RX | AF7 |
  225. ### STM32F072 [Datasheet](https://www.st.com/resource/en/datasheet/stm32f072c8.pdf)
  226. Pin Swap available: :heavy_check_mark:
  227. | Pin | Function | Mode |
  228. | ------ | -------- | ---- |
  229. | USART1 | | |
  230. | PA9 | TX | AF1 |
  231. | PA10 | RX | AF1 |
  232. | PB6 | TX | AF0 |
  233. | PB7 | RX | AF0 |
  234. | USART2 | | |
  235. | PA2 | TX | AF1 |
  236. | PA3 | RX | AF1 |
  237. | PA14 | TX | AF1 |
  238. | PA15 | RX | AF1 |
  239. | USART3 | | |
  240. | PB10 | TX | AF4 |
  241. | PB11 | RX | AF4 |
  242. | PC4 | TX | AF1 |
  243. | PC5 | RX | AF1 |
  244. | PC10 | TX | AF1 |
  245. | PC11 | RX | AF1 |
  246. | PD8 | TX | AF0 |
  247. | PD9 | RX | AF0 |
  248. | USART4 | | |
  249. | PA0 | TX | AF4 |
  250. | PA1 | RX | AF4 |
  251. ### STM32F103 Medium Density (C8-CB) [Datasheet](https://www.st.com/resource/en/datasheet/stm32f103c8.pdf)
  252. Pin Swap available: N/A
  253. TX Pin is always Alternate Function Push-Pull, RX Pin is always regular input pin for any USART peripheral. **For STM32F103 no additional Alternate Function configuration is necessary. QMK is already configured.**
  254. Pin remapping:
  255. The pins of USART Peripherals use default Pins that can be remapped to use other pins using the AFIO registers. Default pins are marked **bold**. Add the appropriate defines to your config.h file.
  256. | Pin | Function | Mode | USART_REMAP |
  257. | ---------- | -------- | ---- | ------------------- |
  258. | **USART1** | | | |
  259. | **PA9** | TX | AFPP | |
  260. | **PA10** | RX | IN | |
  261. | PB6 | TX | AFPP | USART1_REMAP |
  262. | PB7 | RX | IN | USART1_REMAP |
  263. | **USART2** | | | |
  264. | **PA2** | TX | AFPP | |
  265. | **PA3** | RX | IN | |
  266. | PD5 | TX | AFPP | USART2_REMAP |
  267. | PD6 | RX | IN | USART2_REMAP |
  268. | **USART3** | | | |
  269. | **PB10** | TX | AFPP | |
  270. | **PB11** | RX | IN | |
  271. | PC10 | TX | AFPP | USART3_PARTIALREMAP |
  272. | PC11 | RX | IN | USART3_PARTIALREMAP |
  273. | PD8 | TX | AFPP | USART3_FULLREMAP |
  274. | PD9 | RX | IN | USART3_FULLREMAP |