stm32l0xx_hal_adc_ex.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. /* Prevent unused argument(s) compilation warning */
  99. UNUSED(SingleDiff);
  100. /* Process locked */
  101. __HAL_LOCK(hadc);
  102. /* Calibration prerequisite: ADC must be disabled. */
  103. if (ADC_IS_ENABLE(hadc) == RESET)
  104. {
  105. /* Set ADC state */
  106. ADC_STATE_CLR_SET(hadc->State,
  107. HAL_ADC_STATE_REG_BUSY,
  108. HAL_ADC_STATE_BUSY_INTERNAL);
  109. /* Disable ADC DMA transfer request during calibration */
  110. /* Note: Specificity of this STM32 series: Calibration factor is */
  111. /* available in data register and also transferred by DMA. */
  112. /* To not insert ADC calibration factor among ADC conversion data */
  113. /* in array variable, DMA transfer must be disabled during */
  114. /* calibration. */
  115. backup_setting_adc_dma_transfer = READ_BIT(hadc->Instance->CFGR1, ADC_CFGR1_DMAEN | ADC_CFGR1_DMACFG);
  116. CLEAR_BIT(hadc->Instance->CFGR1, ADC_CFGR1_DMAEN | ADC_CFGR1_DMACFG);
  117. /* Start ADC calibration */
  118. hadc->Instance->CR |= ADC_CR_ADCAL;
  119. tickstart = HAL_GetTick();
  120. /* Wait for calibration completion */
  121. while (HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADCAL))
  122. {
  123. if ((HAL_GetTick() - tickstart) > ADC_CALIBRATION_TIMEOUT)
  124. {
  125. /* New check to avoid false timeout detection in case of preemption */
  126. if (HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADCAL))
  127. {
  128. /* Update ADC state machine to error */
  129. ADC_STATE_CLR_SET(hadc->State,
  130. HAL_ADC_STATE_BUSY_INTERNAL,
  131. HAL_ADC_STATE_ERROR_INTERNAL);
  132. /* Process unlocked */
  133. __HAL_UNLOCK(hadc);
  134. return HAL_ERROR;
  135. }
  136. }
  137. }
  138. /* Restore ADC DMA transfer request after calibration */
  139. SET_BIT(hadc->Instance->CFGR1, backup_setting_adc_dma_transfer);
  140. /* Set ADC state */
  141. ADC_STATE_CLR_SET(hadc->State,
  142. HAL_ADC_STATE_BUSY_INTERNAL,
  143. HAL_ADC_STATE_READY);
  144. }
  145. else
  146. {
  147. /* Update ADC state machine to error */
  148. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  149. tmp_hal_status = HAL_ERROR;
  150. }
  151. /* Process unlocked */
  152. __HAL_UNLOCK(hadc);
  153. /* Return function status */
  154. return tmp_hal_status;
  155. }
  156. /**
  157. * @brief Get the calibration factor.
  158. * @param hadc ADC handle.
  159. * @param SingleDiff This parameter can be only:
  160. * @arg ADC_SINGLE_ENDED: Channel in mode input single ended.
  161. * @retval Calibration value.
  162. */
  163. uint32_t HAL_ADCEx_Calibration_GetValue(ADC_HandleTypeDef *hadc, uint32_t SingleDiff)
  164. {
  165. /* Check the parameters */
  166. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  167. assert_param(IS_ADC_SINGLE_DIFFERENTIAL(SingleDiff));
  168. /* Prevent unused argument(s) compilation warning */
  169. UNUSED(SingleDiff);
  170. /* Return the ADC calibration value */
  171. return ((hadc->Instance->CALFACT) & 0x0000007FU);
  172. }
  173. /**
  174. * @brief Set the calibration factor to overwrite automatic conversion result.
  175. * ADC must be enabled and no conversion is ongoing.
  176. * @param hadc ADC handle
  177. * @param SingleDiff This parameter can be only:
  178. * @arg ADC_SINGLE_ENDED: Channel in mode input single ended.
  179. * @param CalibrationFactor Calibration factor (coded on 7 bits maximum)
  180. * @retval HAL state
  181. */
  182. HAL_StatusTypeDef HAL_ADCEx_Calibration_SetValue(ADC_HandleTypeDef *hadc, uint32_t SingleDiff, uint32_t CalibrationFactor)
  183. {
  184. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  185. /* Check the parameters */
  186. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  187. assert_param(IS_ADC_SINGLE_DIFFERENTIAL(SingleDiff));
  188. assert_param(IS_ADC_CALFACT(CalibrationFactor));
  189. /* Prevent unused argument(s) compilation warning */
  190. UNUSED(SingleDiff);
  191. /* Process locked */
  192. __HAL_LOCK(hadc);
  193. /* Verification of hardware constraints before modifying the calibration */
  194. /* factors register: ADC must be enabled, no conversion on going. */
  195. if ((ADC_IS_ENABLE(hadc) != RESET) &&
  196. (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET))
  197. {
  198. /* Set the selected ADC calibration value */
  199. hadc->Instance->CALFACT &= ~ADC_CALFACT_CALFACT;
  200. hadc->Instance->CALFACT |= CalibrationFactor;
  201. }
  202. else
  203. {
  204. /* Update ADC state machine to error */
  205. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  206. /* Update ADC state machine to error */
  207. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  208. /* Update ADC state machine to error */
  209. tmp_hal_status = HAL_ERROR;
  210. }
  211. /* Process unlocked */
  212. __HAL_UNLOCK(hadc);
  213. /* Return function status */
  214. return tmp_hal_status;
  215. }
  216. /**
  217. * @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)
  218. * However ADC is not functional in low-power mode, therefore these functions is not targeted for ADC (obsolete)
  219. * For more details on procedure and buffer current consumption, refer to device reference manual.
  220. * @note This is functional only if the LOCK is not set.
  221. * @note This API is obsolete. This equivalent configuration is done in HAL_ADC_ConfigChannel().
  222. bit fields in ADC_CCR and SYSCFG_CFGR3 control the same signals to VREFINT and TempSensor buffers
  223. * @retval None
  224. */
  225. HAL_StatusTypeDef HAL_ADCEx_EnableVREFINT(void)
  226. {
  227. uint32_t tickstart = 0U;
  228. /* Enable the Buffer for the ADC by setting ENBUF_SENSOR_ADC bit in the CFGR3 register */
  229. SET_BIT(SYSCFG->CFGR3, SYSCFG_CFGR3_ENBUF_VREFINT_ADC);
  230. /* Wait for Vrefint buffer effectively enabled */
  231. /* Get tick count */
  232. tickstart = HAL_GetTick();
  233. while (HAL_IS_BIT_CLR(SYSCFG->CFGR3, SYSCFG_CFGR3_VREFINT_RDYF))
  234. {
  235. if ((HAL_GetTick() - tickstart) > SYSCFG_BUF_VREFINT_ENABLE_TIMEOUT)
  236. {
  237. /* New check to avoid false timeout detection in case of preemption */
  238. if (HAL_IS_BIT_CLR(SYSCFG->CFGR3, SYSCFG_CFGR3_VREFINT_RDYF))
  239. {
  240. return HAL_ERROR;
  241. }
  242. }
  243. }
  244. return HAL_OK;
  245. }
  246. /**
  247. * @brief Disables the Buffer Vrefint for the ADC.
  248. * @note This is functional only if the LOCK is not set.
  249. * @note This API is obsolete. This equivalent configuration is done in HAL_ADC_ConfigChannel().
  250. bit fields in ADC_CCR and SYSCFG_CFGR3 control the same signals to VREFINT and TempSensor buffers.
  251. * @retval None
  252. */
  253. void HAL_ADCEx_DisableVREFINT(void)
  254. {
  255. /* Disable the Vrefint by resetting ENBUF_SENSOR_ADC bit in the CFGR3 register */
  256. CLEAR_BIT(SYSCFG->CFGR3, SYSCFG_CFGR3_ENBUF_VREFINT_ADC);
  257. }
  258. /**
  259. * @brief Enables the buffer of temperature sensor, when device is in mode low-power (low-power run, low-power sleep or stop mode)
  260. * However ADC is not functional in low-power mode, therefore these functions is not targeted for ADC (obsolete)
  261. * For more details on procedure and buffer current consumption, refer to device reference manual.
  262. * @note This is functional only if the LOCK is not set.
  263. * @note This API is obsolete. This equivalent configuration is done in HAL_ADC_ConfigChannel().
  264. bit fields in ADC_CCR and SYSCFG_CFGR3 control the same signals to VREFINT and TempSensor buffers
  265. * @retval None
  266. */
  267. HAL_StatusTypeDef HAL_ADCEx_EnableVREFINTTempSensor(void)
  268. {
  269. uint32_t tickstart = 0U;
  270. /* Enable the Buffer for the ADC by setting ENBUF_SENSOR_ADC bit in the CFGR3 register */
  271. SET_BIT(SYSCFG->CFGR3, SYSCFG_CFGR3_ENBUF_SENSOR_ADC);
  272. /* Wait for Vrefint buffer effectively enabled */
  273. /* Get tick count */
  274. tickstart = HAL_GetTick();
  275. while (HAL_IS_BIT_CLR(SYSCFG->CFGR3, SYSCFG_CFGR3_VREFINT_RDYF))
  276. {
  277. if ((HAL_GetTick() - tickstart) > SYSCFG_BUF_TEMPSENSOR_ENABLE_TIMEOUT)
  278. {
  279. /* New check to avoid false timeout detection in case of preemption */
  280. if (HAL_IS_BIT_CLR(SYSCFG->CFGR3, SYSCFG_CFGR3_VREFINT_RDYF))
  281. {
  282. return HAL_ERROR;
  283. }
  284. }
  285. }
  286. return HAL_OK;
  287. }
  288. /**
  289. * @brief Disables the VREFINT and Sensor for the ADC.
  290. * @note This is functional only if the LOCK is not set.
  291. * @note This API is obsolete. This equivalent configuration is done in HAL_ADC_ConfigChannel().
  292. bit fields in ADC_CCR and SYSCFG_CFGR3 control the same signals to VREFINT and TempSensor buffers.
  293. * @retval None
  294. */
  295. void HAL_ADCEx_DisableVREFINTTempSensor(void)
  296. {
  297. /* Disable the Vrefint by resetting ENBUF_SENSOR_ADC bit in the CFGR3 register */
  298. CLEAR_BIT(SYSCFG->CFGR3, SYSCFG_CFGR3_ENBUF_SENSOR_ADC);
  299. }
  300. /**
  301. * @}
  302. */
  303. /**
  304. * @}
  305. */
  306. /**
  307. * @}
  308. */
  309. #endif /* HAL_ADC_MODULE_ENABLED */
  310. /**
  311. * @}
  312. */
  313. /**
  314. * @}
  315. */