Skip to content

Commit b95a88a

Browse files
committed
[bsp][stm32][drv_usart_v2] transmit返回错误值,RXNE增加超时逻辑
1 parent 7be8b69 commit b95a88a

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

bsp/stm32/libraries/HAL_Drivers/drivers/drv_usart_v2.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -389,12 +389,18 @@ static rt_ssize_t stm32_transmit(struct rt_serial_device *serial,
389389
if (uart->uart_dma_flag & RT_DEVICE_FLAG_DMA_TX)
390390
{
391391
if (HAL_UART_Transmit_DMA(&uart->handle, buf, size) != HAL_OK)
392-
return 0;
392+
{
393+
return -RT_EIO;
394+
}
395+
393396
return size;
394397
}
395398

396399
/* NOLINTNEXTLINE(performance-no-int-to-ptr) */
397-
stm32_control(serial, RT_DEVICE_CTRL_SET_INT, (void *)tx_flag);
400+
if (stm32_control(serial, RT_DEVICE_CTRL_SET_INT, (void *)tx_flag) != RT_EOK)
401+
{
402+
return -RT_EIO;
403+
}
398404

399405
return size;
400406
}
@@ -441,11 +447,15 @@ static void uart_isr(struct rt_serial_device *serial)
441447
/* If the Read data register is not empty and the RXNE interrupt is enabled (RDR) */
442448
if ((__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_RXNE)) && (__HAL_UART_GET_IT_SOURCE(&(uart->handle), UART_IT_RXNE)))
443449
{
450+
rt_uint8_t chr;
451+
rt_uint32_t rx_drain_limit = 1024;
452+
rt_uint32_t mask = stm32_uart_get_mask(uart->handle.Init.WordLength, uart->handle.Init.Parity);
444453
do {
445-
char chr = UART_GET_RDR(&uart->handle, stm32_uart_get_mask(uart->handle.Init.WordLength, uart->handle.Init.Parity));
454+
chr = UART_GET_RDR(&uart->handle, mask);
446455
rt_hw_serial_control_isr(serial, RT_HW_SERIAL_CTRL_PUTC, (void *)&chr);
447456
rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
448-
} while (__HAL_UART_GET_FLAG(&uart->handle, UART_FLAG_RXNE));
457+
rx_drain_limit--;
458+
} while (__HAL_UART_GET_FLAG(&uart->handle, UART_FLAG_RXNE) && rx_drain_limit > 0);
449459
}
450460
/* If the Transmit data register is empty and the TXE interrupt enable is enabled (TDR) */
451461
if ((__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_TXE)) && (__HAL_UART_GET_IT_SOURCE(&(uart->handle), UART_IT_TXE)))

0 commit comments

Comments
 (0)