/********************************************************************************
 * @file    TIM_InputCapture.c
 * @author  SOC AE Team
 * @version V1.1
 * @date    2025-06-27
 * @brief   捕获在TIM1 Tx上PWM波形的周期和占空比。
 *
 *******************************************************************************/
#include "sc32_conf.h"
#include "Function_Init.h"
#include "stdio.h"

typedef	struct
{
  uint16_t FirstRising;
  uint16_t FirstFailing;
  uint16_t SecondRising;
}
TIM_ICValueTypeDef;

TIM_ICValueTypeDef TIM_ICValue;
uint8_t TIM_ICNum = 0;

void TIM_InputCapture_Function(void)
{
  volatile uint16_t TIM_IC_Cycle, TIM_IC_Duty;
  TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
  TIM_IC_InitTypeDef TIM_IC_InitStruct;

  /* 使能TIM1时钟 */
  RCC_APB0Cmd(ENABLE);
  RCC_APB0PeriphClockCmd(RCC_APB0Periph_TIM1, ENABLE);

  /* TIM配置结构体初始化 */
  TIM_TimeBaseStructInit(&TIM_TimeBaseInitStruct);
  TIM_ICStructInit(&TIM_IC_InitStruct);

  /* 设置TIM时基配置结构体 */
  TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;	//TIM工作在向上计数模式
  TIM_TimeBaseInitStruct.TIM_EXENX = TIM_EXENX_Disable;	//外部重加载管脚失能
#if defined(SC32f10xx)||defined(SC32f12xx)
  TIM_TimeBaseInitStruct.TIM_Preload = 65535 - (32000000 / 1000);	//设置TIM的定时器评论为1000Hz@APB0Clock=32M
#endif
  TIM_TimeBaseInitStruct.TIM_Prescaler = TIM_PRESCALER_1;	//TIM分频为1分频
  TIM_TimeBaseInitStruct.TIM_WorkMode = TIM_WorkMode_Timer;	//TIM工作在定时器模式，时钟源为APB0时钟

  /* TIM时基配置 */
  TIM_TIMBaseInit(TIM1, &TIM_TimeBaseInitStruct);

  /* 设置TIM捕获通道配置结构体 */
  TIM_IC_InitStruct.TIM_FICPIN = TIM_FICPin_Tn;
  TIM_IC_InitStruct.TIM_RICPIN = TIM_RICPin_Tn;

  /* TIM捕获配置 */
  TIM_ICInit(TIM1, &TIM_IC_InitStruct);
  TIM_ICCmd(TIM1,ENABLE);
  /* 使能TIM1中断 */
  TIM_ITConfig(TIM1, TIM_IT_TI | TIM_IT_INTEN | TIM_IT_EXF | TIM_IT_EXR, ENABLE);
  NVIC_EnableIRQ(TIMER1_IRQn);

  /* TIM外设初始化 */
  TIM_Cmd(TIM1, ENABLE);

  while(1)
  {
    if(TIM_ICNum == 3)
    {
      /* 周期为两次上升沿的差值，占空比为下降沿减去第一次上升沿的差值 */
      TIM_IC_Cycle = TIM_ICValue.SecondRising - TIM_ICValue.FirstRising;
      TIM_IC_Duty = TIM_ICValue.FirstFailing - TIM_ICValue.FirstRising;
      
      /* 打印捕获值 */
      printf("The captured cycle is %d.\r\n", TIM_IC_Cycle);
      printf("The captured duty is %d.\r\n ", TIM_IC_Duty);
      
      TIM_ICNum = 0;
    }
  }
}

void TIM_InputCapture_TIM1Handler(void)
{
  /* 判断TIM1中断是否置起 */
  if(TIM_GetFlagStatus(TIM1, TIM_Flag_TI) == SET)
  {
    TIM_ClearFlag(TIM1, TIM_Flag_TI);	//清除TIM1中断
    TIM_ICNum = 0;	//清除捕获标志值
  }
  if(TIM_GetFlagStatus(TIM1, TIM_Flag_EXR) == SET)
  {
    TIM_ClearFlag(TIM1, TIM_Flag_EXR);	//清除TIM1中断
    if(TIM_ICNum == 0)	//采集到第一次上升沿
    {
      TIM_ICNum ++;
      TIM_ICValue.FirstRising = TIM_GetRisingCapture(TIM1);
    }
    if(TIM_ICNum == 2)	//采集到第二次上升沿
    {
      TIM_ICNum ++;
      TIM_ICValue.SecondRising = TIM_GetRisingCapture(TIM1);
    }
  }
  if(TIM_GetFlagStatus(TIM1, TIM_Flag_EXF) == SET)
  {
    TIM_ClearFlag(TIM1, TIM_Flag_EXF);	//清除TIM1中断
    if(TIM_ICNum == 1)	//采集到第一次下降沿
    {
      TIM_ICNum++;
      TIM_ICValue.FirstFailing = TIM_GetFailingCapture(TIM1);
    }
  }
}
