#include "timer.h"

void timer_nvic_config(void)
{
    NVIC_InitTypeDef NVIC_InitStructure;
    // Usart1 NVIC 配置
    NVIC_InitStructure.NVIC_IRQChannel                   = TIM3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}

void timer_config(void)
{
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); ///使能TIM2时钟

    /* TIMER1 configuration */
    TIM_TimeBaseInitStructure.TIM_Period        = 16800;              // 239;
    TIM_TimeBaseInitStructure.TIM_CounterMode   = TIM_CounterMode_Up; //向上计数模式
    TIM_TimeBaseInitStructure.TIM_Prescaler     = 10000;              // 7999;
    TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure); //初始化TIM2

    TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); //允许定时器2更新中断

    TIM_Cmd(TIM3, ENABLE); //使能定时器3
}

void user_timer_init(void)
{
    timer_nvic_config();
    timer_config();
}

void TIM3_IRQHandler(void)
{
    // static uint16_t cnt = 0;
    // if (cnt >= 100)
    // {
    //     IWDG_ReloadCounter();
    //     cnt = 0;
    // }
    // else
    // {
    //     cnt++;
    // }
    if (TIM_GetITStatus(TIM3, TIM_IT_Update))
    {
        IWDG_ReloadCounter();
    }
    TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
}
