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

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

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

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

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

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

/*!
 * @brief PWM1_Callback
 *        PWM1 interrupts user callback function
 *
 * @param[in] wpara: Overflow interrupt flag parameter
 *
 * @param[in] lpara: Channel matching interrupt flag parameter
 *
 * @return none
 */
void PWM1_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 the interrupt determination */
    {

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

    }
}

/*!
 * @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_CH2SF_Msk)  /* Channel 2 matches interrupt determination */
    {

    }
    if (lpara & PWM_STR_CH3SF_Msk)  /* Channel 3 matches interrupt determination */
    {

    }
}

/*!
 * @brief PWM1_OutputIndependent_Init
 *        The PWM1 module is initialized, and PWM1_CH0 and PWM1_CH1 are configured in independent channel output, upcount mode.
 *        The frequency is 2KHz with a duty cycle of 20% for CH0 and 80% for CH1
 *
 * @param[in] none
 *
 * @return none
 */
void PWM1_OutputIndependent_Init(void)
{
    PWM_ConfigType config;
    PWM_ModulationConfigType pwmConfig;
    PWM_IndependentChConfig independentChConfig[2];

    memset(&config, 0U, sizeof(config));
    memset(&pwmConfig, 0U, sizeof(pwmConfig));
    memset(&independentChConfig, 0U, sizeof(independentChConfig));

    GPIO_SetFunc(GPIOC, GPIO_PIN7, GPIO_FUN5);          /* PC7 PWM1_CH0 */
    GPIO_SetFunc(GPIOC, GPIO_PIN6, GPIO_FUN5);          /* PC6 PWM1_CH1 */

    independentChConfig[0].channel = PWM_CH_0;          /* Independent output channel selection */
    independentChConfig[0].chValue = PWM1_CH0_VALUE - 1;/* The channel matches the value and determines the duty cycle */
    independentChConfig[0].levelMode = PWM_HIGH_TRUE;   /* Output level polarity after the channel matching value is reached */

    /* Channel output polarity, which affects the level after mask, the level after triggering fault control,
       and the level for deadzone insertion. Here, the polarity is configured to be high, that is, low output after mask */
    independentChConfig[0].polarity = PWM_OUTPUT_POLARITY_ACTIVE_HIGH;
    independentChConfig[0].interruptEn = ENABLE;        /* The channel matches the interrupt enable bit */
    independentChConfig[0].initLevel = PWM_LOW_LEVEL;   /* Channel initial level configuration, which affects the level before channel matching after initialization */
    independentChConfig[0].triggerEn = DISABLE;         /* When the channel value is matched, the trigger signal can be generated for the trigger source of other modules */


    independentChConfig[1].channel = PWM_CH_1;
    independentChConfig[1].chValue = PWM1_CH1_VALUE - 1;
    independentChConfig[1].levelMode = PWM_HIGH_TRUE;
    independentChConfig[1].polarity = PWM_OUTPUT_POLARITY_ACTIVE_HIGH;
    independentChConfig[1].interruptEn = ENABLE;
    independentChConfig[1].initLevel = PWM_LOW_LEVEL;
    independentChConfig[1].triggerEn = DISABLE;

    pwmConfig.countMode = PWM_UP_COUNT;                 /* PWM counter mode Settings */
    pwmConfig.independentChannelNum = 2;                /* Number of independent channels */
    pwmConfig.combineChannelNum = 0;                    /* Combined channel logarithm */
    pwmConfig.independentChConfig = independentChConfig;/* Independent channel configuration struct assignment */
    pwmConfig.combineChConfig = NULL;                   /* Combined channel configuration struct assignment */
    pwmConfig.initChOutputEn = ENABLE;                  /* Initialize the channel output enable bit */
    pwmConfig.initTriggerEn = DISABLE;                  /* The PWM initial value triggers the enable bit */

    config.mode = PWM_MODE_MODULATION;     /* PWM mode configuration */
    config.initModeStruct = &pwmConfig;    /* Different modes initialize configuration struct assignments accordingly */
    config.clkSource = PWM_CLK_SOURCE_APB; /* PWM clock source selection */
    config.clkPsc = PWM1_CLK_PSC - 1;      /* Clock division factor */
    config.initValue = 0;                  /* Initial count value */
    config.maxValue = PWM1_MAX_VALUE - 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 = PWM1_Callback;       /* PWM interrupts user callback function */

    PWM_Init(PWM1, &config);
}

