修复ppp中PPP_DO_SERIAL_TEST的用法 2474/1
author孙春阳 <snickersun@163.com>
Fri, 10 Oct 2025 03:15:34 +0000 (11:15 +0800)
committer孙春阳 <snickersun@163.com>
Fri, 10 Oct 2025 03:15:34 +0000 (11:15 +0800)
code/sdk/zhihui/ppp/src/ppp_api.c
code/sdk/zhihui/ppp/src/ppp_at_handler.c
code/sdk/zhihui/ppp/src/ppp_interrupt.c
code/sdk/zhihui/ppp/src/ppp_lwip_thread.c
code/sdk/zhihui/ppp/src/ppp_power_ctl.c

index abfbe7a..8928442 100644 (file)
@@ -143,14 +143,14 @@ errcode_t zhihui_ppp_init(const ppp_config_t *config)
     memcpy_s(&g_ppp_config, sizeof(ppp_config_t), config, sizeof(ppp_config_t));
 
     // PPP_DO_SERIAL_TEST 不进行电源初始化, 用于调试串口读取能力测试。
-    #if PPP_DO_SERIAL_TEST
+#if defined(PPP_DO_SERIAL_TEST) && (PPP_DO_SERIAL_TEST == 1)
+    PPP_DEBUG_PRINT("PPP_DO_SERIAL_TEST is defined, skip power init\n");
+#else
     ppp_api_power_init();
-    #endif
+#endif
 
 #define PPP_SERIAL_INIT 1 //外接串口调试模组时,串口要设置input防止干扰。
 #if PPP_SERIAL_INIT
-    
-
     // 3. UART初始化
     PPP_DEBUG_PRINT("Step 3: Initializing UART for AT mode baudrate: %d\n", g_ppp_config.baudrate);
     ppp_serial_config_t serial_config = {
@@ -171,8 +171,17 @@ errcode_t zhihui_ppp_init(const ppp_config_t *config)
     }
 
 
-    #if PPP_DO_SERIAL_TEST
-
+#if defined(PPP_DO_SERIAL_TEST) && (PPP_DO_SERIAL_TEST == 1)
+    PPP_DEBUG_PRINT("PPP_DO_SERIAL_TEST is defined, skip switch to AT mode\n");
+    ret = ppp_serial_switch_to_ppp_mode();
+    if (ret != PPP_ERR_OK) {
+        PPP_DEBUG_PRINT("Failed to switch to PPP mode: %d\n", ret);
+        ppp_serial_deinit();
+        goto init_failed;
+    } else {
+        PPP_DEBUG_PRINT("Successfully switched to PPP mode\n");
+    }
+#else
     // 初始化AT命令处理器
     ret = ppp_at_handler_init(SIO_UART_BUS, g_ppp_config.baudrate, true);
     if (ret != PPP_ERR_OK) {
@@ -190,16 +199,6 @@ errcode_t zhihui_ppp_init(const ppp_config_t *config)
     } else {
         PPP_DEBUG_PRINT("Successfully switched to AT mode\n");
     }
-#else
-    PPP_DEBUG_PRINT("PPP_DO_SERIAL_TEST is defined, skip switch to AT mode\n");
-    ret = ppp_serial_switch_to_ppp_mode();
-    if (ret != PPP_ERR_OK) {
-        PPP_DEBUG_PRINT("Failed to switch to PPP mode: %d\n", ret);
-        ppp_serial_deinit();
-        goto init_failed;
-    } else {
-        PPP_DEBUG_PRINT("Successfully switched to PPP mode\n");
-    }
 #endif
 
 #else
@@ -213,7 +212,28 @@ errcode_t zhihui_ppp_init(const ppp_config_t *config)
     // 打开4G电源
     ppp_api_power_control(true);
 
