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

#define ExClkSrcGPIO             GPIO_11
#define TimerPeriodCount         10              /* 10MS */
#define ExternalClockSource      3               /* 0:RCO0  1:RCO1  2:XO  3:PLL */


/*************************************************************************************************************************
*
* @brief This case shows how to use a external clock as the GTimer clock, and GTimer works as a counter.
*
* @note
*        DCLK (GPIO 11) works as GTimer external clock.
*
*************************************************************************************************************************/

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

    /* Select external Clock Source */
    if (ExternalClockSource == 0)
    {
        CLOCK->CLKDETCTL.bit.DCLKSEL = CLKDETCTL_BIT_DCLKSEL_RCO0;
    }
    else if (ExternalClockSource == 1)
    {
        CLOCK->CLKDETCTL.bit.DCLKSEL = CLKDETCTL_BIT_DCLKSEL_RCO1;
    }
    else if (ExternalClockSource == 2)
    {
        CLOCK->CLKDETCTL.bit.DCLKSEL = CLKDETCTL_BIT_DCLKSEL_XO;
    }
    else
    {
        CLOCK->CLKDETCTL.bit.DCLKSEL = CLKDETCTL_BIT_DCLKSEL_PLL;
    }

    CLOCK->CLKDETCTL.bit.DCLKDIV = CLKDETCTL_BIT_RCLKDIV_32;

    CLOCK->CLKDETCTL.bit.EN = CLKDETCTL_BIT_EN_ENABLE;

    /* Set GPIO 11 DCLK function */
    GPIO_SetPinChannel(ExClkSrcGPIO, GPIO11_CLK_DCLK);
    GPIO_SetPinDir(ExClkSrcGPIO, GPIO_INPUT);

    /* Open Global INT for TIMER1 */
    NVIC_EnableIRQ(TIMER1_IRQn);
    
    /* Init TIMWE1 */
    TIMER_Init(TIMER1, TimerPeriodCount);

    /* Set external input signal mode */
    TIMER1->TMRCTL.bit.EXTINMODE = TMRCTL_BIT_EXTINMODE_EXTCLK;

    /* Select external input GPIO */
    TIMER1->TMRCTL.bit.EXTSEL = ExClkSrcGPIO;

    while (1)
    {

    }
}

void TIMER1_IRQHandler()
{
    /* Clear the INT */
    TIMER_ClearInt(TIMER1);

    printf("G timer extern gpio clk test OK\n");
}
