/******************************************************************************
 * @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 25% duty will be output through PWM5 A channel, bringing out by PWM up
 *             counting mode. we can measure this waveform with GPIO36 connected to a oscilloscope.
 *
 *************************************************************************************************************************/


int main()
{
  FLASH_WALLOW();
  FLASH_SetTiming(200000000);
  /* Disable flash write access after flash operation had done */
  FLASH_WDIS();

  CLOCK_InitWithRCO(CLOCK_HCLK_200MHZ);
  
  FLASH_WDIS();
  
  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 PWM5,PWM freq will be set as 20kHz in this function */
  PWM_SingleChannelInit(PWM5,PWM_CHA, 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_ActionQualifierCHA(PWM5, AQCTLA_ALL_ZRO_SET_HIGH
                              | AQCTLA_ALL_CAU_DO_NOTHING
                              | AQCTLA_ALL_PRD_DO_NOTHING
                              | AQCTLA_ALL_CAD_SET_LOW);

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

  /* Select GPIO36 as the channel A output of PWM5 */
  GPIO_SetPinChannel(GPIO_36, GPIO36_PWM5A);

  /* Start PWM5 */
  PWM_RunCounter(PWM5);

  while(1)
  {

  }
}
