STM32 RTOS 0.2.3
Bare-metal preemptive RTOS for STM32F446RE
Loading...
Searching...
No Matches
UART

Blocking asynchronous USART/UART transfer. More...

Collaboration diagram for UART:

Macros

#define UART_BRR_MIN   (16U)
 Smallest valid BRR: USARTDIV below 1, a baud rate above pclk / 16, cannot be generated.

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.
static DriverStatus_e uartComputeBrr (uint32_t pclk_hz, uint32_t baud, uint32_t *brr)
 Compute BRR for 16x oversampling: round(pclk_hz / baud).
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.

Detailed Description

Blocking asynchronous USART/UART transfer.

Macro Definition Documentation

◆ UART_BRR_MIN

#define UART_BRR_MIN   (16U)

Smallest valid BRR: USARTDIV below 1, a baud rate above pclk / 16, cannot be generated.

Definition at line 13 of file uart.c.

Referenced by uartComputeBrr().

Function Documentation

◆ uartInit()

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.

Clears UE, writes BRR (16x oversampling), CR2.STOP and CR1's M, OVER8, PCE, PS, TE and RE, then sets UE last. Other CR1 bits and all of CR3 are kept, so call uartDeinit() first when reusing an instance that had interrupts or DMA enabled.

Parameters
uartInstance (e.g. USART2).
pclk_hzIts bus clock: APB1 for USART2/3 and UART4/5, APB2 for USART1/6.
baudBaud rate, in bit/s.
stop_bitsUSART_CR2_STOP_1 or USART_CR2_STOP_2.
parity_enable0 for no parity, or USART_CR1_PCE.
parity_select0 for even, or USART_CR1_PS for odd; validated even without parity.
directionUSART_CR1_TE and/or USART_CR1_RE, or 0.
Precondition
The instance's clock is on, and its pins are set to the right alternate function.
Return values
DRIVER_STATUS_OKConfigured and enabled.
DRIVER_STATUS_ERR_INVALID_PARAMA parameter is not one of its values, pclk_hz or baud is 0, or the divisor does not fit BRR (baud is above pclk_hz / 16 or too low).

Definition at line 60 of file uart.c.

References UartRegisters_t::BRR, UartRegisters_t::CR1, UartRegisters_t::CR2, DRIVER_STATUS_ERR_INVALID_PARAM, DRIVER_STATUS_OK, uartComputeBrr(), uartInit(), USART_CR1_M, USART_CR1_OVER8, USART_CR1_PCE, USART_CR1_PS, USART_CR1_RE, USART_CR1_TE, USART_CR1_UE, USART_CR2_STOP_1, USART_CR2_STOP_2, USART_CR2_STOP_Msk, and USART_CR2_STOP_Pos.

Referenced by uartInit().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ uartDeinit()

void uartDeinit ( UartRegisters_t * uart)

Return CR1, CR2, CR3, BRR and GTPR to their reset values (disabled).

Parameters
uartInstance (e.g. USART2).
Precondition
Any transmission has finished (uartFlush()); disabling mid-frame cuts it short.

Definition at line 128 of file uart.c.

References UartRegisters_t::BRR, UartRegisters_t::CR1, UartRegisters_t::CR2, UartRegisters_t::CR3, UartRegisters_t::GTPR, and uartDeinit().

Referenced by uartDeinit().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ uartFlush()

DriverStatus_e uartFlush ( const UartRegisters_t * uart,
uint32_t timeout )

Wait until the last byte has left the shift register (SR.TC).

TXE only means DR can take another byte. Call this before deinitialising, gating the clock or changing the baud rate after uartTransmitByte(); uartTransmit() already does.

Parameters
uartInstance (e.g. USART2); only SR is read, which does not clear TC.
timeoutSR polls to allow.
Precondition
The transmitter is enabled.
Return values
DRIVER_STATUS_OKTC is set.
DRIVER_STATUS_ERR_TIMEOUTTC did not set within timeout polls.

Definition at line 167 of file uart.c.

References uartFlush(), uartWaitSrFlag(), and USART_SR_TC.

Referenced by uartFlush(), and uartTransmit().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ uartTransmitByte()

DriverStatus_e uartTransmitByte ( UartRegisters_t * uart,
uint8_t byte,
uint32_t timeout )

Send one byte once DR is free (SR.TXE).

Parameters
uartInstance (e.g. USART2).
byteByte to send.
timeoutSR polls to allow; one frame time is the longest legitimate wait.
Precondition
The transmitter is enabled.
Return values
DRIVER_STATUS_OKThe byte is in DR.
DRIVER_STATUS_ERR_TIMEOUTTXE did not set within timeout polls.

Definition at line 172 of file uart.c.

References UartRegisters_t::DR, DRIVER_STATUS_OK, uartTransmitByte(), uartWaitSrFlag(), and USART_SR_TXE.

Referenced by uartTransmit(), and uartTransmitByte().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ uartTransmit()

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.

Ends with uartFlush(), so the instance can be reconfigured or deinitialised straight after.