-#if PPP_DO_SERIAL_TEST
+#if defined(PPP_DO_SERIAL_TEST) && (PPP_DO_SERIAL_TEST == 1)
+    // 9. 启动PPP数据接收处理(根据配置选择中断或轮询模式)
+    #if PPP_USE_INTERRUPT_MODE
+    // 中断模式:注册UART中断回调(UART总线号从ppp_serial_get_uart_bus()获取)
+    PPP_DEBUG_PRINT("Enabling PPP interrupt mode...\n");
+    ret = ppp_interrupt_enable();
+    if (ret != PPP_ERR_OK) {
+        PPP_DEBUG_PRINT("Failed to enable PPP interrupt: %d\n", ret);
+        goto init_failed;
+    }
+    PPP_DEBUG_PRINT("PPP interrupt mode enabled successfully\n");
+    #else
+    // 轮询模式:启动接收线程
+    PPP_DEBUG_PRINT("Starting PPP receive thread (polling mode)...\n");
+    ret = ppp_lwip_start_receive_thread();
+    if (ret != PPP_ERR_OK) {
+        PPP_DEBUG_PRINT("Failed to start PPP data receive thread: %d\n", ret);
+        goto init_failed;
+    }
+    PPP_DEBUG_PRINT("PPP receive thread started successfully\n");
+    #endif
+#else
     PPP_DEBUG_PRINT("Waiting for modem stabilization after power control...\n");
     // 等待5秒让调制解调器完全稳定
     osal_msleep(5000);
@@ -223,27 +243,7 @@ errcode_t zhihui_ppp_init(const ppp_config_t *config)
     ppp_api_update_all_runtime_status();
     
     ppp_get_network_signal_info();
-#else
-    // 9. 启动PPP数据接收处理(根据配置选择中断或轮询模式)
-    #if PPP_USE_INTERRUPT_MODE
-        // 中断模式:注册UART中断回调(UART总线号从ppp_serial_get_uart_bus()获取)
-        PPP_DEBUG_PRINT("Enabling PPP interrupt mode...\n");
-        ret = ppp_interrupt_enable();
-        if (ret != PPP_ERR_OK) {
-            PPP_DEBUG_PRINT("Failed to enable PPP interrupt: %d\n", ret);
-            goto init_failed;
-        }
-        PPP_DEBUG_PRINT("PPP interrupt mode enabled successfully\n");
-    #else
-        // 轮询模式:启动接收线程
-        PPP_DEBUG_PRINT("Starting PPP receive thread (polling mode)...\n");
-        ret = ppp_lwip_start_receive_thread();
-        if (ret != PPP_ERR_OK) {
-            PPP_DEBUG_PRINT("Failed to start PPP data receive thread: %d\n", ret);
-            goto init_failed;
-        }
-        PPP_DEBUG_PRINT("PPP receive thread started successfully\n");
-    #endif
+    
 #endif
 
 return ERRCODE_SUCC;
index dd1443c..f6019ce 100644 (file)
@@ -280,8 +280,9 @@ ppp_error_t ppp_at_set_baudrate(uint32_t baudrate)
         PPP_DEBUG_ERROR("Unsupported baudrate: %u", baudrate);
         return PPP_ERR_INVALID_PARAM;
     }
-#if PPP_DO_SERIAL_TEST
-
+#if defined(PPP_DO_SERIAL_TEST) && (PPP_DO_SERIAL_TEST == 1)
+    uint32_t current_modem_baudrate = baudrate; // 测试模式下,直接使用输入的波特率
+#else
     /* 先查询当前modem波特率 */
     uint32_t current_modem_baudrate = ppp_query_current_modem_baudrate();
     if (current_modem_baudrate == baudrate) {
@@ -321,8 +322,6 @@ ppp_error_t ppp_at_set_baudrate(uint32_t baudrate)
         PPP_DEBUG_ERROR("Failed to set baudrate to %u", baudrate);
         return ret;
     }
