You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

205 lines
7.5 KiB

  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. * + Operation functions
  9. * ++ Calibration (ADC automatic self-calibration)
  10. * Other functions (generic functions) are available in file
  11. * "stm32f0xx_hal_adc.c".
  12. *
  13. @verbatim
  14. [..]
  15. (@) Sections "ADC peripheral features" and "How to use this driver" are
  16. available in file of generic functions "stm32l1xx_hal_adc.c".
  17. [..]
  18. @endverbatim
  19. ******************************************************************************
  20. * @attention
  21. *
  22. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  23. *
  24. * Redistribution and use in source and binary forms, with or without modification,
  25. * are permitted provided that the following conditions are met:
  26. * 1. Redistributions of source code must retain the above copyright notice,
  27. * this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright notice,
  29. * this list of conditions and the following disclaimer in the documentation
  30. * and/or other materials provided with the distribution.
  31. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  32. * may be used to endorse or promote products derived from this software
  33. * without specific prior written permission.
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  36. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  39. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  40. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  41. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  42. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  43. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. ******************************************************************************
  47. */
  48. /* Includes ------------------------------------------------------------------*/
  49. #include "stm32f0xx_hal.h"
  50. /** @addtogroup STM32F0xx_HAL_Driver
  51. * @{
  52. */
  53. /** @defgroup ADCEx ADCEx
  54. * @brief ADC HAL module driver
  55. * @{
  56. */
  57. #ifdef HAL_ADC_MODULE_ENABLED
  58. /* Private typedef -----------------------------------------------------------*/
  59. /* Private define ------------------------------------------------------------*/
  60. /** @defgroup ADCEx_Private_Constants ADCEx Private Constants
  61. * @{
  62. */
  63. /* Fixed timeout values for ADC calibration, enable settling time, disable */
  64. /* settling time. */
  65. /* Values defined to be higher than worst cases: low clock frequency, */
  66. /* maximum prescaler. */
  67. /* Ex of profile low frequency : Clock source at 0.1 MHz, ADC clock */
  68. /* prescaler 4. */
  69. /* Unit: ms */
  70. #define ADC_DISABLE_TIMEOUT 2
  71. #define ADC_CALIBRATION_TIMEOUT 2U
  72. /**
  73. * @}
  74. */
  75. /* Private macros -------------------------------------------------------------*/
  76. /* Private variables ---------------------------------------------------------*/
  77. /* Private function prototypes -----------------------------------------------*/
  78. /* Private functions ---------------------------------------------------------*/
  79. /** @defgroup ADCEx_Exported_Functions ADCEx Exported Functions
  80. * @{
  81. */
  82. /** @defgroup ADCEx_Exported_Functions_Group1 Extended Initialization/de-initialization functions
  83. * @brief Extended Initialization and Configuration functions
  84. *
  85. @verbatim
  86. ===============================================================================
  87. ##### IO operation functions #####
  88. ===============================================================================
  89. [..] This section provides functions allowing to:
  90. (+) Perform the ADC calibration.
  91. @endverbatim
  92. * @{
  93. */
  94. /**
  95. * @brief Perform an ADC automatic self-calibration
  96. * Calibration prerequisite: ADC must be disabled (execute this
  97. * function before HAL_ADC_Start() or after HAL_ADC_Stop() ).
  98. * @note Calibration factor can be read after calibration, using function
  99. * HAL_ADC_GetValue() (value on 7 bits: from DR[6;0]).
  100. * @param hadc ADC handle
  101. * @retval HAL status
  102. */
  103. HAL_StatusTypeDef HAL_ADCEx_Calibration_Start(ADC_HandleTypeDef* hadc)
  104. {
  105. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  106. uint32_t tickstart = 0U;
  107. uint32_t backup_setting_adc_dma_transfer = 0; /* Note: Variable not declared as volatile because register read is already declared as volatile */
  108. /* Check the parameters */
  109. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  110. /* Process locked */
  111. __HAL_LOCK(hadc);
  112. /* Calibration prerequisite: ADC must be disabled. */
  113. if (ADC_IS_ENABLE(hadc) == RESET)
  114. {
  115. /* Set ADC state */
  116. ADC_STATE_CLR_SET(hadc->State,
  117. HAL_ADC_STATE_REG_BUSY,
  118. HAL_ADC_STATE_BUSY_INTERNAL);
  119. /* Disable ADC DMA transfer request during calibration */
  120. /* Note: Specificity of this STM32 serie: Calibration factor is */
  121. /* available in data register and also transfered by DMA. */
  122. /* To not insert ADC calibration factor among ADC conversion data */
  123. /* in array variable, DMA transfer must be disabled during */
  124. /* calibration. */
  125. backup_setting_adc_dma_transfer = READ_BIT(hadc->Instance->CFGR1, ADC_CFGR1_DMAEN | ADC_CFGR1_DMACFG);
  126. CLEAR_BIT(hadc->Instance->CFGR1, ADC_CFGR1_DMAEN | ADC_CFGR1_DMACFG);
  127. /* Start ADC calibration */
  128. hadc->Instance->CR |= ADC_CR_ADCAL;
  129. tickstart = HAL_GetTick();
  130. /* Wait for calibration completion */
  131. while(HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADCAL))
  132. {
  133. if((HAL_GetTick() - tickstart) > ADC_CALIBRATION_TIMEOUT)
  134. {
  135. /* Update ADC state machine to error */
  136. ADC_STATE_CLR_SET(hadc->State,
  137. HAL_ADC_STATE_BUSY_INTERNAL,
  138. HAL_ADC_STATE_ERROR_INTERNAL);
  139. /* Process unlocked */
  140. __HAL_UNLOCK(hadc);
  141. return HAL_ERROR;
  142. }
  143. }
  144. /* Restore ADC DMA transfer request after calibration */
  145. SET_BIT(hadc->Instance->CFGR1, backup_setting_adc_dma_transfer);
  146. /* Set ADC state */
  147. ADC_STATE_CLR_SET(hadc->State,
  148. HAL_ADC_STATE_BUSY_INTERNAL,
  149. HAL_ADC_STATE_READY);
  150. }
  151. else
  152. {
  153. /* Update ADC state machine to error */
  154. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  155. tmp_hal_status = HAL_ERROR;
  156. }
  157. /* Process unlocked */
  158. __HAL_UNLOCK(hadc);
  159. /* Return function status */
  160. return tmp_hal_status;
  161. }
  162. /**
  163. * @}
  164. */
  165. /**
  166. * @}
  167. */
  168. #endif /* HAL_ADC_MODULE_ENABLED */
  169. /**
  170. * @}
  171. */
  172. /**
  173. * @}
  174. */
  175. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/