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.

128 lines
4.5 KiB

  1. # UART Driver :id=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. ## Usage :id=usage
  5. In most cases, the UART driver code is automatically included if you are using a feature or driver which requires it.
  6. However, if you need to use the driver standalone, add the following to your `rules.mk`:
  7. ```make
  8. UART_DRIVER_REQUIRED = yes
  9. ```
  10. You can then call the UART API by including `uart.h` in your code.
  11. ## AVR Configuration :id=avr-configuration
  12. No special setup is required - just connect the `RX` and `TX` pins of your UART device to the opposite pins on the MCU:
  13. |MCU |`TX`|`RX`|`CTS`|`RTS`|
  14. |-------------|----|----|-----|-----|
  15. |ATmega16/32U2|`D3`|`D2`|`D7` |`D6` |
  16. |ATmega16/32U4|`D3`|`D2`|`D5` |`B7` |
  17. |AT90USB64/128|`D3`|`D2`|*n/a*|*n/a*|
  18. |ATmega32A |`D1`|`D0`|*n/a*|*n/a*|
  19. |ATmega328/P |`D1`|`D0`|*n/a*|*n/a*|
  20. ## ChibiOS/ARM Configuration :id=arm-configuration
  21. 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.
  22. To enable UART, modify your board's `halconf.h` to enable the serial driver:
  23. ```c
  24. #define HAL_USE_SERIAL TRUE
  25. ```
  26. Then, modify your board's `mcuconf.h` to enable the peripheral you've chosen, for example:
  27. ```c
  28. #undef STM32_SERIAL_USE_USART2
  29. #define STM32_SERIAL_USE_USART2 TRUE
  30. ```
  31. 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.
  32. |`config.h` override |Description |Default Value|
  33. |--------------------------|---------------------------------------------------------------|-------------|
  34. |`#define SERIAL_DRIVER` |USART peripheral to use - USART1 -> `SD1`, USART2 -> `SD2` etc.|`SD1` |
  35. |`#define SD1_TX_PIN` |The pin to use for TX |`A9` |
  36. |`#define SD1_TX_PAL_MODE` |The alternate function mode for TX |`7` |
  37. |`#define SD1_RX_PIN` |The pin to use for RX |`A10` |
  38. |`#define SD1_RX_PAL_MODE` |The alternate function mode for RX |`7` |
  39. |`#define SD1_CTS_PIN` |The pin to use for CTS |`A11` |
  40. |`#define SD1_CTS_PAL_MODE`|The alternate function mode for CTS |`7` |
  41. |`#define SD1_RTS_PIN` |The pin to use for RTS |`A12` |
  42. |`#define SD1_RTS_PAL_MODE`|The alternate function mode for RTS |`7` |
  43. ## API :id=api
  44. ### `void uart_init(uint32_t baud)` :id=api-uart-init
  45. Initialize the UART driver. This function must be called only once, before any of the below functions can be called.
  46. #### Arguments :id=api-uart-init-arguments
  47. - `uint32_t baud`
  48. 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.
  49. ---
  50. ### `void uart_write(uint8_t data)` :id=api-uart-write
  51. Transmit a single byte.
  52. #### Arguments :id=api-uart-write-arguments
  53. - `uint8_t data`
  54. The byte to write.
  55. ---
  56. ### `uint8_t uart_read(void)` :id=api-uart-read
  57. Receive a single byte.
  58. #### Return Value :id=api-uart-read-return
  59. The byte read from the receive buffer. This function will block if the buffer is empty (ie. no data to read).
  60. ---
  61. ### `void uart_transmit(const uint8_t *data, uint16_t length)` :id=api-uart-transmit
  62. Transmit multiple bytes.
  63. #### Arguments :id=api-uart-transmit-arguments
  64. - `const uint8_t *data`
  65. A pointer to the data to write from.
  66. - `uint16_t length`
  67. The number of bytes to write. Take care not to overrun the length of `data`.
  68. ---
  69. ### `void uart_receive(char *data, uint16_t length)` :id=api-uart-receive
  70. Receive multiple bytes.
  71. #### Arguments :id=api-uart-receive-arguments
  72. - `uint8_t *data`
  73. A pointer to the buffer to read into.
  74. - `uint16_t length`
  75. The number of bytes to read. Take care not to overrun the length of `data`.
  76. ---
  77. ### `bool uart_available(void)` :id=api-uart-available
  78. Return whether the receive buffer contains data. Call this function to determine if `uart_read()` will return data immediately.
  79. #### Return Value :id=api-uart-available-return
  80. `true` if the receive buffer length is non-zero.