Go to most recent revision | Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 2 | mjames | 1 | /** |
| 2 | ****************************************************************************** |
||
| 3 | * @file stm32f1xx_hal_adc.c |
||
| 4 | * @author MCD Application Team |
||
| 5 | * @version V1.0.1 |
||
| 6 | * @date 31-July-2015 |
||
| 7 | * @brief This file provides firmware functions to manage the following |
||
| 8 | * functionalities of the Analog to Digital Convertor (ADC) |
||
| 9 | * peripheral: |
||
| 10 | * + Initialization and de-initialization functions |
||
| 11 | * ++ Initialization and Configuration of ADC |
||
| 12 | * + Operation functions |
||
| 13 | * ++ Start, stop, get result of conversions of regular |
||
| 14 | * group, using 3 possible modes: polling, interruption or DMA. |
||
| 15 | * + Control functions |
||
| 16 | * ++ Channels configuration on regular group |
||
| 17 | * ++ Channels configuration on injected group |
||
| 18 | * ++ Analog Watchdog configuration |
||
| 19 | * + State functions |
||
| 20 | * ++ ADC state machine management |
||
| 21 | * ++ Interrupts and flags management |
||
| 22 | * Other functions (extended functions) are available in file |
||
| 23 | * "stm32f1xx_hal_adc_ex.c". |
||
| 24 | * |
||
| 25 | @verbatim |
||
| 26 | ============================================================================== |
||
| 27 | ##### ADC peripheral features ##### |
||
| 28 | ============================================================================== |
||
| 29 | [..] |
||
| 30 | (+) 12-bit resolution |
||
| 31 | |||
| 32 | (+) Interrupt generation at the end of regular conversion, end of injected |
||
| 33 | conversion, and in case of analog watchdog or overrun events. |
||
| 34 | |||
| 35 | (+) Single and continuous conversion modes. |
||
| 36 | |||
| 37 | (+) Scan mode for conversion of several channels sequentially. |
||
| 38 | |||
| 39 | (+) Data alignment with in-built data coherency. |
||
| 40 | |||
| 41 | (+) Programmable sampling time (channel wise) |
||
| 42 | |||
| 43 | (+) ADC conversion of regular group and injected group. |
||
| 44 | |||
| 45 | (+) External trigger (timer or EXTI) |
||
| 46 | for both regular and injected groups. |
||
| 47 | |||
| 48 | (+) DMA request generation for transfer of conversions data of regular group. |
||
| 49 | |||
| 50 | (+) Multimode Dual mode (available on devices with 2 ADCs or more). |
||
| 51 | |||
| 52 | (+) Configurable DMA data storage in Multimode Dual mode (available on devices |
||
| 53 | with 2 DCs or more). |
||
| 54 | |||
| 55 | (+) Configurable delay between conversions in Dual interleaved mode (available |
||
| 56 | on devices with 2 DCs or more). |
||
| 57 | |||
| 58 | (+) ADC calibration |
||
| 59 | |||
| 60 | (+) ADC supply requirements: 2.4 V to 3.6 V at full speed and down to 1.8 V at |
||
| 61 | slower speed. |
||
| 62 | |||
| 63 | (+) ADC input range: from Vref- (connected to Vssa) to Vref+ (connected to |
||
| 64 | Vdda or to an external voltage reference). |
||
| 65 | |||
| 66 | |||
| 67 | ##### How to use this driver ##### |
||
| 68 | ============================================================================== |
||
| 69 | [..] |
||
| 70 | |||
| 71 | *** Configuration of top level parameters related to ADC *** |
||
| 72 | ============================================================ |
||
| 73 | [..] |
||
| 74 | |||
| 75 | (#) Enable the ADC interface |
||
| 76 | (++) As prerequisite, ADC clock must be configured at RCC top level. |
||
| 77 | Caution: On STM32F1, ADC clock frequency max is 14MHz (refer |
||
| 78 | to device datasheet). |
||
| 79 | Therefore, ADC clock prescaler must be configured in |
||
| 80 | function of ADC clock source frequency to remain below |
||
| 81 | this maximum frequency. |
||
| 82 | (++) One clock setting is mandatory: |
||
| 83 | ADC clock (core clock, also possibly conversion clock). |
||
| 84 | (+++) Example: |
||
| 85 | Into HAL_ADC_MspInit() (recommended code location) or with |
||
| 86 | other device clock parameters configuration: |
||
| 87 | (+++) RCC_PeriphCLKInitTypeDef PeriphClkInit; |
||
| 88 | (+++) __ADC1_CLK_ENABLE(); |
||
| 89 | (+++) PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC; |
||
| 90 | (+++) PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV2; |
||
| 91 | (+++) HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit); |
||
| 92 | |||
| 93 | (#) ADC pins configuration |
||
| 94 | (++) Enable the clock for the ADC GPIOs |
||
| 95 | using macro __HAL_RCC_GPIOx_CLK_ENABLE() |
||
| 96 | (++) Configure these ADC pins in analog mode |
||
| 97 | using function HAL_GPIO_Init() |
||
| 98 | |||
| 99 | (#) Optionally, in case of usage of ADC with interruptions: |
||
| 100 | (++) Configure the NVIC for ADC |
||
| 101 | using function HAL_NVIC_EnableIRQ(ADCx_IRQn) |
||
| 102 | (++) Insert the ADC interruption handler function HAL_ADC_IRQHandler() |
||
| 103 | into the function of corresponding ADC interruption vector |
||
| 104 | ADCx_IRQHandler(). |
||
| 105 | |||
| 106 | (#) Optionally, in case of usage of DMA: |
||
| 107 | (++) Configure the DMA (DMA channel, mode normal or circular, ...) |
||
| 108 | using function HAL_DMA_Init(). |
||
| 109 | (++) Configure the NVIC for DMA |
||
| 110 | using function HAL_NVIC_EnableIRQ(DMAx_Channelx_IRQn) |
||
| 111 | (++) Insert the ADC interruption handler function HAL_ADC_IRQHandler() |
||
| 112 | into the function of corresponding DMA interruption vector |
||
| 113 | DMAx_Channelx_IRQHandler(). |
||
| 114 | |||
| 115 | *** Configuration of ADC, groups regular/injected, channels parameters *** |
||
| 116 | ========================================================================== |
||
| 117 | [..] |
||
| 118 | |||
| 119 | (#) Configure the ADC parameters (resolution, data alignment, ...) |
||
| 120 | and regular group parameters (conversion trigger, sequencer, ...) |
||
| 121 | using function HAL_ADC_Init(). |
||
| 122 | |||
| 123 | (#) Configure the channels for regular group parameters (channel number, |
||
| 124 | channel rank into sequencer, ..., into regular group) |
||
| 125 | using function HAL_ADC_ConfigChannel(). |
||
| 126 | |||
| 127 | (#) Optionally, configure the injected group parameters (conversion trigger, |
||
| 128 | sequencer, ..., of injected group) |
||
| 129 | and the channels for injected group parameters (channel number, |
||
| 130 | channel rank into sequencer, ..., into injected group) |
||
| 131 | using function HAL_ADCEx_InjectedConfigChannel(). |
||
| 132 | |||
| 133 | (#) Optionally, configure the analog watchdog parameters (channels |
||
| 134 | monitored, thresholds, ...) |
||
| 135 | using function HAL_ADC_AnalogWDGConfig(). |
||
| 136 | |||
| 137 | (#) Optionally, for devices with several ADC instances: configure the |
||
| 138 | multimode parameters |
||
| 139 | using function HAL_ADCEx_MultiModeConfigChannel(). |
||
| 140 | |||
| 141 | *** Execution of ADC conversions *** |
||
| 142 | ==================================== |
||
| 143 | [..] |
||
| 144 | |||
| 145 | (#) Optionally, perform an automatic ADC calibration to improve the |
||
| 146 | conversion accuracy |
||
| 147 | using function HAL_ADCEx_Calibration_Start(). |
||
| 148 | |||
| 149 | (#) ADC driver can be used among three modes: polling, interruption, |
||
| 150 | transfer by DMA. |
||
| 151 | |||
| 152 | (++) ADC conversion by polling: |
||
| 153 | (+++) Activate the ADC peripheral and start conversions |
||
| 154 | using function HAL_ADC_Start() |
||
| 155 | (+++) Wait for ADC conversion completion |
||
| 156 | using function HAL_ADC_PollForConversion() |
||
| 157 | (or for injected group: HAL_ADCEx_InjectedPollForConversion() ) |
||
| 158 | (+++) Retrieve conversion results |
||
| 159 | using function HAL_ADC_GetValue() |
||
| 160 | (or for injected group: HAL_ADCEx_InjectedGetValue() ) |
||
| 161 | (+++) Stop conversion and disable the ADC peripheral |
||
| 162 | using function HAL_ADC_Stop() |
||
| 163 | |||
| 164 | (++) ADC conversion by interruption: |
||
| 165 | (+++) Activate the ADC peripheral and start conversions |
||
| 166 | using function HAL_ADC_Start_IT() |
||
| 167 | (+++) Wait for ADC conversion completion by call of function |
||
| 168 | HAL_ADC_ConvCpltCallback() |
||
| 169 | (this function must be implemented in user program) |
||
| 170 | (or for injected group: HAL_ADCEx_InjectedConvCpltCallback() ) |
||
| 171 | (+++) Retrieve conversion results |
||
| 172 | using function HAL_ADC_GetValue() |
||
| 173 | (or for injected group: HAL_ADCEx_InjectedGetValue() ) |
||
| 174 | (+++) Stop conversion and disable the ADC peripheral |
||
| 175 | using function HAL_ADC_Stop_IT() |
||
| 176 | |||
| 177 | (++) ADC conversion with transfer by DMA: |
||
| 178 | (+++) Activate the ADC peripheral and start conversions |
||
| 179 | using function HAL_ADC_Start_DMA() |
||
| 180 | (+++) Wait for ADC conversion completion by call of function |
||
| 181 | HAL_ADC_ConvCpltCallback() or HAL_ADC_ConvHalfCpltCallback() |
||
| 182 | (these functions must be implemented in user program) |
||
| 183 | (+++) Conversion results are automatically transferred by DMA into |
||
| 184 | destination variable address. |
||
| 185 | (+++) Stop conversion and disable the ADC peripheral |
||
| 186 | using function HAL_ADC_Stop_DMA() |
||
| 187 | |||
| 188 | (++) For devices with several ADCs: ADC multimode conversion |
||
| 189 | with transfer by DMA: |
||
| 190 | (+++) Activate the ADC peripheral (slave) and start conversions |
||
| 191 | using function HAL_ADC_Start() |
||
| 192 | (+++) Activate the ADC peripheral (master) and start conversions |
||
| 193 | using function HAL_ADCEx_MultiModeStart_DMA() |
||
| 194 | (+++) Wait for ADC conversion completion by call of function |
||
| 195 | HAL_ADC_ConvCpltCallback() or HAL_ADC_ConvHalfCpltCallback() |
||
| 196 | (these functions must be implemented in user program) |
||
| 197 | (+++) Conversion results are automatically transferred by DMA into |
||
| 198 | destination variable address. |
||
| 199 | (+++) Stop conversion and disable the ADC peripheral (master) |
||
| 200 | using function HAL_ADCEx_MultiModeStop_DMA() |
||
| 201 | (+++) Stop conversion and disable the ADC peripheral (slave) |
||
| 202 | using function HAL_ADC_Stop_IT() |
||
| 203 | |||
| 204 | [..] |
||
| 205 | |||
| 206 | (@) Callback functions must be implemented in user program: |
||
| 207 | (+@) HAL_ADC_ErrorCallback() |
||
| 208 | (+@) HAL_ADC_LevelOutOfWindowCallback() (callback of analog watchdog) |
||
| 209 | (+@) HAL_ADC_ConvCpltCallback() |
||
| 210 | (+@) HAL_ADC_ConvHalfCpltCallback |
||
| 211 | (+@) HAL_ADCEx_InjectedConvCpltCallback() |
||
| 212 | |||
| 213 | *** Deinitialization of ADC *** |
||
| 214 | ============================================================ |
||
| 215 | [..] |
||
| 216 | |||
| 217 | (#) Disable the ADC interface |
||
| 218 | (++) ADC clock can be hard reset and disabled at RCC top level. |
||
| 219 | (++) Hard reset of ADC peripherals |
||
| 220 | using macro __ADCx_FORCE_RESET(), __ADCx_RELEASE_RESET(). |
||
| 221 | (++) ADC clock disable |
||
| 222 | using the equivalent macro/functions as configuration step. |
||
| 223 | (+++) Example: |
||
| 224 | Into HAL_ADC_MspDeInit() (recommended code location) or with |
||
| 225 | other device clock parameters configuration: |
||
| 226 | (+++) PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC |
||
| 227 | (+++) PeriphClkInit.AdcClockSelection = RCC_ADCPLLCLK2_OFF |
||
| 228 | (+++) HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) |
||
| 229 | |||
| 230 | (#) ADC pins configuration |
||
| 231 | (++) Disable the clock for the ADC GPIOs |
||
| 232 | using macro __HAL_RCC_GPIOx_CLK_DISABLE() |
||
| 233 | |||
| 234 | (#) Optionally, in case of usage of ADC with interruptions: |
||
| 235 | (++) Disable the NVIC for ADC |
||
| 236 | using function HAL_NVIC_EnableIRQ(ADCx_IRQn) |
||
| 237 | |||
| 238 | (#) Optionally, in case of usage of DMA: |
||
| 239 | (++) Deinitialize the DMA |
||
| 240 | using function HAL_DMA_Init(). |
||
| 241 | (++) Disable the NVIC for DMA |
||
| 242 | using function HAL_NVIC_EnableIRQ(DMAx_Channelx_IRQn) |
||
| 243 | |||
| 244 | [..] |
||
| 245 | |||
| 246 | @endverbatim |
||
| 247 | ****************************************************************************** |
||
| 248 | * @attention |
||
| 249 | * |
||
| 250 | * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> |
||
| 251 | * |
||
| 252 | * Redistribution and use in source and binary forms, with or without modification, |
||
| 253 | * are permitted provided that the following conditions are met: |
||
| 254 | * 1. Redistributions of source code must retain the above copyright notice, |
||
| 255 | * this list of conditions and the following disclaimer. |
||
| 256 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
||
| 257 | * this list of conditions and the following disclaimer in the documentation |
||
| 258 | * and/or other materials provided with the distribution. |
||
| 259 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
||
| 260 | * may be used to endorse or promote products derived from this software |
||
| 261 | * without specific prior written permission. |
||
| 262 | * |
||
| 263 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
||
| 264 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
||
| 265 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
||
| 266 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
||
| 267 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
||
| 268 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
||
| 269 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
||
| 270 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
||
| 271 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||
| 272 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
| 273 | * |
||
| 274 | ****************************************************************************** |
||
| 275 | */ |
||
| 276 | |||
| 277 | /* Includes ------------------------------------------------------------------*/ |
||
| 278 | #include "stm32f1xx_hal.h" |
||
| 279 | |||
| 280 | /** @addtogroup STM32F1xx_HAL_Driver |
||
| 281 | * @{ |
||
| 282 | */ |
||
| 283 | |||
| 284 | /** @defgroup ADC ADC |
||
| 285 | * @brief ADC HAL module driver |
||
| 286 | * @{ |
||
| 287 | */ |
||
| 288 | |||
| 289 | #ifdef HAL_ADC_MODULE_ENABLED |
||
| 290 | |||
| 291 | /* Private typedef -----------------------------------------------------------*/ |
||
| 292 | /* Private define ------------------------------------------------------------*/ |
||
| 293 | /** @defgroup ADC_Private_Constants ADC Private Constants |
||
| 294 | * @{ |
||
| 295 | */ |
||
| 296 | |||
| 297 | /* Timeout values for ADC enable and disable settling time. */ |
||
| 298 | /* Values defined to be higher than worst cases: low clocks freq, */ |
||
| 299 | /* maximum prescaler. */ |
||
| 300 | /* Ex of profile low frequency : Clock source at 0.1 MHz, ADC clock */ |
||
| 301 | /* prescaler 4, sampling time 12.5 ADC clock cycles, resolution 12 bits. */ |
||
| 302 | /* Unit: ms */ |
||
| 303 | #define ADC_ENABLE_TIMEOUT ((uint32_t) 2) |
||
| 304 | #define ADC_DISABLE_TIMEOUT ((uint32_t) 2) |
||
| 305 | |||
| 306 | /* Delay for ADC stabilization time. */ |
||
| 307 | /* Maximum delay is 1us (refer to device datasheet, parameter tSTAB). */ |
||
| 308 | /* Unit: us */ |
||
| 309 | #define ADC_STAB_DELAY_US ((uint32_t) 1) |
||
| 310 | |||
| 311 | /* Delay for temperature sensor stabilization time. */ |
||
| 312 | /* Maximum delay is 10us (refer to device datasheet, parameter tSTART). */ |
||
| 313 | /* Unit: us */ |
||
| 314 | #define ADC_TEMPSENSOR_DELAY_US ((uint32_t) 10) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @} |
||
| 318 | */ |
||
| 319 | |||
| 320 | /* Private macro -------------------------------------------------------------*/ |
||
| 321 | /* Private variables ---------------------------------------------------------*/ |
||
| 322 | /* Private function prototypes -----------------------------------------------*/ |
||
| 323 | /** @defgroup ADC_Private_Functions ADC Private Functions |
||
| 324 | * @{ |
||
| 325 | */ |
||
| 326 | /** |
||
| 327 | * @} |
||
| 328 | */ |
||
| 329 | |||
| 330 | /* Exported functions --------------------------------------------------------*/ |
||
| 331 | |||
| 332 | /** @defgroup ADC_Exported_Functions ADC Exported Functions |
||
| 333 | * @{ |
||
| 334 | */ |
||
| 335 | |||
| 336 | /** @defgroup ADC_Exported_Functions_Group1 Initialization/de-initialization functions |
||
| 337 | * @brief Initialization and Configuration functions |
||
| 338 | * |
||
| 339 | @verbatim |
||
| 340 | =============================================================================== |
||
| 341 | ##### Initialization and de-initialization functions ##### |
||
| 342 | =============================================================================== |
||
| 343 | [..] This section provides functions allowing to: |
||
| 344 | (+) Initialize and configure the ADC. |
||
| 345 | (+) De-initialize the ADC. |
||
| 346 | |||
| 347 | @endverbatim |
||
| 348 | * @{ |
||
| 349 | */ |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @brief Initializes the ADC peripheral and regular group according to |
||
| 353 | * parameters specified in structure "ADC_InitTypeDef". |
||
| 354 | * @note As prerequisite, ADC clock must be configured at RCC top level |
||
| 355 | * (clock source APB2). |
||
| 356 | * See commented example code below that can be copied and uncommented |
||
| 357 | * into HAL_ADC_MspInit(). |
||
| 358 | * @note Possibility to update parameters on the fly: |
||
| 359 | * This function initializes the ADC MSP (HAL_ADC_MspInit()) only when |
||
| 360 | * coming from ADC state reset. Following calls to this function can |
||
| 361 | * be used to reconfigure some parameters of ADC_InitTypeDef |
||
| 362 | * structure on the fly, without modifying MSP configuration. If ADC |
||
| 363 | * MSP has to be modified again, HAL_ADC_DeInit() must be called |
||
| 364 | * before HAL_ADC_Init(). |
||
| 365 | * The setting of these parameters is conditioned to ADC state. |
||
| 366 | * For parameters constraints, see comments of structure |
||
| 367 | * "ADC_InitTypeDef". |
||
| 368 | * @note This function configures the ADC within 2 scopes: scope of entire |
||
| 369 | * ADC and scope of regular group. For parameters details, see comments |
||
| 370 | * of structure "ADC_InitTypeDef". |
||
| 371 | * @param hadc: ADC handle |
||
| 372 | * @retval HAL status |
||
| 373 | */ |
||
| 374 | HAL_StatusTypeDef HAL_ADC_Init(ADC_HandleTypeDef* hadc) |
||
| 375 | { |
||
| 376 | HAL_StatusTypeDef tmp_hal_status = HAL_OK; |
||
| 377 | uint32_t tmp_cr1 = 0; |
||
| 378 | uint32_t tmp_cr2 = 0; |
||
| 379 | uint32_t tmp_sqr1 = 0; |
||
| 380 | |||
| 381 | /* Check ADC handle */ |
||
| 382 | if(hadc == NULL) |
||
| 383 | { |
||
| 384 | return HAL_ERROR; |
||
| 385 | } |
||
| 386 | |||
| 387 | /* Check the parameters */ |
||
| 388 | assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance)); |
||
| 389 | assert_param(IS_ADC_DATA_ALIGN(hadc->Init.DataAlign)); |
||
| 390 | assert_param(IS_ADC_SCAN_MODE(hadc->Init.ScanConvMode)); |
||
| 391 | assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode)); |
||
| 392 | assert_param(IS_ADC_EXTTRIG(hadc->Init.ExternalTrigConv)); |
||
| 393 | |||
| 394 | if(hadc->Init.ScanConvMode != ADC_SCAN_DISABLE) |
||
| 395 | { |
||
| 396 | assert_param(IS_ADC_REGULAR_NB_CONV(hadc->Init.NbrOfConversion)); |
||
| 397 | assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DiscontinuousConvMode)); |
||
| 398 | assert_param(IS_ADC_REGULAR_DISCONT_NUMBER(hadc->Init.NbrOfDiscConversion)); |
||
| 399 | } |
||
| 400 | |||
| 401 | /* As prerequisite, into HAL_ADC_MspInit(), ADC clock must be configured */ |
||
| 402 | /* at RCC top level. */ |
||
| 403 | /* Refer to header of this file for more details on clock enabling */ |
||
| 404 | /* procedure. */ |
||
| 405 | |||
| 406 | /* Actions performed only if ADC is coming from state reset: */ |
||
| 407 | /* - Initialization of ADC MSP */ |
||
| 408 | if (hadc->State == HAL_ADC_STATE_RESET) |
||
| 409 | { |
||
| 410 | /* Initialize ADC error code */ |
||
| 411 | ADC_CLEAR_ERRORCODE(hadc); |
||
| 412 | |||
| 413 | /* Allocate lock resource and initialize it */ |
||
| 414 | hadc->Lock = HAL_UNLOCKED; |
||
| 415 | |||
| 416 | /* Init the low level hardware */ |
||
| 417 | HAL_ADC_MspInit(hadc); |
||
| 418 | } |
||
| 419 | |||
| 420 | /* Stop potential conversion on going, on regular and injected groups */ |
||
| 421 | /* Disable ADC peripheral */ |
||
| 422 | /* Note: In case of ADC already enabled, precaution to not launch an */ |
||
| 423 | /* unwanted conversion while modifying register CR2 by writing 1 to */ |
||
| 424 | /* bit ADON. */ |
||
| 425 | tmp_hal_status = ADC_ConversionStop_Disable(hadc); |
||
| 426 | |||
| 427 | |||
| 428 | /* Configuration of ADC parameters if previous preliminary actions are */ |
||
| 429 | /* correctly completed. */ |
||
| 430 | if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL) && |
||
| 431 | (tmp_hal_status == HAL_OK) ) |
||
| 432 | { |
||
| 433 | /* Set ADC state */ |
||
| 434 | ADC_STATE_CLR_SET(hadc->State, |
||
| 435 | HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY, |
||
| 436 | HAL_ADC_STATE_BUSY_INTERNAL); |
||
| 437 | |||
| 438 | /* Set ADC parameters */ |
||
| 439 | |||
| 440 | /* Configuration of ADC: */ |
||
| 441 | /* - data alignment */ |
||
| 442 | /* - external trigger to start conversion */ |
||
| 443 | /* - external trigger polarity (always set to 1, because needed for all */ |
||
| 444 | /* triggers: external trigger of SW start) */ |
||
| 445 | /* - continuous conversion mode */ |
||
| 446 | /* Note: External trigger polarity (ADC_CR2_EXTTRIG) is set into */ |
||
| 447 | /* HAL_ADC_Start_xxx functions because if set in this function, */ |
||
| 448 | /* a conversion on injected group would start a conversion also on */ |
||
| 449 | /* regular group after ADC enabling. */ |
||
| 450 | tmp_cr2 |= (hadc->Init.DataAlign | |
||
| 451 | ADC_CFGR_EXTSEL(hadc, hadc->Init.ExternalTrigConv) | |
||
| 452 | ADC_CR2_CONTINUOUS(hadc->Init.ContinuousConvMode) ); |
||
| 453 | |||
| 454 | /* Configuration of ADC: */ |
||
| 455 | /* - scan mode */ |
||
| 456 | /* - discontinuous mode disable/enable */ |
||
| 457 | /* - discontinuous mode number of conversions */ |
||
| 458 | tmp_cr1 |= (ADC_CR1_SCAN_SET(hadc->Init.ScanConvMode)); |
||
| 459 | |||
| 460 | /* Enable discontinuous mode only if continuous mode is disabled */ |
||
| 461 | if (hadc->Init.DiscontinuousConvMode == ENABLE) |
||
| 462 | { |
||
| 463 | if (hadc->Init.ContinuousConvMode == DISABLE) |
||
| 464 | { |
||
| 465 | /* Enable the selected ADC regular discontinuous mode */ |
||
| 466 | /* Set the number of channels to be converted in discontinuous mode */ |
||
| 467 | SET_BIT(tmp_cr1, ADC_CR1_DISCEN | |
||
| 468 | ADC_CR1_DISCONTINUOUS_NUM(hadc->Init.NbrOfDiscConversion) ); |
||
| 469 | } |
||
| 470 | else |
||
| 471 | { |
||
| 472 | /* ADC regular group settings continuous and sequencer discontinuous*/ |
||
| 473 | /* cannot be enabled simultaneously. */ |
||
| 474 | |||
| 475 | /* Update ADC state machine to error */ |
||
| 476 | SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG); |
||
| 477 | |||
| 478 | /* Set ADC error code to ADC IP internal error */ |
||
| 479 | SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL); |
||
| 480 | } |
||
| 481 | } |
||
| 482 | |||
| 483 | /* Update ADC configuration register CR1 with previous settings */ |
||
| 484 | MODIFY_REG(hadc->Instance->CR1, |
||
| 485 | ADC_CR1_SCAN | |
||
| 486 | ADC_CR1_DISCEN | |
||
| 487 | ADC_CR1_DISCNUM , |
||
| 488 | tmp_cr1 ); |
||
| 489 | |||
| 490 | /* Update ADC configuration register CR2 with previous settings */ |
||
| 491 | MODIFY_REG(hadc->Instance->CR2, |
||
| 492 | ADC_CR2_ALIGN | |
||
| 493 | ADC_CR2_EXTSEL | |
||
| 494 | ADC_CR2_EXTTRIG | |
||
| 495 | ADC_CR2_CONT , |
||
| 496 | tmp_cr2 ); |
||
| 497 | |||
| 498 | /* Configuration of regular group sequencer: */ |
||
| 499 | /* - if scan mode is disabled, regular channels sequence length is set to */ |
||
| 500 | /* 0x00: 1 channel converted (channel on regular rank 1) */ |
||
| 501 | /* Parameter "NbrOfConversion" is discarded. */ |
||
| 502 | /* Note: Scan mode is present by hardware on this device and, if */ |
||
| 503 | /* disabled, discards automatically nb of conversions. Anyway, nb of */ |
||
| 504 | /* conversions is forced to 0x00 for alignment over all STM32 devices. */ |
||
| 505 | /* - if scan mode is enabled, regular channels sequence length is set to */ |
||
| 506 | /* parameter "NbrOfConversion" */ |
||
| 507 | if (hadc->Init.ScanConvMode == ADC_SCAN_ENABLE) |
||
| 508 | { |
||
| 509 | tmp_sqr1 = ADC_SQR1_L_SHIFT(hadc->Init.NbrOfConversion); |
||
| 510 | } |
||
| 511 | |||
| 512 | MODIFY_REG(hadc->Instance->SQR1, |
||
| 513 | ADC_SQR1_L , |
||
| 514 | tmp_sqr1 ); |
||
| 515 | |||
| 516 | /* Check back that ADC registers have effectively been configured to */ |
||
| 517 | /* ensure of no potential problem of ADC core IP clocking. */ |
||
| 518 | /* Check through register CR2 (excluding bits set in other functions: */ |
||
| 519 | /* execution control bits (ADON, JSWSTART, SWSTART), regular group bits */ |
||
| 520 | /* (DMA), injected group bits (JEXTTRIG and JEXTSEL), channel internal */ |
||
| 521 | /* measurement path bit (TSVREFE). */ |
||
| 522 | if (READ_BIT(hadc->Instance->CR2, ~(ADC_CR2_ADON | ADC_CR2_DMA | |
||
| 523 | ADC_CR2_SWSTART | ADC_CR2_JSWSTART | |
||
| 524 | ADC_CR2_JEXTTRIG | ADC_CR2_JEXTSEL | |
||
| 525 | ADC_CR2_TSVREFE )) |
||
| 526 | == tmp_cr2) |
||
| 527 | { |
||
| 528 | /* Set ADC error code to none */ |
||
| 529 | ADC_CLEAR_ERRORCODE(hadc); |
||
| 530 | |||
| 531 | /* Set the ADC state */ |
||
| 532 | ADC_STATE_CLR_SET(hadc->State, |
||
| 533 | HAL_ADC_STATE_BUSY_INTERNAL, |
||
| 534 | HAL_ADC_STATE_READY); |
||
| 535 | } |
||
| 536 | else |
||
| 537 | { |
||
| 538 | /* Update ADC state machine to error */ |
||
| 539 | ADC_STATE_CLR_SET(hadc->State, |
||
| 540 | HAL_ADC_STATE_BUSY_INTERNAL, |
||
| 541 | HAL_ADC_STATE_ERROR_INTERNAL); |
||
| 542 | |||
| 543 | /* Set ADC error code to ADC IP internal error */ |
||
| 544 | SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL); |
||
| 545 | |||
| 546 | tmp_hal_status = HAL_ERROR; |
||
| 547 | } |
||
| 548 | |||
| 549 | } |
||
| 550 | else |
||
| 551 | { |
||
| 552 | /* Update ADC state machine to error */ |
||
| 553 | SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL); |
||
| 554 | |||
| 555 | tmp_hal_status = HAL_ERROR; |
||
| 556 | } |
||
| 557 | |||
| 558 | /* Return function status */ |
||
| 559 | return tmp_hal_status; |
||
| 560 | } |
||
| 561 | |||
| 562 | /** |
||
| 563 | * @brief Deinitialize the ADC peripheral registers to their default reset |
||
| 564 | * values, with deinitialization of the ADC MSP. |
||
| 565 | * If needed, the example code can be copied and uncommented into |
||
| 566 | * function HAL_ADC_MspDeInit(). |
||
| 567 | * @param hadc: ADC handle |
||
| 568 | * @retval HAL status |
||
| 569 | */ |
||
| 570 | HAL_StatusTypeDef HAL_ADC_DeInit(ADC_HandleTypeDef* hadc) |
||
| 571 | { |
||
| 572 | HAL_StatusTypeDef tmp_hal_status = HAL_OK; |
||
| 573 | |||
| 574 | /* Check ADC handle */ |
||
| 575 | if(hadc == NULL) |
||
| 576 | { |
||
| 577 | return HAL_ERROR; |
||
| 578 | } |
||
| 579 | |||
| 580 | /* Check the parameters */ |
||
| 581 | assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance)); |
||
| 582 | |||
| 583 | /* Set ADC state */ |
||
| 584 | SET_BIT(hadc->State, HAL_ADC_STATE_BUSY_INTERNAL); |
||
| 585 | |||
| 586 | /* Stop potential conversion on going, on regular and injected groups */ |
||
| 587 | /* Disable ADC peripheral */ |
||
| 588 | tmp_hal_status = ADC_ConversionStop_Disable(hadc); |
||
| 589 | |||
| 590 | |||
| 591 | /* Configuration of ADC parameters if previous preliminary actions are */ |
||
| 592 | /* correctly completed. */ |
||
| 593 | if (tmp_hal_status == HAL_OK) |
||
| 594 | { |
||
| 595 | /* ========== Reset ADC registers ========== */ |
||
| 596 | |||
| 597 | |||
| 598 | |||
| 599 | |||
| 600 | /* Reset register SR */ |
||
| 601 | __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_AWD | ADC_FLAG_JEOC | ADC_FLAG_EOC | |
||
| 602 | ADC_FLAG_JSTRT | ADC_FLAG_STRT)); |
||
| 603 | |||
| 604 | /* Reset register CR1 */ |
||
| 605 | CLEAR_BIT(hadc->Instance->CR1, (ADC_CR1_AWDEN | ADC_CR1_JAWDEN | ADC_CR1_DISCNUM | |
||
| 606 | ADC_CR1_JDISCEN | ADC_CR1_DISCEN | ADC_CR1_JAUTO | |
||
| 607 | ADC_CR1_AWDSGL | ADC_CR1_SCAN | ADC_CR1_JEOCIE | |
||
| 608 | ADC_CR1_AWDIE | ADC_CR1_EOCIE | ADC_CR1_AWDCH )); |
||
| 609 | |||
| 610 | /* Reset register CR2 */ |
||
| 611 | CLEAR_BIT(hadc->Instance->CR2, (ADC_CR2_TSVREFE | ADC_CR2_SWSTART | ADC_CR2_JSWSTART | |
||
| 612 | ADC_CR2_EXTTRIG | ADC_CR2_EXTSEL | ADC_CR2_JEXTTRIG | |
||
| 613 | ADC_CR2_JEXTSEL | ADC_CR2_ALIGN | ADC_CR2_DMA | |
||
| 614 | ADC_CR2_RSTCAL | ADC_CR2_CAL | ADC_CR2_CONT | |
||
| 615 | ADC_CR2_ADON )); |
||
| 616 | |||
| 617 | /* Reset register SMPR1 */ |
||
| 618 | CLEAR_BIT(hadc->Instance->SMPR1, (ADC_SMPR1_SMP17 | ADC_SMPR1_SMP16 | ADC_SMPR1_SMP15 | |
||
| 619 | ADC_SMPR1_SMP14 | ADC_SMPR1_SMP13 | ADC_SMPR1_SMP12 | |
||
| 620 | ADC_SMPR1_SMP11 | ADC_SMPR1_SMP10 )); |
||
| 621 | |||
| 622 | /* Reset register SMPR2 */ |
||
| 623 | CLEAR_BIT(hadc->Instance->SMPR2, (ADC_SMPR2_SMP9 | ADC_SMPR2_SMP8 | ADC_SMPR2_SMP7 | |
||
| 624 | ADC_SMPR2_SMP6 | ADC_SMPR2_SMP5 | ADC_SMPR2_SMP4 | |
||
| 625 | ADC_SMPR2_SMP3 | ADC_SMPR2_SMP2 | ADC_SMPR2_SMP1 | |
||
| 626 | ADC_SMPR2_SMP0 )); |
||
| 627 | |||
| 628 | /* Reset register JOFR1 */ |
||
| 629 | CLEAR_BIT(hadc->Instance->JOFR1, ADC_JOFR1_JOFFSET1); |
||
| 630 | /* Reset register JOFR2 */ |
||
| 631 | CLEAR_BIT(hadc->Instance->JOFR2, ADC_JOFR2_JOFFSET2); |
||
| 632 | /* Reset register JOFR3 */ |
||
| 633 | CLEAR_BIT(hadc->Instance->JOFR3, ADC_JOFR3_JOFFSET3); |
||
| 634 | /* Reset register JOFR4 */ |
||
| 635 | CLEAR_BIT(hadc->Instance->JOFR4, ADC_JOFR4_JOFFSET4); |
||
| 636 | |||
| 637 | /* Reset register HTR */ |
||
| 638 | CLEAR_BIT(hadc->Instance->HTR, ADC_HTR_HT); |
||
| 639 | /* Reset register LTR */ |
||
| 640 | CLEAR_BIT(hadc->Instance->LTR, ADC_LTR_LT); |
||
| 641 | |||
| 642 | /* Reset register SQR1 */ |
||
| 643 | CLEAR_BIT(hadc->Instance->SQR1, ADC_SQR1_L | |
||
| 644 | ADC_SQR1_SQ16 | ADC_SQR1_SQ15 | |
||
| 645 | ADC_SQR1_SQ14 | ADC_SQR1_SQ13 ); |
||
| 646 | |||
| 647 | /* Reset register SQR1 */ |
||
| 648 | CLEAR_BIT(hadc->Instance->SQR1, ADC_SQR1_L | |
||
| 649 | ADC_SQR1_SQ16 | ADC_SQR1_SQ15 | |
||
| 650 | ADC_SQR1_SQ14 | ADC_SQR1_SQ13 ); |
||
| 651 | |||
| 652 | /* Reset register SQR2 */ |
||
| 653 | CLEAR_BIT(hadc->Instance->SQR2, ADC_SQR2_SQ12 | ADC_SQR2_SQ11 | ADC_SQR2_SQ10 | |
||
| 654 | ADC_SQR2_SQ9 | ADC_SQR2_SQ8 | ADC_SQR2_SQ7 ); |
||
| 655 | |||
| 656 | /* Reset register SQR3 */ |
||
| 657 | CLEAR_BIT(hadc->Instance->SQR3, ADC_SQR3_SQ6 | ADC_SQR3_SQ5 | ADC_SQR3_SQ4 | |
||
| 658 | ADC_SQR3_SQ3 | ADC_SQR3_SQ2 | ADC_SQR3_SQ1 ); |
||
| 659 | |||
| 660 | /* Reset register JSQR */ |
||
| 661 | CLEAR_BIT(hadc->Instance->JSQR, ADC_JSQR_JL | |
||
| 662 | ADC_JSQR_JSQ4 | ADC_JSQR_JSQ3 | |
||
| 663 | ADC_JSQR_JSQ2 | ADC_JSQR_JSQ1 ); |
||
| 664 | |||
| 665 | /* Reset register JSQR */ |
||
| 666 | CLEAR_BIT(hadc->Instance->JSQR, ADC_JSQR_JL | |
||
| 667 | ADC_JSQR_JSQ4 | ADC_JSQR_JSQ3 | |
||
| 668 | ADC_JSQR_JSQ2 | ADC_JSQR_JSQ1 ); |
||
| 669 | |||
| 670 | /* Reset register DR */ |
||
| 671 | /* bits in access mode read only, no direct reset applicable*/ |
||
| 672 | |||
| 673 | /* Reset registers JDR1, JDR2, JDR3, JDR4 */ |
||
| 674 | /* bits in access mode read only, no direct reset applicable*/ |
||
| 675 | |||
| 676 | /* ========== Hard reset ADC peripheral ========== */ |
||
| 677 | /* Performs a global reset of the entire ADC peripheral: ADC state is */ |
||
| 678 | /* forced to a similar state after device power-on. */ |
||
| 679 | /* If needed, copy-paste and uncomment the following reset code into */ |
||
| 680 | /* function "void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)": */ |
||
| 681 | /* */ |
||
| 682 | /* __HAL_RCC_ADC1_FORCE_RESET() */ |
||
| 683 | /* __HAL_RCC_ADC1_RELEASE_RESET() */ |
||
| 684 | |||
| 685 | /* DeInit the low level hardware */ |
||
| 686 | HAL_ADC_MspDeInit(hadc); |
||
| 687 | |||
| 688 | /* Set ADC error code to none */ |
||
| 689 | ADC_CLEAR_ERRORCODE(hadc); |
||
| 690 | |||
| 691 | /* Set ADC state */ |
||
| 692 | hadc->State = HAL_ADC_STATE_RESET; |
||
| 693 | |||
| 694 | } |
||
| 695 | |||
| 696 | /* Process unlocked */ |
||
| 697 | __HAL_UNLOCK(hadc); |
||
| 698 | |||
| 699 | /* Return function status */ |
||
| 700 | return tmp_hal_status; |
||
| 701 | } |
||
| 702 | |||
| 703 | /** |
||
| 704 | * @brief Initializes the ADC MSP. |
||
| 705 | * @param hadc: ADC handle |
||
| 706 | * @retval None |
||
| 707 | */ |
||
| 708 | __weak void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc) |
||
| 709 | { |
||
| 710 | /* NOTE : This function should not be modified. When the callback is needed, |
||
| 711 | function HAL_ADC_MspInit must be implemented in the user file. |
||
| 712 | */ |
||
| 713 | } |
||
| 714 | |||
| 715 | /** |
||
| 716 | * @brief DeInitializes the ADC MSP. |
||
| 717 | * @param hadc: ADC handle |
||
| 718 | * @retval None |
||
| 719 | */ |
||
| 720 | __weak void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc) |
||
| 721 | { |
||
| 722 | /* NOTE : This function should not be modified. When the callback is needed, |
||
| 723 | function HAL_ADC_MspDeInit must be implemented in the user file. |
||
| 724 | */ |
||
| 725 | } |
||
| 726 | |||
| 727 | /** |
||
| 728 | * @} |
||
| 729 | */ |
||
| 730 | |||
| 731 | /** @defgroup ADC_Exported_Functions_Group2 IO operation functions |
||
| 732 | * @brief Input and Output operation functions |
||
| 733 | * |
||
| 734 | @verbatim |
||
| 735 | =============================================================================== |
||
| 736 | ##### IO operation functions ##### |
||
| 737 | =============================================================================== |
||
| 738 | [..] This section provides functions allowing to: |
||
| 739 | (+) Start conversion of regular group. |
||
| 740 | (+) Stop conversion of regular group. |
||
| 741 | (+) Poll for conversion complete on regular group. |
||
| 742 | (+) Poll for conversion event. |
||
| 743 | (+) Get result of regular channel conversion. |
||
| 744 | (+) Start conversion of regular group and enable interruptions. |
||
| 745 | (+) Stop conversion of regular group and disable interruptions. |
||
| 746 | (+) Handle ADC interrupt request |
||
| 747 | (+) Start conversion of regular group and enable DMA transfer. |
||
| 748 | (+) Stop conversion of regular group and disable ADC DMA transfer. |
||
| 749 | @endverbatim |
||
| 750 | * @{ |
||
| 751 | */ |
||
| 752 | |||
| 753 | /** |
||
| 754 | * @brief Enables ADC, starts conversion of regular group. |
||
| 755 | * Interruptions enabled in this function: None. |
||
| 756 | * @param hadc: ADC handle |
||
| 757 | * @retval HAL status |
||
| 758 | */ |
||
| 759 | HAL_StatusTypeDef HAL_ADC_Start(ADC_HandleTypeDef* hadc) |
||
| 760 | { |
||
| 761 | HAL_StatusTypeDef tmp_hal_status = HAL_OK; |
||
| 762 | |||
| 763 | /* Check the parameters */ |
||
| 764 | assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance)); |
||
| 765 | |||
| 766 | /* Process locked */ |
||
| 767 | __HAL_LOCK(hadc); |
||
| 768 | |||
| 769 | /* Enable the ADC peripheral */ |
||
| 770 | tmp_hal_status = ADC_Enable(hadc); |
||
| 771 | |||
| 772 | /* Start conversion if ADC is effectively enabled */ |
||
| 773 | if (tmp_hal_status == HAL_OK) |
||
| 774 | { |
||
| 775 | /* Set ADC state */ |
||
| 776 | /* - Clear state bitfield related to regular group conversion results */ |
||
| 777 | /* - Set state bitfield related to regular operation */ |
||
| 778 | ADC_STATE_CLR_SET(hadc->State, |
||
| 779 | HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC, |
||
| 780 | HAL_ADC_STATE_REG_BUSY); |
||
| 781 | |||
| 782 | /* Set group injected state (from auto-injection) and multimode state */ |
||
| 783 | /* for all cases of multimode: independent mode, multimode ADC master */ |
||
| 784 | /* or multimode ADC slave (for devices with several ADCs): */ |
||
| 785 | if (ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc)) |
||
| 786 | { |
||
| 787 | /* Set ADC state (ADC independent or master) */ |
||
| 788 | CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE); |
||
| 789 | |||
| 790 | /* If conversions on group regular are also triggering group injected, */ |
||
| 791 | /* update ADC state. */ |
||
| 792 | if (READ_BIT(hadc->Instance->CR1, ADC_CR1_JAUTO) != RESET) |
||
| 793 | { |
||
| 794 | ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY); |
||
| 795 | } |
||
| 796 | } |
||
| 797 | else |
||
| 798 | { |
||
| 799 | /* Set ADC state (ADC slave) */ |
||
| 800 | SET_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE); |
||
| 801 | |||
| 802 | /* If conversions on group regular are also triggering group injected, */ |
||
| 803 | /* update ADC state. */ |
||
| 804 | if (ADC_MULTIMODE_AUTO_INJECTED(hadc)) |
||
| 805 | { |
||
| 806 | ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY); |
||
| 807 | } |
||
| 808 | } |
||
| 809 | |||
| 810 | /* State machine update: Check if an injected conversion is ongoing */ |
||
| 811 | if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_INJ_BUSY)) |
||
| 812 | { |
||
| 813 | /* Reset ADC error code fields related to conversions on group regular */ |
||
| 814 | CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA)); |
||
| 815 | } |
||
| 816 | else |
||
| 817 | { |
||
| 818 | /* Reset ADC all error code fields */ |
||
| 819 | ADC_CLEAR_ERRORCODE(hadc); |
||
| 820 | } |
||
| 821 | |||
| 822 | /* Process unlocked */ |
||
| 823 | /* Unlock before starting ADC conversions: in case of potential */ |
||
| 824 | /* interruption, to let the process to ADC IRQ Handler. */ |
||
| 825 | __HAL_UNLOCK(hadc); |
||
| 826 | |||
| 827 | /* Clear regular group conversion flag */ |
||
| 828 | /* (To ensure of no unknown state from potential previous ADC operations) */ |
||
| 829 | __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOC); |
||
| 830 | |||
| 831 | /* Enable conversion of regular group. */ |
||
| 832 | /* If software start has been selected, conversion starts immediately. */ |
||
| 833 | /* If external trigger has been selected, conversion will start at next */ |
||
| 834 | /* trigger event. */ |
||
| 835 | /* Case of multimode enabled: */ |
||
| 836 | /* - if ADC is slave, ADC is enabled only (conversion is not started). */ |
||
| 837 | /* - if ADC is master, ADC is enabled and conversion is started. */ |
||
| 838 | /* If ADC is master, ADC is enabled and conversion is started. */ |
||
| 839 | /* Note: Alternate trigger for single conversion could be to force an */ |
||
| 840 | /* additional set of bit ADON "hadc->Instance->CR2 |= ADC_CR2_ADON;"*/ |
||
| 841 | if (ADC_IS_SOFTWARE_START_REGULAR(hadc) && |
||
| 842 | ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc) ) |
||
| 843 | { |
||
| 844 | /* Start ADC conversion on regular group with SW start */ |
||
| 845 | SET_BIT(hadc->Instance->CR2, (ADC_CR2_SWSTART | ADC_CR2_EXTTRIG)); |
||
| 846 | } |
||
| 847 | else |
||
| 848 | { |
||
| 849 | /* Start ADC conversion on regular group with external trigger */ |
||
| 850 | SET_BIT(hadc->Instance->CR2, ADC_CR2_EXTTRIG); |
||
| 851 | } |
||
| 852 | } |
||
| 853 | else |
||
| 854 | { |
||
| 855 | /* Process unlocked */ |
||
| 856 | __HAL_UNLOCK(hadc); |
||
| 857 | } |
||
| 858 | |||
| 859 | /* Return function status */ |
||
| 860 | return tmp_hal_status; |
||
| 861 | } |
||
| 862 | |||
| 863 | /** |
||
| 864 | * @brief Stop ADC conversion of regular group (and injected channels in |
||
| 865 | * case of auto_injection mode), disable ADC peripheral. |
||
| 866 | * @note: ADC peripheral disable is forcing stop of potential |
||
| 867 | * conversion on injected group. If injected group is under use, it |
||
| 868 | * should be preliminarily stopped using HAL_ADCEx_InjectedStop function. |
||
| 869 | * @param hadc: ADC handle |
||
| 870 | * @retval HAL status. |
||
| 871 | */ |
||
| 872 | HAL_StatusTypeDef HAL_ADC_Stop(ADC_HandleTypeDef* hadc) |
||
| 873 | { |
||
| 874 | HAL_StatusTypeDef tmp_hal_status = HAL_OK; |
||
| 875 | |||
| 876 | /* Check the parameters */ |
||
| 877 | assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance)); |
||
| 878 | |||
| 879 | /* Process locked */ |
||
| 880 | __HAL_LOCK(hadc); |
||
| 881 | |||
| 882 | /* Stop potential conversion on going, on regular and injected groups */ |
||
| 883 | /* Disable ADC peripheral */ |
||
| 884 | tmp_hal_status = ADC_ConversionStop_Disable(hadc); |
||
| 885 | |||
| 886 | /* Check if ADC is effectively disabled */ |
||
| 887 | if (tmp_hal_status == HAL_OK) |
||
| 888 | { |
||
| 889 | /* Set ADC state */ |
||
| 890 | ADC_STATE_CLR_SET(hadc->State, |
||
| 891 | HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY, |
||
| 892 | HAL_ADC_STATE_READY); |
||
| 893 | } |
||
| 894 | |||
| 895 | /* Process unlocked */ |
||
| 896 | __HAL_UNLOCK(hadc); |
||
| 897 | |||
| 898 | /* Return function status */ |
||
| 899 | return tmp_hal_status; |
||
| 900 | } |
||
| 901 | |||
| 902 | /** |
||
| 903 | * @brief Wait for regular group conversion to be completed. |
||
| 904 | * @note This function cannot be used in a particular setup: ADC configured |
||
| 905 | * in DMA mode. |
||
| 906 | * In this case, DMA resets the flag EOC and polling cannot be |
||
| 907 | * performed on each conversion. |
||
| 908 | * @note On STM32F1 devices, limitation in case of sequencer enabled |
||
| 909 | * (several ranks selected): polling cannot be done on each |
||
| 910 | * conversion inside the sequence. In this case, polling is replaced by |
||
| 911 | * wait for maximum conversion time. |
||
| 912 | * @param hadc: ADC handle |
||
| 913 | * @param Timeout: Timeout value in millisecond. |
||
| 914 | * @retval HAL status |
||
| 915 | */ |
||
| 916 | HAL_StatusTypeDef HAL_ADC_PollForConversion(ADC_HandleTypeDef* hadc, uint32_t Timeout) |
||
| 917 | { |
||
| 918 | uint32_t tickstart = 0; |
||
| 919 | |||
| 920 | /* Variables for polling in case of scan mode enabled and polling for each */ |
||
| 921 | /* conversion. */ |
||
| 922 | __IO uint32_t Conversion_Timeout_CPU_cycles = 0; |
||
| 923 | uint32_t Conversion_Timeout_CPU_cycles_max = 0; |
||
| 924 | |||
| 925 | /* Check the parameters */ |
||
| 926 | assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance)); |
||
| 927 | |||
| 928 | /* Get tick count */ |
||
| 929 | tickstart = HAL_GetTick(); |
||
| 930 | |||
| 931 | /* Verification that ADC configuration is compliant with polling for */ |
||
| 932 | /* each conversion: */ |
||
| 933 | /* Particular case is ADC configured in DMA mode */ |
||
| 934 | if (HAL_IS_BIT_SET(hadc->Instance->CR2, ADC_CR2_DMA)) |
||
| 935 | { |
||
| 936 | /* Update ADC state machine to error */ |
||
| 937 | SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG); |
||
| 938 | |||
| 939 | /* Process unlocked */ |
||
| 940 | __HAL_UNLOCK(hadc); |
||
| 941 | |||
| 942 | return HAL_ERROR; |
||
| 943 | } |
||
| 944 | |||
| 945 | /* Polling for end of conversion: differentiation if single/sequence */ |
||
| 946 | /* conversion. */ |
||
| 947 | /* - If single conversion for regular group (Scan mode disabled or enabled */ |
||
| 948 | /* with NbrOfConversion =1), flag EOC is used to determine the */ |
||
| 949 | /* conversion completion. */ |
||
| 950 | /* - If sequence conversion for regular group (scan mode enabled and */ |
||
| 951 | /* NbrOfConversion >=2), flag EOC is set only at the end of the */ |
||
| 952 | /* sequence. */ |
||
| 953 | /* To poll for each conversion, the maximum conversion time is computed */ |
||
| 954 | /* from ADC conversion time (selected sampling time + conversion time of */ |
||
| 955 | /* 12.5 ADC clock cycles) and APB2/ADC clock prescalers (depending on */ |
||
| 956 | /* settings, conversion time range can be from 28 to 32256 CPU cycles). */ |
||
| 957 | /* As flag EOC is not set after each conversion, no timeout status can */ |
||
| 958 | /* be set. */ |
||
| 959 | if (HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_SCAN) && |
||
| 960 | HAL_IS_BIT_CLR(hadc->Instance->SQR1, ADC_SQR1_L) ) |
||
| 961 | { |
||
| 962 | /* Wait until End of Conversion flag is raised */ |
||
| 963 | while(HAL_IS_BIT_CLR(hadc->Instance->SR, ADC_FLAG_EOC)) |
||
| 964 | { |
||
| 965 | /* Check if timeout is disabled (set to infinite wait) */ |
||
| 966 | if(Timeout != HAL_MAX_DELAY) |
||
| 967 | { |
||
| 968 | if((Timeout == 0) || ((HAL_GetTick() - tickstart ) > Timeout)) |
||
| 969 | { |
||
| 970 | /* Update ADC state machine to timeout */ |
||
| 971 | SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT); |
||
| 972 | |||
| 973 | /* Process unlocked */ |
||
| 974 | __HAL_UNLOCK(hadc); |
||
| 975 | |||
| 976 | return HAL_TIMEOUT; |
||
| 977 | } |
||
| 978 | } |
||
| 979 | } |
||
| 980 | } |
||
| 981 | else |
||
| 982 | { |
||
| 983 | /* Replace polling by wait for maximum conversion time */ |
||
| 984 | /* - Computation of CPU clock cycles corresponding to ADC clock cycles */ |
||
| 985 | /* and ADC maximum conversion cycles on all channels. */ |
||
| 986 | /* - Wait for the expected ADC clock cycles delay */ |
||
| 987 | Conversion_Timeout_CPU_cycles_max = ((SystemCoreClock |
||
| 988 | / HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_ADC)) |
||
| 989 | * ADC_CONVCYCLES_MAX_RANGE(hadc) ); |
||
| 990 | |||
| 991 | while(Conversion_Timeout_CPU_cycles < Conversion_Timeout_CPU_cycles_max) |
||
| 992 | { |
||
| 993 | /* Check if timeout is disabled (set to infinite wait) */ |
||
| 994 | if(Timeout != HAL_MAX_DELAY) |
||
| 995 | { |
||
| 996 | if((Timeout == 0) || ((HAL_GetTick() - tickstart) > Timeout)) |
||
| 997 | { |
||
| 998 | /* Update ADC state machine to timeout */ |
||
| 999 | SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT); |
||
| 1000 | |||
| 1001 | /* Process unlocked */ |
||
| 1002 | __HAL_UNLOCK(hadc); |
||
| 1003 | |||
| 1004 | return HAL_TIMEOUT; |
||
| 1005 | } |
||
| 1006 | } |
||
| 1007 | Conversion_Timeout_CPU_cycles ++; |
||
| 1008 | } |
||
| 1009 | } |
||
| 1010 | |||
| 1011 | /* Clear regular group conversion flag */ |
||
| 1012 | __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_STRT | ADC_FLAG_EOC); |
||
| 1013 | |||
| 1014 | /* Update ADC state machine */ |
||
| 1015 | SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC); |
||
| 1016 | |||
| 1017 | /* Determine whether any further conversion upcoming on group regular */ |
||
| 1018 | /* by external trigger, continuous mode or scan sequence on going. */ |
||
| 1019 | /* Note: On STM32F1 devices, in case of sequencer enabled */ |
||
| 1020 | /* (several ranks selected), end of conversion flag is raised */ |
||
| 1021 | /* at the end of the sequence. */ |
||
| 1022 | if(ADC_IS_SOFTWARE_START_REGULAR(hadc) && |
||
| 1023 | (hadc->Init.ContinuousConvMode == DISABLE) ) |
||
| 1024 | { |
||
| 1025 | /* Set ADC state */ |
||
| 1026 | CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY); |
||
| 1027 | |||
| 1028 | if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_INJ_BUSY)) |
||
| 1029 | { |
||
| 1030 | SET_BIT(hadc->State, HAL_ADC_STATE_READY); |
||
| 1031 | } |
||
| 1032 | } |
||
| 1033 | |||
| 1034 | /* Return ADC state */ |
||
| 1035 | return HAL_OK; |
||
| 1036 | } |
||
| 1037 | |||
| 1038 | /** |
||
| 1039 | * @brief Poll for conversion event. |
||
| 1040 | * @param hadc: ADC handle |
||
| 1041 | * @param EventType: the ADC event type. |
||
| 1042 | * This parameter can be one of the following values: |
||
| 1043 | * @arg ADC_AWD_EVENT: ADC Analog watchdog event. |
||
| 1044 | * @param Timeout: Timeout value in millisecond. |
||
| 1045 | * @retval HAL status |
||
| 1046 | */ |
||
| 1047 | HAL_StatusTypeDef HAL_ADC_PollForEvent(ADC_HandleTypeDef* hadc, uint32_t EventType, uint32_t Timeout) |
||
| 1048 | { |
||
| 1049 | uint32_t tickstart = 0; |
||
| 1050 | |||
| 1051 | /* Check the parameters */ |
||
| 1052 | assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance)); |
||
| 1053 | assert_param(IS_ADC_EVENT_TYPE(EventType)); |
||
| 1054 | |||
| 1055 | /* Get tick count */ |
||
| 1056 | tickstart = HAL_GetTick(); |
||
| 1057 | |||
| 1058 | /* Check selected event flag */ |
||
| 1059 | while(__HAL_ADC_GET_FLAG(hadc, EventType) == RESET) |
||
| 1060 | { |
||
| 1061 | /* Check if timeout is disabled (set to infinite wait) */ |
||
| 1062 | if(Timeout != HAL_MAX_DELAY) |
||
| 1063 | { |
||
| 1064 | if((Timeout == 0) || ((HAL_GetTick() - tickstart ) > Timeout)) |
||
| 1065 | { |
||
| 1066 | /* Update ADC state machine to timeout */ |
||
| 1067 | SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT); |
||
| 1068 | |||
| 1069 | /* Process unlocked */ |
||
| 1070 | __HAL_UNLOCK(hadc); |
||
| 1071 | |||
| 1072 | return HAL_ERROR; |
||
| 1073 | } |
||
| 1074 | } |
||
| 1075 | } |
||
| 1076 | |||
| 1077 | /* Analog watchdog (level out of window) event */ |
||
| 1078 | /* Set ADC state */ |
||
| 1079 | SET_BIT(hadc->State, HAL_ADC_STATE_AWD1); |
||
| 1080 | |||
| 1081 | /* Clear ADC analog watchdog flag */ |
||
| 1082 | __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD); |
||
| 1083 | |||
| 1084 | /* Return ADC state */ |
||
| 1085 | return HAL_OK; |
||
| 1086 | } |
||
| 1087 | |||
| 1088 | /** |
||
| 1089 | * @brief Enables ADC, starts conversion of regular group with interruption. |
||
| 1090 | * Interruptions enabled in this function: |
||
| 1091 | * - EOC (end of conversion of regular group) |
||
| 1092 | * Each of these interruptions has its dedicated callback function. |
||
| 1093 | * @param hadc: ADC handle |
||
| 1094 | * @retval HAL status |
||
| 1095 | */ |
||
| 1096 | HAL_StatusTypeDef HAL_ADC_Start_IT(ADC_HandleTypeDef* hadc) |
||
| 1097 | { |
||
| 1098 | HAL_StatusTypeDef tmp_hal_status = HAL_OK; |
||
| 1099 | |||
| 1100 | /* Check the parameters */ |
||
| 1101 | assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance)); |
||
| 1102 | |||
| 1103 | /* Process locked */ |
||
| 1104 | __HAL_LOCK(hadc); |
||
| 1105 | |||
| 1106 | /* Enable the ADC peripheral */ |
||
| 1107 | tmp_hal_status = ADC_Enable(hadc); |
||
| 1108 | |||
| 1109 | /* Start conversion if ADC is effectively enabled */ |
||
| 1110 | if (tmp_hal_status == HAL_OK) |
||
| 1111 | { |
||
| 1112 | /* Set ADC state */ |
||
| 1113 | /* - Clear state bitfield related to regular group conversion results */ |
||
| 1114 | /* - Set state bitfield related to regular operation */ |
||
| 1115 | ADC_STATE_CLR_SET(hadc->State, |
||
| 1116 | HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP, |
||
| 1117 | HAL_ADC_STATE_REG_BUSY); |
||
| 1118 | |||
| 1119 | /* Set group injected state (from auto-injection) and multimode state */ |
||
| 1120 | /* for all cases of multimode: independent mode, multimode ADC master */ |
||
| 1121 | /* or multimode ADC slave (for devices with several ADCs): */ |
||
| 1122 | if (ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc)) |
||
| 1123 | { |
||
| 1124 | /* Set ADC state (ADC independent or master) */ |
||
| 1125 | CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE); |
||
| 1126 | |||
| 1127 | /* If conversions on group regular are also triggering group injected, */ |
||
| 1128 | /* update ADC state. */ |
||
| 1129 | if (READ_BIT(hadc->Instance->CR1, ADC_CR1_JAUTO) != RESET) |
||
| 1130 | { |
||
| 1131 | ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY); |
||
| 1132 | } |
||
| 1133 | } |
||
| 1134 | else |
||
| 1135 | { |
||
| 1136 | /* Set ADC state (ADC slave) */ |
||
| 1137 | SET_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE); |
||
| 1138 | |||
| 1139 | /* If conversions on group regular are also triggering group injected, */ |
||
| 1140 | /* update ADC state. */ |
||
| 1141 | if (ADC_MULTIMODE_AUTO_INJECTED(hadc)) |
||
| 1142 | { |
||
| 1143 | ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY); |
||
| 1144 | } |
||
| 1145 | } |
||
| 1146 | |||
| 1147 | /* State machine update: Check if an injected conversion is ongoing */ |
||
| 1148 | if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_INJ_BUSY)) |
||
| 1149 | { |
||
| 1150 | /* Reset ADC error code fields related to conversions on group regular */ |
||
| 1151 | CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA)); |
||
| 1152 | } |
||
| 1153 | else |
||
| 1154 | { |
||
| 1155 | /* Reset ADC all error code fields */ |
||
| 1156 | ADC_CLEAR_ERRORCODE(hadc); |
||
| 1157 | } |
||
| 1158 | |||
| 1159 | /* Process unlocked */ |
||
| 1160 | /* Unlock before starting ADC conversions: in case of potential */ |
||
| 1161 | /* interruption, to let the process to ADC IRQ Handler. */ |
||
| 1162 | __HAL_UNLOCK(hadc); |
||
| 1163 | |||
| 1164 | /* Clear regular group conversion flag and overrun flag */ |
||
| 1165 | /* (To ensure of no unknown state from potential previous ADC operations) */ |
||
| 1166 | __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOC); |
||
| 1167 | |||
| 1168 | /* Enable end of conversion interrupt for regular group */ |
||
| 1169 | __HAL_ADC_ENABLE_IT(hadc, ADC_IT_EOC); |
||
| 1170 | |||
| 1171 | /* Enable conversion of regular group. */ |
||
| 1172 | /* If software start has been selected, conversion starts immediately. */ |
||
| 1173 | /* If external trigger has been selected, conversion will start at next */ |
||
| 1174 | /* trigger event. */ |
||
| 1175 | /* Case of multimode enabled: */ |
||
| 1176 | /* - if ADC is slave, ADC is enabled only (conversion is not started). */ |
||
| 1177 | /* - if ADC is master, ADC is enabled and conversion is started. */ |
||
| 1178 | if (ADC_IS_SOFTWARE_START_REGULAR(hadc) && |
||
| 1179 | ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc) ) |
||
| 1180 | { |
||
| 1181 | /* Start ADC conversion on regular group with SW start */ |
||
| 1182 | SET_BIT(hadc->Instance->CR2, (ADC_CR2_SWSTART | ADC_CR2_EXTTRIG)); |
||
| 1183 | } |
||
| 1184 | else |
||
| 1185 | { |
||
| 1186 | /* Start ADC conversion on regular group with external trigger */ |
||
| 1187 | SET_BIT(hadc->Instance->CR2, ADC_CR2_EXTTRIG); |
||
| 1188 | } |
||
| 1189 | } |
||
| 1190 | else |
||
| 1191 | { |
||
| 1192 | /* Process unlocked */ |
||
| 1193 | __HAL_UNLOCK(hadc); |
||
| 1194 | } |
||
| 1195 | |||
| 1196 | /* Return function status */ |
||
| 1197 | return tmp_hal_status; |
||
| 1198 | } |
||
| 1199 | |||
| 1200 | /** |
||
| 1201 | * @brief Stop ADC conversion of regular group (and injected group in |
||
| 1202 | * case of auto_injection mode), disable interrution of |
||
| 1203 | * end-of-conversion, disable ADC peripheral. |
||
| 1204 | * @param hadc: ADC handle |
||
| 1205 | * @retval None |
||
| 1206 | */ |
||
| 1207 | HAL_StatusTypeDef HAL_ADC_Stop_IT(ADC_HandleTypeDef* hadc) |
||
| 1208 | { |
||
| 1209 | HAL_StatusTypeDef tmp_hal_status = HAL_OK; |
||
| 1210 | |||
| 1211 | /* Check the parameters */ |
||
| 1212 | assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance)); |
||
| 1213 | |||
| 1214 | /* Process locked */ |
||
| 1215 | __HAL_LOCK(hadc); |
||
| 1216 | |||
| 1217 | /* Stop potential conversion on going, on regular and injected groups */ |
||
| 1218 | /* Disable ADC peripheral */ |
||
| 1219 | tmp_hal_status = ADC_ConversionStop_Disable(hadc); |
||
| 1220 | |||
| 1221 | /* Check if ADC is effectively disabled */ |
||
| 1222 | if (tmp_hal_status == HAL_OK) |
||
| 1223 | { |
||
| 1224 | /* Disable ADC end of conversion interrupt for regular group */ |
||
| 1225 | __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC); |
||
| 1226 | |||
| 1227 | /* Set ADC state */ |
||
| 1228 | ADC_STATE_CLR_SET(hadc->State, |
||
| 1229 | HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY, |
||
| 1230 | HAL_ADC_STATE_READY); |
||
| 1231 | } |
||
| 1232 | |||
| 1233 | /* Process unlocked */ |
||
| 1234 | __HAL_UNLOCK(hadc); |
||
| 1235 | |||
| 1236 | /* Return function status */ |
||
| 1237 | return tmp_hal_status; |
||
| 1238 | } |
||
| 1239 | |||
| 1240 | /** |
||
| 1241 | * @brief Enables ADC, starts conversion of regular group and transfers result |
||
| 1242 | * through DMA. |
||
| 1243 | * Interruptions enabled in this function: |
||
| 1244 | * - DMA transfer complete |
||
| 1245 | * - DMA half transfer |
||
| 1246 | * Each of these interruptions has its dedicated callback function. |
||
| 1247 | * @note For devices with several ADCs: This function is for single-ADC mode |
||
| 1248 | * only. For multimode, use the dedicated MultimodeStart function. |
||
| 1249 | * @note On STM32F1 devices, only ADC1 and ADC3 (ADC availability depending |
||
| 1250 | * on devices) have DMA capability. |
||
| 1251 | * ADC2 converted data can be transferred in dual ADC mode using DMA |
||
| 1252 | * of ADC1 (ADC master in multimode). |
||
| 1253 | * In case of using ADC1 with DMA on a device featuring 2 ADC |
||
| 1254 | * instances: ADC1 conversion register DR contains ADC1 conversion |
||
| 1255 | * result (ADC1 register DR bits 0 to 11) and, additionally, ADC2 last |
||
| 1256 | * conversion result (ADC1 register DR bits 16 to 27). Therefore, to |
||
| 1257 | * have DMA transferring the conversion results of ADC1 only, DMA must |
||
| 1258 | * be configured to transfer size: half word. |
||
| 1259 | * @param hadc: ADC handle |
||
| 1260 | * @param pData: The destination Buffer address. |
||
| 1261 | * @param Length: The length of data to be transferred from ADC peripheral to memory. |
||
| 1262 | * @retval None |
||
| 1263 | */ |
||
| 1264 | HAL_StatusTypeDef HAL_ADC_Start_DMA(ADC_HandleTypeDef* hadc, uint32_t* pData, uint32_t Length) |
||
| 1265 | { |
||
| 1266 | HAL_StatusTypeDef tmp_hal_status = HAL_OK; |
||
| 1267 | |||
| 1268 | /* Check the parameters */ |
||
| 1269 | assert_param(IS_ADC_DMA_CAPABILITY_INSTANCE(hadc->Instance)); |
||
| 1270 | |||
| 1271 | /* Verification if multimode is disabled (for devices with several ADC) */ |
||
| 1272 | /* If multimode is enabled, dedicated function multimode conversion */ |
||
| 1273 | /* start DMA must be used. */ |
||
| 1274 | if(ADC_MULTIMODE_IS_ENABLE(hadc) == RESET) |
||
| 1275 | { |
||
| 1276 | /* Process locked */ |
||
| 1277 | __HAL_LOCK(hadc); |
||
| 1278 | |||
| 1279 | /* Enable the ADC peripheral */ |
||
| 1280 | tmp_hal_status = ADC_Enable(hadc); |
||
| 1281 | |||
| 1282 | /* Start conversion if ADC is effectively enabled */ |
||
| 1283 | if (tmp_hal_status == HAL_OK) |
||
| 1284 | { |
||
| 1285 | /* Set ADC state */ |
||
| 1286 | /* - Clear state bitfield related to regular group conversion results */ |
||
| 1287 | /* - Set state bitfield related to regular operation */ |
||
| 1288 | ADC_STATE_CLR_SET(hadc->State, |
||
| 1289 | HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP, |
||
| 1290 | HAL_ADC_STATE_REG_BUSY); |
||
| 1291 | |||
| 1292 | /* Set group injected state (from auto-injection) and multimode state */ |
||
| 1293 | /* for all cases of multimode: independent mode, multimode ADC master */ |
||
| 1294 | /* or multimode ADC slave (for devices with several ADCs): */ |
||
| 1295 | if (ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc)) |
||
| 1296 | { |
||
| 1297 | /* Set ADC state (ADC independent or master) */ |
||
| 1298 | CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE); |
||
| 1299 | |||
| 1300 | /* If conversions on group regular are also triggering group injected, */ |
||
| 1301 | /* update ADC state. */ |
||
| 1302 | if (READ_BIT(hadc->Instance->CR1, ADC_CR1_JAUTO) != RESET) |
||
| 1303 | { |
||
| 1304 | ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY); |
||
| 1305 | } |
||
| 1306 | } |
||
| 1307 | else |
||
| 1308 | { |
||
| 1309 | /* Set ADC state (ADC slave) */ |
||
| 1310 | SET_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE); |
||
| 1311 | |||
| 1312 | /* If conversions on group regular are also triggering group injected, */ |
||
| 1313 | /* update ADC state. */ |
||
| 1314 | if (ADC_MULTIMODE_AUTO_INJECTED(hadc)) |
||
| 1315 | { |
||
| 1316 | ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY); |
||
| 1317 | } |
||
| 1318 | } |
||
| 1319 | |||
| 1320 | /* State machine update: Check if an injected conversion is ongoing */ |
||
| 1321 | if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_INJ_BUSY)) |
||
| 1322 | { |
||
| 1323 | /* Reset ADC error code fields related to conversions on group regular */ |
||
| 1324 | CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA)); |
||
| 1325 | } |
||
| 1326 | else |
||
| 1327 | { |
||
| 1328 | /* Reset ADC all error code fields */ |
||
| 1329 | ADC_CLEAR_ERRORCODE(hadc); |
||
| 1330 | } |
||
| 1331 | |||
| 1332 | /* Process unlocked */ |
||
| 1333 | /* Unlock before starting ADC conversions: in case of potential */ |
||
| 1334 | /* interruption, to let the process to ADC IRQ Handler. */ |
||
| 1335 | __HAL_UNLOCK(hadc); |
||
| 1336 | |||
| 1337 | /* Set the DMA transfer complete callback */ |
||
| 1338 | hadc->DMA_Handle->XferCpltCallback = ADC_DMAConvCplt; |
||
| 1339 | |||
| 1340 | /* Set the DMA half transfer complete callback */ |
||
| 1341 | hadc->DMA_Handle->XferHalfCpltCallback = ADC_DMAHalfConvCplt; |
||
| 1342 | |||
| 1343 | /* Set the DMA error callback */ |
||
| 1344 | hadc->DMA_Handle->XferErrorCallback = ADC_DMAError; |
||
| 1345 | |||
| 1346 | |||
| 1347 | /* Manage ADC and DMA start: ADC overrun interruption, DMA start, ADC */ |
||
| 1348 | /* start (in case of SW start): */ |
||
| 1349 | |||
| 1350 | /* Clear regular group conversion flag and overrun flag */ |
||
| 1351 | /* (To ensure of no unknown state from potential previous ADC */ |
||
| 1352 | /* operations) */ |
||
| 1353 | __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOC); |
||
| 1354 | |||
| 1355 | /* Enable ADC DMA mode */ |
||
| 1356 | SET_BIT(hadc->Instance->CR2, ADC_CR2_DMA); |
||
| 1357 | |||
| 1358 | /* Start the DMA channel */ |
||
| 1359 | HAL_DMA_Start_IT(hadc->DMA_Handle, (uint32_t)&hadc->Instance->DR, (uint32_t)pData, Length); |
||
| 1360 | |||
| 1361 | /* Enable conversion of regular group. */ |
||
| 1362 | /* If software start has been selected, conversion starts immediately. */ |
||
| 1363 | /* If external trigger has been selected, conversion will start at next */ |
||
| 1364 | /* trigger event. */ |
||
| 1365 | if (ADC_IS_SOFTWARE_START_REGULAR(hadc)) |
||
| 1366 | { |
||
| 1367 | /* Start ADC conversion on regular group with SW start */ |
||
| 1368 | SET_BIT(hadc->Instance->CR2, (ADC_CR2_SWSTART | ADC_CR2_EXTTRIG)); |
||
| 1369 | } |
||
| 1370 | else |
||
| 1371 | { |
||
| 1372 | /* Start ADC conversion on regular group with external trigger */ |
||
| 1373 | SET_BIT(hadc->Instance->CR2, ADC_CR2_EXTTRIG); |
||
| 1374 | } |
||
| 1375 | } |
||
| 1376 | else |
||
| 1377 | { |
||
| 1378 | /* Process unlocked */ |
||
| 1379 | __HAL_UNLOCK(hadc); |
||
| 1380 | } |
||
| 1381 | } |
||
| 1382 | else |
||
| 1383 | { |
||
| 1384 | tmp_hal_status = HAL_ERROR; |
||
| 1385 | } |
||
| 1386 | |||
| 1387 | /* Return function status */ |
||
| 1388 | return tmp_hal_status; |
||
| 1389 | } |
||
| 1390 | |||
| 1391 | /** |
||
| 1392 | * @brief Stop ADC conversion of regular group (and injected group in |
||
| 1393 | * case of auto_injection mode), disable ADC DMA transfer, disable |
||
| 1394 | * ADC peripheral. |
||
| 1395 | * @note: ADC peripheral disable is forcing stop of potential |
||
| 1396 | * conversion on injected group. If injected group is under use, it |
||
| 1397 | * should be preliminarily stopped using HAL_ADCEx_InjectedStop function. |
||
| 1398 | * @note For devices with several ADCs: This function is for single-ADC mode |
||
| 1399 | * only. For multimode, use the dedicated MultimodeStop function. |
||
| 1400 | * @note On STM32F1 devices, only ADC1 and ADC3 (ADC availability depending |
||
| 1401 | * on devices) have DMA capability. |
||
| 1402 | * @param hadc: ADC handle |
||
| 1403 | * @retval HAL status. |
||
| 1404 | */ |
||
| 1405 | HAL_StatusTypeDef HAL_ADC_Stop_DMA(ADC_HandleTypeDef* hadc) |
||
| 1406 | { |
||
| 1407 | HAL_StatusTypeDef tmp_hal_status = HAL_OK; |
||
| 1408 | |||
| 1409 | /* Check the parameters */ |
||
| 1410 | assert_param(IS_ADC_DMA_CAPABILITY_INSTANCE(hadc->Instance)); |
||
| 1411 | |||
| 1412 | /* Process locked */ |
||
| 1413 | __HAL_LOCK(hadc); |
||
| 1414 | |||
| 1415 | /* Stop potential conversion on going, on regular and injected groups */ |
||
| 1416 | /* Disable ADC peripheral */ |
||
| 1417 | tmp_hal_status = ADC_ConversionStop_Disable(hadc); |
||
| 1418 | |||
| 1419 | /* Check if ADC is effectively disabled */ |
||
| 1420 | if (tmp_hal_status == HAL_OK) |
||
| 1421 | { |
||
| 1422 | /* Disable ADC DMA mode */ |
||
| 1423 | CLEAR_BIT(hadc->Instance->CR2, ADC_CR2_DMA); |
||
| 1424 | |||
| 1425 | /* Disable the DMA channel (in case of DMA in circular mode or stop while */ |
||
| 1426 | /* DMA transfer is on going) */ |
||
| 1427 | tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle); |
||
| 1428 | |||
| 1429 | /* Check if DMA channel effectively disabled */ |
||
| 1430 | if (tmp_hal_status == HAL_OK) |
||
| 1431 | { |
||
| 1432 | /* Set ADC state */ |
||
| 1433 | ADC_STATE_CLR_SET(hadc->State, |
||
| 1434 | HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY, |
||
| 1435 | HAL_ADC_STATE_READY); |
||
| 1436 | } |
||
| 1437 | else |
||
| 1438 | { |
||
| 1439 | /* Update ADC state machine to error */ |
||
| 1440 | SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA); |
||
| 1441 | } |
||
| 1442 | } |
||
| 1443 | |||
| 1444 | /* Process unlocked */ |
||
| 1445 | __HAL_UNLOCK(hadc); |
||
| 1446 | |||
| 1447 | /* Return function status */ |
||
| 1448 | return tmp_hal_status; |
||
| 1449 | } |
||
| 1450 | |||
| 1451 | /** |
||
| 1452 | * @brief Get ADC regular group conversion result. |
||
| 1453 | * @note Reading DR register automatically clears EOC (end of conversion of |
||
| 1454 | * regular group) flag. |
||
| 1455 | * @param hadc: ADC handle |
||
| 1456 | * @retval Converted value |
||
| 1457 | */ |
||
| 1458 | uint32_t HAL_ADC_GetValue(ADC_HandleTypeDef* hadc) |
||
| 1459 | { |
||
| 1460 | /* Check the parameters */ |
||
| 1461 | assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance)); |
||
| 1462 | |||
| 1463 | /* Note: EOC flag is not cleared here by software because automatically */ |
||
| 1464 | /* cleared by hardware when reading register DR. */ |
||
| 1465 | |||
| 1466 | /* Return ADC converted value */ |
||
| 1467 | return hadc->Instance->DR; |
||
| 1468 | } |
||
| 1469 | |||
| 1470 | /** |
||
| 1471 | * @brief Handles ADC interrupt request |
||
| 1472 | * @param hadc: ADC handle |
||
| 1473 | * @retval None |
||
| 1474 | */ |
||
| 1475 | void HAL_ADC_IRQHandler(ADC_HandleTypeDef* hadc) |
||
| 1476 | { |
||
| 1477 | /* Check the parameters */ |
||
| 1478 | assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance)); |
||
| 1479 | assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode)); |
||
| 1480 | assert_param(IS_ADC_REGULAR_NB_CONV(hadc->Init.NbrOfConversion)); |
||
| 1481 | |||
| 1482 | |||
| 1483 | /* ========== Check End of Conversion flag for regular group ========== */ |
||
| 1484 | if(__HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_EOC)) |
||
| 1485 | { |
||
| 1486 | if(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOC) ) |
||
| 1487 | { |
||
| 1488 | /* Update state machine on conversion status if not in error state */ |
||
| 1489 | if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL)) |
||
| 1490 | { |
||
| 1491 | /* Set ADC state */ |
||
| 1492 | SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC); |
||
| 1493 | } |
||
| 1494 | |||
| 1495 | /* Determine whether any further conversion upcoming on group regular */ |
||
| 1496 | /* by external trigger, continuous mode or scan sequence on going. */ |
||
| 1497 | /* Note: On STM32F1 devices, in case of sequencer enabled */ |
||
| 1498 | /* (several ranks selected), end of conversion flag is raised */ |
||
| 1499 | /* at the end of the sequence. */ |
||
| 1500 | if(ADC_IS_SOFTWARE_START_REGULAR(hadc) && |
||
| 1501 | (hadc->Init.ContinuousConvMode == DISABLE) ) |
||
| 1502 | { |
||
| 1503 | /* Disable ADC end of conversion interrupt on group regular */ |
||
| 1504 | __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC); |
||
| 1505 | |||
| 1506 | /* Set ADC state */ |
||
| 1507 | CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY); |
||
| 1508 | |||
| 1509 | if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_INJ_BUSY)) |
||
| 1510 | { |
||
| 1511 | SET_BIT(hadc->State, HAL_ADC_STATE_READY); |
||
| 1512 | } |
||
| 1513 | } |
||
| 1514 | |||
| 1515 | /* Conversion complete callback */ |
||
| 1516 | HAL_ADC_ConvCpltCallback(hadc); |
||
| 1517 | |||
| 1518 | /* Clear regular group conversion flag */ |
||
| 1519 | __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_STRT | ADC_FLAG_EOC); |
||
| 1520 | } |
||
| 1521 | } |
||
| 1522 | |||
| 1523 | /* ========== Check End of Conversion flag for injected group ========== */ |
||
| 1524 | if(__HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_JEOC)) |
||
| 1525 | { |
||
| 1526 | if(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_JEOC)) |
||
| 1527 | { |
||
| 1528 | /* Update state machine on conversion status if not in error state */ |
||
| 1529 | if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL)) |
||
| 1530 | { |
||
| 1531 | /* Set ADC state */ |
||
| 1532 | SET_BIT(hadc->State, HAL_ADC_STATE_INJ_EOC); |
||
| 1533 | } |
||
| 1534 | |||
| 1535 | /* Determine whether any further conversion upcoming on group injected */ |
||
| 1536 | /* by external trigger, scan sequence on going or by automatic injected */ |
||
| 1537 | /* conversion from group regular (same conditions as group regular */ |
||
| 1538 | /* interruption disabling above). */ |
||
| 1539 | /* Note: On STM32F1 devices, in case of sequencer enabled */ |
||
| 1540 | /* (several ranks selected), end of conversion flag is raised */ |
||
| 1541 | /* at the end of the sequence. */ |
||
| 1542 | if(ADC_IS_SOFTWARE_START_INJECTED(hadc) || |
||
| 1543 | (HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO) && |
||
| 1544 | (ADC_IS_SOFTWARE_START_REGULAR(hadc) && |
||
| 1545 | (hadc->Init.ContinuousConvMode == DISABLE) ) ) ) |
||
| 1546 | { |
||
| 1547 | /* Disable ADC end of conversion interrupt on group injected */ |
||
| 1548 | __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC); |
||
| 1549 | |||
| 1550 | /* Set ADC state */ |
||
| 1551 | CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY); |
||
| 1552 | |||
| 1553 | if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_REG_BUSY)) |
||
| 1554 | { |
||
| 1555 | SET_BIT(hadc->State, HAL_ADC_STATE_READY); |
||
| 1556 | } |
||
| 1557 | } |
||
| 1558 | |||
| 1559 | /* Conversion complete callback */ |
||
| 1560 | HAL_ADCEx_InjectedConvCpltCallback(hadc); |
||
| 1561 | |||
| 1562 | /* Clear injected group conversion flag */ |
||
| 1563 | __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_JSTRT | ADC_FLAG_JEOC)); |
||
| 1564 | } |
||
| 1565 | } |
||
| 1566 | |||
| 1567 | /* ========== Check Analog watchdog flags ========== */ |
||
| 1568 | if(__HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_AWD)) |
||
| 1569 | { |
||
| 1570 | if(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_AWD)) |
||
| 1571 | { |
||
| 1572 | /* Set ADC state */ |
||
| 1573 | SET_BIT(hadc->State, HAL_ADC_STATE_AWD1); |
||
| 1574 | |||
| 1575 | /* Level out of window callback */ |
||
| 1576 | HAL_ADC_LevelOutOfWindowCallback(hadc); |
||
| 1577 | |||
| 1578 | /* Clear the ADC analog watchdog flag */ |
||
| 1579 | __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD); |
||
| 1580 | } |
||
| 1581 | } |
||
| 1582 | |||
| 1583 | } |
||
| 1584 | |||
| 1585 | /** |
||
| 1586 | * @brief Conversion complete callback in non blocking mode |
||
| 1587 | * @param hadc: ADC handle |
||
| 1588 | * @retval None |
||
| 1589 | */ |
||
| 1590 | __weak void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) |
||
| 1591 | { |
||
| 1592 | /* NOTE : This function should not be modified. When the callback is needed, |
||
| 1593 | function HAL_ADC_ConvCpltCallback must be implemented in the user file. |
||
| 1594 | */ |
||
| 1595 | } |
||
| 1596 | |||
| 1597 | /** |
||
| 1598 | * @brief Conversion DMA half-transfer callback in non blocking mode |
||
| 1599 | * @param hadc: ADC handle |
||
| 1600 | * @retval None |
||
| 1601 | */ |
||
| 1602 | __weak void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc) |
||
| 1603 | { |
||
| 1604 | /* NOTE : This function should not be modified. When the callback is needed, |
||
| 1605 | function HAL_ADC_ConvHalfCpltCallback must be implemented in the user file. |
||
| 1606 | */ |
||
| 1607 | } |
||
| 1608 | |||
| 1609 | /** |
||
| 1610 | * @brief Analog watchdog callback in non blocking mode. |
||
| 1611 | * @param hadc: ADC handle |
||
| 1612 | * @retval None |
||
| 1613 | */ |
||
| 1614 | __weak void HAL_ADC_LevelOutOfWindowCallback(ADC_HandleTypeDef* hadc) |
||
| 1615 | { |
||
| 1616 | /* NOTE : This function should not be modified. When the callback is needed, |
||
| 1617 | function HAL_ADC_LevelOutOfWindowCallback must be implemented in the user file. |
||
| 1618 | */ |
||
| 1619 | } |
||
| 1620 | |||
| 1621 | /** |
||
| 1622 | * @brief ADC error callback in non blocking mode |
||
| 1623 | * (ADC conversion with interruption or transfer by DMA) |
||
| 1624 | * @param hadc: ADC handle |
||
| 1625 | * @retval None |
||
| 1626 | */ |
||
| 1627 | __weak void HAL_ADC_ErrorCallback(ADC_HandleTypeDef *hadc) |
||
| 1628 | { |
||
| 1629 | /* NOTE : This function should not be modified. When the callback is needed, |
||
| 1630 | function HAL_ADC_ErrorCallback must be implemented in the user file. |
||
| 1631 | */ |
||
| 1632 | } |
||
| 1633 | |||
| 1634 | |||
| 1635 | /** |
||
| 1636 | * @} |
||
| 1637 | */ |
||
| 1638 | |||
| 1639 | /** @defgroup ADC_Exported_Functions_Group3 Peripheral Control functions |
||
| 1640 | * @brief Peripheral Control functions |
||
| 1641 | * |
||
| 1642 | @verbatim |
||
| 1643 | =============================================================================== |
||
| 1644 | ##### Peripheral Control functions ##### |
||
| 1645 | =============================================================================== |
||
| 1646 | [..] This section provides functions allowing to: |
||
| 1647 | (+) Configure channels on regular group |
||
| 1648 | (+) Configure the analog watchdog |
||
| 1649 | |||
| 1650 | @endverbatim |
||
| 1651 | * @{ |
||
| 1652 | */ |
||
| 1653 | |||
| 1654 | /** |
||
| 1655 | * @brief Configures the the selected channel to be linked to the regular |
||
| 1656 | * group. |
||
| 1657 | * @note In case of usage of internal measurement channels: |
||
| 1658 | * Vbat/VrefInt/TempSensor. |
||
| 1659 | * These internal paths can be be disabled using function |
||
| 1660 | * HAL_ADC_DeInit(). |
||
| 1661 | * @note Possibility to update parameters on the fly: |
||
| 1662 | * This function initializes channel into regular group, following |
||
| 1663 | * calls to this function can be used to reconfigure some parameters |
||
| 1664 | * of structure "ADC_ChannelConfTypeDef" on the fly, without reseting |
||
| 1665 | * the ADC. |
||
| 1666 | * The setting of these parameters is conditioned to ADC state. |
||
| 1667 | * For parameters constraints, see comments of structure |
||
| 1668 | * "ADC_ChannelConfTypeDef". |
||
| 1669 | * @param hadc: ADC handle |
||
| 1670 | * @param sConfig: Structure of ADC channel for regular group. |
||
| 1671 | * @retval HAL status |
||
| 1672 | */ |
||
| 1673 | HAL_StatusTypeDef HAL_ADC_ConfigChannel(ADC_HandleTypeDef* hadc, ADC_ChannelConfTypeDef* sConfig) |
||
| 1674 | { |
||
| 1675 | HAL_StatusTypeDef tmp_hal_status = HAL_OK; |
||
| 1676 | __IO uint32_t wait_loop_index = 0; |
||
| 1677 | |||
| 1678 | /* Check the parameters */ |
||
| 1679 | assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance)); |
||
| 1680 | assert_param(IS_ADC_CHANNEL(sConfig->Channel)); |
||
| 1681 | assert_param(IS_ADC_REGULAR_RANK(sConfig->Rank)); |
||
| 1682 | assert_param(IS_ADC_SAMPLE_TIME(sConfig->SamplingTime)); |
||
| 1683 | |||
| 1684 | /* Process locked */ |
||
| 1685 | __HAL_LOCK(hadc); |
||
| 1686 | |||
| 1687 | |||
| 1688 | /* Regular sequence configuration */ |
||
| 1689 | /* For Rank 1 to 6 */ |
||
| 1690 | if (sConfig->Rank < 7) |
||
| 1691 | { |
||
| 1692 | MODIFY_REG(hadc->Instance->SQR3 , |
||
| 1693 | ADC_SQR3_RK(ADC_SQR3_SQ1, sConfig->Rank) , |
||
| 1694 | ADC_SQR3_RK(sConfig->Channel, sConfig->Rank) ); |
||
| 1695 | } |
||
| 1696 | /* For Rank 7 to 12 */ |
||
| 1697 | else if (sConfig->Rank < 13) |
||
| 1698 | { |
||
| 1699 | MODIFY_REG(hadc->Instance->SQR2 , |
||
| 1700 | ADC_SQR2_RK(ADC_SQR2_SQ7, sConfig->Rank) , |
||
| 1701 | ADC_SQR2_RK(sConfig->Channel, sConfig->Rank) ); |
||
| 1702 | } |
||
| 1703 | /* For Rank 13 to 16 */ |
||
| 1704 | else |
||
| 1705 | { |
||
| 1706 | MODIFY_REG(hadc->Instance->SQR1 , |
||
| 1707 | ADC_SQR1_RK(ADC_SQR1_SQ13, sConfig->Rank) , |
||
| 1708 | ADC_SQR1_RK(sConfig->Channel, sConfig->Rank) ); |
||
| 1709 | } |
||
| 1710 | |||
| 1711 | |||
| 1712 | /* Channel sampling time configuration */ |
||
| 1713 | /* For channels 10 to 17 */ |
||
| 1714 | if (sConfig->Channel >= ADC_CHANNEL_10) |
||
| 1715 | { |
||
| 1716 | MODIFY_REG(hadc->Instance->SMPR1 , |
||
| 1717 | ADC_SMPR1(ADC_SMPR1_SMP10, sConfig->Channel) , |
||
| 1718 | ADC_SMPR1(sConfig->SamplingTime, sConfig->Channel) ); |
||
| 1719 | } |
||
| 1720 | else /* For channels 0 to 9 */ |
||
| 1721 | { |
||
| 1722 | MODIFY_REG(hadc->Instance->SMPR2 , |
||
| 1723 | ADC_SMPR2(ADC_SMPR2_SMP0, sConfig->Channel) , |
||
| 1724 | ADC_SMPR2(sConfig->SamplingTime, sConfig->Channel) ); |
||
| 1725 | } |
||
| 1726 | |||
| 1727 | /* If ADC1 Channel_16 or Channel_17 is selected, enable Temperature sensor */ |
||
| 1728 | /* and VREFINT measurement path. */ |
||
| 1729 | if ((sConfig->Channel == ADC_CHANNEL_TEMPSENSOR) || |
||
| 1730 | (sConfig->Channel == ADC_CHANNEL_VREFINT) ) |
||
| 1731 | { |
||
| 1732 | /* For STM32F1 devices with several ADC: Only ADC1 can access internal */ |
||
| 1733 | /* measurement channels (VrefInt/TempSensor). If these channels are */ |
||
| 1734 | /* intended to be set on other ADC instances, an error is reported. */ |
||
| 1735 | if (hadc->Instance == ADC1) |
||
| 1736 | { |
||
| 1737 | if (READ_BIT(hadc->Instance->CR2, ADC_CR2_TSVREFE) == RESET) |
||
| 1738 | { |
||
| 1739 | SET_BIT(hadc->Instance->CR2, ADC_CR2_TSVREFE); |
||
| 1740 | |||
| 1741 | if ((sConfig->Channel == ADC_CHANNEL_TEMPSENSOR)) |
||
| 1742 | { |
||
| 1743 | /* Delay for temperature sensor stabilization time */ |
||
| 1744 | /* Compute number of CPU cycles to wait for */ |
||
| 1745 | wait_loop_index = (ADC_TEMPSENSOR_DELAY_US * (SystemCoreClock / 1000000)); |
||
| 1746 | while(wait_loop_index != 0) |
||
| 1747 | { |
||
| 1748 | wait_loop_index--; |
||
| 1749 | } |
||
| 1750 | } |
||
| 1751 | } |
||
| 1752 | } |
||
| 1753 | else |
||
| 1754 | { |
||
| 1755 | /* Update ADC state machine to error */ |
||
| 1756 | SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG); |
||
| 1757 | |||
| 1758 | tmp_hal_status = HAL_ERROR; |
||
| 1759 | } |
||
| 1760 | } |
||
| 1761 | |||
| 1762 | /* Process unlocked */ |
||
| 1763 | __HAL_UNLOCK(hadc); |
||
| 1764 | |||
| 1765 | /* Return function status */ |
||
| 1766 | return tmp_hal_status; |
||
| 1767 | } |
||
| 1768 | |||
| 1769 | /** |
||
| 1770 | * @brief Configures the analog watchdog. |
||
| 1771 | * @param hadc: ADC handle |
||
| 1772 | * @param AnalogWDGConfig: Structure of ADC analog watchdog configuration |
||
| 1773 | * @retval HAL status |
||
| 1774 | */ |
||
| 1775 | HAL_StatusTypeDef HAL_ADC_AnalogWDGConfig(ADC_HandleTypeDef* hadc, ADC_AnalogWDGConfTypeDef* AnalogWDGConfig) |
||
| 1776 | { |
||
| 1777 | /* Check the parameters */ |
||
| 1778 | assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance)); |
||
| 1779 | assert_param(IS_ADC_ANALOG_WATCHDOG_MODE(AnalogWDGConfig->WatchdogMode)); |
||
| 1780 | assert_param(IS_FUNCTIONAL_STATE(AnalogWDGConfig->ITMode)); |
||
| 1781 | assert_param(IS_ADC_RANGE(AnalogWDGConfig->HighThreshold)); |
||
| 1782 | assert_param(IS_ADC_RANGE(AnalogWDGConfig->LowThreshold)); |
||
| 1783 | |||
| 1784 | if((AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_REG) || |
||
| 1785 | (AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_INJEC) || |
||
| 1786 | (AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_REGINJEC) ) |
||
| 1787 | { |
||
| 1788 | assert_param(IS_ADC_CHANNEL(AnalogWDGConfig->Channel)); |
||
| 1789 | } |
||
| 1790 | |||
| 1791 | /* Process locked */ |
||
| 1792 | __HAL_LOCK(hadc); |
||
| 1793 | |||
| 1794 | /* Analog watchdog configuration */ |
||
| 1795 | |||
| 1796 | /* Configure ADC Analog watchdog interrupt */ |
||
| 1797 | if(AnalogWDGConfig->ITMode == ENABLE) |
||
| 1798 | { |
||
| 1799 | /* Enable the ADC Analog watchdog interrupt */ |
||
| 1800 | __HAL_ADC_ENABLE_IT(hadc, ADC_IT_AWD); |
||
| 1801 | } |
||
| 1802 | else |
||
| 1803 | { |
||
| 1804 | /* Disable the ADC Analog watchdog interrupt */ |
||
| 1805 | __HAL_ADC_DISABLE_IT(hadc, ADC_IT_AWD); |
||
| 1806 | } |
||
| 1807 | |||
| 1808 | /* Configuration of analog watchdog: */ |
||
| 1809 | /* - Set the analog watchdog enable mode: regular and/or injected groups, */ |
||
| 1810 | /* one or all channels. */ |
||
| 1811 | /* - Set the Analog watchdog channel (is not used if watchdog */ |
||
| 1812 | /* mode "all channels": ADC_CFGR_AWD1SGL=0). */ |
||
| 1813 | MODIFY_REG(hadc->Instance->CR1 , |
||
| 1814 | ADC_CR1_AWDSGL | |
||
| 1815 | ADC_CR1_JAWDEN | |
||
| 1816 | ADC_CR1_AWDEN | |
||
| 1817 | ADC_CR1_AWDCH , |
||
| 1818 | AnalogWDGConfig->WatchdogMode | |
||
| 1819 | AnalogWDGConfig->Channel ); |
||
| 1820 | |||
| 1821 | /* Set the high threshold */ |
||
| 1822 | WRITE_REG(hadc->Instance->HTR, AnalogWDGConfig->HighThreshold); |
||
| 1823 | |||
| 1824 | /* Set the low threshold */ |
||
| 1825 | WRITE_REG(hadc->Instance->LTR, AnalogWDGConfig->LowThreshold); |
||
| 1826 | |||
| 1827 | /* Process unlocked */ |
||
| 1828 | __HAL_UNLOCK(hadc); |
||
| 1829 | |||
| 1830 | /* Return function status */ |
||
| 1831 | return HAL_OK; |
||
| 1832 | } |
||
| 1833 | |||
| 1834 | |||
| 1835 | /** |
||
| 1836 | * @} |
||
| 1837 | */ |
||
| 1838 | |||
| 1839 | |||
| 1840 | /** @defgroup ADC_Exported_Functions_Group4 Peripheral State functions |
||
| 1841 | * @brief Peripheral State functions |
||
| 1842 | * |
||
| 1843 | @verbatim |
||
| 1844 | =============================================================================== |
||
| 1845 | ##### Peripheral State and Errors functions ##### |
||
| 1846 | =============================================================================== |
||
| 1847 | [..] |
||
| 1848 | This subsection provides functions to get in run-time the status of the |
||
| 1849 | peripheral. |
||
| 1850 | (+) Check the ADC state |
||
| 1851 | (+) Check the ADC error code |
||
| 1852 | |||
| 1853 | @endverbatim |
||
| 1854 | * @{ |
||
| 1855 | */ |
||
| 1856 | |||
| 1857 | /** |
||
| 1858 | * @brief return the ADC state |
||
| 1859 | * @param hadc: ADC handle |
||
| 1860 | * @retval HAL state |
||
| 1861 | */ |
||
| 1862 | uint32_t HAL_ADC_GetState(ADC_HandleTypeDef* hadc) |
||
| 1863 | { |
||
| 1864 | /* Return ADC state */ |
||
| 1865 | return hadc->State; |
||
| 1866 | } |
||
| 1867 | |||
| 1868 | /** |
||
| 1869 | * @brief Return the ADC error code |
||
| 1870 | * @param hadc: ADC handle |
||
| 1871 | * @retval ADC Error Code |
||
| 1872 | */ |
||
| 1873 | uint32_t HAL_ADC_GetError(ADC_HandleTypeDef *hadc) |
||
| 1874 | { |
||
| 1875 | return hadc->ErrorCode; |
||
| 1876 | } |
||
| 1877 | |||
| 1878 | /** |
||
| 1879 | * @} |
||
| 1880 | */ |
||
| 1881 | |||
| 1882 | /** |
||
| 1883 | * @} |
||
| 1884 | */ |
||
| 1885 | |||
| 1886 | /** @defgroup ADC_Private_Functions ADC Private Functions |
||
| 1887 | * @{ |
||
| 1888 | */ |
||
| 1889 | |||
| 1890 | /** |
||
| 1891 | * @brief Enable the selected ADC. |
||
| 1892 | * @note Prerequisite condition to use this function: ADC must be disabled |
||
| 1893 | * and voltage regulator must be enabled (done into HAL_ADC_Init()). |
||
| 1894 | * @param hadc: ADC handle |
||
| 1895 | * @retval HAL status. |
||
| 1896 | */ |
||
| 1897 | HAL_StatusTypeDef ADC_Enable(ADC_HandleTypeDef* hadc) |
||
| 1898 | { |
||
| 1899 | uint32_t tickstart = 0; |
||
| 1900 | __IO uint32_t wait_loop_index = 0; |
||
| 1901 | |||
| 1902 | /* ADC enable and wait for ADC ready (in case of ADC is disabled or */ |
||
| 1903 | /* enabling phase not yet completed: flag ADC ready not yet set). */ |
||
| 1904 | /* Timeout implemented to not be stuck if ADC cannot be enabled (possible */ |
||
| 1905 | /* causes: ADC clock not running, ...). */ |
||
| 1906 | if (ADC_IS_ENABLE(hadc) == RESET) |
||
| 1907 | { |
||
| 1908 | /* Enable the Peripheral */ |
||
| 1909 | __HAL_ADC_ENABLE(hadc); |
||
| 1910 | |||
| 1911 | /* Delay for ADC stabilization time */ |
||
| 1912 | /* Compute number of CPU cycles to wait for */ |
||
| 1913 | wait_loop_index = (ADC_STAB_DELAY_US * (SystemCoreClock / 1000000)); |
||
| 1914 | while(wait_loop_index != 0) |
||
| 1915 | { |
||
| 1916 | wait_loop_index--; |
||
| 1917 | } |
||
| 1918 | |||
| 1919 | /* Get tick count */ |
||
| 1920 | tickstart = HAL_GetTick(); |
||
| 1921 | |||
| 1922 | /* Wait for ADC effectively enabled */ |
||
| 1923 | while(ADC_IS_ENABLE(hadc) == RESET) |
||
| 1924 | { |
||
| 1925 | if((HAL_GetTick() - tickstart) > ADC_ENABLE_TIMEOUT) |
||
| 1926 | { |
||
| 1927 | /* Update ADC state machine to error */ |
||
| 1928 | SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL); |
||
| 1929 | |||
| 1930 | /* Set ADC error code to ADC IP internal error */ |
||
| 1931 | SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL); |
||
| 1932 | |||
| 1933 | /* Process unlocked */ |
||
| 1934 | __HAL_UNLOCK(hadc); |
||
| 1935 | |||
| 1936 | return HAL_ERROR; |
||
| 1937 | } |
||
| 1938 | } |
||
| 1939 | } |
||
| 1940 | |||
| 1941 | /* Return HAL status */ |
||
| 1942 | return HAL_OK; |
||
| 1943 | } |
||
| 1944 | |||
| 1945 | /** |
||
| 1946 | * @brief Stop ADC conversion and disable the selected ADC |
||
| 1947 | * @note Prerequisite condition to use this function: ADC conversions must be |
||
| 1948 | * stopped to disable the ADC. |
||
| 1949 | * @param hadc: ADC handle |
||
| 1950 | * @retval HAL status. |
||
| 1951 | */ |
||
| 1952 | HAL_StatusTypeDef ADC_ConversionStop_Disable(ADC_HandleTypeDef* hadc) |
||
| 1953 | { |
||
| 1954 | uint32_t tickstart = 0; |
||
| 1955 | |||
| 1956 | /* Verification if ADC is not already disabled */ |
||
| 1957 | if (ADC_IS_ENABLE(hadc) != RESET) |
||
| 1958 | { |
||
| 1959 | /* Disable the ADC peripheral */ |
||
| 1960 | __HAL_ADC_DISABLE(hadc); |
||
| 1961 | |||
| 1962 | /* Get tick count */ |
||
| 1963 | tickstart = HAL_GetTick(); |
||
| 1964 | |||
| 1965 | /* Wait for ADC effectively disabled */ |
||
| 1966 | while(ADC_IS_ENABLE(hadc) != RESET) |
||
| 1967 | { |
||
| 1968 | if((HAL_GetTick() - tickstart) > ADC_DISABLE_TIMEOUT) |
||
| 1969 | { |
||
| 1970 | /* Update ADC state machine to error */ |
||
| 1971 | SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL); |
||
| 1972 | |||
| 1973 | /* Set ADC error code to ADC IP internal error */ |
||
| 1974 | SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL); |
||
| 1975 | |||
| 1976 | return HAL_ERROR; |
||
| 1977 | } |
||
| 1978 | } |
||
| 1979 | } |
||
| 1980 | |||
| 1981 | /* Return HAL status */ |
||
| 1982 | return HAL_OK; |
||
| 1983 | } |
||
| 1984 | |||
| 1985 | /** |
||
| 1986 | * @brief DMA transfer complete callback. |
||
| 1987 | * @param hdma: pointer to DMA handle. |
||
| 1988 | * @retval None |
||
| 1989 | */ |
||
| 1990 | void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma) |
||
| 1991 | { |
||
| 1992 | /* Retrieve ADC handle corresponding to current DMA handle */ |
||
| 1993 | ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; |
||
| 1994 | |||
| 1995 | /* Update state machine on conversion status if not in error state */ |
||
| 1996 | if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL | HAL_ADC_STATE_ERROR_DMA)) |
||
| 1997 | { |
||
| 1998 | /* Update ADC state machine */ |
||
| 1999 | SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC); |
||
| 2000 | |||
| 2001 | /* Determine whether any further conversion upcoming on group regular */ |
||
| 2002 | /* by external trigger, continuous mode or scan sequence on going. */ |
||
| 2003 | /* Note: On STM32F1 devices, in case of sequencer enabled */ |
||
| 2004 | /* (several ranks selected), end of conversion flag is raised */ |
||
| 2005 | /* at the end of the sequence. */ |
||
| 2006 | if(ADC_IS_SOFTWARE_START_REGULAR(hadc) && |
||
| 2007 | (hadc->Init.ContinuousConvMode == DISABLE) ) |
||
| 2008 | { |
||
| 2009 | /* Set ADC state */ |
||
| 2010 | CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY); |
||
| 2011 | |||
| 2012 | if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_INJ_BUSY)) |
||
| 2013 | { |
||
| 2014 | SET_BIT(hadc->State, HAL_ADC_STATE_READY); |
||
| 2015 | } |
||
| 2016 | } |
||
| 2017 | |||
| 2018 | /* Conversion complete callback */ |
||
| 2019 | HAL_ADC_ConvCpltCallback(hadc); |
||
| 2020 | } |
||
| 2021 | else |
||
| 2022 | { |
||
| 2023 | /* Call DMA error callback */ |
||
| 2024 | hadc->DMA_Handle->XferErrorCallback(hdma); |
||
| 2025 | } |
||
| 2026 | } |
||
| 2027 | |||
| 2028 | /** |
||
| 2029 | * @brief DMA half transfer complete callback. |
||
| 2030 | * @param hdma: pointer to DMA handle. |
||
| 2031 | * @retval None |
||
| 2032 | */ |
||
| 2033 | void ADC_DMAHalfConvCplt(DMA_HandleTypeDef *hdma) |
||
| 2034 | { |
||
| 2035 | /* Retrieve ADC handle corresponding to current DMA handle */ |
||
| 2036 | ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; |
||
| 2037 | |||
| 2038 | /* Half conversion callback */ |
||
| 2039 | HAL_ADC_ConvHalfCpltCallback(hadc); |
||
| 2040 | } |
||
| 2041 | |||
| 2042 | /** |
||
| 2043 | * @brief DMA error callback |
||
| 2044 | * @param hdma: pointer to DMA handle. |
||
| 2045 | * @retval None |
||
| 2046 | */ |
||
| 2047 | void ADC_DMAError(DMA_HandleTypeDef *hdma) |
||
| 2048 | { |
||
| 2049 | /* Retrieve ADC handle corresponding to current DMA handle */ |
||
| 2050 | ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; |
||
| 2051 | |||
| 2052 | /* Set ADC state */ |
||
| 2053 | SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA); |
||
| 2054 | |||
| 2055 | /* Set ADC error code to DMA error */ |
||
| 2056 | SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_DMA); |
||
| 2057 | |||
| 2058 | /* Error callback */ |
||
| 2059 | HAL_ADC_ErrorCallback(hadc); |
||
| 2060 | } |
||
| 2061 | |||
| 2062 | /** |
||
| 2063 | * @} |
||
| 2064 | */ |
||
| 2065 | |||
| 2066 | #endif /* HAL_ADC_MODULE_ENABLED */ |
||
| 2067 | /** |
||
| 2068 | * @} |
||
| 2069 | */ |
||
| 2070 | |||
| 2071 | /** |
||
| 2072 | * @} |
||
| 2073 | */ |
||
| 2074 | |||
| 2075 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |