STM32F103xB HAL User Manual
|
00001 /** 00002 ****************************************************************************** 00003 * @file stm32f1xx_hal_adc.c 00004 * @author MCD Application Team 00005 * @brief This file provides firmware functions to manage the following 00006 * functionalities of the Analog to Digital Convertor (ADC) 00007 * peripheral: 00008 * + Initialization and de-initialization functions 00009 * ++ Initialization and Configuration of ADC 00010 * + Operation functions 00011 * ++ Start, stop, get result of conversions of regular 00012 * group, using 3 possible modes: polling, interruption or DMA. 00013 * + Control functions 00014 * ++ Channels configuration on regular group 00015 * ++ Channels configuration on injected group 00016 * ++ Analog Watchdog configuration 00017 * + State functions 00018 * ++ ADC state machine management 00019 * ++ Interrupts and flags management 00020 * Other functions (extended functions) are available in file 00021 * "stm32f1xx_hal_adc_ex.c". 00022 * 00023 @verbatim 00024 ============================================================================== 00025 ##### ADC peripheral features ##### 00026 ============================================================================== 00027 [..] 00028 (+) 12-bit resolution 00029 00030 (+) Interrupt generation at the end of regular conversion, end of injected 00031 conversion, and in case of analog watchdog or overrun events. 00032 00033 (+) Single and continuous conversion modes. 00034 00035 (+) Scan mode for conversion of several channels sequentially. 00036 00037 (+) Data alignment with in-built data coherency. 00038 00039 (+) Programmable sampling time (channel wise) 00040 00041 (+) ADC conversion of regular group and injected group. 00042 00043 (+) External trigger (timer or EXTI) 00044 for both regular and injected groups. 00045 00046 (+) DMA request generation for transfer of conversions data of regular group. 00047 00048 (+) Multimode Dual mode (available on devices with 2 ADCs or more). 00049 00050 (+) Configurable DMA data storage in Multimode Dual mode (available on devices 00051 with 2 DCs or more). 00052 00053 (+) Configurable delay between conversions in Dual interleaved mode (available 00054 on devices with 2 DCs or more). 00055 00056 (+) ADC calibration 00057 00058 (+) ADC supply requirements: 2.4 V to 3.6 V at full speed and down to 1.8 V at 00059 slower speed. 00060 00061 (+) ADC input range: from Vref- (connected to Vssa) to Vref+ (connected to 00062 Vdda or to an external voltage reference). 00063 00064 00065 ##### How to use this driver ##### 00066 ============================================================================== 00067 [..] 00068 00069 *** Configuration of top level parameters related to ADC *** 00070 ============================================================ 00071 [..] 00072 00073 (#) Enable the ADC interface 00074 (++) As prerequisite, ADC clock must be configured at RCC top level. 00075 Caution: On STM32F1, ADC clock frequency max is 14MHz (refer 00076 to device datasheet). 00077 Therefore, ADC clock prescaler must be configured in 00078 function of ADC clock source frequency to remain below 00079 this maximum frequency. 00080 (++) One clock setting is mandatory: 00081 ADC clock (core clock, also possibly conversion clock). 00082 (+++) Example: 00083 Into HAL_ADC_MspInit() (recommended code location) or with 00084 other device clock parameters configuration: 00085 (+++) RCC_PeriphCLKInitTypeDef PeriphClkInit; 00086 (+++) __ADC1_CLK_ENABLE(); 00087 (+++) PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC; 00088 (+++) PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV2; 00089 (+++) HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit); 00090 00091 (#) ADC pins configuration 00092 (++) Enable the clock for the ADC GPIOs 00093 using macro __HAL_RCC_GPIOx_CLK_ENABLE() 00094 (++) Configure these ADC pins in analog mode 00095 using function HAL_GPIO_Init() 00096 00097 (#) Optionally, in case of usage of ADC with interruptions: 00098 (++) Configure the NVIC for ADC 00099 using function HAL_NVIC_EnableIRQ(ADCx_IRQn) 00100 (++) Insert the ADC interruption handler function HAL_ADC_IRQHandler() 00101 into the function of corresponding ADC interruption vector 00102 ADCx_IRQHandler(). 00103 00104 (#) Optionally, in case of usage of DMA: 00105 (++) Configure the DMA (DMA channel, mode normal or circular, ...) 00106 using function HAL_DMA_Init(). 00107 (++) Configure the NVIC for DMA 00108 using function HAL_NVIC_EnableIRQ(DMAx_Channelx_IRQn) 00109 (++) Insert the ADC interruption handler function HAL_ADC_IRQHandler() 00110 into the function of corresponding DMA interruption vector 00111 DMAx_Channelx_IRQHandler(). 00112 00113 *** Configuration of ADC, groups regular/injected, channels parameters *** 00114 ========================================================================== 00115 [..] 00116 00117 (#) Configure the ADC parameters (resolution, data alignment, ...) 00118 and regular group parameters (conversion trigger, sequencer, ...) 00119 using function HAL_ADC_Init(). 00120 00121 (#) Configure the channels for regular group parameters (channel number, 00122 channel rank into sequencer, ..., into regular group) 00123 using function HAL_ADC_ConfigChannel(). 00124 00125 (#) Optionally, configure the injected group parameters (conversion trigger, 00126 sequencer, ..., of injected group) 00127 and the channels for injected group parameters (channel number, 00128 channel rank into sequencer, ..., into injected group) 00129 using function HAL_ADCEx_InjectedConfigChannel(). 00130 00131 (#) Optionally, configure the analog watchdog parameters (channels 00132 monitored, thresholds, ...) 00133 using function HAL_ADC_AnalogWDGConfig(). 00134 00135 (#) Optionally, for devices with several ADC instances: configure the 00136 multimode parameters 00137 using function HAL_ADCEx_MultiModeConfigChannel(). 00138 00139 *** Execution of ADC conversions *** 00140 ==================================== 00141 [..] 00142 00143 (#) Optionally, perform an automatic ADC calibration to improve the 00144 conversion accuracy 00145 using function HAL_ADCEx_Calibration_Start(). 00146 00147 (#) ADC driver can be used among three modes: polling, interruption, 00148 transfer by DMA. 00149 00150 (++) ADC conversion by polling: 00151 (+++) Activate the ADC peripheral and start conversions 00152 using function HAL_ADC_Start() 00153 (+++) Wait for ADC conversion completion 00154 using function HAL_ADC_PollForConversion() 00155 (or for injected group: HAL_ADCEx_InjectedPollForConversion() ) 00156 (+++) Retrieve conversion results 00157 using function HAL_ADC_GetValue() 00158 (or for injected group: HAL_ADCEx_InjectedGetValue() ) 00159 (+++) Stop conversion and disable the ADC peripheral 00160 using function HAL_ADC_Stop() 00161 00162 (++) ADC conversion by interruption: 00163 (+++) Activate the ADC peripheral and start conversions 00164 using function HAL_ADC_Start_IT() 00165 (+++) Wait for ADC conversion completion by call of function 00166 HAL_ADC_ConvCpltCallback() 00167 (this function must be implemented in user program) 00168 (or for injected group: HAL_ADCEx_InjectedConvCpltCallback() ) 00169 (+++) Retrieve conversion results 00170 using function HAL_ADC_GetValue() 00171 (or for injected group: HAL_ADCEx_InjectedGetValue() ) 00172 (+++) Stop conversion and disable the ADC peripheral 00173 using function HAL_ADC_Stop_IT() 00174 00175 (++) ADC conversion with transfer by DMA: 00176 (+++) Activate the ADC peripheral and start conversions 00177 using function HAL_ADC_Start_DMA() 00178 (+++) Wait for ADC conversion completion by call of function 00179 HAL_ADC_ConvCpltCallback() or HAL_ADC_ConvHalfCpltCallback() 00180 (these functions must be implemented in user program) 00181 (+++) Conversion results are automatically transferred by DMA into 00182 destination variable address. 00183 (+++) Stop conversion and disable the ADC peripheral 00184 using function HAL_ADC_Stop_DMA() 00185 00186 (++) For devices with several ADCs: ADC multimode conversion 00187 with transfer by DMA: 00188 (+++) Activate the ADC peripheral (slave) and start conversions 00189 using function HAL_ADC_Start() 00190 (+++) Activate the ADC peripheral (master) and start conversions 00191 using function HAL_ADCEx_MultiModeStart_DMA() 00192 (+++) Wait for ADC conversion completion by call of function 00193 HAL_ADC_ConvCpltCallback() or HAL_ADC_ConvHalfCpltCallback() 00194 (these functions must be implemented in user program) 00195 (+++) Conversion results are automatically transferred by DMA into 00196 destination variable address. 00197 (+++) Stop conversion and disable the ADC peripheral (master) 00198 using function HAL_ADCEx_MultiModeStop_DMA() 00199 (+++) Stop conversion and disable the ADC peripheral (slave) 00200 using function HAL_ADC_Stop_IT() 00201 00202 [..] 00203 00204 (@) Callback functions must be implemented in user program: 00205 (+@) HAL_ADC_ErrorCallback() 00206 (+@) HAL_ADC_LevelOutOfWindowCallback() (callback of analog watchdog) 00207 (+@) HAL_ADC_ConvCpltCallback() 00208 (+@) HAL_ADC_ConvHalfCpltCallback 00209 (+@) HAL_ADCEx_InjectedConvCpltCallback() 00210 00211 *** Deinitialization of ADC *** 00212 ============================================================ 00213 [..] 00214 00215 (#) Disable the ADC interface 00216 (++) ADC clock can be hard reset and disabled at RCC top level. 00217 (++) Hard reset of ADC peripherals 00218 using macro __ADCx_FORCE_RESET(), __ADCx_RELEASE_RESET(). 00219 (++) ADC clock disable 00220 using the equivalent macro/functions as configuration step. 00221 (+++) Example: 00222 Into HAL_ADC_MspDeInit() (recommended code location) or with 00223 other device clock parameters configuration: 00224 (+++) PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC 00225 (+++) PeriphClkInit.AdcClockSelection = RCC_ADCPLLCLK2_OFF 00226 (+++) HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) 00227 00228 (#) ADC pins configuration 00229 (++) Disable the clock for the ADC GPIOs 00230 using macro __HAL_RCC_GPIOx_CLK_DISABLE() 00231 00232 (#) Optionally, in case of usage of ADC with interruptions: 00233 (++) Disable the NVIC for ADC 00234 using function HAL_NVIC_EnableIRQ(ADCx_IRQn) 00235 00236 (#) Optionally, in case of usage of DMA: 00237 (++) Deinitialize the DMA 00238 using function HAL_DMA_Init(). 00239 (++) Disable the NVIC for DMA 00240 using function HAL_NVIC_EnableIRQ(DMAx_Channelx_IRQn) 00241 00242 [..] 00243 00244 *** Callback registration *** 00245 ============================================= 00246 [..] 00247 00248 The compilation flag USE_HAL_ADC_REGISTER_CALLBACKS, when set to 1, 00249 allows the user to configure dynamically the driver callbacks. 00250 Use Functions @ref HAL_ADC_RegisterCallback() 00251 to register an interrupt callback. 00252 [..] 00253 00254 Function @ref HAL_ADC_RegisterCallback() allows to register following callbacks: 00255 (+) ConvCpltCallback : ADC conversion complete callback 00256 (+) ConvHalfCpltCallback : ADC conversion DMA half-transfer callback 00257 (+) LevelOutOfWindowCallback : ADC analog watchdog 1 callback 00258 (+) ErrorCallback : ADC error callback 00259 (+) InjectedConvCpltCallback : ADC group injected conversion complete callback 00260 (+) MspInitCallback : ADC Msp Init callback 00261 (+) MspDeInitCallback : ADC Msp DeInit callback 00262 This function takes as parameters the HAL peripheral handle, the Callback ID 00263 and a pointer to the user callback function. 00264 [..] 00265 00266 Use function @ref HAL_ADC_UnRegisterCallback to reset a callback to the default 00267 weak function. 00268 [..] 00269 00270 @ref HAL_ADC_UnRegisterCallback takes as parameters the HAL peripheral handle, 00271 and the Callback ID. 00272 This function allows to reset following callbacks: 00273 (+) ConvCpltCallback : ADC conversion complete callback 00274 (+) ConvHalfCpltCallback : ADC conversion DMA half-transfer callback 00275 (+) LevelOutOfWindowCallback : ADC analog watchdog 1 callback 00276 (+) ErrorCallback : ADC error callback 00277 (+) InjectedConvCpltCallback : ADC group injected conversion complete callback 00278 (+) MspInitCallback : ADC Msp Init callback 00279 (+) MspDeInitCallback : ADC Msp DeInit callback 00280 [..] 00281 00282 By default, after the @ref HAL_ADC_Init() and when the state is @ref HAL_ADC_STATE_RESET 00283 all callbacks are set to the corresponding weak functions: 00284 examples @ref HAL_ADC_ConvCpltCallback(), @ref HAL_ADC_ErrorCallback(). 00285 Exception done for MspInit and MspDeInit functions that are 00286 reset to the legacy weak functions in the @ref HAL_ADC_Init()/ @ref HAL_ADC_DeInit() only when 00287 these callbacks are null (not registered beforehand). 00288 [..] 00289 00290 If MspInit or MspDeInit are not null, the @ref HAL_ADC_Init()/ @ref HAL_ADC_DeInit() 00291 keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state. 00292 [..] 00293 00294 Callbacks can be registered/unregistered in @ref HAL_ADC_STATE_READY state only. 00295 Exception done MspInit/MspDeInit functions that can be registered/unregistered 00296 in @ref HAL_ADC_STATE_READY or @ref HAL_ADC_STATE_RESET state, 00297 thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit. 00298 [..] 00299 00300 Then, the user first registers the MspInit/MspDeInit user callbacks 00301 using @ref HAL_ADC_RegisterCallback() before calling @ref HAL_ADC_DeInit() 00302 or @ref HAL_ADC_Init() function. 00303 [..] 00304 00305 When the compilation flag USE_HAL_ADC_REGISTER_CALLBACKS is set to 0 or 00306 not defined, the callback registration feature is not available and all callbacks 00307 are set to the corresponding weak functions. 00308 00309 @endverbatim 00310 ****************************************************************************** 00311 * @attention 00312 * 00313 * <h2><center>© Copyright (c) 2016 STMicroelectronics. 00314 * All rights reserved.</center></h2> 00315 * 00316 * This software component is licensed by ST under BSD 3-Clause license, 00317 * the "License"; You may not use this file except in compliance with the 00318 * License. You may obtain a copy of the License at: 00319 * opensource.org/licenses/BSD-3-Clause 00320 * 00321 ****************************************************************************** 00322 */ 00323 00324 /* Includes ------------------------------------------------------------------*/ 00325 #include "stm32f1xx_hal.h" 00326 00327 /** @addtogroup STM32F1xx_HAL_Driver 00328 * @{ 00329 */ 00330 00331 /** @defgroup ADC ADC 00332 * @brief ADC HAL module driver 00333 * @{ 00334 */ 00335 00336 #ifdef HAL_ADC_MODULE_ENABLED 00337 00338 /* Private typedef -----------------------------------------------------------*/ 00339 /* Private define ------------------------------------------------------------*/ 00340 /** @defgroup ADC_Private_Constants ADC Private Constants 00341 * @{ 00342 */ 00343 00344 /* Timeout values for ADC enable and disable settling time. */ 00345 /* Values defined to be higher than worst cases: low clocks freq, */ 00346 /* maximum prescaler. */ 00347 /* Ex of profile low frequency : Clock source at 0.1 MHz, ADC clock */ 00348 /* prescaler 4, sampling time 12.5 ADC clock cycles, resolution 12 bits. */ 00349 /* Unit: ms */ 00350 #define ADC_ENABLE_TIMEOUT 2U 00351 #define ADC_DISABLE_TIMEOUT 2U 00352 00353 /* Delay for ADC stabilization time. */ 00354 /* Maximum delay is 1us (refer to device datasheet, parameter tSTAB). */ 00355 /* Unit: us */ 00356 #define ADC_STAB_DELAY_US 1U 00357 00358 /* Delay for temperature sensor stabilization time. */ 00359 /* Maximum delay is 10us (refer to device datasheet, parameter tSTART). */ 00360 /* Unit: us */ 00361 #define ADC_TEMPSENSOR_DELAY_US 10U 00362 00363 /** 00364 * @} 00365 */ 00366 00367 /* Private macro -------------------------------------------------------------*/ 00368 /* Private variables ---------------------------------------------------------*/ 00369 /* Private function prototypes -----------------------------------------------*/ 00370 /** @defgroup ADC_Private_Functions ADC Private Functions 00371 * @{ 00372 */ 00373 /** 00374 * @} 00375 */ 00376 00377 /* Exported functions --------------------------------------------------------*/ 00378 00379 /** @defgroup ADC_Exported_Functions ADC Exported Functions 00380 * @{ 00381 */ 00382 00383 /** @defgroup ADC_Exported_Functions_Group1 Initialization/de-initialization functions 00384 * @brief Initialization and Configuration functions 00385 * 00386 @verbatim 00387 =============================================================================== 00388 ##### Initialization and de-initialization functions ##### 00389 =============================================================================== 00390 [..] This section provides functions allowing to: 00391 (+) Initialize and configure the ADC. 00392 (+) De-initialize the ADC. 00393 00394 @endverbatim 00395 * @{ 00396 */ 00397 00398 /** 00399 * @brief Initializes the ADC peripheral and regular group according to 00400 * parameters specified in structure "ADC_InitTypeDef". 00401 * @note As prerequisite, ADC clock must be configured at RCC top level 00402 * (clock source APB2). 00403 * See commented example code below that can be copied and uncommented 00404 * into HAL_ADC_MspInit(). 00405 * @note Possibility to update parameters on the fly: 00406 * This function initializes the ADC MSP (HAL_ADC_MspInit()) only when 00407 * coming from ADC state reset. Following calls to this function can 00408 * be used to reconfigure some parameters of ADC_InitTypeDef 00409 * structure on the fly, without modifying MSP configuration. If ADC 00410 * MSP has to be modified again, HAL_ADC_DeInit() must be called 00411 * before HAL_ADC_Init(). 00412 * The setting of these parameters is conditioned to ADC state. 00413 * For parameters constraints, see comments of structure 00414 * "ADC_InitTypeDef". 00415 * @note This function configures the ADC within 2 scopes: scope of entire 00416 * ADC and scope of regular group. For parameters details, see comments 00417 * of structure "ADC_InitTypeDef". 00418 * @param hadc: ADC handle 00419 * @retval HAL status 00420 */ 00421 HAL_StatusTypeDef HAL_ADC_Init(ADC_HandleTypeDef* hadc) 00422 { 00423 HAL_StatusTypeDef tmp_hal_status = HAL_OK; 00424 uint32_t tmp_cr1 = 0U; 00425 uint32_t tmp_cr2 = 0U; 00426 uint32_t tmp_sqr1 = 0U; 00427 00428 /* Check ADC handle */ 00429 if(hadc == NULL) 00430 { 00431 return HAL_ERROR; 00432 } 00433 00434 /* Check the parameters */ 00435 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance)); 00436 assert_param(IS_ADC_DATA_ALIGN(hadc->Init.DataAlign)); 00437 assert_param(IS_ADC_SCAN_MODE(hadc->Init.ScanConvMode)); 00438 assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode)); 00439 assert_param(IS_ADC_EXTTRIG(hadc->Init.ExternalTrigConv)); 00440 00441 if(hadc->Init.ScanConvMode != ADC_SCAN_DISABLE) 00442 { 00443 assert_param(IS_ADC_REGULAR_NB_CONV(hadc->Init.NbrOfConversion)); 00444 assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DiscontinuousConvMode)); 00445 if(hadc->Init.DiscontinuousConvMode != DISABLE) 00446 { 00447 assert_param(IS_ADC_REGULAR_DISCONT_NUMBER(hadc->Init.NbrOfDiscConversion)); 00448 } 00449 } 00450 00451 /* As prerequisite, into HAL_ADC_MspInit(), ADC clock must be configured */ 00452 /* at RCC top level. */ 00453 /* Refer to header of this file for more details on clock enabling */ 00454 /* procedure. */ 00455 00456 /* Actions performed only if ADC is coming from state reset: */ 00457 /* - Initialization of ADC MSP */ 00458 if (hadc->State == HAL_ADC_STATE_RESET) 00459 { 00460 /* Initialize ADC error code */ 00461 ADC_CLEAR_ERRORCODE(hadc); 00462 00463 /* Allocate lock resource and initialize it */ 00464 hadc->Lock = HAL_UNLOCKED; 00465 00466 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1) 00467 /* Init the ADC Callback settings */ 00468 hadc->ConvCpltCallback = HAL_ADC_ConvCpltCallback; /* Legacy weak callback */ 00469 hadc->ConvHalfCpltCallback = HAL_ADC_ConvHalfCpltCallback; /* Legacy weak callback */ 00470 hadc->LevelOutOfWindowCallback = HAL_ADC_LevelOutOfWindowCallback; /* Legacy weak callback */ 00471 hadc->ErrorCallback = HAL_ADC_ErrorCallback; /* Legacy weak callback */ 00472 hadc->InjectedConvCpltCallback = HAL_ADCEx_InjectedConvCpltCallback; /* Legacy weak callback */ 00473 00474 if (hadc->MspInitCallback == NULL) 00475 { 00476 hadc->MspInitCallback = HAL_ADC_MspInit; /* Legacy weak MspInit */ 00477 } 00478 00479 /* Init the low level hardware */ 00480 hadc->MspInitCallback(hadc); 00481 #else 00482 /* Init the low level hardware */ 00483 HAL_ADC_MspInit(hadc); 00484 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */ 00485 } 00486 00487 /* Stop potential conversion on going, on regular and injected groups */ 00488 /* Disable ADC peripheral */ 00489 /* Note: In case of ADC already enabled, precaution to not launch an */ 00490 /* unwanted conversion while modifying register CR2 by writing 1 to */ 00491 /* bit ADON. */ 00492 tmp_hal_status = ADC_ConversionStop_Disable(hadc); 00493 00494 00495 /* Configuration of ADC parameters if previous preliminary actions are */ 00496 /* correctly completed. */ 00497 if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL) && 00498 (tmp_hal_status == HAL_OK) ) 00499 { 00500 /* Set ADC state */ 00501 ADC_STATE_CLR_SET(hadc->State, 00502 HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY, 00503 HAL_ADC_STATE_BUSY_INTERNAL); 00504 00505 /* Set ADC parameters */ 00506 00507 /* Configuration of ADC: */ 00508 /* - data alignment */ 00509 /* - external trigger to start conversion */ 00510 /* - external trigger polarity (always set to 1, because needed for all */ 00511 /* triggers: external trigger of SW start) */ 00512 /* - continuous conversion mode */ 00513 /* Note: External trigger polarity (ADC_CR2_EXTTRIG) is set into */ 00514 /* HAL_ADC_Start_xxx functions because if set in this function, */ 00515 /* a conversion on injected group would start a conversion also on */ 00516 /* regular group after ADC enabling. */ 00517 tmp_cr2 |= (hadc->Init.DataAlign | 00518 ADC_CFGR_EXTSEL(hadc, hadc->Init.ExternalTrigConv) | 00519 ADC_CR2_CONTINUOUS((uint32_t)hadc->Init.ContinuousConvMode) ); 00520 00521 /* Configuration of ADC: */ 00522 /* - scan mode */ 00523 /* - discontinuous mode disable/enable */ 00524 /* - discontinuous mode number of conversions */ 00525 tmp_cr1 |= (ADC_CR1_SCAN_SET(hadc->Init.ScanConvMode)); 00526 00527 /* Enable discontinuous mode only if continuous mode is disabled */ 00528 /* Note: If parameter "Init.ScanConvMode" is set to disable, parameter */ 00529 /* discontinuous is set anyway, but will have no effect on ADC HW. */ 00530 if (hadc->Init.DiscontinuousConvMode == ENABLE) 00531 { 00532 if (hadc->Init.ContinuousConvMode == DISABLE) 00533 { 00534 /* Enable the selected ADC regular discontinuous mode */ 00535 /* Set the number of channels to be converted in discontinuous mode */ 00536 SET_BIT(tmp_cr1, ADC_CR1_DISCEN | 00537 ADC_CR1_DISCONTINUOUS_NUM(hadc->Init.NbrOfDiscConversion) ); 00538 } 00539 else 00540 { 00541 /* ADC regular group settings continuous and sequencer discontinuous*/ 00542 /* cannot be enabled simultaneously. */ 00543 00544 /* Update ADC state machine to error */ 00545 SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG); 00546 00547 /* Set ADC error code to ADC IP internal error */ 00548 SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL); 00549 } 00550 } 00551 00552 /* Update ADC configuration register CR1 with previous settings */ 00553 MODIFY_REG(hadc->Instance->CR1, 00554 ADC_CR1_SCAN | 00555 ADC_CR1_DISCEN | 00556 ADC_CR1_DISCNUM , 00557 tmp_cr1 ); 00558 00559 /* Update ADC configuration register CR2 with previous settings */ 00560 MODIFY_REG(hadc->Instance->CR2, 00561 ADC_CR2_ALIGN | 00562 ADC_CR2_EXTSEL | 00563 ADC_CR2_EXTTRIG | 00564 ADC_CR2_CONT , 00565 tmp_cr2 ); 00566 00567 /* Configuration of regular group sequencer: */ 00568 /* - if scan mode is disabled, regular channels sequence length is set to */ 00569 /* 0x00: 1 channel converted (channel on regular rank 1) */ 00570 /* Parameter "NbrOfConversion" is discarded. */ 00571 /* Note: Scan mode is present by hardware on this device and, if */ 00572 /* disabled, discards automatically nb of conversions. Anyway, nb of */ 00573 /* conversions is forced to 0x00 for alignment over all STM32 devices. */ 00574 /* - if scan mode is enabled, regular channels sequence length is set to */ 00575 /* parameter "NbrOfConversion" */ 00576 if (ADC_CR1_SCAN_SET(hadc->Init.ScanConvMode) == ADC_SCAN_ENABLE) 00577 { 00578 tmp_sqr1 = ADC_SQR1_L_SHIFT(hadc->Init.NbrOfConversion); 00579 } 00580 00581 MODIFY_REG(hadc->Instance->SQR1, 00582 ADC_SQR1_L , 00583 tmp_sqr1 ); 00584 00585 /* Check back that ADC registers have effectively been configured to */ 00586 /* ensure of no potential problem of ADC core IP clocking. */ 00587 /* Check through register CR2 (excluding bits set in other functions: */ 00588 /* execution control bits (ADON, JSWSTART, SWSTART), regular group bits */ 00589 /* (DMA), injected group bits (JEXTTRIG and JEXTSEL), channel internal */ 00590 /* measurement path bit (TSVREFE). */ 00591 if (READ_BIT(hadc->Instance->CR2, ~(ADC_CR2_ADON | ADC_CR2_DMA | 00592 ADC_CR2_SWSTART | ADC_CR2_JSWSTART | 00593 ADC_CR2_JEXTTRIG | ADC_CR2_JEXTSEL | 00594 ADC_CR2_TSVREFE )) 00595 == tmp_cr2) 00596 { 00597 /* Set ADC error code to none */ 00598 ADC_CLEAR_ERRORCODE(hadc); 00599 00600 /* Set the ADC state */ 00601 ADC_STATE_CLR_SET(hadc->State, 00602 HAL_ADC_STATE_BUSY_INTERNAL, 00603 HAL_ADC_STATE_READY); 00604 } 00605 else 00606 { 00607 /* Update ADC state machine to error */ 00608 ADC_STATE_CLR_SET(hadc->State, 00609 HAL_ADC_STATE_BUSY_INTERNAL, 00610 HAL_ADC_STATE_ERROR_INTERNAL); 00611 00612 /* Set ADC error code to ADC IP internal error */ 00613 SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL); 00614 00615 tmp_hal_status = HAL_ERROR; 00616 } 00617 00618 } 00619 else 00620 { 00621 /* Update ADC state machine to error */ 00622 SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL); 00623 00624 tmp_hal_status = HAL_ERROR; 00625 } 00626 00627 /* Return function status */ 00628 return tmp_hal_status; 00629 } 00630 00631 /** 00632 * @brief Deinitialize the ADC peripheral registers to their default reset 00633 * values, with deinitialization of the ADC MSP. 00634 * If needed, the example code can be copied and uncommented into 00635 * function HAL_ADC_MspDeInit(). 00636 * @param hadc: ADC handle 00637 * @retval HAL status 00638 */ 00639 HAL_StatusTypeDef HAL_ADC_DeInit(ADC_HandleTypeDef* hadc) 00640 { 00641 HAL_StatusTypeDef tmp_hal_status = HAL_OK; 00642 00643 /* Check ADC handle */ 00644 if(hadc == NULL) 00645 { 00646 return HAL_ERROR; 00647 } 00648 00649 /* Check the parameters */ 00650 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance)); 00651 00652 /* Set ADC state */ 00653 SET_BIT(hadc->State, HAL_ADC_STATE_BUSY_INTERNAL); 00654 00655 /* Stop potential conversion on going, on regular and injected groups */ 00656 /* Disable ADC peripheral */ 00657 tmp_hal_status = ADC_ConversionStop_Disable(hadc); 00658 00659 00660 /* Configuration of ADC parameters if previous preliminary actions are */ 00661 /* correctly completed. */ 00662 if (tmp_hal_status == HAL_OK) 00663 { 00664 /* ========== Reset ADC registers ========== */ 00665 00666 00667 00668 00669 /* Reset register SR */ 00670 __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_AWD | ADC_FLAG_JEOC | ADC_FLAG_EOC | 00671 ADC_FLAG_JSTRT | ADC_FLAG_STRT)); 00672 00673 /* Reset register CR1 */ 00674 CLEAR_BIT(hadc->Instance->CR1, (ADC_CR1_AWDEN | ADC_CR1_JAWDEN | ADC_CR1_DISCNUM | 00675 ADC_CR1_JDISCEN | ADC_CR1_DISCEN | ADC_CR1_JAUTO | 00676 ADC_CR1_AWDSGL | ADC_CR1_SCAN | ADC_CR1_JEOCIE | 00677 ADC_CR1_AWDIE | ADC_CR1_EOCIE | ADC_CR1_AWDCH )); 00678 00679 /* Reset register CR2 */ 00680 CLEAR_BIT(hadc->Instance->CR2, (ADC_CR2_TSVREFE | ADC_CR2_SWSTART | ADC_CR2_JSWSTART | 00681 ADC_CR2_EXTTRIG | ADC_CR2_EXTSEL | ADC_CR2_JEXTTRIG | 00682 ADC_CR2_JEXTSEL | ADC_CR2_ALIGN | ADC_CR2_DMA | 00683 ADC_CR2_RSTCAL | ADC_CR2_CAL | ADC_CR2_CONT | 00684 ADC_CR2_ADON )); 00685 00686 /* Reset register SMPR1 */ 00687 CLEAR_BIT(hadc->Instance->SMPR1, (ADC_SMPR1_SMP17 | ADC_SMPR1_SMP16 | ADC_SMPR1_SMP15 | 00688 ADC_SMPR1_SMP14 | ADC_SMPR1_SMP13 | ADC_SMPR1_SMP12 | 00689 ADC_SMPR1_SMP11 | ADC_SMPR1_SMP10 )); 00690 00691 /* Reset register SMPR2 */ 00692 CLEAR_BIT(hadc->Instance->SMPR2, (ADC_SMPR2_SMP9 | ADC_SMPR2_SMP8 | ADC_SMPR2_SMP7 | 00693 ADC_SMPR2_SMP6 | ADC_SMPR2_SMP5 | ADC_SMPR2_SMP4 | 00694 ADC_SMPR2_SMP3 | ADC_SMPR2_SMP2 | ADC_SMPR2_SMP1 | 00695 ADC_SMPR2_SMP0 )); 00696 00697 /* Reset register JOFR1 */ 00698 CLEAR_BIT(hadc->Instance->JOFR1, ADC_JOFR1_JOFFSET1); 00699 /* Reset register JOFR2 */ 00700 CLEAR_BIT(hadc->Instance->JOFR2, ADC_JOFR2_JOFFSET2); 00701 /* Reset register JOFR3 */ 00702 CLEAR_BIT(hadc->Instance->JOFR3, ADC_JOFR3_JOFFSET3); 00703 /* Reset register JOFR4 */ 00704 CLEAR_BIT(hadc->Instance->JOFR4, ADC_JOFR4_JOFFSET4); 00705 00706 /* Reset register HTR */ 00707 CLEAR_BIT(hadc->Instance->HTR, ADC_HTR_HT); 00708 /* Reset register LTR */ 00709 CLEAR_BIT(hadc->Instance->LTR, ADC_LTR_LT); 00710 00711 /* Reset register SQR1 */ 00712 CLEAR_BIT(hadc->Instance->SQR1, ADC_SQR1_L | 00713 ADC_SQR1_SQ16 | ADC_SQR1_SQ15 | 00714 ADC_SQR1_SQ14 | ADC_SQR1_SQ13 ); 00715 00716 /* Reset register SQR1 */ 00717 CLEAR_BIT(hadc->Instance->SQR1, ADC_SQR1_L | 00718 ADC_SQR1_SQ16 | ADC_SQR1_SQ15 | 00719 ADC_SQR1_SQ14 | ADC_SQR1_SQ13 ); 00720 00721 /* Reset register SQR2 */ 00722 CLEAR_BIT(hadc->Instance->SQR2, ADC_SQR2_SQ12 | ADC_SQR2_SQ11 | ADC_SQR2_SQ10 | 00723 ADC_SQR2_SQ9 | ADC_SQR2_SQ8 | ADC_SQR2_SQ7 ); 00724 00725 /* Reset register SQR3 */ 00726 CLEAR_BIT(hadc->Instance->SQR3, ADC_SQR3_SQ6 | ADC_SQR3_SQ5 | ADC_SQR3_SQ4 | 00727 ADC_SQR3_SQ3 | ADC_SQR3_SQ2 | ADC_SQR3_SQ1 ); 00728 00729 /* Reset register JSQR */ 00730 CLEAR_BIT(hadc->Instance->JSQR, ADC_JSQR_JL | 00731 ADC_JSQR_JSQ4 | ADC_JSQR_JSQ3 | 00732 ADC_JSQR_JSQ2 | ADC_JSQR_JSQ1 ); 00733 00734 /* Reset register JSQR */ 00735 CLEAR_BIT(hadc->Instance->JSQR, ADC_JSQR_JL | 00736 ADC_JSQR_JSQ4 | ADC_JSQR_JSQ3 | 00737 ADC_JSQR_JSQ2 | ADC_JSQR_JSQ1 ); 00738 00739 /* Reset register DR */ 00740 /* bits in access mode read only, no direct reset applicable*/ 00741 00742 /* Reset registers JDR1, JDR2, JDR3, JDR4 */ 00743 /* bits in access mode read only, no direct reset applicable*/ 00744 00745 /* ========== Hard reset ADC peripheral ========== */ 00746 /* Performs a global reset of the entire ADC peripheral: ADC state is */ 00747 /* forced to a similar state after device power-on. */ 00748 /* If needed, copy-paste and uncomment the following reset code into */ 00749 /* function "void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)": */ 00750 /* */ 00751 /* __HAL_RCC_ADC1_FORCE_RESET() */ 00752 /* __HAL_RCC_ADC1_RELEASE_RESET() */ 00753 00754 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1) 00755 if (hadc->MspDeInitCallback == NULL) 00756 { 00757 hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit */ 00758 } 00759 00760 /* DeInit the low level hardware */ 00761 hadc->MspDeInitCallback(hadc); 00762 #else 00763 /* DeInit the low level hardware */ 00764 HAL_ADC_MspDeInit(hadc); 00765 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */ 00766 00767 /* Set ADC error code to none */ 00768 ADC_CLEAR_ERRORCODE(hadc); 00769 00770 /* Set ADC state */ 00771 hadc->State = HAL_ADC_STATE_RESET; 00772 00773 } 00774 00775 /* Process unlocked */ 00776 __HAL_UNLOCK(hadc); 00777 00778 /* Return function status */ 00779 return tmp_hal_status; 00780 } 00781 00782 /** 00783 * @brief Initializes the ADC MSP. 00784 * @param hadc: ADC handle 00785 * @retval None 00786 */ 00787 __weak void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc) 00788 { 00789 /* Prevent unused argument(s) compilation warning */ 00790 UNUSED(hadc); 00791 /* NOTE : This function should not be modified. When the callback is needed, 00792 function HAL_ADC_MspInit must be implemented in the user file. 00793 */ 00794 } 00795 00796 /** 00797 * @brief DeInitializes the ADC MSP. 00798 * @param hadc: ADC handle 00799 * @retval None 00800 */ 00801 __weak void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc) 00802 { 00803 /* Prevent unused argument(s) compilation warning */ 00804 UNUSED(hadc); 00805 /* NOTE : This function should not be modified. When the callback is needed, 00806 function HAL_ADC_MspDeInit must be implemented in the user file. 00807 */ 00808 } 00809 00810 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1) 00811 /** 00812 * @brief Register a User ADC Callback 00813 * To be used instead of the weak predefined callback 00814 * @param hadc Pointer to a ADC_HandleTypeDef structure that contains 00815 * the configuration information for the specified ADC. 00816 * @param CallbackID ID of the callback to be registered 00817 * This parameter can be one of the following values: 00818 * @arg @ref HAL_ADC_CONVERSION_COMPLETE_CB_ID ADC conversion complete callback ID 00819 * @arg @ref HAL_ADC_CONVERSION_HALF_CB_ID ADC conversion complete callback ID 00820 * @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID ADC analog watchdog 1 callback ID 00821 * @arg @ref HAL_ADC_ERROR_CB_ID ADC error callback ID 00822 * @arg @ref HAL_ADC_INJ_CONVERSION_COMPLETE_CB_ID ADC group injected conversion complete callback ID 00823 * @arg @ref HAL_ADC_MSPINIT_CB_ID ADC Msp Init callback ID 00824 * @arg @ref HAL_ADC_MSPDEINIT_CB_ID ADC Msp DeInit callback ID 00825 * @arg @ref HAL_ADC_MSPINIT_CB_ID MspInit callback ID 00826 * @arg @ref HAL_ADC_MSPDEINIT_CB_ID MspDeInit callback ID 00827 * @param pCallback pointer to the Callback function 00828 * @retval HAL status 00829 */ 00830 HAL_StatusTypeDef HAL_ADC_RegisterCallback(ADC_HandleTypeDef *hadc, HAL_ADC_CallbackIDTypeDef CallbackID, pADC_CallbackTypeDef pCallback) 00831 { 00832 HAL_StatusTypeDef status = HAL_OK; 00833 00834 if (pCallback == NULL) 00835 { 00836 /* Update the error code */ 00837 hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK; 00838 00839 return HAL_ERROR; 00840 } 00841 00842 if ((hadc->State & HAL_ADC_STATE_READY) != 0) 00843 { 00844 switch (CallbackID) 00845 { 00846 case HAL_ADC_CONVERSION_COMPLETE_CB_ID : 00847 hadc->ConvCpltCallback = pCallback; 00848 break; 00849 00850 case HAL_ADC_CONVERSION_HALF_CB_ID : 00851 hadc->ConvHalfCpltCallback = pCallback; 00852 break; 00853 00854 case HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID : 00855 hadc->LevelOutOfWindowCallback = pCallback; 00856 break; 00857 00858 case HAL_ADC_ERROR_CB_ID : 00859 hadc->ErrorCallback = pCallback; 00860 break; 00861 00862 case HAL_ADC_INJ_CONVERSION_COMPLETE_CB_ID : 00863 hadc->InjectedConvCpltCallback = pCallback; 00864 break; 00865 00866 case HAL_ADC_MSPINIT_CB_ID : 00867 hadc->MspInitCallback = pCallback; 00868 break; 00869 00870 case HAL_ADC_MSPDEINIT_CB_ID : 00871 hadc->MspDeInitCallback = pCallback; 00872 break; 00873 00874 default : 00875 /* Update the error code */ 00876 hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK; 00877 00878 /* Return error status */ 00879 status = HAL_ERROR; 00880 break; 00881 } 00882 } 00883 else if (HAL_ADC_STATE_RESET == hadc->State) 00884 { 00885 switch (CallbackID) 00886 { 00887 case HAL_ADC_MSPINIT_CB_ID : 00888 hadc->MspInitCallback = pCallback; 00889 break; 00890 00891 case HAL_ADC_MSPDEINIT_CB_ID : 00892 hadc->MspDeInitCallback = pCallback; 00893 break; 00894 00895 default : 00896 /* Update the error code */ 00897 hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK; 00898 00899 /* Return error status */ 00900 status = HAL_ERROR; 00901 break; 00902 } 00903 } 00904 else 00905 { 00906 /* Update the error code */ 00907 hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK; 00908 00909 /* Return error status */ 00910 status = HAL_ERROR; 00911 } 00912 00913 return status; 00914 } 00915 00916 /** 00917 * @brief Unregister a ADC Callback 00918 * ADC callback is redirected to the weak predefined callback 00919 * @param hadc Pointer to a ADC_HandleTypeDef structure that contains 00920 * the configuration information for the specified ADC. 00921 * @param CallbackID ID of the callback to be unregistered 00922 * This parameter can be one of the following values: 00923 * @arg @ref HAL_ADC_CONVERSION_COMPLETE_CB_ID ADC conversion complete callback ID 00924 * @arg @ref HAL_ADC_CONVERSION_HALF_CB_ID ADC conversion complete callback ID 00925 * @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID ADC analog watchdog 1 callback ID 00926 * @arg @ref HAL_ADC_ERROR_CB_ID ADC error callback ID 00927 * @arg @ref HAL_ADC_INJ_CONVERSION_COMPLETE_CB_ID ADC group injected conversion complete callback ID 00928 * @arg @ref HAL_ADC_MSPINIT_CB_ID ADC Msp Init callback ID 00929 * @arg @ref HAL_ADC_MSPDEINIT_CB_ID ADC Msp DeInit callback ID 00930 * @arg @ref HAL_ADC_MSPINIT_CB_ID MspInit callback ID 00931 * @arg @ref HAL_ADC_MSPDEINIT_CB_ID MspDeInit callback ID 00932 * @retval HAL status 00933 */ 00934 HAL_StatusTypeDef HAL_ADC_UnRegisterCallback(ADC_HandleTypeDef *hadc, HAL_ADC_CallbackIDTypeDef CallbackID) 00935 { 00936 HAL_StatusTypeDef status = HAL_OK; 00937 00938 if ((hadc->State & HAL_ADC_STATE_READY) != 0) 00939 { 00940 switch (CallbackID) 00941 { 00942 case HAL_ADC_CONVERSION_COMPLETE_CB_ID : 00943 hadc->ConvCpltCallback = HAL_ADC_ConvCpltCallback; 00944 break; 00945 00946 case HAL_ADC_CONVERSION_HALF_CB_ID : 00947 hadc->ConvHalfCpltCallback = HAL_ADC_ConvHalfCpltCallback; 00948 break; 00949 00950 case HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID : 00951 hadc->LevelOutOfWindowCallback = HAL_ADC_LevelOutOfWindowCallback; 00952 break; 00953 00954 case HAL_ADC_ERROR_CB_ID : 00955 hadc->ErrorCallback = HAL_ADC_ErrorCallback; 00956 break; 00957 00958 case HAL_ADC_INJ_CONVERSION_COMPLETE_CB_ID : 00959 hadc->InjectedConvCpltCallback = HAL_ADCEx_InjectedConvCpltCallback; 00960 break; 00961 00962 case HAL_ADC_MSPINIT_CB_ID : 00963 hadc->MspInitCallback = HAL_ADC_MspInit; /* Legacy weak MspInit */ 00964 break; 00965 00966 case HAL_ADC_MSPDEINIT_CB_ID : 00967 hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit */ 00968 break; 00969 00970 default : 00971 /* Update the error code */ 00972 hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK; 00973 00974 /* Return error status */ 00975 status = HAL_ERROR; 00976 break; 00977 } 00978 } 00979 else if (HAL_ADC_STATE_RESET == hadc->State) 00980 { 00981 switch (CallbackID) 00982 { 00983 case HAL_ADC_MSPINIT_CB_ID : 00984 hadc->MspInitCallback = HAL_ADC_MspInit; /* Legacy weak MspInit */ 00985 break; 00986 00987 case HAL_ADC_MSPDEINIT_CB_ID : 00988 hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit */ 00989 break; 00990 00991 default : 00992 /* Update the error code */ 00993 hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK; 00994 00995 /* Return error status */ 00996 status = HAL_ERROR; 00997 break; 00998 } 00999 } 01000 else 01001 { 01002 /* Update the error code */ 01003 hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK; 01004 01005 /* Return error status */ 01006 status = HAL_ERROR; 01007 } 01008 01009 return status; 01010 } 01011 01012 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */ 01013 01014 /** 01015 * @} 01016 */ 01017 01018 /** @defgroup ADC_Exported_Functions_Group2 IO operation functions 01019 * @brief Input and Output operation functions 01020 * 01021 @verbatim 01022 =============================================================================== 01023 ##### IO operation functions ##### 01024 =============================================================================== 01025 [..] This section provides functions allowing to: 01026 (+) Start conversion of regular group. 01027 (+) Stop conversion of regular group. 01028 (+) Poll for conversion complete on regular group. 01029 (+) Poll for conversion event. 01030 (+) Get result of regular channel conversion. 01031 (+) Start conversion of regular group and enable interruptions. 01032 (+) Stop conversion of regular group and disable interruptions. 01033 (+) Handle ADC interrupt request 01034 (+) Start conversion of regular group and enable DMA transfer. 01035 (+) Stop conversion of regular group and disable ADC DMA transfer. 01036 @endverbatim 01037 * @{ 01038 */ 01039 01040 /** 01041 * @brief Enables ADC, starts conversion of regular group. 01042 * Interruptions enabled in this function: None. 01043 * @param hadc: ADC handle 01044 * @retval HAL status 01045 */ 01046 HAL_StatusTypeDef HAL_ADC_Start(ADC_HandleTypeDef* hadc) 01047 { 01048 HAL_StatusTypeDef tmp_hal_status = HAL_OK; 01049 01050 /* Check the parameters */ 01051 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance)); 01052 01053 /* Process locked */ 01054 __HAL_LOCK(hadc); 01055 01056 /* Enable the ADC peripheral */ 01057 tmp_hal_status = ADC_Enable(hadc); 01058 01059 /* Start conversion if ADC is effectively enabled */ 01060 if (tmp_hal_status == HAL_OK) 01061 { 01062 /* Set ADC state */ 01063 /* - Clear state bitfield related to regular group conversion results */ 01064 /* - Set state bitfield related to regular operation */ 01065 ADC_STATE_CLR_SET(hadc->State, 01066 HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC, 01067 HAL_ADC_STATE_REG_BUSY); 01068 01069 /* Set group injected state (from auto-injection) and multimode state */ 01070 /* for all cases of multimode: independent mode, multimode ADC master */ 01071 /* or multimode ADC slave (for devices with several ADCs): */ 01072 if (ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc)) 01073 { 01074 /* Set ADC state (ADC independent or master) */ 01075 CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE); 01076 01077 /* If conversions on group regular are also triggering group injected, */ 01078 /* update ADC state. */ 01079 if (READ_BIT(hadc->Instance->CR1, ADC_CR1_JAUTO) != RESET) 01080 { 01081 ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY); 01082 } 01083 } 01084 else 01085 { 01086 /* Set ADC state (ADC slave) */ 01087 SET_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE); 01088 01089 /* If conversions on group regular are also triggering group injected, */ 01090 /* update ADC state. */ 01091 if (ADC_MULTIMODE_AUTO_INJECTED(hadc)) 01092 { 01093 ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY); 01094 } 01095 } 01096 01097 /* State machine update: Check if an injected conversion is ongoing */ 01098 if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_INJ_BUSY)) 01099 { 01100 /* Reset ADC error code fields related to conversions on group regular */ 01101 CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA)); 01102 } 01103 else 01104 { 01105 /* Reset ADC all error code fields */ 01106 ADC_CLEAR_ERRORCODE(hadc); 01107 } 01108 01109 /* Process unlocked */ 01110 /* Unlock before starting ADC conversions: in case of potential */ 01111 /* interruption, to let the process to ADC IRQ Handler. */ 01112 __HAL_UNLOCK(hadc); 01113 01114 /* Clear regular group conversion flag */ 01115 /* (To ensure of no unknown state from potential previous ADC operations) */ 01116 __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOC); 01117 01118 /* Enable conversion of regular group. */ 01119 /* If software start has been selected, conversion starts immediately. */ 01120 /* If external trigger has been selected, conversion will start at next */ 01121 /* trigger event. */ 01122 /* Case of multimode enabled: */ 01123 /* - if ADC is slave, ADC is enabled only (conversion is not started). */ 01124 /* - if ADC is master, ADC is enabled and conversion is started. */ 01125 /* If ADC is master, ADC is enabled and conversion is started. */ 01126 /* Note: Alternate trigger for single conversion could be to force an */ 01127 /* additional set of bit ADON "hadc->Instance->CR2 |= ADC_CR2_ADON;"*/ 01128 if (ADC_IS_SOFTWARE_START_REGULAR(hadc) && 01129 ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc) ) 01130 { 01131 /* Start ADC conversion on regular group with SW start */ 01132 SET_BIT(hadc->Instance->CR2, (ADC_CR2_SWSTART | ADC_CR2_EXTTRIG)); 01133 } 01134 else 01135 { 01136 /* Start ADC conversion on regular group with external trigger */ 01137 SET_BIT(hadc->Instance->CR2, ADC_CR2_EXTTRIG); 01138 } 01139 } 01140 else 01141 { 01142 /* Process unlocked */ 01143 __HAL_UNLOCK(hadc); 01144 } 01145 01146 /* Return function status */ 01147 return tmp_hal_status; 01148 } 01149 01150 /** 01151 * @brief Stop ADC conversion of regular group (and injected channels in 01152 * case of auto_injection mode), disable ADC peripheral. 01153 * @note: ADC peripheral disable is forcing stop of potential 01154 * conversion on injected group. If injected group is under use, it 01155 * should be preliminarily stopped using HAL_ADCEx_InjectedStop function. 01156 * @param hadc: ADC handle 01157 * @retval HAL status. 01158 */ 01159 HAL_StatusTypeDef HAL_ADC_Stop(ADC_HandleTypeDef* hadc) 01160 { 01161 HAL_StatusTypeDef tmp_hal_status = HAL_OK; 01162 01163 /* Check the parameters */ 01164 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance)); 01165 01166 /* Process locked */ 01167 __HAL_LOCK(hadc); 01168 01169 /* Stop potential conversion on going, on regular and injected groups */ 01170 /* Disable ADC peripheral */ 01171 tmp_hal_status = ADC_ConversionStop_Disable(hadc); 01172 01173 /* Check if ADC is effectively disabled */ 01174 if (tmp_hal_status == HAL_OK) 01175 { 01176 /* Set ADC state */ 01177 ADC_STATE_CLR_SET(hadc->State, 01178 HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY, 01179 HAL_ADC_STATE_READY); 01180 } 01181 01182 /* Process unlocked */ 01183 __HAL_UNLOCK(hadc); 01184 01185 /* Return function status */ 01186 return tmp_hal_status; 01187 } 01188 01189 /** 01190 * @brief Wait for regular group conversion to be completed. 01191 * @note This function cannot be used in a particular setup: ADC configured 01192 * in DMA mode. 01193 * In this case, DMA resets the flag EOC and polling cannot be 01194 * performed on each conversion. 01195 * @note On STM32F1 devices, limitation in case of sequencer enabled 01196 * (several ranks selected): polling cannot be done on each 01197 * conversion inside the sequence. In this case, polling is replaced by 01198 * wait for maximum conversion time. 01199 * @param hadc: ADC handle 01200 * @param Timeout: Timeout value in millisecond. 01201 * @retval HAL status 01202 */ 01203 HAL_StatusTypeDef HAL_ADC_PollForConversion(ADC_HandleTypeDef* hadc, uint32_t Timeout) 01204 { 01205 uint32_t tickstart = 0U; 01206 01207 /* Variables for polling in case of scan mode enabled and polling for each */ 01208 /* conversion. */ 01209 __IO uint32_t Conversion_Timeout_CPU_cycles = 0U; 01210 uint32_t Conversion_Timeout_CPU_cycles_max = 0U; 01211 01212 /* Check the parameters */ 01213 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance)); 01214 01215 /* Get tick count */ 01216 tickstart = HAL_GetTick(); 01217 01218 /* Verification that ADC configuration is compliant with polling for */ 01219 /* each conversion: */ 01220 /* Particular case is ADC configured in DMA mode */ 01221 if (HAL_IS_BIT_SET(hadc->Instance->CR2, ADC_CR2_DMA)) 01222 { 01223 /* Update ADC state machine to error */ 01224 SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG); 01225 01226 /* Process unlocked */ 01227 __HAL_UNLOCK(hadc); 01228 01229 return HAL_ERROR; 01230 } 01231 01232 /* Polling for end of conversion: differentiation if single/sequence */ 01233 /* conversion. */ 01234 /* - If single conversion for regular group (Scan mode disabled or enabled */ 01235 /* with NbrOfConversion =1), flag EOC is used to determine the */ 01236 /* conversion completion. */ 01237 /* - If sequence conversion for regular group (scan mode enabled and */ 01238 /* NbrOfConversion >=2), flag EOC is set only at the end of the */ 01239 /* sequence. */ 01240 /* To poll for each conversion, the maximum conversion time is computed */ 01241 /* from ADC conversion time (selected sampling time + conversion time of */ 01242 /* 12.5 ADC clock cycles) and APB2/ADC clock prescalers (depending on */ 01243 /* settings, conversion time range can be from 28 to 32256 CPU cycles). */ 01244 /* As flag EOC is not set after each conversion, no timeout status can */ 01245 /* be set. */ 01246 if (HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_SCAN) && 01247 HAL_IS_BIT_CLR(hadc->Instance->SQR1, ADC_SQR1_L) ) 01248 { 01249 /* Wait until End of Conversion flag is raised */ 01250 while(HAL_IS_BIT_CLR(hadc->Instance->SR, ADC_FLAG_EOC)) 01251 { 01252 /* Check if timeout is disabled (set to infinite wait) */ 01253 if(Timeout != HAL_MAX_DELAY) 01254 { 01255 if((Timeout == 0U) || ((HAL_GetTick() - tickstart ) > Timeout)) 01256 { 01257 /* New check to avoid false timeout detection in case of preemption */ 01258 if(HAL_IS_BIT_CLR(hadc->Instance->SR, ADC_FLAG_EOC)) 01259 { 01260 /* Update ADC state machine to timeout */ 01261 SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT); 01262 01263 /* Process unlocked */ 01264 __HAL_UNLOCK(hadc); 01265 01266 return HAL_TIMEOUT; 01267 } 01268 } 01269 } 01270 } 01271 } 01272 else 01273 { 01274 /* Replace polling by wait for maximum conversion time */ 01275 /* - Computation of CPU clock cycles corresponding to ADC clock cycles */ 01276 /* and ADC maximum conversion cycles on all channels. */ 01277 /* - Wait for the expected ADC clock cycles delay */ 01278 Conversion_Timeout_CPU_cycles_max = ((SystemCoreClock 01279 / HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_ADC)) 01280 * ADC_CONVCYCLES_MAX_RANGE(hadc) ); 01281 01282 while(Conversion_Timeout_CPU_cycles < Conversion_Timeout_CPU_cycles_max) 01283 { 01284 /* Check if timeout is disabled (set to infinite wait) */ 01285 if(Timeout != HAL_MAX_DELAY) 01286 { 01287 if((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout)) 01288 { 01289 /* New check to avoid false timeout detection in case of preemption */ 01290 if(Conversion_Timeout_CPU_cycles < Conversion_Timeout_CPU_cycles_max) 01291 { 01292 /* Update ADC state machine to timeout */ 01293 SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT); 01294 01295 /* Process unlocked */ 01296 __HAL_UNLOCK(hadc); 01297 01298 return HAL_TIMEOUT; 01299 } 01300 } 01301 } 01302 Conversion_Timeout_CPU_cycles ++; 01303 } 01304 } 01305 01306 /* Clear regular group conversion flag */ 01307 __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_STRT | ADC_FLAG_EOC); 01308 01309 /* Update ADC state machine */ 01310 SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC); 01311 01312 /* Determine whether any further conversion upcoming on group regular */ 01313 /* by external trigger, continuous mode or scan sequence on going. */ 01314 /* Note: On STM32F1 devices, in case of sequencer enabled */ 01315 /* (several ranks selected), end of conversion flag is raised */ 01316 /* at the end of the sequence. */ 01317 if(ADC_IS_SOFTWARE_START_REGULAR(hadc) && 01318 (hadc->Init.ContinuousConvMode == DISABLE) ) 01319 { 01320 /* Set ADC state */ 01321 CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY); 01322 01323 if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_INJ_BUSY)) 01324 { 01325 SET_BIT(hadc->State, HAL_ADC_STATE_READY); 01326 } 01327 } 01328 01329 /* Return ADC state */ 01330 return HAL_OK; 01331 } 01332 01333 /** 01334 * @brief Poll for conversion event. 01335 * @param hadc: ADC handle 01336 * @param EventType: the ADC event type. 01337 * This parameter can be one of the following values: 01338 * @arg ADC_AWD_EVENT: ADC Analog watchdog event. 01339 * @param Timeout: Timeout value in millisecond. 01340 * @retval HAL status 01341 */ 01342 HAL_StatusTypeDef HAL_ADC_PollForEvent(ADC_HandleTypeDef* hadc, uint32_t EventType, uint32_t Timeout) 01343 { 01344 uint32_t tickstart = 0U; 01345 01346 /* Check the parameters */ 01347 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance)); 01348 assert_param(IS_ADC_EVENT_TYPE(EventType)); 01349 01350 /* Get tick count */ 01351 tickstart = HAL_GetTick(); 01352 01353 /* Check selected event flag */ 01354 while(__HAL_ADC_GET_FLAG(hadc, EventType) == RESET) 01355 { 01356 /* Check if timeout is disabled (set to infinite wait) */ 01357 if(Timeout != HAL_MAX_DELAY) 01358 { 01359 if((Timeout == 0U) || ((HAL_GetTick() - tickstart ) > Timeout)) 01360 { 01361 /* New check to avoid false timeout detection in case of preemption */ 01362 if(__HAL_ADC_GET_FLAG(hadc, EventType) == RESET) 01363 { 01364 /* Update ADC state machine to timeout */ 01365 SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT); 01366 01367 /* Process unlocked */ 01368 __HAL_UNLOCK(hadc); 01369 01370 return HAL_TIMEOUT; 01371 } 01372 } 01373 } 01374 } 01375 01376 /* Analog watchdog (level out of window) event */ 01377 /* Set ADC state */ 01378 SET_BIT(hadc->State, HAL_ADC_STATE_AWD1); 01379 01380 /* Clear ADC analog watchdog flag */ 01381 __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD); 01382 01383 /* Return ADC state */ 01384 return HAL_OK; 01385 } 01386 01387 /** 01388 * @brief Enables ADC, starts conversion of regular group with interruption. 01389 * Interruptions enabled in this function: 01390 * - EOC (end of conversion of regular group) 01391 * Each of these interruptions has its dedicated callback function. 01392 * @param hadc: ADC handle 01393 * @retval HAL status 01394 */ 01395 HAL_StatusTypeDef HAL_ADC_Start_IT(ADC_HandleTypeDef* hadc) 01396 { 01397 HAL_StatusTypeDef tmp_hal_status = HAL_OK; 01398 01399 /* Check the parameters */ 01400 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance)); 01401 01402 /* Process locked */ 01403 __HAL_LOCK(hadc); 01404 01405 /* Enable the ADC peripheral */ 01406 tmp_hal_status = ADC_Enable(hadc); 01407 01408 /* Start conversion if ADC is effectively enabled */ 01409 if (tmp_hal_status == HAL_OK) 01410 { 01411 /* Set ADC state */ 01412 /* - Clear state bitfield related to regular group conversion results */ 01413 /* - Set state bitfield related to regular operation */ 01414 ADC_STATE_CLR_SET(hadc->State, 01415 HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP, 01416 HAL_ADC_STATE_REG_BUSY); 01417 01418 /* Set group injected state (from auto-injection) and multimode state */ 01419 /* for all cases of multimode: independent mode, multimode ADC master */ 01420 /* or multimode ADC slave (for devices with several ADCs): */ 01421 if (ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc)) 01422 { 01423 /* Set ADC state (ADC independent or master) */ 01424 CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE); 01425 01426 /* If conversions on group regular are also triggering group injected, */ 01427 /* update ADC state. */ 01428 if (READ_BIT(hadc->Instance->CR1, ADC_CR1_JAUTO) != RESET) 01429 { 01430 ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY); 01431 } 01432 } 01433 else 01434 { 01435 /* Set ADC state (ADC slave) */ 01436 SET_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE); 01437 01438 /* If conversions on group regular are also triggering group injected, */ 01439 /* update ADC state. */ 01440 if (ADC_MULTIMODE_AUTO_INJECTED(hadc)) 01441 { 01442 ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY); 01443 } 01444 } 01445 01446 /* State machine update: Check if an injected conversion is ongoing */ 01447 if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_INJ_BUSY)) 01448 { 01449 /* Reset ADC error code fields related to conversions on group regular */ 01450 CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA)); 01451 } 01452 else 01453 { 01454 /* Reset ADC all error code fields */ 01455 ADC_CLEAR_ERRORCODE(hadc); 01456 } 01457 01458 /* Process unlocked */ 01459 /* Unlock before starting ADC conversions: in case of potential */ 01460 /* interruption, to let the process to ADC IRQ Handler. */ 01461 __HAL_UNLOCK(hadc); 01462 01463 /* Clear regular group conversion flag and overrun flag */ 01464 /* (To ensure of no unknown state from potential previous ADC operations) */ 01465 __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOC); 01466 01467 /* Enable end of conversion interrupt for regular group */ 01468 __HAL_ADC_ENABLE_IT(hadc, ADC_IT_EOC); 01469 01470 /* Enable conversion of regular group. */ 01471 /* If software start has been selected, conversion starts immediately. */ 01472 /* If external trigger has been selected, conversion will start at next */ 01473 /* trigger event. */ 01474 /* Case of multimode enabled: */ 01475 /* - if ADC is slave, ADC is enabled only (conversion is not started). */ 01476 /* - if ADC is master, ADC is enabled and conversion is started. */ 01477 if (ADC_IS_SOFTWARE_START_REGULAR(hadc) && 01478 ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc) ) 01479 { 01480 /* Start ADC conversion on regular group with SW start */ 01481 SET_BIT(hadc->Instance->CR2, (ADC_CR2_SWSTART | ADC_CR2_EXTTRIG)); 01482 } 01483 else 01484 { 01485 /* Start ADC conversion on regular group with external trigger */ 01486 SET_BIT(hadc->Instance->CR2, ADC_CR2_EXTTRIG); 01487 } 01488 } 01489 else 01490 { 01491 /* Process unlocked */ 01492 __HAL_UNLOCK(hadc); 01493 } 01494 01495 /* Return function status */ 01496 return tmp_hal_status; 01497 } 01498 01499 /** 01500 * @brief Stop ADC conversion of regular group (and injected group in 01501 * case of auto_injection mode), disable interrution of 01502 * end-of-conversion, disable ADC peripheral. 01503 * @param hadc: ADC handle 01504 * @retval None 01505 */ 01506 HAL_StatusTypeDef HAL_ADC_Stop_IT(ADC_HandleTypeDef* hadc) 01507 { 01508 HAL_StatusTypeDef tmp_hal_status = HAL_OK; 01509 01510 /* Check the parameters */ 01511 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance)); 01512 01513 /* Process locked */ 01514 __HAL_LOCK(hadc); 01515 01516 /* Stop potential conversion on going, on regular and injected groups */ 01517 /* Disable ADC peripheral */ 01518 tmp_hal_status = ADC_ConversionStop_Disable(hadc); 01519 01520 /* Check if ADC is effectively disabled */ 01521 if (tmp_hal_status == HAL_OK) 01522 { 01523 /* Disable ADC end of conversion interrupt for regular group */ 01524 __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC); 01525 01526 /* Set ADC state */ 01527 ADC_STATE_CLR_SET(hadc->State, 01528 HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY, 01529 HAL_ADC_STATE_READY); 01530 } 01531 01532 /* Process unlocked */ 01533 __HAL_UNLOCK(hadc); 01534 01535 /* Return function status */ 01536 return tmp_hal_status; 01537 } 01538 01539 /** 01540 * @brief Enables ADC, starts conversion of regular group and transfers result 01541 * through DMA. 01542 * Interruptions enabled in this function: 01543 * - DMA transfer complete 01544 * - DMA half transfer 01545 * Each of these interruptions has its dedicated callback function. 01546 * @note For devices with several ADCs: This function is for single-ADC mode 01547 * only. For multimode, use the dedicated MultimodeStart function. 01548 * @note On STM32F1 devices, only ADC1 and ADC3 (ADC availability depending 01549 * on devices) have DMA capability. 01550 * ADC2 converted data can be transferred in dual ADC mode using DMA 01551 * of ADC1 (ADC master in multimode). 01552 * In case of using ADC1 with DMA on a device featuring 2 ADC 01553 * instances: ADC1 conversion register DR contains ADC1 conversion 01554 * result (ADC1 register DR bits 0 to 11) and, additionally, ADC2 last 01555 * conversion result (ADC1 register DR bits 16 to 27). Therefore, to 01556 * have DMA transferring the conversion results of ADC1 only, DMA must 01557 * be configured to transfer size: half word. 01558 * @param hadc: ADC handle 01559 * @param pData: The destination Buffer address. 01560 * @param Length: The length of data to be transferred from ADC peripheral to memory. 01561 * @retval None 01562 */ 01563 HAL_StatusTypeDef HAL_ADC_Start_DMA(ADC_HandleTypeDef* hadc, uint32_t* pData, uint32_t Length) 01564 { 01565 HAL_StatusTypeDef tmp_hal_status = HAL_OK; 01566 01567 /* Check the parameters */ 01568 assert_param(IS_ADC_DMA_CAPABILITY_INSTANCE(hadc->Instance)); 01569 01570 /* Verification if multimode is disabled (for devices with several ADC) */ 01571 /* If multimode is enabled, dedicated function multimode conversion */ 01572 /* start DMA must be used. */ 01573 if(ADC_MULTIMODE_IS_ENABLE(hadc) == RESET) 01574 { 01575 /* Process locked */ 01576 __HAL_LOCK(hadc); 01577 01578 /* Enable the ADC peripheral */ 01579 tmp_hal_status = ADC_Enable(hadc); 01580 01581 /* Start conversion if ADC is effectively enabled */ 01582 if (tmp_hal_status == HAL_OK) 01583 { 01584 /* Set ADC state */ 01585 /* - Clear state bitfield related to regular group conversion results */ 01586 /* - Set state bitfield related to regular operation */ 01587 ADC_STATE_CLR_SET(hadc->State, 01588 HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP, 01589 HAL_ADC_STATE_REG_BUSY); 01590 01591 /* Set group injected state (from auto-injection) and multimode state */ 01592 /* for all cases of multimode: independent mode, multimode ADC master */ 01593 /* or multimode ADC slave (for devices with several ADCs): */ 01594 if (ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc)) 01595 { 01596 /* Set ADC state (ADC independent or master) */ 01597 CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE); 01598 01599 /* If conversions on group regular are also triggering group injected, */ 01600 /* update ADC state. */ 01601 if (READ_BIT(hadc->Instance->CR1, ADC_CR1_JAUTO) != RESET) 01602 { 01603 ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY); 01604 } 01605 } 01606 else 01607 { 01608 /* Set ADC state (ADC slave) */ 01609 SET_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE); 01610 01611 /* If conversions on group regular are also triggering group injected, */ 01612 /* update ADC state. */ 01613 if (ADC_MULTIMODE_AUTO_INJECTED(hadc)) 01614 { 01615 ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY); 01616 } 01617 } 01618 01619 /* State machine update: Check if an injected conversion is ongoing */ 01620 if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_INJ_BUSY)) 01621 { 01622 /* Reset ADC error code fields related to conversions on group regular */ 01623 CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA)); 01624 } 01625 else 01626 { 01627 /* Reset ADC all error code fields */ 01628 ADC_CLEAR_ERRORCODE(hadc); 01629 } 01630 01631 /* Process unlocked */ 01632 /* Unlock before starting ADC conversions: in case of potential */ 01633 /* interruption, to let the process to ADC IRQ Handler. */ 01634 __HAL_UNLOCK(hadc); 01635 01636 /* Set the DMA transfer complete callback */ 01637 hadc->DMA_Handle->XferCpltCallback = ADC_DMAConvCplt; 01638 01639 /* Set the DMA half transfer complete callback */ 01640 hadc->DMA_Handle->XferHalfCpltCallback = ADC_DMAHalfConvCplt; 01641 01642 /* Set the DMA error callback */ 01643 hadc->DMA_Handle->XferErrorCallback = ADC_DMAError; 01644 01645 01646 /* Manage ADC and DMA start: ADC overrun interruption, DMA start, ADC */ 01647 /* start (in case of SW start): */ 01648 01649 /* Clear regular group conversion flag and overrun flag */ 01650 /* (To ensure of no unknown state from potential previous ADC */ 01651 /* operations) */ 01652 __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOC); 01653 01654 /* Enable ADC DMA mode */ 01655 SET_BIT(hadc->Instance->CR2, ADC_CR2_DMA); 01656 01657 /* Start the DMA channel */ 01658 HAL_DMA_Start_IT(hadc->DMA_Handle, (uint32_t)&hadc->Instance->DR, (uint32_t)pData, Length); 01659 01660 /* Enable conversion of regular group. */ 01661 /* If software start has been selected, conversion starts immediately. */ 01662 /* If external trigger has been selected, conversion will start at next */ 01663 /* trigger event. */ 01664 if (ADC_IS_SOFTWARE_START_REGULAR(hadc)) 01665 { 01666 /* Start ADC conversion on regular group with SW start */ 01667 SET_BIT(hadc->Instance->CR2, (ADC_CR2_SWSTART | ADC_CR2_EXTTRIG)); 01668 } 01669 else 01670 { 01671 /* Start ADC conversion on regular group with external trigger */ 01672 SET_BIT(hadc->Instance->CR2, ADC_CR2_EXTTRIG); 01673 } 01674 } 01675 else 01676 { 01677 /* Process unlocked */ 01678 __HAL_UNLOCK(hadc); 01679 } 01680 } 01681 else 01682 { 01683 tmp_hal_status = HAL_ERROR; 01684 } 01685 01686 /* Return function status */ 01687 return tmp_hal_status; 01688 } 01689 01690 /** 01691 * @brief Stop ADC conversion of regular group (and injected group in 01692 * case of auto_injection mode), disable ADC DMA transfer, disable 01693 * ADC peripheral. 01694 * @note: ADC peripheral disable is forcing stop of potential 01695 * conversion on injected group. If injected group is under use, it 01696 * should be preliminarily stopped using HAL_ADCEx_InjectedStop function. 01697 * @note For devices with several ADCs: This function is for single-ADC mode 01698 * only. For multimode, use the dedicated MultimodeStop function. 01699 * @note On STM32F1 devices, only ADC1 and ADC3 (ADC availability depending 01700 * on devices) have DMA capability. 01701 * @param hadc: ADC handle 01702 * @retval HAL status. 01703 */ 01704 HAL_StatusTypeDef HAL_ADC_Stop_DMA(ADC_HandleTypeDef* hadc) 01705 { 01706 HAL_StatusTypeDef tmp_hal_status = HAL_OK; 01707 01708 /* Check the parameters */ 01709 assert_param(IS_ADC_DMA_CAPABILITY_INSTANCE(hadc->Instance)); 01710 01711 /* Process locked */ 01712 __HAL_LOCK(hadc); 01713 01714 /* Stop potential conversion on going, on regular and injected groups */ 01715 /* Disable ADC peripheral */ 01716 tmp_hal_status = ADC_ConversionStop_Disable(hadc); 01717 01718 /* Check if ADC is effectively disabled */ 01719 if (tmp_hal_status == HAL_OK) 01720 { 01721 /* Disable ADC DMA mode */ 01722 CLEAR_BIT(hadc->Instance->CR2, ADC_CR2_DMA); 01723 01724 /* Disable the DMA channel (in case of DMA in circular mode or stop while */ 01725 /* DMA transfer is on going) */ 01726 if (hadc->DMA_Handle->State == HAL_DMA_STATE_BUSY) 01727 { 01728 tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle); 01729 01730 /* Check if DMA channel effectively disabled */ 01731 if (tmp_hal_status == HAL_OK) 01732 { 01733 /* Set ADC state */ 01734 ADC_STATE_CLR_SET(hadc->State, 01735 HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY, 01736 HAL_ADC_STATE_READY); 01737 } 01738 else 01739 { 01740 /* Update ADC state machine to error */ 01741 SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA); 01742 } 01743 } 01744 } 01745 01746 /* Process unlocked */ 01747 __HAL_UNLOCK(hadc); 01748 01749 /* Return function status */ 01750 return tmp_hal_status; 01751 } 01752 01753 /** 01754 * @brief Get ADC regular group conversion result. 01755 * @note Reading register DR automatically clears ADC flag EOC 01756 * (ADC group regular end of unitary conversion). 01757 * @note This function does not clear ADC flag EOS 01758 * (ADC group regular end of sequence conversion). 01759 * Occurrence of flag EOS rising: 01760 * - If sequencer is composed of 1 rank, flag EOS is equivalent 01761 * to flag EOC. 01762 * - If sequencer is composed of several ranks, during the scan 01763 * sequence flag EOC only is raised, at the end of the scan sequence 01764 * both flags EOC and EOS are raised. 01765 * To clear this flag, either use function: 01766 * in programming model IT: @ref HAL_ADC_IRQHandler(), in programming 01767 * model polling: @ref HAL_ADC_PollForConversion() 01768 * or @ref __HAL_ADC_CLEAR_FLAG(&hadc, ADC_FLAG_EOS). 01769 * @param hadc: ADC handle 01770 * @retval ADC group regular conversion data 01771 */ 01772 uint32_t HAL_ADC_GetValue(ADC_HandleTypeDef* hadc) 01773 { 01774 /* Check the parameters */ 01775 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance)); 01776 01777 /* Note: EOC flag is not cleared here by software because automatically */ 01778 /* cleared by hardware when reading register DR. */ 01779 01780 /* Return ADC converted value */ 01781 return hadc->Instance->DR; 01782 } 01783 01784 /** 01785 * @brief Handles ADC interrupt request 01786 * @param hadc: ADC handle 01787 * @retval None 01788 */ 01789 void HAL_ADC_IRQHandler(ADC_HandleTypeDef* hadc) 01790 { 01791 /* Check the parameters */ 01792 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance)); 01793 assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode)); 01794 assert_param(IS_ADC_REGULAR_NB_CONV(hadc->Init.NbrOfConversion)); 01795 01796 01797 /* ========== Check End of Conversion flag for regular group ========== */ 01798 if(__HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_EOC)) 01799 { 01800 if(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOC) ) 01801 { 01802 /* Update state machine on conversion status if not in error state */ 01803 if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL)) 01804 { 01805 /* Set ADC state */ 01806 SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC); 01807 } 01808 01809 /* Determine whether any further conversion upcoming on group regular */ 01810 /* by external trigger, continuous mode or scan sequence on going. */ 01811 /* Note: On STM32F1 devices, in case of sequencer enabled */ 01812 /* (several ranks selected), end of conversion flag is raised */ 01813 /* at the end of the sequence. */ 01814 if(ADC_IS_SOFTWARE_START_REGULAR(hadc) && 01815 (hadc->Init.ContinuousConvMode == DISABLE) ) 01816 { 01817 /* Disable ADC end of conversion interrupt on group regular */ 01818 __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC); 01819 01820 /* Set ADC state */ 01821 CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY); 01822 01823 if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_INJ_BUSY)) 01824 { 01825 SET_BIT(hadc->State, HAL_ADC_STATE_READY); 01826 } 01827 } 01828 01829 /* Conversion complete callback */ 01830 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1) 01831 hadc->ConvCpltCallback(hadc); 01832 #else 01833 HAL_ADC_ConvCpltCallback(hadc); 01834 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */ 01835 01836 /* Clear regular group conversion flag */ 01837 __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_STRT | ADC_FLAG_EOC); 01838 } 01839 } 01840 01841 /* ========== Check End of Conversion flag for injected group ========== */ 01842 if(__HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_JEOC)) 01843 { 01844 if(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_JEOC)) 01845 { 01846 /* Update state machine on conversion status if not in error state */ 01847 if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL)) 01848 { 01849 /* Set ADC state */ 01850 SET_BIT(hadc->State, HAL_ADC_STATE_INJ_EOC); 01851 } 01852 01853 /* Determine whether any further conversion upcoming on group injected */ 01854 /* by external trigger, scan sequence on going or by automatic injected */ 01855 /* conversion from group regular (same conditions as group regular */ 01856 /* interruption disabling above). */ 01857 /* Note: On STM32F1 devices, in case of sequencer enabled */ 01858 /* (several ranks selected), end of conversion flag is raised */ 01859 /* at the end of the sequence. */ 01860 if(ADC_IS_SOFTWARE_START_INJECTED(hadc) || 01861 (HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO) && 01862 (ADC_IS_SOFTWARE_START_REGULAR(hadc) && 01863 (hadc->Init.ContinuousConvMode == DISABLE) ) ) ) 01864 { 01865 /* Disable ADC end of conversion interrupt on group injected */ 01866 __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC); 01867 01868 /* Set ADC state */ 01869 CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY); 01870 01871 if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_REG_BUSY)) 01872 { 01873 SET_BIT(hadc->State, HAL_ADC_STATE_READY); 01874 } 01875 } 01876 01877 /* Conversion complete callback */ 01878 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1) 01879 hadc->InjectedConvCpltCallback(hadc); 01880 #else 01881 HAL_ADCEx_InjectedConvCpltCallback(hadc); 01882 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */ 01883 01884 /* Clear injected group conversion flag */ 01885 __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_JSTRT | ADC_FLAG_JEOC)); 01886 } 01887 } 01888 01889 /* ========== Check Analog watchdog flags ========== */ 01890 if(__HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_AWD)) 01891 { 01892 if(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_AWD)) 01893 { 01894 /* Set ADC state */ 01895 SET_BIT(hadc->State, HAL_ADC_STATE_AWD1); 01896 01897 /* Level out of window callback */ 01898 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1) 01899 hadc->LevelOutOfWindowCallback(hadc); 01900 #else 01901 HAL_ADC_LevelOutOfWindowCallback(hadc); 01902 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */ 01903 01904 /* Clear the ADC analog watchdog flag */ 01905 __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD); 01906 } 01907 } 01908 01909 } 01910 01911 /** 01912 * @brief Conversion complete callback in non blocking mode 01913 * @param hadc: ADC handle 01914 * @retval None 01915 */ 01916 __weak void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) 01917 { 01918 /* Prevent unused argument(s) compilation warning */ 01919 UNUSED(hadc); 01920 /* NOTE : This function should not be modified. When the callback is needed, 01921 function HAL_ADC_ConvCpltCallback must be implemented in the user file. 01922 */ 01923 } 01924 01925 /** 01926 * @brief Conversion DMA half-transfer callback in non blocking mode 01927 * @param hadc: ADC handle 01928 * @retval None 01929 */ 01930 __weak void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc) 01931 { 01932 /* Prevent unused argument(s) compilation warning */ 01933 UNUSED(hadc); 01934 /* NOTE : This function should not be modified. When the callback is needed, 01935 function HAL_ADC_ConvHalfCpltCallback must be implemented in the user file. 01936 */ 01937 } 01938 01939 /** 01940 * @brief Analog watchdog callback in non blocking mode. 01941 * @param hadc: ADC handle 01942 * @retval None 01943 */ 01944 __weak void HAL_ADC_LevelOutOfWindowCallback(ADC_HandleTypeDef* hadc) 01945 { 01946 /* Prevent unused argument(s) compilation warning */ 01947 UNUSED(hadc); 01948 /* NOTE : This function should not be modified. When the callback is needed, 01949 function HAL_ADC_LevelOutOfWindowCallback must be implemented in the user file. 01950 */ 01951 } 01952 01953 /** 01954 * @brief ADC error callback in non blocking mode 01955 * (ADC conversion with interruption or transfer by DMA) 01956 * @param hadc: ADC handle 01957 * @retval None 01958 */ 01959 __weak void HAL_ADC_ErrorCallback(ADC_HandleTypeDef *hadc) 01960 { 01961 /* Prevent unused argument(s) compilation warning */ 01962 UNUSED(hadc); 01963 /* NOTE : This function should not be modified. When the callback is needed, 01964 function HAL_ADC_ErrorCallback must be implemented in the user file. 01965 */ 01966 } 01967 01968 01969 /** 01970 * @} 01971 */ 01972 01973 /** @defgroup ADC_Exported_Functions_Group3 Peripheral Control functions 01974 * @brief Peripheral Control functions 01975 * 01976 @verbatim 01977 =============================================================================== 01978 ##### Peripheral Control functions ##### 01979 =============================================================================== 01980 [..] This section provides functions allowing to: 01981 (+) Configure channels on regular group 01982 (+) Configure the analog watchdog 01983 01984 @endverbatim 01985 * @{ 01986 */ 01987 01988 /** 01989 * @brief Configures the the selected channel to be linked to the regular 01990 * group. 01991 * @note In case of usage of internal measurement channels: 01992 * Vbat/VrefInt/TempSensor. 01993 * These internal paths can be be disabled using function 01994 * HAL_ADC_DeInit(). 01995 * @note Possibility to update parameters on the fly: 01996 * This function initializes channel into regular group, following 01997 * calls to this function can be used to reconfigure some parameters 01998 * of structure "ADC_ChannelConfTypeDef" on the fly, without reseting 01999 * the ADC. 02000 * The setting of these parameters is conditioned to ADC state. 02001 * For parameters constraints, see comments of structure 02002 * "ADC_ChannelConfTypeDef". 02003 * @param hadc: ADC handle 02004 * @param sConfig: Structure of ADC channel for regular group. 02005 * @retval HAL status 02006 */ 02007 HAL_StatusTypeDef HAL_ADC_ConfigChannel(ADC_HandleTypeDef* hadc, ADC_ChannelConfTypeDef* sConfig) 02008 { 02009 HAL_StatusTypeDef tmp_hal_status = HAL_OK; 02010 __IO uint32_t wait_loop_index = 0U; 02011 02012 /* Check the parameters */ 02013 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance)); 02014 assert_param(IS_ADC_CHANNEL(sConfig->Channel)); 02015 assert_param(IS_ADC_REGULAR_RANK(sConfig->Rank)); 02016 assert_param(IS_ADC_SAMPLE_TIME(sConfig->SamplingTime)); 02017 02018 /* Process locked */ 02019 __HAL_LOCK(hadc); 02020 02021 02022 /* Regular sequence configuration */ 02023 /* For Rank 1 to 6 */ 02024 if (sConfig->Rank < 7U) 02025 { 02026 MODIFY_REG(hadc->Instance->SQR3 , 02027 ADC_SQR3_RK(ADC_SQR3_SQ1, sConfig->Rank) , 02028 ADC_SQR3_RK(sConfig->Channel, sConfig->Rank) ); 02029 } 02030 /* For Rank 7 to 12 */ 02031 else if (sConfig->Rank < 13U) 02032 { 02033 MODIFY_REG(hadc->Instance->SQR2 , 02034 ADC_SQR2_RK(ADC_SQR2_SQ7, sConfig->Rank) , 02035 ADC_SQR2_RK(sConfig->Channel, sConfig->Rank) ); 02036 } 02037 /* For Rank 13 to 16 */ 02038 else 02039 { 02040 MODIFY_REG(hadc->Instance->SQR1 , 02041 ADC_SQR1_RK(ADC_SQR1_SQ13, sConfig->Rank) , 02042 ADC_SQR1_RK(sConfig->Channel, sConfig->Rank) ); 02043 } 02044 02045 02046 /* Channel sampling time configuration */ 02047 /* For channels 10 to 17 */ 02048 if (sConfig->Channel >= ADC_CHANNEL_10) 02049 { 02050 MODIFY_REG(hadc->Instance->SMPR1 , 02051 ADC_SMPR1(ADC_SMPR1_SMP10, sConfig->Channel) , 02052 ADC_SMPR1(sConfig->SamplingTime, sConfig->Channel) ); 02053 } 02054 else /* For channels 0 to 9 */ 02055 { 02056 MODIFY_REG(hadc->Instance->SMPR2 , 02057 ADC_SMPR2(ADC_SMPR2_SMP0, sConfig->Channel) , 02058 ADC_SMPR2(sConfig->SamplingTime, sConfig->Channel) ); 02059 } 02060 02061 /* If ADC1 Channel_16 or Channel_17 is selected, enable Temperature sensor */ 02062 /* and VREFINT measurement path. */ 02063 if ((sConfig->Channel == ADC_CHANNEL_TEMPSENSOR) || 02064 (sConfig->Channel == ADC_CHANNEL_VREFINT) ) 02065 { 02066 /* For STM32F1 devices with several ADC: Only ADC1 can access internal */ 02067 /* measurement channels (VrefInt/TempSensor). If these channels are */ 02068 /* intended to be set on other ADC instances, an error is reported. */ 02069 if (hadc->Instance == ADC1) 02070 { 02071 if (READ_BIT(hadc->Instance->CR2, ADC_CR2_TSVREFE) == RESET) 02072 { 02073 SET_BIT(hadc->Instance->CR2, ADC_CR2_TSVREFE); 02074 02075 if (sConfig->Channel == ADC_CHANNEL_TEMPSENSOR) 02076 { 02077 /* Delay for temperature sensor stabilization time */ 02078 /* Compute number of CPU cycles to wait for */ 02079 wait_loop_index = (ADC_TEMPSENSOR_DELAY_US * (SystemCoreClock / 1000000U)); 02080 while(wait_loop_index != 0U) 02081 { 02082 wait_loop_index--; 02083 } 02084 } 02085 } 02086 } 02087 else 02088 { 02089 /* Update ADC state machine to error */ 02090 SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG); 02091 02092 tmp_hal_status = HAL_ERROR; 02093 } 02094 } 02095 02096 /* Process unlocked */ 02097 __HAL_UNLOCK(hadc); 02098 02099 /* Return function status */ 02100 return tmp_hal_status; 02101 } 02102 02103 /** 02104 * @brief Configures the analog watchdog. 02105 * @note Analog watchdog thresholds can be modified while ADC conversion 02106 * is on going. 02107 * In this case, some constraints must be taken into account: 02108 * the programmed threshold values are effective from the next 02109 * ADC EOC (end of unitary conversion). 02110 * Considering that registers write delay may happen due to 02111 * bus activity, this might cause an uncertainty on the 02112 * effective timing of the new programmed threshold values. 02113 * @param hadc: ADC handle 02114 * @param AnalogWDGConfig: Structure of ADC analog watchdog configuration 02115 * @retval HAL status 02116 */ 02117 HAL_StatusTypeDef HAL_ADC_AnalogWDGConfig(ADC_HandleTypeDef* hadc, ADC_AnalogWDGConfTypeDef* AnalogWDGConfig) 02118 { 02119 /* Check the parameters */ 02120 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance)); 02121 assert_param(IS_ADC_ANALOG_WATCHDOG_MODE(AnalogWDGConfig->WatchdogMode)); 02122 assert_param(IS_FUNCTIONAL_STATE(AnalogWDGConfig->ITMode)); 02123 assert_param(IS_ADC_RANGE(AnalogWDGConfig->HighThreshold)); 02124 assert_param(IS_ADC_RANGE(AnalogWDGConfig->LowThreshold)); 02125 02126 if((AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_REG) || 02127 (AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_INJEC) || 02128 (AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_REGINJEC) ) 02129 { 02130 assert_param(IS_ADC_CHANNEL(AnalogWDGConfig->Channel)); 02131 } 02132 02133 /* Process locked */ 02134 __HAL_LOCK(hadc); 02135 02136 /* Analog watchdog configuration */ 02137 02138 /* Configure ADC Analog watchdog interrupt */ 02139 if(AnalogWDGConfig->ITMode == ENABLE) 02140 { 02141 /* Enable the ADC Analog watchdog interrupt */ 02142 __HAL_ADC_ENABLE_IT(hadc, ADC_IT_AWD); 02143 } 02144 else 02145 { 02146 /* Disable the ADC Analog watchdog interrupt */ 02147 __HAL_ADC_DISABLE_IT(hadc, ADC_IT_AWD); 02148 } 02149 02150 /* Configuration of analog watchdog: */ 02151 /* - Set the analog watchdog enable mode: regular and/or injected groups, */ 02152 /* one or all channels. */ 02153 /* - Set the Analog watchdog channel (is not used if watchdog */ 02154 /* mode "all channels": ADC_CFGR_AWD1SGL=0). */ 02155 MODIFY_REG(hadc->Instance->CR1 , 02156 ADC_CR1_AWDSGL | 02157 ADC_CR1_JAWDEN | 02158 ADC_CR1_AWDEN | 02159 ADC_CR1_AWDCH , 02160 AnalogWDGConfig->WatchdogMode | 02161 AnalogWDGConfig->Channel ); 02162 02163 /* Set the high threshold */ 02164 WRITE_REG(hadc->Instance->HTR, AnalogWDGConfig->HighThreshold); 02165 02166 /* Set the low threshold */ 02167 WRITE_REG(hadc->Instance->LTR, AnalogWDGConfig->LowThreshold); 02168 02169 /* Process unlocked */ 02170 __HAL_UNLOCK(hadc); 02171 02172 /* Return function status */ 02173 return HAL_OK; 02174 } 02175 02176 02177 /** 02178 * @} 02179 */ 02180 02181 02182 /** @defgroup ADC_Exported_Functions_Group4 Peripheral State functions 02183 * @brief Peripheral State functions 02184 * 02185 @verbatim 02186 =============================================================================== 02187 ##### Peripheral State and Errors functions ##### 02188 =============================================================================== 02189 [..] 02190 This subsection provides functions to get in run-time the status of the 02191 peripheral. 02192 (+) Check the ADC state 02193 (+) Check the ADC error code 02194 02195 @endverbatim 02196 * @{ 02197 */ 02198 02199 /** 02200 * @brief return the ADC state 02201 * @param hadc: ADC handle 02202 * @retval HAL state 02203 */ 02204 uint32_t HAL_ADC_GetState(ADC_HandleTypeDef* hadc) 02205 { 02206 /* Return ADC state */ 02207 return hadc->State; 02208 } 02209 02210 /** 02211 * @brief Return the ADC error code 02212 * @param hadc: ADC handle 02213 * @retval ADC Error Code 02214 */ 02215 uint32_t HAL_ADC_GetError(ADC_HandleTypeDef *hadc) 02216 { 02217 return hadc->ErrorCode; 02218 } 02219 02220 /** 02221 * @} 02222 */ 02223 02224 /** 02225 * @} 02226 */ 02227 02228 /** @defgroup ADC_Private_Functions ADC Private Functions 02229 * @{ 02230 */ 02231 02232 /** 02233 * @brief Enable the selected ADC. 02234 * @note Prerequisite condition to use this function: ADC must be disabled 02235 * and voltage regulator must be enabled (done into HAL_ADC_Init()). 02236 * @param hadc: ADC handle 02237 * @retval HAL status. 02238 */ 02239 HAL_StatusTypeDef ADC_Enable(ADC_HandleTypeDef* hadc) 02240 { 02241 uint32_t tickstart = 0U; 02242 __IO uint32_t wait_loop_index = 0U; 02243 02244 /* ADC enable and wait for ADC ready (in case of ADC is disabled or */ 02245 /* enabling phase not yet completed: flag ADC ready not yet set). */ 02246 /* Timeout implemented to not be stuck if ADC cannot be enabled (possible */ 02247 /* causes: ADC clock not running, ...). */ 02248 if (ADC_IS_ENABLE(hadc) == RESET) 02249 { 02250 /* Enable the Peripheral */ 02251 __HAL_ADC_ENABLE(hadc); 02252 02253 /* Delay for ADC stabilization time */ 02254 /* Compute number of CPU cycles to wait for */ 02255 wait_loop_index = (ADC_STAB_DELAY_US * (SystemCoreClock / 1000000U)); 02256 while(wait_loop_index != 0U) 02257 { 02258 wait_loop_index--; 02259 } 02260 02261 /* Get tick count */ 02262 tickstart = HAL_GetTick(); 02263 02264 /* Wait for ADC effectively enabled */ 02265 while(ADC_IS_ENABLE(hadc) == RESET) 02266 { 02267 if((HAL_GetTick() - tickstart) > ADC_ENABLE_TIMEOUT) 02268 { 02269 /* New check to avoid false timeout detection in case of preemption */ 02270 if(ADC_IS_ENABLE(hadc) == RESET) 02271 { 02272 /* Update ADC state machine to error */ 02273 SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL); 02274 02275 /* Set ADC error code to ADC IP internal error */ 02276 SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL); 02277 02278 /* Process unlocked */ 02279 __HAL_UNLOCK(hadc); 02280 02281 return HAL_ERROR; 02282 } 02283 } 02284 } 02285 } 02286 02287 /* Return HAL status */ 02288 return HAL_OK; 02289 } 02290 02291 /** 02292 * @brief Stop ADC conversion and disable the selected ADC 02293 * @note Prerequisite condition to use this function: ADC conversions must be 02294 * stopped to disable the ADC. 02295 * @param hadc: ADC handle 02296 * @retval HAL status. 02297 */ 02298 HAL_StatusTypeDef ADC_ConversionStop_Disable(ADC_HandleTypeDef* hadc) 02299 { 02300 uint32_t tickstart = 0U; 02301 02302 /* Verification if ADC is not already disabled */ 02303 if (ADC_IS_ENABLE(hadc) != RESET) 02304 { 02305 /* Disable the ADC peripheral */ 02306 __HAL_ADC_DISABLE(hadc); 02307 02308 /* Get tick count */ 02309 tickstart = HAL_GetTick(); 02310 02311 /* Wait for ADC effectively disabled */ 02312 while(ADC_IS_ENABLE(hadc) != RESET) 02313 { 02314 if((HAL_GetTick() - tickstart) > ADC_DISABLE_TIMEOUT) 02315 { 02316 /* New check to avoid false timeout detection in case of preemption */ 02317 if(ADC_IS_ENABLE(hadc) != RESET) 02318 { 02319 /* Update ADC state machine to error */ 02320 SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL); 02321 02322 /* Set ADC error code to ADC IP internal error */ 02323 SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL); 02324 02325 return HAL_ERROR; 02326 } 02327 } 02328 } 02329 } 02330 02331 /* Return HAL status */ 02332 return HAL_OK; 02333 } 02334 02335 /** 02336 * @brief DMA transfer complete callback. 02337 * @param hdma: pointer to DMA handle. 02338 * @retval None 02339 */ 02340 void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma) 02341 { 02342 /* Retrieve ADC handle corresponding to current DMA handle */ 02343 ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; 02344 02345 /* Update state machine on conversion status if not in error state */ 02346 if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL | HAL_ADC_STATE_ERROR_DMA)) 02347 { 02348 /* Update ADC state machine */ 02349 SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC); 02350 02351 /* Determine whether any further conversion upcoming on group regular */ 02352 /* by external trigger, continuous mode or scan sequence on going. */ 02353 /* Note: On STM32F1 devices, in case of sequencer enabled */ 02354 /* (several ranks selected), end of conversion flag is raised */ 02355 /* at the end of the sequence. */ 02356 if(ADC_IS_SOFTWARE_START_REGULAR(hadc) && 02357 (hadc->Init.ContinuousConvMode == DISABLE) ) 02358 { 02359 /* Set ADC state */ 02360 CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY); 02361 02362 if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_INJ_BUSY)) 02363 { 02364 SET_BIT(hadc->State, HAL_ADC_STATE_READY); 02365 } 02366 } 02367 02368 /* Conversion complete callback */ 02369 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1) 02370 hadc->ConvCpltCallback(hadc); 02371 #else 02372 HAL_ADC_ConvCpltCallback(hadc); 02373 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */ 02374 } 02375 else 02376 { 02377 /* Call DMA error callback */ 02378 hadc->DMA_Handle->XferErrorCallback(hdma); 02379 } 02380 } 02381 02382 /** 02383 * @brief DMA half transfer complete callback. 02384 * @param hdma: pointer to DMA handle. 02385 * @retval None 02386 */ 02387 void ADC_DMAHalfConvCplt(DMA_HandleTypeDef *hdma) 02388 { 02389 /* Retrieve ADC handle corresponding to current DMA handle */ 02390 ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; 02391 02392 /* Half conversion callback */ 02393 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1) 02394 hadc->ConvHalfCpltCallback(hadc); 02395 #else 02396 HAL_ADC_ConvHalfCpltCallback(hadc); 02397 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */ 02398 } 02399 02400 /** 02401 * @brief DMA error callback 02402 * @param hdma: pointer to DMA handle. 02403 * @retval None 02404 */ 02405 void ADC_DMAError(DMA_HandleTypeDef *hdma) 02406 { 02407 /* Retrieve ADC handle corresponding to current DMA handle */ 02408 ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; 02409 02410 /* Set ADC state */ 02411 SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA); 02412 02413 /* Set ADC error code to DMA error */ 02414 SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_DMA); 02415 02416 /* Error callback */ 02417 #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1) 02418 hadc->ErrorCallback(hadc); 02419 #else 02420 HAL_ADC_ErrorCallback(hadc); 02421 #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */ 02422 } 02423 02424 /** 02425 * @} 02426 */ 02427 02428 #endif /* HAL_ADC_MODULE_ENABLED */ 02429 /** 02430 * @} 02431 */ 02432 02433 /** 02434 * @} 02435 */ 02436 02437 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/