/*!
 * @file        main.c
 *
 * @brief       Main program body
 *
 * @version     V1.0.2
 *
 * @date        2023-03-31
 *
 * @attention
 *
 *  Copyright (C) 2023 Geehy Semiconductor
 *
 *  You may not use this file except in compliance with the
 *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
 *
 *  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 SOFTWARE PACKAGE LICENSE for the governing permissions
 *  and limitations under the License.
 */

/* Includes */
#include "main.h"

/** @addtogroup Examples
  @{
*/

/** @addtogroup TMR4_TimeBase
  @{
*/

/** @defgroup TMR4_TimeBase_Variables Variables
  @{
*/

/* Timer tick */
volatile uint32_t tick = 0;

/**@} end of group TMR4_TimeBase_Variables */

/** @defgroup TMR4_TimeBase_Functions Functions
  @{
*/

/* TMR4 Init */
void TMR4_Init(void);

/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 */
int main(void)
{
    /* Master clock frequency = 48 / 8 = 6MHZ */
    RCM_ConfigHIRCDiv(RCM_HIRC_DIV_8);

    APM_MINI_LEDInit(LED2);
    APM_MINI_LEDInit(LED3);
    TMR4_Init();

    while(1)
    {
        if(tick >= 100)
        {
            tick = 0;

            APM_MINI_LEDToggle(LED2);
            APM_MINI_LEDToggle(LED3);
        }
    }
}

/*!
 * @brief       TMR4 Init
 *
 * @param       None
 *
 * @retval      None
 */
void TMR4_Init(void)
{
    /*  Divider = 7, counter = 0xff  */
    TMR4_ConfigTimerBase(7, 0XFF);
    /*  Enable update interrupt  */
    TMR4_EnableInterrupt(TMR4_INT_UPDATE);
    /*  Enable TMR4  */
    TMR4_Enable();

    NVIC_EnableIRQRequest(TMR4_IRQn, 0X01);
}

/*!
 * @brief       TMR4 interrupt service runtine
 *
 * @param       None
 *
 * @retval      None
 */
void TMR4_Isr(void)
{
    if(TMR4_ReadIntFlag(TMR4_INT_UPDATE) == SET)
    {
        TMR4_ClearIntFlag(TMR4_INT_UPDATE);

        tick++;
    }
}

/**@} end of group TMR4_TimeBase_Functions */
/**@} end of group TMR4_TimeBase */
/**@} end of group Examples */
