2 ******************************************************************************
3 * @file stm32f7xx_hal_adc_ex.c
4 * @author MCD Application Team
7 * @brief This file provides firmware functions to manage the following
8 * functionalities of the ADC extension peripheral:
9 * + Extended features functions
12 ==============================================================================
13 ##### How to use this driver #####
14 ==============================================================================
16 (#)Initialize the ADC low level resources by implementing the HAL_ADC_MspInit():
17 (##) Enable the ADC interface clock using __HAL_RCC_ADC_CLK_ENABLE()
18 (##) ADC pins configuration
19 (+++) Enable the clock for the ADC GPIOs using the following function:
20 __HAL_RCC_GPIOx_CLK_ENABLE()
21 (+++) Configure these ADC pins in analog mode using HAL_GPIO_Init()
22 (##) In case of using interrupts (e.g. HAL_ADC_Start_IT())
23 (+++) Configure the ADC interrupt priority using HAL_NVIC_SetPriority()
24 (+++) Enable the ADC IRQ handler using HAL_NVIC_EnableIRQ()
25 (+++) In ADC IRQ handler, call HAL_ADC_IRQHandler()
26 (##) In case of using DMA to control data transfer (e.g. HAL_ADC_Start_DMA())
27 (+++) Enable the DMAx interface clock using __HAL_RCC_DMAx_CLK_ENABLE()
28 (+++) Configure and enable two DMA streams stream for managing data
29 transfer from peripheral to memory (output stream)
30 (+++) Associate the initialized DMA handle to the ADC DMA handle
32 (+++) Configure the priority and enable the NVIC for the transfer complete
33 interrupt on the two DMA Streams. The output stream should have higher
34 priority than the input stream.
35 (#) Configure the ADC Prescaler, conversion resolution and data alignment
36 using the HAL_ADC_Init() function.
38 (#) Configure the ADC Injected channels group features, use HAL_ADC_Init()
39 and HAL_ADC_ConfigChannel() functions.
41 (#) Three operation modes are available within this driver :
43 *** Polling mode IO operation ***
44 =================================
46 (+) Start the ADC peripheral using HAL_ADCEx_InjectedStart()
47 (+) Wait for end of conversion using HAL_ADC_PollForConversion(), at this stage
48 user can specify the value of timeout according to his end application
49 (+) To read the ADC converted values, use the HAL_ADCEx_InjectedGetValue() function.
50 (+) Stop the ADC peripheral using HAL_ADCEx_InjectedStop()
52 *** Interrupt mode IO operation ***
53 ===================================
55 (+) Start the ADC peripheral using HAL_ADCEx_InjectedStart_IT()
56 (+) Use HAL_ADC_IRQHandler() called under ADC_IRQHandler() Interrupt subroutine
57 (+) At ADC end of conversion HAL_ADCEx_InjectedConvCpltCallback() function is executed and user can
58 add his own code by customization of function pointer HAL_ADCEx_InjectedConvCpltCallback
59 (+) In case of ADC Error, HAL_ADCEx_InjectedErrorCallback() function is executed and user can
60 add his own code by customization of function pointer HAL_ADCEx_InjectedErrorCallback
61 (+) Stop the ADC peripheral using HAL_ADCEx_InjectedStop_IT()
64 *** DMA mode IO operation ***
65 ==============================
67 (+) Start the ADC peripheral using HAL_ADCEx_InjectedStart_DMA(), at this stage the user specify the length
68 of data to be transferred at each end of conversion
69 (+) At The end of data transfer ba HAL_ADCEx_InjectedConvCpltCallback() function is executed and user can
70 add his own code by customization of function pointer HAL_ADCEx_InjectedConvCpltCallback
71 (+) In case of transfer Error, HAL_ADCEx_InjectedErrorCallback() function is executed and user can
72 add his own code by customization of function pointer HAL_ADCEx_InjectedErrorCallback
73 (+) Stop the ADC peripheral using HAL_ADCEx_InjectedStop_DMA()
75 *** Multi mode ADCs Regular channels configuration ***
76 ======================================================
78 (+) Select the Multi mode ADC regular channels features (dual or triple mode)
79 and configure the DMA mode using HAL_ADCEx_MultiModeConfigChannel() functions.
80 (+) Start the ADC peripheral using HAL_ADCEx_MultiModeStart_DMA(), at this stage the user specify the length
81 of data to be transferred at each end of conversion
82 (+) Read the ADCs converted values using the HAL_ADCEx_MultiModeGetValue() function.
86 ******************************************************************************
89 * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
91 * Redistribution and use in source and binary forms, with or without modification,
92 * are permitted provided that the following conditions are met:
93 * 1. Redistributions of source code must retain the above copyright notice,
94 * this list of conditions and the following disclaimer.
95 * 2. Redistributions in binary form must reproduce the above copyright notice,
96 * this list of conditions and the following disclaimer in the documentation
97 * and/or other materials provided with the distribution.
98 * 3. Neither the name of STMicroelectronics nor the names of its contributors
99 * may be used to endorse or promote products derived from this software
100 * without specific prior written permission.
102 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
103 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
104 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
105 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
106 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
107 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
108 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
109 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
110 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
111 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
113 ******************************************************************************
116 /* Includes ------------------------------------------------------------------*/
117 #include "stm32f7xx_hal.h"
119 /** @addtogroup STM32F7xx_HAL_Driver
123 /** @defgroup ADCEx ADCEx
124 * @brief ADC Extended driver modules
128 #ifdef HAL_ADC_MODULE_ENABLED
130 /* Private typedef -----------------------------------------------------------*/
131 /* Private define ------------------------------------------------------------*/
132 /* Private macro -------------------------------------------------------------*/
133 /* Private variables ---------------------------------------------------------*/
134 /** @addtogroup ADCEx_Private_Functions
137 /* Private function prototypes -----------------------------------------------*/
138 static void ADC_MultiModeDMAConvCplt(DMA_HandleTypeDef
*hdma
);
139 static void ADC_MultiModeDMAError(DMA_HandleTypeDef
*hdma
);
140 static void ADC_MultiModeDMAHalfConvCplt(DMA_HandleTypeDef
*hdma
);
145 /* Exported functions --------------------------------------------------------*/
146 /** @defgroup ADCEx_Exported_Functions ADC Exported Functions
150 /** @defgroup ADCEx_Exported_Functions_Group1 Extended features functions
151 * @brief Extended features functions
154 ===============================================================================
155 ##### Extended features functions #####
156 ===============================================================================
157 [..] This section provides functions allowing to:
158 (+) Start conversion of injected channel.
159 (+) Stop conversion of injected channel.
160 (+) Start multimode and enable DMA transfer.
161 (+) Stop multimode and disable DMA transfer.
162 (+) Get result of injected channel conversion.
163 (+) Get result of multimode conversion.
164 (+) Configure injected channels.
165 (+) Configure multimode.
172 * @brief Enables the selected ADC software start conversion of the injected channels.
173 * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
174 * the configuration information for the specified ADC.
177 HAL_StatusTypeDef
HAL_ADCEx_InjectedStart(ADC_HandleTypeDef
* hadc
)
179 __IO
uint32_t counter
= 0;
180 uint32_t tmp1
= 0, tmp2
= 0;
185 /* Enable the ADC peripheral */
187 /* Check if ADC peripheral is disabled in order to enable it and wait during
188 Tstab time the ADC's stabilization */
189 if((hadc
->Instance
->CR2
& ADC_CR2_ADON
) != ADC_CR2_ADON
)
191 /* Enable the Peripheral */
192 __HAL_ADC_ENABLE(hadc
);
194 /* Delay for ADC stabilization time */
195 /* Compute number of CPU cycles to wait for */
196 counter
= (ADC_STAB_DELAY_US
* (SystemCoreClock
/ 1000000));
203 /* Start conversion if ADC is effectively enabled */
204 if(HAL_IS_BIT_SET(hadc
->Instance
->CR2
, ADC_CR2_ADON
))
207 /* - Clear state bitfield related to injected group conversion results */
208 /* - Set state bitfield related to injected operation */
209 ADC_STATE_CLR_SET(hadc
->State
,
210 HAL_ADC_STATE_READY
| HAL_ADC_STATE_INJ_EOC
,
211 HAL_ADC_STATE_INJ_BUSY
);
213 /* Check if a regular conversion is ongoing */
214 /* Note: On this device, there is no ADC error code fields related to */
215 /* conversions on group injected only. In case of conversion on */
216 /* going on group regular, no error code is reset. */
217 if (HAL_IS_BIT_CLR(hadc
->State
, HAL_ADC_STATE_REG_BUSY
))
219 /* Reset ADC all error code fields */
220 ADC_CLEAR_ERRORCODE(hadc
);
223 /* Process unlocked */
224 /* Unlock before starting ADC conversions: in case of potential */
225 /* interruption, to let the process to ADC IRQ Handler. */
228 /* Clear injected group conversion flag */
229 /* (To ensure of no unknown state from potential previous ADC operations) */
230 __HAL_ADC_CLEAR_FLAG(hadc
, ADC_FLAG_JEOC
);
232 /* Check if Multimode enabled */
233 if(HAL_IS_BIT_CLR(ADC
->CCR
, ADC_CCR_MULTI
))
235 tmp1
= HAL_IS_BIT_CLR(hadc
->Instance
->CR2
, ADC_CR2_JEXTEN
);
236 tmp2
= HAL_IS_BIT_CLR(hadc
->Instance
->CR1
, ADC_CR1_JAUTO
);
239 /* Enable the selected ADC software conversion for injected group */
240 hadc
->Instance
->CR2
|= ADC_CR2_JSWSTART
;
245 tmp1
= HAL_IS_BIT_CLR(hadc
->Instance
->CR2
, ADC_CR2_JEXTEN
);
246 tmp2
= HAL_IS_BIT_CLR(hadc
->Instance
->CR1
, ADC_CR1_JAUTO
);
247 if((hadc
->Instance
== ADC1
) && tmp1
&& tmp2
)
249 /* Enable the selected ADC software conversion for injected group */
250 hadc
->Instance
->CR2
|= ADC_CR2_JSWSTART
;
255 /* Return function status */
260 * @brief Enables the interrupt and starts ADC conversion of injected channels.
261 * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
262 * the configuration information for the specified ADC.
264 * @retval HAL status.
266 HAL_StatusTypeDef
HAL_ADCEx_InjectedStart_IT(ADC_HandleTypeDef
* hadc
)
268 __IO
uint32_t counter
= 0;
269 uint32_t tmp1
= 0, tmp2
= 0;
274 /* Enable the ADC peripheral */
276 /* Check if ADC peripheral is disabled in order to enable it and wait during
277 Tstab time the ADC's stabilization */
278 if((hadc
->Instance
->CR2
& ADC_CR2_ADON
) != ADC_CR2_ADON
)
280 /* Enable the Peripheral */
281 __HAL_ADC_ENABLE(hadc
);
283 /* Delay for ADC stabilization time */
284 /* Compute number of CPU cycles to wait for */
285 counter
= (ADC_STAB_DELAY_US
* (SystemCoreClock
/ 1000000));
292 /* Start conversion if ADC is effectively enabled */
293 if(HAL_IS_BIT_SET(hadc
->Instance
->CR2
, ADC_CR2_ADON
))
296 /* - Clear state bitfield related to injected group conversion results */
297 /* - Set state bitfield related to injected operation */
298 ADC_STATE_CLR_SET(hadc
->State
,
299 HAL_ADC_STATE_READY
| HAL_ADC_STATE_INJ_EOC
,
300 HAL_ADC_STATE_INJ_BUSY
);
302 /* Check if a regular conversion is ongoing */
303 /* Note: On this device, there is no ADC error code fields related to */
304 /* conversions on group injected only. In case of conversion on */
305 /* going on group regular, no error code is reset. */
306 if (HAL_IS_BIT_CLR(hadc
->State
, HAL_ADC_STATE_REG_BUSY
))
308 /* Reset ADC all error code fields */
309 ADC_CLEAR_ERRORCODE(hadc
);
312 /* Process unlocked */
313 /* Unlock before starting ADC conversions: in case of potential */
314 /* interruption, to let the process to ADC IRQ Handler. */
317 /* Clear injected group conversion flag */
318 /* (To ensure of no unknown state from potential previous ADC operations) */
319 __HAL_ADC_CLEAR_FLAG(hadc
, ADC_FLAG_JEOC
);
321 /* Enable end of conversion interrupt for injected channels */
322 __HAL_ADC_ENABLE_IT(hadc
, ADC_IT_JEOC
);
324 /* Check if Multimode enabled */
325 if(HAL_IS_BIT_CLR(ADC
->CCR
, ADC_CCR_MULTI
))
327 tmp1
= HAL_IS_BIT_CLR(hadc
->Instance
->CR2
, ADC_CR2_JEXTEN
);
328 tmp2
= HAL_IS_BIT_CLR(hadc
->Instance
->CR1
, ADC_CR1_JAUTO
);
331 /* Enable the selected ADC software conversion for injected group */
332 hadc
->Instance
->CR2
|= ADC_CR2_JSWSTART
;
337 tmp1
= HAL_IS_BIT_CLR(hadc
->Instance
->CR2
, ADC_CR2_JEXTEN
);
338 tmp2
= HAL_IS_BIT_CLR(hadc
->Instance
->CR1
, ADC_CR1_JAUTO
);
339 if((hadc
->Instance
== ADC1
) && tmp1
&& tmp2
)
341 /* Enable the selected ADC software conversion for injected group */
342 hadc
->Instance
->CR2
|= ADC_CR2_JSWSTART
;
347 /* Return function status */
352 * @brief Stop conversion of injected channels. Disable ADC peripheral if
353 * no regular conversion is on going.
354 * @note If ADC must be disabled and if conversion is on going on
355 * regular group, function HAL_ADC_Stop must be used to stop both
356 * injected and regular groups, and disable the ADC.
357 * @note If injected group mode auto-injection is enabled,
358 * function HAL_ADC_Stop must be used.
359 * @note In case of auto-injection mode, HAL_ADC_Stop must be used.
360 * @param hadc: ADC handle
363 HAL_StatusTypeDef
HAL_ADCEx_InjectedStop(ADC_HandleTypeDef
* hadc
)
365 HAL_StatusTypeDef tmp_hal_status
= HAL_OK
;
367 /* Check the parameters */
368 assert_param(IS_ADC_ALL_INSTANCE(hadc
->Instance
));
373 /* Stop potential conversion and disable ADC peripheral */
374 /* Conditioned to: */
375 /* - No conversion on the other group (regular group) is intended to */
376 /* continue (injected and regular groups stop conversion and ADC disable */
378 /* - In case of auto-injection mode, HAL_ADC_Stop must be used. */
379 if(((hadc
->State
& HAL_ADC_STATE_REG_BUSY
) == RESET
) &&
380 HAL_IS_BIT_CLR(hadc
->Instance
->CR1
, ADC_CR1_JAUTO
) )
382 /* Stop potential conversion on going, on regular and injected groups */
383 /* Disable ADC peripheral */
384 __HAL_ADC_DISABLE(hadc
);
386 /* Check if ADC is effectively disabled */
387 if(HAL_IS_BIT_CLR(hadc
->Instance
->CR2
, ADC_CR2_ADON
))
390 ADC_STATE_CLR_SET(hadc
->State
,
391 HAL_ADC_STATE_REG_BUSY
| HAL_ADC_STATE_INJ_BUSY
,
392 HAL_ADC_STATE_READY
);
397 /* Update ADC state machine to error */
398 SET_BIT(hadc
->State
, HAL_ADC_STATE_ERROR_CONFIG
);
400 tmp_hal_status
= HAL_ERROR
;
403 /* Process unlocked */
406 /* Return function status */
407 return tmp_hal_status
;
411 * @brief Poll for injected conversion complete
412 * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
413 * the configuration information for the specified ADC.
414 * @param Timeout: Timeout value in millisecond.
417 HAL_StatusTypeDef
HAL_ADCEx_InjectedPollForConversion(ADC_HandleTypeDef
* hadc
, uint32_t Timeout
)
419 uint32_t tickstart
= 0;
422 tickstart
= HAL_GetTick();
424 /* Check End of conversion flag */
425 while(!(__HAL_ADC_GET_FLAG(hadc
, ADC_FLAG_JEOC
)))
427 /* Check for the Timeout */
428 if(Timeout
!= HAL_MAX_DELAY
)
430 if((Timeout
== 0)||((HAL_GetTick() - tickstart
) > Timeout
))
432 hadc
->State
= HAL_ADC_STATE_TIMEOUT
;
433 /* Process unlocked */
440 /* Clear injected group conversion flag */
441 __HAL_ADC_CLEAR_FLAG(hadc
, ADC_FLAG_JSTRT
| ADC_FLAG_JEOC
);
443 /* Update ADC state machine */
444 SET_BIT(hadc
->State
, HAL_ADC_STATE_INJ_EOC
);
446 /* Determine whether any further conversion upcoming on group injected */
447 /* by external trigger, continuous mode or scan sequence on going. */
448 /* Note: On STM32F7, there is no independent flag of end of sequence. */
449 /* The test of scan sequence on going is done either with scan */
450 /* sequence disabled or with end of conversion flag set to */
451 /* of end of sequence. */
452 if(ADC_IS_SOFTWARE_START_INJECTED(hadc
) &&
453 (HAL_IS_BIT_CLR(hadc
->Instance
->JSQR
, ADC_JSQR_JL
) ||
454 HAL_IS_BIT_CLR(hadc
->Instance
->CR2
, ADC_CR2_EOCS
) ) &&
455 (HAL_IS_BIT_CLR(hadc
->Instance
->CR1
, ADC_CR1_JAUTO
) &&
456 (ADC_IS_SOFTWARE_START_REGULAR(hadc
) &&
457 (hadc
->Init
.ContinuousConvMode
== DISABLE
) ) ) )
460 CLEAR_BIT(hadc
->State
, HAL_ADC_STATE_INJ_BUSY
);
462 if (HAL_IS_BIT_CLR(hadc
->State
, HAL_ADC_STATE_REG_BUSY
))
464 SET_BIT(hadc
->State
, HAL_ADC_STATE_READY
);
468 /* Return ADC state */
473 * @brief Stop conversion of injected channels, disable interruption of
474 * end-of-conversion. Disable ADC peripheral if no regular conversion
476 * @note If ADC must be disabled and if conversion is on going on
477 * regular group, function HAL_ADC_Stop must be used to stop both
478 * injected and regular groups, and disable the ADC.
479 * @note If injected group mode auto-injection is enabled,
480 * function HAL_ADC_Stop must be used.
481 * @param hadc: ADC handle
484 HAL_StatusTypeDef
HAL_ADCEx_InjectedStop_IT(ADC_HandleTypeDef
* hadc
)
486 HAL_StatusTypeDef tmp_hal_status
= HAL_OK
;
488 /* Check the parameters */
489 assert_param(IS_ADC_ALL_INSTANCE(hadc
->Instance
));
494 /* Stop potential conversion and disable ADC peripheral */
495 /* Conditioned to: */
496 /* - No conversion on the other group (regular group) is intended to */
497 /* continue (injected and regular groups stop conversion and ADC disable */
499 /* - In case of auto-injection mode, HAL_ADC_Stop must be used. */
500 if(((hadc
->State
& HAL_ADC_STATE_REG_BUSY
) == RESET
) &&
501 HAL_IS_BIT_CLR(hadc
->Instance
->CR1
, ADC_CR1_JAUTO
) )
503 /* Stop potential conversion on going, on regular and injected groups */
504 /* Disable ADC peripheral */
505 __HAL_ADC_DISABLE(hadc
);
507 /* Check if ADC is effectively disabled */
508 if(HAL_IS_BIT_CLR(hadc
->Instance
->CR2
, ADC_CR2_ADON
))
510 /* Disable ADC end of conversion interrupt for injected channels */
511 __HAL_ADC_DISABLE_IT(hadc
, ADC_IT_JEOC
);
514 ADC_STATE_CLR_SET(hadc
->State
,
515 HAL_ADC_STATE_REG_BUSY
| HAL_ADC_STATE_INJ_BUSY
,
516 HAL_ADC_STATE_READY
);
521 /* Update ADC state machine to error */
522 SET_BIT(hadc
->State
, HAL_ADC_STATE_ERROR_CONFIG
);
524 tmp_hal_status
= HAL_ERROR
;
527 /* Process unlocked */
530 /* Return function status */
531 return tmp_hal_status
;
535 * @brief Gets the converted value from data register of injected channel.
536 * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
537 * the configuration information for the specified ADC.
538 * @param InjectedRank: the ADC injected rank.
539 * This parameter can be one of the following values:
540 * @arg ADC_INJECTED_RANK_1: Injected Channel1 selected
541 * @arg ADC_INJECTED_RANK_2: Injected Channel2 selected
542 * @arg ADC_INJECTED_RANK_3: Injected Channel3 selected
543 * @arg ADC_INJECTED_RANK_4: Injected Channel4 selected
546 uint32_t HAL_ADCEx_InjectedGetValue(ADC_HandleTypeDef
* hadc
, uint32_t InjectedRank
)
548 __IO
uint32_t tmp
= 0;
550 /* Check the parameters */
551 assert_param(IS_ADC_INJECTED_RANK(InjectedRank
));
553 /* Clear injected group conversion flag to have similar behaviour as */
554 /* regular group: reading data register also clears end of conversion flag. */
555 __HAL_ADC_CLEAR_FLAG(hadc
, ADC_FLAG_JEOC
);
557 /* Return the selected ADC converted value */
560 case ADC_INJECTED_RANK_4
:
562 tmp
= hadc
->Instance
->JDR4
;
565 case ADC_INJECTED_RANK_3
:
567 tmp
= hadc
->Instance
->JDR3
;
570 case ADC_INJECTED_RANK_2
:
572 tmp
= hadc
->Instance
->JDR2
;
575 case ADC_INJECTED_RANK_1
:
577 tmp
= hadc
->Instance
->JDR1
;
587 * @brief Enables ADC DMA request after last transfer (Multi-ADC mode) and enables ADC peripheral
589 * @note Caution: This function must be used only with the ADC master.
591 * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
592 * the configuration information for the specified ADC.
593 * @param pData: Pointer to buffer in which transferred from ADC peripheral to memory will be stored.
594 * @param Length: The length of data to be transferred from ADC peripheral to memory.
597 HAL_StatusTypeDef
HAL_ADCEx_MultiModeStart_DMA(ADC_HandleTypeDef
* hadc
, uint32_t* pData
, uint32_t Length
)
599 __IO
uint32_t counter
= 0;
601 /* Check the parameters */
602 assert_param(IS_FUNCTIONAL_STATE(hadc
->Init
.ContinuousConvMode
));
603 assert_param(IS_ADC_EXT_TRIG_EDGE(hadc
->Init
.ExternalTrigConvEdge
));
604 assert_param(IS_FUNCTIONAL_STATE(hadc
->Init
.DMAContinuousRequests
));
609 /* Check if ADC peripheral is disabled in order to enable it and wait during
610 Tstab time the ADC's stabilization */
611 if((hadc
->Instance
->CR2
& ADC_CR2_ADON
) != ADC_CR2_ADON
)
613 /* Enable the Peripheral */
614 __HAL_ADC_ENABLE(hadc
);
616 /* Delay for temperature sensor stabilization time */
617 /* Compute number of CPU cycles to wait for */
618 counter
= (ADC_STAB_DELAY_US
* (SystemCoreClock
/ 1000000));
625 /* Start conversion if ADC is effectively enabled */
626 if(HAL_IS_BIT_SET(hadc
->Instance
->CR2
, ADC_CR2_ADON
))
629 /* - Clear state bitfield related to regular group conversion results */
630 /* - Set state bitfield related to regular group operation */
631 ADC_STATE_CLR_SET(hadc
->State
,
632 HAL_ADC_STATE_READY
| HAL_ADC_STATE_REG_EOC
| HAL_ADC_STATE_REG_OVR
,
633 HAL_ADC_STATE_REG_BUSY
);
635 /* If conversions on group regular are also triggering group injected, */
636 /* update ADC state. */
637 if (READ_BIT(hadc
->Instance
->CR1
, ADC_CR1_JAUTO
) != RESET
)
639 ADC_STATE_CLR_SET(hadc
->State
, HAL_ADC_STATE_INJ_EOC
, HAL_ADC_STATE_INJ_BUSY
);
642 /* State machine update: Check if an injected conversion is ongoing */
643 if (HAL_IS_BIT_SET(hadc
->State
, HAL_ADC_STATE_INJ_BUSY
))
645 /* Reset ADC error code fields related to conversions on group regular */
646 CLEAR_BIT(hadc
->ErrorCode
, (HAL_ADC_ERROR_OVR
| HAL_ADC_ERROR_DMA
));
650 /* Reset ADC all error code fields */
651 ADC_CLEAR_ERRORCODE(hadc
);
654 /* Process unlocked */
655 /* Unlock before starting ADC conversions: in case of potential */
656 /* interruption, to let the process to ADC IRQ Handler. */
659 /* Set the DMA transfer complete callback */
660 hadc
->DMA_Handle
->XferCpltCallback
= ADC_MultiModeDMAConvCplt
;
662 /* Set the DMA half transfer complete callback */
663 hadc
->DMA_Handle
->XferHalfCpltCallback
= ADC_MultiModeDMAHalfConvCplt
;
665 /* Set the DMA error callback */
666 hadc
->DMA_Handle
->XferErrorCallback
= ADC_MultiModeDMAError
;
668 /* Manage ADC and DMA start: ADC overrun interruption, DMA start, ADC */
669 /* start (in case of SW start): */
671 /* Clear regular group conversion flag and overrun flag */
672 /* (To ensure of no unknown state from potential previous ADC operations) */
673 __HAL_ADC_CLEAR_FLAG(hadc
, ADC_FLAG_EOC
);
675 /* Enable ADC overrun interrupt */
676 __HAL_ADC_ENABLE_IT(hadc
, ADC_IT_OVR
);
678 if (hadc
->Init
.DMAContinuousRequests
!= DISABLE
)
680 /* Enable the selected ADC DMA request after last transfer */
681 ADC
->CCR
|= ADC_CCR_DDS
;
685 /* Disable the selected ADC EOC rising on each regular channel conversion */
686 ADC
->CCR
&= ~ADC_CCR_DDS
;
689 /* Enable the DMA Stream */
690 HAL_DMA_Start_IT(hadc
->DMA_Handle
, (uint32_t)&ADC
->CDR
, (uint32_t)pData
, Length
);
692 /* if no external trigger present enable software conversion of regular channels */
693 if((hadc
->Instance
->CR2
& ADC_CR2_EXTEN
) == RESET
)
695 /* Enable the selected ADC software conversion for regular group */
696 hadc
->Instance
->CR2
|= (uint32_t)ADC_CR2_SWSTART
;
700 /* Return function status */
705 * @brief Disables ADC DMA (multi-ADC mode) and disables ADC peripheral
706 * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
707 * the configuration information for the specified ADC.
710 HAL_StatusTypeDef
HAL_ADCEx_MultiModeStop_DMA(ADC_HandleTypeDef
* hadc
)
712 HAL_StatusTypeDef tmp_hal_status
= HAL_OK
;
714 /* Check the parameters */
715 assert_param(IS_ADC_ALL_INSTANCE(hadc
->Instance
));
720 /* Stop potential conversion on going, on regular and injected groups */
721 /* Disable ADC peripheral */
722 __HAL_ADC_DISABLE(hadc
);
724 /* Check if ADC is effectively disabled */
725 if(HAL_IS_BIT_CLR(hadc
->Instance
->CR2
, ADC_CR2_ADON
))
727 /* Disable the selected ADC DMA mode for multimode */
728 ADC
->CCR
&= ~ADC_CCR_DDS
;
730 /* Disable the DMA channel (in case of DMA in circular mode or stop while */
731 /* DMA transfer is on going) */
732 tmp_hal_status
= HAL_DMA_Abort(hadc
->DMA_Handle
);
734 /* Disable ADC overrun interrupt */
735 __HAL_ADC_DISABLE_IT(hadc
, ADC_IT_OVR
);
738 ADC_STATE_CLR_SET(hadc
->State
,
739 HAL_ADC_STATE_REG_BUSY
| HAL_ADC_STATE_INJ_BUSY
,
740 HAL_ADC_STATE_READY
);
743 /* Process unlocked */
746 /* Return function status */
747 return tmp_hal_status
;
751 * @brief Returns the last ADC1, ADC2 and ADC3 regular conversions results
752 * data in the selected multi mode.
753 * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
754 * the configuration information for the specified ADC.
755 * @retval The converted data value.
757 uint32_t HAL_ADCEx_MultiModeGetValue(ADC_HandleTypeDef
* hadc
)
759 /* Return the multi mode conversion value */
764 * @brief Injected conversion complete callback in non blocking mode
765 * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
766 * the configuration information for the specified ADC.
769 __weak
void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef
* hadc
)
771 /* Prevent unused argument(s) compilation warning */
773 /* NOTE : This function Should not be modified, when the callback is needed,
774 the HAL_ADC_InjectedConvCpltCallback could be implemented in the user file
779 * @brief Configures for the selected ADC injected channel its corresponding
780 * rank in the sequencer and its sample time.
781 * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
782 * the configuration information for the specified ADC.
783 * @param sConfigInjected: ADC configuration structure for injected channel.
786 HAL_StatusTypeDef
HAL_ADCEx_InjectedConfigChannel(ADC_HandleTypeDef
* hadc
, ADC_InjectionConfTypeDef
* sConfigInjected
)
789 #ifdef USE_FULL_ASSERT
791 #endif /* USE_FULL_ASSERT */
793 /* Check the parameters */
794 assert_param(IS_ADC_CHANNEL(sConfigInjected
->InjectedChannel
));
795 assert_param(IS_ADC_INJECTED_RANK(sConfigInjected
->InjectedRank
));
796 assert_param(IS_ADC_SAMPLE_TIME(sConfigInjected
->InjectedSamplingTime
));
797 assert_param(IS_ADC_EXT_INJEC_TRIG(sConfigInjected
->ExternalTrigInjecConv
));
798 assert_param(IS_ADC_INJECTED_LENGTH(sConfigInjected
->InjectedNbrOfConversion
));
799 assert_param(IS_FUNCTIONAL_STATE(sConfigInjected
->AutoInjectedConv
));
800 assert_param(IS_FUNCTIONAL_STATE(sConfigInjected
->InjectedDiscontinuousConvMode
));
802 #ifdef USE_FULL_ASSERT
803 tmp
= ADC_GET_RESOLUTION(hadc
);
804 assert_param(IS_ADC_RANGE(tmp
, sConfigInjected
->InjectedOffset
));
805 #endif /* USE_FULL_ASSERT */
807 if(sConfigInjected
->ExternalTrigInjecConvEdge
!= ADC_INJECTED_SOFTWARE_START
)
809 assert_param(IS_ADC_EXT_INJEC_TRIG_EDGE(sConfigInjected
->ExternalTrigInjecConvEdge
));
815 /* if ADC_Channel_10 ... ADC_Channel_18 is selected */
816 if (sConfigInjected
->InjectedChannel
> ADC_CHANNEL_9
)
818 /* Clear the old sample time */
819 hadc
->Instance
->SMPR1
&= ~ADC_SMPR1(ADC_SMPR1_SMP10
, sConfigInjected
->InjectedChannel
);
821 /* Set the new sample time */
822 hadc
->Instance
->SMPR1
|= ADC_SMPR1(sConfigInjected
->InjectedSamplingTime
, sConfigInjected
->InjectedChannel
);
824 else /* ADC_Channel include in ADC_Channel_[0..9] */
826 /* Clear the old sample time */
827 hadc
->Instance
->SMPR2
&= ~ADC_SMPR2(ADC_SMPR2_SMP0
, sConfigInjected
->InjectedChannel
);
829 /* Set the new sample time */
830 hadc
->Instance
->SMPR2
|= ADC_SMPR2(sConfigInjected
->InjectedSamplingTime
, sConfigInjected
->InjectedChannel
);
833 /*---------------------------- ADCx JSQR Configuration -----------------*/
834 hadc
->Instance
->JSQR
&= ~(ADC_JSQR_JL
);
835 hadc
->Instance
->JSQR
|= ADC_SQR1(sConfigInjected
->InjectedNbrOfConversion
);
837 /* Rank configuration */
839 /* Clear the old SQx bits for the selected rank */
840 hadc
->Instance
->JSQR
&= ~ADC_JSQR(ADC_JSQR_JSQ1
, sConfigInjected
->InjectedRank
,sConfigInjected
->InjectedNbrOfConversion
);
842 /* Set the SQx bits for the selected rank */
843 hadc
->Instance
->JSQR
|= ADC_JSQR(sConfigInjected
->InjectedChannel
, sConfigInjected
->InjectedRank
,sConfigInjected
->InjectedNbrOfConversion
);
845 /* Enable external trigger if trigger selection is different of software */
847 /* Note: This configuration keeps the hardware feature of parameter */
848 /* ExternalTrigConvEdge "trigger edge none" equivalent to */
849 /* software start. */
850 if(sConfigInjected
->ExternalTrigInjecConv
!= ADC_INJECTED_SOFTWARE_START
)
852 /* Select external trigger to start conversion */
853 hadc
->Instance
->CR2
&= ~(ADC_CR2_JEXTSEL
);
854 hadc
->Instance
->CR2
|= sConfigInjected
->ExternalTrigInjecConv
;
856 /* Select external trigger polarity */
857 hadc
->Instance
->CR2
&= ~(ADC_CR2_JEXTEN
);
858 hadc
->Instance
->CR2
|= sConfigInjected
->ExternalTrigInjecConvEdge
;
862 /* Reset the external trigger */
863 hadc
->Instance
->CR2
&= ~(ADC_CR2_JEXTSEL
);
864 hadc
->Instance
->CR2
&= ~(ADC_CR2_JEXTEN
);
867 if (sConfigInjected
->AutoInjectedConv
!= DISABLE
)
869 /* Enable the selected ADC automatic injected group conversion */
870 hadc
->Instance
->CR1
|= ADC_CR1_JAUTO
;
874 /* Disable the selected ADC automatic injected group conversion */
875 hadc
->Instance
->CR1
&= ~(ADC_CR1_JAUTO
);
878 if (sConfigInjected
->InjectedDiscontinuousConvMode
!= DISABLE
)
880 /* Enable the selected ADC injected discontinuous mode */
881 hadc
->Instance
->CR1
|= ADC_CR1_JDISCEN
;
885 /* Disable the selected ADC injected discontinuous mode */
886 hadc
->Instance
->CR1
&= ~(ADC_CR1_JDISCEN
);
889 switch(sConfigInjected
->InjectedRank
)
892 /* Set injected channel 1 offset */
893 hadc
->Instance
->JOFR1
&= ~(ADC_JOFR1_JOFFSET1
);
894 hadc
->Instance
->JOFR1
|= sConfigInjected
->InjectedOffset
;
897 /* Set injected channel 2 offset */
898 hadc
->Instance
->JOFR2
&= ~(ADC_JOFR2_JOFFSET2
);
899 hadc
->Instance
->JOFR2
|= sConfigInjected
->InjectedOffset
;
902 /* Set injected channel 3 offset */
903 hadc
->Instance
->JOFR3
&= ~(ADC_JOFR3_JOFFSET3
);
904 hadc
->Instance
->JOFR3
|= sConfigInjected
->InjectedOffset
;
907 /* Set injected channel 4 offset */
908 hadc
->Instance
->JOFR4
&= ~(ADC_JOFR4_JOFFSET4
);
909 hadc
->Instance
->JOFR4
|= sConfigInjected
->InjectedOffset
;
913 /* if ADC1 Channel_18 is selected enable VBAT Channel */
914 if ((hadc
->Instance
== ADC1
) && (sConfigInjected
->InjectedChannel
== ADC_CHANNEL_VBAT
))
916 /* Enable the VBAT channel*/
917 ADC
->CCR
|= ADC_CCR_VBATE
;
920 /* if ADC1 Channel_16 or Channel_17 is selected enable TSVREFE Channel(Temperature sensor and VREFINT) */
921 if ((hadc
->Instance
== ADC1
) && ((sConfigInjected
->InjectedChannel
== ADC_CHANNEL_TEMPSENSOR
) || (sConfigInjected
->InjectedChannel
== ADC_CHANNEL_VREFINT
)))
923 /* Enable the TSVREFE channel*/
924 ADC
->CCR
|= ADC_CCR_TSVREFE
;
927 /* Process unlocked */
930 /* Return function status */
935 * @brief Configures the ADC multi-mode
936 * @param hadc : pointer to a ADC_HandleTypeDef structure that contains
937 * the configuration information for the specified ADC.
938 * @param multimode : pointer to an ADC_MultiModeTypeDef structure that contains
939 * the configuration information for multimode.
942 HAL_StatusTypeDef
HAL_ADCEx_MultiModeConfigChannel(ADC_HandleTypeDef
* hadc
, ADC_MultiModeTypeDef
* multimode
)
944 /* Check the parameters */
945 assert_param(IS_ADC_MODE(multimode
->Mode
));
946 assert_param(IS_ADC_DMA_ACCESS_MODE(multimode
->DMAAccessMode
));
947 assert_param(IS_ADC_SAMPLING_DELAY(multimode
->TwoSamplingDelay
));
953 ADC
->CCR
&= ~(ADC_CCR_MULTI
);
954 ADC
->CCR
|= multimode
->Mode
;
956 /* Set the ADC DMA access mode */
957 ADC
->CCR
&= ~(ADC_CCR_DMA
);
958 ADC
->CCR
|= multimode
->DMAAccessMode
;
960 /* Set delay between two sampling phases */
961 ADC
->CCR
&= ~(ADC_CCR_DELAY
);
962 ADC
->CCR
|= multimode
->TwoSamplingDelay
;
964 /* Process unlocked */
967 /* Return function status */
976 * @brief DMA transfer complete callback.
977 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
978 * the configuration information for the specified DMA module.
981 static void ADC_MultiModeDMAConvCplt(DMA_HandleTypeDef
*hdma
)
983 /* Retrieve ADC handle corresponding to current DMA handle */
984 ADC_HandleTypeDef
* hadc
= ( ADC_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
986 /* Update state machine on conversion status if not in error state */
987 if (HAL_IS_BIT_CLR(hadc
->State
, HAL_ADC_STATE_ERROR_INTERNAL
| HAL_ADC_STATE_ERROR_DMA
))
989 /* Update ADC state machine */
990 SET_BIT(hadc
->State
, HAL_ADC_STATE_REG_EOC
);
992 /* Determine whether any further conversion upcoming on group regular */
993 /* by external trigger, continuous mode or scan sequence on going. */
994 /* Note: On STM32F7, there is no independent flag of end of sequence. */
995 /* The test of scan sequence on going is done either with scan */
996 /* sequence disabled or with end of conversion flag set to */
997 /* of end of sequence. */
998 if(ADC_IS_SOFTWARE_START_REGULAR(hadc
) &&
999 (hadc
->Init
.ContinuousConvMode
== DISABLE
) &&
1000 (HAL_IS_BIT_CLR(hadc
->Instance
->SQR1
, ADC_SQR1_L
) ||
1001 HAL_IS_BIT_CLR(hadc
->Instance
->CR2
, ADC_CR2_EOCS
) ) )
1003 /* Disable ADC end of single conversion interrupt on group regular */
1004 /* Note: Overrun interrupt was enabled with EOC interrupt in */
1005 /* HAL_ADC_Start_IT(), but is not disabled here because can be used */
1006 /* by overrun IRQ process below. */
1007 __HAL_ADC_DISABLE_IT(hadc
, ADC_IT_EOC
);
1010 CLEAR_BIT(hadc
->State
, HAL_ADC_STATE_REG_BUSY
);
1012 if (HAL_IS_BIT_CLR(hadc
->State
, HAL_ADC_STATE_INJ_BUSY
))
1014 SET_BIT(hadc
->State
, HAL_ADC_STATE_READY
);
1018 /* Conversion complete callback */
1019 HAL_ADC_ConvCpltCallback(hadc
);
1023 /* Call DMA error callback */
1024 hadc
->DMA_Handle
->XferErrorCallback(hdma
);
1029 * @brief DMA half transfer complete callback.
1030 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
1031 * the configuration information for the specified DMA module.
1034 static void ADC_MultiModeDMAHalfConvCplt(DMA_HandleTypeDef
*hdma
)
1036 ADC_HandleTypeDef
* hadc
= ( ADC_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
1037 /* Conversion complete callback */
1038 HAL_ADC_ConvHalfCpltCallback(hadc
);
1042 * @brief DMA error callback
1043 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
1044 * the configuration information for the specified DMA module.
1047 static void ADC_MultiModeDMAError(DMA_HandleTypeDef
*hdma
)
1049 ADC_HandleTypeDef
* hadc
= ( ADC_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
1050 hadc
->State
= HAL_ADC_STATE_ERROR_DMA
;
1051 /* Set ADC error code to DMA error */
1052 hadc
->ErrorCode
|= HAL_ADC_ERROR_DMA
;
1053 HAL_ADC_ErrorCallback(hadc
);
1060 #endif /* HAL_ADC_MODULE_ENABLED */
1069 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/