/************************************************************************************************/
/**
* @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        (0xFFFFU)
#define TIM_PRESCALER_VALUE     (0x07U)
#define TIM_PULSE1_VALUE        (uint32_t)(TIM_PERIOD_VALUE/2)
#define TIM_PULSE2_VALUE        (uint32_t)(TIM_PERIOD_VALUE*37.5/100)
#define TIM_PULSE3_VALUE        (uint32_t)(TIM_PERIOD_VALUE/4) 
#define TIM_PULSE4_VALUE        (uint32_t)(TIM_PERIOD_VALUE*12.5/100)

/*-------------------------------------------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  GPIO初始化
* @retval 无
*/
void gpio_init(void)
{
    std_gpio_init_t tim3_gpio_init = {0};
    
    /* GPIOA时钟使能 */
    std_rcc_gpio_clk_enable(RCC_PERIPH_CLK_GPIOA);
    
    /* TIM3 GPIO 配置
    PA5     ------> TIM3_CH1
    PA6     ------> TIM3_CH2 
    PA7     ------> TIM3_CH3 
    PA2     ------> TIM3_CH4 */
    tim3_gpio_init.pin = GPIO_PIN_2 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7;
    tim3_gpio_init.mode = GPIO_MODE_ALTERNATE;
    tim3_gpio_init.pull = GPIO_NOPULL;
    tim3_gpio_init.alternate = GPIO_AF4_TIM3;
    std_gpio_init(GPIOA, &tim3_gpio_init); 
}


/**
* @brief  TIM3初始化
* @retval 无
*/
void tim3_init(void)
{
    std_tim_basic_init_t basic_init_struct = {0};
    std_tim_output_compare_init_t oc_config_struct = {0};
    
    /* TIM3时钟使能 */
    std_rcc_apb1_clk_enable(RCC_PERIPH_CLK_TIM3);
    
    /* 配置TIM3计数器参数 */
    basic_init_struct.prescaler = TIM_PRESCALER_VALUE;
    basic_init_struct.counter_mode = TIM_COUNTER_MODE_UP;
    basic_init_struct.period = TIM_PERIOD_VALUE;
    basic_init_struct.clock_div = TIM_CLOCK_DTS_DIV1;
    basic_init_struct.auto_reload_preload = TIM_AUTORELOAD_PRE_DISABLE;
    std_tim_init(TIM3, &basic_init_struct);
    
    /* 配置通道1输出模式为PWM1模式 */
    oc_config_struct.output_compare_mode = TIM_OUTPUT_MODE_PWM1;
    oc_config_struct.output_pol = TIM_OUTPUT_POL_HIGH;
    oc_config_struct.pulse = TIM_PULSE1_VALUE;
    oc_config_struct.output_state = TIM_OUTPUT_ENABLE;
    std_tim_output_compare_init(TIM3, &oc_config_struct, TIM_CHANNEL_1);
    
    /* 配置通道2输出模式为PWM1模式 */
    oc_config_struct.pulse = TIM_PULSE2_VALUE;
    std_tim_output_compare_init(TIM3, &oc_config_struct, TIM_CHANNEL_2);
    
    /* 配置通道3输出模式为PWM1模式 */
    oc_config_struct.pulse = TIM_PULSE3_VALUE;
    std_tim_output_compare_init(TIM3, &oc_config_struct, TIM_CHANNEL_3);
    
    /* 配置通道4输出模式为PWM1模式 */
    oc_config_struct.pulse = TIM_PULSE4_VALUE;
    std_tim_output_compare_init(TIM3, &oc_config_struct, TIM_CHANNEL_4);
}



