/* 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>
#include "ac780x_pwm.h"
#include "ac780x_gpio.h"

/* ============================================  Define  ============================================ */
#define PWM1_FREQ      (2000UL)                                      /* PWM1 frequency, here set to 2KHz */
#define PWM1_CLK_PSC   (1UL)                                         /* PWM1 clock frequency division, here set 1 frequency division */
#define PWM1_MAX_VALUE (g_periphAPBClock / PWM1_CLK_PSC / PWM1_FREQ) /* PWM1 counter maximum count value */

#define PWM1_CH0_DUTY  (20UL)                                   /* PWM1 channel 0 duty cycle, here set to 20% */
#define PWM1_CH0_VALUE (PWM1_CH0_DUTY * PWM1_MAX_VALUE / 100UL) /* PWM1 channel 0 match value */

#define PWM1_CH1_DUTY  (80UL)                                   /* PWM1 channel 1 duty cycle, here set to 80% */
#define PWM1_CH1_VALUE (PWM1_CH1_DUTY * PWM1_MAX_VALUE / 100UL) /* PWM1 channel 1 match value */

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

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

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

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

/*!
 * @brief PWM1_Output_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_Output_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));

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

    /* Independent output parameters */
    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;

    /* PWM parameters */
    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 parameters */
    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        = DISABLE;             /* PWM total interrupt enable bit */
    config.callback           = NULL;                /* PWM interrupts user callback function */

    PWM_Init(PWM1, &config);
}

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