/*!
 * @file        main.c
 *
 * @brief       Main program body
 *
 * @version     V1.0.0
 *
 * @date        2026-02-25
 *
 * @attention
 *
 *  Copyright (C) 2026 Geehy Semiconductor
 *
 *  You may not use this file except in compliance with the
 *  GEEHY COPYRIGHT NOTICE (Geehy Semiconductor Software License Agreement).
 *
 *  The program is only for reference, which is distributed in the hope
 *  that it will be useful and instructional for customers to develop
 *  their software. Unless required by applicable law or agreed to in
 *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
 *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the Geehy Semiconductor Software License Agreement for the governing permissions
 *  and limitations under the License.
 */

/* Includes */
#include "Board.h"
#include "stdio.h"
#include "g32a10xx_gpio.h"
#include "g32a10xx_misc.h"
#include "g32a10xx_eint.h"
#include "g32a10xx_rcm.h"
#include "g32a10xx_int.h"
/** @addtogroup Examples
  @{
*/

/** @addtogroup SysTick_Examples
  @{
*/

/** @addtogroup SysTick
  @{
  */

/** @defgroup SysTick_Variables Variables
  @{
*/

/* System tick */
volatile uint32_t sysTick = 0;

/**@} end of group SysTick_Variables */

/** @defgroup SysTick_Functions Functions
  @{
  */

/**
 * @brief  Initializes the SysTick timer with a specified tick count.
 * @param  Ticks: Number of clock cycles between two consecutive interrupts.
 *                For example, if the system clock is 64MHz, setting this to 64,000 will generate a 1ms interrupt period.
 *                Note: This is a 24-bit value (max 0xFFFFFF).
 * @retval initSts: 0: SysTick Init config success
 *                  1: SysTick Init config fail
 * @note   This function configures the SysTick using the standard CMSIS 
 *         SysTick_Config() function. It also explicitly clears the NVIC 
 *         pending bit to ensure the timer starts in a clean state, preventing
 *         any premature interrupts caused by residual flags or the 
 *         configuration sequence itself.
 */
static uint32_t SysTick_Init(uint32_t Ticks)
{
    uint32_t initSts = 0;
    /* Configure the SysTick timer using the standard CMSIS function */
    if (SysTick_Config(Ticks) == 0UL)
    {
        initSts = 0;
        /*
        * Immediately clear any pending interrupt flag that might have been
        * generated during the configuration process or left over from previous state.
        * This ensures the first interrupt occurs strictly after the specified period.
        */
        NVIC_ClearPendingIRQ(SysTick_IRQn);
    }
    else
    {
        initSts = 1;
    }
    return initSts;
}

/*!
 * @brief       Disables the SysTick timer and clears its interrupt.
 *
 * @param       None.
 *
 * @retval      None.
 *
 * @note        Stops the counter, disables the interrupt, and clears any 
 *              remaining pending state in the NVIC.
 */
static void SysTick_Stop(void)
{
    /* Disable counter and interrupt */
    SysTick->CTRL &= ~(SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk);
    
    /* Clear residual pending interrupt */
    NVIC_ClearPendingIRQ(SysTick_IRQn);
}

/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
int main(void)
{
    uint32_t status = 0;
    uint32_t stopflag = 0;
    
    Board_LedInit(LED2);
    Board_LedInit(LED3);

    Board_LedOff(LED2);
    Board_LedOn(LED3);

    /* Configure the SysTick to generate a time base equal to 1 ms */
    status = SysTick_Init(SystemCoreClock / 1000U);
    
    if (status == 1U)
    {
        while (1)
        {
            /* nothing */
        }
    }
    else
    {
        while (1)
        {
            if (sysTick >= 1000U)
            {
                sysTick = 0;
                stopflag++;
                Board_LedToggle(LED2);
                Board_LedToggle(LED3);
            }
            else
            {
                /* nothing */
            }
            if(stopflag == 10U)
            {
                SysTick_Stop();
                stopflag = 0;
            }
            else
            {
                /* nothing */
            }
        }
    }
}

/**@} end of group SysTick_Functions */
/**@} end of group SysTick */
/**@} end of group SysTick_Examples */
/**@} end of group Examples */
