/******************************************************************************
 * @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_U                                 PWM2
#define                PWM_V                                 PWM1
#define                PWM_W                                 PWM0
#define                LINK_PWM_W                            SEL_PWM0
#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               u32ActureReLoadVal1;
uint32_t               u32ActureReLoadVal2;


 /*************************************************************************************************************************
 *
 * @brief      In this case, PWM0 will output SYNC signal at the moument when 'TBCNT=zero' continuously, the SYNC signal
 *             reach the PWM1 and then pass to the PWM2.
 * 
 *             Key Point:
 *                       <1> PWM0/1/2 output waveform will be sent to PRE-DRIVER0, for more information, please refer to
 *                           reference manual.
 *
 *************************************************************************************************************************/


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

  /*
   * Init PWM0/1/2 and output 20kHz waveform on both channel A and channel B.
   *
   * There is a key point, because of PWM5 sent the SYNC signal when TBCNT=ZERO,
   * the counter will be in up counting, this makes when the SYNC signal of PWM5
   * is coming, PWM1 and PWM2 must be also in up counting, which was set in 
   * 'PWM_ComplementaryPairChannelInit()'.
   */
  PWM_ComplementaryPairChannelInit(PWM_W, PWM_FREQ, PWM_DB_NS);
  PWM_ComplementaryPairChannelInit(PWM_V, PWM_FREQ, PWM_DB_NS);
  PWM_ComplementaryPairChannelInit(PWM_U, PWM_FREQ, PWM_DB_NS);

  /* Set PWMxB output 50% duty waveform */
  u32PWMPeriod = PWMPeriod(PWM_FREQ);
  PWM_SetCMPA(PWM_W, (u32PWMPeriod * 3)/ 4);
  PWM_SetCMPA(PWM_V, (u32PWMPeriod * 3)/ 4);
  PWM_SetCMPA(PWM_U, (u32PWMPeriod * 3)/ 4);

  /* Start counting */
  PWM_RunCounter(PWM_W);
  /* Add delay to make PWMs waveform asynchronous */
  Delay_Us(7);
  PWM_RunCounter(PWM_V);
  Delay_Us(7);
  PWM_RunCounter(PWM_U);

  /* 
   * There is a delay between the moment SYNC signal generated and the time reached PWM1 and PWM2,
   * so PWM1 and PWM2 can not use the same reload value as PWM0, we must re-calculate the value for
   * PWM1 and PWM2.
   * 
   * 
   * Key point : in this mode, PWM0 reload count value from TBPRD register(not TBPHS register), in
   * this demo, PWM0 works at counting up-down mode, the value reload from TBPRD (when TNCNT=0, it 
   * begin reload value) is 0.
   */
  u32ActureReLoadVal1 = PWM_CalculateSyncReloadValue(u32PWMPeriod,
                                                     COUNT_UP_DOWN,
                                                     COUNT_UP,
                                                     0,
                                                     0);
                                              
  /* Set the re-load value when SYNC signal happended */
  PWM_SetSyncReloadValue(PWM_V, u32ActureReLoadVal1);

  /* PWM1 reload value is 'u32ActureReLoadVal1', we use this value to calculate value for PWM2 */
  u32ActureReLoadVal2 = PWM_CalculateSyncReloadValue(u32PWMPeriod,
                                                     COUNT_UP_DOWN,
                                                     COUNT_UP,
                                                     u32ActureReLoadVal1,
                                                     0);
                                              
  /* Set the re-load value when SYNC signal happended */
  PWM_SetSyncReloadValue(PWM_U, u32ActureReLoadVal2);

  while(1)
  {
    
  }
}
