/******************************************************************************
 * @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;

#if defined(SPD1188)
    #define               PWM_U                             PWM0
    #define               PWM_V                             PWM1
    #define               PWM_W                             PWM2
    #define               LINK_PWM_U                        SEL_PWM0
    #define               GPIO_PWM_U_H                      GPIO_30
    #define               GPIO_PWM_V_H                      GPIO_36
    #define               GPIO_PWM_W_H                      GPIO_38
    #define               GPIO_PWM_U_H_SEL                  GPIO30_PWM0A
    #define               GPIO_PWM_V_H_SEL                  GPIO36_PWM1A
    #define               GPIO_PWM_W_H_SEL                  GPIO38_PWM2A
    #define               GPIO_PWM_U_L                      GPIO_21
    #define               GPIO_PWM_V_L                      GPIO_37
    #define               GPIO_PWM_W_L                      GPIO_39
    #define               GPIO_PWM_U_L_SEL                  GPIO21_PWM0B
    #define               GPIO_PWM_V_L_SEL                  GPIO37_PWM1B
    #define               GPIO_PWM_W_L_SEL                  GPIO39_PWM2B
#else
    #define               PWM_U                             PWM5
    #define               PWM_V                             PWM1
    #define               PWM_W                             PWM2
    #define               LINK_PWM_U                        SEL_PWM5
    #define               GPIO_PWM_U_H                      GPIO_28
    #define               GPIO_PWM_V_H                      GPIO_20
    #define               GPIO_PWM_W_H                      GPIO_22
    #define               GPIO_PWM_U_H_SEL                  GPIO28_PWM5A
    #define               GPIO_PWM_V_H_SEL                  GPIO20_PWM1A
    #define               GPIO_PWM_W_H_SEL                  GPIO22_PWM2A
    #define               GPIO_PWM_U_L                      GPIO_29
    #define               GPIO_PWM_V_L                      GPIO_21
    #define               GPIO_PWM_W_L                      GPIO_23
    #define               GPIO_PWM_U_L_SEL                  GPIO29_PWM5B
    #define               GPIO_PWM_V_L_SEL                  GPIO21_PWM1B
    #define               GPIO_PWM_W_L_SEL                  GPIO23_PWM2B
#endif


#define               PWM_FREQ                          20000                                   /* 20kHz PWM */
#define               PWM_DB_NS                         1000                                    /* 1000ns */

uint32_t              u3232PWMPeriod;


/*************************************************************************************************************************
*
* @brief      In this case, A demo how to output tri-phase waveform of BLDC(brushless direct current) is given. The output
*             waveform can be measured by using an oscilloscope to measure GPIO20/22/28 simultaneously.
*
*************************************************************************************************************************/


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

    /* Select GPIO28/29 as the channel A/B output of PWM5 respectively */
    GPIO_SetPinChannel(GPIO_PWM_U_H, GPIO_PWM_U_H_SEL);
    GPIO_SetPinChannel(GPIO_PWM_U_L, GPIO_PWM_U_L_SEL);

    /* Select GPIO20/21 as the channel A/B output of PWM1 respectively */
    GPIO_SetPinChannel(GPIO_PWM_V_H, GPIO_PWM_V_H_SEL);
    GPIO_SetPinChannel(GPIO_PWM_V_L, GPIO_PWM_V_L_SEL);

    /* Select GPIO22/23 as the channel A/B output of PWM2 respectively */
    GPIO_SetPinChannel(GPIO_PWM_W_H, GPIO_PWM_W_H_SEL);
    GPIO_SetPinChannel(GPIO_PWM_W_L, GPIO_PWM_W_L_SEL);

    /*
     * Init PWM5/1/2 and output 20kHz waveform on both channel A and channel B.
     */
    PWM_ComplementaryPairChannelInit(PWM_U, PWM_FREQ, PWM_DB_NS);
    PWM_ComplementaryPairChannelInit(PWM_V, PWM_FREQ, PWM_DB_NS);
    PWM_ComplementaryPairChannelInit(PWM_W, PWM_FREQ, PWM_DB_NS);

    /* Start counting */
    PWM_RunCounter(PWM_U);
    PWM_RunCounter(PWM_V);
    PWM_RunCounter(PWM_W);

    /* PWM5 set CMPA and loading the same value to PWM1/2 synchronously */
    PWM_LinkCMPA(PWM_V, LINK_PWM_U);
    PWM_LinkCMPA(PWM_W, LINK_PWM_U);

    /* Set the duty as 75% */
    u3232PWMPeriod = PWMPeriod(PWM_FREQ);
    PWM_SetCMPA(PWM_U, u3232PWMPeriod / 4);

    while (1)
    {

    }
}
