]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M7_STM32F7_STM32756G-EVAL_IAR_Keil/ST_Library/stm32f7xx_hal_adc_ex.c
Final V8.2.1 release ready for tagging:
[freertos] / FreeRTOS / Demo / CORTEX_M7_STM32F7_STM32756G-EVAL_IAR_Keil / ST_Library / stm32f7xx_hal_adc_ex.c
1 /**\r
2   ******************************************************************************\r
3   * @file    stm32f7xx_hal_adc_ex.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 ADC extension peripheral:\r
9   *           + Extended features functions\r
10   *         \r
11   @verbatim\r
12   ==============================================================================\r
13                     ##### How to use this driver #####\r
14   ==============================================================================\r
15     [..]\r
16     (#)Initialize the ADC low level resources by implementing the HAL_ADC_MspInit():\r
17        (##) Enable the ADC interface clock using __HAL_RCC_ADC_CLK_ENABLE()\r
18        (##) ADC pins configuration\r
19              (+++) Enable the clock for the ADC GPIOs using the following function:\r
20                    __HAL_RCC_GPIOx_CLK_ENABLE()  \r
21              (+++) Configure these ADC pins in analog mode using HAL_GPIO_Init() \r
22        (##) In case of using interrupts (e.g. HAL_ADC_Start_IT())\r
23              (+++) Configure the ADC interrupt priority using HAL_NVIC_SetPriority()\r
24              (+++) Enable the ADC IRQ handler using HAL_NVIC_EnableIRQ()\r
25              (+++) In ADC IRQ handler, call HAL_ADC_IRQHandler()\r
26       (##) In case of using DMA to control data transfer (e.g. HAL_ADC_Start_DMA())\r
27              (+++) Enable the DMAx interface clock using __HAL_RCC_DMAx_CLK_ENABLE()\r
28              (+++) Configure and enable two DMA streams stream for managing data\r
29                  transfer from peripheral to memory (output stream)\r
30              (+++) Associate the initialized DMA handle to the ADC DMA handle\r
31                  using  __HAL_LINKDMA()\r
32              (+++) Configure the priority and enable the NVIC for the transfer complete\r
33                  interrupt on the two DMA Streams. The output stream should have higher\r
34                  priority than the input stream.                  \r
35      (#) Configure the ADC Prescaler, conversion resolution and data alignment \r
36          using the HAL_ADC_Init() function. \r
37   \r
38      (#) Configure the ADC Injected channels group features, use HAL_ADC_Init()\r
39          and HAL_ADC_ConfigChannel() functions.\r
40          \r
41      (#) Three operation modes are available within this driver :     \r
42   \r
43      *** Polling mode IO operation ***\r
44      =================================\r
45      [..]    \r
46        (+) Start the ADC peripheral using HAL_ADCEx_InjectedStart() \r
47        (+) Wait for end of conversion using HAL_ADC_PollForConversion(), at this stage\r
48            user can specify the value of timeout according to his end application      \r
49        (+) To read the ADC converted values, use the HAL_ADCEx_InjectedGetValue() function.\r
50        (+) Stop the ADC peripheral using HAL_ADCEx_InjectedStop()\r
51   \r
52      *** Interrupt mode IO operation ***    \r
53      ===================================\r
54      [..]    \r
55        (+) Start the ADC peripheral using HAL_ADCEx_InjectedStart_IT() \r
56        (+) Use HAL_ADC_IRQHandler() called under ADC_IRQHandler() Interrupt subroutine\r
57        (+) At ADC end of conversion HAL_ADCEx_InjectedConvCpltCallback() function is executed and user can \r
58             add his own code by customization of function pointer HAL_ADCEx_InjectedConvCpltCallback \r
59        (+) In case of ADC Error, HAL_ADCEx_InjectedErrorCallback() function is executed and user can \r
60             add his own code by customization of function pointer HAL_ADCEx_InjectedErrorCallback\r
61        (+) Stop the ADC peripheral using HAL_ADCEx_InjectedStop_IT()\r
62        \r
63             \r
64      *** DMA mode IO operation ***    \r
65      ==============================\r
66      [..]    \r
67        (+) Start the ADC peripheral using HAL_ADCEx_InjectedStart_DMA(), at this stage the user specify the length \r
68            of data to be transferred at each end of conversion \r
69        (+) At The end of data transfer ba HAL_ADCEx_InjectedConvCpltCallback() function is executed and user can \r
70             add his own code by customization of function pointer HAL_ADCEx_InjectedConvCpltCallback \r
71        (+) In case of transfer Error, HAL_ADCEx_InjectedErrorCallback() function is executed and user can \r
72             add his own code by customization of function pointer HAL_ADCEx_InjectedErrorCallback\r
73         (+) Stop the ADC peripheral using HAL_ADCEx_InjectedStop_DMA()\r
74         \r
75      *** Multi mode ADCs Regular channels configuration ***\r
76      ======================================================\r
77      [..]        \r
78        (+) Select the Multi mode ADC regular channels features (dual or triple mode)  \r
79           and configure the DMA mode using HAL_ADCEx_MultiModeConfigChannel() functions. \r
80        (+) Start the ADC peripheral using HAL_ADCEx_MultiModeStart_DMA(), at this stage the user specify the length \r
81            of data to be transferred at each end of conversion           \r
82        (+) Read the ADCs converted values using the HAL_ADCEx_MultiModeGetValue() function.\r
83   \r
84   \r
85     @endverbatim\r
86   ******************************************************************************\r
87   * @attention\r
88   *\r
89   * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>\r
90   *\r
91   * Redistribution and use in source and binary forms, with or without modification,\r
92   * are permitted provided that the following conditions are met:\r
93   *   1. Redistributions of source code must retain the above copyright notice,\r
94   *      this list of conditions and the following disclaimer.\r
95   *   2. Redistributions in binary form must reproduce the above copyright notice,\r
96   *      this list of conditions and the following disclaimer in the documentation\r
97   *      and/or other materials provided with the distribution.\r
98   *   3. Neither the name of STMicroelectronics nor the names of its contributors\r
99   *      may be used to endorse or promote products derived from this software\r
100   *      without specific prior written permission.\r
101   *\r
102   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"\r
103   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
104   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
105   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\r
106   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
107   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\r
108   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\r
109   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\r
110   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\r
111   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
112   *\r
113   ******************************************************************************\r
114   */ \r
115 \r
116 /* Includes ------------------------------------------------------------------*/\r
117 #include "stm32f7xx_hal.h"\r
118 \r
119 /** @addtogroup STM32F7xx_HAL_Driver\r
120   * @{\r
121   */\r
122 \r
123 /** @defgroup ADCEx ADCEx\r
124   * @brief ADC Extended driver modules\r
125   * @{\r
126   */ \r
127 \r
128 #ifdef HAL_ADC_MODULE_ENABLED\r
129     \r
130 /* Private typedef -----------------------------------------------------------*/\r
131 /* Private define ------------------------------------------------------------*/ \r
132 /* Private macro -------------------------------------------------------------*/\r
133 /* Private variables ---------------------------------------------------------*/\r
134 /* Private function prototypes -----------------------------------------------*/\r
135 /** @addtogroup ADCEx_Private_Functions\r
136   * @{\r
137   */\r
138 static void ADC_MultiModeDMAConvCplt(DMA_HandleTypeDef *hdma);\r
139 static void ADC_MultiModeDMAError(DMA_HandleTypeDef *hdma);\r
140 static void ADC_MultiModeDMAHalfConvCplt(DMA_HandleTypeDef *hdma);\r
141 /**\r
142   * @}\r
143   */\r
144 \r
145 /* Exported functions ---------------------------------------------------------*/\r
146 /** @defgroup ADCEx_Exported_Functions ADC Exported Functions\r
147   * @{\r
148   */ \r
149 \r
150 /** @defgroup ADCEx_Exported_Functions_Group1  Extended features functions\r
151  *  @brief    Extended features functions  \r
152  *\r
153 @verbatim   \r
154  ===============================================================================\r
155                  ##### Extended features functions #####\r
156  ===============================================================================  \r
157     [..]  This section provides functions allowing to:\r
158       (+) Start conversion of injected channel.\r
159       (+) Stop conversion of injected channel.\r
160       (+) Start multimode and enable DMA transfer.\r
161       (+) Stop multimode and disable DMA transfer.\r
162       (+) Get result of injected channel conversion.\r
163       (+) Get result of multimode conversion.\r
164       (+) Configure injected channels.\r
165       (+) Configure multimode.\r
166                \r
167 @endverbatim\r
168   * @{\r
169   */\r
170 \r
171 /**\r
172   * @brief  Enables the selected ADC software start conversion of the injected channels.\r
173   * @param  hadc: pointer to a ADC_HandleTypeDef structure that contains\r
174   *         the configuration information for the specified ADC.\r
175   * @retval HAL status\r
176   */\r
177 HAL_StatusTypeDef HAL_ADCEx_InjectedStart(ADC_HandleTypeDef* hadc)\r
178 {\r
179   __IO uint32_t counter = 0;\r
180   uint32_t tmp1 = 0, tmp2 = 0;\r
181   \r
182   /* Process locked */\r
183   __HAL_LOCK(hadc);\r
184   \r
185   /* Check if a regular conversion is ongoing */\r
186   if(hadc->State == HAL_ADC_STATE_BUSY_REG)\r
187   {\r
188     /* Change ADC state */\r
189     hadc->State = HAL_ADC_STATE_BUSY_INJ_REG;  \r
190   }\r
191   else\r
192   {\r
193     /* Change ADC state */\r
194     hadc->State = HAL_ADC_STATE_BUSY_INJ;\r
195   } \r
196   \r
197   /* Check if ADC peripheral is disabled in order to enable it and wait during \r
198      Tstab time the ADC's stabilization */\r
199   if((hadc->Instance->CR2 & ADC_CR2_ADON) != ADC_CR2_ADON)\r
200   {  \r
201     /* Enable the Peripheral */\r
202     __HAL_ADC_ENABLE(hadc);\r
203     \r
204     /* Delay for temperature sensor stabilization time */\r
205     /* Compute number of CPU cycles to wait for */\r
206     counter = (ADC_STAB_DELAY_US * (SystemCoreClock / 1000000));\r
207     while(counter != 0)\r
208     {\r
209       counter--;\r
210     }\r
211   }\r
212   \r
213   /* Check if Multimode enabled */\r
214   if(HAL_IS_BIT_CLR(ADC->CCR, ADC_CCR_MULTI))\r
215   {\r
216     tmp1 = HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_JEXTEN);\r
217     tmp2 = HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO);\r
218     if(tmp1 && tmp2)\r
219     {\r
220       /* Enable the selected ADC software conversion for injected group */\r
221       hadc->Instance->CR2 |= ADC_CR2_JSWSTART;\r
222     }\r
223   }\r
224   else\r
225   {\r
226     tmp1 = HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_JEXTEN);\r
227     tmp2 = HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO);\r
228     if((hadc->Instance == ADC1) && tmp1 && tmp2)  \r
229     {\r
230       /* Enable the selected ADC software conversion for injected group */\r
231       hadc->Instance->CR2 |= ADC_CR2_JSWSTART;\r
232     }\r
233   }\r
234   \r
235   /* Process unlocked */\r
236   __HAL_UNLOCK(hadc);\r
237   \r
238   /* Return function status */\r
239   return HAL_OK;\r
240 }\r
241 \r
242 /**\r
243   * @brief  Enables the interrupt and starts ADC conversion of injected channels.\r
244   * @param  hadc: pointer to a ADC_HandleTypeDef structure that contains\r
245   *         the configuration information for the specified ADC.\r
246   *\r
247   * @retval HAL status.\r
248   */\r
249 HAL_StatusTypeDef HAL_ADCEx_InjectedStart_IT(ADC_HandleTypeDef* hadc)\r
250 {\r
251   __IO uint32_t counter = 0;\r
252   uint32_t tmp1 = 0, tmp2 =0;\r
253   \r
254   /* Process locked */\r
255   __HAL_LOCK(hadc);\r
256   \r
257   /* Check if a regular conversion is ongoing */\r
258   if(hadc->State == HAL_ADC_STATE_BUSY_REG)\r
259   {\r
260     /* Change ADC state */\r
261     hadc->State = HAL_ADC_STATE_BUSY_INJ_REG;  \r
262   }\r
263   else\r
264   {\r
265     /* Change ADC state */\r
266     hadc->State = HAL_ADC_STATE_BUSY_INJ;\r
267   }\r
268   \r
269   /* Set ADC error code to none */\r
270   hadc->ErrorCode = HAL_ADC_ERROR_NONE;\r
271   \r
272   /* Check if ADC peripheral is disabled in order to enable it and wait during \r
273      Tstab time the ADC's stabilization */\r
274   if((hadc->Instance->CR2 & ADC_CR2_ADON) != ADC_CR2_ADON)\r
275   {  \r
276     /* Enable the Peripheral */\r
277     __HAL_ADC_ENABLE(hadc);\r
278     \r
279     /* Delay for temperature sensor stabilization time */\r
280     /* Compute number of CPU cycles to wait for */\r
281     counter = (ADC_STAB_DELAY_US * (SystemCoreClock / 1000000));\r
282     while(counter != 0)\r
283     {\r
284       counter--;\r
285     }\r
286   }\r
287   \r
288   /* Enable the ADC end of conversion interrupt for injected group */\r
289   __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOC);\r
290   \r
291   /* Enable the ADC overrun interrupt */\r
292   __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);\r
293   \r
294   /* Check if Multimode enabled */\r
295   if(HAL_IS_BIT_CLR(ADC->CCR, ADC_CCR_MULTI))\r
296   {\r
297     tmp1 = HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_JEXTEN);\r
298     tmp2 = HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO);\r
299     if(tmp1 && tmp2)\r
300     {\r
301       /* Enable the selected ADC software conversion for injected group */\r
302       hadc->Instance->CR2 |= ADC_CR2_JSWSTART;\r
303     }\r
304   }\r
305   else\r
306   {\r
307     tmp1 = HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_JEXTEN);\r
308     tmp2 = HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO);\r
309     if((hadc->Instance == ADC1) && tmp1 && tmp2)  \r
310     {\r
311       /* Enable the selected ADC software conversion for injected group */\r
312       hadc->Instance->CR2 |= ADC_CR2_JSWSTART;\r
313     }\r
314   }\r
315   \r
316   /* Process unlocked */\r
317   __HAL_UNLOCK(hadc);\r
318   \r
319   /* Return function status */\r
320   return HAL_OK;\r
321 }\r
322 \r
323 /**\r
324   * @brief  Disables ADC and stop conversion of injected channels.\r
325   *\r
326   * @note   Caution: This function will stop also regular channels.  \r
327   *\r
328   * @param  hadc: pointer to a ADC_HandleTypeDef structure that contains\r
329   *         the configuration information for the specified ADC.\r
330   * @retval HAL status.\r
331   */\r
332 HAL_StatusTypeDef HAL_ADCEx_InjectedStop(ADC_HandleTypeDef* hadc)\r
333 {\r
334   /* Disable the Peripheral */\r
335   __HAL_ADC_DISABLE(hadc);\r
336   \r
337   /* Change ADC state */\r
338   hadc->State = HAL_ADC_STATE_READY;\r
339   \r
340   /* Return function status */\r
341   return HAL_OK;\r
342 }\r
343 \r
344 /**\r
345   * @brief  Poll for injected conversion complete\r
346   * @param  hadc: pointer to a ADC_HandleTypeDef structure that contains\r
347   *         the configuration information for the specified ADC.\r
348   * @param  Timeout: Timeout value in millisecond.  \r
349   * @retval HAL status\r
350   */\r
351 HAL_StatusTypeDef HAL_ADCEx_InjectedPollForConversion(ADC_HandleTypeDef* hadc, uint32_t Timeout)\r
352 {\r
353   uint32_t tickstart = 0;\r
354 \r
355   /* Get tick */ \r
356   tickstart = HAL_GetTick();\r
357 \r
358   /* Check End of conversion flag */\r
359   while(!(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_JEOC)))\r
360   {\r
361     /* Check for the Timeout */\r
362     if(Timeout != HAL_MAX_DELAY)\r
363     {\r
364       if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))\r
365       {\r
366         hadc->State= HAL_ADC_STATE_TIMEOUT;\r
367         /* Process unlocked */\r
368         __HAL_UNLOCK(hadc);\r
369         return HAL_TIMEOUT;\r
370       }\r
371     }\r
372   }\r
373   \r
374   /* Check if a regular conversion is ready */\r
375   if(hadc->State == HAL_ADC_STATE_EOC_REG)\r
376   {\r
377     /* Change ADC state */\r
378     hadc->State = HAL_ADC_STATE_EOC_INJ_REG;  \r
379   }\r
380   else\r
381   {\r
382     /* Change ADC state */\r
383     hadc->State = HAL_ADC_STATE_EOC_INJ;\r
384   }\r
385   \r
386   /* Return ADC state */\r
387   return HAL_OK;\r
388 }      \r
389   \r
390 /**\r
391   * @brief  Disables the interrupt and stop ADC conversion of injected channels.\r
392   * \r
393   * @note   Caution: This function will stop also regular channels.  \r
394   *\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_ADCEx_InjectedStop_IT(ADC_HandleTypeDef* hadc)\r
400 {\r
401   /* Disable the ADC end of conversion interrupt for regular group */\r
402   __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC);\r
403   \r
404   /* Disable the ADC end of conversion interrupt for injected group */\r
405   __HAL_ADC_DISABLE_IT(hadc, ADC_CR1_JEOCIE);\r
406   \r
407   /* Enable the Peripheral */\r
408   __HAL_ADC_DISABLE(hadc);\r
409   \r
410   /* Change ADC state */\r
411   hadc->State = HAL_ADC_STATE_READY;\r
412   \r
413   /* Return function status */\r
414   return HAL_OK;\r
415 }\r
416 \r
417 /**\r
418   * @brief  Gets the converted value from data register of injected channel.\r
419   * @param  hadc: pointer to a ADC_HandleTypeDef structure that contains\r
420   *         the configuration information for the specified ADC.\r
421   * @param  InjectedRank: the ADC injected rank.\r
422   *          This parameter can be one of the following values:\r
423   *            @arg ADC_INJECTED_RANK_1: Injected Channel1 selected\r
424   *            @arg ADC_INJECTED_RANK_2: Injected Channel2 selected\r
425   *            @arg ADC_INJECTED_RANK_3: Injected Channel3 selected\r
426   *            @arg ADC_INJECTED_RANK_4: Injected Channel4 selected\r
427   * @retval None\r
428   */\r
429 uint32_t HAL_ADCEx_InjectedGetValue(ADC_HandleTypeDef* hadc, uint32_t InjectedRank)\r
430 {\r
431   __IO uint32_t tmp = 0;\r
432   \r
433   /* Check the parameters */\r
434   assert_param(IS_ADC_INJECTED_RANK(InjectedRank));\r
435   \r
436    /* Clear the ADCx's flag for injected end of conversion */\r
437    __HAL_ADC_CLEAR_FLAG(hadc,ADC_FLAG_JEOC);\r
438   \r
439   /* Return the selected ADC converted value */ \r
440   switch(InjectedRank)\r
441   {  \r
442     case ADC_INJECTED_RANK_4:\r
443     {\r
444       tmp =  hadc->Instance->JDR4;\r
445     }  \r
446     break;\r
447     case ADC_INJECTED_RANK_3: \r
448     {  \r
449       tmp =  hadc->Instance->JDR3;\r
450     }  \r
451     break;\r
452     case ADC_INJECTED_RANK_2: \r
453     {  \r
454       tmp =  hadc->Instance->JDR2;\r
455     }\r
456     break;\r
457     case ADC_INJECTED_RANK_1:\r
458     {\r
459       tmp =  hadc->Instance->JDR1;\r
460     }\r
461     break;\r
462     default:\r
463     break;  \r
464   }\r
465   return tmp;\r
466 }\r
467 \r
468 /**\r
469   * @brief  Enables ADC DMA request after last transfer (Multi-ADC mode) and enables ADC peripheral\r
470   * \r
471   * @note   Caution: This function must be used only with the ADC master.  \r
472   *\r
473   * @param  hadc: pointer to a ADC_HandleTypeDef structure that contains\r
474   *         the configuration information for the specified ADC.\r
475   * @param  pData:   Pointer to buffer in which transferred from ADC peripheral to memory will be stored. \r
476   * @param  Length:  The length of data to be transferred from ADC peripheral to memory.  \r
477   * @retval HAL status\r
478   */\r
479 HAL_StatusTypeDef HAL_ADCEx_MultiModeStart_DMA(ADC_HandleTypeDef* hadc, uint32_t* pData, uint32_t Length)\r
480 {\r
481   __IO uint32_t counter = 0;\r
482   \r
483   /* Check the parameters */\r
484   assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));\r
485   assert_param(IS_ADC_EXT_TRIG_EDGE(hadc->Init.ExternalTrigConvEdge));\r
486   assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DMAContinuousRequests));\r
487   \r
488   /* Process locked */\r
489   __HAL_LOCK(hadc);\r
490   \r
491   /* Enable ADC overrun interrupt */\r
492   __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);\r
493   \r
494   if (hadc->Init.DMAContinuousRequests != DISABLE)\r
495   {\r
496     /* Enable the selected ADC DMA request after last transfer */\r
497     ADC->CCR |= ADC_CCR_DDS;\r
498   }\r
499   else\r
500   {\r
501     /* Disable the selected ADC EOC rising on each regular channel conversion */\r
502     ADC->CCR &= ~ADC_CCR_DDS;\r
503   }\r
504   \r
505   /* Set the DMA transfer complete callback */\r
506   hadc->DMA_Handle->XferCpltCallback = ADC_MultiModeDMAConvCplt;\r
507   \r
508   /* Set the DMA half transfer complete callback */\r
509   hadc->DMA_Handle->XferHalfCpltCallback = ADC_MultiModeDMAHalfConvCplt;\r
510      \r
511   /* Set the DMA error callback */\r
512   hadc->DMA_Handle->XferErrorCallback = ADC_MultiModeDMAError ;\r
513   \r
514   /* Enable the DMA Stream */\r
515   HAL_DMA_Start_IT(hadc->DMA_Handle, (uint32_t)&ADC->CDR, (uint32_t)pData, Length);\r
516   \r
517   /* Change ADC state */\r
518   hadc->State = HAL_ADC_STATE_BUSY_REG;\r
519   \r
520   /* Check if ADC peripheral is disabled in order to enable it and wait during \r
521      Tstab time the ADC's stabilization */\r
522   if((hadc->Instance->CR2 & ADC_CR2_ADON) != ADC_CR2_ADON)\r
523   {  \r
524     /* Enable the Peripheral */\r
525     __HAL_ADC_ENABLE(hadc);\r
526     \r
527     /* Delay for temperature sensor stabilization time */\r
528     /* Compute number of CPU cycles to wait for */\r
529     counter = (ADC_STAB_DELAY_US * (SystemCoreClock / 1000000));\r
530     while(counter != 0)\r
531     {\r
532       counter--;\r
533     }\r
534   }\r
535   \r
536   /* if no external trigger present enable software conversion of regular channels */\r
537   if((hadc->Instance->CR2 & ADC_CR2_EXTEN) == RESET)\r
538   {\r
539     /* Enable the selected ADC software conversion for regular group */\r
540     hadc->Instance->CR2 |= (uint32_t)ADC_CR2_SWSTART;\r
541   }\r
542   \r
543   /* Process unlocked */\r
544   __HAL_UNLOCK(hadc);\r
545   \r
546   /* Return function status */\r
547   return HAL_OK;\r
548 }\r
549 \r
550 /**\r
551   * @brief  Disables ADC DMA (multi-ADC mode) and disables ADC peripheral    \r
552   * @param  hadc: pointer to a ADC_HandleTypeDef structure that contains\r
553   *         the configuration information for the specified ADC.\r
554   * @retval HAL status\r
555   */\r
556 HAL_StatusTypeDef HAL_ADCEx_MultiModeStop_DMA(ADC_HandleTypeDef* hadc)\r
557 {\r
558   /* Process locked */\r
559   __HAL_LOCK(hadc);\r
560   \r
561   /* Enable the Peripheral */\r
562   __HAL_ADC_DISABLE(hadc);\r
563   \r
564   /* Disable ADC overrun interrupt */\r
565   __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);\r
566   \r
567   /* Disable the selected ADC DMA request after last transfer */\r
568   ADC->CCR &= ~ADC_CCR_DDS;\r
569   \r
570   /* Disable the ADC DMA Stream */\r
571   HAL_DMA_Abort(hadc->DMA_Handle);\r
572   \r
573   /* Change ADC state */\r
574   hadc->State = HAL_ADC_STATE_READY;\r
575   \r
576   /* Process unlocked */\r
577   __HAL_UNLOCK(hadc);\r
578     \r
579   /* Return function status */\r
580   return HAL_OK;\r
581 }\r
582 \r
583 /**\r
584   * @brief  Returns the last ADC1, ADC2 and ADC3 regular conversions results \r
585   *         data in the selected multi mode.\r
586   * @param  hadc: pointer to a ADC_HandleTypeDef structure that contains\r
587   *         the configuration information for the specified ADC.\r
588   * @retval The converted data value.\r
589   */\r
590 uint32_t HAL_ADCEx_MultiModeGetValue(ADC_HandleTypeDef* hadc)\r
591 {\r
592   /* Return the multi mode conversion value */\r
593   return ADC->CDR;\r
594 }\r
595 \r
596 /**\r
597   * @brief  Injected conversion complete callback in non blocking mode \r
598   * @param  hadc: pointer to a ADC_HandleTypeDef structure that contains\r
599   *         the configuration information for the specified ADC.\r
600   * @retval None\r
601   */\r
602 __weak void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef* hadc)\r
603 {\r
604   /* NOTE : This function Should not be modified, when the callback is needed,\r
605             the HAL_ADC_InjectedConvCpltCallback could be implemented in the user file\r
606    */\r
607 }\r
608 \r
609 /**\r
610   * @brief  Configures for the selected ADC injected channel its corresponding\r
611   *         rank in the sequencer and its sample time.\r
612   * @param  hadc: pointer to a ADC_HandleTypeDef structure that contains\r
613   *         the configuration information for the specified ADC.\r
614   * @param  sConfigInjected: ADC configuration structure for injected channel. \r
615   * @retval None\r
616   */\r
617 HAL_StatusTypeDef HAL_ADCEx_InjectedConfigChannel(ADC_HandleTypeDef* hadc, ADC_InjectionConfTypeDef* sConfigInjected)\r
618 {\r
619 \r
620 #ifdef USE_FULL_ASSERT  \r
621   uint32_t tmp = 0;\r
622 #endif /* USE_FULL_ASSERT  */\r
623   \r
624   /* Check the parameters */\r
625   assert_param(IS_ADC_CHANNEL(sConfigInjected->InjectedChannel));\r
626   assert_param(IS_ADC_INJECTED_RANK(sConfigInjected->InjectedRank));\r
627   assert_param(IS_ADC_SAMPLE_TIME(sConfigInjected->InjectedSamplingTime));\r
628   assert_param(IS_ADC_EXT_INJEC_TRIG(sConfigInjected->ExternalTrigInjecConv));\r
629   assert_param(IS_ADC_EXT_INJEC_TRIG_EDGE(sConfigInjected->ExternalTrigInjecConvEdge));\r
630   assert_param(IS_ADC_INJECTED_LENGTH(sConfigInjected->InjectedNbrOfConversion));\r
631   assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->AutoInjectedConv));\r
632   assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->InjectedDiscontinuousConvMode));\r
633 \r
634 #ifdef USE_FULL_ASSERT\r
635   tmp = ADC_GET_RESOLUTION(hadc);\r
636   assert_param(IS_ADC_RANGE(tmp, sConfigInjected->InjectedOffset));\r
637 #endif /* USE_FULL_ASSERT  */\r
638 \r
639   /* Process locked */\r
640   __HAL_LOCK(hadc);\r
641   \r
642   /* if ADC_Channel_10 ... ADC_Channel_18 is selected */\r
643   if (sConfigInjected->InjectedChannel > ADC_CHANNEL_9)\r
644   {\r
645     /* Clear the old sample time */\r
646     hadc->Instance->SMPR1 &= ~ADC_SMPR1(ADC_SMPR1_SMP10, sConfigInjected->InjectedChannel);\r
647     \r
648     /* Set the new sample time */\r
649     hadc->Instance->SMPR1 |= ADC_SMPR1(sConfigInjected->InjectedSamplingTime, sConfigInjected->InjectedChannel);\r
650   }\r
651   else /* ADC_Channel include in ADC_Channel_[0..9] */\r
652   {\r
653     /* Clear the old sample time */\r
654     hadc->Instance->SMPR2 &= ~ADC_SMPR2(ADC_SMPR2_SMP0, sConfigInjected->InjectedChannel);\r
655     \r
656     /* Set the new sample time */\r
657     hadc->Instance->SMPR2 |= ADC_SMPR2(sConfigInjected->InjectedSamplingTime, sConfigInjected->InjectedChannel);\r
658   }\r
659   \r
660   /*---------------------------- ADCx JSQR Configuration -----------------*/\r
661   hadc->Instance->JSQR &= ~(ADC_JSQR_JL);\r
662   hadc->Instance->JSQR |=  ADC_SQR1(sConfigInjected->InjectedNbrOfConversion);\r
663   \r
664   /* Rank configuration */\r
665   \r
666   /* Clear the old SQx bits for the selected rank */\r
667   hadc->Instance->JSQR &= ~ADC_JSQR(ADC_JSQR_JSQ1, sConfigInjected->InjectedRank,sConfigInjected->InjectedNbrOfConversion);\r
668    \r
669   /* Set the SQx bits for the selected rank */\r
670   hadc->Instance->JSQR |= ADC_JSQR(sConfigInjected->InjectedChannel, sConfigInjected->InjectedRank,sConfigInjected->InjectedNbrOfConversion);\r
671 \r
672   /* Select external trigger to start conversion */\r
673   hadc->Instance->CR2 &= ~(ADC_CR2_JEXTSEL);\r
674   hadc->Instance->CR2 |=  sConfigInjected->ExternalTrigInjecConv;\r
675   \r
676   /* Select external trigger polarity */\r
677   hadc->Instance->CR2 &= ~(ADC_CR2_JEXTEN);\r
678   hadc->Instance->CR2 |= sConfigInjected->ExternalTrigInjecConvEdge;\r
679   \r
680   if (sConfigInjected->AutoInjectedConv != DISABLE)\r
681   {\r
682     /* Enable the selected ADC automatic injected group conversion */\r
683     hadc->Instance->CR1 |= ADC_CR1_JAUTO;\r
684   }\r
685   else\r
686   {\r
687     /* Disable the selected ADC automatic injected group conversion */\r
688     hadc->Instance->CR1 &= ~(ADC_CR1_JAUTO);\r
689   }\r
690   \r
691   if (sConfigInjected->InjectedDiscontinuousConvMode != DISABLE)\r
692   {\r
693     /* Enable the selected ADC injected discontinuous mode */\r
694     hadc->Instance->CR1 |= ADC_CR1_JDISCEN;\r
695   }\r
696   else\r
697   {\r
698     /* Disable the selected ADC injected discontinuous mode */\r
699     hadc->Instance->CR1 &= ~(ADC_CR1_JDISCEN);\r
700   }\r
701   \r
702   switch(sConfigInjected->InjectedRank)\r
703   {\r
704     case 1:\r
705       /* Set injected channel 1 offset */\r
706       hadc->Instance->JOFR1 &= ~(ADC_JOFR1_JOFFSET1);\r
707       hadc->Instance->JOFR1 |= sConfigInjected->InjectedOffset;\r
708       break;\r
709     case 2:\r
710       /* Set injected channel 2 offset */\r
711       hadc->Instance->JOFR2 &= ~(ADC_JOFR2_JOFFSET2);\r
712       hadc->Instance->JOFR2 |= sConfigInjected->InjectedOffset;\r
713       break;\r
714     case 3:\r
715       /* Set injected channel 3 offset */\r
716       hadc->Instance->JOFR3 &= ~(ADC_JOFR3_JOFFSET3);\r
717       hadc->Instance->JOFR3 |= sConfigInjected->InjectedOffset;\r
718       break;\r
719     default:\r
720       /* Set injected channel 4 offset */\r
721       hadc->Instance->JOFR4 &= ~(ADC_JOFR4_JOFFSET4);\r
722       hadc->Instance->JOFR4 |= sConfigInjected->InjectedOffset;\r
723       break;\r
724   }\r
725   \r
726   /* if ADC1 Channel_18 is selected enable VBAT Channel */\r
727   if ((hadc->Instance == ADC1) && (sConfigInjected->InjectedChannel == ADC_CHANNEL_VBAT))\r
728   {\r
729     /* Enable the VBAT channel*/\r
730     ADC->CCR |= ADC_CCR_VBATE;\r
731   }\r
732   \r
733   /* if ADC1 Channel_16 or Channel_17 is selected enable TSVREFE Channel(Temperature sensor and VREFINT) */\r
734   if ((hadc->Instance == ADC1) && ((sConfigInjected->InjectedChannel == ADC_CHANNEL_TEMPSENSOR) || (sConfigInjected->InjectedChannel == ADC_CHANNEL_VREFINT)))\r
735   {\r
736     /* Enable the TSVREFE channel*/\r
737     ADC->CCR |= ADC_CCR_TSVREFE;\r
738   }\r
739   \r
740   /* Process unlocked */\r
741   __HAL_UNLOCK(hadc);\r
742   \r
743   /* Return function status */\r
744   return HAL_OK;\r
745 }\r
746 \r
747 /**\r
748   * @brief  Configures the ADC multi-mode \r
749   * @param  hadc      : pointer to a ADC_HandleTypeDef structure that contains\r
750   *                     the configuration information for the specified ADC.  \r
751   * @param  multimode : pointer to an ADC_MultiModeTypeDef structure that contains \r
752   *                     the configuration information for  multimode.\r
753   * @retval HAL status\r
754   */\r
755 HAL_StatusTypeDef HAL_ADCEx_MultiModeConfigChannel(ADC_HandleTypeDef* hadc, ADC_MultiModeTypeDef* multimode)\r
756 {\r
757   /* Check the parameters */\r
758   assert_param(IS_ADC_MODE(multimode->Mode));\r
759   assert_param(IS_ADC_DMA_ACCESS_MODE(multimode->DMAAccessMode));\r
760   assert_param(IS_ADC_SAMPLING_DELAY(multimode->TwoSamplingDelay));\r
761   \r
762   /* Process locked */\r
763   __HAL_LOCK(hadc);\r
764   \r
765   /* Set ADC mode */\r
766   ADC->CCR &= ~(ADC_CCR_MULTI);\r
767   ADC->CCR |= multimode->Mode;\r
768   \r
769   /* Set the ADC DMA access mode */\r
770   ADC->CCR &= ~(ADC_CCR_DMA);\r
771   ADC->CCR |= multimode->DMAAccessMode;\r
772   \r
773   /* Set delay between two sampling phases */\r
774   ADC->CCR &= ~(ADC_CCR_DELAY);\r
775   ADC->CCR |= multimode->TwoSamplingDelay;\r
776   \r
777   /* Process unlocked */\r
778   __HAL_UNLOCK(hadc);\r
779   \r
780   /* Return function status */\r
781   return HAL_OK;\r
782 }\r
783 \r
784 /**\r
785   * @}\r
786   */\r
787 \r
788   /**\r
789   * @brief  DMA transfer complete callback. \r
790   * @param  hdma: pointer to a DMA_HandleTypeDef structure that contains\r
791   *                the configuration information for the specified DMA module.\r
792   * @retval None\r
793   */\r
794 static void ADC_MultiModeDMAConvCplt(DMA_HandleTypeDef *hdma)   \r
795 {\r
796     ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;\r
797     \r
798   /* Check if an injected conversion is ready */\r
799   if(hadc->State == HAL_ADC_STATE_EOC_INJ)\r
800   {\r
801     /* Change ADC state */\r
802     hadc->State = HAL_ADC_STATE_EOC_INJ_REG;  \r
803   }\r
804   else\r
805   {\r
806     /* Change ADC state */\r
807     hadc->State = HAL_ADC_STATE_EOC_REG;\r
808   }\r
809     \r
810     HAL_ADC_ConvCpltCallback(hadc); \r
811 }\r
812 \r
813 /**\r
814   * @brief  DMA half transfer complete callback. \r
815   * @param  hdma: pointer to a DMA_HandleTypeDef structure that contains\r
816   *                the configuration information for the specified DMA module.\r
817   * @retval None\r
818   */\r
819 static void ADC_MultiModeDMAHalfConvCplt(DMA_HandleTypeDef *hdma)   \r
820 {\r
821     ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;\r
822     /* Conversion complete callback */\r
823     HAL_ADC_ConvHalfCpltCallback(hadc); \r
824 }\r
825 \r
826 /**\r
827   * @brief  DMA error callback \r
828   * @param  hdma: pointer to a DMA_HandleTypeDef structure that contains\r
829   *                the configuration information for the specified DMA module.\r
830   * @retval None\r
831   */\r
832 static void ADC_MultiModeDMAError(DMA_HandleTypeDef *hdma)   \r
833 {\r
834     ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;\r
835     hadc->State= HAL_ADC_STATE_ERROR;\r
836     /* Set ADC error code to DMA error */\r
837     hadc->ErrorCode |= HAL_ADC_ERROR_DMA;\r
838     HAL_ADC_ErrorCallback(hadc); \r
839 }\r
840 \r
841 /**\r
842   * @}\r
843   */\r
844 \r
845 #endif /* HAL_ADC_MODULE_ENABLED */\r
846 /**\r
847   * @}\r
848   */ \r
849 \r
850 /**\r
851   * @}\r
852   */ \r
853 \r
854 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/\r