/******************************************************************************
 * @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>

/*Used for judging if INT had happended*/
int               iFlag                   = 0;
uint32_t          u32FeedDogTime          = 0;
uint32_t          u32WDTFreq              =  0;
uint32_t          u32count                = 100;

#define           TimeMs                  500                             /*500ms*/


 /*************************************************************************************************************************
 *
 * @brief      In this case, we start to feed dog every 30ms, so that WDT can not trigger a INT to CUP.
 *
 *************************************************************************************************************************/


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 WDT1 and set count time to 500ms */
  WDT_Init(WDT1, TimeMs);
  
  /* Enable the NVIC INT of WDT1 */
  NVIC_EnableIRQ(WDT1_IRQn);
  
  /* Enable the counter and the interruput */
  WDT_EnableInt(WDT1);
  
  while(u32count--)
  {
      /* Wait for 60ms, less then TimeMs, to privent WDT1 process INT to CPU */
      Delay_Ms(30);
    
      /* Feed dog */
      WDT_FeedDog(WDT1);
    
      /* Disable INT at the last count */
      if(u32count == 1)
        WDT_DisableInt(WDT1);
  }
  
  if(iFlag)
    printf("WDT Feed DOG test FAIL\n");
  else
    printf("WDT Feed DOG test PASS\n");
  
  while(1)
  {
    
  }
}

void WDT1_IRQHandler()
{
  iFlag++;

  /* Clear INT, So that SYS would restart */
  WDT_ClearInt(WDT1);

  /* Set WDT1 Stop count and disable the INT */
  WDT_DisableInt(WDT1);
}
