/******************************************************************************
 * @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_US                              1000                                            /* 1us */
#define                PWM_SYNC_RELOADVAL                     100                                             /* the value load to TBCNT after sync signal */
#define                TIMER_MS                               2000                                            /* 2s */

uint32_t               u32PWMPeriod;


 /*************************************************************************************************************************
 *
 * @brief      In this case, timer0 asserted SYNC signal to all PWMs(PWM0/1/2) synchronously, PWMs's waveform will be
 *             synchronous after 2s.
 *
 *             The key point :
 *                            <1> keeping the same counting mode and SYNC re-load value after SYNC signal came in these PWMs
 *                                (PWM0/1/2).
 *
 *************************************************************************************************************************/


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_US);
  PWM_ComplementaryPairChannelInit(PWM_V, PWM_FREQ, PWM_DB_US);
  PWM_ComplementaryPairChannelInit(PWM_U, PWM_FREQ, PWM_DB_US);

  /* If we want to use TIMER as the SYNC signal, TBCNT SYNC out signal when TBCNT=0 must be close */
  PWM_SetSyncOutEvent(PWM_W, TBCTL_BIT_SYNCOSEL_DISABLE);
  PWM_SetSyncOutEvent(PWM_V, TBCTL_BIT_SYNCOSEL_DISABLE);

  /* Set PWMxA output 25% 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);
  /* Generate a delay between these waveform deliberately */
  Delay_Us(7);
  PWM_RunCounter(PWM_V);
  Delay_Us(7);
  PWM_RunCounter(PWM_U);

  /* Set the PWMs work at counting up mode after SYNC */
  PWM_SetCounterDirAfterSync(PWM_W, COUNT_UP);
  PWM_SetCounterDirAfterSync(PWM_V, COUNT_UP);
  PWM_SetCounterDirAfterSync(PWM_U, COUNT_UP);

  /*
   * 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_W, PWM_SYNC_RELOADVAL);
  PWM_SetSyncReloadValue(PWM_V, PWM_SYNC_RELOADVAL);
  PWM_SetSyncReloadValue(PWM_U, PWM_SYNC_RELOADVAL);

  /* Enable PWM SYNC by the signal coming from TIMER1 */
  PWM_EnableSyncFromTIMER1(INC_PWM0|INC_PWM1|INC_PWM2);

  /* Init TIMER1 */
  TIMER_Init(TIMER1, TIMER_MS);

  /* Set TIMER1 generate SYNC to PWM when count down to 0 */
  TIMER_EnablePWMSync(TIMER1);

  /* Open Global INT for TIMER1 */
  NVIC_EnableIRQ(TIMER1_IRQn);

  while(1)
  {
    
  }
}

void TIMER1_IRQHandler()
{
  /* Clear the INT */
  TIMER_ClearInt(TIMER1);

  /* Disable the TIMER1 */
  TIMER_Stop(TIMER1);

  printf("timer1 INT happened\n");
  }
