main.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. /******************************************************************************
  2. * Copyright (C) 2021, Xiaohua Semiconductor Co., Ltd. All rights reserved.
  3. *
  4. * This software component is licensed by XHSC under BSD 3-Clause license
  5. * (the "License"); You may not use this file except in compliance with the
  6. * License. You may obtain a copy of the License at:
  7. * opensource.org/licenses/BSD-3-Clause
  8. *
  9. ******************************************************************************/
  10. /******************************************************************************
  11. * @file main.c
  12. *
  13. * @brief Source file for GPIO example
  14. *
  15. * @author MADS Team
  16. *
  17. ******************************************************************************/
  18. /******************************************************************************
  19. * Include files
  20. ******************************************************************************/
  21. #include "board.h"
  22. #include "tdc_tof.h"
  23. #include "smp_shell.h"
  24. #include "param.h"
  25. #include "lcd_disp.h"
  26. #include "bill.h"
  27. #include "app_nb.h"
  28. #include "check.h"
  29. #include "alarm.h"
  30. #include "rtc_run.h"
  31. #include "stat.h"
  32. #include "ymodem.h"
  33. /******************************************************************************
  34. * Local pre-processor symbols/macros ('#define')
  35. ******************************************************************************/
  36. /* 按键参数(可直接修改) */
  37. #define SHORT_PRESS_MS 50 // 短按阈值
  38. #define LONG_PRESS_MS 5000 // 长按阈值
  39. #define INVALID_LONG_PRESS_MS 20000 // 无效长按阈值
  40. #define DEBOUNCE_US 5000 // 消抖时间
  41. #define KEY_SLEEP_BLOCK_MS 800 // 按键后延迟允许休眠时间
  42. /* 全局变量(仅保留必要的) */
  43. struct rt_semaphore lcd_isr_sem; // lcd 中断信号量
  44. struct rt_semaphore rtc_isr_sem; // rtc 中断信号量
  45. struct rt_semaphore tdc_isr_sem; // TDC 中断信号量:中断->线程通知
  46. static rt_timer_t key_timer = RT_NULL; // 按键计时定时器
  47. static uint32_t key_press_time = 0; // 按键按下时长(ms)
  48. volatile rt_tick_t g_key_last_active_tick = 0; // add by HBR 20260623,按键最后活动时间,用于睡眠门禁
  49. uint8_t KEY_SHORT_PRESS_FLAG = 0; // 短按标志,用于翻屏
  50. uint8_t KEY_LONG_PRESS_FLAG = 0; // 长按标志,用于触发上报
  51. /************************** 线程配置 **************************/
  52. #define TDC_THREAD_STACK 1024 // 核心线程栈1K,满足需求 堆栈大小
  53. #define TDC_THREAD_PRIORITY 1 // 高优先级,确保及时响应
  54. #define TDC_THREAD_TIMESLICE 500
  55. #define RTC_THREAD_STACK 1024 // 核心线程栈1K,满足需求
  56. #define RTC_THREAD_PRIORITY 2 // 高优先级,确保及时响应
  57. #define RTC_THREAD_TIMESLICE 500
  58. #define NB_THREAD_STACK 2048 // 核心线程栈1K,满足需求
  59. #define NB_THREAD_PRIORITY 3 // 高优先级,确保及时响应
  60. #define NB_THREAD_TIMESLICE 500
  61. #define LCD_THREAD_STACK 1024 // 核心线程栈1K,满足需求
  62. #define LCD_THREAD_PRIORITY 4 // 高优先级,确保及时响应
  63. #define LCD_THREAD_TIMESLICE 500
  64. /******************************************************************************
  65. * Global variable definitions (declared in header file with 'extern')
  66. ******************************************************************************/
  67. uint8_t G_NB_CAN_DEEP_SLEEP = 1; // NB是否允许进入休眠,1:允许,0:不允许
  68. uint8_t G_KEY_CAN_DEEP_SLEEP = 1; // KEY是否允许进入休眠,1:允许,0:不允许
  69. uint8_t G_TDC_DEEP_SLEEP = 1;
  70. /******************************************************************************
  71. * Local type definitions ('typedef')
  72. ******************************************************************************/
  73. /******************************************************************************
  74. * Local function prototypes ('static')
  75. ******************************************************************************/
  76. /******************************************************************************
  77. * Local variable definitions ('static') *
  78. ******************************************************************************/
  79. /******************************************************************************
  80. * Local pre-processor symbols/macros ('#define')
  81. ******************************************************************************/
  82. /*****************************************************************************
  83. * Function implementation - global ('extern') and local ('static')
  84. ******************************************************************************/
  85. /**
  86. ******************************************************************************
  87. ** \brief Main function of project
  88. **
  89. ** \return uint32_t return value, if needed
  90. **
  91. ** This sample
  92. **
  93. ******************************************************************************/
  94. // uint32_t SOFT_VER = 0x56313031; // V101
  95. uint32_t BUILD_DATE = ((2025 << 16) | (12 << 8) | 16);
  96. uint16_t BUILD_TIME = ((17 << 8) | 23);
  97. //////////wxl test low power////////////
  98. /////////////////////////////////////////
  99. void show_stack_by_name(char *name)
  100. {
  101. rt_thread_t thread = rt_thread_find(name);
  102. if (thread == RT_NULL)
  103. {
  104. LOG("no thread name is:%s\n!", name);
  105. return;
  106. }
  107. rt_uint32_t total = thread->stack_size;
  108. rt_uint32_t free = (rt_uint32_t)thread->sp - (rt_uint32_t)thread->stack_addr;
  109. rt_uint32_t used = total - free;
  110. LOG("[name] %-8s | total:%4d | used:%4d | free:%4d\n",
  111. thread->name, total, used, free);
  112. }
  113. extern void test_dtof_var(void);
  114. void PeriodProc(void) // 测试更改提交 2026-5-19-1-2-3
  115. {
  116. rtc_run_t *rr;
  117. get_rtc_run(&rr);
  118. if (rr->flag_1s)
  119. {
  120. rr->flag_1s = 0;
  121. // add by yuewei 20260519 start
  122. if (alarm_status(ERR_BLANK_PIPE))
  123. {
  124. // 空管,设置RTC中断2秒1次
  125. if ((uint8_t)get_rtc_interrupt_time() != 2)
  126. {
  127. set_rtc_interrupt_time(2);
  128. }
  129. }
  130. else
  131. {
  132. // 不是空管,设置RTC中断0.5秒1次
  133. if ((get_rtc_interrupt_time() == 2) || (get_rtc_interrupt_time() == 1))
  134. {
  135. set_rtc_interrupt_time(0.5); // 0.5
  136. }
  137. }
  138. // add by yuewei 20260519 start
  139. // LOG("Diff=%.6f, AvgDiff=%.6f, T=%.6f, vol=%.6f\n", SerPlot.f.Data[0], SerPlot.f.Data[3], WaterPrmt[0], FlowVol * 0.000001f * 3600);
  140. // rt_thread_mdelay(10);
  141. // LOG("main-sec = %d ", rr->second);
  142. // rt_thread_mdelay(2);
  143. // add by yuewei 20260416 to countdown LcdTimer per 1 second
  144. lcd_timer_countdown();
  145. rt_sem_release(&lcd_isr_sem);
  146. bw_flow_monitor(); // 反向计量检测
  147. large_flow_monitor(); // 大流量检测
  148. cnti_flow_monitor(); // 持续流量检测
  149. leak_flow_monitor(); // 渗漏流量检测
  150. high_water_temp_monitor(); // 高温检测
  151. low_water_temp_monitor(); // 低温检测
  152. tdc_rw_err_monitor(); // 传感器异常检测
  153. eeprom_rw_err_monitor(); // 存储器异常检测
  154. // LOG("ch-%d:%.6f,%.6f,%.6f,%.6f\n",get_mux_ch(),SerPlot.f.Data[0],SerPlot.f.Data[1],SerPlot.f.Data[2],SerPlot.f.Data[3]);
  155. CheckRxTimeout();
  156. CheckingCoundown();
  157. nb_task_run_check(); // nb上报触发判断
  158. update_run_app_info();
  159. app_bill_run(); // 各种阶段计量定时存储
  160. run_clk_task_handler(); // wxl2026-5-8 一秒一次一些零星任务放在这个里面
  161. AlarmHandle(); // wxl2026-4-30
  162. feed_dog();
  163. }
  164. if (rr->flag_1m)
  165. {
  166. rr->flag_1m = 0;
  167. adc_get(); // 采集电池电压与压力
  168. low_power_monitor(); // 低电压检测
  169. high_pressure_monitor(); // 高压检测
  170. low_pressure_monitor(); // 低压检测
  171. // 跨00点
  172. if (rr->hour == 0 && rr->minute == 0)
  173. {
  174. // 清除今日日最高流量,同时将今日最高流量赋值给昨日最高流量
  175. clr_max_flow_rate();
  176. }
  177. }
  178. }
  179. /**
  180. * @brief 磁阻按键中断函数,下降沿触发
  181. * @param 无
  182. * @retval 无
  183. */
  184. // void PORTB_IRQHandler(void)
  185. //{
  186. // if (GpioGetEvents(GpioPortB, GpioBitMsk0))
  187. // {
  188. // // 简单消抖
  189. // us_delay(DEBOUNCE_US);
  190. // if (GpioInPins(GpioPortB, GpioBitMsk0) == 0)
  191. // {
  192. // key_press_time = 0;
  193. // rt_timer_start(key_timer); // 启动计时
  194. // G_KEY_CAN_DEEP_SLEEP = 0; // 按键不允许休眠
  195. // }
  196. // M0P_GPIO->PB_ICLR &= ~(1 << 0);
  197. // }
  198. //}
  199. uint8_t key_handle_runing = 0;
  200. uint8_t key_irq_handler(void) // WXL2026-5-22
  201. {
  202. if (GpioGetEvents(GpioPortD, GpioBitMsk4))
  203. {
  204. if (key_handle_runing != 0 || key_press_time != 0) // waiting for key task complete wxl2026-5-26
  205. {
  206. // M0P_GPIO->PD_ICLR &= ~(1 << 4);
  207. M0P_GPIO->PD_ICLR = (1u << 4);
  208. return 0;
  209. }
  210. // us_delay(DEBOUNCE_US);
  211. if (GpioInPins(GpioPortD, GpioBitMsk4) != 0)
  212. {
  213. key_press_time = 0;
  214. G_KEY_CAN_DEEP_SLEEP = 0; // 按键不允许休眠
  215. g_key_last_active_tick = rt_tick_get(); //add by HBR 20260623,按键中断触发记录一次时间
  216. rt_timer_stop(key_timer);
  217. rt_timer_start(key_timer);
  218. }
  219. // M0P_GPIO->PD_ICLR &= ~(1 << 4);
  220. M0P_GPIO->PD_ICLR = (1u << 4);
  221. return 1;
  222. }
  223. return 0;
  224. }
  225. /**
  226. * @brief 定时器回调:1ms计时,检测释放并判断长短按
  227. */
  228. static void key_timer_cb(void *param)
  229. {
  230. key_press_time++; // 累计按下时间
  231. if (GpioInPins(GpioPortD, GpioBitMsk4) == 0) // WXL2026-5-20
  232. {
  233. rt_timer_stop(key_timer); // 停止计时
  234. G_KEY_CAN_DEEP_SLEEP = 0; // modify by HBR 20260622,按键,不允许休眠
  235. g_key_last_active_tick = rt_tick_get(); //add by HBR 20260622,判断出短按记录一次时间
  236. // 核心:判断长短按
  237. if (key_press_time >= SHORT_PRESS_MS && key_press_time < LONG_PRESS_MS)
  238. {
  239. LOG("short press:%dms\n", key_press_time); // 短按逻辑
  240. KEY_SHORT_PRESS_FLAG = 1; // 翻屏
  241. }
  242. key_press_time = 0; // 重置计时
  243. }
  244. if ((G_KEY_CAN_DEEP_SLEEP == 0) && (key_press_time >= LONG_PRESS_MS))
  245. {
  246. G_KEY_CAN_DEEP_SLEEP = 1; // 允许休眠
  247. LOG("long press:%dms\n", key_press_time); // 长按逻辑
  248. KEY_LONG_PRESS_FLAG = 1; // 上报
  249. rt_timer_stop(key_timer); // 停止计时
  250. key_press_time = 0;
  251. }
  252. }
  253. /**
  254. * @brief RTC相关业务线程
  255. * @param parameter: 线程参数,RT_NULL
  256. * @retval 无
  257. */
  258. static void rtc_business_thread(void *parameter)
  259. {
  260. rt_err_t ret = RT_EOK;
  261. while (1)
  262. {
  263. /* 等待中断信号(永久等待,不占用CPU) */
  264. if (rt_sem_take(&rtc_isr_sem, RT_WAITING_FOREVER) == RT_EOK)
  265. {
  266. rtc_interrupt_deal_tdc();
  267. //add by HBR 20260617 to deal static ot count do not display when set stativ ot
  268. if(G_STATIC_OT_SETTING_FLAG == 1)
  269. {
  270. rt_sem_release(&lcd_isr_sem);
  271. }
  272. }
  273. }
  274. }
  275. /**
  276. * @brief 创建rtc线程
  277. * @retval 0-成功,-1-失败
  278. */
  279. static int rtc_thread_create(void)
  280. {
  281. rt_thread_t rtc_thread = RT_NULL;
  282. rtc_thread = rt_thread_create("rtc_thread",
  283. rtc_business_thread,
  284. RT_NULL,
  285. RTC_THREAD_STACK,
  286. RTC_THREAD_PRIORITY,
  287. RTC_THREAD_TIMESLICE);
  288. if (rtc_thread == RT_NULL)
  289. {
  290. LOG("RTC thread create failed!\n");
  291. return -1;
  292. }
  293. rt_thread_startup(rtc_thread);
  294. LOG("RTC thread create success!\n");
  295. return 0;
  296. }
  297. /**
  298. * @brief TDC相关业务线程
  299. * @param parameter: 线程参数,RT_NULL
  300. * @retval 无
  301. */
  302. static void tdc_business_thread(void *parameter)
  303. {
  304. rt_err_t ret = RT_EOK;
  305. uint32_t cnt;
  306. while (1)
  307. {
  308. // LOG("tdc_business_thread\n");
  309. /* 等待中断信号(永久等待,不占用CPU) */
  310. if (rt_sem_take(&tdc_isr_sem, RT_WAITING_FOREVER) == RT_EOK)
  311. {
  312. // LOG("rt_sem_take\n");
  313. tof_isr_convert_tof_to_flow();
  314. }
  315. TdcIntStep=0xff;
  316. if (TdcIntStep == 0xff)
  317. {
  318. // cnt=SYS_MS_CNT;
  319. #ifdef APP_DEBUG_PRINT
  320. G_TDC_DEEP_SLEEP = 0;
  321. // rt_thread_mdelay(15);
  322. rtc_run_t *rr;
  323. get_rtc_run(&rr);
  324. #ifdef DEBUG_TDC
  325. LOG("Diff=%.6f, AvgDiff=%.6f, T=%.6f, vol=%.6f\n", SerPlot.f.Data[0], SerPlot.f.Data[3], WaterPrmt[0], FlowVol * 0.000001f * 3600);
  326. // LOG("diff=%.6f, U=%.6f, D=%.6f\n", SerPlot.f.Data[0], SerPlot.f.Data[1], SerPlot.f.Data[2]);
  327. rt_thread_mdelay(4);
  328. #endif
  329. #endif
  330. G_TDC_DEEP_SLEEP = 1;
  331. // rt_thread_mdelay(10);
  332. NB_CHECK_TDC_COMPLETE_FLAG = 1;
  333. // get_time[gtcnt++]=SYS_MS_CNT-cnt;
  334. // gtcnt&=0x1f;
  335. }
  336. // G_TDC_DEEP_SLEEP = 1;
  337. }
  338. }
  339. /**
  340. * @brief 创建tdc线程
  341. * @retval 0-成功,-1-失败
  342. */
  343. static int tdc_thread_create(void)
  344. {
  345. rt_thread_t tdc_thread = RT_NULL;
  346. tdc_thread = rt_thread_create("tdc_thread",
  347. tdc_business_thread,
  348. RT_NULL,
  349. TDC_THREAD_STACK,
  350. TDC_THREAD_PRIORITY,
  351. TDC_THREAD_TIMESLICE);
  352. if (tdc_thread == RT_NULL)
  353. {
  354. LOG("TDC thread create failed!\n");
  355. return -1;
  356. }
  357. rt_thread_startup(tdc_thread);
  358. LOG("TDC thread create success!\n");
  359. return 0;
  360. }
  361. /**
  362. * @brief NB相关业务线程
  363. * @param parameter: 线程参数,RT_NULL
  364. * @retval 无
  365. */
  366. // extern rt_sem_t Nb_thread_run_sem;
  367. static void nb_business_thread(void *parameter)
  368. {
  369. NbTread(); // 满足上报条件后就上报,否则持续挂起等待
  370. }
  371. /**
  372. * @brief 创建NB线程
  373. * @retval 0-成功,-1-失败
  374. */
  375. static int nb_thread_create(void)
  376. {
  377. rt_thread_t nb_thread = RT_NULL;
  378. nb_thread = rt_thread_create("nb_thread",
  379. nb_business_thread,
  380. RT_NULL,
  381. NB_THREAD_STACK,
  382. NB_THREAD_PRIORITY,
  383. NB_THREAD_TIMESLICE);
  384. if (nb_thread == RT_NULL)
  385. {
  386. LOG("NB thread create failed!\n");
  387. return -1;
  388. }
  389. rt_thread_startup(nb_thread);
  390. LOG("NB thread create success!\n");
  391. return 0;
  392. }
  393. /**
  394. * @brief LCD相关业务线程
  395. * @param parameter: 线程参数,RT_NULL
  396. * @retval 无
  397. */
  398. static void lcd_business_thread(void *parameter)
  399. {
  400. while (1)
  401. {
  402. if (rt_sem_take(&lcd_isr_sem, RT_WAITING_FOREVER) == RT_EOK)
  403. {
  404. if (lcd_disp_pass_flag != 0)
  405. {
  406. lcd_disp_pass_flag--;
  407. lcd_show_key_trigger_report_pass();
  408. }
  409. else if (G_OT_ERROR_FLAG_CNT != 0)
  410. {
  411. G_OT_ERROR_FLAG_CNT--;
  412. lcd_show_ot_error();
  413. }
  414. else
  415. LcdHandler();
  416. key_handle_runing = 0;
  417. }
  418. }
  419. }
  420. /**
  421. * @brief 创建LCD线程
  422. * @retval 0-成功,-1-失败
  423. */
  424. static int lcd_thread_create(void)
  425. {
  426. rt_thread_t lcd_thread = RT_NULL;
  427. lcd_thread = rt_thread_create("lcd_thread",
  428. lcd_business_thread,
  429. RT_NULL,
  430. LCD_THREAD_STACK,
  431. LCD_THREAD_PRIORITY,
  432. LCD_THREAD_TIMESLICE);
  433. if (lcd_thread == RT_NULL)
  434. {
  435. LOG("LCD thread create failed!\n");
  436. return -1;
  437. }
  438. rt_thread_startup(lcd_thread);
  439. LOG("LCD thread create success!\n");
  440. return 0;
  441. }
  442. /**
  443. * @brief 创建按键时长检测定时器
  444. * @retval 0-成功,-1-失败
  445. */
  446. static int Key_press_timer_create(void)
  447. {
  448. // 创建1ms周期的软件定时器(RTOS自带,无需配置硬件)
  449. key_timer = rt_timer_create("key_timer", key_timer_cb,
  450. RT_NULL, 1, RT_TIMER_FLAG_PERIODIC);
  451. if (key_timer == RT_NULL)
  452. {
  453. LOG("key_timer create failed!\n");
  454. return -1;
  455. }
  456. LOG("key_timer create success!\n");
  457. return 0;
  458. }
  459. int32_t main(void)
  460. {
  461. int32_t res;
  462. uint8_t ch_num;
  463. uint8_t ch;
  464. uint8_t i;
  465. uint32_t flag;
  466. rt_base_t level;
  467. #ifdef DOG_ENABLE
  468. watchdog_init(WdtResetEn, WdtT6s55); // 看门狗初始化
  469. #endif
  470. feed_dog();
  471. rt_enter_critical(); //!!!!!do not delete!!!!!!
  472. set_tof_state(TOF_ST_INIT);
  473. HardwareIdInit(); // 初始化硬件ID接口
  474. ButtonInit(); // 初始化磁组按键
  475. tdc_tof_init(); // ms1030 spi驱动
  476. //nb_power_on();
  477. finsh_drv_init(); // 红外串口初始化波特率2400、8位数据、1位停止位、even偶校验
  478. lpuart0_drv_init(0); // NB串口初始化波特率9600、8位数据、1位停止位、none校验
  479. nb_run_init(); // NB运行数据初始化
  480. McuFlash_init(); // FLASH读写初始化
  481. #ifdef APP_DEBUG_PRINT
  482. debug_uart_init(); // 初始化调试打印接口,波特率9600、8位数据、1位停止位、none校验
  483. #endif // DEBUG
  484. set_check_rx_itf(CheckRxData); // 设置红外中断处理函数为CheckRxData
  485. check_init(); // 初始化红外接收数据结构
  486. lcd_init(); // 液晶屏初始化
  487. // LCD_DispDouble(1234.567890, 0, 10, 4);
  488. lcd_show_all(); // 液晶屏全显
  489. LCD_Update();
  490. us_delay(100); // 延时大约1秒
  491. feed_dog();
  492. adc_init(); // adc初始化,电压采集
  493. adc_get(); // 采集一次数据
  494. eeprom_init(); // EEPROM驱动初始化,I2C
  495. rtc_run_t *rr;
  496. get_rtc_run(&rr); // 获取RTC时间
  497. rtc_init(rr); // 设置RTC系统时钟源为外部32K(ms1030),同时设置定时中断为1秒钟一次
  498. set_tof_state(TOF_ST_VOID); // 设置ms1030状态,降低功耗
  499. // set_tof_state(TOF_ST_METER);
  500. ParamInit(); // 初始化全局变量参数
  501. set_tdc_itf(&TdcItf); //!!!!!!do not move!!!!!!only can be after ParamInit()!!!!!!!
  502. LOG("------------TFUW001------------\n");
  503. ch_num = GetChNum();
  504. tof_filter_init(ch_num); // 20260429
  505. LOG("ch_num = %d\n", ch_num);
  506. // LOG("TdcItf.ch_num = %d\n", TdcItf.ch_num);
  507. // LOG("pTdcItf->ch_num = %d\n", pTdcItf->ch_num);
  508. for (i = 0; i < ch_num; i++)
  509. {
  510. ch = TdcItf.mux_ch_no[i];
  511. LOG("Ch%d TdcPathSkew = %.3fns.\n", ch, TdcItf.path_skew[ch]);
  512. }
  513. LOG("pressure_sensor = %d\n", Paramx.pressure_sensor);
  514. LOG("PipeCoe = %.6f\n", TdcItf.pipe_coe);
  515. LOG("dev_id =%02x%02x%02x%02x%02x%02x\n", Paramx.dev_id[0], Paramx.dev_id[1], Paramx.dev_id[2], Paramx.dev_id[3], Paramx.dev_id[4], Paramx.dev_id[5]);
  516. LOG("b=%d,n=%d,q=%d,r=%d\n", Paramx.b, Paramx.n, Paramx.q, Paramx.r);
  517. int tmp = 0;
  518. for (i = 0; i < CHECK_POINT_NUM; i++)
  519. {
  520. LOG("spdlvl[%d][%d] = %d\n", tmp, i, Paramx.check_spdLvl_c[tmp].spd_lvl[i]);
  521. LOG("base c[%d][%d] = %d\n", tmp, i, Paramx.check_spdLvl_c[tmp].base_c[i]);
  522. LOG("current c[%d][%d] = %d\n", tmp, i, Paramx.check_spdLvl_c[tmp].cur_c[i]);
  523. }
  524. ///////////debug//////////////////////
  525. // set_rtc_interrupt_time(1);
  526. ///////////////////////////////
  527. rtc_thread_create(); // 创建RTC线程
  528. tdc_thread_create(); // 创建TDC线程
  529. nb_thread_create(); // 创建NB线程
  530. lcd_thread_create(); // 创建LCD线程
  531. Key_press_timer_create(); // 创建按键时长检测定时器
  532. rt_sem_init(&tdc_isr_sem, "tdc_isr", 0, RT_IPC_FLAG_PRIO);
  533. rt_sem_init(&rtc_isr_sem, "rtc_isr", 0, RT_IPC_FLAG_PRIO);
  534. rt_sem_init(&lcd_isr_sem, "lcd_isr", 0, RT_IPC_FLAG_PRIO);
  535. rt_exit_critical(); ////!!!!!do not delete!!!!!!
  536. // us_delay(10000);
  537. // nb_tx_str(NB_AT);
  538. // us_delay(10000);
  539. // nb_tx_str(NB_AT);
  540. // us_delay(10000);
  541. while (1)
  542. {
  543. // static uint8_t tested = 0;
  544. // static uint8_t read_data[2] = {0};
  545. // if (tested == 0)
  546. // {
  547. // uint8_t write_data[2] = {12, 0};
  548. // tested = 1;
  549. // write_eeprom(0x1FFF0u, write_data, sizeof(write_data));
  550. // read_eeprom(0x1FFF0u, read_data, sizeof(read_data));
  551. // }
  552. // tdc_spi_wr_code(0x04);
  553. // uint16_t aa=0;
  554. // aa=tdc_spi_rd_d16(0xd0);
  555. PeriodProc();
  556. CheckRxHandler(); // 红外接收指令处理
  557. // add by yuewei 20260416
  558. // if ((G_KEY_CAN_DEEP_SLEEP == 0) && (key_press_time >= LONG_PRESS_MS))
  559. // {
  560. // G_KEY_CAN_DEEP_SLEEP = 1; // 允许休眠
  561. // LOG("long press:%dms\n", key_press_time); // 长按逻辑
  562. // KEY_LONG_PRESS_FLAG = 1; // 上报
  563. // rt_timer_stop(key_timer); // 停止计时
  564. // key_press_time = 0;
  565. // }
  566. // 翻屏
  567. if (KEY_SHORT_PRESS_FLAG == 1)
  568. {
  569. // at_cmd_send(COAP_IMEI, NB_CGSN, NB_CGSN_ACK, 3, 2);
  570. // at_cmd_send(COAP_ICCID, NB_NCCID, NB_NCCID_ACK, 3, 2);
  571. //
  572. // LOG("NB raw length=%u\r\n", NbRun.rx_idx);
  573. // for (uint16_t i = 0; i < NbRun.rx_idx; i++)
  574. // {
  575. // LOG("[%03u] 0x%02X '%c'\r\n",
  576. // i,
  577. // NbRun.rx_buf[i],
  578. // (NbRun.rx_buf[i] >= 32 &&
  579. // NbRun.rx_buf[i] <= 126)
  580. // ? NbRun.rx_buf[i]
  581. // : '.');
  582. // }
  583. uint32_t p1=0;
  584. uint32_t p2=0;
  585. uint16_t aa=0;
  586. extern float TdcResist;
  587. extern uint8_t temp_flag;
  588. tdc_spi_wr_code(0x04);
  589. if(temp_flag)
  590. {
  591. aa=tdc_spi_rd_d16(0xd0);
  592. p1= tdc_spi_rd_d32(0xd1); // 参考电阻
  593. p2 = tdc_spi_rd_d32(0xd2); // NTC
  594. temp_flag=0;
  595. }
  596. // if (p1>0xB00000u && p1<0xE00000u){
  597. TdcResist = 2500.0f*p2/p1; // ohmX10
  598. Ohm2Dgr2USSpd(TdcResist, WaterPrmt); // [0]:Temperature; [1]:ultrasonic speed @Temp
  599. // }
  600. // for (i = 0; i < NbRun.rx_idx; i++)
  601. // {
  602. // LOG("%c", NbRun.rx_buf[i]);
  603. // };
  604. // 处于检表状态
  605. key_handle_runing = 1;
  606. if (GetCheckState() == CHECKING)
  607. {
  608. // add by yuewei 20260416 to toggle display fw and bw cumflow in checking mode
  609. g_lcd_display_check_bw_cum_flow = ~g_lcd_display_check_bw_cum_flow;
  610. rt_sem_release(&lcd_isr_sem); //add by HBR 20260622
  611. }
  612. else
  613. {
  614. lcd_page_switch();
  615. rt_sem_release(&lcd_isr_sem);
  616. // LcdHandler();//add by yw 20260126
  617. }
  618. KEY_SHORT_PRESS_FLAG = 0; // 清除标志
  619. }
  620. // 长按
  621. if (KEY_LONG_PRESS_FLAG == 1)
  622. {
  623. key_handle_runing = 1;
  624. LOG("-----------------------------LcdPage=%d\n", LcdPage);
  625. // 处于检表状态
  626. if (GetCheckState() == CHECKING)
  627. {
  628. // 进入正常模式
  629. check_enter_normal_mode_by_key_press();
  630. LOG("check_enter_normal_mode_by_key_press!\n");
  631. }
  632. else
  633. {
  634. // 液晶屏当前处于时间显示页面
  635. if (LcdPage == disp_time)
  636. {
  637. // 进入检表模式
  638. check_enter_check_mode_by_key_press();
  639. LOG("check_enter_check_mode_by_key_press!\n");
  640. }
  641. else
  642. {
  643. // add by yuewei 20260416
  644. // lcd_show_key_trigger_report_pass();
  645. // us_delay(100000000);
  646. lcd_disp_pass_flag = 2; // wxl20260423 触发显示pass
  647. // rt_sem_release(&lcd_isr_sem);
  648. // 在此处触发NB上报
  649. upload_dat.manual_runflag = 1; // wxl 2026-4-1 这个标志会触发手动上报
  650. }
  651. }
  652. KEY_LONG_PRESS_FLAG = 0;
  653. }
  654. // show_stack_by_name("tidle");
  655. // show_stack_by_name("main");
  656. // show_stack_by_name("lcd_thread");
  657. // show_stack_by_name("nb_thread");
  658. if ((G_KEY_CAN_DEEP_SLEEP == 0) && //add by HBR 20260623
  659. (key_press_time == 0) &&
  660. (rt_tick_get() - g_key_last_active_tick > rt_tick_from_millisecond(KEY_SLEEP_BLOCK_MS))) //按键按下后延迟800ms并且按键计数为0后,才进入休眠
  661. {
  662. G_KEY_CAN_DEEP_SLEEP = 1;
  663. }
  664. if (G_NB_CAN_DEEP_SLEEP && G_TDC_DEEP_SLEEP && G_KEY_CAN_DEEP_SLEEP)
  665. {
  666. EnterLowPower();
  667. }
  668. }
  669. }
  670. /******************************************************************************
  671. * EOF (not truncated)
  672. ******************************************************************************/
  673. #ifdef DEBUG_LOW_POWER
  674. uint32_t get_time[32];
  675. uint8_t gtcnt;
  676. // void get_internal_time(void);
  677. // extern uint16_t tms;
  678. #endif