/******************************************************************************
 * @file     main.c
 * @brief    Main program body
 * @version  V4.0.3
 * @date     30-July-2026
 *
 * @note
 * Copyright (C) 2022 Spintrol Electronic Technology (Shanghai) Co., Ltd.. All rights reserved.
 *
 * @attention
 * THIS SOFTWARE JUST PROVIDES CUSTOMERS WITH CODING INFORMATION REGARDING
 * THEIR PRODUCTS, WHICH AIMS AT SAVING TIME FOR THEM. SPINTROL SHALL NOT BE
 * LIABLE FOR THE USE OF THE SOFTWARE. SPINTROL DOES NOT GUARANTEE THE
 * CORRECTNESS OF THIS SOFTWARE AND RESERVES THE RIGHT TO MODIFY THE SOFTWARE
 * WITHOUT NOTIFICATION.
 *
 ******************************************************************************/

#include<stdio.h>

#if defined(SPC1158)
#include "spc1158.h"
#elif defined(SPD1148)
#include "spd1148.h"
#elif defined(SPD1178)
#include "spd1178.h"
#elif defined(SPD1188)
#include "spd1188.h"
#elif defined(SPD1163)
#include "spd1163.h"
#else
#include "spc1168.h"
#endif

/* This macro is used to get the PWM period with a specified frequency */
#define              PWMPeriod(u32PWMFreqHz)            ((CLOCK_GetModuleClock(PWM_MODULE)) / u32PWMFreqHz) / 2;

#define              PWM_Frequency                      20000                              /* 20kHz PWM */

#if defined(SPD1163)
    #define             PWM_Sel                PWM2
    #define             PIN_PWMA               GPIO_38
    #define             PIN_PWMA_CH            GPIO38_PWM2A
#else
    #define             PWM_Sel                PWM5
    #define             PIN_PWMA               GPIO_36
    #define             PIN_PWMA_CH            GPIO36_PWM5A
#endif

uint32_t             u32PWMPeriod;


/*************************************************************************************************************************
*
* @brief      In this case, 20kHz waveform with 50% duty will be output through PWM_Sel A channel, bringing out by PWM up
*             counting mode. we can measure this waveform with GPIO36 connected to a oscilloscope.
*
*************************************************************************************************************************/


int main()
{
    FLASH_WALLOW();

#if defined(SPC1158)
    FLASH_SetTiming(100000000);
    /* Disable flash write access after flash operation had done */
    FLASH_WDIS();

    CLOCK_InitWithRCO(100000000UL);
#else
    FLASH_SetTiming(200000000);
    /* Disable flash write access after flash operation had done */
    FLASH_WDIS();

    CLOCK_InitWithRCO(200000000UL);
#endif

    Delay_Init();

    /*
     * Init the UART
     *
     * 1.Set the GPIO34/35 as UART FUNC
     *
     * 2.Enable the UART CLK
     *
     * 3.Set the rest para
     */
    GPIO_SetPinChannel(GPIO_34, GPIO34_UART_TXD);
    GPIO_SetPinChannel(GPIO_35, GPIO35_UART_RXD);
    CLOCK_EnableModule(UART_MODULE);
    UART_Init(UART, 38400);

    /* Init PWM_Sel and output 20kHz waveform on channel A */
    PWM_SingleChannelInit(PWM_Sel, PWM_CHA, PWM_Frequency);

    /* Configure PWM counter as up counting mode */
    PWM_SetCounterMode(PWM_Sel, COUNT_UP);

    /*
     * The PWM counting mode is set to be up-down by default, as a result, we need
     * to re-config the AQCTLA register, which is used for controlling the PWM output
     * action when ZRO/CAU/PRD/CAD (for more information, please read the PWM application
     * note) event had happened.
     *
     * Key Point: the PWM freq will be double because of the ZRO/CAU/PRD/CAD event.
     */
    PWM_ActionQualifierCHA(PWM_Sel, AQCTLA_ALL_ZRO_SET_HIGH
                           | AQCTLA_ALL_CAU_SET_LOW
                           | AQCTLA_ALL_PRD_DO_NOTHING
                           | AQCTLA_ALL_CAD_DO_NOTHING);

    /* Set PWM_SelA output 50% duty waveform */
    u32PWMPeriod = PWMPeriod(PWM_Frequency);
    PWM_SetCMPA(PWM_Sel, u32PWMPeriod / 2);

    /* Select the channel A output of PWM_Sel */
    GPIO_SetPinChannel(PIN_PWMA, PIN_PWMA_CH);

    /* Start PWM_Sel */
    PWM_RunCounter(PWM_Sel);

    while (1)
    {

    }
}
