STM32F4xx_HAL_Driver  1.8.3
stm32f4xx_hal_adc_ex.c
Go to the documentation of this file.
1 
85 /* Includes ------------------------------------------------------------------*/
86 #include "stm32f4xx_hal.h"
87 
97 #ifdef HAL_ADC_MODULE_ENABLED
98 
99 /* Private typedef -----------------------------------------------------------*/
100 /* Private define ------------------------------------------------------------*/
101 /* Private macro -------------------------------------------------------------*/
102 /* Private variables ---------------------------------------------------------*/
106 /* Private function prototypes -----------------------------------------------*/
107 static void ADC_MultiModeDMAConvCplt(DMA_HandleTypeDef *hdma);
108 static void ADC_MultiModeDMAError(DMA_HandleTypeDef *hdma);
109 static void ADC_MultiModeDMAHalfConvCplt(DMA_HandleTypeDef *hdma);
114 /* Exported functions --------------------------------------------------------*/
147 {
148  __IO uint32_t counter = 0U;
149  uint32_t tmp1 = 0U, tmp2 = 0U;
150  ADC_Common_TypeDef *tmpADC_Common;
151 
152  /* Process locked */
153  __HAL_LOCK(hadc);
154 
155  /* Enable the ADC peripheral */
156 
157  /* Check if ADC peripheral is disabled in order to enable it and wait during
158  Tstab time the ADC's stabilization */
159  if ((hadc->Instance->CR2 & ADC_CR2_ADON) != ADC_CR2_ADON)
160  {
161  /* Enable the Peripheral */
162  __HAL_ADC_ENABLE(hadc);
163 
164  /* Delay for ADC stabilization time */
165  /* Compute number of CPU cycles to wait for */
166  counter = (ADC_STAB_DELAY_US * (SystemCoreClock / 1000000U));
167  while (counter != 0U)
168  {
169  counter--;
170  }
171  }
172 
173  /* Start conversion if ADC is effectively enabled */
174  if (HAL_IS_BIT_SET(hadc->Instance->CR2, ADC_CR2_ADON))
175  {
176  /* Set ADC state */
177  /* - Clear state bitfield related to injected group conversion results */
178  /* - Set state bitfield related to injected operation */
179  ADC_STATE_CLR_SET(hadc->State,
180  HAL_ADC_STATE_READY | HAL_ADC_STATE_INJ_EOC,
181  HAL_ADC_STATE_INJ_BUSY);
182 
183  /* Check if a regular conversion is ongoing */
184  /* Note: On this device, there is no ADC error code fields related to */
185  /* conversions on group injected only. In case of conversion on */
186  /* going on group regular, no error code is reset. */
187  if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_REG_BUSY))
188  {
189  /* Reset ADC all error code fields */
190  ADC_CLEAR_ERRORCODE(hadc);
191  }
192 
193  /* Process unlocked */
194  /* Unlock before starting ADC conversions: in case of potential */
195  /* interruption, to let the process to ADC IRQ Handler. */
196  __HAL_UNLOCK(hadc);
197 
198  /* Clear injected group conversion flag */
199  /* (To ensure of no unknown state from potential previous ADC operations) */
200  __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOC);
201 
202  /* Pointer to the common control register to which is belonging hadc */
203  /* (Depending on STM32F4 product, there may be up to 3 ADC and 1 common */
204  /* control register) */
205  tmpADC_Common = ADC_COMMON_REGISTER(hadc);
206 
207  /* Check if Multimode enabled */
208  if (HAL_IS_BIT_CLR(tmpADC_Common->CCR, ADC_CCR_MULTI))
209  {
210  tmp1 = HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_JEXTEN);
211  tmp2 = HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO);
212  if (tmp1 && tmp2)
213  {
214  /* Enable the selected ADC software conversion for injected group */
215  hadc->Instance->CR2 |= ADC_CR2_JSWSTART;
216  }
217  }
218  else
219  {
220  tmp1 = HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_JEXTEN);
221  tmp2 = HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO);
222  if ((hadc->Instance == ADC1) && tmp1 && tmp2)
223  {
224  /* Enable the selected ADC software conversion for injected group */
225  hadc->Instance->CR2 |= ADC_CR2_JSWSTART;
226  }
227  }
228  }
229  else
230  {
231  /* Update ADC state machine to error */
232  SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
233 
234  /* Set ADC error code to ADC IP internal error */
235  SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
236  }
237 
238  /* Return function status */
239  return HAL_OK;
240 }
241 
250 {
251  __IO uint32_t counter = 0U;
252  uint32_t tmp1 = 0U, tmp2 = 0U;
253  ADC_Common_TypeDef *tmpADC_Common;
254 
255  /* Process locked */
256  __HAL_LOCK(hadc);
257 
258  /* Enable the ADC peripheral */
259 
260  /* Check if ADC peripheral is disabled in order to enable it and wait during
261  Tstab time the ADC's stabilization */
262  if ((hadc->Instance->CR2 & ADC_CR2_ADON) != ADC_CR2_ADON)
263  {
264  /* Enable the Peripheral */
265  __HAL_ADC_ENABLE(hadc);
266 
267  /* Delay for ADC stabilization time */
268  /* Compute number of CPU cycles to wait for */
269  counter = (ADC_STAB_DELAY_US * (SystemCoreClock / 1000000U));
270  while (counter != 0U)
271  {
272  counter--;
273  }
274  }
275 
276  /* Start conversion if ADC is effectively enabled */
277  if (HAL_IS_BIT_SET(hadc->Instance->CR2, ADC_CR2_ADON))
278  {
279  /* Set ADC state */
280  /* - Clear state bitfield related to injected group conversion results */
281  /* - Set state bitfield related to injected operation */
282  ADC_STATE_CLR_SET(hadc->State,
283  HAL_ADC_STATE_READY | HAL_ADC_STATE_INJ_EOC,
284  HAL_ADC_STATE_INJ_BUSY);
285 
286  /* Check if a regular conversion is ongoing */
287  /* Note: On this device, there is no ADC error code fields related to */
288  /* conversions on group injected only. In case of conversion on */
289  /* going on group regular, no error code is reset. */
290  if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_REG_BUSY))
291  {
292  /* Reset ADC all error code fields */
293  ADC_CLEAR_ERRORCODE(hadc);
294  }
295 
296  /* Process unlocked */
297  /* Unlock before starting ADC conversions: in case of potential */
298  /* interruption, to let the process to ADC IRQ Handler. */
299  __HAL_UNLOCK(hadc);
300 
301  /* Clear injected group conversion flag */
302  /* (To ensure of no unknown state from potential previous ADC operations) */
303  __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOC);
304 
305  /* Enable end of conversion interrupt for injected channels */
306  __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOC);
307 
308  /* Pointer to the common control register to which is belonging hadc */
309  /* (Depending on STM32F4 product, there may be up to 3 ADC and 1 common */
310  /* control register) */
311  tmpADC_Common = ADC_COMMON_REGISTER(hadc);
312 
313  /* Check if Multimode enabled */
314  if (HAL_IS_BIT_CLR(tmpADC_Common->CCR, ADC_CCR_MULTI))
315  {
316  tmp1 = HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_JEXTEN);
317  tmp2 = HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO);
318  if (tmp1 && tmp2)
319  {
320  /* Enable the selected ADC software conversion for injected group */
321  hadc->Instance->CR2 |= ADC_CR2_JSWSTART;
322  }
323  }
324  else
325  {
326  tmp1 = HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_JEXTEN);
327  tmp2 = HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO);
328  if ((hadc->Instance == ADC1) && tmp1 && tmp2)
329  {
330  /* Enable the selected ADC software conversion for injected group */
331  hadc->Instance->CR2 |= ADC_CR2_JSWSTART;
332  }
333  }
334  }
335  else
336  {
337  /* Update ADC state machine to error */
338  SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
339 
340  /* Set ADC error code to ADC IP internal error */
341  SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
342  }
343 
344  /* Return function status */
345  return HAL_OK;
346 }
347 
361 {
362  HAL_StatusTypeDef tmp_hal_status = HAL_OK;
363 
364  /* Check the parameters */
365  assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
366 
367  /* Process locked */
368  __HAL_LOCK(hadc);
369 
370  /* Stop potential conversion and disable ADC peripheral */
371  /* Conditioned to: */
372  /* - No conversion on the other group (regular group) is intended to */
373  /* continue (injected and regular groups stop conversion and ADC disable */
374  /* are common) */
375  /* - In case of auto-injection mode, HAL_ADC_Stop must be used. */
376  if (((hadc->State & HAL_ADC_STATE_REG_BUSY) == RESET) &&
377  HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO))
378  {
379  /* Stop potential conversion on going, on regular and injected groups */
380  /* Disable ADC peripheral */
381  __HAL_ADC_DISABLE(hadc);
382 
383  /* Check if ADC is effectively disabled */
384  if (HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_ADON))
385  {
386  /* Set ADC state */
387  ADC_STATE_CLR_SET(hadc->State,
388  HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
389  HAL_ADC_STATE_READY);
390  }
391  }
392  else
393  {
394  /* Update ADC state machine to error */
395  SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
396 
397  tmp_hal_status = HAL_ERROR;
398  }
399 
400  /* Process unlocked */
401  __HAL_UNLOCK(hadc);
402 
403  /* Return function status */
404  return tmp_hal_status;
405 }
406 
414 HAL_StatusTypeDef HAL_ADCEx_InjectedPollForConversion(ADC_HandleTypeDef *hadc, uint32_t Timeout)
415 {
416  uint32_t tickstart = 0U;
417 
418  /* Get tick */
419  tickstart = HAL_GetTick();
420 
421  /* Check End of conversion flag */
422  while (!(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_JEOC)))
423  {
424  /* Check for the Timeout */
425  if (Timeout != HAL_MAX_DELAY)
426  {
427  if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
428  {
429  /* New check to avoid false timeout detection in case of preemption */
430  if (!(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_JEOC)))
431  {
432  hadc->State = HAL_ADC_STATE_TIMEOUT;
433  /* Process unlocked */
434  __HAL_UNLOCK(hadc);
435  return HAL_TIMEOUT;
436  }
437  }
438  }
439  }
440 
441  /* Clear injected group conversion flag */
442  __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JSTRT | ADC_FLAG_JEOC);
443 
444  /* Update ADC state machine */
445  SET_BIT(hadc->State, HAL_ADC_STATE_INJ_EOC);
446 
447  /* Determine whether any further conversion upcoming on group injected */
448  /* by external trigger, continuous mode or scan sequence on going. */
449  /* Note: On STM32F4, there is no independent flag of end of sequence. */
450  /* The test of scan sequence on going is done either with scan */
451  /* sequence disabled or with end of conversion flag set to */
452  /* of end of sequence. */
453  if (ADC_IS_SOFTWARE_START_INJECTED(hadc) &&
454  (HAL_IS_BIT_CLR(hadc->Instance->JSQR, ADC_JSQR_JL) ||
455  HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_EOCS)) &&
456  (HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO) &&
457  (ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
458  (hadc->Init.ContinuousConvMode == DISABLE))))
459  {
460  /* Set ADC state */
461  CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
462 
463  if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_REG_BUSY))
464  {
465  SET_BIT(hadc->State, HAL_ADC_STATE_READY);
466  }
467  }
468 
469  /* Return ADC state */
470  return HAL_OK;
471 }
472 
486 {
487  HAL_StatusTypeDef tmp_hal_status = HAL_OK;
488 
489  /* Check the parameters */
490  assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
491 
492  /* Process locked */
493  __HAL_LOCK(hadc);
494 
495  /* Stop potential conversion and disable ADC peripheral */
496  /* Conditioned to: */
497  /* - No conversion on the other group (regular group) is intended to */
498  /* continue (injected and regular groups stop conversion and ADC disable */
499  /* are common) */
500  /* - In case of auto-injection mode, HAL_ADC_Stop must be used. */
501  if (((hadc->State & HAL_ADC_STATE_REG_BUSY) == RESET) &&
502  HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO))
503  {
504  /* Stop potential conversion on going, on regular and injected groups */
505  /* Disable ADC peripheral */
506  __HAL_ADC_DISABLE(hadc);
507 
508  /* Check if ADC is effectively disabled */
509  if (HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_ADON))
510  {
511  /* Disable ADC end of conversion interrupt for injected channels */
512  __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC);
513 
514  /* Set ADC state */
515  ADC_STATE_CLR_SET(hadc->State,
516  HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
517  HAL_ADC_STATE_READY);
518  }
519  }
520  else
521  {
522  /* Update ADC state machine to error */
523  SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
524 
525  tmp_hal_status = HAL_ERROR;
526  }
527 
528  /* Process unlocked */
529  __HAL_UNLOCK(hadc);
530 
531  /* Return function status */
532  return tmp_hal_status;
533 }
534 
547 uint32_t HAL_ADCEx_InjectedGetValue(ADC_HandleTypeDef *hadc, uint32_t InjectedRank)
548 {
549  __IO uint32_t tmp = 0U;
550 
551  /* Check the parameters */
552  assert_param(IS_ADC_INJECTED_RANK(InjectedRank));
553 
554  /* Clear injected group conversion flag to have similar behaviour as */
555  /* regular group: reading data register also clears end of conversion flag. */
556  __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOC);
557 
558  /* Return the selected ADC converted value */
559  switch (InjectedRank)
560  {
561  case ADC_INJECTED_RANK_4:
562  {
563  tmp = hadc->Instance->JDR4;
564  }
565  break;
566  case ADC_INJECTED_RANK_3:
567  {
568  tmp = hadc->Instance->JDR3;
569  }
570  break;
571  case ADC_INJECTED_RANK_2:
572  {
573  tmp = hadc->Instance->JDR2;
574  }
575  break;
576  case ADC_INJECTED_RANK_1:
577  {
578  tmp = hadc->Instance->JDR1;
579  }
580  break;
581  default:
582  break;
583  }
584  return tmp;
585 }
586 
598 HAL_StatusTypeDef HAL_ADCEx_MultiModeStart_DMA(ADC_HandleTypeDef *hadc, uint32_t *pData, uint32_t Length)
599 {
600  __IO uint32_t counter = 0U;
601  ADC_Common_TypeDef *tmpADC_Common;
602 
603  /* Check the parameters */
604  assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
605  assert_param(IS_ADC_EXT_TRIG_EDGE(hadc->Init.ExternalTrigConvEdge));
606  assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DMAContinuousRequests));
607 
608  /* Process locked */
609  __HAL_LOCK(hadc);
610 
611  /* Check if ADC peripheral is disabled in order to enable it and wait during
612  Tstab time the ADC's stabilization */
613  if ((hadc->Instance->CR2 & ADC_CR2_ADON) != ADC_CR2_ADON)
614  {
615  /* Enable the Peripheral */
616  __HAL_ADC_ENABLE(hadc);
617 
618  /* Delay for temperature sensor stabilization time */
619  /* Compute number of CPU cycles to wait for */
620  counter = (ADC_STAB_DELAY_US * (SystemCoreClock / 1000000U));
621  while (counter != 0U)
622  {
623  counter--;
624  }
625  }
626 
627  /* Start conversion if ADC is effectively enabled */
628  if (HAL_IS_BIT_SET(hadc->Instance->CR2, ADC_CR2_ADON))
629  {
630  /* Set ADC state */
631  /* - Clear state bitfield related to regular group conversion results */
632  /* - Set state bitfield related to regular group operation */
633  ADC_STATE_CLR_SET(hadc->State,
634  HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR,
635  HAL_ADC_STATE_REG_BUSY);
636 
637  /* If conversions on group regular are also triggering group injected, */
638  /* update ADC state. */
639  if (READ_BIT(hadc->Instance->CR1, ADC_CR1_JAUTO) != RESET)
640  {
641  ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
642  }
643 
644  /* State machine update: Check if an injected conversion is ongoing */
645  if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_INJ_BUSY))
646  {
647  /* Reset ADC error code fields related to conversions on group regular */
648  CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA));
649  }
650  else
651  {
652  /* Reset ADC all error code fields */
653  ADC_CLEAR_ERRORCODE(hadc);
654  }
655 
656  /* Process unlocked */
657  /* Unlock before starting ADC conversions: in case of potential */
658  /* interruption, to let the process to ADC IRQ Handler. */
659  __HAL_UNLOCK(hadc);
660 
661  /* Set the DMA transfer complete callback */
662  hadc->DMA_Handle->XferCpltCallback = ADC_MultiModeDMAConvCplt;
663 
664  /* Set the DMA half transfer complete callback */
665  hadc->DMA_Handle->XferHalfCpltCallback = ADC_MultiModeDMAHalfConvCplt;
666 
667  /* Set the DMA error callback */
668  hadc->DMA_Handle->XferErrorCallback = ADC_MultiModeDMAError ;
669 
670  /* Manage ADC and DMA start: ADC overrun interruption, DMA start, ADC */
671  /* start (in case of SW start): */
672 
673  /* Clear regular group conversion flag and overrun flag */
674  /* (To ensure of no unknown state from potential previous ADC operations) */
675  __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOC);
676 
677  /* Enable ADC overrun interrupt */
678  __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);
679 
680  /* Pointer to the common control register to which is belonging hadc */
681  /* (Depending on STM32F4 product, there may be up to 3 ADC and 1 common */
682  /* control register) */
683  tmpADC_Common = ADC_COMMON_REGISTER(hadc);
684 
685  if (hadc->Init.DMAContinuousRequests != DISABLE)
686  {
687  /* Enable the selected ADC DMA request after last transfer */
688  tmpADC_Common->CCR |= ADC_CCR_DDS;
689  }
690  else
691  {
692  /* Disable the selected ADC EOC rising on each regular channel conversion */
693  tmpADC_Common->CCR &= ~ADC_CCR_DDS;
694  }
695 
696  /* Enable the DMA Stream */
697  HAL_DMA_Start_IT(hadc->DMA_Handle, (uint32_t)&tmpADC_Common->CDR, (uint32_t)pData, Length);
698 
699  /* if no external trigger present enable software conversion of regular channels */
700  if ((hadc->Instance->CR2 & ADC_CR2_EXTEN) == RESET)
701  {
702  /* Enable the selected ADC software conversion for regular group */
703  hadc->Instance->CR2 |= (uint32_t)ADC_CR2_SWSTART;
704  }
705  }
706  else
707  {
708  /* Update ADC state machine to error */
709  SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
710 
711  /* Set ADC error code to ADC IP internal error */
712  SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
713  }
714 
715  /* Return function status */
716  return HAL_OK;
717 }
718 
726 {
727  HAL_StatusTypeDef tmp_hal_status = HAL_OK;
728  ADC_Common_TypeDef *tmpADC_Common;
729 
730  /* Check the parameters */
731  assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
732 
733  /* Process locked */
734  __HAL_LOCK(hadc);
735 
736  /* Stop potential conversion on going, on regular and injected groups */
737  /* Disable ADC peripheral */
738  __HAL_ADC_DISABLE(hadc);
739 
740  /* Pointer to the common control register to which is belonging hadc */
741  /* (Depending on STM32F4 product, there may be up to 3 ADC and 1 common */
742  /* control register) */
743  tmpADC_Common = ADC_COMMON_REGISTER(hadc);
744 
745  /* Check if ADC is effectively disabled */
746  if (HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_ADON))
747  {
748  /* Disable the selected ADC DMA mode for multimode */
749  tmpADC_Common->CCR &= ~ADC_CCR_DDS;
750 
751  /* Disable the DMA channel (in case of DMA in circular mode or stop while */
752  /* DMA transfer is on going) */
753  tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
754 
755  /* Disable ADC overrun interrupt */
756  __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
757 
758  /* Set ADC state */
759  ADC_STATE_CLR_SET(hadc->State,
760  HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
761  HAL_ADC_STATE_READY);
762  }
763 
764  /* Process unlocked */
765  __HAL_UNLOCK(hadc);
766 
767  /* Return function status */
768  return tmp_hal_status;
769 }
770 
779 {
780  ADC_Common_TypeDef *tmpADC_Common;
781 
782  /* Prevent unused argument(s) compilation warning */
783  UNUSED(hadc);
784 
785  /* Pointer to the common control register to which is belonging hadc */
786  /* (Depending on STM32F4 product, there may be up to 3 ADC and 1 common */
787  /* control register) */
788  tmpADC_Common = ADC_COMMON_REGISTER(hadc);
789 
790  /* Return the multi mode conversion value */
791  return tmpADC_Common->CDR;
792 }
793 
801 {
802  /* Prevent unused argument(s) compilation warning */
803  UNUSED(hadc);
804  /* NOTE : This function Should not be modified, when the callback is needed,
805  the HAL_ADC_InjectedConvCpltCallback could be implemented in the user file
806  */
807 }
808 
818 {
819 
820 #ifdef USE_FULL_ASSERT
821  uint32_t tmp = 0U;
822 
823 #endif /* USE_FULL_ASSERT */
824 
825  ADC_Common_TypeDef *tmpADC_Common;
826 
827  /* Check the parameters */
828  assert_param(IS_ADC_CHANNEL(sConfigInjected->InjectedChannel));
829  assert_param(IS_ADC_INJECTED_RANK(sConfigInjected->InjectedRank));
830  assert_param(IS_ADC_SAMPLE_TIME(sConfigInjected->InjectedSamplingTime));
831  assert_param(IS_ADC_EXT_INJEC_TRIG(sConfigInjected->ExternalTrigInjecConv));
832  assert_param(IS_ADC_INJECTED_LENGTH(sConfigInjected->InjectedNbrOfConversion));
833  assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->AutoInjectedConv));
834  assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->InjectedDiscontinuousConvMode));
835 
836 #ifdef USE_FULL_ASSERT
837  tmp = ADC_GET_RESOLUTION(hadc);
838  assert_param(IS_ADC_RANGE(tmp, sConfigInjected->InjectedOffset));
839 #endif /* USE_FULL_ASSERT */
840 
841  if (sConfigInjected->ExternalTrigInjecConv != ADC_INJECTED_SOFTWARE_START)
842  {
843  assert_param(IS_ADC_EXT_INJEC_TRIG_EDGE(sConfigInjected->ExternalTrigInjecConvEdge));
844  }
845 
846  /* Process locked */
847  __HAL_LOCK(hadc);
848 
849  /* if ADC_Channel_10 ... ADC_Channel_18 is selected */
850  if (sConfigInjected->InjectedChannel > ADC_CHANNEL_9)
851  {
852  /* Clear the old sample time */
853  hadc->Instance->SMPR1 &= ~ADC_SMPR1(ADC_SMPR1_SMP10, sConfigInjected->InjectedChannel);
854 
855  /* Set the new sample time */
856  hadc->Instance->SMPR1 |= ADC_SMPR1(sConfigInjected->InjectedSamplingTime, sConfigInjected->InjectedChannel);
857  }
858  else /* ADC_Channel include in ADC_Channel_[0..9] */
859  {
860  /* Clear the old sample time */
861  hadc->Instance->SMPR2 &= ~ADC_SMPR2(ADC_SMPR2_SMP0, sConfigInjected->InjectedChannel);
862 
863  /* Set the new sample time */
864  hadc->Instance->SMPR2 |= ADC_SMPR2(sConfigInjected->InjectedSamplingTime, sConfigInjected->InjectedChannel);
865  }
866 
867  /*---------------------------- ADCx JSQR Configuration -----------------*/
868  hadc->Instance->JSQR &= ~(ADC_JSQR_JL);
869  hadc->Instance->JSQR |= ADC_SQR1(sConfigInjected->InjectedNbrOfConversion);
870 
871  /* Rank configuration */
872 
873  /* Clear the old SQx bits for the selected rank */
874  hadc->Instance->JSQR &= ~ADC_JSQR(ADC_JSQR_JSQ1, sConfigInjected->InjectedRank, sConfigInjected->InjectedNbrOfConversion);
875 
876  /* Set the SQx bits for the selected rank */
877  hadc->Instance->JSQR |= ADC_JSQR(sConfigInjected->InjectedChannel, sConfigInjected->InjectedRank, sConfigInjected->InjectedNbrOfConversion);
878 
879  /* Enable external trigger if trigger selection is different of software */
880  /* start. */
881  /* Note: This configuration keeps the hardware feature of parameter */
882  /* ExternalTrigConvEdge "trigger edge none" equivalent to */
883  /* software start. */
884  if (sConfigInjected->ExternalTrigInjecConv != ADC_INJECTED_SOFTWARE_START)
885  {
886  /* Select external trigger to start conversion */
887  hadc->Instance->CR2 &= ~(ADC_CR2_JEXTSEL);
888  hadc->Instance->CR2 |= sConfigInjected->ExternalTrigInjecConv;
889 
890  /* Select external trigger polarity */
891  hadc->Instance->CR2 &= ~(ADC_CR2_JEXTEN);
892  hadc->Instance->CR2 |= sConfigInjected->ExternalTrigInjecConvEdge;
893  }
894  else
895  {
896  /* Reset the external trigger */
897  hadc->Instance->CR2 &= ~(ADC_CR2_JEXTSEL);
898  hadc->Instance->CR2 &= ~(ADC_CR2_JEXTEN);
899  }
900 
901  if (sConfigInjected->AutoInjectedConv != DISABLE)
902  {
903  /* Enable the selected ADC automatic injected group conversion */
904  hadc->Instance->CR1 |= ADC_CR1_JAUTO;
905  }
906  else
907  {
908  /* Disable the selected ADC automatic injected group conversion */
909  hadc->Instance->CR1 &= ~(ADC_CR1_JAUTO);
910  }
911 
912  if (sConfigInjected->InjectedDiscontinuousConvMode != DISABLE)
913  {
914  /* Enable the selected ADC injected discontinuous mode */
915  hadc->Instance->CR1 |= ADC_CR1_JDISCEN;
916  }
917  else
918  {
919  /* Disable the selected ADC injected discontinuous mode */
920  hadc->Instance->CR1 &= ~(ADC_CR1_JDISCEN);
921  }
922 
923  switch (sConfigInjected->InjectedRank)
924  {
925  case 1U:
926  /* Set injected channel 1 offset */
927  hadc->Instance->JOFR1 &= ~(ADC_JOFR1_JOFFSET1);
928  hadc->Instance->JOFR1 |= sConfigInjected->InjectedOffset;
929  break;
930  case 2U:
931  /* Set injected channel 2 offset */
932  hadc->Instance->JOFR2 &= ~(ADC_JOFR2_JOFFSET2);
933  hadc->Instance->JOFR2 |= sConfigInjected->InjectedOffset;
934  break;
935  case 3U:
936  /* Set injected channel 3 offset */
937  hadc->Instance->JOFR3 &= ~(ADC_JOFR3_JOFFSET3);
938  hadc->Instance->JOFR3 |= sConfigInjected->InjectedOffset;
939  break;
940  default:
941  /* Set injected channel 4 offset */
942  hadc->Instance->JOFR4 &= ~(ADC_JOFR4_JOFFSET4);
943  hadc->Instance->JOFR4 |= sConfigInjected->InjectedOffset;
944  break;
945  }
946 
947  /* Pointer to the common control register to which is belonging hadc */
948  /* (Depending on STM32F4 product, there may be up to 3 ADC and 1 common */
949  /* control register) */
950  tmpADC_Common = ADC_COMMON_REGISTER(hadc);
951 
952  /* if ADC1 Channel_18 is selected enable VBAT Channel */
953  if ((hadc->Instance == ADC1) && (sConfigInjected->InjectedChannel == ADC_CHANNEL_VBAT))
954  {
955  /* Enable the VBAT channel*/
956  tmpADC_Common->CCR |= ADC_CCR_VBATE;
957  }
958 
959  /* if ADC1 Channel_16 or Channel_17 is selected enable TSVREFE Channel(Temperature sensor and VREFINT) */
960  if ((hadc->Instance == ADC1) && ((sConfigInjected->InjectedChannel == ADC_CHANNEL_TEMPSENSOR) || (sConfigInjected->InjectedChannel == ADC_CHANNEL_VREFINT)))
961  {
962  /* Enable the TSVREFE channel*/
963  tmpADC_Common->CCR |= ADC_CCR_TSVREFE;
964  }
965 
966  /* Process unlocked */
967  __HAL_UNLOCK(hadc);
968 
969  /* Return function status */
970  return HAL_OK;
971 }
972 
982 {
983 
984  ADC_Common_TypeDef *tmpADC_Common;
985 
986  /* Check the parameters */
987  assert_param(IS_ADC_MODE(multimode->Mode));
988  assert_param(IS_ADC_DMA_ACCESS_MODE(multimode->DMAAccessMode));
989  assert_param(IS_ADC_SAMPLING_DELAY(multimode->TwoSamplingDelay));
990 
991  /* Process locked */
992  __HAL_LOCK(hadc);
993 
994  /* Pointer to the common control register to which is belonging hadc */
995  /* (Depending on STM32F4 product, there may be up to 3 ADC and 1 common */
996  /* control register) */
997  tmpADC_Common = ADC_COMMON_REGISTER(hadc);
998 
999  /* Set ADC mode */
1000  tmpADC_Common->CCR &= ~(ADC_CCR_MULTI);
1001  tmpADC_Common->CCR |= multimode->Mode;
1002 
1003  /* Set the ADC DMA access mode */
1004  tmpADC_Common->CCR &= ~(ADC_CCR_DMA);
1005  tmpADC_Common->CCR |= multimode->DMAAccessMode;
1006 
1007  /* Set delay between two sampling phases */
1008  tmpADC_Common->CCR &= ~(ADC_CCR_DELAY);
1009  tmpADC_Common->CCR |= multimode->TwoSamplingDelay;
1010 
1011  /* Process unlocked */
1012  __HAL_UNLOCK(hadc);
1013 
1014  /* Return function status */
1015  return HAL_OK;
1016 }
1017 
1028 static void ADC_MultiModeDMAConvCplt(DMA_HandleTypeDef *hdma)
1029 {
1030  /* Retrieve ADC handle corresponding to current DMA handle */
1031  ADC_HandleTypeDef *hadc = (ADC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
1032 
1033  /* Update state machine on conversion status if not in error state */
1034  if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL | HAL_ADC_STATE_ERROR_DMA))
1035  {
1036  /* Update ADC state machine */
1037  SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
1038 
1039  /* Determine whether any further conversion upcoming on group regular */
1040  /* by external trigger, continuous mode or scan sequence on going. */
1041  /* Note: On STM32F4, there is no independent flag of end of sequence. */
1042  /* The test of scan sequence on going is done either with scan */
1043  /* sequence disabled or with end of conversion flag set to */
1044  /* of end of sequence. */
1045  if (ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
1046  (hadc->Init.ContinuousConvMode == DISABLE) &&
1047  (HAL_IS_BIT_CLR(hadc->Instance->SQR1, ADC_SQR1_L) ||
1048  HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_EOCS)))
1049  {
1050  /* Disable ADC end of single conversion interrupt on group regular */
1051  /* Note: Overrun interrupt was enabled with EOC interrupt in */
1052  /* HAL_ADC_Start_IT(), but is not disabled here because can be used */
1053  /* by overrun IRQ process below. */
1054  __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC);
1055 
1056  /* Set ADC state */
1057  CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
1058 
1059  if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_INJ_BUSY))
1060  {
1061  SET_BIT(hadc->State, HAL_ADC_STATE_READY);
1062  }
1063  }
1064 
1065  /* Conversion complete callback */
1067  }
1068  else
1069  {
1070  /* Call DMA error callback */
1071  hadc->DMA_Handle->XferErrorCallback(hdma);
1072  }
1073 }
1074 
1081 static void ADC_MultiModeDMAHalfConvCplt(DMA_HandleTypeDef *hdma)
1082 {
1083  ADC_HandleTypeDef *hadc = (ADC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
1084  /* Conversion complete callback */
1086 }
1087 
1094 static void ADC_MultiModeDMAError(DMA_HandleTypeDef *hdma)
1095 {
1096  ADC_HandleTypeDef *hadc = (ADC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
1097  hadc->State = HAL_ADC_STATE_ERROR_DMA;
1098  /* Set ADC error code to DMA error */
1099  hadc->ErrorCode |= HAL_ADC_ERROR_DMA;
1100  HAL_ADC_ErrorCallback(hadc);
1101 }
1102 
1107 #endif /* HAL_ADC_MODULE_ENABLED */
HAL_StatusTypeDef HAL_ADCEx_InjectedConfigChannel(ADC_HandleTypeDef *hadc, ADC_InjectionConfTypeDef *sConfigInjected)
Configures for the selected ADC injected channel its corresponding rank in the sequencer and its samp...
uint32_t HAL_ADCEx_MultiModeGetValue(ADC_HandleTypeDef *hadc)
Returns the last ADC1, ADC2 and ADC3 regular conversions results data in the selected multi mode.
HAL_StatusTypeDef HAL_ADCEx_InjectedPollForConversion(ADC_HandleTypeDef *hadc, uint32_t Timeout)
Poll for injected conversion complete.
HAL_StatusTypeDef HAL_ADCEx_MultiModeStop_DMA(ADC_HandleTypeDef *hadc)
Disables ADC DMA (multi-ADC mode) and disables ADC peripheral.
uint32_t HAL_ADCEx_InjectedGetValue(ADC_HandleTypeDef *hadc, uint32_t InjectedRank)
Gets the converted value from data register of injected channel.
HAL_StatusTypeDef HAL_ADCEx_InjectedStop(ADC_HandleTypeDef *hadc)
Stop conversion of injected channels. Disable ADC peripheral if no regular conversion is on going.
HAL_StatusTypeDef HAL_ADCEx_InjectedStart(ADC_HandleTypeDef *hadc)
Enables the selected ADC software start conversion of the injected channels.
HAL_StatusTypeDef HAL_ADCEx_MultiModeStart_DMA(ADC_HandleTypeDef *hadc, uint32_t *pData, uint32_t Length)
Enables ADC DMA request after last transfer (Multi-ADC mode) and enables ADC peripheral.
HAL_StatusTypeDef HAL_ADCEx_MultiModeConfigChannel(ADC_HandleTypeDef *hadc, ADC_MultiModeTypeDef *multimode)
Configures the ADC multi-mode.
void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef *hadc)
Injected conversion complete callback in non blocking mode.
HAL_StatusTypeDef HAL_ADCEx_InjectedStop_IT(ADC_HandleTypeDef *hadc)
Stop conversion of injected channels, disable interruption of end-of-conversion. Disable ADC peripher...
HAL_StatusTypeDef HAL_ADCEx_InjectedStart_IT(ADC_HandleTypeDef *hadc)
Enables the interrupt and starts ADC conversion of injected channels.
FunctionalState InjectedDiscontinuousConvMode
ADC Configuration injected Channel structure definition.
ADC Configuration multi-mode structure definition.
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
Regular conversion complete callback in non blocking mode.
void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef *hadc)
Regular conversion half DMA transfer callback in non blocking mode.
void HAL_ADC_ErrorCallback(ADC_HandleTypeDef *hadc)
Error ADC callback.
struct __ADC_HandleTypeDef else typedef struct endif ADC_HandleTypeDef
ADC handle Structure definition.
HAL_StatusTypeDef HAL_DMA_Abort(DMA_HandleTypeDef *hdma)
Aborts the DMA Transfer.
HAL_StatusTypeDef HAL_DMA_Start_IT(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
Start the DMA Transfer with interrupt enabled.
uint32_t HAL_GetTick(void)
Provides a tick value in millisecond.
This file contains all the functions prototypes for the HAL module driver.
DMA handle Structure definition.