|
|
@@ -7,6 +7,7 @@
|
|
|
#include "dtu_uart2.h"
|
|
|
#include "sht30_collect.h"
|
|
|
#include <user_mb_app.h>
|
|
|
+#include <shell.h> /* finsh_set_device(): uart_stop_for_ota 要把 finsh 摘回 uart1 */
|
|
|
#define DBG_ENABLE
|
|
|
#define DBG_COLOR
|
|
|
#define DBG_TAG "control"
|
|
|
@@ -54,6 +55,234 @@ int thread_restart_workloop(void)
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
+/* ==================== OTA 升级前的整机静默(与 PCB_V2 同一机制) ====================
|
|
|
+ * 为什么必须停线程: ymodem_ota() 会把 console 切到 uart2(ymodem 数据通道),
|
|
|
+ * 之后**任何**线程的 LOG_xxx / rt_kprintf 都会打进 uart2, 直接污染固件字节流。
|
|
|
+ * 本分支线程名清单按实际创建名给出; 内核只保留前 RT_NAME_MAX-1(=7) 个字符,
|
|
|
+ * 所以下面的前缀都是 7 字符 —— 正好等价于全名匹配。
|
|
|
+ * ⚠️ DTU_Uart_thread_send / DTU_Uart_thread_recv 截断后同为 "DTU_Uar",
|
|
|
+ * 无法用名字区分; 发送线程一律由 dtu_stop_send_thread()(显式句柄)处理,
|
|
|
+ * 接收线程是执行 upgrade 的自身, 由下面的 self 判断跳过。 */
|
|
|
+#define OTA_THREAD_MAX 16
|
|
|
+
|
|
|
+/* 只动这些线程(前缀, 7 字符以内) */
|
|
|
+static const char *g_ota_stop[] =
|
|
|
+{
|
|
|
+ "work_lo", /* work_loop_thread 工作流程, LOG 密集 */
|
|
|
+ "work_st", /* work_status_check_thread 工作状态, 每 10s 一批日志 */
|
|
|
+ "Check_S", /* Check_Self_thread_entry 清扫, 驱动电机 */
|
|
|
+ "Check_g", /* Check_gaoya_thread_entry 恒温, 每秒 LOG_I */
|
|
|
+ "Hmi_Fre", /* Hmi_Fresh_thread 屏幕刷新 */
|
|
|
+ "Ctr mot", /* Ctr motor 仓门/光源, 带 rt_kprintf */
|
|
|
+ "adc_thr", /* adc_thread ADC 采集(已先由 thread_delete_adc 优雅停) */
|
|
|
+ "Power C", /* Power Check thread 每秒读电流/电压 */
|
|
|
+ "md_m_po", /* md_m_poll modbus 轮询 */
|
|
|
+ "md_m_se", /* md_m_send modbus 发送 */
|
|
|
+ "thread_", /* thread_485play 485 播报 */
|
|
|
+};
|
|
|
+
|
|
|
+/* 这些绝对不能删 */
|
|
|
+static const char *g_ota_keep[] =
|
|
|
+{
|
|
|
+ "tshell", /* finsh 控制台 */
|
|
|
+ "tidle", /* 空闲线程 */
|
|
|
+ "main", /* 主线程: Check_ParamConfig 之后只剩 LED 心跳 */
|
|
|
+ "timer", /* 软件定时器线程 */
|
|
|
+ "syswatch", /* 看门狗守护 */
|
|
|
+ "DTU_Ua", /* DTU 收发线程(recv 是自身, 由 self 判断再兜一层) */
|
|
|
+};
|
|
|
+
|
|
|
+/* 名字前缀比较: 内核只存了前 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 / main / timer / DTU 收发。
|
|
|
+ *
|
|
|
+ * 顺序: ① adc_thread 优雅退出(它可能正持有 ADC 设备锁)
|
|
|
+ * ② 收集待停线程(先收集再删, 不能边遍历边删)
|
|
|
+ * ③ 逐个 rt_thread_delete
|
|
|
+ *
|
|
|
+ * @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;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* ① adc_thread 必须优雅退出: 直接 rt_thread_delete 若刚好删在
|
|
|
+ rt_adc_enable/read 中间, 它持有的 ADC 设备锁永远不会释放 */
|
|
|
+ thread_delete_adc();
|
|
|
+
|
|
|
+ /* ② 先收集后删除: 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;
|
|
|
+}
|
|
|
+
|
|
|
+/* 关闭一个串口: 先摘接收回调, 再循环 close 到引用计数归零 */
|
|
|
+static void ota_close_uart(rt_device_t dev, const char *name)
|
|
|
+{
|
|
|
+ rt_uint8_t guard = 8;
|
|
|
+
|
|
|
+ if (dev == RT_NULL)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* ① 先摘掉接收回调。close() 内部也会清, 但清回调后到 close 之间
|
|
|
+ 还有窗口期, 此刻若有数据到达, 回调里会 release 已经没人等的
|
|
|
+ 信号量 / 往已删线程的消息队列里塞消息。 */
|
|
|
+ rt_device_set_rx_indicate(dev, RT_NULL);
|
|
|
+
|
|
|
+ /* ② 循环 close 到引用计数归零。guard 兜底, 防止内核行为异常时空转。 */
|
|
|
+ while (dev->ref_count > 0 && guard-- > 0)
|
|
|
+ {
|
|
|
+ if (rt_device_close(dev) != RT_EOK)
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ LOG_I("[ota] %s closed, ref_count=%d", name, (int)dev->ref_count);
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * OTA 升级前关闭串口4(HMI屏) 和串口5(485)。
|
|
|
+ *
|
|
|
+ * 为什么要关:
|
|
|
+ * 1) ymodem_ota() 内部会 rt_console_set_device("uart2"), 之后 console 指向
|
|
|
+ * uart2。此时 uart4/uart5 若还在工作, 它们的 ISR、以及仍可能残留的
|
|
|
+ * 485 播报/屏幕刷新, 都会和 ymodem 抢 CPU 与中断, 导致传输丢字节;
|
|
|
+ * 2) 485 是**半双工**总线, 升级完复位后从机状态与 MCU 不同步, 现场表现为
|
|
|
+ * "升级完 485 读不到数据";
|
|
|
+ * 3) 屏幕在升级过程中继续刷新, 白白占用带宽, 且部分屏会因长时间收不到
|
|
|
+ * 指令而重新初始化自身。
|
|
|
+ *
|
|
|
+ * ★ 本函数必须在 thread_stop_all_for_ota() **之后**调用:
|
|
|
+ * 先停线程(没人再发数据) → 再关设备。反过来的话, 关设备的瞬间还有
|
|
|
+ * 线程在 HMI_WRITE/Uart5_Write, 会写到已被 disable 的 fifo 上。
|
|
|
+ *
|
|
|
+ * @return 关闭的串口个数
|
|
|
+ */
|
|
|
+int uart_stop_for_ota(void)
|
|
|
+{
|
|
|
+ int n = 0;
|
|
|
+
|
|
|
+ /* 置标志必须**先于**关设备: 中间还有别的线程可能正在 HMI_WRITE,
|
|
|
+ 看到标志后直接 return, 不会碰到正在被拆掉的 fifo。 */
|
|
|
+ g_hmi_port_closed = 1;
|
|
|
+
|
|
|
+ /* ---- 串口4: HMI 屏 ----
|
|
|
+ 先把 console / finsh 摘回 uart1, 否则它们各自持有 uart4 的一次 open
|
|
|
+ (rt_console_set_device / finsh_set_device 内部都是 open 新的 + close 旧的),
|
|
|
+ 引用计数降不到 0, 设备等于没关; 而且 tshell 正阻塞在 uart4 的 read 上。 */
|
|
|
+ if (rt_console_get_device() != RT_NULL &&
|
|
|
+ rt_strcmp(rt_console_get_device()->parent.name, UART_NAME) == 0)
|
|
|
+ {
|
|
|
+ rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
|
|
|
+ }
|
|
|
+#if defined(RT_USING_FINSH) && !defined(RT_USING_POSIX_STDIO) && defined(RT_USING_DEVICE)
|
|
|
+ finsh_set_device(RT_CONSOLE_DEVICE_NAME);
|
|
|
+#endif
|
|
|
+ if (serial != RT_NULL)
|
|
|
+ {
|
|
|
+ ota_close_uart(serial, UART_NAME);
|
|
|
+ n++;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* ---- 串口5: 485 ---- */
|
|
|
+ if (serial_485 != RT_NULL)
|
|
|
+ {
|
|
|
+ ota_close_uart(serial_485, UART_485);
|
|
|
+ n++;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* 把句柄置 NULL: 让上层即使漏判 g_hmi_port_closed 也不会往已废设备写。
|
|
|
+ ⚠️ 置 NULL 之后 ymodem 失败返回也不会有任何 HMI/485 输出, 这是预期的
|
|
|
+ ——升级流程本来就要求这段时间对外静默。 */
|
|
|
+ serial = RT_NULL;
|
|
|
+ serial_485 = RT_NULL;
|
|
|
+
|
|
|
+ LOG_I("[ota] uart4/uart5 stopped (%d)", n);
|
|
|
+ return n;
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
/**********************************************************************/
|
|
|
/*光控*/
|
|
|
@@ -203,7 +432,7 @@ void ResetInit(void)
|
|
|
ctr_router(ON);
|
|
|
#else
|
|
|
ctr_gaoya_12v(ON); //相机供电
|
|
|
- ctr_TG_Motor(OPEN);
|
|
|
+ ctr_TG_Motor(CLOSE);
|
|
|
#endif
|
|
|
ctr_hmi_5v(ON);
|
|
|
//2. RTU供电默认是开的,不能调用ctr_RTU_12V(ON);
|