/******************************************************************************
 * @file     main.c
 * @brief    Main program body
 * @version  V4.0.1
 * @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



/*************************************************************************************************************************
*
* @brief      In this case, GPIO8 output a rising edge, GPIO9 connecting with GPIO8 will detect this rising edge,
*             corresponding information will ba print when ever rising edge happened or not.
*
*************************************************************************************************************************/


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

    /* Set GPIO8/GPIO9 as GPIO FUNC */
    GPIO_SetPinAsGPIO(GPIO_8);
    GPIO_SetPinAsGPIO(GPIO_9);

    /* Set GPIO8/GPIO9 direction */
    GPIO_SetPinDir(GPIO_8, GPIO_OUTPUT);
    GPIO_SetPinDir(GPIO_9, GPIO_INPUT);

    /*
     * De-glitch settings, if IO signal state can keep 128 PCLK cycles,
     * then it will will be assumed valid
     */
    GPIO_SetDeglitchWindow(0x7);
    GPIO_EnableDeglitch(GPIO_9);


    /* Enable GPIO edge trigger INT in MCU side */
    NVIC_EnableIRQ(GPIO_EDGE_IRQn);
    
    /* GPIO INT configuration: rising edge will trigger INT */
    GPIO_SetEdgeIntMode(GPIO_9, GPIO_INT_RISING_EDGE);
    GPIO_EnableEdgeInt(GPIO_9);

    /* Generate a rising edge */
    GPIO_SetLow(GPIO_8);
    Delay_Ms(100);
    GPIO_TogglePin(GPIO_8);

    while (1)
    {

    }
}

void GPIO_EDGE_IRQHandler()
{
    int iResult = -1;

    /* Checking if Edge had detected */
    iResult = GPIO_GetEdgeIntStatus(GPIO_9);
    if (iResult == 1)
    {
        printf("Rising edge had detected\n");
    }
    else
    {
        printf("No rising edge, test failed\n");
    }

    /* Clear GPIO INT */
    GPIO_ClearEdgeInt(GPIO_9);

    GPIO_ClearGlobalEdgeInt();
}
