|
|
@@ -44,6 +44,237 @@
|
|
|
|
|
|
|
|
|
static char rx_buffer[HMI_UART_RXBUFFER_SIZE + 1];
|
|
|
+/* 批量读缓冲:一次 DMA 收到的原始数据先读到这里,再按帧切分交给 HMI_Decode */
|
|
|
+static char rx_pkt[HMI_UART_RXBUFFER_SIZE + 1];
|
|
|
+/* 消息队列内存池:只装"有新数据"的通知(每条 8 字节),512B 足够深 */
|
|
|
+static char msg_pool[512];
|
|
|
+
|
|
|
+/* 串口设备句柄:发送框架(下面几个函数)要用,故定义提前到此处;
|
|
|
+ 原来那句在 globals 区中,位置在框架函数之后会导致隐式声明。 */
|
|
|
+rt_device_t serial;
|
|
|
+
|
|
|
+/* ==================== 发送框架(对齐 PCB_V2) ====================
|
|
|
+ * 四层保护,任一层都不能单独省掉:
|
|
|
+ * ① 写互斥:HMI_WRITE 用文件级静态缓冲做 vsnprintf,HMI/DTU/自检多个线程
|
|
|
+ * 同时调用会把格式化结果互相踩掉(串出半个帧、屏幕出现乱码)。
|
|
|
+ * ② 内容去重:整页刷新 60+ 条属性、屏幕每秒催一次,115200 下光刷新就吃掉
|
|
|
+ * 大半带宽。同一调用点(用格式化串地址唯一标识)内容没变时直接跳过。
|
|
|
+ * 槽位冲突按"未命中"处理 —— 只会多发、绝不会漏发。
|
|
|
+ * ③ 批量发送:HMI_BATCH_BEGIN/END 之间的多次写先累积,END 时一次写出;
|
|
|
+ * 整页刷新由 60+ 次串口写合并成 1~3 次。
|
|
|
+ * ④ 完整写入:uart4 以 TX_NON_BLOCKING 打开(见 HMI_uart4_init),
|
|
|
+ * rt_device_write 只接受 TX 环形缓冲装得下的部分、超出的静默丢弃
|
|
|
+ * (见 serial_v2.c 的 _serial_fifo_tx_nonblocking)。批量一次 flush 可达
|
|
|
+ * 1KB,缓冲满时必须等待重试,否则整批丢失 —— PCB_V2 2.49 实测踩过。
|
|
|
+ * 维护提示:新增刷新项直接用 HMI_WRITE 即可,无需关心批量/去重。
|
|
|
+ */
|
|
|
+
|
|
|
+/* 串口4在准备关闭后置 1,所有发送路径直接 return。
|
|
|
+ 不能只靠 serial 判空:rt_device_close() 释放的是 serial_v2 内部的 rx/tx fifo,
|
|
|
+ 设备指针本身仍非空,继续 rt_device_write 会解引用已释放的 tx_fifo 而 HardFault。 */
|
|
|
+rt_uint8_t g_hmi_port_closed = 0;
|
|
|
+
|
|
|
+static struct rt_mutex hmi_write_mutex;
|
|
|
+static rt_bool_t hmi_mutex_inited = RT_FALSE;
|
|
|
+
|
|
|
+static rt_bool_t hmi_write_lock(void)
|
|
|
+{
|
|
|
+ /* 中断上下文不加锁(互斥锁不允许在中断中获取) */
|
|
|
+ if (rt_interrupt_get_nest() != 0)
|
|
|
+ {
|
|
|
+ return RT_FALSE;
|
|
|
+ }
|
|
|
+ if (!hmi_mutex_inited)
|
|
|
+ {
|
|
|
+ rt_base_t level = rt_hw_interrupt_disable();
|
|
|
+ if (!hmi_mutex_inited)
|
|
|
+ {
|
|
|
+ if (rt_mutex_init(&hmi_write_mutex, "hmi_mtx", RT_IPC_FLAG_FIFO) == RT_EOK)
|
|
|
+ {
|
|
|
+ hmi_mutex_inited = RT_TRUE;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ rt_hw_interrupt_enable(level);
|
|
|
+ }
|
|
|
+ if (hmi_mutex_inited)
|
|
|
+ {
|
|
|
+ rt_mutex_take(&hmi_write_mutex, RT_WAITING_FOREVER);
|
|
|
+ return RT_TRUE;
|
|
|
+ }
|
|
|
+ return RT_FALSE;
|
|
|
+}
|
|
|
+
|
|
|
+static void hmi_write_unlock(void)
|
|
|
+{
|
|
|
+ if (hmi_mutex_inited)
|
|
|
+ {
|
|
|
+ rt_mutex_release(&hmi_write_mutex);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/* ---------- ② 内容去重 ---------- */
|
|
|
+#define HMI_DEDUPE_EN 1 /* 置 0 可整体关闭重复过滤(调试用) */
|
|
|
+#define HMI_DEDUPE_ENTRIES 96 /* 槽位数;冲突只影响压缩率,不影响正确性 */
|
|
|
+#define HMI_DEDUPE_RESYNC_SEC 10 /* 定期全量刷新周期(s) */
|
|
|
+/* 两次 getFullData 间隔超过本值即视为"换页/唤醒",强制全量刷新一次。
|
|
|
+ 屏幕正常停留时约 1s 一次请求,故 2500ms 有充足余量。 */
|
|
|
+#define HMI_FULLDATA_GAP_MS 2500
|
|
|
+
|
|
|
+typedef struct
|
|
|
+{
|
|
|
+ const char *key; /* 格式化串地址:每个调用点唯一 */
|
|
|
+ rt_uint32_t crc; /* 上次发送内容的 CRC32 */
|
|
|
+ rt_uint16_t len; /* 上次发送内容长度 */
|
|
|
+} hmi_dedupe_item_t;
|
|
|
+
|
|
|
+static hmi_dedupe_item_t hmi_dedupe_tab[HMI_DEDUPE_ENTRIES];
|
|
|
+static rt_uint16_t hmi_dedupe_tick_cnt = 0;
|
|
|
+
|
|
|
+static rt_uint32_t hmi_crc32(const char *s, rt_uint16_t len)
|
|
|
+{
|
|
|
+ rt_uint32_t crc = 0xFFFFFFFFu;
|
|
|
+ rt_uint16_t i;
|
|
|
+ rt_uint8_t k;
|
|
|
+
|
|
|
+ for (i = 0; i < len; i++)
|
|
|
+ {
|
|
|
+ crc ^= (rt_uint8_t)s[i];
|
|
|
+ for (k = 0; k < 8; k++)
|
|
|
+ {
|
|
|
+ crc = (crc >> 1) ^ (0xEDB88320u & (rt_uint32_t)(-(rt_int32_t)(crc & 1u)));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return ~crc;
|
|
|
+}
|
|
|
+
|
|
|
+static rt_uint16_t hmi_dedupe_index(const char *key)
|
|
|
+{
|
|
|
+ return (rt_uint16_t)(((rt_uint32_t)key >> 2) % HMI_DEDUPE_ENTRIES);
|
|
|
+}
|
|
|
+
|
|
|
+/* 返回 RT_TRUE:本次内容与上次完全相同,可跳过发送 */
|
|
|
+static rt_bool_t hmi_dedupe_skip(const char *key, const char *line, rt_uint16_t len)
|
|
|
+{
|
|
|
+ rt_uint16_t idx = hmi_dedupe_index(key);
|
|
|
+ hmi_dedupe_item_t *it = &hmi_dedupe_tab[idx];
|
|
|
+ rt_uint32_t crc = hmi_crc32(line, len);
|
|
|
+
|
|
|
+ if (it->key == key && it->len == len && it->crc == crc)
|
|
|
+ {
|
|
|
+ return RT_TRUE;
|
|
|
+ }
|
|
|
+ /* 写入顺序:crc/len 先写、key 最后写;读取时先比 key ——
|
|
|
+ * 这样并发时"命中"的条目其 crc/len 一定是完整的 */
|
|
|
+ it->crc = crc;
|
|
|
+ it->len = len;
|
|
|
+ it->key = key;
|
|
|
+ return RT_FALSE;
|
|
|
+}
|
|
|
+
|
|
|
+static void hmi_dedupe_reset(void)
|
|
|
+{
|
|
|
+ rt_memset(hmi_dedupe_tab, 0, sizeof(hmi_dedupe_tab)); /* key=NULL,任何真实调用点都不会命中 */
|
|
|
+ hmi_dedupe_tick_cnt = 0;
|
|
|
+}
|
|
|
+
|
|
|
+/* 由 Ctr_Mortor_entry 每 100ms 调用一次:到点复位过滤表(等价于下一次刷新全量重发) */
|
|
|
+static void hmi_dedupe_tick(void)
|
|
|
+{
|
|
|
+ if (++hmi_dedupe_tick_cnt >= (HMI_DEDUPE_RESYNC_SEC * 10))
|
|
|
+ {
|
|
|
+ hmi_dedupe_reset();
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/* ---------- ③ 批量发送 ---------- */
|
|
|
+#define HMI_BATCH_BUF_SIZE 1024 /* 累积缓冲;满则自动 flush,不丢指令 */
|
|
|
+static char hmi_batch_buf[HMI_BATCH_BUF_SIZE];
|
|
|
+static rt_uint16_t hmi_batch_len = 0;
|
|
|
+static volatile rt_bool_t hmi_batch_on = RT_FALSE;
|
|
|
+static rt_thread_t hmi_batch_owner = RT_NULL; /* 批量会话所属线程 */
|
|
|
+
|
|
|
+/* ---------- ④ 完整写入 ----------
|
|
|
+ * 分块写入并检查实际写入量,未写完时让出等待 TX 缓冲释放后重试;
|
|
|
+ * 中断上下文不等待(保持 HMI_WRITE 的中断可调用性),仅尽力发送。 */
|
|
|
+#define HMI_TX_RETRY_MAX 400 /* 最长等待约 400ms(1KB@115200≈89ms,留足余量) */
|
|
|
+static rt_uint16_t hmi_write_all(const void *data, rt_uint16_t len)
|
|
|
+{
|
|
|
+ const char *pbuf = (const char *)data;
|
|
|
+ rt_uint16_t sent = 0;
|
|
|
+ rt_uint16_t retry = 0;
|
|
|
+
|
|
|
+ while (sent < len)
|
|
|
+ {
|
|
|
+ rt_size_t n = rt_device_write(serial, 0, pbuf + sent, len - sent);
|
|
|
+
|
|
|
+ sent += (rt_uint16_t)n;
|
|
|
+ if (sent >= len)
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* 中断上下文不能等待:放弃剩余(正常业务发送都走线程上下文) */
|
|
|
+ if (rt_interrupt_get_nest() != 0)
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (++retry > HMI_TX_RETRY_MAX)
|
|
|
+ {
|
|
|
+ LOG_W("hmi tx wait timeout, dropped %u bytes", (unsigned)(len - sent));
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ rt_thread_mdelay(1);
|
|
|
+ }
|
|
|
+ return sent;
|
|
|
+}
|
|
|
+
|
|
|
+void Hmi_Batch_Begin(void)
|
|
|
+{
|
|
|
+ hmi_batch_len = 0;
|
|
|
+ hmi_batch_owner = rt_thread_self();
|
|
|
+ hmi_batch_on = RT_TRUE;
|
|
|
+}
|
|
|
+
|
|
|
+/* 落地当前累积的批量内容。
|
|
|
+ * ★ 调用者必须已持有 hmi_write_lock(hmi_write_va 在锁内直接调它,
|
|
|
+ * Hmi_Batch_End 自己先加锁)—— 内部不再加锁,否则 rt_mutex 非递归会自锁死。 */
|
|
|
+static void hmibatch_flush_locked(void)
|
|
|
+{
|
|
|
+ if (hmi_batch_len > 0)
|
|
|
+ {
|
|
|
+ if (!g_hmi_port_closed && serial != RT_NULL)
|
|
|
+ {
|
|
|
+ hmi_write_all(hmi_batch_buf, hmi_batch_len);
|
|
|
+ }
|
|
|
+ hmi_batch_len = 0;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void Hmi_Batch_End(void)
|
|
|
+{
|
|
|
+ rt_bool_t locked;
|
|
|
+
|
|
|
+ hmi_batch_on = RT_FALSE;
|
|
|
+ hmi_batch_owner = RT_NULL;
|
|
|
+
|
|
|
+ if (hmi_batch_len > 0)
|
|
|
+ {
|
|
|
+ locked = hmi_write_lock();
|
|
|
+ hmibatch_flush_locked();
|
|
|
+ if (locked)
|
|
|
+ {
|
|
|
+ hmi_write_unlock();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/* 访问批量缓冲只允许批量会话的属主线程:其它线程即便在批量期间调用 HMI_WRITE
|
|
|
+ 也直接立即发送,避免两线程同时往同一缓冲追加导致内容错乱。 */
|
|
|
+static rt_bool_t hmi_batch_owned(void)
|
|
|
+{
|
|
|
+ return (hmi_batch_on && hmi_batch_owner == rt_thread_self()) ? RT_TRUE : RT_FALSE;
|
|
|
+}
|
|
|
|
|
|
void list_mem(void);
|
|
|
int mb_master_samlpe();
|
|
|
@@ -65,13 +296,11 @@ struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; /*初始化配置
|
|
|
////
|
|
|
static rt_thread_t hmi1 = RT_NULL;
|
|
|
static rt_thread_t ctr_motor = RT_NULL;
|
|
|
-rt_device_t serial; /*串口设备句柄*/
|
|
|
|
|
|
rt_timer_t timer_uart2 = RT_NULL;
|
|
|
|
|
|
/* 消息队列控制块 */
|
|
|
static struct rt_messagequeue rx_mq;//接收数据消息队列
|
|
|
-static struct rt_semaphore rx_sem_uart4; /* 用于接收消息的信号量 */
|
|
|
|
|
|
static void HMI_Decode(void);
|
|
|
static rt_err_t uart_input(rt_device_t dev, rt_size_t size);
|
|
|
@@ -112,50 +341,188 @@ int HMI_uart4_init(void)
|
|
|
|
|
|
void close_uart4(void)
|
|
|
{
|
|
|
+ /* 先置关闭标志再关设备:rt_device_close() 会释放 serial_v2 的 rx/tx fifo,
|
|
|
+ 之后任何 rt_device_write 都会解引用已释放的 tx_fifo 而 HardFault
|
|
|
+ (设备指针本身仍非空,判空拦不住)。 */
|
|
|
+ g_hmi_port_closed = 1;
|
|
|
rt_device_close(serial);
|
|
|
}
|
|
|
|
|
|
-/*串口屏写数据*/
|
|
|
-void HMI_WRITE(char *fmt, ...)
|
|
|
+/*串口屏写数据
|
|
|
+ * 与 PCB_V2 同一套发送框架:端口关闭早退 → 加锁 → 格式化 → 去重 → 批量/立即发送。
|
|
|
+ * ★ 格式化必须在锁内:str 是文件级静态缓冲,多线程同时 vsnprintf 会把结果
|
|
|
+ * 互相踩掉(原实现就没有任何保护,是"屏幕偶发乱码/半截指令"的根因)。
|
|
|
+ * 注意去重键用的是 fmt 指针(每个调用点唯一),不要在循环里用同一变量拼 fmt。 */
|
|
|
+static void hmi_write_va(char *fmt, va_list ap, rt_bool_t allow_dedupe)
|
|
|
{
|
|
|
- va_list ap;
|
|
|
//末尾需要拼接的3个ff
|
|
|
static const char hmi_hex[3]={0xff,0xff,0xff};
|
|
|
- static char str[MAX_SIZE];
|
|
|
- //memset(str,'\0',sizeof(str));
|
|
|
+ static char str[MAX_SIZE + 3];
|
|
|
rt_size_t str_length;
|
|
|
- va_start(ap,fmt);
|
|
|
+ rt_bool_t locked;
|
|
|
+
|
|
|
+ /* 端口已关闭 / 句柄为空 / fmt 为空:直接返回(见 close_uart4 的说明) */
|
|
|
+ if (g_hmi_port_closed || serial == RT_NULL || fmt == RT_NULL)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ locked = hmi_write_lock();
|
|
|
+
|
|
|
// 预留3字节空间给 hmi_hex
|
|
|
str_length = rt_vsnprintf(str, sizeof(str) - 3, fmt, ap);
|
|
|
- va_end(ap);
|
|
|
- if (str_length > MAX_SIZE - 3)
|
|
|
- str_length = MAX_SIZE - 3;
|
|
|
- // 追加三个 0xff
|
|
|
+
|
|
|
+ if (str_length == 0 || str_length >= sizeof(str) - 3)
|
|
|
+ {
|
|
|
+ if (str_length > 0)
|
|
|
+ {
|
|
|
+ LOG_W("HMI_WRITE: frame too long(%d), dropped", (int)str_length);
|
|
|
+ }
|
|
|
+ goto __exit;
|
|
|
+ }
|
|
|
+
|
|
|
+#if HMI_DEDUPE_EN
|
|
|
+ /* ② 同一调用点内容未变化则不重发(动作类指令必须传 allow_dedupe=RT_FALSE) */
|
|
|
+ if (allow_dedupe && hmi_dedupe_skip(fmt, str, (rt_uint16_t)str_length))
|
|
|
+ {
|
|
|
+ goto __exit;
|
|
|
+ }
|
|
|
+#endif
|
|
|
+
|
|
|
+ /* 结束符与指令合并为一次写入,减少写入次数、避免发送被拆散 */
|
|
|
memcpy(str + str_length, hmi_hex, sizeof(hmi_hex));
|
|
|
- rt_device_write(serial, 0, str, str_length + sizeof(hmi_hex));
|
|
|
- rt_thread_mdelay(5); //2025-0308
|
|
|
+
|
|
|
+ if (hmi_batch_owned())
|
|
|
+ {
|
|
|
+ rt_uint16_t need = (rt_uint16_t)(str_length + 3);
|
|
|
+ if ((rt_uint32_t)hmi_batch_len + need > sizeof(hmi_batch_buf))
|
|
|
+ {
|
|
|
+ hmibatch_flush_locked(); /* 缓冲不足先落地一批(锁已持有,内部不再加锁) */
|
|
|
+ }
|
|
|
+ if ((rt_uint32_t)hmi_batch_len + need <= sizeof(hmi_batch_buf))
|
|
|
+ {
|
|
|
+ rt_memcpy(hmi_batch_buf + hmi_batch_len, str, need);
|
|
|
+ hmi_batch_len += need;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ /* 单条本身就超过批量缓冲(异常情况):直接发,宁可多一次写也不丢指令 */
|
|
|
+ hmi_write_all(str, need);
|
|
|
+ }
|
|
|
+ goto __exit;
|
|
|
+ }
|
|
|
+
|
|
|
+ hmi_write_all(str, (rt_uint16_t)(str_length + 3));
|
|
|
+
|
|
|
+__exit:
|
|
|
+ if (locked)
|
|
|
+ {
|
|
|
+ hmi_write_unlock();
|
|
|
+ }
|
|
|
}
|
|
|
-/* 接收数据回调函数 */
|
|
|
+
|
|
|
+void HMI_WRITE(char *fmt, ...)
|
|
|
+{
|
|
|
+ va_list ap;
|
|
|
+
|
|
|
+ va_start(ap, fmt);
|
|
|
+ hmi_write_va(fmt, ap, RT_TRUE);
|
|
|
+ va_end(ap);
|
|
|
+}
|
|
|
+
|
|
|
+/* 动作类指令(播放提示音 / 切页 / 一次性弹窗):内容重复也必须发。
|
|
|
+ * 为什么单独开一个入口:去重是按"调用点 + 内容"判重的,像 MA_save() 的
|
|
|
+ * `play 1,4,0` 每次参数都相同,若走去重则第二次起会被直接吞掉 ——
|
|
|
+ * 现场表现就是"保存只有第一次响"。PCB_V2 里 MA_save 走的是普通 HMI_WRITE,
|
|
|
+ * 存在同样的隐患,本分支在此修正。 */
|
|
|
+void HMI_WRITE_ACTION(char *fmt, ...)
|
|
|
+{
|
|
|
+ va_list ap;
|
|
|
+
|
|
|
+ va_start(ap, fmt);
|
|
|
+ hmi_write_va(fmt, ap, RT_FALSE);
|
|
|
+ va_end(ap);
|
|
|
+}
|
|
|
+
|
|
|
+/* 接收数据回调函数:只投递消息,实际读取放在 HMI 线程里做
|
|
|
+ * (DMA 完成回调里读串口不安全,逐字节读也会把 CPU 拖死)。 */
|
|
|
static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
|
|
|
{
|
|
|
- /* 串口接收到数据后产生中断,调用此回调函数,然后发送接收信号量 */
|
|
|
- rt_sem_release(&rx_sem_uart4);
|
|
|
- return RT_EOK;
|
|
|
+ struct rx_msg msg;
|
|
|
+ rt_err_t result;
|
|
|
+
|
|
|
+ msg.dev = dev;
|
|
|
+ msg.size = size;
|
|
|
+ result = rt_mq_send(&rx_mq, &msg, sizeof(msg));
|
|
|
+ if (result == -RT_EFULL)
|
|
|
+ {
|
|
|
+ /* 消息队列满:清空队列。宁可丢一次"有新数据"的通知,也不要卡在回调里 */
|
|
|
+ LOG_E("hmi message queue full!");
|
|
|
+ rt_mq_control(&rx_mq, RT_IPC_CMD_RESET, RT_NULL);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
}
|
|
|
|
|
|
+/* ---------- 帧组装 ---------- */
|
|
|
+/* 屏幕一帧的最大长度。原实现用 128 字节累积缓冲,超长帧直接丢弃;
|
|
|
+ 512 与串口 rx_bufsz 一致,屏幕一次性下发多条属性时不会再被截断。 */
|
|
|
+#ifndef HMI_FRAME_MAX
|
|
|
+#define HMI_FRAME_MAX 512
|
|
|
+#endif
|
|
|
+static char hmi_frame[HMI_FRAME_MAX + 1];
|
|
|
+static rt_uint16_t hmi_frame_len = 0;
|
|
|
|
|
|
-/*入口函数*/
|
|
|
-static void Hmi_thread_entry(void *parameter)
|
|
|
+/* 把攒好的一帧交给解析入口。rx_buffer 是 HMI_Decode 的解析缓冲。 */
|
|
|
+static void Hmi_Frame_Handle(void)
|
|
|
+{
|
|
|
+ rt_uint16_t copy_len;
|
|
|
+
|
|
|
+ if (hmi_frame_len == 0)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ hmi_frame[hmi_frame_len] = '\0';
|
|
|
+
|
|
|
+ copy_len = (hmi_frame_len < HMI_UART_RXBUFFER_SIZE) ? hmi_frame_len : HMI_UART_RXBUFFER_SIZE;
|
|
|
+ rt_memcpy(rx_buffer, hmi_frame, copy_len);
|
|
|
+ rx_buffer[copy_len] = '\0';
|
|
|
+ hmi_frame_len = 0;
|
|
|
+
|
|
|
+ HMI_Decode();
|
|
|
+}
|
|
|
+
|
|
|
+/* 喂入一次读到的原始字节流。
|
|
|
+ * 帧尾同时接受 '\r' 和 '\n':本工程屏幕发 "\r",PCB_V2 那套屏发 "\n",
|
|
|
+ * 两种都认可以免换屏/换固件后整机收不到指令(空帧直接丢弃)。 */
|
|
|
+static void Hmi_Rx_Feed(const char *data, rt_uint16_t len)
|
|
|
{
|
|
|
+ rt_uint16_t i;
|
|
|
|
|
|
- static char accum_buf[128]; // 累积缓冲区(根据实际数据量调整)
|
|
|
- static size_t accum_len = 0; // 累积缓冲区当前长度
|
|
|
- char ch;
|
|
|
+ for (i = 0; i < len; i++)
|
|
|
+ {
|
|
|
+ char c = data[i];
|
|
|
|
|
|
- // 【新增】超时计数,防止硬件持续发送不带 \r 的垃圾数据导致缓冲区填满后死锁
|
|
|
- // 如果连续收到 N 个字节都没遇到 \r,强制清空缓冲区
|
|
|
- const size_t MAX_NO_CR_COUNT = sizeof(accum_buf) - 1;
|
|
|
- size_t no_cr_count = 0;
|
|
|
+ if (c == '\r' || c == '\n')
|
|
|
+ {
|
|
|
+ Hmi_Frame_Handle();
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (hmi_frame_len >= HMI_FRAME_MAX)
|
|
|
+ {
|
|
|
+ LOG_W("hmi frame too long(%d), drop!", hmi_frame_len);
|
|
|
+ hmi_frame_len = 0;
|
|
|
+ }
|
|
|
+ hmi_frame[hmi_frame_len++] = c;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/*入口函数*/
|
|
|
+static void Hmi_thread_entry(void *parameter)
|
|
|
+{
|
|
|
+ struct rx_msg msg;
|
|
|
+ rt_err_t result;
|
|
|
+ rt_uint32_t rx_length;
|
|
|
|
|
|
if(device_status.dtype==3){
|
|
|
device_status.collt_box_sta = 0;
|
|
|
@@ -164,56 +531,18 @@ static void Hmi_thread_entry(void *parameter)
|
|
|
device_status.camsta = 1;
|
|
|
while(1)
|
|
|
{
|
|
|
-
|
|
|
- /* 从串口读取一个字节的数据,没有读取到则等待接收信号量 */
|
|
|
- while (rt_device_read(serial, -1, &ch, 1) != 1)
|
|
|
+ rt_memset(&msg, 0, sizeof(msg));
|
|
|
+ /* 从消息队列读通知(50ms 超时:没有数据时也让出 CPU,不再空转) */
|
|
|
+ result = rt_mq_recv(&rx_mq, &msg, sizeof(msg), 50);
|
|
|
+ if (result == RT_EOK)
|
|
|
{
|
|
|
- /* 阻塞等待接收信号量,等到信号量后再次读取数据 */
|
|
|
- rt_sem_take(&rx_sem_uart4, RT_WAITING_FOREVER);
|
|
|
- }
|
|
|
- if(ch=='\r'){
|
|
|
- // 添加字符串结束符,方便后续字符串函数处理
|
|
|
- if (accum_len < sizeof(accum_buf)) {
|
|
|
- accum_buf[accum_len] = '\0';
|
|
|
-
|
|
|
- // 只有当数据长度大于0时才处理(避免处理空的 "\r")
|
|
|
- if (accum_len > 0) {
|
|
|
- // 【重要】如果你的 HMI_Decode() 依赖全局变量 rx_buffer,需要拷贝过去
|
|
|
- // 假设 rx_buffer 足够大
|
|
|
- if (accum_len < sizeof(rx_buffer)) {
|
|
|
- rt_memcpy(rx_buffer, accum_buf, accum_len + 1); // +1 是为了拷贝 '\0'
|
|
|
- HMI_Decode();
|
|
|
- } else {
|
|
|
- LOG_W("Packet too long for global buffer");
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // 重置状态,准备接收下一包
|
|
|
- accum_len = 0;
|
|
|
- no_cr_count = 0;
|
|
|
- }else{
|
|
|
- // --- 普通字符,存入缓冲区 ---
|
|
|
-
|
|
|
- // 【修正点】这里必须把 ch 放入 accum_buf!
|
|
|
- if (accum_len < sizeof(accum_buf) - 1) {
|
|
|
- accum_buf[accum_len] = ch; // <=== 关键赋值语句
|
|
|
- accum_len++;
|
|
|
- no_cr_count++;
|
|
|
- } else {
|
|
|
- // 缓冲区已满仍未遇到 \r,视为异常包(溢出)
|
|
|
- LOG_W("HMI Buffer Overflow! Dropping packet.");
|
|
|
- accum_len = 0; // 清空计数器
|
|
|
- no_cr_count = 0; // 重置异常计数
|
|
|
- // 当前这个 ch 也丢弃,等待下一个包开始
|
|
|
- // 或者你可以选择把 ch 作为新包的第一个字节:accum_buf[0]=ch; accum_len=1;
|
|
|
- }
|
|
|
-
|
|
|
- // 【防御机制】如果连续收到太多字节都没有 \r,强制复位
|
|
|
- if (no_cr_count >= MAX_NO_CR_COUNT) {
|
|
|
- LOG_W("No \\r detected for %d bytes, force reset.", no_cr_count);
|
|
|
- accum_len = 0;
|
|
|
- no_cr_count = 0;
|
|
|
+ /* 一次把 rx fifo 里的数据全读出来(原来逐字节读,syscall 次数是这里的 N 倍) */
|
|
|
+ rx_length = rt_device_read(msg.dev, 0, rx_pkt, HMI_UART_RXBUFFER_SIZE);
|
|
|
+ if (rx_length > 0)
|
|
|
+ {
|
|
|
+ rx_pkt[rx_length] = '\0';
|
|
|
+ LOG_I("hmi rx(%u): %s", (unsigned)rx_length, rx_pkt);
|
|
|
+ Hmi_Rx_Feed(rx_pkt, (rt_uint16_t)rx_length);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -253,6 +582,28 @@ static void HMI_Decode(void)
|
|
|
static char *var;
|
|
|
if(device_status.refresh_num==0) device_status.refresh_num = 10;
|
|
|
if(strstr(rx_buffer,"getFullData") != NULL){
|
|
|
+ /* ★ 换页/唤醒后的强制全量刷新。
|
|
|
+ * 本工程屏幕没有"换页"通知指令(只有周期性的 getFullData),而内容去重会让
|
|
|
+ * 数值未变的控件不再重发 —— 换到新页面后那些控件就一直是空的,要等到
|
|
|
+ * HMI_DEDUPE_RESYNC_SEC(10s) 的周期复位才补上,现场能明显看出来。
|
|
|
+ * 这里按"两次 getFullData 的间隔"判断:屏幕正常停留时约 1s 一次,
|
|
|
+ * 一旦间隔超过 2.5s(换页/熄屏唤醒/刚上电),就复位过滤表强制全量发一次。 */
|
|
|
+ {
|
|
|
+ static rt_tick_t s_last_fulldata_tick = 0;
|
|
|
+ rt_tick_t now_tick = rt_tick_get();
|
|
|
+
|
|
|
+ if (s_last_fulldata_tick == 0 ||
|
|
|
+ (rt_tick_t)(now_tick - s_last_fulldata_tick) > rt_tick_from_millisecond(HMI_FULLDATA_GAP_MS))
|
|
|
+ {
|
|
|
+ hmi_dedupe_reset();
|
|
|
+ }
|
|
|
+ s_last_fulldata_tick = now_tick;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* 整页刷新开始:本分支里约 60 条 HMI_WRITE 先累积到批量缓冲,
|
|
|
+ 末尾 HMI_BATCH_END() 一次写出。仅在 HMI 线程上下文生效,
|
|
|
+ 期间其它线程的 HMI_WRITE 仍走立即发送(见 hmi_batch_owned)。 */
|
|
|
+ HMI_BATCH_BEGIN();
|
|
|
freq_refresh_cb0++;
|
|
|
char *readinfo = makeJson_ReadInfo();
|
|
|
if(device_status.refresh_num==5)
|
|
|
@@ -413,6 +764,9 @@ static void HMI_Decode(void)
|
|
|
HMI_WRITE("workSta.net.val=2");
|
|
|
}
|
|
|
/**********************************************************************/
|
|
|
+ /* 整页刷新结束:把上面累积的指令一次性写出(约 60 条 → 1~3 次串口写)。
|
|
|
+ 本分支内没有提前 return,BEGIN/END 必定成对。 */
|
|
|
+ HMI_BATCH_END();
|
|
|
}
|
|
|
else if (strstr(rx_buffer,"getClockData")!=NULL) {
|
|
|
//更新定时模式
|
|
|
@@ -920,6 +1274,11 @@ static void Ctr_Mortor_entry(void* parameter)
|
|
|
uint8_t tg_motor_running = 0;
|
|
|
while(1)
|
|
|
{
|
|
|
+ /* 去重过滤表的周期复位(本循环 100ms 一拍,故内部按 10 拍=1s 累计):
|
|
|
+ 到 HMI_DEDUPE_RESYNC_SEC 秒整体清表,下一次刷新即全量重发,
|
|
|
+ 保证屏幕不会长期停在某个陈旧值上(屏幕重启/换页也能自愈)。 */
|
|
|
+ hmi_dedupe_tick();
|
|
|
+
|
|
|
#if 1
|
|
|
time_t time_reset12V;
|
|
|
time(&time_reset12V);
|
|
|
@@ -989,10 +1348,22 @@ int Hmi_Fresh(void)
|
|
|
//初始化串口4
|
|
|
//HMI_uart4_init();
|
|
|
rt_err_t ret = RT_EOK;
|
|
|
- /* 初始化信号量 */
|
|
|
- rt_sem_init(&rx_sem_uart4, "rx_sem_uart4", 0, RT_IPC_FLAG_FIFO);
|
|
|
- /* 设置接收回调函数 */
|
|
|
- rt_device_set_rx_indicate(serial, uart_input);
|
|
|
+ /* 初始化消息队列(缓冲区用上面定义的 msg_pool) */
|
|
|
+ rt_mq_init(&rx_mq, "rx_mq",
|
|
|
+ msg_pool, /* 存放消息的缓冲区 */
|
|
|
+ sizeof(struct rx_msg), /* 一条消息的最大长度 */
|
|
|
+ sizeof(msg_pool), /* 存放消息的缓冲区大小 */
|
|
|
+ RT_IPC_FLAG_FIFO); /* 多个线程等待时先来先得到 */
|
|
|
+ /* 设置接收回调函数(serial 可能是 NULL:设备名配错/未注册时 rt_device_find
|
|
|
+ 返回空,这里必须判空,否则直接把空句柄喂给 rt_device_set_rx_indicate) */
|
|
|
+ if (serial != RT_NULL)
|
|
|
+ {
|
|
|
+ rt_device_set_rx_indicate(serial, uart_input);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ LOG_E("%s 未找到,屏幕收发功能不可用!", UART_NAME);
|
|
|
+ }
|
|
|
/* 创建线程*/
|
|
|
hmi1 = rt_thread_create("Hmi_Fresh_thread",
|
|
|
Hmi_thread_entry,
|
|
|
@@ -1368,7 +1739,8 @@ void ResetParamDefault(void)
|
|
|
/* ★ 恢复出厂后必须同步镜像: 镜像只在"iSave 丢失"时兜底,不同步的话
|
|
|
下次开机抢救会把恢复出厂前的旧参数整份搬回来,"恢复出厂"就落空了。 */
|
|
|
param_mirror_sync();
|
|
|
- HMI_WRITE("play %d,%d,%d",1,5,0);
|
|
|
+ /* 提示音属动作类指令,绕过去重(连续两次恢复出厂内容相同,第二次会被吞掉) */
|
|
|
+ HMI_WRITE_ACTION("play %d,%d,%d",1,5,0);
|
|
|
DTU_Write("reset.mp3");
|
|
|
rt_thread_delay(1000);
|
|
|
}
|
|
|
@@ -1434,18 +1806,27 @@ void Debug_log_OFF(void)
|
|
|
rt_console_set_device("uart1");
|
|
|
finsh_set_device("uart1");
|
|
|
|
|
|
- rt_device_set_rx_indicate(serial, uart_input);
|
|
|
- hmi1 = rt_thread_create("Hmi_Fresh_thread",
|
|
|
- Hmi_thread_entry,
|
|
|
- RT_NULL,
|
|
|
- HMI_THREAD_STACK_SIZE,
|
|
|
- HMI_THREAD_PRIORITY,
|
|
|
- HMI_THREAD_TIMESLICE);
|
|
|
- if (hmi1 != RT_NULL)
|
|
|
+ /* ★ 屏幕接收线程在 Hmi_Fresh() 里已经建好了。原实现这里无条件再建一个,
|
|
|
+ 每执行一次 Debug_log_OFF 就多一个 Hmi_thread_entry 抢同一个消息队列:
|
|
|
+ 指令会被两个线程随机瓜分(同一帧只被其中一个处理,看似正常但时序错乱),
|
|
|
+ 日志也会重复。改为"确实还没建过才补建"。 */
|
|
|
+ if (hmi1 == RT_NULL)
|
|
|
{
|
|
|
- rt_thread_startup(hmi1);
|
|
|
+ if (serial != RT_NULL)
|
|
|
+ {
|
|
|
+ rt_device_set_rx_indicate(serial, uart_input);
|
|
|
+ }
|
|
|
+ hmi1 = rt_thread_create("Hmi_Fresh_thread",
|
|
|
+ Hmi_thread_entry,
|
|
|
+ RT_NULL,
|
|
|
+ HMI_THREAD_STACK_SIZE,
|
|
|
+ HMI_THREAD_PRIORITY,
|
|
|
+ HMI_THREAD_TIMESLICE);
|
|
|
+ if (hmi1 != RT_NULL)
|
|
|
+ {
|
|
|
+ rt_thread_startup(hmi1);
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
MSH_CMD_EXPORT(Debug_log_ON,Debug_log_ON);
|
|
|
MSH_CMD_EXPORT(Debug_log_OFF,Debug_log_OFF);
|