/* * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2024-12-04 Administrator the first version */ /* * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2024-09-29 Administrator the first version */ #include #include #include #include "selfcheck.h" #include #include #include "beep.h" #define DBG_ENABLE #define DBG_COLOR #define DBG_TAG "selfcheck" #define DBG_LEVEL DBG_LOG #include //must after of DBG_LVL, DBG_TAG or other options rt_timer_t timer_takephoto = RT_NULL; Device_Status_typedef device_status; static int calculate_average(int arr[], int n) { int sum = 0; // 用于存储数组元素的总和 for (int i = 0; i < n; i++) { sum += arr[i]; // 累加每个元素 } // 计算平均值 int average = sum / n; // 强制类型转换确保得到浮点数结果 return average; } static void t_takephoto_callback(void *parameter) { device_status.C01 |= (1<<2); device_status.C01 |= (1<<3); HMI_WRITE("selfcheck.t3.bco=63488"); HMI_WRITE("selfcheck.t4.bco=63488"); HMI_WRITE("sysSet.va0.txt=\"拍照超时\""); LOG_E("t_takephoto_callback"); } int selfcheck(uint8_t type) { uint16_t C01 = 0; int current_i[6]; int current_,current_open = 0; //删除workloop线程 防止自动运行干扰 LOG_I("删除线程workloop:%d",thread_delete_workloop()); //初始化硬件 HardWare_Init(0); /********************************************************/ //读取5次电流值,取平均值 for(int i = 0;i<5;i++) { current_i[i] = atoi(device_status.power); rt_thread_delay(1000); } current_ = calculate_average(current_i,5); if(current_<50||current_>3000) { device_status.C01 |= (1<<12); HMI_WRITE("selfcheck.t13.bco=63488"); } else { HMI_WRITE("selfcheck.t13.bco=2024"); device_status.C01 &= ~(1<<12); } if(device_status.bat_status==0) { HMI_WRITE("selfcheck.t5.bco=2024"); device_status.C01 &= ~(1<<4); } else { HMI_WRITE("selfcheck.t5.bco=63488"); device_status.C01 |= (1<<4); } if(device_status.rain_status) { HMI_WRITE("selfcheck.t6.bco=63488"); device_status.C01 |= (1<<5); } else { device_status.C01 &= ~(1<<5); HMI_WRITE("selfcheck.t6.bco=2024"); } if(device_status.temp_status) { HMI_WRITE("selfcheck.t8.bco=63488"); device_status.C01 |= (1<<7); } else { device_status.C01 &= ~(1<<7); HMI_WRITE("selfcheck.t8.bco=2024"); } if(device_status.sun_status) { HMI_WRITE("selfcheck.t7.bco=63488"); device_status.C01 |= (1<<6); } else { device_status.C01 &= ~(1<<6); HMI_WRITE("selfcheck.t7.bco=2024"); } rt_thread_delay(1000); /********************************************************/ ctr_lamp(ON, 1); //打开镇流器 rt_thread_delay(1000); for(int i = 0;i<5;i++) { current_i[i] = atoi(device_status.power); rt_thread_delay(1000); } ctr_lamp(OFF, 1); //关闭镇流器 current_open = calculate_average(current_i,5); LOG_I("待机电流:%d",current_); LOG_I("镇流器开启电流:%d",current_open); if(current_open-current_>100) { HMI_WRITE("selfcheck.t1.bco=2024"); device_status.C01 &= ~(1<<0); HMI_WRITE("sysSet.va0.txt=\"灯管正常\""); } else { HMI_WRITE("selfcheck.t1.bco=63488"); device_status.C01 |= (1<<0); HMI_WRITE("sysSet.va0.txt=\"灯管异常\""); } rt_thread_delay(1000); /********************************************************/ //打开上仓门 ctr_cangmen_up(OPEN); for(int i = 0;i<5;i++) { current_i[i] = atoi(device_status.power); rt_thread_delay(1000); } ctr_cangmen_up(OFF); current_open = calculate_average(current_i,5); if(current_open-current_>100) { HMI_WRITE("selfcheck.t9.bco=2024"); device_status.C01 &= ~(1<<8); HMI_WRITE("sysSet.va0.txt=\"上仓门正常\""); } else{ HMI_WRITE("selfcheck.t9.bco=63488"); device_status.C01 |= (1<<8); HMI_WRITE("sysSet.va0.txt=\"上仓门故障\""); } rt_thread_delay(1000); /********************************************************/ //打开下仓门 ctr_cangmen_down(OPEN); for(int i = 0;i<5;i++) { current_i[i] = atoi(device_status.power); rt_thread_delay(1000); } ctr_cangmen_down(OFF); current_open = calculate_average(current_i,5); if(current_open-current_>100) { HMI_WRITE("selfcheck.t10.bco=2024"); device_status.C01 &= ~(1<<9); HMI_WRITE("sysSet.va0.txt=\"下仓门正常\""); } else{ HMI_WRITE("selfcheck.t10.bco=63488"); device_status.C01 |= (1<<9); HMI_WRITE("sysSet.va0.txt=\"下仓门故障\""); } rt_thread_delay(1000); /********************************************************/ //记录当前电压、温度探头温度 float voltage_off = atof(device_status.voltage); uint8_t heat_temp_off = device_status.heattem; HMI_WRITE("sysSet.va0.txt=\"当前电压:%s-温度:%d\"",device_status.voltage,heat_temp_off); //开启加热 rt_thread_delay(1000); /* 注意:原实现用“电压是否在50~150之间”判断加热是否已工作,该条件恒不成立(死代码)。 * 这里改为始终执行加热测试:加热60s后,温度上升≥2℃ 或 电压跌落≥1.5V 视为加热回路正常。 */ ctr_Heat(ON); //持续1分钟 for(int i = 60;i>0;i--) { HMI_WRITE("sysSet.va0.txt=\"加热测试中...%d\"",i); rt_thread_delay(1000); } LOG_I("当前温度:%d电压:%s",device_status.heatvalue,device_status.voltage); //比较当前电压、温度探头温度和开启前(注意使用有符号比较,避免无符号回绕误判为正常) if(((int)device_status.heattem - (int)heat_temp_off)>=2 || (atof(device_status.voltage) - voltage_off)>=1.5) { HMI_WRITE("selfcheck.t2.bco=2024"); device_status.C01 &= ~(1<<1); HMI_WRITE("sysSet.va0.txt=\"加热正常\""); } else { HMI_WRITE("selfcheck.t2.bco=63488"); device_status.C01 |= (1<<1); HMI_WRITE("sysSet.va0.txt=\"加热故障\""); } ctr_Heat(OFF); rt_thread_delay(1000); /*********************************************************/ HMI_WRITE("sysSet.va0.txt=\"开始拍照\""); rt_timer_start(timer_takephoto); HK_TakePhoto(); for(int i = 20;i>0;i--) { HMI_WRITE("sysSet.va0.txt=\"拍照中...%d\"",i); rt_thread_delay(1000); } rt_thread_delay(1000); /*******************************************************/ /* 清扫测试:自检开始时清扫线程已被删除(device_status.clear_status 无人消费), * 原来仅置位 clear_status 并不会真正执行清扫。这里在自检流程内直接驱动 * 清扫电机,并在运行过程中采样电流,与待机电流比较判定。 */ HMI_WRITE("sysSet.va0.txt=\"清扫测试中...\""); #if PWM_EN == 0 rt_pin_mode(CLEAR_R, PIN_MODE_OUTPUT); rt_pin_mode(CLEAR_L, PIN_MODE_OUTPUT); rt_pin_write(CLEAR_R, ON); rt_pin_write(CLEAR_L, OFF); #else Motor_Qingsao_R_PWM(ON); Motor_Qingsao_L_PWM(OFF); #endif rt_thread_delay(2000); for(int i = 0;i<5;i++) { current_i[i] = atoi(device_status.power); rt_thread_delay(1000); } current_open = calculate_average(current_i,5); #if PWM_EN == 0 rt_pin_write(CLEAR_R, ON); rt_pin_write(CLEAR_L, ON); #else Motor_Qingsao_R_PWM(OFF); Motor_Qingsao_L_PWM(OFF); #endif rt_thread_delay(1000); /* 与待机电流比较(判据与灯管/仓门一致) */ if(current_open-current_>150) { HMI_WRITE("selfcheck.t11.bco=2024"); device_status.C01 &= ~(1<<10); HMI_WRITE("sysSet.va0.txt=\"清扫正常\""); } else { HMI_WRITE("selfcheck.t11.bco=63488"); device_status.C01 |= (1<<10); HMI_WRITE("sysSet.va0.txt=\"清扫故障\""); } device_status.clear_status = 0; /* 自检自行完成清扫,清除标志防止残留触发 */ /*******************************************************/ HMI_WRITE("sysSet.va0.txt=\"故障码:%d\"",device_status.C01); char *js = makeJson_C01_Warn(device_status.C01,1.0); DTU_Write("%s",js); free(js); LOG_I("重启线程workloop:%d",thread_restart_workloop()); device_status.selfcheck_ws = 0; device_status.process = 0.0; } int rt_software_timer_init(void) { //启动拍照超时定时器 /* 必须使用软件定时器:回调中会调用 HMI_WRITE/LOG 等阻塞接口,不能运行在中断上下文 */ timer_takephoto = rt_timer_create("t_takephoto",t_takephoto_callback,RT_NULL, 100000, RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_SOFT_TIMER); if(timer_takephoto==RT_NULL) { LOG_E("rt_timer_create t_takephoto failed!"); return RT_ERROR; } else { return RT_EOK; } } INIT_APP_EXPORT(rt_software_timer_init); /* ==================== 串口自环回自检(自动化测试用) ==================== * 前置:目标串口 TX-RX 短接(自环回)。 * 用途:一次调用即可验证 驱动→DMA接收→IDLE中断→FIFO→读 的完整收发链路, * 排除应用层线程/消息队列的干扰,用于区分"接线问题"与"固件接收路径问题"。 * 用法:uart_loop_test <2|4> [len] len 默认 64,最大 2048(uart4 限制 512) * 说明:测试期间临时接管该串口的接收回调(close→open→换回调),结束后恢复应用侧 * (uart2: dtu_uart2_reinit_rx / uart4: hmi_uart4_reinit_rx)。 */ #define LP_MAX_LEN 2048 static rt_uint8_t lp_tx[LP_MAX_LEN]; static rt_uint8_t lp_rx[LP_MAX_LEN + 64]; static struct rt_semaphore lp_sem; static rt_bool_t lp_sem_ready = RT_FALSE; static rt_err_t lp_rx_ind(rt_device_t dev, rt_size_t size) { rt_sem_release(&lp_sem); return RT_EOK; } /* 恢复应用侧接收回调。 * 注意:这里刻意【不做 close/open】。本驱动的 close/open 会调 HAL_UART_Receive_DMA * 重新武装接收,一旦失败(例如 HAL 的 RxState 未复位导致返回 HAL_BUSY), * 该串口的接收链路就会永久失效直到重启。仅重新注册 rx_indicate 即可达到 * "把接收权交还应用"的目的,对硬件状态零扰动。 */ static void lp_resume_app(rt_device_t dev) { if (dev == dtuserial) { dtu_uart2_reinit_rx(); } else { hmi_uart4_reinit_rx(); } } static void uart_loop_test(int argc, char **argv) { rt_device_t dev; const char *port; int len = 64; int i, ok = 0, hdr_ok = 0; rt_uint32_t got = 0, t0, timeout_ms; char drain[64]; if (argc < 2) { rt_kprintf("usage: uart_loop_test <2|4> [len<=%d]\n", LP_MAX_LEN); return; } if (argv[1][0] == '2') { dev = dtuserial; port = "uart2"; } else if (argv[1][0] == '4') { dev = serial; port = "uart4"; } else { rt_kprintf("[LP] invalid port(arg=%s)\n", argv[1]); return; } if (dev == RT_NULL) { rt_kprintf("[LP %s] device handle is NULL\n", port); return; } if (argc >= 3) { len = atoi(argv[2]); } if (len < 4) len = 4; if (len > LP_MAX_LEN) len = LP_MAX_LEN; if (dev == serial && len > 512) { len = 512; /* uart4 接收缓冲 512B,避免回绕丢数据 */ rt_kprintf("[LP uart4] len clamped to 512\n"); } if (!lp_sem_ready) { rt_sem_init(&lp_sem, "lp_sem", 0, RT_IPC_FLAG_FIFO); lp_sem_ready = RT_TRUE; } /* 图案:55 AA + 长度 + 递增异或值,便于在被其它串口流量混入时定位 */ rt_memset(lp_tx, 0, sizeof(lp_tx)); for (i = 4; i < len; i++) { lp_tx[i] = (rt_uint8_t)(0xA5 ^ i); } lp_tx[0] = 0x55; lp_tx[1] = 0xAA; lp_tx[2] = (rt_uint8_t)(len & 0xFF); lp_tx[3] = (rt_uint8_t)((len >> 8) & 0xFF); rt_memset(lp_rx, 0, sizeof(lp_rx)); /* ---- 接管端口(只换回调,不做 close/open)---- * 只替换 rx_indicate:应用接收线程收不到信号量就不会来抢数据。 * 应用线程此前已把 FIFO 读空并阻塞在信号量上,我们保留这段等待以确保它进入阻塞。 * 刻意避开 close/open:该路径会重新武装 DMA,历史上曾因 HAL RxState 未复位 * 而静默失败,导致该串口接收永久失效(详见 lp_resume_app 注释)。 */ rt_device_set_rx_indicate(dev, lp_rx_ind); rt_thread_mdelay(120); /* 清掉在途残留,避免把上次的尾巴当成回环数据 */ while (rt_device_read(dev, 0, drain, sizeof(drain)) > 0) { } /* ---- 发送 ---- */ t0 = rt_tick_get(); if (rt_device_write(dev, 0, lp_tx, len) != (rt_size_t)len) { rt_kprintf("[LP %s] FAIL: write error\n", port); lp_resume_app(dev); return; } /* 读回:超时 = 10bit/字节@115200 + 200ms 余量 */ timeout_ms = (rt_uint32_t)(((rt_uint32_t)len * 10UL * 1000UL) / 115200UL + 200UL); while (got < (rt_uint32_t)len && (rt_tick_get() - t0) < timeout_ms) { rt_size_t n = rt_device_read(dev, 0, lp_rx + got, (rt_size_t)(len - (int)got)); if (n > 0) { got += n; continue; } rt_sem_take(&lp_sem, 20); } /* ---- 校验:允许混入其它串口流量,按图案子串匹配 ---- */ for (i = 0; i + len <= (int)got; i++) { if (rt_memcmp(lp_rx + i, lp_tx, len) == 0) { ok = 1; break; } } /* 次判据:整帧未匹配上时,若能找到 4 字节报文头(55 AA len_lo len_hi), * 说明收发链路是通的,只是被应用线程/其它流量抢走了部分字节。 */ if (!ok && got >= 4) { for (i = 0; i + 4 <= (int)got; i++) { if (rt_memcmp(lp_rx + i, lp_tx, 4) == 0) { hdr_ok = 1; break; } } } rt_kprintf("[LP %s] len=%d recv=%d t=%dms %s\n", port, len, (int)got, (int)(rt_tick_get() - t0), ok ? "PASS" : (hdr_ok ? "PASS(仅报文头)" : "FAIL")); if (!ok && hdr_ok) { rt_kprintf("[LP %s] 找到报文头但整帧不全:链路通,部分字节被应用线程取走\n", port); } if (!ok && !hdr_ok) { if (got > 0) { rt_kprintf("[LP %s] head:", port); for (i = 0; i < ((int)got < 16 ? (int)got : 16); i++) { rt_kprintf(" %02x", lp_rx[i]); } rt_kprintf("\n[LP %s] 收到数据但与图案不符(可能有其它流量混入或丢字节)\n", port); } else { rt_kprintf("[LP %s] 未收到任何数据:请检查 TX-RX 是否短接、DMA/中断配置\n", port); } } /* 清空测试残留:否则这些图案字节会被应用解析器当成非法报文 */ while (rt_device_read(dev, 0, drain, sizeof(drain)) > 0) { } lp_resume_app(dev); } MSH_CMD_EXPORT(uart_loop_test, uart_loop_test <2|4> [len] : UART TX-RX loopback self test);