]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M7_STM32F7_STM32756G-EVAL_IAR_Keil/ST_Library/stm32f7xx_hal_adc.c
Final V8.2.1 release ready for tagging:
[freertos] / FreeRTOS / Demo / CORTEX_M7_STM32F7_STM32756G-EVAL_IAR_Keil / ST_Library / stm32f7xx_hal_adc.c
1 /**\r
2   ******************************************************************************\r
3   * @file    stm32f7xx_hal_adc.c\r
4   * @author  MCD Application Team\r
5   * @version V1.0.0RC1\r
6   * @date    24-March-2015\r
7   * @brief   This file provides firmware functions to manage the following \r
8   *          functionalities of the Analog to Digital Convertor (ADC) peripheral:\r
9   *           + Initialization and de-initialization functions\r
10   *           + IO operation functions\r
11   *           + State and errors functions\r
12   *         \r
13   @verbatim\r
14   ==============================================================================\r
15                     ##### ADC Peripheral features #####\r
16   ==============================================================================\r
17   [..] \r
18   (#) 12-bit, 10-bit, 8-bit or 6-bit configurable resolution.\r
19   (#) Interrupt generation at the end of conversion, end of injected conversion,  \r
20       and in case of analog watchdog or overrun events\r
21   (#) Single and continuous conversion modes.\r
22   (#) Scan mode for automatic conversion of channel 0 to channel x.\r
23   (#) Data alignment with in-built data coherency.\r
24   (#) Channel-wise programmable sampling time.\r
25   (#) External trigger option with configurable polarity for both regular and \r
26       injected conversion.\r
27   (#) Dual/Triple mode (on devices with 2 ADCs or more).\r
28   (#) Configurable DMA data storage in Dual/Triple ADC mode. \r
29   (#) Configurable delay between conversions in Dual/Triple interleaved mode.\r
30   (#) ADC conversion type (refer to the datasheets).\r
31   (#) ADC supply requirements: 2.4 V to 3.6 V at full speed and down to 1.8 V at \r
32       slower speed.\r
33   (#) ADC input range: VREF(minus) = VIN = VREF(plus).\r
34   (#) DMA request generation during regular channel conversion.\r
35 \r
36 \r
37                      ##### How to use this driver #####\r
38   ==============================================================================\r
39   [..]\r
40   (#)Initialize the ADC low level resources by implementing the HAL_ADC_MspInit():\r
41        (##) Enable the ADC interface clock using __HAL_RCC_ADC_CLK_ENABLE()\r
42        (##) ADC pins configuration\r
43              (+++) Enable the clock for the ADC GPIOs using the following function:\r
44                    __HAL_RCC_GPIOx_CLK_ENABLE()  \r
45              (+++) Configure these ADC pins in analog mode using HAL_GPIO_Init() \r
46        (##) In case of using interrupts (e.g. HAL_ADC_Start_IT())\r
47              (+++) Configure the ADC interrupt priority using HAL_NVIC_SetPriority()\r
48              (+++) Enable the ADC IRQ handler using HAL_NVIC_EnableIRQ()\r
49              (+++) In ADC IRQ handler, call HAL_ADC_IRQHandler()\r
50        (##) In case of using DMA to control data transfer (e.g. HAL_ADC_Start_DMA())\r
51              (+++) Enable the DMAx interface clock using __HAL_RCC_DMAx_CLK_ENABLE()\r
52              (+++) Configure and enable two DMA streams stream for managing data\r
53                  transfer from peripheral to memory (output stream)\r
54              (+++) Associate the initialized DMA handle to the CRYP DMA handle\r
55                  using  __HAL_LINKDMA()\r
56              (+++) Configure the priority and enable the NVIC for the transfer complete\r
57                  interrupt on the two DMA Streams. The output stream should have higher\r
58                  priority than the input stream.\r
59                        \r
60     *** Configuration of ADC, groups regular/injected, channels parameters ***\r
61   ==============================================================================\r
62   [..]\r
63   (#) Configure the ADC parameters (resolution, data alignment, ...)\r
64       and regular group parameters (conversion trigger, sequencer, ...)\r
65       using function HAL_ADC_Init().\r
66 \r
67   (#) Configure the channels for regular group parameters (channel number, \r
68       channel rank into sequencer, ..., into regular group)\r
69       using function HAL_ADC_ConfigChannel().\r
70 \r
71   (#) Optionally, configure the injected group parameters (conversion trigger, \r
72       sequencer, ..., of injected group)\r
73       and the channels for injected group parameters (channel number, \r
74       channel rank into sequencer, ..., into injected group)\r
75       using function HAL_ADCEx_InjectedConfigChannel().\r
76 \r
77   (#) Optionally, configure the analog watchdog parameters (channels\r
78       monitored, thresholds, ...) using function HAL_ADC_AnalogWDGConfig().\r
79 \r
80   (#) Optionally, for devices with several ADC instances: configure the \r
81       multimode parameters using function HAL_ADCEx_MultiModeConfigChannel().\r
82 \r
83                        *** Execution of ADC conversions ***\r
84   ==============================================================================\r
85   [..]  \r
86   (#) ADC driver can be used among three modes: polling, interruption,\r
87       transfer by DMA.    \r
88 \r
89      *** Polling mode IO operation ***\r
90      =================================\r
91      [..]    \r
92        (+) Start the ADC peripheral using HAL_ADC_Start() \r
93        (+) Wait for end of conversion using HAL_ADC_PollForConversion(), at this stage\r
94            user can specify the value of timeout according to his end application      \r
95        (+) To read the ADC converted values, use the HAL_ADC_GetValue() function.\r
96        (+) Stop the ADC peripheral using HAL_ADC_Stop()\r
97        \r
98      *** Interrupt mode IO operation ***    \r
99      ===================================\r
100      [..]    \r
101        (+) Start the ADC peripheral using HAL_ADC_Start_IT() \r
102        (+) Use HAL_ADC_IRQHandler() called under ADC_IRQHandler() Interrupt subroutine\r
103        (+) At ADC end of conversion HAL_ADC_ConvCpltCallback() function is executed and user can \r
104            add his own code by customization of function pointer HAL_ADC_ConvCpltCallback \r
105        (+) In case of ADC Error, HAL_ADC_ErrorCallback() function is executed and user can \r
106            add his own code by customization of function pointer HAL_ADC_ErrorCallback\r
107        (+) Stop the ADC peripheral using HAL_ADC_Stop_IT()     \r
108 \r
109      *** DMA mode IO operation ***    \r
110      ==============================\r
111      [..]    \r
112        (+) Start the ADC peripheral using HAL_ADC_Start_DMA(), at this stage the user specify the length \r
113            of data to be transferred at each end of conversion \r
114        (+) At The end of data transfer by HAL_ADC_ConvCpltCallback() function is executed and user can \r
115            add his own code by customization of function pointer HAL_ADC_ConvCpltCallback \r
116        (+) In case of transfer Error, HAL_ADC_ErrorCallback() function is executed and user can \r
117            add his own code by customization of function pointer HAL_ADC_ErrorCallback\r
118        (+) Stop the ADC peripheral using HAL_ADC_Stop_DMA()\r
119                     \r
120      *** ADC HAL driver macros list ***\r
121      ============================================= \r
122      [..]\r
123        Below the list of most used macros in ADC HAL driver.\r
124        \r
125       (+) __HAL_ADC_ENABLE : Enable the ADC peripheral\r
126       (+) __HAL_ADC_DISABLE : Disable the ADC peripheral\r
127       (+) __HAL_ADC_ENABLE_IT: Enable the ADC end of conversion interrupt\r
128       (+) __HAL_ADC_DISABLE_IT: Disable the ADC end of conversion interrupt\r
129       (+) __HAL_ADC_GET_IT_SOURCE: Check if the specified ADC interrupt source is enabled or disabled\r
130       (+) __HAL_ADC_CLEAR_FLAG: Clear the ADC's pending flags\r
131       (+) __HAL_ADC_GET_FLAG: Get the selected ADC's flag status\r
132       (+) ADC_GET_RESOLUTION: Return resolution bits in CR1 register \r
133       \r
134      [..] \r
135        (@) You can refer to the ADC HAL driver header file for more useful macros \r
136 \r
137                       *** Deinitialization of ADC ***\r
138   ==============================================================================\r
139   [..]\r
140   (#) Disable the ADC interface\r
141      (++) ADC clock can be hard reset and disabled at RCC top level.\r
142      (++) Hard reset of ADC peripherals\r
143           using macro __HAL_RCC_ADC_FORCE_RESET(), __HAL_RCC_ADC_RELEASE_RESET().\r
144      (++) ADC clock disable using the equivalent macro/functions as configuration step.\r
145                (+++) Example:\r
146                    Into HAL_ADC_MspDeInit() (recommended code location) or with\r
147                    other device clock parameters configuration:\r
148                (+++) HAL_RCC_GetOscConfig(&RCC_OscInitStructure);\r
149                (+++) RCC_OscInitStructure.OscillatorType = RCC_OSCILLATORTYPE_HSI;\r
150                (+++) RCC_OscInitStructure.HSIState = RCC_HSI_OFF; (if not used for system clock)\r
151                (+++) HAL_RCC_OscConfig(&RCC_OscInitStructure);\r
152 \r
153   (#) ADC pins configuration\r
154      (++) Disable the clock for the ADC GPIOs using macro __HAL_RCC_GPIOx_CLK_DISABLE()\r
155 \r
156   (#) Optionally, in case of usage of ADC with interruptions:\r
157      (++) Disable the NVIC for ADC using function HAL_NVIC_DisableIRQ(ADCx_IRQn)\r
158 \r
159   (#) Optionally, in case of usage of DMA:\r
160         (++) Deinitialize the DMA using function HAL_DMA_DeInit().\r
161         (++) Disable the NVIC for DMA using function HAL_NVIC_DisableIRQ(DMAx_Channelx_IRQn)   \r
162 \r
163     @endverbatim\r
164   ******************************************************************************\r
165   * @attention\r
166   *\r
167   * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>\r
168   *\r
169   * Redistribution and use in source and binary forms, with or without modification,\r
170   * are permitted provided that the following conditions are met:\r
171   *   1. Redistributions of source code must retain the above copyright notice,\r
172   *      this list of conditions and the following disclaimer.\r
173   *   2. Redistributions in binary form must reproduce the above copyright notice,\r
174   *      this list of conditions and the following disclaimer in the documentation\r
175   *      and/or other materials provided with the distribution.\r
176   *   3. Neither the name of STMicroelectronics nor the names of its contributors\r
177   *      may be used to endorse or promote products derived from this software\r
178   *      without specific prior written permission.\r
179   *\r
180   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"\r
181   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
182   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
183   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\r
184   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
185   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\r
186   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\r
187   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\r
188   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\r
189   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
190   *\r
191   ******************************************************************************\r
192   */ \r
193 \r
194 /* Includes ------------------------------------------------------------------*/\r
195 #include "stm32f7xx_hal.h"\r
196 \r
197 /** @addtogroup STM32F7xx_HAL_Driver\r
198   * @{\r
199   */\r
200 \r
201 /** @defgroup ADC ADC\r
202   * @brief ADC driver modules\r
203   * @{\r
204   */ \r
205 \r
206 #ifdef HAL_ADC_MODULE_ENABLED\r
207     \r
208 /* Private typedef -----------------------------------------------------------*/\r
209 /* Private define ------------------------------------------------------------*/\r
210 /* Private macro -------------------------------------------------------------*/\r
211 /* Private variables ---------------------------------------------------------*/\r
212 /** @addtogroup ADC_Private_Functions\r
213   * @{\r
214   */\r
215 /* Private function prototypes -----------------------------------------------*/\r
216 static void ADC_Init(ADC_HandleTypeDef* hadc);\r
217 static void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma);\r
218 static void ADC_DMAError(DMA_HandleTypeDef *hdma);\r
219 static void ADC_DMAHalfConvCplt(DMA_HandleTypeDef *hdma);\r
220 /**\r
221   * @}\r
222   */\r
223 \r
224 /* Exported functions --------------------------------------------------------*/\r
225 /** @defgroup ADC_Exported_Functions ADC Exported Functions\r
226   * @{\r
227   */\r
228 \r
229 /** @defgroup ADC_Exported_Functions_Group1 Initialization and de-initialization functions \r
230  *  @brief    Initialization and Configuration functions \r
231  *\r
232 @verbatim    \r
233  ===============================================================================\r
234               ##### Initialization and de-initialization functions #####\r
235  ===============================================================================\r
236     [..]  This section provides functions allowing to:\r
237       (+) Initialize and configure the ADC. \r
238       (+) De-initialize the ADC. \r
239          \r
240 @endverbatim\r
241   * @{\r
242   */\r
243 \r
244 /**\r
245   * @brief  Initializes the ADCx peripheral according to the specified parameters \r
246   *         in the ADC_InitStruct and initializes the ADC MSP.\r
247   *           \r
248   * @note   This function is used to configure the global features of the ADC ( \r
249   *         ClockPrescaler, Resolution, Data Alignment and number of conversion), however,\r
250   *         the rest of the configuration parameters are specific to the regular\r
251   *         channels group (scan mode activation, continuous mode activation,\r
252   *         External trigger source and edge, DMA continuous request after the  \r
253   *         last transfer and End of conversion selection).\r
254   *             \r
255   * @param  hadc: pointer to a ADC_HandleTypeDef structure that contains\r
256   *         the configuration information for the specified ADC.  \r
257   * @retval HAL status\r
258   */\r
259 HAL_StatusTypeDef HAL_ADC_Init(ADC_HandleTypeDef* hadc)\r
260 {\r
261   /* Check ADC handle */\r
262   if(hadc == NULL)\r
263   {\r
264      return HAL_ERROR;\r
265   }\r
266   \r
267   /* Check the parameters */\r
268   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));\r
269   assert_param(IS_ADC_CLOCKPRESCALER(hadc->Init.ClockPrescaler));\r
270   assert_param(IS_ADC_RESOLUTION(hadc->Init.Resolution));\r
271   assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ScanConvMode));\r
272   assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));\r
273   assert_param(IS_ADC_EXT_TRIG(hadc->Init.ExternalTrigConv));\r
274   assert_param(IS_ADC_DATA_ALIGN(hadc->Init.DataAlign));\r
275   assert_param(IS_ADC_REGULAR_LENGTH(hadc->Init.NbrOfConversion));\r
276   assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DMAContinuousRequests));\r
277   assert_param(IS_ADC_EOCSelection(hadc->Init.EOCSelection));\r
278   assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DiscontinuousConvMode));\r
279 \r
280   if(hadc->Init.ExternalTrigConv != ADC_SOFTWARE_START)\r
281   {\r
282     assert_param(IS_ADC_EXT_TRIG_EDGE(hadc->Init.ExternalTrigConvEdge));\r
283   }\r
284 \r
285   if(hadc->State == HAL_ADC_STATE_RESET)\r
286   {\r
287     /* Init the low level hardware */\r
288     HAL_ADC_MspInit(hadc);\r
289   }\r
290 \r
291   /* Initialize the ADC state */\r
292   hadc->State = HAL_ADC_STATE_BUSY;\r
293   \r
294   /* Set ADC parameters */\r
295   ADC_Init(hadc);\r
296   \r
297   /* Set ADC error code to none */\r
298   hadc->ErrorCode = HAL_ADC_ERROR_NONE;\r
299   \r
300   /* Initialize the ADC state */\r
301   hadc->State = HAL_ADC_STATE_READY;\r
302 \r
303   /* Release Lock */\r
304   __HAL_UNLOCK(hadc);\r
305 \r
306   /* Return function status */\r
307   return HAL_OK;\r
308 }\r
309 \r
310 /**\r
311   * @brief  Deinitializes the ADCx peripheral registers to their default reset values. \r
312   * @param  hadc: pointer to a ADC_HandleTypeDef structure that contains\r
313   *         the configuration information for the specified ADC.  \r
314   * @retval HAL status\r
315   */\r
316 HAL_StatusTypeDef HAL_ADC_DeInit(ADC_HandleTypeDef* hadc)\r
317 {\r
318   /* Check ADC handle */\r
319   if(hadc == NULL)\r
320   {\r
321      return HAL_ERROR;\r
322   } \r
323   \r
324   /* Check the parameters */\r
325   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));\r
326   \r
327   /* Change ADC state */\r
328   hadc->State = HAL_ADC_STATE_BUSY;\r
329   \r
330   /* DeInit the low level hardware */\r
331   HAL_ADC_MspDeInit(hadc);\r
332   \r
333   /* Set ADC error code to none */\r
334   hadc->ErrorCode = HAL_ADC_ERROR_NONE;\r
335   \r
336   /* Change ADC state */\r
337   hadc->State = HAL_ADC_STATE_RESET;\r
338   \r
339   /* Return function status */\r
340   return HAL_OK;\r
341 }\r
342 \r
343 /**\r
344   * @brief  Initializes the ADC MSP.\r
345   * @param  hadc: pointer to a ADC_HandleTypeDef structure that contains\r
346   *         the configuration information for the specified ADC.  \r
347   * @retval None\r
348   */\r
349 __weak void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)\r
350 {\r
351   /* NOTE : This function Should not be modified, when the callback is needed,\r
352             the HAL_ADC_MspInit could be implemented in the user file\r
353    */ \r
354 }\r
355 \r
356 /**\r
357   * @brief  DeInitializes the ADC MSP.\r
358   * @param  hadc: pointer to a ADC_HandleTypeDef structure that contains\r
359   *         the configuration information for the specified ADC.  \r
360   * @retval None\r
361   */\r
362 __weak void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc)\r
363 {\r
364   /* NOTE : This function Should not be modified, when the callback is needed,\r
365             the HAL_ADC_MspDeInit could be implemented in the user file\r
366    */ \r
367 }\r
368 \r
369 /**\r
370   * @}\r
371   */\r
372 \r
373 /** @defgroup ADC_Exported_Functions_Group2 IO operation functions\r
374  *  @brief    IO operation functions \r
375  *\r
376 @verbatim   \r
377  ===============================================================================\r
378              ##### IO operation functions #####\r
379  ===============================================================================  \r
380     [..]  This section provides functions allowing to:\r
381       (+) Start conversion of regular channel.\r
382       (+) Stop conversion of regular channel.\r
383       (+) Start conversion of regular channel and enable interrupt.\r
384       (+) Stop conversion of regular channel and disable interrupt.\r
385       (+) Start conversion of regular channel and enable DMA transfer.\r
386       (+) Stop conversion of regular channel and disable DMA transfer.\r
387       (+) Handle ADC interrupt request. \r
388                \r
389 @endverbatim\r
390   * @{\r
391   */\r
392 \r
393 /**\r
394   * @brief  Enables ADC and starts conversion of the regular channels.\r
395   * @param  hadc: pointer to a ADC_HandleTypeDef structure that contains\r
396   *         the configuration information for the specified ADC.\r
397   * @retval HAL status\r
398   */\r
399 HAL_StatusTypeDef HAL_ADC_Start(ADC_HandleTypeDef* hadc)\r
400 {\r
401   __IO uint32_t counter = 0;\r
402   \r
403   /* Check the parameters */\r
404   assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));\r
405   assert_param(IS_ADC_EXT_TRIG_EDGE(hadc->Init.ExternalTrigConvEdge)); \r
406   \r
407   /* Process locked */\r
408   __HAL_LOCK(hadc);\r
409   \r
410   /* Check if an injected conversion is ongoing */\r
411   if(hadc->State == HAL_ADC_STATE_BUSY_INJ)\r
412   {\r
413     /* Change ADC state */\r
414     hadc->State = HAL_ADC_STATE_BUSY_INJ_REG;  \r
415   }\r
416   else\r
417   {\r
418     /* Change ADC state */\r
419     hadc->State = HAL_ADC_STATE_BUSY_REG;\r
420   } \r
421     \r
422   /* Check if ADC peripheral is disabled in order to enable it and wait during \r
423   Tstab time the ADC's stabilization */\r
424   if((hadc->Instance->CR2 & ADC_CR2_ADON) != ADC_CR2_ADON)\r
425   {  \r
426     /* Enable the Peripheral */\r
427     __HAL_ADC_ENABLE(hadc);\r
428     \r
429     /* Delay for ADC stabilization time */\r
430     /* Compute number of CPU cycles to wait for */\r
431     counter = (ADC_STAB_DELAY_US * (SystemCoreClock / 1000000));\r
432     while(counter != 0)\r
433     {\r
434       counter--;\r
435     }\r
436   }\r
437         \r
438   /* Process unlocked */\r
439   __HAL_UNLOCK(hadc);\r
440 \r
441   /* Check if Multimode enabled */\r
442   if(HAL_IS_BIT_CLR(ADC->CCR, ADC_CCR_MULTI))\r
443   {\r
444     /* if no external trigger present enable software conversion of regular channels */\r
445     if((hadc->Instance->CR2 & ADC_CR2_EXTEN) == RESET)\r
446     {\r
447       /* Enable the selected ADC software conversion for regular group */\r
448       hadc->Instance->CR2 |= (uint32_t)ADC_CR2_SWSTART;\r
449     }\r
450   }\r
451   else\r
452   {\r
453     /* if instance of handle correspond to ADC1 and  no external trigger present enable software conversion of regular channels */\r
454     if((hadc->Instance == ADC1) && ((hadc->Instance->CR2 & ADC_CR2_EXTEN) == RESET))\r
455     {\r
456       /* Enable the selected ADC software conversion for regular group */\r
457         hadc->Instance->CR2 |= (uint32_t)ADC_CR2_SWSTART;\r
458     }\r
459   }\r
460   \r
461   /* Return function status */\r
462   return HAL_OK;\r
463 }\r
464 \r
465 /**\r
466   * @brief  Disables ADC and stop conversion of regular channels.\r
467   * \r
468   * @note   Caution: This function will stop also injected channels.  \r
469   *\r
470   * @param  hadc: pointer to a ADC_HandleTypeDef structure that contains\r
471   *         the configuration information for the specified ADC.\r
472   *\r
473   * @retval HAL status.\r
474   */\r
475 HAL_StatusTypeDef HAL_ADC_Stop(ADC_HandleTypeDef* hadc)\r
476 {\r
477   /* Disable the Peripheral */\r
478   __HAL_ADC_DISABLE(hadc);\r
479   \r
480   /* Change ADC state */\r
481   hadc->State = HAL_ADC_STATE_READY;\r
482   \r
483   /* Return function status */\r
484   return HAL_OK;\r
485 }\r
486 \r
487 /**\r
488   * @brief  Poll for regular conversion complete\r
489   * @param  hadc: pointer to a ADC_HandleTypeDef structure that contains\r
490   *         the configuration information for the specified ADC.\r
491   * @param  Timeout: Timeout value in millisecond.  \r
492   * @retval HAL status\r
493   */\r
494 HAL_StatusTypeDef HAL_ADC_PollForConversion(ADC_HandleTypeDef* hadc, uint32_t Timeout)\r
495 {\r
496   uint32_t tickstart = 0;\r
497   \r
498   /* Verification that ADC configuration is compliant with polling for      */\r
499   /* each conversion:                                                       */\r
500   /* Particular case is ADC configured in DMA mode and ADC sequencer with   */\r
501   /* several ranks and polling for end of each conversion.                  */\r
502   /* For code simplicity sake, this particular case is generalized to       */\r
503   /* ADC configured in DMA mode and polling for end of each conversion.     */\r
504   if (HAL_IS_BIT_SET(hadc->Instance->CR2, ADC_CR2_EOCS) &&\r
505       HAL_IS_BIT_SET(hadc->Instance->CR2, ADC_CR2_DMA)    )\r
506   {\r
507     /* Update ADC state machine to error */\r
508     hadc->State = HAL_ADC_STATE_ERROR;\r
509     \r
510     /* Process unlocked */\r
511     __HAL_UNLOCK(hadc);\r
512     \r
513     return HAL_ERROR;\r
514   }\r
515  \r
516   /* Get tick */ \r
517   tickstart = HAL_GetTick();\r
518 \r
519   /* Check End of conversion flag */\r
520   while(!(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOC)))\r
521   {\r
522     /* Check for the Timeout */\r
523     if(Timeout != HAL_MAX_DELAY)\r
524     {\r
525       if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))\r
526       {\r
527         hadc->State= HAL_ADC_STATE_TIMEOUT;\r
528         /* Process unlocked */\r
529         __HAL_UNLOCK(hadc);\r
530         return HAL_TIMEOUT;\r
531       }\r
532     }\r
533   }\r
534   \r
535   /* Check if an injected conversion is ready */\r
536   if(hadc->State == HAL_ADC_STATE_EOC_INJ)\r
537   {\r
538     /* Change ADC state */\r
539     hadc->State = HAL_ADC_STATE_EOC_INJ_REG;  \r
540   }\r
541   else\r
542   {\r
543     /* Change ADC state */\r
544     hadc->State = HAL_ADC_STATE_EOC_REG;\r
545   }\r
546   \r
547   /* Return ADC state */\r
548   return HAL_OK;\r
549 }\r
550 \r
551 /**\r
552   * @brief  Poll for conversion event\r
553   * @param  hadc: pointer to a ADC_HandleTypeDef structure that contains\r
554   *         the configuration information for the specified ADC.\r
555   * @param  EventType: the ADC event type.\r
556   *          This parameter can be one of the following values:\r
557   *            @arg ADC_AWD_EVENT: ADC Analog watch Dog event.\r
558   *            @arg ADC_OVR_EVENT: ADC Overrun event.\r
559   * @param  Timeout: Timeout value in millisecond.   \r
560   * @retval HAL status\r
561   */\r
562 HAL_StatusTypeDef HAL_ADC_PollForEvent(ADC_HandleTypeDef* hadc, uint32_t EventType, uint32_t Timeout)\r
563 {\r
564   uint32_t tickstart = 0;\r
565   \r
566   /* Check the parameters */\r
567   assert_param(IS_ADC_EVENT_TYPE(EventType));\r
568 \r
569   /* Get tick */\r
570   tickstart = HAL_GetTick();\r
571 \r
572   /* Check selected event flag */\r
573   while(!(__HAL_ADC_GET_FLAG(hadc,EventType)))\r
574   {\r
575     /* Check for the Timeout */\r
576     if(Timeout != HAL_MAX_DELAY)\r
577     {\r
578       if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))\r
579       {\r
580         hadc->State= HAL_ADC_STATE_TIMEOUT;\r
581         /* Process unlocked */\r
582         __HAL_UNLOCK(hadc);\r
583         return HAL_TIMEOUT;\r
584       }\r
585     }\r
586   }\r
587   \r
588   /* Check analog watchdog flag */\r
589   if(EventType == ADC_AWD_EVENT)\r
590   {\r
591      /* Change ADC state */\r
592      hadc->State = HAL_ADC_STATE_AWD;\r
593       \r
594      /* Clear the ADCx's analog watchdog flag */\r
595      __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD);\r
596   }\r
597   else\r
598   {\r
599      /* Change ADC state */\r
600      hadc->State = HAL_ADC_STATE_ERROR;\r
601      \r
602      /* Clear the ADCx's Overrun flag */\r
603      __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);\r
604   }\r
605   \r
606   /* Return ADC state */\r
607   return HAL_OK;\r
608 }\r
609 \r
610 \r
611 /**\r
612   * @brief  Enables the interrupt and starts ADC conversion of regular channels.\r
613   * @param  hadc: pointer to a ADC_HandleTypeDef structure that contains\r
614   *         the configuration information for the specified ADC.\r
615   * @retval HAL status.\r
616   */\r
617 HAL_StatusTypeDef HAL_ADC_Start_IT(ADC_HandleTypeDef* hadc)\r
618 {\r
619   __IO uint32_t counter = 0;\r
620   \r
621   /* Check the parameters */\r
622   assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));\r
623   assert_param(IS_ADC_EXT_TRIG_EDGE(hadc->Init.ExternalTrigConvEdge));\r
624   \r
625   /* Process locked */\r
626   __HAL_LOCK(hadc);\r
627   \r
628   /* Check if an injected conversion is ongoing */\r
629   if(hadc->State == HAL_ADC_STATE_BUSY_INJ)\r
630   {\r
631     /* Change ADC state */\r
632     hadc->State = HAL_ADC_STATE_BUSY_INJ_REG;  \r
633   }\r
634   else\r
635   {\r
636     /* Change ADC state */\r
637     hadc->State = HAL_ADC_STATE_BUSY_REG;\r
638   } \r
639   \r
640   /* Set ADC error code to none */\r
641   hadc->ErrorCode = HAL_ADC_ERROR_NONE;\r
642   \r
643   /* Check if ADC peripheral is disabled in order to enable it and wait during \r
644      Tstab time the ADC's stabilization */\r
645   if((hadc->Instance->CR2 & ADC_CR2_ADON) != ADC_CR2_ADON)\r
646   {  \r
647     /* Enable the Peripheral */\r
648     __HAL_ADC_ENABLE(hadc);\r
649     \r
650     /* Delay for ADC stabilization time */\r
651     /* Compute number of CPU cycles to wait for */\r
652     counter = (ADC_STAB_DELAY_US * (SystemCoreClock / 1000000));\r
653     while(counter != 0)\r
654     {\r
655       counter--;\r
656     }\r
657   }\r
658   \r
659   /* Enable the ADC overrun interrupt */\r
660   __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);\r
661   \r
662   /* Enable the ADC end of conversion interrupt for regular group */\r
663   __HAL_ADC_ENABLE_IT(hadc, ADC_IT_EOC);\r
664         \r
665   /* Process unlocked */\r
666   __HAL_UNLOCK(hadc);\r
667   \r
668   /* Check if Multimode enabled */\r
669   if(HAL_IS_BIT_CLR(ADC->CCR, ADC_CCR_MULTI))\r
670   {\r
671     /* if no external trigger present enable software conversion of regular channels */\r
672     if((hadc->Instance->CR2 & ADC_CR2_EXTEN) == RESET)\r
673     {\r
674       /* Enable the selected ADC software conversion for regular group */\r
675       hadc->Instance->CR2 |= (uint32_t)ADC_CR2_SWSTART;\r
676     }\r
677   }\r
678   else\r
679   {\r
680     /* if instance of handle correspond to ADC1 and  no external trigger present enable software conversion of regular channels */\r
681     if((hadc->Instance == (ADC_TypeDef*)0x40012000) && ((hadc->Instance->CR2 & ADC_CR2_EXTEN) == RESET))\r
682     {\r
683       /* Enable the selected ADC software conversion for regular group */\r
684         hadc->Instance->CR2 |= (uint32_t)ADC_CR2_SWSTART;\r
685     }\r
686   }\r
687   \r
688   /* Return function status */\r
689   return HAL_OK;\r
690 }\r
691 \r
692 /**\r
693   * @brief  Disables the interrupt and stop ADC conversion of regular channels.\r
694   * \r
695   * @note   Caution: This function will stop also injected channels.  \r
696   *\r
697   * @param  hadc: pointer to a ADC_HandleTypeDef structure that contains\r
698   *         the configuration information for the specified ADC.\r
699   * @retval HAL status.\r
700   */\r
701 HAL_StatusTypeDef HAL_ADC_Stop_IT(ADC_HandleTypeDef* hadc)\r
702 {\r
703   /* Disable the ADC end of conversion interrupt for regular group */\r
704   __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC);\r
705   \r
706   /* Disable the ADC end of conversion interrupt for injected group */\r
707   __HAL_ADC_DISABLE_IT(hadc, ADC_CR1_JEOCIE);\r
708   \r
709   /* Enable the Peripheral */\r
710   __HAL_ADC_DISABLE(hadc);\r
711   \r
712   /* Change ADC state */\r
713   hadc->State = HAL_ADC_STATE_READY;\r
714   \r
715   /* Return function status */\r
716   return HAL_OK;\r
717 }\r
718 \r
719 /**\r
720   * @brief  Handles ADC interrupt request  \r
721   * @param  hadc: pointer to a ADC_HandleTypeDef structure that contains\r
722   *         the configuration information for the specified ADC.\r
723   * @retval None\r
724   */\r
725 void HAL_ADC_IRQHandler(ADC_HandleTypeDef* hadc)\r
726 {\r
727   uint32_t tmp1 = 0, tmp2 = 0;\r
728   \r
729   /* Check the parameters */\r
730   assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));\r
731   assert_param(IS_ADC_REGULAR_LENGTH(hadc->Init.NbrOfConversion));\r
732   assert_param(IS_ADC_EOCSelection(hadc->Init.EOCSelection));\r
733   \r
734   tmp1 = __HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOC);\r
735   tmp2 = __HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_EOC);\r
736   /* Check End of conversion flag for regular channels */\r
737   if(tmp1 && tmp2)\r
738   {\r
739     /* Check if an injected conversion is ready */\r
740     if(hadc->State == HAL_ADC_STATE_EOC_INJ)\r
741     {\r
742       /* Change ADC state */\r
743       hadc->State = HAL_ADC_STATE_EOC_INJ_REG;  \r
744     }\r
745     else\r
746     {\r
747       /* Change ADC state */\r
748       hadc->State = HAL_ADC_STATE_EOC_REG;\r
749     }\r
750 \r
751     if((hadc->Init.ContinuousConvMode == DISABLE) && ((hadc->Instance->CR2 & ADC_CR2_EXTEN) == RESET))\r
752     {\r
753       if(hadc->Init.EOCSelection == ADC_EOC_SEQ_CONV)\r
754       { \r
755         /* DISABLE the ADC end of conversion interrupt for regular group */\r
756         __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC);\r
757         \r
758         /* DISABLE the ADC overrun interrupt */\r
759         __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);\r
760       }\r
761       else\r
762       {\r
763         if (hadc->NbrOfCurrentConversionRank == 0)\r
764         {\r
765           hadc->NbrOfCurrentConversionRank = hadc->Init.NbrOfConversion;\r
766         }\r
767         \r
768         /* Decrement the number of conversion when an interrupt occurs */\r
769         hadc->NbrOfCurrentConversionRank--;\r
770         \r
771         /* Check if all conversions are finished */\r
772         if(hadc->NbrOfCurrentConversionRank == 0)\r
773         {\r
774           /* DISABLE the ADC end of conversion interrupt for regular group */\r
775           __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC);\r
776           \r
777           /* DISABLE the ADC overrun interrupt */\r
778           __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);\r
779         }\r
780       }\r
781     }\r
782     \r
783     /* Conversion complete callback */ \r
784     HAL_ADC_ConvCpltCallback(hadc);\r
785     \r
786    /* Clear the ADCx flag for regular end of conversion */\r
787     __HAL_ADC_CLEAR_FLAG(hadc,ADC_FLAG_EOC);\r
788   }\r
789   \r
790   tmp1 = __HAL_ADC_GET_FLAG(hadc, ADC_FLAG_JEOC);\r
791   tmp2 = __HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_JEOC);                               \r
792   /* Check End of conversion flag for injected channels */\r
793   if(tmp1 && tmp2)\r
794   {\r
795     /* Check if a regular conversion is ready */\r
796     if(hadc->State == HAL_ADC_STATE_EOC_REG)\r
797     {\r
798       /* Change ADC state */\r
799       hadc->State = HAL_ADC_STATE_EOC_INJ_REG;  \r
800     }\r
801     else\r
802     {\r
803       /* Change ADC state */\r
804       hadc->State = HAL_ADC_STATE_EOC_INJ;\r
805     }\r
806     \r
807     tmp1 = HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO);\r
808     tmp2 = HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_JEXTEN);\r
809     if(((hadc->Init.ContinuousConvMode == DISABLE) || tmp1) && tmp2)\r
810     {\r
811       /* DISABLE the ADC end of conversion interrupt for injected group */\r
812       __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC);\r
813     }\r
814     \r
815     /* Conversion complete callback */ \r
816     HAL_ADCEx_InjectedConvCpltCallback(hadc);\r
817     \r
818    /* Clear the ADCx flag for injected end of conversion */\r
819     __HAL_ADC_CLEAR_FLAG(hadc,ADC_FLAG_JEOC);\r
820   }\r
821   \r
822   tmp1 = __HAL_ADC_GET_FLAG(hadc, ADC_FLAG_AWD);\r
823   tmp2 = __HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_AWD);                          \r
824   /* Check Analog watchdog flag */\r
825   if(tmp1 && tmp2)\r
826   {\r
827     /* Change ADC state */\r
828     hadc->State = HAL_ADC_STATE_AWD;\r
829       \r
830     /* Clear the ADCx's Analog watchdog flag */\r
831     __HAL_ADC_CLEAR_FLAG(hadc,ADC_FLAG_AWD);\r
832     \r
833     /* Level out of window callback */ \r
834     HAL_ADC_LevelOutOfWindowCallback(hadc);\r
835   }\r
836   \r
837   tmp1 = __HAL_ADC_GET_FLAG(hadc, ADC_FLAG_OVR);\r
838   tmp2 = __HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_OVR);\r
839   /* Check Overrun flag */\r
840   if(tmp1 && tmp2)\r
841   {\r
842     /* Change ADC state to overrun state */\r
843     hadc->State = HAL_ADC_STATE_ERROR;\r
844     \r
845     /* Set ADC error code to overrun */\r
846     hadc->ErrorCode |= HAL_ADC_ERROR_OVR;\r
847     \r
848     /* Clear the Overrun flag */\r
849     __HAL_ADC_CLEAR_FLAG(hadc,ADC_FLAG_OVR);\r
850     \r
851     /* Error callback */ \r
852     HAL_ADC_ErrorCallback(hadc);\r
853   }\r
854 }\r
855 \r
856 /**\r
857   * @brief  Enables ADC DMA request after last transfer (Single-ADC mode) and enables ADC peripheral  \r
858   * @param  hadc: pointer to a ADC_HandleTypeDef structure that contains\r
859   *         the configuration information for the specified ADC.\r
860   * @param  pData: The destination Buffer address.\r
861   * @param  Length: The length of data to be transferred from ADC peripheral to memory.\r
862   * @retval HAL status\r
863   */\r
864 HAL_StatusTypeDef HAL_ADC_Start_DMA(ADC_HandleTypeDef* hadc, uint32_t* pData, uint32_t Length)\r
865 {\r
866   __IO uint32_t counter = 0;\r
867   \r
868   /* Check the parameters */\r
869   assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));\r
870   assert_param(IS_ADC_EXT_TRIG_EDGE(hadc->Init.ExternalTrigConvEdge));\r
871   \r
872   /* Process locked */\r
873   __HAL_LOCK(hadc);\r
874   \r
875   /* Enable ADC overrun interrupt */\r
876   __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);\r
877   \r
878   /* Enable ADC DMA mode */\r
879   hadc->Instance->CR2 |= ADC_CR2_DMA;\r
880   \r
881   /* Set the DMA transfer complete callback */\r
882   hadc->DMA_Handle->XferCpltCallback = ADC_DMAConvCplt;\r
883   \r
884   /* Set the DMA half transfer complete callback */\r
885   hadc->DMA_Handle->XferHalfCpltCallback = ADC_DMAHalfConvCplt;\r
886      \r
887   /* Set the DMA error callback */\r
888   hadc->DMA_Handle->XferErrorCallback = ADC_DMAError ;\r
889   \r
890   /* Enable the DMA Stream */\r
891   HAL_DMA_Start_IT(hadc->DMA_Handle, (uint32_t)&hadc->Instance->DR, (uint32_t)pData, Length);\r
892   \r
893   /* Change ADC state */\r
894   hadc->State = HAL_ADC_STATE_BUSY_REG;\r
895 \r
896   /* Process unlocked */\r
897   __HAL_UNLOCK(hadc);\r
898 \r
899   /* Check if ADC peripheral is disabled in order to enable it and wait during \r
900      Tstab time the ADC's stabilization */\r
901   if((hadc->Instance->CR2 & ADC_CR2_ADON) != ADC_CR2_ADON)\r
902   {  \r
903     /* Enable the Peripheral */\r
904     __HAL_ADC_ENABLE(hadc);\r
905     \r
906     /* Delay for ADC stabilization time */\r
907     /* Compute number of CPU cycles to wait for */\r
908     counter = (ADC_STAB_DELAY_US * (SystemCoreClock / 1000000));\r
909     while(counter != 0)\r
910     {\r
911       counter--;\r
912     }\r
913   }\r
914   \r
915   /* if no external trigger present enable software conversion of regular channels */\r
916   if((hadc->Instance->CR2 & ADC_CR2_EXTEN) == RESET)\r
917   {\r
918     /* Enable the selected ADC software conversion for regular group */\r
919     hadc->Instance->CR2 |= ADC_CR2_SWSTART;\r
920   }\r
921   \r
922   /* Return function status */\r
923   return HAL_OK;\r
924 }\r
925 \r
926 /**\r
927   * @brief  Disables ADC DMA (Single-ADC mode) and disables ADC peripheral    \r
928   * @param  hadc: pointer to a ADC_HandleTypeDef structure that contains\r
929   *         the configuration information for the specified ADC.\r
930   * @retval HAL status\r
931   */\r
932 HAL_StatusTypeDef HAL_ADC_Stop_DMA(ADC_HandleTypeDef* hadc)\r
933 {\r
934   /* Disable the Peripheral */\r
935   __HAL_ADC_DISABLE(hadc);\r
936   \r
937   /* Disable ADC overrun interrupt */\r
938   __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);\r
939   \r
940   /* Disable the selected ADC DMA mode */\r
941   hadc->Instance->CR2 &= ~ADC_CR2_DMA;\r
942   \r
943   /* Disable the ADC DMA Stream */\r
944   HAL_DMA_Abort(hadc->DMA_Handle);\r
945   \r
946   /* Change ADC state */\r
947   hadc->State = HAL_ADC_STATE_READY;\r
948   \r
949   /* Return function status */\r
950   return HAL_OK;\r
951 }\r
952 \r
953 /**\r
954   * @brief  Gets the converted value from data register of regular channel.\r
955   * @param  hadc: pointer to a ADC_HandleTypeDef structure that contains\r
956   *         the configuration information for the specified ADC.\r
957   * @retval Converted value\r
958   */\r
959 uint32_t HAL_ADC_GetValue(ADC_HandleTypeDef* hadc)\r
960 {       \r
961   /* Return the selected ADC converted value */ \r
962   return hadc->Instance->DR;\r
963 }\r
964 \r
965 /**\r
966   * @brief  Regular conversion complete callback in non blocking mode \r
967   * @param  hadc: pointer to a ADC_HandleTypeDef structure that contains\r
968   *         the configuration information for the specified ADC.\r
969   * @retval None\r
970   */\r
971 __weak void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)\r
972 {\r
973   /* NOTE : This function Should not be modified, when the callback is needed,\r
974             the HAL_ADC_ConvCpltCallback could be implemented in the user file\r
975    */\r
976 }\r
977 \r
978 /**\r
979   * @brief  Regular conversion half DMA transfer callback in non blocking mode \r
980   * @param  hadc: pointer to a ADC_HandleTypeDef structure that contains\r
981   *         the configuration information for the specified ADC.\r
982   * @retval None\r
983   */\r
984 __weak void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc)\r
985 {\r
986   /* NOTE : This function Should not be modified, when the callback is needed,\r
987             the HAL_ADC_ConvHalfCpltCallback could be implemented in the user file\r
988    */\r
989 }\r
990 \r
991 /**\r
992   * @brief  Analog watchdog callback in non blocking mode \r
993   * @param  hadc: pointer to a ADC_HandleTypeDef structure that contains\r
994   *         the configuration information for the specified ADC.\r
995   * @retval None\r
996   */\r
997 __weak void HAL_ADC_LevelOutOfWindowCallback(ADC_HandleTypeDef* hadc)\r
998 {\r
999   /* NOTE : This function Should not be modified, when the callback is needed,\r
1000             the HAL_ADC_LevelOoutOfWindowCallback could be implemented in the user file\r
1001    */\r
1002 }\r
1003 \r
1004 /**\r
1005   * @brief  Error ADC callback.\r
1006   * @param  hadc: pointer to a ADC_HandleTypeDef structure that contains\r
1007   *         the configuration information for the specified ADC.\r
1008   * @retval None\r
1009   */\r
1010 __weak void HAL_ADC_ErrorCallback(ADC_HandleTypeDef *hadc)\r
1011 {\r
1012   /* NOTE : This function Should not be modified, when the callback is needed,\r
1013             the HAL_ADC_ErrorCallback could be implemented in the user file\r
1014    */\r
1015 }\r
1016 \r
1017 /**\r
1018   * @}\r
1019   */\r
1020   \r
1021 /** @defgroup ADC_Exported_Functions_Group3 Peripheral Control functions\r
1022  *  @brief      Peripheral Control functions \r
1023  *\r
1024 @verbatim   \r
1025  ===============================================================================\r
1026              ##### Peripheral Control functions #####\r
1027  ===============================================================================  \r
1028     [..]  This section provides functions allowing to:\r
1029       (+) Configure regular channels. \r
1030       (+) Configure injected channels.\r
1031       (+) Configure multimode.\r
1032       (+) Configure the analog watch dog.\r
1033       \r
1034 @endverbatim\r
1035   * @{\r
1036   */\r
1037 \r
1038   /**\r
1039   * @brief  Configures for the selected ADC regular channel its corresponding\r
1040   *         rank in the sequencer and its sample time.\r
1041   * @param  hadc: pointer to a ADC_HandleTypeDef structure that contains\r
1042   *         the configuration information for the specified ADC.\r
1043   * @param  sConfig: ADC configuration structure. \r
1044   * @retval HAL status\r
1045   */\r
1046 HAL_StatusTypeDef HAL_ADC_ConfigChannel(ADC_HandleTypeDef* hadc, ADC_ChannelConfTypeDef* sConfig)\r
1047 {\r
1048   __IO uint32_t counter = 0;\r
1049 \r
1050   /* Check the parameters */\r
1051   assert_param(IS_ADC_CHANNEL(sConfig->Channel));\r
1052   assert_param(IS_ADC_REGULAR_RANK(sConfig->Rank));\r
1053   assert_param(IS_ADC_SAMPLE_TIME(sConfig->SamplingTime));\r
1054   \r
1055   /* Process locked */\r
1056   __HAL_LOCK(hadc);\r
1057     \r
1058   /* if ADC_Channel_10 ... ADC_Channel_18 is selected */\r
1059   if (sConfig->Channel > ADC_CHANNEL_9)\r
1060   {\r
1061     /* Clear the old sample time */\r
1062     hadc->Instance->SMPR1 &= ~ADC_SMPR1(ADC_SMPR1_SMP10, sConfig->Channel);\r
1063     \r
1064     /* Set the new sample time */\r
1065     hadc->Instance->SMPR1 |= ADC_SMPR1(sConfig->SamplingTime, sConfig->Channel);\r
1066   }\r
1067   else /* ADC_Channel include in ADC_Channel_[0..9] */\r
1068   {\r
1069     /* Clear the old sample time */\r
1070     hadc->Instance->SMPR2 &= ~ADC_SMPR2(ADC_SMPR2_SMP0, sConfig->Channel);\r
1071     \r
1072     /* Set the new sample time */\r
1073     hadc->Instance->SMPR2 |= ADC_SMPR2(sConfig->SamplingTime, sConfig->Channel);\r
1074   }\r
1075   \r
1076   /* For Rank 1 to 6 */\r
1077   if (sConfig->Rank < 7)\r
1078   {\r
1079     /* Clear the old SQx bits for the selected rank */\r
1080     hadc->Instance->SQR3 &= ~ADC_SQR3_RK(ADC_SQR3_SQ1, sConfig->Rank);\r
1081     \r
1082     /* Set the SQx bits for the selected rank */\r
1083     hadc->Instance->SQR3 |= ADC_SQR3_RK(sConfig->Channel, sConfig->Rank);\r
1084   }\r
1085   /* For Rank 7 to 12 */\r
1086   else if (sConfig->Rank < 13)\r
1087   {\r
1088     /* Clear the old SQx bits for the selected rank */\r
1089     hadc->Instance->SQR2 &= ~ADC_SQR2_RK(ADC_SQR2_SQ7, sConfig->Rank);\r
1090     \r
1091     /* Set the SQx bits for the selected rank */\r
1092     hadc->Instance->SQR2 |= ADC_SQR2_RK(sConfig->Channel, sConfig->Rank);\r
1093   }\r
1094   /* For Rank 13 to 16 */\r
1095   else\r
1096   {\r
1097     /* Clear the old SQx bits for the selected rank */\r
1098     hadc->Instance->SQR1 &= ~ADC_SQR1_RK(ADC_SQR1_SQ13, sConfig->Rank);\r
1099     \r
1100     /* Set the SQx bits for the selected rank */\r
1101     hadc->Instance->SQR1 |= ADC_SQR1_RK(sConfig->Channel, sConfig->Rank);\r
1102   }\r
1103   \r
1104   /* if ADC1 Channel_18 is selected enable VBAT Channel */\r
1105   if ((hadc->Instance == ADC1) && (sConfig->Channel == ADC_CHANNEL_VBAT))\r
1106   {\r
1107     /* Enable the VBAT channel*/\r
1108     ADC->CCR |= ADC_CCR_VBATE;\r
1109   }\r
1110   \r
1111   /* if ADC1 Channel_16 or Channel_17 is selected enable TSVREFE Channel(Temperature sensor and VREFINT) */\r
1112   if ((hadc->Instance == ADC1) && ((sConfig->Channel == ADC_CHANNEL_TEMPSENSOR) || (sConfig->Channel == ADC_CHANNEL_VREFINT)))\r
1113   {\r
1114     /* Enable the TSVREFE channel*/\r
1115     ADC->CCR |= ADC_CCR_TSVREFE;\r
1116 \r
1117     if((sConfig->Channel == ADC_CHANNEL_TEMPSENSOR))\r
1118     {\r
1119       /* Delay for temperature sensor stabilization time */\r
1120       /* Compute number of CPU cycles to wait for */\r
1121       counter = (ADC_TEMPSENSOR_DELAY_US * (SystemCoreClock / 1000000));\r
1122       while(counter != 0)\r
1123       {\r
1124         counter--;\r
1125       }\r
1126     }\r
1127   }\r
1128   \r
1129   /* Process unlocked */\r
1130   __HAL_UNLOCK(hadc);\r
1131   \r
1132   /* Return function status */\r
1133   return HAL_OK;\r
1134 }\r
1135 \r
1136 /**\r
1137   * @brief  Configures the analog watchdog.\r
1138   * @param  hadc: pointer to a ADC_HandleTypeDef structure that contains\r
1139   *         the configuration information for the specified ADC.\r
1140   * @param  AnalogWDGConfig : pointer to an ADC_AnalogWDGConfTypeDef structure \r
1141   *         that contains the configuration information of ADC analog watchdog.\r
1142   * @retval HAL status    \r
1143   */\r
1144 HAL_StatusTypeDef HAL_ADC_AnalogWDGConfig(ADC_HandleTypeDef* hadc, ADC_AnalogWDGConfTypeDef* AnalogWDGConfig)\r
1145 {\r
1146 #ifdef USE_FULL_ASSERT  \r
1147   uint32_t tmp = 0;\r
1148 #endif /* USE_FULL_ASSERT  */  \r
1149   \r
1150   /* Check the parameters */\r
1151   assert_param(IS_ADC_ANALOG_WATCHDOG(AnalogWDGConfig->WatchdogMode));\r
1152   assert_param(IS_ADC_CHANNEL(AnalogWDGConfig->Channel));\r
1153   assert_param(IS_FUNCTIONAL_STATE(AnalogWDGConfig->ITMode));\r
1154 \r
1155 #ifdef USE_FULL_ASSERT  \r
1156   tmp = ADC_GET_RESOLUTION(hadc);\r
1157   assert_param(IS_ADC_RANGE(tmp, AnalogWDGConfig->HighThreshold));\r
1158   assert_param(IS_ADC_RANGE(tmp, AnalogWDGConfig->LowThreshold));\r
1159 #endif /* USE_FULL_ASSERT  */\r
1160   \r
1161   /* Process locked */\r
1162   __HAL_LOCK(hadc);\r
1163   \r
1164   if(AnalogWDGConfig->ITMode == ENABLE)\r
1165   {\r
1166     /* Enable the ADC Analog watchdog interrupt */\r
1167     __HAL_ADC_ENABLE_IT(hadc, ADC_IT_AWD);\r
1168   }\r
1169   else\r
1170   {\r
1171     /* Disable the ADC Analog watchdog interrupt */\r
1172     __HAL_ADC_DISABLE_IT(hadc, ADC_IT_AWD);\r
1173   }\r
1174   \r
1175   /* Clear AWDEN, JAWDEN and AWDSGL bits */\r
1176   hadc->Instance->CR1 &=  ~(ADC_CR1_AWDSGL | ADC_CR1_JAWDEN | ADC_CR1_AWDEN);\r
1177   \r
1178   /* Set the analog watchdog enable mode */\r
1179   hadc->Instance->CR1 |= AnalogWDGConfig->WatchdogMode;\r
1180   \r
1181   /* Set the high threshold */\r
1182   hadc->Instance->HTR = AnalogWDGConfig->HighThreshold;\r
1183   \r
1184   /* Set the low threshold */\r
1185   hadc->Instance->LTR = AnalogWDGConfig->LowThreshold;\r
1186   \r
1187   /* Clear the Analog watchdog channel select bits */\r
1188   hadc->Instance->CR1 &= ~ADC_CR1_AWDCH;\r
1189   \r
1190   /* Set the Analog watchdog channel */\r
1191   hadc->Instance->CR1 |= (uint32_t)((uint16_t)(AnalogWDGConfig->Channel));\r
1192   \r
1193   /* Process unlocked */\r
1194   __HAL_UNLOCK(hadc);\r
1195   \r
1196   /* Return function status */\r
1197   return HAL_OK;\r
1198 }\r
1199 \r
1200 /**\r
1201   * @}\r
1202   */\r
1203 \r
1204 /** @defgroup ADC_Exported_Functions_Group4 ADC Peripheral State functions\r
1205  *  @brief   ADC Peripheral State functions \r
1206  *\r
1207 @verbatim   \r
1208  ===============================================================================\r
1209             ##### Peripheral State and errors functions #####\r
1210  ===============================================================================  \r
1211     [..]\r
1212     This subsection provides functions allowing to\r
1213       (+) Check the ADC state\r
1214       (+) Check the ADC Error\r
1215          \r
1216 @endverbatim\r
1217   * @{\r
1218   */\r
1219   \r
1220 /**\r
1221   * @brief  return the ADC state\r
1222   * @param  hadc: pointer to a ADC_HandleTypeDef structure that contains\r
1223   *         the configuration information for the specified ADC.\r
1224   * @retval HAL state\r
1225   */\r
1226 HAL_ADC_StateTypeDef HAL_ADC_GetState(ADC_HandleTypeDef* hadc)\r
1227 {\r
1228   /* Return ADC state */\r
1229   return hadc->State;\r
1230 }\r
1231 \r
1232 /**\r
1233   * @brief  Return the ADC error code\r
1234   * @param  hadc: pointer to a ADC_HandleTypeDef structure that contains\r
1235   *         the configuration information for the specified ADC.\r
1236   * @retval ADC Error Code\r
1237   */\r
1238 uint32_t HAL_ADC_GetError(ADC_HandleTypeDef *hadc)\r
1239 {\r
1240   return hadc->ErrorCode;\r
1241 }\r
1242 \r
1243 /**\r
1244   * @}\r
1245   */\r
1246 \r
1247 /**\r
1248   * @}\r
1249   */\r
1250 \r
1251 /* Private functions ---------------------------------------------------------*/\r
1252 \r
1253 /** @defgroup ADC_Private_Functions\r
1254   * @{\r
1255   */\r
1256 \r
1257 /**\r
1258   * @brief  Initializes the ADCx peripheral according to the specified parameters \r
1259   *         in the ADC_InitStruct without initializing the ADC MSP.       \r
1260   * @param  hadc: pointer to a ADC_HandleTypeDef structure that contains\r
1261   *         the configuration information for the specified ADC.  \r
1262   * @retval None\r
1263   */\r
1264 static void ADC_Init(ADC_HandleTypeDef* hadc)\r
1265 {\r
1266   /* Set ADC parameters */\r
1267   /* Set the ADC clock prescaler */\r
1268   ADC->CCR &= ~(ADC_CCR_ADCPRE);\r
1269   ADC->CCR |=  hadc->Init.ClockPrescaler;\r
1270   \r
1271   /* Set ADC scan mode */\r
1272   hadc->Instance->CR1 &= ~(ADC_CR1_SCAN);\r
1273   hadc->Instance->CR1 |=  ADC_CR1_SCANCONV(hadc->Init.ScanConvMode);\r
1274   \r
1275   /* Set ADC resolution */\r
1276   hadc->Instance->CR1 &= ~(ADC_CR1_RES);\r
1277   hadc->Instance->CR1 |=  hadc->Init.Resolution;\r
1278   \r
1279   /* Set ADC data alignment */\r
1280   hadc->Instance->CR2 &= ~(ADC_CR2_ALIGN);\r
1281   hadc->Instance->CR2 |= hadc->Init.DataAlign;\r
1282   \r
1283   /* Enable external trigger if trigger selection is different of software  */\r
1284   /* start.                                                                 */\r
1285   /* Note: This configuration keeps the hardware feature of parameter       */\r
1286   /*       ExternalTrigConvEdge "trigger edge none" equivalent to           */\r
1287   /*       software start.                                                  */\r
1288   if(hadc->Init.ExternalTrigConv != ADC_SOFTWARE_START)\r
1289   {\r
1290     /* Select external trigger to start conversion */\r
1291     hadc->Instance->CR2 &= ~(ADC_CR2_EXTSEL);\r
1292     hadc->Instance->CR2 |= hadc->Init.ExternalTrigConv;\r
1293     \r
1294     /* Select external trigger polarity */\r
1295     hadc->Instance->CR2 &= ~(ADC_CR2_EXTEN);\r
1296     hadc->Instance->CR2 |= hadc->Init.ExternalTrigConvEdge;\r
1297   }\r
1298   else\r
1299   {\r
1300     /* Reset the external trigger */\r
1301     hadc->Instance->CR2 &= ~(ADC_CR2_EXTSEL);\r
1302     hadc->Instance->CR2 &= ~(ADC_CR2_EXTEN);\r
1303   }\r
1304   \r
1305   /* Enable or disable ADC continuous conversion mode */\r
1306   hadc->Instance->CR2 &= ~(ADC_CR2_CONT);\r
1307   hadc->Instance->CR2 |= ADC_CR2_CONTINUOUS(hadc->Init.ContinuousConvMode);\r
1308   \r
1309   if(hadc->Init.DiscontinuousConvMode != DISABLE)\r
1310   {\r
1311     assert_param(IS_ADC_REGULAR_DISC_NUMBER(hadc->Init.NbrOfDiscConversion));\r
1312   \r
1313     /* Enable the selected ADC regular discontinuous mode */\r
1314     hadc->Instance->CR1 |= (uint32_t)ADC_CR1_DISCEN;\r
1315     \r
1316     /* Set the number of channels to be converted in discontinuous mode */\r
1317     hadc->Instance->CR1 &= ~(ADC_CR1_DISCNUM);\r
1318     hadc->Instance->CR1 |=  ADC_CR1_DISCONTINUOUS(hadc->Init.NbrOfDiscConversion);\r
1319   }\r
1320   else\r
1321   {\r
1322     /* Disable the selected ADC regular discontinuous mode */\r
1323     hadc->Instance->CR1 &= ~(ADC_CR1_DISCEN);\r
1324   }\r
1325   \r
1326   /* Set ADC number of conversion */\r
1327   hadc->Instance->SQR1 &= ~(ADC_SQR1_L);\r
1328   hadc->Instance->SQR1 |=  ADC_SQR1(hadc->Init.NbrOfConversion);\r
1329   \r
1330   /* Enable or disable ADC DMA continuous request */\r
1331   hadc->Instance->CR2 &= ~(ADC_CR2_DDS);\r
1332   hadc->Instance->CR2 |= ADC_CR2_DMAContReq(hadc->Init.DMAContinuousRequests);\r
1333   \r
1334   /* Enable or disable ADC end of conversion selection */\r
1335   hadc->Instance->CR2 &= ~(ADC_CR2_EOCS);\r
1336   hadc->Instance->CR2 |= ADC_CR2_EOCSelection(hadc->Init.EOCSelection);\r
1337 }\r
1338 \r
1339 /**\r
1340   * @brief  DMA transfer complete callback. \r
1341   * @param  hdma: pointer to a DMA_HandleTypeDef structure that contains\r
1342   *                the configuration information for the specified DMA module.\r
1343   * @retval None\r
1344   */\r
1345 static void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma)   \r
1346 {\r
1347   ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;\r
1348     \r
1349   /* Check if an injected conversion is ready */\r
1350   if(hadc->State == HAL_ADC_STATE_EOC_INJ)\r
1351   {\r
1352     /* Change ADC state */\r
1353     hadc->State = HAL_ADC_STATE_EOC_INJ_REG;  \r
1354   }\r
1355   else\r
1356   {\r
1357     /* Change ADC state */\r
1358     hadc->State = HAL_ADC_STATE_EOC_REG;\r
1359   }\r
1360     \r
1361   HAL_ADC_ConvCpltCallback(hadc); \r
1362 }\r
1363 \r
1364 /**\r
1365   * @brief  DMA half transfer complete callback. \r
1366   * @param  hdma: pointer to a DMA_HandleTypeDef structure that contains\r
1367   *                the configuration information for the specified DMA module.\r
1368   * @retval None\r
1369   */\r
1370 static void ADC_DMAHalfConvCplt(DMA_HandleTypeDef *hdma)   \r
1371 {\r
1372   ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;\r
1373   /* Conversion complete callback */\r
1374   HAL_ADC_ConvHalfCpltCallback(hadc); \r
1375 }\r
1376 \r
1377 /**\r
1378   * @brief  DMA error callback \r
1379   * @param  hdma: pointer to a DMA_HandleTypeDef structure that contains\r
1380   *                the configuration information for the specified DMA module.\r
1381   * @retval None\r
1382   */\r
1383 static void ADC_DMAError(DMA_HandleTypeDef *hdma)   \r
1384 {\r
1385   ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;\r
1386   hadc->State= HAL_ADC_STATE_ERROR;\r
1387   /* Set ADC error code to DMA error */\r
1388   hadc->ErrorCode |= HAL_ADC_ERROR_DMA;\r
1389   HAL_ADC_ErrorCallback(hadc); \r
1390 }\r
1391 \r
1392 \r
1393 /**\r
1394   * @}\r
1395   */\r
1396 \r
1397 #endif /* HAL_ADC_MODULE_ENABLED */\r
1398 /**\r
1399   * @}\r
1400   */ \r
1401 \r
1402 /**\r
1403   * @}\r
1404   */ \r
1405 \r
1406 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/\r