/******************************************************************************
 * @file     main.c
 * @brief    Main program body
 * @version  V4.0.1
 * @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;

#if defined(SPC1158)
    #define                PWM_2                                 PWM2
    #define                PIN_PWM2A                             GPIO_38
    #define                PIN_PWM2A_CH                          GPIO38_PWM2A
    #define                PIN_PWM2B                             GPIO_39
    #define                PIN_PWM2B_CH                          GPIO39_PWM2B
    #define                PWM_1                                 PWM1
    #define                PIN_PWM1A                             GPIO_36
    #define                PIN_PWM1A_CH                          GPIO36_PWM1A
    #define                PIN_PWM1B                             GPIO_37
    #define                PIN_PWM1B_CH                          GPIO37_PWM1B
    #define                PWM_0                                 PWM0
    #define                PIN_PWM0A                             GPIO_18
    #define                PIN_PWM0A_CH                          GPIO18_PWM0A
    #define                PIN_PWM0B                             GPIO_19
    #define                PIN_PWM0B_CH                          GPIO19_PWM0B
#else
    #define                PWM_2                                 PWM2
    #define                PIN_PWM2A                             GPIO_38
    #define                PIN_PWM2A_CH                          GPIO38_PWM2A
    #define                PIN_PWM2B                             GPIO_39
    #define                PIN_PWM2B_CH                          GPIO39_PWM2B
    #define                PWM_1                                 PWM1
    #define                PIN_PWM1A                             GPIO_36
    #define                PIN_PWM1A_CH                          GPIO36_PWM1A
    #define                PIN_PWM1B                             GPIO_37
    #define                PIN_PWM1B_CH                          GPIO37_PWM1B
    #define                PWM_0                                 PWM0
    #define                PIN_PWM0A                             GPIO_30
    #define                PIN_PWM0A_CH                          GPIO30_PWM0A
    #define                PIN_PWM0B                             GPIO_31
    #define                PIN_PWM0B_CH                          GPIO31_PWM0B
#endif
#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 */
#define                TIMER_MS                               2000                                            /* 2s */

uint32_t               u32PWMPeriod;


/*************************************************************************************************************************
 *
 * @brief      In this case, SYNC signal triggered by TIMER1 will be asserted to all PWMs(PWM0/PWM1/PWM2) synchronously.
 *
 *
 *************************************************************************************************************************/


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

    GPIO_SetPinChannel(PIN_PWM2A, PIN_PWM2A_CH);
    GPIO_SetPinChannel(PIN_PWM2B, PIN_PWM2B_CH);

    GPIO_SetPinChannel(PIN_PWM1A, PIN_PWM1A_CH);
    GPIO_SetPinChannel(PIN_PWM1B, PIN_PWM1B_CH);

    GPIO_SetPinChannel(PIN_PWM0A, PIN_PWM0A_CH);
    GPIO_SetPinChannel(PIN_PWM0B, PIN_PWM0B_CH);

    /* Init PWM on both channel A and channel B */
    PWM_ComplementaryPairChannelInit(PWM_0, PWM_FREQ, PWM_DB_NS);
    PWM_ComplementaryPairChannelInit(PWM_1, PWM_FREQ, PWM_DB_NS);
    PWM_ComplementaryPairChannelInit(PWM_2, PWM_FREQ, PWM_DB_NS);
    PWM_ActionQualifierCHA(PWM_2, CAU_SET_LOW | CAD_SET_HIGH);

    /* Disable all SYNCO */
    PWM_SetSyncOutEvent(PWM_0, TBCTL_BIT_SYNCOSEL_DISABLE);
    PWM_SetSyncOutEvent(PWM_1, TBCTL_BIT_SYNCOSEL_DISABLE);
    PWM_SetSyncOutEvent(PWM_2, TBCTL_BIT_SYNCOSEL_DISABLE);

    /* Set PWM0A output duty waveform */
    u32PWMPeriod = PWMPeriod(PWM_FREQ);
    PWM_SetCMPA(PWM_0, (u32PWMPeriod * 2) / 3);
    PWM_SetCMPA(PWM_1, (u32PWMPeriod * 2) / 3);
    PWM_SetCMPA(PWM_2, (u32PWMPeriod * 1) / 3);

    /* Enable PWM synchronization */
    PWM_EnableSync(PWM_0);
    PWM_EnableSync(PWM_1);
    PWM_EnableSync(PWM_2);

    /* Set PWMx counting up after SYNC */
    PWM_SetCounterDirAfterSync(PWM_0, COUNT_UP);
    PWM_SetCounterDirAfterSync(PWM_1, COUNT_UP);
    PWM_SetCounterDirAfterSync(PWM_2, COUNT_UP);

    /*
     * Set the value will be loaded to TBCNT after SYNCI signal.
     */
    PWM_SetSyncReloadValue(PWM_0, (PWM0)->CMPA.all);
    PWM_SetSyncReloadValue(PWM_1, 0);
    PWM_SetSyncReloadValue(PWM_2, (PWM2)->CMPA.all);

    /* Enable PWM SYNC by the signal coming from TIMER1 */
    PWM_EnableSyncFromTIMER1(INC_PWM0 | INC_PWM1 | INC_PWM2);
    
    /*Start counting*/
    PWM_RunCounter(PWM_2);
    PWM_RunCounter(PWM_1);
    PWM_RunCounter(PWM_0);

    /* Open Global INT for TIMER1 */
    NVIC_EnableIRQ(TIMER1_IRQn);
    
    /* Init TIMER1 */
    TIMER_Init(TIMER1, TIMER_MS);

    /* Set TIMER1 generate SYNC to PWM when count down to 0 */
    TIMER_EnablePWMSync(TIMER1);

    while (1)
    {

    }
}

void TIMER1_IRQHandler()
{
    /* Clear the INT */
    TIMER_ClearInt(TIMER1);

    /* Disable the TIMER1 */
    TIMER_Stop(TIMER1);

    printf("timer1 INT happened\n");
}
