/* 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) 2023. 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 <string.h>

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

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

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

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

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


/*!
 * @brief PWM2_Callback
 *        PWM2 interrupts user callback function
 *
 * @param[in] wpara: Overflow interrupt flag parameter
 *
 * @param[in] lpara: Channel matching interrupt flag parameter
 *
 * @return none
 */
void PWM2_Callback(void *device, uint32_t wpara, uint32_t lpara)
{
    if (wpara & PWM_INIT_CNTOF_Msk) /* Overflow interrupt determination */
    {

    }
    if (lpara & PWM_STR_CH0SF_Msk)  /* Channel 0 matches interrupt determination */
    {

    }
    if (lpara & PWM_STR_CH1SF_Msk)  /* Channel 1 matches interrupt determination */
    {

    }
    if (lpara & PWM_STR_CH2SF_Msk)  /* Channel 2 matches interrupt determination */
    {

    }
}

/*!
 * @brief   PWM2_OutputCompare_Init
 *          PWM2_CH0 output compare mode and flips the output when matching
 *          PWM2_CH1 output compare mode and cleans the output when matched
 *          PWM2_CH2 output compare mode and set the output when matching
 *
 * @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(GPIOB, GPIO_PIN3, GPIO_FUN1); /* PB3  PWM2_CH2 */

    /*output channel config*/
    outputChConfig[0].channel = PWM_CH_0;                /* Output compare channel */
    outputChConfig[0].comparedValue = 3000;              /* Output compare mode matching points */
    outputChConfig[0].mode = PWM_TOGGLE_OUTPUT;          /* Output mode selection, flipping the channel output when the counter value matches the channel value */
    outputChConfig[0].interruptEn = DISABLE;             /* The channel matches the interrupt enable bit */
    outputChConfig[0].initLevel = PWM_LOW_LEVEL;        /* Output level polarity after reaching the first matching value */
    outputChConfig[0].triggerEn = DISABLE;               /* The channel matching triggers the enable bit, which can generate a trigger signal for other modules when the channel value is matched */

    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;
    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;
    outputChConfig[2].triggerEn = DISABLE;

    /*output compare config*/
    outputCompconfig.channelNum = 3;                     /* Output the number of compare channels */
    outputCompconfig.channelConfig = outputChConfig;     /* Output compare channel configuration assignment */
    outputCompconfig.initChOutputEn = ENABLE;            /* Initialize the channel output enable bit */

    /*pwm config*/
    config.mode = PWM_MODE_OUTPUT_COMPARE;               /* PWM mode configuration */
    config.initModeStruct = &outputCompconfig;           /* Different modes initialize configuration struct assignments accordingly */
    config.clkSource = PWM_CLK_SOURCE_APB;               /* PWM clock source selection */
    config.clkPsc = 0;                                   /* Clock division factor */
    config.initValue = 0;                                /* Initial count value */
    config.maxValue = 8000 - 1;                          /* Maximum count value. Determines the output waveform frequency */
    config.overflowInterrupEn = ENABLE;                  /* Overflow interrupts the enable bit */
    config.cntOverflowFreq = 0;                          /* The frequency at which overflow interrupts are generated, 0 means that each overflow produces an interrupt */
    config.interruptEn = ENABLE;                         /* PWM total interrupt enable bit */
    config.callback = PWM2_Callback;                     /* PWM interrupts user callback function */

    PWM_Init(PWM2, &config);
}

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