/*!
 * @file        bmp561_cadc.c
 *
 * @brief       This file provides all the CADC 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_cadc.h"
#include "bmp561_sysctrl.h"

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

/** @addtogroup CADC_Driver CADC Driver
  @{
*/

/** @addtogroup CADC_Functions Functions
  @{
*/

/*!
 * @brief       Resets the CADC peripheral.
 *
 * @param       None
 *
 * @retval      None
 *
 */
void CADC_Reset(void)
{
    SYSCTRL_Unlock();
    SYSCTRL_EnableAPBPeriphReset(SYSCTRL_APB_CADC);
    SYSCTRL_DisableAPBPeriphReset(SYSCTRL_APB_CADC);
    SYSCTRL_Lock();
}

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

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

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

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

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

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

/*!
 * @brief       Configures CADC OSR.
 *
 * @param       OSRVal: Specifies the OSR value.
 *                      This parameter can be one of the following value:
 *                      @arg CADC_OSR_128 : 128 OSR
 *                      @arg CADC_OSR_256 : 256 OSR
 *                      @arg CADC_OSR_512 : 512 OSR
 *                      @arg CADC_OSR_1024: 1024 OSR
 *
 * @retval      None
 *
 */
void CADC_ConfigOSR(CADC_OSR_T OSRVal)
{
    CADC->CTRL_B.OSR_SEL = OSRVal;
}

/*!
 * @brief     Enables CADC boost mode.
 *
 * @param     None
 *
 * @retval    None
 *
 */
void CADC_EnableBoostMode(void)
{
    CADC->CTRL_B.CADC_CLK_BOOST = BIT_SET;
}

/*!
 * @brief     Disables CADC boost mode.
 *
 * @param     None
 *
 * @retval    None
 *
 */
void CADC_DisableBoostMode(void)
{
    CADC->CTRL_B.CADC_CLK_BOOST = BIT_RESET;
}

/*!
 * @brief     Get CADC data.
 *
 * @param     None
 *
 * @retval    The CADC data.
 *
 */
uint16_t CADC_GetData(void)
{
    return (uint16_t)CADC->DATA;
}

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

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

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

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

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

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

/**@} end of group CADC_Functions*/
/**@} end of group CADC_Driver*/
/**@} end of group Peripherals_Library*/
