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.
 
 
 

3253 lines
125 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32wbxx_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. * "stm32wbxx_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 (channel wise)
  34. (+) External trigger (timer or EXTI) with configurable polarity
  35. (+) DMA request generation for transfer of conversions data of regular group.
  36. (+) ADC channels selectable single/differential input.
  37. (+) ADC offset shared on 4 offset instances.
  38. (+) ADC calibration
  39. (+) ADC conversion of regular group.
  40. (+) ADC supply requirements: 1.62 V to 3.6 V.
  41. (+) ADC input range: from Vref- (connected to Vssa) to Vref+ (connected to
  42. Vdda or to an external voltage reference).
  43. ##### How to use this driver #####
  44. ==============================================================================
  45. [..]
  46. *** Configuration of top level parameters related to ADC ***
  47. ============================================================
  48. [..]
  49. (#) Enable the ADC interface
  50. (++) As prerequisite, ADC clock must be configured at RCC top level.
  51. (++) Two clock settings are mandatory:
  52. (+++) ADC clock (core clock, also possibly conversion clock).
  53. (+++) ADC clock (conversions clock).
  54. Two possible clock sources: synchronous clock derived from AHB2 clock
  55. or asynchronous clock derived from system clock, PLLSAI1 (output divider R) or the PLL system (output divider P)
  56. running up to 64MHz.
  57. (+++) Example:
  58. Into HAL_ADC_MspInit() (recommended code location) or with
  59. other device clock parameters configuration:
  60. (+++) __HAL_RCC_ADC_CLK_ENABLE(); (mandatory)
  61. RCC_ADCCLKSOURCE_PLL enable: (optional: if asynchronous clock selected)
  62. (+++) RCC_PeriphClkInitTypeDef RCC_PeriphClkInit;
  63. (+++) PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  64. (+++) PeriphClkInit.AdcClockSelection = RCC_ADCCLKSOURCE_PLL;
  65. (+++) HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
  66. (++) ADC clock source and clock prescaler are configured at ADC level with
  67. parameter "ClockPrescaler" using function HAL_ADC_Init().
  68. (#) ADC pins configuration
  69. (++) Enable the clock for the ADC GPIOs
  70. using macro __HAL_RCC_GPIOx_CLK_ENABLE()
  71. (++) Configure these ADC pins in analog mode
  72. using function HAL_GPIO_Init()
  73. (#) Optionally, in case of usage of ADC with interruptions:
  74. (++) Configure the NVIC for ADC
  75. using function HAL_NVIC_EnableIRQ(ADCx_IRQn)
  76. (++) Insert the ADC interruption handler function HAL_ADC_IRQHandler()
  77. into the function of corresponding ADC interruption vector
  78. ADCx_IRQHandler().
  79. (#) Optionally, in case of usage of DMA:
  80. (++) Configure the DMA (DMA channel, mode normal or circular, ...)
  81. using function HAL_DMA_Init().
  82. (++) Configure the NVIC for DMA
  83. using function HAL_NVIC_EnableIRQ(DMAx_Channelx_IRQn)
  84. (++) Insert the ADC interruption handler function HAL_ADC_IRQHandler()
  85. into the function of corresponding DMA interruption vector
  86. DMAx_Channelx_IRQHandler().
  87. *** Configuration of ADC, group regular, channels parameters ***
  88. ================================================================
  89. [..]
  90. (#) Configure the ADC parameters (resolution, data alignment, ...)
  91. and regular group parameters (conversion trigger, sequencer, ...)
  92. using function HAL_ADC_Init().
  93. (#) Configure the channels for regular group parameters (channel number,
  94. channel rank into sequencer, ..., into regular group)
  95. using function HAL_ADC_ConfigChannel().
  96. (#) Optionally, configure the analog watchdog parameters (channels
  97. monitored, thresholds, ...)
  98. using function HAL_ADC_AnalogWDGConfig().
  99. *** Execution of ADC conversions ***
  100. ====================================
  101. [..]
  102. (#) Optionally, perform an automatic ADC calibration to improve the
  103. conversion accuracy
  104. using function HAL_ADCEx_Calibration_Start().
  105. (#) ADC driver can be used among three modes: polling, interruption,
  106. transfer by DMA.
  107. (++) ADC conversion by polling:
  108. (+++) Activate the ADC peripheral and start conversions
  109. using function HAL_ADC_Start()
  110. (+++) Wait for ADC conversion completion
  111. using function HAL_ADC_PollForConversion()
  112. (+++) Retrieve conversion results
  113. using function HAL_ADC_GetValue()
  114. (+++) Stop conversion and disable the ADC peripheral
  115. using function HAL_ADC_Stop()
  116. (++) ADC conversion by interruption:
  117. (+++) Activate the ADC peripheral and start conversions
  118. using function HAL_ADC_Start_IT()
  119. (+++) Wait for ADC conversion completion by call of function
  120. HAL_ADC_ConvCpltCallback()
  121. (this function must be implemented in user program)
  122. (+++) Retrieve conversion results
  123. using function HAL_ADC_GetValue()
  124. (+++) Stop conversion and disable the ADC peripheral
  125. using function HAL_ADC_Stop_IT()
  126. (++) ADC conversion with transfer by DMA:
  127. (+++) Activate the ADC peripheral and start conversions
  128. using function HAL_ADC_Start_DMA()
  129. (+++) Wait for ADC conversion completion by call of function
  130. HAL_ADC_ConvCpltCallback() or HAL_ADC_ConvHalfCpltCallback()
  131. (these functions must be implemented in user program)
  132. (+++) Conversion results are automatically transferred by DMA into
  133. destination variable address.
  134. (+++) Stop conversion and disable the ADC peripheral
  135. using function HAL_ADC_Stop_DMA()
  136. [..]
  137. (@) Callback functions must be implemented in user program:
  138. (+@) HAL_ADC_ErrorCallback()
  139. (+@) HAL_ADC_LevelOutOfWindowCallback() (callback of analog watchdog)
  140. (+@) HAL_ADC_ConvCpltCallback()
  141. (+@) HAL_ADC_ConvHalfCpltCallback
  142. *** Deinitialization of ADC ***
  143. ============================================================
  144. [..]
  145. (#) Disable the ADC interface
  146. (++) ADC clock can be hard reset and disabled at RCC top level.
  147. (++) Hard reset of ADC peripherals
  148. using macro __ADCx_FORCE_RESET(), __ADCx_RELEASE_RESET().
  149. (++) ADC clock disable
  150. using the equivalent macro/functions as configuration step.
  151. (+++) Example:
  152. Into HAL_ADC_MspDeInit() (recommended code location) or with
  153. other device clock parameters configuration:
  154. (+++) RCC_OscInitStructure.OscillatorType = RCC_OSCILLATORTYPE_HSI14;
  155. (+++) RCC_OscInitStructure.HSI14State = RCC_HSI14_OFF; (if not used for system clock)
  156. (+++) HAL_RCC_OscConfig(&RCC_OscInitStructure);
  157. (#) ADC pins configuration
  158. (++) Disable the clock for the ADC GPIOs
  159. using macro __HAL_RCC_GPIOx_CLK_DISABLE()
  160. (#) Optionally, in case of usage of ADC with interruptions:
  161. (++) Disable the NVIC for ADC
  162. using function HAL_NVIC_EnableIRQ(ADCx_IRQn)
  163. (#) Optionally, in case of usage of DMA:
  164. (++) Deinitialize the DMA
  165. using function HAL_DMA_Init().
  166. (++) Disable the NVIC for DMA
  167. using function HAL_NVIC_EnableIRQ(DMAx_Channelx_IRQn)
  168. [..]
  169. *** Callback registration ***
  170. =============================================
  171. [..]
  172. The compilation flag USE_HAL_ADC_REGISTER_CALLBACKS, when set to 1,
  173. allows the user to configure dynamically the driver callbacks.
  174. Use Functions @ref HAL_ADC_RegisterCallback()
  175. to register an interrupt callback.
  176. [..]
  177. Function @ref HAL_ADC_RegisterCallback() allows to register following callbacks:
  178. (+) ConvCpltCallback : ADC conversion complete callback
  179. (+) ConvHalfCpltCallback : ADC conversion DMA half-transfer callback
  180. (+) LevelOutOfWindowCallback : ADC analog watchdog 1 callback
  181. (+) ErrorCallback : ADC error callback
  182. (+) InjectedConvCpltCallback : ADC group injected conversion complete callback
  183. (+) InjectedQueueOverflowCallback : ADC group injected context queue overflow callback
  184. (+) LevelOutOfWindow2Callback : ADC analog watchdog 2 callback
  185. (+) LevelOutOfWindow3Callback : ADC analog watchdog 3 callback
  186. (+) EndOfSamplingCallback : ADC end of sampling callback
  187. (+) MspInitCallback : ADC Msp Init callback
  188. (+) MspDeInitCallback : ADC Msp DeInit callback
  189. This function takes as parameters the HAL peripheral handle, the Callback ID
  190. and a pointer to the user callback function.
  191. [..]
  192. Use function @ref HAL_ADC_UnRegisterCallback to reset a callback to the default
  193. weak function.
  194. [..]
  195. @ref HAL_ADC_UnRegisterCallback takes as parameters the HAL peripheral handle,
  196. and the Callback ID.
  197. This function allows to reset following callbacks:
  198. (+) ConvCpltCallback : ADC conversion complete callback
  199. (+) ConvHalfCpltCallback : ADC conversion DMA half-transfer callback
  200. (+) LevelOutOfWindowCallback : ADC analog watchdog 1 callback
  201. (+) ErrorCallback : ADC error callback
  202. (+) InjectedConvCpltCallback : ADC group injected conversion complete callback
  203. (+) InjectedQueueOverflowCallback : ADC group injected context queue overflow callback
  204. (+) LevelOutOfWindow2Callback : ADC analog watchdog 2 callback
  205. (+) LevelOutOfWindow3Callback : ADC analog watchdog 3 callback
  206. (+) EndOfSamplingCallback : ADC end of sampling callback
  207. (+) MspInitCallback : ADC Msp Init callback
  208. (+) MspDeInitCallback : ADC Msp DeInit callback
  209. [..]
  210. By default, after the @ref HAL_ADC_Init() and when the state is @ref HAL_ADC_STATE_RESET
  211. all callbacks are set to the corresponding weak functions:
  212. examples @ref HAL_ADC_ConvCpltCallback(), @ref HAL_ADC_ErrorCallback().
  213. Exception done for MspInit and MspDeInit functions that are
  214. reset to the legacy weak functions in the @ref HAL_ADC_Init()/ @ref HAL_ADC_DeInit() only when
  215. these callbacks are null (not registered beforehand).
  216. [..]
  217. If MspInit or MspDeInit are not null, the @ref HAL_ADC_Init()/ @ref HAL_ADC_DeInit()
  218. keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
  219. [..]
  220. Callbacks can be registered/unregistered in @ref HAL_ADC_STATE_READY state only.
  221. Exception done MspInit/MspDeInit functions that can be registered/unregistered
  222. in @ref HAL_ADC_STATE_READY or @ref HAL_ADC_STATE_RESET state,
  223. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  224. [..]
  225. Then, the user first registers the MspInit/MspDeInit user callbacks
  226. using @ref HAL_ADC_RegisterCallback() before calling @ref HAL_ADC_DeInit()
  227. or @ref HAL_ADC_Init() function.
  228. [..]
  229. When the compilation flag USE_HAL_ADC_REGISTER_CALLBACKS is set to 0 or
  230. not defined, the callback registration feature is not available and all callbacks
  231. are set to the corresponding weak functions.
  232. @endverbatim
  233. ******************************************************************************
  234. * @attention
  235. *
  236. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  237. * All rights reserved.</center></h2>
  238. *
  239. * This software component is licensed by ST under BSD 3-Clause license,
  240. * the "License"; You may not use this file except in compliance with the
  241. * License. You may obtain a copy of the License at:
  242. * opensource.org/licenses/BSD-3-Clause
  243. *
  244. ******************************************************************************
  245. */
  246. /* Includes ------------------------------------------------------------------*/
  247. #include "stm32wbxx_hal.h"
  248. /** @addtogroup STM32WBxx_HAL_Driver
  249. * @{
  250. */
  251. /** @defgroup ADC ADC
  252. * @brief ADC HAL module driver
  253. * @{
  254. */
  255. #ifdef HAL_ADC_MODULE_ENABLED
  256. /* Private typedef -----------------------------------------------------------*/
  257. /* Private define ------------------------------------------------------------*/
  258. /** @defgroup ADC_Private_Constants ADC Private Constants
  259. * @{
  260. */
  261. #define ADC_CFGR_FIELDS_1 ((ADC_CFGR_RES | ADC_CFGR_ALIGN |\
  262. ADC_CFGR_CONT | ADC_CFGR_OVRMOD |\
  263. ADC_CFGR_DISCEN | ADC_CFGR_DISCNUM |\
  264. ADC_CFGR_EXTEN | ADC_CFGR_EXTSEL)) /*!< ADC_CFGR fields of parameters that can be updated
  265. when no regular conversion is on-going */
  266. /* Timeout values for ADC operations (enable settling time, */
  267. /* disable settling time, ...). */
  268. /* Values defined to be higher than worst cases: low clock frequency, */
  269. /* maximum prescalers. */
  270. #define ADC_ENABLE_TIMEOUT (2UL) /*!< ADC enable time-out value */
  271. #define ADC_DISABLE_TIMEOUT (2UL) /*!< ADC disable time-out value */
  272. /* Timeout to wait for current conversion on going to be completed. */
  273. /* Timeout fixed to longest ADC conversion possible, for 1 channel: */
  274. /* - maximum sampling time (640.5 adc_clk) */
  275. /* - ADC resolution (Tsar 12 bits= 12.5 adc_clk) */
  276. /* - System clock / ADC clock <= 4096 (hypothesis of maximum clock ratio) */
  277. /* - ADC oversampling ratio 256 */
  278. /* Calculation: 653 * 4096 * 256 CPU clock cycles max */
  279. /* Unit: cycles of CPU clock. */
  280. #define ADC_CONVERSION_TIME_MAX_CPU_CYCLES (653UL * 4096UL * 256UL) /*!< ADC conversion completion time-out value */
  281. /**
  282. * @}
  283. */
  284. /* Private macro -------------------------------------------------------------*/
  285. /* Private variables ---------------------------------------------------------*/
  286. /* Private function prototypes -----------------------------------------------*/
  287. /* Exported functions --------------------------------------------------------*/
  288. /** @defgroup ADC_Exported_Functions ADC Exported Functions
  289. * @{
  290. */
  291. /** @defgroup ADC_Exported_Functions_Group1 Initialization and de-initialization functions
  292. * @brief ADC Initialization and Configuration functions
  293. *
  294. @verbatim
  295. ===============================================================================
  296. ##### Initialization and de-initialization functions #####
  297. ===============================================================================
  298. [..] This section provides functions allowing to:
  299. (+) Initialize and configure the ADC.
  300. (+) De-initialize the ADC.
  301. @endverbatim
  302. * @{
  303. */
  304. /**
  305. * @brief Initialize the ADC peripheral and regular group according to
  306. * parameters specified in structure "ADC_InitTypeDef".
  307. * @note As prerequisite, ADC clock must be configured at RCC top level
  308. * (refer to description of RCC configuration for ADC
  309. * in header of this file).
  310. * @note Possibility to update parameters on the fly:
  311. * This function initializes the ADC MSP (HAL_ADC_MspInit()) only when
  312. * coming from ADC state reset. Following calls to this function can
  313. * be used to reconfigure some parameters of ADC_InitTypeDef
  314. * structure on the fly, without modifying MSP configuration. If ADC
  315. * MSP has to be modified again, HAL_ADC_DeInit() must be called
  316. * before HAL_ADC_Init().
  317. * The setting of these parameters is conditioned to ADC state.
  318. * For parameters constraints, see comments of structure
  319. * "ADC_InitTypeDef".
  320. * @note This function configures the ADC within 2 scopes: scope of entire
  321. * ADC and scope of regular group. For parameters details, see comments
  322. * of structure "ADC_InitTypeDef".
  323. * @param hadc ADC handle
  324. * @retval HAL status
  325. */
  326. HAL_StatusTypeDef HAL_ADC_Init(ADC_HandleTypeDef *hadc)
  327. {
  328. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  329. uint32_t tmpCFGR;
  330. uint32_t tmp_adc_reg_is_conversion_on_going;
  331. __IO uint32_t wait_loop_index = 0UL;
  332. uint32_t tmp_adc_is_conversion_on_going_regular;
  333. uint32_t tmp_adc_is_conversion_on_going_injected;
  334. /* Check ADC handle */
  335. if (hadc == NULL)
  336. {
  337. return HAL_ERROR;
  338. }
  339. /* Check the parameters */
  340. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  341. assert_param(IS_ADC_CLOCKPRESCALER(hadc->Init.ClockPrescaler));
  342. assert_param(IS_ADC_RESOLUTION(hadc->Init.Resolution));
  343. assert_param(IS_ADC_DATA_ALIGN(hadc->Init.DataAlign));
  344. assert_param(IS_ADC_SCAN_MODE(hadc->Init.ScanConvMode));
  345. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
  346. assert_param(IS_ADC_EXTTRIG_EDGE(hadc->Init.ExternalTrigConvEdge));
  347. assert_param(IS_ADC_EXTTRIG(hadc, hadc->Init.ExternalTrigConv));
  348. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DMAContinuousRequests));
  349. assert_param(IS_ADC_EOC_SELECTION(hadc->Init.EOCSelection));
  350. assert_param(IS_ADC_OVERRUN(hadc->Init.Overrun));
  351. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.LowPowerAutoWait));
  352. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.OversamplingMode));
  353. if (hadc->Init.ScanConvMode != ADC_SCAN_DISABLE)
  354. {
  355. assert_param(IS_ADC_REGULAR_NB_CONV(hadc->Init.NbrOfConversion));
  356. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DiscontinuousConvMode));
  357. if (hadc->Init.DiscontinuousConvMode == ENABLE)
  358. {
  359. assert_param(IS_ADC_REGULAR_DISCONT_NUMBER(hadc->Init.NbrOfDiscConversion));
  360. }
  361. }
  362. /* DISCEN and CONT bits cannot be set at the same time */
  363. assert_param(!((hadc->Init.DiscontinuousConvMode == ENABLE) && (hadc->Init.ContinuousConvMode == ENABLE)));
  364. /* Actions performed only if ADC is coming from state reset: */
  365. /* - Initialization of ADC MSP */
  366. if (hadc->State == HAL_ADC_STATE_RESET)
  367. {
  368. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  369. /* Init the ADC Callback settings */
  370. hadc->ConvCpltCallback = HAL_ADC_ConvCpltCallback; /* Legacy weak callback */
  371. hadc->ConvHalfCpltCallback = HAL_ADC_ConvHalfCpltCallback; /* Legacy weak callback */
  372. hadc->LevelOutOfWindowCallback = HAL_ADC_LevelOutOfWindowCallback; /* Legacy weak callback */
  373. hadc->ErrorCallback = HAL_ADC_ErrorCallback; /* Legacy weak callback */
  374. hadc->InjectedConvCpltCallback = HAL_ADCEx_InjectedConvCpltCallback; /* Legacy weak callback */
  375. hadc->InjectedQueueOverflowCallback = HAL_ADCEx_InjectedQueueOverflowCallback; /* Legacy weak callback */
  376. hadc->LevelOutOfWindow2Callback = HAL_ADCEx_LevelOutOfWindow2Callback; /* Legacy weak callback */
  377. hadc->LevelOutOfWindow3Callback = HAL_ADCEx_LevelOutOfWindow3Callback; /* Legacy weak callback */
  378. hadc->EndOfSamplingCallback = HAL_ADCEx_EndOfSamplingCallback; /* 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. /* Set ADC error code to none */
  390. ADC_CLEAR_ERRORCODE(hadc);
  391. /* Initialize Lock */
  392. hadc->Lock = HAL_UNLOCKED;
  393. }
  394. /* - Exit from deep-power-down mode and ADC voltage regulator enable */
  395. if (LL_ADC_IsDeepPowerDownEnabled(hadc->Instance) != 0UL)
  396. {
  397. /* Disable ADC deep power down mode */
  398. LL_ADC_DisableDeepPowerDown(hadc->Instance);
  399. /* System was in deep power down mode, calibration must
  400. be relaunched or a previously saved calibration factor
  401. re-applied once the ADC voltage regulator is enabled */
  402. }
  403. if (LL_ADC_IsInternalRegulatorEnabled(hadc->Instance) == 0UL)
  404. {
  405. /* Enable ADC internal voltage regulator */
  406. LL_ADC_EnableInternalRegulator(hadc->Instance);
  407. /* Note: Variable divided by 2 to compensate partially */
  408. /* CPU processing cycles, scaling in us split to not */
  409. /* exceed 32 bits register capacity and handle low frequency. */
  410. wait_loop_index = ((LL_ADC_DELAY_INTERNAL_REGUL_STAB_US / 10UL) * (SystemCoreClock / (100000UL * 2UL)));
  411. while (wait_loop_index != 0UL)
  412. {
  413. wait_loop_index--;
  414. }
  415. }
  416. /* Verification that ADC voltage regulator is correctly enabled, whether */
  417. /* or not ADC is coming from state reset (if any potential problem of */
  418. /* clocking, voltage regulator would not be enabled). */
  419. if (LL_ADC_IsInternalRegulatorEnabled(hadc->Instance) == 0UL)
  420. {
  421. /* Update ADC state machine to error */
  422. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  423. /* Set ADC error code to ADC peripheral internal error */
  424. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  425. tmp_hal_status = HAL_ERROR;
  426. }
  427. /* Configuration of ADC parameters if previous preliminary actions are */
  428. /* correctly completed and if there is no conversion on going on regular */
  429. /* group (ADC may already be enabled at this point if HAL_ADC_Init() is */
  430. /* called to update a parameter on the fly). */
  431. tmp_adc_reg_is_conversion_on_going = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
  432. if (((hadc->State & HAL_ADC_STATE_ERROR_INTERNAL) == 0UL)
  433. && (tmp_adc_reg_is_conversion_on_going == 0UL)
  434. )
  435. {
  436. /* Set ADC state */
  437. ADC_STATE_CLR_SET(hadc->State,
  438. HAL_ADC_STATE_REG_BUSY,
  439. HAL_ADC_STATE_BUSY_INTERNAL);
  440. /* Configuration of common ADC parameters */
  441. /* Parameters update conditioned to ADC state: */
  442. /* Parameters that can be updated only when ADC is disabled: */
  443. /* - clock configuration */
  444. if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
  445. {
  446. if (__LL_ADC_IS_ENABLED_ALL_COMMON_INSTANCE(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) == 0UL)
  447. {
  448. /* Reset configuration of ADC common register CCR: */
  449. /* */
  450. /* - ADC clock mode and ACC prescaler (CKMODE and PRESC bits)are set */
  451. /* according to adc->Init.ClockPrescaler. It selects the clock */
  452. /* source and sets the clock division factor. */
  453. /* */
  454. /* Some parameters of this register are not reset, since they are set */
  455. /* by other functions and must be kept in case of usage of this */
  456. /* function on the fly (update of a parameter of ADC_InitTypeDef */
  457. /* without needing to reconfigure all other ADC groups/channels */
  458. /* parameters): */
  459. /* - when multimode feature is available, multimode-related */
  460. /* parameters: MDMA, DMACFG, DELAY, DUAL (set by API */
  461. /* HAL_ADCEx_MultiModeConfigChannel() ) */
  462. /* - internal measurement paths: Vbat, temperature sensor, Vref */
  463. /* (set into HAL_ADC_ConfigChannel() or */
  464. /* HAL_ADCEx_InjectedConfigChannel() ) */
  465. LL_ADC_SetCommonClock(__LL_ADC_COMMON_INSTANCE(hadc->Instance), hadc->Init.ClockPrescaler);
  466. }
  467. }
  468. /* Configuration of ADC: */
  469. /* - resolution Init.Resolution */
  470. /* - data alignment Init.DataAlign */
  471. /* - external trigger to start conversion Init.ExternalTrigConv */
  472. /* - external trigger polarity Init.ExternalTrigConvEdge */
  473. /* - continuous conversion mode Init.ContinuousConvMode */
  474. /* - overrun Init.Overrun */
  475. /* - discontinuous mode Init.DiscontinuousConvMode */
  476. /* - discontinuous mode channel count Init.NbrOfDiscConversion */
  477. tmpCFGR = (ADC_CFGR_CONTINUOUS((uint32_t)hadc->Init.ContinuousConvMode) |
  478. hadc->Init.Overrun |
  479. hadc->Init.DataAlign |
  480. hadc->Init.Resolution |
  481. ADC_CFGR_REG_DISCONTINUOUS((uint32_t)hadc->Init.DiscontinuousConvMode));
  482. if (hadc->Init.DiscontinuousConvMode == ENABLE)
  483. {
  484. tmpCFGR |= ADC_CFGR_DISCONTINUOUS_NUM(hadc->Init.NbrOfDiscConversion);
  485. }
  486. /* Enable external trigger if trigger selection is different of software */
  487. /* start. */
  488. /* Note: This configuration keeps the hardware feature of parameter */
  489. /* ExternalTrigConvEdge "trigger edge none" equivalent to */
  490. /* software start. */
  491. if (hadc->Init.ExternalTrigConv != ADC_SOFTWARE_START)
  492. {
  493. tmpCFGR |= ((hadc->Init.ExternalTrigConv & ADC_CFGR_EXTSEL)
  494. | hadc->Init.ExternalTrigConvEdge
  495. );
  496. }
  497. /* Update Configuration Register CFGR */
  498. MODIFY_REG(hadc->Instance->CFGR, ADC_CFGR_FIELDS_1, tmpCFGR);
  499. /* Parameters update conditioned to ADC state: */
  500. /* Parameters that can be updated when ADC is disabled or enabled without */
  501. /* conversion on going on regular and injected groups: */
  502. /* - DMA continuous request Init.DMAContinuousRequests */
  503. /* - LowPowerAutoWait feature Init.LowPowerAutoWait */
  504. /* - Oversampling parameters Init.Oversampling */
  505. tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
  506. tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
  507. if ((tmp_adc_is_conversion_on_going_regular == 0UL)
  508. && (tmp_adc_is_conversion_on_going_injected == 0UL)
  509. )
  510. {
  511. tmpCFGR = (ADC_CFGR_DFSDM(hadc) |
  512. ADC_CFGR_AUTOWAIT((uint32_t)hadc->Init.LowPowerAutoWait) |
  513. ADC_CFGR_DMACONTREQ((uint32_t)hadc->Init.DMAContinuousRequests));
  514. MODIFY_REG(hadc->Instance->CFGR, ADC_CFGR_FIELDS_2, tmpCFGR);
  515. if (hadc->Init.OversamplingMode == ENABLE)
  516. {
  517. assert_param(IS_ADC_OVERSAMPLING_RATIO(hadc->Init.Oversampling.Ratio));
  518. assert_param(IS_ADC_RIGHT_BIT_SHIFT(hadc->Init.Oversampling.RightBitShift));
  519. assert_param(IS_ADC_TRIGGERED_OVERSAMPLING_MODE(hadc->Init.Oversampling.TriggeredMode));
  520. assert_param(IS_ADC_REGOVERSAMPLING_MODE(hadc->Init.Oversampling.OversamplingStopReset));
  521. /* Configuration of Oversampler: */
  522. /* - Oversampling Ratio */
  523. /* - Right bit shift */
  524. /* - Triggered mode */
  525. /* - Oversampling mode (continued/resumed) */
  526. MODIFY_REG(hadc->Instance->CFGR2,
  527. ADC_CFGR2_OVSR |
  528. ADC_CFGR2_OVSS |
  529. ADC_CFGR2_TROVS |
  530. ADC_CFGR2_ROVSM,
  531. ADC_CFGR2_ROVSE |
  532. hadc->Init.Oversampling.Ratio |
  533. hadc->Init.Oversampling.RightBitShift |
  534. hadc->Init.Oversampling.TriggeredMode |
  535. hadc->Init.Oversampling.OversamplingStopReset
  536. );
  537. }
  538. else
  539. {
  540. /* Disable ADC oversampling scope on ADC group regular */
  541. CLEAR_BIT(hadc->Instance->CFGR2, ADC_CFGR2_ROVSE);
  542. }
  543. }
  544. /* Configuration of regular group sequencer: */
  545. /* - if scan mode is disabled, regular channels sequence length is set to */
  546. /* 0x00: 1 channel converted (channel on regular rank 1) */
  547. /* Parameter "NbrOfConversion" is discarded. */
  548. /* Note: Scan mode is not present by hardware on this device, but */
  549. /* emulated by software for alignment over all STM32 devices. */
  550. /* - if scan mode is enabled, regular channels sequence length is set to */
  551. /* parameter "NbrOfConversion". */
  552. if (hadc->Init.ScanConvMode == ADC_SCAN_ENABLE)
  553. {
  554. /* Set number of ranks in regular group sequencer */
  555. MODIFY_REG(hadc->Instance->SQR1, ADC_SQR1_L, (hadc->Init.NbrOfConversion - (uint8_t)1));
  556. }
  557. else
  558. {
  559. CLEAR_BIT(hadc->Instance->SQR1, ADC_SQR1_L);
  560. }
  561. /* Initialize the ADC state */
  562. /* Clear HAL_ADC_STATE_BUSY_INTERNAL bit, set HAL_ADC_STATE_READY bit */
  563. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_BUSY_INTERNAL, HAL_ADC_STATE_READY);
  564. }
  565. else
  566. {
  567. /* Update ADC state machine to error */
  568. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  569. tmp_hal_status = HAL_ERROR;
  570. }
  571. /* Return function status */
  572. return tmp_hal_status;
  573. }
  574. /**
  575. * @brief Deinitialize the ADC peripheral registers to their default reset
  576. * values, with deinitialization of the ADC MSP.
  577. * @note For devices with several ADCs: reset of ADC common registers is done
  578. * only if all ADCs sharing the same common group are disabled.
  579. * (function "HAL_ADC_MspDeInit()" is also called under the same conditions:
  580. * all ADC instances use the same core clock at RCC level, disabling
  581. * the core clock reset all ADC instances).
  582. * If this is not the case, reset of these common parameters reset is
  583. * bypassed without error reporting: it can be the intended behavior in
  584. * case of reset of a single ADC while the other ADCs sharing the same
  585. * common group is still running.
  586. * @note By default, HAL_ADC_DeInit() set ADC in mode deep power-down:
  587. * this saves more power by reducing leakage currents
  588. * and is particularly interesting before entering MCU low-power modes.
  589. * @param hadc ADC handle
  590. * @retval HAL status
  591. */
  592. HAL_StatusTypeDef HAL_ADC_DeInit(ADC_HandleTypeDef *hadc)
  593. {
  594. HAL_StatusTypeDef tmp_hal_status;
  595. /* Check ADC handle */
  596. if (hadc == NULL)
  597. {
  598. return HAL_ERROR;
  599. }
  600. /* Check the parameters */
  601. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  602. /* Set ADC state */
  603. SET_BIT(hadc->State, HAL_ADC_STATE_BUSY_INTERNAL);
  604. /* Stop potential conversion on going */
  605. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_INJECTED_GROUP);
  606. /* Disable ADC peripheral if conversions are effectively stopped */
  607. /* Flush register JSQR: reset the queue sequencer when injected */
  608. /* queue sequencer is enabled and ADC disabled. */
  609. /* The software and hardware triggers of the injected sequence are both */
  610. /* internally disabled just after the completion of the last valid */
  611. /* injected sequence. */
  612. SET_BIT(hadc->Instance->CFGR, ADC_CFGR_JQM);
  613. /* Disable ADC peripheral if conversions are effectively stopped */
  614. if (tmp_hal_status == HAL_OK)
  615. {
  616. /* Disable the ADC peripheral */
  617. tmp_hal_status = ADC_Disable(hadc);
  618. /* Check if ADC is effectively disabled */
  619. if (tmp_hal_status == HAL_OK)
  620. {
  621. /* Change ADC state */
  622. hadc->State = HAL_ADC_STATE_READY;
  623. }
  624. }
  625. /* Note: HAL ADC deInit is done independently of ADC conversion stop */
  626. /* and disable return status. In case of status fail, attempt to */
  627. /* perform deinitialization anyway and it is up user code in */
  628. /* in HAL_ADC_MspDeInit() to reset the ADC peripheral using */
  629. /* system RCC hard reset. */
  630. /* ========== Reset ADC registers ========== */
  631. /* Reset register IER */
  632. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_AWD3 | ADC_IT_AWD2 | ADC_IT_AWD1 |
  633. ADC_IT_JQOVF | ADC_IT_OVR |
  634. ADC_IT_JEOS | ADC_IT_JEOC |
  635. ADC_IT_EOS | ADC_IT_EOC |
  636. ADC_IT_EOSMP | ADC_IT_RDY));
  637. /* Reset register ISR */
  638. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_AWD3 | ADC_FLAG_AWD2 | ADC_FLAG_AWD1 |
  639. ADC_FLAG_JQOVF | ADC_FLAG_OVR |
  640. ADC_FLAG_JEOS | ADC_FLAG_JEOC |
  641. ADC_FLAG_EOS | ADC_FLAG_EOC |
  642. ADC_FLAG_EOSMP | ADC_FLAG_RDY));
  643. /* Reset register CR */
  644. /* Bits ADC_CR_JADSTP, ADC_CR_ADSTP, ADC_CR_JADSTART, ADC_CR_ADSTART,
  645. ADC_CR_ADCAL, ADC_CR_ADDIS and ADC_CR_ADEN are in access mode "read-set":
  646. no direct reset applicable.
  647. Update CR register to reset value where doable by software */
  648. CLEAR_BIT(hadc->Instance->CR, ADC_CR_ADVREGEN | ADC_CR_ADCALDIF);
  649. SET_BIT(hadc->Instance->CR, ADC_CR_DEEPPWD);
  650. /* Reset register CFGR */
  651. CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_FIELDS);
  652. SET_BIT(hadc->Instance->CFGR, ADC_CFGR_JQDIS);
  653. /* Reset register CFGR2 */
  654. CLEAR_BIT(hadc->Instance->CFGR2, ADC_CFGR2_ROVSM | ADC_CFGR2_TROVS | ADC_CFGR2_OVSS |
  655. ADC_CFGR2_OVSR | ADC_CFGR2_JOVSE | ADC_CFGR2_ROVSE);
  656. /* Reset register SMPR1 */
  657. CLEAR_BIT(hadc->Instance->SMPR1, ADC_SMPR1_FIELDS);
  658. /* Reset register SMPR2 */
  659. CLEAR_BIT(hadc->Instance->SMPR2, ADC_SMPR2_SMP18 | ADC_SMPR2_SMP17 | ADC_SMPR2_SMP16 |
  660. ADC_SMPR2_SMP15 | ADC_SMPR2_SMP14 | ADC_SMPR2_SMP13 |
  661. ADC_SMPR2_SMP12 | ADC_SMPR2_SMP11 | ADC_SMPR2_SMP10);
  662. /* Reset register TR1 */
  663. CLEAR_BIT(hadc->Instance->TR1, ADC_TR1_HT1 | ADC_TR1_LT1);
  664. /* Reset register TR2 */
  665. CLEAR_BIT(hadc->Instance->TR2, ADC_TR2_HT2 | ADC_TR2_LT2);
  666. /* Reset register TR3 */
  667. CLEAR_BIT(hadc->Instance->TR3, ADC_TR3_HT3 | ADC_TR3_LT3);
  668. /* Reset register SQR1 */
  669. CLEAR_BIT(hadc->Instance->SQR1, ADC_SQR1_SQ4 | ADC_SQR1_SQ3 | ADC_SQR1_SQ2 |
  670. ADC_SQR1_SQ1 | ADC_SQR1_L);
  671. /* Reset register SQR2 */
  672. CLEAR_BIT(hadc->Instance->SQR2, ADC_SQR2_SQ9 | ADC_SQR2_SQ8 | ADC_SQR2_SQ7 |
  673. ADC_SQR2_SQ6 | ADC_SQR2_SQ5);
  674. /* Reset register SQR3 */
  675. CLEAR_BIT(hadc->Instance->SQR3, ADC_SQR3_SQ14 | ADC_SQR3_SQ13 | ADC_SQR3_SQ12 |
  676. ADC_SQR3_SQ11 | ADC_SQR3_SQ10);
  677. /* Reset register SQR4 */
  678. CLEAR_BIT(hadc->Instance->SQR4, ADC_SQR4_SQ16 | ADC_SQR4_SQ15);
  679. /* Register JSQR was reset when the ADC was disabled */
  680. /* Reset register DR */
  681. /* bits in access mode read only, no direct reset applicable*/
  682. /* Reset register OFR1 */
  683. CLEAR_BIT(hadc->Instance->OFR1, ADC_OFR1_OFFSET1_EN | ADC_OFR1_OFFSET1_CH | ADC_OFR1_OFFSET1);
  684. /* Reset register OFR2 */
  685. CLEAR_BIT(hadc->Instance->OFR2, ADC_OFR2_OFFSET2_EN | ADC_OFR2_OFFSET2_CH | ADC_OFR2_OFFSET2);
  686. /* Reset register OFR3 */
  687. CLEAR_BIT(hadc->Instance->OFR3, ADC_OFR3_OFFSET3_EN | ADC_OFR3_OFFSET3_CH | ADC_OFR3_OFFSET3);
  688. /* Reset register OFR4 */
  689. CLEAR_BIT(hadc->Instance->OFR4, ADC_OFR4_OFFSET4_EN | ADC_OFR4_OFFSET4_CH | ADC_OFR4_OFFSET4);
  690. /* Reset registers JDR1, JDR2, JDR3, JDR4 */
  691. /* bits in access mode read only, no direct reset applicable*/
  692. /* Reset register AWD2CR */
  693. CLEAR_BIT(hadc->Instance->AWD2CR, ADC_AWD2CR_AWD2CH);
  694. /* Reset register AWD3CR */
  695. CLEAR_BIT(hadc->Instance->AWD3CR, ADC_AWD3CR_AWD3CH);
  696. /* Reset register DIFSEL */
  697. CLEAR_BIT(hadc->Instance->DIFSEL, ADC_DIFSEL_DIFSEL);
  698. /* Reset register CALFACT */
  699. CLEAR_BIT(hadc->Instance->CALFACT, ADC_CALFACT_CALFACT_D | ADC_CALFACT_CALFACT_S);
  700. /* ========== Reset common ADC registers ========== */
  701. /* Software is allowed to change common parameters only when all the other
  702. ADCs are disabled. */
  703. if (__LL_ADC_IS_ENABLED_ALL_COMMON_INSTANCE(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) == 0UL)
  704. {
  705. /* Reset configuration of ADC common register CCR:
  706. - clock mode: CKMODE, PRESCEN
  707. - multimode related parameters (when this feature is available): MDMA,
  708. DMACFG, DELAY, DUAL (set by HAL_ADCEx_MultiModeConfigChannel() API)
  709. - internal measurement paths: Vbat, temperature sensor, Vref (set into
  710. HAL_ADC_ConfigChannel() or HAL_ADCEx_InjectedConfigChannel() )
  711. */
  712. ADC_CLEAR_COMMON_CONTROL_REGISTER(hadc);
  713. }
  714. /* DeInit the low level hardware.
  715. For example:
  716. __HAL_RCC_ADC_FORCE_RESET();
  717. __HAL_RCC_ADC_RELEASE_RESET();
  718. __HAL_RCC_ADC_CLK_DISABLE();
  719. Keep in mind that all ADCs use the same clock: disabling
  720. the clock will reset all ADCs.
  721. */
  722. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  723. if (hadc->MspDeInitCallback == NULL)
  724. {
  725. hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit */
  726. }
  727. /* DeInit the low level hardware: RCC clock, NVIC */
  728. hadc->MspDeInitCallback(hadc);
  729. #else
  730. /* DeInit the low level hardware: RCC clock, NVIC */
  731. HAL_ADC_MspDeInit(hadc);
  732. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  733. /* Set ADC error code to none */
  734. ADC_CLEAR_ERRORCODE(hadc);
  735. /* Reset injected channel configuration parameters */
  736. hadc->InjectionConfig.ContextQueue = 0;
  737. hadc->InjectionConfig.ChannelCount = 0;
  738. /* Set ADC state */
  739. hadc->State = HAL_ADC_STATE_RESET;
  740. /* Process unlocked */
  741. __HAL_UNLOCK(hadc);
  742. /* Return function status */
  743. return tmp_hal_status;
  744. }
  745. /**
  746. * @brief Initialize the ADC MSP.
  747. * @param hadc ADC handle
  748. * @retval None
  749. */
  750. __weak void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc)
  751. {
  752. /* Prevent unused argument(s) compilation warning */
  753. UNUSED(hadc);
  754. /* NOTE : This function should not be modified. When the callback is needed,
  755. function HAL_ADC_MspInit must be implemented in the user file.
  756. */
  757. }
  758. /**
  759. * @brief DeInitialize the ADC MSP.
  760. * @param hadc ADC handle
  761. * @note All ADC instances use the same core clock at RCC level, disabling
  762. * the core clock reset all ADC instances).
  763. * @retval None
  764. */
  765. __weak void HAL_ADC_MspDeInit(ADC_HandleTypeDef *hadc)
  766. {
  767. /* Prevent unused argument(s) compilation warning */
  768. UNUSED(hadc);
  769. /* NOTE : This function should not be modified. When the callback is needed,
  770. function HAL_ADC_MspDeInit must be implemented in the user file.
  771. */
  772. }
  773. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  774. /**
  775. * @brief Register a User ADC Callback
  776. * To be used instead of the weak predefined callback
  777. * @param hadc Pointer to a ADC_HandleTypeDef structure that contains
  778. * the configuration information for the specified ADC.
  779. * @param CallbackID ID of the callback to be registered
  780. * This parameter can be one of the following values:
  781. * @arg @ref HAL_ADC_CONVERSION_COMPLETE_CB_ID ADC conversion complete callback ID
  782. * @arg @ref HAL_ADC_CONVERSION_HALF_CB_ID ADC conversion DMA half-transfer callback ID
  783. * @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID ADC analog watchdog 1 callback ID
  784. * @arg @ref HAL_ADC_ERROR_CB_ID ADC error callback ID
  785. * @arg @ref HAL_ADC_INJ_CONVERSION_COMPLETE_CB_ID ADC group injected conversion complete callback ID
  786. * @arg @ref HAL_ADC_INJ_QUEUE_OVEFLOW_CB_ID ADC group injected context queue overflow callback ID
  787. * @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_2_CB_ID ADC analog watchdog 2 callback ID
  788. * @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_3_CB_ID ADC analog watchdog 3 callback ID
  789. * @arg @ref HAL_ADC_END_OF_SAMPLING_CB_ID ADC end of sampling callback ID
  790. * @arg @ref HAL_ADC_MSPINIT_CB_ID ADC Msp Init callback ID
  791. * @arg @ref HAL_ADC_MSPDEINIT_CB_ID ADC Msp DeInit callback ID
  792. * @arg @ref HAL_ADC_MSPINIT_CB_ID MspInit callback ID
  793. * @arg @ref HAL_ADC_MSPDEINIT_CB_ID MspDeInit callback ID
  794. * @param pCallback pointer to the Callback function
  795. * @retval HAL status
  796. */
  797. HAL_StatusTypeDef HAL_ADC_RegisterCallback(ADC_HandleTypeDef *hadc, HAL_ADC_CallbackIDTypeDef CallbackID, pADC_CallbackTypeDef pCallback)
  798. {
  799. HAL_StatusTypeDef status = HAL_OK;
  800. if (pCallback == NULL)
  801. {
  802. /* Update the error code */
  803. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  804. return HAL_ERROR;
  805. }
  806. if ((hadc->State & HAL_ADC_STATE_READY) != 0UL)
  807. {
  808. switch (CallbackID)
  809. {
  810. case HAL_ADC_CONVERSION_COMPLETE_CB_ID :
  811. hadc->ConvCpltCallback = pCallback;
  812. break;
  813. case HAL_ADC_CONVERSION_HALF_CB_ID :
  814. hadc->ConvHalfCpltCallback = pCallback;
  815. break;
  816. case HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID :
  817. hadc->LevelOutOfWindowCallback = pCallback;
  818. break;
  819. case HAL_ADC_ERROR_CB_ID :
  820. hadc->ErrorCallback = pCallback;
  821. break;
  822. case HAL_ADC_INJ_CONVERSION_COMPLETE_CB_ID :
  823. hadc->InjectedConvCpltCallback = pCallback;
  824. break;
  825. case HAL_ADC_INJ_QUEUE_OVEFLOW_CB_ID :
  826. hadc->InjectedQueueOverflowCallback = pCallback;
  827. break;
  828. case HAL_ADC_LEVEL_OUT_OF_WINDOW_2_CB_ID :
  829. hadc->LevelOutOfWindow2Callback = pCallback;
  830. break;
  831. case HAL_ADC_LEVEL_OUT_OF_WINDOW_3_CB_ID :
  832. hadc->LevelOutOfWindow3Callback = pCallback;
  833. break;
  834. case HAL_ADC_END_OF_SAMPLING_CB_ID :
  835. hadc->EndOfSamplingCallback = pCallback;
  836. break;
  837. case HAL_ADC_MSPINIT_CB_ID :
  838. hadc->MspInitCallback = pCallback;
  839. break;
  840. case HAL_ADC_MSPDEINIT_CB_ID :
  841. hadc->MspDeInitCallback = pCallback;
  842. break;
  843. default :
  844. /* Update the error code */
  845. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  846. /* Return error status */
  847. status = HAL_ERROR;
  848. break;
  849. }
  850. }
  851. else if (HAL_ADC_STATE_RESET == hadc->State)
  852. {
  853. switch (CallbackID)
  854. {
  855. case HAL_ADC_MSPINIT_CB_ID :
  856. hadc->MspInitCallback = pCallback;
  857. break;
  858. case HAL_ADC_MSPDEINIT_CB_ID :
  859. hadc->MspDeInitCallback = pCallback;
  860. break;
  861. default :
  862. /* Update the error code */
  863. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  864. /* Return error status */
  865. status = HAL_ERROR;
  866. break;
  867. }
  868. }
  869. else
  870. {
  871. /* Update the error code */
  872. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  873. /* Return error status */
  874. status = HAL_ERROR;
  875. }
  876. return status;
  877. }
  878. /**
  879. * @brief Unregister a ADC Callback
  880. * ADC callback is redirected to the weak predefined callback
  881. * @param hadc Pointer to a ADC_HandleTypeDef structure that contains
  882. * the configuration information for the specified ADC.
  883. * @param CallbackID ID of the callback to be unregistered
  884. * This parameter can be one of the following values:
  885. * @arg @ref HAL_ADC_CONVERSION_COMPLETE_CB_ID ADC conversion complete callback ID
  886. * @arg @ref HAL_ADC_CONVERSION_HALF_CB_ID ADC conversion DMA half-transfer callback ID
  887. * @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID ADC analog watchdog 1 callback ID
  888. * @arg @ref HAL_ADC_ERROR_CB_ID ADC error callback ID
  889. * @arg @ref HAL_ADC_INJ_CONVERSION_COMPLETE_CB_ID ADC group injected conversion complete callback ID
  890. * @arg @ref HAL_ADC_INJ_QUEUE_OVEFLOW_CB_ID ADC group injected context queue overflow callback ID
  891. * @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_2_CB_ID ADC analog watchdog 2 callback ID
  892. * @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_3_CB_ID ADC analog watchdog 3 callback ID
  893. * @arg @ref HAL_ADC_END_OF_SAMPLING_CB_ID ADC end of sampling callback ID
  894. * @arg @ref HAL_ADC_MSPINIT_CB_ID ADC Msp Init callback ID
  895. * @arg @ref HAL_ADC_MSPDEINIT_CB_ID ADC Msp DeInit callback ID
  896. * @arg @ref HAL_ADC_MSPINIT_CB_ID MspInit callback ID
  897. * @arg @ref HAL_ADC_MSPDEINIT_CB_ID MspDeInit callback ID
  898. * @retval HAL status
  899. */
  900. HAL_StatusTypeDef HAL_ADC_UnRegisterCallback(ADC_HandleTypeDef *hadc, HAL_ADC_CallbackIDTypeDef CallbackID)
  901. {
  902. HAL_StatusTypeDef status = HAL_OK;
  903. if ((hadc->State & HAL_ADC_STATE_READY) != 0UL)
  904. {
  905. switch (CallbackID)
  906. {
  907. case HAL_ADC_CONVERSION_COMPLETE_CB_ID :
  908. hadc->ConvCpltCallback = HAL_ADC_ConvCpltCallback;
  909. break;
  910. case HAL_ADC_CONVERSION_HALF_CB_ID :
  911. hadc->ConvHalfCpltCallback = HAL_ADC_ConvHalfCpltCallback;
  912. break;
  913. case HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID :
  914. hadc->LevelOutOfWindowCallback = HAL_ADC_LevelOutOfWindowCallback;
  915. break;
  916. case HAL_ADC_ERROR_CB_ID :
  917. hadc->ErrorCallback = HAL_ADC_ErrorCallback;
  918. break;
  919. case HAL_ADC_INJ_CONVERSION_COMPLETE_CB_ID :
  920. hadc->InjectedConvCpltCallback = HAL_ADCEx_InjectedConvCpltCallback;
  921. break;
  922. case HAL_ADC_INJ_QUEUE_OVEFLOW_CB_ID :
  923. hadc->InjectedQueueOverflowCallback = HAL_ADCEx_InjectedQueueOverflowCallback;
  924. break;
  925. case HAL_ADC_LEVEL_OUT_OF_WINDOW_2_CB_ID :
  926. hadc->LevelOutOfWindow2Callback = HAL_ADCEx_LevelOutOfWindow2Callback;
  927. break;
  928. case HAL_ADC_LEVEL_OUT_OF_WINDOW_3_CB_ID :
  929. hadc->LevelOutOfWindow3Callback = HAL_ADCEx_LevelOutOfWindow3Callback;
  930. break;
  931. case HAL_ADC_END_OF_SAMPLING_CB_ID :
  932. hadc->EndOfSamplingCallback = HAL_ADCEx_EndOfSamplingCallback;
  933. break;
  934. case HAL_ADC_MSPINIT_CB_ID :
  935. hadc->MspInitCallback = HAL_ADC_MspInit; /* Legacy weak MspInit */
  936. break;
  937. case HAL_ADC_MSPDEINIT_CB_ID :
  938. hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit */
  939. break;
  940. default :
  941. /* Update the error code */
  942. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  943. /* Return error status */
  944. status = HAL_ERROR;
  945. break;
  946. }
  947. }
  948. else if (HAL_ADC_STATE_RESET == hadc->State)
  949. {
  950. switch (CallbackID)
  951. {
  952. case HAL_ADC_MSPINIT_CB_ID :
  953. hadc->MspInitCallback = HAL_ADC_MspInit; /* Legacy weak MspInit */
  954. break;
  955. case HAL_ADC_MSPDEINIT_CB_ID :
  956. hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit */
  957. break;
  958. default :
  959. /* Update the error code */
  960. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  961. /* Return error status */
  962. status = HAL_ERROR;
  963. break;
  964. }
  965. }
  966. else
  967. {
  968. /* Update the error code */
  969. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  970. /* Return error status */
  971. status = HAL_ERROR;
  972. }
  973. return status;
  974. }
  975. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  976. /**
  977. * @}
  978. */
  979. /** @defgroup ADC_Exported_Functions_Group2 ADC Input and Output operation functions
  980. * @brief ADC IO operation functions
  981. *
  982. @verbatim
  983. ===============================================================================
  984. ##### IO operation functions #####
  985. ===============================================================================
  986. [..] This section provides functions allowing to:
  987. (+) Start conversion of regular group.
  988. (+) Stop conversion of regular group.
  989. (+) Poll for conversion complete on regular group.
  990. (+) Poll for conversion event.
  991. (+) Get result of regular channel conversion.
  992. (+) Start conversion of regular group and enable interruptions.
  993. (+) Stop conversion of regular group and disable interruptions.
  994. (+) Handle ADC interrupt request
  995. (+) Start conversion of regular group and enable DMA transfer.
  996. (+) Stop conversion of regular group and disable ADC DMA transfer.
  997. @endverbatim
  998. * @{
  999. */
  1000. /**
  1001. * @brief Enable ADC, start conversion of regular group.
  1002. * @note Interruptions enabled in this function: None.
  1003. * @param hadc ADC handle
  1004. * @retval HAL status
  1005. */
  1006. HAL_StatusTypeDef HAL_ADC_Start(ADC_HandleTypeDef *hadc)
  1007. {
  1008. HAL_StatusTypeDef tmp_hal_status;
  1009. /* Check the parameters */
  1010. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1011. /* Perform ADC enable and conversion start if no conversion is on going */
  1012. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
  1013. {
  1014. /* Process locked */
  1015. __HAL_LOCK(hadc);
  1016. /* Enable the ADC peripheral */
  1017. tmp_hal_status = ADC_Enable(hadc);
  1018. /* Start conversion if ADC is effectively enabled */
  1019. if (tmp_hal_status == HAL_OK)
  1020. {
  1021. /* Set ADC state */
  1022. /* - Clear state bitfield related to regular group conversion results */
  1023. /* - Set state bitfield related to regular operation */
  1024. ADC_STATE_CLR_SET(hadc->State,
  1025. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP,
  1026. HAL_ADC_STATE_REG_BUSY);
  1027. /* Set ADC error code */
  1028. /* Check if a conversion is on going on ADC group injected */
  1029. if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  1030. {
  1031. /* Reset ADC error code fields related to regular conversions only */
  1032. CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA));
  1033. }
  1034. else
  1035. {
  1036. /* Reset all ADC error code fields */
  1037. ADC_CLEAR_ERRORCODE(hadc);
  1038. }
  1039. /* Clear ADC group regular conversion flag and overrun flag */
  1040. /* (To ensure of no unknown state from potential previous ADC operations) */
  1041. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
  1042. /* Process unlocked */
  1043. /* Unlock before starting ADC conversions: in case of potential */
  1044. /* interruption, to let the process to ADC IRQ Handler. */
  1045. __HAL_UNLOCK(hadc);
  1046. /* Enable conversion of regular group. */
  1047. /* If software start has been selected, conversion starts immediately. */
  1048. /* If external trigger has been selected, conversion will start at next */
  1049. /* trigger event. */
  1050. /* Start ADC group regular conversion */
  1051. LL_ADC_REG_StartConversion(hadc->Instance);
  1052. }
  1053. else
  1054. {
  1055. /* Process unlocked */
  1056. __HAL_UNLOCK(hadc);
  1057. }
  1058. }
  1059. else
  1060. {
  1061. tmp_hal_status = HAL_BUSY;
  1062. }
  1063. /* Return function status */
  1064. return tmp_hal_status;
  1065. }
  1066. /**
  1067. * @brief Stop ADC conversion of regular group (and injected channels in
  1068. * case of auto_injection mode), disable ADC peripheral.
  1069. * @note: ADC peripheral disable is forcing stop of potential
  1070. * conversion on injected group. If injected group is under use, it
  1071. * should be preliminarily stopped using HAL_ADCEx_InjectedStop function.
  1072. * @param hadc ADC handle
  1073. * @retval HAL status.
  1074. */
  1075. HAL_StatusTypeDef HAL_ADC_Stop(ADC_HandleTypeDef *hadc)
  1076. {
  1077. HAL_StatusTypeDef tmp_hal_status;
  1078. /* Check the parameters */
  1079. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1080. /* Process locked */
  1081. __HAL_LOCK(hadc);
  1082. /* 1. Stop potential conversion on going, on ADC groups regular and injected */
  1083. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_INJECTED_GROUP);
  1084. /* Disable ADC peripheral if conversions are effectively stopped */
  1085. if (tmp_hal_status == HAL_OK)
  1086. {
  1087. /* 2. Disable the ADC peripheral */
  1088. tmp_hal_status = ADC_Disable(hadc);
  1089. /* Check if ADC is effectively disabled */
  1090. if (tmp_hal_status == HAL_OK)
  1091. {
  1092. /* Set ADC state */
  1093. ADC_STATE_CLR_SET(hadc->State,
  1094. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  1095. HAL_ADC_STATE_READY);
  1096. }
  1097. }
  1098. /* Process unlocked */
  1099. __HAL_UNLOCK(hadc);
  1100. /* Return function status */
  1101. return tmp_hal_status;
  1102. }
  1103. /**
  1104. * @brief Wait for regular group conversion to be completed.
  1105. * @note ADC conversion flags EOS (end of sequence) and EOC (end of
  1106. * conversion) are cleared by this function, with an exception:
  1107. * if low power feature "LowPowerAutoWait" is enabled, flags are
  1108. * not cleared to not interfere with this feature until data register
  1109. * is read using function HAL_ADC_GetValue().
  1110. * @note This function cannot be used in a particular setup: ADC configured
  1111. * in DMA mode and polling for end of each conversion (ADC init
  1112. * parameter "EOCSelection" set to ADC_EOC_SINGLE_CONV).
  1113. * In this case, DMA resets the flag EOC and polling cannot be
  1114. * performed on each conversion. Nevertheless, polling can still
  1115. * be performed on the complete sequence (ADC init
  1116. * parameter "EOCSelection" set to ADC_EOC_SEQ_CONV).
  1117. * @param hadc ADC handle
  1118. * @param Timeout Timeout value in millisecond.
  1119. * @retval HAL status
  1120. */
  1121. HAL_StatusTypeDef HAL_ADC_PollForConversion(ADC_HandleTypeDef *hadc, uint32_t Timeout)
  1122. {
  1123. uint32_t tickstart;
  1124. uint32_t tmp_Flag_End;
  1125. /* Check the parameters */
  1126. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1127. /* If end of conversion selected to end of sequence conversions */
  1128. if (hadc->Init.EOCSelection == ADC_EOC_SEQ_CONV)
  1129. {
  1130. tmp_Flag_End = ADC_FLAG_EOS;
  1131. }
  1132. /* If end of conversion selected to end of unitary conversion */
  1133. else /* ADC_EOC_SINGLE_CONV */
  1134. {
  1135. /* Verification that ADC configuration is compliant with polling for */
  1136. /* each conversion: */
  1137. /* Particular case is ADC configured in DMA mode and ADC sequencer with */
  1138. /* several ranks and polling for end of each conversion. */
  1139. /* For code simplicity sake, this particular case is generalized to */
  1140. /* ADC configured in DMA mode and and polling for end of each conversion. */
  1141. if(READ_BIT(hadc->Instance->CFGR, ADC_CFGR_DMAEN) != 0UL)
  1142. {
  1143. /* Update ADC state machine to error */
  1144. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1145. return HAL_ERROR;
  1146. }
  1147. else
  1148. {
  1149. tmp_Flag_End = (ADC_FLAG_EOC);
  1150. }
  1151. }
  1152. /* Get tick count */
  1153. tickstart = HAL_GetTick();
  1154. /* Wait until End of unitary conversion or sequence conversions flag is raised */
  1155. while ((hadc->Instance->ISR & tmp_Flag_End) == 0UL)
  1156. {
  1157. /* Check if timeout is disabled (set to infinite wait) */
  1158. if (Timeout != HAL_MAX_DELAY)
  1159. {
  1160. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0UL))
  1161. {
  1162. /* Update ADC state machine to timeout */
  1163. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  1164. /* Process unlocked */
  1165. __HAL_UNLOCK(hadc);
  1166. return HAL_TIMEOUT;
  1167. }
  1168. }
  1169. }
  1170. /* Update ADC state machine */
  1171. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  1172. /* Determine whether any further conversion upcoming on group regular */
  1173. /* by external trigger, continuous mode or scan sequence on going. */
  1174. if ((LL_ADC_REG_IsTriggerSourceSWStart(hadc->Instance) != 0UL)
  1175. && (hadc->Init.ContinuousConvMode == DISABLE)
  1176. )
  1177. {
  1178. /* Check whether end of sequence is reached */
  1179. if (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOS))
  1180. {
  1181. /* Set ADC state */
  1182. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  1183. if ((hadc->State & HAL_ADC_STATE_INJ_BUSY) == 0UL)
  1184. {
  1185. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  1186. }
  1187. }
  1188. }
  1189. /* Clear polled flag */
  1190. if (tmp_Flag_End == ADC_FLAG_EOS)
  1191. {
  1192. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOS);
  1193. }
  1194. else
  1195. {
  1196. /* Clear end of conversion EOC flag of regular group if low power feature */
  1197. /* "LowPowerAutoWait " is disabled, to not interfere with this feature */
  1198. /* until data register is read using function HAL_ADC_GetValue(). */
  1199. if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_AUTDLY) == 0UL)
  1200. {
  1201. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS));
  1202. }
  1203. }
  1204. /* Return function status */
  1205. return HAL_OK;
  1206. }
  1207. /**
  1208. * @brief Poll for ADC event.
  1209. * @param hadc ADC handle
  1210. * @param EventType the ADC event type.
  1211. * This parameter can be one of the following values:
  1212. * @arg @ref ADC_EOSMP_EVENT ADC End of Sampling event
  1213. * @arg @ref ADC_AWD1_EVENT ADC Analog watchdog 1 event (main analog watchdog, present on all STM32 devices)
  1214. * @arg @ref ADC_AWD2_EVENT ADC Analog watchdog 2 event (additional analog watchdog, not present on all STM32 families)
  1215. * @arg @ref ADC_AWD3_EVENT ADC Analog watchdog 3 event (additional analog watchdog, not present on all STM32 families)
  1216. * @arg @ref ADC_OVR_EVENT ADC Overrun event
  1217. * @arg @ref ADC_JQOVF_EVENT ADC Injected context queue overflow event
  1218. * @param Timeout Timeout value in millisecond.
  1219. * @note The relevant flag is cleared if found to be set, except for ADC_FLAG_OVR.
  1220. * Indeed, the latter is reset only if hadc->Init.Overrun field is set
  1221. * to ADC_OVR_DATA_OVERWRITTEN. Otherwise, data register may be potentially overwritten
  1222. * by a new converted data as soon as OVR is cleared.
  1223. * To reset OVR flag once the preserved data is retrieved, the user can resort
  1224. * to macro __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  1225. * @retval HAL status
  1226. */
  1227. HAL_StatusTypeDef HAL_ADC_PollForEvent(ADC_HandleTypeDef *hadc, uint32_t EventType, uint32_t Timeout)
  1228. {
  1229. uint32_t tickstart;
  1230. /* Check the parameters */
  1231. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1232. assert_param(IS_ADC_EVENT_TYPE(EventType));
  1233. /* Get tick count */
  1234. tickstart = HAL_GetTick();
  1235. /* Check selected event flag */
  1236. while (__HAL_ADC_GET_FLAG(hadc, EventType) == 0UL)
  1237. {
  1238. /* Check if timeout is disabled (set to infinite wait) */
  1239. if (Timeout != HAL_MAX_DELAY)
  1240. {
  1241. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0UL))
  1242. {
  1243. /* Update ADC state machine to timeout */
  1244. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  1245. /* Process unlocked */
  1246. __HAL_UNLOCK(hadc);
  1247. return HAL_TIMEOUT;
  1248. }
  1249. }
  1250. }
  1251. switch (EventType)
  1252. {
  1253. /* End Of Sampling event */
  1254. case ADC_EOSMP_EVENT:
  1255. /* Set ADC state */
  1256. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOSMP);
  1257. /* Clear the End Of Sampling flag */
  1258. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOSMP);
  1259. break;
  1260. /* Analog watchdog (level out of window) event */
  1261. /* Note: In case of several analog watchdog enabled, if needed to know */
  1262. /* which one triggered and on which ADCx, test ADC state of analog watchdog */
  1263. /* flags HAL_ADC_STATE_AWD1/2/3 using function "HAL_ADC_GetState()". */
  1264. /* For example: */
  1265. /* " if ((HAL_ADC_GetState(hadc1) & HAL_ADC_STATE_AWD1) != 0UL) " */
  1266. /* " if ((HAL_ADC_GetState(hadc1) & HAL_ADC_STATE_AWD2) != 0UL) " */
  1267. /* " if ((HAL_ADC_GetState(hadc1) & HAL_ADC_STATE_AWD3) != 0UL) " */
  1268. /* Check analog watchdog 1 flag */
  1269. case ADC_AWD_EVENT:
  1270. /* Set ADC state */
  1271. SET_BIT(hadc->State, HAL_ADC_STATE_AWD1);
  1272. /* Clear ADC analog watchdog flag */
  1273. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD1);
  1274. break;
  1275. /* Check analog watchdog 2 flag */
  1276. case ADC_AWD2_EVENT:
  1277. /* Set ADC state */
  1278. SET_BIT(hadc->State, HAL_ADC_STATE_AWD2);
  1279. /* Clear ADC analog watchdog flag */
  1280. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD2);
  1281. break;
  1282. /* Check analog watchdog 3 flag */
  1283. case ADC_AWD3_EVENT:
  1284. /* Set ADC state */
  1285. SET_BIT(hadc->State, HAL_ADC_STATE_AWD3);
  1286. /* Clear ADC analog watchdog flag */
  1287. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD3);
  1288. break;
  1289. /* Injected context queue overflow event */
  1290. case ADC_JQOVF_EVENT:
  1291. /* Set ADC state */
  1292. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_JQOVF);
  1293. /* Set ADC error code to Injected context queue overflow */
  1294. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_JQOVF);
  1295. /* Clear ADC Injected context queue overflow flag */
  1296. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JQOVF);
  1297. break;
  1298. /* Overrun event */
  1299. default: /* Case ADC_OVR_EVENT */
  1300. /* If overrun is set to overwrite previous data, overrun event is not */
  1301. /* considered as an error. */
  1302. /* (cf ref manual "Managing conversions without using the DMA and without */
  1303. /* overrun ") */
  1304. if (hadc->Init.Overrun == ADC_OVR_DATA_PRESERVED)
  1305. {
  1306. /* Set ADC state */
  1307. SET_BIT(hadc->State, HAL_ADC_STATE_REG_OVR);
  1308. /* Set ADC error code to overrun */
  1309. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_OVR);
  1310. }
  1311. else
  1312. {
  1313. /* Clear ADC Overrun flag only if Overrun is set to ADC_OVR_DATA_OVERWRITTEN
  1314. otherwise, data register is potentially overwritten by new converted data as soon
  1315. as OVR is cleared. */
  1316. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  1317. }
  1318. break;
  1319. }
  1320. /* Return function status */
  1321. return HAL_OK;
  1322. }
  1323. /**
  1324. * @brief Enable ADC, start conversion of regular group with interruption.
  1325. * @note Interruptions enabled in this function according to initialization
  1326. * setting : EOC (end of conversion), EOS (end of sequence),
  1327. * OVR overrun.
  1328. * Each of these interruptions has its dedicated callback function.
  1329. * @note To guarantee a proper reset of all interruptions once all the needed
  1330. * conversions are obtained, HAL_ADC_Stop_IT() must be called to ensure
  1331. * a correct stop of the IT-based conversions.
  1332. * @note By default, HAL_ADC_Start_IT() does not enable the End Of Sampling
  1333. * interruption. If required (e.g. in case of oversampling with trigger
  1334. * mode), the user must:
  1335. * 1. first clear the EOSMP flag if set with macro __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOSMP)
  1336. * 2. then enable the EOSMP interrupt with macro __HAL_ADC_ENABLE_IT(hadc, ADC_IT_EOSMP)
  1337. * before calling HAL_ADC_Start_IT().
  1338. * @param hadc ADC handle
  1339. * @retval HAL status
  1340. */
  1341. HAL_StatusTypeDef HAL_ADC_Start_IT(ADC_HandleTypeDef *hadc)
  1342. {
  1343. HAL_StatusTypeDef tmp_hal_status;
  1344. /* Check the parameters */
  1345. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1346. /* Perform ADC enable and conversion start if no conversion is on going */
  1347. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
  1348. {
  1349. /* Process locked */
  1350. __HAL_LOCK(hadc);
  1351. /* Enable the ADC peripheral */
  1352. tmp_hal_status = ADC_Enable(hadc);
  1353. /* Start conversion if ADC is effectively enabled */
  1354. if (tmp_hal_status == HAL_OK)
  1355. {
  1356. /* Set ADC state */
  1357. /* - Clear state bitfield related to regular group conversion results */
  1358. /* - Set state bitfield related to regular operation */
  1359. ADC_STATE_CLR_SET(hadc->State,
  1360. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP,
  1361. HAL_ADC_STATE_REG_BUSY);
  1362. /* Set ADC error code */
  1363. /* Check if a conversion is on going on ADC group injected */
  1364. if ((hadc->State & HAL_ADC_STATE_INJ_BUSY) != 0UL)
  1365. {
  1366. /* Reset ADC error code fields related to regular conversions only */
  1367. CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA));
  1368. }
  1369. else
  1370. {
  1371. /* Reset all ADC error code fields */
  1372. ADC_CLEAR_ERRORCODE(hadc);
  1373. }
  1374. /* Clear ADC group regular conversion flag and overrun flag */
  1375. /* (To ensure of no unknown state from potential previous ADC operations) */
  1376. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
  1377. /* Process unlocked */
  1378. /* Unlock before starting ADC conversions: in case of potential */
  1379. /* interruption, to let the process to ADC IRQ Handler. */
  1380. __HAL_UNLOCK(hadc);
  1381. /* Disable all interruptions before enabling the desired ones */
  1382. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_EOS | ADC_IT_OVR));
  1383. /* Enable ADC end of conversion interrupt */
  1384. switch (hadc->Init.EOCSelection)
  1385. {
  1386. case ADC_EOC_SEQ_CONV:
  1387. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_EOS);
  1388. break;
  1389. /* case ADC_EOC_SINGLE_CONV */
  1390. default:
  1391. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_EOC);
  1392. break;
  1393. }
  1394. /* Enable ADC overrun interrupt */
  1395. /* If hadc->Init.Overrun is set to ADC_OVR_DATA_PRESERVED, only then is
  1396. ADC_IT_OVR enabled; otherwise data overwrite is considered as normal
  1397. behavior and no CPU time is lost for a non-processed interruption */
  1398. if (hadc->Init.Overrun == ADC_OVR_DATA_PRESERVED)
  1399. {
  1400. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);
  1401. }
  1402. /* Enable conversion of regular group. */
  1403. /* If software start has been selected, conversion starts immediately. */
  1404. /* If external trigger has been selected, conversion will start at next */
  1405. /* trigger event. */
  1406. /* Start ADC group regular conversion */
  1407. LL_ADC_REG_StartConversion(hadc->Instance);
  1408. }
  1409. else
  1410. {
  1411. /* Process unlocked */
  1412. __HAL_UNLOCK(hadc);
  1413. }
  1414. }
  1415. else
  1416. {
  1417. tmp_hal_status = HAL_BUSY;
  1418. }
  1419. /* Return function status */
  1420. return tmp_hal_status;
  1421. }
  1422. /**
  1423. * @brief Stop ADC conversion of regular group (and injected group in
  1424. * case of auto_injection mode), disable interrution of
  1425. * end-of-conversion, disable ADC peripheral.
  1426. * @param hadc ADC handle
  1427. * @retval HAL status.
  1428. */
  1429. HAL_StatusTypeDef HAL_ADC_Stop_IT(ADC_HandleTypeDef *hadc)
  1430. {
  1431. HAL_StatusTypeDef tmp_hal_status;
  1432. /* Check the parameters */
  1433. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1434. /* Process locked */
  1435. __HAL_LOCK(hadc);
  1436. /* 1. Stop potential conversion on going, on ADC groups regular and injected */
  1437. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_INJECTED_GROUP);
  1438. /* Disable ADC peripheral if conversions are effectively stopped */
  1439. if (tmp_hal_status == HAL_OK)
  1440. {
  1441. /* Disable ADC end of conversion interrupt for regular group */
  1442. /* Disable ADC overrun interrupt */
  1443. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_EOS | ADC_IT_OVR));
  1444. /* 2. Disable the ADC peripheral */
  1445. tmp_hal_status = ADC_Disable(hadc);
  1446. /* Check if ADC is effectively disabled */
  1447. if (tmp_hal_status == HAL_OK)
  1448. {
  1449. /* Set ADC state */
  1450. ADC_STATE_CLR_SET(hadc->State,
  1451. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  1452. HAL_ADC_STATE_READY);
  1453. }
  1454. }
  1455. /* Process unlocked */
  1456. __HAL_UNLOCK(hadc);
  1457. /* Return function status */
  1458. return tmp_hal_status;
  1459. }
  1460. /**
  1461. * @brief Enable ADC, start conversion of regular group and transfer result through DMA.
  1462. * @note Interruptions enabled in this function:
  1463. * overrun (if applicable), DMA half transfer, DMA transfer complete.
  1464. * Each of these interruptions has its dedicated callback function.
  1465. * @param hadc ADC handle
  1466. * @param pData Destination Buffer address.
  1467. * @param Length Number of data to be transferred from ADC peripheral to memory
  1468. * @retval HAL status.
  1469. */
  1470. HAL_StatusTypeDef HAL_ADC_Start_DMA(ADC_HandleTypeDef *hadc, uint32_t *pData, uint32_t Length)
  1471. {
  1472. HAL_StatusTypeDef tmp_hal_status;
  1473. /* Check the parameters */
  1474. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1475. /* Perform ADC enable and conversion start if no conversion is on going */
  1476. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
  1477. {
  1478. /* Process locked */
  1479. __HAL_LOCK(hadc);
  1480. /* Enable the ADC peripheral */
  1481. tmp_hal_status = ADC_Enable(hadc);
  1482. /* Start conversion if ADC is effectively enabled */
  1483. if (tmp_hal_status == HAL_OK)
  1484. {
  1485. /* Set ADC state */
  1486. /* - Clear state bitfield related to regular group conversion results */
  1487. /* - Set state bitfield related to regular operation */
  1488. ADC_STATE_CLR_SET(hadc->State,
  1489. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP,
  1490. HAL_ADC_STATE_REG_BUSY);
  1491. /* Check if a conversion is on going on ADC group injected */
  1492. if ((hadc->State & HAL_ADC_STATE_INJ_BUSY) != 0UL)
  1493. {
  1494. /* Reset ADC error code fields related to regular conversions only */
  1495. CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA));
  1496. }
  1497. else
  1498. {
  1499. /* Reset all ADC error code fields */
  1500. ADC_CLEAR_ERRORCODE(hadc);
  1501. }
  1502. /* Set the DMA transfer complete callback */
  1503. hadc->DMA_Handle->XferCpltCallback = ADC_DMAConvCplt;
  1504. /* Set the DMA half transfer complete callback */
  1505. hadc->DMA_Handle->XferHalfCpltCallback = ADC_DMAHalfConvCplt;
  1506. /* Set the DMA error callback */
  1507. hadc->DMA_Handle->XferErrorCallback = ADC_DMAError;
  1508. /* Manage ADC and DMA start: ADC overrun interruption, DMA start, */
  1509. /* ADC start (in case of SW start): */
  1510. /* Clear regular group conversion flag and overrun flag */
  1511. /* (To ensure of no unknown state from potential previous ADC */
  1512. /* operations) */
  1513. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
  1514. /* Process unlocked */
  1515. /* Unlock before starting ADC conversions: in case of potential */
  1516. /* interruption, to let the process to ADC IRQ Handler. */
  1517. __HAL_UNLOCK(hadc);
  1518. /* With DMA, overrun event is always considered as an error even if
  1519. hadc->Init.Overrun is set to ADC_OVR_DATA_OVERWRITTEN. Therefore,
  1520. ADC_IT_OVR is enabled. */
  1521. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);
  1522. /* Enable ADC DMA mode */
  1523. SET_BIT(hadc->Instance->CFGR, ADC_CFGR_DMAEN);
  1524. /* Start the DMA channel */
  1525. tmp_hal_status = HAL_DMA_Start_IT(hadc->DMA_Handle, (uint32_t)&hadc->Instance->DR, (uint32_t)pData, Length);
  1526. /* Enable conversion of regular group. */
  1527. /* If software start has been selected, conversion starts immediately. */
  1528. /* If external trigger has been selected, conversion will start at next */
  1529. /* trigger event. */
  1530. /* Start ADC group regular conversion */
  1531. LL_ADC_REG_StartConversion(hadc->Instance);
  1532. }
  1533. else
  1534. {
  1535. /* Process unlocked */
  1536. __HAL_UNLOCK(hadc);
  1537. }
  1538. }
  1539. else
  1540. {
  1541. tmp_hal_status = HAL_BUSY;
  1542. }
  1543. /* Return function status */
  1544. return tmp_hal_status;
  1545. }
  1546. /**
  1547. * @brief Stop ADC conversion of regular group (and injected group in
  1548. * case of auto_injection mode), disable ADC DMA transfer, disable
  1549. * ADC peripheral.
  1550. * @note: ADC peripheral disable is forcing stop of potential
  1551. * conversion on ADC group injected. If ADC group injected is under use, it
  1552. * should be preliminarily stopped using HAL_ADCEx_InjectedStop function.
  1553. * @param hadc ADC handle
  1554. * @retval HAL status.
  1555. */
  1556. HAL_StatusTypeDef HAL_ADC_Stop_DMA(ADC_HandleTypeDef *hadc)
  1557. {
  1558. HAL_StatusTypeDef tmp_hal_status;
  1559. /* Check the parameters */
  1560. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1561. /* Process locked */
  1562. __HAL_LOCK(hadc);
  1563. /* 1. Stop potential ADC group regular conversion on going */
  1564. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_INJECTED_GROUP);
  1565. /* Disable ADC peripheral if conversions are effectively stopped */
  1566. if (tmp_hal_status == HAL_OK)
  1567. {
  1568. /* Disable ADC DMA (ADC DMA configuration of continous requests is kept) */
  1569. CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_DMAEN);
  1570. /* Disable the DMA channel (in case of DMA in circular mode or stop */
  1571. /* while DMA transfer is on going) */
  1572. if (hadc->DMA_Handle->State == HAL_DMA_STATE_BUSY)
  1573. {
  1574. tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
  1575. /* Check if DMA channel effectively disabled */
  1576. if (tmp_hal_status != HAL_OK)
  1577. {
  1578. /* Update ADC state machine to error */
  1579. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  1580. }
  1581. }
  1582. /* Disable ADC overrun interrupt */
  1583. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
  1584. /* 2. Disable the ADC peripheral */
  1585. /* Update "tmp_hal_status" only if DMA channel disabling passed, */
  1586. /* to keep in memory a potential failing status. */
  1587. if (tmp_hal_status == HAL_OK)
  1588. {
  1589. tmp_hal_status = ADC_Disable(hadc);
  1590. }
  1591. else
  1592. {
  1593. (void)ADC_Disable(hadc);
  1594. }
  1595. /* Check if ADC is effectively disabled */
  1596. if (tmp_hal_status == HAL_OK)
  1597. {
  1598. /* Set ADC state */
  1599. ADC_STATE_CLR_SET(hadc->State,
  1600. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  1601. HAL_ADC_STATE_READY);
  1602. }
  1603. }
  1604. /* Process unlocked */
  1605. __HAL_UNLOCK(hadc);
  1606. /* Return function status */
  1607. return tmp_hal_status;
  1608. }
  1609. /**
  1610. * @brief Get ADC regular group conversion result.
  1611. * @note Reading register DR automatically clears ADC flag EOC
  1612. * (ADC group regular end of unitary conversion).
  1613. * @note This function does not clear ADC flag EOS
  1614. * (ADC group regular end of sequence conversion).
  1615. * Occurrence of flag EOS rising:
  1616. * - If sequencer is composed of 1 rank, flag EOS is equivalent
  1617. * to flag EOC.
  1618. * - If sequencer is composed of several ranks, during the scan
  1619. * sequence flag EOC only is raised, at the end of the scan sequence
  1620. * both flags EOC and EOS are raised.
  1621. * To clear this flag, either use function:
  1622. * in programming model IT: @ref HAL_ADC_IRQHandler(), in programming
  1623. * model polling: @ref HAL_ADC_PollForConversion()
  1624. * or @ref __HAL_ADC_CLEAR_FLAG(&hadc, ADC_FLAG_EOS).
  1625. * @param hadc ADC handle
  1626. * @retval ADC group regular conversion data
  1627. */
  1628. uint32_t HAL_ADC_GetValue(ADC_HandleTypeDef *hadc)
  1629. {
  1630. /* Check the parameters */
  1631. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1632. /* Note: EOC flag is not cleared here by software because automatically */
  1633. /* cleared by hardware when reading register DR. */
  1634. /* Return ADC converted value */
  1635. return hadc->Instance->DR;
  1636. }
  1637. /**
  1638. * @brief Handle ADC interrupt request.
  1639. * @param hadc ADC handle
  1640. * @retval None
  1641. */
  1642. void HAL_ADC_IRQHandler(ADC_HandleTypeDef *hadc)
  1643. {
  1644. uint32_t overrun_error = 0UL; /* flag set if overrun occurrence has to be considered as an error */
  1645. uint32_t tmp_isr = hadc->Instance->ISR;
  1646. uint32_t tmp_ier = hadc->Instance->IER;
  1647. uint32_t tmp_adc_inj_is_trigger_source_sw_start;
  1648. uint32_t tmp_adc_reg_is_trigger_source_sw_start;
  1649. uint32_t tmp_cfgr;
  1650. /* Check the parameters */
  1651. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1652. assert_param(IS_ADC_EOC_SELECTION(hadc->Init.EOCSelection));
  1653. /* ========== Check End of Sampling flag for ADC group regular ========== */
  1654. if (((tmp_isr & ADC_FLAG_EOSMP) == ADC_FLAG_EOSMP) && ((tmp_ier & ADC_IT_EOSMP) == ADC_IT_EOSMP))
  1655. {
  1656. /* Update state machine on end of sampling status if not in error state */
  1657. if ((hadc->State & HAL_ADC_STATE_ERROR_INTERNAL) == 0UL)
  1658. {
  1659. /* Set ADC state */
  1660. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOSMP);
  1661. }
  1662. /* End Of Sampling callback */
  1663. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  1664. hadc->EndOfSamplingCallback(hadc);
  1665. #else
  1666. HAL_ADCEx_EndOfSamplingCallback(hadc);
  1667. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  1668. /* Clear regular group conversion flag */
  1669. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOSMP);
  1670. }
  1671. /* ====== Check ADC group regular end of unitary conversion sequence conversions ===== */
  1672. if ((((tmp_isr & ADC_FLAG_EOC) == ADC_FLAG_EOC) && ((tmp_ier & ADC_IT_EOC) == ADC_IT_EOC)) ||
  1673. (((tmp_isr & ADC_FLAG_EOS) == ADC_FLAG_EOS) && ((tmp_ier & ADC_IT_EOS) == ADC_IT_EOS)))
  1674. {
  1675. /* Update state machine on conversion status if not in error state */
  1676. if ((hadc->State & HAL_ADC_STATE_ERROR_INTERNAL) == 0UL)
  1677. {
  1678. /* Set ADC state */
  1679. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  1680. }
  1681. /* Determine whether any further conversion upcoming on group regular */
  1682. /* by external trigger, continuous mode or scan sequence on going */
  1683. /* to disable interruption. */
  1684. if (LL_ADC_REG_IsTriggerSourceSWStart(hadc->Instance) != 0UL)
  1685. {
  1686. /* Carry on if continuous mode is disabled */
  1687. if (READ_BIT (hadc->Instance->CFGR, ADC_CFGR_CONT) != ADC_CFGR_CONT)
  1688. {
  1689. /* If End of Sequence is reached, disable interrupts */
  1690. if (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOS))
  1691. {
  1692. /* Allowed to modify bits ADC_IT_EOC/ADC_IT_EOS only if bit */
  1693. /* ADSTART==0 (no conversion on going) */
  1694. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
  1695. {
  1696. /* Disable ADC end of sequence conversion interrupt */
  1697. /* Note: Overrun interrupt was enabled with EOC interrupt in */
  1698. /* HAL_Start_IT(), but is not disabled here because can be used */
  1699. /* by overrun IRQ process below. */
  1700. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC | ADC_IT_EOS);
  1701. /* Set ADC state */
  1702. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  1703. if ((hadc->State & HAL_ADC_STATE_INJ_BUSY) == 0UL)
  1704. {
  1705. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  1706. }
  1707. }
  1708. else
  1709. {
  1710. /* Change ADC state to error state */
  1711. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  1712. /* Set ADC error code to ADC peripheral internal error */
  1713. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  1714. }
  1715. }
  1716. }
  1717. }
  1718. /* Conversion complete callback */
  1719. /* Note: Into callback function "HAL_ADC_ConvCpltCallback()", */
  1720. /* to determine if conversion has been triggered from EOC or EOS, */
  1721. /* possibility to use: */
  1722. /* " if( __HAL_ADC_GET_FLAG(&hadc, ADC_FLAG_EOS)) " */
  1723. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  1724. hadc->ConvCpltCallback(hadc);
  1725. #else
  1726. HAL_ADC_ConvCpltCallback(hadc);
  1727. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  1728. /* Clear regular group conversion flag */
  1729. /* Note: in case of overrun set to ADC_OVR_DATA_PRESERVED, end of */
  1730. /* conversion flags clear induces the release of the preserved data.*/
  1731. /* Therefore, if the preserved data value is needed, it must be */
  1732. /* read preliminarily into HAL_ADC_ConvCpltCallback(). */
  1733. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS));
  1734. }
  1735. /* ====== Check ADC group injected end of unitary conversion sequence conversions ===== */
  1736. if ((((tmp_isr & ADC_FLAG_JEOC) == ADC_FLAG_JEOC) && ((tmp_ier & ADC_IT_JEOC) == ADC_IT_JEOC)) ||
  1737. (((tmp_isr & ADC_FLAG_JEOS) == ADC_FLAG_JEOS) && ((tmp_ier & ADC_IT_JEOS) == ADC_IT_JEOS)))
  1738. {
  1739. /* Update state machine on conversion status if not in error state */
  1740. if ((hadc->State & HAL_ADC_STATE_ERROR_INTERNAL) == 0UL)
  1741. {
  1742. /* Set ADC state */
  1743. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_EOC);
  1744. }
  1745. /* Retrieve ADC configuration */
  1746. tmp_adc_inj_is_trigger_source_sw_start = LL_ADC_INJ_IsTriggerSourceSWStart(hadc->Instance);
  1747. tmp_adc_reg_is_trigger_source_sw_start = LL_ADC_REG_IsTriggerSourceSWStart(hadc->Instance);
  1748. tmp_cfgr = READ_REG(hadc->Instance->CFGR);
  1749. /* Disable interruption if no further conversion upcoming by injected */
  1750. /* external trigger or by automatic injected conversion with regular */
  1751. /* group having no further conversion upcoming (same conditions as */
  1752. /* regular group interruption disabling above), */
  1753. /* and if injected scan sequence is completed. */
  1754. if ((tmp_adc_inj_is_trigger_source_sw_start != 0UL) ||
  1755. ((READ_BIT(tmp_cfgr, ADC_CFGR_JAUTO) == 0UL) &&
  1756. ((tmp_adc_reg_is_trigger_source_sw_start != 0UL) &&
  1757. (READ_BIT(tmp_cfgr, ADC_CFGR_CONT) == 0UL))))
  1758. {
  1759. /* If End of Sequence is reached, disable interrupts */
  1760. if (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_JEOS))
  1761. {
  1762. /* Particular case if injected contexts queue is enabled: */
  1763. /* when the last context has been fully processed, JSQR is reset */
  1764. /* by the hardware. Even if no injected conversion is planned to come */
  1765. /* (queue empty, triggers are ignored), it can start again */
  1766. /* immediately after setting a new context (JADSTART is still set). */
  1767. /* Therefore, state of HAL ADC injected group is kept to busy. */
  1768. if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_JQM) == 0UL)
  1769. {
  1770. /* Allowed to modify bits ADC_IT_JEOC/ADC_IT_JEOS only if bit */
  1771. /* JADSTART==0 (no conversion on going) */
  1772. if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) == 0UL)
  1773. {
  1774. /* Disable ADC end of sequence conversion interrupt */
  1775. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC | ADC_IT_JEOS);
  1776. /* Set ADC state */
  1777. CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  1778. if ((hadc->State & HAL_ADC_STATE_REG_BUSY) == 0UL)
  1779. {
  1780. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  1781. }
  1782. }
  1783. else
  1784. {
  1785. /* Update ADC state machine to error */
  1786. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  1787. /* Set ADC error code to ADC peripheral internal error */
  1788. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  1789. }
  1790. }
  1791. }
  1792. }
  1793. /* Injected Conversion complete callback */
  1794. /* Note: HAL_ADCEx_InjectedConvCpltCallback can resort to
  1795. if( __HAL_ADC_GET_FLAG(&hadc, ADC_FLAG_JEOS)) or
  1796. if( __HAL_ADC_GET_FLAG(&hadc, ADC_FLAG_JEOC)) to determine whether
  1797. interruption has been triggered by end of conversion or end of
  1798. sequence. */
  1799. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  1800. hadc->InjectedConvCpltCallback(hadc);
  1801. #else
  1802. HAL_ADCEx_InjectedConvCpltCallback(hadc);
  1803. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  1804. /* Clear injected group conversion flag */
  1805. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOC | ADC_FLAG_JEOS);
  1806. }
  1807. /* ========== Check Analog watchdog 1 flag ========== */
  1808. if (((tmp_isr & ADC_FLAG_AWD1) == ADC_FLAG_AWD1) && ((tmp_ier & ADC_IT_AWD1) == ADC_IT_AWD1))
  1809. {
  1810. /* Set ADC state */
  1811. SET_BIT(hadc->State, HAL_ADC_STATE_AWD1);
  1812. /* Level out of window 1 callback */
  1813. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  1814. hadc->LevelOutOfWindowCallback(hadc);
  1815. #else
  1816. HAL_ADC_LevelOutOfWindowCallback(hadc);
  1817. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  1818. /* Clear ADC analog watchdog flag */
  1819. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD1);
  1820. }
  1821. /* ========== Check analog watchdog 2 flag ========== */
  1822. if (((tmp_isr & ADC_FLAG_AWD2) == ADC_FLAG_AWD2) && ((tmp_ier & ADC_IT_AWD2) == ADC_IT_AWD2))
  1823. {
  1824. /* Set ADC state */
  1825. SET_BIT(hadc->State, HAL_ADC_STATE_AWD2);
  1826. /* Level out of window 2 callback */
  1827. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  1828. hadc->LevelOutOfWindow2Callback(hadc);
  1829. #else
  1830. HAL_ADCEx_LevelOutOfWindow2Callback(hadc);
  1831. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  1832. /* Clear ADC analog watchdog flag */
  1833. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD2);
  1834. }
  1835. /* ========== Check analog watchdog 3 flag ========== */
  1836. if (((tmp_isr & ADC_FLAG_AWD3) == ADC_FLAG_AWD3) && ((tmp_ier & ADC_IT_AWD3) == ADC_IT_AWD3))
  1837. {
  1838. /* Set ADC state */
  1839. SET_BIT(hadc->State, HAL_ADC_STATE_AWD3);
  1840. /* Level out of window 3 callback */
  1841. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  1842. hadc->LevelOutOfWindow3Callback(hadc);
  1843. #else
  1844. HAL_ADCEx_LevelOutOfWindow3Callback(hadc);
  1845. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  1846. /* Clear ADC analog watchdog flag */
  1847. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD3);
  1848. }
  1849. /* ========== Check Overrun flag ========== */
  1850. if (((tmp_isr & ADC_FLAG_OVR) == ADC_FLAG_OVR) && ((tmp_ier & ADC_IT_OVR) == ADC_IT_OVR))
  1851. {
  1852. /* If overrun is set to overwrite previous data (default setting), */
  1853. /* overrun event is not considered as an error. */
  1854. /* (cf ref manual "Managing conversions without using the DMA and without */
  1855. /* overrun ") */
  1856. /* Exception for usage with DMA overrun event always considered as an */
  1857. /* error. */
  1858. if (hadc->Init.Overrun == ADC_OVR_DATA_PRESERVED)
  1859. {
  1860. overrun_error = 1UL;
  1861. }
  1862. else
  1863. {
  1864. /* Check DMA configuration */
  1865. if (LL_ADC_REG_GetDMATransfer(hadc->Instance) != LL_ADC_REG_DMA_TRANSFER_NONE)
  1866. {
  1867. overrun_error = 1UL;
  1868. }
  1869. }
  1870. if (overrun_error == 1UL)
  1871. {
  1872. /* Change ADC state to error state */
  1873. SET_BIT(hadc->State, HAL_ADC_STATE_REG_OVR);
  1874. /* Set ADC error code to overrun */
  1875. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_OVR);
  1876. /* Error callback */
  1877. /* Note: In case of overrun, ADC conversion data is preserved until */
  1878. /* flag OVR is reset. */
  1879. /* Therefore, old ADC conversion data can be retrieved in */
  1880. /* function "HAL_ADC_ErrorCallback()". */
  1881. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  1882. hadc->ErrorCallback(hadc);
  1883. #else
  1884. HAL_ADC_ErrorCallback(hadc);
  1885. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  1886. }
  1887. /* Clear ADC overrun flag */
  1888. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  1889. }
  1890. /* ========== Check Injected context queue overflow flag ========== */
  1891. if (((tmp_isr & ADC_FLAG_JQOVF) == ADC_FLAG_JQOVF) && ((tmp_ier & ADC_IT_JQOVF) == ADC_IT_JQOVF))
  1892. {
  1893. /* Change ADC state to overrun state */
  1894. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_JQOVF);
  1895. /* Set ADC error code to Injected context queue overflow */
  1896. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_JQOVF);
  1897. /* Clear the Injected context queue overflow flag */
  1898. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JQOVF);
  1899. /* Injected context queue overflow callback */
  1900. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  1901. hadc->InjectedQueueOverflowCallback(hadc);
  1902. #else
  1903. HAL_ADCEx_InjectedQueueOverflowCallback(hadc);
  1904. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  1905. }
  1906. }
  1907. /**
  1908. * @brief Conversion complete callback in non-blocking mode.
  1909. * @param hadc ADC handle
  1910. * @retval None
  1911. */
  1912. __weak void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
  1913. {
  1914. /* Prevent unused argument(s) compilation warning */
  1915. UNUSED(hadc);
  1916. /* NOTE : This function should not be modified. When the callback is needed,
  1917. function HAL_ADC_ConvCpltCallback must be implemented in the user file.
  1918. */
  1919. }
  1920. /**
  1921. * @brief Conversion DMA half-transfer callback in non-blocking mode.
  1922. * @param hadc ADC handle
  1923. * @retval None
  1924. */
  1925. __weak void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef *hadc)
  1926. {
  1927. /* Prevent unused argument(s) compilation warning */
  1928. UNUSED(hadc);
  1929. /* NOTE : This function should not be modified. When the callback is needed,
  1930. function HAL_ADC_ConvHalfCpltCallback must be implemented in the user file.
  1931. */
  1932. }
  1933. /**
  1934. * @brief Analog watchdog 1 callback in non-blocking mode.
  1935. * @param hadc ADC handle
  1936. * @retval None
  1937. */
  1938. __weak void HAL_ADC_LevelOutOfWindowCallback(ADC_HandleTypeDef *hadc)
  1939. {
  1940. /* Prevent unused argument(s) compilation warning */
  1941. UNUSED(hadc);
  1942. /* NOTE : This function should not be modified. When the callback is needed,
  1943. function HAL_ADC_LevelOutOfWindowCallback must be implemented in the user file.
  1944. */
  1945. }
  1946. /**
  1947. * @brief ADC error callback in non-blocking mode
  1948. * (ADC conversion with interruption or transfer by DMA).
  1949. * @note In case of error due to overrun when using ADC with DMA transfer
  1950. * (HAL ADC handle parameter "ErrorCode" to state "HAL_ADC_ERROR_OVR"):
  1951. * - Reinitialize the DMA using function "HAL_ADC_Stop_DMA()".
  1952. * - If needed, restart a new ADC conversion using function
  1953. * "HAL_ADC_Start_DMA()"
  1954. * (this function is also clearing overrun flag)
  1955. * @param hadc ADC handle
  1956. * @retval None
  1957. */
  1958. __weak void HAL_ADC_ErrorCallback(ADC_HandleTypeDef *hadc)
  1959. {
  1960. /* Prevent unused argument(s) compilation warning */
  1961. UNUSED(hadc);
  1962. /* NOTE : This function should not be modified. When the callback is needed,
  1963. function HAL_ADC_ErrorCallback must be implemented in the user file.
  1964. */
  1965. }
  1966. /**
  1967. * @}
  1968. */
  1969. /** @defgroup ADC_Exported_Functions_Group3 Peripheral Control functions
  1970. * @brief Peripheral Control functions
  1971. *
  1972. @verbatim
  1973. ===============================================================================
  1974. ##### Peripheral Control functions #####
  1975. ===============================================================================
  1976. [..] This section provides functions allowing to:
  1977. (+) Configure channels on regular group
  1978. (+) Configure the analog watchdog
  1979. @endverbatim
  1980. * @{
  1981. */
  1982. /**
  1983. * @brief Configure a channel to be assigned to ADC group regular.
  1984. * @note In case of usage of internal measurement channels:
  1985. * Vbat/VrefInt/TempSensor.
  1986. * These internal paths can be disabled using function
  1987. * HAL_ADC_DeInit().
  1988. * @note Possibility to update parameters on the fly:
  1989. * This function initializes channel into ADC group regular,
  1990. * following calls to this function can be used to reconfigure
  1991. * some parameters of structure "ADC_ChannelConfTypeDef" on the fly,
  1992. * without resetting the ADC.
  1993. * The setting of these parameters is conditioned to ADC state:
  1994. * Refer to comments of structure "ADC_ChannelConfTypeDef".
  1995. * @param hadc ADC handle
  1996. * @param sConfig Structure of ADC channel assigned to ADC group regular.
  1997. * @retval HAL status
  1998. */
  1999. HAL_StatusTypeDef HAL_ADC_ConfigChannel(ADC_HandleTypeDef *hadc, ADC_ChannelConfTypeDef *sConfig)
  2000. {
  2001. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  2002. uint32_t tmpOffsetShifted;
  2003. uint32_t tmp_config_internal_channel;
  2004. __IO uint32_t wait_loop_index = 0;
  2005. uint32_t tmp_adc_is_conversion_on_going_regular;
  2006. uint32_t tmp_adc_is_conversion_on_going_injected;
  2007. /* Check the parameters */
  2008. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2009. assert_param(IS_ADC_REGULAR_RANK(sConfig->Rank));
  2010. assert_param(IS_ADC_SAMPLE_TIME(sConfig->SamplingTime));
  2011. assert_param(IS_ADC_SINGLE_DIFFERENTIAL(sConfig->SingleDiff));
  2012. assert_param(IS_ADC_OFFSET_NUMBER(sConfig->OffsetNumber));
  2013. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), sConfig->Offset));
  2014. /* if ROVSE is set, the value of the OFFSETy_EN bit in ADCx_OFRy register is
  2015. ignored (considered as reset) */
  2016. assert_param(!((sConfig->OffsetNumber != ADC_OFFSET_NONE) && (hadc->Init.OversamplingMode == ENABLE)));
  2017. /* Verification of channel number */
  2018. if (sConfig->SingleDiff != ADC_DIFFERENTIAL_ENDED)
  2019. {
  2020. assert_param(IS_ADC_CHANNEL(hadc, sConfig->Channel));
  2021. }
  2022. else
  2023. {
  2024. assert_param(IS_ADC_DIFF_CHANNEL(hadc, sConfig->Channel));
  2025. }
  2026. /* Process locked */
  2027. __HAL_LOCK(hadc);
  2028. /* Parameters update conditioned to ADC state: */
  2029. /* Parameters that can be updated when ADC is disabled or enabled without */
  2030. /* conversion on going on regular group: */
  2031. /* - Channel number */
  2032. /* - Channel rank */
  2033. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
  2034. {
  2035. /* Set ADC group regular sequence: channel on the selected scan sequence rank */
  2036. LL_ADC_REG_SetSequencerRanks(hadc->Instance, sConfig->Rank, sConfig->Channel);
  2037. /* Parameters update conditioned to ADC state: */
  2038. /* Parameters that can be updated when ADC is disabled or enabled without */
  2039. /* conversion on going on regular group: */
  2040. /* - Channel sampling time */
  2041. /* - Channel offset */
  2042. tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
  2043. tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
  2044. if ((tmp_adc_is_conversion_on_going_regular == 0UL)
  2045. && (tmp_adc_is_conversion_on_going_injected == 0UL)
  2046. )
  2047. {
  2048. /* Set sampling time of the selected ADC channel */
  2049. LL_ADC_SetChannelSamplingTime(hadc->Instance, sConfig->Channel, sConfig->SamplingTime);
  2050. /* Configure the offset: offset enable/disable, channel, offset value */
  2051. /* Shift the offset with respect to the selected ADC resolution. */
  2052. /* Offset has to be left-aligned on bit 11, the LSB (right bits) are set to 0 */
  2053. tmpOffsetShifted = ADC_OFFSET_SHIFT_RESOLUTION(hadc, (uint32_t)sConfig->Offset);
  2054. if (sConfig->OffsetNumber != ADC_OFFSET_NONE)
  2055. {
  2056. /* Set ADC selected offset number */
  2057. LL_ADC_SetOffset(hadc->Instance, sConfig->OffsetNumber, sConfig->Channel, tmpOffsetShifted);
  2058. }
  2059. else
  2060. {
  2061. /* Scan each offset register to check if the selected channel is targeted. */
  2062. /* If this is the case, the corresponding offset number is disabled. */
  2063. if(__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_1)) == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfig->Channel))
  2064. {
  2065. LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_1, LL_ADC_OFFSET_DISABLE);
  2066. }
  2067. if(__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_2)) == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfig->Channel))
  2068. {
  2069. LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_2, LL_ADC_OFFSET_DISABLE);
  2070. }
  2071. if(__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_3)) == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfig->Channel))
  2072. {
  2073. LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_3, LL_ADC_OFFSET_DISABLE);
  2074. }
  2075. if(__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_4)) == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfig->Channel))
  2076. {
  2077. LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_4, LL_ADC_OFFSET_DISABLE);
  2078. }
  2079. }
  2080. }
  2081. /* Parameters update conditioned to ADC state: */
  2082. /* Parameters that can be updated only when ADC is disabled: */
  2083. /* - Single or differential mode */
  2084. /* - Internal measurement channels: Vbat/VrefInt/TempSensor */
  2085. if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
  2086. {
  2087. /* Set mode single-ended or differential input of the selected ADC channel */
  2088. LL_ADC_SetChannelSingleDiff(hadc->Instance, sConfig->Channel, sConfig->SingleDiff);
  2089. /* Configuration of differential mode */
  2090. if (sConfig->SingleDiff == ADC_DIFFERENTIAL_ENDED)
  2091. {
  2092. /* Set sampling time of the selected ADC channel */
  2093. /* Note: ADC channel number masked with value "0x1F" to ensure shift value within 32 bits range */
  2094. LL_ADC_SetChannelSamplingTime(hadc->Instance,
  2095. (uint32_t)(__LL_ADC_DECIMAL_NB_TO_CHANNEL((__LL_ADC_CHANNEL_TO_DECIMAL_NB((uint32_t)sConfig->Channel) + 1UL) & 0x1FUL)),
  2096. sConfig->SamplingTime);
  2097. }
  2098. /* Management of internal measurement channels: Vbat/VrefInt/TempSensor. */
  2099. /* If internal channel selected, enable dedicated internal buffers and */
  2100. /* paths. */
  2101. /* Note: these internal measurement paths can be disabled using */
  2102. /* HAL_ADC_DeInit(). */
  2103. if(__LL_ADC_IS_CHANNEL_INTERNAL(sConfig->Channel))
  2104. {
  2105. /* Configuration of common ADC parameters */
  2106. tmp_config_internal_channel = LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
  2107. /* Software is allowed to change common parameters only when all ADCs */
  2108. /* of the common group are disabled. */
  2109. if (__LL_ADC_IS_ENABLED_ALL_COMMON_INSTANCE(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) == 0UL)
  2110. {
  2111. /* If the requested internal measurement path has already been enabled, */
  2112. /* bypass the configuration processing. */
  2113. if ((sConfig->Channel == ADC_CHANNEL_TEMPSENSOR) && ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_TEMPSENSOR) == 0UL))
  2114. {
  2115. if (ADC_TEMPERATURE_SENSOR_INSTANCE(hadc))
  2116. {
  2117. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_TEMPSENSOR | tmp_config_internal_channel);
  2118. /* Delay for temperature sensor stabilization time */
  2119. /* Wait loop initialization and execution */
  2120. /* Note: Variable divided by 2 to compensate partially */
  2121. /* CPU processing cycles, scaling in us split to not */
  2122. /* exceed 32 bits register capacity and handle low frequency. */
  2123. wait_loop_index = ((LL_ADC_DELAY_TEMPSENSOR_STAB_US / 10UL) * (SystemCoreClock / (100000UL * 2UL)));
  2124. while(wait_loop_index != 0UL)
  2125. {
  2126. wait_loop_index--;
  2127. }
  2128. }
  2129. }
  2130. else if ((sConfig->Channel == ADC_CHANNEL_VBAT) && ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_VBAT) == 0UL))
  2131. {
  2132. if (ADC_BATTERY_VOLTAGE_INSTANCE(hadc))
  2133. {
  2134. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_VBAT | tmp_config_internal_channel);
  2135. }
  2136. }
  2137. else if ((sConfig->Channel == ADC_CHANNEL_VREFINT) && ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_VREFINT) == 0UL))
  2138. {
  2139. if (ADC_VREFINT_INSTANCE(hadc))
  2140. {
  2141. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_VREFINT | tmp_config_internal_channel);
  2142. }
  2143. }
  2144. else
  2145. {
  2146. /* nothing to do */
  2147. }
  2148. }
  2149. /* If the requested internal measurement path has already been */
  2150. /* enabled and other ADC of the common group are enabled, internal */
  2151. /* measurement paths cannot be enabled. */
  2152. else
  2153. {
  2154. /* Update ADC state machine to error */
  2155. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  2156. tmp_hal_status = HAL_ERROR;
  2157. }
  2158. }
  2159. }
  2160. }
  2161. /* If a conversion is on going on regular group, no update on regular */
  2162. /* channel could be done on neither of the channel configuration structure */
  2163. /* parameters. */
  2164. else
  2165. {
  2166. /* Update ADC state machine to error */
  2167. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  2168. tmp_hal_status = HAL_ERROR;
  2169. }
  2170. /* Process unlocked */
  2171. __HAL_UNLOCK(hadc);
  2172. /* Return function status */
  2173. return tmp_hal_status;
  2174. }
  2175. /**
  2176. * @brief Configure the analog watchdog.
  2177. * @note Possibility to update parameters on the fly:
  2178. * This function initializes the selected analog watchdog, successive
  2179. * calls to this function can be used to reconfigure some parameters
  2180. * of structure "ADC_AnalogWDGConfTypeDef" on the fly, without resetting
  2181. * the ADC.
  2182. * The setting of these parameters is conditioned to ADC state.
  2183. * For parameters constraints, see comments of structure
  2184. * "ADC_AnalogWDGConfTypeDef".
  2185. * @note On this STM32 serie, analog watchdog thresholds cannot be modified
  2186. * while ADC conversion is on going.
  2187. * @param hadc ADC handle
  2188. * @param AnalogWDGConfig Structure of ADC analog watchdog configuration
  2189. * @retval HAL status
  2190. */
  2191. HAL_StatusTypeDef HAL_ADC_AnalogWDGConfig(ADC_HandleTypeDef *hadc, ADC_AnalogWDGConfTypeDef *AnalogWDGConfig)
  2192. {
  2193. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  2194. uint32_t tmpAWDHighThresholdShifted;
  2195. uint32_t tmpAWDLowThresholdShifted;
  2196. uint32_t tmp_adc_is_conversion_on_going_regular;
  2197. uint32_t tmp_adc_is_conversion_on_going_injected;
  2198. /* Check the parameters */
  2199. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2200. assert_param(IS_ADC_ANALOG_WATCHDOG_NUMBER(AnalogWDGConfig->WatchdogNumber));
  2201. assert_param(IS_ADC_ANALOG_WATCHDOG_MODE(AnalogWDGConfig->WatchdogMode));
  2202. assert_param(IS_FUNCTIONAL_STATE(AnalogWDGConfig->ITMode));
  2203. if ((AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_REG) ||
  2204. (AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_INJEC) ||
  2205. (AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_REGINJEC))
  2206. {
  2207. assert_param(IS_ADC_CHANNEL(hadc, AnalogWDGConfig->Channel));
  2208. }
  2209. /* Verify thresholds range */
  2210. if (hadc->Init.OversamplingMode == ENABLE)
  2211. {
  2212. /* Case of oversampling enabled: depending on ratio and shift configuration,
  2213. analog watchdog thresholds can be higher than ADC resolution.
  2214. Verify if thresholds are within maximum thresholds range. */
  2215. assert_param(IS_ADC_RANGE(ADC_RESOLUTION_12B, AnalogWDGConfig->HighThreshold));
  2216. assert_param(IS_ADC_RANGE(ADC_RESOLUTION_12B, AnalogWDGConfig->LowThreshold));
  2217. }
  2218. else
  2219. {
  2220. /* Verify if thresholds are within the selected ADC resolution */
  2221. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), AnalogWDGConfig->HighThreshold));
  2222. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), AnalogWDGConfig->LowThreshold));
  2223. }
  2224. /* Process locked */
  2225. __HAL_LOCK(hadc);
  2226. /* Parameters update conditioned to ADC state: */
  2227. /* Parameters that can be updated when ADC is disabled or enabled without */
  2228. /* conversion on going on ADC groups regular and injected: */
  2229. /* - Analog watchdog channels */
  2230. /* - Analog watchdog thresholds */
  2231. tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
  2232. tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
  2233. if ((tmp_adc_is_conversion_on_going_regular == 0UL)
  2234. && (tmp_adc_is_conversion_on_going_injected == 0UL)
  2235. )
  2236. {
  2237. /* Analog watchdog configuration */
  2238. if (AnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_1)
  2239. {
  2240. /* Configuration of analog watchdog: */
  2241. /* - Set the analog watchdog enable mode: one or overall group of */
  2242. /* channels, on groups regular and-or injected. */
  2243. switch (AnalogWDGConfig->WatchdogMode)
  2244. {
  2245. case ADC_ANALOGWATCHDOG_SINGLE_REG:
  2246. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, __LL_ADC_ANALOGWD_CHANNEL_GROUP(AnalogWDGConfig->Channel,
  2247. LL_ADC_GROUP_REGULAR));
  2248. break;
  2249. case ADC_ANALOGWATCHDOG_SINGLE_INJEC:
  2250. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, __LL_ADC_ANALOGWD_CHANNEL_GROUP(AnalogWDGConfig->Channel,
  2251. LL_ADC_GROUP_INJECTED));
  2252. break;
  2253. case ADC_ANALOGWATCHDOG_SINGLE_REGINJEC:
  2254. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, __LL_ADC_ANALOGWD_CHANNEL_GROUP(AnalogWDGConfig->Channel,
  2255. LL_ADC_GROUP_REGULAR_INJECTED));
  2256. break;
  2257. case ADC_ANALOGWATCHDOG_ALL_REG:
  2258. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, LL_ADC_AWD_ALL_CHANNELS_REG);
  2259. break;
  2260. case ADC_ANALOGWATCHDOG_ALL_INJEC:
  2261. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, LL_ADC_AWD_ALL_CHANNELS_INJ);
  2262. break;
  2263. case ADC_ANALOGWATCHDOG_ALL_REGINJEC:
  2264. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, LL_ADC_AWD_ALL_CHANNELS_REG_INJ);
  2265. break;
  2266. default: /* ADC_ANALOGWATCHDOG_NONE */
  2267. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, LL_ADC_AWD_DISABLE);
  2268. break;
  2269. }
  2270. /* Shift the offset in function of the selected ADC resolution: */
  2271. /* Thresholds have to be left-aligned on bit 11, the LSB (right bits) */
  2272. /* are set to 0 */
  2273. tmpAWDHighThresholdShifted = ADC_AWD1THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->HighThreshold);
  2274. tmpAWDLowThresholdShifted = ADC_AWD1THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->LowThreshold);
  2275. /* Set ADC analog watchdog thresholds value of both thresholds high and low */
  2276. LL_ADC_ConfigAnalogWDThresholds(hadc->Instance, AnalogWDGConfig->WatchdogNumber, tmpAWDHighThresholdShifted, tmpAWDLowThresholdShifted);
  2277. /* Update state, clear previous result related to AWD1 */
  2278. CLEAR_BIT(hadc->State, HAL_ADC_STATE_AWD1);
  2279. /* Clear flag ADC analog watchdog */
  2280. /* Note: Flag cleared Clear the ADC Analog watchdog flag to be ready */
  2281. /* to use for HAL_ADC_IRQHandler() or HAL_ADC_PollForEvent() */
  2282. /* (in case left enabled by previous ADC operations). */
  2283. LL_ADC_ClearFlag_AWD1(hadc->Instance);
  2284. /* Configure ADC analog watchdog interrupt */
  2285. if (AnalogWDGConfig->ITMode == ENABLE)
  2286. {
  2287. LL_ADC_EnableIT_AWD1(hadc->Instance);
  2288. }
  2289. else
  2290. {
  2291. LL_ADC_DisableIT_AWD1(hadc->Instance);
  2292. }
  2293. }
  2294. /* Case of ADC_ANALOGWATCHDOG_2 or ADC_ANALOGWATCHDOG_3 */
  2295. else
  2296. {
  2297. switch (AnalogWDGConfig->WatchdogMode)
  2298. {
  2299. case ADC_ANALOGWATCHDOG_SINGLE_REG:
  2300. case ADC_ANALOGWATCHDOG_SINGLE_INJEC:
  2301. case ADC_ANALOGWATCHDOG_SINGLE_REGINJEC:
  2302. /* Update AWD by bitfield to keep the possibility to monitor */
  2303. /* several channels by successive calls of this function. */
  2304. if (AnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_2)
  2305. {
  2306. SET_BIT(hadc->Instance->AWD2CR, (1UL << (__LL_ADC_CHANNEL_TO_DECIMAL_NB(AnalogWDGConfig->Channel) & 0x1FUL)));
  2307. }
  2308. else
  2309. {
  2310. SET_BIT(hadc->Instance->AWD3CR, (1UL << (__LL_ADC_CHANNEL_TO_DECIMAL_NB(AnalogWDGConfig->Channel) & 0x1FUL)));
  2311. }
  2312. break;
  2313. case ADC_ANALOGWATCHDOG_ALL_REG:
  2314. case ADC_ANALOGWATCHDOG_ALL_INJEC:
  2315. case ADC_ANALOGWATCHDOG_ALL_REGINJEC:
  2316. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, AnalogWDGConfig->WatchdogNumber, LL_ADC_AWD_ALL_CHANNELS_REG_INJ);
  2317. break;
  2318. default: /* ADC_ANALOGWATCHDOG_NONE */
  2319. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, AnalogWDGConfig->WatchdogNumber, LL_ADC_AWD_DISABLE);
  2320. break;
  2321. }
  2322. /* Shift the thresholds in function of the selected ADC resolution */
  2323. /* have to be left-aligned on bit 7, the LSB (right bits) are set to 0 */
  2324. tmpAWDHighThresholdShifted = ADC_AWD23THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->HighThreshold);
  2325. tmpAWDLowThresholdShifted = ADC_AWD23THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->LowThreshold);
  2326. /* Set ADC analog watchdog thresholds value of both thresholds high and low */
  2327. LL_ADC_ConfigAnalogWDThresholds(hadc->Instance, AnalogWDGConfig->WatchdogNumber, tmpAWDHighThresholdShifted, tmpAWDLowThresholdShifted);
  2328. if (AnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_2)
  2329. {
  2330. /* Update state, clear previous result related to AWD2 */
  2331. CLEAR_BIT(hadc->State, HAL_ADC_STATE_AWD2);
  2332. /* Clear flag ADC analog watchdog */
  2333. /* Note: Flag cleared Clear the ADC Analog watchdog flag to be ready */
  2334. /* to use for HAL_ADC_IRQHandler() or HAL_ADC_PollForEvent() */
  2335. /* (in case left enabled by previous ADC operations). */
  2336. LL_ADC_ClearFlag_AWD2(hadc->Instance);
  2337. /* Configure ADC analog watchdog interrupt */
  2338. if (AnalogWDGConfig->ITMode == ENABLE)
  2339. {
  2340. LL_ADC_EnableIT_AWD2(hadc->Instance);
  2341. }
  2342. else
  2343. {
  2344. LL_ADC_DisableIT_AWD2(hadc->Instance);
  2345. }
  2346. }
  2347. /* (AnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_3) */
  2348. else
  2349. {
  2350. /* Update state, clear previous result related to AWD3 */
  2351. CLEAR_BIT(hadc->State, HAL_ADC_STATE_AWD3);
  2352. /* Clear flag ADC analog watchdog */
  2353. /* Note: Flag cleared Clear the ADC Analog watchdog flag to be ready */
  2354. /* to use for HAL_ADC_IRQHandler() or HAL_ADC_PollForEvent() */
  2355. /* (in case left enabled by previous ADC operations). */
  2356. LL_ADC_ClearFlag_AWD3(hadc->Instance);
  2357. /* Configure ADC analog watchdog interrupt */
  2358. if (AnalogWDGConfig->ITMode == ENABLE)
  2359. {
  2360. LL_ADC_EnableIT_AWD3(hadc->Instance);
  2361. }
  2362. else
  2363. {
  2364. LL_ADC_DisableIT_AWD3(hadc->Instance);
  2365. }
  2366. }
  2367. }
  2368. }
  2369. /* If a conversion is on going on ADC group regular or injected, no update */
  2370. /* could be done on neither of the AWD configuration structure parameters. */
  2371. else
  2372. {
  2373. /* Update ADC state machine to error */
  2374. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  2375. tmp_hal_status = HAL_ERROR;
  2376. }
  2377. /* Process unlocked */
  2378. __HAL_UNLOCK(hadc);
  2379. /* Return function status */
  2380. return tmp_hal_status;
  2381. }
  2382. /**
  2383. * @}
  2384. */
  2385. /** @defgroup ADC_Exported_Functions_Group4 Peripheral State functions
  2386. * @brief ADC Peripheral State functions
  2387. *
  2388. @verbatim
  2389. ===============================================================================
  2390. ##### Peripheral state and errors functions #####
  2391. ===============================================================================
  2392. [..]
  2393. This subsection provides functions to get in run-time the status of the
  2394. peripheral.
  2395. (+) Check the ADC state
  2396. (+) Check the ADC error code
  2397. @endverbatim
  2398. * @{
  2399. */
  2400. /**
  2401. * @brief Return the ADC handle state.
  2402. * @note ADC state machine is managed by bitfields, ADC status must be
  2403. * compared with states bits.
  2404. * For example:
  2405. * " if ((HAL_ADC_GetState(hadc1) & HAL_ADC_STATE_REG_BUSY) != 0UL) "
  2406. * " if ((HAL_ADC_GetState(hadc1) & HAL_ADC_STATE_AWD1) != 0UL) "
  2407. * @param hadc ADC handle
  2408. * @retval ADC handle state (bitfield on 32 bits)
  2409. */
  2410. uint32_t HAL_ADC_GetState(ADC_HandleTypeDef *hadc)
  2411. {
  2412. /* Check the parameters */
  2413. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2414. /* Return ADC handle state */
  2415. return hadc->State;
  2416. }
  2417. /**
  2418. * @brief Return the ADC error code.
  2419. * @param hadc ADC handle
  2420. * @retval ADC error code (bitfield on 32 bits)
  2421. */
  2422. uint32_t HAL_ADC_GetError(ADC_HandleTypeDef *hadc)
  2423. {
  2424. /* Check the parameters */
  2425. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2426. return hadc->ErrorCode;
  2427. }
  2428. /**
  2429. * @}
  2430. */
  2431. /**
  2432. * @}
  2433. */
  2434. /** @defgroup ADC_Private_Functions ADC Private Functions
  2435. * @{
  2436. */
  2437. /**
  2438. * @brief Stop ADC conversion.
  2439. * @param hadc ADC handle
  2440. * @param ConversionGroup ADC group regular and/or injected.
  2441. * This parameter can be one of the following values:
  2442. * @arg @ref ADC_REGULAR_GROUP ADC regular conversion type.
  2443. * @arg @ref ADC_INJECTED_GROUP ADC injected conversion type.
  2444. * @arg @ref ADC_REGULAR_INJECTED_GROUP ADC regular and injected conversion type.
  2445. * @retval HAL status.
  2446. */
  2447. HAL_StatusTypeDef ADC_ConversionStop(ADC_HandleTypeDef *hadc, uint32_t ConversionGroup)
  2448. {
  2449. uint32_t tickstart;
  2450. uint32_t Conversion_Timeout_CPU_cycles = 0UL;
  2451. uint32_t conversion_group_reassigned = ConversionGroup;
  2452. uint32_t tmp_ADC_CR_ADSTART_JADSTART;
  2453. uint32_t tmp_adc_is_conversion_on_going_regular;
  2454. uint32_t tmp_adc_is_conversion_on_going_injected;
  2455. /* Check the parameters */
  2456. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2457. assert_param(IS_ADC_CONVERSION_GROUP(ConversionGroup));
  2458. /* Verification if ADC is not already stopped (on regular and injected */
  2459. /* groups) to bypass this function if not needed. */
  2460. tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
  2461. tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
  2462. if ((tmp_adc_is_conversion_on_going_regular != 0UL)
  2463. || (tmp_adc_is_conversion_on_going_injected != 0UL)
  2464. )
  2465. {
  2466. /* Particular case of continuous auto-injection mode combined with */
  2467. /* auto-delay mode. */
  2468. /* In auto-injection mode, regular group stop ADC_CR_ADSTP is used (not */
  2469. /* injected group stop ADC_CR_JADSTP). */
  2470. /* Procedure to be followed: Wait until JEOS=1, clear JEOS, set ADSTP=1 */
  2471. /* (see reference manual). */
  2472. if (((hadc->Instance->CFGR & ADC_CFGR_JAUTO) != 0UL)
  2473. && (hadc->Init.ContinuousConvMode == ENABLE)
  2474. && (hadc->Init.LowPowerAutoWait == ENABLE)
  2475. )
  2476. {
  2477. /* Use stop of regular group */
  2478. conversion_group_reassigned = ADC_REGULAR_GROUP;
  2479. /* Wait until JEOS=1 (maximum Timeout: 4 injected conversions) */
  2480. while (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_JEOS) == 0UL)
  2481. {
  2482. if (Conversion_Timeout_CPU_cycles >= (ADC_CONVERSION_TIME_MAX_CPU_CYCLES * 4UL))
  2483. {
  2484. /* Update ADC state machine to error */
  2485. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2486. /* Set ADC error code to ADC peripheral internal error */
  2487. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2488. return HAL_ERROR;
  2489. }
  2490. Conversion_Timeout_CPU_cycles ++;
  2491. }
  2492. /* Clear JEOS */
  2493. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOS);
  2494. }
  2495. /* Stop potential conversion on going on ADC group regular */
  2496. if (conversion_group_reassigned != ADC_INJECTED_GROUP)
  2497. {
  2498. /* Software is allowed to set ADSTP only when ADSTART=1 and ADDIS=0 */
  2499. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) != 0UL)
  2500. {
  2501. if (LL_ADC_IsDisableOngoing(hadc->Instance) == 0UL)
  2502. {
  2503. /* Stop ADC group regular conversion */
  2504. LL_ADC_REG_StopConversion(hadc->Instance);
  2505. }
  2506. }
  2507. }
  2508. /* Stop potential conversion on going on ADC group injected */
  2509. if (conversion_group_reassigned != ADC_REGULAR_GROUP)
  2510. {
  2511. /* Software is allowed to set JADSTP only when JADSTART=1 and ADDIS=0 */
  2512. if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) != 0UL)
  2513. {
  2514. if (LL_ADC_IsDisableOngoing(hadc->Instance) == 0UL)
  2515. {
  2516. /* Stop ADC group injected conversion */
  2517. LL_ADC_INJ_StopConversion(hadc->Instance);
  2518. }
  2519. }
  2520. }
  2521. /* Selection of start and stop bits with respect to the regular or injected group */
  2522. switch (conversion_group_reassigned)
  2523. {
  2524. case ADC_REGULAR_INJECTED_GROUP:
  2525. tmp_ADC_CR_ADSTART_JADSTART = (ADC_CR_ADSTART | ADC_CR_JADSTART);
  2526. break;
  2527. case ADC_INJECTED_GROUP:
  2528. tmp_ADC_CR_ADSTART_JADSTART = ADC_CR_JADSTART;
  2529. break;
  2530. /* Case ADC_REGULAR_GROUP only*/
  2531. default:
  2532. tmp_ADC_CR_ADSTART_JADSTART = ADC_CR_ADSTART;
  2533. break;
  2534. }
  2535. /* Wait for conversion effectively stopped */
  2536. tickstart = HAL_GetTick();
  2537. while ((hadc->Instance->CR & tmp_ADC_CR_ADSTART_JADSTART) != 0UL)
  2538. {
  2539. if ((HAL_GetTick() - tickstart) > ADC_STOP_CONVERSION_TIMEOUT)
  2540. {
  2541. /* Update ADC state machine to error */
  2542. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2543. /* Set ADC error code to ADC peripheral internal error */
  2544. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2545. return HAL_ERROR;
  2546. }
  2547. }
  2548. }
  2549. /* Return HAL status */
  2550. return HAL_OK;
  2551. }
  2552. /**
  2553. * @brief Enable the selected ADC.
  2554. * @note Prerequisite condition to use this function: ADC must be disabled
  2555. * and voltage regulator must be enabled (done into HAL_ADC_Init()).
  2556. * @param hadc ADC handle
  2557. * @retval HAL status.
  2558. */
  2559. HAL_StatusTypeDef ADC_Enable(ADC_HandleTypeDef *hadc)
  2560. {
  2561. uint32_t tickstart;
  2562. /* ADC enable and wait for ADC ready (in case of ADC is disabled or */
  2563. /* enabling phase not yet completed: flag ADC ready not yet set). */
  2564. /* Timeout implemented to not be stuck if ADC cannot be enabled (possible */
  2565. /* causes: ADC clock not running, ...). */
  2566. if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
  2567. {
  2568. /* Check if conditions to enable the ADC are fulfilled */
  2569. if ((hadc->Instance->CR & (ADC_CR_ADCAL | ADC_CR_JADSTP | ADC_CR_ADSTP | ADC_CR_JADSTART | ADC_CR_ADSTART | ADC_CR_ADDIS | ADC_CR_ADEN)) != 0UL)
  2570. {
  2571. /* Update ADC state machine to error */
  2572. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2573. /* Set ADC error code to ADC peripheral internal error */
  2574. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2575. return HAL_ERROR;
  2576. }
  2577. /* Enable the ADC peripheral */
  2578. LL_ADC_Enable(hadc->Instance);
  2579. /* Wait for ADC effectively enabled */
  2580. tickstart = HAL_GetTick();
  2581. while (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_RDY) == 0UL)
  2582. {
  2583. /* If ADEN bit is set less than 4 ADC clock cycles after the ADCAL bit
  2584. has been cleared (after a calibration), ADEN bit is reset by the
  2585. calibration logic.
  2586. The workaround is to continue setting ADEN until ADRDY is becomes 1.
  2587. Additionally, ADC_ENABLE_TIMEOUT is defined to encompass this
  2588. 4 ADC clock cycle duration */
  2589. /* Note: Test of ADC enabled required due to hardware constraint to */
  2590. /* not enable ADC if already enabled. */
  2591. if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
  2592. {
  2593. LL_ADC_Enable(hadc->Instance);
  2594. }
  2595. if ((HAL_GetTick() - tickstart) > ADC_ENABLE_TIMEOUT)
  2596. {
  2597. /* Update ADC state machine to error */
  2598. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2599. /* Set ADC error code to ADC peripheral internal error */
  2600. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2601. return HAL_ERROR;
  2602. }
  2603. }
  2604. }
  2605. /* Return HAL status */
  2606. return HAL_OK;
  2607. }
  2608. /**
  2609. * @brief Disable the selected ADC.
  2610. * @note Prerequisite condition to use this function: ADC conversions must be
  2611. * stopped.
  2612. * @param hadc ADC handle
  2613. * @retval HAL status.
  2614. */
  2615. HAL_StatusTypeDef ADC_Disable(ADC_HandleTypeDef *hadc)
  2616. {
  2617. uint32_t tickstart;
  2618. const uint32_t tmp_adc_is_disable_on_going = LL_ADC_IsDisableOngoing(hadc->Instance);
  2619. /* Verification if ADC is not already disabled: */
  2620. /* Note: forbidden to disable ADC (set bit ADC_CR_ADDIS) if ADC is already */
  2621. /* disabled. */
  2622. if ((LL_ADC_IsEnabled(hadc->Instance) != 0UL)
  2623. && (tmp_adc_is_disable_on_going == 0UL)
  2624. )
  2625. {
  2626. /* Check if conditions to disable the ADC are fulfilled */
  2627. if ((hadc->Instance->CR & (ADC_CR_JADSTART | ADC_CR_ADSTART | ADC_CR_ADEN)) == ADC_CR_ADEN)
  2628. {
  2629. /* Disable the ADC peripheral */
  2630. LL_ADC_Disable(hadc->Instance);
  2631. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOSMP | ADC_FLAG_RDY));
  2632. }
  2633. else
  2634. {
  2635. /* Update ADC state machine to error */
  2636. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2637. /* Set ADC error code to ADC peripheral internal error */
  2638. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2639. return HAL_ERROR;
  2640. }
  2641. /* Wait for ADC effectively disabled */
  2642. /* Get tick count */
  2643. tickstart = HAL_GetTick();
  2644. while ((hadc->Instance->CR & ADC_CR_ADEN) != 0UL)
  2645. {
  2646. if ((HAL_GetTick() - tickstart) > ADC_DISABLE_TIMEOUT)
  2647. {
  2648. /* Update ADC state machine to error */
  2649. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2650. /* Set ADC error code to ADC peripheral internal error */
  2651. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2652. return HAL_ERROR;
  2653. }
  2654. }
  2655. }
  2656. /* Return HAL status */
  2657. return HAL_OK;
  2658. }
  2659. /**
  2660. * @brief DMA transfer complete callback.
  2661. * @param hdma pointer to DMA handle.
  2662. * @retval None
  2663. */
  2664. void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma)
  2665. {
  2666. /* Retrieve ADC handle corresponding to current DMA handle */
  2667. ADC_HandleTypeDef *hadc = (ADC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2668. /* Update state machine on conversion status if not in error state */
  2669. if ((hadc->State & (HAL_ADC_STATE_ERROR_INTERNAL | HAL_ADC_STATE_ERROR_DMA)) == 0UL)
  2670. {
  2671. /* Set ADC state */
  2672. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  2673. /* Determine whether any further conversion upcoming on group regular */
  2674. /* by external trigger, continuous mode or scan sequence on going */
  2675. /* to disable interruption. */
  2676. /* Is it the end of the regular sequence ? */
  2677. if ((hadc->Instance->ISR & ADC_FLAG_EOS) != 0UL)
  2678. {
  2679. /* Are conversions software-triggered ? */
  2680. if (LL_ADC_REG_IsTriggerSourceSWStart(hadc->Instance) != 0UL)
  2681. {
  2682. /* Is CONT bit set ? */
  2683. if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_CONT) == 0UL)
  2684. {
  2685. /* CONT bit is not set, no more conversions expected */
  2686. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  2687. if ((hadc->State & HAL_ADC_STATE_INJ_BUSY) == 0UL)
  2688. {
  2689. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  2690. }
  2691. }
  2692. }
  2693. }
  2694. else
  2695. {
  2696. /* DMA End of Transfer interrupt was triggered but conversions sequence
  2697. is not over. If DMACFG is set to 0, conversions are stopped. */
  2698. if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_DMACFG) == 0UL)
  2699. {
  2700. /* DMACFG bit is not set, conversions are stopped. */
  2701. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  2702. if ((hadc->State & HAL_ADC_STATE_INJ_BUSY) == 0UL)
  2703. {
  2704. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  2705. }
  2706. }
  2707. }
  2708. /* Conversion complete callback */
  2709. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2710. hadc->ConvCpltCallback(hadc);
  2711. #else
  2712. HAL_ADC_ConvCpltCallback(hadc);
  2713. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2714. }
  2715. else /* DMA and-or internal error occurred */
  2716. {
  2717. if ((hadc->State & HAL_ADC_STATE_ERROR_INTERNAL) != 0UL)
  2718. {
  2719. /* Call HAL ADC Error Callback function */
  2720. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2721. hadc->ErrorCallback(hadc);
  2722. #else
  2723. HAL_ADC_ErrorCallback(hadc);
  2724. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2725. }
  2726. else
  2727. {
  2728. /* Call ADC DMA error callback */
  2729. hadc->DMA_Handle->XferErrorCallback(hdma);
  2730. }
  2731. }
  2732. }
  2733. /**
  2734. * @brief DMA half transfer complete callback.
  2735. * @param hdma pointer to DMA handle.
  2736. * @retval None
  2737. */
  2738. void ADC_DMAHalfConvCplt(DMA_HandleTypeDef *hdma)
  2739. {
  2740. /* Retrieve ADC handle corresponding to current DMA handle */
  2741. ADC_HandleTypeDef *hadc = (ADC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2742. /* Half conversion callback */
  2743. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2744. hadc->ConvHalfCpltCallback(hadc);
  2745. #else
  2746. HAL_ADC_ConvHalfCpltCallback(hadc);
  2747. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2748. }
  2749. /**
  2750. * @brief DMA error callback.
  2751. * @param hdma pointer to DMA handle.
  2752. * @retval None
  2753. */
  2754. void ADC_DMAError(DMA_HandleTypeDef *hdma)
  2755. {
  2756. /* Retrieve ADC handle corresponding to current DMA handle */
  2757. ADC_HandleTypeDef *hadc = (ADC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2758. /* Set ADC state */
  2759. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  2760. /* Set ADC error code to DMA error */
  2761. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_DMA);
  2762. /* Error callback */
  2763. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2764. hadc->ErrorCallback(hadc);
  2765. #else
  2766. HAL_ADC_ErrorCallback(hadc);
  2767. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2768. }
  2769. /**
  2770. * @}
  2771. */
  2772. #endif /* HAL_ADC_MODULE_ENABLED */
  2773. /**
  2774. * @}
  2775. */
  2776. /**
  2777. * @}
  2778. */
  2779. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/