mbed w/ spi bug fig

Dependents:   display-puck

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Thu May 22 20:00:09 2014 +0100
Revision:
205:c41fc65bcfb4
Child:
218:44081b78fdc2
Synchronized with git revision ea4b6f76efab17a3f7d7777b0cc1ef05fec6d1cb

Full URL: https://github.com/mbedmicro/mbed/commit/ea4b6f76efab17a3f7d7777b0cc1ef05fec6d1cb/

[NUCLEO_F072RB] cmsis files

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 205:c41fc65bcfb4 1 /**
mbed_official 205:c41fc65bcfb4 2 ******************************************************************************
mbed_official 205:c41fc65bcfb4 3 * @file stm32f0xx_hal_adc.c
mbed_official 205:c41fc65bcfb4 4 * @author MCD Application Team
mbed_official 205:c41fc65bcfb4 5 * @version V1.0.0
mbed_official 205:c41fc65bcfb4 6 * @date 20-May-2014
mbed_official 205:c41fc65bcfb4 7 * @brief This file provides firmware functions to manage the following
mbed_official 205:c41fc65bcfb4 8 * functionalities of the Analog to Digital Convertor (ADC)
mbed_official 205:c41fc65bcfb4 9 * peripheral:
mbed_official 205:c41fc65bcfb4 10 * + Initialization and de-initialization functions
mbed_official 205:c41fc65bcfb4 11 * ++ Initialization and Configuration of ADC
mbed_official 205:c41fc65bcfb4 12 * + Operation functions
mbed_official 205:c41fc65bcfb4 13 * ++ Start, stop, get result of conversions of regular group,
mbed_official 205:c41fc65bcfb4 14 * using 3 possible modes: polling, interruption or DMA.
mbed_official 205:c41fc65bcfb4 15 * + Control functions
mbed_official 205:c41fc65bcfb4 16 * ++ Analog Watchdog configuration
mbed_official 205:c41fc65bcfb4 17 * ++ Channels configuration on regular group
mbed_official 205:c41fc65bcfb4 18 * + State functions
mbed_official 205:c41fc65bcfb4 19 * ++ ADC state machine management
mbed_official 205:c41fc65bcfb4 20 * ++ Interrupts and flags management
mbed_official 205:c41fc65bcfb4 21 *
mbed_official 205:c41fc65bcfb4 22 @verbatim
mbed_official 205:c41fc65bcfb4 23 ==============================================================================
mbed_official 205:c41fc65bcfb4 24 ##### ADC specific features #####
mbed_official 205:c41fc65bcfb4 25 ==============================================================================
mbed_official 205:c41fc65bcfb4 26 [..]
mbed_official 205:c41fc65bcfb4 27 (#) 12-bit, 10-bit, 8-bit or 6-bit configurable resolution
mbed_official 205:c41fc65bcfb4 28
mbed_official 205:c41fc65bcfb4 29 (#) Interrupt generation at the end of regular conversion and in case of
mbed_official 205:c41fc65bcfb4 30 analog watchdog or overrun events.
mbed_official 205:c41fc65bcfb4 31
mbed_official 205:c41fc65bcfb4 32 (#) Single and continuous conversion modes.
mbed_official 205:c41fc65bcfb4 33
mbed_official 205:c41fc65bcfb4 34 (#) Scan mode for automatic conversion of channel 0 to channel ‘n’.
mbed_official 205:c41fc65bcfb4 35
mbed_official 205:c41fc65bcfb4 36 (#) Data alignment with in-built data coherency.
mbed_official 205:c41fc65bcfb4 37
mbed_official 205:c41fc65bcfb4 38 (#) Programmable sampling time.
mbed_official 205:c41fc65bcfb4 39
mbed_official 205:c41fc65bcfb4 40 (#) ADC conversion group Regular.
mbed_official 205:c41fc65bcfb4 41
mbed_official 205:c41fc65bcfb4 42 (#) External trigger (timer or EXTI) with configurable polarity.
mbed_official 205:c41fc65bcfb4 43
mbed_official 205:c41fc65bcfb4 44 (#) DMA request generation for transfer of conversions data of regular group.
mbed_official 205:c41fc65bcfb4 45
mbed_official 205:c41fc65bcfb4 46 (#) ADC calibration
mbed_official 205:c41fc65bcfb4 47
mbed_official 205:c41fc65bcfb4 48 (#) ADC supply requirements: 2.4 V to 3.6 V at full speed and down to 1.8 V at
mbed_official 205:c41fc65bcfb4 49 slower speed.
mbed_official 205:c41fc65bcfb4 50
mbed_official 205:c41fc65bcfb4 51 (#) ADC input range: from Vref– (connected to Vssa) to Vref+ (connected to
mbed_official 205:c41fc65bcfb4 52 Vdda or to an external voltage reference).
mbed_official 205:c41fc65bcfb4 53
mbed_official 205:c41fc65bcfb4 54
mbed_official 205:c41fc65bcfb4 55 ##### How to use this driver #####
mbed_official 205:c41fc65bcfb4 56 ==============================================================================
mbed_official 205:c41fc65bcfb4 57 [..]
mbed_official 205:c41fc65bcfb4 58
mbed_official 205:c41fc65bcfb4 59 (#) Enable the ADC interface
mbed_official 205:c41fc65bcfb4 60 As prerequisite, into HAL_ADC_MspInit(), ADC clock must be configured
mbed_official 205:c41fc65bcfb4 61 at RCC top level: clock source and clock prescaler.
mbed_official 205:c41fc65bcfb4 62 Two possible clock sources: synchronous clock derived from APB clock
mbed_official 205:c41fc65bcfb4 63 or asynchronous clock derived from ADC dedicated HSI RC oscillator
mbed_official 205:c41fc65bcfb4 64 14MHz.
mbed_official 205:c41fc65bcfb4 65 Example:
mbed_official 205:c41fc65bcfb4 66 __ADC1_CLK_ENABLE(); (mandatory)
mbed_official 205:c41fc65bcfb4 67
mbed_official 205:c41fc65bcfb4 68 HI14 enable or let under control of ADC: (optional)
mbed_official 205:c41fc65bcfb4 69
mbed_official 205:c41fc65bcfb4 70 RCC_OscInitTypeDef RCC_OscInitStructure;
mbed_official 205:c41fc65bcfb4 71 RCC_OscInitStructure.OscillatorType = RCC_OSCILLATORTYPE_HSI14;
mbed_official 205:c41fc65bcfb4 72 RCC_OscInitStructure.HSI14CalibrationValue = RCC_HSI14CALIBRATION_DEFAULT;
mbed_official 205:c41fc65bcfb4 73 RCC_OscInitStructure.HSI14State = RCC_HSI14_ADC_CONTROL;
mbed_official 205:c41fc65bcfb4 74 RCC_OscInitStructure.PLL... (optional if used for system clock)
mbed_official 205:c41fc65bcfb4 75 HAL_RCC_OscConfig(&RCC_OscInitStructure);
mbed_official 205:c41fc65bcfb4 76
mbed_official 205:c41fc65bcfb4 77 Parameter "HSI14State" must be set either:
mbed_official 205:c41fc65bcfb4 78 - to "...HSI14State = RCC_HSI14_ADC_CONTROL" to let the ADC control
mbed_official 205:c41fc65bcfb4 79 the HSI14 oscillator enable/disable (if not used to supply the main
mbed_official 205:c41fc65bcfb4 80 system clock): feature used if ADC mode LowPowerAutoPowerOff is
mbed_official 205:c41fc65bcfb4 81 enabled.
mbed_official 205:c41fc65bcfb4 82 - to "...HSI14State = RCC_HSI14_ON" to maintain the HSI14 oscillator
mbed_official 205:c41fc65bcfb4 83 always enabled: can be used to supply the main system clock.
mbed_official 205:c41fc65bcfb4 84
mbed_official 205:c41fc65bcfb4 85 (#) ADC pins configuration
mbed_official 205:c41fc65bcfb4 86 (++) Enable the clock for the ADC GPIOs using the following function:
mbed_official 205:c41fc65bcfb4 87 __GPIOx_CLK_ENABLE();
mbed_official 205:c41fc65bcfb4 88 (++) Configure these ADC pins in analog mode using HAL_GPIO_Init();
mbed_official 205:c41fc65bcfb4 89
mbed_official 205:c41fc65bcfb4 90 (#) Configure the ADC parameters (conversion resolution, data alignment,
mbed_official 205:c41fc65bcfb4 91 continuous mode, ...) using the HAL_ADC_Init() function.
mbed_official 205:c41fc65bcfb4 92
mbed_official 205:c41fc65bcfb4 93 (#) Activate the ADC peripheral using one of the start functions:
mbed_official 205:c41fc65bcfb4 94 HAL_ADC_Start(), HAL_ADC_Start_IT(), HAL_ADC_Start_DMA().
mbed_official 205:c41fc65bcfb4 95
mbed_official 205:c41fc65bcfb4 96 *** Channels to regular group configuration ***
mbed_official 205:c41fc65bcfb4 97 ============================================
mbed_official 205:c41fc65bcfb4 98 [..]
mbed_official 205:c41fc65bcfb4 99 (+) To configure the ADC regular group features, use
mbed_official 205:c41fc65bcfb4 100 HAL_ADC_Init() and HAL_ADC_ConfigChannel() functions.
mbed_official 205:c41fc65bcfb4 101 (+) To activate the continuous mode, use the HAL_ADC_Init() function.
mbed_official 205:c41fc65bcfb4 102 (+) To read the ADC converted values, use the HAL_ADC_GetValue() function.
mbed_official 205:c41fc65bcfb4 103
mbed_official 205:c41fc65bcfb4 104 *** DMA for regular configuration ***
mbed_official 205:c41fc65bcfb4 105 =============================================================
mbed_official 205:c41fc65bcfb4 106 [..]
mbed_official 205:c41fc65bcfb4 107 (+) To enable the DMA mode for regular group, use the
mbed_official 205:c41fc65bcfb4 108 HAL_ADC_Start_DMA() function.
mbed_official 205:c41fc65bcfb4 109 (+) To enable the generation of DMA requests continuously at the end of
mbed_official 205:c41fc65bcfb4 110 the last DMA transfer, use the HAL_ADC_Init() function.
mbed_official 205:c41fc65bcfb4 111
mbed_official 205:c41fc65bcfb4 112 @endverbatim
mbed_official 205:c41fc65bcfb4 113 ******************************************************************************
mbed_official 205:c41fc65bcfb4 114 * @attention
mbed_official 205:c41fc65bcfb4 115 *
mbed_official 205:c41fc65bcfb4 116 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
mbed_official 205:c41fc65bcfb4 117 *
mbed_official 205:c41fc65bcfb4 118 * Redistribution and use in source and binary forms, with or without modification,
mbed_official 205:c41fc65bcfb4 119 * are permitted provided that the following conditions are met:
mbed_official 205:c41fc65bcfb4 120 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 205:c41fc65bcfb4 121 * this list of conditions and the following disclaimer.
mbed_official 205:c41fc65bcfb4 122 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 205:c41fc65bcfb4 123 * this list of conditions and the following disclaimer in the documentation
mbed_official 205:c41fc65bcfb4 124 * and/or other materials provided with the distribution.
mbed_official 205:c41fc65bcfb4 125 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 205:c41fc65bcfb4 126 * may be used to endorse or promote products derived from this software
mbed_official 205:c41fc65bcfb4 127 * without specific prior written permission.
mbed_official 205:c41fc65bcfb4 128 *
mbed_official 205:c41fc65bcfb4 129 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 205:c41fc65bcfb4 130 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 205:c41fc65bcfb4 131 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 205:c41fc65bcfb4 132 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 205:c41fc65bcfb4 133 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 205:c41fc65bcfb4 134 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 205:c41fc65bcfb4 135 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 205:c41fc65bcfb4 136 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 205:c41fc65bcfb4 137 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 205:c41fc65bcfb4 138 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 205:c41fc65bcfb4 139 *
mbed_official 205:c41fc65bcfb4 140 ******************************************************************************
mbed_official 205:c41fc65bcfb4 141 */
mbed_official 205:c41fc65bcfb4 142
mbed_official 205:c41fc65bcfb4 143 /* Includes ------------------------------------------------------------------*/
mbed_official 205:c41fc65bcfb4 144 #include "stm32f0xx_hal.h"
mbed_official 205:c41fc65bcfb4 145
mbed_official 205:c41fc65bcfb4 146 /** @addtogroup STM32F0xx_HAL_Driver
mbed_official 205:c41fc65bcfb4 147 * @{
mbed_official 205:c41fc65bcfb4 148 */
mbed_official 205:c41fc65bcfb4 149
mbed_official 205:c41fc65bcfb4 150 /** @defgroup ADC
mbed_official 205:c41fc65bcfb4 151 * @brief ADC HAL module driver
mbed_official 205:c41fc65bcfb4 152 * @{
mbed_official 205:c41fc65bcfb4 153 */
mbed_official 205:c41fc65bcfb4 154
mbed_official 205:c41fc65bcfb4 155 #ifdef HAL_ADC_MODULE_ENABLED
mbed_official 205:c41fc65bcfb4 156
mbed_official 205:c41fc65bcfb4 157 /* Private typedef -----------------------------------------------------------*/
mbed_official 205:c41fc65bcfb4 158 /* Private define ------------------------------------------------------------*/
mbed_official 205:c41fc65bcfb4 159 /* Fixed timeout values for ADC calibration, enable settling time, disable */
mbed_official 205:c41fc65bcfb4 160 /* settling time. */
mbed_official 205:c41fc65bcfb4 161 /* Values defined to be higher than worst cases: low clock frequency, */
mbed_official 205:c41fc65bcfb4 162 /* maximum prescaler. */
mbed_official 205:c41fc65bcfb4 163 /* Ex of profile low frequency : Clock source at 0.1 MHz, ADC clock */
mbed_official 205:c41fc65bcfb4 164 /* prescaler 4, sampling time 7.5 ADC clock cycles, resolution 12 bits. */
mbed_official 205:c41fc65bcfb4 165 /* Unit: ms */
mbed_official 205:c41fc65bcfb4 166 #define ADC_ENABLE_TIMEOUT 2
mbed_official 205:c41fc65bcfb4 167 #define ADC_DISABLE_TIMEOUT 2
mbed_official 205:c41fc65bcfb4 168 #define ADC_STOP_CONVERSION_TIMEOUT 2
mbed_official 205:c41fc65bcfb4 169
mbed_official 205:c41fc65bcfb4 170 /* Delay of 10us fixed to worst case: maximum CPU frequency 180MHz to have */
mbed_official 205:c41fc65bcfb4 171 /* the minimum number of CPU cycles to fulfill this delay */
mbed_official 205:c41fc65bcfb4 172 #define ADC_DELAY_10US_MIN_CPU_CYCLES 1800
mbed_official 205:c41fc65bcfb4 173
mbed_official 205:c41fc65bcfb4 174 /* Private macro -------------------------------------------------------------*/
mbed_official 205:c41fc65bcfb4 175 /* Private variables ---------------------------------------------------------*/
mbed_official 205:c41fc65bcfb4 176 /* Private function prototypes -----------------------------------------------*/
mbed_official 205:c41fc65bcfb4 177 static HAL_StatusTypeDef ADC_Enable(ADC_HandleTypeDef* hadc);
mbed_official 205:c41fc65bcfb4 178 static HAL_StatusTypeDef ADC_Disable(ADC_HandleTypeDef* hadc);
mbed_official 205:c41fc65bcfb4 179 static HAL_StatusTypeDef ADC_ConversionStop(ADC_HandleTypeDef* hadc);
mbed_official 205:c41fc65bcfb4 180 static void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma);
mbed_official 205:c41fc65bcfb4 181 static void ADC_DMAHalfConvCplt(DMA_HandleTypeDef *hdma);
mbed_official 205:c41fc65bcfb4 182 static void ADC_DMAError(DMA_HandleTypeDef *hdma);
mbed_official 205:c41fc65bcfb4 183 /* Private functions ---------------------------------------------------------*/
mbed_official 205:c41fc65bcfb4 184
mbed_official 205:c41fc65bcfb4 185 /** @defgroup ADC_Private_Functions
mbed_official 205:c41fc65bcfb4 186 * @{
mbed_official 205:c41fc65bcfb4 187 */
mbed_official 205:c41fc65bcfb4 188
mbed_official 205:c41fc65bcfb4 189 /** @defgroup ADC_Group1 Initialization/de-initialization functions
mbed_official 205:c41fc65bcfb4 190 * @brief Initialization and Configuration functions
mbed_official 205:c41fc65bcfb4 191 *
mbed_official 205:c41fc65bcfb4 192 @verbatim
mbed_official 205:c41fc65bcfb4 193 ===============================================================================
mbed_official 205:c41fc65bcfb4 194 ##### Initialization and de-initialization functions #####
mbed_official 205:c41fc65bcfb4 195 ===============================================================================
mbed_official 205:c41fc65bcfb4 196 [..] This section provides functions allowing to:
mbed_official 205:c41fc65bcfb4 197 (+) Initialize and configure the ADC.
mbed_official 205:c41fc65bcfb4 198 (+) De-initialize the ADC
mbed_official 205:c41fc65bcfb4 199 @endverbatim
mbed_official 205:c41fc65bcfb4 200 * @{
mbed_official 205:c41fc65bcfb4 201 */
mbed_official 205:c41fc65bcfb4 202
mbed_official 205:c41fc65bcfb4 203 /**
mbed_official 205:c41fc65bcfb4 204 * @brief Initializes the ADC peripheral and regular group according to
mbed_official 205:c41fc65bcfb4 205 * parameters specified in structure "ADC_InitTypeDef".
mbed_official 205:c41fc65bcfb4 206 * @note As prerequisite, ADC clock must be configured at RCC top level
mbed_official 205:c41fc65bcfb4 207 * depending on both possible clock sources: APB clock of HSI clock.
mbed_official 205:c41fc65bcfb4 208 * See commented example code below that can be copied and uncommented
mbed_official 205:c41fc65bcfb4 209 * into HAL_ADC_MspInit().
mbed_official 205:c41fc65bcfb4 210 * @note Possibility to update parameters on the fly:
mbed_official 205:c41fc65bcfb4 211 * This function initializes the ADC MSP (HAL_ADC_MspInit()) only when
mbed_official 205:c41fc65bcfb4 212 * coming from ADC state reset. Following calls to this function can
mbed_official 205:c41fc65bcfb4 213 * be used to reconfigure some parameters of ADC_InitTypeDef
mbed_official 205:c41fc65bcfb4 214 * structure on the fly, without modifiying MSP configuration. If ADC
mbed_official 205:c41fc65bcfb4 215 * MSP has to be modified again, HAL_ADC_DeInit() must be called
mbed_official 205:c41fc65bcfb4 216 * before HAL_ADC_Init().
mbed_official 205:c41fc65bcfb4 217 * The setting of these parameters is conditioned to ADC state.
mbed_official 205:c41fc65bcfb4 218 * For parameters constraints, see comments of structure
mbed_official 205:c41fc65bcfb4 219 * "ADC_InitTypeDef".
mbed_official 205:c41fc65bcfb4 220 * @note This function configures the ADC within 2 scopes: scope of entire
mbed_official 205:c41fc65bcfb4 221 * ADC and scope of regular group. For parameters details, see comments
mbed_official 205:c41fc65bcfb4 222 * of structure "ADC_InitTypeDef".
mbed_official 205:c41fc65bcfb4 223 * @param hadc: ADC handle
mbed_official 205:c41fc65bcfb4 224 * @retval HAL status
mbed_official 205:c41fc65bcfb4 225 */
mbed_official 205:c41fc65bcfb4 226 HAL_StatusTypeDef HAL_ADC_Init(ADC_HandleTypeDef* hadc)
mbed_official 205:c41fc65bcfb4 227 {
mbed_official 205:c41fc65bcfb4 228 HAL_StatusTypeDef tmpHALStatus = HAL_OK;
mbed_official 205:c41fc65bcfb4 229 uint32_t tmpCFGR1 = 0;
mbed_official 205:c41fc65bcfb4 230
mbed_official 205:c41fc65bcfb4 231 /* Check ADC handle */
mbed_official 205:c41fc65bcfb4 232 if(hadc == NULL)
mbed_official 205:c41fc65bcfb4 233 {
mbed_official 205:c41fc65bcfb4 234 return HAL_ERROR;
mbed_official 205:c41fc65bcfb4 235 }
mbed_official 205:c41fc65bcfb4 236
mbed_official 205:c41fc65bcfb4 237 /* Check the parameters */
mbed_official 205:c41fc65bcfb4 238 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
mbed_official 205:c41fc65bcfb4 239 assert_param(IS_ADC_CLOCKPRESCALER(hadc->Init.ClockPrescaler));
mbed_official 205:c41fc65bcfb4 240 assert_param(IS_ADC_RESOLUTION(hadc->Init.Resolution));
mbed_official 205:c41fc65bcfb4 241 assert_param(IS_ADC_DATA_ALIGN(hadc->Init.DataAlign));
mbed_official 205:c41fc65bcfb4 242 assert_param(IS_ADC_SCAN_MODE(hadc->Init.ScanConvMode));
mbed_official 205:c41fc65bcfb4 243 assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
mbed_official 205:c41fc65bcfb4 244 assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DiscontinuousConvMode));
mbed_official 205:c41fc65bcfb4 245 assert_param(IS_ADC_EXTTRIG_EDGE(hadc->Init.ExternalTrigConvEdge));
mbed_official 205:c41fc65bcfb4 246 assert_param(IS_ADC_EXTTRIG(hadc->Init.ExternalTrigConv));
mbed_official 205:c41fc65bcfb4 247 assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DMAContinuousRequests));
mbed_official 205:c41fc65bcfb4 248 assert_param(IS_ADC_EOC_SELECTION(hadc->Init.EOCSelection));
mbed_official 205:c41fc65bcfb4 249 assert_param(IS_ADC_OVERRUN(hadc->Init.Overrun));
mbed_official 205:c41fc65bcfb4 250 assert_param(IS_FUNCTIONAL_STATE(hadc->Init.LowPowerAutoWait));
mbed_official 205:c41fc65bcfb4 251 assert_param(IS_FUNCTIONAL_STATE(hadc->Init.LowPowerAutoPowerOff));
mbed_official 205:c41fc65bcfb4 252
mbed_official 205:c41fc65bcfb4 253 /* As prerequisite, into HAL_ADC_MspInit(), ADC clock must be configured */
mbed_official 205:c41fc65bcfb4 254 /* at RCC top level depending on both possible clock sources: */
mbed_official 205:c41fc65bcfb4 255 /* APB clock or HSI clock. */
mbed_official 205:c41fc65bcfb4 256 /* For example: */
mbed_official 205:c41fc65bcfb4 257 /* __ADC1_CLK_ENABLE(); */
mbed_official 205:c41fc65bcfb4 258 /* __HAL_RCC_HSI14ADC_ENABLE(); */
mbed_official 205:c41fc65bcfb4 259
mbed_official 205:c41fc65bcfb4 260
mbed_official 205:c41fc65bcfb4 261 /* Actions performed only if ADC is coming from state reset: */
mbed_official 205:c41fc65bcfb4 262 /* - Initialization of ADC MSP */
mbed_official 205:c41fc65bcfb4 263 /* - ADC voltage regulator enable */
mbed_official 205:c41fc65bcfb4 264 if (hadc->State == HAL_ADC_STATE_RESET)
mbed_official 205:c41fc65bcfb4 265 {
mbed_official 205:c41fc65bcfb4 266 /* Init the low level hardware */
mbed_official 205:c41fc65bcfb4 267 HAL_ADC_MspInit(hadc);
mbed_official 205:c41fc65bcfb4 268
mbed_official 205:c41fc65bcfb4 269 }
mbed_official 205:c41fc65bcfb4 270
mbed_official 205:c41fc65bcfb4 271 /* Configuration of ADC parameters if previous preliminary actions are */
mbed_official 205:c41fc65bcfb4 272 /* correctly completed. */
mbed_official 205:c41fc65bcfb4 273 /* and if there is no conversion on going on regular group (ADC can be */
mbed_official 205:c41fc65bcfb4 274 /* enabled anyway, in case of call of this function to update a parameter */
mbed_official 205:c41fc65bcfb4 275 /* on the fly). */
mbed_official 205:c41fc65bcfb4 276 if ((hadc->State != HAL_ADC_STATE_ERROR) &&
mbed_official 205:c41fc65bcfb4 277 (__HAL_ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET) )
mbed_official 205:c41fc65bcfb4 278 {
mbed_official 205:c41fc65bcfb4 279 /* Initialize the ADC state */
mbed_official 205:c41fc65bcfb4 280 hadc->State = HAL_ADC_STATE_BUSY;
mbed_official 205:c41fc65bcfb4 281
mbed_official 205:c41fc65bcfb4 282 /* Parameters update conditioned to ADC state: */
mbed_official 205:c41fc65bcfb4 283 /* Parameters that can be updated only when ADC is disabled: */
mbed_official 205:c41fc65bcfb4 284 /* - ADC clock mode */
mbed_official 205:c41fc65bcfb4 285 /* - ADC clock prescaler */
mbed_official 205:c41fc65bcfb4 286 if (__HAL_ADC_IS_ENABLED(hadc) == RESET)
mbed_official 205:c41fc65bcfb4 287 {
mbed_official 205:c41fc65bcfb4 288 /* Some parameters of this register are not reset, since they are set */
mbed_official 205:c41fc65bcfb4 289 /* by other functions and must be kept in case of usage of this */
mbed_official 205:c41fc65bcfb4 290 /* function on the fly (update of a parameter of ADC_InitTypeDef */
mbed_official 205:c41fc65bcfb4 291 /* without needing to reconfigure all other ADC groups/channels */
mbed_official 205:c41fc65bcfb4 292 /* parameters): */
mbed_official 205:c41fc65bcfb4 293 /* - internal measurement paths: Vbat, temperature sensor, Vref */
mbed_official 205:c41fc65bcfb4 294 /* (set into HAL_ADC_ConfigChannel() */
mbed_official 205:c41fc65bcfb4 295
mbed_official 205:c41fc65bcfb4 296 /* Reset configuration of ADC configuration register CFGR2: */
mbed_official 205:c41fc65bcfb4 297 /* - ADC clock mode: CKMODE */
mbed_official 205:c41fc65bcfb4 298 hadc->Instance->CFGR2 &= ~(ADC_CFGR2_CKMODE);
mbed_official 205:c41fc65bcfb4 299
mbed_official 205:c41fc65bcfb4 300 /* Configuration of ADC clock mode: clock source AHB or HSI with */
mbed_official 205:c41fc65bcfb4 301 /* selectable prescaler */
mbed_official 205:c41fc65bcfb4 302 hadc->Instance->CFGR2 |= hadc->Init.ClockPrescaler;
mbed_official 205:c41fc65bcfb4 303 }
mbed_official 205:c41fc65bcfb4 304
mbed_official 205:c41fc65bcfb4 305 /* Configuration of ADC: */
mbed_official 205:c41fc65bcfb4 306 /* - discontinuous mode */
mbed_official 205:c41fc65bcfb4 307 /* - LowPowerAutoWait mode */
mbed_official 205:c41fc65bcfb4 308 /* - LowPowerAutoPowerOff mode */
mbed_official 205:c41fc65bcfb4 309 /* - continuous conversion mode */
mbed_official 205:c41fc65bcfb4 310 /* - overrun */
mbed_official 205:c41fc65bcfb4 311 /* - external trigger to start conversion */
mbed_official 205:c41fc65bcfb4 312 /* - external trigger polarity */
mbed_official 205:c41fc65bcfb4 313 /* - data alignment */
mbed_official 205:c41fc65bcfb4 314 /* - resolution */
mbed_official 205:c41fc65bcfb4 315 /* - scan direction */
mbed_official 205:c41fc65bcfb4 316 /* - DMA continuous request */
mbed_official 205:c41fc65bcfb4 317 hadc->Instance->CFGR1 &= ~( ADC_CFGR1_DISCEN |
mbed_official 205:c41fc65bcfb4 318 ADC_CFGR1_AUTOFF |
mbed_official 205:c41fc65bcfb4 319 ADC_CFGR1_AUTDLY |
mbed_official 205:c41fc65bcfb4 320 ADC_CFGR1_CONT |
mbed_official 205:c41fc65bcfb4 321 ADC_CFGR1_OVRMOD |
mbed_official 205:c41fc65bcfb4 322 ADC_CFGR1_EXTSEL |
mbed_official 205:c41fc65bcfb4 323 ADC_CFGR1_EXTEN |
mbed_official 205:c41fc65bcfb4 324 ADC_CFGR1_ALIGN |
mbed_official 205:c41fc65bcfb4 325 ADC_CFGR1_RES |
mbed_official 205:c41fc65bcfb4 326 ADC_CFGR1_SCANDIR |
mbed_official 205:c41fc65bcfb4 327 ADC_CFGR1_DMACFG );
mbed_official 205:c41fc65bcfb4 328
mbed_official 205:c41fc65bcfb4 329 tmpCFGR1 |= (__HAL_ADC_CFGR1_AUTOWAIT(hadc->Init.LowPowerAutoWait) |
mbed_official 205:c41fc65bcfb4 330 __HAL_ADC_CFGR1_AUTOOFF(hadc->Init.LowPowerAutoPowerOff) |
mbed_official 205:c41fc65bcfb4 331 __HAL_ADC_CFGR1_CONTINUOUS(hadc->Init.ContinuousConvMode) |
mbed_official 205:c41fc65bcfb4 332 __HAL_ADC_CFGR1_OVERRUN(hadc->Init.Overrun) |
mbed_official 205:c41fc65bcfb4 333 hadc->Init.DataAlign |
mbed_official 205:c41fc65bcfb4 334 hadc->Init.Resolution |
mbed_official 205:c41fc65bcfb4 335 __HAL_ADC_CFGR1_SCANDIR(hadc->Init.ScanConvMode) |
mbed_official 205:c41fc65bcfb4 336 __HAL_ADC_CFGR1_DMACONTREQ(hadc->Init.DMAContinuousRequests) );
mbed_official 205:c41fc65bcfb4 337
mbed_official 205:c41fc65bcfb4 338 /* Enable discontinuous mode only if continuous mode is disabled */
mbed_official 205:c41fc65bcfb4 339 if ((hadc->Init.DiscontinuousConvMode == ENABLE) &&
mbed_official 205:c41fc65bcfb4 340 (hadc->Init.ContinuousConvMode == DISABLE) )
mbed_official 205:c41fc65bcfb4 341 {
mbed_official 205:c41fc65bcfb4 342 /* Enable discontinuous mode of regular group */
mbed_official 205:c41fc65bcfb4 343 tmpCFGR1 |= ADC_CFGR1_DISCEN;
mbed_official 205:c41fc65bcfb4 344 }
mbed_official 205:c41fc65bcfb4 345
mbed_official 205:c41fc65bcfb4 346 /* Enable external trigger if trigger selection is different of software */
mbed_official 205:c41fc65bcfb4 347 /* start. */
mbed_official 205:c41fc65bcfb4 348 /* Note: This configuration keeps the hardware feature of parameter */
mbed_official 205:c41fc65bcfb4 349 /* ExternalTrigConvEdge "trigger edge none" equivalent to */
mbed_official 205:c41fc65bcfb4 350 /* software start. */
mbed_official 205:c41fc65bcfb4 351 if (hadc->Init.ExternalTrigConv != ADC_SOFTWARE_START)
mbed_official 205:c41fc65bcfb4 352 {
mbed_official 205:c41fc65bcfb4 353 tmpCFGR1 |= ( hadc->Init.ExternalTrigConv |
mbed_official 205:c41fc65bcfb4 354 hadc->Init.ExternalTrigConvEdge );
mbed_official 205:c41fc65bcfb4 355 }
mbed_official 205:c41fc65bcfb4 356
mbed_official 205:c41fc65bcfb4 357 /* Update ADC configuration register with previous settings */
mbed_official 205:c41fc65bcfb4 358 hadc->Instance->CFGR1 |= tmpCFGR1;
mbed_official 205:c41fc65bcfb4 359
mbed_official 205:c41fc65bcfb4 360 /* Check back if CFGR1 has been effectively configured into ADC register */
mbed_official 205:c41fc65bcfb4 361 /* to ensure of no potential problem of ADC clocking. */
mbed_official 205:c41fc65bcfb4 362 /* (check excluding analog watchdog configuration: set into separate */
mbed_official 205:c41fc65bcfb4 363 /* dedicated function) */
mbed_official 205:c41fc65bcfb4 364 if ((hadc->Instance->CFGR1 & ~(ADC_CFGR1_AWDCH | ADC_CFGR1_AWDEN | ADC_CFGR1_AWDSGL))
mbed_official 205:c41fc65bcfb4 365 == tmpCFGR1)
mbed_official 205:c41fc65bcfb4 366 {
mbed_official 205:c41fc65bcfb4 367 /* Set ADC error code to none */
mbed_official 205:c41fc65bcfb4 368 __HAL_ADC_CLEAR_ERRORCODE(hadc);
mbed_official 205:c41fc65bcfb4 369
mbed_official 205:c41fc65bcfb4 370 /* Initialize the ADC state */
mbed_official 205:c41fc65bcfb4 371 hadc->State = HAL_ADC_STATE_READY;
mbed_official 205:c41fc65bcfb4 372 }
mbed_official 205:c41fc65bcfb4 373 else
mbed_official 205:c41fc65bcfb4 374 {
mbed_official 205:c41fc65bcfb4 375 /* Update ADC state machine to error */
mbed_official 205:c41fc65bcfb4 376 hadc->State = HAL_ADC_STATE_ERROR;
mbed_official 205:c41fc65bcfb4 377
mbed_official 205:c41fc65bcfb4 378 /* Set ADC error code to ADC IP internal error */
mbed_official 205:c41fc65bcfb4 379 hadc->ErrorCode |= HAL_ADC_ERROR_INTERNAL;
mbed_official 205:c41fc65bcfb4 380
mbed_official 205:c41fc65bcfb4 381 tmpHALStatus = HAL_ERROR;
mbed_official 205:c41fc65bcfb4 382 }
mbed_official 205:c41fc65bcfb4 383
mbed_official 205:c41fc65bcfb4 384 }
mbed_official 205:c41fc65bcfb4 385 else
mbed_official 205:c41fc65bcfb4 386 {
mbed_official 205:c41fc65bcfb4 387 /* Update ADC state machine to error */
mbed_official 205:c41fc65bcfb4 388 hadc->State = HAL_ADC_STATE_ERROR;
mbed_official 205:c41fc65bcfb4 389
mbed_official 205:c41fc65bcfb4 390 tmpHALStatus = HAL_ERROR;
mbed_official 205:c41fc65bcfb4 391 }
mbed_official 205:c41fc65bcfb4 392
mbed_official 205:c41fc65bcfb4 393
mbed_official 205:c41fc65bcfb4 394 /* Return function status */
mbed_official 205:c41fc65bcfb4 395 return tmpHALStatus;
mbed_official 205:c41fc65bcfb4 396 }
mbed_official 205:c41fc65bcfb4 397
mbed_official 205:c41fc65bcfb4 398
mbed_official 205:c41fc65bcfb4 399 /**
mbed_official 205:c41fc65bcfb4 400 * @brief Deinitialize the ADC peripheral registers to their default reset
mbed_official 205:c41fc65bcfb4 401 * values, with deinitialization of the ADC MSP.
mbed_official 205:c41fc65bcfb4 402 * @note For devices with several ADCs: reset of ADC common registers is done
mbed_official 205:c41fc65bcfb4 403 * only if all ADCs sharing the same common group are disabled.
mbed_official 205:c41fc65bcfb4 404 * If this is not the case, reset of these common parameters reset is
mbed_official 205:c41fc65bcfb4 405 * bypassed without error reporting: it can be the intended behaviour in
mbed_official 205:c41fc65bcfb4 406 * case of reset of a single ADC while the other ADCs sharing the same
mbed_official 205:c41fc65bcfb4 407 * common group is still running.
mbed_official 205:c41fc65bcfb4 408 * @param hadc: ADC handle
mbed_official 205:c41fc65bcfb4 409 * @retval HAL status
mbed_official 205:c41fc65bcfb4 410 */
mbed_official 205:c41fc65bcfb4 411 HAL_StatusTypeDef HAL_ADC_DeInit(ADC_HandleTypeDef* hadc)
mbed_official 205:c41fc65bcfb4 412 {
mbed_official 205:c41fc65bcfb4 413 HAL_StatusTypeDef tmpHALStatus = HAL_OK;
mbed_official 205:c41fc65bcfb4 414
mbed_official 205:c41fc65bcfb4 415 /* Check ADC handle */
mbed_official 205:c41fc65bcfb4 416 if(hadc == NULL)
mbed_official 205:c41fc65bcfb4 417 {
mbed_official 205:c41fc65bcfb4 418 return HAL_ERROR;
mbed_official 205:c41fc65bcfb4 419 }
mbed_official 205:c41fc65bcfb4 420
mbed_official 205:c41fc65bcfb4 421 /* Check the parameters */
mbed_official 205:c41fc65bcfb4 422 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
mbed_official 205:c41fc65bcfb4 423
mbed_official 205:c41fc65bcfb4 424 /* Change ADC state */
mbed_official 205:c41fc65bcfb4 425 hadc->State = HAL_ADC_STATE_BUSY;
mbed_official 205:c41fc65bcfb4 426
mbed_official 205:c41fc65bcfb4 427 /* Stop potential conversion on going, on regular group */
mbed_official 205:c41fc65bcfb4 428 tmpHALStatus = ADC_ConversionStop(hadc);
mbed_official 205:c41fc65bcfb4 429
mbed_official 205:c41fc65bcfb4 430 /* Disable ADC peripheral if conversions are effectively stopped */
mbed_official 205:c41fc65bcfb4 431 if (tmpHALStatus != HAL_ERROR)
mbed_official 205:c41fc65bcfb4 432 {
mbed_official 205:c41fc65bcfb4 433 /* Disable the ADC peripheral */
mbed_official 205:c41fc65bcfb4 434 tmpHALStatus = ADC_Disable(hadc);
mbed_official 205:c41fc65bcfb4 435
mbed_official 205:c41fc65bcfb4 436 /* Check if ADC is effectively disabled */
mbed_official 205:c41fc65bcfb4 437 if (tmpHALStatus != HAL_ERROR)
mbed_official 205:c41fc65bcfb4 438 {
mbed_official 205:c41fc65bcfb4 439 /* Change ADC state */
mbed_official 205:c41fc65bcfb4 440 hadc->State = HAL_ADC_STATE_READY;
mbed_official 205:c41fc65bcfb4 441 }
mbed_official 205:c41fc65bcfb4 442 }
mbed_official 205:c41fc65bcfb4 443
mbed_official 205:c41fc65bcfb4 444
mbed_official 205:c41fc65bcfb4 445 /* Configuration of ADC parameters if previous preliminary actions are */
mbed_official 205:c41fc65bcfb4 446 /* correctly completed. */
mbed_official 205:c41fc65bcfb4 447 if (tmpHALStatus != HAL_ERROR)
mbed_official 205:c41fc65bcfb4 448 {
mbed_official 205:c41fc65bcfb4 449
mbed_official 205:c41fc65bcfb4 450 /* ========== Reset ADC registers ========== */
mbed_official 205:c41fc65bcfb4 451 /* Reset register IER */
mbed_official 205:c41fc65bcfb4 452 __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_AWD | ADC_IT_OVR |
mbed_official 205:c41fc65bcfb4 453 ADC_IT_EOS | ADC_IT_EOC |
mbed_official 205:c41fc65bcfb4 454 ADC_IT_EOSMP | ADC_IT_RDY ) );
mbed_official 205:c41fc65bcfb4 455
mbed_official 205:c41fc65bcfb4 456 /* Reset register ISR */
mbed_official 205:c41fc65bcfb4 457 __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_AWD | ADC_FLAG_OVR |
mbed_official 205:c41fc65bcfb4 458 ADC_FLAG_EOS | ADC_FLAG_EOC |
mbed_official 205:c41fc65bcfb4 459 ADC_FLAG_EOSMP | ADC_FLAG_RDY ) );
mbed_official 205:c41fc65bcfb4 460
mbed_official 205:c41fc65bcfb4 461 /* Reset register CR */
mbed_official 205:c41fc65bcfb4 462 /* Bits ADC_CR_ADCAL, ADC_CR_ADSTP, ADC_CR_ADSTART are in access mode */
mbed_official 205:c41fc65bcfb4 463 /* "read-set": no direct reset applicable. */
mbed_official 205:c41fc65bcfb4 464
mbed_official 205:c41fc65bcfb4 465 /* Reset register CFGR1 */
mbed_official 205:c41fc65bcfb4 466 hadc->Instance->CFGR1 &= ~(ADC_CFGR1_AWDCH | ADC_CFGR1_AWDEN | ADC_CFGR1_AWDSGL | ADC_CFGR1_DISCEN |
mbed_official 205:c41fc65bcfb4 467 ADC_CFGR1_AUTOFF | ADC_CFGR1_WAIT | ADC_CFGR1_CONT | ADC_CFGR1_OVRMOD |
mbed_official 205:c41fc65bcfb4 468 ADC_CFGR1_EXTEN | ADC_CFGR1_EXTSEL | ADC_CFGR1_ALIGN | ADC_CFGR1_RES |
mbed_official 205:c41fc65bcfb4 469 ADC_CFGR1_SCANDIR | ADC_CFGR1_DMACFG | ADC_CFGR1_DMAEN );
mbed_official 205:c41fc65bcfb4 470
mbed_official 205:c41fc65bcfb4 471 /* Reset register CFGR2 */
mbed_official 205:c41fc65bcfb4 472 /* Note: Update of ADC clock mode is conditioned to ADC state disabled: */
mbed_official 205:c41fc65bcfb4 473 /* already done above. */
mbed_official 205:c41fc65bcfb4 474 hadc->Instance->CFGR2 &= ~ADC_CFGR2_CKMODE;
mbed_official 205:c41fc65bcfb4 475
mbed_official 205:c41fc65bcfb4 476 /* Reset register SMPR */
mbed_official 205:c41fc65bcfb4 477 hadc->Instance->SMPR &= ~ADC_SMPR_SMP;
mbed_official 205:c41fc65bcfb4 478
mbed_official 205:c41fc65bcfb4 479 /* Reset register TR1 */
mbed_official 205:c41fc65bcfb4 480 hadc->Instance->TR &= ~(ADC_TR_HT | ADC_TR_LT);
mbed_official 205:c41fc65bcfb4 481
mbed_official 205:c41fc65bcfb4 482 /* Reset register CHSELR */
mbed_official 205:c41fc65bcfb4 483 hadc->Instance->CHSELR &= ~(ADC_CHSELR_CHSEL18 | ADC_CHSELR_CHSEL17 | ADC_CHSELR_CHSEL16 |
mbed_official 205:c41fc65bcfb4 484 ADC_CHSELR_CHSEL15 | ADC_CHSELR_CHSEL14 | ADC_CHSELR_CHSEL13 | ADC_CHSELR_CHSEL12 |
mbed_official 205:c41fc65bcfb4 485 ADC_CHSELR_CHSEL11 | ADC_CHSELR_CHSEL10 | ADC_CHSELR_CHSEL9 | ADC_CHSELR_CHSEL8 |
mbed_official 205:c41fc65bcfb4 486 ADC_CHSELR_CHSEL7 | ADC_CHSELR_CHSEL6 | ADC_CHSELR_CHSEL5 | ADC_CHSELR_CHSEL4 |
mbed_official 205:c41fc65bcfb4 487 ADC_CHSELR_CHSEL3 | ADC_CHSELR_CHSEL2 | ADC_CHSELR_CHSEL1 | ADC_CHSELR_CHSEL0 );
mbed_official 205:c41fc65bcfb4 488
mbed_official 205:c41fc65bcfb4 489 /* Reset register DR */
mbed_official 205:c41fc65bcfb4 490 /* bits in access mode read only, no direct reset applicable*/
mbed_official 205:c41fc65bcfb4 491
mbed_official 205:c41fc65bcfb4 492 /* Reset register CCR */
mbed_official 205:c41fc65bcfb4 493 ADC->CCR &= ~( ADC_CCR_VBATEN |
mbed_official 205:c41fc65bcfb4 494 ADC_CCR_TSEN |
mbed_official 205:c41fc65bcfb4 495 ADC_CCR_VREFEN );
mbed_official 205:c41fc65bcfb4 496
mbed_official 205:c41fc65bcfb4 497 /* ========== Hard reset ADC peripheral ========== */
mbed_official 205:c41fc65bcfb4 498 /* Performs a global reset of the entire ADC peripheral: ADC state is */
mbed_official 205:c41fc65bcfb4 499 /* forced to a similar state after device power-on. */
mbed_official 205:c41fc65bcfb4 500 /* If needed, copy-paste and uncomment the following reset code into */
mbed_official 205:c41fc65bcfb4 501 /* function "void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)": */
mbed_official 205:c41fc65bcfb4 502 /* */
mbed_official 205:c41fc65bcfb4 503 /* __ADC1_FORCE_RESET(); */
mbed_official 205:c41fc65bcfb4 504 /* __ADC1_RELEASE_RESET(); */
mbed_official 205:c41fc65bcfb4 505
mbed_official 205:c41fc65bcfb4 506 /* DeInit the low level hardware */
mbed_official 205:c41fc65bcfb4 507 HAL_ADC_MspDeInit(hadc);
mbed_official 205:c41fc65bcfb4 508
mbed_official 205:c41fc65bcfb4 509 /* Set ADC error code to none */
mbed_official 205:c41fc65bcfb4 510 __HAL_ADC_CLEAR_ERRORCODE(hadc);
mbed_official 205:c41fc65bcfb4 511
mbed_official 205:c41fc65bcfb4 512 /* Change ADC state */
mbed_official 205:c41fc65bcfb4 513 hadc->State = HAL_ADC_STATE_RESET;
mbed_official 205:c41fc65bcfb4 514 }
mbed_official 205:c41fc65bcfb4 515
mbed_official 205:c41fc65bcfb4 516 /* Process unlocked */
mbed_official 205:c41fc65bcfb4 517 __HAL_UNLOCK(hadc);
mbed_official 205:c41fc65bcfb4 518
mbed_official 205:c41fc65bcfb4 519 /* Return function status */
mbed_official 205:c41fc65bcfb4 520 return tmpHALStatus;
mbed_official 205:c41fc65bcfb4 521 }
mbed_official 205:c41fc65bcfb4 522
mbed_official 205:c41fc65bcfb4 523
mbed_official 205:c41fc65bcfb4 524 /**
mbed_official 205:c41fc65bcfb4 525 * @brief Initializes the ADC MSP.
mbed_official 205:c41fc65bcfb4 526 * @param hadc: ADC handle
mbed_official 205:c41fc65bcfb4 527 * @retval None
mbed_official 205:c41fc65bcfb4 528 */
mbed_official 205:c41fc65bcfb4 529 __weak void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
mbed_official 205:c41fc65bcfb4 530 {
mbed_official 205:c41fc65bcfb4 531 /* NOTE : This function should not be modified. When the callback is needed,
mbed_official 205:c41fc65bcfb4 532 function HAL_ADC_MspInit must be implemented in the user file.
mbed_official 205:c41fc65bcfb4 533 */
mbed_official 205:c41fc65bcfb4 534 }
mbed_official 205:c41fc65bcfb4 535
mbed_official 205:c41fc65bcfb4 536 /**
mbed_official 205:c41fc65bcfb4 537 * @brief DeInitializes the ADC MSP.
mbed_official 205:c41fc65bcfb4 538 * @param hadc: ADC handle
mbed_official 205:c41fc65bcfb4 539 * @retval None
mbed_official 205:c41fc65bcfb4 540 */
mbed_official 205:c41fc65bcfb4 541 __weak void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc)
mbed_official 205:c41fc65bcfb4 542 {
mbed_official 205:c41fc65bcfb4 543 /* NOTE : This function should not be modified. When the callback is needed,
mbed_official 205:c41fc65bcfb4 544 function HAL_ADC_MspDeInit must be implemented in the user file.
mbed_official 205:c41fc65bcfb4 545 */
mbed_official 205:c41fc65bcfb4 546 }
mbed_official 205:c41fc65bcfb4 547
mbed_official 205:c41fc65bcfb4 548 /**
mbed_official 205:c41fc65bcfb4 549 * @}
mbed_official 205:c41fc65bcfb4 550 */
mbed_official 205:c41fc65bcfb4 551
mbed_official 205:c41fc65bcfb4 552 /** @defgroup ADC_Group2 IO operation functions
mbed_official 205:c41fc65bcfb4 553 * @brief IO operation functions
mbed_official 205:c41fc65bcfb4 554 *
mbed_official 205:c41fc65bcfb4 555 @verbatim
mbed_official 205:c41fc65bcfb4 556 ===============================================================================
mbed_official 205:c41fc65bcfb4 557 ##### IO operation functions #####
mbed_official 205:c41fc65bcfb4 558 ===============================================================================
mbed_official 205:c41fc65bcfb4 559 [..] This section provides functions allowing to:
mbed_official 205:c41fc65bcfb4 560 (+) Start conversion of regular group.
mbed_official 205:c41fc65bcfb4 561 (+) Stop conversion of regular group.
mbed_official 205:c41fc65bcfb4 562 (+) Poll for conversion complete on regular group.
mbed_official 205:c41fc65bcfb4 563 (+) Poll for conversion event.
mbed_official 205:c41fc65bcfb4 564 (+) Get result of regular channel conversion.
mbed_official 205:c41fc65bcfb4 565 (+) Start conversion of regular group and enable interruptions.
mbed_official 205:c41fc65bcfb4 566 (+) Stop conversion of regular group and disable interruptions.
mbed_official 205:c41fc65bcfb4 567 (+) Handle ADC interrupt request
mbed_official 205:c41fc65bcfb4 568 (+) Start conversion of regular group and enable DMA transfer.
mbed_official 205:c41fc65bcfb4 569 (+) Stop conversion of regular group and disable ADC DMA transfer.
mbed_official 205:c41fc65bcfb4 570 @endverbatim
mbed_official 205:c41fc65bcfb4 571 * @{
mbed_official 205:c41fc65bcfb4 572 */
mbed_official 205:c41fc65bcfb4 573
mbed_official 205:c41fc65bcfb4 574 /**
mbed_official 205:c41fc65bcfb4 575 * @brief Enables ADC, starts conversion of regular group.
mbed_official 205:c41fc65bcfb4 576 * Interruptions enabled in this function: None.
mbed_official 205:c41fc65bcfb4 577 * @param hadc: ADC handle
mbed_official 205:c41fc65bcfb4 578 * @retval HAL status
mbed_official 205:c41fc65bcfb4 579 */
mbed_official 205:c41fc65bcfb4 580 HAL_StatusTypeDef HAL_ADC_Start(ADC_HandleTypeDef* hadc)
mbed_official 205:c41fc65bcfb4 581 {
mbed_official 205:c41fc65bcfb4 582 HAL_StatusTypeDef tmpHALStatus = HAL_OK;
mbed_official 205:c41fc65bcfb4 583
mbed_official 205:c41fc65bcfb4 584 /* Check the parameters */
mbed_official 205:c41fc65bcfb4 585 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
mbed_official 205:c41fc65bcfb4 586
mbed_official 205:c41fc65bcfb4 587 /* Process locked */
mbed_official 205:c41fc65bcfb4 588 __HAL_LOCK(hadc);
mbed_official 205:c41fc65bcfb4 589
mbed_official 205:c41fc65bcfb4 590 /* Enable the ADC peripheral */
mbed_official 205:c41fc65bcfb4 591 /* If low power mode AutoPowerOff is enabled, power-on/off phases are */
mbed_official 205:c41fc65bcfb4 592 /* performed automatically by hardware. */
mbed_official 205:c41fc65bcfb4 593 if (hadc->Init.LowPowerAutoPowerOff != ENABLE)
mbed_official 205:c41fc65bcfb4 594 {
mbed_official 205:c41fc65bcfb4 595 tmpHALStatus = ADC_Enable(hadc);
mbed_official 205:c41fc65bcfb4 596 }
mbed_official 205:c41fc65bcfb4 597
mbed_official 205:c41fc65bcfb4 598 /* Start conversion if ADC is effectively enabled */
mbed_official 205:c41fc65bcfb4 599 if (tmpHALStatus != HAL_ERROR)
mbed_official 205:c41fc65bcfb4 600 {
mbed_official 205:c41fc65bcfb4 601 /* State machine update: Change ADC state */
mbed_official 205:c41fc65bcfb4 602 hadc->State = HAL_ADC_STATE_BUSY_REG;
mbed_official 205:c41fc65bcfb4 603
mbed_official 205:c41fc65bcfb4 604 /* Set ADC error code to none */
mbed_official 205:c41fc65bcfb4 605 __HAL_ADC_CLEAR_ERRORCODE(hadc);
mbed_official 205:c41fc65bcfb4 606
mbed_official 205:c41fc65bcfb4 607 /* Clear regular group conversion flag and overrun flag */
mbed_official 205:c41fc65bcfb4 608 /* (To ensure of no unknown state from potential previous ADC operations) */
mbed_official 205:c41fc65bcfb4 609 __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
mbed_official 205:c41fc65bcfb4 610
mbed_official 205:c41fc65bcfb4 611 /* Enable conversion of regular group. */
mbed_official 205:c41fc65bcfb4 612 /* If software start has been selected, conversion starts immediately. */
mbed_official 205:c41fc65bcfb4 613 /* If external trigger has been selected, conversion will start at next */
mbed_official 205:c41fc65bcfb4 614 /* trigger event. */
mbed_official 205:c41fc65bcfb4 615 hadc->Instance->CR |= ADC_CR_ADSTART;
mbed_official 205:c41fc65bcfb4 616 }
mbed_official 205:c41fc65bcfb4 617
mbed_official 205:c41fc65bcfb4 618 /* Process unlocked */
mbed_official 205:c41fc65bcfb4 619 __HAL_UNLOCK(hadc);
mbed_official 205:c41fc65bcfb4 620
mbed_official 205:c41fc65bcfb4 621 /* Return function status */
mbed_official 205:c41fc65bcfb4 622 return tmpHALStatus;
mbed_official 205:c41fc65bcfb4 623 }
mbed_official 205:c41fc65bcfb4 624
mbed_official 205:c41fc65bcfb4 625 /**
mbed_official 205:c41fc65bcfb4 626 * @brief Stop ADC conversion of regular group, disable ADC peripheral.
mbed_official 205:c41fc65bcfb4 627 * @param hadc: ADC handle
mbed_official 205:c41fc65bcfb4 628 * @retval HAL status.
mbed_official 205:c41fc65bcfb4 629 */
mbed_official 205:c41fc65bcfb4 630 HAL_StatusTypeDef HAL_ADC_Stop(ADC_HandleTypeDef* hadc)
mbed_official 205:c41fc65bcfb4 631 {
mbed_official 205:c41fc65bcfb4 632 HAL_StatusTypeDef tmpHALStatus = HAL_OK;
mbed_official 205:c41fc65bcfb4 633
mbed_official 205:c41fc65bcfb4 634 /* Check the parameters */
mbed_official 205:c41fc65bcfb4 635 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
mbed_official 205:c41fc65bcfb4 636
mbed_official 205:c41fc65bcfb4 637 /* Process locked */
mbed_official 205:c41fc65bcfb4 638 __HAL_LOCK(hadc);
mbed_official 205:c41fc65bcfb4 639
mbed_official 205:c41fc65bcfb4 640 /* 1. Stop potential conversion on going, on regular group */
mbed_official 205:c41fc65bcfb4 641 tmpHALStatus = ADC_ConversionStop(hadc);
mbed_official 205:c41fc65bcfb4 642
mbed_official 205:c41fc65bcfb4 643 /* Disable ADC peripheral if conversions are effectively stopped */
mbed_official 205:c41fc65bcfb4 644 if (tmpHALStatus != HAL_ERROR)
mbed_official 205:c41fc65bcfb4 645 {
mbed_official 205:c41fc65bcfb4 646 /* 2. Disable the ADC peripheral */
mbed_official 205:c41fc65bcfb4 647 tmpHALStatus = ADC_Disable(hadc);
mbed_official 205:c41fc65bcfb4 648
mbed_official 205:c41fc65bcfb4 649 /* Check if ADC is effectively disabled */
mbed_official 205:c41fc65bcfb4 650 if (tmpHALStatus != HAL_ERROR)
mbed_official 205:c41fc65bcfb4 651 {
mbed_official 205:c41fc65bcfb4 652 /* Change ADC state */
mbed_official 205:c41fc65bcfb4 653 hadc->State = HAL_ADC_STATE_READY;
mbed_official 205:c41fc65bcfb4 654 }
mbed_official 205:c41fc65bcfb4 655 }
mbed_official 205:c41fc65bcfb4 656
mbed_official 205:c41fc65bcfb4 657 /* Process unlocked */
mbed_official 205:c41fc65bcfb4 658 __HAL_UNLOCK(hadc);
mbed_official 205:c41fc65bcfb4 659
mbed_official 205:c41fc65bcfb4 660 /* Return function status */
mbed_official 205:c41fc65bcfb4 661 return tmpHALStatus;
mbed_official 205:c41fc65bcfb4 662 }
mbed_official 205:c41fc65bcfb4 663
mbed_official 205:c41fc65bcfb4 664 /**
mbed_official 205:c41fc65bcfb4 665 * @brief Wait for regular group conversion to be completed.
mbed_official 205:c41fc65bcfb4 666 * @param hadc: ADC handle
mbed_official 205:c41fc65bcfb4 667 * @param Timeout: Timeout value in millisecond.
mbed_official 205:c41fc65bcfb4 668 * @retval HAL status
mbed_official 205:c41fc65bcfb4 669 */
mbed_official 205:c41fc65bcfb4 670 HAL_StatusTypeDef HAL_ADC_PollForConversion(ADC_HandleTypeDef* hadc, uint32_t Timeout)
mbed_official 205:c41fc65bcfb4 671 {
mbed_official 205:c41fc65bcfb4 672 uint32_t tickstart;
mbed_official 205:c41fc65bcfb4 673 uint32_t tmp_Flag_EOC;
mbed_official 205:c41fc65bcfb4 674
mbed_official 205:c41fc65bcfb4 675 /* Check the parameters */
mbed_official 205:c41fc65bcfb4 676 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
mbed_official 205:c41fc65bcfb4 677
mbed_official 205:c41fc65bcfb4 678 /* If end of conversion selected to end of sequence */
mbed_official 205:c41fc65bcfb4 679 if (hadc->Init.EOCSelection == EOC_SEQ_CONV)
mbed_official 205:c41fc65bcfb4 680 {
mbed_official 205:c41fc65bcfb4 681 tmp_Flag_EOC = ADC_FLAG_EOS;
mbed_official 205:c41fc65bcfb4 682 }
mbed_official 205:c41fc65bcfb4 683 /* If end of conversion selected to end of each conversion */
mbed_official 205:c41fc65bcfb4 684 else /* EOC_SINGLE_CONV */
mbed_official 205:c41fc65bcfb4 685 {
mbed_official 205:c41fc65bcfb4 686 tmp_Flag_EOC = (ADC_FLAG_EOC | ADC_FLAG_EOS);
mbed_official 205:c41fc65bcfb4 687 }
mbed_official 205:c41fc65bcfb4 688
mbed_official 205:c41fc65bcfb4 689 /* Get timeout */
mbed_official 205:c41fc65bcfb4 690 tickstart = HAL_GetTick();
mbed_official 205:c41fc65bcfb4 691
mbed_official 205:c41fc65bcfb4 692 /* Wait until End of Conversion flag is raised */
mbed_official 205:c41fc65bcfb4 693 while(HAL_IS_BIT_CLR(hadc->Instance->ISR, tmp_Flag_EOC))
mbed_official 205:c41fc65bcfb4 694 {
mbed_official 205:c41fc65bcfb4 695 /* Check if timeout is disabled (set to infinite wait) */
mbed_official 205:c41fc65bcfb4 696 if(Timeout != HAL_MAX_DELAY)
mbed_official 205:c41fc65bcfb4 697 {
mbed_official 205:c41fc65bcfb4 698 if((HAL_GetTick() - tickstart) > Timeout)
mbed_official 205:c41fc65bcfb4 699 {
mbed_official 205:c41fc65bcfb4 700 /* Update ADC state machine to timeout */
mbed_official 205:c41fc65bcfb4 701 hadc->State = HAL_ADC_STATE_TIMEOUT;
mbed_official 205:c41fc65bcfb4 702
mbed_official 205:c41fc65bcfb4 703 /* Process unlocked */
mbed_official 205:c41fc65bcfb4 704 __HAL_UNLOCK(hadc);
mbed_official 205:c41fc65bcfb4 705
mbed_official 205:c41fc65bcfb4 706 return HAL_ERROR;
mbed_official 205:c41fc65bcfb4 707 }
mbed_official 205:c41fc65bcfb4 708 }
mbed_official 205:c41fc65bcfb4 709 }
mbed_official 205:c41fc65bcfb4 710
mbed_official 205:c41fc65bcfb4 711 /* Clear end of conversion flag of regular group if low power feature */
mbed_official 205:c41fc65bcfb4 712 /* "LowPowerAutoWait " is disabled, to not interfere with this feature */
mbed_official 205:c41fc65bcfb4 713 /* until data register is read using function HAL_ADC_GetValue(). */
mbed_official 205:c41fc65bcfb4 714 if (hadc->Init.LowPowerAutoWait == DISABLE)
mbed_official 205:c41fc65bcfb4 715 {
mbed_official 205:c41fc65bcfb4 716 /* Clear regular group conversion flag */
mbed_official 205:c41fc65bcfb4 717 __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS));
mbed_official 205:c41fc65bcfb4 718 }
mbed_official 205:c41fc65bcfb4 719
mbed_official 205:c41fc65bcfb4 720 /* Update state machine on conversion status if not in error state */
mbed_official 205:c41fc65bcfb4 721 if(hadc->State != HAL_ADC_STATE_ERROR)
mbed_official 205:c41fc65bcfb4 722 {
mbed_official 205:c41fc65bcfb4 723 /* Change ADC state */
mbed_official 205:c41fc65bcfb4 724 hadc->State = HAL_ADC_STATE_EOC_REG;
mbed_official 205:c41fc65bcfb4 725 }
mbed_official 205:c41fc65bcfb4 726
mbed_official 205:c41fc65bcfb4 727 /* Return ADC state */
mbed_official 205:c41fc65bcfb4 728 return HAL_OK;
mbed_official 205:c41fc65bcfb4 729 }
mbed_official 205:c41fc65bcfb4 730
mbed_official 205:c41fc65bcfb4 731 /**
mbed_official 205:c41fc65bcfb4 732 * @brief Poll for conversion event.
mbed_official 205:c41fc65bcfb4 733 * @param hadc: ADC handle
mbed_official 205:c41fc65bcfb4 734 * @param EventType: the ADC event type.
mbed_official 205:c41fc65bcfb4 735 * This parameter can be one of the following values:
mbed_official 205:c41fc65bcfb4 736 * @arg AWD_EVENT: ADC Analog watchdog event
mbed_official 205:c41fc65bcfb4 737 * @arg OVR_EVENT: ADC Overrun event
mbed_official 205:c41fc65bcfb4 738 * @param Timeout: Timeout value in millisecond.
mbed_official 205:c41fc65bcfb4 739 * @retval HAL status
mbed_official 205:c41fc65bcfb4 740 */
mbed_official 205:c41fc65bcfb4 741 HAL_StatusTypeDef HAL_ADC_PollForEvent(ADC_HandleTypeDef* hadc, uint32_t EventType, uint32_t Timeout)
mbed_official 205:c41fc65bcfb4 742 {
mbed_official 205:c41fc65bcfb4 743 uint32_t tickstart=0;
mbed_official 205:c41fc65bcfb4 744
mbed_official 205:c41fc65bcfb4 745 /* Check the parameters */
mbed_official 205:c41fc65bcfb4 746 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
mbed_official 205:c41fc65bcfb4 747 assert_param(IS_ADC_EVENT_TYPE(EventType));
mbed_official 205:c41fc65bcfb4 748
mbed_official 205:c41fc65bcfb4 749 tickstart = HAL_GetTick();
mbed_official 205:c41fc65bcfb4 750
mbed_official 205:c41fc65bcfb4 751 /* Check selected event flag */
mbed_official 205:c41fc65bcfb4 752 while(__HAL_ADC_GET_FLAG(hadc, EventType) == RESET)
mbed_official 205:c41fc65bcfb4 753 {
mbed_official 205:c41fc65bcfb4 754 /* Check if timeout is disabled (set to infinite wait) */
mbed_official 205:c41fc65bcfb4 755 if(Timeout != HAL_MAX_DELAY)
mbed_official 205:c41fc65bcfb4 756 {
mbed_official 205:c41fc65bcfb4 757 if((HAL_GetTick() - tickstart) > Timeout)
mbed_official 205:c41fc65bcfb4 758 {
mbed_official 205:c41fc65bcfb4 759 /* Update ADC state machine to timeout */
mbed_official 205:c41fc65bcfb4 760 hadc->State = HAL_ADC_STATE_TIMEOUT;
mbed_official 205:c41fc65bcfb4 761
mbed_official 205:c41fc65bcfb4 762 /* Process unlocked */
mbed_official 205:c41fc65bcfb4 763 __HAL_UNLOCK(hadc);
mbed_official 205:c41fc65bcfb4 764
mbed_official 205:c41fc65bcfb4 765 return HAL_ERROR;
mbed_official 205:c41fc65bcfb4 766 }
mbed_official 205:c41fc65bcfb4 767 }
mbed_official 205:c41fc65bcfb4 768 }
mbed_official 205:c41fc65bcfb4 769
mbed_official 205:c41fc65bcfb4 770
mbed_official 205:c41fc65bcfb4 771 switch(EventType)
mbed_official 205:c41fc65bcfb4 772 {
mbed_official 205:c41fc65bcfb4 773 /* Analog watchdog (level out of window) event */
mbed_official 205:c41fc65bcfb4 774 case AWD_EVENT:
mbed_official 205:c41fc65bcfb4 775 /* Change ADC state */
mbed_official 205:c41fc65bcfb4 776 hadc->State = HAL_ADC_STATE_AWD;
mbed_official 205:c41fc65bcfb4 777
mbed_official 205:c41fc65bcfb4 778 /* Clear ADC analog watchdog flag */
mbed_official 205:c41fc65bcfb4 779 __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD);
mbed_official 205:c41fc65bcfb4 780 break;
mbed_official 205:c41fc65bcfb4 781
mbed_official 205:c41fc65bcfb4 782 /* Overrun event */
mbed_official 205:c41fc65bcfb4 783 default: /* Case OVR_EVENT */
mbed_official 205:c41fc65bcfb4 784 /* If overrun is set to overwrite previous data, overrun event is not */
mbed_official 205:c41fc65bcfb4 785 /* considered as an error. */
mbed_official 205:c41fc65bcfb4 786 /* (cf ref manual "Managing conversions without using the DMA and without */
mbed_official 205:c41fc65bcfb4 787 /* overrun ") */
mbed_official 205:c41fc65bcfb4 788 if (hadc->Init.Overrun == OVR_DATA_PRESERVED)
mbed_official 205:c41fc65bcfb4 789 {
mbed_official 205:c41fc65bcfb4 790 /* Change ADC state */
mbed_official 205:c41fc65bcfb4 791 hadc->State = HAL_ADC_STATE_ERROR;
mbed_official 205:c41fc65bcfb4 792
mbed_official 205:c41fc65bcfb4 793 /* Set ADC error code to overrun */
mbed_official 205:c41fc65bcfb4 794 hadc->ErrorCode |= HAL_ADC_ERROR_OVR;
mbed_official 205:c41fc65bcfb4 795 }
mbed_official 205:c41fc65bcfb4 796
mbed_official 205:c41fc65bcfb4 797 /* Clear ADC Overrun flag */
mbed_official 205:c41fc65bcfb4 798 __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
mbed_official 205:c41fc65bcfb4 799 break;
mbed_official 205:c41fc65bcfb4 800 }
mbed_official 205:c41fc65bcfb4 801
mbed_official 205:c41fc65bcfb4 802 /* Return ADC state */
mbed_official 205:c41fc65bcfb4 803 return HAL_OK;
mbed_official 205:c41fc65bcfb4 804 }
mbed_official 205:c41fc65bcfb4 805
mbed_official 205:c41fc65bcfb4 806 /**
mbed_official 205:c41fc65bcfb4 807 * @brief Enables ADC, starts conversion of regular group with interruption.
mbed_official 205:c41fc65bcfb4 808 * Interruptions enabled in this function: EOC (end of conversion),
mbed_official 205:c41fc65bcfb4 809 * overrun.
mbed_official 205:c41fc65bcfb4 810 * Each of these interruptions has its dedicated callback function.
mbed_official 205:c41fc65bcfb4 811 * @param hadc: ADC handle
mbed_official 205:c41fc65bcfb4 812 * @retval HAL status
mbed_official 205:c41fc65bcfb4 813 */
mbed_official 205:c41fc65bcfb4 814 HAL_StatusTypeDef HAL_ADC_Start_IT(ADC_HandleTypeDef* hadc)
mbed_official 205:c41fc65bcfb4 815 {
mbed_official 205:c41fc65bcfb4 816 HAL_StatusTypeDef tmpHALStatus = HAL_OK;
mbed_official 205:c41fc65bcfb4 817
mbed_official 205:c41fc65bcfb4 818 /* Check the parameters */
mbed_official 205:c41fc65bcfb4 819 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
mbed_official 205:c41fc65bcfb4 820
mbed_official 205:c41fc65bcfb4 821 /* Process locked */
mbed_official 205:c41fc65bcfb4 822 __HAL_LOCK(hadc);
mbed_official 205:c41fc65bcfb4 823
mbed_official 205:c41fc65bcfb4 824 /* Enable the ADC peripheral */
mbed_official 205:c41fc65bcfb4 825 /* If low power mode AutoPowerOff is enabled, power-on/off phases are */
mbed_official 205:c41fc65bcfb4 826 /* performed automatically by hardware. */
mbed_official 205:c41fc65bcfb4 827 if (hadc->Init.LowPowerAutoPowerOff != ENABLE)
mbed_official 205:c41fc65bcfb4 828 {
mbed_official 205:c41fc65bcfb4 829 tmpHALStatus = ADC_Enable(hadc);
mbed_official 205:c41fc65bcfb4 830 }
mbed_official 205:c41fc65bcfb4 831
mbed_official 205:c41fc65bcfb4 832 /* Start conversion if ADC is effectively enabled */
mbed_official 205:c41fc65bcfb4 833 if (tmpHALStatus != HAL_ERROR)
mbed_official 205:c41fc65bcfb4 834 {
mbed_official 205:c41fc65bcfb4 835 /* State machine update: Change ADC state */
mbed_official 205:c41fc65bcfb4 836 hadc->State = HAL_ADC_STATE_BUSY_REG;
mbed_official 205:c41fc65bcfb4 837
mbed_official 205:c41fc65bcfb4 838 /* Set ADC error code to none */
mbed_official 205:c41fc65bcfb4 839 __HAL_ADC_CLEAR_ERRORCODE(hadc);
mbed_official 205:c41fc65bcfb4 840
mbed_official 205:c41fc65bcfb4 841 /* Clear regular group conversion flag and overrun flag */
mbed_official 205:c41fc65bcfb4 842 /* (To ensure of no unknown state from potential previous ADC operations) */
mbed_official 205:c41fc65bcfb4 843 __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
mbed_official 205:c41fc65bcfb4 844
mbed_official 205:c41fc65bcfb4 845 /* Enable ADC end of conversion interrupt */
mbed_official 205:c41fc65bcfb4 846 /* Enable ADC overrun interrupt */
mbed_official 205:c41fc65bcfb4 847 switch(hadc->Init.EOCSelection)
mbed_official 205:c41fc65bcfb4 848 {
mbed_official 205:c41fc65bcfb4 849 case EOC_SEQ_CONV:
mbed_official 205:c41fc65bcfb4 850 __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC);
mbed_official 205:c41fc65bcfb4 851 __HAL_ADC_ENABLE_IT(hadc, (ADC_IT_EOS | ADC_IT_OVR));
mbed_official 205:c41fc65bcfb4 852 break;
mbed_official 205:c41fc65bcfb4 853 /* case EOC_SINGLE_CONV */
mbed_official 205:c41fc65bcfb4 854 default:
mbed_official 205:c41fc65bcfb4 855 __HAL_ADC_ENABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_EOS | ADC_IT_OVR));
mbed_official 205:c41fc65bcfb4 856 break;
mbed_official 205:c41fc65bcfb4 857 }
mbed_official 205:c41fc65bcfb4 858
mbed_official 205:c41fc65bcfb4 859 /* Enable conversion of regular group. */
mbed_official 205:c41fc65bcfb4 860 /* If software start has been selected, conversion starts immediately. */
mbed_official 205:c41fc65bcfb4 861 /* If external trigger has been selected, conversion will start at next */
mbed_official 205:c41fc65bcfb4 862 /* trigger event. */
mbed_official 205:c41fc65bcfb4 863 hadc->Instance->CR |= ADC_CR_ADSTART;
mbed_official 205:c41fc65bcfb4 864 }
mbed_official 205:c41fc65bcfb4 865
mbed_official 205:c41fc65bcfb4 866 /* Process unlocked */
mbed_official 205:c41fc65bcfb4 867 __HAL_UNLOCK(hadc);
mbed_official 205:c41fc65bcfb4 868
mbed_official 205:c41fc65bcfb4 869 /* Return function status */
mbed_official 205:c41fc65bcfb4 870 return tmpHALStatus;
mbed_official 205:c41fc65bcfb4 871 }
mbed_official 205:c41fc65bcfb4 872
mbed_official 205:c41fc65bcfb4 873
mbed_official 205:c41fc65bcfb4 874 /**
mbed_official 205:c41fc65bcfb4 875 * @brief Stop ADC conversion of regular group, disable interruption of
mbed_official 205:c41fc65bcfb4 876 * end-of-conversion, disable ADC peripheral.
mbed_official 205:c41fc65bcfb4 877 * @param hadc: ADC handle
mbed_official 205:c41fc65bcfb4 878 * @retval HAL status.
mbed_official 205:c41fc65bcfb4 879 */
mbed_official 205:c41fc65bcfb4 880 HAL_StatusTypeDef HAL_ADC_Stop_IT(ADC_HandleTypeDef* hadc)
mbed_official 205:c41fc65bcfb4 881 {
mbed_official 205:c41fc65bcfb4 882 HAL_StatusTypeDef tmpHALStatus = HAL_OK;
mbed_official 205:c41fc65bcfb4 883
mbed_official 205:c41fc65bcfb4 884 /* Check the parameters */
mbed_official 205:c41fc65bcfb4 885 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
mbed_official 205:c41fc65bcfb4 886
mbed_official 205:c41fc65bcfb4 887 /* Process locked */
mbed_official 205:c41fc65bcfb4 888 __HAL_LOCK(hadc);
mbed_official 205:c41fc65bcfb4 889
mbed_official 205:c41fc65bcfb4 890 /* 1. Stop potential conversion on going, on regular group */
mbed_official 205:c41fc65bcfb4 891 tmpHALStatus = ADC_ConversionStop(hadc);
mbed_official 205:c41fc65bcfb4 892
mbed_official 205:c41fc65bcfb4 893 /* Disable ADC peripheral if conversions are effectively stopped */
mbed_official 205:c41fc65bcfb4 894 if (tmpHALStatus != HAL_ERROR)
mbed_official 205:c41fc65bcfb4 895 {
mbed_official 205:c41fc65bcfb4 896 /* Disable ADC end of conversion interrupt for regular group */
mbed_official 205:c41fc65bcfb4 897 /* Disable ADC overrun interrupt */
mbed_official 205:c41fc65bcfb4 898 __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_EOS | ADC_IT_OVR));
mbed_official 205:c41fc65bcfb4 899
mbed_official 205:c41fc65bcfb4 900 /* 2. Disable the ADC peripheral */
mbed_official 205:c41fc65bcfb4 901 tmpHALStatus = ADC_Disable(hadc);
mbed_official 205:c41fc65bcfb4 902
mbed_official 205:c41fc65bcfb4 903 /* Check if ADC is effectively disabled */
mbed_official 205:c41fc65bcfb4 904 if (tmpHALStatus != HAL_ERROR)
mbed_official 205:c41fc65bcfb4 905 {
mbed_official 205:c41fc65bcfb4 906 /* Change ADC state */
mbed_official 205:c41fc65bcfb4 907 hadc->State = HAL_ADC_STATE_READY;
mbed_official 205:c41fc65bcfb4 908 }
mbed_official 205:c41fc65bcfb4 909 }
mbed_official 205:c41fc65bcfb4 910
mbed_official 205:c41fc65bcfb4 911 /* Process unlocked */
mbed_official 205:c41fc65bcfb4 912 __HAL_UNLOCK(hadc);
mbed_official 205:c41fc65bcfb4 913
mbed_official 205:c41fc65bcfb4 914 /* Return function status */
mbed_official 205:c41fc65bcfb4 915 return tmpHALStatus;
mbed_official 205:c41fc65bcfb4 916 }
mbed_official 205:c41fc65bcfb4 917
mbed_official 205:c41fc65bcfb4 918 /**
mbed_official 205:c41fc65bcfb4 919 * @brief Enables ADC, starts conversion of regular group and transfers result
mbed_official 205:c41fc65bcfb4 920 * through DMA.
mbed_official 205:c41fc65bcfb4 921 * Interruptions enabled in this function:
mbed_official 205:c41fc65bcfb4 922 * overrun, DMA half transfer, DMA transfer complete.
mbed_official 205:c41fc65bcfb4 923 * Each of these interruptions has its dedicated callback function.
mbed_official 205:c41fc65bcfb4 924 * @param hadc: ADC handle
mbed_official 205:c41fc65bcfb4 925 * @param pData: The destination Buffer address.
mbed_official 205:c41fc65bcfb4 926 * @param Length: The length of data to be transferred from ADC peripheral to memory.
mbed_official 205:c41fc65bcfb4 927 * @retval None
mbed_official 205:c41fc65bcfb4 928 */
mbed_official 205:c41fc65bcfb4 929 HAL_StatusTypeDef HAL_ADC_Start_DMA(ADC_HandleTypeDef* hadc, uint32_t* pData, uint32_t Length)
mbed_official 205:c41fc65bcfb4 930 {
mbed_official 205:c41fc65bcfb4 931 HAL_StatusTypeDef tmpHALStatus = HAL_OK;
mbed_official 205:c41fc65bcfb4 932
mbed_official 205:c41fc65bcfb4 933 /* Check the parameters */
mbed_official 205:c41fc65bcfb4 934 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
mbed_official 205:c41fc65bcfb4 935
mbed_official 205:c41fc65bcfb4 936 /* Process locked */
mbed_official 205:c41fc65bcfb4 937 __HAL_LOCK(hadc);
mbed_official 205:c41fc65bcfb4 938
mbed_official 205:c41fc65bcfb4 939 /* Enable the ADC peripheral */
mbed_official 205:c41fc65bcfb4 940 /* If low power mode AutoPowerOff is enabled, power-on/off phases are */
mbed_official 205:c41fc65bcfb4 941 /* performed automatically by hardware. */
mbed_official 205:c41fc65bcfb4 942 if (hadc->Init.LowPowerAutoPowerOff != ENABLE)
mbed_official 205:c41fc65bcfb4 943 {
mbed_official 205:c41fc65bcfb4 944 tmpHALStatus = ADC_Enable(hadc);
mbed_official 205:c41fc65bcfb4 945 }
mbed_official 205:c41fc65bcfb4 946
mbed_official 205:c41fc65bcfb4 947 /* Start conversion if ADC is effectively enabled */
mbed_official 205:c41fc65bcfb4 948 if (tmpHALStatus != HAL_ERROR)
mbed_official 205:c41fc65bcfb4 949 {
mbed_official 205:c41fc65bcfb4 950 /* State machine update: Change ADC state */
mbed_official 205:c41fc65bcfb4 951 hadc->State = HAL_ADC_STATE_BUSY_REG;
mbed_official 205:c41fc65bcfb4 952
mbed_official 205:c41fc65bcfb4 953 /* Set ADC error code to none */
mbed_official 205:c41fc65bcfb4 954 __HAL_ADC_CLEAR_ERRORCODE(hadc);
mbed_official 205:c41fc65bcfb4 955
mbed_official 205:c41fc65bcfb4 956
mbed_official 205:c41fc65bcfb4 957 /* Set the DMA transfer complete callback */
mbed_official 205:c41fc65bcfb4 958 hadc->DMA_Handle->XferCpltCallback = ADC_DMAConvCplt;
mbed_official 205:c41fc65bcfb4 959
mbed_official 205:c41fc65bcfb4 960 /* Set the DMA half transfer complete callback */
mbed_official 205:c41fc65bcfb4 961 hadc->DMA_Handle->XferHalfCpltCallback = ADC_DMAHalfConvCplt;
mbed_official 205:c41fc65bcfb4 962
mbed_official 205:c41fc65bcfb4 963 /* Set the DMA error callback */
mbed_official 205:c41fc65bcfb4 964 hadc->DMA_Handle->XferErrorCallback = ADC_DMAError;
mbed_official 205:c41fc65bcfb4 965
mbed_official 205:c41fc65bcfb4 966
mbed_official 205:c41fc65bcfb4 967 /* Manage ADC and DMA start: ADC overrun interruption, DMA start, ADC */
mbed_official 205:c41fc65bcfb4 968 /* start (in case of SW start): */
mbed_official 205:c41fc65bcfb4 969
mbed_official 205:c41fc65bcfb4 970 /* Clear regular group conversion flag and overrun flag */
mbed_official 205:c41fc65bcfb4 971 /* (To ensure of no unknown state from potential previous ADC */
mbed_official 205:c41fc65bcfb4 972 /* operations) */
mbed_official 205:c41fc65bcfb4 973 __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
mbed_official 205:c41fc65bcfb4 974
mbed_official 205:c41fc65bcfb4 975 /* Enable ADC overrun interrupt */
mbed_official 205:c41fc65bcfb4 976 __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);
mbed_official 205:c41fc65bcfb4 977
mbed_official 205:c41fc65bcfb4 978 /* Enable ADC DMA mode */
mbed_official 205:c41fc65bcfb4 979 hadc->Instance->CFGR1 |= ADC_CFGR1_DMAEN;
mbed_official 205:c41fc65bcfb4 980
mbed_official 205:c41fc65bcfb4 981 /* Start the DMA channel */
mbed_official 205:c41fc65bcfb4 982 HAL_DMA_Start_IT(hadc->DMA_Handle, (uint32_t)&hadc->Instance->DR, (uint32_t)pData, Length);
mbed_official 205:c41fc65bcfb4 983
mbed_official 205:c41fc65bcfb4 984 /* Enable conversion of regular group. */
mbed_official 205:c41fc65bcfb4 985 /* If software start has been selected, conversion starts immediately. */
mbed_official 205:c41fc65bcfb4 986 /* If external trigger has been selected, conversion will start at next */
mbed_official 205:c41fc65bcfb4 987 /* trigger event. */
mbed_official 205:c41fc65bcfb4 988 hadc->Instance->CR |= ADC_CR_ADSTART;
mbed_official 205:c41fc65bcfb4 989 }
mbed_official 205:c41fc65bcfb4 990
mbed_official 205:c41fc65bcfb4 991
mbed_official 205:c41fc65bcfb4 992 /* Process unlocked */
mbed_official 205:c41fc65bcfb4 993 __HAL_UNLOCK(hadc);
mbed_official 205:c41fc65bcfb4 994
mbed_official 205:c41fc65bcfb4 995 /* Return function status */
mbed_official 205:c41fc65bcfb4 996 return tmpHALStatus;
mbed_official 205:c41fc65bcfb4 997 }
mbed_official 205:c41fc65bcfb4 998
mbed_official 205:c41fc65bcfb4 999 /**
mbed_official 205:c41fc65bcfb4 1000 * @brief Stop ADC conversion of regular group, disable ADC DMA transfer, disable
mbed_official 205:c41fc65bcfb4 1001 * ADC peripheral.
mbed_official 205:c41fc65bcfb4 1002 * Each of these interruptions has its dedicated callback function.
mbed_official 205:c41fc65bcfb4 1003 * @param hadc: ADC handle
mbed_official 205:c41fc65bcfb4 1004 * @retval HAL status.
mbed_official 205:c41fc65bcfb4 1005 */
mbed_official 205:c41fc65bcfb4 1006 HAL_StatusTypeDef HAL_ADC_Stop_DMA(ADC_HandleTypeDef* hadc)
mbed_official 205:c41fc65bcfb4 1007 {
mbed_official 205:c41fc65bcfb4 1008 HAL_StatusTypeDef tmpHALStatus = HAL_OK;
mbed_official 205:c41fc65bcfb4 1009
mbed_official 205:c41fc65bcfb4 1010 /* Check the parameters */
mbed_official 205:c41fc65bcfb4 1011 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
mbed_official 205:c41fc65bcfb4 1012
mbed_official 205:c41fc65bcfb4 1013 /* Process locked */
mbed_official 205:c41fc65bcfb4 1014 __HAL_LOCK(hadc);
mbed_official 205:c41fc65bcfb4 1015
mbed_official 205:c41fc65bcfb4 1016 /* 1. Stop potential conversion on going, on regular group */
mbed_official 205:c41fc65bcfb4 1017 tmpHALStatus = ADC_ConversionStop(hadc);
mbed_official 205:c41fc65bcfb4 1018
mbed_official 205:c41fc65bcfb4 1019 /* Disable ADC peripheral if conversions are effectively stopped */
mbed_official 205:c41fc65bcfb4 1020 if (tmpHALStatus != HAL_ERROR)
mbed_official 205:c41fc65bcfb4 1021 {
mbed_official 205:c41fc65bcfb4 1022 /* Disable ADC DMA (ADC DMA configuration ADC_CFGR_DMACFG is kept) */
mbed_official 205:c41fc65bcfb4 1023 hadc->Instance->CFGR1 &= ~ADC_CFGR1_DMAEN;
mbed_official 205:c41fc65bcfb4 1024
mbed_official 205:c41fc65bcfb4 1025 /* Disable the DMA channel (in case of DMA in circular mode or stop while */
mbed_official 205:c41fc65bcfb4 1026 /* while DMA transfer is on going) */
mbed_official 205:c41fc65bcfb4 1027 tmpHALStatus = HAL_DMA_Abort(hadc->DMA_Handle);
mbed_official 205:c41fc65bcfb4 1028
mbed_official 205:c41fc65bcfb4 1029 /* Check if DMA channel effectively disabled */
mbed_official 205:c41fc65bcfb4 1030 if (tmpHALStatus != HAL_OK)
mbed_official 205:c41fc65bcfb4 1031 {
mbed_official 205:c41fc65bcfb4 1032 /* Update ADC state machine to error */
mbed_official 205:c41fc65bcfb4 1033 hadc->State = HAL_ADC_STATE_ERROR;
mbed_official 205:c41fc65bcfb4 1034 }
mbed_official 205:c41fc65bcfb4 1035
mbed_official 205:c41fc65bcfb4 1036 /* Disable ADC overrun interrupt */
mbed_official 205:c41fc65bcfb4 1037 __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
mbed_official 205:c41fc65bcfb4 1038
mbed_official 205:c41fc65bcfb4 1039 /* 2. Disable the ADC peripheral */
mbed_official 205:c41fc65bcfb4 1040 /* Update "tmpHALStatus" only if DMA channel disabling passed, to keep in */
mbed_official 205:c41fc65bcfb4 1041 /* memory a potential failing status. */
mbed_official 205:c41fc65bcfb4 1042 if (tmpHALStatus == HAL_OK)
mbed_official 205:c41fc65bcfb4 1043 {
mbed_official 205:c41fc65bcfb4 1044 tmpHALStatus = ADC_Disable(hadc);
mbed_official 205:c41fc65bcfb4 1045 }
mbed_official 205:c41fc65bcfb4 1046 else
mbed_official 205:c41fc65bcfb4 1047 {
mbed_official 205:c41fc65bcfb4 1048 ADC_Disable(hadc);
mbed_official 205:c41fc65bcfb4 1049 }
mbed_official 205:c41fc65bcfb4 1050
mbed_official 205:c41fc65bcfb4 1051 /* Check if ADC is effectively disabled */
mbed_official 205:c41fc65bcfb4 1052 if (tmpHALStatus != HAL_OK)
mbed_official 205:c41fc65bcfb4 1053 {
mbed_official 205:c41fc65bcfb4 1054 /* Change ADC state */
mbed_official 205:c41fc65bcfb4 1055 hadc->State = HAL_ADC_STATE_READY;
mbed_official 205:c41fc65bcfb4 1056 }
mbed_official 205:c41fc65bcfb4 1057
mbed_official 205:c41fc65bcfb4 1058 }
mbed_official 205:c41fc65bcfb4 1059
mbed_official 205:c41fc65bcfb4 1060 /* Process unlocked */
mbed_official 205:c41fc65bcfb4 1061 __HAL_UNLOCK(hadc);
mbed_official 205:c41fc65bcfb4 1062
mbed_official 205:c41fc65bcfb4 1063 /* Return function status */
mbed_official 205:c41fc65bcfb4 1064 return tmpHALStatus;
mbed_official 205:c41fc65bcfb4 1065 }
mbed_official 205:c41fc65bcfb4 1066
mbed_official 205:c41fc65bcfb4 1067 /**
mbed_official 205:c41fc65bcfb4 1068 * @brief Get ADC regular group conversion result.
mbed_official 205:c41fc65bcfb4 1069 * @param hadc: ADC handle
mbed_official 205:c41fc65bcfb4 1070 * @retval Converted value
mbed_official 205:c41fc65bcfb4 1071 */
mbed_official 205:c41fc65bcfb4 1072 uint32_t HAL_ADC_GetValue(ADC_HandleTypeDef* hadc)
mbed_official 205:c41fc65bcfb4 1073 {
mbed_official 205:c41fc65bcfb4 1074 /* Check the parameters */
mbed_official 205:c41fc65bcfb4 1075 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
mbed_official 205:c41fc65bcfb4 1076
mbed_official 205:c41fc65bcfb4 1077 /* Note: EOC flag is automatically cleared by hardware when reading */
mbed_official 205:c41fc65bcfb4 1078 /* register DR. Additionally, clear flag EOS by software. */
mbed_official 205:c41fc65bcfb4 1079
mbed_official 205:c41fc65bcfb4 1080 /* Clear regular group conversion flag */
mbed_official 205:c41fc65bcfb4 1081 __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS) );
mbed_official 205:c41fc65bcfb4 1082
mbed_official 205:c41fc65bcfb4 1083 /* Return ADC converted value */
mbed_official 205:c41fc65bcfb4 1084 return hadc->Instance->DR;
mbed_official 205:c41fc65bcfb4 1085 }
mbed_official 205:c41fc65bcfb4 1086
mbed_official 205:c41fc65bcfb4 1087 /**
mbed_official 205:c41fc65bcfb4 1088 * @brief DMA transfer complete callback.
mbed_official 205:c41fc65bcfb4 1089 * @param hdma: pointer to DMA handle.
mbed_official 205:c41fc65bcfb4 1090 * @retval None
mbed_official 205:c41fc65bcfb4 1091 */
mbed_official 205:c41fc65bcfb4 1092 static void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma)
mbed_official 205:c41fc65bcfb4 1093 {
mbed_official 205:c41fc65bcfb4 1094 /* Retrieve ADC handle corresponding to current DMA handle */
mbed_official 205:c41fc65bcfb4 1095 ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
mbed_official 205:c41fc65bcfb4 1096
mbed_official 205:c41fc65bcfb4 1097 /* Update state machine on conversion status if not in error state */
mbed_official 205:c41fc65bcfb4 1098 if(hadc->State != HAL_ADC_STATE_ERROR)
mbed_official 205:c41fc65bcfb4 1099 {
mbed_official 205:c41fc65bcfb4 1100 /* Change ADC state */
mbed_official 205:c41fc65bcfb4 1101 hadc->State = HAL_ADC_STATE_EOC_REG;
mbed_official 205:c41fc65bcfb4 1102 }
mbed_official 205:c41fc65bcfb4 1103
mbed_official 205:c41fc65bcfb4 1104 /* Conversion complete callback */
mbed_official 205:c41fc65bcfb4 1105 HAL_ADC_ConvCpltCallback(hadc);
mbed_official 205:c41fc65bcfb4 1106 }
mbed_official 205:c41fc65bcfb4 1107
mbed_official 205:c41fc65bcfb4 1108 /**
mbed_official 205:c41fc65bcfb4 1109 * @brief DMA half transfer complete callback.
mbed_official 205:c41fc65bcfb4 1110 * @param hdma: pointer to DMA handle.
mbed_official 205:c41fc65bcfb4 1111 * @retval None
mbed_official 205:c41fc65bcfb4 1112 */
mbed_official 205:c41fc65bcfb4 1113 static void ADC_DMAHalfConvCplt(DMA_HandleTypeDef *hdma)
mbed_official 205:c41fc65bcfb4 1114 {
mbed_official 205:c41fc65bcfb4 1115 /* Retrieve ADC handle corresponding to current DMA handle */
mbed_official 205:c41fc65bcfb4 1116 ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
mbed_official 205:c41fc65bcfb4 1117
mbed_official 205:c41fc65bcfb4 1118 /* Half conversion callback */
mbed_official 205:c41fc65bcfb4 1119 HAL_ADC_ConvHalfCpltCallback(hadc);
mbed_official 205:c41fc65bcfb4 1120 }
mbed_official 205:c41fc65bcfb4 1121
mbed_official 205:c41fc65bcfb4 1122 /**
mbed_official 205:c41fc65bcfb4 1123 * @brief DMA error callback
mbed_official 205:c41fc65bcfb4 1124 * @param hdma: pointer to DMA handle.
mbed_official 205:c41fc65bcfb4 1125 * @retval None
mbed_official 205:c41fc65bcfb4 1126 */
mbed_official 205:c41fc65bcfb4 1127 static void ADC_DMAError(DMA_HandleTypeDef *hdma)
mbed_official 205:c41fc65bcfb4 1128 {
mbed_official 205:c41fc65bcfb4 1129 /* Retrieve ADC handle corresponding to current DMA handle */
mbed_official 205:c41fc65bcfb4 1130 ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
mbed_official 205:c41fc65bcfb4 1131
mbed_official 205:c41fc65bcfb4 1132 /* Change ADC state */
mbed_official 205:c41fc65bcfb4 1133 hadc->State = HAL_ADC_STATE_ERROR;
mbed_official 205:c41fc65bcfb4 1134
mbed_official 205:c41fc65bcfb4 1135 /* Set ADC error code to DMA error */
mbed_official 205:c41fc65bcfb4 1136 hadc->ErrorCode |= HAL_ADC_ERROR_DMA;
mbed_official 205:c41fc65bcfb4 1137
mbed_official 205:c41fc65bcfb4 1138 /* Error callback */
mbed_official 205:c41fc65bcfb4 1139 HAL_ADC_ErrorCallback(hadc);
mbed_official 205:c41fc65bcfb4 1140 }
mbed_official 205:c41fc65bcfb4 1141
mbed_official 205:c41fc65bcfb4 1142 /**
mbed_official 205:c41fc65bcfb4 1143 * @brief Handles ADC interrupt request.
mbed_official 205:c41fc65bcfb4 1144 * @param hadc: ADC handle
mbed_official 205:c41fc65bcfb4 1145 * @retval None
mbed_official 205:c41fc65bcfb4 1146 */
mbed_official 205:c41fc65bcfb4 1147 void HAL_ADC_IRQHandler(ADC_HandleTypeDef* hadc)
mbed_official 205:c41fc65bcfb4 1148 {
mbed_official 205:c41fc65bcfb4 1149 /* Check the parameters */
mbed_official 205:c41fc65bcfb4 1150 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
mbed_official 205:c41fc65bcfb4 1151 assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
mbed_official 205:c41fc65bcfb4 1152 assert_param(IS_ADC_EOC_SELECTION(hadc->Init.EOCSelection));
mbed_official 205:c41fc65bcfb4 1153
mbed_official 205:c41fc65bcfb4 1154 /* ========== Check End of Conversion flag for regular group ========== */
mbed_official 205:c41fc65bcfb4 1155 if( (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOC) && __HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_EOC)) ||
mbed_official 205:c41fc65bcfb4 1156 (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOS) && __HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_EOS)) )
mbed_official 205:c41fc65bcfb4 1157 {
mbed_official 205:c41fc65bcfb4 1158 /* Update state machine on conversion status if not in error state */
mbed_official 205:c41fc65bcfb4 1159 if(hadc->State != HAL_ADC_STATE_ERROR)
mbed_official 205:c41fc65bcfb4 1160 {
mbed_official 205:c41fc65bcfb4 1161 /* Change ADC state */
mbed_official 205:c41fc65bcfb4 1162 hadc->State = HAL_ADC_STATE_EOC_REG;
mbed_official 205:c41fc65bcfb4 1163 }
mbed_official 205:c41fc65bcfb4 1164
mbed_official 205:c41fc65bcfb4 1165 /* Disable interruption if no further conversion upcoming by regular */
mbed_official 205:c41fc65bcfb4 1166 /* external trigger or by continuous mode, */
mbed_official 205:c41fc65bcfb4 1167 /* and if scan sequence if completed. */
mbed_official 205:c41fc65bcfb4 1168 if(__HAL_ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
mbed_official 205:c41fc65bcfb4 1169 (hadc->Init.ContinuousConvMode == DISABLE) )
mbed_official 205:c41fc65bcfb4 1170 {
mbed_official 205:c41fc65bcfb4 1171 /* If End of Sequence is reached, disable interrupts */
mbed_official 205:c41fc65bcfb4 1172 if( __HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOS) )
mbed_official 205:c41fc65bcfb4 1173 {
mbed_official 205:c41fc65bcfb4 1174 /* Allowed to modify bits ADC_IT_EOC/ADC_IT_EOS only if bit */
mbed_official 205:c41fc65bcfb4 1175 /* ADSTART==0 (no conversion on going) */
mbed_official 205:c41fc65bcfb4 1176 if (__HAL_ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
mbed_official 205:c41fc65bcfb4 1177 {
mbed_official 205:c41fc65bcfb4 1178 /* Disable ADC end of sequence conversion interrupt */
mbed_official 205:c41fc65bcfb4 1179 /* Note: Overrun interrupt was enabled with EOC interrupt in */
mbed_official 205:c41fc65bcfb4 1180 /* HAL_Start_IT(), but is not disabled here because can be used */
mbed_official 205:c41fc65bcfb4 1181 /* by overrun IRQ process below. */
mbed_official 205:c41fc65bcfb4 1182 __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC | ADC_IT_EOS);
mbed_official 205:c41fc65bcfb4 1183 }
mbed_official 205:c41fc65bcfb4 1184 else
mbed_official 205:c41fc65bcfb4 1185 {
mbed_official 205:c41fc65bcfb4 1186 /* Change ADC state to error state */
mbed_official 205:c41fc65bcfb4 1187 hadc->State = HAL_ADC_STATE_ERROR;
mbed_official 205:c41fc65bcfb4 1188
mbed_official 205:c41fc65bcfb4 1189 /* Set ADC error code to ADC IP internal error */
mbed_official 205:c41fc65bcfb4 1190 hadc->ErrorCode |= HAL_ADC_ERROR_INTERNAL;
mbed_official 205:c41fc65bcfb4 1191 }
mbed_official 205:c41fc65bcfb4 1192 }
mbed_official 205:c41fc65bcfb4 1193 }
mbed_official 205:c41fc65bcfb4 1194
mbed_official 205:c41fc65bcfb4 1195 /* Conversion complete callback */
mbed_official 205:c41fc65bcfb4 1196 /* Note: into callback, to determine if conversion has been triggered */
mbed_official 205:c41fc65bcfb4 1197 /* from EOC or EOS, possibility to use: */
mbed_official 205:c41fc65bcfb4 1198 /* " if( __HAL_ADC_GET_FLAG(&hadc, ADC_FLAG_EOS)) " */
mbed_official 205:c41fc65bcfb4 1199 HAL_ADC_ConvCpltCallback(hadc);
mbed_official 205:c41fc65bcfb4 1200
mbed_official 205:c41fc65bcfb4 1201
mbed_official 205:c41fc65bcfb4 1202 /* Clear regular group conversion flag */
mbed_official 205:c41fc65bcfb4 1203 /* Note: in case of overrun set to OVR_DATA_PRESERVED, end of conversion */
mbed_official 205:c41fc65bcfb4 1204 /* flags clear induces the release of the preserved data. */
mbed_official 205:c41fc65bcfb4 1205 /* Therefore, if the preserved data value is needed, it must be */
mbed_official 205:c41fc65bcfb4 1206 /* read preliminarily into HAL_ADC_ConvCpltCallback(). */
mbed_official 205:c41fc65bcfb4 1207 __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS) );
mbed_official 205:c41fc65bcfb4 1208 }
mbed_official 205:c41fc65bcfb4 1209
mbed_official 205:c41fc65bcfb4 1210 /* ========== Check Analog watchdog flags ========== */
mbed_official 205:c41fc65bcfb4 1211 if(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_AWD) && __HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_AWD))
mbed_official 205:c41fc65bcfb4 1212 {
mbed_official 205:c41fc65bcfb4 1213 /* Change ADC state */
mbed_official 205:c41fc65bcfb4 1214 hadc->State = HAL_ADC_STATE_AWD;
mbed_official 205:c41fc65bcfb4 1215
mbed_official 205:c41fc65bcfb4 1216 /* Clear ADC Analog watchdog flag */
mbed_official 205:c41fc65bcfb4 1217 __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD);
mbed_official 205:c41fc65bcfb4 1218
mbed_official 205:c41fc65bcfb4 1219 /* Level out of window callback */
mbed_official 205:c41fc65bcfb4 1220 HAL_ADC_LevelOutOfWindowCallback(hadc);
mbed_official 205:c41fc65bcfb4 1221 }
mbed_official 205:c41fc65bcfb4 1222
mbed_official 205:c41fc65bcfb4 1223
mbed_official 205:c41fc65bcfb4 1224 /* ========== Check Overrun flag ========== */
mbed_official 205:c41fc65bcfb4 1225 if(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_OVR) && __HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_OVR))
mbed_official 205:c41fc65bcfb4 1226 {
mbed_official 205:c41fc65bcfb4 1227 /* If overrun is set to overwrite previous data (default setting), */
mbed_official 205:c41fc65bcfb4 1228 /* overrun event is not considered as an error. */
mbed_official 205:c41fc65bcfb4 1229 /* (cf ref manual "Managing conversions without using the DMA and without */
mbed_official 205:c41fc65bcfb4 1230 /* overrun ") */
mbed_official 205:c41fc65bcfb4 1231 /* Exception for usage with DMA overrun event always considered as an */
mbed_official 205:c41fc65bcfb4 1232 /* error. */
mbed_official 205:c41fc65bcfb4 1233 if ((hadc->Init.Overrun == OVR_DATA_PRESERVED) ||
mbed_official 205:c41fc65bcfb4 1234 HAL_IS_BIT_SET(hadc->Instance->CFGR1, ADC_CFGR1_DMAEN) )
mbed_official 205:c41fc65bcfb4 1235 {
mbed_official 205:c41fc65bcfb4 1236 /* Change ADC state to error state */
mbed_official 205:c41fc65bcfb4 1237 hadc->State = HAL_ADC_STATE_ERROR;
mbed_official 205:c41fc65bcfb4 1238
mbed_official 205:c41fc65bcfb4 1239 /* Set ADC error code to overrun */
mbed_official 205:c41fc65bcfb4 1240 hadc->ErrorCode |= HAL_ADC_ERROR_OVR;
mbed_official 205:c41fc65bcfb4 1241
mbed_official 205:c41fc65bcfb4 1242 /* Error callback */
mbed_official 205:c41fc65bcfb4 1243 HAL_ADC_ErrorCallback(hadc);
mbed_official 205:c41fc65bcfb4 1244 }
mbed_official 205:c41fc65bcfb4 1245
mbed_official 205:c41fc65bcfb4 1246 /* Clear the Overrun flag */
mbed_official 205:c41fc65bcfb4 1247 __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
mbed_official 205:c41fc65bcfb4 1248 }
mbed_official 205:c41fc65bcfb4 1249
mbed_official 205:c41fc65bcfb4 1250 }
mbed_official 205:c41fc65bcfb4 1251
mbed_official 205:c41fc65bcfb4 1252
mbed_official 205:c41fc65bcfb4 1253 /**
mbed_official 205:c41fc65bcfb4 1254 * @brief Conversion complete callback in non blocking mode
mbed_official 205:c41fc65bcfb4 1255 * @param hadc: ADC handle
mbed_official 205:c41fc65bcfb4 1256 * @retval None
mbed_official 205:c41fc65bcfb4 1257 */
mbed_official 205:c41fc65bcfb4 1258 __weak void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
mbed_official 205:c41fc65bcfb4 1259 {
mbed_official 205:c41fc65bcfb4 1260 /* NOTE : This function should not be modified. When the callback is needed,
mbed_official 205:c41fc65bcfb4 1261 function HAL_ADC_ConvCpltCallback must be implemented in the user file.
mbed_official 205:c41fc65bcfb4 1262 */
mbed_official 205:c41fc65bcfb4 1263 }
mbed_official 205:c41fc65bcfb4 1264
mbed_official 205:c41fc65bcfb4 1265 /**
mbed_official 205:c41fc65bcfb4 1266 * @brief Conversion DMA half-transfer callback in non blocking mode
mbed_official 205:c41fc65bcfb4 1267 * @param hadc: ADC handle
mbed_official 205:c41fc65bcfb4 1268 * @retval None
mbed_official 205:c41fc65bcfb4 1269 */
mbed_official 205:c41fc65bcfb4 1270 __weak void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc)
mbed_official 205:c41fc65bcfb4 1271 {
mbed_official 205:c41fc65bcfb4 1272 /* NOTE : This function should not be modified. When the callback is needed,
mbed_official 205:c41fc65bcfb4 1273 function HAL_ADC_ConvHalfCpltCallback must be implemented in the user file.
mbed_official 205:c41fc65bcfb4 1274 */
mbed_official 205:c41fc65bcfb4 1275 }
mbed_official 205:c41fc65bcfb4 1276
mbed_official 205:c41fc65bcfb4 1277 /**
mbed_official 205:c41fc65bcfb4 1278 * @brief Analog watchdog callback in non blocking mode.
mbed_official 205:c41fc65bcfb4 1279 * @note: In case of several analog watchdog enabled, if needed to know
mbed_official 205:c41fc65bcfb4 1280 which one triggered and on which ADCx, check Analog Watchdog flag
mbed_official 205:c41fc65bcfb4 1281 ADC_FLAG_AWD1/2/3 into HAL_ADC_LevelOutOfWindowCallback() function.
mbed_official 205:c41fc65bcfb4 1282 For example:"if (__HAL_ADC_GET_FLAG(hadc1, ADC_FLAG_AWD1) != RESET)"
mbed_official 205:c41fc65bcfb4 1283 * @param hadc: ADC handle
mbed_official 205:c41fc65bcfb4 1284 * @retval None
mbed_official 205:c41fc65bcfb4 1285 */
mbed_official 205:c41fc65bcfb4 1286 __weak void HAL_ADC_LevelOutOfWindowCallback(ADC_HandleTypeDef* hadc)
mbed_official 205:c41fc65bcfb4 1287 {
mbed_official 205:c41fc65bcfb4 1288 /* NOTE : This function should not be modified. When the callback is needed,
mbed_official 205:c41fc65bcfb4 1289 function HAL_ADC_LevelOoutOfWindowCallback must be implemented in the user file.
mbed_official 205:c41fc65bcfb4 1290 */
mbed_official 205:c41fc65bcfb4 1291 }
mbed_official 205:c41fc65bcfb4 1292
mbed_official 205:c41fc65bcfb4 1293 /**
mbed_official 205:c41fc65bcfb4 1294 * @brief ADC error callback in non blocking mode
mbed_official 205:c41fc65bcfb4 1295 * (ADC conversion with interruption or transfer by DMA)
mbed_official 205:c41fc65bcfb4 1296 * @param hadc: ADC handle
mbed_official 205:c41fc65bcfb4 1297 * @retval None
mbed_official 205:c41fc65bcfb4 1298 */
mbed_official 205:c41fc65bcfb4 1299 __weak void HAL_ADC_ErrorCallback(ADC_HandleTypeDef *hadc)
mbed_official 205:c41fc65bcfb4 1300 {
mbed_official 205:c41fc65bcfb4 1301 /* NOTE : This function should not be modified. When the callback is needed,
mbed_official 205:c41fc65bcfb4 1302 function HAL_ADC_ErrorCallback must be implemented in the user file.
mbed_official 205:c41fc65bcfb4 1303 */
mbed_official 205:c41fc65bcfb4 1304 }
mbed_official 205:c41fc65bcfb4 1305
mbed_official 205:c41fc65bcfb4 1306
mbed_official 205:c41fc65bcfb4 1307 /**
mbed_official 205:c41fc65bcfb4 1308 * @}
mbed_official 205:c41fc65bcfb4 1309 */
mbed_official 205:c41fc65bcfb4 1310
mbed_official 205:c41fc65bcfb4 1311 /** @defgroup ADC_Group3 Peripheral Control functions
mbed_official 205:c41fc65bcfb4 1312 * @brief Peripheral Control functions
mbed_official 205:c41fc65bcfb4 1313 *
mbed_official 205:c41fc65bcfb4 1314 @verbatim
mbed_official 205:c41fc65bcfb4 1315 ===============================================================================
mbed_official 205:c41fc65bcfb4 1316 ##### Peripheral Control functions #####
mbed_official 205:c41fc65bcfb4 1317 ===============================================================================
mbed_official 205:c41fc65bcfb4 1318 [..] This section provides functions allowing to:
mbed_official 205:c41fc65bcfb4 1319 (+) Configure channels on regular group
mbed_official 205:c41fc65bcfb4 1320 (+) Configure the analog watchdog
mbed_official 205:c41fc65bcfb4 1321
mbed_official 205:c41fc65bcfb4 1322 @endverbatim
mbed_official 205:c41fc65bcfb4 1323 * @{
mbed_official 205:c41fc65bcfb4 1324 */
mbed_official 205:c41fc65bcfb4 1325
mbed_official 205:c41fc65bcfb4 1326 /**
mbed_official 205:c41fc65bcfb4 1327 * @brief Configures the the selected channel to be linked to the regular
mbed_official 205:c41fc65bcfb4 1328 * group.
mbed_official 205:c41fc65bcfb4 1329 * @note In case of usage of internal measurement channels:
mbed_official 205:c41fc65bcfb4 1330 * VrefInt/Vbat/TempSensor.
mbed_official 205:c41fc65bcfb4 1331 * Sampling time constraints must be respected (sampling time can be
mbed_official 205:c41fc65bcfb4 1332 * adjusted in function of ADC clock frequency and sampling time
mbed_official 205:c41fc65bcfb4 1333 * setting).
mbed_official 205:c41fc65bcfb4 1334 * Refer to device datasheet for timings values, parameters TS_vrefint,
mbed_official 205:c41fc65bcfb4 1335 * TS_vbat, TS_temp (values rough order: 5us to 17us).
mbed_official 205:c41fc65bcfb4 1336 * These internal paths can be be disabled using function
mbed_official 205:c41fc65bcfb4 1337 * HAL_ADC_DeInit().
mbed_official 205:c41fc65bcfb4 1338 * @note Possibility to update parameters on the fly:
mbed_official 205:c41fc65bcfb4 1339 * This function initializes channel into regular group, following
mbed_official 205:c41fc65bcfb4 1340 * calls to this function can be used to reconfigure some parameters
mbed_official 205:c41fc65bcfb4 1341 * of structure "ADC_ChannelConfTypeDef" on the fly, without reseting
mbed_official 205:c41fc65bcfb4 1342 * the ADC.
mbed_official 205:c41fc65bcfb4 1343 * The setting of these parameters is conditioned to ADC state.
mbed_official 205:c41fc65bcfb4 1344 * For parameters constraints, see comments of structure
mbed_official 205:c41fc65bcfb4 1345 * "ADC_ChannelConfTypeDef".
mbed_official 205:c41fc65bcfb4 1346 * @param hadc: ADC handle
mbed_official 205:c41fc65bcfb4 1347 * @param sConfig: Structure ADC channel for regular group.
mbed_official 205:c41fc65bcfb4 1348 * @retval HAL status
mbed_official 205:c41fc65bcfb4 1349 */
mbed_official 205:c41fc65bcfb4 1350 HAL_StatusTypeDef HAL_ADC_ConfigChannel(ADC_HandleTypeDef* hadc, ADC_ChannelConfTypeDef* sConfig)
mbed_official 205:c41fc65bcfb4 1351 {
mbed_official 205:c41fc65bcfb4 1352 HAL_StatusTypeDef tmpHALStatus = HAL_OK;
mbed_official 205:c41fc65bcfb4 1353
mbed_official 205:c41fc65bcfb4 1354 /* Check the parameters */
mbed_official 205:c41fc65bcfb4 1355 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
mbed_official 205:c41fc65bcfb4 1356 assert_param(IS_ADC_CHANNEL(sConfig->Channel));
mbed_official 205:c41fc65bcfb4 1357 assert_param(IS_ADC_RANK(sConfig->Rank));
mbed_official 205:c41fc65bcfb4 1358 assert_param(IS_ADC_SAMPLE_TIME(sConfig->SamplingTime));
mbed_official 205:c41fc65bcfb4 1359
mbed_official 205:c41fc65bcfb4 1360 /* Process locked */
mbed_official 205:c41fc65bcfb4 1361 __HAL_LOCK(hadc);
mbed_official 205:c41fc65bcfb4 1362
mbed_official 205:c41fc65bcfb4 1363 /* Parameters update conditioned to ADC state: */
mbed_official 205:c41fc65bcfb4 1364 /* Parameters that can be updated when ADC is disabled or enabled without */
mbed_official 205:c41fc65bcfb4 1365 /* conversion on going on regular group: */
mbed_official 205:c41fc65bcfb4 1366 /* - Channel number */
mbed_official 205:c41fc65bcfb4 1367 /* - Channel sampling time */
mbed_official 205:c41fc65bcfb4 1368 /* - Management of internal measurement channels: Vbat/VrefInt/TempSensor */
mbed_official 205:c41fc65bcfb4 1369 if (__HAL_ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
mbed_official 205:c41fc65bcfb4 1370 {
mbed_official 205:c41fc65bcfb4 1371 /* Configure channel: depending on rank setting, add it or remove it from */
mbed_official 205:c41fc65bcfb4 1372 /* ADC conversion sequencer. */
mbed_official 205:c41fc65bcfb4 1373 if (sConfig->Rank != ADC_RANK_NONE)
mbed_official 205:c41fc65bcfb4 1374 {
mbed_official 205:c41fc65bcfb4 1375 /* Regular sequence configuration */
mbed_official 205:c41fc65bcfb4 1376 /* Set the channel selection register from the selected channel */
mbed_official 205:c41fc65bcfb4 1377 hadc->Instance->CHSELR |= __HAL_ADC_CHSELR_CHANNEL(sConfig->Channel);
mbed_official 205:c41fc65bcfb4 1378
mbed_official 205:c41fc65bcfb4 1379 /* Channel sampling time configuration */
mbed_official 205:c41fc65bcfb4 1380 /* Modify sampling time if needed (not needed in case of reoccurrence */
mbed_official 205:c41fc65bcfb4 1381 /* for several channels programmed consecutively into the sequencer) */
mbed_official 205:c41fc65bcfb4 1382 if (sConfig->SamplingTime != __HAL_ADC_GET_SAMPLINGTIME(hadc))
mbed_official 205:c41fc65bcfb4 1383 {
mbed_official 205:c41fc65bcfb4 1384 /* Channel sampling time configuration */
mbed_official 205:c41fc65bcfb4 1385 /* Clear the old sample time */
mbed_official 205:c41fc65bcfb4 1386 hadc->Instance->SMPR &= ~(ADC_SMPR_SMP);
mbed_official 205:c41fc65bcfb4 1387
mbed_official 205:c41fc65bcfb4 1388 /* Set the new sample time */
mbed_official 205:c41fc65bcfb4 1389 hadc->Instance->SMPR |= (sConfig->SamplingTime);
mbed_official 205:c41fc65bcfb4 1390 }
mbed_official 205:c41fc65bcfb4 1391
mbed_official 205:c41fc65bcfb4 1392 /* Management of internal measurement channels: Vbat/VrefInt/TempSensor */
mbed_official 205:c41fc65bcfb4 1393 /* internal measurement paths enable: If internal channel selected, */
mbed_official 205:c41fc65bcfb4 1394 /* enable dedicated internal buffers and path. */
mbed_official 205:c41fc65bcfb4 1395 /* Note: these internal measurement paths can be disabled using */
mbed_official 205:c41fc65bcfb4 1396 /* HAL_ADC_DeInit() or removing the channel from sequencer with */
mbed_official 205:c41fc65bcfb4 1397 /* channel configuration parameter "Rank". */
mbed_official 205:c41fc65bcfb4 1398
mbed_official 205:c41fc65bcfb4 1399 /* If Channel_16 is selected, enable Temp. sensor measurement path. */
mbed_official 205:c41fc65bcfb4 1400 if (sConfig->Channel == ADC_CHANNEL_TEMPSENSOR)
mbed_official 205:c41fc65bcfb4 1401 {
mbed_official 205:c41fc65bcfb4 1402 ADC->CCR |= ADC_CCR_TSEN;
mbed_official 205:c41fc65bcfb4 1403 }
mbed_official 205:c41fc65bcfb4 1404 /* If Channel_17 is selected, enable VBAT measurement path. */
mbed_official 205:c41fc65bcfb4 1405 else if (sConfig->Channel == ADC_CHANNEL_VBAT)
mbed_official 205:c41fc65bcfb4 1406 {
mbed_official 205:c41fc65bcfb4 1407 ADC->CCR |= ADC_CCR_VBATEN;
mbed_official 205:c41fc65bcfb4 1408 }
mbed_official 205:c41fc65bcfb4 1409 /* If Channel_18 is selected, enable VREFINT measurement path. */
mbed_official 205:c41fc65bcfb4 1410 else if (sConfig->Channel == ADC_CHANNEL_VREFINT)
mbed_official 205:c41fc65bcfb4 1411 {
mbed_official 205:c41fc65bcfb4 1412 ADC->CCR |= ADC_CCR_VREFEN;
mbed_official 205:c41fc65bcfb4 1413 }
mbed_official 205:c41fc65bcfb4 1414
mbed_official 205:c41fc65bcfb4 1415 }
mbed_official 205:c41fc65bcfb4 1416 else
mbed_official 205:c41fc65bcfb4 1417 {
mbed_official 205:c41fc65bcfb4 1418 /* Regular sequence configuration */
mbed_official 205:c41fc65bcfb4 1419 /* Reset the channel selection register from the selected channel */
mbed_official 205:c41fc65bcfb4 1420 hadc->Instance->CHSELR &= ~__HAL_ADC_CHSELR_CHANNEL(sConfig->Channel);
mbed_official 205:c41fc65bcfb4 1421
mbed_official 205:c41fc65bcfb4 1422 /* Management of internal measurement channels: Vbat/VrefInt/TempSensor */
mbed_official 205:c41fc65bcfb4 1423 /* internal measurement paths disable: If internal channel selected, */
mbed_official 205:c41fc65bcfb4 1424 /* disable dedicated internal buffers and path. */
mbed_official 205:c41fc65bcfb4 1425
mbed_official 205:c41fc65bcfb4 1426 /* If Channel_16 is selected, disable Temp. sensor measurement path. */
mbed_official 205:c41fc65bcfb4 1427 if (sConfig->Channel == ADC_CHANNEL_TEMPSENSOR)
mbed_official 205:c41fc65bcfb4 1428 {
mbed_official 205:c41fc65bcfb4 1429 ADC->CCR &= ~ADC_CCR_TSEN;
mbed_official 205:c41fc65bcfb4 1430 }
mbed_official 205:c41fc65bcfb4 1431 /* If Channel_17 is selected, disable VBAT measurement path. */
mbed_official 205:c41fc65bcfb4 1432 else if (sConfig->Channel == ADC_CHANNEL_VBAT)
mbed_official 205:c41fc65bcfb4 1433 {
mbed_official 205:c41fc65bcfb4 1434 ADC->CCR &= ~ADC_CCR_VBATEN;
mbed_official 205:c41fc65bcfb4 1435 }
mbed_official 205:c41fc65bcfb4 1436 /* If Channel_18 is selected, disable VREFINT measurement path. */
mbed_official 205:c41fc65bcfb4 1437 else if (sConfig->Channel == ADC_CHANNEL_VREFINT)
mbed_official 205:c41fc65bcfb4 1438 {
mbed_official 205:c41fc65bcfb4 1439 ADC->CCR &= ~ADC_CCR_VREFEN;
mbed_official 205:c41fc65bcfb4 1440 }
mbed_official 205:c41fc65bcfb4 1441 }
mbed_official 205:c41fc65bcfb4 1442
mbed_official 205:c41fc65bcfb4 1443 }
mbed_official 205:c41fc65bcfb4 1444
mbed_official 205:c41fc65bcfb4 1445
mbed_official 205:c41fc65bcfb4 1446 /* If a conversion is on going on regular group, no update on regular */
mbed_official 205:c41fc65bcfb4 1447 /* channel could be done on neither of the channel configuration structure */
mbed_official 205:c41fc65bcfb4 1448 /* parameters. */
mbed_official 205:c41fc65bcfb4 1449 else
mbed_official 205:c41fc65bcfb4 1450 {
mbed_official 205:c41fc65bcfb4 1451 /* Update ADC state machine to error */
mbed_official 205:c41fc65bcfb4 1452 hadc->State = HAL_ADC_STATE_ERROR;
mbed_official 205:c41fc65bcfb4 1453
mbed_official 205:c41fc65bcfb4 1454 tmpHALStatus = HAL_ERROR;
mbed_official 205:c41fc65bcfb4 1455 }
mbed_official 205:c41fc65bcfb4 1456
mbed_official 205:c41fc65bcfb4 1457 /* Process unlocked */
mbed_official 205:c41fc65bcfb4 1458 __HAL_UNLOCK(hadc);
mbed_official 205:c41fc65bcfb4 1459
mbed_official 205:c41fc65bcfb4 1460 /* Return function status */
mbed_official 205:c41fc65bcfb4 1461 return tmpHALStatus;
mbed_official 205:c41fc65bcfb4 1462 }
mbed_official 205:c41fc65bcfb4 1463
mbed_official 205:c41fc65bcfb4 1464
mbed_official 205:c41fc65bcfb4 1465 /**
mbed_official 205:c41fc65bcfb4 1466 * @brief Configures the analog watchdog.
mbed_official 205:c41fc65bcfb4 1467 * @note Possibility to update parameters on the fly:
mbed_official 205:c41fc65bcfb4 1468 * This function initializes the selected analog watchdog, following
mbed_official 205:c41fc65bcfb4 1469 * calls to this function can be used to reconfigure some parameters
mbed_official 205:c41fc65bcfb4 1470 * of structure "ADC_AnalogWDGConfTypeDef" on the fly, without reseting
mbed_official 205:c41fc65bcfb4 1471 * the ADC.
mbed_official 205:c41fc65bcfb4 1472 * The setting of these parameters is conditioned to ADC state.
mbed_official 205:c41fc65bcfb4 1473 * For parameters constraints, see comments of structure
mbed_official 205:c41fc65bcfb4 1474 * "ADC_AnalogWDGConfTypeDef".
mbed_official 205:c41fc65bcfb4 1475 * @param hadc: ADC handle
mbed_official 205:c41fc65bcfb4 1476 * @param AnalogWDGConfig: Structure of ADC analog watchdog configuration
mbed_official 205:c41fc65bcfb4 1477 * @retval HAL status
mbed_official 205:c41fc65bcfb4 1478 */
mbed_official 205:c41fc65bcfb4 1479 HAL_StatusTypeDef HAL_ADC_AnalogWDGConfig(ADC_HandleTypeDef* hadc, ADC_AnalogWDGConfTypeDef* AnalogWDGConfig)
mbed_official 205:c41fc65bcfb4 1480 {
mbed_official 205:c41fc65bcfb4 1481 HAL_StatusTypeDef tmpHALStatus = HAL_OK;
mbed_official 205:c41fc65bcfb4 1482
mbed_official 205:c41fc65bcfb4 1483 uint32_t tmpAWDHighThresholdShifted;
mbed_official 205:c41fc65bcfb4 1484 uint32_t tmpAWDLowThresholdShifted;
mbed_official 205:c41fc65bcfb4 1485
mbed_official 205:c41fc65bcfb4 1486 /* Check the parameters */
mbed_official 205:c41fc65bcfb4 1487 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
mbed_official 205:c41fc65bcfb4 1488 assert_param(IS_ADC_CHANNEL(AnalogWDGConfig->Channel));
mbed_official 205:c41fc65bcfb4 1489 assert_param(IS_FUNCTIONAL_STATE(AnalogWDGConfig->ITMode));
mbed_official 205:c41fc65bcfb4 1490
mbed_official 205:c41fc65bcfb4 1491 /* Verify if threshold is within the selected ADC resolution */
mbed_official 205:c41fc65bcfb4 1492 assert_param(IS_ADC_RANGE(__HAL_ADC_GET_RESOLUTION(hadc), AnalogWDGConfig->HighThreshold));
mbed_official 205:c41fc65bcfb4 1493 assert_param(IS_ADC_RANGE(__HAL_ADC_GET_RESOLUTION(hadc), AnalogWDGConfig->LowThreshold));
mbed_official 205:c41fc65bcfb4 1494
mbed_official 205:c41fc65bcfb4 1495 /* Process locked */
mbed_official 205:c41fc65bcfb4 1496 __HAL_LOCK(hadc);
mbed_official 205:c41fc65bcfb4 1497
mbed_official 205:c41fc65bcfb4 1498 /* Parameters update conditioned to ADC state: */
mbed_official 205:c41fc65bcfb4 1499 /* Parameters that can be updated when ADC is disabled or enabled without */
mbed_official 205:c41fc65bcfb4 1500 /* conversion on going on regular group: */
mbed_official 205:c41fc65bcfb4 1501 /* - Analog watchdog channels */
mbed_official 205:c41fc65bcfb4 1502 /* - Analog watchdog thresholds */
mbed_official 205:c41fc65bcfb4 1503 if (__HAL_ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
mbed_official 205:c41fc65bcfb4 1504 {
mbed_official 205:c41fc65bcfb4 1505 /* Configuration of analog watchdog: */
mbed_official 205:c41fc65bcfb4 1506 /* - Set the analog watchdog enable mode: one or overall group of */
mbed_official 205:c41fc65bcfb4 1507 /* channels. */
mbed_official 205:c41fc65bcfb4 1508 /* - Set the Analog watchdog channel (is not used if watchdog */
mbed_official 205:c41fc65bcfb4 1509 /* mode "all channels": ADC_CFGR_AWD1SGL=0). */
mbed_official 205:c41fc65bcfb4 1510 hadc->Instance->CFGR1 &= ~( ADC_CFGR1_AWDSGL |
mbed_official 205:c41fc65bcfb4 1511 ADC_CFGR1_AWDEN |
mbed_official 205:c41fc65bcfb4 1512 ADC_CFGR1_AWDCH );
mbed_official 205:c41fc65bcfb4 1513
mbed_official 205:c41fc65bcfb4 1514 hadc->Instance->CFGR1 |= ( AnalogWDGConfig->WatchdogMode |
mbed_official 205:c41fc65bcfb4 1515 __HAL_ADC_CFGR_AWDCH(AnalogWDGConfig->Channel) );
mbed_official 205:c41fc65bcfb4 1516
mbed_official 205:c41fc65bcfb4 1517 /* Shift the offset in function of the selected ADC resolution: Thresholds*/
mbed_official 205:c41fc65bcfb4 1518 /* have to be left-aligned on bit 11, the LSB (right bits) are set to 0 */
mbed_official 205:c41fc65bcfb4 1519 tmpAWDHighThresholdShifted = __HAL_ADC_AWD1THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->HighThreshold);
mbed_official 205:c41fc65bcfb4 1520 tmpAWDLowThresholdShifted = __HAL_ADC_AWD1THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->LowThreshold);
mbed_official 205:c41fc65bcfb4 1521
mbed_official 205:c41fc65bcfb4 1522 /* Set the high and low thresholds */
mbed_official 205:c41fc65bcfb4 1523 hadc->Instance->TR &= ~(ADC_TR_HT | ADC_TR_LT);
mbed_official 205:c41fc65bcfb4 1524 hadc->Instance->TR |= ( __HAL_ADC_TRX_HIGHTHRESHOLD (tmpAWDHighThresholdShifted) |
mbed_official 205:c41fc65bcfb4 1525 tmpAWDLowThresholdShifted );
mbed_official 205:c41fc65bcfb4 1526
mbed_official 205:c41fc65bcfb4 1527 /* Clear the ADC Analog watchdog flag (in case of let enabled by */
mbed_official 205:c41fc65bcfb4 1528 /* previous ADC operations) to be ready to use for HAL_ADC_IRQHandler() */
mbed_official 205:c41fc65bcfb4 1529 /* or HAL_ADC_PollForEvent(). */
mbed_official 205:c41fc65bcfb4 1530 __HAL_ADC_CLEAR_FLAG(hadc, ADC_IT_AWD);
mbed_official 205:c41fc65bcfb4 1531
mbed_official 205:c41fc65bcfb4 1532 /* Configure ADC Analog watchdog interrupt */
mbed_official 205:c41fc65bcfb4 1533 if(AnalogWDGConfig->ITMode == ENABLE)
mbed_official 205:c41fc65bcfb4 1534 {
mbed_official 205:c41fc65bcfb4 1535 /* Enable the ADC Analog watchdog interrupt */
mbed_official 205:c41fc65bcfb4 1536 __HAL_ADC_ENABLE_IT(hadc, ADC_IT_AWD);
mbed_official 205:c41fc65bcfb4 1537 }
mbed_official 205:c41fc65bcfb4 1538 else
mbed_official 205:c41fc65bcfb4 1539 {
mbed_official 205:c41fc65bcfb4 1540 /* Disable the ADC Analog watchdog interrupt */
mbed_official 205:c41fc65bcfb4 1541 __HAL_ADC_DISABLE_IT(hadc, ADC_IT_AWD);
mbed_official 205:c41fc65bcfb4 1542 }
mbed_official 205:c41fc65bcfb4 1543
mbed_official 205:c41fc65bcfb4 1544 }
mbed_official 205:c41fc65bcfb4 1545 /* If a conversion is on going on regular group, no update could be done */
mbed_official 205:c41fc65bcfb4 1546 /* on neither of the AWD configuration structure parameters. */
mbed_official 205:c41fc65bcfb4 1547 else
mbed_official 205:c41fc65bcfb4 1548 {
mbed_official 205:c41fc65bcfb4 1549 /* Update ADC state machine to error */
mbed_official 205:c41fc65bcfb4 1550 hadc->State = HAL_ADC_STATE_ERROR;
mbed_official 205:c41fc65bcfb4 1551
mbed_official 205:c41fc65bcfb4 1552 tmpHALStatus = HAL_ERROR;
mbed_official 205:c41fc65bcfb4 1553 }
mbed_official 205:c41fc65bcfb4 1554
mbed_official 205:c41fc65bcfb4 1555
mbed_official 205:c41fc65bcfb4 1556 /* Process unlocked */
mbed_official 205:c41fc65bcfb4 1557 __HAL_UNLOCK(hadc);
mbed_official 205:c41fc65bcfb4 1558
mbed_official 205:c41fc65bcfb4 1559 /* Return function status */
mbed_official 205:c41fc65bcfb4 1560 return tmpHALStatus;
mbed_official 205:c41fc65bcfb4 1561 }
mbed_official 205:c41fc65bcfb4 1562
mbed_official 205:c41fc65bcfb4 1563
mbed_official 205:c41fc65bcfb4 1564 /**
mbed_official 205:c41fc65bcfb4 1565 * @}
mbed_official 205:c41fc65bcfb4 1566 */
mbed_official 205:c41fc65bcfb4 1567
mbed_official 205:c41fc65bcfb4 1568
mbed_official 205:c41fc65bcfb4 1569 /** @defgroup ADC_Group4 Peripheral State functions
mbed_official 205:c41fc65bcfb4 1570 * @brief Peripheral State functions
mbed_official 205:c41fc65bcfb4 1571 *
mbed_official 205:c41fc65bcfb4 1572 @verbatim
mbed_official 205:c41fc65bcfb4 1573 ===============================================================================
mbed_official 205:c41fc65bcfb4 1574 ##### Peripheral State and Errors functions #####
mbed_official 205:c41fc65bcfb4 1575 ===============================================================================
mbed_official 205:c41fc65bcfb4 1576 [..]
mbed_official 205:c41fc65bcfb4 1577 This subsection provides functions to get in run-time the status of the
mbed_official 205:c41fc65bcfb4 1578 peripheral.
mbed_official 205:c41fc65bcfb4 1579 (+) Check the ADC state
mbed_official 205:c41fc65bcfb4 1580 (+) Check the ADC error code
mbed_official 205:c41fc65bcfb4 1581
mbed_official 205:c41fc65bcfb4 1582 @endverbatim
mbed_official 205:c41fc65bcfb4 1583 * @{
mbed_official 205:c41fc65bcfb4 1584 */
mbed_official 205:c41fc65bcfb4 1585
mbed_official 205:c41fc65bcfb4 1586 /**
mbed_official 205:c41fc65bcfb4 1587 * @brief return the ADC state
mbed_official 205:c41fc65bcfb4 1588 * @param hadc: ADC handle
mbed_official 205:c41fc65bcfb4 1589 * @retval HAL state
mbed_official 205:c41fc65bcfb4 1590 */
mbed_official 205:c41fc65bcfb4 1591 HAL_ADC_StateTypeDef HAL_ADC_GetState(ADC_HandleTypeDef* hadc)
mbed_official 205:c41fc65bcfb4 1592 {
mbed_official 205:c41fc65bcfb4 1593 /* Check the parameters */
mbed_official 205:c41fc65bcfb4 1594 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
mbed_official 205:c41fc65bcfb4 1595
mbed_official 205:c41fc65bcfb4 1596 /* Return ADC state */
mbed_official 205:c41fc65bcfb4 1597 return hadc->State;
mbed_official 205:c41fc65bcfb4 1598 }
mbed_official 205:c41fc65bcfb4 1599
mbed_official 205:c41fc65bcfb4 1600 /**
mbed_official 205:c41fc65bcfb4 1601 * @brief Return the ADC error code
mbed_official 205:c41fc65bcfb4 1602 * @param hadc: ADC handle
mbed_official 205:c41fc65bcfb4 1603 * @retval ADC Error Code
mbed_official 205:c41fc65bcfb4 1604 */
mbed_official 205:c41fc65bcfb4 1605 uint32_t HAL_ADC_GetError(ADC_HandleTypeDef *hadc)
mbed_official 205:c41fc65bcfb4 1606 {
mbed_official 205:c41fc65bcfb4 1607 return hadc->ErrorCode;
mbed_official 205:c41fc65bcfb4 1608 }
mbed_official 205:c41fc65bcfb4 1609
mbed_official 205:c41fc65bcfb4 1610 /**
mbed_official 205:c41fc65bcfb4 1611 * @}
mbed_official 205:c41fc65bcfb4 1612 */
mbed_official 205:c41fc65bcfb4 1613
mbed_official 205:c41fc65bcfb4 1614
mbed_official 205:c41fc65bcfb4 1615 /**
mbed_official 205:c41fc65bcfb4 1616 * @brief Enable the selected ADC.
mbed_official 205:c41fc65bcfb4 1617 * @note Prerequisite condition to use this function: ADC must be disabled
mbed_official 205:c41fc65bcfb4 1618 * and voltage regulator must be enabled (done into HAL_ADC_Init()).
mbed_official 205:c41fc65bcfb4 1619 * @param hadc: ADC handle
mbed_official 205:c41fc65bcfb4 1620 * @retval HAL status.
mbed_official 205:c41fc65bcfb4 1621 */
mbed_official 205:c41fc65bcfb4 1622 static HAL_StatusTypeDef ADC_Enable(ADC_HandleTypeDef* hadc)
mbed_official 205:c41fc65bcfb4 1623 {
mbed_official 205:c41fc65bcfb4 1624 uint32_t tickstart = 0;
mbed_official 205:c41fc65bcfb4 1625
mbed_official 205:c41fc65bcfb4 1626 /* ADC enable and wait for ADC ready (in case of ADC is disabled or */
mbed_official 205:c41fc65bcfb4 1627 /* enabling phase not yet completed: flag ADC ready not yet set). */
mbed_official 205:c41fc65bcfb4 1628 /* Timeout implemented to not be stuck if ADC cannot be enabled (possible */
mbed_official 205:c41fc65bcfb4 1629 /* causes: ADC clock not running, ...). */
mbed_official 205:c41fc65bcfb4 1630 if (__HAL_ADC_IS_ENABLED(hadc) == RESET)
mbed_official 205:c41fc65bcfb4 1631 {
mbed_official 205:c41fc65bcfb4 1632 /* Check if conditions to enable the ADC are fulfilled */
mbed_official 205:c41fc65bcfb4 1633 if (__HAL_ADC_ENABLING_CONDITIONS(hadc) == RESET)
mbed_official 205:c41fc65bcfb4 1634 {
mbed_official 205:c41fc65bcfb4 1635 /* Update ADC state machine to error */
mbed_official 205:c41fc65bcfb4 1636 hadc->State = HAL_ADC_STATE_ERROR;
mbed_official 205:c41fc65bcfb4 1637
mbed_official 205:c41fc65bcfb4 1638 /* Set ADC error code to ADC IP internal error */
mbed_official 205:c41fc65bcfb4 1639 hadc->ErrorCode |= HAL_ADC_ERROR_INTERNAL;
mbed_official 205:c41fc65bcfb4 1640
mbed_official 205:c41fc65bcfb4 1641 return HAL_ERROR;
mbed_official 205:c41fc65bcfb4 1642 }
mbed_official 205:c41fc65bcfb4 1643
mbed_official 205:c41fc65bcfb4 1644 /* Enable the ADC peripheral */
mbed_official 205:c41fc65bcfb4 1645 __HAL_ADC_ENABLE(hadc);
mbed_official 205:c41fc65bcfb4 1646
mbed_official 205:c41fc65bcfb4 1647 /* Wait for ADC effectively enabled */
mbed_official 205:c41fc65bcfb4 1648 tickstart = HAL_GetTick();
mbed_official 205:c41fc65bcfb4 1649
mbed_official 205:c41fc65bcfb4 1650 while(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_RDY) == RESET)
mbed_official 205:c41fc65bcfb4 1651 {
mbed_official 205:c41fc65bcfb4 1652 if((HAL_GetTick() - tickstart) > ADC_ENABLE_TIMEOUT)
mbed_official 205:c41fc65bcfb4 1653 {
mbed_official 205:c41fc65bcfb4 1654 /* Update ADC state machine to error */
mbed_official 205:c41fc65bcfb4 1655 hadc->State = HAL_ADC_STATE_ERROR;
mbed_official 205:c41fc65bcfb4 1656
mbed_official 205:c41fc65bcfb4 1657 /* Set ADC error code to ADC IP internal error */
mbed_official 205:c41fc65bcfb4 1658 hadc->ErrorCode |= HAL_ADC_ERROR_INTERNAL;
mbed_official 205:c41fc65bcfb4 1659
mbed_official 205:c41fc65bcfb4 1660 return HAL_ERROR;
mbed_official 205:c41fc65bcfb4 1661 }
mbed_official 205:c41fc65bcfb4 1662 }
mbed_official 205:c41fc65bcfb4 1663
mbed_official 205:c41fc65bcfb4 1664 }
mbed_official 205:c41fc65bcfb4 1665
mbed_official 205:c41fc65bcfb4 1666 /* Return HAL status */
mbed_official 205:c41fc65bcfb4 1667 return HAL_OK;
mbed_official 205:c41fc65bcfb4 1668 }
mbed_official 205:c41fc65bcfb4 1669
mbed_official 205:c41fc65bcfb4 1670 /**
mbed_official 205:c41fc65bcfb4 1671 * @brief Disable the selected ADC.
mbed_official 205:c41fc65bcfb4 1672 * @note Prerequisite condition to use this function: ADC conversions must be
mbed_official 205:c41fc65bcfb4 1673 * stopped.
mbed_official 205:c41fc65bcfb4 1674 * @param hadc: ADC handle
mbed_official 205:c41fc65bcfb4 1675 * @retval HAL status.
mbed_official 205:c41fc65bcfb4 1676 */
mbed_official 205:c41fc65bcfb4 1677 static HAL_StatusTypeDef ADC_Disable(ADC_HandleTypeDef* hadc)
mbed_official 205:c41fc65bcfb4 1678 {
mbed_official 205:c41fc65bcfb4 1679 uint32_t tickstart = 0;
mbed_official 205:c41fc65bcfb4 1680
mbed_official 205:c41fc65bcfb4 1681 /* Verification if ADC is not already disabled: */
mbed_official 205:c41fc65bcfb4 1682 /* Note: forbidden to disable ADC (set bit ADC_CR_ADDIS) if ADC is already */
mbed_official 205:c41fc65bcfb4 1683 /* disabled. */
mbed_official 205:c41fc65bcfb4 1684 if (__HAL_ADC_IS_ENABLED(hadc) != RESET )
mbed_official 205:c41fc65bcfb4 1685 {
mbed_official 205:c41fc65bcfb4 1686 /* Check if conditions to disable the ADC are fulfilled */
mbed_official 205:c41fc65bcfb4 1687 if (__HAL_ADC_DISABLING_CONDITIONS(hadc) != RESET)
mbed_official 205:c41fc65bcfb4 1688 {
mbed_official 205:c41fc65bcfb4 1689 /* Disable the ADC peripheral */
mbed_official 205:c41fc65bcfb4 1690 __HAL_ADC_DISABLE(hadc);
mbed_official 205:c41fc65bcfb4 1691 }
mbed_official 205:c41fc65bcfb4 1692 else
mbed_official 205:c41fc65bcfb4 1693 {
mbed_official 205:c41fc65bcfb4 1694 /* Update ADC state machine to error */
mbed_official 205:c41fc65bcfb4 1695 hadc->State = HAL_ADC_STATE_ERROR;
mbed_official 205:c41fc65bcfb4 1696
mbed_official 205:c41fc65bcfb4 1697 /* Set ADC error code to ADC IP internal error */
mbed_official 205:c41fc65bcfb4 1698 hadc->ErrorCode |= HAL_ADC_ERROR_INTERNAL;
mbed_official 205:c41fc65bcfb4 1699
mbed_official 205:c41fc65bcfb4 1700 return HAL_ERROR;
mbed_official 205:c41fc65bcfb4 1701 }
mbed_official 205:c41fc65bcfb4 1702
mbed_official 205:c41fc65bcfb4 1703 /* Wait for ADC effectively disabled */
mbed_official 205:c41fc65bcfb4 1704 tickstart = HAL_GetTick();
mbed_official 205:c41fc65bcfb4 1705
mbed_official 205:c41fc65bcfb4 1706 while(HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADEN))
mbed_official 205:c41fc65bcfb4 1707 {
mbed_official 205:c41fc65bcfb4 1708 if((HAL_GetTick() - tickstart) > ADC_ENABLE_TIMEOUT)
mbed_official 205:c41fc65bcfb4 1709 {
mbed_official 205:c41fc65bcfb4 1710 /* Update ADC state machine to error */
mbed_official 205:c41fc65bcfb4 1711 hadc->State = HAL_ADC_STATE_ERROR;
mbed_official 205:c41fc65bcfb4 1712
mbed_official 205:c41fc65bcfb4 1713 /* Set ADC error code to ADC IP internal error */
mbed_official 205:c41fc65bcfb4 1714 hadc->ErrorCode |= HAL_ADC_ERROR_INTERNAL;
mbed_official 205:c41fc65bcfb4 1715
mbed_official 205:c41fc65bcfb4 1716 return HAL_ERROR;
mbed_official 205:c41fc65bcfb4 1717 }
mbed_official 205:c41fc65bcfb4 1718 }
mbed_official 205:c41fc65bcfb4 1719 }
mbed_official 205:c41fc65bcfb4 1720
mbed_official 205:c41fc65bcfb4 1721 /* Return HAL status */
mbed_official 205:c41fc65bcfb4 1722 return HAL_OK;
mbed_official 205:c41fc65bcfb4 1723 }
mbed_official 205:c41fc65bcfb4 1724
mbed_official 205:c41fc65bcfb4 1725
mbed_official 205:c41fc65bcfb4 1726 /**
mbed_official 205:c41fc65bcfb4 1727 * @brief Stop ADC conversion.
mbed_official 205:c41fc65bcfb4 1728 * @note Prerequisite condition to use this function: ADC conversions must be
mbed_official 205:c41fc65bcfb4 1729 * stopped to disable the ADC.
mbed_official 205:c41fc65bcfb4 1730 * @param hadc: ADC handle
mbed_official 205:c41fc65bcfb4 1731 * @retval HAL status.
mbed_official 205:c41fc65bcfb4 1732 */
mbed_official 205:c41fc65bcfb4 1733 static HAL_StatusTypeDef ADC_ConversionStop(ADC_HandleTypeDef* hadc)
mbed_official 205:c41fc65bcfb4 1734 {
mbed_official 205:c41fc65bcfb4 1735 uint32_t tickstart = 0;
mbed_official 205:c41fc65bcfb4 1736
mbed_official 205:c41fc65bcfb4 1737 /* Check the parameters */
mbed_official 205:c41fc65bcfb4 1738 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
mbed_official 205:c41fc65bcfb4 1739
mbed_official 205:c41fc65bcfb4 1740 /* Verification if ADC is not already stopped on regular group to bypass */
mbed_official 205:c41fc65bcfb4 1741 /* this function if not needed. */
mbed_official 205:c41fc65bcfb4 1742 if (__HAL_ADC_IS_CONVERSION_ONGOING_REGULAR(hadc))
mbed_official 205:c41fc65bcfb4 1743 {
mbed_official 205:c41fc65bcfb4 1744
mbed_official 205:c41fc65bcfb4 1745 /* Stop potential conversion on going on regular group */
mbed_official 205:c41fc65bcfb4 1746 /* Software is allowed to set ADSTP only when ADSTART=1 and ADDIS=0 */
mbed_official 205:c41fc65bcfb4 1747 if (HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADSTART) &&
mbed_official 205:c41fc65bcfb4 1748 HAL_IS_BIT_CLR(hadc->Instance->CR, ADC_CR_ADDIS) )
mbed_official 205:c41fc65bcfb4 1749 {
mbed_official 205:c41fc65bcfb4 1750 /* Stop conversions on regular group */
mbed_official 205:c41fc65bcfb4 1751 hadc->Instance->CR |= ADC_CR_ADSTP;
mbed_official 205:c41fc65bcfb4 1752 }
mbed_official 205:c41fc65bcfb4 1753
mbed_official 205:c41fc65bcfb4 1754 /* Wait for conversion effectively stopped */
mbed_official 205:c41fc65bcfb4 1755 tickstart = HAL_GetTick();
mbed_official 205:c41fc65bcfb4 1756
mbed_official 205:c41fc65bcfb4 1757 while((hadc->Instance->CR & ADC_CR_ADSTART) != RESET)
mbed_official 205:c41fc65bcfb4 1758 {
mbed_official 205:c41fc65bcfb4 1759 if((HAL_GetTick() - tickstart) > ADC_STOP_CONVERSION_TIMEOUT)
mbed_official 205:c41fc65bcfb4 1760 {
mbed_official 205:c41fc65bcfb4 1761 /* Update ADC state machine to error */
mbed_official 205:c41fc65bcfb4 1762 hadc->State = HAL_ADC_STATE_ERROR;
mbed_official 205:c41fc65bcfb4 1763
mbed_official 205:c41fc65bcfb4 1764 /* Set ADC error code to ADC IP internal error */
mbed_official 205:c41fc65bcfb4 1765 hadc->ErrorCode |= HAL_ADC_ERROR_INTERNAL;
mbed_official 205:c41fc65bcfb4 1766
mbed_official 205:c41fc65bcfb4 1767 return HAL_ERROR;
mbed_official 205:c41fc65bcfb4 1768 }
mbed_official 205:c41fc65bcfb4 1769 }
mbed_official 205:c41fc65bcfb4 1770
mbed_official 205:c41fc65bcfb4 1771 }
mbed_official 205:c41fc65bcfb4 1772
mbed_official 205:c41fc65bcfb4 1773 /* Return HAL status */
mbed_official 205:c41fc65bcfb4 1774 return HAL_OK;
mbed_official 205:c41fc65bcfb4 1775 }
mbed_official 205:c41fc65bcfb4 1776
mbed_official 205:c41fc65bcfb4 1777 /**
mbed_official 205:c41fc65bcfb4 1778 * @}
mbed_official 205:c41fc65bcfb4 1779 */
mbed_official 205:c41fc65bcfb4 1780
mbed_official 205:c41fc65bcfb4 1781 #endif /* HAL_ADC_MODULE_ENABLED */
mbed_official 205:c41fc65bcfb4 1782 /**
mbed_official 205:c41fc65bcfb4 1783 * @}
mbed_official 205:c41fc65bcfb4 1784 */
mbed_official 205:c41fc65bcfb4 1785
mbed_official 205:c41fc65bcfb4 1786 /**
mbed_official 205:c41fc65bcfb4 1787 * @}
mbed_official 205:c41fc65bcfb4 1788 */
mbed_official 205:c41fc65bcfb4 1789
mbed_official 205:c41fc65bcfb4 1790 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/