21
21
22
22
#define MODULE_NAME "stm32-uart"
23
23
24
+ #define USART_CR1_FIFOEN (1 << 29)
25
+
24
26
25
27
/*********************************************************************************************************************
26
28
* Uart interface implementation
@@ -292,6 +294,10 @@ stm32_uart_ret_t stm32_uart_init(Stm32Uart *self, uint32_t port) {
292
294
usart_set_stopbits (self -> port , USART_STOPBITS_1 );
293
295
usart_set_parity (self -> port , USART_PARITY_NONE );
294
296
usart_set_flow_control (self -> port , USART_FLOWCONTROL_NONE );
297
+
298
+ /* Enable FIFO mode */
299
+ USART_CR1 (self -> port ) |= USART_CR1_FIFOEN ;
300
+
295
301
usart_enable (self -> port );
296
302
297
303
/* Allocate IPC primitives. */
@@ -305,6 +311,7 @@ stm32_uart_ret_t stm32_uart_init(Stm32Uart *self, uint32_t port) {
305
311
goto err ;
306
312
}
307
313
314
+
308
315
/* Enable RX not empty interrupt to receive data. */
309
316
USART_CR3 (self -> port ) |= USART_CR3_OVRDIS ;
310
317
USART_CR1 (self -> port ) |= USART_CR1_RXNEIE ;
@@ -338,18 +345,18 @@ stm32_uart_ret_t stm32_uart_free(Stm32Uart *self) {
338
345
339
346
340
347
stm32_uart_ret_t stm32_uart_interrupt_handler (Stm32Uart * self ) {
341
- /** @TODO tested on L4 only */
348
+ BaseType_t woken = pdFALSE ;
342
349
343
- if ((USART_ISR (self -> port ) & USART_ISR_TXE )) {
350
+ while ((USART_ISR (self -> port ) & USART_ISR_TXE )) {
344
351
uint8_t b = 0 ;
345
352
size_t r = xStreamBufferReceiveFromISR (self -> txbuf , & b , sizeof (b ), 0 );
346
353
if (r > 0 ) {
347
354
usart_send (self -> port , b );
348
355
}
349
356
if (r == 0 ) {
350
- /* No more bytes to send. Clear and disable the interrupt. */
351
- USART_RQR (self -> port ) |= USART_RQR_TXFRQ ;
357
+ /* No more bytes to send. Disable the interrupt. */
352
358
USART_CR1 (self -> port ) &= ~USART_CR1_TXEIE ;
359
+ break ;
353
360
}
354
361
}
355
362
@@ -362,10 +369,9 @@ stm32_uart_ret_t stm32_uart_interrupt_handler(Stm32Uart *self) {
362
369
USART_ICR (self -> port ) |= USART_ICR_TCCF ;
363
370
}
364
371
365
- if ((USART_ISR (self -> port ) & USART_ISR_RXNE )) {
372
+ while ((USART_ISR (self -> port ) & USART_ISR_RXNE )) {
366
373
uint8_t b = usart_recv (self -> port );
367
374
368
- BaseType_t woken = pdFALSE ;
369
375
/* For any character < 0x20, prepend ESC */
370
376
if (b < 0x20 ) {
371
377
const uint8_t esc = 0x1b ;
@@ -374,13 +380,11 @@ stm32_uart_ret_t stm32_uart_interrupt_handler(Stm32Uart *self) {
374
380
/* Do not check the return value. If there was not enough space to save
375
381
* the received byte, we have nothing else to do. */
376
382
xStreamBufferSendFromISR (self -> rxbuf , & b , sizeof (b ), & woken );
377
- portYIELD_FROM_ISR (woken );
378
383
}
379
384
380
385
if (USART_ISR (self -> port ) & USART_ISR_RTOF ) {
381
386
USART_ICR (self -> port ) |= USART_ICR_RTOCF ;
382
387
383
- BaseType_t woken = pdFALSE ;
384
388
const uint8_t etb = 0x17 ;
385
389
xStreamBufferSendFromISR (self -> rxbuf , & etb , sizeof (etb ), & woken );
386
390
portYIELD_FROM_ISR (woken );
@@ -391,6 +395,7 @@ stm32_uart_ret_t stm32_uart_interrupt_handler(Stm32Uart *self) {
391
395
USART_ICR (self -> port ) |= USART_ICR_ORECF ;
392
396
}
393
397
398
+ portYIELD_FROM_ISR (woken );
394
399
return STM32_UART_RET_OK ;
395
400
}
396
401
0 commit comments