/******************************************************************************
 * @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                                             /* 1us */
#define                PWM_SYNC_RELOADVAL                    0x0                                              /* the value load to TBCNT after sync signal */

uint32_t               u32RELoadValA;
uint32_t               u32PWMPeriod;


 /*************************************************************************************************************************
 *
 * @brief      In this case, PWM0 will output SYNC signal triggered by software force to PWM1, and then PWM1 pass the SYNC
 *             signal to PWM2. There is no phase difference between PWMx A/B channel, this can be measured at the LO PADs 
 *             of different FETs on board(such as, PRE-DRIVER0 phaseU LO and phaseV LO have no phase difference).
 * 
 *             The key point of this demo is keeping the same counting mode and SYNC re-load value after SYNC signal came
 *             in these PWMs .
 *
 *************************************************************************************************************************/


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);
  
  /* PWM_U set CMPA and loading the same value to PWM_V/PWM_W synchronously */
  PWM_LinkCMPA(PWM_V, LINK_PWM_W);
  PWM_LinkCMPA(PWM_U, LINK_PWM_W);

  /* Set PWM0A output 25% duty waveform */
  u32PWMPeriod = PWMPeriod(PWM_FREQ);
  PWM_SetCMPA(PWM_W, (u32PWMPeriod * 3)/ 4);

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

  /*
   * In order to generate a sotfware force SYNC signal, we need to set 'SYNCOSEL' of PWM0 as 
   * 'TBCTL_BIT_SYNCOSEL_SYNCI_AND_FRCSYNC'. In order to pass through the same SYNC signal of
   * PWM0 to the PWM2, we also need to set PWM1 as 'TBCTL_BIT_SYNCOSEL_SYNCI_AND_FRCSYNC'.
   *
   *
   * Key point : the SYNC signal can only pass through two group(from 0 to 2 and 3 to 5, 2 to
   * 3 is not permitted).
   */
  PWM_SetSyncOutEvent(PWM_W,TBCTL_BIT_SYNCOSEL_SYNCI_AND_FRCSYNC);
  PWM_SetSyncOutEvent(PWM_V,TBCTL_BIT_SYNCOSEL_SYNCI_AND_FRCSYNC);

  /* Enable load SYNC value from SYNC signal is happeded*/
  PWM_EnableSync(PWM_U);
  PWM_EnableSync(PWM_V);
  PWM_EnableSync(PWM_W);

  /* Set PWMx counting down after SYNC */
  PWM_SetCounterDirAfterSync(PWM_U, COUNT_DOWN);
  PWM_SetCounterDirAfterSync(PWM_V, COUNT_DOWN);
  PWM_SetCounterDirAfterSync(PWM_W, COUNT_DOWN);

  /*
   * Set the value will be loaded to TBCNT after SYNC signal.
   *
   * All the PWMs must be the same SYNC reload value to get the synchronized
   * waveform.
   */
  PWM_SetSyncReloadValue(PWM_U, PWM_SYNC_RELOADVAL);
  PWM_SetSyncReloadValue(PWM_V, PWM_SYNC_RELOADVAL);
  PWM_SetSyncReloadValue(PWM_W, PWM_SYNC_RELOADVAL);

  /* Force a sofrware SYNC signal of PWM0 */
  PWM_ForceSync(INC_PWM0);

  while(1)
  {

  }
}
