13#define UART_BRR_MIN (16U)
35 if ((pclk_hz == 0U) || (baud == 0U))
41 uint64_t rounded = ((uint64_t)pclk_hz + ((uint64_t)baud / 2U)) / (uint64_t)baud;
53 *brr = (uint32_t)rounded;
61 uint32_t parity_enable, uint32_t parity_select, uint32_t direction)
77 if ((parity_enable != 0U) && (parity_enable !=
USART_CR1_PCE))
85 if ((parity_select != 0U) && (parity_select !=
USART_CR1_PS))
106 uint32_t frame_length = 0U;
108 if (parity_enable != 0U)
119 (uart->
CR1 & ~cr1_mask) | frame_length | parity_enable | parity_select | direction;
152 uint32_t remaining = timeout;
154 while (((uart->
SR & flag) == 0U) && (remaining > 0U))
159 if ((uart->
SR & flag) == 0U)
189 if ((data == NULL) && (length > 0U))
227 uint32_t sr = uart->
SR;
229 *
byte = (uint8_t)uart->
DR;
244 if ((data == NULL) && (length > 0U))
DriverStatus_e
Outcome of a driver call: a category for control flow, not a diagnosis.
@ DRIVER_STATUS_OK
Succeeded.
@ DRIVER_STATUS_ERR_HW_FAULT
The hardware answered with a fault, such as a UART framing error or a rejected write.
@ DRIVER_STATUS_ERR_INVALID_PARAM
A parameter is outside its valid domain.
@ DRIVER_STATUS_ERR_TIMEOUT
A bounded wait for a hardware flag ran out.
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 uartFlush(const UartRegisters_t *uart, uint32_t timeout)
Wait until the last byte has left the shift register (SR.TC).
static DriverStatus_e uartWaitSrFlag(const UartRegisters_t *uart, uint32_t flag, uint32_t timeout)
Poll an SR flag until it sets or timeout polls pass.
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.
#define UART_BRR_MIN
Smallest valid BRR: USARTDIV below 1, a baud rate above pclk / 16, cannot be generated.
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.
void uartDeinit(UartRegisters_t *uart)
Return CR1, CR2, CR3, BRR and GTPR to their reset values (disabled).
DRIVER_MUST_CHECK DriverStatus_e uartReceiveByte(UartRegisters_t *uart, uint8_t *byte, uint32_t timeout)
Receive one byte once SR.RXNE sets.
static DriverStatus_e uartComputeBrr(uint32_t pclk_hz, uint32_t baud, uint32_t *brr)
Compute BRR for 16x oversampling: round(pclk_hz / baud).
DRIVER_MUST_CHECK DriverStatus_e uartTransmitByte(UartRegisters_t *uart, uint8_t byte, uint32_t timeout)
Send one byte once DR is free (SR.TXE).
#define USART_CR2_STOP_2
2 stop bits
#define USART_BRR_DIV_FRACTION_Msk
<span class="regaccess">rw</span> USARTDIV fraction, in sixteenths.
#define USART_CR2_STOP_1
1 stop bit
#define USART_CR1_UE
USART enable.
#define USART_CR1_PS
Parity: 0 even, 1 odd.
#define USART_SR_ORE
<span class="regaccess">ro</span> Overrun error.
#define USART_CR2_STOP_Pos
Bits 13:12.
#define USART_CR1_PCE
Parity control enable.
#define USART_SR_TXE
<span class="regaccess">ro</span> DR can take the next byte; a DR write clears it.
#define USART_SR_TC
<span class="regaccess">rc_w0</span> Transmission complete: the shift register is empty.
#define USART_SR_NE
<span class="regaccess">ro</span> Noise detected (RM0390 names it NF).
#define USART_CR1_M
Word length: 0 = 8 bits, 1 = 9 bits on the wire (parity included).
#define USART_SR_FE
<span class="regaccess">ro</span> Framing error.
#define USART_SR_RXNE
<span class="regaccess">rc_w0</span> Receive data ready; a DR read also clears it.
#define USART_SR_PE
<span class="regaccess">ro</span> Parity error.
#define USART_CR1_RE
Receiver enable.
#define USART_CR1_TE
Transmitter enable.
#define USART_BRR_DIV_MANTISSA_Msk
<span class="regaccess">rw</span> USARTDIV mantissa.
#define USART_CR1_OVER8
Oversampling: 0 = 16x, 1 = 8x.
#define USART_CR2_STOP_Msk
<span class="regaccess">rw</span> Stop bits; values below.
USART/UART register block.
volatile uint32_t DR
0x04: data
volatile uint32_t CR1
0x0C: control 1
volatile uint32_t BRR
0x08: baud rate
volatile uint32_t CR3
0x14: control 3
volatile uint32_t GTPR
0x18: guard time and prescaler (USART only)
volatile uint32_t SR
0x00: status
volatile uint32_t CR2
0x10: control 2
UART driver: asynchronous configuration and blocking transfer.