1
0
Fork 0
forked from forks/qmk_firmware
qmk_firmware/docs/drivers/uart.md

123 lines
4.5 KiB
Markdown
Raw Normal View History

2024-05-30 04:00:41 +02:00
# UART Driver {#uart-driver}
2021-01-27 07:42:49 +01:00
The UART drivers used in QMK have a set of common functions to allow portability between MCUs.
Currently, this driver does not support enabling hardware flow control (the `RTS` and `CTS` pins) if available, but may do so in future.
2024-05-30 04:00:41 +02:00
## Usage {#usage}
In most cases, the UART driver code is automatically included if you are using a feature or driver which requires it.
However, if you need to use the driver standalone, add the following to your `rules.mk`:
```make
UART_DRIVER_REQUIRED = yes
```
You can then call the UART API by including `uart.h` in your code.
2024-05-30 04:00:41 +02:00
## AVR Configuration {#avr-configuration}
2021-01-27 07:42:49 +01:00
No special setup is required - just connect the `RX` and `TX` pins of your UART device to the opposite pins on the MCU:
|MCU |`TX`|`RX`|`CTS`|`RTS`|
|-------------|----|----|-----|-----|
|ATmega16/32U2|`D3`|`D2`|`D7` |`D6` |
|ATmega16/32U4|`D3`|`D2`|`D5` |`B7` |
|AT90USB64/128|`D3`|`D2`|*n/a*|*n/a*|
|ATmega32A |`D1`|`D0`|*n/a*|*n/a*|
|ATmega328/P |`D1`|`D0`|*n/a*|*n/a*|
2024-05-30 04:00:41 +02:00
## ChibiOS/ARM Configuration {#arm-configuration}
2021-01-27 07:42:49 +01:00
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.
To enable UART, modify your board's `mcuconf.h` to enable the peripheral you've chosen, for example:
2021-01-27 07:42:49 +01:00
```c
#undef STM32_SERIAL_USE_USART2
#define STM32_SERIAL_USE_USART2 TRUE
```
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.
| `config.h` override | Description | Default Value |
| --------------------------- | --------------------------------------------------------------- | ------------- |
| `#define UART_DRIVER` | USART peripheral to use - USART1 -> `SD1`, USART2 -> `SD2` etc. | `SD1` |
| `#define UART_TX_PIN` | The pin to use for TX | `A9` |
| `#define UART_TX_PAL_MODE` | The alternate function mode for TX | `7` |
| `#define UART_RX_PIN` | The pin to use for RX | `A10` |
| `#define UART_RX_PAL_MODE` | The alternate function mode for RX | `7` |
| `#define UART_CTS_PIN` | The pin to use for CTS | `A11` |
| `#define UART_CTS_PAL_MODE` | The alternate function mode for CTS | `7` |
| `#define UART_RTS_PIN` | The pin to use for RTS | `A12` |
| `#define UART_RTS_PAL_MODE` | The alternate function mode for RTS | `7` |
2021-01-27 07:42:49 +01:00
2024-05-30 04:00:41 +02:00
## API {#api}
2021-01-27 07:42:49 +01:00
2024-05-30 04:00:41 +02:00
### `void uart_init(uint32_t baud)` {#api-uart-init}
2021-01-27 07:42:49 +01:00
Initialize the UART driver. This function must be called only once, before any of the below functions can be called.
2024-05-30 04:00:41 +02:00
#### Arguments {#api-uart-init-arguments}
2021-01-27 07:42:49 +01:00
- `uint32_t baud`
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.
---
2024-05-30 04:00:41 +02:00
### `void uart_write(uint8_t data)` {#api-uart-write}
2021-01-27 07:42:49 +01:00
Transmit a single byte.
2024-05-30 04:00:41 +02:00
#### Arguments {#api-uart-write-arguments}
2021-01-27 07:42:49 +01:00
- `uint8_t data`
The byte to write.
2021-01-27 07:42:49 +01:00
---
2024-05-30 04:00:41 +02:00
### `uint8_t uart_read(void)` {#api-uart-read}
2021-01-27 07:42:49 +01:00
Receive a single byte.
2024-05-30 04:00:41 +02:00
#### Return Value {#api-uart-read-return}
2021-01-27 07:42:49 +01:00
The byte read from the receive buffer. This function will block if the buffer is empty (ie. no data to read).
---
2024-05-30 04:00:41 +02:00
### `void uart_transmit(const uint8_t *data, uint16_t length)` {#api-uart-transmit}
Transmit multiple bytes.
2024-05-30 04:00:41 +02:00
#### Arguments {#api-uart-transmit-arguments}
- `const uint8_t *data`
A pointer to the data to write from.
- `uint16_t length`
The number of bytes to write. Take care not to overrun the length of `data`.
---
2024-05-30 04:00:41 +02:00
### `void uart_receive(char *data, uint16_t length)` {#api-uart-receive}
Receive multiple bytes.
2024-05-30 04:00:41 +02:00
#### Arguments {#api-uart-receive-arguments}
- `uint8_t *data`
A pointer to the buffer to read into.
- `uint16_t length`
The number of bytes to read. Take care not to overrun the length of `data`.
2021-01-27 07:42:49 +01:00
---
2024-05-30 04:00:41 +02:00
### `bool uart_available(void)` {#api-uart-available}
2021-01-27 07:42:49 +01:00
Return whether the receive buffer contains data. Call this function to determine if `uart_read()` will return data immediately.
2021-01-27 07:42:49 +01:00
2024-05-30 04:00:41 +02:00
#### Return Value {#api-uart-available-return}
2021-01-27 07:42:49 +01:00
`true` if the receive buffer length is non-zero.