/************************************************************************************************/
/**
* @file               tim_bsp.c
* @author             MCU Ecosystem Development Team
* @brief              TIM BSP驱动函数,实现TIM PWM输入波形计数
*                           
*
**************************************************************************************************
* @attention
* Copyright (c) CEC Huada Electronic Design Co.,Ltd. All rights reserved.
*
**************************************************************************************************
*/


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

/*------------------------------------------variables-------------------------------------------*/
uint32_t g_enter_cnt = 0U;

uint32_t g_ch1_ccx_value = 0U;
uint32_t g_ch2_ccx_value = 0U;

uint32_t g_pwm_duty = 0x0U;
uint32_t g_pwm_frequency = 0x0U;

/*-------------------------------------------functions------------------------------------------*/
/**
* @brief  TIM3中断处理程序
* @retval 无
*/
void TIM3_IRQHandler(void)
{
    /* CC1上升沿捕获 */
    if ((std_tim_get_flag(TIM3, TIM_FLAG_CC1)) && (std_tim_get_interrupt_enable(TIM3, TIM_INTERRUPT_CC1)))
    {
        /* 从模式控制器配置为复位模式，且TI1FP1信号用作触发输入信号，首次捕获的CC值忽略 */
        if (g_enter_cnt == 0)
        {
            g_enter_cnt = 1;
            std_tim_get_ccx_value(TIM3, TIM_CHANNEL_1);
        }
        
        /* 获取捕获的CC值 */
        else if (g_enter_cnt == 1)
        {
            /* 关闭CC1中断 */
            std_tim_interrupt_disable(TIM3, TIM_INTERRUPT_CC1);
            g_ch1_ccx_value = std_tim_get_ccx_value(TIM3, TIM_CHANNEL_1);
            
            /* 计算占空比 */
            g_pwm_duty = ((g_ch2_ccx_value + 1) * 100) / (g_ch1_ccx_value + 1);
            
            /* 计算输入频率 */
            g_pwm_frequency = (std_rcc_get_pclkfreq() / (g_ch1_ccx_value + 1));
        }
    }
    
    /* CC2下降沿捕获 */
    if ((std_tim_get_flag(TIM3, TIM_FLAG_CC2)) && (std_tim_get_interrupt_enable(TIM3, TIM_INTERRUPT_CC2)))
    {
        /* 关闭CC2中断 */
        std_tim_interrupt_disable(TIM3, TIM_INTERRUPT_CC2);
        g_ch2_ccx_value = std_tim_get_ccx_value(TIM3, TIM_CHANNEL_2);
        
    }    
}

/**
* @brief  TIM3启动计数，并使能捕获事件中断
* @retval 无
*/
void bsp_tim3_capture_start(void)
{
    /* 使能TIM3输入捕获通道1和通道2中断 */
    std_tim_interrupt_enable(TIM3, TIM_INTERRUPT_CC1 | TIM_INTERRUPT_CC2);
    
    /* 使能输入捕获通道 */
    std_tim_ccx_channel_enable(TIM3, TIM_CHANNEL_1);
    std_tim_ccx_channel_enable(TIM3, TIM_CHANNEL_2);
    
    /* 启动TIM3计数 */
    std_tim_enable(TIM3);

}

/**
* @brief  配置MCO输出RCH的16分频时钟
* @retval 无
*/
void bsp_rcc_mco_config(void)
{
    std_gpio_init_t mco_gpio_cfg = {0};
    
    /* 配置PA8作为MCO输出引脚 */    
    std_rcc_gpio_clk_enable(RCC_PERIPH_CLK_GPIOA);
    
    mco_gpio_cfg.pin = GPIO_PIN_8;
    mco_gpio_cfg.mode = GPIO_MODE_ALTERNATE;
    mco_gpio_cfg.alternate = GPIO_AF7_MCO;
    mco_gpio_cfg.pull = GPIO_NOPULL;
    
    std_gpio_init(GPIOA, &mco_gpio_cfg);
    
    /* MCO输出RCH48 64分频（750KHz） */
    std_rcc_mco_config(RCC_MCO_SRC_RCH, RCC_MCO_DIV64);
}
