stm32g0xx_hal_adc_ex.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /**
  2. ******************************************************************************
  3. * @file stm32g0xx_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 Converter (ADC)
  7. * peripheral:
  8. * + Peripheral Control functions
  9. * Other functions (generic functions) are available in file
  10. * "stm32g0xx_hal_adc.c".
  11. *
  12. ******************************************************************************
  13. * @attention
  14. *
  15. * Copyright (c) 2018 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 "stm32g0xx_hal_adc.c".
  27. [..]
  28. @endverbatim
  29. ******************************************************************************
  30. */
  31. /* Includes ------------------------------------------------------------------*/
  32. #include "stm32g0xx_hal.h"
  33. /** @addtogroup STM32G0xx_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 value for ADC calibration. */
  47. /* Values defined to be higher than worst cases: maximum ratio between ADC */
  48. /* and CPU clock frequencies. */
  49. /* Example of profile low frequency : ADC frequency at 31.25kHz (ADC clock */
  50. /* source PLL 8MHz, ADC clock prescaler 256), CPU frequency 52MHz. */
  51. /* Calibration time max = 116 / fADC (refer to datasheet) */
  52. /* = 193 024 CPU cycles */
  53. #define ADC_CALIBRATION_TIMEOUT (193024UL) /*!< ADC calibration time-out value (unit: CPU cycles) */
  54. #define ADC_DISABLE_TIMEOUT (2UL)
  55. /**
  56. * @}
  57. */
  58. /* Private macro -------------------------------------------------------------*/
  59. /* Private variables ---------------------------------------------------------*/
  60. /* Private function prototypes -----------------------------------------------*/
  61. /* Exported functions --------------------------------------------------------*/
  62. /** @defgroup ADCEx_Exported_Functions ADC Extended Exported Functions
  63. * @{
  64. */
  65. /** @defgroup ADCEx_Exported_Functions_Group1 Extended Input and Output operation functions
  66. * @brief Extended IO operation functions
  67. *
  68. @verbatim
  69. ===============================================================================
  70. ##### IO operation functions #####
  71. ===============================================================================
  72. [..] This section provides functions allowing to:
  73. (+) Perform the ADC self-calibration.
  74. (+) Get calibration factors.
  75. (+) Set calibration factors.
  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. * @retval HAL status
  87. */
  88. HAL_StatusTypeDef HAL_ADCEx_Calibration_Start(ADC_HandleTypeDef *hadc)
  89. {
  90. HAL_StatusTypeDef tmp_hal_status;
  91. __IO uint32_t wait_loop_index = 0UL;
  92. uint32_t backup_setting_cfgr1;
  93. uint32_t calibration_index;
  94. uint32_t calibration_factor_accumulated = 0;
  95. uint32_t tickstart;
  96. uint32_t adc_clk_async_presc;
  97. __IO uint32_t delay_cpu_cycles;
  98. /* Check the parameters */
  99. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  100. __HAL_LOCK(hadc);
  101. /* Calibration prerequisite: ADC must be disabled. */
  102. /* Disable the ADC (if not already disabled) */
  103. tmp_hal_status = ADC_Disable(hadc);
  104. /* Check if ADC is effectively disabled */
  105. if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
  106. {
  107. /* Set ADC state */
  108. ADC_STATE_CLR_SET(hadc->State,
  109. HAL_ADC_STATE_REG_BUSY,
  110. HAL_ADC_STATE_BUSY_INTERNAL);
  111. /* Manage settings impacting calibration */
  112. /* - Disable ADC mode auto power-off */
  113. /* - Disable ADC DMA transfer request during calibration */
  114. /* Note: Specificity of this STM32 series: Calibration factor is */
  115. /* available in data register and also transferred by DMA. */
  116. /* To not insert ADC calibration factor among ADC conversion data */
  117. /* in array variable, DMA transfer must be disabled during */
  118. /* calibration. */
  119. backup_setting_cfgr1 = READ_BIT(hadc->Instance->CFGR1, ADC_CFGR1_DMAEN | ADC_CFGR1_DMACFG | ADC_CFGR1_AUTOFF);
  120. CLEAR_BIT(hadc->Instance->CFGR1, ADC_CFGR1_DMAEN | ADC_CFGR1_DMACFG | ADC_CFGR1_AUTOFF);
  121. /* ADC calibration procedure */
  122. /* Note: Perform an averaging of 8 calibrations for optimized accuracy */
  123. for (calibration_index = 0UL; calibration_index < 8UL; calibration_index++)
  124. {
  125. /* Start ADC calibration */
  126. LL_ADC_StartCalibration(hadc->Instance);
  127. /* Wait for calibration completion */
  128. while (LL_ADC_IsCalibrationOnGoing(hadc->Instance) != 0UL)
  129. {
  130. wait_loop_index++;
  131. if (wait_loop_index >= ADC_CALIBRATION_TIMEOUT)
  132. {
  133. /* Update ADC state machine to error */
  134. ADC_STATE_CLR_SET(hadc->State,
  135. HAL_ADC_STATE_BUSY_INTERNAL,
  136. HAL_ADC_STATE_ERROR_INTERNAL);
  137. __HAL_UNLOCK(hadc);
  138. return HAL_ERROR;
  139. }
  140. }
  141. calibration_factor_accumulated += LL_ADC_GetCalibrationFactor(hadc->Instance);
  142. }
  143. /* Compute average */
  144. calibration_factor_accumulated /= calibration_index;
  145. /* Apply calibration factor (requires ADC enable and disable process) */
  146. LL_ADC_Enable(hadc->Instance);
  147. /* Case of ADC clocked at low frequency: Delay required between ADC enable and disable actions */
  148. if (LL_ADC_GetClock(hadc->Instance) == LL_ADC_CLOCK_ASYNC)
  149. {
  150. adc_clk_async_presc = LL_ADC_GetCommonClock(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
  151. if (adc_clk_async_presc >= LL_ADC_CLOCK_ASYNC_DIV16)
  152. {
  153. /* Delay loop initialization and execution */
  154. /* Delay depends on ADC clock prescaler: Compute ADC clock asynchronous prescaler to decimal format */
  155. delay_cpu_cycles = (1UL << ((adc_clk_async_presc >> ADC_CCR_PRESC_Pos) - 3UL));
  156. /* Divide variable by 2 to compensate partially CPU processing cycles */
  157. delay_cpu_cycles >>= 1UL;
  158. while (delay_cpu_cycles != 0UL)
  159. {
  160. delay_cpu_cycles--;
  161. }
  162. }
  163. }
  164. LL_ADC_SetCalibrationFactor(hadc->Instance, calibration_factor_accumulated);
  165. LL_ADC_Disable(hadc->Instance);
  166. /* Wait for ADC effectively disabled before changing configuration */
  167. /* Get tick count */
  168. tickstart = HAL_GetTick();
  169. while (LL_ADC_IsEnabled(hadc->Instance) != 0UL)
  170. {
  171. if ((HAL_GetTick() - tickstart) > ADC_DISABLE_TIMEOUT)
  172. {
  173. /* New check to avoid false timeout detection in case of preemption */
  174. if (LL_ADC_IsEnabled(hadc->Instance) != 0UL)
  175. {
  176. /* Update ADC state machine to error */
  177. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  178. /* Set ADC error code to ADC peripheral internal error */
  179. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  180. return HAL_ERROR;
  181. }
  182. }
  183. }
  184. /* Restore configuration after calibration */
  185. SET_BIT(hadc->Instance->CFGR1, backup_setting_cfgr1);
  186. /* Set ADC state */
  187. ADC_STATE_CLR_SET(hadc->State,
  188. HAL_ADC_STATE_BUSY_INTERNAL,
  189. HAL_ADC_STATE_READY);
  190. }
  191. else
  192. {
  193. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  194. /* Note: No need to update variable "tmp_hal_status" here: already set */
  195. /* to state "HAL_ERROR" by function disabling the ADC. */
  196. }
  197. __HAL_UNLOCK(hadc);
  198. return tmp_hal_status;
  199. }
  200. /**
  201. * @brief Get the calibration factor.
  202. * @param hadc ADC handle.
  203. * @retval Calibration value.
  204. */
  205. uint32_t HAL_ADCEx_Calibration_GetValue(const ADC_HandleTypeDef *hadc)
  206. {
  207. /* Check the parameters */
  208. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  209. /* Return the selected ADC calibration value */
  210. return ((hadc->Instance->CALFACT) & 0x0000007FU);
  211. }
  212. /**
  213. * @brief Set the calibration factor to overwrite automatic conversion result.
  214. * ADC must be enabled and no conversion is ongoing.
  215. * @param hadc ADC handle
  216. * @param CalibrationFactor Calibration factor (coded on 7 bits maximum)
  217. * @retval HAL state
  218. */
  219. HAL_StatusTypeDef HAL_ADCEx_Calibration_SetValue(ADC_HandleTypeDef *hadc, uint32_t CalibrationFactor)
  220. {
  221. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  222. uint32_t tmp_adc_is_conversion_on_going_regular;
  223. /* Check the parameters */
  224. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  225. assert_param(IS_ADC_CALFACT(CalibrationFactor));
  226. __HAL_LOCK(hadc);
  227. /* Verification of hardware constraints before modifying the calibration */
  228. /* factors register: ADC must be enabled, no conversion on going. */
  229. tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
  230. if ((LL_ADC_IsEnabled(hadc->Instance) != 0UL)
  231. && (tmp_adc_is_conversion_on_going_regular == 0UL)
  232. )
  233. {
  234. hadc->Instance->CALFACT &= ~ADC_CALFACT_CALFACT;
  235. hadc->Instance->CALFACT |= CalibrationFactor;
  236. }
  237. else
  238. {
  239. /* Update ADC state machine */
  240. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  241. /* Update ADC error code */
  242. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  243. /* Update ADC state machine to error */
  244. tmp_hal_status = HAL_ERROR;
  245. }
  246. __HAL_UNLOCK(hadc);
  247. return tmp_hal_status;
  248. }
  249. /**
  250. * @brief Analog watchdog 2 callback in non-blocking mode.
  251. * @param hadc ADC handle
  252. * @retval None
  253. */
  254. __weak void HAL_ADCEx_LevelOutOfWindow2Callback(ADC_HandleTypeDef *hadc)
  255. {
  256. /* Prevent unused argument(s) compilation warning */
  257. UNUSED(hadc);
  258. /* NOTE : This function should not be modified. When the callback is needed,
  259. function HAL_ADCEx_LevelOutOfWindow2Callback must be implemented in the user file.
  260. */
  261. }
  262. /**
  263. * @brief Analog watchdog 3 callback in non-blocking mode.
  264. * @param hadc ADC handle
  265. * @retval None
  266. */
  267. __weak void HAL_ADCEx_LevelOutOfWindow3Callback(ADC_HandleTypeDef *hadc)
  268. {
  269. /* Prevent unused argument(s) compilation warning */
  270. UNUSED(hadc);
  271. /* NOTE : This function should not be modified. When the callback is needed,
  272. function HAL_ADCEx_LevelOutOfWindow3Callback must be implemented in the user file.
  273. */
  274. }
  275. /**
  276. * @brief End Of Sampling callback in non-blocking mode.
  277. * @param hadc ADC handle
  278. * @retval None
  279. */
  280. __weak void HAL_ADCEx_EndOfSamplingCallback(ADC_HandleTypeDef *hadc)
  281. {
  282. /* Prevent unused argument(s) compilation warning */
  283. UNUSED(hadc);
  284. /* NOTE : This function should not be modified. When the callback is needed,
  285. function HAL_ADCEx_EndOfSamplingCallback must be implemented in the user file.
  286. */
  287. }
  288. /**
  289. * @brief ADC channel configuration ready callback in non-blocking mode.
  290. * @param hadc ADC handle
  291. * @retval None
  292. */
  293. __weak void HAL_ADCEx_ChannelConfigReadyCallback(ADC_HandleTypeDef *hadc)
  294. {
  295. /* Prevent unused argument(s) compilation warning */
  296. UNUSED(hadc);
  297. /* NOTE : This function should not be modified. When the callback is needed,
  298. function HAL_ADCEx_ChannelConfigReadyCallback must be implemented in the user file.
  299. */
  300. }
  301. /**
  302. * @}
  303. */
  304. /**
  305. * @brief Disable ADC voltage regulator.
  306. * @note Disabling voltage regulator allows to save power. This operation can
  307. * be carried out only when ADC is disabled.
  308. * @note To enable again the voltage regulator, the user is expected to
  309. * resort to HAL_ADC_Init() API.
  310. * @param hadc ADC handle
  311. * @retval HAL status
  312. */
  313. HAL_StatusTypeDef HAL_ADCEx_DisableVoltageRegulator(ADC_HandleTypeDef *hadc)
  314. {
  315. HAL_StatusTypeDef tmp_hal_status;
  316. /* Check the parameters */
  317. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  318. /* Setting of this feature is conditioned to ADC state: ADC must be ADC disabled */
  319. if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
  320. {
  321. LL_ADC_DisableInternalRegulator(hadc->Instance);
  322. tmp_hal_status = HAL_OK;
  323. }
  324. else
  325. {
  326. tmp_hal_status = HAL_ERROR;
  327. }
  328. return tmp_hal_status;
  329. }
  330. /**
  331. * @}
  332. */
  333. /**
  334. * @}
  335. */
  336. #endif /* HAL_ADC_MODULE_ENABLED */
  337. /**
  338. * @}
  339. */
  340. /**
  341. * @}
  342. */