/*!
 * @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_i2c.h"
#include "bmp561_gpio.h"
#include "bmp561_misc.h"

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

/* Private macro **********************************************************/
#define SLAVE_ADDR 0x20

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

/* Private variables ******************************************************/
volatile uint8_t receiveData[256] = {0};
volatile uint8_t offset = 0;

/* Private function prototypes ********************************************/
void I2C_GPIO_Init(void);
void I2C_Init(void);
void I2C_IRQHandler(void);

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

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

/*!
 * @brief   Main program
 *
 * @param   None
 *
 * @retval  None
 */
int main(void)
{
    /* Board Init */
    BOARD_Delay_Config();

    /* I2C Init*/
    I2C_Init();

    while (1)
    {
        if (I2C_ReadFlag(I2C_FLAG_ADR) != RESET)
        {
            I2C_ClearFlag(I2C_FLAG_ADR);
        }

        if (I2C_ReadFlag(I2C_FLAG_RXFE) == RESET)
        {
            receiveData[offset++] = I2C_ReadData();
        }

        if (I2C_ReadFlag(I2C_FLAG_STOP) != RESET)
        {
            I2C_ClearFlag(I2C_FLAG_STOP);
        }
    }
}

/*!
 * @brief   I2C Init
 *
 * @param   None
 *
 * @retval  None
 */
void I2C_Init(void)
{
    /* Enable Clock */
    SYSCTRL_Unlock();
    SYSCTRL_EnableAPBPeriphBCGClock(SYSCTRL_APB_I2C);
    SYSCTRL_EnableAPBPeriphWCGClock(SYSCTRL_APB_I2C);
    SYSCTRL_Lock();

    I2C_GPIO_Init();

    I2C_ConfigFilter(I2C_FILTER_1PCLK);
    I2C_ConfigClockPeriod(16, 16);
    I2C_ConfigSlaveAddr(SLAVE_ADDR);
    I2C_SendACK();
    I2C_Enable();
}

/*!
 * @brief   I2C GPIO Init
 *
 * @param   None
 *
 * @retval  None
 */
void I2C_GPIO_Init(void)
{
    /* Enable Clock */
    SYSCTRL_Unlock();
    SYSCTRL_EnableAHBPeriphClock(SYSCTRL_AHB_GPIO);
    SYSCTRL_Lock();

    /* Config GPIO */
    GPIO_Config_T gpioConfig;
    gpioConfig.drive = GPIO_DS_HIGH;
    gpioConfig.mode = GPIO_MODE_AF;
    gpioConfig.pin = GPIO_PIN_2;
    gpioConfig.polarity = GPIO_POL_UP;
    gpioConfig.pullEn = ENABLE;
    gpioConfig.speed = GPIO_OS_HIGH;
    gpioConfig.type = GPIO_OT_OD;
    GPIO_Config(&gpioConfig);

    gpioConfig.pin = GPIO_PIN_3;
    GPIO_Config(&gpioConfig);
    GPIO_ConfigPinReuse(GPIO_PIN_2, GPIO_REUSE_AF0);
    GPIO_ConfigPinReuse(GPIO_PIN_2, GPIO_REUSE_AF0);
}
