Procházet zdrojové kódy

修复串口驱动热插拔异常

zhunk před 8 měsíci
rodič
revize
bf63f29d4c

+ 1 - 1
Application_cbd_apm32f1/applications/dtu_uart2.c

@@ -105,7 +105,7 @@ int DTU_uart2_init(void)
     /* step4:打开串口设备。以DMA接收及轮询发送模式打开串口设备 */
     #ifdef RT_USING_SERIAL_V2
     rt_device_close(dtuserial);
-    rt_err_t uart_res = rt_device_open(dtuserial, RT_DEVICE_FLAG_RX_NON_BLOCKING | RT_DEVICE_FLAG_TX_NON_BLOCKING); // 串口设备使用模式为 (发送阻塞 接收非阻塞) 模式
+    rt_err_t uart_res = rt_device_open(dtuserial, RT_DEVICE_FLAG_RX_NON_BLOCKING | RT_DEVICE_FLAG_TX_NON_BLOCKING); // 串口设备使用模式为 (发送非阻塞 接收非阻塞) 模式
     #else
     rt_err_t uart_res = rt_device_open(dtuserial, RT_DEVICE_FLAG_DMA_RX);  //RT_DEVICE_FLAG_DMA_RX
     #endif

+ 83 - 20
Application_cbd_apm32f1/drivers/drv_usart_v2.c

@@ -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)
         {

+ 2 - 2
Application_cbd_apm32f1/packages/ota_downloader-v1.0.0/src/ymodem_ota.c

@@ -44,7 +44,7 @@ static enum rym_code ymodem_on_begin(struct rym_ctx *ctx, rt_uint8_t *buf, rt_si
     file_name = (char *)&buf[0];
     file_size = (char *)&buf[rt_strlen(file_name) + 1];
     update_file_total_size = atol(file_size);
-    rt_kprintf("Ymodem file_size:%d\n", update_file_total_size);
+    //rt_kprintf("Ymodem file_size:%d\n", update_file_total_size);
 
     update_file_cur_size = 0;
 
@@ -61,7 +61,7 @@ static enum rym_code ymodem_on_begin(struct rym_ctx *ctx, rt_uint8_t *buf, rt_si
         return RYM_CODE_CAN;
     }
 
-    LOG_I("Start erase. Size (%d)", update_file_total_size);
+    //LOG_I("Start erase. Size (%d)", update_file_total_size);
 
     /* erase DL section */
     if (fal_partition_erase(dl_part, 0, update_file_total_size) < 0)

+ 3 - 1
Application_cbd_apm32f1/readme.md

@@ -3,7 +3,9 @@
 	1. 屏幕串口驱动使用信号量接收数据,取消使用消息队列
 	2. 解析IMEI时增加判空操作,解决传递过来的空IMEI替换已保存的IMEI,ICCID也一样;
 	3. 工作状态下休眠不关闭屏幕
-	4. 修改串口的打开模式为发送非阻塞,否则断电RTU时会导致串口异常,无法收发数据;
+	4. 修改串口的打开模式为发送非阻塞,否则断电RTU时会导致串口异常,无法收发数据,并且修改putc函数;
+		优化driver_usartv2.c驱动!,解决热插拔串口后串口无法发送数据!!!!
+	5. ymodem调试打印关闭
 # 2026-03-11 更新说明
 	版本:3.3.3
 	1. 关闭屏幕供电会导致rtu12V供电同时关闭,故增加休眠模式:只在上传/拍照时提前通电,结束后延迟掉电

+ 9 - 0
Application_cbd_apm32f1/rt-thread/components/finsh/cmd.c

@@ -49,6 +49,15 @@ static long clear(void)
 }
 MSH_CMD_EXPORT(clear, clear the terminal screen);
 
+
+
+static long reboot(void)
+{
+    rt_hw_cpu_reset();
+    return 0;
+}
+MSH_CMD_EXPORT(reboot, rt_hw_cpu_reset);
+
 extern void rt_show_version(void);
 long version(void)
 {