#include "stm32f0xx_hal.h"
#include "pwm_in.h"
#include <stdio.h>


#define PWM_IN_TIM_FREQ     1000000

static TIM_HandleTypeDef       TimHandle;
static TIM_IC_InitTypeDef      sConfig;
static TIM_SlaveConfigTypeDef  sSlaveConfig;

static uint16_t prescaler;
static uint32_t tim_freq;

__IO uint32_t cap_value_1 = 0;  // Captured Value
__IO uint32_t uwDutyCycle = 0; // Duty Cycle Value
__IO uint32_t uwFrequency = 0; // Frequency Value

//
void tim_pwm_in_init(void)
{
    GPIO_InitTypeDef   GPIO_InitStruct;
    
    __HAL_RCC_GPIOA_CLK_ENABLE();
    __HAL_RCC_TIM3_CLK_ENABLE();
  
    GPIO_InitStruct.Pin = GPIO_PIN_6;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF1_TIM3;//GPIO_AF0_TIM3;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    
    HAL_NVIC_SetPriority(TIM3_IRQn, 0, 1);
    //HAL_NVIC_SetPriority(TIM3_IRQn, 2, 0); // From lasertag project
    HAL_NVIC_EnableIRQ(TIM3_IRQn);
    
    prescaler = HAL_RCC_GetHCLKFreq()/PWM_IN_TIM_FREQ - 1;
    
    TimHandle.Instance = TIM3;
    TimHandle.Init.Period            = 0xFFFF;
    TimHandle.Init.Prescaler         = 47;
    TimHandle.Init.ClockDivision     = 0;
    TimHandle.Init.CounterMode       = TIM_COUNTERMODE_UP;
    TimHandle.Init.RepetitionCounter = 0;
    TimHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
    HAL_TIM_IC_Init(&TimHandle);
    //HAL_TIM_Base_Init(&TimHandle);

    sConfig.ICPrescaler = TIM_ICPSC_DIV1;
    sConfig.ICFilter = 0;
  
    sConfig.ICPolarity = TIM_ICPOLARITY_RISING; ;
    sConfig.ICSelection = TIM_ICSELECTION_DIRECTTI;
    HAL_TIM_IC_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1);
    
    sConfig.ICPolarity = TIM_ICPOLARITY_FALLING;
    sConfig.ICSelection = TIM_ICSELECTION_INDIRECTTI;
    HAL_TIM_IC_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_2);
    
    sSlaveConfig.SlaveMode        = TIM_SLAVEMODE_RESET;
    sSlaveConfig.InputTrigger     = TIM_TS_TI1FP1;
    sSlaveConfig.TriggerPolarity  = TIM_TRIGGERPOLARITY_NONINVERTED;
    sSlaveConfig.TriggerPrescaler = TIM_TRIGGERPRESCALER_DIV1;
    sSlaveConfig.TriggerFilter    = 0;
    
    HAL_TIM_SlaveConfigSynchro(&TimHandle, &sSlaveConfig);
    
    HAL_TIM_IC_Start_IT(&TimHandle, TIM_CHANNEL_1);
    HAL_TIM_IC_Start_IT(&TimHandle, TIM_CHANNEL_2);
}

//
void tim_print_out_pwm(void)
{
    printf("Captured Value %u\r\n", uwIC2Value);
    printf("Duty Cycle Value %u\r\n", uwDutyCycle);
    printf("Frequency Value %u\r\n\n", uwFrequency);
}


void TIM3_IRQHandler(void)
{
    HAL_TIM_IRQHandler(&TimHandle);
}


void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
    static uint32_t cnt = 0;
    
    cnt++;
    
#if 1  
    if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
    {
        // Get the Input Capture value
        uwIC2Value = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);

        if (uwIC2Value != 0)
        {
            // Duty cycle computation
            //uwDutyCycle = ((HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2)) * 100) / uwIC2Value;
          
            uwDutyCycle = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2);

            // uwFrequency computation
            //TIM3 counter clock = (RCC_Clocks.HCLK_Frequency)
            uwFrequency = tim_freq/uwIC2Value;
        }
        else
        {
            uwDutyCycle = 0;
            uwFrequency = 0;
        }
    }
#endif    
}

