2 ******************************************************************************
3 * @file stm32f4xx_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 __ADC_CLK_ENABLE()
18 (##) ADC pins configuration
19 (+++) Enable the clock for the ADC GPIOs using the following function:
21 (+++) Configure these ADC pins in analog mode using HAL_GPIO_Init()
22 (##) In case of using interrupts (e.g. HAL_ADC_Start_IT())
23 (+++) Configure the ADC interrupt priority using HAL_NVIC_SetPriority()
24 (+++) Enable the ADC IRQ handler using HAL_NVIC_EnableIRQ()
25 (+++) In ADC IRQ handler, call HAL_ADC_IRQHandler()
26 (##) In case of using DMA to control data transfer (e.g. HAL_ADC_Start_DMA())
27 (+++) Enable the DMAx interface clock using __DMAx_CLK_ENABLE()
28 (+++) Configure and enable two DMA streams stream for managing data
29 transfer from peripheral to memory (output stream)
30 (+++) Associate the initilalized DMA handle to the ADC DMA handle
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) 2014 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 "stm32f4xx_hal.h"
119 /** @addtogroup STM32F4xx_HAL_Driver
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 /* Private function prototypes -----------------------------------------------*/
135 static void ADC_MultiModeDMAConvCplt(DMA_HandleTypeDef *hdma);
136 static void ADC_MultiModeDMAError(DMA_HandleTypeDef *hdma);
137 static void ADC_MultiModeDMAHalfConvCplt(DMA_HandleTypeDef *hdma);
138 /* Private functions ---------------------------------------------------------*/
140 /** @defgroup ADCEx_Private_Functions
144 /** @defgroup ADCEx_Group1 Extended features functions
145 * @brief Extended features functions
148 ===============================================================================
149 ##### Extended features functions #####
150 ===============================================================================
151 [..] This section provides functions allowing to:
152 (+) Start conversion of injected channel.
153 (+) Stop conversion of injected channel.
154 (+) Start multimode and enable DMA transfer.
155 (+) Stop multimode and disable DMA transfer.
156 (+) Get result of injected channel conversion.
157 (+) Get result of multimode conversion.
158 (+) Configure injected channels.
159 (+) Configure multimode.
166 * @brief Enables the selected ADC software start conversion of the injected channels.
167 * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
168 * the configuration information for the specified ADC.
171 HAL_StatusTypeDef HAL_ADCEx_InjectedStart(ADC_HandleTypeDef* hadc)
173 uint32_t i = 0, tmp1 = 0, tmp2 = 0;
178 /* Check if a regular conversion is ongoing */
179 if(hadc->State == HAL_ADC_STATE_BUSY_REG)
181 /* Change ADC state */
182 hadc->State = HAL_ADC_STATE_BUSY_INJ_REG;
186 /* Change ADC state */
187 hadc->State = HAL_ADC_STATE_BUSY_INJ;
190 /* Check if ADC peripheral is disabled in order to enable it and wait during
191 Tstab time the ADC's stabilization */
192 if((hadc->Instance->CR2 & ADC_CR2_ADON) != ADC_CR2_ADON)
194 /* Enable the Peripheral */
195 __HAL_ADC_ENABLE(hadc);
197 /* Delay inserted to wait during Tstab time the ADC's stabilazation */
204 /* Check if Multimode enabled */
205 if(HAL_IS_BIT_CLR(ADC->CCR, ADC_CCR_MULTI))
207 tmp1 = HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_JEXTEN);
208 tmp2 = HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO);
211 /* Enable the selected ADC software conversion for injected group */
212 hadc->Instance->CR2 |= ADC_CR2_JSWSTART;
217 tmp1 = HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_JEXTEN);
218 tmp2 = HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO);
219 if((hadc->Instance == ADC1) && tmp1 && tmp2)
221 /* Enable the selected ADC software conversion for injected group */
222 hadc->Instance->CR2 |= ADC_CR2_JSWSTART;
226 /* Process unlocked */
229 /* Return function status */
234 * @brief Enables the interrupt and starts ADC conversion of injected channels.
235 * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
236 * the configuration information for the specified ADC.
238 * @retval HAL status.
240 HAL_StatusTypeDef HAL_ADCEx_InjectedStart_IT(ADC_HandleTypeDef* hadc)
242 uint32_t i = 0, tmp1 = 0, tmp2 =0;
247 /* Check if a regular conversion is ongoing */
248 if(hadc->State == HAL_ADC_STATE_BUSY_REG)
250 /* Change ADC state */
251 hadc->State = HAL_ADC_STATE_BUSY_INJ_REG;
255 /* Change ADC state */
256 hadc->State = HAL_ADC_STATE_BUSY_INJ;
259 /* Set ADC error code to none */
260 hadc->ErrorCode = HAL_ADC_ERROR_NONE;
262 /* Check if ADC peripheral is disabled in order to enable it and wait during
263 Tstab time the ADC's stabilization */
264 if((hadc->Instance->CR2 & ADC_CR2_ADON) != ADC_CR2_ADON)
266 /* Enable the Peripheral */
267 __HAL_ADC_ENABLE(hadc);
269 /* Delay inserted to wait during Tstab time the ADC's stabilazation */
276 /* Enable the ADC end of conversion interrupt for injected group */
277 __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOC);
279 /* Enable the ADC overrun interrupt */
280 __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);
282 /* Check if Multimode enabled */
283 if(HAL_IS_BIT_CLR(ADC->CCR, ADC_CCR_MULTI))
285 tmp1 = HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_JEXTEN);
286 tmp2 = HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO);
289 /* Enable the selected ADC software conversion for injected group */
290 hadc->Instance->CR2 |= ADC_CR2_JSWSTART;
295 tmp1 = HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_JEXTEN);
296 tmp2 = HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO);
297 if((hadc->Instance == ADC1) && tmp1 && tmp2)
299 /* Enable the selected ADC software conversion for injected group */
300 hadc->Instance->CR2 |= ADC_CR2_JSWSTART;
304 /* Process unlocked */
307 /* Return function status */
312 * @brief Disables ADC and stop conversion of injected channels.
314 * @note Caution: This function will stop also regular channels.
316 * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
317 * the configuration information for the specified ADC.
318 * @retval HAL status.
320 HAL_StatusTypeDef HAL_ADCEx_InjectedStop(ADC_HandleTypeDef* hadc)
322 /* Disable the Peripheral */
323 __HAL_ADC_DISABLE(hadc);
325 /* Change ADC state */
326 hadc->State = HAL_ADC_STATE_READY;
328 /* Return function status */
333 * @brief Poll for injected conversion complete
334 * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
335 * the configuration information for the specified ADC.
336 * @param Timeout: Timeout value in millisecond.
339 HAL_StatusTypeDef HAL_ADCEx_InjectedPollForConversion(ADC_HandleTypeDef* hadc, uint32_t Timeout)
341 uint32_t tickstart = 0;
344 tickstart = HAL_GetTick();
346 /* Check End of conversion flag */
347 while(!(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_JEOC)))
349 /* Check for the Timeout */
350 if(Timeout != HAL_MAX_DELAY)
352 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
354 hadc->State= HAL_ADC_STATE_TIMEOUT;
355 /* Process unlocked */
362 /* Check if a regular conversion is ready */
363 if(hadc->State == HAL_ADC_STATE_EOC_REG)
365 /* Change ADC state */
366 hadc->State = HAL_ADC_STATE_EOC_INJ_REG;
370 /* Change ADC state */
371 hadc->State = HAL_ADC_STATE_EOC_INJ;
374 /* Return ADC state */
379 * @brief Disables the interrupt and stop ADC conversion of injected channels.
381 * @note Caution: This function will stop also regular channels.
383 * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
384 * the configuration information for the specified ADC.
385 * @retval HAL status.
387 HAL_StatusTypeDef HAL_ADCEx_InjectedStop_IT(ADC_HandleTypeDef* hadc)
389 /* Disable the ADC end of conversion interrupt for regular group */
390 __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC);
392 /* Disable the ADC end of conversion interrupt for injected group */
393 __HAL_ADC_DISABLE_IT(hadc, ADC_CR1_JEOCIE);
395 /* Enable the Periphral */
396 __HAL_ADC_DISABLE(hadc);
398 /* Change ADC state */
399 hadc->State = HAL_ADC_STATE_READY;
401 /* Return function status */
406 * @brief Gets the converted value from data register of injected channel.
407 * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
408 * the configuration information for the specified ADC.
409 * @param InjectedRank: the ADC injected rank.
410 * This parameter can be one of the following values:
411 * @arg ADC_INJECTED_RANK_1: Injected Channel1 selected
412 * @arg ADC_INJECTED_RANK_2: Injected Channel2 selected
413 * @arg ADC_INJECTED_RANK_3: Injected Channel3 selected
414 * @arg ADC_INJECTED_RANK_4: Injected Channel4 selected
417 uint32_t HAL_ADCEx_InjectedGetValue(ADC_HandleTypeDef* hadc, uint32_t InjectedRank)
419 __IO uint32_t tmp = 0;
421 /* Check the parameters */
422 assert_param(IS_ADC_INJECTED_RANK(InjectedRank));
424 /* Clear the ADCx's flag for injected end of conversion */
425 __HAL_ADC_CLEAR_FLAG(hadc,ADC_FLAG_JEOC);
427 /* Return the selected ADC converted value */
430 case ADC_INJECTED_RANK_4:
432 tmp = hadc->Instance->JDR4;
435 case ADC_INJECTED_RANK_3:
437 tmp = hadc->Instance->JDR3;
440 case ADC_INJECTED_RANK_2:
442 tmp = hadc->Instance->JDR2;
445 case ADC_INJECTED_RANK_1:
447 tmp = hadc->Instance->JDR1;
457 * @brief Enables ADC DMA request after last transfer (Multi-ADC mode) and enables ADC peripheral
459 * @note Caution: This function must be used only with the ADC master.
461 * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
462 * the configuration information for the specified ADC.
463 * @param pData: Pointer to buffer in which transferred from ADC peripheral to memory will be stored.
464 * @param Length: The length of data to be transferred from ADC peripheral to memory.
467 HAL_StatusTypeDef HAL_ADCEx_MultiModeStart_DMA(ADC_HandleTypeDef* hadc, uint32_t* pData, uint32_t Length)
469 uint16_t counter = 0;
471 /* Check the parameters */
472 assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
473 assert_param(IS_ADC_EXT_TRIG_EDGE(hadc->Init.ExternalTrigConvEdge));
474 assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DMAContinuousRequests));
479 /* Enable ADC overrun interrupt */
480 __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);
482 if (hadc->Init.DMAContinuousRequests != DISABLE)
484 /* Enable the selected ADC DMA request after last transfer */
485 ADC->CCR |= ADC_CCR_DDS;
489 /* Disable the selected ADC EOC rising on each regular channel conversion */
490 ADC->CCR &= ~ADC_CCR_DDS;
493 /* Set the DMA transfer complete callback */
494 hadc->DMA_Handle->XferCpltCallback = ADC_MultiModeDMAConvCplt;
496 /* Set the DMA half transfer complete callback */
497 hadc->DMA_Handle->XferHalfCpltCallback = ADC_MultiModeDMAHalfConvCplt;
499 /* Set the DMA error callback */
500 hadc->DMA_Handle->XferErrorCallback = ADC_MultiModeDMAError ;
502 /* Enable the DMA Stream */
503 HAL_DMA_Start_IT(hadc->DMA_Handle, (uint32_t)&ADC->CDR, (uint32_t)pData, Length);
505 /* Change ADC state */
506 hadc->State = HAL_ADC_STATE_BUSY_REG;
508 /* Check if ADC peripheral is disabled in order to enable it and wait during
509 Tstab time the ADC's stabilization */
510 if((hadc->Instance->CR2 & ADC_CR2_ADON) != ADC_CR2_ADON)
512 /* Enable the Peripheral */
513 __HAL_ADC_ENABLE(hadc);
515 /* Delay inserted to wait during Tstab time the ADC's stabilazation */
516 for(; counter <= 540; counter++)
522 /* if no external trigger present enable software conversion of regular channels */
523 if (hadc->Init.ExternalTrigConvEdge == ADC_EXTERNALTRIGCONVEDGE_NONE)
525 /* Enable the selected ADC software conversion for regular group */
526 hadc->Instance->CR2 |= (uint32_t)ADC_CR2_SWSTART;
529 /* Process unlocked */
532 /* Return function status */
537 * @brief Disables ADC DMA (multi-ADC mode) and disables ADC peripheral
538 * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
539 * the configuration information for the specified ADC.
542 HAL_StatusTypeDef HAL_ADCEx_MultiModeStop_DMA(ADC_HandleTypeDef* hadc)
547 /* Enable the Peripheral */
548 __HAL_ADC_DISABLE(hadc);
550 /* Disable ADC overrun interrupt */
551 __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
553 /* Disable the selected ADC DMA request after last transfer */
554 ADC->CCR &= ~ADC_CCR_DDS;
556 /* Disable the ADC DMA Stream */
557 HAL_DMA_Abort(hadc->DMA_Handle);
559 /* Change ADC state */
560 hadc->State = HAL_ADC_STATE_READY;
562 /* Process unlocked */
565 /* Return function status */
570 * @brief Returns the last ADC1, ADC2 and ADC3 regular conversions results
571 * data in the selected multi mode.
572 * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
573 * the configuration information for the specified ADC.
574 * @retval The converted data value.
576 uint32_t HAL_ADCEx_MultiModeGetValue(ADC_HandleTypeDef* hadc)
578 /* Return the multi mode conversion value */
583 * @brief Injected conversion complete callback in non blocking mode
584 * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
585 * the configuration information for the specified ADC.
588 __weak void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef* hadc)
590 /* NOTE : This function Should not be modified, when the callback is needed,
591 the HAL_ADC_InjectedConvCpltCallback could be implemented in the user file
596 * @brief Configures for the selected ADC injected channel its corresponding
597 * rank in the sequencer and its sample time.
598 * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
599 * the configuration information for the specified ADC.
600 * @param sConfigInjected: ADC configuration structure for injected channel.
603 HAL_StatusTypeDef HAL_ADCEx_InjectedConfigChannel(ADC_HandleTypeDef* hadc, ADC_InjectionConfTypeDef* sConfigInjected)
606 #ifdef USE_FULL_ASSERT
608 #endif /* USE_FULL_ASSERT */
610 /* Check the parameters */
611 assert_param(IS_ADC_CHANNEL(sConfigInjected->InjectedChannel));
612 assert_param(IS_ADC_INJECTED_RANK(sConfigInjected->InjectedRank));
613 assert_param(IS_ADC_SAMPLE_TIME(sConfigInjected->InjectedSamplingTime));
614 assert_param(IS_ADC_EXT_INJEC_TRIG(sConfigInjected->ExternalTrigInjecConv));
615 assert_param(IS_ADC_EXT_INJEC_TRIG_EDGE(sConfigInjected->ExternalTrigInjecConvEdge));
616 assert_param(IS_ADC_INJECTED_LENGTH(sConfigInjected->InjectedNbrOfConversion));
617 assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->AutoInjectedConv));
618 assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->InjectedDiscontinuousConvMode));
620 #ifdef USE_FULL_ASSERT
621 tmp = __HAL_ADC_GET_RESOLUTION(hadc);
622 assert_param(IS_ADC_RANGE(tmp, sConfigInjected->InjectedOffset));
623 #endif /* USE_FULL_ASSERT */
628 /* if ADC_Channel_10 ... ADC_Channel_18 is selected */
629 if (sConfigInjected->InjectedChannel > ADC_CHANNEL_9)
631 /* Clear the old sample time */
632 hadc->Instance->SMPR1 &= ~__HAL_ADC_SMPR1(ADC_SMPR1_SMP10, sConfigInjected->InjectedChannel);
634 /* Set the new sample time */
635 hadc->Instance->SMPR1 |= __HAL_ADC_SMPR1(sConfigInjected->InjectedSamplingTime, sConfigInjected->InjectedChannel);
637 else /* ADC_Channel include in ADC_Channel_[0..9] */
639 /* Clear the old sample time */
640 hadc->Instance->SMPR2 &= ~__HAL_ADC_SMPR2(ADC_SMPR2_SMP0, sConfigInjected->InjectedChannel);
642 /* Set the new sample time */
643 hadc->Instance->SMPR2 |= __HAL_ADC_SMPR2(sConfigInjected->InjectedSamplingTime, sConfigInjected->InjectedChannel);
646 /*---------------------------- ADCx JSQR Configuration -----------------*/
647 hadc->Instance->JSQR &= ~(ADC_JSQR_JL);
648 hadc->Instance->JSQR |= __HAL_ADC_SQR1(sConfigInjected->InjectedNbrOfConversion);
650 /* Rank configuration */
652 /* Clear the old SQx bits for the selected rank */
653 hadc->Instance->JSQR &= ~__HAL_ADC_JSQR(ADC_JSQR_JSQ1, sConfigInjected->InjectedRank,sConfigInjected->InjectedNbrOfConversion);
655 /* Set the SQx bits for the selected rank */
656 hadc->Instance->JSQR |= __HAL_ADC_JSQR(sConfigInjected->InjectedChannel, sConfigInjected->InjectedRank,sConfigInjected->InjectedNbrOfConversion);
658 /* Select external trigger to start conversion */
659 hadc->Instance->CR2 &= ~(ADC_CR2_JEXTSEL);
660 hadc->Instance->CR2 |= sConfigInjected->ExternalTrigInjecConv;
662 /* Select external trigger polarity */
663 hadc->Instance->CR2 &= ~(ADC_CR2_JEXTEN);
664 hadc->Instance->CR2 |= sConfigInjected->ExternalTrigInjecConvEdge;
666 if (sConfigInjected->AutoInjectedConv != DISABLE)
668 /* Enable the selected ADC automatic injected group conversion */
669 hadc->Instance->CR1 |= ADC_CR1_JAUTO;
673 /* Disable the selected ADC automatic injected group conversion */
674 hadc->Instance->CR1 &= ~(ADC_CR1_JAUTO);
677 if (sConfigInjected->InjectedDiscontinuousConvMode != DISABLE)
679 /* Enable the selected ADC injected discontinuous mode */
680 hadc->Instance->CR1 |= ADC_CR1_JDISCEN;
684 /* Disable the selected ADC injected discontinuous mode */
685 hadc->Instance->CR1 &= ~(ADC_CR1_JDISCEN);
688 switch(sConfigInjected->InjectedRank)
691 /* Set injected channel 1 offset */
692 hadc->Instance->JOFR1 &= ~(ADC_JOFR1_JOFFSET1);
693 hadc->Instance->JOFR1 |= sConfigInjected->InjectedOffset;
696 /* Set injected channel 2 offset */
697 hadc->Instance->JOFR2 &= ~(ADC_JOFR2_JOFFSET2);
698 hadc->Instance->JOFR2 |= sConfigInjected->InjectedOffset;
701 /* Set injected channel 3 offset */
702 hadc->Instance->JOFR3 &= ~(ADC_JOFR3_JOFFSET3);
703 hadc->Instance->JOFR3 |= sConfigInjected->InjectedOffset;
706 /* Set injected channel 4 offset */
707 hadc->Instance->JOFR4 &= ~(ADC_JOFR4_JOFFSET4);
708 hadc->Instance->JOFR4 |= sConfigInjected->InjectedOffset;
712 /* if ADC1 Channel_18 is selected enable VBAT Channel */
713 if ((hadc->Instance == ADC1) && (sConfigInjected->InjectedChannel == ADC_CHANNEL_VBAT))
715 /* Enable the VBAT channel*/
716 ADC->CCR |= ADC_CCR_VBATE;
719 /* if ADC1 Channel_16 or Channel_17 is selected enable TSVREFE Channel(Temperature sensor and VREFINT) */
720 if ((hadc->Instance == ADC1) && ((sConfigInjected->InjectedChannel == ADC_CHANNEL_TEMPSENSOR) || (sConfigInjected->InjectedChannel == ADC_CHANNEL_VREFINT)))
722 /* Enable the TSVREFE channel*/
723 ADC->CCR |= ADC_CCR_TSVREFE;
726 /* Process unlocked */
729 /* Return function status */
734 * @brief Configures the ADC multi-mode
735 * @param hadc : pointer to a ADC_HandleTypeDef structure that contains
736 * the configuration information for the specified ADC.
737 * @param multimode : pointer to an ADC_MultiModeTypeDef structure that contains
738 * the configuration information for multimode.
741 HAL_StatusTypeDef HAL_ADCEx_MultiModeConfigChannel(ADC_HandleTypeDef* hadc, ADC_MultiModeTypeDef* multimode)
743 /* Check the parameters */
744 assert_param(IS_ADC_MODE(multimode->Mode));
745 assert_param(IS_ADC_DMA_ACCESS_MODE(multimode->DMAAccessMode));
746 assert_param(IS_ADC_SAMPLING_DELAY(multimode->TwoSamplingDelay));
752 ADC->CCR &= ~(ADC_CCR_MULTI);
753 ADC->CCR |= multimode->Mode;
755 /* Set the ADC DMA access mode */
756 ADC->CCR &= ~(ADC_CCR_DMA);
757 ADC->CCR |= multimode->DMAAccessMode;
759 /* Set delay between two sampling phases */
760 ADC->CCR &= ~(ADC_CCR_DELAY);
761 ADC->CCR |= multimode->TwoSamplingDelay;
763 /* Process unlocked */
766 /* Return function status */
775 * @brief DMA transfer complete callback.
776 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
777 * the configuration information for the specified DMA module.
780 static void ADC_MultiModeDMAConvCplt(DMA_HandleTypeDef *hdma)
782 ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
784 /* Check if an injected conversion is ready */
785 if(hadc->State == HAL_ADC_STATE_EOC_INJ)
787 /* Change ADC state */
788 hadc->State = HAL_ADC_STATE_EOC_INJ_REG;
792 /* Change ADC state */
793 hadc->State = HAL_ADC_STATE_EOC_REG;
796 HAL_ADC_ConvCpltCallback(hadc);
800 * @brief DMA half transfer complete callback.
801 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
802 * the configuration information for the specified DMA module.
805 static void ADC_MultiModeDMAHalfConvCplt(DMA_HandleTypeDef *hdma)
807 ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
808 /* Conversion complete callback */
809 HAL_ADC_ConvHalfCpltCallback(hadc);
813 * @brief DMA error callback
814 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
815 * the configuration information for the specified DMA module.
818 static void ADC_MultiModeDMAError(DMA_HandleTypeDef *hdma)
820 ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
821 hadc->State= HAL_ADC_STATE_ERROR;
822 /* Set ADC error code to DMA error */
823 hadc->ErrorCode |= HAL_ADC_ERROR_DMA;
824 HAL_ADC_ErrorCallback(hadc);
831 #endif /* HAL_ADC_MODULE_ENABLED */
840 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/