
#include "speed_to_hall.h"


speed_hall g_speed_to_hall = {0};

/**
  * @brief  初始化定时器+霍尔计算速度
  * @param  
  * @retval
  *
  */
void init_hall_speed_module(void)
{
    /* 计时中断 */
    timer_parameter_struct timer_initpara;

    rcu_periph_clock_enable(RCU_TIMER6);
    timer_struct_para_init(&timer_initpara);
    timer_deinit(TIMER6);

    // TIMER1 configuration
    timer_initpara.prescaler         = 99;
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;//计数模式，边缘对齐
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;//计数方向
    timer_initpara.period            = SPEED_TIMER_PERIOD;//计数重装载值，确定周期，，该值可在任意时刻进行修改以改变周期，TIMER_CAR(timer_periph) = (uint32_t)initpara->period;
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;//时钟分频
    timer_initpara.repetitioncounter = 0;//计数重复值
    timer_init(TIMER6, &timer_initpara);

    timer_auto_reload_shadow_enable(TIMER6);
    timer_enable(TIMER6);

    timer_interrupt_enable(TIMER6, TIMER_INT_UP);//定时器更新中断使能
    nvic_irq_enable(TIMER6_IRQn, 2U, 2U);

    /* 控制中断 */
    rcu_periph_clock_enable(RCU_TIMER12);
    timer_struct_para_init(&timer_initpara);
    timer_deinit(TIMER12);

    // TIMER1 configuration
    timer_initpara.prescaler         = 99;
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;//计数模式，边缘对齐
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;//计数方向
    timer_initpara.period            = 19999;//计数重装载值，确定周期，，该值可在任意时刻进行修改以改变周期，TIMER_CAR(timer_periph) = (uint32_t)initpara->period;
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;//时钟分频
    timer_initpara.repetitioncounter = 0;//计数重复值
    timer_init(TIMER12, &timer_initpara);

    timer_auto_reload_shadow_enable(TIMER12);
    timer_enable(TIMER12);

    timer_interrupt_enable(TIMER12, TIMER_INT_UP);//定时器更新中断使能
    nvic_irq_enable(TIMER7_UP_TIMER12_IRQn, 1U, 2U);
}

/**
  * @brief  定时器6中断服务函数
  * @param  
  * @retval
  *
  */
void TIMER6_IRQHandler(void)
{
    if(RESET != timer_interrupt_flag_get(TIMER6, TIMER_INT_FLAG_UP)) {
        g_speed_to_hall.time_60ms++;
        if (g_speed_to_hall.time_60ms >= TIME_60MS_MAX) {
            g_speed_to_hall.time_60ms = 0;
        }
        timer_interrupt_flag_clear(TIMER6, TIMER_INT_FLAG_UP);
    }
}

/**
  * @brief  定时器7中断服务函数
  * @param  
  * @retval
  *
  */
void TIMER7_UP_TIMER12_IRQHandler(void)
{
    if(RESET != timer_interrupt_flag_get(TIMER12, TIMER_INT_FLAG_UP)) {
        get_hori_speed();
        get_vert_speed();

        timer_interrupt_flag_clear(TIMER12, TIMER_INT_FLAG_UP);
    }
}