Parameters
uartInstance (e.g. USART2).
dataBytes to send; may be NULL only if length is 0.
lengthNumber of bytes. 0 sends nothing and returns OK without waiting.
timeoutSR polls to allow per byte, and again for the final TC wait.
Return values
DRIVER_STATUS_OKSent, and the transmitter is idle.
DRIVER_STATUS_ERR_INVALID_PARAMdata is NULL with a non-zero length.
DRIVER_STATUS_ERR_TIMEOUTA byte or the final TC wait timed out; sending stops there.

Definition at line 184 of file uart.c.

References DRIVER_STATUS_ERR_INVALID_PARAM, DRIVER_STATUS_OK, uartFlush(), uartTransmit(), and uartTransmitByte().

Referenced by uartTransmit().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ uartReceiveByte()

DriverStatus_e uartReceiveByte ( UartRegisters_t * uart,
uint8_t * byte,
uint32_t timeout )

Receive one byte once SR.RXNE sets.

DR is always read once RXNE sets, even on an error, because the SR-then-DR read is what clears ORE/NE/FE/PE. That side effect is why uart is not const (see const register parameters).

Parameters
uartInstance (e.g. USART2).
byteReceives the byte, also on HW_FAULT, when it is unreliable. Not NULL.
timeoutSR polls to allow; this waits on the sender, so it is usually large.
Precondition
The receiver is enabled.
Return values
DRIVER_STATUS_OKA byte with no error was received.
DRIVER_STATUS_ERR_INVALID_PARAMbyte is NULL; DR is not read.
DRIVER_STATUS_ERR_HW_FAULTSR reported ORE, NE, FE or PE for this byte.
DRIVER_STATUS_ERR_TIMEOUTRXNE did not set within timeout polls.
Warning
Single owner only: two contexts reading the same instance race on SR and DR.

Definition at line 212 of file uart.c.

References UartRegisters_t::DR, DRIVER_STATUS_ERR_HW_FAULT, DRIVER_STATUS_ERR_INVALID_PARAM, DRIVER_STATUS_OK, UartRegisters_t::SR, uartReceiveByte(), uartWaitSrFlag(), USART_SR_FE, USART_SR_NE, USART_SR_ORE, USART_SR_PE, and USART_SR_RXNE.

Referenced by uartReceive(), and uartReceiveByte().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ uartReceive()

DriverStatus_e uartReceive ( UartRegisters_t * uart,
uint8_t * data,
size_t length,
uint32_t timeout )

Receive a buffer, one byte at a time.

Parameters
uartInstance (e.g. USART2).
dataDestination; may be NULL only if length is 0.
lengthNumber of bytes.
timeoutSR polls to allow per byte.
Return values
DRIVER_STATUS_OKEvery byte received with no error.
DRIVER_STATUS_ERR_INVALID_PARAMdata is NULL with a non-zero length.
DRIVER_STATUS_ERR_HW_FAULTA byte had an error; receiving stops there.
DRIVER_STATUS_ERR_TIMEOUTA byte timed out; receiving stops there.

Definition at line 240 of file uart.c.

References DRIVER_STATUS_ERR_INVALID_PARAM, DRIVER_STATUS_OK, uartReceive(), and uartReceiveByte().

Referenced by uartReceive().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ uartComputeBrr()

DriverStatus_e uartComputeBrr ( uint32_t pclk_hz,
uint32_t baud,
uint32_t * brr )
static

Compute BRR for 16x oversampling: round(pclk_hz / baud).

BRR's mantissa (bits 15:4) and sixteenths (bits 3:0) together are just USARTDIV x 16 = pclk_hz / baud, so the rounded quotient is the register value. Rounding once at the end matters: 42 MHz at 115200 is 364.58, which rounds to 365 where a truncated intermediate gives 364. The sum is done in 64 bits because pclk_hz is unbounded.

Parameters
pclk_hzBus clock, in Hz.
baudBaud rate, in bit/s.
brrReceives the value; untouched on failure.
Return values
DRIVER_STATUS_OKComputed.
DRIVER_STATUS_ERR_INVALID_PARAMAn input is 0, or the result is below UART_BRR_MIN or above 16 bits.

Definition at line 31 of file uart.c.

References DRIVER_STATUS_ERR_INVALID_PARAM, DRIVER_STATUS_OK, UART_BRR_MIN, uartComputeBrr(), USART_BRR_DIV_FRACTION_Msk, and USART_BRR_DIV_MANTISSA_Msk.

Referenced by uartComputeBrr(), and uartInit().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ uartWaitSrFlag()

DriverStatus_e uartWaitSrFlag ( const UartRegisters_t * uart,
uint32_t flag,
uint32_t timeout )
static

Poll an SR flag until it sets or timeout polls pass.

The flag is re-read after the loop, so one that sets on the last poll counts as success.

Parameters
uartInstance.
flagUSART_SR_TXE, USART_SR_TC or USART_SR_RXNE.
timeoutPolls to allow; 0 checks once.
Return values
DRIVER_STATUS_OKThe flag is set.
DRIVER_STATUS_ERR_TIMEOUTIt did not set in time.

Definition at line 149 of file uart.c.

References DRIVER_STATUS_ERR_TIMEOUT, DRIVER_STATUS_OK, UartRegisters_t::SR, and uartWaitSrFlag().

Referenced by uartFlush(), uartReceiveByte(), uartTransmitByte(), and uartWaitSrFlag().

Here is the call graph for this function:
Here is the caller graph for this function: