2 ******************************************************************************
3 * @file stm32l1xx_hal_adc_ex.c
4 * @author MCD Application Team
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)
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.
16 * ++ Analog Watchdog configuration
17 * ++ Channels configuration on regular group
19 * ++ ADC state machine management
20 * ++ Interrupts and flags management
21 * Other functions (generic functions) are available in file
22 * "stm32l1xx_hal_adc.c".
25 ==============================================================================
26 ##### How to use this driver #####
27 ==============================================================================
30 (#) Activate the ADC peripheral using one of the start functions:
31 HAL_ADCEx_InjectedStart(), HAL_ADCEx_InjectedStart_IT().
33 *** Channels configuration to injected group ***
34 ================================================
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()
43 ******************************************************************************
46 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
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.
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.
70 ******************************************************************************
73 /* Includes ------------------------------------------------------------------*/
74 #include "stm32l1xx_hal.h"
76 /** @addtogroup STM32L1xx_HAL_Driver
80 /** @defgroup ADCEx ADCEx
81 * @brief ADC HAL module driver
85 #ifdef HAL_ADC_MODULE_ENABLED
87 /* Private typedef -----------------------------------------------------------*/
88 /* Private define ------------------------------------------------------------*/
89 /** @defgroup ADCEx_Private_Constants ADCEx Private Constants
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)
108 /* Private macro -------------------------------------------------------------*/
109 /* Private variables ---------------------------------------------------------*/
110 /* Private function prototypes -----------------------------------------------*/
111 /* Private functions ---------------------------------------------------------*/
113 /** @defgroup ADCEx_Exported_Functions ADCEx Exported Functions
117 /** @defgroup ADCEx_Exported_Functions_Group1 Extended Initialization/de-initialization functions
118 * @brief Extended Initialization and Configuration functions
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.
137 * @brief Enables ADC, starts conversion of injected group.
138 * Interruptions enabled in this function: None.
139 * @param hadc: ADC handle
142 HAL_StatusTypeDef HAL_ADCEx_InjectedStart(ADC_HandleTypeDef* hadc)
144 HAL_StatusTypeDef tmpHALStatus = HAL_OK;
146 /* Check the parameters */
147 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
152 /* Enable the ADC peripheral */
153 tmpHALStatus = ADC_Enable(hadc);
155 /* Start conversion if ADC is effectively enabled */
156 if (tmpHALStatus != HAL_ERROR)
158 /* Check if a regular conversion is ongoing */
159 if(hadc->State == HAL_ADC_STATE_BUSY_REG)
161 /* Change ADC state */
162 hadc->State = HAL_ADC_STATE_BUSY_INJ_REG;
166 /* Change ADC state */
167 hadc->State = HAL_ADC_STATE_BUSY_INJ;
170 /* Set ADC error code to none */
171 __HAL_ADC_CLEAR_ERRORCODE(hadc);
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);
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 */
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) )
186 /* Enable ADC software conversion for injected channels */
187 SET_BIT(hadc->Instance->CR2, ADC_CR2_JSWSTART);
191 /* Process unlocked */
194 /* Return function status */
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
207 HAL_StatusTypeDef HAL_ADCEx_InjectedStop(ADC_HandleTypeDef* hadc)
209 HAL_StatusTypeDef tmpHALStatus = HAL_OK;
211 /* Check the parameters */
212 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
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 */
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) )
227 /* Stop potential conversion on going, on regular and injected groups */
228 /* Disable ADC peripheral */
229 tmpHALStatus = ADC_ConversionStop_Disable(hadc);
231 /* Check if ADC is effectively disabled */
232 if (tmpHALStatus != HAL_ERROR)
234 /* Change ADC state */
235 hadc->State = HAL_ADC_STATE_READY;
240 /* Update ADC state machine to error */
241 hadc->State = HAL_ADC_STATE_ERROR;
243 tmpHALStatus = HAL_ERROR;
246 /* Process unlocked */
249 /* Return function status */
254 * @brief Wait for injected group conversion to be completed.
255 * @param hadc: ADC handle
256 * @param Timeout: Timeout value in millisecond.
259 HAL_StatusTypeDef HAL_ADCEx_InjectedPollForConversion(ADC_HandleTypeDef* hadc, uint32_t Timeout)
263 /* Variables for polling in case of scan mode enabled and polling for each */
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;
271 /* Check the parameters */
272 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
275 tickstart = HAL_GetTick();
277 /* Polling for end of conversion: differentiation if single/sequence */
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 */
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). */
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) )
301 /* Wait until End of Conversion flag is raised */
302 while(HAL_IS_BIT_CLR(hadc->Instance->SR, ADC_FLAG_JEOC))
304 /* Check if timeout is disabled (set to infinite wait) */
305 if(Timeout != HAL_MAX_DELAY)
307 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
309 /* Update ADC state machine to timeout */
310 hadc->State = HAL_ADC_STATE_TIMEOUT;
312 /* Process unlocked */
322 /* Computation of CPU cycles corresponding to ADC conversion cycles. */
323 /* Retrieve ADC clock prescaler and ADC maximum conversion cycles on all */
325 conversion_timeout_cpu_cycles_max = __ADC_GET_CLOCK_PRESCALER_DECIMAL(hadc);
326 conversion_timeout_cpu_cycles_max *= __ADC_CONVCYCLES_MAX_RANGE(hadc);
328 /* Poll with maximum conversion time */
329 while(conversion_timeout_cpu_cycles < conversion_timeout_cpu_cycles_max)
331 /* Check if timeout is disabled (set to infinite wait) */
332 if(Timeout != HAL_MAX_DELAY)
334 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
336 /* Update ADC state machine to timeout */
337 hadc->State = HAL_ADC_STATE_TIMEOUT;
339 /* Process unlocked */
345 conversion_timeout_cpu_cycles ++;
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)
354 /* Clear injected group conversion flag */
355 __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JSTRT | ADC_FLAG_JEOC);
358 /* Update state machine on conversion status if not in error state */
359 if(hadc->State != HAL_ADC_STATE_ERROR)
361 /* Update ADC state machine */
362 if(hadc->State != HAL_ADC_STATE_EOC_INJ_REG)
365 if(hadc->State == HAL_ADC_STATE_EOC_REG)
367 /* Change ADC state */
368 hadc->State = HAL_ADC_STATE_EOC_INJ_REG;
372 /* Change ADC state */
373 hadc->State = HAL_ADC_STATE_EOC_INJ;
378 /* Return ADC state */
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.
389 HAL_StatusTypeDef HAL_ADCEx_InjectedStart_IT(ADC_HandleTypeDef* hadc)
391 HAL_StatusTypeDef tmpHALStatus = HAL_OK;
393 /* Check the parameters */
394 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
399 /* Enable the ADC peripheral */
400 tmpHALStatus = ADC_Enable(hadc);
402 /* Start conversion if ADC is effectively enabled */
403 if (tmpHALStatus != HAL_ERROR)
405 /* Check if a regular conversion is ongoing */
406 if(hadc->State == HAL_ADC_STATE_BUSY_REG)
408 /* Change ADC state */
409 hadc->State = HAL_ADC_STATE_BUSY_INJ_REG;
413 /* Change ADC state */
414 hadc->State = HAL_ADC_STATE_BUSY_INJ;
417 /* Set ADC error code to none */
418 __HAL_ADC_CLEAR_ERRORCODE(hadc);
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);
424 /* Enable end of conversion interrupt for injected channels */
425 __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOC);
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 */
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) )
436 /* Enable ADC software conversion for injected channels */
437 SET_BIT(hadc->Instance->CR2, ADC_CR2_JSWSTART);
441 /* Process unlocked */
444 /* Return function status */
449 * @brief Stop conversion of injected channels, disable interruption of
450 * end-of-conversion. Disable ADC peripheral if no regular conversion
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
457 HAL_StatusTypeDef HAL_ADCEx_InjectedStop_IT(ADC_HandleTypeDef* hadc)
459 HAL_StatusTypeDef tmpHALStatus = HAL_OK;
461 /* Check the parameters */
462 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
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 */
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) )
477 /* Stop potential conversion on going, on regular and injected groups */
478 /* Disable ADC peripheral */
479 tmpHALStatus = ADC_ConversionStop_Disable(hadc);
481 /* Check if ADC is effectively disabled */
482 if (tmpHALStatus != HAL_ERROR)
484 /* Disable ADC end of conversion interrupt for injected channels */
485 __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC);
487 /* Change ADC state */
488 hadc->State = HAL_ADC_STATE_READY;
493 /* Update ADC state machine to error */
494 hadc->State = HAL_ADC_STATE_ERROR;
496 tmpHALStatus = HAL_ERROR;
499 /* Process unlocked */
502 /* Return function status */
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
517 uint32_t HAL_ADCEx_InjectedGetValue(ADC_HandleTypeDef* hadc, uint32_t InjectedRank)
519 uint32_t tmp_jdr = 0;
521 /* Check the parameters */
522 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
523 assert_param(IS_ADC_INJECTED_RANK(InjectedRank));
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);
529 /* Get ADC converted value */
532 case ADC_INJECTED_RANK_4:
533 tmp_jdr = hadc->Instance->JDR4;
535 case ADC_INJECTED_RANK_3:
536 tmp_jdr = hadc->Instance->JDR3;
538 case ADC_INJECTED_RANK_2:
539 tmp_jdr = hadc->Instance->JDR2;
541 case ADC_INJECTED_RANK_1:
543 tmp_jdr = hadc->Instance->JDR1;
547 /* Return ADC converted value */
552 * @brief Injected conversion complete callback in non blocking mode
553 * @param hadc: ADC handle
556 __weak void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef* hadc)
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
567 /** @defgroup ADCEx_Exported_Functions_Group2 Extended Peripheral Control functions
568 * @brief Extended Peripheral Control functions
571 ===============================================================================
572 ##### Peripheral Control functions #####
573 ===============================================================================
574 [..] This section provides functions allowing to:
575 (+) Configure channels on injected group
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
595 HAL_StatusTypeDef HAL_ADCEx_InjectedConfigChannel(ADC_HandleTypeDef* hadc, ADC_InjectionConfTypeDef* sConfigInjected)
597 HAL_StatusTypeDef tmpHALStatus = HAL_OK;
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));
607 if(hadc->Init.ScanConvMode != ADC_SCAN_DISABLE)
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));
614 if(sConfigInjected->ExternalTrigInjecConvEdge != ADC_INJECTED_SOFTWARE_START)
616 assert_param(IS_ADC_EXTTRIGINJEC_EDGE(sConfigInjected->ExternalTrigInjecConvEdge));
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)
633 if (sConfigInjected->InjectedRank == ADC_INJECTED_RANK_1)
635 /* Clear the old SQx bits for all injected ranks */
636 MODIFY_REG(hadc->Instance->JSQR ,
642 __ADC_JSQR_RK(sConfigInjected->InjectedChannel,
646 /* If another injected rank than rank1 was intended to be set, and could */
647 /* not due to ScanConvMode disabled, error is reported. */
650 /* Update ADC state machine to error */
651 hadc->State = HAL_ADC_STATE_ERROR;
653 tmpHALStatus = HAL_ERROR;
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)
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 ,
668 __ADC_JSQR_RK(ADC_JSQR_JSQ1,
669 sConfigInjected->InjectedRank,
670 sConfigInjected->InjectedNbrOfConversion) ,
672 __ADC_JSQR_JL(sConfigInjected->InjectedNbrOfConversion) |
673 __ADC_JSQR_RK(sConfigInjected->InjectedChannel,
674 sConfigInjected->InjectedRank,
675 sConfigInjected->InjectedNbrOfConversion) );
679 /* Clear the old SQx bits for the selected rank */
680 MODIFY_REG(hadc->Instance->JSQR ,
683 __ADC_JSQR_RK(ADC_JSQR_JSQ1,
684 sConfigInjected->InjectedRank,
685 sConfigInjected->InjectedNbrOfConversion) ,
691 /* Enable external trigger if trigger selection is different of software */
693 /* Note: This configuration keeps the hardware feature of parameter */
694 /* ExternalTrigConvEdge "trigger edge none" equivalent to */
695 /* software start. */
697 if (sConfigInjected->ExternalTrigInjecConv != ADC_INJECTED_SOFTWARE_START)
699 MODIFY_REG(hadc->Instance->CR2 ,
702 sConfigInjected->ExternalTrigInjecConv |
703 sConfigInjected->ExternalTrigInjecConvEdge );
707 MODIFY_REG(hadc->Instance->CR2,
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))
720 hadc->Instance->CR1 &= ~(ADC_CR1_JAUTO |
723 /* Automatic injected conversion can be enabled if injected group */
724 /* external triggers are disabled. */
725 if (sConfigInjected->AutoInjectedConv == ENABLE)
727 if (sConfigInjected->ExternalTrigInjecConv == ADC_INJECTED_SOFTWARE_START)
729 SET_BIT(hadc->Instance->CR1, ADC_CR1_JAUTO);
733 /* Update ADC state machine to error */
734 hadc->State = HAL_ADC_STATE_ERROR;
736 tmpHALStatus = HAL_ERROR;
740 /* Injected discontinuous can be enabled only if auto-injected mode is */
742 if (sConfigInjected->InjectedDiscontinuousConvMode == ENABLE)
744 if (sConfigInjected->AutoInjectedConv == DISABLE)
746 SET_BIT(hadc->Instance->CR1, ADC_CR1_JDISCEN);
750 /* Update ADC state machine to error */
751 hadc->State = HAL_ADC_STATE_ERROR;
753 tmpHALStatus = HAL_ERROR;
758 /* InjectedChannel sampling time configuration */
759 /* For InjectedChannels 0 to 9 */
760 if (sConfigInjected->InjectedChannel < ADC_CHANNEL_10)
762 MODIFY_REG(hadc->Instance->SMPR3,
763 __ADC_SMPR3(ADC_SMPR3_SMP0, sConfigInjected->InjectedChannel),
764 __ADC_SMPR3(sConfigInjected->InjectedSamplingTime, sConfigInjected->InjectedChannel) );
766 /* For InjectedChannels 10 to 19 */
767 else if (sConfigInjected->InjectedChannel < ADC_CHANNEL_20)
769 MODIFY_REG(hadc->Instance->SMPR2,
770 __ADC_SMPR2(ADC_SMPR2_SMP10, sConfigInjected->InjectedChannel),
771 __ADC_SMPR2(sConfigInjected->InjectedSamplingTime, sConfigInjected->InjectedChannel) );
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)
777 MODIFY_REG(hadc->Instance->SMPR1,
778 __ADC_SMPR1(ADC_SMPR1_SMP20, sConfigInjected->InjectedChannel),
779 __ADC_SMPR1(sConfigInjected->InjectedSamplingTime, sConfigInjected->InjectedChannel) );
781 /* For InjectedChannels 30 to 31 for devices Cat4, Cat.5 */
784 __ADC_SMPR0_CHANNEL_SET(hadc, sConfigInjected->InjectedSamplingTime, sConfigInjected->InjectedChannel);
788 /* Configure the offset: offset enable/disable, InjectedChannel, offset value */
789 switch(sConfigInjected->InjectedRank)
792 /* Set injected channel 1 offset */
793 MODIFY_REG(hadc->Instance->JOFR1,
795 sConfigInjected->InjectedOffset);
798 /* Set injected channel 2 offset */
799 MODIFY_REG(hadc->Instance->JOFR2,
801 sConfigInjected->InjectedOffset);
804 /* Set injected channel 3 offset */
805 MODIFY_REG(hadc->Instance->JOFR3,
807 sConfigInjected->InjectedOffset);
811 MODIFY_REG(hadc->Instance->JOFR4,
813 sConfigInjected->InjectedOffset);
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) )
822 SET_BIT(ADC->CCR, ADC_CCR_TSVREFE);
825 /* Process unlocked */
828 /* Return function status */
840 #endif /* HAL_ADC_MODULE_ENABLED */
849 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/