STM32 RTOS 0.2.3
Bare-metal preemptive RTOS for STM32F446RE
Loading...
Searching...
No Matches
uart.c
Go to the documentation of this file.
1
5#include "uart.h"
6
11
13#define UART_BRR_MIN (16U)
14
31static DriverStatus_e uartComputeBrr(uint32_t pclk_hz, uint32_t baud, uint32_t *brr)
32{
34
35 if ((pclk_hz == 0U) || (baud == 0U))
36 {
38 }
39 else
40 {
41 uint64_t rounded = ((uint64_t)pclk_hz + ((uint64_t)baud / 2U)) / (uint64_t)baud;
42
43 if (rounded < UART_BRR_MIN)
44 {
46 }
48 {
50 }
51 else
52 {
53 *brr = (uint32_t)rounded;
54 }
55 }
56
57 return status;
58}
59
60DriverStatus_e uartInit(UartRegisters_t *uart, uint32_t pclk_hz, uint32_t baud, uint32_t stop_bits,
61 uint32_t parity_enable, uint32_t parity_select, uint32_t direction)
62{
63 uint32_t brr = 0U;
64 DriverStatus_e status = uartComputeBrr(pclk_hz, baud, &brr);
65
66 if (status == DRIVER_STATUS_OK)
67 {
68 /* 0.5 and 1.5 stop bits are smartcard-only; not offered. */
69 if ((stop_bits != USART_CR2_STOP_1) && (stop_bits != USART_CR2_STOP_2))
70 {
72 }
73 }
74
75 if (status == DRIVER_STATUS_OK)
76 {
77 if ((parity_enable != 0U) && (parity_enable != USART_CR1_PCE))
78 {
80 }
81 }
82
83 if (status == DRIVER_STATUS_OK)
84 {
85 if ((parity_select != 0U) && (parity_select != USART_CR1_PS))
86 {
88 }
89 }
90
91 if (status == DRIVER_STATUS_OK)
92 {
93 if ((direction & ~(USART_CR1_TE | USART_CR1_RE)) != 0U)
94 {
96 }
97 }
98
99 if (status == DRIVER_STATUS_OK)
100 {
101 /* OVER8 is cleared because BRR assumes 16x oversampling. M is set with
102 * parity: the parity bit counts inside the M frame, so M clear would
103 * leave 7 data bits and overwrite bit 7 with parity. */
104 uint32_t cr1_mask = USART_CR1_M | USART_CR1_OVER8 | USART_CR1_PCE | USART_CR1_PS
106 uint32_t frame_length = 0U;
107
108 if (parity_enable != 0U)
109 {
110 frame_length = USART_CR1_M;
111 }
112
113 /* Disable first so a reconfiguration never changes BRR mid-frame. */
114 uart->CR1 &= ~USART_CR1_UE;
115
116 uart->BRR = brr;
117 uart->CR2 = (uart->CR2 & ~USART_CR2_STOP_Msk) | (stop_bits << USART_CR2_STOP_Pos);
118 uart->CR1 =
119 (uart->CR1 & ~cr1_mask) | frame_length | parity_enable | parity_select | direction;
120
121 /* Enable last, once every field is set. */
122 uart->CR1 |= USART_CR1_UE;
123 }
124
125 return status;
126}
127
129{
130 uart->CR1 = 0U;
131 uart->CR2 = 0U;
132 uart->CR3 = 0U;
133 uart->BRR = 0U;
134 uart->GTPR = 0U;
135}
136
149static DriverStatus_e uartWaitSrFlag(const UartRegisters_t *uart, uint32_t flag, uint32_t timeout)
150{
152 uint32_t remaining = timeout;
153
154 while (((uart->SR & flag) == 0U) && (remaining > 0U))
155 {
156 remaining--;
157 }
158
159 if ((uart->SR & flag) == 0U)
160 {
162 }
163
164 return status;
165}
166
167DriverStatus_e uartFlush(const UartRegisters_t *uart, uint32_t timeout)
168{
169 return uartWaitSrFlag(uart, USART_SR_TC, timeout);
170}
171
172DriverStatus_e uartTransmitByte(UartRegisters_t *uart, uint8_t byte, uint32_t timeout)
173{
174 DriverStatus_e status = uartWaitSrFlag(uart, USART_SR_TXE, timeout);
175
176 if (status == DRIVER_STATUS_OK)
177 {
178 uart->DR = byte;
179 }
180
181 return status;
182}
183
184DriverStatus_e uartTransmit(UartRegisters_t *uart, const uint8_t *data, size_t length,
185 uint32_t timeout)
186{
188
189 if ((data == NULL) && (length > 0U))
190 {
192 }
193
194 for (size_t i = 0U; (i < length) && (status == DRIVER_STATUS_OK); i++)
195 {
196 status = uartTransmitByte(uart, data[i], timeout);
197 }
198
199 /* Wait for TC so the line is idle. Skipped for length 0: nothing was sent. */
200 if ((status == DRIVER_STATUS_OK) && (length > 0U))
201 {
202 status = uartFlush(uart, timeout);
203 }
204
205 return status;
206}
207
208/* cppcheck-suppress constParameterPointer
209 * Nothing is written through `uart`, but reading DR clears RXNE and the error
210 * flags, so the call changes the peripheral: see docs/DESIGN.md,
211 * "const register parameters". */
212DriverStatus_e uartReceiveByte(UartRegisters_t *uart, uint8_t *byte, uint32_t timeout)
213{
215
216 if (byte == NULL)
217 {
219 }
220 else
221 {
222 status = uartWaitSrFlag(uart, USART_SR_RXNE, timeout);
223 }
224
225 if (status == DRIVER_STATUS_OK)
226 {
227 uint32_t sr = uart->SR;
228
229 *byte = (uint8_t)uart->DR;
230
231 if ((sr & (USART_SR_ORE | USART_SR_NE | USART_SR_FE | USART_SR_PE)) != 0U)
232 {
234 }
235 }
236
237 return status;
238}
239
240DriverStatus_e uartReceive(UartRegisters_t *uart, uint8_t *data, size_t length, uint32_t timeout)
241{
243
244 if ((data == NULL) && (length > 0U))
245 {
247 }
248
249 for (size_t i = 0U; (i < length) && (status == DRIVER_STATUS_OK); i++)
250 {
251 status = uartReceiveByte(uart, &data[i], timeout);
252 }
253
254 return status;
255}
256
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.
Definition uart.c:184
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).
Definition uart.c:167
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.
Definition uart.c:149
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 UART_BRR_MIN
Smallest valid BRR: USARTDIV below 1, a baud rate above pclk / 16, cannot be generated.
Definition uart.c:13
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.
Definition uart.c:240
void uartDeinit(UartRegisters_t *uart)
Return CR1, CR2, CR3, BRR and GTPR to their reset values (disabled).
Definition uart.c:128
DRIVER_MUST_CHECK DriverStatus_e uartReceiveByte(UartRegisters_t *uart, uint8_t *byte, uint32_t timeout)
Receive one byte once SR.RXNE sets.
Definition uart.c:212
static DriverStatus_e uartComputeBrr(uint32_t pclk_hz, uint32_t baud, uint32_t *brr)
Compute BRR for 16x oversampling: round(pclk_hz / baud).
Definition uart.c:31
DRIVER_MUST_CHECK DriverStatus_e uartTransmitByte(UartRegisters_t *uart, uint8_t byte, uint32_t timeout)
Send one byte once DR is free (SR.TXE).
Definition uart.c:172
#define USART_CR2_STOP_2
2 stop bits
Definition uart_reg.h:142
#define USART_BRR_DIV_FRACTION_Msk
<span class="regaccess">rw</span> USARTDIV fraction, in sixteenths.
Definition uart_reg.h:87
#define USART_CR2_STOP_1
1 stop bit
Definition uart_reg.h:140
#define USART_CR1_UE
USART enable.
Definition uart_reg.h:124
#define USART_CR1_PS
Parity: 0 even, 1 odd.
Definition uart_reg.h:115
#define USART_SR_ORE
<span class="regaccess">ro</span> Overrun error.
Definition uart_reg.h:67
#define USART_CR2_STOP_Pos
Bits 13:12.
Definition uart_reg.h:131
#define USART_CR1_PCE
Parity control enable.
Definition uart_reg.h:117
#define USART_SR_TXE
<span class="regaccess">ro</span> DR can take the next byte; a DR write clears it.
Definition uart_reg.h:79
#define USART_SR_TC
<span class="regaccess">rc_w0</span> Transmission complete: the shift register is empty.
Definition uart_reg.h:76
#define USART_SR_NE
<span class="regaccess">ro</span> Noise detected (RM0390 names it NF).
Definition uart_reg.h:64
#define USART_CR1_M
Word length: 0 = 8 bits, 1 = 9 bits on the wire (parity included).
Definition uart_reg.h:122
#define USART_SR_FE
<span class="regaccess">ro</span> Framing error.
Definition uart_reg.h:61
#define USART_SR_RXNE
<span class="regaccess">rc_w0</span> Receive data ready; a DR read also clears it.
Definition uart_reg.h:73
#define USART_SR_PE
<span class="regaccess">ro</span> Parity error.
Definition uart_reg.h:58
#define USART_CR1_RE
Receiver enable.
Definition uart_reg.h:101
#define USART_CR1_TE
Transmitter enable.
Definition uart_reg.h:103
#define USART_BRR_DIV_MANTISSA_Msk
<span class="regaccess">rw</span> USARTDIV mantissa.
Definition uart_reg.h:90
#define USART_CR1_OVER8
Oversampling: 0 = 16x, 1 = 8x.
Definition uart_reg.h:126
#define USART_CR2_STOP_Msk
<span class="regaccess">rw</span> Stop bits; values below.
Definition uart_reg.h:133
USART/UART register block.
Definition uart_reg.h:33
volatile uint32_t DR
0x04: data
Definition uart_reg.h:35
volatile uint32_t CR1
0x0C: control 1
Definition uart_reg.h:37
volatile uint32_t BRR
0x08: baud rate
Definition uart_reg.h:36
volatile uint32_t CR3
0x14: control 3
Definition uart_reg.h:39
volatile uint32_t GTPR
0x18: guard time and prescaler (USART only)
Definition uart_reg.h:40
volatile uint32_t SR
0x00: status
Definition uart_reg.h:34
volatile uint32_t CR2
0x10: control 2
Definition uart_reg.h:38
UART driver: asynchronous configuration and blocking transfer.