/*!
 * @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 "board_delay.h"
#include <stdio.h>
#include "bmp561_sysctrl.h"
#include "bmp561_hsc.h"
#include "bmp561_gpio.h"

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

/* Private macro **********************************************************/
#define CMD_READ    0 << 7
#define CMD_WRITE   1 << 7

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

/* Private variables ******************************************************/

static uint8_t sendData = 0; 
static uint8_t receiveData = 0;

/* Private function prototypes ********************************************/
void HSC_GPIO_Init(void);
void HSC_Init(void);

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

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

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

    HSC_ClearFlag(HSC_FLAG_DATA);

    while (1)
    {
        /* Send Data*/
        HSC_ConfigAddress(0xB | CMD_READ);
        receiveData = HSC_ReadData();
        while (HSC_ReadFlag(HSC_FLAG_DATA) != RESET);
        HSC_ClearFlag(HSC_FLAG_DATA);

        /* Wait data received */
        HSC_ConfigAddress(0xB | CMD_WRITE);
        HSC_WriteData(sendData++);
        while (HSC_ReadFlag(HSC_FLAG_DATA) != RESET);
        HSC_ClearFlag(HSC_FLAG_DATA);
        (void)receiveData;
    }
}

/*!
 * @brief   HSC Init
 *
 * @param   None
 *
 * @retval  None
 */
void HSC_Init(void)
{
    SYSCTRL_Unlock();
    SYSCTRL_EnableAPBPeriphBCGClock(SYSCTRL_APB_HSC);
    SYSCTRL_EnableAPBPeriphWCGClock(SYSCTRL_APB_HSC);
    SYSCTRL_Lock();

    HSC_GPIO_Init();

    HSC_Config_T hscConfig;
    hscConfig.addr = 0xB;
    hscConfig.bits = HSC_PROTOCOL_8BIT;
    hscConfig.brkEn = DISABLE;
    hscConfig.clock = HSC_SYSCLK_1MHZ;
    hscConfig.enable = ENABLE;
    hscConfig.mode = HSC_MODE_SLAVE;
    hscConfig.threshold = HSC_THRETIME_0P5US;
    hscConfig.timeout = ENABLE;

    HSC_Config(&hscConfig);
}

/*!
 * @brief   HSC GPIO Init
 *
 * @param   None
 *
 * @retval  None
 */
void HSC_GPIO_Init(void)
{
    SYSCTRL_Unlock();
    SYSCTRL_EnableAHBPeriphClock(SYSCTRL_AHB_GPIO);
    SYSCTRL_Lock();
    
    GPIO_Config_T gpioConfig;
    gpioConfig.drive = GPIO_DS_HIGH;
    gpioConfig.mode = GPIO_MODE_AF;
    gpioConfig.pin = GPIO_PIN_3;
    gpioConfig.polarity = GPIO_POL_UP;
    gpioConfig.pullEn = ENABLE;
    gpioConfig.speed = GPIO_OS_HIGH;
    gpioConfig.type = GPIO_OT_PP;

    GPIO_Config(&gpioConfig);

    GPIO_ConfigPinReuse(GPIO_PIN_3, GPIO_REUSE_AF1);
}
