/******************************************************************************
 * @file     main.c
 * @brief    Main program body
 * @version  V4.0.3
 * @date     30-July-2026
 *
 * @note
 * Copyright (C) 2022 Spintrol Electronic Technology (Shanghai) Co., Ltd.. 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<stdio.h>

#if defined(SPC1158)
#include "spc1158.h"
#elif defined(SPD1148)
#include "spd1148.h"
#elif defined(SPD1178)
#include "spd1178.h"
#elif defined(SPD1188)
#include "spd1188.h"
#elif defined(SPD1163)
#include "spd1163.h"
#else
#include "spc1168.h"
#endif

/*Used for judging if INT had happened*/
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 MCU.
*
*************************************************************************************************************************/


int main()
{
    FLASH_WALLOW();

#if defined(SPC1158)
    FLASH_SetTiming(100000000);
    /* Disable flash write access after flash operation had done */
    FLASH_WDIS();

    CLOCK_InitWithRCO(100000000UL);
#else
    FLASH_SetTiming(200000000);
    /* Disable flash write access after flash operation had done */
    FLASH_WDIS();

    CLOCK_InitWithRCO(200000000UL);
#endif

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

    /* Enable the NVIC INT of WDT1 */
    NVIC_EnableIRQ(WDT1_IRQn);
    
    /* Init WDT1 and set count time to 500ms */
    WDT_Init(WDT1, TimeMs);

    /* Enable the counter and the interrupt */
    WDT_EnableInt(WDT1);

    while (u32count--)
    {
        /* Wait for 60ms, less then TimeMs, to prevent WDT1 process INT to MCU */
        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);
}
