import { _ as _export_sfc, c as createElementBlock, o as openBlock, a8 as createStaticVNode } from "./chunks/framework.Cauyuiy8.js"; const __pageData = JSON.parse('{"title":"UART Driver","description":"","frontmatter":{},"headers":[],"relativePath":"drivers/uart.md","filePath":"drivers/uart.md","lastUpdated":null}'); const _sfc_main = { name: "drivers/uart.md" }; const _hoisted_1 = /* @__PURE__ */ createStaticVNode('
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.
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
:
UART_DRIVER_REQUIRED = yes
You can then call the UART API by including uart.h
in your code.
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 |
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:
#pragma once\n\n#include_next <mcuconf.h>\n\n#undef STM32_SERIAL_USE_USART2\n#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 |
---|---|---|
UART_DRIVER | USART peripheral to use - USART1 -> SD1 , USART2 -> SD2 etc. | SD1 |
UART_TX_PIN | The pin to use for TX | A9 |
UART_TX_PAL_MODE | The alternate function mode for TX | 7 |
UART_RX_PIN | The pin to use for RX | A10 |
UART_RX_PAL_MODE | The alternate function mode for RX | 7 |
UART_CTS_PIN | The pin to use for CTS | A11 |
UART_CTS_PAL_MODE | The alternate function mode for CTS | 7 |
UART_RTS_PIN | The pin to use for RTS | A12 |
UART_RTS_PAL_MODE | The alternate function mode for RTS | 7 |
void uart_init(uint32_t baud)
Initialize the UART driver. This function must be called only once, before any of the below functions can be called.
uint32_t baud
void uart_write(uint8_t data)
Transmit a single byte.
uint8_t data
uint8_t uart_read(void)
Receive a single byte.
The byte read from the receive buffer. This function will block if the buffer is empty (ie. no data to read).
void uart_transmit(const uint8_t *data, uint16_t length)
Transmit multiple bytes.
const uint8_t *data
uint16_t length
data
.void uart_receive(char *data, uint16_t length)
Receive multiple bytes.
uint8_t *data
uint16_t length
data
.bool uart_available(void)
Return whether the receive buffer contains data. Call this function to determine if uart_read()
will return data immediately.
true
if there is data available to read.