stm32l0xx_hal_adc_ex.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /**
  2. ******************************************************************************
  3. * @file stm32l0xx_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. * "stm32l0xx_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 "stm32l0xx_hal_adc.c".
  27. [..]
  28. @endverbatim
  29. ******************************************************************************
  30. */
  31. /* Includes ------------------------------------------------------------------*/
  32. #include "stm32l0xx_hal.h"
  33. /** @addtogroup STM32L0xx_HAL_Driver
  34. * @{
  35. */
  36. /** @defgroup ADCEx ADCEx
  37. * @brief ADC Extended HAL module driver
  38. * @{
  39. */
  40. #ifdef HAL_ADC_MODULE_ENABLED
  41. /* Private typedef -----------------------------------------------------------*/
  42. /* Private define ------------------------------------------------------------*/
  43. /** @defgroup ADCEx_Private_Constants ADC Extended Private Constants
  44. * @{
  45. */
  46. /* Fixed timeout values for ADC calibration, enable settling time, disable */
  47. /* settling time. */
  48. /* Values defined to be higher than worst cases: low clock frequency, */
  49. /* maximum prescaler. */
  50. /* Unit: ms */
  51. #define ADC_CALIBRATION_TIMEOUT 10U
  52. /* Delay for VREFINT stabilization time. */
  53. /* Internal reference startup time max value is 3ms (refer to device datasheet, parameter TVREFINT). */
  54. /* Unit: ms */
  55. #define SYSCFG_BUF_VREFINT_ENABLE_TIMEOUT (3U)
  56. /* Delay for TEMPSENSOR stabilization time. */
  57. /* Temperature sensor startup time max value is 10us (refer to device datasheet, parameter tSTART). */
  58. /* Unit: ms */
  59. #define SYSCFG_BUF_TEMPSENSOR_ENABLE_TIMEOUT (1U)
  60. /* Private macro -------------------------------------------------------------*/
  61. /* Private variables ---------------------------------------------------------*/
  62. /* Private function prototypes -----------------------------------------------*/
  63. /* Exported functions --------------------------------------------------------*/
  64. /** @defgroup ADCEx_Exported_Functions ADC Extended Exported Functions
  65. * @{
  66. */
  67. /** @defgroup ADCEx_Exported_Functions_Group1 Extended Input and Output operation functions
  68. * @brief Extended IO operation functions
  69. *
  70. @verbatim
  71. ===============================================================================
  72. ##### IO operation functions #####
  73. ===============================================================================
  74. [..] This section provides functions allowing to:
  75. (+) Perform the ADC calibration.
  76. @endverbatim
  77. * @{
  78. */
  79. /**
  80. * @brief Perform an ADC automatic self-calibration
  81. * Calibration prerequisite: ADC must be disabled (execute this
  82. * function before HAL_ADC_Start() or after HAL_ADC_Stop() ).
  83. * @note Calibration factor can be read after calibration, using function
  84. * HAL_ADC_GetValue() (value on 7 bits: from DR[6;0]).
  85. * @param hadc ADC handle
  86. * @param SingleDiff Selection of single-ended or differential input
  87. * This parameter can be only of the following values:
  88. * @arg ADC_SINGLE_ENDED: Channel in mode input single ended
  89. * @retval HAL status
  90. */
  91. HAL_StatusTypeDef HAL_ADCEx_Calibration_Start(ADC_HandleTypeDef *hadc, uint32_t SingleDiff)
  92. {
  93. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  94. uint32_t tickstart = 0U;
  95. uint32_t backup_setting_adc_dma_transfer = 0U; /* Note: Variable not declared as volatile because register read is already declared as volatile */
  96. /* Check the parameters */
  97. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  98. /* Process locked */
  99. __HAL_LOCK(hadc);
  100. /* Calibration prerequisite: ADC must be disabled. */
  101. if (ADC_IS_ENABLE(hadc) == RESET)
  102. {
  103. /* Set ADC state */
  104. ADC_STATE_CLR_SET(hadc->State,
  105. HAL_ADC_STATE_REG_BUSY,
  106. HAL_ADC_STATE_BUSY_INTERNAL);
  107. /* Disable ADC DMA transfer request during calibration */
  108. /* Note: Specificity of this STM32 series: Calibration factor is */
  109. /* available in data register and also transferred by DMA. */
  110. /* To not insert ADC calibration factor among ADC conversion data */
  111. /* in array variable, DMA transfer must be disabled during */
  112. /* calibration. */
  113. backup_setting_adc_dma_transfer = READ_BIT(hadc->Instance->CFGR1, ADC_CFGR1_DMAEN | ADC_CFGR1_DMACFG);
  114. CLEAR_BIT(hadc->Instance->CFGR1, ADC_CFGR1_DMAEN | ADC_CFGR1_DMACFG);
  115. /* Start ADC calibration */
  116. hadc->Instance->CR |= ADC_CR_ADCAL;
  117. tickstart = HAL_GetTick();
  118. /* Wait for calibration completion */
  119. while (HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADCAL))
  120. {
  121. if ((HAL_GetTick() - tickstart) > ADC_CALIBRATION_TIMEOUT)
  122. {
  123. /* New check to avoid false timeout detection in case of preemption */
  124. if (HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADCAL))
  125. {
  126. /* Update ADC state machine to error */
  127. ADC_STATE_CLR_SET(hadc->State,
  128. HAL_ADC_STATE_BUSY_INTERNAL,
  129. HAL_ADC_STATE_ERROR_INTERNAL);
  130. /* Process unlocked */
  131. __HAL_UNLOCK(hadc);
  132. return HAL_ERROR;
  133. }
  134. }
  135. }
  136. /* Restore ADC DMA transfer request after calibration */
  137. SET_BIT(hadc->Instance->CFGR1, backup_setting_adc_dma_transfer);
  138. /* Set ADC state */
  139. ADC_STATE_CLR_SET(hadc->State,
  140. HAL_ADC_STATE_BUSY_INTERNAL,
  141. HAL_ADC_STATE_READY);
  142. }
  143. else
  144. {
  145. /* Update ADC state machine to error */
  146. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  147. tmp_hal_status = HAL_ERROR;
  148. }
  149. /* Process unlocked */
  150. __HAL_UNLOCK(hadc);
  151. /* Return function status */
  152. return tmp_hal_status;
  153. }
  154. /**
  155. * @brief Get the calibration factor.
  156. * @param hadc ADC handle.
  157. * @param SingleDiff This parameter can be only:
  158. * @arg ADC_SINGLE_ENDED: Channel in mode input single ended.
  159. * @retval Calibration value.
  160. */
  161. uint32_t HAL_ADCEx_Calibration_GetValue(ADC_HandleTypeDef *hadc, uint32_t SingleDiff)
  162. {
  163. /* Check the parameters */
  164. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  165. assert_param(IS_ADC_SINGLE_DIFFERENTIAL(SingleDiff));
  166. /* Return the ADC calibration value */
  167. return ((hadc->Instance->CALFACT) & 0x0000007FU);
  168. }
  169. /**
  170. * @brief Set the calibration factor to overwrite automatic conversion result.
  171. * ADC must be enabled and no conversion is ongoing.
  172. * @param hadc ADC handle
  173. * @param SingleDiff This parameter can be only:
  174. * @arg ADC_SINGLE_ENDED: Channel in mode input single ended.
  175. * @param CalibrationFactor Calibration factor (coded on 7 bits maximum)
  176. * @retval HAL state
  177. */
  178. HAL_StatusTypeDef HAL_ADCEx_Calibration_SetValue(ADC_HandleTypeDef *hadc, uint32_t SingleDiff, uint32_t CalibrationFactor)
  179. {
  180. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  181. /* Check the parameters */
  182. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  183. assert_param(IS_ADC_SINGLE_DIFFERENTIAL(SingleDiff));
  184. assert_param(IS_ADC_CALFACT(CalibrationFactor));
  185. /* Process locked */
  186. __HAL_LOCK(hadc);
  187. /* Verification of hardware constraints before modifying the calibration */
  188. /* factors register: ADC must be enabled, no conversion on going. */
  189. if ((ADC_IS_ENABLE(hadc) != RESET) &&
  190. (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET))
  191. {
  192. /* Set the selected ADC calibration value */
  193. hadc->Instance->CALFACT &= ~ADC_CALFACT_CALFACT;
  194. hadc->Instance->CALFACT |= CalibrationFactor;
  195. }
  196. else
  197. {
  198. /* Update ADC state machine to error */
  199. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  200. /* Update ADC state machine to error */
  201. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  202. /* Update ADC state machine to error */
  203. tmp_hal_status = HAL_ERROR;
  204. }
  205. /* Process unlocked */
  206. __HAL_UNLOCK(hadc);
  207. /* Return function status */
  208. return tmp_hal_status;
  209. }
  210. /**
  211. * @brief Enables the buffer of Vrefint for the ADC, required when device is in mode low-power (low-power run, low-power sleep or stop mode)
  212. * This function must be called before function HAL_ADC_Init()
  213. * (in case of previous ADC operations: function HAL_ADC_DeInit() must be called first)
  214. * For more details on procedure and buffer current consumption, refer to device reference manual.
  215. * @note This is functional only if the LOCK is not set.
  216. * @note This API is obsolete. This configuration is done in HAL_ADC_ConfigChannel().
  217. * @retval None
  218. */
  219. HAL_StatusTypeDef HAL_ADCEx_EnableVREFINT(void)
  220. {
  221. uint32_t tickstart = 0U;
  222. /* Enable the Buffer for the ADC by setting ENBUF_SENSOR_ADC bit in the CFGR3 register */
  223. SET_BIT(SYSCFG->CFGR3, SYSCFG_CFGR3_ENBUF_VREFINT_ADC);
  224. /* Wait for Vrefint buffer effectively enabled */
  225. /* Get tick count */
  226. tickstart = HAL_GetTick();
  227. while (HAL_IS_BIT_CLR(SYSCFG->CFGR3, SYSCFG_CFGR3_VREFINT_RDYF))
  228. {
  229. if ((HAL_GetTick() - tickstart) > SYSCFG_BUF_VREFINT_ENABLE_TIMEOUT)
  230. {
  231. /* New check to avoid false timeout detection in case of preemption */
  232. if (HAL_IS_BIT_CLR(SYSCFG->CFGR3, SYSCFG_CFGR3_VREFINT_RDYF))
  233. {
  234. return HAL_ERROR;
  235. }
  236. }
  237. }
  238. return HAL_OK;
  239. }
  240. /**
  241. * @brief Disables the Buffer Vrefint for the ADC.
  242. * @note This is functional only if the LOCK is not set.
  243. * @note This API is obsolete. This configuration is done in HAL_ADC_ConfigChannel().
  244. * @retval None
  245. */
  246. void HAL_ADCEx_DisableVREFINT(void)
  247. {
  248. /* Disable the Vrefint by resetting ENBUF_SENSOR_ADC bit in the CFGR3 register */
  249. CLEAR_BIT(SYSCFG->CFGR3, SYSCFG_CFGR3_ENBUF_VREFINT_ADC);
  250. }
  251. /**
  252. * @brief Enables the buffer of temperature sensor for the ADC, required when device is in mode low-power (low-power run, low-power sleep or stop mode)
  253. * This function must be called before function HAL_ADC_Init()
  254. * (in case of previous ADC operations: function HAL_ADC_DeInit() must be called first)
  255. * For more details on procedure and buffer current consumption, refer to device reference manual.
  256. * @note This is functional only if the LOCK is not set.
  257. * @note This API is obsolete. This configuration is done in HAL_ADC_ConfigChannel().
  258. * @retval None
  259. */
  260. HAL_StatusTypeDef HAL_ADCEx_EnableVREFINTTempSensor(void)
  261. {
  262. uint32_t tickstart = 0U;
  263. /* Enable the Buffer for the ADC by setting ENBUF_SENSOR_ADC bit in the CFGR3 register */
  264. SET_BIT(SYSCFG->CFGR3, SYSCFG_CFGR3_ENBUF_SENSOR_ADC);
  265. /* Wait for Vrefint buffer effectively enabled */
  266. /* Get tick count */
  267. tickstart = HAL_GetTick();
  268. while (HAL_IS_BIT_CLR(SYSCFG->CFGR3, SYSCFG_CFGR3_VREFINT_RDYF))
  269. {
  270. if ((HAL_GetTick() - tickstart) > SYSCFG_BUF_TEMPSENSOR_ENABLE_TIMEOUT)
  271. {
  272. /* New check to avoid false timeout detection in case of preemption */
  273. if (HAL_IS_BIT_CLR(SYSCFG->CFGR3, SYSCFG_CFGR3_VREFINT_RDYF))
  274. {
  275. return HAL_ERROR;
  276. }
  277. }
  278. }
  279. return HAL_OK;
  280. }
  281. /**
  282. * @brief Disables the VREFINT and Sensor for the ADC.
  283. * @note This is functional only if the LOCK is not set.
  284. * @note This API is obsolete. This configuration is done in HAL_ADC_ConfigChannel().
  285. * @retval None
  286. */
  287. void HAL_ADCEx_DisableVREFINTTempSensor(void)
  288. {
  289. /* Disable the Vrefint by resetting ENBUF_SENSOR_ADC bit in the CFGR3 register */
  290. CLEAR_BIT(SYSCFG->CFGR3, SYSCFG_CFGR3_ENBUF_SENSOR_ADC);
  291. }
  292. /**
  293. * @}
  294. */
  295. /**
  296. * @}
  297. */
  298. /**
  299. * @}
  300. */
  301. #endif /* HAL_ADC_MODULE_ENABLED */
  302. /**
  303. * @}
  304. */
  305. /**
  306. * @}
  307. */