/* 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 "pwm_fault.h"
 #include "ac780x_pwm.h"
 #include "ac780x_pwm_reg.h"
 #include "string.h"
 #include "ac780x_gpio.h"
 
 uint16_t g_falutCnt =0; /*PWM_FAULT产生次数*/

/**
* PWM1_CallBack
* @param[in] none
*
* @return none
*
* @brief PWM1中断回调函数，在溢出中断里修改每个通道的占空比，在FAULT中断里设置故障标志，
*
*/ 		
void PWM1_CallBack(void *device, uint32_t wpara, uint32_t lpara)
{
    static uint16_t data=0;
    /*PWM计数器溢出中断*/
    if (wpara & PWM_INIT_CNTOF_Msk) 
    {
       
    }
    /*故障中断*/
    if(PWM_GetFaultChannelFlag(PWM1, PWM_FAULT_CH_1))
    {
        /*若配为手动清除故障，清标志后，PWM会继续输出。*/
        g_falutCnt ++;
        if(GPIO_GetPinLevel(GPIOB,GPIO_PIN6)==GPIO_LEVEL_HIGH)
        {
            PWM_ClearFaultChannelFlag(PWM1, PWM_FAULT_CH_1); /*配为手动清除故障标志时，需软件清标志，否则会一直触发中断*/
        }
        else
        {
        }
    }
    /*PWM通道中断*/
    /*通过判断回调返回的lpara对应PWM_STR_CHnSF_Msk位状态判断是否发生通道中断*/
    if (lpara & PWM_STR_CHSF_Msk)
    {
        /*通道0中断*/
        if (lpara & PWM_STR_CH0SF_Msk)
        {
            
        }
        /*通道1中断*/
        if (lpara & PWM_STR_CH1SF_Msk)
        {
            
        }
    }
    
}
/**
* PWM_GPIOInit
* @param[in] none
*
* @return none
*
* @brief 配置GPIO
*
*/ 
void PWM_GPIOInit(void)
{
    /*config PWM1 pinmux*/
    GPIO_SetFunc(GPIOB, GPIO_PIN8, GPIO_FUN1);  /*PWM1_CH2 ->PB8*/
    GPIO_SetFunc(GPIOB, GPIO_PIN7, GPIO_FUN1);  /*PWM1_CH3 ->PB7*/

    GPIO_SetFunc(GPIOB, GPIO_PIN6, GPIO_FUN2);  /*PWM1_FLT0 ->PB6*/
}

