|
|
@@ -291,37 +291,86 @@ static rt_err_t stm32_control(struct rt_serial_device *serial, int cmd, void *ar
|
|
|
}
|
|
|
|
|
|
|
|
|
+//static int stm32_putc(struct rt_serial_device *serial, char c)
|
|
|
+//{
|
|
|
+// struct stm32_uart *uart;
|
|
|
+// rt_tick_t start_tick;
|
|
|
+//
|
|
|
+// RT_ASSERT(serial != RT_NULL);
|
|
|
+//
|
|
|
+// uart = rt_container_of(serial, struct stm32_uart, serial);
|
|
|
+//
|
|
|
+// start_tick = rt_tick_get();
|
|
|
+//
|
|
|
+// /* 等待发送完成 TC */
|
|
|
+// while (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_TC) == RESET)
|
|
|
+// {
|
|
|
+// /* 5ms超时 */
|
|
|
+// if (rt_tick_get() - start_tick > rt_tick_from_millisecond(1000))
|
|
|
+// {
|
|
|
+// LOG_E("UART TX timeout, reset uart");
|
|
|
+//
|
|
|
+// /* 清标志 */
|
|
|
+// __HAL_UART_CLEAR_FLAG(&(uart->handle), UART_FLAG_TC);
|
|
|
+//
|
|
|
+//// /* 复位 UART */
|
|
|
+//// HAL_UART_DeInit(&uart->handle);
|
|
|
+//// HAL_UART_Init(&uart->handle);
|
|
|
+//
|
|
|
+// return 0;
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_TC);
|
|
|
+ //UART_SET_TDR(&uart->handle, c);
|
|
|
+//
|
|
|
+// return 1;
|
|
|
+//}
|
|
|
+static void uart_tx_recover(struct stm32_uart *uart)
|
|
|
+{
|
|
|
+ struct rt_serial_tx_fifo *tx_fifo;
|
|
|
+
|
|
|
+ tx_fifo = (struct rt_serial_tx_fifo *)uart->serial.serial_tx;
|
|
|
+
|
|
|
+ if (tx_fifo == RT_NULL)
|
|
|
+ return;
|
|
|
+
|
|
|
+ /* 如果发送buffer还有数据 */
|
|
|
+ if (rt_ringbuffer_data_len(&tx_fifo->rb) > 0)
|
|
|
+ {
|
|
|
+ /* TXE中断未开启 */
|
|
|
+ if (__HAL_UART_GET_IT_SOURCE(&uart->handle, UART_IT_TXE) == RESET)
|
|
|
+ {
|
|
|
+ LOG_W("UART TXE lost, recover!");
|
|
|
+
|
|
|
+ __HAL_UART_ENABLE_IT(&uart->handle, UART_IT_TXE);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
static int stm32_putc(struct rt_serial_device *serial, char c)
|
|
|
{
|
|
|
struct stm32_uart *uart;
|
|
|
- rt_tick_t start_tick;
|
|
|
-
|
|
|
- RT_ASSERT(serial != RT_NULL);
|
|
|
+ rt_tick_t start;
|
|
|
|
|
|
uart = rt_container_of(serial, struct stm32_uart, serial);
|
|
|
|
|
|
- start_tick = rt_tick_get();
|
|
|
+ start = rt_tick_get();
|
|
|
|
|
|
- /* 等待发送完成 TC */
|
|
|
- while (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_TC) == RESET)
|
|
|
+ while (__HAL_UART_GET_FLAG(&uart->handle, UART_FLAG_TXE) == RESET)
|
|
|
{
|
|
|
- /* 5ms超时 */
|
|
|
- if (rt_tick_get() - start_tick > rt_tick_from_millisecond(1000))
|
|
|
+ if (rt_tick_get() - start > rt_tick_from_millisecond(100))
|
|
|
{
|
|
|
- LOG_E("UART TX timeout, reset uart");
|
|
|
-
|
|
|
- /* 清标志 */
|
|
|
- __HAL_UART_CLEAR_FLAG(&(uart->handle), UART_FLAG_TC);
|
|
|
+ LOG_E("UART TX stuck, recover");
|
|
|
|
|
|
-// /* 复位 UART */
|
|
|
-// HAL_UART_DeInit(&uart->handle);
|
|
|
-// HAL_UART_Init(&uart->handle);
|
|
|
+ HAL_UART_DeInit(&uart->handle);
|
|
|
+ HAL_UART_Init(&uart->handle);
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_TC);
|
|
|
UART_SET_TDR(&uart->handle, c);
|
|
|
|
|
|
return 1;
|
|
|
@@ -405,7 +454,7 @@ static rt_size_t stm32_transmit(struct rt_serial_device *serial,
|
|
|
}
|
|
|
|
|
|
stm32_control(serial, RT_DEVICE_CTRL_SET_INT, (void *)tx_flag);
|
|
|
-
|
|
|
+ uart_tx_recover(uart);
|
|
|
return size;
|
|
|
}
|
|
|
|
|
|
@@ -467,6 +516,20 @@ static void uart_isr(struct rt_serial_device *serial)
|
|
|
|
|
|
RT_ASSERT(serial != RT_NULL);
|
|
|
uart = rt_container_of(serial, struct stm32_uart, serial);
|
|
|
+ if (__HAL_UART_GET_FLAG(&uart->handle, UART_FLAG_ORE))
|
|
|
+ {
|
|
|
+ __HAL_UART_CLEAR_OREFLAG(&uart->handle);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (__HAL_UART_GET_FLAG(&uart->handle, UART_FLAG_FE))
|
|
|
+ {
|
|
|
+ __HAL_UART_CLEAR_FEFLAG(&uart->handle);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (__HAL_UART_GET_FLAG(&uart->handle, UART_FLAG_NE))
|
|
|
+ {
|
|
|
+ __HAL_UART_CLEAR_NEFLAG(&uart->handle);
|
|
|
+ }
|
|
|
/* If the Read data register is not empty and the RXNE interrupt is enabled (RDR) */
|
|
|
if ((__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_RXNE) != RESET) &&
|
|
|
(__HAL_UART_GET_IT_SOURCE(&(uart->handle), UART_IT_RXNE) != RESET))
|
|
|
@@ -513,7 +576,7 @@ static void uart_isr(struct rt_serial_device *serial)
|
|
|
rt_hw_serial_isr(serial, RT_SERIAL_EVENT_TX_DONE);
|
|
|
}
|
|
|
/* Clear Transmission complete interrupt flag ( ISR Register ) */
|
|
|
- UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_TC);
|
|
|
+ //UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_TC);
|
|
|
}
|
|
|
|
|
|
#ifdef RT_SERIAL_USING_DMA
|
|
|
@@ -557,11 +620,11 @@ static void uart_isr(struct rt_serial_device *serial)
|
|
|
}
|
|
|
if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_TXE) != RESET)
|
|
|
{
|
|
|
- UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_TXE);
|
|
|
+ //UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_TXE);
|
|
|
}
|
|
|
if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_TC) != RESET)
|
|
|
{
|
|
|
- UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_TC);
|
|
|
+ //UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_TC);
|
|
|
}
|
|
|
if (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_RXNE) != RESET)
|
|
|
{
|