#include "common.h"
#include "drv_adas.h"

void ADAS_Timer_Initial(void)
{
   
    timer_parameter_struct timer_initpara;

    rcu_periph_clock_enable(RCU_TIMER2);

    timer_deinit(TIMER2);
    /* initialize TIMER init parameter struct */
    timer_struct_para_init(&timer_initpara);
    /* TIMER1 configuration */
    timer_initpara.prescaler         = 107;
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;
    timer_initpara.period            = 400;
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
	timer_initpara.repetitioncounter = 0;
    timer_init(TIMER2, &timer_initpara);

    timer_interrupt_enable(TIMER2, TIMER_INT_UP);
    timer_disable(TIMER2);

	nvic_irq_enable(TIMER2_IRQn, 1, 0);
	
}



void TIMER2_IRQHandler(void)
{
	if(SET == timer_interrupt_flag_get(TIMER2, TIMER_INT_UP))
	{
        /* clear channel 0 interrupt bit */
        timer_interrupt_flag_clear(TIMER2, TIMER_INT_UP);
		
		timer_disable(TIMER1);

		timer_disable(TIMER2);

		ADAS_PWM_IO_Reset();
       
    }
	
}

/**
    \brief      configure the GPIO ports
    \param[in]  none
    \param[out] none
    \retval     none
  */
void ADAS_PWM_IO_Reset(void)
{
    rcu_periph_clock_enable(RCU_GPIOB);

    /*Configure PA1 PA2 PA3(TIMER1 CH1 CH2 CH3) as alternate function*/
	gpio_pin_remap_config(GPIO_TIMER1_PARTIAL_REMAP1,DISABLE);
    gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_10|GPIO_PIN_11);
	gpio_bit_reset(GPIOB,GPIO_PIN_10);
	gpio_bit_reset(GPIOB,GPIO_PIN_11);
}

/**
    \brief      configure the TIMER peripheral
    \param[in]  none
    \param[out] none
    \retval     none
  */
void ADAS_PWM_Initial(uint8_t pwm_no)
{
	uint16_t chno;
	uint32_t pin;
    /* -----------------------------------------------------------------------
    TIMER1 configuration: generate 3 PWM signals with 3 different duty cycles:
    TIMER1CLK = SystemCoreClock / 108 = 1MHz

    TIMER1 channel1 duty cycle = (4000/ 16000)* 100  = 25%
    TIMER1 channel2 duty cycle = (8000/ 16000)* 100  = 50%
    TIMER1 channel3 duty cycle = (12000/ 16000)* 100 = 75%
    ----------------------------------------------------------------------- */
    timer_oc_parameter_struct timer_ocintpara;
    timer_parameter_struct timer_initpara;


	switch(pwm_no)
	{
		case ADAS_PWM_1:
			chno = TIMER_CH_2;
			pin = GPIO_PIN_10;
			break;
		case ADAS_PWM_2:
			chno = TIMER_CH_3;
			pin = GPIO_PIN_11;
			break;
		default:
			return;
	}

	rcu_periph_clock_enable(RCU_GPIOB);
    rcu_periph_clock_enable(RCU_AF);

    /*Configure PA1 PA2 PA3(TIMER1 CH1 CH2 CH3) as alternate function*/
    gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, pin);	
	gpio_pin_remap_config(GPIO_TIMER1_PARTIAL_REMAP1,ENABLE);


	
    rcu_periph_clock_enable(RCU_TIMER1);

    timer_deinit(TIMER1);

    /* TIMER1 configuration 2M*/
    timer_initpara.prescaler         = 53;
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;
    timer_initpara.period            = 49;
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 0;
    timer_init(TIMER1,&timer_initpara);

    /* CH2 and CH3 configuration in PWM mode1 */
    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(TIMER1,chno,&timer_ocintpara);



    /* CH2 configuration in PWM mode1,duty cycle 50% */
    timer_channel_output_pulse_value_config(TIMER1,chno,24);
    timer_channel_output_mode_config(TIMER1,chno,TIMER_OC_MODE_PWM0);
    timer_channel_output_shadow_config(TIMER1,chno,TIMER_OC_SHADOW_DISABLE);

  
    /* auto-reload preload enable */
    timer_auto_reload_shadow_enable(TIMER1);
    /* auto-reload preload enable */
    timer_enable(TIMER1);
	
}




//***********************************************************************************************************************//
void ADAS_Receive_Timer_Initial(void)
{
   
    timer_parameter_struct timer_initpara;

    rcu_periph_clock_enable(RCU_TIMER0);

    timer_deinit(TIMER0);
    /* initialize TIMER init parameter struct */
    timer_struct_para_init(&timer_initpara);
    /* TIMER1 configuration */
    timer_initpara.prescaler         = 107;
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;
    timer_initpara.period            = 0xFFFFFFFF;
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
	timer_initpara.repetitioncounter = 0;
    timer_init(TIMER0, &timer_initpara);

    timer_interrupt_disable(TIMER0, TIMER_INT_UP);
    timer_disable(TIMER0);

	//nvic_irq_enable(TIMER7_IRQn, 1, 0);
	
}

void ADAS_Drv_IO_Initial(void)
{
	rcu_periph_clock_enable(RCU_GPIOC);
	
	gpio_init(GPIOC,GPIO_MODE_IPU,GPIO_OSPEED_50MHZ,GPIO_PIN_8|GPIO_PIN_9);

	gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOC, GPIO_PIN_SOURCE_8);
	exti_init(EXTI_8, EXTI_INTERRUPT, EXTI_TRIG_BOTH);
    exti_interrupt_flag_clear(EXTI_8);

	gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOC, GPIO_PIN_SOURCE_9);
	exti_init(EXTI_9, EXTI_INTERRUPT, EXTI_TRIG_BOTH);
    exti_interrupt_flag_clear(EXTI_9);
}
/*!
    \brief      main function
    \param[in]  none
    \param[out] none
    \retval     none
*/
void ADAS_Drv_Initial(void)
{
    ADAS_PWM_IO_Reset();
	
	ADAS_Timer_Initial();

	ADAS_Receive_Timer_Initial();

	ADAS_Drv_IO_Initial();
}




