/******************************************************************************
 * @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                                 PWM5
#define                PWM_V                                 PWM1
#define                PWM_W                                 PWM2
#define                LINK_PWM_U                            SEL_PWM5
#define                GPIO_PWM_U_H                          GPIO_28
#define                GPIO_PWM_V_H                          GPIO_20
#define                GPIO_PWM_W_H                          GPIO_22
#define                GPIO_PWM_U_H_SEL                      GPIO28_PWM5A
#define                GPIO_PWM_V_H_SEL                      GPIO20_PWM1A
#define                GPIO_PWM_W_H_SEL                      GPIO22_PWM2A
#define                GPIO_PWM_U_L                          GPIO_29
#define                GPIO_PWM_V_L                          GPIO_21
#define                GPIO_PWM_W_L                          GPIO_23
#define                GPIO_PWM_U_L_SEL                      GPIO29_PWM5B
#define                GPIO_PWM_V_L_SEL                      GPIO21_PWM1B
#define                GPIO_PWM_W_L_SEL                      GPIO23_PWM2B


#define                PWM_FREQ                              20000                                   /* 20kHz PWM */
#define                PWM_DB_NS                             1000                                    /* 1us */

uint32_t               u3232PWMPeriod;


 /*************************************************************************************************************************
 *
 * @brief      In this case, A demo how to output trip-phase waveform of BLDC(brushless direct current) is gaven.
 *
 *************************************************************************************************************************/


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/1/2 and output 20kHz waveform on both channel A and channel B.
   */
  PWM_ComplementaryPairChannelInit(PWM_U, PWM_FREQ, PWM_DB_NS);
  PWM_ComplementaryPairChannelInit(PWM_V, PWM_FREQ, PWM_DB_NS);
  PWM_ComplementaryPairChannelInit(PWM_W, PWM_FREQ, PWM_DB_NS);

  /* Start counting */
  PWM_RunCounter(PWM_U);
  PWM_RunCounter(PWM_V);
  PWM_RunCounter(PWM_W);
  
  /* PWM5 set CMPA and loading the same value to PWM1/2 synchronously */
  PWM_LinkCMPA(PWM_V, LINK_PWM_U);
  PWM_LinkCMPA(PWM_W, LINK_PWM_U);

  /* Set the duty as 75% */
  u3232PWMPeriod = PWMPeriod(PWM_FREQ);
  PWM_SetCMPA(PWM_U, u3232PWMPeriod / 4);

  while(1)
  {
    
  }
}
