/******************************************************************************
 * @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                              20000                                            /* 20kHz PWM */

uint32_t               u32PWMPeriod;


 /*************************************************************************************************************************
 *
 * @brief      In this case, 20kHz waveform with 50% duty will be output through PWM5 B channel, bringing out by PWM up-down 
 *             counting mode. we can measure this waveform with GPIO37 connected to a oscilloscope.
 *
 *             GPIO16 set as One-shot TZ0 signal and trigger event when it is low, GPIO17/18 set as CBC TZ1/2 signal and 
 *             trigger event when it is low. As a result, when GPIO16 is low PWM disable the output waveform untill TZ flag
 *             is clear. when GPIO17/18 is low, PWM disable output waveform and enable ouyput waveform immediately at the
 *             next PWM clock.
 *
 *************************************************************************************************************************/


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

  /* Select GPIO37 as the channel B output of PWM5 */
  GPIO_SetPinChannel(GPIO_37, GPIO37_PWM5B);

  /* Init PWM5 and output 20kHz waveform on channel B */
  PWM_SingleChannelInit(PWM5, PWM_CHB,PWM_FREQ);

  /* Configure PWM counter as up counting mode */
  PWM_SetCounterMode(PWM5, COUNT_DOWN);

  /*
   * The PWM counting mode is set to be up-down by default, as a result, we need 
   * to re-config the AQCTLA register, which is used for controlling the PWM output
   * action when ZRO/CAU/PRD/CAD (for more infomation, please read the PWM application
   * note) event had happened.
   * 
   * Key Point: the PWM freq will be double because of the ZRO/CAU/PRD/CAD event.
   */
   PWM_ActionQualifierCHB(PWM5, AQCTLB_ALL_ZRO_SET_HIGH
                              | AQCTLB_ALL_CAU_DO_NOTHING
                              | AQCTLB_ALL_PRD_DO_NOTHING
                              | AQCTLB_ALL_CAD_SET_LOW);

  /* Set PWM5B output 50% duty waveform */
  u32PWMPeriod = PWMPeriod(PWM_FREQ);
  PWM_SetCMPA(PWM5,u32PWMPeriod / 2);

  /* Start PWM5 */
  PWM_RunCounter(PWM5);

  /*
   * Trigger TZ0 event when GPIO16 is high, The GPIO need to be configured as input and
   * select the GPIO as the trip-zone source. 'PWM_SetTZ0()' had achieved this function.
   *
   * 
   * Key point : Beacuse of the GPIO is pulled up internal by MCU. so, TZ triggered by
   * GPIO low level is recommended.
   */
  PWM_SetTZ0FromGPIO(GPIO_16, GPIO_LEVEL_LOW);

  /* Trigger TZ1 event when GPIO17 is low */
  PWM_SetTZ1FromGPIO(GPIO_17, GPIO_LEVEL_LOW);

  /* Trigger TZ2 event when GPIO18 is low */
  PWM_SetTZ2FromGPIO(GPIO_18, GPIO_LEVEL_LOW);

  /*
   * Set TZ0 as one-shot trip event. The one-shot mode means once the
   * corresponding event has happended, the PWM waveform will stop, and
   * will not start untill the flag is cleared.
   *
   * The key poiny there is 'TZEVT_ASYNC_ONLY_FOR_DEBUG' can not be 
   * set in practical engineering, it can only be used in DEBUG mode.
   */
  PWM_SetOneShotTripEvent(PWM5, TRIP_EVENT_TZ0, TRIP_OUTPUT_ASYNC_OR_LATCH);

  /*
   * Set TZ1 and TZ2 as CBC trip event. The symbol 'CBC' means once the
   * corressponding event has happended, the PWM waveform will stop, but
   * start again at the next PWM CLK period with do nothing manually.
   *
   * The key point there is the same as 'PWM_SetOneShotTripEvent()'
   */
  PWM_SetCBCTripEvent(PWM5,TRIP_EVENT_TZ1|TRIP_EVENT_TZ2, TRIP_OUTPUT_ASYNC_OR_LATCH);

  /* 
   * Set PWM0 output as tristate upon one-shot and CBC trip event,
   * need to explicitly specify actions for all 6 trip scenarios 
   */
  PWM_SetCHAOutputWhenTrip(PWM5, TZU_TRIP_AS_TRI_STATE |
                                 TZD_TRIP_AS_TRI_STATE |
                                 DCEVT0U_TRIP_DO_NOTHING |
                                 DCEVT0D_TRIP_DO_NOTHING |
                                 DCEVT1U_TRIP_DO_NOTHING |
                                 DCEVT1D_TRIP_DO_NOTHING);

  PWM_SetCHBOutputWhenTrip(PWM5, TZU_TRIP_AS_TRI_STATE |
                                 TZD_TRIP_AS_TRI_STATE |
                                 DCEVT0U_TRIP_DO_NOTHING |
                                 DCEVT0D_TRIP_DO_NOTHING |
                                 DCEVT1U_TRIP_DO_NOTHING |
                                 DCEVT1D_TRIP_DO_NOTHING);

  while(1)
  {

  }
}
