protect.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. * Protection Detection Module - v1.4 (Fixed timing issues)
  3. *
  4. * 功能:统一保护检测线程,实现欠压保护、温控保护
  5. *
  6. * 设计原则:
  7. * - 保护检测线程基础时间单位100ms
  8. * - 各保护模块独立计数,互不干扰
  9. * - 定时修复:使用毫秒计时,确保触发时间准确(60秒 = 60000ms)
  10. *
  11. * Version: 1.4.0 (Lamp protection removed)
  12. */
  13. /*********************↓ include ↓************************/
  14. #define DBG_TAG "PROTECT"
  15. #define DBG_LVL DBG_INFO
  16. #include <rtdbg.h>
  17. #include <rtthread.h>
  18. #include <rtdevice.h>
  19. #include <board.h>
  20. #include <string.h>
  21. #include "ecode.h"
  22. #include "ym310_protecl.h"
  23. /*********************↑ include ↑************************/
  24. /*********************↓ define ↓************************/
  25. /* 保护线程基础时间片(毫秒) */
  26. #define PROTECT_BASE_MS 100
  27. /* 欠压保护参数(毫秒:60秒 = 60000ms) */
  28. #define UVP_TRIGGER_MS 60000
  29. #define UVP_RECOVER_MS 60000
  30. #define UVP_PRE_TRIGGER_MS 10000
  31. /* 温控保护参数(毫秒:60秒 = 60000ms) */
  32. #define TEMP_TRIGGER_MS 60000
  33. #define TEMP_RECOVER_MS 60000
  34. /* 保护状态值定义 */
  35. #define PROTECT_NONE 0
  36. #define PROTECT_LOW_TEMP 0x7A
  37. #define PROTECT_HIGH_TEMP 0x9D
  38. #define PROTECT_UNDER_VOLT 0x9D
  39. /*********************↑ define ↑************************/
  40. /*********************↓ 外部变量声明 ↓***********************/
  41. extern SYS_STATUS sys_sta;
  42. extern LIMIT limit;
  43. extern DEV_INFO dev_info;
  44. extern void save_dev_info(void);
  45. extern void report_now(void);
  46. /*********************↑ 外部变量声明 ↑***********************/
  47. /*********************↓ 静态变量 ↓************************/
  48. static rt_thread_t s_protect_thread = RT_NULL;
  49. /* 欠压保护状态机 */
  50. static struct
  51. {
  52. uint32_t low_voltage_start_ms; /* 电压过低开始时间(毫秒) */
  53. uint32_t normal_voltage_start_ms; /* 电压正常开始时间(毫秒) */
  54. uint8_t pre_protect;
  55. } s_uvp = {0};
  56. /* 温控保护状态机 */
  57. static struct
  58. {
  59. uint32_t high_temp_start_ms; /* 高温开始时间(毫秒) */
  60. uint32_t low_temp_start_ms; /* 低温开始时间(毫秒) */
  61. uint32_t normal_temp_start_ms; /* 温度正常开始时间(毫秒) */
  62. uint8_t protect_type;
  63. } s_temp = {0};
  64. /*********************↑ 静态变量 ↑************************/
  65. /*********************↓ 欠压保护模块 ↓***********************/
  66. static void uvp_detect(uint16_t voltage_mv)
  67. {
  68. uint32_t now = rt_tick_get_millisecond();
  69. char buf[50] = {0};
  70. if (sys_sta.undervolt_protect == PROTECT_NONE)
  71. {
  72. if (voltage_mv < limit.uvon)
  73. {
  74. if (s_uvp.low_voltage_start_ms == 0)
  75. {
  76. s_uvp.low_voltage_start_ms = now;
  77. s_uvp.normal_voltage_start_ms = 0;
  78. LOG_D("[UVP] Voltage low (%dmV < %dmV), start counting", voltage_mv, limit.uvon);
  79. }
  80. uint32_t elapsed = now - s_uvp.low_voltage_start_ms;
  81. if (elapsed >= UVP_PRE_TRIGGER_MS && !s_uvp.pre_protect)
  82. {
  83. s_uvp.pre_protect = 1;
  84. LOG_W("[UVP] Pre-protect, voltage=%dmV < %dmV for %lu ms", voltage_mv, limit.uvon, elapsed);
  85. }
  86. if (elapsed >= UVP_TRIGGER_MS)
  87. {
  88. sys_sta.undervolt_protect = PROTECT_UNDER_VOLT;
  89. s_uvp.normal_voltage_start_ms = 0;
  90. LOG_W("[UVP] Under-voltage protection activated, voltage=%dmV, lasted %lu ms", voltage_mv, elapsed);
  91. sprintf(buf ,"欠压保护触发(%dmV < %dmV)" , voltage_mv, limit.uvon);
  92. Recode_e_code(e_protect, 1, buf);
  93. rt_thread_mdelay(1000);
  94. report_now();
  95. }
  96. }
  97. else
  98. {
  99. s_uvp.low_voltage_start_ms = 0;
  100. s_uvp.pre_protect = 0;
  101. }
  102. }
  103. else if (sys_sta.undervolt_protect == PROTECT_UNDER_VOLT)
  104. {
  105. if (voltage_mv > limit.uvoff)
  106. {
  107. if (s_uvp.normal_voltage_start_ms == 0)
  108. {
  109. s_uvp.normal_voltage_start_ms = now;
  110. LOG_D("[UVP] Voltage normal (%dmV > %dmV), start recovery count", voltage_mv, limit.uvoff);
  111. }
  112. uint32_t elapsed = now - s_uvp.normal_voltage_start_ms;
  113. if (elapsed >= UVP_RECOVER_MS)
  114. {
  115. sys_sta.undervolt_protect = PROTECT_NONE;
  116. s_uvp.low_voltage_start_ms = 0;
  117. s_uvp.pre_protect = 0;
  118. LOG_I("[UVP] Under-voltage protection cleared, voltage=%dmV, stable for %lu ms", voltage_mv, elapsed);
  119. sprintf(buf ,"欠压保护解除(%dmV > %dmV)" , voltage_mv, limit.uvoff);
  120. Recode_e_code(e_protect, 0, buf);
  121. rt_thread_mdelay(1000);
  122. report_now();
  123. }
  124. }
  125. else
  126. {
  127. s_uvp.normal_voltage_start_ms = 0;
  128. }
  129. }
  130. }
  131. /*********************↑ 欠压保护模块 ↑***********************/
  132. /*********************↓ 温控保护模块 ↓***********************/
  133. /* 温度源切换防抖时间(毫秒) */
  134. #define TEMP_SOURCE_STABLE_MS 5000 /* 5秒内稳定才切换 */
  135. static void temp_detect(void)
  136. {
  137. uint32_t now = rt_tick_get_millisecond();
  138. uint8_t is_high_temp = 0;
  139. uint8_t is_low_temp = 0;
  140. /* 静态变量:保存当前源和切换计时 */
  141. static uint8_t s_current_source = 0; /* 当前使用的温度源 */
  142. static uint8_t s_pending_source = 0; /* 待切换的源 */
  143. static uint32_t s_pending_start_ms = 0; /* 待切换开始时间 */
  144. /* ========== 1. 确定当前可用温度源 ========== */
  145. uint8_t available_source = 0;
  146. int16_t raw_temp = 0;
  147. /* 优先级:SHT30 > NTC > PLAT > 默认值 */
  148. if (sensor.valid_sht == VALID)
  149. {
  150. available_source = 1;
  151. raw_temp = sensor.temp_sht;
  152. }
  153. else if (sensor.valid_ntc == VALID)
  154. {
  155. available_source = 2;
  156. raw_temp = sensor.temp_ntc;
  157. }
  158. else if (sensor.valid_plat == VALID)
  159. {
  160. available_source = 3;
  161. raw_temp = sensor.temp_plat;
  162. }
  163. else
  164. {
  165. available_source = 4;
  166. raw_temp = 220; /* 默认22.0°C */
  167. }
  168. /* ========== 2. 源切换防抖逻辑 ========== */
  169. if (available_source != s_current_source)
  170. {
  171. /* 检测到源变化 */
  172. if (available_source != s_pending_source)
  173. {
  174. /* 新的源变化,重新开始计时 */
  175. s_pending_source = available_source;
  176. s_pending_start_ms = now;
  177. LOG_D("[TEMP] Source change pending: %d -> %d",
  178. s_current_source, s_pending_source);
  179. }
  180. else if ((now - s_pending_start_ms) >= TEMP_SOURCE_STABLE_MS)
  181. {
  182. /* 稳定时间达到,执行切换 */
  183. s_current_source = s_pending_source;
  184. LOG_I("[TEMP] Source switched: %d", s_current_source);
  185. s_pending_source = 0;
  186. s_pending_start_ms = 0;
  187. }
  188. }
  189. else
  190. {
  191. /* 源未变化,清除待切换状态 */
  192. s_pending_source = 0;
  193. s_pending_start_ms = 0;
  194. }
  195. /* ========== 3. 获取最终温度值 ========== */
  196. if (s_current_source == 0)
  197. {
  198. /* 首次运行,直接使用当前可用源 */
  199. s_current_source = available_source;
  200. LOG_I("[TEMP] Initial source: %d", s_current_source);
  201. }
  202. /* 根据情况获取温度值 */
  203. int16_t temp_value = 0;
  204. if (s_pending_source != 0 && (now - s_pending_start_ms) < TEMP_SOURCE_STABLE_MS)
  205. {
  206. /* 切换期间:使用新源的数据,避免使用已失效的旧源 */
  207. temp_value = raw_temp;
  208. LOG_D("[TEMP] Switch in progress, using source %d temporarily", available_source);
  209. }
  210. else
  211. {
  212. /* 正常情况:根据最终确定的源获取温度 */
  213. switch (s_current_source)
  214. {
  215. case 1:
  216. temp_value = sensor.temp_sht;
  217. break;
  218. case 2:
  219. temp_value = sensor.temp_ntc;
  220. break;
  221. case 3:
  222. temp_value = sensor.temp_plat;
  223. break;
  224. case 4:
  225. temp_value = 220;
  226. break;
  227. default:
  228. temp_value = 220;
  229. break;
  230. }
  231. }
  232. sys_sta.board_temp = temp_value;
  233. /* 可选:每10秒打印一次当前状态(调试用)*/
  234. static uint32_t last_log_ms = 0;
  235. if ((now - last_log_ms) >= 10000)
  236. {
  237. last_log_ms = now;
  238. LOG_D("[TEMP] Source=%d%s, Temp=%d.%d°C",
  239. s_current_source,
  240. (s_pending_source != 0) ? "(switching)" : "",
  241. sys_sta.board_temp / 10,
  242. sys_sta.board_temp % 10);
  243. }
  244. /* ========== 4. 原有的温控保护判断逻辑(完全不变) ========== */
  245. if (s_temp.protect_type == PROTECT_NONE)
  246. {
  247. if (sys_sta.board_temp > limit.overtemp)
  248. {
  249. is_high_temp = 1;
  250. }
  251. else if (sys_sta.board_temp < limit.lowtemp)
  252. {
  253. is_low_temp = 1;
  254. }
  255. if (is_high_temp)
  256. {
  257. if (s_temp.high_temp_start_ms == 0)
  258. {
  259. s_temp.high_temp_start_ms = now;
  260. s_temp.low_temp_start_ms = 0;
  261. LOG_D("[TEMP] High temp (%d.%d°C > %d.%d°C), start counting",
  262. sys_sta.board_temp / 10, sys_sta.board_temp % 10,
  263. limit.overtemp / 10, limit.overtemp % 10);
  264. }
  265. uint32_t elapsed = now - s_temp.high_temp_start_ms;
  266. if (elapsed >= TEMP_TRIGGER_MS)
  267. {
  268. s_temp.protect_type = PROTECT_HIGH_TEMP;
  269. sys_sta.temp_protect = PROTECT_HIGH_TEMP;
  270. LOG_W("[TEMP] High-temperature protection, temp=%d.%d°C, lasted %lu ms",
  271. sys_sta.board_temp / 10, sys_sta.board_temp % 10, elapsed);
  272. Recode_e_code(e_protect, 2, "高温保护触发");
  273. rt_thread_mdelay(1000);
  274. report_now();
  275. }
  276. }
  277. else if (is_low_temp)
  278. {
  279. if (s_temp.low_temp_start_ms == 0)
  280. {
  281. s_temp.low_temp_start_ms = now;
  282. s_temp.high_temp_start_ms = 0;
  283. LOG_D("[TEMP] Low temp (%d.%d°C < %d.%d°C), start counting",
  284. sys_sta.board_temp / 10, sys_sta.board_temp % 10,
  285. limit.lowtemp / 10, limit.lowtemp % 10);
  286. }
  287. uint32_t elapsed = now - s_temp.low_temp_start_ms;
  288. if (elapsed >= TEMP_TRIGGER_MS)
  289. {
  290. s_temp.protect_type = PROTECT_LOW_TEMP;
  291. sys_sta.temp_protect = PROTECT_LOW_TEMP;
  292. LOG_W("[TEMP] Low-temperature protection, temp=%d.%d°C, lasted %lu ms",
  293. sys_sta.board_temp / 10, sys_sta.board_temp % 10, elapsed);
  294. Recode_e_code(e_protect, 3, "低温保护触发");
  295. rt_thread_mdelay(1000);
  296. report_now();
  297. }
  298. }
  299. else
  300. {
  301. s_temp.high_temp_start_ms = 0;
  302. s_temp.low_temp_start_ms = 0;
  303. }
  304. }
  305. else if (s_temp.protect_type != PROTECT_NONE)
  306. {
  307. uint8_t is_recovered = 0;
  308. if (s_temp.protect_type == PROTECT_HIGH_TEMP)
  309. {
  310. if (sys_sta.board_temp < limit.unovert)
  311. is_recovered = 1;
  312. }
  313. else if (s_temp.protect_type == PROTECT_LOW_TEMP)
  314. {
  315. if (sys_sta.board_temp > limit.unlowt)
  316. is_recovered = 1;
  317. }
  318. if (is_recovered)
  319. {
  320. if (s_temp.normal_temp_start_ms == 0)
  321. {
  322. s_temp.normal_temp_start_ms = now;
  323. LOG_D("[TEMP] Temperature recovered, start recovery count");
  324. }
  325. uint32_t elapsed = now - s_temp.normal_temp_start_ms;
  326. if (elapsed >= TEMP_RECOVER_MS)
  327. {
  328. LOG_I("[TEMP] Temperature protection cleared, temp=%d.%d°C, stable for %lu ms",
  329. sys_sta.board_temp / 10, sys_sta.board_temp % 10, elapsed);
  330. Recode_e_code(e_protect, 0,
  331. (s_temp.protect_type == PROTECT_HIGH_TEMP) ? "高温保护解除" : "低温保护解除");
  332. s_temp.protect_type = PROTECT_NONE;
  333. sys_sta.temp_protect = PROTECT_NONE;
  334. s_temp.high_temp_start_ms = 0;
  335. s_temp.low_temp_start_ms = 0;
  336. s_temp.normal_temp_start_ms = 0;
  337. report_now();
  338. }
  339. }
  340. else
  341. {
  342. s_temp.normal_temp_start_ms = 0;
  343. }
  344. }
  345. }
  346. /*********************↑ 温控保护模块 ↑***********************/
  347. /*********************↓ 主保护线程(100ms) ↓***********************/
  348. static void protect_thread_entry(void *param)
  349. {
  350. // LOG_I("[PROTECT] Protection monitor thread started (base=%dms)", PROTECT_BASE_MS);
  351. while (1)
  352. {
  353. uvp_detect(sys_sta.battery_voltage);
  354. temp_detect();
  355. static uint32_t debug_counter = 0;
  356. debug_counter++;
  357. if (debug_counter >= 100) /* 100 * 100ms = 10秒 */
  358. {
  359. debug_counter = 0;
  360. LOG_D("[PROTECT] UVP: state=%d, pre=%d | TEMP: type=%d",
  361. sys_sta.undervolt_protect, s_uvp.pre_protect,
  362. s_temp.protect_type);
  363. }
  364. rt_thread_mdelay(PROTECT_BASE_MS);
  365. }
  366. }
  367. static void protect_monitor_start(void)
  368. {
  369. if (s_protect_thread != RT_NULL)
  370. {
  371. LOG_D("[PROTECT] Already running");
  372. return;
  373. }
  374. memset(&s_uvp, 0, sizeof(s_uvp));
  375. memset(&s_temp, 0, sizeof(s_temp));
  376. s_protect_thread = rt_thread_create("protect", protect_thread_entry, NULL,
  377. 2048, 12, 10);
  378. if (s_protect_thread == RT_NULL)
  379. {
  380. LOG_E("[PROTECT] Failed to create thread");
  381. return;
  382. }
  383. rt_thread_startup(s_protect_thread);
  384. // LOG_I("[PROTECT] Protection monitor started");
  385. }
  386. /*********************↑ 主保护线程 ↑***********************/
  387. /*********************↓ 自动初始化 ↓***********************/
  388. static int protect_auto_init(void)
  389. {
  390. protect_monitor_start();
  391. return 0;
  392. }
  393. INIT_APP_EXPORT(protect_auto_init);
  394. /*********************↑ 自动初始化 ↑***********************/
  395. /*********************↓ MSH命令 ↓***********************/
  396. static void cmd_protect_status(int argc, char **argv)
  397. {
  398. (void)argc;
  399. (void)argv;
  400. uint32_t now = rt_tick_get_millisecond();
  401. rt_kprintf("\n========== PROTECTION STATUS ==========\n");
  402. rt_kprintf("[UVP] Status: ");
  403. if (sys_sta.undervolt_protect == PROTECT_UNDER_VOLT)
  404. rt_kprintf("ACTIVE\n");
  405. else if (s_uvp.pre_protect)
  406. rt_kprintf("PRE-ACTIVE\n");
  407. else if (s_uvp.low_voltage_start_ms != 0)
  408. {
  409. uint32_t elapsed = now - s_uvp.low_voltage_start_ms;
  410. rt_kprintf("COUNTING (%lu/%lu ms)\n", elapsed, UVP_TRIGGER_MS);
  411. }
  412. else
  413. rt_kprintf("NORMAL\n");
  414. rt_kprintf(" Voltage: %dmV, UVON=%dmV, UVOFF=%dmV\n",
  415. sys_sta.battery_voltage, limit.uvon, limit.uvoff);
  416. rt_kprintf("[TEMP] Status: ");
  417. if (s_temp.protect_type == PROTECT_HIGH_TEMP)
  418. rt_kprintf("HIGH-TEMP PROTECT\n");
  419. else if (s_temp.protect_type == PROTECT_LOW_TEMP)
  420. rt_kprintf("LOW-TEMP PROTECT\n");
  421. else if (s_temp.high_temp_start_ms != 0)
  422. {
  423. uint32_t elapsed = now - s_temp.high_temp_start_ms;
  424. rt_kprintf("HIGH COUNTING (%lu/%lu ms)\n", elapsed, TEMP_TRIGGER_MS);
  425. }
  426. else if (s_temp.low_temp_start_ms != 0)
  427. {
  428. uint32_t elapsed = now - s_temp.low_temp_start_ms;
  429. rt_kprintf("LOW COUNTING (%lu/%lu ms)\n", elapsed, TEMP_TRIGGER_MS);
  430. }
  431. else
  432. rt_kprintf("NORMAL\n");
  433. rt_kprintf(" Temp: %d.%d°C\n", sys_sta.board_temp / 10, sys_sta.board_temp % 10);
  434. rt_kprintf("======================================\n");
  435. }
  436. MSH_CMD_EXPORT(cmd_protect_status, Show protection status);
  437. static void cmd_protect_clear(int argc, char **argv)
  438. {
  439. if (argc < 2)
  440. {
  441. rt_kprintf("Usage: protect_clear [uvp|temp|all]\n");
  442. return;
  443. }
  444. if (strcmp(argv[1], "uvp") == 0 || strcmp(argv[1], "all") == 0)
  445. {
  446. sys_sta.undervolt_protect = PROTECT_NONE;
  447. memset(&s_uvp, 0, sizeof(s_uvp));
  448. LOG_I("[CMD] UVP cleared");
  449. }
  450. if (strcmp(argv[1], "temp") == 0 || strcmp(argv[1], "all") == 0)
  451. {
  452. sys_sta.temp_protect = PROTECT_NONE;
  453. memset(&s_temp, 0, sizeof(s_temp));
  454. LOG_I("[CMD] TEMP cleared");
  455. }
  456. rt_kprintf("Protection status cleared\n");
  457. cmd_protect_status(0, NULL);
  458. }
  459. MSH_CMD_EXPORT(cmd_protect_clear, Clear protection status : protect_clear[uvp | temp | all]);
  460. /*********************↑ MSH命令 ↑***********************/