/*!
 * @brief PWM2_OutputIndependent_Init
 *        The PWM2 module is initialized, and PWM2_CH0, PWM2_CH2, and PWM2_CH3 are configured in independent channel output, up/down count mode.
 *        The frequency was 20KHz with a CH0 duty cycle of 20%, CH2 duty cycle of 40%, and CH3 duty cycle of 60%
 *
 * @param[in] none
 *
 * @return none
 */
void PWM2_OutputIndependent_Init(void)
{
    PWM_ConfigType config;
    PWM_ModulationConfigType pwmConfig;
    PWM_IndependentChConfig independentChConfig[3];

    memset(&config, 0U, sizeof(config));
    memset(&pwmConfig, 0U, sizeof(pwmConfig));
    memset(&independentChConfig, 0U, sizeof(independentChConfig));

    GPIO_SetFunc(GPIOA, GPIO_PIN3, GPIO_FUN1);  /* PA3  PWM2_CH0 */
    GPIO_SetFunc(GPIOB, GPIO_PIN3, GPIO_FUN1);  /* PB3  PWM2_CH2 */
    GPIO_SetFunc(GPIOB, GPIO_PIN13, GPIO_FUN1); /* PB13 PWM2_CH3 */

    independentChConfig[0].channel = PWM_CH_0;                        /* Independent output channel selection */
    independentChConfig[0].chValue = PWM2_CH0_VALUE;                  /* The channel matches the value and determines the duty cycle */
    independentChConfig[0].levelMode = PWM_HIGH_TRUE;                 /* Output level polarity after the channel matching value is reached */

    /* Channel output polarity, which affects the level after mask, the level after triggering fault control,
       and the level for deadzone insertion. Here, the polarity is configured to be high, that is, low output after mask */
    independentChConfig[0].polarity = PWM_OUTPUT_POLARITY_ACTIVE_HIGH;
    independentChConfig[0].interruptEn = ENABLE;                      /* The channel matches the interrupt enable bit */
    independentChConfig[0].initLevel = PWM_LOW_LEVEL;                 /* Channel initial level configuration, which affects the level before channel matching after initialization */
    independentChConfig[0].triggerEn = DISABLE;                       /* When the channel value is matched, the trigger signal can be generated for the trigger source of other modules */


    independentChConfig[1].channel = PWM_CH_2;
    independentChConfig[1].chValue = PWM2_CH2_VALUE;
    independentChConfig[1].levelMode = PWM_HIGH_TRUE;
    independentChConfig[1].polarity = PWM_OUTPUT_POLARITY_ACTIVE_HIGH;
    independentChConfig[1].interruptEn = ENABLE;
    independentChConfig[1].initLevel = PWM_LOW_LEVEL;
    independentChConfig[1].triggerEn = DISABLE;


    independentChConfig[2].channel = PWM_CH_3;
    independentChConfig[2].chValue = PWM2_CH3_VALUE;
    independentChConfig[2].levelMode = PWM_HIGH_TRUE;
    independentChConfig[2].polarity = PWM_OUTPUT_POLARITY_ACTIVE_HIGH;
    independentChConfig[2].interruptEn = ENABLE;
    independentChConfig[2].initLevel = PWM_LOW_LEVEL;
    independentChConfig[2].triggerEn = DISABLE;

    pwmConfig.countMode = PWM_UP_DOWN_COUNT;            /* PWM counter mode Settings */
    pwmConfig.independentChannelNum = 3;                /* Number of independent channels */
    pwmConfig.combineChannelNum = 0;                    /* Combined channel logarithm */
    pwmConfig.independentChConfig = independentChConfig;/* Independent channel configuration struct assignment */
    pwmConfig.combineChConfig = NULL;                   /* Combined channel configuration struct assignment */
    pwmConfig.initChOutputEn = ENABLE;                  /* Initialize the channel output enable bit */
    pwmConfig.initTriggerEn = DISABLE;                  /* The PWM initial value triggers the enable bit */

    config.mode = PWM_MODE_MODULATION;     /* PWM mode configuration */
    config.initModeStruct = &pwmConfig;    /* Different modes initialize configuration struct assignments accordingly */
    config.clkSource = PWM_CLK_SOURCE_APB; /* PWM clock source selection */
    config.clkPsc = PWM2_CLK_PSC - 1;      /* Clock division factor */
    config.initValue = 0;                  /* Initial count value */
    config.maxValue = PWM2_MAX_VALUE;      /* 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  ============================================== */
