zhunk 1 miesiąc temu
rodzic
commit
dd522f7a03

+ 147 - 0
Application_cbd_apm32f1/applications/control.c

@@ -61,6 +61,153 @@ int thread_restart_workloop(void)
     result = Work_Loop_Task();
     return result;
 }
+
+/***************************************************************************/
+/* ==================== OTA 升级前的线程清理 ==================== */
+
+#define OTA_THREAD_MAX   24
+
+/* 保留名单: 这几类线程删掉反而会让升级失败 */
+static const char *g_ota_keep[] =
+{
+    "syswatch",     /* 喂狗线程: 删了 5s 后硬件看门狗就复位, 升级必中断 */
+    "tshell",       /* 控制台 */
+    "tidle",
+    "main",
+};
+
+/* 需要停掉的业务线程(写完整名即可, 比较时自动按 RT_NAME_MAX-1 截断)。
+ *
+ *   为什么用"前缀匹配 + 遍历"而不是 rt_thread_find(全名):
+ *     rtconfig.h 里 RT_NAME_MAX = 8, 线程名被内核截断到 7 个字符 ——
+ *       "Control_Clear_thread_entry" / "Control_Heat_thread_entry" 都变成 "Control";
+ *       "DTU_Uart_thread_send" / "DTU_Uart_thread_recv" 都截成 "DTU_Uar",
+ *     而 recv 正是执行升级的当前线程, 用 rt_thread_find 有可能把调用者自己删掉。
+ *   所以改成遍历线程对象链表, 并显式跳过 rt_thread_self()。 */
+static const char *g_ota_stop[] =
+{
+    "work_loop_thread",             /* 工作流程 */
+    "work_status_check_thread",     /* WorkCheck */
+    "Control_Clear_thread_entry",   /* 清扫, 会驱动电机 */
+    "Control_Heat_thread_entry",    /* 恒温 */
+    "sht30_collect_thread",         /* 光控/温控, 每 5s 打一行 LOG_I */
+    "Power Check thread",           /* 每秒读电流/电压 */
+    "Hmi_Fresh_thread",             /* 屏幕刷新 */
+    "Ctr motor",                    /* 仓门/光源, 带 rt_kprintf */
+    "sys_monitor",                  /* CPU/统计 */
+    "DTU_Uart_thread_send",         /* DTU 发送 (recv 是自身, 会被跳过) */
+    "md_m_poll",                    /* modbus 轮询 */
+    "md_m_send",                    /* modbus 发送 */
+    "usage",                        /* cpu_usage 临时线程 */
+};
+
+/* 名字前缀比较: 内核只存了前 RT_NAME_MAX-1 个字符, 所以最多比这么多 */
+static int ota_name_match(const char *thread_name, const char *prefix)
+{
+    rt_size_t n;
+
+    if (thread_name == RT_NULL || prefix == RT_NULL || thread_name[0] == '\0')
+    {
+        return 0;
+    }
+    n = rt_strlen(prefix);
+    if (n > RT_NAME_MAX - 1)
+    {
+        n = RT_NAME_MAX - 1;
+    }
+    if (n == 0)
+    {
+        return 0;
+    }
+    return (rt_strncmp(thread_name, prefix, n) == 0) ? 1 : 0;
+}
+
+static int ota_in_list(const char *thread_name, const char **list, int cnt)
+{
+    int i;
+
+    for (i = 0; i < cnt; i++)
+    {
+        if (ota_name_match(thread_name, list[i]))
+        {
+            return 1;
+        }
+    }
+    return 0;
+}
+
+/**
+ * OTA 升级前把所有业务线程停掉, 只留 tshell / idle / syswatch。
+ *
+ * 为什么必须停:
+ *   1) ymodem_ota() 会把 console 切到 uart2(ymodem_ota.c:117), 之后**任何线程**
+ *      的 LOG_xxx / rt_kprintf 都会打进 uart2, 直接污染 ymodem 字节流 -> 校验失败;
+ *   2) sht30 / Power Check 持续读 ADC, 抢 CPU 会让 ymodem 丢字节;
+ *   3) Control_Clear / Control_Heat / Ctr motor 会驱动电机, 升级过程中不该动机械部件。
+ *
+ * 实现说明: 本工程的线程**没有** `update_flag==1 break` 这类自退机制, 所以这里
+ *   不做"置标志 + 等它自己退", 直接遍历 + 强删。强删时若线程正持有 IPC 或外设锁,
+ *   由于紧接着就进 ymodem、结束即 rt_hw_cpu_reset(), 不会影响升级本身。
+ *   ★ 全程不改 device_status.update_flag —— 那个值一旦被落盘, 下次开机会被
+ *     Check_ParamConfig 判成"有升级请求"而再进 OTA。
+ *
+ * @return 实际删除的线程数; 负值为错误
+ */
+int thread_stop_all_for_ota(void)
+{
+    struct rt_object_information *info;
+    rt_list_t   *node;
+    rt_thread_t  victims[OTA_THREAD_MAX];
+    rt_thread_t  self;
+    int          nv = 0, i, done = 0;
+
+    info = rt_object_get_information(RT_Object_Class_Thread);
+    if (info == RT_NULL)
+    {
+        LOG_E("[ota] thread object information NOT found");
+        return -RT_ERROR;
+    }
+
+    /* 收集待停线程。必须"先收集后删除": rt_thread_delete 会摘掉链表节点,
+       边遍历边删会把 object_list 走坏 */
+    self = rt_thread_self();
+    rt_enter_critical();
+    rt_list_for_each(node, &info->object_list)
+    {
+        rt_thread_t t = rt_list_entry(node, struct rt_thread, list);
+
+        if (t == self)                                        /* 不能删自己 */
+        {
+            continue;
+        }
+        if (ota_in_list(t->name, g_ota_keep, sizeof(g_ota_keep) / sizeof(g_ota_keep[0])))
+        {
+            continue;
+        }
+        if (!ota_in_list(t->name, g_ota_stop, sizeof(g_ota_stop) / sizeof(g_ota_stop[0])))
+        {
+            continue;                                          /* 不在名单里, 不动 */
+        }
+        if (nv < OTA_THREAD_MAX)
+        {
+            victims[nv++] = t;
+        }
+    }
+    rt_exit_critical();
+
+    /* 逐个删除 */
+    for (i = 0; i < nv; i++)
+    {
+        if (rt_thread_delete(victims[i]) == RT_EOK)
+        {
+            done++;
+        }
+    }
+
+    LOG_I("[ota] stopped %d of %d candidate thread(s)", done, nv);
+    return done;
+}
+
 /**************************1.0定制2000W*************************************/
 uint8_t ctr_Fan_PUT(uint8_t status_ws)
 {

+ 5 - 1
Application_cbd_apm32f1/applications/control.h

@@ -25,7 +25,7 @@
 
 ////////////////////////////////////////////////////////////////
 
-#define VERSION  "2.5.9"
+#define VERSION  "2.5.10"
 
 
 extern rt_event_t event_485_play;
@@ -325,5 +325,9 @@ int thread_485_play(void);
 
 int thread_delete_workloop(void);
 int thread_restart_workloop(void);
+/* OTA 升级前停掉全部业务线程(只留 tshell/idle/syswatch), 返回实际删除数。
+   原因: ymodem 会把 console 切到 uart2, 任何线程打印都会污染数据流。
+   详见 control.c 的实现说明。 */
+int thread_stop_all_for_ota(void);
 
 #endif /* APPLICATIONS_INC_CONTROL_H_ */

+ 40 - 7
Application_cbd_apm32f1/applications/dtu_uart2.c

@@ -1425,14 +1425,47 @@ int parseDtu(char *json_string)
              }
               else if (strstr(item_cmd->valuestring,"update")!=RT_NULL)
               {
-                  rt_kprintf("准备升级!!!");
-                  device_status.update_flag = 1;
-                  ef_set_env_blob("update_flag", &device_status.update_flag, sizeof(device_status.update_flag));
-                  rt_kprintf("%s\n",makeJson_Warn("system will update","control"));
-                  rt_thread_delay(2000);
+                  /* ★ 直接在本线程进入 ymodem, 不再"置标志 + 重启"。
+                     老路子要把 update_flag 写进 EasyFlash 再靠开机读回来, 而 EasyFlash
+                     正是丢参数的重灾区, 标志一丢升级就静默跳过(现场已复现)。
+
+                     ★★ 本分支内禁止使用 rt_kprintf ★★
+                     ymodem_ota() 内部会 rt_console_set_device("uart2"), 把调试口切到 uart2
+                     当 ymodem 接收口。此后任何 rt_kprintf 都会往 uart2 打, 污染 ymodem
+                     数据流导致校验失败。所以这里只用 LOG_W(在切换 console 之前输出)。 */
+
+                  LOG_W("准备升级!!!");
                   DTU_Write("%s",makeJson_Warn("system will update","control"));
-                  rt_thread_delay(20);
-                  rt_hw_cpu_reset();
+                  rt_thread_delay(1000);          /* 让"即将升级"这帧真正发出去 */
+
+                  /* 清掉 flash 里可能残留的升级标志。
+                     开机 Check_ParamConfig 里仍有 `update_flag==1 -> ymodem_ota`,
+                     若不清掉, ymodem 结束复位后开机会再进一次 OTA。 */
+                  device_status.update_flag = 0;
+                  ef_set_env_blob("update_flag", &device_status.update_flag, sizeof(device_status.update_flag));
+
+                  /* ---- 以下把 uart2 彻底让给 ymodem ----
+                     DTU 与工控机通信走的就是 uart2(DTU_UART_NAME), 和 ymodem 接收口
+                     是同一个串口。不停掉这两个, 上报数据会插进 ymodem 字节流里,
+                     传输必然校验失败。 */
+                  if (timer_uart2 != RT_NULL)
+                  {
+                      rt_timer_stop(timer_uart2);          /* 停 uart2 周期上报 */
+                  }
+                  if (dtu_send_data != RT_NULL)
+                  {
+                      rt_thread_delete(dtu_send_data);     /* 停 uart2 发送线程 */
+                      dtu_send_data = RT_NULL;
+                  }
+
+                  /* ---- 其余业务线程全部停掉 ----
+                     凡是会持续打印日志的线程都必须退: ymodem 期间 console 已被切到
+                     uart2, 它们每一行日志都会打进 ymodem 数据流。
+                     另外 sht30/PowerCheck 读 ADC、Control_Clear/Control_Heat/Ctr motor
+                     驱动电机, 升级过程中都该停。 */
+                  thread_stop_all_for_ota();
+
+                  ymodem_ota(1,0);   /* 内部会切 console 到 uart2, 成功/失败均自行复位 */
               }
               else if (strstr(item_cmd->valuestring,"read")!=RT_NULL)
               {

+ 1 - 6
Application_cbd_apm32f1/applications/hmi.c

@@ -1309,14 +1309,9 @@ void Check_ParamConfig(void)
 
     }
     //RTC时间判断,如果当前年份小于2020,提示需要设置时间,切换为光控
