/******************************************************************************
 * @file     main.c
 * @brief    Main program body
 * @version  V4.0.0
 * @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_FREQ                            20000              /* 20kHz PWM */
#define             PWM_DB_NS                           1000               /* 1000ns */
#define             PWM_SYNC_ReLOADVAL                  100                /* the value load to TBCNT after sync signal */

uint32_t            u32PWMPeriod;
uint32_t            u32ActualReLoadVal1;
uint32_t            u32ActualReLoadVal2;


/*************************************************************************************************************************
 *
 * @brief      In this case, PWM0 will output SYNC signal on the point 'TBCNT=zero' continuously, the SYNC signal reach the
 *             PWM1 and then pass to the PWM2.
 *
 *
 *************************************************************************************************************************/


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);

#if defined(SPD1148) || defined(SPD1178) || defined(SPD1188)
    /* Select GPIO30/31 as the channel A/B output of PWM0 respectively */
    GPIO_SetPinChannel(GPIO_30, GPIO30_PWM0A);
    GPIO_SetPinChannel(GPIO_31, GPIO31_PWM0B);
    /* Select GPIO36/37 as the channel A/B output of PWM1 respectively */
    GPIO_SetPinChannel(GPIO_36, GPIO36_PWM1A);
    GPIO_SetPinChannel(GPIO_37, GPIO37_PWM1B);
    /* Select GPIO38/39 as the channel A/B output of PWM2 respectively */
    GPIO_SetPinChannel(GPIO_38, GPIO38_PWM2A);
    GPIO_SetPinChannel(GPIO_39, GPIO39_PWM2B);
#else
    /* Select GPIO18/19 as the channel A/B output of PWM0 respectively */
    GPIO_SetPinChannel(GPIO_18, GPIO18_PWM0A);
    GPIO_SetPinChannel(GPIO_19, GPIO19_PWM0B);
    /* Select GPIO20/21 as the channel A/B output of PWM1 respectively */
    GPIO_SetPinChannel(GPIO_20, GPIO20_PWM1A);
    GPIO_SetPinChannel(GPIO_21, GPIO21_PWM1B);
    /* Select GPIO22/23 as the channel A/B output of PWM2 respectively */
    GPIO_SetPinChannel(GPIO_22, GPIO22_PWM2A);
    GPIO_SetPinChannel(GPIO_23, GPIO23_PWM2B);
#endif

    /* Init PWM on both channel A and channel B */
    PWM_ComplementaryPairChannelInit(PWM0, PWM_FREQ, PWM_DB_NS);
    PWM_ComplementaryPairChannelInit(PWM1, PWM_FREQ, PWM_DB_NS);
    PWM_ComplementaryPairChannelInit(PWM2, PWM_FREQ, PWM_DB_NS);
    PWM_ActionQualifierCHA(PWM2, CAU_SET_LOW | CAD_SET_HIGH);

    /* Set PWMxA output duty waveform */
    u32PWMPeriod = PWMPeriod(PWM_FREQ);
    PWM_SetCMPA(PWM0, (u32PWMPeriod * 2) / 3);
    PWM_SetCMPA(PWM1, (u32PWMPeriod * 2) / 3);
    PWM_SetCMPA(PWM2, (u32PWMPeriod * 1) / 3);

    PWM_SetSyncOutEvent(PWM0, TBCTL_BIT_SYNCOSEL_TBCNT_EQU_ZERO);
    PWM_SetSyncOutEvent(PWM1, TBCTL_BIT_SYNCOSEL_SYNCI_OR_FRCSYNC);

    /* Enable PWM synchronization */
    PWM_EnableSync(PWM0);
    PWM_EnableSync(PWM1);
    PWM_EnableSync(PWM2);

    /* Set PWMx counting up after SYNC */
    PWM_SetCounterDirAfterSync(PWM0, COUNT_UP);
    PWM_SetCounterDirAfterSync(PWM1, COUNT_UP);
    PWM_SetCounterDirAfterSync(PWM2, COUNT_UP);

    /* Set the re-load value when SYNC signal happened */
    PWM_SetSyncReloadValue(PWM1, (PWM1)->CMPA.all);
    PWM_SetSyncReloadValue(PWM2, ((PWM2)->CMPA.all));

    /* Start counting */
    PWM_RunCounter(PWM2);
    PWM_RunCounter(PWM1);
    PWM_RunCounter(PWM0);

    while (1)
    {

    }
}
