/********************************************************************************************************* * Module : timer * Abstract : defined data structure! * Version : 1.0.0 * Author : Leopul(COPYRIGHT 2025 ~ 2027 Leopul. All rights reserved.) * Complete : 2025-04-09 * Content : None * Note : None *********************************************************************************************************/ #include "timer.h" #include "gd32e23x.h" #include "board_cfg.h" #include "common.h" static board_t *board = 0; static void timer14_config(uint16_t arr,uint16_t psc); static void relay_remember(void); static uint16_t count_1s = 0; static uint16_t count_10s = 0; static uint16_t count_100ms = 0; uint16_t rl_delay_cnt[RelaySlaveChaNum] = {0}; uint16_t rl_delay_flag[RelaySlaveChaNum] = {0}; #if SUPPORT_PWM_OUT_RELAY uint8_t rl_open_delay_flag[RelaySlaveChaNum] = {0}; uint8_t rl_open_delay_count[RelaySlaveChaNum] = {0}; uint8_t rl_pwm_flag[RelaySlaveChaNum] = {0}; static void timer15_config(uint16_t arr,uint16_t psc); #endif void time_init(void) { timer14_config(9999,71); #if SUPPORT_PWM_OUT_RELAY timer15_config(24,71); #endif } static void timer14_config(uint16_t arr,uint16_t psc) { board = get_board(); timer_parameter_struct timer_initpara; rcu_periph_clock_enable(RCU_TIMER14); timer_deinit(TIMER14); timer_struct_para_init(&timer_initpara); timer_initpara.prescaler = psc; timer_initpara.counterdirection = TIMER_COUNTER_UP; timer_initpara.period = arr; timer_initpara.clockdivision = TIMER_CKDIV_DIV1; timer_init(TIMER14, &timer_initpara); timer_interrupt_enable(TIMER14, TIMER_INT_UP); nvic_irq_enable(TIMER14_IRQn, 2); timer_enable(TIMER14); } extern uint8_t relay_counter_flag[RelaySlaveChaNum]; void TIMER14_IRQHandler(void) { if(timer_interrupt_flag_get(TIMER14, TIMER_INT_FLAG_UP) == SET) { timer_interrupt_flag_clear(TIMER14, TIMER_INT_FLAG_UP); } if(board) { count_1s ++; for(int i = 0 ; i < RelaySlaveChaNum;i++) { if(IS_RL_COUNTER_BEGINE_FLAG_EN(board->relay_staging_staus[i])) { if(rl_delay_cnt[i] < board->relay_limit_counter[i]) { rl_delay_cnt[i]++; }else { rl_delay_cnt[i] = 0; CLEAR_RL_COUNTER_BEGIN_FLAG(board->relay_staging_staus[i]); SET_RL_COUNTER_END_FLAG(board->relay_staging_staus[i]); } } #if SUPPORT_PWM_OUT_RELAY if(rl_open_delay_flag[i]) { if(rl_open_delay_count[i] < 3) { rl_open_delay_count[i]++; }else { rl_pwm_flag[i] = 1; rl_open_delay_count[i] = 0; rl_open_delay_flag[i] = 0; } } #endif } if(count_1s == 100) { board->flag_1s = 1; count_1s = 0; count_10s ++; if(count_10s == 10) { board->flag_10s = 1; count_10s = 0; } } count_100ms++; if(count_100ms == 10) { count_100ms = 0; board->flag_100ms = 1; } board->detection_1_count++; if(board->detection_1_count >= 100) { board->detection_1_count = 0; board->detection_1_value = 0; } #if SURPPORT_2_PT board->detection_2_count++; if(board->detection_2_count >= 100) { board->detection_2_count = 0; board->detection_2_value = 0; } #endif } } #if SUPPORT_PWM_OUT_RELAY static void timer15_config(uint16_t arr,uint16_t psc) { board = get_board(); timer_parameter_struct timer_initpara; rcu_periph_clock_enable(RCU_TIMER15); timer_deinit(TIMER15); timer_struct_para_init(&timer_initpara); timer_initpara.prescaler = psc; timer_initpara.counterdirection = TIMER_COUNTER_UP; timer_initpara.period = arr; timer_initpara.clockdivision = TIMER_CKDIV_DIV1; timer_init(TIMER15, &timer_initpara); timer_interrupt_enable(TIMER15, TIMER_INT_UP); nvic_irq_enable(TIMER15_IRQn, 2); timer_enable(TIMER15); } void TIMER15_IRQHandler(void) { if(timer_interrupt_flag_get(TIMER15, TIMER_INT_FLAG_UP) == SET) { timer_interrupt_flag_clear(TIMER15, TIMER_INT_FLAG_UP); } board = get_board(); uint32_t value = 0; for(int i = 0; i < RelaySlaveChaNum;i++) { if(board->md_data.r_data.state.state[i].state == 1 && rl_pwm_flag[i]) { value |= (1<<(i)); GPIO_TG(GPIOB) = BIT((8+i)); } } if(value) { board->unlock(); board->lock(); } } #endif