/************************************************************************************************/
/**
* @file               common.c
* @author             MCU Ecosystem Development Team
* @brief              通用函数或本外设相关的配置实现函数。
*                           
*
**************************************************************************************************
* @attention
* Copyright (c) CEC Huada Electronic Design Co.,Ltd. All rights reserved.
*
**************************************************************************************************
*/


/*------------------------------------------includes--------------------------------------------*/
#include "common.h"


/*--------------------------------------------define--------------------------------------------*/
#define  TIM_PERIOD_VALUE       0xFFF
#define  TIM_PULSE1_VALUE       (uint32_t)(TIM_PERIOD_VALUE/2)        /* Capture Compare 1 Value  */
#define  TIM_PULSE2_VALUE       (uint32_t)(TIM_PERIOD_VALUE*37.5/100) /* Capture Compare 2 Value  */
#define  TIM_PULSE3_VALUE       (uint32_t)(TIM_PERIOD_VALUE/4)        /* Capture Compare 3 Value  */


/*-------------------------------------------functions------------------------------------------*/
/**
* @brief  系统时钟配置
* @retval 无
*/
void system_clock_config(void)
{
    uint32_t ahbdiv_tmp;
    
    /* 设置Flash读访问等待时间 */
    std_flash_set_latency(FLASH_LATENCY_1CLK);

    /* 使能RCH */
    std_rcc_rch_enable();
    while(!std_rcc_get_rch_ready());
    
    /* 备份AHB分频参数 */
    ahbdiv_tmp = std_rcc_get_ahbdiv();    
    /* 配置AHB最大分频：128分频 */
    std_rcc_set_ahbdiv(RCC_HCLK_DIV128);  
    
    /* 切换RCH4到RCH48 */    
    std_rcc_set_rch_source(RCC_RCH_FREQ_48M);
    
    /* 恢复AHB分频参数 */
    std_rcc_set_ahbdiv(ahbdiv_tmp);     

    /* 设置系统时钟源为RCH */
    std_rcc_set_sysclk_source(RCC_SYSCLK_SRC_RCH);
    while(std_rcc_get_sysclk_source() != RCC_SYSCLK_SRC_STATUS_RCH);

    /* 设置AHB分频因子 */
    std_rcc_set_ahbdiv(RCC_HCLK_DIV1);
    /* 设置APB分频因子 */
    std_rcc_set_apbdiv(RCC_PCLK_DIV1);
    
    SystemCoreClockUpdate();
}


/**
* @brief  TIM1初始化
* @retval 无
*/
void tim1_init(void)
{
    std_tim_basic_init_t basic_init = {0};    
    std_tim_output_compare_init_t output_init = {0};
    std_tim_break_init_t break_config = {0};
    
    /* TIM1时钟使能 */
    std_rcc_apb2_clk_enable(RCC_PERIPH_CLK_TIM1);
        
    /* 配置TIM1计数器参数 */
    basic_init.counter_mode = TIM_COUNTER_MODE_UP;
    basic_init.period = TIM_PERIOD_VALUE;
    basic_init.clock_div = TIM_CLOCK_DTS_DIV1;
    std_tim_init(TIM1, &basic_init);
       
    /* 配置通道1的比较输出参数 */
    output_init.output_compare_mode = TIM_OUTPUT_MODE_FROZEN;
    output_init.pulse = TIM_PULSE1_VALUE;
    output_init.output_pol = TIM_OUTPUT_POL_HIGH; 
    output_init.output_negtive_pol = TIM_OUTPUT_NEGTIVE_POL_HIGH; 
    output_init.output_idle_state = TIM_OUTPUT_IDLE_SET; 
    output_init.output_negtive_idle_state = TIM_OUTPUT_NEGTIVE_IDLE_RESET;
    output_init.output_state = TIM_OUTPUT_ENABLE;
    output_init.output_negtive_state = TIM_OUTPUT_NEGTIVE_ENABLE;
    std_tim_output_compare_init(TIM1, &output_init, TIM_CHANNEL_1);
   
    /* 配置通道2的比较输出参数 */
    output_init.pulse = TIM_PULSE2_VALUE;
    std_tim_output_compare_init(TIM1, &output_init, TIM_CHANNEL_2);    

    /* 配置通道3的比较输出参数 */ 
    output_init.pulse = TIM_PULSE3_VALUE;
    std_tim_output_compare_init(TIM1, &output_init, TIM_CHANNEL_3);    
    
    /* 配置断路输入源为GPIO */
    std_tim_break_source_enable(TIM1, TIM_BREAK_INPUT_SRC_GPIO);
    std_tim_set_break_pol(TIM1, TIM_BREAK_INPUT_SRC_GPIO, TIM_BREAK_INPUT_POL_HIGH);
    
    /* 配置断路输入参数 */
    break_config.off_state_run_mode = TIM_OSSR_ENABLE;
    break_config.off_state_idle_mode = TIM_OSSI_ENABLE;
    break_config.lock_level = TIM_LOCK_LEVEL_OFF;
    break_config.dead_time = 0x3F;
    break_config.break_state = TIM_BREAK_ENABLE;    
    std_tim_break_deadtime_init(TIM1, &break_config);    
}

/**
* @brief  GPIO初始化
* @retval 无
*/
void gpio_init(void)
{
    std_gpio_init_t gpio_config = {0};
    
    /* GPIOA、GPIOB时钟使能 */
    std_rcc_gpio_clk_enable(RCC_PERIPH_CLK_GPIOA | RCC_PERIPH_CLK_GPIOB);
        
    /* TIM1 GPIO 配置
    PB8     ------> TIM1_BK
    PA1     ------> TIM1_CH3
    PA2     ------> TIM1_CH2
    PA3     ------> TIM1_CH1

    PA4     ------> TIM1_CH1N
    PA5     ------> TIM1_CH2N
    PA6     ------> TIM1_CH3N  */
    gpio_config.pin = GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6;
    gpio_config.mode = GPIO_MODE_ALTERNATE;
    gpio_config.pull = GPIO_NOPULL;
    gpio_config.alternate = GPIO_AF3_TIM1;
    std_gpio_init(GPIOA, &gpio_config);
    
    gpio_config.pin = GPIO_PIN_8;
    gpio_config.mode = GPIO_MODE_ALTERNATE;    
    gpio_config.pull = GPIO_PULLDOWN;
    gpio_config.alternate = GPIO_AF3_TIM1;    
    std_gpio_init(GPIOB, &gpio_config);       
}


/**
* @brief  NVIC初始化，使能TIM1、SysTick中断
* @retval 无
*/
void nvic_init(void)
{
    /* 配置SysTick中断优先级 */
    NVIC_SetPriority(SysTick_IRQn, NVIC_PRIO_0);
    
    /* 配置TIM1中断使能 */
    NVIC_SetPriority(TIM1_BRK_UP_TRIG_COM_IRQn, NVIC_PRIO_1);
    NVIC_EnableIRQ(TIM1_BRK_UP_TRIG_COM_IRQn);
}

