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.
 
 
 

2495 lines
91 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32l0xx_hal_adc.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. * + Initialization and de-initialization functions
  9. * ++ Initialization and Configuration of ADC
  10. * + Operation functions
  11. * ++ Start, stop, get result of conversions of regular
  12. * group, using 3 possible modes: polling, interruption or DMA.
  13. * + Control functions
  14. * ++ Channels configuration on regular group
  15. * ++ Analog Watchdog configuration
  16. * + State functions
  17. * ++ ADC state machine management
  18. * ++ Interrupts and flags management
  19. * Other functions (extended functions) are available in file
  20. * "stm32l0xx_hal_adc_ex.c".
  21. *
  22. @verbatim
  23. ==============================================================================
  24. ##### ADC peripheral features #####
  25. ==============================================================================
  26. [..]
  27. (+) 12-bit, 10-bit, 8-bit or 6-bit configurable resolution.
  28. (+) Interrupt generation at the end of regular conversion and in case of
  29. analog watchdog or overrun events.
  30. (+) Single and continuous conversion modes.
  31. (+) Scan mode for conversion of several channels sequentially.
  32. (+) Data alignment with in-built data coherency.
  33. (+) Programmable sampling time (common for all channels)
  34. (+) External trigger (timer or EXTI) with configurable polarity
  35. (+) DMA request generation for transfer of conversions data of regular group.
  36. (+) ADC calibration
  37. (+) ADC conversion of regular group.
  38. (+) ADC supply requirements: 1.62 V to 3.6 V.
  39. (+) ADC input range: from Vref- (connected to Vssa) to Vref+ (connected to
  40. Vdda or to an external voltage reference).
  41. ##### How to use this driver #####
  42. ==============================================================================
  43. [..]
  44. *** Configuration of top level parameters related to ADC ***
  45. ============================================================
  46. [..]
  47. (#) Enable the ADC interface
  48. (++) As prerequisite, ADC clock must be configured at RCC top level.
  49. Caution: On STM32L0, ADC clock frequency max is 16MHz (refer
  50. to device datasheet).
  51. Therefore, ADC clock prescaler must be configured in
  52. function of ADC clock source frequency to remain below
  53. this maximum frequency.
  54. (++) Two clock settings are mandatory:
  55. (+++) ADC clock (core clock, also possibly conversion clock).
  56. (+++) ADC clock (conversions clock).
  57. Two possible clock sources: synchronous clock derived from APB clock
  58. or asynchronous clock derived from ADC dedicated HSI RC oscillator
  59. 16MHz.
  60. If asynchronous clock is selected, parameter "HSIState" must be set either:
  61. - to "...HSIState = RCC_HSI_ON" to maintain the HSI16 oscillator
  62. always enabled: can be used to supply the main system clock.
  63. (+++) Example:
  64. Into HAL_ADC_MspInit() (recommended code location) or with
  65. other device clock parameters configuration:
  66. (+++) __HAL_RCC_ADC1_CLK_ENABLE(); (mandatory)
  67. HSI enable (optional: if asynchronous clock selected)
  68. (+++) RCC_OscInitTypeDef RCC_OscInitStructure;
  69. (+++) RCC_OscInitStructure.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  70. (+++) RCC_OscInitStructure.HSI16CalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  71. (+++) RCC_OscInitStructure.HSIState = RCC_HSI_ON;
  72. (+++) RCC_OscInitStructure.PLL... (optional if used for system clock)
  73. (+++) HAL_RCC_OscConfig(&RCC_OscInitStructure);
  74. (++) ADC clock source and clock prescaler are configured at ADC level with
  75. parameter "ClockPrescaler" using function HAL_ADC_Init().
  76. (#) ADC pins configuration
  77. (++) Enable the clock for the ADC GPIOs
  78. using macro __HAL_RCC_GPIOx_CLK_ENABLE()
  79. (++) Configure these ADC pins in analog mode
  80. using function HAL_GPIO_Init()
  81. (#) Optionally, in case of usage of ADC with interruptions:
  82. (++) Configure the NVIC for ADC
  83. using function HAL_NVIC_EnableIRQ(ADCx_IRQn)
  84. (++) Insert the ADC interruption handler function HAL_ADC_IRQHandler()
  85. into the function of corresponding ADC interruption vector
  86. ADCx_IRQHandler().
  87. (#) Optionally, in case of usage of DMA:
  88. (++) Configure the DMA (DMA channel, mode normal or circular, ...)
  89. using function HAL_DMA_Init().
  90. (++) Configure the NVIC for DMA
  91. using function HAL_NVIC_EnableIRQ(DMAx_Channelx_IRQn)
  92. (++) Insert the ADC interruption handler function HAL_ADC_IRQHandler()
  93. into the function of corresponding DMA interruption vector
  94. DMAx_Channelx_IRQHandler().
  95. *** Configuration of ADC, group regular, channels parameters ***
  96. ================================================================
  97. [..]
  98. (#) Configure the ADC parameters (resolution, data alignment, ...)
  99. and regular group parameters (conversion trigger, sequencer, ...)
  100. using function HAL_ADC_Init().
  101. (#) Configure the channels for regular group parameters (channel number,
  102. channel rank into sequencer, ..., into regular group)
  103. using function HAL_ADC_ConfigChannel().
  104. (#) Optionally, configure the analog watchdog parameters (channels
  105. monitored, thresholds, ...)
  106. using function HAL_ADC_AnalogWDGConfig().
  107. (#) When device is in mode low-power (low-power run, low-power sleep or stop mode),
  108. function "HAL_ADCEx_EnableVREFINT()" must be called before function HAL_ADC_Init().
  109. In case of internal temperature sensor to be measured:
  110. function "HAL_ADCEx_EnableVREFINTTempSensor()" must be called similarilly
  111. *** Execution of ADC conversions ***
  112. ====================================
  113. [..]
  114. (#) Optionally, perform an automatic ADC calibration to improve the
  115. conversion accuracy
  116. using function HAL_ADCEx_Calibration_Start().
  117. (#) ADC driver can be used among three modes: polling, interruption,
  118. transfer by DMA.
  119. (++) ADC conversion by polling:
  120. (+++) Activate the ADC peripheral and start conversions
  121. using function HAL_ADC_Start()
  122. (+++) Wait for ADC conversion completion
  123. using function HAL_ADC_PollForConversion()
  124. (+++) Retrieve conversion results
  125. using function HAL_ADC_GetValue()
  126. (+++) Stop conversion and disable the ADC peripheral
  127. using function HAL_ADC_Stop()
  128. (++) ADC conversion by interruption:
  129. (+++) Activate the ADC peripheral and start conversions
  130. using function HAL_ADC_Start_IT()
  131. (+++) Wait for ADC conversion completion by call of function
  132. HAL_ADC_ConvCpltCallback()
  133. (this function must be implemented in user program)
  134. (+++) Retrieve conversion results
  135. using function HAL_ADC_GetValue()
  136. (+++) Stop conversion and disable the ADC peripheral
  137. using function HAL_ADC_Stop_IT()
  138. (++) ADC conversion with transfer by DMA:
  139. (+++) Activate the ADC peripheral and start conversions
  140. using function HAL_ADC_Start_DMA()
  141. (+++) Wait for ADC conversion completion by call of function
  142. HAL_ADC_ConvCpltCallback() or HAL_ADC_ConvHalfCpltCallback()
  143. (these functions must be implemented in user program)
  144. (+++) Conversion results are automatically transferred by DMA into
  145. destination variable address.
  146. (+++) Stop conversion and disable the ADC peripheral
  147. using function HAL_ADC_Stop_DMA()
  148. [..]
  149. (@) Callback functions must be implemented in user program:
  150. (+@) HAL_ADC_ErrorCallback()
  151. (+@) HAL_ADC_LevelOutOfWindowCallback() (callback of analog watchdog)
  152. (+@) HAL_ADC_ConvCpltCallback()
  153. (+@) HAL_ADC_ConvHalfCpltCallback
  154. *** Deinitialization of ADC ***
  155. ============================================================
  156. [..]
  157. (#) Disable the ADC interface
  158. (++) ADC clock can be hard reset and disabled at RCC top level.
  159. (++) Hard reset of ADC peripherals
  160. using macro __ADCx_FORCE_RESET(), __ADCx_RELEASE_RESET().
  161. (++) ADC clock disable
  162. using the equivalent macro/functions as configuration step.
  163. (+++) Example:
  164. Into HAL_ADC_MspDeInit() (recommended code location) or with
  165. other device clock parameters configuration:
  166. (+++) RCC_OscInitStructure.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  167. (+++) RCC_OscInitStructure.HSIState = RCC_HSI_OFF; (if not used for system clock)
  168. (+++) HAL_RCC_OscConfig(&RCC_OscInitStructure);
  169. (#) ADC pins configuration
  170. (++) Disable the clock for the ADC GPIOs
  171. using macro __HAL_RCC_GPIOx_CLK_DISABLE()
  172. (#) Optionally, in case of usage of ADC with interruptions:
  173. (++) Disable the NVIC for ADC
  174. using function HAL_NVIC_EnableIRQ(ADCx_IRQn)
  175. (#) Optionally, in case of usage of DMA:
  176. (++) Deinitialize the DMA
  177. using function HAL_DMA_Init().
  178. (++) Disable the NVIC for DMA
  179. using function HAL_NVIC_EnableIRQ(DMAx_Channelx_IRQn)
  180. [..]
  181. *** Callback registration ***
  182. =============================================
  183. [..]
  184. The compilation flag USE_HAL_ADC_REGISTER_CALLBACKS, when set to 1,
  185. allows the user to configure dynamically the driver callbacks.
  186. Use Functions @ref HAL_ADC_RegisterCallback()
  187. to register an interrupt callback.
  188. [..]
  189. Function @ref HAL_ADC_RegisterCallback() allows to register following callbacks:
  190. (+) ConvCpltCallback : ADC conversion complete callback
  191. (+) ConvHalfCpltCallback : ADC conversion DMA half-transfer callback
  192. (+) LevelOutOfWindowCallback : ADC analog watchdog 1 callback
  193. (+) ErrorCallback : ADC error callback
  194. (+) MspInitCallback : ADC Msp Init callback
  195. (+) MspDeInitCallback : ADC Msp DeInit callback
  196. This function takes as parameters the HAL peripheral handle, the Callback ID
  197. and a pointer to the user callback function.
  198. [..]
  199. Use function @ref HAL_ADC_UnRegisterCallback to reset a callback to the default
  200. weak function.
  201. [..]
  202. @ref HAL_ADC_UnRegisterCallback takes as parameters the HAL peripheral handle,
  203. and the Callback ID.
  204. This function allows to reset following callbacks:
  205. (+) ConvCpltCallback : ADC conversion complete callback
  206. (+) ConvHalfCpltCallback : ADC conversion DMA half-transfer callback
  207. (+) LevelOutOfWindowCallback : ADC analog watchdog 1 callback
  208. (+) ErrorCallback : ADC error callback
  209. (+) MspInitCallback : ADC Msp Init callback
  210. (+) MspDeInitCallback : ADC Msp DeInit callback
  211. [..]
  212. By default, after the @ref HAL_ADC_Init() and when the state is @ref HAL_ADC_STATE_RESET
  213. all callbacks are set to the corresponding weak functions:
  214. examples @ref HAL_ADC_ConvCpltCallback(), @ref HAL_ADC_ErrorCallback().
  215. Exception done for MspInit and MspDeInit functions that are
  216. reset to the legacy weak functions in the @ref HAL_ADC_Init()/ @ref HAL_ADC_DeInit() only when
  217. these callbacks are null (not registered beforehand).
  218. [..]
  219. If MspInit or MspDeInit are not null, the @ref HAL_ADC_Init()/ @ref HAL_ADC_DeInit()
  220. keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
  221. [..]
  222. Callbacks can be registered/unregistered in @ref HAL_ADC_STATE_READY state only.
  223. Exception done MspInit/MspDeInit functions that can be registered/unregistered
  224. in @ref HAL_ADC_STATE_READY or @ref HAL_ADC_STATE_RESET state,
  225. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  226. [..]
  227. Then, the user first registers the MspInit/MspDeInit user callbacks
  228. using @ref HAL_ADC_RegisterCallback() before calling @ref HAL_ADC_DeInit()
  229. or @ref HAL_ADC_Init() function.
  230. [..]
  231. When the compilation flag USE_HAL_ADC_REGISTER_CALLBACKS is set to 0 or
  232. not defined, the callback registration feature is not available and all callbacks
  233. are set to the corresponding weak functions.
  234. @endverbatim
  235. ******************************************************************************
  236. * @attention
  237. *
  238. * <h2><center>&copy; Copyright(c) 2016 STMicroelectronics.
  239. * All rights reserved.</center></h2>
  240. *
  241. * This software component is licensed by ST under BSD 3-Clause license,
  242. * the "License"; You may not use this file except in compliance with the
  243. * License. You may obtain a copy of the License at:
  244. * opensource.org/licenses/BSD-3-Clause
  245. *
  246. ******************************************************************************
  247. */
  248. /* Includes ------------------------------------------------------------------*/
  249. #include "stm32l0xx_hal.h"
  250. /** @addtogroup STM32L0xx_HAL_Driver
  251. * @{
  252. */
  253. /** @defgroup ADC ADC
  254. * @brief ADC HAL module driver
  255. * @{
  256. */
  257. #ifdef HAL_ADC_MODULE_ENABLED
  258. /* Private typedef -----------------------------------------------------------*/
  259. /* Private define ------------------------------------------------------------*/
  260. /** @defgroup ADC_Private_Constants ADC Private Constants
  261. * @{
  262. */
  263. /* Delay for ADC stabilization time. */
  264. /* Maximum delay is 1us (refer to device datasheet, parameter tSTART). */
  265. /* Unit: us */
  266. #define ADC_STAB_DELAY_US ((uint32_t) 1U)
  267. /* Delay for temperature sensor stabilization time. */
  268. /* Maximum delay is 10us (refer to device datasheet, parameter tSTART). */
  269. /* Unit: us */
  270. #define ADC_TEMPSENSOR_DELAY_US ((uint32_t) 10U)
  271. /**
  272. * @}
  273. */
  274. /* Private macro -------------------------------------------------------------*/
  275. /* Private variables ---------------------------------------------------------*/
  276. /* Private function prototypes -----------------------------------------------*/
  277. /** @defgroup ADC_Private_Functions ADC Private Functions
  278. * @{
  279. */
  280. static HAL_StatusTypeDef ADC_Enable(ADC_HandleTypeDef* hadc);
  281. static HAL_StatusTypeDef ADC_Disable(ADC_HandleTypeDef* hadc);
  282. static HAL_StatusTypeDef ADC_ConversionStop(ADC_HandleTypeDef* hadc);
  283. static void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma);
  284. static void ADC_DMAHalfConvCplt(DMA_HandleTypeDef *hdma);
  285. static void ADC_DMAError(DMA_HandleTypeDef *hdma);
  286. static void ADC_DelayMicroSecond(uint32_t microSecond);
  287. /**
  288. * @}
  289. */
  290. /* Exported functions ---------------------------------------------------------*/
  291. /** @defgroup ADC_Exported_Functions ADC Exported Functions
  292. * @{
  293. */
  294. /** @defgroup ADC_Exported_Functions_Group1 Initialization and de-initialization functions
  295. * @brief ADC Initialization and Configuration functions
  296. *
  297. @verbatim
  298. ===============================================================================
  299. ##### Initialization and de-initialization functions #####
  300. ===============================================================================
  301. [..] This section provides functions allowing to:
  302. (+) Initialize and configure the ADC.
  303. (+) De-initialize the ADC.
  304. @endverbatim
  305. * @{
  306. */
  307. /**
  308. * @brief Initialize the ADC peripheral and regular group according to
  309. * parameters specified in structure "ADC_InitTypeDef".
  310. * @note As prerequisite, ADC clock must be configured at RCC top level
  311. * depending on possible clock sources: APB clock of HSI clock.
  312. * See commented example code below that can be copied and uncommented
  313. * into HAL_ADC_MspInit().
  314. * @note Possibility to update parameters on the fly:
  315. * This function initializes the ADC MSP (HAL_ADC_MspInit()) only when
  316. * coming from ADC state reset. Following calls to this function can
  317. * be used to reconfigure some parameters of ADC_InitTypeDef
  318. * structure on the fly, without modifying MSP configuration. If ADC
  319. * MSP has to be modified again, HAL_ADC_DeInit() must be called
  320. * before HAL_ADC_Init().
  321. * The setting of these parameters is conditioned to ADC state.
  322. * For parameters constraints, see comments of structure
  323. * "ADC_InitTypeDef".
  324. * @note This function configures the ADC within 2 scopes: scope of entire
  325. * ADC and scope of regular group. For parameters details, see comments
  326. * of structure "ADC_InitTypeDef".
  327. * @note When device is in mode low-power (low-power run, low-power sleep or stop mode),
  328. * function "HAL_ADCEx_EnableVREFINT()" must be called before function HAL_ADC_Init()
  329. * (in case of previous ADC operations: function HAL_ADC_DeInit() must be called first).
  330. * In case of internal temperature sensor to be measured:
  331. * function "HAL_ADCEx_EnableVREFINTTempSensor()" must be called similarilly.
  332. * @param hadc ADC handle
  333. * @retval HAL status
  334. */
  335. HAL_StatusTypeDef HAL_ADC_Init(ADC_HandleTypeDef* hadc)
  336. {
  337. /* Check ADC handle */
  338. if(hadc == NULL)
  339. {
  340. return HAL_ERROR;
  341. }
  342. /* Check the parameters */
  343. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  344. assert_param(IS_ADC_CLOCKPRESCALER(hadc->Init.ClockPrescaler));
  345. assert_param(IS_ADC_RESOLUTION(hadc->Init.Resolution));
  346. assert_param(IS_ADC_DATA_ALIGN(hadc->Init.DataAlign));
  347. assert_param(IS_ADC_SCAN_MODE(hadc->Init.ScanConvMode));
  348. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
  349. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DiscontinuousConvMode));
  350. assert_param(IS_ADC_EXTTRIG_EDGE(hadc->Init.ExternalTrigConvEdge));
  351. assert_param(IS_ADC_EXTTRIG(hadc->Init.ExternalTrigConv));
  352. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DMAContinuousRequests));
  353. assert_param(IS_ADC_EOC_SELECTION(hadc->Init.EOCSelection));
  354. assert_param(IS_ADC_OVERRUN(hadc->Init.Overrun));
  355. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.LowPowerAutoWait));
  356. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.LowPowerFrequencyMode));
  357. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.LowPowerAutoPowerOff));
  358. assert_param(IS_ADC_SAMPLE_TIME(hadc->Init.SamplingTime));
  359. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.OversamplingMode));
  360. /* As prerequisite, into HAL_ADC_MspInit(), ADC clock must be configured */
  361. /* at RCC top level depending on both possible clock sources: */
  362. /* APB clock or HSI clock. */
  363. /* Refer to header of this file for more details on clock enabling procedure*/
  364. /* Actions performed only if ADC is coming from state reset: */
  365. /* - Initialization of ADC MSP */
  366. /* - ADC voltage regulator enable */
  367. if(hadc->State == HAL_ADC_STATE_RESET)
  368. {
  369. /* Initialize ADC error code */
  370. ADC_CLEAR_ERRORCODE(hadc);
  371. /* Allocate lock resource and initialize it */
  372. hadc->Lock = HAL_UNLOCKED;
  373. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  374. /* Init the ADC Callback settings */
  375. hadc->ConvCpltCallback = HAL_ADC_ConvCpltCallback; /* Legacy weak callback */
  376. hadc->ConvHalfCpltCallback = HAL_ADC_ConvHalfCpltCallback; /* Legacy weak callback */
  377. hadc->LevelOutOfWindowCallback = HAL_ADC_LevelOutOfWindowCallback; /* Legacy weak callback */
  378. hadc->ErrorCallback = HAL_ADC_ErrorCallback; /* Legacy weak callback */
  379. if (hadc->MspInitCallback == NULL)
  380. {
  381. hadc->MspInitCallback = HAL_ADC_MspInit; /* Legacy weak MspInit */
  382. }
  383. /* Init the low level hardware */
  384. hadc->MspInitCallback(hadc);
  385. #else
  386. /* Init the low level hardware */
  387. HAL_ADC_MspInit(hadc);
  388. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  389. }
  390. /* Configuration of ADC parameters if previous preliminary actions are */
  391. /* correctly completed. */
  392. /* and if there is no conversion on going on regular group (ADC can be */
  393. /* enabled anyway, in case of call of this function to update a parameter */
  394. /* on the fly). */
  395. if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL) ||
  396. (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) != RESET) )
  397. {
  398. /* Update ADC state machine to error */
  399. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  400. /* Process unlocked */
  401. __HAL_UNLOCK(hadc);
  402. return HAL_ERROR;
  403. }
  404. /* Set ADC state */
  405. ADC_STATE_CLR_SET(hadc->State,
  406. HAL_ADC_STATE_REG_BUSY,
  407. HAL_ADC_STATE_BUSY_INTERNAL);
  408. /* Parameters update conditioned to ADC state: */
  409. /* Parameters that can be updated only when ADC is disabled: */
  410. /* - ADC clock mode */
  411. /* - ADC clock prescaler */
  412. /* - ADC Resolution */
  413. if (ADC_IS_ENABLE(hadc) == RESET)
  414. {
  415. /* Some parameters of this register are not reset, since they are set */
  416. /* by other functions and must be kept in case of usage of this */
  417. /* function on the fly (update of a parameter of ADC_InitTypeDef */
  418. /* without needing to reconfigure all other ADC groups/channels */
  419. /* parameters): */
  420. /* - internal measurement paths: Vbat, temperature sensor, Vref */
  421. /* (set into HAL_ADC_ConfigChannel() ) */
  422. /* Configuration of ADC clock: clock source PCLK or asynchronous with
  423. selectable prescaler */
  424. __HAL_ADC_CLOCK_PRESCALER(hadc);
  425. /* Configuration of ADC: */
  426. /* - Resolution */
  427. hadc->Instance->CFGR1 &= ~( ADC_CFGR1_RES);
  428. hadc->Instance->CFGR1 |= hadc->Init.Resolution;
  429. }
  430. /* Set the Low Frequency mode */
  431. ADC->CCR &= (uint32_t)~ADC_CCR_LFMEN;
  432. ADC->CCR |=__HAL_ADC_CCR_LOWFREQUENCY(hadc->Init.LowPowerFrequencyMode);
  433. /* Enable voltage regulator (if disabled at this step) */
  434. if (HAL_IS_BIT_CLR(hadc->Instance->CR, ADC_CR_ADVREGEN))
  435. {
  436. /* Set ADVREGEN bit */
  437. hadc->Instance->CR |= ADC_CR_ADVREGEN;
  438. }
  439. /* Configuration of ADC: */
  440. /* - Resolution */
  441. /* - Data alignment */
  442. /* - Scan direction */
  443. /* - External trigger to start conversion */
  444. /* - External trigger polarity */
  445. /* - Continuous conversion mode */
  446. /* - DMA continuous request */
  447. /* - Overrun */
  448. /* - AutoDelay feature */
  449. /* - Discontinuous mode */
  450. hadc->Instance->CFGR1 &= ~(ADC_CFGR1_ALIGN |
  451. ADC_CFGR1_SCANDIR |
  452. ADC_CFGR1_EXTSEL |
  453. ADC_CFGR1_EXTEN |
  454. ADC_CFGR1_CONT |
  455. ADC_CFGR1_DMACFG |
  456. ADC_CFGR1_OVRMOD |
  457. ADC_CFGR1_AUTDLY |
  458. ADC_CFGR1_AUTOFF |
  459. ADC_CFGR1_DISCEN );
  460. hadc->Instance->CFGR1 |= (hadc->Init.DataAlign |
  461. ADC_SCANDIR(hadc->Init.ScanConvMode) |
  462. ADC_CONTINUOUS((uint32_t)hadc->Init.ContinuousConvMode) |
  463. ADC_DMACONTREQ((uint32_t)hadc->Init.DMAContinuousRequests) |
  464. hadc->Init.Overrun |
  465. __HAL_ADC_CFGR1_AutoDelay(hadc->Init.LowPowerAutoWait) |
  466. __HAL_ADC_CFGR1_AUTOFF(hadc->Init.LowPowerAutoPowerOff));
  467. /* Enable external trigger if trigger selection is different of software */
  468. /* start. */
  469. /* Note: This configuration keeps the hardware feature of parameter */
  470. /* ExternalTrigConvEdge "trigger edge none" equivalent to */
  471. /* software start. */
  472. if (hadc->Init.ExternalTrigConv != ADC_SOFTWARE_START)
  473. {
  474. hadc->Instance->CFGR1 |= hadc->Init.ExternalTrigConv |
  475. hadc->Init.ExternalTrigConvEdge;
  476. }
  477. /* Enable discontinuous mode only if continuous mode is disabled */
  478. if (hadc->Init.DiscontinuousConvMode == ENABLE)
  479. {
  480. if (hadc->Init.ContinuousConvMode == DISABLE)
  481. {
  482. /* Enable the selected ADC group regular discontinuous mode */
  483. hadc->Instance->CFGR1 |= (ADC_CFGR1_DISCEN);
  484. }
  485. else
  486. {
  487. /* ADC regular group discontinuous was intended to be enabled, */
  488. /* but ADC regular group modes continuous and sequencer discontinuous */
  489. /* cannot be enabled simultaneously. */
  490. /* Update ADC state machine to error */
  491. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  492. /* Set ADC error code to ADC peripheral internal error */
  493. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  494. }
  495. }
  496. if (hadc->Init.OversamplingMode == ENABLE)
  497. {
  498. assert_param(IS_ADC_OVERSAMPLING_RATIO(hadc->Init.Oversample.Ratio));
  499. assert_param(IS_ADC_RIGHT_BIT_SHIFT(hadc->Init.Oversample.RightBitShift));
  500. assert_param(IS_ADC_TRIGGERED_OVERSAMPLING_MODE(hadc->Init.Oversample.TriggeredMode));
  501. /* Configuration of Oversampler: */
  502. /* - Oversampling Ratio */
  503. /* - Right bit shift */
  504. /* - Triggered mode */
  505. hadc->Instance->CFGR2 &= ~( ADC_CFGR2_OVSR |
  506. ADC_CFGR2_OVSS |
  507. ADC_CFGR2_TOVS );
  508. hadc->Instance->CFGR2 |= ( hadc->Init.Oversample.Ratio |
  509. hadc->Init.Oversample.RightBitShift |
  510. hadc->Init.Oversample.TriggeredMode );
  511. /* Enable OverSampling mode */
  512. hadc->Instance->CFGR2 |= ADC_CFGR2_OVSE;
  513. }
  514. else
  515. {
  516. if(HAL_IS_BIT_SET(hadc->Instance->CFGR2, ADC_CFGR2_OVSE))
  517. {
  518. /* Disable OverSampling mode if needed */
  519. hadc->Instance->CFGR2 &= ~ADC_CFGR2_OVSE;
  520. }
  521. }
  522. /* Clear the old sampling time */
  523. hadc->Instance->SMPR &= (uint32_t)(~ADC_SMPR_SMPR);
  524. /* Set the new sample time */
  525. hadc->Instance->SMPR |= hadc->Init.SamplingTime;
  526. /* Clear ADC error code */
  527. ADC_CLEAR_ERRORCODE(hadc);
  528. /* Set the ADC state */
  529. ADC_STATE_CLR_SET(hadc->State,
  530. HAL_ADC_STATE_BUSY_INTERNAL,
  531. HAL_ADC_STATE_READY);
  532. /* Return function status */
  533. return HAL_OK;
  534. }
  535. /**
  536. * @brief Deinitialize the ADC peripheral registers to their default reset
  537. * values, with deinitialization of the ADC MSP.
  538. * @note For devices with several ADCs: reset of ADC common registers is done
  539. * only if all ADCs sharing the same common group are disabled.
  540. * If this is not the case, reset of these common parameters reset is
  541. * bypassed without error reporting: it can be the intended behavior in
  542. * case of reset of a single ADC while the other ADCs sharing the same
  543. * common group is still running.
  544. * @param hadc ADC handle
  545. * @retval HAL status
  546. */
  547. HAL_StatusTypeDef HAL_ADC_DeInit(ADC_HandleTypeDef* hadc)
  548. {
  549. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  550. /* Check ADC handle */
  551. if(hadc == NULL)
  552. {
  553. return HAL_ERROR;
  554. }
  555. /* Check the parameters */
  556. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  557. /* Set ADC state */
  558. SET_BIT(hadc->State, HAL_ADC_STATE_BUSY_INTERNAL);
  559. /* Stop potential conversion on going, on regular group */
  560. tmp_hal_status = ADC_ConversionStop(hadc);
  561. /* Disable ADC peripheral if conversions are effectively stopped */
  562. if (tmp_hal_status == HAL_OK)
  563. {
  564. /* Disable the ADC peripheral */
  565. tmp_hal_status = ADC_Disable(hadc);
  566. /* Check if ADC is effectively disabled */
  567. if (tmp_hal_status != HAL_ERROR)
  568. {
  569. /* Change ADC state */
  570. hadc->State = HAL_ADC_STATE_READY;
  571. }
  572. }
  573. /* Configuration of ADC parameters if previous preliminary actions are */
  574. /* correctly completed. */
  575. if (tmp_hal_status != HAL_ERROR)
  576. {
  577. /* ========== Reset ADC registers ========== */
  578. /* Reset register IER */
  579. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_AWD | ADC_IT_OVR | ADC_IT_EOCAL | ADC_IT_EOS | \
  580. ADC_IT_EOC | ADC_IT_RDY | ADC_IT_EOSMP ));
  581. /* Reset register ISR */
  582. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_AWD | ADC_FLAG_EOCAL | ADC_FLAG_OVR | ADC_FLAG_EOS | \
  583. ADC_FLAG_EOC | ADC_FLAG_EOSMP | ADC_FLAG_RDY));
  584. /* Reset register CR */
  585. /* Disable voltage regulator */
  586. /* Note: Regulator disable useful for power saving */
  587. /* Reset ADVREGEN bit */
  588. hadc->Instance->CR &= ~ADC_CR_ADVREGEN;
  589. /* Bits ADC_CR_ADSTP, ADC_CR_ADSTART are in access mode "read-set": no direct reset applicable */
  590. /* No action */
  591. /* Reset register CFGR1 */
  592. hadc->Instance->CFGR1 &= ~(ADC_CFGR1_AWDCH | ADC_CFGR1_AWDEN | ADC_CFGR1_AWDSGL | \
  593. ADC_CFGR1_DISCEN | ADC_CFGR1_AUTOFF | ADC_CFGR1_AUTDLY | \
  594. ADC_CFGR1_CONT | ADC_CFGR1_OVRMOD | ADC_CFGR1_EXTEN | \
  595. ADC_CFGR1_EXTSEL | ADC_CFGR1_ALIGN | ADC_CFGR1_RES | \
  596. ADC_CFGR1_SCANDIR| ADC_CFGR1_DMACFG | ADC_CFGR1_DMAEN);
  597. /* Reset register CFGR2 */
  598. hadc->Instance->CFGR2 &= ~(ADC_CFGR2_TOVS | ADC_CFGR2_OVSS | ADC_CFGR2_OVSR | \
  599. ADC_CFGR2_OVSE | ADC_CFGR2_CKMODE );
  600. /* Reset register SMPR */
  601. hadc->Instance->SMPR &= ~(ADC_SMPR_SMPR);
  602. /* Reset register TR */
  603. hadc->Instance->TR &= ~(ADC_TR_LT | ADC_TR_HT);
  604. /* Reset register CALFACT */
  605. hadc->Instance->CALFACT &= ~(ADC_CALFACT_CALFACT);
  606. /* Reset register DR */
  607. /* bits in access mode read only, no direct reset applicable*/
  608. /* Reset register CALFACT */
  609. hadc->Instance->CALFACT &= ~(ADC_CALFACT_CALFACT);
  610. /* ========== Hard reset ADC peripheral ========== */
  611. /* Performs a global reset of the entire ADC peripheral: ADC state is */
  612. /* forced to a similar state after device power-on. */
  613. /* If needed, copy-paste and uncomment the following reset code into */
  614. /* function "void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)": */
  615. /* */
  616. /* __HAL_RCC_ADC1_FORCE_RESET() */
  617. /* __HAL_RCC_ADC1_RELEASE_RESET() */
  618. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  619. if (hadc->MspDeInitCallback == NULL)
  620. {
  621. hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit */
  622. }
  623. /* DeInit the low level hardware */
  624. hadc->MspDeInitCallback(hadc);
  625. #else
  626. /* DeInit the low level hardware */
  627. HAL_ADC_MspDeInit(hadc);
  628. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  629. /* Set ADC error code to none */
  630. ADC_CLEAR_ERRORCODE(hadc);
  631. /* Set ADC state */
  632. hadc->State = HAL_ADC_STATE_RESET;
  633. }
  634. /* Process unlocked */
  635. __HAL_UNLOCK(hadc);
  636. /* Return function status */
  637. return tmp_hal_status;
  638. }
  639. /**
  640. * @brief Initialize the ADC MSP.
  641. * @param hadc ADC handle
  642. * @retval None
  643. */
  644. __weak void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
  645. {
  646. /* Prevent unused argument(s) compilation warning */
  647. UNUSED(hadc);
  648. /* NOTE : This function should not be modified. When the callback is needed,
  649. function HAL_ADC_MspInit must be implemented in the user file.
  650. */
  651. }
  652. /**
  653. * @brief DeInitialize the ADC MSP.
  654. * @param hadc ADC handle
  655. * @retval None
  656. */
  657. __weak void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc)
  658. {
  659. /* Prevent unused argument(s) compilation warning */
  660. UNUSED(hadc);
  661. /* NOTE : This function should not be modified. When the callback is needed,
  662. function HAL_ADC_MspDeInit must be implemented in the user file.
  663. */
  664. }
  665. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  666. /**
  667. * @brief Register a User ADC Callback
  668. * To be used instead of the weak predefined callback
  669. * @param hadc Pointer to a ADC_HandleTypeDef structure that contains
  670. * the configuration information for the specified ADC.
  671. * @param CallbackID ID of the callback to be registered
  672. * This parameter can be one of the following values:
  673. * @arg @ref HAL_ADC_CONVERSION_COMPLETE_CB_ID ADC conversion complete callback ID
  674. * @arg @ref HAL_ADC_CONVERSION_HALF_CB_ID ADC conversion complete callback ID
  675. * @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID ADC analog watchdog 1 callback ID
  676. * @arg @ref HAL_ADC_ERROR_CB_ID ADC error callback ID
  677. * @arg @ref HAL_ADC_INJ_CONVERSION_COMPLETE_CB_ID ADC group injected conversion complete callback ID
  678. * @arg @ref HAL_ADC_MSPINIT_CB_ID ADC Msp Init callback ID
  679. * @arg @ref HAL_ADC_MSPDEINIT_CB_ID ADC Msp DeInit callback ID
  680. * @arg @ref HAL_ADC_MSPINIT_CB_ID MspInit callback ID
  681. * @arg @ref HAL_ADC_MSPDEINIT_CB_ID MspDeInit callback ID
  682. * @param pCallback pointer to the Callback function
  683. * @retval HAL status
  684. */
  685. HAL_StatusTypeDef HAL_ADC_RegisterCallback(ADC_HandleTypeDef *hadc, HAL_ADC_CallbackIDTypeDef CallbackID, pADC_CallbackTypeDef pCallback)
  686. {
  687. HAL_StatusTypeDef status = HAL_OK;
  688. if (pCallback == NULL)
  689. {
  690. /* Update the error code */
  691. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  692. return HAL_ERROR;
  693. }
  694. if ((hadc->State & HAL_ADC_STATE_READY) != 0)
  695. {
  696. switch (CallbackID)
  697. {
  698. case HAL_ADC_CONVERSION_COMPLETE_CB_ID :
  699. hadc->ConvCpltCallback = pCallback;
  700. break;
  701. case HAL_ADC_CONVERSION_HALF_CB_ID :
  702. hadc->ConvHalfCpltCallback = pCallback;
  703. break;
  704. case HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID :
  705. hadc->LevelOutOfWindowCallback = pCallback;
  706. break;
  707. case HAL_ADC_ERROR_CB_ID :
  708. hadc->ErrorCallback = pCallback;
  709. break;
  710. case HAL_ADC_MSPINIT_CB_ID :
  711. hadc->MspInitCallback = pCallback;
  712. break;
  713. case HAL_ADC_MSPDEINIT_CB_ID :
  714. hadc->MspDeInitCallback = pCallback;
  715. break;
  716. default :
  717. /* Update the error code */
  718. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  719. /* Return error status */
  720. status = HAL_ERROR;
  721. break;
  722. }
  723. }
  724. else if (HAL_ADC_STATE_RESET == hadc->State)
  725. {
  726. switch (CallbackID)
  727. {
  728. case HAL_ADC_MSPINIT_CB_ID :
  729. hadc->MspInitCallback = pCallback;
  730. break;
  731. case HAL_ADC_MSPDEINIT_CB_ID :
  732. hadc->MspDeInitCallback = pCallback;
  733. break;
  734. default :
  735. /* Update the error code */
  736. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  737. /* Return error status */
  738. status = HAL_ERROR;
  739. break;
  740. }
  741. }
  742. else
  743. {
  744. /* Update the error code */
  745. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  746. /* Return error status */
  747. status = HAL_ERROR;
  748. }
  749. return status;
  750. }
  751. /**
  752. * @brief Unregister a ADC Callback
  753. * ADC callback is redirected to the weak predefined callback
  754. * @param hadc Pointer to a ADC_HandleTypeDef structure that contains
  755. * the configuration information for the specified ADC.
  756. * @param CallbackID ID of the callback to be unregistered
  757. * This parameter can be one of the following values:
  758. * @arg @ref HAL_ADC_CONVERSION_COMPLETE_CB_ID ADC conversion complete callback ID
  759. * @arg @ref HAL_ADC_CONVERSION_HALF_CB_ID ADC conversion complete callback ID
  760. * @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID ADC analog watchdog 1 callback ID
  761. * @arg @ref HAL_ADC_ERROR_CB_ID ADC error callback ID
  762. * @arg @ref HAL_ADC_INJ_CONVERSION_COMPLETE_CB_ID ADC group injected conversion complete callback ID
  763. * @arg @ref HAL_ADC_MSPINIT_CB_ID ADC Msp Init callback ID
  764. * @arg @ref HAL_ADC_MSPDEINIT_CB_ID ADC Msp DeInit callback ID
  765. * @arg @ref HAL_ADC_MSPINIT_CB_ID MspInit callback ID
  766. * @arg @ref HAL_ADC_MSPDEINIT_CB_ID MspDeInit callback ID
  767. * @retval HAL status
  768. */
  769. HAL_StatusTypeDef HAL_ADC_UnRegisterCallback(ADC_HandleTypeDef *hadc, HAL_ADC_CallbackIDTypeDef CallbackID)
  770. {
  771. HAL_StatusTypeDef status = HAL_OK;
  772. if ((hadc->State & HAL_ADC_STATE_READY) != 0)
  773. {
  774. switch (CallbackID)
  775. {
  776. case HAL_ADC_CONVERSION_COMPLETE_CB_ID :
  777. hadc->ConvCpltCallback = HAL_ADC_ConvCpltCallback;
  778. break;
  779. case HAL_ADC_CONVERSION_HALF_CB_ID :
  780. hadc->ConvHalfCpltCallback = HAL_ADC_ConvHalfCpltCallback;
  781. break;
  782. case HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID :
  783. hadc->LevelOutOfWindowCallback = HAL_ADC_LevelOutOfWindowCallback;
  784. break;
  785. case HAL_ADC_ERROR_CB_ID :
  786. hadc->ErrorCallback = HAL_ADC_ErrorCallback;
  787. break;
  788. case HAL_ADC_MSPINIT_CB_ID :
  789. hadc->MspInitCallback = HAL_ADC_MspInit; /* Legacy weak MspInit */
  790. break;
  791. case HAL_ADC_MSPDEINIT_CB_ID :
  792. hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit */
  793. break;
  794. default :
  795. /* Update the error code */
  796. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  797. /* Return error status */
  798. status = HAL_ERROR;
  799. break;
  800. }
  801. }
  802. else if (HAL_ADC_STATE_RESET == hadc->State)
  803. {
  804. switch (CallbackID)
  805. {
  806. case HAL_ADC_MSPINIT_CB_ID :
  807. hadc->MspInitCallback = HAL_ADC_MspInit; /* Legacy weak MspInit */
  808. break;
  809. case HAL_ADC_MSPDEINIT_CB_ID :
  810. hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit */
  811. break;
  812. default :
  813. /* Update the error code */
  814. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  815. /* Return error status */
  816. status = HAL_ERROR;
  817. break;
  818. }
  819. }
  820. else
  821. {
  822. /* Update the error code */
  823. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  824. /* Return error status */
  825. status = HAL_ERROR;
  826. }
  827. return status;
  828. }
  829. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  830. /**
  831. * @}
  832. */
  833. /** @defgroup ADC_Exported_Functions_Group2 ADC Input and Output operation functions
  834. * @brief ADC IO operation functions
  835. *
  836. @verbatim
  837. ===============================================================================
  838. ##### IO operation functions #####
  839. ===============================================================================
  840. [..] This section provides functions allowing to:
  841. (+) Start conversion of regular group.
  842. (+) Stop conversion of regular group.
  843. (+) Poll for conversion complete on regular group.
  844. (+) Poll for conversion event.
  845. (+) Get result of regular channel conversion.
  846. (+) Start conversion of regular group and enable interruptions.
  847. (+) Stop conversion of regular group and disable interruptions.
  848. (+) Handle ADC interrupt request
  849. (+) Start conversion of regular group and enable DMA transfer.
  850. (+) Stop conversion of regular group and disable ADC DMA transfer.
  851. @endverbatim
  852. * @{
  853. */
  854. /**
  855. * @brief Enable ADC, start conversion of regular group.
  856. * @note Interruptions enabled in this function: None.
  857. * @param hadc ADC handle
  858. * @retval HAL status
  859. */
  860. HAL_StatusTypeDef HAL_ADC_Start(ADC_HandleTypeDef* hadc)
  861. {
  862. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  863. /* Check the parameters */
  864. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  865. /* Perform ADC enable and conversion start if no conversion is on going */
  866. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  867. {
  868. /* Process locked */
  869. __HAL_LOCK(hadc);
  870. /* Enable the ADC peripheral */
  871. /* If low power mode AutoPowerOff is enabled, power-on/off phases are */
  872. /* performed automatically by hardware. */
  873. if (hadc->Init.LowPowerAutoPowerOff != ENABLE)
  874. {
  875. tmp_hal_status = ADC_Enable(hadc);
  876. }
  877. /* Start conversion if ADC is effectively enabled */
  878. if (tmp_hal_status == HAL_OK)
  879. {
  880. /* Set ADC state */
  881. /* - Clear state bitfield related to regular group conversion results */
  882. /* - Set state bitfield related to regular operation */
  883. ADC_STATE_CLR_SET(hadc->State,
  884. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP,
  885. HAL_ADC_STATE_REG_BUSY);
  886. /* Reset ADC all error code fields */
  887. ADC_CLEAR_ERRORCODE(hadc);
  888. /* Process unlocked */
  889. /* Unlock before starting ADC conversions: in case of potential */
  890. /* interruption, to let the process to ADC IRQ Handler. */
  891. __HAL_UNLOCK(hadc);
  892. /* Clear regular group conversion flag and overrun flag */
  893. /* (To ensure of no unknown state from potential previous ADC */
  894. /* operations) */
  895. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
  896. /* Enable conversion of regular group. */
  897. /* If software start has been selected, conversion starts immediately. */
  898. /* If external trigger has been selected, conversion will start at next */
  899. /* trigger event. */
  900. hadc->Instance->CR |= ADC_CR_ADSTART;
  901. }
  902. }
  903. else
  904. {
  905. tmp_hal_status = HAL_BUSY;
  906. }
  907. /* Return function status */
  908. return tmp_hal_status;
  909. }
  910. /**
  911. * @brief Stop ADC conversion of regular group (and injected channels in
  912. * case of auto_injection mode), disable ADC peripheral.
  913. * @param hadc ADC handle
  914. * @retval HAL status.
  915. */
  916. HAL_StatusTypeDef HAL_ADC_Stop(ADC_HandleTypeDef* hadc)
  917. {
  918. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  919. /* Check the parameters */
  920. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  921. /* Process locked */
  922. __HAL_LOCK(hadc);
  923. /* 1. Stop potential conversion on going, on ADC group regular */
  924. tmp_hal_status = ADC_ConversionStop(hadc);
  925. /* Disable ADC peripheral if conversions are effectively stopped */
  926. if (tmp_hal_status == HAL_OK)
  927. {
  928. /* 2. Disable the ADC peripheral */
  929. tmp_hal_status = ADC_Disable(hadc);
  930. /* Check if ADC is effectively disabled */
  931. if (tmp_hal_status == HAL_OK)
  932. {
  933. /* Set ADC state */
  934. ADC_STATE_CLR_SET(hadc->State,
  935. HAL_ADC_STATE_REG_BUSY,
  936. HAL_ADC_STATE_READY);
  937. }
  938. }
  939. /* Process unlocked */
  940. __HAL_UNLOCK(hadc);
  941. /* Return function status */
  942. return tmp_hal_status;
  943. }
  944. /**
  945. * @brief Wait for regular group conversion to be completed.
  946. * @note ADC conversion flags EOS (end of sequence) and EOC (end of
  947. * conversion) are cleared by this function, with an exception:
  948. * if low power feature "LowPowerAutoWait" is enabled, flags are
  949. * not cleared to not interfere with this feature until data register
  950. * is read using function HAL_ADC_GetValue().
  951. * @note This function cannot be used in a particular setup: ADC configured
  952. * in DMA mode and polling for end of each conversion (ADC init
  953. * parameter "EOCSelection" set to ADC_EOC_SINGLE_CONV).
  954. * In this case, DMA resets the flag EOC and polling cannot be
  955. * performed on each conversion. Nevertheless, polling can still
  956. * be performed on the complete sequence (ADC init
  957. * parameter "EOCSelection" set to ADC_EOC_SEQ_CONV).
  958. * @param hadc ADC handle
  959. * @param Timeout Timeout value in millisecond.
  960. * @retval HAL status
  961. */
  962. HAL_StatusTypeDef HAL_ADC_PollForConversion(ADC_HandleTypeDef* hadc, uint32_t Timeout)
  963. {
  964. uint32_t tickstart = 0;
  965. uint32_t tmp_Flag_EOC = 0x00;
  966. /* Check the parameters */
  967. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  968. /* If end of conversion selected to end of sequence conversions */
  969. if (hadc->Init.EOCSelection == ADC_EOC_SEQ_CONV)
  970. {
  971. tmp_Flag_EOC = ADC_FLAG_EOS;
  972. }
  973. /* If end of conversion selected to end of unitary conversion */
  974. else /* ADC_EOC_SINGLE_CONV */
  975. {
  976. /* Verification that ADC configuration is compliant with polling for */
  977. /* each conversion: */
  978. /* Particular case is ADC configured in DMA mode and ADC sequencer with */
  979. /* several ranks and polling for end of each conversion. */
  980. /* For code simplicity sake, this particular case is generalized to */
  981. /* ADC configured in DMA mode and and polling for end of each conversion. */
  982. if (HAL_IS_BIT_SET(hadc->Instance->CFGR1, ADC_CFGR1_DMAEN))
  983. {
  984. /* Update ADC state machine to error */
  985. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  986. /* Process unlocked */
  987. __HAL_UNLOCK(hadc);
  988. return HAL_ERROR;
  989. }
  990. else
  991. {
  992. tmp_Flag_EOC = (ADC_FLAG_EOC | ADC_FLAG_EOS);
  993. }
  994. }
  995. /* Get tick count */
  996. tickstart = HAL_GetTick();
  997. /* Wait until End of unitary conversion or sequence conversions flag is raised */
  998. while(HAL_IS_BIT_CLR(hadc->Instance->ISR, tmp_Flag_EOC))
  999. {
  1000. /* Check if timeout is disabled (set to infinite wait) */
  1001. if(Timeout != HAL_MAX_DELAY)
  1002. {
  1003. if((Timeout == 0U) || ((HAL_GetTick()-tickstart) > Timeout))
  1004. {
  1005. /* Update ADC state machine to timeout */
  1006. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  1007. /* Process unlocked */
  1008. __HAL_UNLOCK(hadc);
  1009. return HAL_TIMEOUT;
  1010. }
  1011. }
  1012. }
  1013. /* Update ADC state machine */
  1014. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  1015. /* Determine whether any further conversion upcoming on group regular */
  1016. /* by external trigger, continuous mode or scan sequence on going. */
  1017. if(ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  1018. (hadc->Init.ContinuousConvMode == DISABLE) )
  1019. {
  1020. /* If End of Sequence is reached, disable interrupts */
  1021. if( __HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOS) )
  1022. {
  1023. /* Allowed to modify bits ADC_IT_EOC/ADC_IT_EOS only if bit */
  1024. /* ADSTART==0 (no conversion on going) */
  1025. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  1026. {
  1027. /* Disable ADC end of single conversion interrupt on group regular */
  1028. /* Note: Overrun interrupt was enabled with EOC interrupt in */
  1029. /* HAL_Start_IT(), but is not disabled here because can be used */
  1030. /* by overrun IRQ process below. */
  1031. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC | ADC_IT_EOS);
  1032. /* Set ADC state */
  1033. ADC_STATE_CLR_SET(hadc->State,
  1034. HAL_ADC_STATE_REG_BUSY,
  1035. HAL_ADC_STATE_READY);
  1036. }
  1037. else
  1038. {
  1039. /* Change ADC state to error state */
  1040. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1041. /* Set ADC error code to ADC peripheral internal error */
  1042. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  1043. }
  1044. }
  1045. }
  1046. /* Clear end of conversion flag of regular group if low power feature */
  1047. /* "LowPowerAutoWait " is disabled, to not interfere with this feature */
  1048. /* until data register is read using function HAL_ADC_GetValue(). */
  1049. if (hadc->Init.LowPowerAutoWait == DISABLE)
  1050. {
  1051. /* Clear regular group conversion flag */
  1052. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS));
  1053. }
  1054. /* Return function status */
  1055. return HAL_OK;
  1056. }
  1057. /**
  1058. * @brief Poll for ADC event.
  1059. * @param hadc ADC handle
  1060. * @param EventType the ADC event type.
  1061. * This parameter can be one of the following values:
  1062. * @arg ADC_AWD_EVENT: ADC Analog watchdog event
  1063. * @arg ADC_OVR_EVENT: ADC Overrun event
  1064. * @param Timeout Timeout value in millisecond.
  1065. * @note The relevant flag is cleared if found to be set, except for ADC_FLAG_OVR.
  1066. * Indeed, the latter is reset only if hadc->Init.Overrun field is set
  1067. * to ADC_OVR_DATA_OVERWRITTEN. Otherwise, data register may be potentially overwritten
  1068. * by a new converted data as soon as OVR is cleared.
  1069. * To reset OVR flag once the preserved data is retrieved, the user can resort
  1070. * to macro __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  1071. * @retval HAL status
  1072. */
  1073. HAL_StatusTypeDef HAL_ADC_PollForEvent(ADC_HandleTypeDef* hadc, uint32_t EventType, uint32_t Timeout)
  1074. {
  1075. uint32_t tickstart = 0U;
  1076. /* Check the parameters */
  1077. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1078. assert_param(IS_ADC_EVENT_TYPE(EventType));
  1079. /* Get tick count */
  1080. tickstart = HAL_GetTick();
  1081. /* Check selected event flag */
  1082. while(__HAL_ADC_GET_FLAG(hadc, EventType) == RESET)
  1083. {
  1084. /* Check if timeout is disabled (set to infinite wait) */
  1085. if(Timeout != HAL_MAX_DELAY)
  1086. {
  1087. if((Timeout == 0U) ||((HAL_GetTick() - tickstart ) > Timeout))
  1088. {
  1089. /* Update ADC state machine to timeout */
  1090. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  1091. /* Process unlocked */
  1092. __HAL_UNLOCK(hadc);
  1093. return HAL_TIMEOUT;
  1094. }
  1095. }
  1096. }
  1097. switch(EventType)
  1098. {
  1099. /* Analog watchdog (level out of window) event */
  1100. case ADC_AWD_EVENT:
  1101. /* Set ADC state */
  1102. SET_BIT(hadc->State, HAL_ADC_STATE_AWD1);
  1103. /* Clear ADC analog watchdog flag */
  1104. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD);
  1105. break;
  1106. /* Overrun event */
  1107. default: /* Case ADC_OVR_EVENT */
  1108. /* If overrun is set to overwrite previous data, overrun event is not */
  1109. /* considered as an error. */
  1110. /* (cf ref manual "Managing conversions without using the DMA and without */
  1111. /* overrun ") */
  1112. if (hadc->Init.Overrun == ADC_OVR_DATA_PRESERVED)
  1113. {
  1114. /* Set ADC state */
  1115. SET_BIT(hadc->State, HAL_ADC_STATE_REG_OVR);
  1116. /* Set ADC error code to overrun */
  1117. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_OVR);
  1118. }
  1119. /* Clear ADC Overrun flag */
  1120. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  1121. break;
  1122. }
  1123. /* Return function status */
  1124. return HAL_OK;
  1125. }
  1126. /**
  1127. * @brief Enable ADC, start conversion of regular group with interruption.
  1128. * @note Interruptions enabled in this function according to initialization
  1129. * setting : EOC (end of conversion), EOS (end of sequence),
  1130. * OVR overrun.
  1131. * Each of these interruptions has its dedicated callback function.
  1132. * @note To guarantee a proper reset of all interruptions once all the needed
  1133. * conversions are obtained, HAL_ADC_Stop_IT() must be called to ensure
  1134. * a correct stop of the IT-based conversions.
  1135. * @note By default, HAL_ADC_Start_IT() doesn't enable the End Of Sampling
  1136. * interruption. If required (e.g. in case of oversampling with trigger
  1137. * mode), the user must:
  1138. * 1. first clear the EOSMP flag if set with macro __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOSMP)
  1139. * 2. then enable the EOSMP interrupt with macro __HAL_ADC_ENABLE_IT(hadc, ADC_IT_EOSMP)
  1140. * before calling HAL_ADC_Start_IT().
  1141. * @param hadc ADC handle
  1142. * @retval HAL status
  1143. */
  1144. HAL_StatusTypeDef HAL_ADC_Start_IT(ADC_HandleTypeDef* hadc)
  1145. {
  1146. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1147. /* Check the parameters */
  1148. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1149. /* Perform ADC enable and conversion start if no conversion is on going */
  1150. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  1151. {
  1152. /* Process locked */
  1153. __HAL_LOCK(hadc);
  1154. /* Enable the ADC peripheral */
  1155. /* If low power mode AutoPowerOff is enabled, power-on/off phases are */
  1156. /* performed automatically by hardware. */
  1157. if (hadc->Init.LowPowerAutoPowerOff != ENABLE)
  1158. {
  1159. tmp_hal_status = ADC_Enable(hadc);
  1160. }
  1161. /* Start conversion if ADC is effectively enabled */
  1162. if (tmp_hal_status == HAL_OK)
  1163. {
  1164. /* Set ADC state */
  1165. /* - Clear state bitfield related to regular group conversion results */
  1166. /* - Set state bitfield related to regular operation */
  1167. ADC_STATE_CLR_SET(hadc->State,
  1168. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP,
  1169. HAL_ADC_STATE_REG_BUSY);
  1170. /* Reset ADC all error code fields */
  1171. ADC_CLEAR_ERRORCODE(hadc);
  1172. /* Process unlocked */
  1173. /* Unlock before starting ADC conversions: in case of potential */
  1174. /* interruption, to let the process to ADC IRQ Handler. */
  1175. __HAL_UNLOCK(hadc);
  1176. /* Clear regular group conversion flag and overrun flag */
  1177. /* (To ensure of no unknown state from potential previous ADC */
  1178. /* operations) */
  1179. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
  1180. /* Enable ADC end of conversion interrupt */
  1181. /* Enable ADC overrun interrupt */
  1182. switch(hadc->Init.EOCSelection)
  1183. {
  1184. case ADC_EOC_SEQ_CONV:
  1185. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC);
  1186. __HAL_ADC_ENABLE_IT(hadc, (ADC_IT_EOS | ADC_IT_OVR));
  1187. break;
  1188. /* case ADC_EOC_SINGLE_CONV */
  1189. default:
  1190. __HAL_ADC_ENABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_EOS | ADC_IT_OVR));
  1191. break;
  1192. }
  1193. /* Enable conversion of regular group. */
  1194. /* If software start has been selected, conversion starts immediately. */
  1195. /* If external trigger has been selected, conversion will start at next */
  1196. /* trigger event. */
  1197. hadc->Instance->CR |= ADC_CR_ADSTART;
  1198. }
  1199. }
  1200. else
  1201. {
  1202. tmp_hal_status = HAL_BUSY;
  1203. }
  1204. /* Return function status */
  1205. return tmp_hal_status;
  1206. }
  1207. /**
  1208. * @brief Stop ADC conversion of regular group (and injected group in
  1209. * case of auto_injection mode), disable interrution of
  1210. * end-of-conversion, disable ADC peripheral.
  1211. * @param hadc ADC handle
  1212. * @retval HAL status.
  1213. */
  1214. HAL_StatusTypeDef HAL_ADC_Stop_IT(ADC_HandleTypeDef* hadc)
  1215. {
  1216. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1217. /* Check the parameters */
  1218. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1219. /* Process locked */
  1220. __HAL_LOCK(hadc);
  1221. /* 1. Stop potential conversion on going, on ADC group regular */
  1222. tmp_hal_status = ADC_ConversionStop(hadc);
  1223. /* Disable ADC peripheral if conversions are effectively stopped */
  1224. if (tmp_hal_status == HAL_OK)
  1225. {
  1226. /* Disable ADC end of conversion interrupt for regular group */
  1227. /* Disable ADC overrun interrupt */
  1228. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_EOS | ADC_IT_OVR));
  1229. /* 2. Disable the ADC peripheral */
  1230. tmp_hal_status = ADC_Disable(hadc);
  1231. /* Check if ADC is effectively disabled */
  1232. if (tmp_hal_status == HAL_OK)
  1233. {
  1234. /* Set ADC state */
  1235. ADC_STATE_CLR_SET(hadc->State,
  1236. HAL_ADC_STATE_REG_BUSY,
  1237. HAL_ADC_STATE_READY);
  1238. }
  1239. }
  1240. /* Process unlocked */
  1241. __HAL_UNLOCK(hadc);
  1242. /* Return function status */
  1243. return tmp_hal_status;
  1244. }
  1245. /**
  1246. * @brief Enable ADC, start conversion of regular group and transfer result through DMA.
  1247. * @note Interruptions enabled in this function:
  1248. * overrun (if applicable), DMA half transfer, DMA transfer complete.
  1249. * Each of these interruptions has its dedicated callback function.
  1250. * @param hadc ADC handle
  1251. * @param pData Destination Buffer address.
  1252. * @param Length Length of data to be transferred from ADC peripheral to memory (in bytes)
  1253. * @retval HAL status.
  1254. */
  1255. HAL_StatusTypeDef HAL_ADC_Start_DMA(ADC_HandleTypeDef* hadc, uint32_t* pData, uint32_t Length)
  1256. {
  1257. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1258. /* Check the parameters */
  1259. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1260. /* Perform ADC enable and conversion start if no conversion is on going */
  1261. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  1262. {
  1263. /* Process locked */
  1264. __HAL_LOCK(hadc);
  1265. /* Enable the ADC peripheral */
  1266. /* If low power mode AutoPowerOff is enabled, power-on/off phases are */
  1267. /* performed automatically by hardware. */
  1268. if (hadc->Init.LowPowerAutoPowerOff != ENABLE)
  1269. {
  1270. tmp_hal_status = ADC_Enable(hadc);
  1271. }
  1272. /* Start conversion if ADC is effectively enabled */
  1273. if (tmp_hal_status == HAL_OK)
  1274. {
  1275. /* Set ADC state */
  1276. /* - Clear state bitfield related to regular group conversion results */
  1277. /* - Set state bitfield related to regular operation */
  1278. ADC_STATE_CLR_SET(hadc->State,
  1279. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP,
  1280. HAL_ADC_STATE_REG_BUSY);
  1281. /* Reset ADC all error code fields */
  1282. ADC_CLEAR_ERRORCODE(hadc);
  1283. /* Process unlocked */
  1284. /* Unlock before starting ADC conversions: in case of potential */
  1285. /* interruption, to let the process to ADC IRQ Handler. */
  1286. __HAL_UNLOCK(hadc);
  1287. /* Set the DMA transfer complete callback */
  1288. hadc->DMA_Handle->XferCpltCallback = ADC_DMAConvCplt;
  1289. /* Set the DMA half transfer complete callback */
  1290. hadc->DMA_Handle->XferHalfCpltCallback = ADC_DMAHalfConvCplt;
  1291. /* Set the DMA error callback */
  1292. hadc->DMA_Handle->XferErrorCallback = ADC_DMAError;
  1293. /* Manage ADC and DMA start: ADC overrun interruption, DMA start, ADC */
  1294. /* start (in case of SW start): */
  1295. /* Clear regular group conversion flag and overrun flag */
  1296. /* (To ensure of no unknown state from potential previous ADC */
  1297. /* operations) */
  1298. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
  1299. /* Enable ADC overrun interrupt */
  1300. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);
  1301. /* Enable ADC DMA mode */
  1302. hadc->Instance->CFGR1 |= ADC_CFGR1_DMAEN;
  1303. /* Start the DMA channel */
  1304. HAL_DMA_Start_IT(hadc->DMA_Handle, (uint32_t)&hadc->Instance->DR, (uint32_t)pData, Length);
  1305. /* Enable conversion of regular group. */
  1306. /* If software start has been selected, conversion starts immediately. */
  1307. /* If external trigger has been selected, conversion will start at next */
  1308. /* trigger event. */
  1309. hadc->Instance->CR |= ADC_CR_ADSTART;
  1310. }
  1311. }
  1312. else
  1313. {
  1314. tmp_hal_status = HAL_BUSY;
  1315. }
  1316. /* Return function status */
  1317. return tmp_hal_status;
  1318. }
  1319. /**
  1320. * @brief Stop ADC conversion of regular group (and injected group in
  1321. * case of auto_injection mode), disable ADC DMA transfer, disable
  1322. * ADC peripheral.
  1323. * Each of these interruptions has its dedicated callback function.
  1324. * @param hadc ADC handle
  1325. * @retval HAL status.
  1326. */
  1327. HAL_StatusTypeDef HAL_ADC_Stop_DMA(ADC_HandleTypeDef* hadc)
  1328. {
  1329. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1330. /* Check the parameters */
  1331. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1332. /* Process locked */
  1333. __HAL_LOCK(hadc);
  1334. /* 1. Stop potential ADC group regular conversion on going */
  1335. tmp_hal_status = ADC_ConversionStop(hadc);
  1336. /* Disable ADC peripheral if conversions are effectively stopped */
  1337. if (tmp_hal_status == HAL_OK)
  1338. {
  1339. /* Disable ADC DMA (ADC DMA configuration ADC_CFGR_DMACFG is kept) */
  1340. CLEAR_BIT(hadc->Instance->CFGR1, ADC_CFGR1_DMAEN);
  1341. /* Disable the DMA channel (in case of DMA in circular mode or stop */
  1342. /* while DMA transfer is on going) */
  1343. tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
  1344. /* Check if DMA channel effectively disabled */
  1345. if (tmp_hal_status != HAL_OK)
  1346. {
  1347. /* Update ADC state machine to error */
  1348. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  1349. }
  1350. /* Disable ADC overrun interrupt */
  1351. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
  1352. /* 2. Disable the ADC peripheral */
  1353. /* Update "tmp_hal_status" only if DMA channel disabling passed, to keep */
  1354. /* in memory a potential failing status. */
  1355. if (tmp_hal_status == HAL_OK)
  1356. {
  1357. tmp_hal_status = ADC_Disable(hadc);
  1358. }
  1359. else
  1360. {
  1361. ADC_Disable(hadc);
  1362. }
  1363. /* Check if ADC is effectively disabled */
  1364. if (tmp_hal_status == HAL_OK)
  1365. {
  1366. /* Set ADC state */
  1367. ADC_STATE_CLR_SET(hadc->State,
  1368. HAL_ADC_STATE_REG_BUSY,
  1369. HAL_ADC_STATE_READY);
  1370. }
  1371. }
  1372. /* Process unlocked */
  1373. __HAL_UNLOCK(hadc);
  1374. /* Return function status */
  1375. return tmp_hal_status;
  1376. }
  1377. /**
  1378. * @brief Get ADC regular group conversion result.
  1379. * @note Reading register DR automatically clears ADC flag EOC
  1380. * (ADC group regular end of unitary conversion).
  1381. * @note This function does not clear ADC flag EOS
  1382. * (ADC group regular end of sequence conversion).
  1383. * Occurrence of flag EOS rising:
  1384. * - If sequencer is composed of 1 rank, flag EOS is equivalent
  1385. * to flag EOC.
  1386. * - If sequencer is composed of several ranks, during the scan
  1387. * sequence flag EOC only is raised, at the end of the scan sequence
  1388. * both flags EOC and EOS are raised.
  1389. * To clear this flag, either use function:
  1390. * in programming model IT: @ref HAL_ADC_IRQHandler(), in programming
  1391. * model polling: @ref HAL_ADC_PollForConversion()
  1392. * or @ref __HAL_ADC_CLEAR_FLAG(&hadc, ADC_FLAG_EOS).
  1393. * @param hadc ADC handle
  1394. * @retval ADC group regular conversion data
  1395. */
  1396. uint32_t HAL_ADC_GetValue(ADC_HandleTypeDef* hadc)
  1397. {
  1398. /* Check the parameters */
  1399. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1400. /* Note: EOC flag is not cleared here by software because automatically */
  1401. /* cleared by hardware when reading register DR. */
  1402. /* Return ADC converted value */
  1403. return hadc->Instance->DR;
  1404. }
  1405. /**
  1406. * @brief Handle ADC interrupt request.
  1407. * @param hadc ADC handle
  1408. * @retval None
  1409. */
  1410. void HAL_ADC_IRQHandler(ADC_HandleTypeDef* hadc)
  1411. {
  1412. /* Check the parameters */
  1413. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1414. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
  1415. assert_param(IS_ADC_EOC_SELECTION(hadc->Init.EOCSelection));
  1416. /* ========== Check End of Conversion flag for regular group ========== */
  1417. if( (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOC) && __HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_EOC)) ||
  1418. (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOS) && __HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_EOS)) )
  1419. {
  1420. /* Update state machine on conversion status if not in error state */
  1421. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL))
  1422. {
  1423. /* Set ADC state */
  1424. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  1425. }
  1426. /* Determine whether any further conversion upcoming on group regular */
  1427. /* by external trigger, continuous mode or scan sequence on going. */
  1428. if(ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  1429. (hadc->Init.ContinuousConvMode == DISABLE) )
  1430. {
  1431. /* If End of Sequence is reached, disable interrupts */
  1432. if( __HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOS) )
  1433. {
  1434. /* Allowed to modify bits ADC_IT_EOC/ADC_IT_EOS only if bit */
  1435. /* ADSTART==0 (no conversion on going) */
  1436. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  1437. {
  1438. /* Disable ADC end of single conversion interrupt on group regular */
  1439. /* Note: Overrun interrupt was enabled with EOC interrupt in */
  1440. /* HAL_Start_IT(), but is not disabled here because can be used */
  1441. /* by overrun IRQ process below. */
  1442. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC | ADC_IT_EOS);
  1443. /* Set ADC state */
  1444. ADC_STATE_CLR_SET(hadc->State,
  1445. HAL_ADC_STATE_REG_BUSY,
  1446. HAL_ADC_STATE_READY);
  1447. }
  1448. else
  1449. {
  1450. /* Change ADC state to error state */
  1451. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1452. /* Set ADC error code to ADC peripheral internal error */
  1453. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  1454. }
  1455. }
  1456. }
  1457. /* Note: into callback, to determine if conversion has been triggered */
  1458. /* from EOC or EOS, possibility to use: */
  1459. /* " if( __HAL_ADC_GET_FLAG(&hadc, ADC_FLAG_EOS)) " */
  1460. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  1461. hadc->ConvCpltCallback(hadc);
  1462. #else
  1463. HAL_ADC_ConvCpltCallback(hadc);
  1464. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  1465. /* Clear regular group conversion flag */
  1466. /* Note: in case of overrun set to ADC_OVR_DATA_PRESERVED, end of */
  1467. /* conversion flags clear induces the release of the preserved data.*/
  1468. /* Therefore, if the preserved data value is needed, it must be */
  1469. /* read preliminarily into HAL_ADC_ConvCpltCallback(). */
  1470. /* Note: Management of low power auto-wait enabled: flags must be cleared */
  1471. /* by user when fetching ADC conversion data. */
  1472. /* This case is managed in IRQ handler, but this low-power mode */
  1473. /* should not be used with programming model IT or DMA. */
  1474. /* Refer to comment of parameter "LowPowerAutoWait". */
  1475. if (hadc->Init.LowPowerAutoWait != ENABLE)
  1476. {
  1477. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS));
  1478. }
  1479. }
  1480. /* ========== Check analog watchdog 1 flag ========== */
  1481. if(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_AWD) && __HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_AWD))
  1482. {
  1483. /* Set ADC state */
  1484. SET_BIT(hadc->State, HAL_ADC_STATE_AWD1);
  1485. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  1486. hadc->LevelOutOfWindowCallback(hadc);
  1487. #else
  1488. HAL_ADC_LevelOutOfWindowCallback(hadc);
  1489. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  1490. /* Clear ADC Analog watchdog flag */
  1491. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD);
  1492. }
  1493. /* ========== Check Overrun flag ========== */
  1494. if(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_OVR) && __HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_OVR))
  1495. {
  1496. /* If overrun is set to overwrite previous data (default setting), */
  1497. /* overrun event is not considered as an error. */
  1498. /* (cf ref manual "Managing conversions without using the DMA and without */
  1499. /* overrun ") */
  1500. /* Exception for usage with DMA overrun event always considered as an */
  1501. /* error. */
  1502. if ((hadc->Init.Overrun == ADC_OVR_DATA_PRESERVED) ||
  1503. HAL_IS_BIT_SET(hadc->Instance->CFGR1, ADC_CFGR1_DMAEN) )
  1504. {
  1505. /* Set ADC error code to overrun */
  1506. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_OVR);
  1507. /* Clear ADC overrun flag */
  1508. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  1509. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  1510. hadc->ErrorCallback(hadc);
  1511. #else
  1512. HAL_ADC_ErrorCallback(hadc);
  1513. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  1514. }
  1515. /* Clear the Overrun flag */
  1516. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  1517. }
  1518. }
  1519. /**
  1520. * @brief Conversion complete callback in non-blocking mode.
  1521. * @param hadc ADC handle
  1522. * @retval None
  1523. */
  1524. __weak void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
  1525. {
  1526. /* Prevent unused argument(s) compilation warning */
  1527. UNUSED(hadc);
  1528. /* NOTE : This function should not be modified. When the callback is needed,
  1529. function HAL_ADC_ConvCpltCallback must be implemented in the user file.
  1530. */
  1531. }
  1532. /**
  1533. * @brief Conversion DMA half-transfer callback in non-blocking mode.
  1534. * @param hadc ADC handle
  1535. * @retval None
  1536. */
  1537. __weak void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc)
  1538. {
  1539. /* Prevent unused argument(s) compilation warning */
  1540. UNUSED(hadc);
  1541. /* NOTE : This function should not be modified. When the callback is needed,
  1542. function HAL_ADC_ConvHalfCpltCallback must be implemented in the user file.
  1543. */
  1544. }
  1545. /**
  1546. * @brief Analog watchdog 1 callback in non-blocking mode.
  1547. * @param hadc ADC handle
  1548. * @retval None
  1549. */
  1550. __weak void HAL_ADC_LevelOutOfWindowCallback(ADC_HandleTypeDef* hadc)
  1551. {
  1552. /* Prevent unused argument(s) compilation warning */
  1553. UNUSED(hadc);
  1554. /* NOTE : This function should not be modified. When the callback is needed,
  1555. function HAL_ADC_LevelOutOfWindowCallback must be implemented in the user file.
  1556. */
  1557. }
  1558. /**
  1559. * @brief ADC error callback in non-blocking mode
  1560. * (ADC conversion with interruption or transfer by DMA).
  1561. * @note In case of error due to overrun when using ADC with DMA transfer
  1562. * (HAL ADC handle paramater "ErrorCode" to state "HAL_ADC_ERROR_OVR"):
  1563. * - Reinitialize the DMA using function "HAL_ADC_Stop_DMA()".
  1564. * - If needed, restart a new ADC conversion using function
  1565. * "HAL_ADC_Start_DMA()"
  1566. * (this function is also clearing overrun flag)
  1567. * @param hadc ADC handle
  1568. * @retval None
  1569. */
  1570. __weak void HAL_ADC_ErrorCallback(ADC_HandleTypeDef *hadc)
  1571. {
  1572. /* Prevent unused argument(s) compilation warning */
  1573. UNUSED(hadc);
  1574. /* NOTE : This function should not be modified. When the callback is needed,
  1575. function HAL_ADC_ErrorCallback must be implemented in the user file.
  1576. */
  1577. }
  1578. /**
  1579. * @}
  1580. */
  1581. /** @defgroup ADC_Exported_Functions_Group3 Peripheral Control functions
  1582. * @brief Peripheral Control functions
  1583. *
  1584. @verbatim
  1585. ===============================================================================
  1586. ##### Peripheral Control functions #####
  1587. ===============================================================================
  1588. [..] This section provides functions allowing to:
  1589. (+) Configure channels on regular group
  1590. (+) Configure the analog watchdog
  1591. @endverbatim
  1592. * @{
  1593. */
  1594. /**
  1595. * @brief Configure a channel to be assigned to ADC group regular.
  1596. * @note In case of usage of internal measurement channels:
  1597. * VrefInt/Vlcd(STM32L0x3xx only)/TempSensor.
  1598. * Sampling time constraints must be respected (sampling time can be
  1599. * adjusted in function of ADC clock frequency and sampling time
  1600. * setting).
  1601. * Refer to device datasheet for timings values, parameters TS_vrefint,
  1602. * TS_vlcd (STM32L0x3xx only), TS_temp (values rough order: 5us to 17us).
  1603. * These internal paths can be be disabled using function
  1604. * HAL_ADC_DeInit().
  1605. * @note Possibility to update parameters on the fly:
  1606. * This function initializes channel into ADC group regular,
  1607. * following calls to this function can be used to reconfigure
  1608. * some parameters of structure "ADC_ChannelConfTypeDef" on the fly,
  1609. * without resetting the ADC.
  1610. * The setting of these parameters is conditioned to ADC state:
  1611. * Refer to comments of structure "ADC_ChannelConfTypeDef".
  1612. * @param hadc ADC handle
  1613. * @param sConfig Structure of ADC channel assigned to ADC group regular.
  1614. * @retval HAL status
  1615. */
  1616. HAL_StatusTypeDef HAL_ADC_ConfigChannel(ADC_HandleTypeDef* hadc, ADC_ChannelConfTypeDef* sConfig)
  1617. {
  1618. /* Check the parameters */
  1619. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1620. assert_param(IS_ADC_CHANNEL(sConfig->Channel));
  1621. assert_param(IS_ADC_RANK(sConfig->Rank));
  1622. /* Process locked */
  1623. __HAL_LOCK(hadc);
  1624. /* Parameters update conditioned to ADC state: */
  1625. /* Parameters that can be updated when ADC is disabled or enabled without */
  1626. /* conversion on going on regular group: */
  1627. /* - Channel number */
  1628. /* - Management of internal measurement channels: Vbat/VrefInt/TempSensor */
  1629. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) != RESET)
  1630. {
  1631. /* Update ADC state machine to error */
  1632. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1633. /* Process unlocked */
  1634. __HAL_UNLOCK(hadc);
  1635. return HAL_ERROR;
  1636. }
  1637. if (sConfig->Rank != ADC_RANK_NONE)
  1638. {
  1639. /* Enable selected channels */
  1640. hadc->Instance->CHSELR |= (uint32_t)(sConfig->Channel & ADC_CHANNEL_MASK);
  1641. /* Management of internal measurement channels: Vlcd (STM32L0x3xx only)/VrefInt/TempSensor */
  1642. /* internal measurement paths enable: If internal channel selected, enable */
  1643. /* dedicated internal buffers and path. */
  1644. #if defined(ADC_CCR_TSEN)
  1645. /* If Temperature sensor channel is selected, then enable the internal */
  1646. /* buffers and path */
  1647. if (((sConfig->Channel & ADC_CHANNEL_MASK) & ADC_CHANNEL_TEMPSENSOR ) == (ADC_CHANNEL_TEMPSENSOR & ADC_CHANNEL_MASK))
  1648. {
  1649. ADC->CCR |= ADC_CCR_TSEN;
  1650. /* Delay for temperature sensor stabilization time */
  1651. ADC_DelayMicroSecond(ADC_TEMPSENSOR_DELAY_US);
  1652. }
  1653. #endif
  1654. /* If VRefInt channel is selected, then enable the internal buffers and path */
  1655. if (((sConfig->Channel & ADC_CHANNEL_MASK) & ADC_CHANNEL_VREFINT) == (ADC_CHANNEL_VREFINT & ADC_CHANNEL_MASK))
  1656. {
  1657. ADC->CCR |= ADC_CCR_VREFEN;
  1658. }
  1659. #if defined (STM32L053xx) || defined (STM32L063xx) || defined (STM32L073xx) || defined (STM32L083xx)
  1660. /* If Vlcd channel is selected, then enable the internal buffers and path */
  1661. if (((sConfig->Channel & ADC_CHANNEL_MASK) & ADC_CHANNEL_VLCD) == (ADC_CHANNEL_VLCD & ADC_CHANNEL_MASK))
  1662. {
  1663. ADC->CCR |= ADC_CCR_VLCDEN;
  1664. }
  1665. #endif
  1666. }
  1667. else
  1668. {
  1669. /* Regular sequence configuration */
  1670. /* Reset the channel selection register from the selected channel */
  1671. hadc->Instance->CHSELR &= ~((uint32_t)(sConfig->Channel & ADC_CHANNEL_MASK));
  1672. /* Management of internal measurement channels: VrefInt/TempSensor/Vbat */
  1673. /* internal measurement paths disable: If internal channel selected, */
  1674. /* disable dedicated internal buffers and path. */
  1675. #if defined(ADC_CCR_TSEN)
  1676. if (((sConfig->Channel & ADC_CHANNEL_MASK) & ADC_CHANNEL_TEMPSENSOR ) == (ADC_CHANNEL_TEMPSENSOR & ADC_CHANNEL_MASK))
  1677. {
  1678. ADC->CCR &= ~ADC_CCR_TSEN;
  1679. }
  1680. #endif
  1681. /* If VRefInt channel is selected, then enable the internal buffers and path */
  1682. if (((sConfig->Channel & ADC_CHANNEL_MASK) & ADC_CHANNEL_VREFINT) == (ADC_CHANNEL_VREFINT & ADC_CHANNEL_MASK))
  1683. {
  1684. ADC->CCR &= ~ADC_CCR_VREFEN;
  1685. }
  1686. #if defined (STM32L053xx) || defined (STM32L063xx) || defined (STM32L073xx) || defined (STM32L083xx)
  1687. /* If Vlcd channel is selected, then enable the internal buffers and path */
  1688. if (((sConfig->Channel & ADC_CHANNEL_MASK) & ADC_CHANNEL_VLCD) == (ADC_CHANNEL_VLCD & ADC_CHANNEL_MASK))
  1689. {
  1690. ADC->CCR &= ~ADC_CCR_VLCDEN;
  1691. }
  1692. #endif
  1693. }
  1694. /* Process unlocked */
  1695. __HAL_UNLOCK(hadc);
  1696. /* Return function status */
  1697. return HAL_OK;
  1698. }
  1699. /**
  1700. * @brief Configure the analog watchdog.
  1701. * @note Possibility to update parameters on the fly:
  1702. * This function initializes the selected analog watchdog, successive
  1703. * calls to this function can be used to reconfigure some parameters
  1704. * of structure "ADC_AnalogWDGConfTypeDef" on the fly, without resetting
  1705. * the ADC.
  1706. * The setting of these parameters is conditioned to ADC state.
  1707. * For parameters constraints, see comments of structure
  1708. * "ADC_AnalogWDGConfTypeDef".
  1709. * @note Analog watchdog thresholds can be modified while ADC conversion
  1710. * is on going.
  1711. * In this case, some constraints must be taken into account:
  1712. * the programmed threshold values are effective from the next
  1713. * ADC EOC (end of unitary conversion).
  1714. * Considering that registers write delay may happen due to
  1715. * bus activity, this might cause an uncertainty on the
  1716. * effective timing of the new programmed threshold values.
  1717. * @param hadc ADC handle
  1718. * @param AnalogWDGConfig Structure of ADC analog watchdog configuration
  1719. * @retval HAL status
  1720. */
  1721. HAL_StatusTypeDef HAL_ADC_AnalogWDGConfig(ADC_HandleTypeDef* hadc, ADC_AnalogWDGConfTypeDef* AnalogWDGConfig)
  1722. {
  1723. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1724. uint32_t tmpAWDHighThresholdShifted;
  1725. uint32_t tmpAWDLowThresholdShifted;
  1726. /* Check the parameters */
  1727. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1728. assert_param(IS_ADC_ANALOG_WATCHDOG_MODE(AnalogWDGConfig->WatchdogMode));
  1729. assert_param(IS_FUNCTIONAL_STATE(AnalogWDGConfig->ITMode));
  1730. if(AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_REG)
  1731. {
  1732. assert_param(IS_ADC_CHANNEL(AnalogWDGConfig->Channel));
  1733. }
  1734. /* Verify if threshold is within the selected ADC resolution */
  1735. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), AnalogWDGConfig->HighThreshold));
  1736. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), AnalogWDGConfig->LowThreshold));
  1737. /* Process locked */
  1738. __HAL_LOCK(hadc);
  1739. /* Parameters update conditioned to ADC state: */
  1740. /* Parameters that can be updated when ADC is disabled or enabled without */
  1741. /* conversion on going on regular group: */
  1742. /* - Analog watchdog channels */
  1743. /* - Analog watchdog thresholds */
  1744. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  1745. {
  1746. /* Configure ADC Analog watchdog interrupt */
  1747. if(AnalogWDGConfig->ITMode == ENABLE)
  1748. {
  1749. /* Enable the ADC Analog watchdog interrupt */
  1750. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_AWD);
  1751. }
  1752. else
  1753. {
  1754. /* Disable the ADC Analog watchdog interrupt */
  1755. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_AWD);
  1756. }
  1757. /* Configuration of analog watchdog: */
  1758. /* - Set the analog watchdog mode */
  1759. /* - Set the Analog watchdog channel (is not used if watchdog */
  1760. /* mode "all channels": ADC_CFGR1_AWD1SGL=0) */
  1761. hadc->Instance->CFGR1 &= ~( ADC_CFGR1_AWDSGL |
  1762. ADC_CFGR1_AWDEN |
  1763. ADC_CFGR1_AWDCH);
  1764. hadc->Instance->CFGR1 |= ( AnalogWDGConfig->WatchdogMode |
  1765. (AnalogWDGConfig->Channel & ADC_CHANNEL_AWD_MASK));
  1766. /* Shift the offset in function of the selected ADC resolution: Thresholds */
  1767. /* have to be left-aligned on bit 11, the LSB (right bits) are set to 0 */
  1768. tmpAWDHighThresholdShifted = ADC_AWD1THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->HighThreshold);
  1769. tmpAWDLowThresholdShifted = ADC_AWD1THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->LowThreshold);
  1770. /* Clear High & Low high thresholds */
  1771. hadc->Instance->TR &= (uint32_t) ~ (ADC_TR_HT | ADC_TR_LT);
  1772. /* Set the high threshold */
  1773. hadc->Instance->TR = ADC_TRX_HIGHTHRESHOLD (tmpAWDHighThresholdShifted);
  1774. /* Set the low threshold */
  1775. hadc->Instance->TR |= tmpAWDLowThresholdShifted;
  1776. }
  1777. /* If a conversion is on going on regular group, no update could be done */
  1778. /* on neither of the AWD configuration structure parameters. */
  1779. else
  1780. {
  1781. /* Update ADC state machine to error */
  1782. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1783. tmp_hal_status = HAL_ERROR;
  1784. }
  1785. /* Process unlocked */
  1786. __HAL_UNLOCK(hadc);
  1787. /* Return function status */
  1788. return tmp_hal_status;
  1789. }
  1790. /**
  1791. * @}
  1792. */
  1793. /** @defgroup ADC_Exported_Functions_Group4 Peripheral State functions
  1794. * @brief ADC Peripheral State functions
  1795. *
  1796. @verbatim
  1797. ===============================================================================
  1798. ##### Peripheral state and errors functions #####
  1799. ===============================================================================
  1800. [..]
  1801. This subsection provides functions to get in run-time the status of the
  1802. peripheral.
  1803. (+) Check the ADC state
  1804. (+) Check the ADC error code
  1805. @endverbatim
  1806. * @{
  1807. */
  1808. /**
  1809. * @brief Return the ADC handle state.
  1810. * @note ADC state machine is managed by bitfields, ADC status must be
  1811. * compared with states bits.
  1812. * For example:
  1813. * " if (HAL_IS_BIT_SET(HAL_ADC_GetState(hadc1), HAL_ADC_STATE_REG_BUSY)) "
  1814. * " if (HAL_IS_BIT_SET(HAL_ADC_GetState(hadc1), HAL_ADC_STATE_AWD1) ) "
  1815. * @param hadc ADC handle
  1816. * @retval ADC handle state (bitfield on 32 bits)
  1817. */
  1818. uint32_t HAL_ADC_GetState(ADC_HandleTypeDef* hadc)
  1819. {
  1820. /* Check the parameters */
  1821. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1822. /* Return ADC handle state */
  1823. return hadc->State;
  1824. }
  1825. /**
  1826. * @brief Return the ADC error code.
  1827. * @param hadc ADC handle
  1828. * @retval ADC error code (bitfield on 32 bits)
  1829. */
  1830. uint32_t HAL_ADC_GetError(ADC_HandleTypeDef *hadc)
  1831. {
  1832. /* Check the parameters */
  1833. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1834. return hadc->ErrorCode;
  1835. }
  1836. /**
  1837. * @}
  1838. */
  1839. /**
  1840. * @}
  1841. */
  1842. /** @defgroup ADC_Private_Functions ADC Private Functions
  1843. * @{
  1844. */
  1845. /**
  1846. * @brief Enable the selected ADC.
  1847. * @note Prerequisite condition to use this function: ADC must be disabled
  1848. * and voltage regulator must be enabled (done into HAL_ADC_Init()).
  1849. * @note If low power mode AutoPowerOff is enabled, power-on/off phases are
  1850. * performed automatically by hardware.
  1851. * In this mode, this function is useless and must not be called because
  1852. * flag ADC_FLAG_RDY is not usable.
  1853. * Therefore, this function must be called under condition of
  1854. * "if (hadc->Init.LowPowerAutoPowerOff != ENABLE)".
  1855. * @param hadc ADC handle
  1856. * @retval HAL status.
  1857. */
  1858. static HAL_StatusTypeDef ADC_Enable(ADC_HandleTypeDef* hadc)
  1859. {
  1860. uint32_t tickstart = 0U;
  1861. /* ADC enable and wait for ADC ready (in case of ADC is disabled or */
  1862. /* enabling phase not yet completed: flag ADC ready not yet set). */
  1863. /* Timeout implemented to not be stuck if ADC cannot be enabled (possible */
  1864. /* causes: ADC clock not running, ...). */
  1865. if (ADC_IS_ENABLE(hadc) == RESET)
  1866. {
  1867. /* Check if conditions to enable the ADC are fulfilled */
  1868. if (ADC_ENABLING_CONDITIONS(hadc) == RESET)
  1869. {
  1870. /* Update ADC state machine to error */
  1871. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  1872. /* Set ADC error code to ADC peripheral internal error */
  1873. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  1874. return HAL_ERROR;
  1875. }
  1876. /* Enable the ADC peripheral */
  1877. __HAL_ADC_ENABLE(hadc);
  1878. /* Delay for ADC stabilization time. */
  1879. ADC_DelayMicroSecond(ADC_STAB_DELAY_US);
  1880. /* Get tick count */
  1881. tickstart = HAL_GetTick();
  1882. /* Wait for ADC effectively enabled */
  1883. while(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_RDY) == RESET)
  1884. {
  1885. if((HAL_GetTick() - tickstart) > ADC_ENABLE_TIMEOUT)
  1886. {
  1887. /* Update ADC state machine to error */
  1888. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  1889. /* Set ADC error code to ADC peripheral internal error */
  1890. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  1891. return HAL_ERROR;
  1892. }
  1893. }
  1894. }
  1895. /* Return HAL status */
  1896. return HAL_OK;
  1897. }
  1898. /**
  1899. * @brief Disable the selected ADC.
  1900. * @note Prerequisite condition to use this function: ADC conversions must be
  1901. * stopped.
  1902. * @param hadc ADC handle
  1903. * @retval HAL status.
  1904. */
  1905. static HAL_StatusTypeDef ADC_Disable(ADC_HandleTypeDef* hadc)
  1906. {
  1907. uint32_t tickstart = 0U;
  1908. /* Verification if ADC is not already disabled: */
  1909. /* Note: forbidden to disable ADC (set bit ADC_CR_ADDIS) if ADC is already */
  1910. /* disabled. */
  1911. if (ADC_IS_ENABLE(hadc) != RESET)
  1912. {
  1913. /* Check if conditions to disable the ADC are fulfilled */
  1914. if (ADC_DISABLING_CONDITIONS(hadc) != RESET)
  1915. {
  1916. /* Disable the ADC peripheral */
  1917. __HAL_ADC_DISABLE(hadc);
  1918. }
  1919. else
  1920. {
  1921. /* Update ADC state machine to error */
  1922. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  1923. /* Set ADC error code to ADC peripheral internal error */
  1924. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  1925. return HAL_ERROR;
  1926. }
  1927. /* Wait for ADC effectively disabled */
  1928. /* Get tick count */
  1929. tickstart = HAL_GetTick();
  1930. while(HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADEN))
  1931. {
  1932. if((HAL_GetTick() - tickstart) > ADC_DISABLE_TIMEOUT)
  1933. {
  1934. /* Update ADC state machine to error */
  1935. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  1936. /* Set ADC error code to ADC peripheral internal error */
  1937. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  1938. return HAL_ERROR;
  1939. }
  1940. }
  1941. }
  1942. /* Return HAL status */
  1943. return HAL_OK;
  1944. }
  1945. /**
  1946. * @brief Stop ADC conversion.
  1947. * @note Prerequisite condition to use this function: ADC conversions must be
  1948. * stopped to disable the ADC.
  1949. * @param hadc ADC handle
  1950. * @retval HAL status.
  1951. */
  1952. static HAL_StatusTypeDef ADC_ConversionStop(ADC_HandleTypeDef* hadc)
  1953. {
  1954. uint32_t tickstart = 0U;
  1955. /* Check the parameters */
  1956. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1957. /* Verification if ADC is not already stopped on regular group to bypass */
  1958. /* this function if not needed. */
  1959. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc))
  1960. {
  1961. /* Stop potential conversion on going on regular group */
  1962. /* Software is allowed to set ADSTP only when ADSTART=1 and ADDIS=0 */
  1963. if (HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADSTART) &&
  1964. HAL_IS_BIT_CLR(hadc->Instance->CR, ADC_CR_ADDIS) )
  1965. {
  1966. /* Stop conversions on regular group */
  1967. hadc->Instance->CR |= ADC_CR_ADSTP;
  1968. }
  1969. /* Wait for conversion effectively stopped */
  1970. /* Get tick count */
  1971. tickstart = HAL_GetTick();
  1972. while((hadc->Instance->CR & ADC_CR_ADSTART) != RESET)
  1973. {
  1974. if((HAL_GetTick() - tickstart) > ADC_STOP_CONVERSION_TIMEOUT)
  1975. {
  1976. /* Update ADC state machine to error */
  1977. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  1978. /* Set ADC error code to ADC peripheral internal error */
  1979. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  1980. return HAL_ERROR;
  1981. }
  1982. }
  1983. }
  1984. /* Return HAL status */
  1985. return HAL_OK;
  1986. }
  1987. /**
  1988. * @brief DMA transfer complete callback.
  1989. * @param hdma pointer to DMA handle.
  1990. * @retval None
  1991. */
  1992. static void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma)
  1993. {
  1994. /* Retrieve ADC handle corresponding to current DMA handle */
  1995. ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1996. /* Update state machine on conversion status if not in error state */
  1997. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL | HAL_ADC_STATE_ERROR_DMA))
  1998. {
  1999. /* Set ADC state */
  2000. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  2001. /* Determine whether any further conversion upcoming on group regular */
  2002. /* by external trigger, continuous mode or scan sequence on going. */
  2003. if(ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  2004. (hadc->Init.ContinuousConvMode == DISABLE) )
  2005. {
  2006. /* If End of Sequence is reached, disable interrupts */
  2007. if( __HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOS) )
  2008. {
  2009. /* Allowed to modify bits ADC_IT_EOC/ADC_IT_EOS only if bit */
  2010. /* ADSTART==0 (no conversion on going) */
  2011. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  2012. {
  2013. /* Disable ADC end of single conversion interrupt on group regular */
  2014. /* Note: Overrun interrupt was enabled with EOC interrupt in */
  2015. /* HAL_Start_IT(), but is not disabled here because can be used */
  2016. /* by overrun IRQ process below. */
  2017. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC | ADC_IT_EOS);
  2018. /* Set ADC state */
  2019. ADC_STATE_CLR_SET(hadc->State,
  2020. HAL_ADC_STATE_REG_BUSY,
  2021. HAL_ADC_STATE_READY);
  2022. }
  2023. else
  2024. {
  2025. /* Change ADC state to error state */
  2026. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  2027. /* Set ADC error code to ADC peripheral internal error */
  2028. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2029. }
  2030. }
  2031. }
  2032. /* Conversion complete callback */
  2033. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2034. hadc->ConvCpltCallback(hadc);
  2035. #else
  2036. HAL_ADC_ConvCpltCallback(hadc);
  2037. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2038. }
  2039. else
  2040. {
  2041. /* Call DMA error callback */
  2042. hadc->DMA_Handle->XferErrorCallback(hdma);
  2043. }
  2044. }
  2045. /**
  2046. * @brief DMA half transfer complete callback.
  2047. * @param hdma pointer to DMA handle.
  2048. * @retval None
  2049. */
  2050. static void ADC_DMAHalfConvCplt(DMA_HandleTypeDef *hdma)
  2051. {
  2052. /* Retrieve ADC handle corresponding to current DMA handle */
  2053. ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  2054. /* Half conversion callback */
  2055. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2056. hadc->ConvHalfCpltCallback(hadc);
  2057. #else
  2058. HAL_ADC_ConvHalfCpltCallback(hadc);
  2059. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2060. }
  2061. /**
  2062. * @brief DMA error callback.
  2063. * @param hdma pointer to DMA handle.
  2064. * @retval None
  2065. */
  2066. static void ADC_DMAError(DMA_HandleTypeDef *hdma)
  2067. {
  2068. /* Retrieve ADC handle corresponding to current DMA handle */
  2069. ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  2070. /* Set ADC state */
  2071. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  2072. /* Set ADC error code to DMA error */
  2073. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_DMA);
  2074. /* Error callback */
  2075. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2076. hadc->ErrorCallback(hadc);
  2077. #else
  2078. HAL_ADC_ErrorCallback(hadc);
  2079. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2080. }
  2081. /**
  2082. * @brief Delay micro seconds
  2083. * @param microSecond delay
  2084. * @retval None
  2085. */
  2086. static void ADC_DelayMicroSecond(uint32_t microSecond)
  2087. {
  2088. /* Compute number of CPU cycles to wait for */
  2089. __IO uint32_t waitLoopIndex = (microSecond * (SystemCoreClock / 1000000U));
  2090. while(waitLoopIndex != 0U)
  2091. {
  2092. waitLoopIndex--;
  2093. }
  2094. }
  2095. /**
  2096. * @}
  2097. */
  2098. #endif /* HAL_ADC_MODULE_ENABLED */
  2099. /**
  2100. * @}
  2101. */
  2102. /**
  2103. * @}
  2104. */
  2105. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/