/******************************************************************************
 * @file     main.c
 * @brief    Main program body
 * @version  V1.8.0
 * @date     31-Dec-2021
 *
 * @note
 * Copyright (C) 2021 Spintrol Limited Corporation. 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"spc1168.h"
#include<stdio.h>

/* 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                              1000                                              /* 1kHz PWM */
#define                PWM_DB_NS                             1000                                              /* 1000ns */

uint32_t               u32PWMPeriod;


 /*************************************************************************************************************************
 *
 * @brief      In this case,1kHz waveform with 75% duty will be output through PWM3 A/B channel respectively, so the high
 *             level is 2.5e4ns=25us in one period(50us). but 1us delay will add to the rsing edge of channel A and another
 *             1us to the fall edge of channel B with the sub-module deadband is working. As a result, the high level is 
 *             (25-1=24)us (low level is 50-24=26us) in one period of channel A, and the channel B is the same as channel A,
 *             but a phase offset between channel A and B. There give a sketch map.
 *
 *             This waveform can be measured at GPIO31 and 31.
 *
 *                                                            |<---------- one period ---------> |
 *                                                      ->|1us|<-                                |
 *                                                        |                                      |
 *                                                        |    ---------                         |
 *                                                        |   |         |                        |
 *                                                        |   |         |                        |
 *                             PhaseW HO     -----------------           -----------------------------
 *                             PhaseW LO     -------------            ->|1us|<------------------------
 *                                                        |                 |                |     
 *                                                        |                 |                |   |
 *                                                         -----------------               ->|1us|<-
 *                                                        |                                  |
 *                                                        |                                  |
 *                                                        |<----------- one period --------->|
 *
 *************************************************************************************************************************/


int main()
{ 
  FLASH_WALLOW();
  FLASH_SetTiming(200000000);
  /* Disable flash write access after flash operation had done */
  FLASH_WDIS();

  CLOCK_InitWithRCO(CLOCK_HCLK_200MHZ);

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

  /* Set GPIO30/31 as the A and B channel of PWM3 */
  GPIO_SetPinChannel(GPIO_30,GPIO30_PWM3A);
  GPIO_SetPinChannel(GPIO_31,GPIO31_PWM3B);

  /* 
   * 1. Init PWM3 and output 1kHz waveform on channel A
   * 
   * 2. Add 1ns dead band at falling and rsing edge 
   *
   * 3. Channel B output the cchannel A waveform which added DB
   */
  PWM_ComplementaryPairChannelInit(PWM3, PWM_FREQ, PWM_DB_NS);

  /* Set PWM3A output 75% duty waveform */
  u32PWMPeriod = PWMPeriod(PWM_FREQ);
  PWM_SetCMPA(PWM3, u32PWMPeriod / 4);

  /* Start PWM3 */
  PWM_RunCounter(PWM3);

  while(1)
  {

  }
}