/**
* PWM1_Output_EdgeAlign
* @param[in] none
*
* @return none
*
* @brief 配置PWM1模块，CH2/CH3,两两组合模式，生成互补带死区的波形。
*        使能PWM_FLT0的故障控制功能，高有效。
*/
void PWM1_Output_Fault(void)
{
    PWM_CombineChConfig      combineChConfig[1];    /*组合模式相关结构体，两个通道组合为一组*/
    PWM_ModulationConfigType pwmConfig;             /*PWM模式相关结构体*/
    PWM_FaultConfig          faultConfig;           /*PWM故障控制结构体*/
    PWM_ConfigType           config;                /*PWM模块结构体*/
    /*结构体数据清零*/
    memset(&combineChConfig, 0, sizeof(combineChConfig));
    memset(&pwmConfig, 0, sizeof(pwmConfig));
    memset(&faultConfig, 0, sizeof(faultConfig));
    memset(&config, 0, sizeof(config));
     
    /*组合通道配置*/
    combineChConfig[0].pairChannel = PWM_CH_2;                           /*PWM通道对数，PWM_CH_0/2/4/6对应PAIR0/1/2/3*/
    combineChConfig[0].ch1stValue = (MOD_PWM1>>1);                       /*通道2n channel值，n为PWM对数编号 */
    combineChConfig[0].ch2ndValue = (MOD_PWM1>>1);                       /*通道2n+1 channel值，n为PWM对数编号 */
    combineChConfig[0].levelMode = PWM_LOW_TRUE;                         /*输出PWM高有效,如果占空比设为25%，是指的高有效电平占比25%*/
    combineChConfig[0].deadtimeEn = ENABLE;                              /*死区插入使能，组合模式才支持死区插入*/
    combineChConfig[0].complementEn = ENABLE;                            /*互补模式使能位。使能：PWM通道波形输出互补，不使能：PWM通道波形输出同向*/
    combineChConfig[0].ch1stMatchDir = PWM_MATCH_DIR_UP;                 /*通道2n匹配点方向配置。仅在向上-向下计数(countMode为PWM_UP_DOWN_COUNT)组合模式有效，用于选择匹配生效点方向*/
    combineChConfig[0].ch2ndMatchDir = PWM_MATCH_DIR_DOWN;               /*通道2n+1匹配点方向配置。仅在向上-向下计数(countMode为PWM_UP_DOWN_COUNT)组合模式有效，用于选择匹配生效点方向*/
    combineChConfig[0].ch1stPolarity = PWM_OUTPUT_POLARITY_ACTIVE_HIGH;  /*输出极性高有效，PWM mask后PWM输出低电平*/
    combineChConfig[0].ch2ndPolarity = PWM_OUTPUT_POLARITY_ACTIVE_HIGH;  /*输出极性高有效，PWM mask后PWM输出低电平*/
    combineChConfig[0].ch1stInterruptEn = DISABLE;                       /*PWM通道匹配中断使能位*/
    combineChConfig[0].ch2ndInterruptEn = DISABLE;                       /*PWM通道匹配中断使能位*/
    combineChConfig[0].ch1stInitLevel = PWM_LOW_LEVEL;                   /*PWM初始电平输出为低，该配置受initChOutputEn控制，决定PWM计数器未工作前PWM口的输出电平值。*/
    combineChConfig[0].ch2ndInitLevel = PWM_LOW_LEVEL;                   /*PWM初始电平输出为低，该配置受initChOutputEn控制，决定PWM计数器未工作前PWM口的输出电平值。*/
    combineChConfig[0].ch1stTriggerEn = DISABLE;                         /*通道2n匹配触发器使能，n为PWM对数编号*/
    combineChConfig[0].ch2ndTriggerEn = DISABLE;                         /*通道2n+1匹配触发器使能，n为PWM对数编号*/
   
    /*modulation mode config*/
    pwmConfig.countMode = PWM_UP_DOWN_COUNT;                /*PWM计数器模式 */
    pwmConfig.independentChannelNum = 0;                    /*独立通道数*/
    pwmConfig.combineChannelNum = 1;                        /*组合对数*/
    pwmConfig.independentChConfig = NULL;                   /*独立通道配置变量地址赋值*/
    pwmConfig.combineChConfig = combineChConfig;            /*组合通道配置变量地址赋值*/
    pwmConfig.deadtimePsc = PWM_DEADTIME_DIVID_1;           /*死区插入分频值，与deadtime一起决定插入死区的时间 */
    pwmConfig.deadtime = 24;                                /*死区时间 = (DTPSC * DTVAL)/PWM计数器时钟周期 = 1*24/24000000 = 1us*/
    pwmConfig.initChOutputEn = ENABLE;                      /*使能初始化通道输出，使能后独立PWM模式的initLevel和组合PWM模式的ch1stInitLevel和ch2ndPolarity配置才会生效*/
    pwmConfig.initTriggerEn = DISABLE;                      /*PWM初始触发使能位，用于ADC的采样触发*/
    
    /*pwm config*/
    config.mode = PWM_MODE_MODULATION;              /*PWM模块配置为PWM调制模式*/
    config.initModeStruct = &pwmConfig;             /*PWM配置结构体地址赋值*/
    config.clkSource = PWM_CLK_SOURCE_APB;          /*PWM时钟源配置*/
    config.clkPsc = PWM_PRES;                       /*PWM时钟源分频*/
    config.initValue = 0;                           /*计数器初始寄存器值*/
    config.maxValue = (MOD_PWM1);                   /*PWM计数器最大值，决定输出波形频率*/
    config.overflowInterrupEn = ENABLE;             /*计数器溢出中断使能位*/
    config.cntOverflowFreq = 0;                     /*CNTOF中断产生的频率与计数器频率的关系(0-127), 0表示每次计数器溢出都产生溢出中断，1表示间隔1次，2表示间隔2次，以此类推*/
    config.interruptEn = ENABLE;                    /*PWM中断使能*/
    config.callBack = PWM1_CallBack;                /*PWM中断回调*/
    PWM_Init(PWM1, &config);                        /*配置初始化生效*/
    
    /* PWM故障配置 */
    faultConfig.mode = PWM_FAULT_CTRL_MANUAL_ALL;                                              /*故障清除模式选择，手动清除故障*/
    faultConfig.channelConfig[PWM_FAULT_CH_1].faultInputEn = ENABLE;                           /*使能PWM_FLT0通道*/
    faultConfig.channelConfig[PWM_FAULT_CH_1].faultFilterEn = ENABLE;                          /*使能PWM FLT0通道滤波*/
    faultConfig.channelConfig[PWM_FAULT_CH_1].faultPolarity = PWM_INPUT_POLARITY_ACTIVE_LOW;   /*输入故障有效状态配置，高有效，默认为低 */
    faultConfig.filterValue = 3;                                                               /*故障滤波器值，0~255*/
    faultConfig.faultCtrlOutputEn[0] = DISABLE;                                                /*PWM通道CH0,CH1故障使能*/
    faultConfig.faultCtrlOutputEn[1] = ENABLE;                                                 /*PWM通道CH2,CH3故障使能*/
    faultConfig.faultCtrlOutputEn[2] = DISABLE;                                                 /*PWM通道CH4,CH5故障使能*/
    faultConfig.faultCtrlOutputEn[3] = DISABLE;                                                 /*PWM通道CH6,CH7故障使能*/
    faultConfig.interruptEn = ENABLE;                                                          /*故障中断使能*/
    PWM_InitFaultControl(PWM1, &faultConfig);                                                  /*配置PWM故障相关寄存器*/

    NVIC_SetPriority(PWM1_IRQn, 0);  /*设置PWM模块中断的优先级*/
}