-#else
-    uint32_t current_modem_baudrate = baudrate;
 #endif
     /* 更新串口配置 */
     uart_attr_t uart_attr = {
index 856f916..066142c 100644 (file)
@@ -9,8 +9,8 @@
  * - 定时器监控buffer状态,超时自动flush(30ms)
  * - 处理线程负责调用pppos_input_tcpip()并释放buffer
  */
-
 #include "ppp_interrupt.h"
+#include "ppp_config.h"
 #include "ppp_debug.h"
 #include "ppp_lwip.h"
 #include "ppp_serial.h"
@@ -123,7 +123,7 @@ static void ppp_irq_process_thread(void *arg)
     unsigned int msg_size = sizeof(ppp_irq_msg_t);
     
     PPP_DEBUG_INFO("PPP IRQ process thread started (ping-pong mode)");
-    #ifdef PPP_DO_SERIAL_TEST
+    #if defined(PPP_DO_SERIAL_TEST) && (PPP_DO_SERIAL_TEST == 1)
     const uint8_t print_data[] = "PPP IRQ process thread started (ping-pong mode)\n";
     uapi_uart_write(SIO_UART_BUS, print_data, sizeof(print_data), 100);
     #endif
@@ -172,8 +172,20 @@ static void ppp_irq_process_thread(void *arg)
         }
         PPP_DEBUG_PRINT("\n");
         
-#if PPP_DO_SERIAL_TEST
-        // 获取PPP控制块
+#if defined(PPP_DO_SERIAL_TEST) && (PPP_DO_SERIAL_TEST == 1)
+        err_t result = ERR_OK;
+        uint32_t write_result = uapi_uart_write(SIO_UART_BUS, buffer->data, msg.length, 100);
+        if (write_result != msg.length) {
+            PPP_DEBUG_WARN("uapi_uart_write failed: %d, dropped %u bytes", write_result, msg.length);
+            g_ppp_interrupt_stats.dropped_lwip_error += msg.length;
+            g_ppp_interrupt_stats.lwip_error_count++;
+        } else {
+            PPP_DEBUG_WARN("uapi_uart_write : %u bytes", msg.length);
+            g_ppp_interrupt_stats.total_bytes_sent_to_lwip += msg.length;
+            g_ppp_interrupt_stats.success_count++;
+        }
+#else
+        // 获取PPP控制块    
         ppp_pcb *ppp_pcb_ptr = ppp_lwip_get_pcb();
         if (!ppp_pcb_ptr || !ppp_lwip_is_initialized()) {
             PPP_DEBUG_WARN("PPP not initialized, dropping %u bytes", msg.length);
@@ -183,7 +195,6 @@ static void ppp_irq_process_thread(void *arg)
             continue;
         }
 
-        
         // 调用lwIP的线程安全接口处理PPP数据
         // pppos_input_tcpip会复制数据到pbuf,所以我们可以安全地重用buffer
         err_t result = pppos_input_tcpip(ppp_pcb_ptr, buffer->data, msg.length);
@@ -196,18 +207,6 @@ static void ppp_irq_process_thread(void *arg)
             g_ppp_interrupt_stats.total_bytes_sent_to_lwip += msg.length;
             g_ppp_interrupt_stats.success_count++;
         }
-#else
-        err_t result = ERR_OK;
-        uint32_t write_result = uapi_uart_write(SIO_UART_BUS, buffer->data, msg.length, 100);
-        if (write_result != msg.length) {
-            PPP_DEBUG_WARN("uapi_uart_write failed: %d, dropped %u bytes", write_result, msg.length);
-            g_ppp_interrupt_stats.dropped_lwip_error += msg.length;
-            g_ppp_interrupt_stats.lwip_error_count++;
-        } else {
-            PPP_DEBUG_WARN("uapi_uart_write : %u bytes", msg.length);
-            g_ppp_interrupt_stats.total_bytes_sent_to_lwip += msg.length;
-            g_ppp_interrupt_stats.success_count++;
-        }
 #endif
         // 释放buffer(标记为空闲)
         buffer->state = PPP_BUFFER_STATE_EMPTY;
@@ -593,7 +592,12 @@ ppp_error_t ppp_interrupt_disable(void)
         g_ppp_timer_running = false;
         osal_timer_stop(&g_ppp_timeout_timer);
         osal_timer_destroy(&g_ppp_timeout_timer);
