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.

116 lines
3.9 KiB

  1. # UART Driver
  2. The UART drivers used in QMK have a set of common functions to allow portability between MCUs.
  3. Currently, this driver does not support enabling hardware flow control (the `RTS` and `CTS` pins) if available, but may do so in future.
  4. ## AVR Configuration
  5. No special setup is required - just connect the `RX` and `TX` pins of your UART device to the opposite pins on the MCU:
  6. |MCU |`TX`|`RX`|`CTS`|`RTS`|
  7. |-------------|----|----|-----|-----|
  8. |ATmega16/32U2|`D3`|`D2`|`D7` |`D6` |
  9. |ATmega16/32U4|`D3`|`D2`|`D5` |`B7` |
  10. |AT90USB64/128|`D3`|`D2`|*n/a*|*n/a*|
  11. |ATmega32A |`D1`|`D0`|*n/a*|*n/a*|
  12. |ATmega328/P |`D1`|`D0`|*n/a*|*n/a*|
  13. ## ChibiOS/ARM Configuration
  14. You'll need to determine which pins can be used for UART -- as an example, STM32 parts generally have multiple UART peripherals, labeled USART1, USART2, USART3 etc.
  15. To enable UART, modify your board's `halconf.h` to enable the serial driver:
  16. ```c
  17. #define HAL_USE_SERIAL TRUE
  18. ```
  19. Then, modify your board's `mcuconf.h` to enable the peripheral you've chosen, for example:
  20. ```c
  21. #undef STM32_SERIAL_USE_USART2
  22. #define STM32_SERIAL_USE_USART2 TRUE
  23. ```
  24. Configuration-wise, you'll need to set up the peripheral as per your MCU's datasheet -- the defaults match the pins for a Proton-C, i.e. STM32F303.
  25. |`config.h` override |Description |Default Value|
  26. |--------------------------|---------------------------------------------------------------|-------------|
  27. |`#define SERIAL_DRIVER` |USART peripheral to use - USART1 -> `SD1`, USART2 -> `SD2` etc.|`SD1` |
  28. |`#define SD1_TX_PIN` |The pin to use for TX |`A9` |
  29. |`#define SD1_TX_PAL_MODE` |The alternate function mode for TX |`7` |
  30. |`#define SD1_RX_PIN` |The pin to use for RX |`A10` |
  31. |`#define SD1_RX_PAL_MODE` |The alternate function mode for RX |`7` |
  32. |`#define SD1_CTS_PIN` |The pin to use for CTS |`A11` |
  33. |`#define SD1_CTS_PAL_MODE`|The alternate function mode for CTS |`7` |
  34. |`#define SD1_RTS_PIN` |The pin to use for RTS |`A12` |
  35. |`#define SD1_RTS_PAL_MODE`|The alternate function mode for RTS |`7` |
  36. ## Functions
  37. ### `void uart_init(uint32_t baud)`
  38. Initialize the UART driver. This function must be called only once, before any of the below functions can be called.
  39. #### Arguments
  40. - `uint32_t baud`
  41. The baud rate to transmit and receive at. This may depend on the device you are communicating with. Common values are 1200, 2400, 4800, 9600, 19200, 38400, 57600, and 115200.
  42. ---
  43. ### `void uart_write(uint8_t data)`
  44. Transmit a single byte.
  45. #### Arguments
  46. - `uint8_t data`
  47. The byte to write.
  48. ---
  49. ### `uint8_t uart_read(void)`
  50. Receive a single byte.
  51. #### Return Value
  52. The byte read from the receive buffer. This function will block if the buffer is empty (ie. no data to read).
  53. ---
  54. ### `void uart_transmit(const uint8_t *data, uint16_t length)`
  55. Transmit multiple bytes.
  56. #### Arguments
  57. - `const uint8_t *data`
  58. A pointer to the data to write from.
  59. - `uint16_t length`
  60. The number of bytes to write. Take care not to overrun the length of `data`.
  61. ---
  62. ### `void uart_receive(char *data, uint16_t length)`
  63. Receive multiple bytes.
  64. #### Arguments
  65. - `uint8_t *data`
  66. A pointer to the buffer to read into.
  67. - `uint16_t length`
  68. The number of bytes to read. Take care not to overrun the length of `data`.
  69. ---
  70. ### `bool uart_available(void)`
  71. Return whether the receive buffer contains data. Call this function to determine if `uart_read()` will return data immediately.
  72. #### Return Value
  73. `true` if the receive buffer length is non-zero.