timer.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. static uint32_t read_count = 0;
  21. uint16_t rl_delay_cnt[RelaySlaveChaNum] = {0};
  22. void time_init(void)
  23. {
  24. timer14_config(9999,71);
  25. }
  26. static void timer14_config(uint16_t arr,uint16_t psc)
  27. {
  28. board = get_board();
  29. timer_parameter_struct timer_initpara;
  30. rcu_periph_clock_enable(RCU_TIMER14);
  31. timer_deinit(TIMER14);
  32. timer_struct_para_init(&timer_initpara);
  33. timer_initpara.prescaler = psc;
  34. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  35. timer_initpara.period = arr;
  36. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  37. timer_init(TIMER14, &timer_initpara);
  38. timer_interrupt_enable(TIMER14, TIMER_INT_UP);
  39. nvic_irq_enable(TIMER14_IRQn, 2);
  40. timer_enable(TIMER14);
  41. }
  42. void TIMER14_IRQHandler(void)
  43. {
  44. if(timer_interrupt_flag_get(TIMER14, TIMER_INT_FLAG_UP) == SET)
  45. {
  46. timer_interrupt_flag_clear(TIMER14, TIMER_INT_FLAG_UP);
  47. }
  48. if(board)
  49. {
  50. count_1s ++;
  51. for(int i = 0 ; i < RelaySlaveChaNum;i++)
  52. {
  53. if(IS_RL_COUNTER_BEGINE_FLAG_EN(board->relay_staging_staus[i]))
  54. {
  55. if(rl_delay_cnt[i] < board->relay_limit_counter[i])
  56. {
  57. rl_delay_cnt[i]++;
  58. }else
  59. {
  60. rl_delay_cnt[i] = 0;
  61. CLEAR_RL_COUNTER_BEGIN_FLAG(board->relay_staging_staus[i]);
  62. SET_RL_COUNTER_END_FLAG(board->relay_staging_staus[i]);
  63. }
  64. }
  65. }
  66. if(count_1s == 100)
  67. {
  68. board->flag_1s = 1;
  69. count_1s = 0;
  70. count_10s ++;
  71. if(count_10s == 10)
  72. {
  73. board->flag_10s = 1;
  74. count_10s = 0;
  75. }
  76. }
  77. count_100ms++;
  78. if(count_100ms == 10)
  79. {
  80. count_100ms = 0;
  81. board->flag_100ms = 1;
  82. }
  83. #ifdef SUPPORT_IM1253E
  84. if(board->flag_im1253_init)
  85. {
  86. read_count ++;
  87. if(read_count >= 10000)
  88. {
  89. board->flag_im1253_init = 0;
  90. read_count = 0;
  91. }
  92. }
  93. #endif
  94. }
  95. }