detection.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. /*********************************************************************************************************
  2. * Module : detection
  3. * Abstract :
  4. * Version : 1.0.0
  5. * Author : Leopul(COPYRIGHT 2025 ~ 2027 Leopul. All rights reserved.)
  6. * Complete : 2025-04-08
  7. * Content : None
  8. * Note : None
  9. *********************************************************************************************************/
  10. #include "detection.h"
  11. #include "gd32e23x.h"
  12. #include "common.h"
  13. #include "board_cfg.h"
  14. #include "relay.h"
  15. #define DERECTION_GPIO_1 GPIOA
  16. #define DERECTION_IO_1 GPIO_PIN_11
  17. #define DERECTION_IO_1_RCU EXTI_SOURCE_GPIOA
  18. #define DERECTION_IO_1_PIN DERECTION_IO_11
  19. #define DERECTION_IO_1_SOURCE EXTI_SOURCE_PIN11
  20. #define DERECTION_IO_1_EXIT EXTI_11
  21. #if (SURPPORT_2_PT)
  22. #define DERECTION_GPIO_2 GPIOB
  23. #define DERECTION_IO_2 GPIO_PIN_3
  24. #define DERECTION_IO_2_RCU EXTI_SOURCE_GPIOB
  25. #define DERECTION_IO_2_PIN DERECTION_IO_3
  26. #define DERECTION_IO_2_SOURCE EXTI_SOURCE_PIN3
  27. #define DERECTION_IO_2_EXIT EXTI_3
  28. #endif
  29. #define KEY_TEST_1_IO GPIO_PIN_14
  30. #define KEY_TEST_2_IO GPIO_PIN_2
  31. #define KEY_TEST_3_IO GPIO_PIN_0
  32. #define KEY_TEST_4_IO GPIO_PIN_1
  33. #define KEY_TEST_1_GPIO GPIOC
  34. #define KEY_TEST_2_GPIO GPIOB
  35. #define KEY_TEST_3_4_GPIO GPIOA
  36. static board_t *board = 0;
  37. #define KEY_RST_RCU RCU_GPIOC
  38. #define KEY_RST_IO GPIOC
  39. #define KEY_RST GPIO_PIN_14
  40. #define KEY_RST_EXTI_SOURCE EXTI_SOURCE_GPIOC
  41. #define KEY_RST_EXTI EXTI_14
  42. #define KEY_RST_EXTI_IO EXTI_SOURCE_PIN14
  43. #define KEY_TEST_RCU RCU_GPIOB
  44. #define KEY_TEST_IO GPIOB
  45. #define KEY_TEST GPIO_PIN_2
  46. #define KEY_TEST_EXTI_SOURCE EXTI_SOURCE_GPIOB
  47. #define KEY_TEST_EXTI EXTI_2
  48. #define KEY_TEST_EXTI_IO EXTI_SOURCE_PIN2
  49. static void get_detection(void);
  50. static void detection_opt(void);
  51. #if SUPPORT_SELF_LOCK
  52. static void get_test_1();
  53. static void get_test_2();
  54. static void get_test_3();
  55. static void get_test_4();
  56. static void get_lock_key(void);
  57. static void self_lock_opt(void);
  58. #endif
  59. void detection_init(void)
  60. {
  61. #if SUPPORT_DETECTION
  62. board = get_board();
  63. rcu_periph_clock_enable(RCU_CFGCMP);
  64. rcu_periph_clock_enable(RCU_GPIOA);
  65. #if SURPPORT_2_PT
  66. rcu_periph_clock_enable(RCU_GPIOB);
  67. #endif
  68. gpio_mode_set(DERECTION_GPIO_1, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, DERECTION_IO_1);
  69. #if SURPPORT_2_PT
  70. gpio_mode_set(DERECTION_GPIO_2, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, DERECTION_IO_2);
  71. #endif
  72. nvic_irq_enable(EXTI4_15_IRQn,2);
  73. syscfg_exti_line_config(DERECTION_IO_1_RCU,DERECTION_IO_1_SOURCE);
  74. exti_init(DERECTION_IO_1_EXIT,EXTI_INTERRUPT,EXTI_TRIG_FALLING);
  75. exti_interrupt_flag_clear(DERECTION_IO_1_EXIT);
  76. #if SURPPORT_2_PT
  77. nvic_irq_enable(EXTI2_3_IRQn,2);
  78. syscfg_exti_line_config(DERECTION_IO_2_RCU,DERECTION_IO_2_SOURCE);
  79. exti_init(DERECTION_IO_2_EXIT,EXTI_INTERRUPT,EXTI_TRIG_FALLING);
  80. exti_interrupt_flag_clear(DERECTION_IO_2_EXIT);
  81. #endif
  82. board->detection = get_detection;
  83. board->opt_detection = detection_opt;
  84. #endif
  85. }
  86. void key_init(void)
  87. {
  88. #if SUPPORT_ALL_ON_OFF
  89. rcu_periph_clock_enable(RCU_CFGCMP);
  90. rcu_periph_clock_enable(KEY_RST_RCU);
  91. gpio_mode_set(KEY_RST_IO, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, KEY_RST);
  92. nvic_irq_enable(EXTI4_15_IRQn,2);
  93. syscfg_exti_line_config(KEY_RST_EXTI_SOURCE,KEY_RST_EXTI_IO);
  94. exti_init(KEY_RST_EXTI,EXTI_INTERRUPT,EXTI_TRIG_FALLING);
  95. exti_interrupt_flag_clear(KEY_RST_EXTI);
  96. rcu_periph_clock_enable(KEY_TEST_RCU);
  97. gpio_mode_set(KEY_TEST_IO, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, KEY_TEST);
  98. nvic_irq_enable(EXTI2_3_IRQn,2);
  99. syscfg_exti_line_config(KEY_TEST_EXTI_SOURCE,KEY_TEST_EXTI_IO);
  100. exti_init(KEY_TEST_EXTI,EXTI_INTERRUPT,EXTI_TRIG_FALLING);
  101. exti_interrupt_flag_clear(KEY_TEST_EXTI);
  102. #elif SUPPORT_SELF_LOCK
  103. rcu_periph_clock_enable(RCU_CFGCMP);
  104. rcu_periph_clock_enable(RCU_GPIOC);
  105. if(!board)
  106. {
  107. board = get_board();
  108. }
  109. #if (RelaySlaveChaNum >= 1)
  110. gpio_mode_set(KEY_TEST_1_GPIO, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, KEY_TEST_1_IO);
  111. #endif
  112. #if (RelaySlaveChaNum >= 2)
  113. rcu_periph_clock_enable(RCU_GPIOB);
  114. gpio_mode_set(KEY_TEST_2_GPIO, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, KEY_TEST_2_IO);
  115. #endif
  116. #if (RelaySlaveChaNum >= 3)
  117. rcu_periph_clock_enable(RCU_GPIOA);
  118. gpio_mode_set(KEY_TEST_3_4_GPIO, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, KEY_TEST_3_IO);
  119. #endif
  120. #if (RelaySlaveChaNum >= 4)
  121. gpio_mode_set(KEY_TEST_3_4_GPIO, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, KEY_TEST_4_IO);
  122. #endif
  123. board->get_lock_key = get_lock_key;
  124. board->opt_lock_key = self_lock_opt;
  125. #endif
  126. }
  127. void EXTI4_15_IRQHandler(void){
  128. #if SUPPORT_ALL_ON_OFF
  129. if(RESET != exti_interrupt_flag_get(KEY_RST_EXTI))
  130. {
  131. exti_interrupt_flag_clear(KEY_RST_EXTI);
  132. if(board)
  133. {
  134. if(gpio_input_bit_get(KEY_TEST_IO,KEY_TEST) != 0)
  135. {
  136. async_data da = {0};
  137. da.fire_or_zero = _FIRE;
  138. da.w_eep_flag = FLAG_W_EEP;
  139. for(uint8_t j = 0; j < PT_SUB_COUNT;j++)
  140. {
  141. // if(board->md_data.r_data.state.state[j].state == RELAY_CLOSE)
  142. // continue;
  143. #if SUPPORT_CTRL2_LOCK2
  144. if(j >= 2)
  145. {
  146. continue;
  147. }
  148. #endif
  149. da.channel = j;
  150. da.method = DELAY_CTRL;
  151. da.reg = &board->relay_staging_staus[j];
  152. da.status = RELAY_CLOSE;
  153. set_relay_sta_async(&da);
  154. }
  155. }
  156. }
  157. }
  158. #endif
  159. if(RESET != exti_interrupt_flag_get(DERECTION_IO_1_EXIT))
  160. {
  161. if(board)
  162. {
  163. board->detection_1_value = 1;
  164. board->detection_1_count = 0;
  165. }
  166. exti_interrupt_flag_clear(DERECTION_IO_1_EXIT);
  167. }
  168. }
  169. void EXTI2_3_IRQHandler(void)
  170. {
  171. #if SUPPORT_ALL_ON_OFF
  172. if(RESET != exti_interrupt_flag_get(KEY_TEST_EXTI))
  173. {
  174. exti_interrupt_flag_clear(KEY_TEST_EXTI);
  175. if(board)
  176. {
  177. if(gpio_input_bit_get(KEY_RST_IO,KEY_RST) != 0)
  178. {
  179. #if SUPPORT_DETECTION
  180. if(board->md_data.r_data.detection & 0x1)
  181. #endif
  182. {
  183. async_data da = {0};
  184. da.fire_or_zero = _FIRE;
  185. da.method = DELAY_CTRL;
  186. da.w_eep_flag = FLAG_W_EEP;
  187. for(uint8_t k = 0; k < PT_SUB_COUNT;k++)
  188. {
  189. // if(board->md_data.r_data.state.state[k].state ==RELAY_OPEN)
  190. // continue;
  191. #if SUPPORT_CTRL2_LOCK2
  192. if(k >= 2)
  193. {
  194. continue;
  195. }
  196. #endif
  197. da.channel =k;
  198. da.reg = &board->relay_staging_staus[k];
  199. da.status = RELAY_OPEN;
  200. set_relay_sta_async(&da);
  201. }
  202. }
  203. }
  204. #if SURPPORT_2_PT
  205. #if SUPPORT_DETECTION
  206. if(board->md_data.r_data.detection & 0x2)
  207. #endif
  208. {
  209. async_data da = {0};
  210. da.fire_or_zero = _FIRE;
  211. da.method = DELAY_CTRL;
  212. da.w_eep_flag = FLAG_W_EEP;
  213. for(uint8_t k = PT_SUB_COUNT; k < RelaySlaveChaNum;k++)
  214. {
  215. if(board->md_data.r_data.state.state[k].state ==RELAY_OPEN)
  216. continue;
  217. da.channel =k;
  218. da.reg = &board->relay_staging_staus[k];
  219. da.status = RELAY_OPEN;
  220. set_relay_sta_async(&da);
  221. }
  222. }
  223. #endif
  224. }
  225. }
  226. #endif
  227. #if SURPPORT_2_PT
  228. if(RESET != exti_interrupt_flag_get(DERECTION_IO_2_EXIT))
  229. {
  230. if(board)
  231. {
  232. board->detection_2_value = 1;
  233. board->detection_2_count = 0;
  234. }
  235. exti_interrupt_flag_clear(DERECTION_IO_2_EXIT);
  236. }
  237. #endif
  238. }
  239. static void get_detection()
  240. {
  241. #if SUPPORT_DETECTION
  242. if(board)
  243. {
  244. uint16_t data = 0;
  245. if(board->detection_1_value > 0)
  246. {
  247. data |= (1 << 0);
  248. }
  249. #if SURPPORT_2_PT
  250. if(board->detection_2_value > 0)
  251. {
  252. data |= (1 << 1);
  253. }
  254. #endif
  255. board->md_data.r_data.detection = data;
  256. }
  257. #endif
  258. }
  259. static void detection_opt(void)
  260. {
  261. if(board)
  262. {
  263. #if SUPPORT_RELAY_REMENBER
  264. if(board->md_data.r_data.detection & 0x1)
  265. {
  266. if(board->handle_input_pt[0] == 0)
  267. {
  268. for(uint8_t k = 0; k < PT_SUB_COUNT;k++)
  269. {
  270. #if SUPPORT_SELF_LOCK
  271. if(board->val_key[k] == 0)
  272. continue;
  273. #endif
  274. #if SUPPORT_CTRL2_LOCK2
  275. if(k >= 2)
  276. {
  277. continue;
  278. }
  279. #endif
  280. async_data da = {0};
  281. da.channel =k;
  282. da.fire_or_zero = _FIRE;
  283. da.method = DELAY_CTRL;
  284. da.reg = &board->relay_staging_staus[k];
  285. da.status = board->handle_status[k];
  286. da.w_eep_flag = FLAG_N_W_EEP;
  287. set_relay_sta_async(&da);
  288. }
  289. board->handle_input_pt[0] = 1;
  290. }
  291. }else
  292. {
  293. if(board->handle_input_pt[0] == 1)
  294. {
  295. for(uint8_t k = 0; k < PT_SUB_COUNT;k++)
  296. {
  297. #if SUPPORT_CTRL2_LOCK2
  298. if(k >= 2)
  299. {
  300. continue;
  301. }
  302. #endif
  303. board->handle_status[k] = board->md_data.r_data.state.state[k].state;
  304. async_data da = {0};
  305. da.channel =k;
  306. da.fire_or_zero = _FIRE;
  307. da.method = PROMPT_CTRL;
  308. da.reg = &board->relay_staging_staus[k];
  309. da.status = RELAY_CLOSE;
  310. da.w_eep_flag = FLAG_N_W_EEP;
  311. set_relay_sta_async(&da);
  312. }
  313. board->handle_input_pt[0] = 0;
  314. }
  315. }
  316. #if SURPPORT_2_PT
  317. if(board->md_data.r_data.detection & 0x2)
  318. {
  319. if(board->handle_input_pt[1] == 0)
  320. {
  321. for(uint8_t k = PT_SUB_COUNT; k < RelaySlaveChaNum;k++)
  322. {
  323. async_data da = {0};
  324. da.channel =k;
  325. da.fire_or_zero = _FIRE;
  326. da.method = DELAY_CTRL;
  327. da.reg = &board->relay_staging_staus[k];
  328. da.status = board->handle_status[k];
  329. da.w_eep_flag = FLAG_N_W_EEP;
  330. set_relay_sta_async(&da);
  331. }
  332. board->handle_input_pt[1] = 1;
  333. }
  334. }else
  335. {
  336. if(board->handle_input_pt[1] == 1)
  337. {
  338. for(uint8_t k = PT_SUB_COUNT; k < RelaySlaveChaNum;k++)
  339. {
  340. board->handle_status[k] = board->md_data.r_data.state.state[k].state;
  341. async_data da = {0};
  342. da.channel =k;
  343. da.fire_or_zero = _FIRE;
  344. da.method = PROMPT_CTRL;
  345. da.reg = &board->relay_staging_staus[k];
  346. da.status = RELAY_CLOSE;
  347. da.w_eep_flag = FLAG_N_W_EEP;
  348. set_relay_sta_async(&da);
  349. }
  350. board->handle_input_pt[1] = 0;
  351. }
  352. }
  353. #endif
  354. #endif
  355. }
  356. }
  357. #if SUPPORT_SELF_LOCK
  358. static void get_lock_key(void)
  359. {
  360. #if (RelaySlaveChaNum >= 1)
  361. if(gpio_input_bit_get(KEY_TEST_1_GPIO,KEY_TEST_1_IO) == RESET)
  362. {
  363. board->val_key[0] = 1;
  364. }else
  365. {
  366. board->val_key[0] = 0;
  367. }
  368. #endif
  369. #if (RelaySlaveChaNum >= 2)
  370. if(gpio_input_bit_get(KEY_TEST_2_GPIO,KEY_TEST_2_IO) == RESET)
  371. {
  372. board->val_key[1] = 1;
  373. }else
  374. {
  375. board->val_key[1] = 0;
  376. }
  377. #endif
  378. #if (RelaySlaveChaNum >= 3)
  379. if(gpio_input_bit_get(KEY_TEST_3_4_GPIO,KEY_TEST_3_IO) == RESET)
  380. {
  381. board->val_key[2] = 1;
  382. }else
  383. {
  384. board->val_key[2] = 0;
  385. }
  386. #endif
  387. #if (RelaySlaveChaNum >= 4)
  388. if(gpio_input_bit_get(KEY_TEST_3_4_GPIO,KEY_TEST_4_IO) == RESET)
  389. {
  390. board->val_key[3] = 1;
  391. }else
  392. {
  393. board->val_key[3] = 0;
  394. }
  395. #endif
  396. }
  397. //#if (RelaySlaveChaNum >= 1)
  398. //static void get_test_1()
  399. //{
  400. // uint8_t val = 0;
  401. // if(gpio_input_bit_get(KEY_TEST_1_GPIO,KEY_TEST_1_IO) == RESET)
  402. // {
  403. // val = 1;
  404. // }
  405. // board->key_1 = val;
  406. //}
  407. //#endif
  408. //#if (RelaySlaveChaNum >= 2)
  409. //static void get_test_2()
  410. //{
  411. // uint8_t val = 0;
  412. // if(gpio_input_bit_get(KEY_TEST_2_GPIO,KEY_TEST_2_IO) == RESET)
  413. // {
  414. // val = 1;
  415. // }
  416. // board->key_2 = val;
  417. //}
  418. //#endif
  419. //#if (RelaySlaveChaNum >= 3)
  420. //static void get_test_3()
  421. //{
  422. // uint8_t val = 0;
  423. // if(gpio_input_bit_get(KEY_TEST_3_4_GPIO,KEY_TEST_3_IO) == RESET)
  424. // {
  425. // val = 1;
  426. // }
  427. // board->key_3 = val;
  428. //}
  429. //#endif
  430. //#if (RelaySlaveChaNum >= 4)
  431. //static void get_test_4()
  432. //{
  433. // uint8_t val = 0;
  434. // if(gpio_input_bit_get(KEY_TEST_3_4_GPIO,KEY_TEST_4_IO) == RESET)
  435. // {
  436. // val = 1;
  437. // }
  438. // board->key_4 = val;
  439. //}
  440. //#endif
  441. static void self_lock_opt(void)
  442. {
  443. #if (RelaySlaveChaNum <= 4)
  444. async_data da = {0};
  445. da.fire_or_zero = _FIRE;
  446. da.method = PROMPT_CTRL;
  447. da.w_eep_flag = FLAG_W_EEP;
  448. for(uint8_t i = 0; i < RelaySlaveChaNum;i++)
  449. {
  450. da.channel = i;
  451. da.reg = &board->relay_staging_staus[i];
  452. if(board->val_key[i] == 0)
  453. {
  454. if(board->handle_k_status[i] == 1)
  455. {
  456. da.status = RELAY_CLOSE;
  457. set_relay_sta_async(&da);
  458. board->handle_k_status[i] = 0;
  459. }
  460. }else
  461. {
  462. if(board->handle_k_status[i] == 0)
  463. {
  464. if(board->md_data.r_data.detection & 0x1)
  465. {
  466. da.status = RELAY_OPEN;
  467. set_relay_sta_async(&da);
  468. }
  469. board->handle_k_status[i] = 1;
  470. }
  471. }
  472. }
  473. #endif
  474. }
  475. //static void self_lock_opt1(void)
  476. //{
  477. // async_data da = {0};
  478. //
  479. // da.fire_or_zero = _FIRE;
  480. // da.method = PROMPT_CTRL;
  481. // da.w_eep_flag = FLAG_W_EEP;
  482. //#if (RelaySlaveChaNum >= 1)
  483. // if(board->key_1 == 0)
  484. // {
  485. // da.channel =0;
  486. // da.reg = &board->relay_staging_staus[0];
  487. // if(board->handle_k_1_status == 1)
  488. // {
  489. // da.status = RELAY_CLOSE;
  490. // set_relay_sta_async(&da);
  491. // board->handle_k_1_status = 0;
  492. // }
  493. // }else
  494. // {
  495. // da.channel =0;
  496. // da.reg = &board->relay_staging_staus[0];
  497. // if(board->handle_k_1_status == 0)
  498. // {
  499. // da.status = RELAY_OPEN;
  500. // set_relay_sta_async(&da);
  501. // board->handle_k_1_status = 1;
  502. // }
  503. // }
  504. //#endif
  505. //
  506. //
  507. //#if (RelaySlaveChaNum >= 2)
  508. // if(board->key_2 == 0)
  509. // {
  510. // da.channel =1;
  511. // da.reg = &board->relay_staging_staus[1];
  512. // if(board->handle_k_2_status == 1)
  513. // {
  514. // da.status = RELAY_CLOSE;
  515. // set_relay_sta_async(&da);
  516. // board->handle_k_2_status = 0;
  517. // }
  518. // }else
  519. // {
  520. // da.channel =1;
  521. // da.reg = &board->relay_staging_staus[1];
  522. // if(board->handle_k_2_status == 0)
  523. // {
  524. // da.status = RELAY_OPEN;
  525. // set_relay_sta_async(&da);
  526. // board->handle_k_2_status = 1;
  527. // }
  528. // }
  529. //#endif
  530. //
  531. //#if (RelaySlaveChaNum >= 3)
  532. // if(board->key_3 == 0)
  533. // {
  534. // da.channel =2;
  535. // da.reg = &board->relay_staging_staus[2];
  536. // if(board->handle_k_3_status == 1)
  537. // {
  538. // da.status = RELAY_CLOSE;
  539. // set_relay_sta_async(&da);
  540. // board->handle_k_3_status = 0;
  541. // }
  542. // }else
  543. // {
  544. // da.channel =2;
  545. // da.reg = &board->relay_staging_staus[2];
  546. // if(board->handle_k_3_status == 0)
  547. // {
  548. // da.status = RELAY_OPEN;
  549. // set_relay_sta_async(&da);
  550. // board->handle_k_3_status = 1;
  551. // }
  552. // }
  553. //#endif
  554. //
  555. //#if (RelaySlaveChaNum >= 4)
  556. // if(board->key_4 == 0)
  557. // {
  558. // da.channel =3;
  559. // da.reg = &board->relay_staging_staus[3];
  560. // if(board->handle_k_4_status == 1)
  561. // {
  562. // da.status = RELAY_CLOSE;
  563. // set_relay_sta_async(&da);
  564. // board->handle_k_4_status = 0;
  565. // }
  566. // }else
  567. // {
  568. // da.channel =3;
  569. // da.reg = &board->relay_staging_staus[3];
  570. // if(board->handle_k_4_status == 0)
  571. // {
  572. // da.status = RELAY_OPEN;
  573. // set_relay_sta_async(&da);
  574. // board->handle_k_4_status = 1;
  575. // }
  576. // }
  577. //#endif
  578. //}
  579. #endif