/* 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_output_compare.h"
 #include "ac780x_pwm.h"
 #include "ac780x_pwm_reg.h"
 #include "string.h"
 #include "ac780x_gpio.h"
 
/**
* 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_FUN1);    /*PWM1_CH6 ->PB6*/
}

/**
* PWM1_Output_Compare
* @param[in] none
*
* @return none
*
* @brief 配置PWM1模块，CH2,CH3,CH6用做输出比较模式
*        CH2--匹配时切换输出，匹配点设置在中点
*        CH3--匹配时清除输出
*        CH6--匹配时设置输出
*/
void PWM1_Output_Compare(void)
{
    PWM_OutputCompareChConfigType outputChConfig[3];   /*输出比较通道配置结构体*/
    PWM_OutputCompareConfigType outputCompconfig;      /*输出比较配置结构体*/
    PWM_ConfigType config;                             /*PWM模块结构体*/
    /*结构体数据清零*/
    memset(&outputChConfig, 0, sizeof(outputChConfig));
    memset(&outputCompconfig, 0, sizeof(outputCompconfig));
    memset(&config, 0, sizeof(config));
    /*配置初始电平状态*/
    CKGEN_Enable(CLK_PWM1, ENABLE);
    CKGEN_SoftReset(SRST_PWM1, ENABLE);
    PWM_SetChannelOutputInitLevel(PWM1, PWM_CH_2, PWM_HIGH_LEVEL);/*通道初始状态为高电平*/
    PWM_SetChannelOutputInitLevel(PWM1, PWM_CH_3, PWM_HIGH_LEVEL);/*通道初始状态为高电平*/
    PWM_SetChannelOutputInitLevel(PWM1, PWM_CH_6, PWM_LOW_LEVEL); /*通道初始状态为低电平*/
    PWM_SetInitChannelOutput(PWM1, ENABLE);                       /*使能初始化通道输出，使能后initLevel的配置才会生效*/
    /*输出比较通道配置*/
    /*CH0--匹配时翻转*/
    outputChConfig[0].channel = PWM_CH_2;                          /*输出比较通道*/
    outputChConfig[0].comparedValue = (MOD_OUTPUT_COMPARE_PWM>>1); /*输出比较模式匹配点，匹配点设到中点。*/
    outputChConfig[0].mode = PWM_TOGGLE_OUTPUT;                    /*当计时器值与CH2V寄存器值匹配时，翻转PWM1 CH2的输出。*/
    outputChConfig[0].interruptEn = DISABLE;                       /*中断使能位*/
    outputChConfig[0].initLevel = PWM_HIGH_LEVEL;                  /*初始输出电平*/
    outputChConfig[0].triggerEn = DISABLE;                         /*通道匹配触发去能*/
    /*CH1--匹配时清除输出*/
    outputChConfig[1].channel = PWM_CH_3;                          /*输出比较通道*/
    outputChConfig[1].comparedValue = (MOD_OUTPUT_COMPARE_PWM>>1); /*输出比较模式匹配点，匹配点设到中点。*/
    outputChConfig[1].mode = PWM_CLEAR_OUTPUT;                     /*当计时器值与CH3V寄存器值匹配时，清除PWM1 CH3的输出，即为低电平。*/
    outputChConfig[1].interruptEn = DISABLE;                       /*中断使能位*/
    outputChConfig[1].initLevel = PWM_HIGH_LEVEL;                  /*初始输出电平*/
    outputChConfig[1].triggerEn = DISABLE;                         /*通道匹配触发去能*/
    /*CH6--匹配时设置输出*/
    outputChConfig[2].channel = PWM_CH_6;                          /*输出比较通道*/
    outputChConfig[2].comparedValue = (MOD_OUTPUT_COMPARE_PWM>>1); /*输出比较模式匹配点，匹配点设到中点。*/
    outputChConfig[2].mode = PWM_SET_OUTPUT;                       /*当计时器值与CH6V寄存器值匹配时，设置PWM1 CH6的输出，即为高电平。*/
    outputChConfig[2].interruptEn = DISABLE;                       /*中断使能位*/
    outputChConfig[2].initLevel = PWM_LOW_LEVEL;                   /*初始输出电平*/
    outputChConfig[2].triggerEn = DISABLE;                         /*通道匹配触发去能*/
    
    /*输出比较配置*/
    outputCompconfig.channelNum = 3;                               /*输出比较通道数*/
    outputCompconfig.channelConfig = outputChConfig;               /*输出比较配置变量赋值*/
    outputCompconfig.initChOutputEn = ENABLE;                      /*使能初始化通道输出，使能后initLevel的配置才会生效*/
    /*PWM模块配置*/
    config.mode = PWM_MODE_OUTPUT_COMPARE;       /*PWM模块配置为输出比较模式*/
    config.initModeStruct = &outputCompconfig;   /*输出比较配置变量赋值*/
    config.clkSource = PWM_CLK_SOURCE_APB;       /*PWM时钟源配置*/
    config.clkPsc = PWM_OUTPUT_COMPARE_PRESCALER;/*PWM时钟源分频*/
    config.initValue = 0;                        /*计数器初始寄存器值*/
    /* 
        设值MOD值，会影响比较事件发生的频率。输出比较模式频率 = PWM模块频率 /(clkPsc+1)/（MOD值 + 1） 
    */
    config.maxValue = MOD_OUTPUT_COMPARE_PWM;    /*PWM计数器最大值*/
    config.overflowInterrupEn = DISABLE;         /*计数器溢出中断使能位*/
    config.cntOverflowFreq = 0;                  /*CNTOF中断产生的频率与计数器频率的关系(0-127), 0表示每次计数器溢出都产生溢出中断，1表示间隔1次，2表示间隔2次，以此类推。*/
    config.interruptEn = DISABLE;                /*PWM中断使能*/
    config.callBack = NULL;                      /*PWM中断回调*/
    
    PWM_Init(PWM1, &config);                     /*配置初始化生效*/

}
