stm32f0xx_hal_adc_ex.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**
  2. ******************************************************************************
  3. * @file stm32f0xx_hal_adc_ex.c
  4. * @author MCD Application Team
  5. * @brief This file provides firmware functions to manage the following
  6. * functionalities of the Analog to Digital Convertor (ADC)
  7. * peripheral:
  8. * + Peripheral Control functions
  9. * Other functions (generic functions) are available in file
  10. * "stm32f0xx_hal_adc.c".
  11. *
  12. ******************************************************************************
  13. * @attention
  14. *
  15. * Copyright (c) 2016 STMicroelectronics.
  16. * All rights reserved.
  17. *
  18. * This software is licensed under terms that can be found in the LICENSE file
  19. * in the root directory of this software component.
  20. * If no LICENSE file comes with this software, it is provided AS-IS.
  21. *
  22. ******************************************************************************
  23. @verbatim
  24. [..]
  25. (@) Sections "ADC peripheral features" and "How to use this driver" are
  26. available in file of generic functions "stm32l1xx_hal_adc.c".
  27. [..]
  28. @endverbatim
  29. */
  30. /* Includes ------------------------------------------------------------------*/
  31. #include "stm32f0xx_hal.h"
  32. /** @addtogroup STM32F0xx_HAL_Driver
  33. * @{
  34. */
  35. /** @defgroup ADCEx ADCEx
  36. * @brief ADC HAL module driver
  37. * @{
  38. */
  39. #ifdef HAL_ADC_MODULE_ENABLED
  40. /* Private typedef -----------------------------------------------------------*/
  41. /* Private define ------------------------------------------------------------*/
  42. /** @defgroup ADCEx_Private_Constants ADCEx Private Constants
  43. * @{
  44. */
  45. /* Fixed timeout values for ADC calibration, enable settling time, disable */
  46. /* settling time. */
  47. /* Values defined to be higher than worst cases: low clock frequency, */
  48. /* maximum prescaler. */
  49. /* Ex of profile low frequency : Clock source at 0.1 MHz, ADC clock */
  50. /* prescaler 4. */
  51. /* Unit: ms */
  52. #define ADC_DISABLE_TIMEOUT 2
  53. #define ADC_CALIBRATION_TIMEOUT 2U
  54. /**
  55. * @}
  56. */
  57. /* Private macros -------------------------------------------------------------*/
  58. /* Private variables ---------------------------------------------------------*/
  59. /* Private function prototypes -----------------------------------------------*/
  60. /* Private functions ---------------------------------------------------------*/
  61. /** @defgroup ADCEx_Exported_Functions ADCEx Exported Functions
  62. * @{
  63. */
  64. /** @defgroup ADCEx_Exported_Functions_Group1 Extended Initialization/de-initialization functions
  65. * @brief Extended Initialization and Configuration functions
  66. *
  67. @verbatim
  68. ===============================================================================
  69. ##### IO operation functions #####
  70. ===============================================================================
  71. [..] This section provides functions allowing to:
  72. (+) Perform the ADC calibration.
  73. @endverbatim
  74. * @{
  75. */
  76. /**
  77. * @brief Perform an ADC automatic self-calibration
  78. * Calibration prerequisite: ADC must be disabled (execute this
  79. * function before HAL_ADC_Start() or after HAL_ADC_Stop() ).
  80. * @note Calibration factor can be read after calibration, using function
  81. * HAL_ADC_GetValue() (value on 7 bits: from DR[6;0]).
  82. * @param hadc ADC handle
  83. * @retval HAL status
  84. */
  85. HAL_StatusTypeDef HAL_ADCEx_Calibration_Start(ADC_HandleTypeDef* hadc)
  86. {
  87. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  88. uint32_t tickstart = 0U;
  89. uint32_t backup_setting_adc_dma_transfer = 0; /* Note: Variable not declared as volatile because register read is already declared as volatile */
  90. /* Check the parameters */
  91. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  92. /* Process locked */
  93. __HAL_LOCK(hadc);
  94. /* Calibration prerequisite: ADC must be disabled. */
  95. if (ADC_IS_ENABLE(hadc) == RESET)
  96. {
  97. /* Set ADC state */
  98. ADC_STATE_CLR_SET(hadc->State,
  99. HAL_ADC_STATE_REG_BUSY,
  100. HAL_ADC_STATE_BUSY_INTERNAL);
  101. /* Disable ADC DMA transfer request during calibration */
  102. /* Note: Specificity of this STM32 series: Calibration factor is */
  103. /* available in data register and also transferred by DMA. */
  104. /* To not insert ADC calibration factor among ADC conversion data */
  105. /* in array variable, DMA transfer must be disabled during */
  106. /* calibration. */
  107. backup_setting_adc_dma_transfer = READ_BIT(hadc->Instance->CFGR1, ADC_CFGR1_DMAEN | ADC_CFGR1_DMACFG);
  108. CLEAR_BIT(hadc->Instance->CFGR1, ADC_CFGR1_DMAEN | ADC_CFGR1_DMACFG);
  109. /* Start ADC calibration */
  110. hadc->Instance->CR |= ADC_CR_ADCAL;
  111. tickstart = HAL_GetTick();
  112. /* Wait for calibration completion */
  113. while(HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADCAL))
  114. {
  115. if((HAL_GetTick() - tickstart) > ADC_CALIBRATION_TIMEOUT)
  116. {
  117. /* New check to avoid false timeout detection in case of preemption */
  118. if(HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADCAL))
  119. {
  120. /* Update ADC state machine to error */
  121. ADC_STATE_CLR_SET(hadc->State,
  122. HAL_ADC_STATE_BUSY_INTERNAL,
  123. HAL_ADC_STATE_ERROR_INTERNAL);
  124. /* Process unlocked */
  125. __HAL_UNLOCK(hadc);
  126. return HAL_ERROR;
  127. }
  128. }
  129. }
  130. /* Restore ADC DMA transfer request after calibration */
  131. SET_BIT(hadc->Instance->CFGR1, backup_setting_adc_dma_transfer);
  132. /* Set ADC state */
  133. ADC_STATE_CLR_SET(hadc->State,
  134. HAL_ADC_STATE_BUSY_INTERNAL,
  135. HAL_ADC_STATE_READY);
  136. }
  137. else
  138. {
  139. /* Update ADC state machine to error */
  140. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  141. tmp_hal_status = HAL_ERROR;
  142. }
  143. /* Process unlocked */
  144. __HAL_UNLOCK(hadc);
  145. /* Return function status */
  146. return tmp_hal_status;
  147. }
  148. /**
  149. * @}
  150. */
  151. /**
  152. * @}
  153. */
  154. #endif /* HAL_ADC_MODULE_ENABLED */
  155. /**
  156. * @}
  157. */
  158. /**
  159. * @}
  160. */