|
STM32 RTOS 0.2.3
Bare-metal preemptive RTOS for STM32F446RE
|
Blocking asynchronous USART/UART transfer. More...
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. | |
Blocking asynchronous USART/UART transfer.
| #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().
| 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.
| uart | Instance (e.g. USART2). |
| pclk_hz | Its bus clock: APB1 for USART2/3 and UART4/5, APB2 for USART1/6. |
| baud | Baud rate, in bit/s. |
| stop_bits | USART_CR2_STOP_1 or USART_CR2_STOP_2. |
| parity_enable | 0 for no parity, or USART_CR1_PCE. |
| parity_select | 0 for even, or USART_CR1_PS for odd; validated even without parity. |
| direction | USART_CR1_TE and/or USART_CR1_RE, or 0. |
| DRIVER_STATUS_OK | Configured and enabled. |
| DRIVER_STATUS_ERR_INVALID_PARAM | A 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().
| void uartDeinit | ( | UartRegisters_t * | uart | ) |
Return CR1, CR2, CR3, BRR and GTPR to their reset values (disabled).
| uart | Instance (e.g. USART2). |
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().
| 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.
| uart | Instance (e.g. USART2); only SR is read, which does not clear TC. |
| timeout | SR polls to allow. |
| DRIVER_STATUS_OK | TC is set. |
| DRIVER_STATUS_ERR_TIMEOUT | TC 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().
| DriverStatus_e uartTransmitByte | ( | UartRegisters_t * | uart, |
| uint8_t | byte, | ||
| uint32_t | timeout ) |
Send one byte once DR is free (SR.TXE).
| uart | Instance (e.g. USART2). |
| byte | Byte to send. |
| timeout | SR polls to allow; one frame time is the longest legitimate wait. |
| DRIVER_STATUS_OK | The byte is in DR. |
| DRIVER_STATUS_ERR_TIMEOUT | TXE 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().
| 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.
| uart | Instance (e.g. USART2). |
| data | Bytes to send; may be NULL only if length is 0. |
| length | Number of bytes. 0 sends nothing and returns OK without waiting. |
| timeout | SR polls to allow per byte, and again for the final TC wait. |
| DRIVER_STATUS_OK | Sent, and the transmitter is idle. |
| DRIVER_STATUS_ERR_INVALID_PARAM | data is NULL with a non-zero length. |
| DRIVER_STATUS_ERR_TIMEOUT | A 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().
| 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).
| uart | Instance (e.g. USART2). |
| byte | Receives the byte, also on HW_FAULT, when it is unreliable. Not NULL. |
| timeout | SR polls to allow; this waits on the sender, so it is usually large. |
| DRIVER_STATUS_OK | A byte with no error was received. |
| DRIVER_STATUS_ERR_INVALID_PARAM | byte is NULL; DR is not read. |
| DRIVER_STATUS_ERR_HW_FAULT | SR reported ORE, NE, FE or PE for this byte. |
| DRIVER_STATUS_ERR_TIMEOUT | RXNE did not set within timeout polls. |
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().
| DriverStatus_e uartReceive | ( | UartRegisters_t * | uart, |
| uint8_t * | data, | ||
| size_t | length, | ||
| uint32_t | timeout ) |
Receive a buffer, one byte at a time.
| uart | Instance (e.g. USART2). |
| data | Destination; may be NULL only if length is 0. |
| length | Number of bytes. |
| timeout | SR polls to allow per byte. |
| DRIVER_STATUS_OK | Every byte received with no error. |
| DRIVER_STATUS_ERR_INVALID_PARAM | data is NULL with a non-zero length. |
| DRIVER_STATUS_ERR_HW_FAULT | A byte had an error; receiving stops there. |
| DRIVER_STATUS_ERR_TIMEOUT | A 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().
|
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.
| pclk_hz | Bus clock, in Hz. |
| baud | Baud rate, in bit/s. |
| brr | Receives the value; untouched on failure. |
| DRIVER_STATUS_OK | Computed. |
| DRIVER_STATUS_ERR_INVALID_PARAM | An 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().
|
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.
| uart | Instance. |
| flag | USART_SR_TXE, USART_SR_TC or USART_SR_RXNE. |
| timeout | Polls to allow; 0 checks once. |
| DRIVER_STATUS_OK | The flag is set. |
| DRIVER_STATUS_ERR_TIMEOUT | It 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().