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 = {
}
- #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) {
} 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
// 打开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);
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;
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) {
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 = {
* - 定时器监控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"
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
}
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);
continue;
}
-
// 调用lwIP的线程安全接口处理PPP数据
// pppos_input_tcpip会复制数据到pbuf,所以我们可以安全地重用buffer
err_t result = pppos_input_tcpip(ppp_pcb_ptr, buffer->data, msg.length);
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;
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. 停止处理线程
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
}
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()) {
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;
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