/******************************************************************************
 * @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_Frequency                        20000                          /* 20kHz PWM */

#if defined(SPD1163)
    #define             PWM_Sel                PWM2
    #define             PIN_PWMB               GPIO_39
    #define             PIN_PWMB_CH            GPIO39_PWM2B
#else
    #define             PWM_Sel                PWM5
    #define             PIN_PWMB               GPIO_37
    #define             PIN_PWMB_CH            GPIO37_PWM5B
#endif

uint32_t               u32PWMPeriod;


/*************************************************************************************************************************
*
* @brief      In this case, 20kHz waveform with 50% duty will be output through PWM_Sel B channel, bringing out by PWM up-down
*             counting mode. we can measure this waveform with GPIO37 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 B */
    PWM_SingleChannelInit(PWM_Sel, PWM_CHB, PWM_Frequency);

    /*
     * 'PWM_SingleChannelInit' had init the count mode as up and down, so,
     * there is no need to call 'PWM_SetCounterMode()'.
     */

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

    /* Select GPIO37 as the channel B output of PWM_Sel */
    GPIO_SetPinChannel(PIN_PWMB, PIN_PWMB_CH);

    /* Start PWM_Sel */
    PWM_RunCounter(PWM_Sel);

    while (1)
    {

    }
}
