STM32 RTOS 0.2.3
Bare-metal preemptive RTOS for STM32F446RE
Loading...
Searching...
No Matches
uart.h File Reference

UART driver: asynchronous configuration and blocking transfer. More...

#include "driver_status.h"
#include "uart_reg.h"
#include <stddef.h>
#include <stdint.h>
Include dependency graph for uart.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

DRIVER_MUST_CHECK DriverStatus_e uartInit (UartRegisters_t *uart, uint32_t pclk_hz, uint32_t baud, uint32_t stop_bits, uint32_t parity_enable, uint32_t parity_select, uint32_t direction)
 Configure an instance for asynchronous 8-bit operation and enable it.
void uartDeinit (UartRegisters_t *uart)
 Return CR1, CR2, CR3, BRR and GTPR to their reset values (disabled).
DRIVER_MUST_CHECK DriverStatus_e uartFlush (const UartRegisters_t *uart, uint32_t timeout)
 Wait until the last byte has left the shift register (SR.TC).
DRIVER_MUST_CHECK DriverStatus_e uartTransmitByte (UartRegisters_t *uart, uint8_t byte, uint32_t timeout)
 Send one byte once DR is free (SR.TXE).
DRIVER_MUST_CHECK DriverStatus_e uartTransmit (UartRegisters_t *uart, const uint8_t *data, size_t length, uint32_t timeout)
 Send a buffer and wait until the line is idle.
DRIVER_MUST_CHECK DriverStatus_e uartReceiveByte (UartRegisters_t *uart, uint8_t *byte, uint32_t timeout)
 Receive one byte once SR.RXNE sets.
DRIVER_MUST_CHECK DriverStatus_e uartReceive (UartRegisters_t *uart, uint8_t *data, size_t length, uint32_t timeout)
 Receive a buffer, one byte at a time.

Detailed Description

UART driver: asynchronous configuration and blocking transfer.

115200 baud, 8N1 on USART2 (the NUCLEO's ST-LINK virtual COM port, PA2/PA3, AF7), with APB1 at 45 MHz:

// PA2/PA3: gpioSetAlternateFunction(..., 7U), then gpioInit(..., GPIO_MODE_AF, ...)
static const uint8_t msg[] = "hello\r\n";
if (uartTransmit(USART2, msg, sizeof msg - 1U, 100000U) != DRIVER_STATUS_OK) { ... }
#define BSP_PCLK1_HZ
APB1 (/4): USART2, PWR.
Definition bsp.h:55
@ DRIVER_STATUS_OK
Succeeded.
void rccUsart2ClockEnable(RccRegisters_t *rcc)
Enable USART2's clock.
Definition rcc.c:96
#define RCC
The RCC instance.
Definition rcc_reg.h:61
DRIVER_MUST_CHECK DriverStatus_e uartTransmit(UartRegisters_t *uart, const uint8_t *data, size_t length, uint32_t timeout)
Send a buffer and wait until the line is idle.
Definition uart.c:184
DRIVER_MUST_CHECK DriverStatus_e uartInit(UartRegisters_t *uart, uint32_t pclk_hz, uint32_t baud, uint32_t stop_bits, uint32_t parity_enable, uint32_t parity_select, uint32_t direction)
Configure an instance for asynchronous 8-bit operation and enable it.
Definition uart.c:60
#define USART_CR2_STOP_1
1 stop bit
Definition uart_reg.h:140
#define USART_CR1_RE
Receiver enable.
Definition uart_reg.h:101
#define USART_CR1_TE
Transmitter enable.
Definition uart_reg.h:103
#define USART2
USART2.
Definition uart_reg.h:46
  • Framing: 8 data bits, optional parity, 1 or 2 stop bits. With parity on, uartInit() sets CR1.M, because RM0390 counts parity inside the M frame. Nine-bit data and the smartcard-only 0.5/1.5 stop bits are not offered, since transfers move uint8_t.
  • Timeouts: every wait is a caller-supplied count of SR polls, not a time. One poll costs roughly 10-30 core cycles, so allow at least ms * core_hz / 1000 / 30. 0 polls once (a non-blocking check), and UINT32_MAX waits for practically ever. See Poll-count timeouts.
  • Blocking: transfers busy-wait. Once tasks exist they monopolise the CPU for the transfer; an interrupt- or DMA-driven variant belongs with the kernel.

Definition in file uart.h.