]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32L1/stm32l1xx_hal_adc_ex.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32L1 / stm32l1xx_hal_adc_ex.c
1 /**
2   ******************************************************************************
3   * @file    stm32l1xx_hal_adc_ex.c
4   * @author  MCD Application Team
5   * @version V1.0.0
6   * @date    5-September-2014
7   * @brief   This file provides firmware functions to manage the following 
8   *          functionalities of the Analog to Digital Convertor (ADC)
9   *          peripheral:
10   *           + Initialization and de-initialization functions
11   *             ++ Initialization and Configuration of ADC
12   *           + Operation functions
13   *             ++ Start, stop, get result of conversions of regular
14   *             group, using 3 possible modes: polling, interruption or DMA.
15   *           + Control functions
16   *             ++ Analog Watchdog configuration
17   *             ++ Channels configuration on regular group
18   *           + State functions
19   *             ++ ADC state machine management
20   *             ++ Interrupts and flags management
21   *          Other functions (generic functions) are available in file 
22   *          "stm32l1xx_hal_adc.c".
23   *
24   @verbatim
25   ==============================================================================
26                      ##### How to use this driver #####
27   ==============================================================================
28     [..]
29
30      (#) Activate the ADC peripheral using one of the start functions: 
31          HAL_ADCEx_InjectedStart(), HAL_ADCEx_InjectedStart_IT().
32     
33      *** Channels configuration to injected group ***
34      ================================================    
35      [..]
36        (+) To configure the ADC Injected channels group features, use 
37            HAL_ADCEx_InjectedConfigChannel() functions.
38        (+) To activate the continuous mode, use the HAL_ADC_Init() function.
39        (+) To read the ADC converted values, use the HAL_ADCEx_InjectedGetValue() 
40            function.
41   
42     @endverbatim
43   ******************************************************************************
44   * @attention
45   *
46   * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
47   *
48   * Redistribution and use in source and binary forms, with or without modification,
49   * are permitted provided that the following conditions are met:
50   *   1. Redistributions of source code must retain the above copyright notice,
51   *      this list of conditions and the following disclaimer.
52   *   2. Redistributions in binary form must reproduce the above copyright notice,
53   *      this list of conditions and the following disclaimer in the documentation
54   *      and/or other materials provided with the distribution.
55   *   3. Neither the name of STMicroelectronics nor the names of its contributors
56   *      may be used to endorse or promote products derived from this software
57   *      without specific prior written permission.
58   *
59   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
60   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
62   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
63   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
65   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
66   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
67   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
68   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
69   *
70   ******************************************************************************  
71   */
72
73 /* Includes ------------------------------------------------------------------*/
74 #include "stm32l1xx_hal.h"
75
76 /** @addtogroup STM32L1xx_HAL_Driver
77   * @{
78   */
79
80 /** @defgroup ADCEx ADCEx
81   * @brief ADC HAL module driver
82   * @{
83   */
84
85 #ifdef HAL_ADC_MODULE_ENABLED
86
87 /* Private typedef -----------------------------------------------------------*/
88 /* Private define ------------------------------------------------------------*/
89 /** @defgroup ADCEx_Private_Constants ADCEx Private Constants
90   * @{
91   */
92
93   /* ADC conversion cycles (unit: ADC clock cycles)                           */
94   /* (selected sampling time + conversion time of 12 ADC clock cycles, with   */
95   /* resolution 12 bits)                                                      */
96   #define ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_4CYCLE5   ((uint32_t) 16)
97   #define ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_9CYCLES   ((uint32_t) 21)
98   #define ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_16CYCLES  ((uint32_t) 28)
99   #define ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_24CYCLES  ((uint32_t) 36)
100   #define ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_48CYCLES  ((uint32_t) 60)
101   #define ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_96CYCLES  ((uint32_t)108)
102   #define ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_192CYCLES ((uint32_t)204)
103   #define ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_384CYCLES ((uint32_t)396)
104 /**
105   * @}
106   */
107
108 /* Private macro -------------------------------------------------------------*/
109 /* Private variables ---------------------------------------------------------*/
110 /* Private function prototypes -----------------------------------------------*/
111 /* Private functions ---------------------------------------------------------*/
112
113 /** @defgroup ADCEx_Exported_Functions ADCEx Exported Functions
114   * @{
115   */
116
117 /** @defgroup ADCEx_Exported_Functions_Group1 Extended Initialization/de-initialization functions 
118  *  @brief    Extended Initialization and Configuration functions 
119  *
120 @verbatim    
121  ===============================================================================
122                       ##### IO operation functions #####
123  ===============================================================================
124     [..]  This section provides functions allowing to:
125       (+) Start conversion of injected group.
126       (+) Stop conversion of injected group.
127       (+) Poll for conversion complete on injected group.
128       (+) Get result of injected channel conversion.
129       (+) Start conversion of injected group and enable interruptions.
130       (+) Stop conversion of injected group and disable interruptions.
131       
132 @endverbatim
133   * @{
134   */
135
136 /**
137   * @brief  Enables ADC, starts conversion of injected group.
138   *         Interruptions enabled in this function: None.
139   * @param  hadc: ADC handle
140   * @retval HAL status
141   */
142 HAL_StatusTypeDef HAL_ADCEx_InjectedStart(ADC_HandleTypeDef* hadc)
143 {
144   HAL_StatusTypeDef tmpHALStatus = HAL_OK;
145   
146   /* Check the parameters */
147   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
148   
149   /* Process locked */
150   __HAL_LOCK(hadc);
151     
152   /* Enable the ADC peripheral */
153   tmpHALStatus = ADC_Enable(hadc);
154   
155   /* Start conversion if ADC is effectively enabled */
156   if (tmpHALStatus != HAL_ERROR)
157   {
158     /* Check if a regular conversion is ongoing */
159     if(hadc->State == HAL_ADC_STATE_BUSY_REG)
160     {
161       /* Change ADC state */
162       hadc->State = HAL_ADC_STATE_BUSY_INJ_REG;
163     }
164     else
165     {
166       /* Change ADC state */
167       hadc->State = HAL_ADC_STATE_BUSY_INJ;
168     }
169     
170     /* Set ADC error code to none */
171     __HAL_ADC_CLEAR_ERRORCODE(hadc);
172     
173     /* Clear injected group conversion flag */
174     /* (To ensure of no unknown state from potential previous ADC operations) */
175     __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOC);
176     
177     /* Start conversion of injected group if software start has been selected */
178     /* and if automatic injected conversion is disabled.                      */
179     /* If external trigger has been selected, conversion will start at next   */
180     /* trigger event.                                                         */
181     /* If automatic injected conversion is enabled, conversion will start     */
182     /* after next regular group conversion.                                   */
183     if (__HAL_ADC_IS_SOFTWARE_START_INJECTED(hadc)        && 
184         HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO)  )
185     {
186       /* Enable ADC software conversion for injected channels */
187       SET_BIT(hadc->Instance->CR2, ADC_CR2_JSWSTART);
188     }
189   }
190
191   /* Process unlocked */
192   __HAL_UNLOCK(hadc);
193   
194   /* Return function status */
195   return tmpHALStatus;
196 }
197
198 /**
199   * @brief  Stop conversion of injected channels. Disable ADC peripheral if
200   *         no regular conversion is on going.
201   * @note   If ADC must be disabled with this function and if regular conversion
202   *         is on going, function HAL_ADC_Stop must be used preliminarily.
203   * @note   In case of auto-injection mode, HAL_ADC_Stop must be used.
204   * @param  hadc: ADC handle
205   * @retval None
206   */
207 HAL_StatusTypeDef HAL_ADCEx_InjectedStop(ADC_HandleTypeDef* hadc)
208 {
209   HAL_StatusTypeDef tmpHALStatus = HAL_OK;
210   
211   /* Check the parameters */
212   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
213
214   /* Process locked */
215   __HAL_LOCK(hadc);
216     
217   /* Stop potential conversion and disable ADC peripheral                     */
218   /* Conditioned to:                                                          */
219   /* - No conversion on the other group (regular group) is intended to        */
220   /*   continue (injected and regular groups stop conversion and ADC disable  */
221   /*   are common)                                                            */
222   /* - In case of auto-injection mode, HAL_ADC_Stop must be used.             */
223     if((hadc->State != HAL_ADC_STATE_BUSY_REG)            &&
224        (hadc->State != HAL_ADC_STATE_BUSY_INJ_REG)        &&
225        HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO)   )
226   {
227     /* Stop potential conversion on going, on regular and injected groups */
228     /* Disable ADC peripheral */
229     tmpHALStatus = ADC_ConversionStop_Disable(hadc);
230     
231     /* Check if ADC is effectively disabled */
232     if (tmpHALStatus != HAL_ERROR)
233     {
234       /* Change ADC state */
235       hadc->State = HAL_ADC_STATE_READY;
236     }
237   }
238   else
239   {
240     /* Update ADC state machine to error */
241     hadc->State = HAL_ADC_STATE_ERROR;
242       
243     tmpHALStatus = HAL_ERROR;
244   }
245   
246   /* Process unlocked */
247   __HAL_UNLOCK(hadc);
248   
249   /* Return function status */
250   return tmpHALStatus;
251 }
252
253 /**
254   * @brief  Wait for injected group conversion to be completed.
255   * @param  hadc: ADC handle
256   * @param  Timeout: Timeout value in millisecond.
257   * @retval HAL status
258   */
259 HAL_StatusTypeDef HAL_ADCEx_InjectedPollForConversion(ADC_HandleTypeDef* hadc, uint32_t Timeout)
260 {
261   uint32_t tickstart;
262
263   /* Variables for polling in case of scan mode enabled and polling for each  */
264   /* conversion.                                                              */
265   /* Note: Variable "conversion_timeout_cpu_cycles" set to offset 28 CPU      */
266   /* cycles to compensate number of CPU cycles for processing of variable     */
267   /* "conversion_timeout_cpu_cycles_max"                                      */
268   uint32_t conversion_timeout_cpu_cycles = 28;
269   uint32_t conversion_timeout_cpu_cycles_max = 0;
270   
271   /* Check the parameters */
272   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
273
274   /* Get timeout */
275   tickstart = HAL_GetTick();  
276      
277   /* Polling for end of conversion: differentiation if single/sequence        */
278   /* conversion.                                                              */
279   /* For injected group, flag JEOC is set only at the end of the sequence,    */
280   /* not for each conversion within the sequence.                             */
281   /* If setting "EOCSelection" is set to poll for each single conversion,     */
282   /* management of polling depends on setting of injected group sequencer:    */ 
283   /*  - If single conversion for injected group (scan mode disabled or        */
284   /*    InjectedNbrOfConversion ==1), flag JEOC is used to determine the      */
285   /*    conversion completion.                                                */
286   /*  - If sequence conversion for injected group (scan mode enabled and      */
287   /*    InjectedNbrOfConversion >=2), flag JEOC is set only at the end of the */
288   /*    sequence.                                                             */
289   /*    To poll for each conversion, the maximum conversion time is computed  */
290   /*    from ADC conversion time (selected sampling time + conversion time of */
291   /*    12 ADC clock cycles) and APB2/ADC clock prescalers (depending on      */
292   /*    settings, conversion time range can vary from 8 to several thousands  */
293   /*    of CPU cycles).                                                       */
294
295   /* Note: On STM32L1, setting "EOCSelection" is related to regular group     */
296   /*       only, by hardware. For compatibility with other STM32 devices,     */
297   /*       this setting is related also to injected group by software.        */
298   if (((hadc->Instance->JSQR & ADC_JSQR_JL) == RESET) ||
299       (hadc->Init.EOCSelection != EOC_SINGLE_CONV)      )
300   {
301     /* Wait until End of Conversion flag is raised */
302     while(HAL_IS_BIT_CLR(hadc->Instance->SR, ADC_FLAG_JEOC))
303     {
304       /* Check if timeout is disabled (set to infinite wait) */
305       if(Timeout != HAL_MAX_DELAY)
306       {
307         if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
308         {
309           /* Update ADC state machine to timeout */
310           hadc->State = HAL_ADC_STATE_TIMEOUT;
311           
312           /* Process unlocked */
313           __HAL_UNLOCK(hadc);
314           
315           return HAL_ERROR;
316         }
317       }
318     }
319   }
320   else
321   {
322     /* Computation of CPU cycles corresponding to ADC conversion cycles.      */
323     /* Retrieve ADC clock prescaler and ADC maximum conversion cycles on all  */
324     /* channels.                                                              */
325     conversion_timeout_cpu_cycles_max = __ADC_GET_CLOCK_PRESCALER_DECIMAL(hadc);
326     conversion_timeout_cpu_cycles_max *= __ADC_CONVCYCLES_MAX_RANGE(hadc);
327
328     /* Poll with maximum conversion time */
329     while(conversion_timeout_cpu_cycles < conversion_timeout_cpu_cycles_max)
330     {
331       /* Check if timeout is disabled (set to infinite wait) */
332       if(Timeout != HAL_MAX_DELAY)
333       {
334         if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
335         {
336           /* Update ADC state machine to timeout */
337           hadc->State = HAL_ADC_STATE_TIMEOUT;
338
339           /* Process unlocked */
340           __HAL_UNLOCK(hadc);
341           
342           return HAL_ERROR;
343         }
344       }
345       conversion_timeout_cpu_cycles ++;
346     }
347   }
348   
349   /* Clear end of conversion flag of injected group if low power feature      */
350   /* "Auto Wait" is disabled, to not interfere with this feature until data   */
351   /* register is read using function HAL_ADCEx_InjectedGetValue().            */
352   if (hadc->Init.LowPowerAutoWait == DISABLE)
353   {
354     /* Clear injected group conversion flag */
355     __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JSTRT | ADC_FLAG_JEOC);
356   }
357   
358   /* Update state machine on conversion status if not in error state */
359   if(hadc->State != HAL_ADC_STATE_ERROR)
360   {
361     /* Update ADC state machine */
362     if(hadc->State != HAL_ADC_STATE_EOC_INJ_REG)
363     {
364
365       if(hadc->State == HAL_ADC_STATE_EOC_REG)
366       {
367         /* Change ADC state */
368         hadc->State = HAL_ADC_STATE_EOC_INJ_REG;  
369       }
370       else
371       {
372         /* Change ADC state */
373         hadc->State = HAL_ADC_STATE_EOC_INJ;
374       }
375     }
376   }
377       
378   /* Return ADC state */
379   return HAL_OK;
380 }
381
382 /**
383   * @brief  Enables ADC, starts conversion of injected group with interruption.
384   *         Interruptions enabled in this function: JEOC (end of conversion).
385   *         Each of these interruptions has its dedicated callback function.
386   * @param  hadc: ADC handle
387   * @retval HAL status.
388   */
389 HAL_StatusTypeDef HAL_ADCEx_InjectedStart_IT(ADC_HandleTypeDef* hadc)
390 {
391   HAL_StatusTypeDef tmpHALStatus = HAL_OK;
392   
393   /* Check the parameters */
394   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
395   
396   /* Process locked */
397   __HAL_LOCK(hadc);
398     
399   /* Enable the ADC peripheral */
400   tmpHALStatus = ADC_Enable(hadc);
401   
402   /* Start conversion if ADC is effectively enabled */
403   if (tmpHALStatus != HAL_ERROR)
404   {
405     /* Check if a regular conversion is ongoing */
406     if(hadc->State == HAL_ADC_STATE_BUSY_REG)
407     {
408       /* Change ADC state */
409       hadc->State = HAL_ADC_STATE_BUSY_INJ_REG;  
410     }
411     else
412     {
413       /* Change ADC state */
414       hadc->State = HAL_ADC_STATE_BUSY_INJ;
415     }
416     
417     /* Set ADC error code to none */
418     __HAL_ADC_CLEAR_ERRORCODE(hadc);
419     
420     /* Clear injected group conversion flag */
421     /* (To ensure of no unknown state from potential previous ADC operations) */
422     __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOC);
423     
424     /* Enable end of conversion interrupt for injected channels */
425     __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOC);
426     
427     /* Start conversion of injected group if software start has been selected */
428     /* and if automatic injected conversion is disabled.                      */
429     /* If external trigger has been selected, conversion will start at next   */
430     /* trigger event.                                                         */
431     /* If automatic injected conversion is enabled, conversion will start     */
432     /* after next regular group conversion.                                   */
433     if (__HAL_ADC_IS_SOFTWARE_START_INJECTED(hadc)        && 
434         HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO)  )
435     {
436       /* Enable ADC software conversion for injected channels */
437       SET_BIT(hadc->Instance->CR2, ADC_CR2_JSWSTART);
438     }
439   }
440
441   /* Process unlocked */
442   __HAL_UNLOCK(hadc);
443   
444   /* Return function status */
445   return tmpHALStatus;
446 }
447
448 /**
449   * @brief  Stop conversion of injected channels, disable interruption of 
450   *         end-of-conversion. Disable ADC peripheral if no regular conversion
451   *         is on going.
452   * @note   If ADC must be disabled with this function and if regular conversion
453   *         is on going, function HAL_ADC_Stop must be used preliminarily.
454   * @param  hadc: ADC handle
455   * @retval None
456   */
457 HAL_StatusTypeDef HAL_ADCEx_InjectedStop_IT(ADC_HandleTypeDef* hadc)
458 {
459   HAL_StatusTypeDef tmpHALStatus = HAL_OK;
460   
461   /* Check the parameters */
462   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
463
464   /* Process locked */
465   __HAL_LOCK(hadc);
466     
467   /* Stop potential conversion and disable ADC peripheral                     */
468   /* Conditioned to:                                                          */
469   /* - No conversion on the other group (regular group) is intended to        */
470   /*   continue (injected and regular groups stop conversion and ADC disable  */
471   /*   are common)                                                            */
472   /* - In case of auto-injection mode, HAL_ADC_Stop must be used.             */ 
473     if((hadc->State != HAL_ADC_STATE_BUSY_REG)            &&
474        (hadc->State != HAL_ADC_STATE_BUSY_INJ_REG)        &&
475        HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO)   )
476   {
477     /* Stop potential conversion on going, on regular and injected groups */
478     /* Disable ADC peripheral */
479     tmpHALStatus = ADC_ConversionStop_Disable(hadc);
480     
481     /* Check if ADC is effectively disabled */
482     if (tmpHALStatus != HAL_ERROR)
483     {
484       /* Disable ADC end of conversion interrupt for injected channels */
485       __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC);
486       
487       /* Change ADC state */
488       hadc->State = HAL_ADC_STATE_READY;
489     }
490   }
491   else
492   {
493     /* Update ADC state machine to error */
494     hadc->State = HAL_ADC_STATE_ERROR;
495       
496     tmpHALStatus = HAL_ERROR;
497   }
498   
499   /* Process unlocked */
500   __HAL_UNLOCK(hadc);
501   
502   /* Return function status */
503   return tmpHALStatus;
504 }
505
506 /**
507   * @brief  Get ADC injected group conversion result.
508   * @param  hadc: ADC handle
509   * @param  InjectedRank: the converted ADC injected rank.
510   *          This parameter can be one of the following values:
511   *            @arg ADC_INJECTED_RANK_1: Injected Channel1 selected
512   *            @arg ADC_INJECTED_RANK_2: Injected Channel2 selected
513   *            @arg ADC_INJECTED_RANK_3: Injected Channel3 selected
514   *            @arg ADC_INJECTED_RANK_4: Injected Channel4 selected
515   * @retval None
516   */
517 uint32_t HAL_ADCEx_InjectedGetValue(ADC_HandleTypeDef* hadc, uint32_t InjectedRank)
518 {
519   uint32_t tmp_jdr = 0;
520   
521   /* Check the parameters */
522   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
523   assert_param(IS_ADC_INJECTED_RANK(InjectedRank));
524    
525   /* Clear injected group conversion flag to have similar behaviour as         */
526   /* regular group: reading data register also clears end of conversion flag. */
527   __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOC);
528   
529   /* Get ADC converted value */ 
530   switch(InjectedRank)
531   {  
532     case ADC_INJECTED_RANK_4: 
533       tmp_jdr = hadc->Instance->JDR4;
534       break;
535     case ADC_INJECTED_RANK_3: 
536       tmp_jdr = hadc->Instance->JDR3;
537       break;
538     case ADC_INJECTED_RANK_2: 
539       tmp_jdr = hadc->Instance->JDR2;
540       break;
541     case ADC_INJECTED_RANK_1:
542     default:
543       tmp_jdr = hadc->Instance->JDR1;
544       break;
545   }
546   
547   /* Return ADC converted value */ 
548   return tmp_jdr;
549 }
550
551 /**
552   * @brief  Injected conversion complete callback in non blocking mode 
553   * @param  hadc: ADC handle
554   * @retval None
555   */
556 __weak void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef* hadc)
557 {
558   /* NOTE : This function Should not be modified, when the callback is needed,
559             the HAL_ADCEx_InjectedConvCpltCallback could be implemented in the user file
560   */
561 }
562
563 /**
564   * @}
565   */
566
567 /** @defgroup ADCEx_Exported_Functions_Group2 Extended Peripheral Control functions
568   * @brief    Extended Peripheral Control functions
569   *
570 @verbatim   
571  ===============================================================================
572              ##### Peripheral Control functions #####
573  ===============================================================================  
574     [..]  This section provides functions allowing to:
575       (+) Configure channels on injected group
576       
577 @endverbatim
578   * @{
579   */
580
581 /**
582   * @brief  Configures the ADC injected group and the selected channel to be
583   *         linked to the injected group.
584   * @note   Possibility to update parameters on the fly:
585   *         This function initializes injected group, following calls to this 
586   *         function can be used to reconfigure some parameters of structure
587   *         "ADC_InjectionConfTypeDef" on the fly, without reseting the ADC.
588   *         The setting of these parameters is conditioned to ADC state: 
589   *         this function must be called when ADC is not under conversion.
590   * @param  hadc: ADC handle
591   * @param  sConfigInjected: Structure of ADC injected group and ADC channel for
592   *         injected group.
593   * @retval None
594   */
595 HAL_StatusTypeDef HAL_ADCEx_InjectedConfigChannel(ADC_HandleTypeDef* hadc, ADC_InjectionConfTypeDef* sConfigInjected)
596 {   
597   HAL_StatusTypeDef tmpHALStatus = HAL_OK;
598   
599   /* Check the parameters */
600   assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
601   assert_param(IS_ADC_CHANNEL(sConfigInjected->InjectedChannel));
602   assert_param(IS_ADC_SAMPLE_TIME(sConfigInjected->InjectedSamplingTime));
603   assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->AutoInjectedConv));
604   assert_param(IS_ADC_EXTTRIGINJEC(sConfigInjected->ExternalTrigInjecConv));
605   assert_param(IS_ADC_RANGE(sConfigInjected->InjectedOffset));
606   
607   if(hadc->Init.ScanConvMode != ADC_SCAN_DISABLE)
608   {
609     assert_param(IS_ADC_INJECTED_RANK(sConfigInjected->InjectedRank));
610     assert_param(IS_ADC_INJECTED_NB_CONV(sConfigInjected->InjectedNbrOfConversion));
611     assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->InjectedDiscontinuousConvMode));
612   }
613   
614   if(sConfigInjected->ExternalTrigInjecConvEdge != ADC_INJECTED_SOFTWARE_START)
615   {
616     assert_param(IS_ADC_EXTTRIGINJEC_EDGE(sConfigInjected->ExternalTrigInjecConvEdge));
617   }
618   
619   /* Process locked */
620   __HAL_LOCK(hadc);
621   
622   /* Configuration of injected group sequencer:                               */
623   /* - if scan mode is disabled, injected channels sequence length is set to  */
624   /*   0x00: 1 channel converted (channel on regular rank 1)                  */
625   /*   Parameter "InjectedNbrOfConversion" is discarded.                      */
626   /*   Note: Scan mode is present by hardware on this device and, if          */
627   /*   disabled, discards automatically nb of conversions. Anyway, nb of      */
628   /*   conversions is forced to 0x00 for alignment over all STM32 devices.    */
629   /* - if scan mode is enabled, injected channels sequence length is set to   */
630   /*   parameter ""InjectedNbrOfConversion".                                  */
631   if (hadc->Init.ScanConvMode == ADC_SCAN_DISABLE)
632   {
633     if (sConfigInjected->InjectedRank == ADC_INJECTED_RANK_1)
634     {
635       /* Clear the old SQx bits for all injected ranks */
636         MODIFY_REG(hadc->Instance->JSQR                           ,
637                    ADC_JSQR_JL   |
638                    ADC_JSQR_JSQ4 |
639                    ADC_JSQR_JSQ3 |
640                    ADC_JSQR_JSQ2 |
641                    ADC_JSQR_JSQ1                                  ,
642                    __ADC_JSQR_RK(sConfigInjected->InjectedChannel,
643                                  ADC_INJECTED_RANK_1,
644                                  0x01)                             );
645     }
646     /* If another injected rank than rank1 was intended to be set, and could  */
647     /* not due to ScanConvMode disabled, error is reported.                   */
648     else
649     {
650         /* Update ADC state machine to error */
651         hadc->State = HAL_ADC_STATE_ERROR;
652         
653         tmpHALStatus = HAL_ERROR;
654     }
655   }
656   else
657   {   
658     /* Since injected channels rank conv. order depends on total number of   */
659     /* injected conversions, selected rank must be below or equal to total   */
660     /* number of injected conversions to be updated.                         */
661     if (sConfigInjected->InjectedRank <= sConfigInjected->InjectedNbrOfConversion)
662     {
663       /* Clear the old SQx bits for the selected rank */
664       /* Set the SQx bits for the selected rank */
665       MODIFY_REG(hadc->Instance->JSQR                                                 ,
666                  
667                  ADC_JSQR_JL                                            |
668                  __ADC_JSQR_RK(ADC_JSQR_JSQ1,                         
669                                sConfigInjected->InjectedRank,         
670                                sConfigInjected->InjectedNbrOfConversion)              ,
671                  
672                  __ADC_JSQR_JL(sConfigInjected->InjectedNbrOfConversion) |
673                  __ADC_JSQR_RK(sConfigInjected->InjectedChannel,      
674                                              sConfigInjected->InjectedRank,         
675                                              sConfigInjected->InjectedNbrOfConversion) );
676     }
677     else
678     {
679       /* Clear the old SQx bits for the selected rank */
680       MODIFY_REG(hadc->Instance->JSQR                                    ,
681                  
682                  ADC_JSQR_JL                                            |
683                  __ADC_JSQR_RK(ADC_JSQR_JSQ1,                         
684                                sConfigInjected->InjectedRank,         
685                                sConfigInjected->InjectedNbrOfConversion) ,
686                  
687                  0x00000000                                               );
688     }
689   } 
690     
691   /* Enable external trigger if trigger selection is different of software    */
692   /* start.                                                                   */
693   /* Note: This configuration keeps the hardware feature of parameter         */
694   /*       ExternalTrigConvEdge "trigger edge none" equivalent to             */
695   /*       software start.                                                    */
696   
697   if (sConfigInjected->ExternalTrigInjecConv != ADC_INJECTED_SOFTWARE_START)
698   {    
699     MODIFY_REG(hadc->Instance->CR2                        ,
700                ADC_CR2_JEXTEN  |
701                ADC_CR2_JEXTSEL                            ,
702                sConfigInjected->ExternalTrigInjecConv    |
703                sConfigInjected->ExternalTrigInjecConvEdge  );
704   }
705   else
706   {
707     MODIFY_REG(hadc->Instance->CR2,
708                ADC_CR2_JEXTEN  |
709                ADC_CR2_JEXTSEL    ,
710                0x00000000          );
711   }
712
713   /* Configuration of injected group                                          */
714   /* Parameters update conditioned to ADC state:                              */
715   /* Parameters that can be updated only when ADC is disabled:                */
716   /*  - Automatic injected conversion                                         */
717   /*  - Injected discontinuous mode                                           */
718   if ((__HAL_ADC_IS_ENABLED(hadc) == RESET))
719   {
720     hadc->Instance->CR1 &= ~(ADC_CR1_JAUTO   |
721                              ADC_CR1_JDISCEN  );
722     
723     /* Automatic injected conversion can be enabled if injected group         */
724     /* external triggers are disabled.                                        */
725     if (sConfigInjected->AutoInjectedConv == ENABLE)
726     {
727       if (sConfigInjected->ExternalTrigInjecConv == ADC_INJECTED_SOFTWARE_START)
728       {
729         SET_BIT(hadc->Instance->CR1, ADC_CR1_JAUTO);
730       }
731       else
732       {
733         /* Update ADC state machine to error */
734         hadc->State = HAL_ADC_STATE_ERROR;
735         
736         tmpHALStatus = HAL_ERROR;
737       }
738     }
739     
740     /* Injected discontinuous can be enabled only if auto-injected mode is    */
741     /* disabled.                                                              */  
742     if (sConfigInjected->InjectedDiscontinuousConvMode == ENABLE)
743     {
744       if (sConfigInjected->AutoInjectedConv == DISABLE)
745       {
746         SET_BIT(hadc->Instance->CR1, ADC_CR1_JDISCEN);
747       } 
748       else
749       {
750         /* Update ADC state machine to error */
751         hadc->State = HAL_ADC_STATE_ERROR;
752         
753         tmpHALStatus = HAL_ERROR;
754       }
755     }
756   }
757
758   /* InjectedChannel sampling time configuration */
759   /* For InjectedChannels 0 to 9 */
760   if (sConfigInjected->InjectedChannel < ADC_CHANNEL_10)
761   {
762     MODIFY_REG(hadc->Instance->SMPR3,
763                __ADC_SMPR3(ADC_SMPR3_SMP0, sConfigInjected->InjectedChannel),
764                __ADC_SMPR3(sConfigInjected->InjectedSamplingTime, sConfigInjected->InjectedChannel) );
765   }
766   /* For InjectedChannels 10 to 19 */
767   else if (sConfigInjected->InjectedChannel < ADC_CHANNEL_20)
768   {
769     MODIFY_REG(hadc->Instance->SMPR2,
770                __ADC_SMPR2(ADC_SMPR2_SMP10, sConfigInjected->InjectedChannel),
771                __ADC_SMPR2(sConfigInjected->InjectedSamplingTime, sConfigInjected->InjectedChannel) );
772   }
773   /* For InjectedChannels 20 to 26 for devices Cat.1, Cat.2, Cat.3 */
774   /* For InjectedChannels 20 to 29 for devices Cat4, Cat.5 */
775   else if (sConfigInjected->InjectedChannel <= ADC_SMPR1_CHANNEL_MAX)
776   {   
777     MODIFY_REG(hadc->Instance->SMPR1,
778                __ADC_SMPR1(ADC_SMPR1_SMP20, sConfigInjected->InjectedChannel),
779                __ADC_SMPR1(sConfigInjected->InjectedSamplingTime, sConfigInjected->InjectedChannel) );
780   }
781   /* For InjectedChannels 30 to 31 for devices Cat4, Cat.5 */
782   else
783   {
784     __ADC_SMPR0_CHANNEL_SET(hadc, sConfigInjected->InjectedSamplingTime, sConfigInjected->InjectedChannel);
785   }
786   
787   
788   /* Configure the offset: offset enable/disable, InjectedChannel, offset value */
789   switch(sConfigInjected->InjectedRank)
790   {
791     case 1:
792       /* Set injected channel 1 offset */     
793       MODIFY_REG(hadc->Instance->JOFR1,
794                  ADC_JOFR1_JOFFSET1,
795                  sConfigInjected->InjectedOffset);
796       break;
797     case 2:
798       /* Set injected channel 2 offset */
799       MODIFY_REG(hadc->Instance->JOFR2,
800                  ADC_JOFR2_JOFFSET2,
801                  sConfigInjected->InjectedOffset);
802       break;
803     case 3:
804       /* Set injected channel 3 offset */
805       MODIFY_REG(hadc->Instance->JOFR3,
806                  ADC_JOFR3_JOFFSET3,
807                  sConfigInjected->InjectedOffset);
808       break;
809     case 4:
810     default:
811       MODIFY_REG(hadc->Instance->JOFR4,
812                  ADC_JOFR4_JOFFSET4,
813                  sConfigInjected->InjectedOffset);
814       break;
815   }
816   
817   /* If ADC1 Channel_16 or Channel_17 is selected, enable Temperature sensor  */
818   /* and VREFINT measurement path.                                            */
819   if ((sConfigInjected->InjectedChannel == ADC_CHANNEL_TEMPSENSOR) ||
820       (sConfigInjected->InjectedChannel == ADC_CHANNEL_VREFINT)      )
821   {
822     SET_BIT(ADC->CCR, ADC_CCR_TSVREFE);
823   }
824   
825   /* Process unlocked */
826   __HAL_UNLOCK(hadc);
827   
828   /* Return function status */
829   return tmpHALStatus;
830 }
831
832 /**
833   * @}
834   */  
835
836 /**
837   * @}
838   */
839
840 #endif /* HAL_ADC_MODULE_ENABLED */
841 /**
842   * @}
843   */
844
845 /**
846   * @}
847   */
848
849 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/