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

uint32_t               u32PWMPeriod;
ErrorStatus            eErrorState;
uint8_t                u8PREDRIDATA;                                                                           /* Data read from PRE-DRIVER */
uint32_t               u32SSPBaundrate                        = 1000000;                                       /* The baundrate of SSP communicating with PRE-DRIVER mode */
uint32_t               u32Timeout                             = 500;                                           /* The number of times try to get PRE-DRIVER mode ID */
uint8_t                u8PREDRIID                             = 0;                                             /* PRE-DRIVER mode ID */


 /*************************************************************************************************************************
 *
 * @brief      In this case, 20kHz waveform with 50% duty will be output through PWM5B channel, bringing out by PWM up-down
 *             counting mode. we can measure this waveform with GPIO37 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);

  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 and output 20kHz waveform on channel B */
  PWM_SingleChannelInit(PWM5,PWM_CHB,PWM_Frequency);

  /*
   * 'PWM_SingleChannelInit' had init the count mode as up and down, so,
   * there is no need to call 'PWM_SetCounterMode()'.
   */

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

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

  /* Start PWM5 */
  PWM_RunCounter(PWM5);

  while(1)
  {

  }
}
