/* 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) 2024. 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: V3.1.0  AC7840x  Demo
 ******************************************************************************/

/*!
 * @file pwm_sample.c
 *
 * @brief This file provides interface for PWM functions.
 */

/* ===============================  Includes  =============================== */
#include "pwm_sample.h"
#include "pwm_common.h"
#include "pwm_hw.h"
#include "pwm_input.h"
#include "pwm_output.h"
#include "gpio_drv.h"
#include "gpio_hw.h"
#include "string.h"

/* ===============================   Define   =============================== */
#define PWM1_INDEX (1U)

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

/* ===============================  Variables =============================== */

/* =========================  Functions declaration  ======================== */

/* =========================  Functions definition  ========================= */

/*!
 * @brief PWM1中断回调函数
 *
 * @param[in] none
 * @return none
 */
void PWM1_Callback(uint8_t instance, uint32_t status, void *userData)
{
    if ((status & PWM_INIT_CNTOF_Msk) != 0) /*! 溢出中断 */
    {
    }
}

/*!
 * @brief PWM 模块初始.
 *        将PWM1_CH4和PWM1_CH5配置为独立通道输出PWM，向上计数模式，频率为1KHZ,CH4占空比为30%，CH5占空比为60%
 *        使能Init触发，用于ADC模块的硬件触发源
 * @param[in] none
 * @return none
 */
void PWM1_OutputIndependent_Init(void)
{
    pwm_independent_ch_config_t independentChConfig[1];
    pwm_modulation_config_t pwmConfig;
    pwm_config_t config;

    /*! GPIO Configuration for PWM1 */
    GPIO_DRV_SetMuxModeSel(PORTA, (gpio_channel_type_t)11U, PORT_MUX_ALT2); /*! PA11: PWM1_CH5 */

    /*! Config PWM Interface */
    memset(independentChConfig, 0U, sizeof(independentChConfig));
    memset(&pwmConfig, 0U, sizeof(pwm_modulation_config_t));
    memset(&config, 0U, sizeof(pwm_config_t));

    /*!
    边沿对齐PWM模式：
    周期=(MCVR-CNTIN+1)*PWM计数器时钟周期
    占空比=(CHnV-CNTIN+1)*PWM计数器时钟周期
    中心对齐PWM模式：
    周期=2*(MCVR-CNTIN)*PWM计数器时钟周期
    占空比=2*(CH(n)V-CNTIN)*PWM计数器时钟周期
    */
    /*! channel config*/
    independentChConfig[0].channel        = PWM_CH_5;                        /*! 独立输出通道选择，这里选择CH5*/
    independentChConfig[0].chValue        = 18000;                           /*! 通道Channel值，决定占空比，输出占空比 = chValue/config.maxValue = 18000/(60000-1) = 30%*/
    independentChConfig[0].chDitherValue  = 0;                               /*! 通道抖动值,可使PWM信号的分辨率更精确*/
    independentChConfig[0].levelMode      = PWM_HIGH_TRUE;                   /*! 输出PWM高有效，可配置高有效或低有效 */
    independentChConfig[0].polarity       = PWM_OUTPUT_POLARITY_ACTIVE_HIGH; /*! 通道输出极性配置为高有效，PWM mask后输出为低电平 */
    independentChConfig[0].interruptEn    = false;                           /*! PWM通道匹配中断使能位*/
    independentChConfig[0].initLevel      = PWM_LOW_LEVEL;                   /*! 通道初始电平输出配置，该配置受initChOutputEn 控制，决定PWM计数器未工作前PWM口的输出电平*/
    independentChConfig[0].matchTriggerEn = false;                           /*! 通道匹配触发使能位，在通道值匹配时可产生触发信号用于其他模块的触发信号 */
    independentChConfig[0].chEventDmaEn   = false;                           /*! 通道的DMA请求使能位*/

    /*! modulation mode config*/
    pwmConfig.countMode             = PWM_UP_COUNT;        /*! PWM计数器模式， PWM_UP_COUNT--向上计数模式，PWM_UP_DOWN_COUNT--向上向下计数模式*/
    pwmConfig.independentChannelNum = 1;                   /*! 独立通道数目 */
    pwmConfig.combineChannelNum     = 0;                   /*! 组合通道对数*/
    pwmConfig.triggerRatio          = 0;                   /*! 起始值/最大值/匹配触发的比率，范围为0~7，0是每1个PWM周期产生一次触发，1是每2个PWM周期产生一次触发，以此类推 */
    pwmConfig.independentChConfig   = independentChConfig; /*! 独立通道配置赋值*/
    pwmConfig.combineChConfig       = NULL;                /*! 组合通道配置赋值*/
    pwmConfig.initChOutputEn        = true;                /*! 初始化通道输出使能位，使能后独立PWM模式的initLevel和组合PWM模式的ch1stInitLevel和ch2ndPolarity配置才会生效*/
    pwmConfig.initTriggerEn         = true;                /*! PWM起始值触发使能位*/
    pwmConfig.maxTriggerEn          = false;               /*! 最大值触发使能位 */
    pwmConfig.centerAlignDutyType   = PWM_DUTY_MODE_0;     /*! 中央对齐的PWM占空比模式 */

    /*!
    向上计数模式下，频率计算为：
    freq = pwm_clk/(maxValue - initValue +1))
    60000000/(60000-1+1) = 1KHz
    */
    config.mode               = PWM_MODE_MODULATION;   /*! PWM模式配置，PWM_MODE_MODULATION--PWM调制模式 */
    config.initModeStruct     = &pwmConfig;            /*! 不同模式相应初始化配置结构体赋值 */
    config.clkSource          = PWM_CLK_SOURCE_SYSTEM; /*! PWM时钟源选择 */
    config.clkPsc             = 1;                     /*! 时钟分频 */
    config.initValue          = 0;                     /*! 初始计数值 */
    config.maxValue           = (60000 - 1);           /*! 最大计数值，决定输出波形频率 */
    config.modDitherValue     = 0;                     /*! 抖动值 */
    config.overflowEventEn    = true;                  /*! 向上溢出事件使能位 */
    config.underflowEventEn   = false;                 /*! 向下溢出事件使能位 */
    config.overflowInterrupEn = true;                  /*! 溢出中断使能位*/
    config.overflowDmaEn      = false;                 /*! 溢出事件的DMA请求使能位 */
    config.dmaLghEn           = false;                 /*! DMA传输长度使能位 */
    config.dmaLgh             = 0;                     /*! 配置一次PWM DMA req请求清除的DMA传输长度（0~31），*/
    config.cntOverflowFreq    = 0;                     /*! 溢出中断产生的频率与计数器频率的关系，0表示每次计数器溢出都产生中断，1表示间隔1次 */
    config.interruptEn        = true;                  /*! PWM总中断使能位 */
    config.overflowCallback   = PWM1_Callback;         /*! 溢出中断回调函数 */
    config.channelCallback    = NULL;                  /*! 通道中断回调函数 */

    PWM_DRV_Init(PWM1_INDEX, &config);
}

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