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