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

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

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

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

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

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

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

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

/*!
 * @brief   Main program
 *
 * @param   None
 *
 * @retval  None
 */
int main(void)
{
    /* After 3 seconds delay, SWD pin will be occupied */
    BOARD_Delay_Config();
    GPIO_Reset();
    BOARD_Delay_Ms(3000);

    GPIO_Init();
    UART_Init();

    while (1)
    {
        if (UART_ReadFlag(UART_FLAG_RXC))
        {
            UART_ClearFlag(UART_CLEAR_RXC);
            rxData = UART_RecieveData();

            while (UART_ReadFlag(UART_FLAG_TXBSY));
            UART_SendData(rxData);
        }
    }
}

/*!
 * @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.mode = GPIO_MODE_AF;
    gpioConfig.speed = GPIO_OS_HIGH;
    gpioConfig.type = GPIO_OT_PP;
    gpioConfig.pullEn = DISABLE;
    gpioConfig.polarity = GPIO_POL_UP;
    gpioConfig.drive = GPIO_DS_HIGH;

    gpioConfig.pin = GPIO_PIN_0;
    GPIO_Config(&gpioConfig);

    gpioConfig.pin = GPIO_PIN_1;
    GPIO_Config(&gpioConfig);

    GPIO_ConfigPinReuse(GPIO_PIN_0, GPIO_REUSE_AF3);
    GPIO_ConfigPinReuse(GPIO_PIN_1, GPIO_REUSE_AF3);
}

/*!
 * @brief   UART initialization
 *
 * @param   None
 *
 * @retval  None
 */
void UART_Init(void)
{
    UART_Config_T uartConfig;

    SYSCTRL_Unlock();
    SYSCTRL_EnableAPBPeriphBCGClock(SYSCTRL_APB_UART);
    SYSCTRL_EnableAPBPeriphWCGClock(SYSCTRL_APB_UART);
    SYSCTRL_Lock();

    UART_ConfigStructInit(&uartConfig);

    uartConfig.baudrate = 0x23;
    uartConfig.stopbit = UART_STOPBIT_1B;
    uartConfig.parity = UART_PARITY_EVEN;
    uartConfig.parityen = DISABLE;
    uartConfig.sequence = UART_TXSEQ_LSB;
    uartConfig.txen = ENABLE;
    uartConfig.rxen = ENABLE;
    UART_Config(&uartConfig);
}
