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.
 
 
 

3982 lines
157 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 Converter (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. #if defined (ADC_SUPPORT_2_5_MSPS)
  282. /* Register CHSELR bits corresponding to ranks 2 to 8 . */
  283. #define ADC_CHSELR_SQ2_TO_SQ8 (ADC_CHSELR_SQ2 | ADC_CHSELR_SQ3 | ADC_CHSELR_SQ4 | ADC_CHSELR_SQ5 | ADC_CHSELR_SQ6 | ADC_CHSELR_SQ7 | ADC_CHSELR_SQ8)
  284. #endif
  285. /**
  286. * @}
  287. */
  288. /* Private macro -------------------------------------------------------------*/
  289. /* Private variables ---------------------------------------------------------*/
  290. /* Private function prototypes -----------------------------------------------*/
  291. /* Exported functions --------------------------------------------------------*/
  292. /** @defgroup ADC_Exported_Functions ADC Exported Functions
  293. * @{
  294. */
  295. /** @defgroup ADC_Exported_Functions_Group1 Initialization and de-initialization functions
  296. * @brief ADC Initialization and Configuration functions
  297. *
  298. @verbatim
  299. ===============================================================================
  300. ##### Initialization and de-initialization functions #####
  301. ===============================================================================
  302. [..] This section provides functions allowing to:
  303. (+) Initialize and configure the ADC.
  304. (+) De-initialize the ADC.
  305. @endverbatim
  306. * @{
  307. */
  308. /**
  309. * @brief Initialize the ADC peripheral and regular group according to
  310. * parameters specified in structure "ADC_InitTypeDef".
  311. * @note As prerequisite, ADC clock must be configured at RCC top level
  312. * (refer to description of RCC configuration for ADC
  313. * in header of this file).
  314. * @note Possibility to update parameters on the fly:
  315. * This function initializes the ADC MSP (HAL_ADC_MspInit()) only when
  316. * coming from ADC state reset. Following calls to this function can
  317. * be used to reconfigure some parameters of ADC_InitTypeDef
  318. * structure on the fly, without modifying MSP configuration. If ADC
  319. * MSP has to be modified again, HAL_ADC_DeInit() must be called
  320. * before HAL_ADC_Init().
  321. * The setting of these parameters is conditioned to ADC state.
  322. * For parameters constraints, see comments of structure
  323. * "ADC_InitTypeDef".
  324. * @note This function configures the ADC within 2 scopes: scope of entire
  325. * ADC and scope of regular group. For parameters details, see comments
  326. * of structure "ADC_InitTypeDef".
  327. * @param hadc ADC handle
  328. * @retval HAL status
  329. */
  330. HAL_StatusTypeDef HAL_ADC_Init(ADC_HandleTypeDef *hadc)
  331. {
  332. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  333. uint32_t tmpCFGR = 0UL;
  334. uint32_t tmp_adc_reg_is_conversion_on_going;
  335. __IO uint32_t wait_loop_index = 0UL;
  336. #if defined (ADC_SUPPORT_2_5_MSPS)
  337. uint32_t tmpCFGR2 = 0UL;
  338. #else
  339. uint32_t tmp_adc_is_conversion_on_going_regular;
  340. uint32_t tmp_adc_is_conversion_on_going_injected;
  341. #endif
  342. /* Check ADC handle */
  343. if (hadc == NULL)
  344. {
  345. return HAL_ERROR;
  346. }
  347. /* Check the parameters */
  348. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  349. assert_param(IS_ADC_CLOCKPRESCALER(hadc->Init.ClockPrescaler));
  350. assert_param(IS_ADC_RESOLUTION(hadc->Init.Resolution));
  351. assert_param(IS_ADC_DATA_ALIGN(hadc->Init.DataAlign));
  352. assert_param(IS_ADC_SCAN_MODE(hadc->Init.ScanConvMode));
  353. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
  354. assert_param(IS_ADC_EXTTRIG_EDGE(hadc->Init.ExternalTrigConvEdge));
  355. assert_param(IS_ADC_EXTTRIG(hadc, hadc->Init.ExternalTrigConv));
  356. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DMAContinuousRequests));
  357. assert_param(IS_ADC_EOC_SELECTION(hadc->Init.EOCSelection));
  358. assert_param(IS_ADC_OVERRUN(hadc->Init.Overrun));
  359. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.LowPowerAutoWait));
  360. #if defined (ADC_SUPPORT_2_5_MSPS)
  361. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.LowPowerAutoPowerOff));
  362. assert_param(IS_ADC_SAMPLE_TIME(hadc->Init.SamplingTimeCommon1));
  363. assert_param(IS_ADC_SAMPLE_TIME(hadc->Init.SamplingTimeCommon2));
  364. assert_param(IS_ADC_TRIGGER_FREQ(hadc->Init.TriggerFrequencyMode));
  365. if(hadc->Init.ScanConvMode != ADC_SCAN_DISABLE)
  366. {
  367. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DiscontinuousConvMode));
  368. if(hadc->Init.ScanConvMode == ADC_SCAN_ENABLE)
  369. {
  370. assert_param(IS_ADC_REGULAR_NB_CONV(hadc->Init.NbrOfConversion));
  371. }
  372. }
  373. #else
  374. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.OversamplingMode));
  375. if (hadc->Init.ScanConvMode != ADC_SCAN_DISABLE)
  376. {
  377. assert_param(IS_ADC_REGULAR_NB_CONV(hadc->Init.NbrOfConversion));
  378. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DiscontinuousConvMode));
  379. if (hadc->Init.DiscontinuousConvMode == ENABLE)
  380. {
  381. assert_param(IS_ADC_REGULAR_DISCONT_NUMBER(hadc->Init.NbrOfDiscConversion));
  382. }
  383. }
  384. /* DISCEN and CONT bits cannot be set at the same time */
  385. assert_param(!((hadc->Init.DiscontinuousConvMode == ENABLE) && (hadc->Init.ContinuousConvMode == ENABLE)));
  386. #endif /* ADC_SUPPORT_2_5_MSPS */
  387. /* Actions performed only if ADC is coming from state reset: */
  388. /* - Initialization of ADC MSP */
  389. if (hadc->State == HAL_ADC_STATE_RESET)
  390. {
  391. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  392. /* Init the ADC Callback settings */
  393. hadc->ConvCpltCallback = HAL_ADC_ConvCpltCallback; /* Legacy weak callback */
  394. hadc->ConvHalfCpltCallback = HAL_ADC_ConvHalfCpltCallback; /* Legacy weak callback */
  395. hadc->LevelOutOfWindowCallback = HAL_ADC_LevelOutOfWindowCallback; /* Legacy weak callback */
  396. hadc->ErrorCallback = HAL_ADC_ErrorCallback; /* Legacy weak callback */
  397. #if !defined (ADC_SUPPORT_2_5_MSPS)
  398. hadc->InjectedConvCpltCallback = HAL_ADCEx_InjectedConvCpltCallback; /* Legacy weak callback */
  399. hadc->InjectedQueueOverflowCallback = HAL_ADCEx_InjectedQueueOverflowCallback; /* Legacy weak callback */
  400. #endif
  401. hadc->LevelOutOfWindow2Callback = HAL_ADCEx_LevelOutOfWindow2Callback; /* Legacy weak callback */
  402. hadc->LevelOutOfWindow3Callback = HAL_ADCEx_LevelOutOfWindow3Callback; /* Legacy weak callback */
  403. hadc->EndOfSamplingCallback = HAL_ADCEx_EndOfSamplingCallback; /* Legacy weak callback */
  404. if (hadc->MspInitCallback == NULL)
  405. {
  406. hadc->MspInitCallback = HAL_ADC_MspInit; /* Legacy weak MspInit */
  407. }
  408. /* Init the low level hardware */
  409. hadc->MspInitCallback(hadc);
  410. #else
  411. /* Init the low level hardware */
  412. HAL_ADC_MspInit(hadc);
  413. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  414. /* Set ADC error code to none */
  415. ADC_CLEAR_ERRORCODE(hadc);
  416. /* Initialize Lock */
  417. hadc->Lock = HAL_UNLOCKED;
  418. }
  419. /* - Exit from deep power-down mode and ADC voltage regulator enable */
  420. #if defined (ADC_SUPPORT_2_5_MSPS)
  421. /* Feature "deep power-down" not available on ADC peripheral of this STM32WB device */
  422. #else
  423. if (LL_ADC_IsDeepPowerDownEnabled(hadc->Instance) != 0UL)
  424. {
  425. /* Disable ADC deep power down mode */
  426. LL_ADC_DisableDeepPowerDown(hadc->Instance);
  427. /* System was in deep power down mode, calibration must
  428. be relaunched or a previously saved calibration factor
  429. re-applied once the ADC voltage regulator is enabled */
  430. }
  431. #endif
  432. if (LL_ADC_IsInternalRegulatorEnabled(hadc->Instance) == 0UL)
  433. {
  434. /* Enable ADC internal voltage regulator */
  435. LL_ADC_EnableInternalRegulator(hadc->Instance);
  436. /* Note: Variable divided by 2 to compensate partially */
  437. /* CPU processing cycles, scaling in us split to not */
  438. /* exceed 32 bits register capacity and handle low frequency. */
  439. wait_loop_index = ((LL_ADC_DELAY_INTERNAL_REGUL_STAB_US / 10UL) * (SystemCoreClock / (100000UL * 2UL)));
  440. while (wait_loop_index != 0UL)
  441. {
  442. wait_loop_index--;
  443. }
  444. }
  445. /* Verification that ADC voltage regulator is correctly enabled, whether */
  446. /* or not ADC is coming from state reset (if any potential problem of */
  447. /* clocking, voltage regulator would not be enabled). */
  448. if (LL_ADC_IsInternalRegulatorEnabled(hadc->Instance) == 0UL)
  449. {
  450. /* Update ADC state machine to error */
  451. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  452. /* Set ADC error code to ADC peripheral internal error */
  453. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  454. tmp_hal_status = HAL_ERROR;
  455. }
  456. /* Configuration of ADC parameters if previous preliminary actions are */
  457. /* correctly completed and if there is no conversion on going on regular */
  458. /* group (ADC may already be enabled at this point if HAL_ADC_Init() is */
  459. /* called to update a parameter on the fly). */
  460. tmp_adc_reg_is_conversion_on_going = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
  461. if (((hadc->State & HAL_ADC_STATE_ERROR_INTERNAL) == 0UL)
  462. && (tmp_adc_reg_is_conversion_on_going == 0UL)
  463. )
  464. {
  465. /* Set ADC state */
  466. ADC_STATE_CLR_SET(hadc->State,
  467. HAL_ADC_STATE_REG_BUSY,
  468. HAL_ADC_STATE_BUSY_INTERNAL);
  469. #if defined (ADC_SUPPORT_2_5_MSPS)
  470. /* Configuration of common ADC parameters */
  471. /* Parameters update conditioned to ADC state: */
  472. /* Parameters that can be updated only when ADC is disabled: */
  473. /* - Internal voltage regulator (no parameter in HAL ADC init structure) */
  474. /* - Clock configuration */
  475. /* - ADC resolution */
  476. /* Note: If low power mode AutoPowerOff is enabled, ADC enable */
  477. /* and disable phases are performed automatically by hardware */
  478. /* (in this case, flag ADC_FLAG_RDY is not set). */
  479. if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
  480. {
  481. /* Some parameters of this register are not reset, since they are set */
  482. /* by other functions and must be kept in case of usage of this */
  483. /* function on the fly (update of a parameter of ADC_InitTypeDef */
  484. /* without needing to reconfigure all other ADC groups/channels */
  485. /* parameters): */
  486. /* - internal measurement paths: Vbat, temperature sensor, Vref */
  487. /* (set into HAL_ADC_ConfigChannel() ) */
  488. /* Configuration of ADC resolution */
  489. MODIFY_REG(hadc->Instance->CFGR1,
  490. ADC_CFGR1_RES ,
  491. hadc->Init.Resolution );
  492. /* Configuration of ADC: */
  493. /* - clock */
  494. /* - oversampling enable */
  495. /* - oversampling ratio */
  496. /* - oversampling shift */
  497. /* - oversampling discontinuous mode (triggered mode) */
  498. /* - trigger frequency mode */
  499. tmpCFGR2 |= ( (hadc->Init.ClockPrescaler & ADC_CFGR2_CKMODE) |
  500. hadc->Init.TriggerFrequencyMode
  501. );
  502. MODIFY_REG(hadc->Instance->CFGR2,
  503. ADC_CFGR2_CKMODE |
  504. ADC_CFGR2_LFTRIG ,
  505. tmpCFGR2 );
  506. /* Configuration of ADC clock mode: asynchronous clock source */
  507. /* with selectable prescaler. */
  508. if (((hadc->Init.ClockPrescaler) != ADC_CLOCK_SYNC_PCLK_DIV1) &&
  509. ((hadc->Init.ClockPrescaler) != ADC_CLOCK_SYNC_PCLK_DIV2) &&
  510. ((hadc->Init.ClockPrescaler) != ADC_CLOCK_SYNC_PCLK_DIV4))
  511. {
  512. MODIFY_REG(ADC1_COMMON->CCR ,
  513. ADC_CCR_PRESC ,
  514. hadc->Init.ClockPrescaler & ADC_CCR_PRESC );
  515. }
  516. }
  517. /* Configuration of ADC: */
  518. /* - discontinuous mode */
  519. /* - LowPowerAutoWait mode */
  520. /* - LowPowerAutoPowerOff mode */
  521. /* - continuous conversion mode */
  522. /* - overrun */
  523. /* - external trigger to start conversion */
  524. /* - external trigger polarity */
  525. /* - data alignment */
  526. /* - resolution */
  527. /* - scan direction */
  528. /* - DMA continuous request */
  529. tmpCFGR |= (ADC_CFGR_AUTOWAIT((uint32_t)hadc->Init.LowPowerAutoWait) |
  530. ADC_CFGR_AUTOOFF((uint32_t)hadc->Init.LowPowerAutoPowerOff) |
  531. ADC_CFGR_CONTINUOUS((uint32_t)hadc->Init.ContinuousConvMode) |
  532. ADC_CFGR_OVERRUN(hadc->Init.Overrun) |
  533. hadc->Init.DataAlign |
  534. ADC_SCAN_SEQ_MODE(hadc->Init.ScanConvMode) |
  535. ADC_CFGR_DMACONTREQ((uint32_t)hadc->Init.DMAContinuousRequests) );
  536. /* Update setting of discontinuous mode only if continuous mode is disabled */
  537. if (hadc->Init.DiscontinuousConvMode == ENABLE)
  538. {
  539. if (hadc->Init.ContinuousConvMode == DISABLE)
  540. {
  541. /* Enable the selected ADC group regular discontinuous mode */
  542. tmpCFGR |= ADC_CFGR1_DISCEN;
  543. }
  544. else
  545. {
  546. /* ADC regular group discontinuous was intended to be enabled, */
  547. /* but ADC regular group modes continuous and sequencer discontinuous */
  548. /* cannot be enabled simultaneously. */
  549. /* Update ADC state machine to error */
  550. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  551. /* Set ADC error code to ADC peripheral internal error */
  552. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  553. }
  554. }
  555. /* Enable external trigger if trigger selection is different of software */
  556. /* start. */
  557. /* Note: This configuration keeps the hardware feature of parameter */
  558. /* ExternalTrigConvEdge "trigger edge none" equivalent to */
  559. /* software start. */
  560. if (hadc->Init.ExternalTrigConv != ADC_SOFTWARE_START)
  561. {
  562. tmpCFGR |= ( (hadc->Init.ExternalTrigConv & ADC_CFGR1_EXTSEL) |
  563. hadc->Init.ExternalTrigConvEdge );
  564. }
  565. /* Update ADC configuration register with previous settings */
  566. MODIFY_REG(hadc->Instance->CFGR1,
  567. ADC_CFGR1_DISCEN |
  568. ADC_CFGR1_AUTOFF |
  569. ADC_CFGR1_WAIT |
  570. ADC_CFGR1_CONT |
  571. ADC_CFGR1_OVRMOD |
  572. ADC_CFGR1_EXTSEL |
  573. ADC_CFGR1_EXTEN |
  574. ADC_CFGR1_ALIGN |
  575. ADC_CFGR1_SCANDIR |
  576. ADC_CFGR1_DMACFG ,
  577. tmpCFGR );
  578. /* Channel sampling time configuration */
  579. LL_ADC_SetSamplingTimeCommonChannels(hadc->Instance, LL_ADC_SAMPLINGTIME_COMMON_1, hadc->Init.SamplingTimeCommon1);
  580. LL_ADC_SetSamplingTimeCommonChannels(hadc->Instance, LL_ADC_SAMPLINGTIME_COMMON_2, hadc->Init.SamplingTimeCommon2);
  581. /* Configuration of regular group sequencer: */
  582. /* - if scan mode is disabled, regular channels sequence length is set to */
  583. /* 0x00: 1 channel converted (channel on regular rank 1) */
  584. /* Parameter "NbrOfConversion" is discarded. */
  585. /* Note: Scan mode is not present by hardware on this device, but */
  586. /* emulated by software for alignment over all STM32 devices. */
  587. /* - if scan mode is enabled, regular channels sequence length is set to */
  588. /* parameter "NbrOfConversion". */
  589. /* Channels must be configured into each rank using function */
  590. /* "HAL_ADC_ConfigChannel()". */
  591. if(hadc->Init.ScanConvMode == ADC_SCAN_DISABLE)
  592. {
  593. /* Set sequencer scan length by clearing ranks above rank 1 */
  594. /* and do not modify rank 1 value. */
  595. SET_BIT(hadc->Instance->CHSELR,
  596. ADC_CHSELR_SQ2_TO_SQ8);
  597. }
  598. else if(hadc->Init.ScanConvMode == ADC_SCAN_ENABLE)
  599. {
  600. /* Count number of ranks available in HAL ADC handle variable */
  601. uint32_t ADCGroupRegularSequencerRanksCount;
  602. /* Parse all ranks from 1 to 8 */
  603. for(ADCGroupRegularSequencerRanksCount = 0UL; ADCGroupRegularSequencerRanksCount < (8UL); ADCGroupRegularSequencerRanksCount++)
  604. {
  605. /* Check each sequencer rank until value of end of sequence */
  606. if(((hadc->ADCGroupRegularSequencerRanks >> (ADCGroupRegularSequencerRanksCount * 4UL)) & ADC_CHSELR_SQ1) == ADC_CHSELR_SQ1)
  607. {
  608. break;
  609. }
  610. }
  611. if(ADCGroupRegularSequencerRanksCount == 1UL)
  612. {
  613. /* Set ADC group regular sequencer: */
  614. /* Set sequencer scan length by clearing ranks above rank 1 */
  615. /* and do not modify rank 1 value. */
  616. SET_BIT(hadc->Instance->CHSELR,
  617. ADC_CHSELR_SQ2_TO_SQ8);
  618. }
  619. else
  620. {
  621. /* Set ADC group regular sequencer: */
  622. /* - Set ADC group regular sequencer to value memorized */
  623. /* in HAL ADC handle */
  624. /* Note: This value maybe be initialized at a unknown value, */
  625. /* therefore afer the first call of "HAL_ADC_Init()", */
  626. /* each rank corresponding to parameter "NbrOfConversion" */
  627. /* must be set using "HAL_ADC_ConfigChannel()". */
  628. /* - Set sequencer scan length by clearing ranks above maximum rank */
  629. /* and do not modify other ranks value. */
  630. MODIFY_REG(hadc->Instance->CHSELR,
  631. ADC_CHSELR_SQ_ALL,
  632. (ADC_CHSELR_SQ2_TO_SQ8 << (((hadc->Init.NbrOfConversion - 1UL) * ADC_REGULAR_RANK_2) & 0x1FUL)) | (hadc->ADCGroupRegularSequencerRanks)
  633. );
  634. }
  635. }
  636. /* Check back that ADC registers have effectively been configured to */
  637. /* ensure of no potential problem of ADC core peripheral clocking. */
  638. /* Check through register CFGR1 (excluding analog watchdog configuration: */
  639. /* set into separate dedicated function, and bits of ADC resolution set */
  640. /* out of temporary variable 'tmpCFGR'). */
  641. if ((hadc->Instance->CFGR1 & ~(ADC_CFGR1_AWD1CH | ADC_CFGR1_AWD1EN | ADC_CFGR1_AWD1SGL | ADC_CFGR1_RES))
  642. == tmpCFGR)
  643. {
  644. /* Set ADC error code to none */
  645. ADC_CLEAR_ERRORCODE(hadc);
  646. /* Set the ADC state */
  647. ADC_STATE_CLR_SET(hadc->State,
  648. HAL_ADC_STATE_BUSY_INTERNAL,
  649. HAL_ADC_STATE_READY);
  650. }
  651. else
  652. {
  653. /* Update ADC state machine to error */
  654. ADC_STATE_CLR_SET(hadc->State,
  655. HAL_ADC_STATE_BUSY_INTERNAL,
  656. HAL_ADC_STATE_ERROR_INTERNAL);
  657. /* Set ADC error code to ADC peripheral internal error */
  658. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  659. tmp_hal_status = HAL_ERROR;
  660. }
  661. #else
  662. /* Configuration of common ADC parameters */
  663. /* Parameters update conditioned to ADC state: */
  664. /* Parameters that can be updated only when ADC is disabled: */
  665. /* - clock configuration */
  666. if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
  667. {
  668. if (__LL_ADC_IS_ENABLED_ALL_COMMON_INSTANCE(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) == 0UL)
  669. {
  670. /* Reset configuration of ADC common register CCR: */
  671. /* */
  672. /* - ADC clock mode and ACC prescaler (CKMODE and PRESC bits)are set */
  673. /* according to adc->Init.ClockPrescaler. It selects the clock */
  674. /* source and sets the clock division factor. */
  675. /* */
  676. /* Some parameters of this register are not reset, since they are set */
  677. /* by other functions and must be kept in case of usage of this */
  678. /* function on the fly (update of a parameter of ADC_InitTypeDef */
  679. /* without needing to reconfigure all other ADC groups/channels */
  680. /* parameters): */
  681. /* - when multimode feature is available, multimode-related */
  682. /* parameters: MDMA, DMACFG, DELAY, DUAL (set by API */
  683. /* HAL_ADCEx_MultiModeConfigChannel() ) */
  684. /* - internal measurement paths: Vbat, temperature sensor, Vref */
  685. /* (set into HAL_ADC_ConfigChannel() or */
  686. /* HAL_ADCEx_InjectedConfigChannel() ) */
  687. LL_ADC_SetCommonClock(__LL_ADC_COMMON_INSTANCE(hadc->Instance), hadc->Init.ClockPrescaler);
  688. }
  689. }
  690. /* Configuration of ADC: */
  691. /* - resolution Init.Resolution */
  692. /* - data alignment Init.DataAlign */
  693. /* - external trigger to start conversion Init.ExternalTrigConv */
  694. /* - external trigger polarity Init.ExternalTrigConvEdge */
  695. /* - continuous conversion mode Init.ContinuousConvMode */
  696. /* - overrun Init.Overrun */
  697. /* - discontinuous mode Init.DiscontinuousConvMode */
  698. /* - discontinuous mode channel count Init.NbrOfDiscConversion */
  699. tmpCFGR |= (ADC_CFGR_CONTINUOUS((uint32_t)hadc->Init.ContinuousConvMode) |
  700. hadc->Init.Overrun |
  701. hadc->Init.DataAlign |
  702. hadc->Init.Resolution |
  703. ADC_CFGR_REG_DISCONTINUOUS((uint32_t)hadc->Init.DiscontinuousConvMode));
  704. if (hadc->Init.DiscontinuousConvMode == ENABLE)
  705. {
  706. tmpCFGR |= ADC_CFGR_DISCONTINUOUS_NUM(hadc->Init.NbrOfDiscConversion);
  707. }
  708. /* Enable external trigger if trigger selection is different of software */
  709. /* start. */
  710. /* Note: This configuration keeps the hardware feature of parameter */
  711. /* ExternalTrigConvEdge "trigger edge none" equivalent to */
  712. /* software start. */
  713. if (hadc->Init.ExternalTrigConv != ADC_SOFTWARE_START)
  714. {
  715. tmpCFGR |= ((hadc->Init.ExternalTrigConv & ADC_CFGR_EXTSEL)
  716. | hadc->Init.ExternalTrigConvEdge
  717. );
  718. }
  719. /* Update Configuration Register CFGR */
  720. MODIFY_REG(hadc->Instance->CFGR, ADC_CFGR_FIELDS_1, tmpCFGR);
  721. /* Parameters update conditioned to ADC state: */
  722. /* Parameters that can be updated when ADC is disabled or enabled without */
  723. /* conversion on going on regular and injected groups: */
  724. /* - DMA continuous request Init.DMAContinuousRequests */
  725. /* - LowPowerAutoWait feature Init.LowPowerAutoWait */
  726. /* - Oversampling parameters Init.Oversampling */
  727. tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
  728. tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
  729. if ((tmp_adc_is_conversion_on_going_regular == 0UL)
  730. && (tmp_adc_is_conversion_on_going_injected == 0UL)
  731. )
  732. {
  733. tmpCFGR = (ADC_CFGR_DFSDM(hadc) |
  734. ADC_CFGR_AUTOWAIT((uint32_t)hadc->Init.LowPowerAutoWait) |
  735. ADC_CFGR_DMACONTREQ((uint32_t)hadc->Init.DMAContinuousRequests));
  736. MODIFY_REG(hadc->Instance->CFGR, ADC_CFGR_FIELDS_2, tmpCFGR);
  737. if (hadc->Init.OversamplingMode == ENABLE)
  738. {
  739. assert_param(IS_ADC_OVERSAMPLING_RATIO(hadc->Init.Oversampling.Ratio));
  740. assert_param(IS_ADC_RIGHT_BIT_SHIFT(hadc->Init.Oversampling.RightBitShift));
  741. assert_param(IS_ADC_TRIGGERED_OVERSAMPLING_MODE(hadc->Init.Oversampling.TriggeredMode));
  742. assert_param(IS_ADC_REGOVERSAMPLING_MODE(hadc->Init.Oversampling.OversamplingStopReset));
  743. /* Configuration of Oversampler: */
  744. /* - Oversampling Ratio */
  745. /* - Right bit shift */
  746. /* - Triggered mode */
  747. /* - Oversampling mode (continued/resumed) */
  748. MODIFY_REG(hadc->Instance->CFGR2,
  749. ADC_CFGR2_OVSR |
  750. ADC_CFGR2_OVSS |
  751. ADC_CFGR2_TROVS |
  752. ADC_CFGR2_ROVSM,
  753. ADC_CFGR2_ROVSE |
  754. hadc->Init.Oversampling.Ratio |
  755. hadc->Init.Oversampling.RightBitShift |
  756. hadc->Init.Oversampling.TriggeredMode |
  757. hadc->Init.Oversampling.OversamplingStopReset
  758. );
  759. }
  760. else
  761. {
  762. /* Disable ADC oversampling scope on ADC group regular */
  763. CLEAR_BIT(hadc->Instance->CFGR2, ADC_CFGR2_ROVSE);
  764. }
  765. }
  766. /* Configuration of regular group sequencer: */
  767. /* - if scan mode is disabled, regular channels sequence length is set to */
  768. /* 0x00: 1 channel converted (channel on regular rank 1) */
  769. /* Parameter "NbrOfConversion" is discarded. */
  770. /* Note: Scan mode is not present by hardware on this device, but */
  771. /* emulated by software for alignment over all STM32 devices. */
  772. /* - if scan mode is enabled, regular channels sequence length is set to */
  773. /* parameter "NbrOfConversion". */
  774. if (hadc->Init.ScanConvMode == ADC_SCAN_ENABLE)
  775. {
  776. /* Set number of ranks in regular group sequencer */
  777. MODIFY_REG(hadc->Instance->SQR1, ADC_SQR1_L, (hadc->Init.NbrOfConversion - (uint8_t)1));
  778. }
  779. else
  780. {
  781. CLEAR_BIT(hadc->Instance->SQR1, ADC_SQR1_L);
  782. }
  783. /* Initialize the ADC state */
  784. /* Clear HAL_ADC_STATE_BUSY_INTERNAL bit, set HAL_ADC_STATE_READY bit */
  785. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_BUSY_INTERNAL, HAL_ADC_STATE_READY);
  786. #endif /* ADC_SUPPORT_2_5_MSPS */
  787. }
  788. else
  789. {
  790. /* Update ADC state machine to error */
  791. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  792. tmp_hal_status = HAL_ERROR;
  793. }
  794. /* Return function status */
  795. return tmp_hal_status;
  796. }
  797. /**
  798. * @brief Deinitialize the ADC peripheral registers to their default reset
  799. * values, with deinitialization of the ADC MSP.
  800. * @note For devices with several ADCs: reset of ADC common registers is done
  801. * only if all ADCs sharing the same common group are disabled.
  802. * (function "HAL_ADC_MspDeInit()" is also called under the same conditions:
  803. * all ADC instances use the same core clock at RCC level, disabling
  804. * the core clock reset all ADC instances).
  805. * If this is not the case, reset of these common parameters reset is
  806. * bypassed without error reporting: it can be the intended behavior in
  807. * case of reset of a single ADC while the other ADCs sharing the same
  808. * common group is still running.
  809. * @note By default, HAL_ADC_DeInit() set ADC in mode deep power-down:
  810. * this saves more power by reducing leakage currents
  811. * and is particularly interesting before entering MCU low-power modes.
  812. * @param hadc ADC handle
  813. * @retval HAL status
  814. */
  815. HAL_StatusTypeDef HAL_ADC_DeInit(ADC_HandleTypeDef *hadc)
  816. {
  817. HAL_StatusTypeDef tmp_hal_status;
  818. /* Check ADC handle */
  819. if (hadc == NULL)
  820. {
  821. return HAL_ERROR;
  822. }
  823. /* Check the parameters */
  824. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  825. /* Set ADC state */
  826. SET_BIT(hadc->State, HAL_ADC_STATE_BUSY_INTERNAL);
  827. /* Stop potential conversion on going */
  828. #if defined (ADC_SUPPORT_2_5_MSPS)
  829. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_GROUP);
  830. #else
  831. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_INJECTED_GROUP);
  832. #endif /* ADC_SUPPORT_2_5_MSPS */
  833. #if defined (ADC_SUPPORT_2_5_MSPS)
  834. /* Feature "ADC group injected" not available on ADC peripheral of this STM32WB device */
  835. #else
  836. /* Disable ADC peripheral if conversions are effectively stopped */
  837. /* Flush register JSQR: reset the queue sequencer when injected */
  838. /* queue sequencer is enabled and ADC disabled. */
  839. /* The software and hardware triggers of the injected sequence are both */
  840. /* internally disabled just after the completion of the last valid */
  841. /* injected sequence. */
  842. SET_BIT(hadc->Instance->CFGR, ADC_CFGR_JQM);
  843. #endif
  844. /* Disable ADC peripheral if conversions are effectively stopped */
  845. if (tmp_hal_status == HAL_OK)
  846. {
  847. /* Disable the ADC peripheral */
  848. tmp_hal_status = ADC_Disable(hadc);
  849. /* Check if ADC is effectively disabled */
  850. if (tmp_hal_status == HAL_OK)
  851. {
  852. /* Change ADC state */
  853. hadc->State = HAL_ADC_STATE_READY;
  854. }
  855. }
  856. /* Note: HAL ADC deInit is done independently of ADC conversion stop */
  857. /* and disable return status. In case of status fail, attempt to */
  858. /* perform deinitialization anyway and it is up user code in */
  859. /* in HAL_ADC_MspDeInit() to reset the ADC peripheral using */
  860. /* system RCC hard reset. */
  861. /* ========== Reset ADC registers ========== */
  862. #if defined (ADC_SUPPORT_2_5_MSPS)
  863. /* Reset register IER */
  864. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_AWD3 | ADC_IT_AWD2 |
  865. ADC_IT_AWD1 | ADC_IT_OVR |
  866. ADC_IT_EOS | ADC_IT_EOC |
  867. ADC_IT_EOSMP | ADC_IT_RDY ) );
  868. /* Reset register ISR */
  869. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_AWD3 | ADC_FLAG_AWD2 |
  870. ADC_FLAG_AWD1 | ADC_FLAG_OVR |
  871. ADC_FLAG_EOS | ADC_FLAG_EOC |
  872. ADC_FLAG_EOSMP | ADC_FLAG_RDY ) );
  873. /* Reset register CR */
  874. /* Bits ADC_CR_ADCAL, ADC_CR_ADSTP, ADC_CR_ADSTART are in access mode */
  875. /* "read-set": no direct reset applicable. */
  876. /* Reset register CFGR1 */
  877. hadc->Instance->CFGR1 &= ~(ADC_CFGR1_AWD1CH | ADC_CFGR1_AWD1EN | ADC_CFGR1_AWD1SGL | ADC_CFGR1_DISCEN |
  878. ADC_CFGR1_AUTOFF | ADC_CFGR1_WAIT | ADC_CFGR1_CONT | ADC_CFGR1_OVRMOD |
  879. ADC_CFGR1_EXTEN | ADC_CFGR1_EXTSEL | ADC_CFGR1_ALIGN | ADC_CFGR1_RES |
  880. ADC_CFGR1_SCANDIR | ADC_CFGR1_DMACFG | ADC_CFGR1_DMAEN );
  881. /* Reset register CFGR2 */
  882. /* Note: Update of ADC clock mode is conditioned to ADC state disabled: */
  883. /* already done above. */
  884. hadc->Instance->CFGR2 &= ~ADC_CFGR2_CKMODE;
  885. /* Reset register SMPR */
  886. hadc->Instance->SMPR &= ~ADC_SMPR_SMP1;
  887. /* Reset register TR1 */
  888. hadc->Instance->TR1 &= ~(ADC_TR1_HT1 | ADC_TR1_LT1);
  889. /* Reset register CHSELR */
  890. hadc->Instance->CHSELR &= ~(ADC_CHSELR_SQ_ALL);
  891. /* Reset register DR */
  892. /* bits in access mode read only, no direct reset applicable */
  893. #else
  894. /* Reset register IER */
  895. __HAL_ADC_DISABLE_IT(hadc, (
  896. #if defined (ADC_SUPPORT_2_5_MSPS)
  897. ADC_IT_AWD3 | ADC_IT_AWD2 | ADC_IT_AWD1 |
  898. ADC_IT_EOCAL | ADC_IT_CCRDY |
  899. #else
  900. ADC_IT_AWD3 | ADC_IT_AWD2 | ADC_IT_AWD1 |
  901. ADC_IT_JEOS | ADC_IT_JEOC | ADC_IT_JQOVF |
  902. #endif
  903. ADC_IT_EOS | ADC_IT_EOC | ADC_IT_OVR |
  904. ADC_IT_EOSMP | ADC_IT_RDY));
  905. /* Reset register ISR */
  906. __HAL_ADC_CLEAR_FLAG(hadc, (
  907. #if defined (ADC_SUPPORT_2_5_MSPS)
  908. ADC_FLAG_AWD3 | ADC_FLAG_AWD2 | ADC_FLAG_AWD1 |
  909. ADC_FLAG_EOCAL | ADC_FLAG_CCRDY |
  910. #else
  911. ADC_FLAG_AWD3 | ADC_FLAG_AWD2 | ADC_FLAG_AWD1 |
  912. ADC_FLAG_JEOS | ADC_FLAG_JEOC | ADC_FLAG_JQOVF |
  913. #endif
  914. ADC_FLAG_EOS | ADC_FLAG_EOC | ADC_FLAG_OVR |
  915. ADC_FLAG_EOSMP | ADC_FLAG_RDY));
  916. /* Reset register CR */
  917. /* Bits ADC_CR_JADSTP, ADC_CR_ADSTP, ADC_CR_JADSTART, ADC_CR_ADSTART,
  918. ADC_CR_ADCAL, ADC_CR_ADDIS and ADC_CR_ADEN are in access mode "read-set":
  919. no direct reset applicable.
  920. Update CR register to reset value where doable by software */
  921. CLEAR_BIT(hadc->Instance->CR, ADC_CR_ADVREGEN | ADC_CR_ADCALDIF);
  922. SET_BIT(hadc->Instance->CR, ADC_CR_DEEPPWD);
  923. /* Reset register CFGR */
  924. CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_FIELDS);
  925. SET_BIT(hadc->Instance->CFGR, ADC_CFGR_JQDIS);
  926. /* Reset register CFGR2 */
  927. CLEAR_BIT(hadc->Instance->CFGR2, ADC_CFGR2_ROVSM | ADC_CFGR2_TROVS | ADC_CFGR2_OVSS |
  928. ADC_CFGR2_OVSR | ADC_CFGR2_JOVSE | ADC_CFGR2_ROVSE);
  929. /* Reset register SMPR1 */
  930. CLEAR_BIT(hadc->Instance->SMPR1, ADC_SMPR1_FIELDS);
  931. /* Reset register SMPR2 */
  932. CLEAR_BIT(hadc->Instance->SMPR2, ADC_SMPR2_SMP18 | ADC_SMPR2_SMP17 | ADC_SMPR2_SMP16 |
  933. ADC_SMPR2_SMP15 | ADC_SMPR2_SMP14 | ADC_SMPR2_SMP13 |
  934. ADC_SMPR2_SMP12 | ADC_SMPR2_SMP11 | ADC_SMPR2_SMP10);
  935. /* Reset register TR1 */
  936. CLEAR_BIT(hadc->Instance->TR1, ADC_TR1_HT1 | ADC_TR1_LT1);
  937. /* Reset register TR2 */
  938. CLEAR_BIT(hadc->Instance->TR2, ADC_TR2_HT2 | ADC_TR2_LT2);
  939. /* Reset register TR3 */
  940. CLEAR_BIT(hadc->Instance->TR3, ADC_TR3_HT3 | ADC_TR3_LT3);
  941. /* Reset register SQR1 */
  942. CLEAR_BIT(hadc->Instance->SQR1, ADC_SQR1_SQ4 | ADC_SQR1_SQ3 | ADC_SQR1_SQ2 |
  943. ADC_SQR1_SQ1 | ADC_SQR1_L);
  944. /* Reset register SQR2 */
  945. CLEAR_BIT(hadc->Instance->SQR2, ADC_SQR2_SQ9 | ADC_SQR2_SQ8 | ADC_SQR2_SQ7 |
  946. ADC_SQR2_SQ6 | ADC_SQR2_SQ5);
  947. /* Reset register SQR3 */
  948. CLEAR_BIT(hadc->Instance->SQR3, ADC_SQR3_SQ14 | ADC_SQR3_SQ13 | ADC_SQR3_SQ12 |
  949. ADC_SQR3_SQ11 | ADC_SQR3_SQ10);
  950. /* Reset register SQR4 */
  951. CLEAR_BIT(hadc->Instance->SQR4, ADC_SQR4_SQ16 | ADC_SQR4_SQ15);
  952. /* Register JSQR was reset when the ADC was disabled */
  953. /* Reset register DR */
  954. /* bits in access mode read only, no direct reset applicable*/
  955. /* Reset register OFR1 */
  956. CLEAR_BIT(hadc->Instance->OFR1, ADC_OFR1_OFFSET1_EN | ADC_OFR1_OFFSET1_CH | ADC_OFR1_OFFSET1);
  957. /* Reset register OFR2 */
  958. CLEAR_BIT(hadc->Instance->OFR2, ADC_OFR2_OFFSET2_EN | ADC_OFR2_OFFSET2_CH | ADC_OFR2_OFFSET2);
  959. /* Reset register OFR3 */
  960. CLEAR_BIT(hadc->Instance->OFR3, ADC_OFR3_OFFSET3_EN | ADC_OFR3_OFFSET3_CH | ADC_OFR3_OFFSET3);
  961. /* Reset register OFR4 */
  962. CLEAR_BIT(hadc->Instance->OFR4, ADC_OFR4_OFFSET4_EN | ADC_OFR4_OFFSET4_CH | ADC_OFR4_OFFSET4);
  963. /* Reset registers JDR1, JDR2, JDR3, JDR4 */
  964. /* bits in access mode read only, no direct reset applicable*/
  965. /* Reset register AWD2CR */
  966. CLEAR_BIT(hadc->Instance->AWD2CR, ADC_AWD2CR_AWD2CH);
  967. /* Reset register AWD3CR */
  968. CLEAR_BIT(hadc->Instance->AWD3CR, ADC_AWD3CR_AWD3CH);
  969. /* Reset register DIFSEL */
  970. CLEAR_BIT(hadc->Instance->DIFSEL, ADC_DIFSEL_DIFSEL);
  971. /* Reset register CALFACT */
  972. CLEAR_BIT(hadc->Instance->CALFACT, ADC_CALFACT_CALFACT_D | ADC_CALFACT_CALFACT_S);
  973. #endif /* ADC_SUPPORT_2_5_MSPS */
  974. /* ========== Reset common ADC registers ========== */
  975. /* Software is allowed to change common parameters only when all the other
  976. ADCs are disabled. */
  977. if (__LL_ADC_IS_ENABLED_ALL_COMMON_INSTANCE(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) == 0UL)
  978. {
  979. /* Reset configuration of ADC common register CCR:
  980. - clock mode: CKMODE, PRESCEN
  981. - multimode related parameters (when this feature is available): MDMA,
  982. DMACFG, DELAY, DUAL (set by HAL_ADCEx_MultiModeConfigChannel() API)
  983. - internal measurement paths: Vbat, temperature sensor, Vref (set into
  984. HAL_ADC_ConfigChannel() or HAL_ADCEx_InjectedConfigChannel() )
  985. */
  986. ADC_CLEAR_COMMON_CONTROL_REGISTER(hadc);
  987. }
  988. /* DeInit the low level hardware.
  989. For example:
  990. __HAL_RCC_ADC_FORCE_RESET();
  991. __HAL_RCC_ADC_RELEASE_RESET();
  992. __HAL_RCC_ADC_CLK_DISABLE();
  993. Keep in mind that all ADCs use the same clock: disabling
  994. the clock will reset all ADCs.
  995. */
  996. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  997. if (hadc->MspDeInitCallback == NULL)
  998. {
  999. hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit */
  1000. }
  1001. /* DeInit the low level hardware: RCC clock, NVIC */
  1002. hadc->MspDeInitCallback(hadc);
  1003. #else
  1004. /* DeInit the low level hardware: RCC clock, NVIC */
  1005. HAL_ADC_MspDeInit(hadc);
  1006. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  1007. /* Set ADC error code to none */
  1008. ADC_CLEAR_ERRORCODE(hadc);
  1009. #if defined (ADC_SUPPORT_2_5_MSPS)
  1010. /* Reset HAL ADC handle variable */
  1011. hadc->ADCGroupRegularSequencerRanks = 0x00000000UL;
  1012. #else
  1013. /* Reset injected channel configuration parameters */
  1014. hadc->InjectionConfig.ContextQueue = 0;
  1015. hadc->InjectionConfig.ChannelCount = 0;
  1016. #endif /* ADC_SUPPORT_2_5_MSPS */
  1017. /* Set ADC state */
  1018. hadc->State = HAL_ADC_STATE_RESET;
  1019. /* Process unlocked */
  1020. __HAL_UNLOCK(hadc);
  1021. /* Return function status */
  1022. return tmp_hal_status;
  1023. }
  1024. /**
  1025. * @brief Initialize the ADC MSP.
  1026. * @param hadc ADC handle
  1027. * @retval None
  1028. */
  1029. __weak void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc)
  1030. {
  1031. /* Prevent unused argument(s) compilation warning */
  1032. UNUSED(hadc);
  1033. /* NOTE : This function should not be modified. When the callback is needed,
  1034. function HAL_ADC_MspInit must be implemented in the user file.
  1035. */
  1036. }
  1037. /**
  1038. * @brief DeInitialize the ADC MSP.
  1039. * @param hadc ADC handle
  1040. * @note All ADC instances use the same core clock at RCC level, disabling
  1041. * the core clock reset all ADC instances).
  1042. * @retval None
  1043. */
  1044. __weak void HAL_ADC_MspDeInit(ADC_HandleTypeDef *hadc)
  1045. {
  1046. /* Prevent unused argument(s) compilation warning */
  1047. UNUSED(hadc);
  1048. /* NOTE : This function should not be modified. When the callback is needed,
  1049. function HAL_ADC_MspDeInit must be implemented in the user file.
  1050. */
  1051. }
  1052. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  1053. /**
  1054. * @brief Register a User ADC Callback
  1055. * To be used instead of the weak predefined callback
  1056. * @param hadc Pointer to a ADC_HandleTypeDef structure that contains
  1057. * the configuration information for the specified ADC.
  1058. * @param CallbackID ID of the callback to be registered
  1059. * This parameter can be one of the following values:
  1060. * @arg @ref HAL_ADC_CONVERSION_COMPLETE_CB_ID ADC conversion complete callback ID
  1061. * @arg @ref HAL_ADC_CONVERSION_HALF_CB_ID ADC conversion DMA half-transfer callback ID
  1062. * @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID ADC analog watchdog 1 callback ID
  1063. * @arg @ref HAL_ADC_ERROR_CB_ID ADC error callback ID
  1064. * @arg @ref HAL_ADC_INJ_CONVERSION_COMPLETE_CB_ID ADC group injected conversion complete callback ID
  1065. * @arg @ref HAL_ADC_INJ_QUEUE_OVEFLOW_CB_ID ADC group injected context queue overflow callback ID
  1066. * @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_2_CB_ID ADC analog watchdog 2 callback ID
  1067. * @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_3_CB_ID ADC analog watchdog 3 callback ID
  1068. * @arg @ref HAL_ADC_END_OF_SAMPLING_CB_ID ADC end of sampling callback ID
  1069. * @arg @ref HAL_ADC_MSPINIT_CB_ID ADC Msp Init callback ID
  1070. * @arg @ref HAL_ADC_MSPDEINIT_CB_ID ADC Msp DeInit callback ID
  1071. * @arg @ref HAL_ADC_MSPINIT_CB_ID MspInit callback ID
  1072. * @arg @ref HAL_ADC_MSPDEINIT_CB_ID MspDeInit callback ID
  1073. * @param pCallback pointer to the Callback function
  1074. * @retval HAL status
  1075. */
  1076. HAL_StatusTypeDef HAL_ADC_RegisterCallback(ADC_HandleTypeDef *hadc, HAL_ADC_CallbackIDTypeDef CallbackID, pADC_CallbackTypeDef pCallback)
  1077. {
  1078. HAL_StatusTypeDef status = HAL_OK;
  1079. if (pCallback == NULL)
  1080. {
  1081. /* Update the error code */
  1082. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  1083. return HAL_ERROR;
  1084. }
  1085. if ((hadc->State & HAL_ADC_STATE_READY) != 0UL)
  1086. {
  1087. switch (CallbackID)
  1088. {
  1089. case HAL_ADC_CONVERSION_COMPLETE_CB_ID :
  1090. hadc->ConvCpltCallback = pCallback;
  1091. break;
  1092. case HAL_ADC_CONVERSION_HALF_CB_ID :
  1093. hadc->ConvHalfCpltCallback = pCallback;
  1094. break;
  1095. case HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID :
  1096. hadc->LevelOutOfWindowCallback = pCallback;
  1097. break;
  1098. case HAL_ADC_ERROR_CB_ID :
  1099. hadc->ErrorCallback = pCallback;
  1100. break;
  1101. case HAL_ADC_INJ_CONVERSION_COMPLETE_CB_ID :
  1102. hadc->InjectedConvCpltCallback = pCallback;
  1103. break;
  1104. case HAL_ADC_INJ_QUEUE_OVEFLOW_CB_ID :
  1105. hadc->InjectedQueueOverflowCallback = pCallback;
  1106. break;
  1107. case HAL_ADC_LEVEL_OUT_OF_WINDOW_2_CB_ID :
  1108. hadc->LevelOutOfWindow2Callback = pCallback;
  1109. break;
  1110. case HAL_ADC_LEVEL_OUT_OF_WINDOW_3_CB_ID :
  1111. hadc->LevelOutOfWindow3Callback = pCallback;
  1112. break;
  1113. case HAL_ADC_END_OF_SAMPLING_CB_ID :
  1114. hadc->EndOfSamplingCallback = pCallback;
  1115. break;
  1116. case HAL_ADC_MSPINIT_CB_ID :
  1117. hadc->MspInitCallback = pCallback;
  1118. break;
  1119. case HAL_ADC_MSPDEINIT_CB_ID :
  1120. hadc->MspDeInitCallback = pCallback;
  1121. break;
  1122. default :
  1123. /* Update the error code */
  1124. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  1125. /* Return error status */
  1126. status = HAL_ERROR;
  1127. break;
  1128. }
  1129. }
  1130. else if (HAL_ADC_STATE_RESET == hadc->State)
  1131. {
  1132. switch (CallbackID)
  1133. {
  1134. case HAL_ADC_MSPINIT_CB_ID :
  1135. hadc->MspInitCallback = pCallback;
  1136. break;
  1137. case HAL_ADC_MSPDEINIT_CB_ID :
  1138. hadc->MspDeInitCallback = pCallback;
  1139. break;
  1140. default :
  1141. /* Update the error code */
  1142. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  1143. /* Return error status */
  1144. status = HAL_ERROR;
  1145. break;
  1146. }
  1147. }
  1148. else
  1149. {
  1150. /* Update the error code */
  1151. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  1152. /* Return error status */
  1153. status = HAL_ERROR;
  1154. }
  1155. return status;
  1156. }
  1157. /**
  1158. * @brief Unregister a ADC Callback
  1159. * ADC callback is redirected to the weak predefined callback
  1160. * @param hadc Pointer to a ADC_HandleTypeDef structure that contains
  1161. * the configuration information for the specified ADC.
  1162. * @param CallbackID ID of the callback to be unregistered
  1163. * This parameter can be one of the following values:
  1164. * @arg @ref HAL_ADC_CONVERSION_COMPLETE_CB_ID ADC conversion complete callback ID
  1165. * @arg @ref HAL_ADC_CONVERSION_HALF_CB_ID ADC conversion DMA half-transfer callback ID
  1166. * @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID ADC analog watchdog 1 callback ID
  1167. * @arg @ref HAL_ADC_ERROR_CB_ID ADC error callback ID
  1168. * @arg @ref HAL_ADC_INJ_CONVERSION_COMPLETE_CB_ID ADC group injected conversion complete callback ID
  1169. * @arg @ref HAL_ADC_INJ_QUEUE_OVEFLOW_CB_ID ADC group injected context queue overflow callback ID
  1170. * @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_2_CB_ID ADC analog watchdog 2 callback ID
  1171. * @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_3_CB_ID ADC analog watchdog 3 callback ID
  1172. * @arg @ref HAL_ADC_END_OF_SAMPLING_CB_ID ADC end of sampling callback ID
  1173. * @arg @ref HAL_ADC_MSPINIT_CB_ID ADC Msp Init callback ID
  1174. * @arg @ref HAL_ADC_MSPDEINIT_CB_ID ADC Msp DeInit callback ID
  1175. * @arg @ref HAL_ADC_MSPINIT_CB_ID MspInit callback ID
  1176. * @arg @ref HAL_ADC_MSPDEINIT_CB_ID MspDeInit callback ID
  1177. * @retval HAL status
  1178. */
  1179. HAL_StatusTypeDef HAL_ADC_UnRegisterCallback(ADC_HandleTypeDef *hadc, HAL_ADC_CallbackIDTypeDef CallbackID)
  1180. {
  1181. HAL_StatusTypeDef status = HAL_OK;
  1182. if ((hadc->State & HAL_ADC_STATE_READY) != 0UL)
  1183. {
  1184. switch (CallbackID)
  1185. {
  1186. case HAL_ADC_CONVERSION_COMPLETE_CB_ID :
  1187. hadc->ConvCpltCallback = HAL_ADC_ConvCpltCallback;
  1188. break;
  1189. case HAL_ADC_CONVERSION_HALF_CB_ID :
  1190. hadc->ConvHalfCpltCallback = HAL_ADC_ConvHalfCpltCallback;
  1191. break;
  1192. case HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID :
  1193. hadc->LevelOutOfWindowCallback = HAL_ADC_LevelOutOfWindowCallback;
  1194. break;
  1195. case HAL_ADC_ERROR_CB_ID :
  1196. hadc->ErrorCallback = HAL_ADC_ErrorCallback;
  1197. break;
  1198. #if defined (ADC_SUPPORT_2_5_MSPS)
  1199. /* Feature "ADC group injected" not available on ADC peripheral of this STM32WB device */
  1200. #else
  1201. case HAL_ADC_INJ_CONVERSION_COMPLETE_CB_ID :
  1202. hadc->InjectedConvCpltCallback = HAL_ADCEx_InjectedConvCpltCallback;
  1203. break;
  1204. case HAL_ADC_INJ_QUEUE_OVEFLOW_CB_ID :
  1205. hadc->InjectedQueueOverflowCallback = HAL_ADCEx_InjectedQueueOverflowCallback;
  1206. break;
  1207. #endif
  1208. case HAL_ADC_LEVEL_OUT_OF_WINDOW_2_CB_ID :
  1209. hadc->LevelOutOfWindow2Callback = HAL_ADCEx_LevelOutOfWindow2Callback;
  1210. break;
  1211. case HAL_ADC_LEVEL_OUT_OF_WINDOW_3_CB_ID :
  1212. hadc->LevelOutOfWindow3Callback = HAL_ADCEx_LevelOutOfWindow3Callback;
  1213. break;
  1214. case HAL_ADC_END_OF_SAMPLING_CB_ID :
  1215. hadc->EndOfSamplingCallback = HAL_ADCEx_EndOfSamplingCallback;
  1216. break;
  1217. case HAL_ADC_MSPINIT_CB_ID :
  1218. hadc->MspInitCallback = HAL_ADC_MspInit; /* Legacy weak MspInit */
  1219. break;
  1220. case HAL_ADC_MSPDEINIT_CB_ID :
  1221. hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit */
  1222. break;
  1223. default :
  1224. /* Update the error code */
  1225. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  1226. /* Return error status */
  1227. status = HAL_ERROR;
  1228. break;
  1229. }
  1230. }
  1231. else if (HAL_ADC_STATE_RESET == hadc->State)
  1232. {
  1233. switch (CallbackID)
  1234. {
  1235. case HAL_ADC_MSPINIT_CB_ID :
  1236. hadc->MspInitCallback = HAL_ADC_MspInit; /* Legacy weak MspInit */
  1237. break;
  1238. case HAL_ADC_MSPDEINIT_CB_ID :
  1239. hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit */
  1240. break;
  1241. default :
  1242. /* Update the error code */
  1243. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  1244. /* Return error status */
  1245. status = HAL_ERROR;
  1246. break;
  1247. }
  1248. }
  1249. else
  1250. {
  1251. /* Update the error code */
  1252. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  1253. /* Return error status */
  1254. status = HAL_ERROR;
  1255. }
  1256. return status;
  1257. }
  1258. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  1259. /**
  1260. * @}
  1261. */
  1262. /** @defgroup ADC_Exported_Functions_Group2 ADC Input and Output operation functions
  1263. * @brief ADC IO operation functions
  1264. *
  1265. @verbatim
  1266. ===============================================================================
  1267. ##### IO operation functions #####
  1268. ===============================================================================
  1269. [..] This section provides functions allowing to:
  1270. (+) Start conversion of regular group.
  1271. (+) Stop conversion of regular group.
  1272. (+) Poll for conversion complete on regular group.
  1273. (+) Poll for conversion event.
  1274. (+) Get result of regular channel conversion.
  1275. (+) Start conversion of regular group and enable interruptions.
  1276. (+) Stop conversion of regular group and disable interruptions.
  1277. (+) Handle ADC interrupt request
  1278. (+) Start conversion of regular group and enable DMA transfer.
  1279. (+) Stop conversion of regular group and disable ADC DMA transfer.
  1280. @endverbatim
  1281. * @{
  1282. */
  1283. /**
  1284. * @brief Enable ADC, start conversion of regular group.
  1285. * @note Interruptions enabled in this function: None.
  1286. * @param hadc ADC handle
  1287. * @retval HAL status
  1288. */
  1289. HAL_StatusTypeDef HAL_ADC_Start(ADC_HandleTypeDef *hadc)
  1290. {
  1291. HAL_StatusTypeDef tmp_hal_status;
  1292. /* Check the parameters */
  1293. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1294. /* Perform ADC enable and conversion start if no conversion is on going */
  1295. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
  1296. {
  1297. /* Process locked */
  1298. __HAL_LOCK(hadc);
  1299. /* Enable the ADC peripheral */
  1300. tmp_hal_status = ADC_Enable(hadc);
  1301. /* Start conversion if ADC is effectively enabled */
  1302. if (tmp_hal_status == HAL_OK)
  1303. {
  1304. /* Set ADC state */
  1305. /* - Clear state bitfield related to regular group conversion results */
  1306. /* - Set state bitfield related to regular operation */
  1307. ADC_STATE_CLR_SET(hadc->State,
  1308. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP,
  1309. HAL_ADC_STATE_REG_BUSY);
  1310. /* Set ADC error code */
  1311. #if defined (ADC_SUPPORT_2_5_MSPS)
  1312. /* Reset all ADC error code fields */
  1313. ADC_CLEAR_ERRORCODE(hadc);
  1314. #else
  1315. /* Check if a conversion is on going on ADC group injected */
  1316. if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  1317. {
  1318. /* Reset ADC error code fields related to regular conversions only */
  1319. CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA));
  1320. }
  1321. else
  1322. {
  1323. /* Reset all ADC error code fields */
  1324. ADC_CLEAR_ERRORCODE(hadc);
  1325. }
  1326. #endif /* ADC_SUPPORT_2_5_MSPS */
  1327. /* Clear ADC group regular conversion flag and overrun flag */
  1328. /* (To ensure of no unknown state from potential previous ADC operations) */
  1329. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
  1330. /* Process unlocked */
  1331. /* Unlock before starting ADC conversions: in case of potential */
  1332. /* interruption, to let the process to ADC IRQ Handler. */
  1333. __HAL_UNLOCK(hadc);
  1334. /* Enable conversion of regular group. */
  1335. /* If software start has been selected, conversion starts immediately. */
  1336. /* If external trigger has been selected, conversion will start at next */
  1337. /* trigger event. */
  1338. /* Start ADC group regular conversion */
  1339. LL_ADC_REG_StartConversion(hadc->Instance);
  1340. }
  1341. else
  1342. {
  1343. /* Process unlocked */
  1344. __HAL_UNLOCK(hadc);
  1345. }
  1346. }
  1347. else
  1348. {
  1349. tmp_hal_status = HAL_BUSY;
  1350. }
  1351. /* Return function status */
  1352. return tmp_hal_status;
  1353. }
  1354. /**
  1355. * @brief Stop ADC conversion of regular group (and injected channels in
  1356. * case of auto_injection mode), disable ADC peripheral.
  1357. * @note: ADC peripheral disable is forcing stop of potential
  1358. * conversion on injected group. If injected group is under use, it
  1359. * should be preliminarily stopped using HAL_ADCEx_InjectedStop function.
  1360. * @param hadc ADC handle
  1361. * @retval HAL status.
  1362. */
  1363. HAL_StatusTypeDef HAL_ADC_Stop(ADC_HandleTypeDef *hadc)
  1364. {
  1365. HAL_StatusTypeDef tmp_hal_status;
  1366. /* Check the parameters */
  1367. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1368. /* Process locked */
  1369. __HAL_LOCK(hadc);
  1370. #if defined (ADC_SUPPORT_2_5_MSPS)
  1371. /* 1. Stop potential conversion on going, on ADC group regular */
  1372. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_GROUP);
  1373. #else
  1374. /* 1. Stop potential conversion on going, on ADC groups regular and injected */
  1375. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_INJECTED_GROUP);
  1376. #endif /* ADC_SUPPORT_2_5_MSPS */
  1377. /* Disable ADC peripheral if conversions are effectively stopped */
  1378. if (tmp_hal_status == HAL_OK)
  1379. {
  1380. /* 2. Disable the ADC peripheral */
  1381. tmp_hal_status = ADC_Disable(hadc);
  1382. /* Check if ADC is effectively disabled */
  1383. if (tmp_hal_status == HAL_OK)
  1384. {
  1385. /* Set ADC state */
  1386. #if defined (ADC_SUPPORT_2_5_MSPS)
  1387. ADC_STATE_CLR_SET(hadc->State,
  1388. HAL_ADC_STATE_REG_BUSY,
  1389. HAL_ADC_STATE_READY);
  1390. #else
  1391. ADC_STATE_CLR_SET(hadc->State,
  1392. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  1393. HAL_ADC_STATE_READY);
  1394. #endif /* ADC_SUPPORT_2_5_MSPS */
  1395. }
  1396. }
  1397. /* Process unlocked */
  1398. __HAL_UNLOCK(hadc);
  1399. /* Return function status */
  1400. return tmp_hal_status;
  1401. }
  1402. /**
  1403. * @brief Wait for regular group conversion to be completed.
  1404. * @note ADC conversion flags EOS (end of sequence) and EOC (end of
  1405. * conversion) are cleared by this function, with an exception:
  1406. * if low power feature "LowPowerAutoWait" is enabled, flags are
  1407. * not cleared to not interfere with this feature until data register
  1408. * is read using function HAL_ADC_GetValue().
  1409. * @note This function cannot be used in a particular setup: ADC configured
  1410. * in DMA mode and polling for end of each conversion (ADC init
  1411. * parameter "EOCSelection" set to ADC_EOC_SINGLE_CONV).
  1412. * In this case, DMA resets the flag EOC and polling cannot be
  1413. * performed on each conversion. Nevertheless, polling can still
  1414. * be performed on the complete sequence (ADC init
  1415. * parameter "EOCSelection" set to ADC_EOC_SEQ_CONV).
  1416. * @param hadc ADC handle
  1417. * @param Timeout Timeout value in millisecond.
  1418. * @retval HAL status
  1419. */
  1420. HAL_StatusTypeDef HAL_ADC_PollForConversion(ADC_HandleTypeDef *hadc, uint32_t Timeout)
  1421. {
  1422. uint32_t tickstart;
  1423. uint32_t tmp_Flag_End;
  1424. /* Check the parameters */
  1425. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1426. /* If end of conversion selected to end of sequence conversions */
  1427. if (hadc->Init.EOCSelection == ADC_EOC_SEQ_CONV)
  1428. {
  1429. tmp_Flag_End = ADC_FLAG_EOS;
  1430. }
  1431. /* If end of conversion selected to end of unitary conversion */
  1432. else /* ADC_EOC_SINGLE_CONV */
  1433. {
  1434. /* Verification that ADC configuration is compliant with polling for */
  1435. /* each conversion: */
  1436. /* Particular case is ADC configured in DMA mode and ADC sequencer with */
  1437. /* several ranks and polling for end of each conversion. */
  1438. /* For code simplicity sake, this particular case is generalized to */
  1439. /* ADC configured in DMA mode and and polling for end of each conversion. */
  1440. #if defined (ADC_SUPPORT_2_5_MSPS)
  1441. if(READ_BIT(hadc->Instance->CFGR1, ADC_CFGR1_DMAEN) != 0UL)
  1442. #else
  1443. if(READ_BIT(hadc->Instance->CFGR, ADC_CFGR_DMAEN) != 0UL)
  1444. #endif
  1445. {
  1446. /* Update ADC state machine to error */
  1447. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1448. return HAL_ERROR;
  1449. }
  1450. else
  1451. {
  1452. tmp_Flag_End = (ADC_FLAG_EOC);
  1453. }
  1454. }
  1455. /* Get tick count */
  1456. tickstart = HAL_GetTick();
  1457. /* Wait until End of unitary conversion or sequence conversions flag is raised */
  1458. while ((hadc->Instance->ISR & tmp_Flag_End) == 0UL)
  1459. {
  1460. /* Check if timeout is disabled (set to infinite wait) */
  1461. if (Timeout != HAL_MAX_DELAY)
  1462. {
  1463. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0UL))
  1464. {
  1465. /* Update ADC state machine to timeout */
  1466. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  1467. /* Process unlocked */
  1468. __HAL_UNLOCK(hadc);
  1469. return HAL_TIMEOUT;
  1470. }
  1471. }
  1472. }
  1473. /* Update ADC state machine */
  1474. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  1475. /* Determine whether any further conversion upcoming on group regular */
  1476. /* by external trigger, continuous mode or scan sequence on going. */
  1477. if ((LL_ADC_REG_IsTriggerSourceSWStart(hadc->Instance) != 0UL)
  1478. && (hadc->Init.ContinuousConvMode == DISABLE)
  1479. )
  1480. {
  1481. /* Check whether end of sequence is reached */
  1482. if (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOS))
  1483. {
  1484. /* Set ADC state */
  1485. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  1486. if ((hadc->State & HAL_ADC_STATE_INJ_BUSY) == 0UL)
  1487. {
  1488. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  1489. }
  1490. }
  1491. }
  1492. /* Clear polled flag */
  1493. if (tmp_Flag_End == ADC_FLAG_EOS)
  1494. {
  1495. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOS);
  1496. }
  1497. else
  1498. {
  1499. /* Clear end of conversion EOC flag of regular group if low power feature */
  1500. /* "LowPowerAutoWait " is disabled, to not interfere with this feature */
  1501. /* until data register is read using function HAL_ADC_GetValue(). */
  1502. #if defined (ADC_SUPPORT_2_5_MSPS)
  1503. if (READ_BIT(hadc->Instance->CFGR1, ADC_CFGR1_WAIT) == 0UL)
  1504. #else
  1505. if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_AUTDLY) == 0UL)
  1506. #endif
  1507. {
  1508. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS));
  1509. }
  1510. }
  1511. /* Return function status */
  1512. return HAL_OK;
  1513. }
  1514. /**
  1515. * @brief Poll for ADC event.
  1516. * @param hadc ADC handle
  1517. * @param EventType the ADC event type.
  1518. * This parameter can be one of the following values:
  1519. * @arg @ref ADC_EOSMP_EVENT ADC End of Sampling event
  1520. * @arg @ref ADC_AWD1_EVENT ADC Analog watchdog 1 event (main analog watchdog, present on all STM32 devices)
  1521. * @arg @ref ADC_AWD2_EVENT ADC Analog watchdog 2 event (additional analog watchdog, not present on all STM32 families)
  1522. * @arg @ref ADC_AWD3_EVENT ADC Analog watchdog 3 event (additional analog watchdog, not present on all STM32 families)
  1523. * @arg @ref ADC_OVR_EVENT ADC Overrun event
  1524. * @arg @ref ADC_JQOVF_EVENT ADC Injected context queue overflow event (1)
  1525. *
  1526. * (1) On STM32WB serie, parameter not available on devices: STM32WB10xx, STM32WB15xx.
  1527. * @param Timeout Timeout value in millisecond.
  1528. * @note The relevant flag is cleared if found to be set, except for ADC_FLAG_OVR.
  1529. * Indeed, the latter is reset only if hadc->Init.Overrun field is set
  1530. * to ADC_OVR_DATA_OVERWRITTEN. Otherwise, data register may be potentially overwritten
  1531. * by a new converted data as soon as OVR is cleared.
  1532. * To reset OVR flag once the preserved data is retrieved, the user can resort
  1533. * to macro __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  1534. * @retval HAL status
  1535. */
  1536. HAL_StatusTypeDef HAL_ADC_PollForEvent(ADC_HandleTypeDef *hadc, uint32_t EventType, uint32_t Timeout)
  1537. {
  1538. uint32_t tickstart;
  1539. /* Check the parameters */
  1540. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1541. assert_param(IS_ADC_EVENT_TYPE(EventType));
  1542. /* Get tick count */
  1543. tickstart = HAL_GetTick();
  1544. /* Check selected event flag */
  1545. while (__HAL_ADC_GET_FLAG(hadc, EventType) == 0UL)
  1546. {
  1547. /* Check if timeout is disabled (set to infinite wait) */
  1548. if (Timeout != HAL_MAX_DELAY)
  1549. {
  1550. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0UL))
  1551. {
  1552. /* Update ADC state machine to timeout */
  1553. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  1554. /* Process unlocked */
  1555. __HAL_UNLOCK(hadc);
  1556. return HAL_TIMEOUT;
  1557. }
  1558. }
  1559. }
  1560. switch (EventType)
  1561. {
  1562. /* End Of Sampling event */
  1563. case ADC_EOSMP_EVENT:
  1564. /* Set ADC state */
  1565. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOSMP);
  1566. /* Clear the End Of Sampling flag */
  1567. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOSMP);
  1568. break;
  1569. /* Analog watchdog (level out of window) event */
  1570. /* Note: In case of several analog watchdog enabled, if needed to know */
  1571. /* which one triggered and on which ADCx, test ADC state of analog watchdog */
  1572. /* flags HAL_ADC_STATE_AWD1/2/3 using function "HAL_ADC_GetState()". */
  1573. /* For example: */
  1574. /* " if ((HAL_ADC_GetState(hadc1) & HAL_ADC_STATE_AWD1) != 0UL) " */
  1575. /* " if ((HAL_ADC_GetState(hadc1) & HAL_ADC_STATE_AWD2) != 0UL) " */
  1576. /* " if ((HAL_ADC_GetState(hadc1) & HAL_ADC_STATE_AWD3) != 0UL) " */
  1577. /* Check analog watchdog 1 flag */
  1578. case ADC_AWD_EVENT:
  1579. /* Set ADC state */
  1580. SET_BIT(hadc->State, HAL_ADC_STATE_AWD1);
  1581. /* Clear ADC analog watchdog flag */
  1582. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD1);
  1583. break;
  1584. /* Check analog watchdog 2 flag */
  1585. case ADC_AWD2_EVENT:
  1586. /* Set ADC state */
  1587. SET_BIT(hadc->State, HAL_ADC_STATE_AWD2);
  1588. /* Clear ADC analog watchdog flag */
  1589. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD2);
  1590. break;
  1591. /* Check analog watchdog 3 flag */
  1592. case ADC_AWD3_EVENT:
  1593. /* Set ADC state */
  1594. SET_BIT(hadc->State, HAL_ADC_STATE_AWD3);
  1595. /* Clear ADC analog watchdog flag */
  1596. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD3);
  1597. break;
  1598. #if defined (ADC_SUPPORT_2_5_MSPS)
  1599. /* Feature "ADC group injected" not available on ADC peripheral of this STM32WB device */
  1600. #else
  1601. /* Injected context queue overflow event */
  1602. case ADC_JQOVF_EVENT:
  1603. /* Set ADC state */
  1604. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_JQOVF);
  1605. /* Set ADC error code to Injected context queue overflow */
  1606. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_JQOVF);
  1607. /* Clear ADC Injected context queue overflow flag */
  1608. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JQOVF);
  1609. break;
  1610. #endif
  1611. /* Overrun event */
  1612. default: /* Case ADC_OVR_EVENT */
  1613. /* If overrun is set to overwrite previous data, overrun event is not */
  1614. /* considered as an error. */
  1615. /* (cf ref manual "Managing conversions without using the DMA and without */
  1616. /* overrun ") */
  1617. if (hadc->Init.Overrun == ADC_OVR_DATA_PRESERVED)
  1618. {
  1619. /* Set ADC state */
  1620. SET_BIT(hadc->State, HAL_ADC_STATE_REG_OVR);
  1621. /* Set ADC error code to overrun */
  1622. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_OVR);
  1623. }
  1624. else
  1625. {
  1626. /* Clear ADC Overrun flag only if Overrun is set to ADC_OVR_DATA_OVERWRITTEN
  1627. otherwise, data register is potentially overwritten by new converted data as soon
  1628. as OVR is cleared. */
  1629. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  1630. }
  1631. break;
  1632. }
  1633. /* Return function status */
  1634. return HAL_OK;
  1635. }
  1636. /**
  1637. * @brief Enable ADC, start conversion of regular group with interruption.
  1638. * @note Interruptions enabled in this function according to initialization
  1639. * setting : EOC (end of conversion), EOS (end of sequence),
  1640. * OVR overrun.
  1641. * Each of these interruptions has its dedicated callback function.
  1642. * @note To guarantee a proper reset of all interruptions once all the needed
  1643. * conversions are obtained, HAL_ADC_Stop_IT() must be called to ensure
  1644. * a correct stop of the IT-based conversions.
  1645. * @note By default, HAL_ADC_Start_IT() does not enable the End Of Sampling
  1646. * interruption. If required (e.g. in case of oversampling with trigger
  1647. * mode), the user must:
  1648. * 1. first clear the EOSMP flag if set with macro __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOSMP)
  1649. * 2. then enable the EOSMP interrupt with macro __HAL_ADC_ENABLE_IT(hadc, ADC_IT_EOSMP)
  1650. * before calling HAL_ADC_Start_IT().
  1651. * @param hadc ADC handle
  1652. * @retval HAL status
  1653. */
  1654. HAL_StatusTypeDef HAL_ADC_Start_IT(ADC_HandleTypeDef *hadc)
  1655. {
  1656. HAL_StatusTypeDef tmp_hal_status;
  1657. /* Check the parameters */
  1658. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1659. /* Perform ADC enable and conversion start if no conversion is on going */
  1660. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
  1661. {
  1662. /* Process locked */
  1663. __HAL_LOCK(hadc);
  1664. /* Enable the ADC peripheral */
  1665. tmp_hal_status = ADC_Enable(hadc);
  1666. /* Start conversion if ADC is effectively enabled */
  1667. if (tmp_hal_status == HAL_OK)
  1668. {
  1669. /* Set ADC state */
  1670. /* - Clear state bitfield related to regular group conversion results */
  1671. /* - Set state bitfield related to regular operation */
  1672. ADC_STATE_CLR_SET(hadc->State,
  1673. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP,
  1674. HAL_ADC_STATE_REG_BUSY);
  1675. /* Set ADC error code */
  1676. #if defined (ADC_SUPPORT_2_5_MSPS)
  1677. /* Reset all ADC error code fields */
  1678. ADC_CLEAR_ERRORCODE(hadc);
  1679. #else
  1680. /* Check if a conversion is on going on ADC group injected */
  1681. if ((hadc->State & HAL_ADC_STATE_INJ_BUSY) != 0UL)
  1682. {
  1683. /* Reset ADC error code fields related to regular conversions only */
  1684. CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA));
  1685. }
  1686. else
  1687. {
  1688. /* Reset all ADC error code fields */
  1689. ADC_CLEAR_ERRORCODE(hadc);
  1690. }
  1691. #endif /* ADC_SUPPORT_2_5_MSPS */
  1692. /* Clear ADC group regular conversion flag and overrun flag */
  1693. /* (To ensure of no unknown state from potential previous ADC operations) */
  1694. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
  1695. /* Process unlocked */
  1696. /* Unlock before starting ADC conversions: in case of potential */
  1697. /* interruption, to let the process to ADC IRQ Handler. */
  1698. __HAL_UNLOCK(hadc);
  1699. /* Disable all interruptions before enabling the desired ones */
  1700. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_EOS | ADC_IT_OVR));
  1701. /* Enable ADC end of conversion interrupt */
  1702. switch (hadc->Init.EOCSelection)
  1703. {
  1704. case ADC_EOC_SEQ_CONV:
  1705. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_EOS);
  1706. break;
  1707. /* case ADC_EOC_SINGLE_CONV */
  1708. default:
  1709. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_EOC);
  1710. break;
  1711. }
  1712. /* Enable ADC overrun interrupt */
  1713. /* If hadc->Init.Overrun is set to ADC_OVR_DATA_PRESERVED, only then is
  1714. ADC_IT_OVR enabled; otherwise data overwrite is considered as normal
  1715. behavior and no CPU time is lost for a non-processed interruption */
  1716. if (hadc->Init.Overrun == ADC_OVR_DATA_PRESERVED)
  1717. {
  1718. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);
  1719. }
  1720. /* Enable conversion of regular group. */
  1721. /* If software start has been selected, conversion starts immediately. */
  1722. /* If external trigger has been selected, conversion will start at next */
  1723. /* trigger event. */
  1724. /* Start ADC group regular conversion */
  1725. LL_ADC_REG_StartConversion(hadc->Instance);
  1726. }
  1727. else
  1728. {
  1729. /* Process unlocked */
  1730. __HAL_UNLOCK(hadc);
  1731. }
  1732. }
  1733. else
  1734. {
  1735. tmp_hal_status = HAL_BUSY;
  1736. }
  1737. /* Return function status */
  1738. return tmp_hal_status;
  1739. }
  1740. /**
  1741. * @brief Stop ADC conversion of regular group (and injected group in
  1742. * case of auto_injection mode), disable interrution of
  1743. * end-of-conversion, disable ADC peripheral.
  1744. * @param hadc ADC handle
  1745. * @retval HAL status.
  1746. */
  1747. HAL_StatusTypeDef HAL_ADC_Stop_IT(ADC_HandleTypeDef *hadc)
  1748. {
  1749. HAL_StatusTypeDef tmp_hal_status;
  1750. /* Check the parameters */
  1751. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1752. /* Process locked */
  1753. __HAL_LOCK(hadc);
  1754. #if defined (ADC_SUPPORT_2_5_MSPS)
  1755. /* 1. Stop potential conversion on going, on ADC group regular */
  1756. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_GROUP);
  1757. #else
  1758. /* 1. Stop potential conversion on going, on ADC groups regular and injected */
  1759. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_INJECTED_GROUP);
  1760. #endif /* ADC_SUPPORT_2_5_MSPS */
  1761. /* Disable ADC peripheral if conversions are effectively stopped */
  1762. if (tmp_hal_status == HAL_OK)
  1763. {
  1764. /* Disable ADC end of conversion interrupt for regular group */
  1765. /* Disable ADC overrun interrupt */
  1766. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_EOS | ADC_IT_OVR));
  1767. /* 2. Disable the ADC peripheral */
  1768. tmp_hal_status = ADC_Disable(hadc);
  1769. /* Check if ADC is effectively disabled */
  1770. if (tmp_hal_status == HAL_OK)
  1771. {
  1772. /* Set ADC state */
  1773. #if defined (ADC_SUPPORT_2_5_MSPS)
  1774. ADC_STATE_CLR_SET(hadc->State,
  1775. HAL_ADC_STATE_REG_BUSY,
  1776. HAL_ADC_STATE_READY);
  1777. #else
  1778. ADC_STATE_CLR_SET(hadc->State,
  1779. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  1780. HAL_ADC_STATE_READY);
  1781. #endif /* ADC_SUPPORT_2_5_MSPS */
  1782. }
  1783. }
  1784. /* Process unlocked */
  1785. __HAL_UNLOCK(hadc);
  1786. /* Return function status */
  1787. return tmp_hal_status;
  1788. }
  1789. /**
  1790. * @brief Enable ADC, start conversion of regular group and transfer result through DMA.
  1791. * @note Interruptions enabled in this function:
  1792. * overrun (if applicable), DMA half transfer, DMA transfer complete.
  1793. * Each of these interruptions has its dedicated callback function.
  1794. * @param hadc ADC handle
  1795. * @param pData Destination Buffer address.
  1796. * @param Length Number of data to be transferred from ADC peripheral to memory
  1797. * @retval HAL status.
  1798. */
  1799. HAL_StatusTypeDef HAL_ADC_Start_DMA(ADC_HandleTypeDef *hadc, uint32_t *pData, uint32_t Length)
  1800. {
  1801. HAL_StatusTypeDef tmp_hal_status;
  1802. /* Check the parameters */
  1803. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1804. /* Perform ADC enable and conversion start if no conversion is on going */
  1805. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
  1806. {
  1807. /* Process locked */
  1808. __HAL_LOCK(hadc);
  1809. /* Enable the ADC peripheral */
  1810. tmp_hal_status = ADC_Enable(hadc);
  1811. /* Start conversion if ADC is effectively enabled */
  1812. if (tmp_hal_status == HAL_OK)
  1813. {
  1814. /* Set ADC state */
  1815. /* - Clear state bitfield related to regular group conversion results */
  1816. /* - Set state bitfield related to regular operation */
  1817. ADC_STATE_CLR_SET(hadc->State,
  1818. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP,
  1819. HAL_ADC_STATE_REG_BUSY);
  1820. #if defined (ADC_SUPPORT_2_5_MSPS)
  1821. /* Reset all ADC error code fields */
  1822. ADC_CLEAR_ERRORCODE(hadc);
  1823. #else
  1824. /* Check if a conversion is on going on ADC group injected */
  1825. if ((hadc->State & HAL_ADC_STATE_INJ_BUSY) != 0UL)
  1826. {
  1827. /* Reset ADC error code fields related to regular conversions only */
  1828. CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA));
  1829. }
  1830. else
  1831. {
  1832. /* Reset all ADC error code fields */
  1833. ADC_CLEAR_ERRORCODE(hadc);
  1834. }
  1835. #endif /* ADC_SUPPORT_2_5_MSPS */
  1836. /* Set the DMA transfer complete callback */
  1837. hadc->DMA_Handle->XferCpltCallback = ADC_DMAConvCplt;
  1838. /* Set the DMA half transfer complete callback */
  1839. hadc->DMA_Handle->XferHalfCpltCallback = ADC_DMAHalfConvCplt;
  1840. /* Set the DMA error callback */
  1841. hadc->DMA_Handle->XferErrorCallback = ADC_DMAError;
  1842. /* Manage ADC and DMA start: ADC overrun interruption, DMA start, */
  1843. /* ADC start (in case of SW start): */
  1844. /* Clear regular group conversion flag and overrun flag */
  1845. /* (To ensure of no unknown state from potential previous ADC */
  1846. /* operations) */
  1847. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
  1848. /* Process unlocked */
  1849. /* Unlock before starting ADC conversions: in case of potential */
  1850. /* interruption, to let the process to ADC IRQ Handler. */
  1851. __HAL_UNLOCK(hadc);
  1852. /* With DMA, overrun event is always considered as an error even if
  1853. hadc->Init.Overrun is set to ADC_OVR_DATA_OVERWRITTEN. Therefore,
  1854. ADC_IT_OVR is enabled. */
  1855. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);
  1856. /* Enable ADC DMA mode */
  1857. #if defined (ADC_SUPPORT_2_5_MSPS)
  1858. SET_BIT(hadc->Instance->CFGR1, ADC_CFGR1_DMAEN);
  1859. #else
  1860. SET_BIT(hadc->Instance->CFGR, ADC_CFGR_DMAEN);
  1861. #endif
  1862. /* Start the DMA channel */
  1863. tmp_hal_status = HAL_DMA_Start_IT(hadc->DMA_Handle, (uint32_t)&hadc->Instance->DR, (uint32_t)pData, Length);
  1864. /* Enable conversion of regular group. */
  1865. /* If software start has been selected, conversion starts immediately. */
  1866. /* If external trigger has been selected, conversion will start at next */
  1867. /* trigger event. */
  1868. /* Start ADC group regular conversion */
  1869. LL_ADC_REG_StartConversion(hadc->Instance);
  1870. }
  1871. else
  1872. {
  1873. /* Process unlocked */
  1874. __HAL_UNLOCK(hadc);
  1875. }
  1876. }
  1877. else
  1878. {
  1879. tmp_hal_status = HAL_BUSY;
  1880. }
  1881. /* Return function status */
  1882. return tmp_hal_status;
  1883. }
  1884. /**
  1885. * @brief Stop ADC conversion of regular group (and injected group in
  1886. * case of auto_injection mode), disable ADC DMA transfer, disable
  1887. * ADC peripheral.
  1888. * @note: ADC peripheral disable is forcing stop of potential
  1889. * conversion on ADC group injected. If ADC group injected is under use, it
  1890. * should be preliminarily stopped using HAL_ADCEx_InjectedStop function.
  1891. * @param hadc ADC handle
  1892. * @retval HAL status.
  1893. */
  1894. HAL_StatusTypeDef HAL_ADC_Stop_DMA(ADC_HandleTypeDef *hadc)
  1895. {
  1896. HAL_StatusTypeDef tmp_hal_status;
  1897. /* Check the parameters */
  1898. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1899. /* Process locked */
  1900. __HAL_LOCK(hadc);
  1901. /* 1. Stop potential ADC group regular conversion on going */
  1902. #if defined (ADC_SUPPORT_2_5_MSPS)
  1903. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_GROUP);
  1904. #else
  1905. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_INJECTED_GROUP);
  1906. #endif /* ADC_SUPPORT_2_5_MSPS */
  1907. /* Disable ADC peripheral if conversions are effectively stopped */
  1908. if (tmp_hal_status == HAL_OK)
  1909. {
  1910. /* Disable ADC DMA (ADC DMA configuration of continous requests is kept) */
  1911. #if defined (ADC_SUPPORT_2_5_MSPS)
  1912. CLEAR_BIT(hadc->Instance->CFGR1, ADC_CFGR1_DMAEN);
  1913. #else
  1914. CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_DMAEN);
  1915. #endif
  1916. /* Disable the DMA channel (in case of DMA in circular mode or stop */
  1917. /* while DMA transfer is on going) */
  1918. if (hadc->DMA_Handle->State == HAL_DMA_STATE_BUSY)
  1919. {
  1920. tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
  1921. /* Check if DMA channel effectively disabled */
  1922. if (tmp_hal_status != HAL_OK)
  1923. {
  1924. /* Update ADC state machine to error */
  1925. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  1926. }
  1927. }
  1928. /* Disable ADC overrun interrupt */
  1929. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
  1930. /* 2. Disable the ADC peripheral */
  1931. /* Update "tmp_hal_status" only if DMA channel disabling passed, */
  1932. /* to keep in memory a potential failing status. */
  1933. if (tmp_hal_status == HAL_OK)
  1934. {
  1935. tmp_hal_status = ADC_Disable(hadc);
  1936. }
  1937. else
  1938. {
  1939. (void)ADC_Disable(hadc);
  1940. }
  1941. /* Check if ADC is effectively disabled */
  1942. if (tmp_hal_status == HAL_OK)
  1943. {
  1944. /* Set ADC state */
  1945. #if defined (ADC_SUPPORT_2_5_MSPS)
  1946. ADC_STATE_CLR_SET(hadc->State,
  1947. HAL_ADC_STATE_REG_BUSY,
  1948. HAL_ADC_STATE_READY);
  1949. #else
  1950. ADC_STATE_CLR_SET(hadc->State,
  1951. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  1952. HAL_ADC_STATE_READY);
  1953. #endif /* ADC_SUPPORT_2_5_MSPS */
  1954. }
  1955. }
  1956. /* Process unlocked */
  1957. __HAL_UNLOCK(hadc);
  1958. /* Return function status */
  1959. return tmp_hal_status;
  1960. }
  1961. /**
  1962. * @brief Get ADC regular group conversion result.
  1963. * @note Reading register DR automatically clears ADC flag EOC
  1964. * (ADC group regular end of unitary conversion).
  1965. * @note This function does not clear ADC flag EOS
  1966. * (ADC group regular end of sequence conversion).
  1967. * Occurrence of flag EOS rising:
  1968. * - If sequencer is composed of 1 rank, flag EOS is equivalent
  1969. * to flag EOC.
  1970. * - If sequencer is composed of several ranks, during the scan
  1971. * sequence flag EOC only is raised, at the end of the scan sequence
  1972. * both flags EOC and EOS are raised.
  1973. * To clear this flag, either use function:
  1974. * in programming model IT: @ref HAL_ADC_IRQHandler(), in programming
  1975. * model polling: @ref HAL_ADC_PollForConversion()
  1976. * or @ref __HAL_ADC_CLEAR_FLAG(&hadc, ADC_FLAG_EOS).
  1977. * @param hadc ADC handle
  1978. * @retval ADC group regular conversion data
  1979. */
  1980. uint32_t HAL_ADC_GetValue(ADC_HandleTypeDef *hadc)
  1981. {
  1982. /* Check the parameters */
  1983. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1984. /* Note: EOC flag is not cleared here by software because automatically */
  1985. /* cleared by hardware when reading register DR. */
  1986. /* Return ADC converted value */
  1987. return hadc->Instance->DR;
  1988. }
  1989. /**
  1990. * @brief Handle ADC interrupt request.
  1991. * @param hadc ADC handle
  1992. * @retval None
  1993. */
  1994. void HAL_ADC_IRQHandler(ADC_HandleTypeDef *hadc)
  1995. {
  1996. uint32_t overrun_error = 0UL; /* flag set if overrun occurrence has to be considered as an error */
  1997. uint32_t tmp_isr = hadc->Instance->ISR;
  1998. uint32_t tmp_ier = hadc->Instance->IER;
  1999. #if !defined (ADC_SUPPORT_2_5_MSPS)
  2000. uint32_t tmp_adc_inj_is_trigger_source_sw_start;
  2001. uint32_t tmp_adc_reg_is_trigger_source_sw_start;
  2002. uint32_t tmp_cfgr;
  2003. #endif
  2004. /* Check the parameters */
  2005. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2006. assert_param(IS_ADC_EOC_SELECTION(hadc->Init.EOCSelection));
  2007. /* ========== Check End of Sampling flag for ADC group regular ========== */
  2008. if (((tmp_isr & ADC_FLAG_EOSMP) == ADC_FLAG_EOSMP) && ((tmp_ier & ADC_IT_EOSMP) == ADC_IT_EOSMP))
  2009. {
  2010. /* Update state machine on end of sampling status if not in error state */
  2011. if ((hadc->State & HAL_ADC_STATE_ERROR_INTERNAL) == 0UL)
  2012. {
  2013. /* Set ADC state */
  2014. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOSMP);
  2015. }
  2016. /* End Of Sampling callback */
  2017. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2018. hadc->EndOfSamplingCallback(hadc);
  2019. #else
  2020. HAL_ADCEx_EndOfSamplingCallback(hadc);
  2021. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2022. /* Clear regular group conversion flag */
  2023. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOSMP);
  2024. }
  2025. /* ====== Check ADC group regular end of unitary conversion sequence conversions ===== */
  2026. if ((((tmp_isr & ADC_FLAG_EOC) == ADC_FLAG_EOC) && ((tmp_ier & ADC_IT_EOC) == ADC_IT_EOC)) ||
  2027. (((tmp_isr & ADC_FLAG_EOS) == ADC_FLAG_EOS) && ((tmp_ier & ADC_IT_EOS) == ADC_IT_EOS)))
  2028. {
  2029. /* Update state machine on conversion status if not in error state */
  2030. if ((hadc->State & HAL_ADC_STATE_ERROR_INTERNAL) == 0UL)
  2031. {
  2032. /* Set ADC state */
  2033. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  2034. }
  2035. /* Determine whether any further conversion upcoming on group regular */
  2036. /* by external trigger, continuous mode or scan sequence on going */
  2037. /* to disable interruption. */
  2038. if (LL_ADC_REG_IsTriggerSourceSWStart(hadc->Instance) != 0UL)
  2039. {
  2040. /* Carry on if continuous mode is disabled */
  2041. #if defined (ADC_SUPPORT_2_5_MSPS)
  2042. if (READ_BIT (hadc->Instance->CFGR1, ADC_CFGR1_CONT) != ADC_CFGR1_CONT)
  2043. #else
  2044. if (READ_BIT (hadc->Instance->CFGR, ADC_CFGR_CONT) != ADC_CFGR_CONT)
  2045. #endif
  2046. {
  2047. /* If End of Sequence is reached, disable interrupts */
  2048. if (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOS))
  2049. {
  2050. /* Allowed to modify bits ADC_IT_EOC/ADC_IT_EOS only if bit */
  2051. /* ADSTART==0 (no conversion on going) */
  2052. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
  2053. {
  2054. /* Disable ADC end of sequence conversion interrupt */
  2055. /* Note: Overrun interrupt was enabled with EOC interrupt in */
  2056. /* HAL_Start_IT(), but is not disabled here because can be used */
  2057. /* by overrun IRQ process below. */
  2058. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC | ADC_IT_EOS);
  2059. /* Set ADC state */
  2060. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  2061. if ((hadc->State & HAL_ADC_STATE_INJ_BUSY) == 0UL)
  2062. {
  2063. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  2064. }
  2065. }
  2066. else
  2067. {
  2068. /* Change ADC state to error state */
  2069. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2070. /* Set ADC error code to ADC peripheral internal error */
  2071. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2072. }
  2073. }
  2074. }
  2075. }
  2076. /* Conversion complete callback */
  2077. /* Note: Into callback function "HAL_ADC_ConvCpltCallback()", */
  2078. /* to determine if conversion has been triggered from EOC or EOS, */
  2079. /* possibility to use: */
  2080. /* " if( __HAL_ADC_GET_FLAG(&hadc, ADC_FLAG_EOS)) " */
  2081. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2082. hadc->ConvCpltCallback(hadc);
  2083. #else
  2084. HAL_ADC_ConvCpltCallback(hadc);
  2085. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2086. /* Clear regular group conversion flag */
  2087. /* Note: in case of overrun set to ADC_OVR_DATA_PRESERVED, end of */
  2088. /* conversion flags clear induces the release of the preserved data.*/
  2089. /* Therefore, if the preserved data value is needed, it must be */
  2090. /* read preliminarily into HAL_ADC_ConvCpltCallback(). */
  2091. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS));
  2092. }
  2093. #if defined (ADC_SUPPORT_2_5_MSPS)
  2094. /* Feature "ADC group injected" not available on ADC peripheral of this STM32WB device */
  2095. #else
  2096. /* ====== Check ADC group injected end of unitary conversion sequence conversions ===== */
  2097. if ((((tmp_isr & ADC_FLAG_JEOC) == ADC_FLAG_JEOC) && ((tmp_ier & ADC_IT_JEOC) == ADC_IT_JEOC)) ||
  2098. (((tmp_isr & ADC_FLAG_JEOS) == ADC_FLAG_JEOS) && ((tmp_ier & ADC_IT_JEOS) == ADC_IT_JEOS)))
  2099. {
  2100. /* Update state machine on conversion status if not in error state */
  2101. if ((hadc->State & HAL_ADC_STATE_ERROR_INTERNAL) == 0UL)
  2102. {
  2103. /* Set ADC state */
  2104. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_EOC);
  2105. }
  2106. /* Retrieve ADC configuration */
  2107. tmp_adc_inj_is_trigger_source_sw_start = LL_ADC_INJ_IsTriggerSourceSWStart(hadc->Instance);
  2108. tmp_adc_reg_is_trigger_source_sw_start = LL_ADC_REG_IsTriggerSourceSWStart(hadc->Instance);
  2109. tmp_cfgr = READ_REG(hadc->Instance->CFGR);
  2110. /* Disable interruption if no further conversion upcoming by injected */
  2111. /* external trigger or by automatic injected conversion with regular */
  2112. /* group having no further conversion upcoming (same conditions as */
  2113. /* regular group interruption disabling above), */
  2114. /* and if injected scan sequence is completed. */
  2115. if (tmp_adc_inj_is_trigger_source_sw_start != 0UL)
  2116. {
  2117. if ((READ_BIT(tmp_cfgr, ADC_CFGR_JAUTO) == 0UL) ||
  2118. ((tmp_adc_reg_is_trigger_source_sw_start != 0UL) &&
  2119. (READ_BIT(tmp_cfgr, ADC_CFGR_CONT) == 0UL)))
  2120. {
  2121. /* If End of Sequence is reached, disable interrupts */
  2122. if (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_JEOS))
  2123. {
  2124. /* Particular case if injected contexts queue is enabled: */
  2125. /* when the last context has been fully processed, JSQR is reset */
  2126. /* by the hardware. Even if no injected conversion is planned to come */
  2127. /* (queue empty, triggers are ignored), it can start again */
  2128. /* immediately after setting a new context (JADSTART is still set). */
  2129. /* Therefore, state of HAL ADC injected group is kept to busy. */
  2130. if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_JQM) == 0UL)
  2131. {
  2132. /* Allowed to modify bits ADC_IT_JEOC/ADC_IT_JEOS only if bit */
  2133. /* JADSTART==0 (no conversion on going) */
  2134. if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) == 0UL)
  2135. {
  2136. /* Disable ADC end of sequence conversion interrupt */
  2137. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC | ADC_IT_JEOS);
  2138. /* Set ADC state */
  2139. CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  2140. if ((hadc->State & HAL_ADC_STATE_REG_BUSY) == 0UL)
  2141. {
  2142. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  2143. }
  2144. }
  2145. else
  2146. {
  2147. /* Update ADC state machine to error */
  2148. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2149. /* Set ADC error code to ADC peripheral internal error */
  2150. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2151. }
  2152. }
  2153. }
  2154. }
  2155. }
  2156. /* Injected Conversion complete callback */
  2157. /* Note: HAL_ADCEx_InjectedConvCpltCallback can resort to
  2158. if( __HAL_ADC_GET_FLAG(&hadc, ADC_FLAG_JEOS)) or
  2159. if( __HAL_ADC_GET_FLAG(&hadc, ADC_FLAG_JEOC)) to determine whether
  2160. interruption has been triggered by end of conversion or end of
  2161. sequence. */
  2162. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2163. hadc->InjectedConvCpltCallback(hadc);
  2164. #else
  2165. HAL_ADCEx_InjectedConvCpltCallback(hadc);
  2166. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2167. /* Clear injected group conversion flag */
  2168. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOC | ADC_FLAG_JEOS);
  2169. }
  2170. #endif /* ADC_SUPPORT_2_5_MSPS */
  2171. /* ========== Check Analog watchdog 1 flag ========== */
  2172. if (((tmp_isr & ADC_FLAG_AWD1) == ADC_FLAG_AWD1) && ((tmp_ier & ADC_IT_AWD1) == ADC_IT_AWD1))
  2173. {
  2174. /* Set ADC state */
  2175. SET_BIT(hadc->State, HAL_ADC_STATE_AWD1);
  2176. /* Level out of window 1 callback */
  2177. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2178. hadc->LevelOutOfWindowCallback(hadc);
  2179. #else
  2180. HAL_ADC_LevelOutOfWindowCallback(hadc);
  2181. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2182. /* Clear ADC analog watchdog flag */
  2183. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD1);
  2184. }
  2185. /* ========== Check analog watchdog 2 flag ========== */
  2186. if (((tmp_isr & ADC_FLAG_AWD2) == ADC_FLAG_AWD2) && ((tmp_ier & ADC_IT_AWD2) == ADC_IT_AWD2))
  2187. {
  2188. /* Set ADC state */
  2189. SET_BIT(hadc->State, HAL_ADC_STATE_AWD2);
  2190. /* Level out of window 2 callback */
  2191. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2192. hadc->LevelOutOfWindow2Callback(hadc);
  2193. #else
  2194. HAL_ADCEx_LevelOutOfWindow2Callback(hadc);
  2195. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2196. /* Clear ADC analog watchdog flag */
  2197. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD2);
  2198. }
  2199. /* ========== Check analog watchdog 3 flag ========== */
  2200. if (((tmp_isr & ADC_FLAG_AWD3) == ADC_FLAG_AWD3) && ((tmp_ier & ADC_IT_AWD3) == ADC_IT_AWD3))
  2201. {
  2202. /* Set ADC state */
  2203. SET_BIT(hadc->State, HAL_ADC_STATE_AWD3);
  2204. /* Level out of window 3 callback */
  2205. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2206. hadc->LevelOutOfWindow3Callback(hadc);
  2207. #else
  2208. HAL_ADCEx_LevelOutOfWindow3Callback(hadc);
  2209. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2210. /* Clear ADC analog watchdog flag */
  2211. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD3);
  2212. }
  2213. /* ========== Check Overrun flag ========== */
  2214. if (((tmp_isr & ADC_FLAG_OVR) == ADC_FLAG_OVR) && ((tmp_ier & ADC_IT_OVR) == ADC_IT_OVR))
  2215. {
  2216. /* If overrun is set to overwrite previous data (default setting), */
  2217. /* overrun event is not considered as an error. */
  2218. /* (cf ref manual "Managing conversions without using the DMA and without */
  2219. /* overrun ") */
  2220. /* Exception for usage with DMA overrun event always considered as an */
  2221. /* error. */
  2222. if (hadc->Init.Overrun == ADC_OVR_DATA_PRESERVED)
  2223. {
  2224. overrun_error = 1UL;
  2225. }
  2226. else
  2227. {
  2228. /* Check DMA configuration */
  2229. if (LL_ADC_REG_GetDMATransfer(hadc->Instance) != LL_ADC_REG_DMA_TRANSFER_NONE)
  2230. {
  2231. overrun_error = 1UL;
  2232. }
  2233. }
  2234. if (overrun_error == 1UL)
  2235. {
  2236. /* Change ADC state to error state */
  2237. SET_BIT(hadc->State, HAL_ADC_STATE_REG_OVR);
  2238. /* Set ADC error code to overrun */
  2239. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_OVR);
  2240. /* Error callback */
  2241. /* Note: In case of overrun, ADC conversion data is preserved until */
  2242. /* flag OVR is reset. */
  2243. /* Therefore, old ADC conversion data can be retrieved in */
  2244. /* function "HAL_ADC_ErrorCallback()". */
  2245. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2246. hadc->ErrorCallback(hadc);
  2247. #else
  2248. HAL_ADC_ErrorCallback(hadc);
  2249. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2250. }
  2251. /* Clear ADC overrun flag */
  2252. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  2253. }
  2254. #if defined (ADC_SUPPORT_2_5_MSPS)
  2255. /* Feature "ADC group injected" not available on ADC peripheral of this STM32WB device */
  2256. #else
  2257. /* ========== Check Injected context queue overflow flag ========== */
  2258. if (((tmp_isr & ADC_FLAG_JQOVF) == ADC_FLAG_JQOVF) && ((tmp_ier & ADC_IT_JQOVF) == ADC_IT_JQOVF))
  2259. {
  2260. /* Change ADC state to overrun state */
  2261. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_JQOVF);
  2262. /* Set ADC error code to Injected context queue overflow */
  2263. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_JQOVF);
  2264. /* Clear the Injected context queue overflow flag */
  2265. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JQOVF);
  2266. /* Injected context queue overflow callback */
  2267. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2268. hadc->InjectedQueueOverflowCallback(hadc);
  2269. #else
  2270. HAL_ADCEx_InjectedQueueOverflowCallback(hadc);
  2271. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2272. }
  2273. #endif /* ADC_SUPPORT_2_5_MSPS */
  2274. }
  2275. /**
  2276. * @brief Conversion complete callback in non-blocking mode.
  2277. * @param hadc ADC handle
  2278. * @retval None
  2279. */
  2280. __weak void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
  2281. {
  2282. /* Prevent unused argument(s) compilation warning */
  2283. UNUSED(hadc);
  2284. /* NOTE : This function should not be modified. When the callback is needed,
  2285. function HAL_ADC_ConvCpltCallback must be implemented in the user file.
  2286. */
  2287. }
  2288. /**
  2289. * @brief Conversion DMA half-transfer callback in non-blocking mode.
  2290. * @param hadc ADC handle
  2291. * @retval None
  2292. */
  2293. __weak void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef *hadc)
  2294. {
  2295. /* Prevent unused argument(s) compilation warning */
  2296. UNUSED(hadc);
  2297. /* NOTE : This function should not be modified. When the callback is needed,
  2298. function HAL_ADC_ConvHalfCpltCallback must be implemented in the user file.
  2299. */
  2300. }
  2301. /**
  2302. * @brief Analog watchdog 1 callback in non-blocking mode.
  2303. * @param hadc ADC handle
  2304. * @retval None
  2305. */
  2306. __weak void HAL_ADC_LevelOutOfWindowCallback(ADC_HandleTypeDef *hadc)
  2307. {
  2308. /* Prevent unused argument(s) compilation warning */
  2309. UNUSED(hadc);
  2310. /* NOTE : This function should not be modified. When the callback is needed,
  2311. function HAL_ADC_LevelOutOfWindowCallback must be implemented in the user file.
  2312. */
  2313. }
  2314. /**
  2315. * @brief ADC error callback in non-blocking mode
  2316. * (ADC conversion with interruption or transfer by DMA).
  2317. * @note In case of error due to overrun when using ADC with DMA transfer
  2318. * (HAL ADC handle parameter "ErrorCode" to state "HAL_ADC_ERROR_OVR"):
  2319. * - Reinitialize the DMA using function "HAL_ADC_Stop_DMA()".
  2320. * - If needed, restart a new ADC conversion using function
  2321. * "HAL_ADC_Start_DMA()"
  2322. * (this function is also clearing overrun flag)
  2323. * @param hadc ADC handle
  2324. * @retval None
  2325. */
  2326. __weak void HAL_ADC_ErrorCallback(ADC_HandleTypeDef *hadc)
  2327. {
  2328. /* Prevent unused argument(s) compilation warning */
  2329. UNUSED(hadc);
  2330. /* NOTE : This function should not be modified. When the callback is needed,
  2331. function HAL_ADC_ErrorCallback must be implemented in the user file.
  2332. */
  2333. }
  2334. /**
  2335. * @}
  2336. */
  2337. /** @defgroup ADC_Exported_Functions_Group3 Peripheral Control functions
  2338. * @brief Peripheral Control functions
  2339. *
  2340. @verbatim
  2341. ===============================================================================
  2342. ##### Peripheral Control functions #####
  2343. ===============================================================================
  2344. [..] This section provides functions allowing to:
  2345. (+) Configure channels on regular group
  2346. (+) Configure the analog watchdog
  2347. @endverbatim
  2348. * @{
  2349. */
  2350. /**
  2351. * @brief Configure a channel to be assigned to ADC group regular.
  2352. * @note In case of usage of internal measurement channels:
  2353. * Vbat/VrefInt/TempSensor.
  2354. * These internal paths can be disabled using function
  2355. * HAL_ADC_DeInit().
  2356. * @note Possibility to update parameters on the fly:
  2357. * This function initializes channel into ADC group regular,
  2358. * following calls to this function can be used to reconfigure
  2359. * some parameters of structure "ADC_ChannelConfTypeDef" on the fly,
  2360. * without resetting the ADC.
  2361. * The setting of these parameters is conditioned to ADC state:
  2362. * Refer to comments of structure "ADC_ChannelConfTypeDef".
  2363. * @param hadc ADC handle
  2364. * @param sConfig Structure of ADC channel assigned to ADC group regular.
  2365. * @retval HAL status
  2366. */
  2367. HAL_StatusTypeDef HAL_ADC_ConfigChannel(ADC_HandleTypeDef *hadc, ADC_ChannelConfTypeDef *sConfig)
  2368. {
  2369. #if defined (ADC_SUPPORT_2_5_MSPS)
  2370. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  2371. uint32_t tmp_config_internal_channel;
  2372. __IO uint32_t wait_loop_index = 0UL;
  2373. /* Check the parameters */
  2374. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2375. assert_param(IS_ADC_CHANNEL(hadc, sConfig->Channel));
  2376. assert_param(IS_ADC_SAMPLING_TIME_COMMON(sConfig->SamplingTime));
  2377. if((hadc->Init.ScanConvMode == ADC_SCAN_SEQ_FIXED) ||
  2378. (hadc->Init.ScanConvMode == ADC_SCAN_SEQ_FIXED_BACKWARD) )
  2379. {
  2380. assert_param(IS_ADC_REGULAR_RANK_SEQ_FIXED(sConfig->Rank));
  2381. }
  2382. else
  2383. {
  2384. assert_param(IS_ADC_REGULAR_NB_CONV(hadc->Init.NbrOfConversion));
  2385. assert_param(IS_ADC_REGULAR_RANK(sConfig->Rank));
  2386. }
  2387. /* Process locked */
  2388. __HAL_LOCK(hadc);
  2389. /* Parameters update conditioned to ADC state: */
  2390. /* Parameters that can be updated when ADC is disabled or enabled without */
  2391. /* conversion on going on regular group: */
  2392. /* - Channel number */
  2393. /* - Channel sampling time */
  2394. /* - Management of internal measurement channels: VrefInt/TempSensor/Vbat */
  2395. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
  2396. {
  2397. /* Configure channel: depending on rank setting, add it or remove it from */
  2398. /* ADC sequencer. */
  2399. /* If sequencer set to not fully configurable with channel rank set to */
  2400. /* none, remove the channel from the sequencer. */
  2401. /* Otherwise (sequencer set to fully configurable or to to not fully */
  2402. /* configurable with channel rank to be set), configure the selected */
  2403. /* channel. */
  2404. if(sConfig->Rank != ADC_RANK_NONE)
  2405. {
  2406. /* Regular sequence configuration */
  2407. /* Note: ADC channel configuration requires few ADC clock cycles */
  2408. /* to be ready. Processing of ADC settings in this function */
  2409. /* induce that a specific wait time is not necessary. */
  2410. /* For more details on ADC channel configuration ready, */
  2411. /* refer to function "LL_ADC_IsActiveFlag_CCRDY()". */
  2412. if((hadc->Init.ScanConvMode == ADC_SCAN_SEQ_FIXED) ||
  2413. (hadc->Init.ScanConvMode == ADC_SCAN_SEQ_FIXED_BACKWARD) )
  2414. {
  2415. /* Sequencer set to not fully configurable: */
  2416. /* Set the channel by enabling the corresponding bitfield. */
  2417. LL_ADC_REG_SetSequencerChAdd(hadc->Instance, sConfig->Channel);
  2418. }
  2419. else
  2420. {
  2421. /* Sequencer set to fully configurable: */
  2422. /* Set the channel by entering it into the selected rank. */
  2423. /* Memorize the channel set into variable in HAL ADC handle */
  2424. MODIFY_REG(hadc->ADCGroupRegularSequencerRanks,
  2425. ADC_CHSELR_SQ1 << (sConfig->Rank & 0x1FUL),
  2426. __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfig->Channel) << (sConfig->Rank & 0x1FUL));
  2427. /* If the selected rank is below ADC group regular sequencer length, */
  2428. /* apply the configuration in ADC register. */
  2429. /* Note: Otherwise, configuration is not applied. */
  2430. /* To apply it, parameter'NbrOfConversion' must be increased. */
  2431. if(((sConfig->Rank >> 2UL) + 1UL) <= hadc->Init.NbrOfConversion)
  2432. {
  2433. LL_ADC_REG_SetSequencerRanks(hadc->Instance, sConfig->Rank, sConfig->Channel);
  2434. }
  2435. }
  2436. /* Set sampling time of the selected ADC channel */
  2437. LL_ADC_SetChannelSamplingTime(hadc->Instance, sConfig->Channel, sConfig->SamplingTime);
  2438. /* Management of internal measurement channels: VrefInt/TempSensor/Vbat */
  2439. /* internal measurement paths enable: If internal channel selected, */
  2440. /* enable dedicated internal buffers and path. */
  2441. /* Note: these internal measurement paths can be disabled using */
  2442. /* HAL_ADC_DeInit() or removing the channel from sequencer with */
  2443. /* channel configuration parameter "Rank". */
  2444. if(__LL_ADC_IS_CHANNEL_INTERNAL(sConfig->Channel))
  2445. {
  2446. tmp_config_internal_channel = LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
  2447. /* If the requested internal measurement path has already been enabled, */
  2448. /* bypass the configuration processing. */
  2449. if ((sConfig->Channel == ADC_CHANNEL_TEMPSENSOR) && ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_TEMPSENSOR) == 0UL))
  2450. {
  2451. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_TEMPSENSOR | tmp_config_internal_channel);
  2452. /* Delay for temperature sensor stabilization time */
  2453. /* Wait loop initialization and execution */
  2454. /* Note: Variable divided by 2 to compensate partially */
  2455. /* CPU processing cycles, scaling in us split to not */
  2456. /* exceed 32 bits register capacity and handle low frequency. */
  2457. wait_loop_index = (((LL_ADC_DELAY_TEMPSENSOR_STAB_US / 10UL) * (SystemCoreClock / (100000UL * 2UL))) + 1UL);
  2458. while(wait_loop_index != 0UL)
  2459. {
  2460. wait_loop_index--;
  2461. }
  2462. }
  2463. else if ((sConfig->Channel == ADC_CHANNEL_VBAT) && ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_VBAT) == 0UL))
  2464. {
  2465. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_VBAT | tmp_config_internal_channel);
  2466. }
  2467. else if ((sConfig->Channel == ADC_CHANNEL_VREFINT) && ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_VREFINT) == 0UL))
  2468. {
  2469. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_VREFINT | tmp_config_internal_channel);
  2470. }
  2471. else
  2472. {
  2473. /* nothing to do */
  2474. }
  2475. }
  2476. }
  2477. else
  2478. {
  2479. /* Regular sequencer configuration */
  2480. /* Note: Case of sequencer set to fully configurable: */
  2481. /* Sequencer rank cannot be disabled, only affected to */
  2482. /* another channel. */
  2483. /* To remove a rank, use parameter 'NbrOfConversion". */
  2484. if((hadc->Init.ScanConvMode == ADC_SCAN_SEQ_FIXED) ||
  2485. (hadc->Init.ScanConvMode == ADC_SCAN_SEQ_FIXED_BACKWARD) )
  2486. {
  2487. /* Sequencer set to not fully configurable: */
  2488. /* Reset the channel by disabling the corresponding bitfield. */
  2489. LL_ADC_REG_SetSequencerChRem(hadc->Instance, sConfig->Channel);
  2490. }
  2491. /* Management of internal measurement channels: Vbat/VrefInt/TempSensor. */
  2492. /* If internal channel selected, enable dedicated internal buffers and */
  2493. /* paths. */
  2494. if(__LL_ADC_IS_CHANNEL_INTERNAL(sConfig->Channel))
  2495. {
  2496. tmp_config_internal_channel = LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
  2497. if (sConfig->Channel == ADC_CHANNEL_TEMPSENSOR)
  2498. {
  2499. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), ~LL_ADC_PATH_INTERNAL_TEMPSENSOR & tmp_config_internal_channel);
  2500. }
  2501. else if (sConfig->Channel == ADC_CHANNEL_VBAT)
  2502. {
  2503. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), ~LL_ADC_PATH_INTERNAL_VBAT & tmp_config_internal_channel);
  2504. }
  2505. else if (sConfig->Channel == ADC_CHANNEL_VREFINT)
  2506. {
  2507. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), ~LL_ADC_PATH_INTERNAL_VREFINT & tmp_config_internal_channel);
  2508. }
  2509. else
  2510. {
  2511. /* nothing to do */
  2512. }
  2513. }
  2514. }
  2515. }
  2516. /* If a conversion is on going on regular group, no update on regular */
  2517. /* channel could be done on neither of the channel configuration structure */
  2518. /* parameters. */
  2519. else
  2520. {
  2521. /* Update ADC state machine to error */
  2522. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  2523. tmp_hal_status = HAL_ERROR;
  2524. }
  2525. /* Process unlocked */
  2526. __HAL_UNLOCK(hadc);
  2527. #else
  2528. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  2529. uint32_t tmpOffsetShifted;
  2530. uint32_t tmp_config_internal_channel;
  2531. __IO uint32_t wait_loop_index = 0;
  2532. #if !defined (ADC_SUPPORT_2_5_MSPS)
  2533. uint32_t tmp_adc_is_conversion_on_going_regular;
  2534. uint32_t tmp_adc_is_conversion_on_going_injected;
  2535. #endif
  2536. /* Check the parameters */
  2537. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2538. assert_param(IS_ADC_REGULAR_RANK(sConfig->Rank));
  2539. assert_param(IS_ADC_SAMPLE_TIME(sConfig->SamplingTime));
  2540. assert_param(IS_ADC_SINGLE_DIFFERENTIAL(sConfig->SingleDiff));
  2541. assert_param(IS_ADC_OFFSET_NUMBER(sConfig->OffsetNumber));
  2542. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), sConfig->Offset));
  2543. /* if ROVSE is set, the value of the OFFSETy_EN bit in ADCx_OFRy register is
  2544. ignored (considered as reset) */
  2545. assert_param(!((sConfig->OffsetNumber != ADC_OFFSET_NONE) && (hadc->Init.OversamplingMode == ENABLE)));
  2546. /* Verification of channel number */
  2547. if (sConfig->SingleDiff != ADC_DIFFERENTIAL_ENDED)
  2548. {
  2549. assert_param(IS_ADC_CHANNEL(hadc, sConfig->Channel));
  2550. }
  2551. else
  2552. {
  2553. assert_param(IS_ADC_DIFF_CHANNEL(hadc, sConfig->Channel));
  2554. }
  2555. /* Process locked */
  2556. __HAL_LOCK(hadc);
  2557. /* Parameters update conditioned to ADC state: */
  2558. /* Parameters that can be updated when ADC is disabled or enabled without */
  2559. /* conversion on going on regular group: */
  2560. /* - Channel number */
  2561. /* - Channel rank */
  2562. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
  2563. {
  2564. /* Set ADC group regular sequence: channel on the selected scan sequence rank */
  2565. LL_ADC_REG_SetSequencerRanks(hadc->Instance, sConfig->Rank, sConfig->Channel);
  2566. /* Parameters update conditioned to ADC state: */
  2567. /* Parameters that can be updated when ADC is disabled or enabled without */
  2568. /* conversion on going on regular group: */
  2569. /* - Channel sampling time */
  2570. /* - Channel offset */
  2571. #if defined (ADC_SUPPORT_2_5_MSPS)
  2572. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
  2573. #else
  2574. tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
  2575. tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
  2576. if ((tmp_adc_is_conversion_on_going_regular == 0UL)
  2577. && (tmp_adc_is_conversion_on_going_injected == 0UL)
  2578. )
  2579. #endif /* ADC_SUPPORT_2_5_MSPS */
  2580. {
  2581. /* Set sampling time of the selected ADC channel */
  2582. LL_ADC_SetChannelSamplingTime(hadc->Instance, sConfig->Channel, sConfig->SamplingTime);
  2583. /* Configure the offset: offset enable/disable, channel, offset value */
  2584. /* Shift the offset with respect to the selected ADC resolution. */
  2585. /* Offset has to be left-aligned on bit 11, the LSB (right bits) are set to 0 */
  2586. tmpOffsetShifted = ADC_OFFSET_SHIFT_RESOLUTION(hadc, (uint32_t)sConfig->Offset);
  2587. if (sConfig->OffsetNumber != ADC_OFFSET_NONE)
  2588. {
  2589. /* Set ADC selected offset number */
  2590. LL_ADC_SetOffset(hadc->Instance, sConfig->OffsetNumber, sConfig->Channel, tmpOffsetShifted);
  2591. }
  2592. else
  2593. {
  2594. /* Scan each offset register to check if the selected channel is targeted. */
  2595. /* If this is the case, the corresponding offset number is disabled. */
  2596. if(__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_1)) == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfig->Channel))
  2597. {
  2598. LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_1, LL_ADC_OFFSET_DISABLE);
  2599. }
  2600. if(__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_2)) == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfig->Channel))
  2601. {
  2602. LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_2, LL_ADC_OFFSET_DISABLE);
  2603. }
  2604. if(__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_3)) == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfig->Channel))
  2605. {
  2606. LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_3, LL_ADC_OFFSET_DISABLE);
  2607. }
  2608. if(__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_4)) == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfig->Channel))
  2609. {
  2610. LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_4, LL_ADC_OFFSET_DISABLE);
  2611. }
  2612. }
  2613. }
  2614. /* Parameters update conditioned to ADC state: */
  2615. /* Parameters that can be updated only when ADC is disabled: */
  2616. /* - Single or differential mode */
  2617. /* - Internal measurement channels: Vbat/VrefInt/TempSensor */
  2618. if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
  2619. {
  2620. /* Set mode single-ended or differential input of the selected ADC channel */
  2621. LL_ADC_SetChannelSingleDiff(hadc->Instance, sConfig->Channel, sConfig->SingleDiff);
  2622. /* Configuration of differential mode */
  2623. if (sConfig->SingleDiff == ADC_DIFFERENTIAL_ENDED)
  2624. {
  2625. /* Set sampling time of the selected ADC channel */
  2626. /* Note: ADC channel number masked with value "0x1F" to ensure shift value within 32 bits range */
  2627. LL_ADC_SetChannelSamplingTime(hadc->Instance,
  2628. (uint32_t)(__LL_ADC_DECIMAL_NB_TO_CHANNEL((__LL_ADC_CHANNEL_TO_DECIMAL_NB((uint32_t)sConfig->Channel) + 1UL) & 0x1FUL)),
  2629. sConfig->SamplingTime);
  2630. }
  2631. /* Management of internal measurement channels: Vbat/VrefInt/TempSensor. */
  2632. /* If internal channel selected, enable dedicated internal buffers and */
  2633. /* paths. */
  2634. /* Note: these internal measurement paths can be disabled using */
  2635. /* HAL_ADC_DeInit(). */
  2636. if(__LL_ADC_IS_CHANNEL_INTERNAL(sConfig->Channel))
  2637. {
  2638. /* Configuration of common ADC parameters */
  2639. tmp_config_internal_channel = LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
  2640. /* Software is allowed to change common parameters only when all ADCs */
  2641. /* of the common group are disabled. */
  2642. if (__LL_ADC_IS_ENABLED_ALL_COMMON_INSTANCE(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) == 0UL)
  2643. {
  2644. /* If the requested internal measurement path has already been enabled, */
  2645. /* bypass the configuration processing. */
  2646. if ((sConfig->Channel == ADC_CHANNEL_TEMPSENSOR) && ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_TEMPSENSOR) == 0UL))
  2647. {
  2648. if (ADC_TEMPERATURE_SENSOR_INSTANCE(hadc))
  2649. {
  2650. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_TEMPSENSOR | tmp_config_internal_channel);
  2651. /* Delay for temperature sensor stabilization time */
  2652. /* Wait loop initialization and execution */
  2653. /* Note: Variable divided by 2 to compensate partially */
  2654. /* CPU processing cycles, scaling in us split to not */
  2655. /* exceed 32 bits register capacity and handle low frequency. */
  2656. wait_loop_index = ((LL_ADC_DELAY_TEMPSENSOR_STAB_US / 10UL) * (SystemCoreClock / (100000UL * 2UL)));
  2657. while(wait_loop_index != 0UL)
  2658. {
  2659. wait_loop_index--;
  2660. }
  2661. }
  2662. }
  2663. else if ((sConfig->Channel == ADC_CHANNEL_VBAT) && ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_VBAT) == 0UL))
  2664. {
  2665. if (ADC_BATTERY_VOLTAGE_INSTANCE(hadc))
  2666. {
  2667. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_VBAT | tmp_config_internal_channel);
  2668. }
  2669. }
  2670. else if ((sConfig->Channel == ADC_CHANNEL_VREFINT) && ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_VREFINT) == 0UL))
  2671. {
  2672. if (ADC_VREFINT_INSTANCE(hadc))
  2673. {
  2674. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_VREFINT | tmp_config_internal_channel);
  2675. }
  2676. }
  2677. else
  2678. {
  2679. /* nothing to do */
  2680. }
  2681. }
  2682. /* If the requested internal measurement path has already been */
  2683. /* enabled and other ADC of the common group are enabled, internal */
  2684. /* measurement paths cannot be enabled. */
  2685. else
  2686. {
  2687. /* Update ADC state machine to error */
  2688. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  2689. tmp_hal_status = HAL_ERROR;
  2690. }
  2691. }
  2692. }
  2693. }
  2694. /* If a conversion is on going on regular group, no update on regular */
  2695. /* channel could be done on neither of the channel configuration structure */
  2696. /* parameters. */
  2697. else
  2698. {
  2699. /* Update ADC state machine to error */
  2700. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  2701. tmp_hal_status = HAL_ERROR;
  2702. }
  2703. /* Process unlocked */
  2704. __HAL_UNLOCK(hadc);
  2705. #endif /* ADC_SUPPORT_2_5_MSPS */
  2706. /* Return function status */
  2707. return tmp_hal_status;
  2708. }
  2709. /**
  2710. * @brief Configure the analog watchdog.
  2711. * @note Possibility to update parameters on the fly:
  2712. * This function initializes the selected analog watchdog, successive
  2713. * calls to this function can be used to reconfigure some parameters
  2714. * of structure "ADC_AnalogWDGConfTypeDef" on the fly, without resetting
  2715. * the ADC.
  2716. * The setting of these parameters is conditioned to ADC state.
  2717. * For parameters constraints, see comments of structure
  2718. * "ADC_AnalogWDGConfTypeDef".
  2719. * @note On this STM32 serie, analog watchdog thresholds cannot be modified
  2720. * while ADC conversion is on going.
  2721. * @param hadc ADC handle
  2722. * @param AnalogWDGConfig Structure of ADC analog watchdog configuration
  2723. * @retval HAL status
  2724. */
  2725. HAL_StatusTypeDef HAL_ADC_AnalogWDGConfig(ADC_HandleTypeDef *hadc, ADC_AnalogWDGConfTypeDef *AnalogWDGConfig)
  2726. {
  2727. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  2728. uint32_t tmpAWDHighThresholdShifted;
  2729. uint32_t tmpAWDLowThresholdShifted;
  2730. #if !defined (ADC_SUPPORT_2_5_MSPS)
  2731. uint32_t tmp_adc_is_conversion_on_going_regular;
  2732. uint32_t tmp_adc_is_conversion_on_going_injected;
  2733. #endif
  2734. /* Check the parameters */
  2735. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2736. assert_param(IS_ADC_ANALOG_WATCHDOG_NUMBER(AnalogWDGConfig->WatchdogNumber));
  2737. assert_param(IS_ADC_ANALOG_WATCHDOG_MODE(AnalogWDGConfig->WatchdogMode));
  2738. assert_param(IS_FUNCTIONAL_STATE(AnalogWDGConfig->ITMode));
  2739. #if defined (ADC_SUPPORT_2_5_MSPS)
  2740. if(AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_REG)
  2741. #else
  2742. if ((AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_REG) ||
  2743. (AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_INJEC) ||
  2744. (AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_REGINJEC))
  2745. #endif
  2746. {
  2747. assert_param(IS_ADC_CHANNEL(hadc, AnalogWDGConfig->Channel));
  2748. }
  2749. /* Verify thresholds range */
  2750. #if defined (ADC_SUPPORT_2_5_MSPS)
  2751. /* Verify if thresholds are within the selected ADC resolution */
  2752. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), AnalogWDGConfig->HighThreshold));
  2753. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), AnalogWDGConfig->LowThreshold));
  2754. #else
  2755. if (hadc->Init.OversamplingMode == ENABLE)
  2756. {
  2757. /* Case of oversampling enabled: depending on ratio and shift configuration,
  2758. analog watchdog thresholds can be higher than ADC resolution.
  2759. Verify if thresholds are within maximum thresholds range. */
  2760. assert_param(IS_ADC_RANGE(ADC_RESOLUTION_12B, AnalogWDGConfig->HighThreshold));
  2761. assert_param(IS_ADC_RANGE(ADC_RESOLUTION_12B, AnalogWDGConfig->LowThreshold));
  2762. }
  2763. else
  2764. {
  2765. /* Verify if thresholds are within the selected ADC resolution */
  2766. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), AnalogWDGConfig->HighThreshold));
  2767. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), AnalogWDGConfig->LowThreshold));
  2768. }
  2769. #endif
  2770. /* Process locked */
  2771. __HAL_LOCK(hadc);
  2772. /* Parameters update conditioned to ADC state: */
  2773. /* Parameters that can be updated when ADC is disabled or enabled without */
  2774. /* conversion on going on ADC groups regular and injected: */
  2775. /* - Analog watchdog channels */
  2776. /* - Analog watchdog thresholds */
  2777. #if defined (ADC_SUPPORT_2_5_MSPS)
  2778. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
  2779. #else
  2780. tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
  2781. tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
  2782. if ((tmp_adc_is_conversion_on_going_regular == 0UL)
  2783. && (tmp_adc_is_conversion_on_going_injected == 0UL)
  2784. )
  2785. #endif /* ADC_SUPPORT_2_5_MSPS */
  2786. {
  2787. /* Analog watchdog configuration */
  2788. if (AnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_1)
  2789. {
  2790. /* Configuration of analog watchdog: */
  2791. /* - Set the analog watchdog enable mode: one or overall group of */
  2792. /* channels, on groups regular and-or injected. */
  2793. switch (AnalogWDGConfig->WatchdogMode)
  2794. {
  2795. case ADC_ANALOGWATCHDOG_SINGLE_REG:
  2796. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, __LL_ADC_ANALOGWD_CHANNEL_GROUP(AnalogWDGConfig->Channel,
  2797. LL_ADC_GROUP_REGULAR));
  2798. break;
  2799. #if defined (ADC_SUPPORT_2_5_MSPS)
  2800. /* Feature "ADC group injected" not available on ADC peripheral of this STM32WB device */
  2801. #else
  2802. case ADC_ANALOGWATCHDOG_SINGLE_INJEC:
  2803. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, __LL_ADC_ANALOGWD_CHANNEL_GROUP(AnalogWDGConfig->Channel,
  2804. LL_ADC_GROUP_INJECTED));
  2805. break;
  2806. case ADC_ANALOGWATCHDOG_SINGLE_REGINJEC:
  2807. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, __LL_ADC_ANALOGWD_CHANNEL_GROUP(AnalogWDGConfig->Channel,
  2808. LL_ADC_GROUP_REGULAR_INJECTED));
  2809. break;
  2810. #endif /* ADC_SUPPORT_2_5_MSPS */
  2811. case ADC_ANALOGWATCHDOG_ALL_REG:
  2812. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, LL_ADC_AWD_ALL_CHANNELS_REG);
  2813. break;
  2814. #if defined (ADC_SUPPORT_2_5_MSPS)
  2815. /* Feature "ADC group injected" not available on ADC peripheral of this STM32WB device */
  2816. #else
  2817. case ADC_ANALOGWATCHDOG_ALL_INJEC:
  2818. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, LL_ADC_AWD_ALL_CHANNELS_INJ);
  2819. break;
  2820. case ADC_ANALOGWATCHDOG_ALL_REGINJEC:
  2821. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, LL_ADC_AWD_ALL_CHANNELS_REG_INJ);
  2822. break;
  2823. #endif /* ADC_SUPPORT_2_5_MSPS */
  2824. default: /* ADC_ANALOGWATCHDOG_NONE */
  2825. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, LL_ADC_AWD_DISABLE);
  2826. break;
  2827. }
  2828. /* Shift the offset in function of the selected ADC resolution: */
  2829. /* Thresholds have to be left-aligned on bit 11, the LSB (right bits) */
  2830. /* are set to 0 */
  2831. tmpAWDHighThresholdShifted = ADC_AWD1THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->HighThreshold);
  2832. tmpAWDLowThresholdShifted = ADC_AWD1THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->LowThreshold);
  2833. /* Set ADC analog watchdog thresholds value of both thresholds high and low */
  2834. LL_ADC_ConfigAnalogWDThresholds(hadc->Instance, AnalogWDGConfig->WatchdogNumber, tmpAWDHighThresholdShifted, tmpAWDLowThresholdShifted);
  2835. /* Update state, clear previous result related to AWD1 */
  2836. CLEAR_BIT(hadc->State, HAL_ADC_STATE_AWD1);
  2837. /* Clear flag ADC analog watchdog */
  2838. /* Note: Flag cleared Clear the ADC Analog watchdog flag to be ready */
  2839. /* to use for HAL_ADC_IRQHandler() or HAL_ADC_PollForEvent() */
  2840. /* (in case left enabled by previous ADC operations). */
  2841. LL_ADC_ClearFlag_AWD1(hadc->Instance);
  2842. /* Configure ADC analog watchdog interrupt */
  2843. if (AnalogWDGConfig->ITMode == ENABLE)
  2844. {
  2845. LL_ADC_EnableIT_AWD1(hadc->Instance);
  2846. }
  2847. else
  2848. {
  2849. LL_ADC_DisableIT_AWD1(hadc->Instance);
  2850. }
  2851. }
  2852. #if defined (ADC_SUPPORT_2_5_MSPS)
  2853. /* Feature "ADC analog watchdog 2 and 3" not available on ADC peripheral of this STM32WB device */
  2854. #else
  2855. /* Case of ADC_ANALOGWATCHDOG_2 or ADC_ANALOGWATCHDOG_3 */
  2856. else
  2857. {
  2858. switch (AnalogWDGConfig->WatchdogMode)
  2859. {
  2860. case ADC_ANALOGWATCHDOG_SINGLE_REG:
  2861. #if !defined (ADC_SUPPORT_2_5_MSPS)
  2862. case ADC_ANALOGWATCHDOG_SINGLE_INJEC:
  2863. case ADC_ANALOGWATCHDOG_SINGLE_REGINJEC:
  2864. #endif
  2865. /* Update AWD by bitfield to keep the possibility to monitor */
  2866. /* several channels by successive calls of this function. */
  2867. if (AnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_2)
  2868. {
  2869. SET_BIT(hadc->Instance->AWD2CR, (1UL << (__LL_ADC_CHANNEL_TO_DECIMAL_NB(AnalogWDGConfig->Channel) & 0x1FUL)));
  2870. }
  2871. else
  2872. {
  2873. SET_BIT(hadc->Instance->AWD3CR, (1UL << (__LL_ADC_CHANNEL_TO_DECIMAL_NB(AnalogWDGConfig->Channel) & 0x1FUL)));
  2874. }
  2875. break;
  2876. case ADC_ANALOGWATCHDOG_ALL_REG:
  2877. #if !defined (ADC_SUPPORT_2_5_MSPS)
  2878. case ADC_ANALOGWATCHDOG_ALL_INJEC:
  2879. case ADC_ANALOGWATCHDOG_ALL_REGINJEC:
  2880. #endif
  2881. #if defined (ADC_SUPPORT_2_5_MSPS)
  2882. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, AnalogWDGConfig->WatchdogNumber, LL_ADC_AWD_ALL_CHANNELS_REG);
  2883. #else
  2884. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, AnalogWDGConfig->WatchdogNumber, LL_ADC_AWD_ALL_CHANNELS_REG_INJ);
  2885. #endif
  2886. break;
  2887. default: /* ADC_ANALOGWATCHDOG_NONE */
  2888. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, AnalogWDGConfig->WatchdogNumber, LL_ADC_AWD_DISABLE);
  2889. break;
  2890. }
  2891. /* Shift the thresholds in function of the selected ADC resolution */
  2892. /* have to be left-aligned on bit 7, the LSB (right bits) are set to 0 */
  2893. tmpAWDHighThresholdShifted = ADC_AWD23THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->HighThreshold);
  2894. tmpAWDLowThresholdShifted = ADC_AWD23THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->LowThreshold);
  2895. /* Set ADC analog watchdog thresholds value of both thresholds high and low */
  2896. LL_ADC_ConfigAnalogWDThresholds(hadc->Instance, AnalogWDGConfig->WatchdogNumber, tmpAWDHighThresholdShifted, tmpAWDLowThresholdShifted);
  2897. if (AnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_2)
  2898. {
  2899. /* Update state, clear previous result related to AWD2 */
  2900. CLEAR_BIT(hadc->State, HAL_ADC_STATE_AWD2);
  2901. /* Clear flag ADC analog watchdog */
  2902. /* Note: Flag cleared Clear the ADC Analog watchdog flag to be ready */
  2903. /* to use for HAL_ADC_IRQHandler() or HAL_ADC_PollForEvent() */
  2904. /* (in case left enabled by previous ADC operations). */
  2905. LL_ADC_ClearFlag_AWD2(hadc->Instance);
  2906. /* Configure ADC analog watchdog interrupt */
  2907. if (AnalogWDGConfig->ITMode == ENABLE)
  2908. {
  2909. LL_ADC_EnableIT_AWD2(hadc->Instance);
  2910. }
  2911. else
  2912. {
  2913. LL_ADC_DisableIT_AWD2(hadc->Instance);
  2914. }
  2915. }
  2916. /* (AnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_3) */
  2917. else
  2918. {
  2919. /* Update state, clear previous result related to AWD3 */
  2920. CLEAR_BIT(hadc->State, HAL_ADC_STATE_AWD3);
  2921. /* Clear flag ADC analog watchdog */
  2922. /* Note: Flag cleared Clear the ADC Analog watchdog flag to be ready */
  2923. /* to use for HAL_ADC_IRQHandler() or HAL_ADC_PollForEvent() */
  2924. /* (in case left enabled by previous ADC operations). */
  2925. LL_ADC_ClearFlag_AWD3(hadc->Instance);
  2926. /* Configure ADC analog watchdog interrupt */
  2927. if (AnalogWDGConfig->ITMode == ENABLE)
  2928. {
  2929. LL_ADC_EnableIT_AWD3(hadc->Instance);
  2930. }
  2931. else
  2932. {
  2933. LL_ADC_DisableIT_AWD3(hadc->Instance);
  2934. }
  2935. }
  2936. }
  2937. #endif /* ADC_SUPPORT_2_5_MSPS */
  2938. }
  2939. /* If a conversion is on going on ADC group regular or injected, no update */
  2940. /* could be done on neither of the AWD configuration structure parameters. */
  2941. else
  2942. {
  2943. /* Update ADC state machine to error */
  2944. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  2945. tmp_hal_status = HAL_ERROR;
  2946. }
  2947. /* Process unlocked */
  2948. __HAL_UNLOCK(hadc);
  2949. /* Return function status */
  2950. return tmp_hal_status;
  2951. }
  2952. /**
  2953. * @}
  2954. */
  2955. /** @defgroup ADC_Exported_Functions_Group4 Peripheral State functions
  2956. * @brief ADC Peripheral State functions
  2957. *
  2958. @verbatim
  2959. ===============================================================================
  2960. ##### Peripheral state and errors functions #####
  2961. ===============================================================================
  2962. [..]
  2963. This subsection provides functions to get in run-time the status of the
  2964. peripheral.
  2965. (+) Check the ADC state
  2966. (+) Check the ADC error code
  2967. @endverbatim
  2968. * @{
  2969. */
  2970. /**
  2971. * @brief Return the ADC handle state.
  2972. * @note ADC state machine is managed by bitfields, ADC status must be
  2973. * compared with states bits.
  2974. * For example:
  2975. * " if ((HAL_ADC_GetState(hadc1) & HAL_ADC_STATE_REG_BUSY) != 0UL) "
  2976. * " if ((HAL_ADC_GetState(hadc1) & HAL_ADC_STATE_AWD1) != 0UL) "
  2977. * @param hadc ADC handle
  2978. * @retval ADC handle state (bitfield on 32 bits)
  2979. */
  2980. uint32_t HAL_ADC_GetState(ADC_HandleTypeDef *hadc)
  2981. {
  2982. /* Check the parameters */
  2983. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2984. /* Return ADC handle state */
  2985. return hadc->State;
  2986. }
  2987. /**
  2988. * @brief Return the ADC error code.
  2989. * @param hadc ADC handle
  2990. * @retval ADC error code (bitfield on 32 bits)
  2991. */
  2992. uint32_t HAL_ADC_GetError(ADC_HandleTypeDef *hadc)
  2993. {
  2994. /* Check the parameters */
  2995. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2996. return hadc->ErrorCode;
  2997. }
  2998. /**
  2999. * @}
  3000. */
  3001. /**
  3002. * @}
  3003. */
  3004. /** @defgroup ADC_Private_Functions ADC Private Functions
  3005. * @{
  3006. */
  3007. /**
  3008. * @brief Stop ADC conversion.
  3009. * @param hadc ADC handle
  3010. * @param ConversionGroup ADC group regular and/or injected.
  3011. * This parameter can be one of the following values:
  3012. * @arg @ref ADC_REGULAR_GROUP ADC regular conversion type.
  3013. * @arg @ref ADC_INJECTED_GROUP (1) ADC injected conversion type.
  3014. * @arg @ref ADC_REGULAR_INJECTED_GROUP (1) ADC regular and injected conversion type.
  3015. *
  3016. * (1) On STM32WB serie, parameter not available on devices: STM32WB10xx, STM32WB15xx.
  3017. * @retval HAL status.
  3018. */
  3019. HAL_StatusTypeDef ADC_ConversionStop(ADC_HandleTypeDef *hadc, uint32_t ConversionGroup)
  3020. {
  3021. #if defined (ADC_SUPPORT_2_5_MSPS)
  3022. UNUSED(ConversionGroup);
  3023. uint32_t tickstart;
  3024. /* Check the parameters */
  3025. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  3026. /* Verification if ADC is not already stopped on regular group to bypass */
  3027. /* this function if not needed. */
  3028. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) != 0UL)
  3029. {
  3030. /* Stop potential conversion on going on regular group */
  3031. /* Software is allowed to set ADSTP only when ADSTART=1 and ADDIS=0 */
  3032. if (LL_ADC_IsDisableOngoing(hadc->Instance) == 0UL)
  3033. {
  3034. /* Stop ADC group regular conversion */
  3035. LL_ADC_REG_StopConversion(hadc->Instance);
  3036. }
  3037. /* Wait for conversion effectively stopped */
  3038. /* Get tick count */
  3039. tickstart = HAL_GetTick();
  3040. while((hadc->Instance->CR & ADC_CR_ADSTART) != 0UL)
  3041. {
  3042. if((HAL_GetTick() - tickstart) > ADC_STOP_CONVERSION_TIMEOUT)
  3043. {
  3044. /* Update ADC state machine to error */
  3045. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  3046. /* Set ADC error code to ADC peripheral internal error */
  3047. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  3048. return HAL_ERROR;
  3049. }
  3050. }
  3051. }
  3052. #else
  3053. uint32_t tickstart;
  3054. uint32_t Conversion_Timeout_CPU_cycles = 0UL;
  3055. uint32_t conversion_group_reassigned = ConversionGroup;
  3056. uint32_t tmp_ADC_CR_ADSTART_JADSTART;
  3057. uint32_t tmp_adc_is_conversion_on_going_regular;
  3058. uint32_t tmp_adc_is_conversion_on_going_injected;
  3059. /* Check the parameters */
  3060. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  3061. assert_param(IS_ADC_CONVERSION_GROUP(ConversionGroup));
  3062. /* Verification if ADC is not already stopped (on regular and injected */
  3063. /* groups) to bypass this function if not needed. */
  3064. tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
  3065. tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
  3066. if ((tmp_adc_is_conversion_on_going_regular != 0UL)
  3067. || (tmp_adc_is_conversion_on_going_injected != 0UL)
  3068. )
  3069. {
  3070. /* Particular case of continuous auto-injection mode combined with */
  3071. /* auto-delay mode. */
  3072. /* In auto-injection mode, regular group stop ADC_CR_ADSTP is used (not */
  3073. /* injected group stop ADC_CR_JADSTP). */
  3074. /* Procedure to be followed: Wait until JEOS=1, clear JEOS, set ADSTP=1 */
  3075. /* (see reference manual). */
  3076. if (((hadc->Instance->CFGR & ADC_CFGR_JAUTO) != 0UL)
  3077. && (hadc->Init.ContinuousConvMode == ENABLE)
  3078. && (hadc->Init.LowPowerAutoWait == ENABLE)
  3079. )
  3080. {
  3081. /* Use stop of regular group */
  3082. conversion_group_reassigned = ADC_REGULAR_GROUP;
  3083. /* Wait until JEOS=1 (maximum Timeout: 4 injected conversions) */
  3084. while (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_JEOS) == 0UL)
  3085. {
  3086. if (Conversion_Timeout_CPU_cycles >= (ADC_CONVERSION_TIME_MAX_CPU_CYCLES * 4UL))
  3087. {
  3088. /* Update ADC state machine to error */
  3089. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  3090. /* Set ADC error code to ADC peripheral internal error */
  3091. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  3092. return HAL_ERROR;
  3093. }
  3094. Conversion_Timeout_CPU_cycles ++;
  3095. }
  3096. /* Clear JEOS */
  3097. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOS);
  3098. }
  3099. /* Stop potential conversion on going on ADC group regular */
  3100. if (conversion_group_reassigned != ADC_INJECTED_GROUP)
  3101. {
  3102. /* Software is allowed to set ADSTP only when ADSTART=1 and ADDIS=0 */
  3103. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) != 0UL)
  3104. {
  3105. if (LL_ADC_IsDisableOngoing(hadc->Instance) == 0UL)
  3106. {
  3107. /* Stop ADC group regular conversion */
  3108. LL_ADC_REG_StopConversion(hadc->Instance);
  3109. }
  3110. }
  3111. }
  3112. /* Stop potential conversion on going on ADC group injected */
  3113. if (conversion_group_reassigned != ADC_REGULAR_GROUP)
  3114. {
  3115. /* Software is allowed to set JADSTP only when JADSTART=1 and ADDIS=0 */
  3116. if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) != 0UL)
  3117. {
  3118. if (LL_ADC_IsDisableOngoing(hadc->Instance) == 0UL)
  3119. {
  3120. /* Stop ADC group injected conversion */
  3121. LL_ADC_INJ_StopConversion(hadc->Instance);
  3122. }
  3123. }
  3124. }
  3125. /* Selection of start and stop bits with respect to the regular or injected group */
  3126. switch (conversion_group_reassigned)
  3127. {
  3128. case ADC_REGULAR_INJECTED_GROUP:
  3129. tmp_ADC_CR_ADSTART_JADSTART = (ADC_CR_ADSTART | ADC_CR_JADSTART);
  3130. break;
  3131. case ADC_INJECTED_GROUP:
  3132. tmp_ADC_CR_ADSTART_JADSTART = ADC_CR_JADSTART;
  3133. break;
  3134. /* Case ADC_REGULAR_GROUP only*/
  3135. default:
  3136. tmp_ADC_CR_ADSTART_JADSTART = ADC_CR_ADSTART;
  3137. break;
  3138. }
  3139. /* Wait for conversion effectively stopped */
  3140. tickstart = HAL_GetTick();
  3141. while ((hadc->Instance->CR & tmp_ADC_CR_ADSTART_JADSTART) != 0UL)
  3142. {
  3143. if ((HAL_GetTick() - tickstart) > ADC_STOP_CONVERSION_TIMEOUT)
  3144. {
  3145. /* Update ADC state machine to error */
  3146. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  3147. /* Set ADC error code to ADC peripheral internal error */
  3148. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  3149. return HAL_ERROR;
  3150. }
  3151. }
  3152. }
  3153. #endif /* ADC_SUPPORT_2_5_MSPS */
  3154. /* Return HAL status */
  3155. return HAL_OK;
  3156. }
  3157. /**
  3158. * @brief Enable the selected ADC.
  3159. * @note Prerequisite condition to use this function: ADC must be disabled
  3160. * and voltage regulator must be enabled (done into HAL_ADC_Init()).
  3161. * @param hadc ADC handle
  3162. * @retval HAL status.
  3163. */
  3164. HAL_StatusTypeDef ADC_Enable(ADC_HandleTypeDef *hadc)
  3165. {
  3166. uint32_t tickstart;
  3167. __IO uint32_t wait_loop_index = 0UL;
  3168. /* ADC enable and wait for ADC ready (in case of ADC is disabled or */
  3169. /* enabling phase not yet completed: flag ADC ready not yet set). */
  3170. /* Timeout implemented to not be stuck if ADC cannot be enabled (possible */
  3171. /* causes: ADC clock not running, ...). */
  3172. if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
  3173. {
  3174. /* Check if conditions to enable the ADC are fulfilled */
  3175. #if defined (ADC_SUPPORT_2_5_MSPS)
  3176. if ((hadc->Instance->CR & (ADC_CR_ADCAL | ADC_CR_ADSTP | ADC_CR_ADSTART | ADC_CR_ADDIS | ADC_CR_ADEN)) != 0UL)
  3177. #else
  3178. 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)
  3179. #endif
  3180. {
  3181. /* Update ADC state machine to error */
  3182. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  3183. /* Set ADC error code to ADC peripheral internal error */
  3184. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  3185. return HAL_ERROR;
  3186. }
  3187. /* Enable the ADC peripheral */
  3188. LL_ADC_Enable(hadc->Instance);
  3189. if((LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) & LL_ADC_PATH_INTERNAL_TEMPSENSOR) != 0UL)
  3190. {
  3191. /* Delay for temperature sensor buffer stabilization time */
  3192. /* Wait loop initialization and execution */
  3193. /* Note: Variable divided by 2 to compensate partially */
  3194. /* CPU processing cycles, scaling in us split to not */
  3195. /* exceed 32 bits register capacity and handle low frequency. */
  3196. wait_loop_index = (((LL_ADC_DELAY_TEMPSENSOR_BUFFER_STAB_US / 10UL) * (SystemCoreClock / (100000UL * 2UL))) + 1UL);
  3197. while(wait_loop_index != 0UL)
  3198. {
  3199. wait_loop_index--;
  3200. }
  3201. }
  3202. #if defined (ADC_SUPPORT_2_5_MSPS)
  3203. /* If low power mode AutoPowerOff is enabled, power-on/off phases are */
  3204. /* performed automatically by hardware and flag ADC ready is not set. */
  3205. if (hadc->Init.LowPowerAutoPowerOff != ENABLE)
  3206. #endif /*ADC_SUPPORT_2_5_MSPS */
  3207. {
  3208. /* Wait for ADC effectively enabled */
  3209. tickstart = HAL_GetTick();
  3210. while(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_RDY) == 0UL)
  3211. {
  3212. /* If ADEN bit is set less than 4 ADC clock cycles after the ADCAL bit
  3213. has been cleared (after a calibration), ADEN bit is reset by the
  3214. calibration logic.
  3215. The workaround is to continue setting ADEN until ADRDY is becomes 1.
  3216. Additionally, ADC_ENABLE_TIMEOUT is defined to encompass this
  3217. 4 ADC clock cycle duration */
  3218. /* Note: Test of ADC enabled required due to hardware constraint to */
  3219. /* not enable ADC if already enabled. */
  3220. if(LL_ADC_IsEnabled(hadc->Instance) == 0UL)
  3221. {
  3222. LL_ADC_Enable(hadc->Instance);
  3223. }
  3224. if((HAL_GetTick() - tickstart) > ADC_ENABLE_TIMEOUT)
  3225. {
  3226. /* Update ADC state machine to error */
  3227. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  3228. /* Set ADC error code to ADC peripheral internal error */
  3229. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  3230. return HAL_ERROR;
  3231. }
  3232. }
  3233. }
  3234. }
  3235. /* Return HAL status */
  3236. return HAL_OK;
  3237. }
  3238. /**
  3239. * @brief Disable the selected ADC.
  3240. * @note Prerequisite condition to use this function: ADC conversions must be
  3241. * stopped.
  3242. * @param hadc ADC handle
  3243. * @retval HAL status.
  3244. */
  3245. HAL_StatusTypeDef ADC_Disable(ADC_HandleTypeDef *hadc)
  3246. {
  3247. uint32_t tickstart;
  3248. const uint32_t tmp_adc_is_disable_on_going = LL_ADC_IsDisableOngoing(hadc->Instance);
  3249. /* Verification if ADC is not already disabled: */
  3250. /* Note: forbidden to disable ADC (set bit ADC_CR_ADDIS) if ADC is already */
  3251. /* disabled. */
  3252. if ((LL_ADC_IsEnabled(hadc->Instance) != 0UL)
  3253. && (tmp_adc_is_disable_on_going == 0UL)
  3254. )
  3255. {
  3256. /* Check if conditions to disable the ADC are fulfilled */
  3257. #if defined (ADC_SUPPORT_2_5_MSPS)
  3258. if ((hadc->Instance->CR & (ADC_CR_ADSTART | ADC_CR_ADEN)) == ADC_CR_ADEN)
  3259. #else
  3260. if ((hadc->Instance->CR & (ADC_CR_JADSTART | ADC_CR_ADSTART | ADC_CR_ADEN)) == ADC_CR_ADEN)
  3261. #endif
  3262. {
  3263. /* Disable the ADC peripheral */
  3264. LL_ADC_Disable(hadc->Instance);
  3265. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOSMP | ADC_FLAG_RDY));
  3266. }
  3267. else
  3268. {
  3269. /* Update ADC state machine to error */
  3270. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  3271. /* Set ADC error code to ADC peripheral internal error */
  3272. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  3273. return HAL_ERROR;
  3274. }
  3275. /* Wait for ADC effectively disabled */
  3276. /* Get tick count */
  3277. tickstart = HAL_GetTick();
  3278. while ((hadc->Instance->CR & ADC_CR_ADEN) != 0UL)
  3279. {
  3280. if ((HAL_GetTick() - tickstart) > ADC_DISABLE_TIMEOUT)
  3281. {
  3282. /* Update ADC state machine to error */
  3283. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  3284. /* Set ADC error code to ADC peripheral internal error */
  3285. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  3286. return HAL_ERROR;
  3287. }
  3288. }
  3289. }
  3290. /* Return HAL status */
  3291. return HAL_OK;
  3292. }
  3293. /**
  3294. * @brief DMA transfer complete callback.
  3295. * @param hdma pointer to DMA handle.
  3296. * @retval None
  3297. */
  3298. void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma)
  3299. {
  3300. /* Retrieve ADC handle corresponding to current DMA handle */
  3301. ADC_HandleTypeDef *hadc = (ADC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  3302. /* Update state machine on conversion status if not in error state */
  3303. if ((hadc->State & (HAL_ADC_STATE_ERROR_INTERNAL | HAL_ADC_STATE_ERROR_DMA)) == 0UL)
  3304. {
  3305. /* Set ADC state */
  3306. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  3307. /* Determine whether any further conversion upcoming on group regular */
  3308. /* by external trigger, continuous mode or scan sequence on going */
  3309. /* to disable interruption. */
  3310. /* Is it the end of the regular sequence ? */
  3311. #if defined (ADC_SUPPORT_2_5_MSPS)
  3312. if( (LL_ADC_REG_IsTriggerSourceSWStart(hadc->Instance) != 0UL)
  3313. && (hadc->Init.ContinuousConvMode == DISABLE)
  3314. )
  3315. {
  3316. /* If End of Sequence is reached, disable interrupts */
  3317. if( __HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOS) )
  3318. {
  3319. /* Allowed to modify bits ADC_IT_EOC/ADC_IT_EOS only if bit */
  3320. /* ADSTART==0 (no conversion on going) */
  3321. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
  3322. {
  3323. /* Disable ADC end of single conversion interrupt on group regular */
  3324. /* Note: Overrun interrupt was enabled with EOC interrupt in */
  3325. /* HAL_Start_IT(), but is not disabled here because can be used */
  3326. /* by overrun IRQ process below. */
  3327. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC | ADC_IT_EOS);
  3328. /* Set ADC state */
  3329. ADC_STATE_CLR_SET(hadc->State,
  3330. HAL_ADC_STATE_REG_BUSY,
  3331. HAL_ADC_STATE_READY);
  3332. }
  3333. else
  3334. {
  3335. /* Change ADC state to error state */
  3336. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  3337. /* Set ADC error code to ADC peripheral internal error */
  3338. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  3339. }
  3340. }
  3341. }
  3342. #else
  3343. if ((hadc->Instance->ISR & ADC_FLAG_EOS) != 0UL)
  3344. {
  3345. /* Are conversions software-triggered ? */
  3346. if (LL_ADC_REG_IsTriggerSourceSWStart(hadc->Instance) != 0UL)
  3347. {
  3348. /* Is CONT bit set ? */
  3349. if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_CONT) == 0UL)
  3350. {
  3351. /* CONT bit is not set, no more conversions expected */
  3352. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  3353. if ((hadc->State & HAL_ADC_STATE_INJ_BUSY) == 0UL)
  3354. {
  3355. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  3356. }
  3357. }
  3358. }
  3359. }
  3360. else
  3361. {
  3362. /* DMA End of Transfer interrupt was triggered but conversions sequence
  3363. is not over. If DMACFG is set to 0, conversions are stopped. */
  3364. if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_DMACFG) == 0UL)
  3365. {
  3366. /* DMACFG bit is not set, conversions are stopped. */
  3367. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  3368. if ((hadc->State & HAL_ADC_STATE_INJ_BUSY) == 0UL)
  3369. {
  3370. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  3371. }
  3372. }
  3373. }
  3374. #endif /* ADC_SUPPORT_2_5_MSPS */
  3375. /* Conversion complete callback */
  3376. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  3377. hadc->ConvCpltCallback(hadc);
  3378. #else
  3379. HAL_ADC_ConvCpltCallback(hadc);
  3380. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  3381. }
  3382. else /* DMA and-or internal error occurred */
  3383. {
  3384. if ((hadc->State & HAL_ADC_STATE_ERROR_INTERNAL) != 0UL)
  3385. {
  3386. /* Call HAL ADC Error Callback function */
  3387. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  3388. hadc->ErrorCallback(hadc);
  3389. #else
  3390. HAL_ADC_ErrorCallback(hadc);
  3391. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  3392. }
  3393. else
  3394. {
  3395. /* Call ADC DMA error callback */
  3396. hadc->DMA_Handle->XferErrorCallback(hdma);
  3397. }
  3398. }
  3399. }
  3400. /**
  3401. * @brief DMA half transfer complete callback.
  3402. * @param hdma pointer to DMA handle.
  3403. * @retval None
  3404. */
  3405. void ADC_DMAHalfConvCplt(DMA_HandleTypeDef *hdma)
  3406. {
  3407. /* Retrieve ADC handle corresponding to current DMA handle */
  3408. ADC_HandleTypeDef *hadc = (ADC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  3409. /* Half conversion callback */
  3410. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  3411. hadc->ConvHalfCpltCallback(hadc);
  3412. #else
  3413. HAL_ADC_ConvHalfCpltCallback(hadc);
  3414. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  3415. }
  3416. /**
  3417. * @brief DMA error callback.
  3418. * @param hdma pointer to DMA handle.
  3419. * @retval None
  3420. */
  3421. void ADC_DMAError(DMA_HandleTypeDef *hdma)
  3422. {
  3423. /* Retrieve ADC handle corresponding to current DMA handle */
  3424. ADC_HandleTypeDef *hadc = (ADC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  3425. /* Set ADC state */
  3426. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  3427. /* Set ADC error code to DMA error */
  3428. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_DMA);
  3429. /* Error callback */
  3430. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  3431. hadc->ErrorCallback(hadc);
  3432. #else
  3433. HAL_ADC_ErrorCallback(hadc);
  3434. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  3435. }
  3436. /**
  3437. * @}
  3438. */
  3439. #endif /* HAL_ADC_MODULE_ENABLED */
  3440. /**
  3441. * @}
  3442. */
  3443. /**
  3444. * @}
  3445. */
  3446. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/