/**
  ******************************************************************************
  * @file    ys32md211_vrefbuf.c
  * @author  YSPRING Application Team
  * @version V1.0.1
  * @date    2025.02.15
  * @brief   This file provides firmware functions to manage the following 
  *          functionalities of the VREFBUF peripheral:
  *           + ADC reference voltage slection management
  *           + Peripherals Configuration
  * 
  *  @verbatim
  *
    ===========================================================================
                         ##### How to use this driver #####
    ===========================================================================
      (#) Enable the SYSCFG APB2 clock using RCC_APB2PeriphClockCmd()
      (#) Configure the ADC reference voltage using VREFBUF_ADC_Config()
      (#) Voltage reference buffer mode enable using VREFBUF_Cmd() function
      (#) Use VREFBUF_Trim() to write CCR.
    @endverbatim
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "ys32md211_vrefbuf.h"

/** @addtogroup YS32MD211_StdPeriph_Driver
  * @{
  */

/** @defgroup VREFBUF 
  * @brief VREFBUF driver modules
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/** @defgroup VREFBUF_Private_Functions
  * @{
  */

/** @defgroup VREFBUF_Group1 Peripherals Configuration functions
 *  @brief   Peripherals Configuration
 *
@verbatim
  ==============================================================================
               ##### Peripherals Configuration functions #####
  ==============================================================================

@endverbatim
  * @{
  */
	
/**
  * @brief  ENABLE or DISABLE the VREFBUF peripheral.
  * @param  NewState: new state of VREFBUF peripheral. 
  *          This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void VREFBUF_Cmd(FunctionalState NewState)
{	
  /* Check the parameters */
  assert_param(IS_FUNCTIONAL_STATE(NewState));

  if (NewState == ENABLE)
  {
    /* Set the ENABLE bit */
    VREFBUF->CSR |= VREFBUF_CSR_ADC1_ENVR;
  }
  else
  {
     /* Reset the ENABLE bit */   
    VREFBUF->CSR &= ~VREFBUF_CSR_ADC1_ENVR;
  }
}

/**
  * @brief  VREFBUF output voltage selection
  * @param  BufvConfig: This parameter can be:
  *            @arg VREFBUF_BUFV_1d2: VREFBUF output 1.2V(default value)
  *            @arg VREFBUF_BUFV_2d0: VREFBUF output 2.0V(VCC >= 2.5V)
  *            @arg VREFBUF_BUFV_2d5: VREFBUF output 2.5V(VCC >= 3V)
  * @retval None
  */
void VREFBUF_Output_Config(uint32_t BufvConfig)
{
  /* Check the parameters */  
  assert_param(IS_VREFBUF_BUFV_CONFIG(BufvConfig));

  VREFBUF->CSR &= ~VREFBUF_CSR_BUFV_SEL;
  VREFBUF->CSR |= BufvConfig;
}

/**
  * @brief  ADC reference voltage selection
  * @param  VrefConfig: This parameter can be:
  *            @arg VREFBUF_ADC_VREF: ADC voltage come from VREF pin
  *            @arg VREFBUF_ADC_VCC: ADC voltage come from VCC
  *            @arg VREFBUF_ADC_VREFBUF: ADC voltage come from VREFBUF
  * @retval None
  */
void VREFBUF_ADC_Config(uint32_t VrefConfig)
{
  /* Check the parameters */  
  assert_param(IS_VREFBUF_ADC_CONFIG(VrefConfig));

  VREFBUF->CSR &= ~VREFBUF_CSR_ADC1_VREF_SEL;
  VREFBUF->CSR |= VrefConfig;
}

/**
  * @brief  Calibration value
  * @param  TrimValue: VREFBUF trimming value 
  *         This parameter can be: from 0x00 to 0x07
  * @retval None
  */
void VREFBUF_Trim(uint32_t TrimValue)
{
  /* Check the parameters */  
  assert_param(IS_VREFBUF_TRIM_VALUE(TrimValue));

  VREFBUF->CCR = TrimValue;
}

/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */
