/* 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) 2022. 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.
 */
/******************************************************************************
 * @version: V1.0.0 demo
 ******************************************************************************/


/*!
* @file pwm.c
*
* @brief This file contains the user pwm function..
*
*/

/* ===========================================  Includes  =========================================== */
#include "pwm.h"
#include "ac780x_gpio.h"
#include "ac780x_pwm.h"
#include "ac780x_pwm_reg.h"
#include "string.h"

/* ============================================  Define  ============================================ */

/* ===========================================  Typedef  ============================================ */


/* ==========================================  Variables  =========================================== */
uint8_t g_pwm2_overflag = 0;
/* ====================================  Functions declaration  ===================================== */


/* ======================================  Functions define  ======================================== */


/*!
 * @brief   PWM2_Callback
 *          PWM2模块中断回调函数
 * @param   wpara:溢出中断标志参数
 *          lpara：通道中断标志参数
 * @return  none
 */
void PWM2_Callback(void *device, uint32_t wpara, uint32_t lpara)
{
    if (0 != wpara)
    {
        g_pwm2_overflag = 1;
    }
}

/*!
 * @brief   PWM2_OutputCompare_Init
 *          PWM2_CH0输出比较模式，匹配时翻转输出
 *          PWM2_CH1输出比较模式，匹配时清除输出
 *          PWM2_CH2输出比较输出，匹配时设置输出
 * @param   none
 * @return  none
 */
void PWM2_OutputCompare_Init(void)
{
    PWM_ConfigType config;
    PWM_OutputCompareConfigType outputCompconfig;
    PWM_OutputCompareChConfigType outputChConfig[3];

    memset(&config, 0U, sizeof(config));
    memset(&outputCompconfig, 0U, sizeof(outputCompconfig));
    memset(&outputChConfig, 0U, sizeof(outputChConfig));

    GPIO_SetFunc(GPIOA, GPIO_PIN3, GPIO_FUN1); /* PA3--PWM2_CH0.*/
    GPIO_SetFunc(GPIOA, GPIO_PIN2, GPIO_FUN1); /* PA2--PWM2_CH1.*/
    GPIO_SetFunc(GPIOA, GPIO_PIN1, GPIO_FUN1); /* PA1--PWM2_CH2.*/

    /*output channel config*/
    outputChConfig[0].channel = PWM_CH_0;                /*输出比较通道 */
    outputChConfig[0].comparedValue = 3000;              /*输出比较模式匹配点 */
    outputChConfig[0].mode = PWM_TOGGLE_OUTPUT;          /*输出模式选择，当计数器值与通道值匹配时，翻转通道输出 */
    outputChConfig[0].interruptEn = DISABLE;             /*通道中断使能位 */
    outputChConfig[0].initLevel = PWM_HIGH_LEVEL;        /*通道初始电平输出配置，该配置受initChOutputEn控制，决定PMM计数器未工作前PWM口的输出电平 */
    outputChConfig[0].triggerEn = DISABLE;               /*通道匹配触发使能位，在通道值匹配时可产生触发信号用于其他模块的触发信号 */

    outputChConfig[1].channel = PWM_CH_1;                /*输出比较通道 */
    outputChConfig[1].comparedValue = 3000;              /*输出比较模式匹配点 */
    outputChConfig[1].mode = PWM_CLEAR_OUTPUT;           /*输出模式选择，当计数器值与通道值匹配时，翻转通道输出 */
    outputChConfig[1].interruptEn = DISABLE;             /*通道中断使能位 */
    outputChConfig[1].initLevel = PWM_HIGH_LEVEL;        /*通道初始电平输出配置，该配置受initChOutputEn控制，决定PMM计数器未工作前PWM口的输出电平 */
    outputChConfig[1].triggerEn = DISABLE;               /*通道匹配触发使能位，在通道值匹配时可产生触发信号用于其他模块的触发信号 */

    outputChConfig[2].channel = PWM_CH_2;                /*输出比较通道 */
    outputChConfig[2].comparedValue = 3000;              /*输出比较模式匹配点 */
    outputChConfig[2].mode = PWM_SET_OUTPUT;             /*输出模式选择，当计数器值与通道值匹配时，翻转通道输出 */
    outputChConfig[2].interruptEn = DISABLE;             /*通道中断使能位 */
    outputChConfig[2].initLevel = PWM_LOW_LEVEL;         /*通道初始电平输出配置，该配置受initChOutputEn控制，决定PMM计数器未工作前PWM口的输出电平 */
    outputChConfig[2].triggerEn = DISABLE;               /*通道匹配触发使能位，在通道值匹配时可产生触发信号用于其他模块的触发信号 */

    /*output compare config*/
    outputCompconfig.channelNum = 3;                     /*输出比较通道数目 */
    outputCompconfig.channelConfig = outputChConfig;     /*输出比较通道配置赋值 */
    outputCompconfig.initChOutputEn = ENABLE;            /*初始化通道输出使能位，使能后独立PWM模式的 initLevel 和组合模式的ch1stInitLevel和ch2ndInitLevel配置才会生效*/

    /*pwm config*/
    config.mode = PWM_MODE_OUTPUT_COMPARE;        /*PWM模式配置，PWM_MODE_MODULATION--PWM调制模式 */
    config.initModeStruct = &outputCompconfig;    /*不同模式相应初始化配置结构体赋值 */
    config.clkSource = PWM_CLK_SOURCE_APB;        /*PWM时钟源选择 */
    config.clkPsc = 0;                            /*时钟分频系数 */
    config.initValue = 0;                         /*初始计数值 */
    config.maxValue = 6000;                       /*最大计数值，决定输出波形频率 */
    config.overflowInterrupEn = ENABLE;           /*溢出中断使能位 */
    config.cntOverflowFreq = 0;                   /*溢出中断产生的频率与计数器频率的关系，0表示每次计数器溢出都产生中断，1表示间隔一次，以此类推 */
    config.interruptEn = ENABLE;                  /*PWM总中断使能位  */
    config.callBack = PWM2_Callback;              /*中断回调函数 */

    PWM_Init(PWM2, &config);

}

/* =============================================  EOF  ============================================== */
