/******************************************************************************
 * @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                                             /* 1ns */
#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, PWM5 will output SYNC signal on the point 'TBCNT=CMPD', the SYNC signal reach the PWM1 and then
 *             pass to the PWM2, what's more, all the PWMs is counting at up mode, this makes there is no need to config
 *             counting mode after SYNC.
 * 
 * 
 *             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.
   */
  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);

  /*PWM0 generate SYNC when TBCNT=CMPD*/
  PWM_SetSyncOutEvent(PWM_W, SYNC_TBCNT_EQU_CMPD);

  /* 
   * In 'PWM_ComplementaryPairChannelInit' PWM was configed TBCNT=CMPA, so TBCNT=CMPB
   * need to be re-configed manually.
   */
  PWM_ActionQualifierCHA(PWM_W, ZRO_DO_NOTHING
                             | PRD_DO_NOTHING
                             | CBU_SET_HIGH
                             | CBD_SET_LOW);
                             
  PWM_ActionQualifierCHA(PWM_V, ZRO_DO_NOTHING
                             | PRD_DO_NOTHING
                             | CBU_SET_HIGH
                             | CBD_SET_LOW);
                             
  PWM_ActionQualifierCHA(PWM_U, ZRO_DO_NOTHING
                             | PRD_DO_NOTHING
                             | CBU_SET_HIGH
                             | CBD_SET_LOW);

  /*
   * In order to pass the SYNC signal to the PWM1 and then to the PWM2, we need 
   * to set PWM1 as 'SYNC_SYNCI_AND_FRCSYNC' mode. 'SYNC_SYNCI_AND_FRCSYNC' is the default
   * mode, so there is no need to set manually, but, keep in mind that you must set the mode
   * when you need.
   *
   * add the follow when you need.
   *   PWM_SetSyncOutEvent(PWM0,TBCTL_BIT_SYNCOSEL_SYNCI_AND_FRCSYNC);
   */


  /* Set PWM5B output 50% duty waveform */
  u32PWMPeriod = PWMPeriod(PWM_FREQ);
  PWM_SetCMPB(PWM_W, (u32PWMPeriod * 3)/ 4);
  PWM_SetCMPB(PWM_V, (u32PWMPeriod * 3)/ 4);
  PWM_SetCMPB(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);

  /*
   * As you see, the counter CLK is obtained from PWM CLK with dividing num not equal to 1, so there
   * is important to keep PWMs CLK is synchronous.
   */
  PWM_ForceClockSync();

  /* 
   * 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)
  {
    
  }
}
