STM32G474xx HAL 用户手册
stm32g4xx_hal_adc_ex.c
转到此文件的文档。
00001 /**
00002   ******************************************************************************
00003   * @file    stm32g4xx_hal_adc_ex.c
00004   * @author  MCD Application Team
00005   * @brief   This file provides firmware functions to manage the following
00006   *          functionalities of the Analog to Digital Converter (ADC)
00007   *          peripheral:
00008   *           + Peripheral Control functions
00009   *          Other functions (generic functions) are available in file
00010   *          "stm32g4xx_hal_adc.c".
00011   *
00012   ******************************************************************************
00013   * @attention
00014   *
00015   * Copyright (c) 2019 STMicroelectronics.
00016   * All rights reserved.
00017   *
00018   * This software is licensed under terms that can be found in the LICENSE file
00019   * in the root directory of this software component.
00020   * If no LICENSE file comes with this software, it is provided AS-IS.
00021   *
00022   ******************************************************************************
00023   @verbatim
00024   [..]
00025   (@) Sections "ADC peripheral features" and "How to use this driver" are
00026       available in file of generic functions "stm32g4xx_hal_adc.c".
00027   [..]
00028   @endverbatim
00029   ******************************************************************************
00030   */
00031 
00032 /* Includes ------------------------------------------------------------------*/
00033 #include "stm32g4xx_hal.h"
00034 
00035 /** @addtogroup STM32G4xx_HAL_Driver
00036   * @{
00037   */
00038 
00039 /** @defgroup ADCEx ADCEx
00040   * @brief ADC Extended HAL module driver
00041   * @{
00042   */
00043 
00044 #ifdef HAL_ADC_MODULE_ENABLED
00045 
00046 /* Private typedef -----------------------------------------------------------*/
00047 /* Private define ------------------------------------------------------------*/
00048 
00049 /** @defgroup ADCEx_Private_Constants ADC Extended Private Constants
00050   * @{
00051   */
00052 
00053 #define ADC_JSQR_FIELDS      ((ADC_JSQR_JL | ADC_JSQR_JEXTSEL | ADC_JSQR_JEXTEN |\
00054                                ADC_JSQR_JSQ1  | ADC_JSQR_JSQ2 |\
00055                                ADC_JSQR_JSQ3 | ADC_JSQR_JSQ4 ))           /*!< ADC_JSQR fields of parameters that can
00056                              be updated anytime once the ADC is enabled */
00057 
00058 /* Fixed timeout value for ADC calibration.                                   */
00059 /* Values defined to be higher than worst cases: low clock frequency,         */
00060 /* maximum prescalers.                                                        */
00061 /* Ex of profile low frequency : f_ADC at f_CPU/3968 (minimum value           */
00062 /* considering both possible ADC clocking scheme:                             */
00063 /*        - ADC clock from synchronous clock with AHB prescaler 512,          */
00064 /*          ADC prescaler 4.                                                  */
00065 /*           Ratio max = 512 *4 = 2048                                        */
00066 /*        - ADC clock from asynchronous clock (PLLP) with prescaler 256.      */
00067 /*          Highest CPU clock PLL (PLLR).                                     */
00068 /*           Ratio max = PLLRmax /PPLPmin * 256 = (VCO/2) / (VCO/31) * 256    */
00069 /*                     = 3968 )                                               */
00070 /* Calibration_time MAX = 81 / f_ADC                                          */
00071 /*                      = 81 / (f_CPU/3938) = 318978 CPU cycles               */
00072 #define ADC_CALIBRATION_TIMEOUT         (318978UL)   /*!< ADC calibration time-out value (unit: CPU cycles) */
00073 
00074 /**
00075   * @}
00076   */
00077 
00078 /* Private macro -------------------------------------------------------------*/
00079 /* Private variables ---------------------------------------------------------*/
00080 /* Private function prototypes -----------------------------------------------*/
00081 /* Exported functions --------------------------------------------------------*/
00082 
00083 /** @defgroup ADCEx_Exported_Functions ADC Extended Exported Functions
00084   * @{
00085   */
00086 
00087 /** @defgroup ADCEx_Exported_Functions_Group1 Extended Input and Output operation functions
00088   * @brief    Extended IO operation functions
00089   *
00090 @verbatim
00091  ===============================================================================
00092                       ##### IO operation functions #####
00093  ===============================================================================
00094     [..]  This section provides functions allowing to:
00095 
00096       (+) Perform the ADC self-calibration for single or differential ending.
00097       (+) Get calibration factors for single or differential ending.
00098       (+) Set calibration factors for single or differential ending.
00099 
00100       (+) Start conversion of ADC group injected.
00101       (+) Stop conversion of ADC group injected.
00102       (+) Poll for conversion complete on ADC group injected.
00103       (+) Get result of ADC group injected channel conversion.
00104       (+) Start conversion of ADC group injected and enable interruptions.
00105       (+) Stop conversion of ADC group injected and disable interruptions.
00106 
00107       (+) When multimode feature is available, start multimode and enable DMA transfer.
00108       (+) Stop multimode and disable ADC DMA transfer.
00109       (+) Get result of multimode conversion.
00110 
00111 @endverbatim
00112   * @{
00113   */
00114 
00115 /**
00116   * @brief  Perform an ADC automatic self-calibration
00117   *         Calibration prerequisite: ADC must be disabled (execute this
00118   *         function before HAL_ADC_Start() or after HAL_ADC_Stop() ).
00119   * @param  hadc       ADC handle
00120   * @param  SingleDiff Selection of single-ended or differential input
00121   *         This parameter can be one of the following values:
00122   *           @arg @ref ADC_SINGLE_ENDED       Channel in mode input single ended
00123   *           @arg @ref ADC_DIFFERENTIAL_ENDED Channel in mode input differential ended
00124   * @retval HAL status
00125   */
00126 HAL_StatusTypeDef HAL_ADCEx_Calibration_Start(ADC_HandleTypeDef *hadc, uint32_t SingleDiff)
00127 {
00128   HAL_StatusTypeDef tmp_hal_status;
00129   __IO uint32_t wait_loop_index = 0UL;
00130 
00131   /* Check the parameters */
00132   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
00133   assert_param(IS_ADC_SINGLE_DIFFERENTIAL(SingleDiff));
00134 
00135   /* Process locked */
00136   __HAL_LOCK(hadc);
00137 
00138   /* Calibration prerequisite: ADC must be disabled. */
00139 
00140   /* Disable the ADC (if not already disabled) */
00141   tmp_hal_status = ADC_Disable(hadc);
00142 
00143   /* Check if ADC is effectively disabled */
00144   if (tmp_hal_status == HAL_OK)
00145   {
00146     /* Set ADC state */
00147     ADC_STATE_CLR_SET(hadc->State,
00148                       HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
00149                       HAL_ADC_STATE_BUSY