-    if(device_status.update_flag==1)
+    if(1)
     {
         device_status.update_flag = 0;
-        ef_set_env_blob("update_flag", &device_status.update_flag, sizeof(device_status.update_flag));
-        ymodem_ota(1,0);
-    }
-    else {
-        device_status.update_flag = 0;
         //初始化联网状态
         rt_sprintf(device_status.net,"1");
         rt_sprintf(device_status.csq,"0");

+ 152 - 1
Application_cbd_apm32f1/readme.md

@@ -1,3 +1,155 @@
+# 2026-09-27 更新说明
+	版本:2.6.10(待定)
+	1. 【修复】远程升级改为"收到指令直接进 ymodem",不再"置标志 + 重启";
+	2. 【修复】升级前停掉全部会持续打印日志的业务线程(14 个),避免污染 ymodem 数据流;
+	3. 【新增】thread_stop_all_for_ota():按线程对象遍历 + 名字前缀匹配来停线程
+	   (RT_NAME_MAX=8 会把线程名截断到 7 字符,不能再用 rt_thread_find 按全名查);
+	(详见下方专题,本轮改动文件:control.c、control.h、dtu_uart2.c、hmi.c)
+
+## 专题:升级链路加固(2026-09-27)
+
+### 一、现象
+- 远程下发 update 指令后设备重启,**升级不执行**,串口也没有任何报错(静默跳过);
+- 即使进了 ymodem,固件传输也容易校验失败。
+
+### 二、根因(2 条)
+
+**① 升级请求只有一条通道,而且是最不可靠的那条**
+
+原链路是 `ef_set_env_blob("update_flag")` → 重启 → 开机 `ef_get_env_blob` 判断 → `ymodem_ota`。
+这条路要过一次 EasyFlash,而本机 EasyFlash **正是丢参数的重灾区**(历史上已多次出现
+"参数丢失导致温控/熄屏时间为 0"):标志一丢,升级就被静默跳过了。
+
+另外还有一处放大器 —— `hmi.c` 的"参数丢失处理"分支里有
+`device_status.update_flag = 0;`:**一旦 iSave 那条 env 丢了,开机就会顺手把升级标志清零**,
+表现就是"触发了升级、重启、没反应"。
+
+**② ymodem 与 DTU 抢同一个串口,其他线程的日志会污染数据流**
+
+`ymodem_ota.c:117` 有一句:
+
+```c
+rt_console_set_device("uart2");
+```
+
+它把**调试口切成 uart2 当 ymodem 接收口**,此后任何线程的 `rt_kprintf` / `LOG_xxx`
+都会打进 uart2;而 `DTU_UART_NAME` 本身就是 `"uart2"`(dtu_uart2.h:13)——
+**DTU 与工控机的通信口和 ymodem 接收口是同一个**。
+
+于是三类东西会插进 ymodem 字节流:
+- `dtu_send_data` 线程 + `timer_uart2` 定时上报(往 uart2 发数据);
+- 各业务线程的日志:`sht30_collect_thread` 每 5s 一行、
+  `Control_Heat_thread_entry` 每秒一行 `process`、`work_loop` / `work_status` 密集打印等;
+- 同时 `sht30` / `Power Check` 持续读 ADC、`Control_Clear` / `Ctr motor` 驱动电机,抢 CPU 也造成丢字节。
+
+### 三、改法
+
+**1. `dtu_uart2.c` — 收到大平台 update 指令直接进 ymodem**
+
+```c
+else if (strstr(item_cmd->valuestring,"update")!=RT_NULL)
+{
+    LOG_W("准备升级!!!");
+    DTU_Write("%s",makeJson_Warn("system will update","control"));
+    rt_thread_delay(1000);                    /* 让这帧真正发出去 */
+
+    device_status.update_flag = 0;            /* 清残留标志, 防复位后开机再进 OTA */
+    ef_set_env_blob("update_flag", &device_status.update_flag, sizeof(device_status.update_flag));
+
+    if (timer_uart2 != RT_NULL)               /* 停 uart2 周期上报 */
+        rt_timer_stop(timer_uart2);
+    if (dtu_send_data != RT_NULL) {           /* 停 uart2 发送线程 */
+        rt_thread_delete(dtu_send_data);
+        dtu_send_data = RT_NULL;
+    }
+
+    thread_stop_all_for_ota();                /* 停掉其余全部业务线程 */
+
+    ymodem_ota(1,0);                          /* 内部切 console 到 uart2, 自行复位 */
+}
+```
+
+**★ 本分支内禁止使用 `rt_kprintf`**(原因见根因②),只允许在切换 console 之前用 `LOG_W`。
+
+**2. `control.c` — 新增 `thread_stop_all_for_ota()`(control.h 导出)**
+
+一次停掉 14 个业务线程:`work_loop_thread`、`work_status_check_thread`、
+`Control_Clear_thread_entry`、`Control_Heat_thread_entry`、`sht30_collect_thread`、
+`Power Check thread`、`Hmi_Fresh_thread`、`Ctr motor`、`sys_monitor`、
+`DTU_Uart_thread_send`、`md_m_poll`、`md_m_send`、`usage`。
+
+**保留** `syswatch`(喂狗线程,删了 5s 后硬件看门狗就会复位)、`tshell`、`tidle`、`main`。
+
+### 四、★ 关键坑:`RT_NAME_MAX = 8`,不能按线程名查找
+
+`rtconfig.h:8` 是 `#define RT_NAME_MAX 8`,内核**只存前 7 个字符**:
+
+| 创建时的名字 | 内核里实际存的 |
+|---|---|
+| `Control_Clear_thread_entry` | `Control` |
+| `Control_Heat_thread_entry` | `Control` ← 与上面撞名 |
+| `DTU_Uart_thread_send` | `DTU_Uar` |
+| `DTU_Uart_thread_recv` | `DTU_Uar` ← 与上面撞名,**而且它就是执行升级的当前线程** |
+
+所以 `rt_thread_find(全名)` 在这里不可靠,甚至可能**把调用者自己删掉**。
+
+改用 `list_thread` 同款做法:遍历内核线程对象链表 + **名字前缀匹配** + **显式跳过 `rt_thread_self()`**:
+
+```c
+struct rt_object_information *info = rt_object_get_information(RT_Object_Class_Thread);
+rt_list_for_each(node, &info->object_list)
+{
+    rt_thread_t t = rt_list_entry(node, struct rt_thread, list);
+    if (t == self) continue;                                    /* 不能删自己 */
+    if (ota_in_list(t->name, g_ota_keep, ...)) continue;         /* syswatch 等保留 */
+    if (!ota_in_list(t->name, g_ota_stop, ...)) continue;
+    if (nv < OTA_THREAD_MAX) victims[nv++] = t;                  /* 先收集 */
+}
+/* 出了临界区再逐个删 —— 边遍历边删会把 object_list 走坏 */
+```
+
+两个细节:
+- 前缀比较长度要截到 `RT_NAME_MAX-1`,这样 `"Control"` 能同时命中那两个 Control 线程;
+- `struct rt_thread` 的 rt_object 部分是**内联展开**的(name/type/flags/list 直接在最前面),
+  没有 `struct rt_object parent;` 成员,所以 `rt_list_entry` 的 member 参数用 `list`。
+
+### 五、`hmi.c` 开机兜底分支也加了停线程
+
+开机侧 `if(device_status.update_flag==1) { ... ymodem_ota(1,0); }` 仍保留
+(屏幕触发的 `hmi_update` 走"置标志 + 重启"后由它接手),但进 ymodem 前同样要停线程:
+此时 `Hmi_Fresh` / `Control_*` 等还在 else 分支没启动,但 `dtu_uart2` / `sys_monitor` /
+`cpu_usage` / modbus 这些 INIT 阶段起的线程已经在跑了。
+
+### 六、验证
+
+`.workbuddy/tools/ota_thread_stop_check.py`(宿主 gcc + python):
+
+```
+[A] thread_stop_all_for_ota() gcc exit=0                      PASS
+[B] 17 项语义核对(遍历方式/跳过自身/先收集后删/不碰 update_flag/
+    update 分支无 rt_kprintf/无 rt_hw_cpu_reset/末行进 ymodem)   全 OK
+[C] ★覆盖性验证: 从源码提取全部线程名, 按 RT_NAME_MAX=8 模拟截断 + 前缀匹配
+    提取 14 个 -> 停掉 14 / 漏网 0
+    syswatch -> syswatc  KEEP (保留)                            <- 喂狗线程确认被保护
+[D] 括号配平                                                   OK
+=== 总体: 全部通过 ===
+```
+
+`[C]` 是自动从源码提取线程名的,**以后再往工程里加线程,跑一次就知道有没有被覆盖**。
+
+### 七、上板前必做
+
+RT-Thread Studio **Refresh(F5) + Clean + Build**(本机无 arm-none-eabi,未本地编译)。
+
+### 八、与本分支的历史行为差异(注意)
+
+- 升级不再写 `update_flag`(原先置 1 后重启),改为**直接进 ymodem**;
+- 进入升级会**删除业务线程**,升级失败时 ymodem 内部 `rt_hw_cpu_reset()` 会整机重启恢复,
+  属预期行为;
+- 大平台下发的 update 指令现在**不会再重启**,而是当场进入等待接收固件状态。
+
+---
+
 # 2026-02-09 更新说明
 	版本:2.6.9
 	1. 增加lwjson库
@@ -6,7 +158,6 @@
 # 2025-11-03 更新说明
 	版本:2.5.9
 	1. 修复屏幕设置北大荒ID的前缀类型为测报灯,修改为性诱;
-
 # 2025-10-29 更新说明
 	版本:2.5.8
 	1. 解决Flash参数丢失导致,温控,屏幕熄屏时间为0;