/* Copyright Statement:
 *
 * This software/firmware and related documentation ("AutoChips Software") are
 * protected under relevant copyright laws. The information contained herein is
 * confidential and proprietary to AutoChips Inc. and/or its licensors. Without
 * the prior written permission of AutoChips inc. and/or its licensors, any
 * reproduction, modification, use or disclosure of AutoChips Software, and
 * information contained herein, in whole or in part, shall be strictly
 * prohibited.
 *
 * AutoChips Inc. (C) 2016. All rights reserved.
 *
 * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
 * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("AUTOCHIPS SOFTWARE")
 * RECEIVED FROM AUTOCHIPS AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
 * ON AN "AS-IS" BASIS ONLY. AUTOCHIPS EXPRESSLY DISCLAIMS ANY AND ALL
 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NONINFRINGEMENT. NEITHER DOES AUTOCHIPS PROVIDE ANY WARRANTY WHATSOEVER WITH
 * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
 * INCORPORATED IN, OR SUPPLIED WITH THE AUTOCHIPS SOFTWARE, AND RECEIVER AGREES
 * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
 * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
 * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN AUTOCHIPS
 * SOFTWARE. AUTOCHIPS SHALL ALSO NOT BE RESPONSIBLE FOR ANY AUTOCHIPS SOFTWARE
 * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
 * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND AUTOCHIPS'S
 * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE AUTOCHIPS SOFTWARE
 * RELEASED HEREUNDER WILL BE, AT AUTOCHIPS'S OPTION, TO REVISE OR REPLACE THE
 * AUTOCHIPS SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
 * CHARGE PAID BY RECEIVER TO AUTOCHIPS FOR SUCH AUTOCHIPS SOFTWARE AT ISSUE.
 */

#include "timer_sample.h"  


Gpio_CapturePwm_Type g_gpioCapturePwm;


/**
*
* @param[in] none
*
* @return none
*
* @brief GPIO_CapturePwm_Init
*
*/
void GPIO_CapturePwm_Init(void)
{
    /* 设置捕获PWM的引脚为普通GPIO功能 */
    GPIO_SetFunc(GPIO_CAPTURE_PWM_PORT, GPIO_CAPTURE_PWM_PIN, GPIO_FUN0);

    /* 设置捕获PWM的引脚为输入 */
    GPIO_SetDir(GPIO_CAPTURE_PWM_PORT, GPIO_CAPTURE_PWM_PIN, GPIO_IN);   

    /* 使能捕获引脚的外部中断,上升沿下降沿都产生中断 */
    GPIO_EnableExtInterrupt(GPIO_CAPTURE_PWM_PORT, GPIO_CAPTURE_PWM_PIN, EXTI_TRIGGER_RISING_FALLING);

    /* 设置捕获引脚外部中断的回调函数 */
    GPIO_SetCallback(GPIO_CAPTURE_PWM_PIN, GpioExtInterruptCallback);
}

/**
*
* @param[in] none
*
* @return none
*
* @brief TIMER_CHANNEL0_Init
*
*/
void TIMER_CHANNEL0_Init(void)
{
    /* TIMER channel configuration structure */
    TIMER_ConfigType  timerConfig0;

    /* timerConfig结构体数据清零 */
    memset(&timerConfig0, 0, sizeof(timerConfig0));

    /* configure TIMER channel 0 */
    timerConfig0.periodValue      = TIMER_CHANNEL0_PERIOD_VALUE;    /* TIMER_CHANNEL0 period value */
    timerConfig0.linkModeEn       = DISABLE;                        /* 禁能链接模式 */
    timerConfig0.interruptEn      = ENABLE;                         /* 使能中断 */   
    timerConfig0.timerEn          = ENABLE;                         /* 使能TIMER_CHANNEL0 */
    timerConfig0.callBack         = TIMER_CHANNEL0_callBack;        /* 设置TIMER_CHANNEL0回调函数 */
    TIMER_Init(TIMER_CHANNEL0, &timerConfig0);                      /* 设置TIMER_CHANNEL0初始化 */
}

/**
*
* @param[in] none
*
* @return    none
*
* @brief  初始化LED引脚
*/
void GPIO_LedInit(void)
{
    /* PIN脚为gpio功能 */
    GPIO_SetFunc(LED2_PORT, LED2_PIN, GPIO_FUN0);

    /* 设置PIN脚为GPIO输出 */
    GPIO_SetDir(LED2_PORT, LED2_PIN, GPIO_OUT);
}

