#include "gd32f4xx_timer.h"
#include "Timer.h"
#include "gd32f4xx_rcu.h"

#define timer_frequence   10000000  //定时器频率1MHZ


/*!
    \brief      none 水平输出频率
    \param[in]  none
    \param[out] none
    \retval     none
    \note       LH  @2022.07.11
*/
void ptz_hori_timer_start(unsigned int f)
{
  unsigned int counter,chcv;
  counter = (unsigned int)(timer_frequence / f + 0.5);
  chcv = (unsigned int)(counter / 2.0 +0.5);
  TIMER_CAR(TIMER3) = counter;
  TIMER_CH0CV(TIMER3) = chcv;
  timer_enable(TIMER3);
}

/*!
    \brief      none 水平停止
    \param[in]  none
    \param[out] none
    \retval     none
    \note       LH  @2022.07.11
*/
void ptz_hori_timer_stop()
{
  TIMER_CH0CV(TIMER3) = 0;
  timer_disable(TIMER3);
}




/*!
    \brief      none 垂直输出频率
    \param[in]  none
    \param[out] none
    \retval     none
    \note       LH  @2022.07.11
*/
void ptz_vert_timer_start(unsigned int f)
{
  unsigned int counter,chcv;
  counter = (unsigned int)(timer_frequence / f + 0.5);
  chcv = (unsigned int)(counter / 2.0 +0.5);
  TIMER_CAR(TIMER2) = counter;
  TIMER_CH0CV(TIMER2) = chcv;
  timer_enable(TIMER2);
}

/*!
    \brief      none 垂直停止
    \param[in]  none
    \param[out] none
    \retval     none
    \note       LH  @2022.07.11
*/
void ptz_vert_timer_stop()
{
  TIMER_CH0CV(TIMER2) = 0;
  timer_disable(TIMER2);
}


/*!
    \brief      none 步进电机定时器初始化
    \param[in]  none
    \param[out] none
    \retval     none
    \note       LH  @2022.07.11
*/
static void Step_timer_cfg(void)
{//APB1总线时钟频率100MHz
    timer_parameter_struct timer_initpara;
    timer_oc_parameter_struct timer_ocintpara;

    rcu_periph_clock_enable(RCU_TIMER2);
    rcu_periph_clock_enable(RCU_TIMER3);
//    rcu_timer_clock_prescaler_config(RCU_TIMER_PSC_MUL4);
    timer_struct_para_init(&timer_initpara);
    timer_deinit(TIMER2);
    timer_deinit(TIMER3);

    // TIMER1 configuration
    timer_initpara.prescaler         = 9;//分频系数，确定频率为1MHZ
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;//计数模式，边缘对齐
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;//计数方向
    timer_initpara.period            = 0;//计数重装载值，确定周期，，该值可在任意时刻进行修改以改变周期，TIMER_CAR(timer_periph) = (uint32_t)initpara->period;
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;//时钟分频
    timer_initpara.repetitioncounter = 0;//计数重复值
    timer_init(TIMER2,&timer_initpara);
    timer_init(TIMER3,&timer_initpara);
    
    // CH0 configuration in PWM mode 0 
    timer_channel_output_struct_para_init(&timer_ocintpara);
    timer_ocintpara.ocpolarity  = TIMER_OC_POLARITY_HIGH;
    timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
    timer_ocintpara.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
    timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
    timer_ocintpara.ocidlestate  = TIMER_OC_IDLE_STATE_LOW;
    timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;

    timer_channel_output_config(TIMER2,TIMER_CH_0,&timer_ocintpara);
    timer_channel_output_config(TIMER3,TIMER_CH_0,&timer_ocintpara);

    // CH0 configuration in PWM mode 0,duty cycle 
    timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_0,0);//输出无效低电平，TIMER_CH0CV(timer_periph) = (uint32_t)pulse;
    timer_channel_output_mode_config(TIMER2,TIMER_CH_0,TIMER_OC_MODE_PWM0);
    timer_channel_output_shadow_config(TIMER2,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);
    // CH0 configuration in PWM mode 0,duty cycle 
    timer_channel_output_pulse_value_config(TIMER3,TIMER_CH_0,0);
    timer_channel_output_mode_config(TIMER3,TIMER_CH_0,TIMER_OC_MODE_PWM0);
    timer_channel_output_shadow_config(TIMER3,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);

    // auto-reload preload enable 
    timer_auto_reload_shadow_enable(TIMER2);
    timer_enable(TIMER2);
    timer_auto_reload_shadow_enable(TIMER3);
    timer_enable(TIMER3);

}


/*!
    \brief      none 引脚复用
    \param[in]  none
    \param[out] none
    \retval     none
    \note       LH  @2022.07.11
*/
static void timer_gpio_init(void)
{
  rcu_periph_clock_enable(RCU_GPIOC);
  rcu_periph_clock_enable(RCU_GPIOD);
  //H-STEP，timer3-ch0
  gpio_mode_set(GPIOD, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_12);
  gpio_output_options_set(GPIOD, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_12);   
  //V-STEP，timer2-ch0
  gpio_mode_set(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_6);
  gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_6);   
  
  //引脚复用
  gpio_af_set(GPIOD, GPIO_AF_2, GPIO_PIN_12);
  gpio_af_set(GPIOC, GPIO_AF_2, GPIO_PIN_6);
  
}



void step_time_init()
{
  Step_timer_cfg();
  timer_gpio_init();
  
}




