timer.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*********************************************************************************************************
  2. * Module : timer
  3. * Abstract : defined data structure!
  4. * Version : 1.0.0
  5. * Author : Leopul(COPYRIGHT 2025 ~ 2027 Leopul. All rights reserved.)
  6. * Complete : 2025-04-09
  7. * Content : None
  8. * Note : None
  9. *********************************************************************************************************/
  10. #include "timer.h"
  11. #include "gd32e23x.h"
  12. #include "board_cfg.h"
  13. #include "common.h"
  14. static board_t *board = 0;
  15. static void timer14_config(uint16_t arr,uint16_t psc);
  16. static void relay_remember(void);
  17. static uint16_t count_1s = 0;
  18. static uint16_t count_10s = 0;
  19. static uint16_t count_100ms = 0;
  20. uint16_t rl_delay_cnt[RelaySlaveChaNum] = {0};
  21. uint16_t rl_delay_flag[RelaySlaveChaNum] = {0};
  22. #if SUPPORT_PWM_OUT_RELAY
  23. uint8_t rl_open_delay_flag[RelaySlaveChaNum] = {0};
  24. uint8_t rl_open_delay_count[RelaySlaveChaNum] = {0};
  25. uint8_t rl_pwm_flag[RelaySlaveChaNum] = {0};
  26. static void timer15_config(uint16_t arr,uint16_t psc);
  27. #endif
  28. void time_init(void)
  29. {
  30. timer14_config(9999,71);
  31. #if SUPPORT_PWM_OUT_RELAY
  32. timer15_config(24,71);
  33. #endif
  34. }
  35. static void timer14_config(uint16_t arr,uint16_t psc)
  36. {
  37. board = get_board();
  38. timer_parameter_struct timer_initpara;
  39. rcu_periph_clock_enable(RCU_TIMER14);
  40. timer_deinit(TIMER14);
  41. timer_struct_para_init(&timer_initpara);
  42. timer_initpara.prescaler = psc;
  43. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  44. timer_initpara.period = arr;
  45. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  46. timer_init(TIMER14, &timer_initpara);
  47. timer_interrupt_enable(TIMER14, TIMER_INT_UP);
  48. nvic_irq_enable(TIMER14_IRQn, 2);
  49. timer_enable(TIMER14);
  50. }
  51. extern uint8_t relay_counter_flag[RelaySlaveChaNum];
  52. void TIMER14_IRQHandler(void)
  53. {
  54. if(timer_interrupt_flag_get(TIMER14, TIMER_INT_FLAG_UP) == SET)
  55. {
  56. timer_interrupt_flag_clear(TIMER14, TIMER_INT_FLAG_UP);
  57. }
  58. if(board)
  59. {
  60. count_1s ++;
  61. for(int i = 0 ; i < RelaySlaveChaNum;i++)
  62. {
  63. if(IS_RL_COUNTER_BEGINE_FLAG_EN(board->relay_staging_staus[i]))
  64. {
  65. if(rl_delay_cnt[i] < board->relay_limit_counter[i])
  66. {
  67. rl_delay_cnt[i]++;
  68. }else
  69. {
  70. rl_delay_cnt[i] = 0;
  71. CLEAR_RL_COUNTER_BEGIN_FLAG(board->relay_staging_staus[i]);
  72. SET_RL_COUNTER_END_FLAG(board->relay_staging_staus[i]);
  73. }
  74. }
  75. #if SUPPORT_PWM_OUT_RELAY
  76. if(rl_open_delay_flag[i])
  77. {
  78. if(rl_open_delay_count[i] < 3)
  79. {
  80. rl_open_delay_count[i]++;
  81. }else
  82. {
  83. rl_pwm_flag[i] = 1;
  84. rl_open_delay_count[i] = 0;
  85. rl_open_delay_flag[i] = 0;
  86. }
  87. }
  88. #endif
  89. }
  90. if(count_1s == 100)
  91. {
  92. board->flag_1s = 1;
  93. count_1s = 0;
  94. count_10s ++;
  95. if(count_10s == 10)
  96. {
  97. board->flag_10s = 1;
  98. count_10s = 0;
  99. }
  100. }
  101. count_100ms++;
  102. if(count_100ms == 10)
  103. {
  104. count_100ms = 0;
  105. board->flag_100ms = 1;
  106. }
  107. board->detection_1_count++;
  108. if(board->detection_1_count >= 100)
  109. {
  110. board->detection_1_count = 0;
  111. board->detection_1_value = 0;
  112. }
  113. #if SURPPORT_2_PT
  114. board->detection_2_count++;
  115. if(board->detection_2_count >= 100)
  116. {
  117. board->detection_2_count = 0;
  118. board->detection_2_value = 0;
  119. }
  120. #endif
  121. }
  122. }
  123. #if SUPPORT_PWM_OUT_RELAY
  124. static void timer15_config(uint16_t arr,uint16_t psc)
  125. {
  126. board = get_board();
  127. timer_parameter_struct timer_initpara;
  128. rcu_periph_clock_enable(RCU_TIMER15);
  129. timer_deinit(TIMER15);
  130. timer_struct_para_init(&timer_initpara);
  131. timer_initpara.prescaler = psc;
  132. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  133. timer_initpara.period = arr;
  134. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  135. timer_init(TIMER15, &timer_initpara);
  136. timer_interrupt_enable(TIMER15, TIMER_INT_UP);
  137. nvic_irq_enable(TIMER15_IRQn, 2);
  138. timer_enable(TIMER15);
  139. }
  140. void TIMER15_IRQHandler(void)
  141. {
  142. if(timer_interrupt_flag_get(TIMER15, TIMER_INT_FLAG_UP) == SET)
  143. {
  144. timer_interrupt_flag_clear(TIMER15, TIMER_INT_FLAG_UP);
  145. }
  146. board = get_board();
  147. uint32_t value = 0;
  148. for(int i = 0; i < RelaySlaveChaNum;i++)
  149. {
  150. if(board->md_data.r_data.state.state[i].state == 1 && rl_pwm_flag[i])
  151. {
  152. value |= (1<<(i));
  153. GPIO_TG(GPIOB) = BIT((8+i));
  154. }
  155. }
  156. if(value)
  157. {
  158. board->unlock();
  159. board->lock();
  160. }
  161. }
  162. #endif