/**
* @param[in] device: TIMER_CHANNEL0
* @param[in] wpara: TIMER_GetTimeoutFlag(TIMER_CHANNEL0)
                    Flag在CMSIS中已被清零,不需再清零.
                    wpara可用来判断.
* @param[in] lpara: 0
*
* @return none
*
* @brief TIMER_CHANNEL0_callBack
*/
void TIMER_CHANNEL0_callBack(void *device, uint32_t wpara, uint32_t lpara)
{
    /* 中断处理 */
    g_gpioCapturePwm.TimerOverCount++;
}

/**
*
* @param[in] device：GPIO_Type
* @param[in] wpara：GPIO_PinType
* @param[in] lpara: 0
*
* @return none
*
* @brief GpioExtInterruptCallback
         外部输入PWM信号,在捕获引脚上产生外部中断,通过读取TIMER_CHANNEL0,计算频率和占空比
*/
void GpioExtInterruptCallback(void *device, uint32_t wpara, uint32_t lpara)
{
    /* 读出当前Timer计数值 */
    g_gpioCapturePwm.TimerCurrentCount = TIMER_GetCurrentValue(TIMER_CHANNEL0);

    if(0U == g_gpioCapturePwm.EdgCount)
    {
        /* PWM下降沿 */
        if (GPIO_GetPinLevel(GPIO_CAPTURE_PWM_PORT,GPIO_CAPTURE_PWM_PIN) == GPIO_LEVEL_LOW)
        {
                g_gpioCapturePwm.FallEdgeOne = g_gpioCapturePwm.TimerCurrentCount;
                g_gpioCapturePwm.EdgCount = 1U;
        }
        else 
        {
                g_gpioCapturePwm.EdgCount = 0U;
        }
    }
    else if(1U == g_gpioCapturePwm.EdgCount)
    {
        /* PWM上升沿 */
        if (GPIO_GetPinLevel(GPIO_CAPTURE_PWM_PORT,GPIO_CAPTURE_PWM_PIN) == GPIO_LEVEL_HIGH)
        {
                g_gpioCapturePwm.RiseEdge = g_gpioCapturePwm.TimerCurrentCount;
                g_gpioCapturePwm.EdgCount = 2U;
        }
        else 
        {
                g_gpioCapturePwm.EdgCount = 0U;
        }
    }
    else if(2U == g_gpioCapturePwm.EdgCount)
    {
        /* PWM下降沿 */
        if(GPIO_GetPinLevel(GPIO_CAPTURE_PWM_PORT,GPIO_CAPTURE_PWM_PIN) == GPIO_LEVEL_LOW)
        {
            g_gpioCapturePwm.FallEdgeTwo = g_gpioCapturePwm.TimerCurrentCount;
            g_gpioCapturePwm.EdgCount = 0U;
        
            /* Timer 为向下计数 */
            if((g_gpioCapturePwm.RiseEdge < g_gpioCapturePwm.FallEdgeOne) \
                 && (g_gpioCapturePwm.FallEdgeTwo < g_gpioCapturePwm.RiseEdge))
            {
                g_gpioCapturePwm.Freq = Timer_CLK / \
                                                                (g_gpioCapturePwm.FallEdgeOne - g_gpioCapturePwm.FallEdgeTwo);
                g_gpioCapturePwm.Duty = (100UL * (g_gpioCapturePwm.RiseEdge - g_gpioCapturePwm.FallEdgeTwo)) \
                                        / (g_gpioCapturePwm.FallEdgeOne - g_gpioCapturePwm.FallEdgeTwo);
            }
            else
            {
                g_gpioCapturePwm.EdgCount = 0U;
            }
        }
        else 
        {
            g_gpioCapturePwm.EdgCount = 0U;
        }
    }
    else
    {
        /* nothing to do*/
    }
}

/**
*
* @param[in] none
*
* @return none
*
* @brief TIMER_CapturePwmInit
* 
*/
void TIMER_CapturePwmInit(void)
{
    GPIO_CapturePwm_Init();
    TIMER_CHANNEL0_Init();
    GPIO_LedInit();
}

