/*!
 * @file        bmp561_vadc.c
 *
 * @brief       This file provides all the VADC firmware functions
 *
 * @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.
 */

#include "bmp561_vadc.h"
#include "bmp561_sysctrl.h"

/** @addtogroup Peripherals_Library Standard Peripheral Library
  @{
*/

/** @addtogroup VADC_Driver VADC Driver
  @{
*/

/** @addtogroup VADC_Functions Functions
  @{
*/

/*!
 * @brief       Resets the VADC peripheral.
 *
 * @param       None
 *
 * @retval      None
 *
 */
void VADC_Reset(void)
{
    SYSCTRL_Unlock();
    SYSCTRL_EnableAPBPeriphReset(SYSCTRL_APB_VADC);
    SYSCTRL_DisableAPBPeriphReset(SYSCTRL_APB_VADC);
    SYSCTRL_Lock();
}


/*!
 * @brief     Enables VADC.
 *
 * @param     None
 *
 * @retval    None
 *
 */
void VADC_Enable(void)
{
    VADC->CTRL_B.EN = BIT_SET;
}

/*!
 * @brief     Disables VADC.
 *
 * @param     None
 *
 * @retval    None
 *
 */
void VADC_Disable(void)
{
    VADC->CTRL_B.EN = BIT_RESET;
}

/*!
 * @brief     Enables VADC chopper.
 *
 * @param     None
 *
 * @retval    None
 *
 */
void VADC_EnableChopper(void)
{
    VADC->CTRL_B.CHP_EN = BIT_SET;
}

/*!
 * @brief     Disables VADC chopper.
 *
 * @param     None
 *
 * @retval    None
 *
 */
void VADC_DisableChopper(void)
{
    VADC->CTRL_B.CHP_EN = BIT_RESET;
}

/*!
 * @brief     Enables VADC chopper delay.
 *
 * @param     None
 *
 * @retval    None
 *
 */
void VADC_EnableChopperDelay(void)
{
    VADC->CTRL_B.CHPDLY_SEL_C = BIT_SET;
}

/*!
 * @brief     Disables VADC chopper delay.
 *
 * @param     None
 *
 * @retval    None
 *
 */
void VADC_DisableChopperDelay(void)
{
    VADC->CTRL_B.CHPDLY_SEL_C = BIT_RESET;
}

/*!
 * @brief       Configures VADC OSR.
 *
 * @param       OSRVal: Specifies the OSR value.
 *                      This parameter can be one of the following value:
 *                      @arg VADC_OSR_128 : 128 OSR
 *                      @arg VADC_OSR_256 : 256 OSR
 *                      @arg VADC_OSR_512 : 512 OSR
 *                      @arg VADC_OSR_1024: 1024 OSR
 *
 * @retval      None
 *
 */
void VADC_ConfigOSR(VADC_OSR_T OSRVal)
{
    VADC->CTRL_B.OSR_SEL = OSRVal;
}

/*!
 * @brief     Get VADC data.
 *
 * @param     None
 *
 * @retval    the VADC data.
 *
 */
uint16_t VADC_GetData(void)
{
    return (uint16_t)VADC->DATA;
}

/*!
 * @brief     Enables VADC sample interrupt.
 *
 * @param     None
 *
 * @retval    None
 *
 */
void VADC_EnableSampleInterrupt(void)
{
    VADC->IEN_B.SPDIEN = BIT_SET;
}

/*!
 * @brief     Disables VADC sample interrupt.
 *
 * @param     None
 *
 * @retval    None
 *
 */
void VADC_DisableSampleInterrupt(void)
{
    VADC->IEN_B.SPDIEN = BIT_RESET;
}

/*!
 * @brief     Reads VADC sample flag.
 *
 * @param     None
 *
 * @retval    The VADC sample flag.
 *
 */
uint8_t VADC_ReadSampleFlag(void)
{
    return VADC->SR_B.SP_DONE;
}

/*!
 * @brief     Reads VADC Vref.
 *
 * @param     None
 *
 * @retval    The VADC Vref.
 *            This parameter can be one of the following values:
 *            @arg VADC_VOL_2P7V: Vref = 2.7V
 *            @arg VADC_VOL_1P8V: Vref = 1.8V
 *
 */
VADC_VOL_T VADC_ReadVoltage(void)
{
    return (VADC_VOL_T)VADC->SR_B.VOL_SEL;
}

/*!
 * @brief     Clears VADC sampling flag.
 *
 * @param     None
 *
 * @retval    None
 *
 */
void VADC_ClearSampleFlag(void)
{
    VADC->SCR_B.SPDC = BIT_SET;
}

/*!
 * @brief     Clears VADC sampling interrupt flag.
 *
 * @param     None
 *
 * @retval    None
 *
 */
void VADC_ClearSampleInterruptFlag(void)
{
    VADC->SCR_B.SPDC = BIT_SET;
}

/*!
 * @brief     Gets VADC raw data.
 *
 * @param     None
 *
 * @retval    The VADC raw data.
 *
 */
uint32_t VADC_GetRawData(void)
{
    return VADC->SDATA_B.DATA;
}

/**@} end of group VADC_Functions*/
/**@} end of group VADC_Driver*/
/**@} end of group Peripherals_Library*/
