1 /**
2 ******************************************************************************
3 * @file stm32mp1xx_hal_adc_ex.c
4 * @author MCD Application Team
5 * @brief This file provides firmware functions to manage the following
6 * functionalities of the Analog to Digital Converter (ADC)
7 * peripheral:
8 * + Peripheral Control functions
9 * Other functions (generic functions) are available in file
10 * "stm32mp1xx_hal_adc.c".
11 *
12 ******************************************************************************
13 * @attention
14 *
15 * Copyright (c) 2019 STMicroelectronics.
16 * All rights reserved.
17 *
18 * This software is licensed under terms that can be found in the LICENSE file
19 * in the root directory of this software component.
20 * If no LICENSE file comes with this software, it is provided AS-IS.
21 *
22 ******************************************************************************
23 @verbatim
24 [..]
25 (@) Sections "ADC peripheral features" and "How to use this driver" are
26 available in file of generic functions "stm32mp1xx_hal_adc.c".
27 [..]
28 @endverbatim
29 ******************************************************************************
30 */
31
32 /* Includes ------------------------------------------------------------------*/
33 #include "stm32mp1xx_hal.h"
34
35 /** @addtogroup STM32MP1xx_HAL_Driver
36 * @{
37 */
38
39 /** @defgroup ADCEx ADCEx
40 * @brief ADC Extended HAL module driver
41 * @{
42 */
43
44 #ifdef HAL_ADC_MODULE_ENABLED
45
46 /* Private typedef -----------------------------------------------------------*/
47 /* Private define ------------------------------------------------------------*/
48
49 /** @defgroup ADCEx_Private_Constants ADC Extended Private Constants
50 * @{
51 */
52
53 #define ADC_JSQR_FIELDS ((ADC_JSQR_JL | ADC_JSQR_JEXTSEL | ADC_JSQR_JEXTEN |\
54 ADC_JSQR_JSQ1 | ADC_JSQR_JSQ2 |\
55 ADC_JSQR_JSQ3 | ADC_JSQR_JSQ4 )) /*!< ADC_JSQR fields of parameters that can be updated anytime once the ADC is enabled */
56
57 /* Fixed timeout value for ADC calibration. */
58 /* Values defined to be higher than worst cases: maximum ratio between ADC */
59 /* and CPU clock frequencies. */
60 /* Example of profile low frequency : ADC frequency at 15.625kHz (ADC clock */
61 /* source PLL4_Q 4MHz, ADC clock prescaler 256), CPU frequency 800MHz. */
62 /* Calibration time max = 16384 / fADC (refer to datasheet) */
63 /* 16384 / 15625 = 1.048s */
64 /* At maximum CPU speed (800 MHz), this means */
65 /* = 839000000 CPU cycles */
66 #define ADC_CALIBRATION_TIMEOUT (839000000UL) /*!< ADC calibration time-out value */
67
68 /**
69 * @}
70 */
71
72 /* Private macro -------------------------------------------------------------*/
73 /* Private variables ---------------------------------------------------------*/
74 /* Private function prototypes -----------------------------------------------*/
75 /* Exported functions --------------------------------------------------------*/
76
77 /** @defgroup ADCEx_Exported_Functions ADC Extended Exported Functions
78 * @{
79 */
80
81 /** @defgroup ADCEx_Exported_Functions_Group1 Extended Input and Output operation functions
82 * @brief Extended IO operation functions
83 *
84 @verbatim
85 ===============================================================================
86 ##### IO operation functions #####
87 ===============================================================================
88 [..] This section provides functions allowing to:
89
90 (+) Perform the ADC self-calibration for single or differential ending.
91 (+) Get calibration factors for single or differential ending.
92 (+) Set calibration factors for single or differential ending.
93
94 (+) Start conversion of ADC group injected.
95 (+) Stop conversion of ADC group injected.
96 (+) Poll for conversion complete on ADC group injected.
97 (+) Get result of ADC group injected channel conversion.
98 (+) Start conversion of ADC group injected and enable interruptions.
99 (+) Stop conversion of ADC group injected and disable interruptions.
100
101 (+) When multimode feature is available, start multimode and enable DMA transfer.
102 (+) Stop multimode and disable ADC DMA transfer.
103 (+) Get result of multimode conversion.
104
105 @endverbatim
106 * @{
107 */
108
109 /**
110 * @brief Perform an ADC automatic self-calibration
111 * Calibration prerequisite: ADC must be disabled (execute this
112 * function before HAL_ADC_Start() or after HAL_ADC_Stop() ).
113 * @param hadc ADC handle
114 * @param CalibrationMode Selection of calibration offset or
115 * linear calibration offset.
116 * @arg ADC_CALIB_OFFSET Channel in mode calibration offset
117 * @arg ADC_CALIB_OFFSET_LINEARITY Channel in mode linear calibration offset
118 * @param SingleDiff Selection of single-ended or differential input
119 * This parameter can be one of the following values:
120 * @arg @ref ADC_SINGLE_ENDED Channel in mode input single ended
121 * @arg @ref ADC_DIFFERENTIAL_ENDED Channel in mode input differential ended
122 * @retval HAL status
123 */
HAL_ADCEx_Calibration_Start(ADC_HandleTypeDef * hadc,uint32_t CalibrationMode,uint32_t SingleDiff)124 HAL_StatusTypeDef HAL_ADCEx_Calibration_Start(ADC_HandleTypeDef *hadc, uint32_t CalibrationMode, uint32_t SingleDiff)
125 {
126 HAL_StatusTypeDef tmp_hal_status;
127 __IO uint32_t wait_loop_index = 0UL;
128
129 /* Check the parameters */
130 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
131 assert_param(IS_ADC_SINGLE_DIFFERENTIAL(SingleDiff));
132
133 /* Process locked */
134 __HAL_LOCK(hadc);
135
136 /* Calibration prerequisite: ADC must be disabled. */
137
138 /* Disable the ADC (if not already disabled) */
139 tmp_hal_status = ADC_Disable(hadc);
140
141 /* Check if ADC is effectively disabled */
142 if (tmp_hal_status == HAL_OK)
143 {
144 /* Set ADC state */
145 ADC_STATE_CLR_SET(hadc->State,
146 HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
147 HAL_ADC_STATE_BUSY_INTERNAL);
148
149 /* Start ADC calibration in mode single-ended or differential */
150 LL_ADC_StartCalibration(hadc->Instance , CalibrationMode, SingleDiff );
151
152 /* Wait for calibration completion */
153 while (LL_ADC_IsCalibrationOnGoing(hadc->Instance) != 0UL)
154 {
155 wait_loop_index++;
156 if (wait_loop_index >= ADC_CALIBRATION_TIMEOUT)
157 {
158 /* Update ADC state machine to error */
159 ADC_STATE_CLR_SET(hadc->State,
160 HAL_ADC_STATE_BUSY_INTERNAL,
161 HAL_ADC_STATE_ERROR_INTERNAL);
162
163 __HAL_UNLOCK(hadc);
164
165 return HAL_ERROR;
166 }
167 }
168
169 /* Set ADC state */
170 ADC_STATE_CLR_SET(hadc->State,
171 HAL_ADC_STATE_BUSY_INTERNAL,
172 HAL_ADC_STATE_READY);
173 }
174 else
175 {
176 SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
177
178 /* Note: No need to update variable "tmp_hal_status" here: already set */
179 /* to state "HAL_ERROR" by function disabling the ADC. */
180 }
181
182 __HAL_UNLOCK(hadc);
183
184 return tmp_hal_status;
185 }
186
187 /**
188 * @brief Get the calibration factor.
189 * @param hadc ADC handle.
190 * @param SingleDiff This parameter can be only:
191 * @arg @ref ADC_SINGLE_ENDED Channel in mode input single ended
192 * @arg @ref ADC_DIFFERENTIAL_ENDED Channel in mode input differential ended
193 * @retval Calibration value.
194 */
HAL_ADCEx_Calibration_GetValue(ADC_HandleTypeDef * hadc,uint32_t SingleDiff)195 uint32_t HAL_ADCEx_Calibration_GetValue(ADC_HandleTypeDef *hadc, uint32_t SingleDiff)
196 {
197 /* Check the parameters */
198 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
199 assert_param(IS_ADC_SINGLE_DIFFERENTIAL(SingleDiff));
200
201 /* Return the selected ADC calibration value */
202 return LL_ADC_GetCalibrationOffsetFactor(hadc->Instance, SingleDiff);
203 }
204
205 /**
206 * @brief Get the calibration factor from automatic conversion result
207 * @param hadc ADC handle
208 * @param LinearCalib_Buffer: Linear calibration factor
209 * @retval HAL state
210 */
HAL_ADCEx_LinearCalibration_GetValue(ADC_HandleTypeDef * hadc,uint32_t * LinearCalib_Buffer)211 HAL_StatusTypeDef HAL_ADCEx_LinearCalibration_GetValue(ADC_HandleTypeDef* hadc, uint32_t* LinearCalib_Buffer)
212 {
213 uint32_t cnt;
214 HAL_StatusTypeDef tmp_hal_status = HAL_OK;
215 uint32_t temp_REG_IsConversionOngoing = 0UL;
216
217 /* Check the parameters */
218 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
219
220 /* Enable the ADC ADEN = 1 to be able to read the linear calibration factor */
221 if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
222 {
223 tmp_hal_status = ADC_Enable(hadc);
224 }
225
226 if (tmp_hal_status == HAL_OK)
227 {
228 if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) != 0UL)
229 {
230 LL_ADC_REG_StopConversion(hadc->Instance);
231 temp_REG_IsConversionOngoing = 1UL;
232 }
233 for(cnt = ADC_LINEAR_CALIB_REG_COUNT; cnt > 0UL; cnt--)
234 {
235 LinearCalib_Buffer[cnt-1U]=LL_ADC_GetCalibrationLinearFactor(hadc->Instance, ADC_CR_LINCALRDYW6 >> (ADC_LINEAR_CALIB_REG_COUNT-cnt));
236 }
237 if (temp_REG_IsConversionOngoing != 0UL)
238 {
239 LL_ADC_REG_StartConversion(hadc->Instance);
240 }
241 }
242
243 return tmp_hal_status;
244 }
245
246 /**
247 * @brief Set the calibration factor to overwrite automatic conversion result.
248 * ADC must be enabled and no conversion is ongoing.
249 * @param hadc ADC handle
250 * @param SingleDiff This parameter can be only:
251 * @arg @ref ADC_SINGLE_ENDED Channel in mode input single ended
252 * @arg @ref ADC_DIFFERENTIAL_ENDED Channel in mode input differential ended
253 * @param CalibrationFactor Calibration factor (coded on 7 bits maximum)
254 * @retval HAL state
255 */
HAL_ADCEx_Calibration_SetValue(ADC_HandleTypeDef * hadc,uint32_t SingleDiff,uint32_t CalibrationFactor)256 HAL_StatusTypeDef HAL_ADCEx_Calibration_SetValue(ADC_HandleTypeDef *hadc, uint32_t SingleDiff,
257 uint32_t CalibrationFactor)
258 {
259 HAL_StatusTypeDef tmp_hal_status = HAL_OK;
260 uint32_t tmp_adc_is_conversion_on_going_regular;
261 uint32_t tmp_adc_is_conversion_on_going_injected;
262
263 /* Check the parameters */
264 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
265 assert_param(IS_ADC_SINGLE_DIFFERENTIAL(SingleDiff));
266 assert_param(IS_ADC_CALFACT(CalibrationFactor));
267
268 /* Process locked */
269 __HAL_LOCK(hadc);
270
271 /* Verification of hardware constraints before modifying the calibration */
272 /* factors register: ADC must be enabled, no conversion on going. */
273 tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
274 tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
275
276 if ((LL_ADC_IsEnabled(hadc->Instance) != 0UL)
277 && (tmp_adc_is_conversion_on_going_regular == 0UL)
278 && (tmp_adc_is_conversion_on_going_injected == 0UL)
279 )
280 {
281 /* Set the selected ADC calibration value */
282 LL_ADC_SetCalibrationOffsetFactor(hadc->Instance, SingleDiff, CalibrationFactor);
283 }
284 else
285 {
286 /* Update ADC state machine */
287 SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
288 /* Update ADC error code */
289 SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
290
291 /* Update ADC state machine to error */
292 tmp_hal_status = HAL_ERROR;
293 }
294
295 __HAL_UNLOCK(hadc);
296
297 return tmp_hal_status;
298 }
299
300 /**
301 * @brief Set the linear calibration factor
302 * @param hadc ADC handle
303 * @param LinearCalib_Buffer: Linear calibration factor
304 * @retval HAL state
305 */
HAL_ADCEx_LinearCalibration_SetValue(ADC_HandleTypeDef * hadc,uint32_t * LinearCalib_Buffer)306 HAL_StatusTypeDef HAL_ADCEx_LinearCalibration_SetValue(ADC_HandleTypeDef *hadc, uint32_t* LinearCalib_Buffer)
307 {
308 uint32_t cnt;
309 __IO uint32_t wait_loop_index = 0;
310
311 /* Check the parameters */
312 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
313
314 /* - Exit from deep-power-down mode and ADC voltage regulator enable */
315 /* Exit deep power down mode if still in that state */
316 if (HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_DEEPPWD))
317 {
318 /* Exit deep power down mode */
319 CLEAR_BIT(hadc->Instance->CR, ADC_CR_DEEPPWD);
320
321 /* System was in deep power down mode, calibration must
322 be relaunched or a previously saved calibration factor
323 re-applied once the ADC voltage regulator is enabled */
324 }
325
326
327 if (HAL_IS_BIT_CLR(hadc->Instance->CR, ADC_CR_ADVREGEN))
328 {
329 /* Enable ADC internal voltage regulator */
330 SET_BIT(hadc->Instance->CR, ADC_CR_ADVREGEN);
331 /* Delay for ADC stabilization time */
332 /* Wait loop initialization and execution */
333 /* Note: Variable divided by 2 to compensate partially */
334 /* CPU processing cycles. */
335 wait_loop_index = (ADC_STAB_DELAY_US * (SystemCoreClock / (1000000UL * 2UL)));
336 while(wait_loop_index != 0UL)
337 {
338 wait_loop_index--;
339 }
340 }
341
342
343 /* Verification that ADC voltage regulator is correctly enabled, whether */
344 /* or not ADC is coming from state reset (if any potential problem of */
345 /* clocking, voltage regulator would not be enabled). */
346 if (HAL_IS_BIT_CLR(hadc->Instance->CR, ADC_CR_ADVREGEN))
347 {
348 /* Update ADC state machine to error */
349 SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
350
351 /* Set ADC error code to ADC peripheral internal error */
352 SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
353
354 return HAL_ERROR;
355 }
356 for(cnt = 0UL; cnt < ADC_LINEAR_CALIB_REG_COUNT; cnt++)
357 {
358 LL_ADC_SetCalibrationLinearFactor(hadc->Instance, ADC_CR_LINCALRDYW6 >> cnt, LinearCalib_Buffer[cnt]);
359 }
360 return HAL_OK;
361 }
362
363 /**
364 * @brief Enable ADC, start conversion of injected group.
365 * @note Interruptions enabled in this function: None.
366 * @note Case of multimode enabled when multimode feature is available:
367 * HAL_ADCEx_InjectedStart() API must be called for ADC slave first,
368 * then for ADC master.
369 * For ADC slave, ADC is enabled only (conversion is not started).
370 * For ADC master, ADC is enabled and multimode conversion is started.
371 * @param hadc ADC handle.
372 * @retval HAL status
373 */
HAL_ADCEx_InjectedStart(ADC_HandleTypeDef * hadc)374 HAL_StatusTypeDef HAL_ADCEx_InjectedStart(ADC_HandleTypeDef *hadc)
375 {
376 HAL_StatusTypeDef tmp_hal_status;
377 uint32_t tmp_config_injected_queue;
378 #if defined(ADC_MULTIMODE_SUPPORT)
379 uint32_t tmp_multimode_config = LL_ADC_GetMultimode(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
380 #endif
381
382 /* Check the parameters */
383 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
384
385 if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) != 0UL)
386 {
387 return HAL_BUSY;
388 }
389 else
390 {
391 /* In case of software trigger detection enabled, JQDIS must be set
392 (which can be done only if ADSTART and JADSTART are both cleared).
393 If JQDIS is not set at that point, returns an error
394 - since software trigger detection is disabled. User needs to
395 resort to HAL_ADCEx_DisableInjectedQueue() API to set JQDIS.
396 - or (if JQDIS is intentionally reset) since JEXTEN = 0 which means
397 the queue is empty */
398 tmp_config_injected_queue = READ_BIT(hadc->Instance->CFGR, ADC_CFGR_JQDIS);
399
400 if ((READ_BIT(hadc->Instance->JSQR, ADC_JSQR_JEXTEN) == 0UL)
401 && (tmp_config_injected_queue == 0UL)
402 )
403 {
404 SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
405 return HAL_ERROR;
406 }
407
408 /* Process locked */
409 __HAL_LOCK(hadc);
410
411 /* Enable the ADC peripheral */
412 tmp_hal_status = ADC_Enable(hadc);
413
414 /* Start conversion if ADC is effectively enabled */
415 if (tmp_hal_status == HAL_OK)
416 {
417 /* Check if a regular conversion is ongoing */
418 if ((hadc->State & HAL_ADC_STATE_REG_BUSY) != 0UL)
419 {
420 /* Reset ADC error code field related to injected conversions only */
421 CLEAR_BIT(hadc->ErrorCode, HAL_ADC_ERROR_JQOVF);
422 }
423 else
424 {
425 /* Set ADC error code to none */
426 ADC_CLEAR_ERRORCODE(hadc);
427 }
428
429 /* Set ADC state */
430 /* - Clear state bitfield related to injected group conversion results */
431 /* - Set state bitfield related to injected operation */
432 ADC_STATE_CLR_SET(hadc->State,
433 HAL_ADC_STATE_READY | HAL_ADC_STATE_INJ_EOC,
434 HAL_ADC_STATE_INJ_BUSY);
435
436 #if defined(ADC_MULTIMODE_SUPPORT)
437 /* Reset HAL_ADC_STATE_MULTIMODE_SLAVE bit
438 - if ADC instance is master or if multimode feature is not available
439 - if multimode setting is disabled (ADC instance slave in independent mode) */
440 if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
441 || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
442 )
443 {
444 CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
445 }
446 #endif
447
448 /* Clear ADC group injected group conversion flag */
449 /* (To ensure of no unknown state from potential previous ADC operations) */
450 __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_JEOC | ADC_FLAG_JEOS));
451
452 /* Unlock before starting ADC conversions: in case of potential */
453 /* interruption, to let the process to ADC IRQ Handler. */
454 __HAL_UNLOCK(hadc);
455
456 /* Enable conversion of injected group, if automatic injected conversion */
457 /* is disabled. */
458 /* If software start has been selected, conversion starts immediately. */
459 /* If external trigger has been selected, conversion will start at next */
460 /* trigger event. */
461 /* Case of multimode enabled (when multimode feature is available): */
462 /* if ADC is slave, */
463 /* - ADC is enabled only (conversion is not started), */
464 /* - if multimode only concerns regular conversion, ADC is enabled */
465 /* and conversion is started. */
466 /* If ADC is master or independent, */
467 /* - ADC is enabled and conversion is started. */
468 #if defined(ADC_MULTIMODE_SUPPORT)
469 if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
470 || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
471 || (tmp_multimode_config == LL_ADC_MULTI_DUAL_REG_SIMULT)
472 || (tmp_multimode_config == LL_ADC_MULTI_DUAL_REG_INTERL)
473 )
474 {
475 /* ADC instance is not a multimode slave instance with multimode injected conversions enabled */
476 if (LL_ADC_INJ_GetTrigAuto(hadc->Instance) == LL_ADC_INJ_TRIG_INDEPENDENT)
477 {
478 LL_ADC_INJ_StartConversion(hadc->Instance);
479 }
480 }
481 else
482 {
483 /* ADC instance is not a multimode slave instance with multimode injected conversions enabled */
484 SET_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
485 }
486 #else
487 if (LL_ADC_INJ_GetTrigAuto(hadc->Instance) == LL_ADC_INJ_TRIG_INDEPENDENT)
488 {
489 /* Start ADC group injected conversion */
490 LL_ADC_INJ_StartConversion(hadc->Instance);
491 }
492 #endif
493
494 }
495 else
496 {
497 __HAL_UNLOCK(hadc);
498 }
499
500 return tmp_hal_status;
501 }
502 }
503
504 /**
505 * @brief Stop conversion of injected channels. Disable ADC peripheral if
506 * no regular conversion is on going.
507 * @note If ADC must be disabled and if conversion is on going on
508 * regular group, function HAL_ADC_Stop must be used to stop both
509 * injected and regular groups, and disable the ADC.
510 * @note If injected group mode auto-injection is enabled,
511 * function HAL_ADC_Stop must be used.
512 * @note In case of multimode enabled (when multimode feature is available),
513 * HAL_ADCEx_InjectedStop() must be called for ADC master first, then for ADC slave.
514 * For ADC master, conversion is stopped and ADC is disabled.
515 * For ADC slave, ADC is disabled only (conversion stop of ADC master
516 * has already stopped conversion of ADC slave).
517 * @param hadc ADC handle.
518 * @retval HAL status
519 */
HAL_ADCEx_InjectedStop(ADC_HandleTypeDef * hadc)520 HAL_StatusTypeDef HAL_ADCEx_InjectedStop(ADC_HandleTypeDef *hadc)
521 {
522 HAL_StatusTypeDef tmp_hal_status;
523
524 /* Check the parameters */
525 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
526
527 /* Process locked */
528 __HAL_LOCK(hadc);
529
530 /* 1. Stop potential conversion on going on injected group only. */
531 tmp_hal_status = ADC_ConversionStop(hadc, ADC_INJECTED_GROUP);
532
533 /* Disable ADC peripheral if injected conversions are effectively stopped */
534 /* and if no conversion on regular group is on-going */
535 if (tmp_hal_status == HAL_OK)
536 {
537 if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
538 {
539 /* 2. Disable the ADC peripheral */
540 tmp_hal_status = ADC_Disable(hadc);
541
542 /* Check if ADC is effectively disabled */
543 if (tmp_hal_status == HAL_OK)
544 {
545 /* Set ADC state */
546 ADC_STATE_CLR_SET(hadc->State,
547 HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
548 HAL_ADC_STATE_READY);
549 }
550 }
551 /* Conversion on injected group is stopped, but ADC not disabled since */
552 /* conversion on regular group is still running. */
553 else
554 {
555 /* Set ADC state */
556 CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
557 }
558 }
559
560 __HAL_UNLOCK(hadc);
561
562 return tmp_hal_status;
563 }
564
565 /**
566 * @brief Wait for injected group conversion to be completed.
567 * @param hadc ADC handle
568 * @param Timeout Timeout value in millisecond.
569 * @note Depending on hadc->Init.EOCSelection, JEOS or JEOC is
570 * checked and cleared depending on AUTDLY bit status.
571 * @retval HAL status
572 */
HAL_ADCEx_InjectedPollForConversion(ADC_HandleTypeDef * hadc,uint32_t Timeout)573 HAL_StatusTypeDef HAL_ADCEx_InjectedPollForConversion(ADC_HandleTypeDef *hadc, uint32_t Timeout)
574 {
575 uint32_t tickstart;
576 uint32_t tmp_Flag_End;
577 uint32_t tmp_adc_inj_is_trigger_source_sw_start;
578 uint32_t tmp_adc_reg_is_trigger_source_sw_start;
579 uint32_t tmp_cfgr;
580 #if defined(ADC_MULTIMODE_SUPPORT)
581 const ADC_TypeDef *tmpADC_Master;
582 uint32_t tmp_multimode_config = LL_ADC_GetMultimode(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
583 #endif
584
585 /* Check the parameters */
586 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
587
588 /* If end of sequence selected */
589 if (hadc->Init.EOCSelection == ADC_EOC_SEQ_CONV)
590 {
591 tmp_Flag_End = ADC_FLAG_JEOS;
592 }
593 else /* end of conversion selected */
594 {
595 tmp_Flag_End = ADC_FLAG_JEOC;
596 }
597
598 /* Get timeout */
599 tickstart = HAL_GetTick();
600
601 /* Wait until End of Conversion or Sequence flag is raised */
602 while ((hadc->Instance->ISR & tmp_Flag_End) == 0UL)
603 {
604 /* Check if timeout is disabled (set to infinite wait) */
605 if (Timeout != HAL_MAX_DELAY)
606 {
607 if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0UL))
608 {
609 /* New check to avoid false timeout detection in case of preemption */
610 if ((hadc->Instance->ISR & tmp_Flag_End) == 0UL)
611 {
612 /* Update ADC state machine to timeout */
613 SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
614
615 __HAL_UNLOCK(hadc);
616
617 return HAL_TIMEOUT;
618 }
619 }
620 }
621 }
622
623 /* Retrieve ADC configuration */
624 tmp_adc_inj_is_trigger_source_sw_start = LL_ADC_INJ_IsTriggerSourceSWStart(hadc->Instance);
625 tmp_adc_reg_is_trigger_source_sw_start = LL_ADC_REG_IsTriggerSourceSWStart(hadc->Instance);
626 /* Get relevant register CFGR in ADC instance of ADC master or slave */
627 /* in function of multimode state (for devices with multimode */
628 /* available). */
629 #if defined(ADC_MULTIMODE_SUPPORT)
630 if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
631 || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
632 || (tmp_multimode_config == LL_ADC_MULTI_DUAL_REG_SIMULT)
633 || (tmp_multimode_config == LL_ADC_MULTI_DUAL_REG_INTERL)
634 )
635 {
636 tmp_cfgr = READ_REG(hadc->Instance->CFGR);
637 }
638 else
639 {
640 tmpADC_Master = __LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance);
641 tmp_cfgr = READ_REG(tmpADC_Master->CFGR);
642 }
643 #else
644 tmp_cfgr = READ_REG(hadc->Instance->CFGR);
645 #endif
646
647 /* Update ADC state machine */
648 SET_BIT(hadc->State, HAL_ADC_STATE_INJ_EOC);
649
650 /* Determine whether any further conversion upcoming on group injected */
651 /* by external trigger or by automatic injected conversion */
652 /* from group regular. */
653 if ((tmp_adc_inj_is_trigger_source_sw_start != 0UL) ||
654 ((READ_BIT(tmp_cfgr, ADC_CFGR_JAUTO) == 0UL) &&
655 ((tmp_adc_reg_is_trigger_source_sw_start != 0UL) &&
656 (READ_BIT(tmp_cfgr, ADC_CFGR_CONT) == 0UL))))
657 {
658 /* Check whether end of sequence is reached */
659 if (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_JEOS))
660 {
661 /* Particular case if injected contexts queue is enabled: */
662 /* when the last context has been fully processed, JSQR is reset */
663 /* by the hardware. Even if no injected conversion is planned to come */
664 /* (queue empty, triggers are ignored), it can start again */
665 /* immediately after setting a new context (JADSTART is still set). */
666 /* Therefore, state of HAL ADC injected group is kept to busy. */
667 if (READ_BIT(tmp_cfgr, ADC_CFGR_JQM) == 0UL)
668 {
669 /* Set ADC state */
670 CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
671
672 if ((hadc->State & HAL_ADC_STATE_REG_BUSY) == 0UL)
673 {
674 SET_BIT(hadc->State, HAL_ADC_STATE_READY);
675 }
676 }
677 }
678 }
679
680 /* Clear polled flag */
681 if (tmp_Flag_End == ADC_FLAG_JEOS)
682 {
683 /* Clear end of sequence JEOS flag of injected group if low power feature */
684 /* "LowPowerAutoWait " is disabled, to not interfere with this feature. */
685 /* For injected groups, no new conversion will start before JEOS is */
686 /* cleared. */
687 if (READ_BIT(tmp_cfgr, ADC_CFGR_AUTDLY) == 0UL)
688 {
689 __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_JEOC | ADC_FLAG_JEOS));
690 }
691 }
692 else
693 {
694 __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOC);
695 }
696
697 /* Return API HAL status */
698 return HAL_OK;
699 }
700
701 /**
702 * @brief Enable ADC, start conversion of injected group with interruption.
703 * @note Interruptions enabled in this function according to initialization
704 * setting : JEOC (end of conversion) or JEOS (end of sequence)
705 * @note Case of multimode enabled (when multimode feature is enabled):
706 * HAL_ADCEx_InjectedStart_IT() API must be called for ADC slave first,
707 * then for ADC master.
708 * For ADC slave, ADC is enabled only (conversion is not started).
709 * For ADC master, ADC is enabled and multimode conversion is started.
710 * @param hadc ADC handle.
711 * @retval HAL status.
712 */
HAL_ADCEx_InjectedStart_IT(ADC_HandleTypeDef * hadc)713 HAL_StatusTypeDef HAL_ADCEx_InjectedStart_IT(ADC_HandleTypeDef *hadc)
714 {
715 HAL_StatusTypeDef tmp_hal_status;
716 uint32_t tmp_config_injected_queue;
717 #if defined(ADC_MULTIMODE_SUPPORT)
718 uint32_t tmp_multimode_config = LL_ADC_GetMultimode(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
719 #endif
720
721 /* Check the parameters */
722 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
723
724 if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) != 0UL)
725 {
726 return HAL_BUSY;
727 }
728 else
729 {
730 /* In case of software trigger detection enabled, JQDIS must be set
731 (which can be done only if ADSTART and JADSTART are both cleared).
732 If JQDIS is not set at that point, returns an error
733 - since software trigger detection is disabled. User needs to
734 resort to HAL_ADCEx_DisableInjectedQueue() API to set JQDIS.
735 - or (if JQDIS is intentionally reset) since JEXTEN = 0 which means
736 the queue is empty */
737 tmp_config_injected_queue = READ_BIT(hadc->Instance->CFGR, ADC_CFGR_JQDIS);
738
739 if ((READ_BIT(hadc->Instance->JSQR, ADC_JSQR_JEXTEN) == 0UL)
740 && (tmp_config_injected_queue == 0UL)
741 )
742 {
743 SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
744 return HAL_ERROR;
745 }
746
747 /* Process locked */
748 __HAL_LOCK(hadc);
749
750 /* Enable the ADC peripheral */
751 tmp_hal_status = ADC_Enable(hadc);
752
753 /* Start conversion if ADC is effectively enabled */
754 if (tmp_hal_status == HAL_OK)
755 {
756 /* Check if a regular conversion is ongoing */
757 if ((hadc->State & HAL_ADC_STATE_REG_BUSY) != 0UL)
758 {
759 /* Reset ADC error code field related to injected conversions only */
760 CLEAR_BIT(hadc->ErrorCode, HAL_ADC_ERROR_JQOVF);
761 }
762 else
763 {
764 /* Set ADC error code to none */
765 ADC_CLEAR_ERRORCODE(hadc);
766 }
767
768 /* Set ADC state */
769 /* - Clear state bitfield related to injected group conversion results */
770 /* - Set state bitfield related to injected operation */
771 ADC_STATE_CLR_SET(hadc->State,
772 HAL_ADC_STATE_READY | HAL_ADC_STATE_INJ_EOC,
773 HAL_ADC_STATE_INJ_BUSY);
774
775 #if defined(ADC_MULTIMODE_SUPPORT)
776 /* Reset HAL_ADC_STATE_MULTIMODE_SLAVE bit
777 - if ADC instance is master or if multimode feature is not available
778 - if multimode setting is disabled (ADC instance slave in independent mode) */
779 if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
780 || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
781 )
782 {
783 CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
784 }
785 #endif
786
787 /* Clear ADC group injected group conversion flag */
788 /* (To ensure of no unknown state from potential previous ADC operations) */
789 __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_JEOC | ADC_FLAG_JEOS));
790
791 /* Unlock before starting ADC conversions: in case of potential */
792 /* interruption, to let the process to ADC IRQ Handler. */
793 __HAL_UNLOCK(hadc);
794
795 /* Enable ADC Injected context queue overflow interrupt if this feature */
796 /* is enabled. */
797 if ((hadc->Instance->CFGR & ADC_CFGR_JQM) != 0UL)
798 {
799 __HAL_ADC_ENABLE_IT(hadc, ADC_FLAG_JQOVF);
800 }
801
802 /* Enable ADC end of conversion interrupt */
803 switch (hadc->Init.EOCSelection)
804 {
805 case ADC_EOC_SEQ_CONV:
806 __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC);
807 __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOS);
808 break;
809 /* case ADC_EOC_SINGLE_CONV */
810 default:
811 __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOS);
812 __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOC);
813 break;
814 }
815
816 /* Enable conversion of injected group, if automatic injected conversion */
817 /* is disabled. */
818 /* If software start has been selected, conversion starts immediately. */
819 /* If external trigger has been selected, conversion will start at next */
820 /* trigger event. */
821 /* Case of multimode enabled (when multimode feature is available): */
822 /* if ADC is slave, */
823 /* - ADC is enabled only (conversion is not started), */
824 /* - if multimode only concerns regular conversion, ADC is enabled */
825 /* and conversion is started. */
826 /* If ADC is master or independent, */
827 /* - ADC is enabled and conversion is started. */
828 #if defined(ADC_MULTIMODE_SUPPORT)
829 if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
830 || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
831 || (tmp_multimode_config == LL_ADC_MULTI_DUAL_REG_SIMULT)
832 || (tmp_multimode_config == LL_ADC_MULTI_DUAL_REG_INTERL)
833 )
834 {
835 /* ADC instance is not a multimode slave instance with multimode injected conversions enabled */
836 if (LL_ADC_INJ_GetTrigAuto(hadc->Instance) == LL_ADC_INJ_TRIG_INDEPENDENT)
837 {
838 LL_ADC_INJ_StartConversion(hadc->Instance);
839 }
840 }
841 else
842 {
843 /* ADC instance is not a multimode slave instance with multimode injected conversions enabled */
844 SET_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
845 }
846 #else
847 if (LL_ADC_INJ_GetTrigAuto(hadc->Instance) == LL_ADC_INJ_TRIG_INDEPENDENT)
848 {
849 /* Start ADC group injected conversion */
850 LL_ADC_INJ_StartConversion(hadc->Instance);
851 }
852 #endif
853
854 }
855 else
856 {
857 __HAL_UNLOCK(hadc);
858 }
859
860 return tmp_hal_status;
861 }
862 }
863
864 /**
865 * @brief Stop conversion of injected channels, disable interruption of
866 * end-of-conversion. Disable ADC peripheral if no regular conversion
867 * is on going.
868 * @note If ADC must be disabled and if conversion is on going on
869 * regular group, function HAL_ADC_Stop must be used to stop both
870 * injected and regular groups, and disable the ADC.
871 * @note If injected group mode auto-injection is enabled,
872 * function HAL_ADC_Stop must be used.
873 * @note Case of multimode enabled (when multimode feature is available):
874 * HAL_ADCEx_InjectedStop_IT() API must be called for ADC master first,
875 * then for ADC slave.
876 * For ADC master, conversion is stopped and ADC is disabled.
877 * For ADC slave, ADC is disabled only (conversion stop of ADC master
878 * has already stopped conversion of ADC slave).
879 * @note In case of auto-injection mode, HAL_ADC_Stop() must be used.
880 * @param hadc ADC handle
881 * @retval HAL status
882 */
HAL_ADCEx_InjectedStop_IT(ADC_HandleTypeDef * hadc)883 HAL_StatusTypeDef HAL_ADCEx_InjectedStop_IT(ADC_HandleTypeDef *hadc)
884 {
885 HAL_StatusTypeDef tmp_hal_status;
886
887 /* Check the parameters */
888 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
889
890 /* Process locked */
891 __HAL_LOCK(hadc);
892
893 /* 1. Stop potential conversion on going on injected group only. */
894 tmp_hal_status = ADC_ConversionStop(hadc, ADC_INJECTED_GROUP);
895
896 /* Disable ADC peripheral if injected conversions are effectively stopped */
897 /* and if no conversion on the other group (regular group) is intended to */
898 /* continue. */
899 if (tmp_hal_status == HAL_OK)
900 {
901 /* Disable ADC end of conversion interrupt for injected channels */
902 __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_JEOC | ADC_IT_JEOS | ADC_FLAG_JQOVF));
903
904 if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
905 {
906 /* 2. Disable the ADC peripheral */
907 tmp_hal_status = ADC_Disable(hadc);
908
909 /* Check if ADC is effectively disabled */
910 if (tmp_hal_status == HAL_OK)
911 {
912 /* Set ADC state */
913 ADC_STATE_CLR_SET(hadc->State,
914 HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
915 HAL_ADC_STATE_READY);
916 }
917 }
918 /* Conversion on injected group is stopped, but ADC not disabled since */
919 /* conversion on regular group is still running. */
920 else
921 {
922 /* Set ADC state */
923 CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
924 }
925 }
926
927 __HAL_UNLOCK(hadc);
928
929 return tmp_hal_status;
930 }
931
932 #if defined(ADC_MULTIMODE_SUPPORT)
933 /**
934 * @brief Enable ADC, start MultiMode conversion and transfer regular results through DMA.
935 * @note Multimode must have been previously configured using
936 * HAL_ADCEx_MultiModeConfigChannel() function.
937 * Interruptions enabled in this function:
938 * overrun, DMA half transfer, DMA transfer complete.
939 * Each of these interruptions has its dedicated callback function.
940 * @note State field of Slave ADC handle is not updated in this configuration:
941 * user should not rely on it for information related to Slave regular
942 * conversions.
943 * @param hadc ADC handle of ADC master (handle of ADC slave must not be used)
944 * @param pData Destination Buffer address.
945 * @param Length Length of data to be transferred from ADC peripheral to memory (in bytes).
946 * @retval HAL status
947 */
HAL_ADCEx_MultiModeStart_DMA(ADC_HandleTypeDef * hadc,uint32_t * pData,uint32_t Length)948 HAL_StatusTypeDef HAL_ADCEx_MultiModeStart_DMA(ADC_HandleTypeDef *hadc, uint32_t *pData, uint32_t Length)
949 {
950 HAL_StatusTypeDef tmp_hal_status;
951 ADC_HandleTypeDef tmphadcSlave={0};
952 ADC_Common_TypeDef *tmpADC_Common;
953
954 /* Check the parameters */
955 assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
956 assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
957 assert_param(IS_ADC_EXTTRIG_EDGE(hadc->Init.ExternalTrigConvEdge));
958
959 if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) != 0UL)
960 {
961 return HAL_BUSY;
962 }
963 else
964 {
965 /* Process locked */
966 __HAL_LOCK(hadc);
967
968 /* Temporary handle minimum initialization */
969 __HAL_ADC_RESET_HANDLE_STATE(&tmphadcSlave);
970 ADC_CLEAR_ERRORCODE(&tmphadcSlave);
971
972 /* Set a temporary handle of the ADC slave associated to the ADC master */
973 ADC_MULTI_SLAVE(hadc, &tmphadcSlave);
974
975 if (tmphadcSlave.Instance == NULL)
976 {
977 /* Set ADC state */
978 SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
979
980 __HAL_UNLOCK(hadc);
981
982 return HAL_ERROR;
983 }
984
985 /* Enable the ADC peripherals: master and slave (in case if not already */
986 /* enabled previously) */
987 tmp_hal_status = ADC_Enable(hadc);
988 if (tmp_hal_status == HAL_OK)
989 {
990 tmp_hal_status = ADC_Enable(&tmphadcSlave);
991 }
992
993 /* Start multimode conversion of ADCs pair */
994 if (tmp_hal_status == HAL_OK)
995 {
996 /* Set ADC state */
997 ADC_STATE_CLR_SET(hadc->State,
998 (HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP),
999 HAL_ADC_STATE_REG_BUSY);
1000
1001 /* Set ADC error code to none */
1002 ADC_CLEAR_ERRORCODE(hadc);
1003
1004 /* Set the DMA transfer complete callback */
1005 hadc->DMA_Handle->XferCpltCallback = ADC_DMAConvCplt;
1006
1007 /* Set the DMA half transfer complete callback */
1008 hadc->DMA_Handle->XferHalfCpltCallback = ADC_DMAHalfConvCplt;
1009
1010 /* Set the DMA error callback */
1011 hadc->DMA_Handle->XferErrorCallback = ADC_DMAError ;
1012
1013 /* Pointer to the common control register */
1014 tmpADC_Common = __LL_ADC_COMMON_INSTANCE(hadc->Instance);
1015
1016 /* Manage ADC and DMA start: ADC overrun interruption, DMA start, ADC */
1017 /* start (in case of SW start): */
1018
1019 /* Clear regular group conversion flag and overrun flag */
1020 /* (To ensure of no unknown state from potential previous ADC operations) */
1021 __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
1022
1023 /* Unlock before starting ADC conversions: in case of potential */
1024 /* interruption, to let the process to ADC IRQ Handler. */
1025 __HAL_UNLOCK(hadc);
1026
1027 /* Enable ADC overrun interrupt */
1028 __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);
1029
1030 /* Start the DMA channel */
1031 tmp_hal_status = HAL_DMA_Start_IT(hadc->DMA_Handle, (uint32_t)&tmpADC_Common->CDR, (uint32_t)pData, Length);
1032
1033 /* Enable conversion of regular group. */
1034 /* If software start has been selected, conversion starts immediately. */
1035 /* If external trigger has been selected, conversion will start at next */
1036 /* trigger event. */
1037 /* Start ADC group regular conversion */
1038 LL_ADC_REG_StartConversion(hadc->Instance);
1039 }
1040 else
1041 {
1042 __HAL_UNLOCK(hadc);
1043 }
1044
1045 return tmp_hal_status;
1046 }
1047 }
1048
1049 /**
1050 * @brief Stop multimode ADC conversion, disable ADC DMA transfer, disable ADC peripheral.
1051 * @note Multimode is kept enabled after this function. MultiMode DMA bits
1052 * (MDMA and DMACFG bits of common CCR register) are maintained. To disable
1053 * Multimode (set with HAL_ADCEx_MultiModeConfigChannel()), ADC must be
1054 * reinitialized using HAL_ADC_Init() or HAL_ADC_DeInit(), or the user can
1055 * resort to HAL_ADCEx_DisableMultiMode() API.
1056 * @note In case of DMA configured in circular mode, function
1057 * HAL_ADC_Stop_DMA() must be called after this function with handle of
1058 * ADC slave, to properly disable the DMA channel.
1059 * @param hadc ADC handle of ADC master (handle of ADC slave must not be used)
1060 * @retval HAL status
1061 */
HAL_ADCEx_MultiModeStop_DMA(ADC_HandleTypeDef * hadc)1062 HAL_StatusTypeDef HAL_ADCEx_MultiModeStop_DMA(ADC_HandleTypeDef *hadc)
1063 {
1064 HAL_StatusTypeDef tmp_hal_status;
1065 uint32_t tickstart;
1066 ADC_HandleTypeDef tmphadcSlave={0};
1067 uint32_t tmphadcSlave_conversion_on_going;
1068 HAL_StatusTypeDef tmphadcSlave_disable_status;
1069
1070 /* Check the parameters */
1071 assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
1072
1073 /* Process locked */
1074 __HAL_LOCK(hadc);
1075
1076
1077 /* 1. Stop potential multimode conversion on going, on regular and injected groups */
1078 tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_INJECTED_GROUP);
1079
1080 /* Disable ADC peripheral if conversions are effectively stopped */
1081 if (tmp_hal_status == HAL_OK)
1082 {
1083 /* Temporary handle minimum initialization */
1084 __HAL_ADC_RESET_HANDLE_STATE(&tmphadcSlave);
1085 ADC_CLEAR_ERRORCODE(&tmphadcSlave);
1086
1087 /* Set a temporary handle of the ADC slave associated to the ADC master */
1088 ADC_MULTI_SLAVE(hadc, &tmphadcSlave);
1089
1090 if (tmphadcSlave.Instance == NULL)
1091 {
1092 /* Update ADC state machine to error */
1093 SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
1094
1095 __HAL_UNLOCK(hadc);
1096
1097 return HAL_ERROR;
1098 }
1099
1100 /* Procedure to disable the ADC peripheral: wait for conversions */
1101 /* effectively stopped (ADC master and ADC slave), then disable ADC */
1102
1103 /* 1. Wait for ADC conversion completion for ADC master and ADC slave */
1104 tickstart = HAL_GetTick();
1105
1106 tmphadcSlave_conversion_on_going = LL_ADC_REG_IsConversionOngoing((&tmphadcSlave)->Instance);
1107 while ((LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 1UL)
1108 || (tmphadcSlave_conversion_on_going == 1UL)
1109 )
1110 {
1111 if ((HAL_GetTick() - tickstart) > ADC_STOP_CONVERSION_TIMEOUT)
1112 {
1113 /* New check to avoid false timeout detection in case of preemption */
1114 tmphadcSlave_conversion_on_going = LL_ADC_REG_IsConversionOngoing((&tmphadcSlave)->Instance);
1115 if ((LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 1UL)
1116 || (tmphadcSlave_conversion_on_going == 1UL)
1117 )
1118 {
1119 /* Update ADC state machine to error */
1120 SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
1121
1122 __HAL_UNLOCK(hadc);
1123
1124 return HAL_ERROR;
1125 }
1126 }
1127
1128 tmphadcSlave_conversion_on_going = LL_ADC_REG_IsConversionOngoing((&tmphadcSlave)->Instance);
1129 }
1130
1131 /* Disable the DMA channel (in case of DMA in circular mode or stop */
1132 /* while DMA transfer is on going) */
1133 /* Note: DMA channel of ADC slave should be stopped after this function */
1134 /* with HAL_ADC_Stop_DMA() API. */
1135 tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
1136
1137 /* Check if DMA channel effectively disabled */
1138 if (tmp_hal_status == HAL_ERROR)
1139 {
1140 /* Update ADC state machine to error */
1141 SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
1142 }
1143
1144 /* Disable ADC overrun interrupt */
1145 __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
1146
1147 /* 2. Disable the ADC peripherals: master and slave */
1148 /* Update "tmp_hal_status" only if DMA channel disabling passed, to keep in */
1149 /* memory a potential failing status. */
1150 if (tmp_hal_status == HAL_OK)
1151 {
1152 tmphadcSlave_disable_status = ADC_Disable(&tmphadcSlave);
1153 if ((ADC_Disable(hadc) == HAL_OK) &&
1154 (tmphadcSlave_disable_status == HAL_OK))
1155 {
1156 tmp_hal_status = HAL_OK;
1157 }
1158 }
1159 else
1160 {
1161 /* In case of error, attempt to disable ADC master and slave without status assert */
1162 (void) ADC_Disable(hadc);
1163 (void) ADC_Disable(&tmphadcSlave);
1164 }
1165
1166 /* Set ADC state (ADC master) */
1167 ADC_STATE_CLR_SET(hadc->State,
1168 HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
1169 HAL_ADC_STATE_READY);
1170 }
1171
1172 __HAL_UNLOCK(hadc);
1173
1174 return tmp_hal_status;
1175 }
1176
1177 /**
1178 * @brief Return the last ADC Master and Slave regular conversions results when in multimode configuration.
1179 * @param hadc ADC handle of ADC Master (handle of ADC Slave must not be used)
1180 * @retval The converted data values.
1181 */
HAL_ADCEx_MultiModeGetValue(ADC_HandleTypeDef * hadc)1182 uint32_t HAL_ADCEx_MultiModeGetValue(ADC_HandleTypeDef *hadc)
1183 {
1184 const ADC_Common_TypeDef *tmpADC_Common;
1185
1186 /* Check the parameters */
1187 assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
1188
1189 /* Prevent unused argument(s) compilation warning if no assert_param check */
1190 /* and possible no usage in __LL_ADC_COMMON_INSTANCE() below */
1191 UNUSED(hadc);
1192
1193 /* Pointer to the common control register */
1194 tmpADC_Common = __LL_ADC_COMMON_INSTANCE(hadc->Instance);
1195
1196 /* Return the multi mode conversion value */
1197 return tmpADC_Common->CDR;
1198 }
1199 #endif /* ADC_MULTIMODE_SUPPORT */
1200
1201 /**
1202 * @brief Get ADC injected group conversion result.
1203 * @note Reading register JDRx automatically clears ADC flag JEOC
1204 * (ADC group injected end of unitary conversion).
1205 * @note This function does not clear ADC flag JEOS
1206 * (ADC group injected end of sequence conversion)
1207 * Occurrence of flag JEOS rising:
1208 * - If sequencer is composed of 1 rank, flag JEOS is equivalent
1209 * to flag JEOC.
1210 * - If sequencer is composed of several ranks, during the scan
1211 * sequence flag JEOC only is raised, at the end of the scan sequence
1212 * both flags JEOC and EOS are raised.
1213 * Flag JEOS must not be cleared by this function because
1214 * it would not be compliant with low power features
1215 * (feature low power auto-wait, not available on all STM32 families).
1216 * To clear this flag, either use function:
1217 * in programming model IT: @ref HAL_ADC_IRQHandler(), in programming
1218 * model polling: @ref HAL_ADCEx_InjectedPollForConversion()
1219 * or @ref __HAL_ADC_CLEAR_FLAG(&hadc, ADC_FLAG_JEOS).
1220 * @param hadc ADC handle
1221 * @param InjectedRank the converted ADC injected rank.
1222 * This parameter can be one of the following values:
1223 * @arg @ref ADC_INJECTED_RANK_1 ADC group injected rank 1
1224 * @arg @ref ADC_INJECTED_RANK_2 ADC group injected rank 2
1225 * @arg @ref ADC_INJECTED_RANK_3 ADC group injected rank 3
1226 * @arg @ref ADC_INJECTED_RANK_4 ADC group injected rank 4
1227 * @retval ADC group injected conversion data
1228 */
HAL_ADCEx_InjectedGetValue(ADC_HandleTypeDef * hadc,uint32_t InjectedRank)1229 uint32_t HAL_ADCEx_InjectedGetValue(ADC_HandleTypeDef *hadc, uint32_t InjectedRank)
1230 {
1231 uint32_t tmp_jdr;
1232
1233 /* Check the parameters */
1234 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
1235 assert_param(IS_ADC_INJECTED_RANK(InjectedRank));
1236
1237 /* Get ADC converted value */
1238 switch (InjectedRank)
1239 {
1240 case ADC_INJECTED_RANK_4:
1241 tmp_jdr = hadc->Instance->JDR4;
1242 break;
1243 case ADC_INJECTED_RANK_3:
1244 tmp_jdr = hadc->Instance->JDR3;
1245 break;
1246 case ADC_INJECTED_RANK_2:
1247 tmp_jdr = hadc->Instance->JDR2;
1248 break;
1249 case ADC_INJECTED_RANK_1:
1250 default:
1251 tmp_jdr = hadc->Instance->JDR1;
1252 break;
1253 }
1254
1255 /* Return ADC converted value */
1256 return tmp_jdr;
1257 }
1258
1259 /**
1260 * @brief Injected conversion complete callback in non-blocking mode.
1261 * @param hadc ADC handle
1262 * @retval None
1263 */
HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef * hadc)1264 __weak void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef *hadc)
1265 {
1266 /* Prevent unused argument(s) compilation warning */
1267 UNUSED(hadc);
1268
1269 /* NOTE : This function should not be modified. When the callback is needed,
1270 function HAL_ADCEx_InjectedConvCpltCallback must be implemented in the user file.
1271 */
1272 }
1273
1274 /**
1275 * @brief Injected context queue overflow callback.
1276 * @note This callback is called if injected context queue is enabled
1277 (parameter "QueueInjectedContext" in injected channel configuration)
1278 and if a new injected context is set when queue is full (maximum 2
1279 contexts).
1280 * @param hadc ADC handle
1281 * @retval None
1282 */
HAL_ADCEx_InjectedQueueOverflowCallback(ADC_HandleTypeDef * hadc)1283 __weak void HAL_ADCEx_InjectedQueueOverflowCallback(ADC_HandleTypeDef *hadc)
1284 {
1285 /* Prevent unused argument(s) compilation warning */
1286 UNUSED(hadc);
1287
1288 /* NOTE : This function should not be modified. When the callback is needed,
1289 function HAL_ADCEx_InjectedQueueOverflowCallback must be implemented in the user file.
1290 */
1291 }
1292
1293 /**
1294 * @brief Analog watchdog 2 callback in non-blocking mode.
1295 * @param hadc ADC handle
1296 * @retval None
1297 */
HAL_ADCEx_LevelOutOfWindow2Callback(ADC_HandleTypeDef * hadc)1298 __weak void HAL_ADCEx_LevelOutOfWindow2Callback(ADC_HandleTypeDef *hadc)
1299 {
1300 /* Prevent unused argument(s) compilation warning */
1301 UNUSED(hadc);
1302
1303 /* NOTE : This function should not be modified. When the callback is needed,
1304 function HAL_ADCEx_LevelOutOfWindow2Callback must be implemented in the user file.
1305 */
1306 }
1307
1308 /**
1309 * @brief Analog watchdog 3 callback in non-blocking mode.
1310 * @param hadc ADC handle
1311 * @retval None
1312 */
HAL_ADCEx_LevelOutOfWindow3Callback(ADC_HandleTypeDef * hadc)1313 __weak void HAL_ADCEx_LevelOutOfWindow3Callback(ADC_HandleTypeDef *hadc)
1314 {
1315 /* Prevent unused argument(s) compilation warning */
1316 UNUSED(hadc);
1317
1318 /* NOTE : This function should not be modified. When the callback is needed,
1319 function HAL_ADCEx_LevelOutOfWindow3Callback must be implemented in the user file.
1320 */
1321 }
1322
1323
1324 /**
1325 * @brief End Of Sampling callback in non-blocking mode.
1326 * @param hadc ADC handle
1327 * @retval None
1328 */
HAL_ADCEx_EndOfSamplingCallback(ADC_HandleTypeDef * hadc)1329 __weak void HAL_ADCEx_EndOfSamplingCallback(ADC_HandleTypeDef *hadc)
1330 {
1331 /* Prevent unused argument(s) compilation warning */
1332 UNUSED(hadc);
1333
1334 /* NOTE : This function should not be modified. When the callback is needed,
1335 function HAL_ADCEx_EndOfSamplingCallback must be implemented in the user file.
1336 */
1337 }
1338
1339 /**
1340 * @brief Stop ADC conversion of regular group (and injected channels in
1341 * case of auto_injection mode), disable ADC peripheral if no
1342 * conversion is on going on injected group.
1343 * @param hadc ADC handle
1344 * @retval HAL status.
1345 */
HAL_ADCEx_RegularStop(ADC_HandleTypeDef * hadc)1346 HAL_StatusTypeDef HAL_ADCEx_RegularStop(ADC_HandleTypeDef *hadc)
1347 {
1348 HAL_StatusTypeDef tmp_hal_status;
1349
1350 /* Check the parameters */
1351 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
1352
1353 /* Process locked */
1354 __HAL_LOCK(hadc);
1355
1356 /* 1. Stop potential regular conversion on going */
1357 tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_GROUP);
1358
1359 /* Disable ADC peripheral if regular conversions are effectively stopped
1360 and if no injected conversions are on-going */
1361 if (tmp_hal_status == HAL_OK)
1362 {
1363 /* Clear HAL_ADC_STATE_REG_BUSY bit */
1364 CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
1365
1366 if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) == 0UL)
1367 {
1368 /* 2. Disable the ADC peripheral */
1369 tmp_hal_status = ADC_Disable(hadc);
1370
1371 /* Check if ADC is effectively disabled */
1372 if (tmp_hal_status == HAL_OK)
1373 {
1374 /* Set ADC state */
1375 ADC_STATE_CLR_SET(hadc->State,
1376 HAL_ADC_STATE_INJ_BUSY,
1377 HAL_ADC_STATE_READY);
1378 }
1379 }
1380 /* Conversion on injected group is stopped, but ADC not disabled since */
1381 /* conversion on regular group is still running. */
1382 else
1383 {
1384 SET_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
1385 }
1386 }
1387
1388 __HAL_UNLOCK(hadc);
1389
1390 return tmp_hal_status;
1391 }
1392
1393
1394 /**
1395 * @brief Stop ADC conversion of ADC groups regular and injected,
1396 * disable interrution of end-of-conversion,
1397 * disable ADC peripheral if no conversion is on going
1398 * on injected group.
1399 * @param hadc ADC handle
1400 * @retval HAL status.
1401 */
HAL_ADCEx_RegularStop_IT(ADC_HandleTypeDef * hadc)1402 HAL_StatusTypeDef HAL_ADCEx_RegularStop_IT(ADC_HandleTypeDef *hadc)
1403 {
1404 HAL_StatusTypeDef tmp_hal_status;
1405
1406 /* Check the parameters */
1407 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
1408
1409 /* Process locked */
1410 __HAL_LOCK(hadc);
1411
1412 /* 1. Stop potential regular conversion on going */
1413 tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_GROUP);
1414
1415 /* Disable ADC peripheral if conversions are effectively stopped
1416 and if no injected conversion is on-going */
1417 if (tmp_hal_status == HAL_OK)
1418 {
1419 /* Clear HAL_ADC_STATE_REG_BUSY bit */
1420 CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
1421
1422 /* Disable all regular-related interrupts */
1423 __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_EOS | ADC_IT_OVR));
1424
1425 /* 2. Disable ADC peripheral if no injected conversions are on-going */
1426 if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) == 0UL)
1427 {
1428 tmp_hal_status = ADC_Disable(hadc);
1429 /* if no issue reported */
1430 if (tmp_hal_status == HAL_OK)
1431 {
1432 /* Set ADC state */
1433 ADC_STATE_CLR_SET(hadc->State,
1434 HAL_ADC_STATE_INJ_BUSY,
1435 HAL_ADC_STATE_READY);
1436 }
1437 }
1438 else
1439 {
1440 SET_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
1441 }
1442 }
1443
1444 __HAL_UNLOCK(hadc);
1445
1446 return tmp_hal_status;
1447 }
1448
1449 /**
1450 * @brief Stop ADC conversion of regular group (and injected group in
1451 * case of auto_injection mode), disable ADC DMA transfer, disable
1452 * ADC peripheral if no conversion is on going
1453 * on injected group.
1454 * @note HAL_ADCEx_RegularStop_DMA() function is dedicated to single-ADC mode only.
1455 * For multimode (when multimode feature is available),
1456 * HAL_ADCEx_RegularMultiModeStop_DMA() API must be used.
1457 * @param hadc ADC handle
1458 * @retval HAL status.
1459 */
HAL_ADCEx_RegularStop_DMA(ADC_HandleTypeDef * hadc)1460 HAL_StatusTypeDef HAL_ADCEx_RegularStop_DMA(ADC_HandleTypeDef *hadc)
1461 {
1462 HAL_StatusTypeDef tmp_hal_status;
1463
1464 /* Check the parameters */
1465 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
1466
1467 /* Process locked */
1468 __HAL_LOCK(hadc);
1469
1470 /* 1. Stop potential regular conversion on going */
1471 tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_GROUP);
1472
1473 /* Disable ADC peripheral if conversions are effectively stopped
1474 and if no injected conversion is on-going */
1475 if (tmp_hal_status == HAL_OK)
1476 {
1477 /* Clear HAL_ADC_STATE_REG_BUSY bit */
1478 CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
1479
1480 /* Disable ADC DMA (ADC DMA configuration ADC_CFGR_DMACFG is kept) */
1481 MODIFY_REG(hadc->Instance->CFGR, ADC_CFGR_DMNGT_0 |ADC_CFGR_DMNGT_1, 0UL);
1482
1483 /* Disable the DMA channel (in case of DMA in circular mode or stop while */
1484 /* while DMA transfer is on going) */
1485 tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
1486
1487 /* Check if DMA channel effectively disabled */
1488 if (tmp_hal_status != HAL_OK)
1489 {
1490 /* Update ADC state machine to error */
1491 SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
1492 }
1493
1494 /* Disable ADC overrun interrupt */
1495 __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
1496
1497 /* 2. Disable the ADC peripheral */
1498 /* Update "tmp_hal_status" only if DMA channel disabling passed, */
1499 /* to keep in memory a potential failing status. */
1500 if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) == 0UL)
1501 {
1502 if (tmp_hal_status == HAL_OK)
1503 {
1504 tmp_hal_status = ADC_Disable(hadc);
1505 }
1506 else
1507 {
1508 (void)ADC_Disable(hadc);
1509 }
1510
1511 /* Check if ADC is effectively disabled */
1512 if (tmp_hal_status == HAL_OK)
1513 {
1514 /* Set ADC state */
1515 ADC_STATE_CLR_SET(hadc->State,
1516 HAL_ADC_STATE_INJ_BUSY,
1517 HAL_ADC_STATE_READY);
1518 }
1519 }
1520 else
1521 {
1522 SET_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
1523 }
1524 }
1525
1526 __HAL_UNLOCK(hadc);
1527
1528 return tmp_hal_status;
1529 }
1530
1531 #if defined(ADC_MULTIMODE_SUPPORT)
1532 /**
1533 * @brief Stop DMA-based multimode ADC conversion, disable ADC DMA transfer, disable ADC peripheral if no injected conversion is on-going.
1534 * @note Multimode is kept enabled after this function. Multimode DMA bits
1535 * (MDMA and DMACFG bits of common CCR register) are maintained. To disable
1536 * multimode (set with HAL_ADCEx_MultiModeConfigChannel()), ADC must be
1537 * reinitialized using HAL_ADC_Init() or HAL_ADC_DeInit(), or the user can
1538 * resort to HAL_ADCEx_DisableMultiMode() API.
1539 * @note In case of DMA configured in circular mode, function
1540 * HAL_ADCEx_RegularStop_DMA() must be called after this function with handle of
1541 * ADC slave, to properly disable the DMA channel.
1542 * @param hadc ADC handle of ADC master (handle of ADC slave must not be used)
1543 * @retval HAL status
1544 */
HAL_ADCEx_RegularMultiModeStop_DMA(ADC_HandleTypeDef * hadc)1545 HAL_StatusTypeDef HAL_ADCEx_RegularMultiModeStop_DMA(ADC_HandleTypeDef *hadc)
1546 {
1547 HAL_StatusTypeDef tmp_hal_status;
1548 uint32_t tickstart;
1549 ADC_HandleTypeDef tmphadcSlave={0};
1550 uint32_t tmphadcSlave_conversion_on_going;
1551
1552 /* Check the parameters */
1553 assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
1554
1555 /* Process locked */
1556 __HAL_LOCK(hadc);
1557
1558
1559 /* 1. Stop potential multimode conversion on going, on regular groups */
1560 tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_GROUP);
1561
1562 /* Disable ADC peripheral if conversions are effectively stopped */
1563 if (tmp_hal_status == HAL_OK)
1564 {
1565 /* Clear HAL_ADC_STATE_REG_BUSY bit */
1566 CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
1567
1568 /* Temporary handle minimum initialization */
1569 __HAL_ADC_RESET_HANDLE_STATE(&tmphadcSlave);
1570 ADC_CLEAR_ERRORCODE(&tmphadcSlave);
1571
1572 /* Set a temporary handle of the ADC slave associated to the ADC master */
1573 ADC_MULTI_SLAVE(hadc, &tmphadcSlave);
1574
1575 if (tmphadcSlave.Instance == NULL)
1576 {
1577 /* Update ADC state machine to error */
1578 SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
1579
1580 __HAL_UNLOCK(hadc);
1581
1582 return HAL_ERROR;
1583 }
1584
1585 /* Procedure to disable the ADC peripheral: wait for conversions */
1586 /* effectively stopped (ADC master and ADC slave), then disable ADC */
1587
1588 /* 1. Wait for ADC conversion completion for ADC master and ADC slave */
1589 tickstart = HAL_GetTick();
1590
1591 tmphadcSlave_conversion_on_going = LL_ADC_REG_IsConversionOngoing((&tmphadcSlave)->Instance);
1592 while ((LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 1UL)
1593 || (tmphadcSlave_conversion_on_going == 1UL)
1594 )
1595 {
1596 if ((HAL_GetTick() - tickstart) > ADC_STOP_CONVERSION_TIMEOUT)
1597 {
1598 /* New check to avoid false timeout detection in case of preemption */
1599 tmphadcSlave_conversion_on_going = LL_ADC_REG_IsConversionOngoing((&tmphadcSlave)->Instance);
1600 if ((LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 1UL)
1601 || (tmphadcSlave_conversion_on_going == 1UL)
1602 )
1603 {
1604 /* Update ADC state machine to error */
1605 SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
1606
1607 __HAL_UNLOCK(hadc);
1608
1609 return HAL_ERROR;
1610 }
1611 }
1612
1613 tmphadcSlave_conversion_on_going = LL_ADC_REG_IsConversionOngoing((&tmphadcSlave)->Instance);
1614 }
1615
1616 /* Disable the DMA channel (in case of DMA in circular mode or stop */
1617 /* while DMA transfer is on going) */
1618 /* Note: DMA channel of ADC slave should be stopped after this function */
1619 /* with HAL_ADCEx_RegularStop_DMA() API. */
1620 tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
1621
1622 /* Check if DMA channel effectively disabled */
1623 if (tmp_hal_status != HAL_OK)
1624 {
1625 /* Update ADC state machine to error */
1626 SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
1627 }
1628
1629 /* Disable ADC overrun interrupt */
1630 __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
1631
1632 /* 2. Disable the ADC peripherals: master and slave if no injected */
1633 /* conversion is on-going. */
1634 /* Update "tmp_hal_status" only if DMA channel disabling passed, to keep in */
1635 /* memory a potential failing status. */
1636 if (tmp_hal_status == HAL_OK)
1637 {
1638 if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) == 0UL)
1639 {
1640 tmp_hal_status = ADC_Disable(hadc);
1641 if (tmp_hal_status == HAL_OK)
1642 {
1643 if (LL_ADC_INJ_IsConversionOngoing((&tmphadcSlave)->Instance) == 0UL)
1644 {
1645 tmp_hal_status = ADC_Disable(&tmphadcSlave);
1646 }
1647 }
1648 }
1649
1650 if (tmp_hal_status == HAL_OK)
1651 {
1652 /* Both Master and Slave ADC's could be disabled. Update Master State */
1653 /* Clear HAL_ADC_STATE_INJ_BUSY bit, set HAL_ADC_STATE_READY bit */
1654 ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_BUSY, HAL_ADC_STATE_READY);
1655 }
1656 else
1657 {
1658 /* injected (Master or Slave) conversions are still on-going,
1659 no Master State change */
1660 }
1661 }
1662 }
1663
1664 __HAL_UNLOCK(hadc);
1665
1666 return tmp_hal_status;
1667 }
1668 #endif /* ADC_MULTIMODE_SUPPORT */
1669
1670 /**
1671 * @}
1672 */
1673
1674 /** @defgroup ADCEx_Exported_Functions_Group2 ADC Extended Peripheral Control functions
1675 * @brief ADC Extended Peripheral Control functions
1676 *
1677 @verbatim
1678 ===============================================================================
1679 ##### Peripheral Control functions #####
1680 ===============================================================================
1681 [..] This section provides functions allowing to:
1682 (+) Configure channels on injected group
1683 (+) Configure multimode when multimode feature is available
1684 (+) Enable or Disable Injected Queue
1685 (+) Disable ADC voltage regulator
1686 (+) Enter ADC deep-power-down mode
1687
1688 @endverbatim
1689 * @{
1690 */
1691
1692 /**
1693 * @brief Configure a channel to be assigned to ADC group injected.
1694 * @note Possibility to update parameters on the fly:
1695 * This function initializes injected group, following calls to this
1696 * function can be used to reconfigure some parameters of structure
1697 * "ADC_InjectionConfTypeDef" on the fly, without resetting the ADC.
1698 * The setting of these parameters is conditioned to ADC state:
1699 * Refer to comments of structure "ADC_InjectionConfTypeDef".
1700 * @note In case of usage of internal measurement channels:
1701 * Vbat/VrefInt/TempSensor/VddCore.
1702 * These internal paths can be disabled using function
1703 * HAL_ADC_DeInit().
1704 * @note Caution: For Injected Context Queue use, a context must be fully
1705 * defined before start of injected conversion. All channels are configured
1706 * consecutively for the same ADC instance. Therefore, the number of calls to
1707 * HAL_ADCEx_InjectedConfigChannel() must be equal to the value of parameter
1708 * InjectedNbrOfConversion for each context.
1709 * - Example 1: If 1 context is intended to be used (or if there is no use of the
1710 * Injected Queue Context feature) and if the context contains 3 injected ranks
1711 * (InjectedNbrOfConversion = 3), HAL_ADCEx_InjectedConfigChannel() must be
1712 * called once for each channel (i.e. 3 times) before starting a conversion.
1713 * This function must not be called to configure a 4th injected channel:
1714 * it would start a new context into context queue.
1715 * - Example 2: If 2 contexts are intended to be used and each of them contains
1716 * 3 injected ranks (InjectedNbrOfConversion = 3),
1717 * HAL_ADCEx_InjectedConfigChannel() must be called once for each channel and
1718 * for each context (3 channels x 2 contexts = 6 calls). Conversion can
1719 * start once the 1st context is set, that is after the first three
1720 * HAL_ADCEx_InjectedConfigChannel() calls. The 2nd context can be set on the fly.
1721 * @param hadc ADC handle
1722 * @param pConfigInjected Structure of ADC injected group and ADC channel for
1723 * injected group.
1724 * @retval HAL status
1725 */
HAL_ADCEx_InjectedConfigChannel(ADC_HandleTypeDef * hadc,ADC_InjectionConfTypeDef * pConfigInjected)1726 HAL_StatusTypeDef HAL_ADCEx_InjectedConfigChannel(ADC_HandleTypeDef *hadc, ADC_InjectionConfTypeDef *pConfigInjected)
1727 {
1728 HAL_StatusTypeDef tmp_hal_status = HAL_OK;
1729 uint32_t tmpOffsetShifted;
1730 uint32_t tmp_config_internal_channel;
1731 uint32_t tmp_adc_is_conversion_on_going_regular;
1732 uint32_t tmp_adc_is_conversion_on_going_injected;
1733 __IO uint32_t wait_loop_index = 0;
1734
1735 uint32_t tmp_JSQR_ContextQueueBeingBuilt = 0U;
1736
1737 /* Check the parameters */
1738 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
1739 assert_param(IS_ADC_SAMPLE_TIME(pConfigInjected->InjectedSamplingTime));
1740 assert_param(IS_ADC_SINGLE_DIFFERENTIAL(pConfigInjected->InjectedSingleDiff));
1741 assert_param(IS_FUNCTIONAL_STATE(pConfigInjected->AutoInjectedConv));
1742 assert_param(IS_FUNCTIONAL_STATE(pConfigInjected->QueueInjectedContext));
1743 assert_param(IS_ADC_EXTTRIGINJEC_EDGE(pConfigInjected->ExternalTrigInjecConvEdge));
1744 assert_param(IS_ADC_EXTTRIGINJEC(pConfigInjected->ExternalTrigInjecConv));
1745 assert_param(IS_ADC_OFFSET_NUMBER(pConfigInjected->InjectedOffsetNumber));
1746 assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), pConfigInjected->InjectedOffset));
1747 assert_param(IS_FUNCTIONAL_STATE(pConfigInjected->InjecOversamplingMode));
1748
1749 if (hadc->Init.ScanConvMode != ADC_SCAN_DISABLE)
1750 {
1751 assert_param(IS_ADC_INJECTED_RANK(pConfigInjected->InjectedRank));
1752 assert_param(IS_ADC_INJECTED_NB_CONV(pConfigInjected->InjectedNbrOfConversion));
1753 assert_param(IS_FUNCTIONAL_STATE(pConfigInjected->InjectedDiscontinuousConvMode));
1754 }
1755
1756 /* Check offset range according to oversampling setting */
1757 if (hadc->Init.OversamplingMode == ENABLE)
1758 {
1759 assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), pConfigInjected->InjectedOffset/(hadc->Init.Oversampling.Ratio+1U)));
1760 }
1761 else
1762 {
1763 assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), pConfigInjected->InjectedOffset));
1764 }
1765
1766 /* JDISCEN and JAUTO bits can't be set at the same time */
1767 assert_param(!((pConfigInjected->InjectedDiscontinuousConvMode == ENABLE) && (pConfigInjected->AutoInjectedConv == ENABLE)));
1768
1769 /* DISCEN and JAUTO bits can't be set at the same time */
1770 assert_param(!((hadc->Init.DiscontinuousConvMode == ENABLE) && (pConfigInjected->AutoInjectedConv == ENABLE)));
1771
1772 /* Verification of channel number */
1773 if (pConfigInjected->InjectedSingleDiff != ADC_DIFFERENTIAL_ENDED)
1774 {
1775 assert_param(IS_ADC_CHANNEL(pConfigInjected->InjectedChannel));
1776 }
1777 else
1778 {
1779 if (hadc->Instance == ADC1)
1780 {
1781 assert_param(IS_ADC1_DIFF_CHANNEL(pConfigInjected->InjectedChannel));
1782 }
1783 if (hadc->Instance == ADC2)
1784 {
1785 assert_param(IS_ADC2_DIFF_CHANNEL(pConfigInjected->InjectedChannel));
1786 }
1787 }
1788
1789 /* Process locked */
1790 __HAL_LOCK(hadc);
1791
1792 /* Configuration of injected group sequencer: */
1793 /* Hardware constraint: Must fully define injected context register JSQR */
1794 /* before make it entering into injected sequencer queue. */
1795 /* */
1796 /* - if scan mode is disabled: */
1797 /* * Injected channels sequence length is set to 0x00: 1 channel */
1798 /* converted (channel on injected rank 1) */
1799 /* Parameter "InjectedNbrOfConversion" is discarded. */
1800 /* * Injected context register JSQR setting is simple: register is fully */
1801 /* defined on one call of this function (for injected rank 1) and can */
1802 /* be entered into queue directly. */
1803 /* - if scan mode is enabled: */
1804 /* * Injected channels sequence length is set to parameter */
1805 /* "InjectedNbrOfConversion". */
1806 /* * Injected context register JSQR setting more complex: register is */
1807 /* fully defined over successive calls of this function, for each */
1808 /* injected channel rank. It is entered into queue only when all */
1809 /* injected ranks have been set. */
1810 /* Note: Scan mode is not present by hardware on this device, but used */
1811 /* by software for alignment over all STM32 devices. */
1812
1813 if ((hadc->Init.ScanConvMode == ADC_SCAN_DISABLE) ||
1814 (pConfigInjected->InjectedNbrOfConversion == 1U))
1815 {
1816 /* Configuration of context register JSQR: */
1817 /* - number of ranks in injected group sequencer: fixed to 1st rank */
1818 /* (scan mode disabled, only rank 1 used) */
1819 /* - external trigger to start conversion */
1820 /* - external trigger polarity */
1821 /* - channel set to rank 1 (scan mode disabled, only rank 1 can be used) */
1822
1823 if (pConfigInjected->InjectedRank == ADC_INJECTED_RANK_1)
1824 {
1825 /* Enable external trigger if trigger selection is different of */
1826 /* software start. */
1827 /* Note: This configuration keeps the hardware feature of parameter */
1828 /* ExternalTrigInjecConvEdge "trigger edge none" equivalent to */
1829 /* software start. */
1830 if (pConfigInjected->ExternalTrigInjecConv != ADC_INJECTED_SOFTWARE_START)
1831 {
1832 tmp_JSQR_ContextQueueBeingBuilt = (ADC_JSQR_RK(pConfigInjected->InjectedChannel, ADC_INJECTED_RANK_1)
1833 | (pConfigInjected->ExternalTrigInjecConv & ADC_JSQR_JEXTSEL)
1834 | pConfigInjected->ExternalTrigInjecConvEdge
1835 );
1836 }
1837 else
1838 {
1839 tmp_JSQR_ContextQueueBeingBuilt = (ADC_JSQR_RK(pConfigInjected->InjectedChannel, ADC_INJECTED_RANK_1));
1840 }
1841
1842 MODIFY_REG(hadc->Instance->JSQR, ADC_JSQR_FIELDS, tmp_JSQR_ContextQueueBeingBuilt);
1843 /* For debug and informative reasons, hadc handle saves JSQR setting */
1844 hadc->InjectionConfig.ContextQueue = tmp_JSQR_ContextQueueBeingBuilt;
1845
1846 }
1847 }
1848 else
1849 {
1850 /* Case of scan mode enabled, several channels to set into injected group */
1851 /* sequencer. */
1852 /* */
1853 /* Procedure to define injected context register JSQR over successive */
1854 /* calls of this function, for each injected channel rank: */
1855 /* 1. Start new context and set parameters related to all injected */
1856 /* channels: injected sequence length and trigger. */
1857
1858 /* if hadc->InjectionConfig.ChannelCount is equal to 0, this is the first */
1859 /* call of the context under setting */
1860 if (hadc->InjectionConfig.ChannelCount == 0U)
1861 {
1862 /* Initialize number of channels that will be configured on the context */
1863 /* being built */
1864 hadc->InjectionConfig.ChannelCount = pConfigInjected->InjectedNbrOfConversion;
1865 /* Handle hadc saves the context under build up over each HAL_ADCEx_InjectedConfigChannel()
1866 call, this context will be written in JSQR register at the last call.
1867 At this point, the context is merely reset */
1868 hadc->InjectionConfig.ContextQueue = 0x00000000U;
1869
1870 /* Configuration of context register JSQR: */
1871 /* - number of ranks in injected group sequencer */
1872 /* - external trigger to start conversion */
1873 /* - external trigger polarity */
1874
1875 /* Enable external trigger if trigger selection is different of */
1876 /* software start. */
1877 /* Note: This configuration keeps the hardware feature of parameter */
1878 /* ExternalTrigInjecConvEdge "trigger edge none" equivalent to */
1879 /* software start. */
1880 if (pConfigInjected->ExternalTrigInjecConv != ADC_INJECTED_SOFTWARE_START)
1881 {
1882 tmp_JSQR_ContextQueueBeingBuilt = ((pConfigInjected->InjectedNbrOfConversion - 1U)
1883 | (pConfigInjected->ExternalTrigInjecConv & ADC_JSQR_JEXTSEL)
1884 | pConfigInjected->ExternalTrigInjecConvEdge
1885 );
1886 }
1887 else
1888 {
1889 tmp_JSQR_ContextQueueBeingBuilt = ((pConfigInjected->InjectedNbrOfConversion - 1U));
1890 }
1891
1892 }
1893
1894 /* 2. Continue setting of context under definition with parameter */
1895 /* related to each channel: channel rank sequence */
1896 /* Clear the old JSQx bits for the selected rank */
1897 tmp_JSQR_ContextQueueBeingBuilt &= ~ADC_JSQR_RK(ADC_SQR3_SQ10, pConfigInjected->InjectedRank);
1898
1899 /* Set the JSQx bits for the selected rank */
1900 tmp_JSQR_ContextQueueBeingBuilt |= ADC_JSQR_RK(pConfigInjected->InjectedChannel, pConfigInjected->InjectedRank);
1901
1902 /* Decrease channel count */
1903 hadc->InjectionConfig.ChannelCount--;
1904
1905 /* 3. tmp_JSQR_ContextQueueBeingBuilt is fully built for this HAL_ADCEx_InjectedConfigChannel()
1906 call, aggregate the setting to those already built during the previous
1907 HAL_ADCEx_InjectedConfigChannel() calls (for the same context of course) */
1908 hadc->InjectionConfig.ContextQueue |= tmp_JSQR_ContextQueueBeingBuilt;
1909
1910 /* 4. End of context setting: if this is the last channel set, then write context
1911 into register JSQR and make it enter into queue */
1912 if (hadc->InjectionConfig.ChannelCount == 0U)
1913 {
1914 MODIFY_REG(hadc->Instance->JSQR, ADC_JSQR_FIELDS, hadc->InjectionConfig.ContextQueue);
1915 }
1916 }
1917
1918 /* Parameters update conditioned to ADC state: */
1919 /* Parameters that can be updated when ADC is disabled or enabled without */
1920 /* conversion on going on injected group: */
1921 /* - Injected context queue: Queue disable (active context is kept) or */
1922 /* enable (context decremented, up to 2 contexts queued) */
1923 /* - Injected discontinuous mode: can be enabled only if auto-injected */
1924 /* mode is disabled. */
1925 if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) == 0UL)
1926 {
1927 /* ADC channels preselection */
1928 hadc->Instance->PCSEL |= (1UL << (__LL_ADC_CHANNEL_TO_DECIMAL_NB(pConfigInjected->InjectedChannel) & 0x1FUL));
1929
1930 /* If auto-injected mode is disabled: no constraint */
1931 if (pConfigInjected->AutoInjectedConv == DISABLE)
1932 {
1933 MODIFY_REG(hadc->Instance->CFGR,
1934 ADC_CFGR_JQM | ADC_CFGR_JDISCEN,
1935 ADC_CFGR_INJECT_CONTEXT_QUEUE((uint32_t)pConfigInjected->QueueInjectedContext) |
1936 ADC_CFGR_INJECT_DISCCONTINUOUS((uint32_t)pConfigInjected->InjectedDiscontinuousConvMode));
1937 }
1938 /* If auto-injected mode is enabled: Injected discontinuous setting is */
1939 /* discarded. */
1940 else
1941 {
1942 MODIFY_REG(hadc->Instance->CFGR,
1943 ADC_CFGR_JQM | ADC_CFGR_JDISCEN,
1944 ADC_CFGR_INJECT_CONTEXT_QUEUE((uint32_t)pConfigInjected->QueueInjectedContext));
1945 }
1946
1947 }
1948
1949 /* Parameters update conditioned to ADC state: */
1950 /* Parameters that can be updated when ADC is disabled or enabled without */
1951 /* conversion on going on regular and injected groups: */
1952 /* - Automatic injected conversion: can be enabled if injected group */
1953 /* external triggers are disabled. */
1954 /* - Channel sampling time */
1955 /* - Channel offset */
1956 tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
1957 tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
1958
1959 if ((tmp_adc_is_conversion_on_going_regular == 0UL)
1960 && (tmp_adc_is_conversion_on_going_injected == 0UL)
1961 )
1962 {
1963 /* If injected group external triggers are disabled (set to injected */
1964 /* software start): no constraint */
1965 if ((pConfigInjected->ExternalTrigInjecConv == ADC_INJECTED_SOFTWARE_START)
1966 || (pConfigInjected->ExternalTrigInjecConvEdge == ADC_EXTERNALTRIGINJECCONV_EDGE_NONE))
1967 {
1968 if (pConfigInjected->AutoInjectedConv == ENABLE)
1969 {
1970 SET_BIT(hadc->Instance->CFGR, ADC_CFGR_JAUTO);
1971 }
1972 else
1973 {
1974 CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_JAUTO);
1975 }
1976 }
1977 /* If Automatic injected conversion was intended to be set and could not */
1978 /* due to injected group external triggers enabled, error is reported. */
1979 else
1980 {
1981 if (pConfigInjected->AutoInjectedConv == ENABLE)
1982 {
1983 /* Update ADC state machine to error */
1984 SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
1985
1986 tmp_hal_status = HAL_ERROR;
1987 }
1988 else
1989 {
1990 CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_JAUTO);
1991 }
1992 }
1993
1994 if (pConfigInjected->InjecOversamplingMode == ENABLE)
1995 {
1996 assert_param(IS_ADC_OVERSAMPLING_RATIO(pConfigInjected->InjecOversampling.Ratio));
1997 assert_param(IS_ADC_RIGHT_BIT_SHIFT(pConfigInjected->InjecOversampling.RightBitShift));
1998
1999 /* JOVSE must be reset in case of triggered regular mode */
2000 assert_param(!(READ_BIT(hadc->Instance->CFGR2, ADC_CFGR2_ROVSE | ADC_CFGR2_TROVS) == (ADC_CFGR2_ROVSE | ADC_CFGR2_TROVS)));
2001
2002 /* Configuration of Injected Oversampler: */
2003 /* - Oversampling Ratio */
2004 /* - Right bit shift */
2005
2006 /* Enable OverSampling mode */
2007 MODIFY_REG(hadc->Instance->CFGR2,
2008 ADC_CFGR2_JOVSE |
2009 ADC_CFGR2_OVSR |
2010 ADC_CFGR2_OVSS,
2011 ADC_CFGR2_JOVSE |
2012 ((pConfigInjected->InjecOversampling.Ratio - 1UL) << ADC_CFGR2_OSR_Pos) |
2013 pConfigInjected->InjecOversampling.RightBitShift
2014 );
2015 }
2016 else
2017 {
2018 /* Disable Regular OverSampling */
2019 CLEAR_BIT(hadc->Instance->CFGR2, ADC_CFGR2_JOVSE);
2020 }
2021
2022 /* Set sampling time of the selected ADC channel */
2023 LL_ADC_SetChannelSamplingTime(hadc->Instance, pConfigInjected->InjectedChannel, pConfigInjected->InjectedSamplingTime);
2024
2025 /* Configure the offset: offset enable/disable, channel, offset value */
2026
2027 /* Shift the offset with respect to the selected ADC resolution. */
2028 /* Offset has to be left-aligned on bit 11, the LSB (right bits) are set to 0 */
2029 tmpOffsetShifted = ADC_OFFSET_SHIFT_RESOLUTION(hadc, pConfigInjected->InjectedOffset);
2030
2031 if (pConfigInjected->InjectedOffsetNumber != ADC_OFFSET_NONE)
2032 {
2033 /* Set ADC selected offset number */
2034 LL_ADC_SetOffset(hadc->Instance, pConfigInjected->InjectedOffsetNumber, pConfigInjected->InjectedChannel,
2035 tmpOffsetShifted);
2036
2037 /* Set ADC selected offset signed saturation */
2038 LL_ADC_SetOffsetSignedSaturation(hadc->Instance, pConfigInjected->InjectedOffsetNumber, (pConfigInjected->InjectedOffsetSignedSaturation == ENABLE) ? LL_ADC_OFFSET_SIGNED_SATURATION_ENABLE : LL_ADC_OFFSET_SIGNED_SATURATION_DISABLE);
2039
2040 /* Set ADC selected offset right shift */
2041 LL_ADC_SetDataRightShift(hadc->Instance, pConfigInjected->InjectedOffsetNumber, (pConfigInjected->InjectedOffsetRightShift == (uint32_t)ENABLE) ? LL_ADC_OFFSET_RSHIFT_ENABLE : LL_ADC_OFFSET_RSHIFT_DISABLE);
2042
2043 }
2044 else
2045 {
2046 /* Scan each offset register to check if the selected channel is targeted. */
2047 /* If this is the case, the corresponding offset number is disabled. */
2048 if (__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_1))
2049 == __LL_ADC_CHANNEL_TO_DECIMAL_NB(pConfigInjected->InjectedChannel))
2050 {
2051 LL_ADC_SetOffset(hadc->Instance, LL_ADC_OFFSET_1, pConfigInjected->InjectedChannel, LL_ADC_OFFSET_SIGNED_SATURATION_DISABLE);
2052 }
2053 if (__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_2))
2054 == __LL_ADC_CHANNEL_TO_DECIMAL_NB(pConfigInjected->InjectedChannel))
2055 {
2056 LL_ADC_SetOffset(hadc->Instance, LL_ADC_OFFSET_2, pConfigInjected->InjectedChannel, LL_ADC_OFFSET_SIGNED_SATURATION_DISABLE);
2057 }
2058 if (__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_3))
2059 == __LL_ADC_CHANNEL_TO_DECIMAL_NB(pConfigInjected->InjectedChannel))
2060 {
2061 LL_ADC_SetOffset(hadc->Instance, LL_ADC_OFFSET_4, pConfigInjected->InjectedChannel, LL_ADC_OFFSET_SIGNED_SATURATION_DISABLE);
2062 }
2063 if (__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_4))
2064 == __LL_ADC_CHANNEL_TO_DECIMAL_NB(pConfigInjected->InjectedChannel))
2065 {
2066 LL_ADC_SetOffset(hadc->Instance, LL_ADC_OFFSET_4, pConfigInjected->InjectedChannel, LL_ADC_OFFSET_SIGNED_SATURATION_DISABLE);
2067 }
2068 }
2069
2070 }
2071
2072 /* Parameters update conditioned to ADC state: */
2073 /* Parameters that can be updated only when ADC is disabled: */
2074 /* - Single or differential mode */
2075 /* - Internal measurement channels: Vbat/VrefInt/TempSensor/VddCore */
2076 if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
2077 {
2078 /* Set mode single-ended or differential input of the selected ADC channel */
2079 LL_ADC_SetChannelSingleDiff(hadc->Instance, pConfigInjected->InjectedChannel, pConfigInjected->InjectedSingleDiff);
2080
2081 /* Configuration of differential mode */
2082 /* Note: ADC channel number masked with value "0x1F" to ensure shift value within 32 bits range */
2083 if (pConfigInjected->InjectedSingleDiff == ADC_DIFFERENTIAL_ENDED)
2084 {
2085 /* Set sampling time of the selected ADC channel */
2086 LL_ADC_SetChannelSamplingTime(hadc->Instance,
2087 (uint32_t)(__LL_ADC_DECIMAL_NB_TO_CHANNEL((__LL_ADC_CHANNEL_TO_DECIMAL_NB((uint32_t)pConfigInjected->InjectedChannel)
2088 + 1UL) & 0x1FUL)), pConfigInjected->InjectedSamplingTime);
2089 }
2090
2091 /* Management of internal measurement channels: Vbat/VrefInt/TempSensor/VddCore */
2092 /* internal measurement paths enable: If internal channel selected, */
2093 /* enable dedicated internal buffers and path. */
2094 /* Note: these internal measurement paths can be disabled using */
2095 /* HAL_ADC_DeInit(). */
2096
2097 if (__LL_ADC_IS_CHANNEL_INTERNAL(pConfigInjected->InjectedChannel))
2098 {
2099 /* Configuration of common ADC parameters (continuation) */
2100 /* Software is allowed to change common parameters only when all ADCs */
2101 /* of the common group are disabled. */
2102 if (__LL_ADC_IS_ENABLED_ALL_COMMON_INSTANCE(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) == 0UL)
2103 {
2104 tmp_config_internal_channel = LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
2105
2106 /* If the requested internal measurement path has already been enabled, */
2107 /* bypass the configuration processing. */
2108 if ((pConfigInjected->InjectedChannel == ADC_CHANNEL_TEMPSENSOR) && ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_TEMPSENSOR) == 0UL))
2109 {
2110 if (ADC_TEMPERATURE_SENSOR_INSTANCE(hadc))
2111 {
2112 LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_TEMPSENSOR | tmp_config_internal_channel);
2113
2114 /* Delay for temperature sensor stabilization time */
2115 /* Wait loop initialization and execution */
2116 /* Note: Variable divided by 2 to compensate partially */
2117 /* CPU processing cycles, scaling in us split to not */
2118 /* exceed 32 bits register capacity and handle low frequency. */
2119 wait_loop_index = ((LL_ADC_DELAY_TEMPSENSOR_STAB_US / 10UL) * ((SystemCoreClock / (100000UL * 2UL)) + 1UL));
2120 while(wait_loop_index != 0UL)
2121 {
2122 wait_loop_index--;
2123 }
2124 }
2125 }
2126 else if ((pConfigInjected->InjectedChannel == ADC_CHANNEL_VBAT) && ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_VBAT) == 0UL))
2127 {
2128 if (ADC_BATTERY_VOLTAGE_INSTANCE(hadc))
2129 {
2130 LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_VBAT | tmp_config_internal_channel);
2131 }
2132 }
2133 else if ((pConfigInjected->InjectedChannel == ADC_CHANNEL_VREFINT) && ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_VREFINT) == 0UL))
2134 {
2135 if (ADC_VREFINT_INSTANCE(hadc))
2136 {
2137 LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_VREFINT | tmp_config_internal_channel);
2138 }
2139 }
2140 else if ((pConfigInjected->InjectedChannel == ADC_CHANNEL_VCORE) && ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_VDDCORE) == 0UL))
2141 {
2142 if (ADC_VDDCORE_INSTANCE(hadc))
2143 {
2144 LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_VDDCORE | tmp_config_internal_channel);
2145 }
2146 }
2147 else
2148 {
2149 /* nothing to do */
2150 }
2151 }
2152 /* If the requested internal measurement path has already been enabled */
2153 /* and other ADC of the common group are enabled, internal */
2154 /* measurement paths cannot be enabled. */
2155 else
2156 {
2157 /* Update ADC state machine to error */
2158 SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
2159
2160 tmp_hal_status = HAL_ERROR;
2161 }
2162 }
2163
2164 }
2165
2166 __HAL_UNLOCK(hadc);
2167
2168 return tmp_hal_status;
2169 }
2170
2171 #if defined(ADC_MULTIMODE_SUPPORT)
2172 /**
2173 * @brief Enable ADC multimode and configure multimode parameters
2174 * @note Possibility to update parameters on the fly:
2175 * This function initializes multimode parameters, following
2176 * calls to this function can be used to reconfigure some parameters
2177 * of structure "ADC_MultiModeTypeDef" on the fly, without resetting
2178 * the ADCs.
2179 * The setting of these parameters is conditioned to ADC state.
2180 * For parameters constraints, see comments of structure
2181 * "ADC_MultiModeTypeDef".
2182 * @note To move back configuration from multimode to single mode, ADC must
2183 * be reset (using function HAL_ADC_Init() ).
2184 * @param hadc Master ADC handle
2185 * @param pmultimode Structure of ADC multimode configuration
2186 * @retval HAL status
2187 */
HAL_ADCEx_MultiModeConfigChannel(ADC_HandleTypeDef * hadc,ADC_MultiModeTypeDef * pmultimode)2188 HAL_StatusTypeDef HAL_ADCEx_MultiModeConfigChannel(ADC_HandleTypeDef *hadc, ADC_MultiModeTypeDef *pmultimode)
2189 {
2190 HAL_StatusTypeDef tmp_hal_status = HAL_OK;
2191 ADC_Common_TypeDef *tmpADC_Common;
2192 ADC_HandleTypeDef tmphadcSlave={0};
2193 uint32_t tmphadcSlave_conversion_on_going;
2194
2195 /* Check the parameters */
2196 assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
2197 assert_param(IS_ADC_MULTIMODE(pmultimode->Mode));
2198 if (pmultimode->Mode != ADC_MODE_INDEPENDENT)
2199 {
2200 assert_param(IS_ADC_DUAL_DATA_MODE(pmultimode->DualModeData));
2201 assert_param(IS_ADC_SAMPLING_DELAY(pmultimode->TwoSamplingDelay));
2202 }
2203
2204 /* Process locked */
2205 __HAL_LOCK(hadc);
2206
2207 /* Temporary handle minimum initialization */
2208 __HAL_ADC_RESET_HANDLE_STATE(&tmphadcSlave);
2209 ADC_CLEAR_ERRORCODE(&tmphadcSlave);
2210
2211 ADC_MULTI_SLAVE(hadc, &tmphadcSlave);
2212
2213 if (tmphadcSlave.Instance == NULL)
2214 {
2215 /* Update ADC state machine to error */
2216 SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
2217
2218 __HAL_UNLOCK(hadc);
2219
2220 return HAL_ERROR;
2221 }
2222
2223 /* Parameters update conditioned to ADC state: */
2224 /* Parameters that can be updated when ADC is disabled or enabled without */
2225 /* conversion on going on regular group: */
2226 /* - Multimode DATA Format configuration */
2227 tmphadcSlave_conversion_on_going = LL_ADC_REG_IsConversionOngoing((&tmphadcSlave)->Instance);
2228 if ((LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
2229 && (tmphadcSlave_conversion_on_going == 0UL))
2230 {
2231 /* Pointer to the common control register */
2232 tmpADC_Common = __LL_ADC_COMMON_INSTANCE(hadc->Instance);
2233
2234 /* If multimode is selected, configure all multimode parameters. */
2235 /* Otherwise, reset multimode parameters (can be used in case of */
2236 /* transition from multimode to independent mode). */
2237 if (pmultimode->Mode != ADC_MODE_INDEPENDENT)
2238 {
2239 MODIFY_REG(tmpADC_Common->CCR, ADC_CCR_DAMDF, pmultimode->DualModeData);
2240
2241 /* Parameters that can be updated only when ADC is disabled: */
2242 /* - Multimode mode selection */
2243 /* - Multimode delay */
2244 /* Note: Delay range depends on selected resolution: */
2245 /* from 1 to 9 clock cycles for 16 bits */
2246 /* from 1 to 9 clock cycles for 14 bits, */
2247 /* from 1 to 8 clock cycles for 12 bits */
2248 /* from 1 to 6 clock cycles for 10 and 8 bits */
2249 /* If a higher delay is selected, it will be clipped to maximum delay */
2250 /* range */
2251
2252 if (__LL_ADC_IS_ENABLED_ALL_COMMON_INSTANCE(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) == 0UL)
2253 {
2254 MODIFY_REG(tmpADC_Common->CCR,
2255 ADC_CCR_DUAL |
2256 ADC_CCR_DELAY,
2257 pmultimode->Mode |
2258 pmultimode->TwoSamplingDelay
2259 );
2260 }
2261 }
2262 else /* ADC_MODE_INDEPENDENT */
2263 {
2264 CLEAR_BIT(tmpADC_Common->CCR, ADC_CCR_DAMDF);
2265
2266 /* Parameters that can be updated only when ADC is disabled: */
2267 /* - Multimode mode selection */
2268 /* - Multimode delay */
2269 if (__LL_ADC_IS_ENABLED_ALL_COMMON_INSTANCE(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) == 0UL)
2270 {
2271 CLEAR_BIT(tmpADC_Common->CCR, ADC_CCR_DUAL | ADC_CCR_DELAY);
2272 }
2273 }
2274 }
2275 /* If one of the ADC sharing the same common group is enabled, no update */
2276 /* could be done on neither of the multimode structure parameters. */
2277 else
2278 {
2279 /* Update ADC state machine to error */
2280 SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
2281
2282 tmp_hal_status = HAL_ERROR;
2283 }
2284
2285 __HAL_UNLOCK(hadc);
2286
2287 return tmp_hal_status;
2288 }
2289 #endif /* ADC_MULTIMODE_SUPPORT */
2290
2291 /**
2292 * @brief Enable Injected Queue
2293 * @note This function resets CFGR register JQDIS bit in order to enable the
2294 * Injected Queue. JQDIS can be written only when ADSTART and JDSTART
2295 * are both equal to 0 to ensure that no regular nor injected
2296 * conversion is ongoing.
2297 * @param hadc ADC handle
2298 * @retval HAL status
2299 */
HAL_ADCEx_EnableInjectedQueue(ADC_HandleTypeDef * hadc)2300 HAL_StatusTypeDef HAL_ADCEx_EnableInjectedQueue(ADC_HandleTypeDef *hadc)
2301 {
2302 HAL_StatusTypeDef tmp_hal_status;
2303 uint32_t tmp_adc_is_conversion_on_going_regular;
2304 uint32_t tmp_adc_is_conversion_on_going_injected;
2305
2306 /* Check the parameters */
2307 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
2308
2309 tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
2310 tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
2311
2312 /* Parameter can be set only if no conversion is on-going */
2313 if ((tmp_adc_is_conversion_on_going_regular == 0UL)
2314 && (tmp_adc_is_conversion_on_going_injected == 0UL)
2315 )
2316 {
2317 CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_JQDIS);
2318
2319 /* Update state, clear previous result related to injected queue overflow */
2320 CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_JQOVF);
2321
2322 tmp_hal_status = HAL_OK;
2323 }
2324 else
2325 {
2326 tmp_hal_status = HAL_ERROR;
2327 }
2328
2329 return tmp_hal_status;
2330 }
2331
2332 /**
2333 * @brief Disable Injected Queue
2334 * @note This function sets CFGR register JQDIS bit in order to disable the
2335 * Injected Queue. JQDIS can be written only when ADSTART and JDSTART
2336 * are both equal to 0 to ensure that no regular nor injected
2337 * conversion is ongoing.
2338 * @param hadc ADC handle
2339 * @retval HAL status
2340 */
HAL_ADCEx_DisableInjectedQueue(ADC_HandleTypeDef * hadc)2341 HAL_StatusTypeDef HAL_ADCEx_DisableInjectedQueue(ADC_HandleTypeDef *hadc)
2342 {
2343 HAL_StatusTypeDef tmp_hal_status;
2344 uint32_t tmp_adc_is_conversion_on_going_regular;
2345 uint32_t tmp_adc_is_conversion_on_going_injected;
2346
2347 /* Check the parameters */
2348 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
2349
2350 tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
2351 tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
2352
2353 /* Parameter can be set only if no conversion is on-going */
2354 if ((tmp_adc_is_conversion_on_going_regular == 0UL)
2355 && (tmp_adc_is_conversion_on_going_injected == 0UL)
2356 )
2357 {
2358 LL_ADC_INJ_SetQueueMode(hadc->Instance, LL_ADC_INJ_QUEUE_DISABLE);
2359 tmp_hal_status = HAL_OK;
2360 }
2361 else
2362 {
2363 tmp_hal_status = HAL_ERROR;
2364 }
2365
2366 return tmp_hal_status;
2367 }
2368
2369 /**
2370 * @brief Disable ADC voltage regulator.
2371 * @note Disabling voltage regulator allows to save power. This operation can
2372 * be carried out only when ADC is disabled.
2373 * @note To enable again the voltage regulator, the user is expected to
2374 * resort to HAL_ADC_Init() API.
2375 * @param hadc ADC handle
2376 * @retval HAL status
2377 */
HAL_ADCEx_DisableVoltageRegulator(ADC_HandleTypeDef * hadc)2378 HAL_StatusTypeDef HAL_ADCEx_DisableVoltageRegulator(ADC_HandleTypeDef *hadc)
2379 {
2380 HAL_StatusTypeDef tmp_hal_status;
2381
2382 /* Check the parameters */
2383 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
2384
2385 /* Setting of this feature is conditioned to ADC state: ADC must be ADC disabled */
2386 if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
2387 {
2388 LL_ADC_DisableInternalRegulator(hadc->Instance);
2389 tmp_hal_status = HAL_OK;
2390 }
2391 else
2392 {
2393 tmp_hal_status = HAL_ERROR;
2394 }
2395
2396 return tmp_hal_status;
2397 }
2398
2399 /**
2400 * @brief Enter ADC deep-power-down mode
2401 * @note This mode is achieved in setting DEEPPWD bit and allows to save power
2402 * in reducing leakage currents. It is particularly interesting before
2403 * entering stop modes.
2404 * @note Setting DEEPPWD automatically clears ADVREGEN bit and disables the
2405 * ADC voltage regulator. This means that this API encompasses
2406 * HAL_ADCEx_DisableVoltageRegulator(). Additionally, the internal
2407 * calibration is lost.
2408 * @note To exit the ADC deep-power-down mode, the user is expected to
2409 * resort to HAL_ADC_Init() API as well as to relaunch a calibration
2410 * with HAL_ADCEx_Calibration_Start() API or to re-apply a previously
2411 * saved calibration factor.
2412 * @param hadc ADC handle
2413 * @retval HAL status
2414 */
HAL_ADCEx_EnterADCDeepPowerDownMode(ADC_HandleTypeDef * hadc)2415 HAL_StatusTypeDef HAL_ADCEx_EnterADCDeepPowerDownMode(ADC_HandleTypeDef *hadc)
2416 {
2417 HAL_StatusTypeDef tmp_hal_status;
2418
2419 /* Check the parameters */
2420 assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
2421
2422 /* Setting of this feature is conditioned to ADC state: ADC must be ADC disabled */
2423 if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
2424 {
2425 LL_ADC_EnableDeepPowerDown(hadc->Instance);
2426 tmp_hal_status = HAL_OK;
2427 }
2428 else
2429 {
2430 tmp_hal_status = HAL_ERROR;
2431 }
2432
2433 return tmp_hal_status;
2434 }
2435
2436 /**
2437 * @}
2438 */
2439
2440 /**
2441 * @}
2442 */
2443
2444 #endif /* HAL_ADC_MODULE_ENABLED */
2445 /**
2446 * @}
2447 */
2448
2449 /**
2450 * @}
2451 */
2452