2 ******************************************************************************
3 * @file stm32f0xx_hal_adc_ex.c
4 * @author MCD Application Team
6 * @date 11-December-2014
7 * @brief This file provides firmware functions to manage the following
8 * functionalities of the Analog to Digital Convertor (ADC)
10 * + Operation functions
11 * ++ Calibration (ADC automatic self-calibration)
14 ==============================================================================
15 ##### ADC specific features #####
16 ==============================================================================
18 (#) 12-bit, 10-bit, 8-bit or 6-bit configurable resolution
20 (#) Interrupt generation at the end of regular conversion and in case of
21 analog watchdog or overrun events.
23 (#) Single and continuous conversion modes.
25 (#) Scan mode for automatic conversion of channel 0 to channel 'n'.
27 (#) Data alignment with in-built data coherency.
29 (#) Programmable sampling time.
31 (#) ADC conversion group Regular.
33 (#) External trigger (timer or EXTI) with configurable polarity.
35 (#) DMA request generation for transfer of conversions data of regular group.
39 (#) ADC supply requirements: 2.4 V to 3.6 V at full speed and down to 1.8 V at
42 (#) ADC input range: from Vref minud (connected to Vssa) to Vref plus(connected to
43 Vdda or to an external voltage reference).
46 ##### How to use this driver #####
47 ==============================================================================
50 (#) Enable the ADC interface
51 As prerequisite, into HAL_ADC_MspInit(), ADC clock must be configured
52 at RCC top level: clock source and clock prescaler.
53 Two possible clock sources: synchronous clock derived from APB clock
54 or asynchronous clock derived from ADC dedicated HSI RC oscillator
57 __ADC1_CLK_ENABLE(); (mandatory)
59 HI14 enable or let under control of ADC: (optional)
61 RCC_OscInitTypeDef RCC_OscInitStructure;
62 RCC_OscInitStructure.OscillatorType = RCC_OSCILLATORTYPE_HSI14;
63 RCC_OscInitStructure.HSI14CalibrationValue = RCC_HSI14CALIBRATION_DEFAULT;
64 RCC_OscInitStructure.HSI14State = RCC_HSI14_ADC_CONTROL;
65 RCC_OscInitStructure.PLL... (optional if used for system clock)
66 HAL_RCC_OscConfig(&RCC_OscInitStructure);
68 Parameter "HSI14State" must be set either:
69 - to "...HSI14State = RCC_HSI14_ADC_CONTROL" to let the ADC control
70 the HSI14 oscillator enable/disable (if not used to supply the main
71 system clock): feature used if ADC mode LowPowerAutoPowerOff is
73 - to "...HSI14State = RCC_HSI14_ON" to maintain the HSI14 oscillator
74 always enabled: can be used to supply the main system clock.
76 (#) ADC pins configuration
77 (++) Enable the clock for the ADC GPIOs using the following function:
79 (++) Configure these ADC pins in analog mode using HAL_GPIO_Init();
81 (#) Configure the ADC parameters (conversion resolution, data alignment,
82 continuous mode, ...) using the HAL_ADC_Init() function.
84 (#) Activate the ADC peripheral using one of the start functions:
85 HAL_ADC_Start(), HAL_ADC_Start_IT(), HAL_ADC_Start_DMA().
87 *** Regular channels group configuration ***
88 ============================================
90 (+) To configure the ADC regular channels group features, use
91 HAL_ADC_Init() and HAL_ADC_ConfigChannel() functions.
92 (+) To activate the continuous mode, use the HAL_ADC_Init() function.
93 (+) To read the ADC converted values, use the HAL_ADC_GetValue() function.
95 *** DMA for Regular channels group features configuration ***
96 =============================================================
98 (+) To enable the DMA mode for regular channels group, use the
99 HAL_ADC_Start_DMA() function.
100 (+) To enable the generation of DMA requests continuously at the end of
101 the last DMA transfer, use the HAL_ADC_Init() function.
104 ******************************************************************************
107 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
109 * Redistribution and use in source and binary forms, with or without modification,
110 * are permitted provided that the following conditions are met:
111 * 1. Redistributions of source code must retain the above copyright notice,
112 * this list of conditions and the following disclaimer.
113 * 2. Redistributions in binary form must reproduce the above copyright notice,
114 * this list of conditions and the following disclaimer in the documentation
115 * and/or other materials provided with the distribution.
116 * 3. Neither the name of STMicroelectronics nor the names of its contributors
117 * may be used to endorse or promote products derived from this software
118 * without specific prior written permission.
120 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
121 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
122 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
123 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
124 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
125 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
126 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
127 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
128 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
129 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
131 ******************************************************************************
134 /* Includes ------------------------------------------------------------------*/
135 #include "stm32f0xx_hal.h"
137 /** @addtogroup STM32F0xx_HAL_Driver
141 /** @defgroup ADCEx ADCEx Extended HAL Module Driver
142 * @brief ADC HAL module driver
146 #ifdef HAL_ADC_MODULE_ENABLED
148 /* Private typedef -----------------------------------------------------------*/
149 /* Private define ------------------------------------------------------------*/
150 /** @defgroup ADCEx_Private_Constants ADCEx Private Constants
154 /* Fixed timeout values for ADC calibration, enable settling time, disable */
156 /* Values defined to be higher than worst cases: low clock frequency, */
157 /* maximum prescaler. */
158 /* Ex of profile low frequency : Clock source at 0.1 MHz, ADC clock */
161 #define ADC_DISABLE_TIMEOUT 2
162 #define ADC_CALIBRATION_TIMEOUT 2
167 /* Private macro -------------------------------------------------------------*/
168 /* Private variables ---------------------------------------------------------*/
169 /* Private function prototypes -----------------------------------------------*/
170 /* Private functions ---------------------------------------------------------*/
172 /** @defgroup ADCEx_Exported_Functions ADCEx Exported Functions
176 /** @defgroup ADCEx_Exported_Functions_Group1 Extended Initialization/de-initialization functions
177 * @brief Extended Initialization and Configuration functions
180 ===============================================================================
181 ##### IO operation functions #####
182 ===============================================================================
183 [..] This section provides functions allowing to:
184 (+) Perform the ADC calibration.
190 * @brief Perform an ADC automatic self-calibration
191 * Calibration prerequisite: ADC must be disabled (execute this
192 * function before HAL_ADC_Start() or after HAL_ADC_Stop() ).
193 * @note Calibration factor can be read after calibration, using function
194 * HAL_ADC_GetValue() (value on 7 bits: from DR[6;0]).
195 * @param hadc: ADC handle
198 HAL_StatusTypeDef HAL_ADCEx_Calibration_Start(ADC_HandleTypeDef* hadc)
200 HAL_StatusTypeDef tmpHALStatus = HAL_OK;
201 uint32_t tickstart=0;
203 /* Check the parameters */
204 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
209 /* Calibration prerequisite: ADC must be disabled. */
210 if (__HAL_ADC_IS_ENABLED(hadc) == RESET )
212 /* Change ADC state */
213 hadc->State = HAL_ADC_STATE_READY;
215 /* Start ADC calibration */
216 hadc->Instance->CR |= ADC_CR_ADCAL;
218 tickstart = HAL_GetTick();
220 /* Wait for calibration completion */
221 while(HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADCAL))
223 if((HAL_GetTick() - tickstart) > ADC_CALIBRATION_TIMEOUT)
225 /* Update ADC state machine to error */
226 hadc->State = HAL_ADC_STATE_ERROR;
228 /* Process unlocked */
237 /* Update ADC state machine to error */
238 hadc->State = HAL_ADC_STATE_ERROR;
241 /* Process unlocked */
244 /* Return function status */
256 #endif /* HAL_ADC_MODULE_ENABLED */
265 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/