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.
 
 
 

349 lines
12 KiB

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