/* 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 "pwmOutput_independent.h"
 #include "ac780x_pwm.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_PIN7, GPIO_FUN1); /*PWM1_CH3 ->PB7*/
    GPIO_SetFunc(GPIOB, GPIO_PIN8, GPIO_FUN1); /*PWM1_CH2 ->PB8*/
    
    /*config PWM0 pinmux*/
    GPIO_SetFunc(GPIOA, GPIO_PIN1, GPIO_FUN1);/*PWM0_CH0 ->PA1*/
    GPIO_SetFunc(GPIOA, GPIO_PIN0, GPIO_FUN1);/*PWM0_CH1 ->PA0*/
    
}

/**
* PWM0_Output_CentralAlign
* @param[in] none
*
* @return none
*
* @brief 配置PWM0模块，CH0和CH1独立输出中央对齐的PWM
*
*/
void PWM0_Output_CentralAlign(void)
{
    PWM_IndependentChConfig  independentChConfig[2];/*独立模式相关结构体*/
    PWM_ModulationConfigType pwmConfig;             /*PWM模式相关结构体*/
    PWM_ConfigType           config;                /*PWM模块结构体*/
    /*结构体数据清零*/
    memset(&independentChConfig, 0, sizeof(independentChConfig));
    memset(&pwmConfig, 0, sizeof(pwmConfig));
    memset(&config, 0, sizeof(config));
     
    /*
    边沿对齐PWM模式：
    周期=(MCVR-CNTIN+1)*PWM计数器时钟周期
    占空比=(CHnV-CNTIN+1)*PWM计数器时钟周期
    中心对齐PWM模式：
    周期=2*(MCVR-CNTIN)*PWM计数器时钟周期
    占空比=2*(CH(n)V-CNTIN)*PWM计数器时钟周期
    */
    /*channel 0*/
    independentChConfig[0].channel = PWM_CH_0;                           /*独立输出通道选择CH0, PA1 */
    independentChConfig[0].chValue = MOD_PWM0 >> 1;                      /*通道0 channel值，输出占空比 = chValue / (config.maxValue ) = 50%*/
    independentChConfig[0].levelMode = PWM_HIGH_TRUE;                    /*输出PWM高有效*/
    independentChConfig[0].polarity = PWM_OUTPUT_POLARITY_ACTIVE_HIGH;   /*输出极性高有效，PWM mask后PWM输出低电平*/
    independentChConfig[0].interruptEn = DISABLE;                        /*PWM通道匹配中断使能位*/
    independentChConfig[0].initLevel = PWM_LOW_LEVEL;                    /*PWM初始电平输出为低，该配置受initChOutputEn控制，决定PWM计数器未工作前PWM口的输出电平值。*/
    independentChConfig[0].triggerEn = DISABLE;                          /*通道匹配触发使能位，用于ADC的采样触发*/
    /*channel 1*/
    independentChConfig[1].channel = PWM_CH_1;                           /*独立输出通道选择CH1, PA2 */
    independentChConfig[1].chValue = MOD_PWM0 >> 2;                       /*通道1 channel值，输出占空比 = chValue / (config.maxValue ) = 25%*/
    independentChConfig[1].levelMode = PWM_HIGH_TRUE;                    /*输出PWM高有效*/
    independentChConfig[1].polarity = PWM_OUTPUT_POLARITY_ACTIVE_HIGH;   /*输出极性高有效，PWM mask后PWM输出低电平*/
    independentChConfig[1].interruptEn = DISABLE;                        /*PWM通道匹配中断使能位*/
    independentChConfig[1].initLevel = PWM_LOW_LEVEL;                    /*PWM初始电平输出为低，该配置受initChOutputEn控制，决定PWM计数器未工作前PWM口的输出电平值。*/
    independentChConfig[1].triggerEn = DISABLE;                          /*通道匹配触发使能位，用于ADC的采样触发*/

    /*modulation mode config*/
    pwmConfig.countMode = PWM_UP_DOWN_COUNT;                /*PWM计数器模式，中央对齐模式，需选择 PWM_UP_DOWN_COUNT 方式 */
    pwmConfig.independentChannelNum = 2;                    /*独立通道数*/
    pwmConfig.combineChannelNum = 0;                        /*组合对数*/
    pwmConfig.independentChConfig = independentChConfig;    /*独立通道配置变量地址赋值*/
    pwmConfig.combineChConfig = NULL;                       /*组合通道配置变量地址赋值*/
    pwmConfig.deadtimePsc = PWM_DEADTIME_DIVID_1;           /*死区插入分频值，与deadtime一起决定插入死区的时间 */
    pwmConfig.deadtime = 0;                                 /*死区时间 = (DTPSC * DTVAL)/PWM计数器时钟周期 */
    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_PWM0);                   /*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(PWM0, &config);                        /*配置初始化生效*/

}
/**
* PWM1_Output_EdgeAlign
* @param[in] none
*
* @return none
*
* @brief 配置PWM1模块，CH2和CH3独立输出边沿对齐的PWM
*
*/
void PWM1_Output_EdgeAlign(void)
{
    PWM_IndependentChConfig  independentChConfig[2];/*独立模式相关结构体*/
    PWM_ModulationConfigType pwmConfig;             /*PWM模式相关结构体*/
    PWM_ConfigType           config;                /*PWM模块结构体*/
    /*结构体数据清零*/
    memset(&independentChConfig, 0, sizeof(independentChConfig));
    memset(&pwmConfig, 0, sizeof(pwmConfig));
    memset(&config, 0, sizeof(config));
     
    /*
    边沿对齐PWM模式：
    周期=(MCVR-CNTIN+1)*PWM计数器时钟周期
    占空比=(CHnV-CNTIN+1)*PWM计数器时钟周期
    中心对齐PWM模式：
    周期=2*(MCVR-CNTIN)*PWM计数器时钟周期
    占空比=2*(CH(n)V-CNTIN)*PWM计数器时钟周期
    */
    /*channel 2*/
    independentChConfig[0].channel = PWM_CH_2;                           /*独立输出通道选择CH2, PB8*/
    independentChConfig[0].chValue = MOD_PWM1 >> 1;                      /*通道2 channel值，输出占空比 = chValue / (config.maxValue + 1 ) = 50%*/
    independentChConfig[0].levelMode = PWM_HIGH_TRUE;                    /*输出PWM高有效*/
    independentChConfig[0].polarity = PWM_OUTPUT_POLARITY_ACTIVE_HIGH;   /*输出极性高有效，PWM mask后PWM输出低电平*/
    independentChConfig[0].interruptEn = DISABLE;                        /*PWM通道匹配中断使能位*/
    independentChConfig[0].initLevel = PWM_HIGH_LEVEL;                   /*PWM初始电平输出为低，该配置受initChOutputEn控制，决定PWM计数器未工作前PWM口的输出电平值。*/
    independentChConfig[0].triggerEn = DISABLE;                          /*通道匹配触发使能位，用于ADC的采样触发*/
    /*channel 3*/
    independentChConfig[1].channel = PWM_CH_3;                           /*独立输出通道选择CH3, PB7*/
    independentChConfig[1].chValue = (MOD_PWM1 - (MOD_PWM1 >> 2));       /*通道3 channel值，输出占空比 = chValue / (config.maxValue + 1 ) = 75%*/
    independentChConfig[1].levelMode = PWM_HIGH_TRUE;                    /*输出PWM高有效*/
    independentChConfig[1].polarity = PWM_OUTPUT_POLARITY_ACTIVE_HIGH;   /*输出极性高有效，PWM mask后PWM输出低电平*/
    independentChConfig[1].interruptEn = DISABLE;                        /*PWM通道匹配中断使能位*/
    independentChConfig[1].initLevel = PWM_HIGH_LEVEL;                   /*PWM初始电平输出为低，该配置受initChOutputEn控制，决定PWM计数器未工作前PWM口的输出电平值。*/
    independentChConfig[1].triggerEn = DISABLE;                          /*通道匹配触发使能位，用于ADC的采样触发*/

    /*modulation mode config*/
    pwmConfig.countMode = PWM_UP_COUNT;                     /*PWM计数器模式，边沿对齐模式，需选择 PWM_UP_COUNT 方式 */
    pwmConfig.independentChannelNum = 2;                    /*独立通道数*/
    pwmConfig.combineChannelNum = 0;                        /*组合对数*/
    pwmConfig.independentChConfig = independentChConfig;    /*独立通道配置变量地址赋值*/
    pwmConfig.combineChConfig = NULL;                       /*组合通道配置变量地址赋值*/
    pwmConfig.deadtimePsc = PWM_DEADTIME_DIVID_1;           /*死区插入分频值，与deadtime一起决定插入死区的时间 */
    pwmConfig.deadtime = 0;                                 /*死区时间 = (DTPSC * DTVAL)/PWM计数器时钟周期 */
    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-1);                 /*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);                        /*配置初始化生效*/
}

