/*!
 * @file        main.c
 *
 * @brief       Main program body
 *
 * @version     V1.0.0
 *
 * @date        2025-01-01
 *
 * @attention
 *
 *  Copyright (C) 2025 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"
#include <stdio.h>
#include "bmp561_sysctrl.h"
#include "bmp561_tmr.h"
#include "bmp561_gpio.h"

/* Private includes *******************************************************/

/* Private macro **********************************************************/

/* Private typedef ********************************************************/

/* Private variables ******************************************************/
volatile uint8_t state = 0;

/* Private function prototypes ********************************************/
void TMR_Init(void);
void GPIO_Init(void);

/* External variables *****************************************************/

/* External functions *****************************************************/

/*!
 * @brief   Main program
 *
 * @param   None
 *
 * @retval  None
 */
int main(void)
{
    GPIO_Init();
    TMR_Init();

    while (1)
    {
        if (TMR_ReadIntFlag(TMR0, TMR_INT_OV))
        {
            TMR_ClearIntFlag(TMR0, TMR_INT_OV);
            /* Toggle GPIO pin 0 */
            state = GPIO_ReadBit(GPIO_PIN_3);
            if (state == 1)
            {
                GPIO_ResetBit(GPIO_PIN_3);
            }
            else
            {
                GPIO_SetBit(GPIO_PIN_3);
            }
        }
    }
}

/*!
 * @brief   TMR initialization
 *
 * @param   None
 *
 * @retval  None
 */
void TMR_Init(void)
{
    SYSCTRL_Unlock();
    SYSCTRL_EnableAPBPeriphBCGClock(SYSCTRL_APB_TMR0);
    SYSCTRL_EnableAPBPeriphWCGClock(SYSCTRL_APB_TMR0);
    SYSCTRL_Lock();

    TMR_ConfigMode(TMR0, TMR_MODE_TMR);

    /* TMR frequency = 4MHz / 4 = 1MHz */
    TMR_ConfigClockDiv(TMR0, TMR_DIV_4);
    TMR_SetCounter(TMR0, 1000);
    TMR_Enable(TMR0);
}

/*!
 * @brief   GPIO initialization
 *
 * @param   None
 *
 * @retval  None
 */
void GPIO_Init(void)
{
    GPIO_Config_T gpioConfig;

    SYSCTRL_Unlock();
    SYSCTRL_EnableAHBPeriphClock(SYSCTRL_AHB_GPIO);
    SYSCTRL_Lock();

    GPIO_ConfigStructInit(&gpioConfig);
    gpioConfig.pin = GPIO_PIN_3;
    gpioConfig.mode = GPIO_MODE_OUT;
    gpioConfig.speed = GPIO_OS_HIGH;
    gpioConfig.type = GPIO_OT_PP;
    gpioConfig.pullEn = DISABLE;
    gpioConfig.polarity = GPIO_POL_UP;
    gpioConfig.drive = GPIO_DS_HIGH;
    GPIO_Config(&gpioConfig);
}