-        PPP_DEBUG_INFO("Timeout timer stopped");
+        
+        // ⚠️ 关键修复:清零 timer 指针,否则下次 osal_timer_init 会因为 timer != NULL 而失败
+        // osal_timer_destroy 不会清零 timer->timer,但 osal_timer_init 要求它必须为 NULL
+        g_ppp_timeout_timer.timer = NULL;
+        
+        PPP_DEBUG_INFO("Timeout timer stopped and cleared");
     }
     
     // 3. 停止处理线程
index ac05f00..f4b2d64 100644 (file)
@@ -220,7 +220,7 @@ static void ppp_lwip_process_thread(void *arg)
     unsigned int msg_size = sizeof(ppp_msg_t);
     
     PPP_DEBUG_INFO("PPP process thread started (ping-pong polling mode)");
-    #ifdef PPP_DO_SERIAL_TEST
+    #if defined(PPP_DO_SERIAL_TEST) && (PPP_DO_SERIAL_TEST == 1)
     const uint8_t print_data[] = "PPP process thread started (ping-pong polling mode)\n";
     uapi_uart_write(SIO_UART_BUS, print_data, sizeof(print_data), 100);
     #endif
@@ -269,7 +269,18 @@ static void ppp_lwip_process_thread(void *arg)
         }
         PPP_DEBUG_PRINT("\n");
         
-#if PPP_DO_SERIAL_TEST
+#if defined(PPP_DO_SERIAL_TEST) && (PPP_DO_SERIAL_TEST == 1)
+        err_t result = ERR_OK;
+        uint32_t write_result = uapi_uart_write(SIO_UART_BUS, buffer->data, msg.length, 100);
+        if (write_result != msg.length) {
+            PPP_DEBUG_WARN("uapi_uart_write failed: %d, dropped %u bytes", write_result, msg.length);
+            g_ppp_stats.total_bytes_discarded += msg.length;
+            g_ppp_stats.lwip_error_count++;
+        } else {
+            PPP_DEBUG_WARN("uapi_uart_write : %u bytes", msg.length);
+            g_ppp_stats.total_bytes_processed += msg.length;
+        }
+#else
         // 获取PPP控制块
         ppp_pcb *ppp_pcb_ptr = ppp_lwip_get_pcb();
         if (!ppp_pcb_ptr || !ppp_lwip_is_initialized()) {
@@ -291,18 +302,7 @@ static void ppp_lwip_process_thread(void *arg)
             g_ppp_stats.lwip_error_count++;
         } else {
             g_ppp_stats.total_bytes_processed += msg.length;
-        }
-#else
-        err_t result = ERR_OK;
-        uint32_t write_result = uapi_uart_write(SIO_UART_BUS, buffer->data, msg.length, 100);
-        if (write_result != msg.length) {
-            PPP_DEBUG_WARN("uapi_uart_write failed: %d, dropped %u bytes", write_result, msg.length);
-            g_ppp_stats.total_bytes_discarded += msg.length;
-            g_ppp_stats.lwip_error_count++;
-        } else {
-            PPP_DEBUG_WARN("uapi_uart_write : %u bytes", msg.length);
-            g_ppp_stats.total_bytes_processed += msg.length;
-        }
+        }        
 #endif
         // 释放buffer(标记为空闲)
         buffer->state = PPP_BUFFER_STATE_EMPTY;
index f52f448..99b1f33 100644 (file)
@@ -294,7 +294,7 @@ void ppp_power_on_cat1(void)
     ppp_api_cat1_bat_pin(1);
     osal_msleep(900);  // 使用OSAL延时函数
     ppp_api_cat1_power_pin(0);  
-#ifdef PPP_DO_SERIAL_TEST
+#if defined(PPP_DO_SERIAL_TEST) && (PPP_DO_SERIAL_TEST == 1)
     ppp_api_cat1_reset_pin(1);
 #endif