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

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

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

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

/* Private variables ******************************************************/
volatile uint32_t voltage = 0;

/* Private function prototypes ********************************************/
void VADC_Init(void);

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

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

/*!
 * @brief   Main program
 *
 * @param   None
 *
 * @retval  None
 */
int main(void)
{
    BOARD_Delay_Config();
    GPIO_Reset();
    BOARD_Delay_Ms(3000);

    VADC_Init();

    while (1)
    {
        if (VADC_ReadSampleFlag())
        {
            voltage = VADC_GetRawData();
            VADC_ClearSampleFlag();
        }
    }
}

/*!
 * @brief   VADC initialization
 *
 * @param   None
 *
 * @retval  None
 */
void VADC_Init(void)
{
    SYSCTRL_Unlock();
    SYSCTRL_EnableAPBPeriphBCGClock(SYSCTRL_APB_VADC);
    SYSCTRL_EnableAPBPeriphWCGClock(SYSCTRL_APB_VADC);
    SYSCTRL_EnableAFEClock();
    SYSCTRL_EnableAFE();
    SYSCTRL_ConfigVADCMUX(SYSCTRL_ANA_VADCMUX_BAT_5P0);
    SYSCTRL_Lock();

    VADC_ConfigOSR(VADC_OSR_512);
    VADC_Enable();
}
