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.
 
 
 

1745 lines
66 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32wbxx_hal_adc_ex.c
  4. * @author MCD Application Team
  5. * @brief This file provides firmware functions to manage the following
  6. * functionalities of the Analog to Digital Converter (ADC)
  7. * peripheral:
  8. * + Operation functions
  9. * ++ Start, stop, get result of conversions of ADC group injected,
  10. * using 2 possible modes: polling, interruption (not available on devices: STM32WB10xx, STM32WB15xx).
  11. * ++ Calibration
  12. * +++ ADC automatic self-calibration
  13. * +++ Calibration factors get or set
  14. * + Control functions
  15. * ++ Channels configuration on ADC group injected (not available on devices: STM32WB10xx, STM32WB15xx)
  16. * + State functions
  17. * ++ ADC group injected contexts queue management (not available on devices: STM32WB10xx, STM32WB15xx)
  18. * Other functions (generic functions) are available in file
  19. * "stm32wbxx_hal_adc.c".
  20. *
  21. @verbatim
  22. [..]
  23. (@) Sections "ADC peripheral features" and "How to use this driver" are
  24. available in file of generic functions "stm32wbxx_hal_adc.c".
  25. [..]
  26. @endverbatim
  27. ******************************************************************************
  28. * @attention
  29. *
  30. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  31. * All rights reserved.</center></h2>
  32. *
  33. * This software component is licensed by ST under BSD 3-Clause license,
  34. * the "License"; You may not use this file except in compliance with the
  35. * License. You may obtain a copy of the License at:
  36. * opensource.org/licenses/BSD-3-Clause
  37. *
  38. ******************************************************************************
  39. */
  40. /* Includes ------------------------------------------------------------------*/
  41. #include "stm32wbxx_hal.h"
  42. /** @addtogroup STM32WBxx_HAL_Driver
  43. * @{
  44. */
  45. /** @defgroup ADCEx ADCEx
  46. * @brief ADC Extended HAL module driver
  47. * @{
  48. */
  49. #ifdef HAL_ADC_MODULE_ENABLED
  50. /* Private typedef -----------------------------------------------------------*/
  51. /* Private define ------------------------------------------------------------*/
  52. /** @defgroup ADCEx_Private_Constants ADC Extended Private Constants
  53. * @{
  54. */
  55. #define ADC_JSQR_FIELDS ((ADC_JSQR_JL | ADC_JSQR_JEXTSEL | ADC_JSQR_JEXTEN |\
  56. ADC_JSQR_JSQ1 | ADC_JSQR_JSQ2 |\
  57. ADC_JSQR_JSQ3 | ADC_JSQR_JSQ4 )) /*!< ADC_JSQR fields of parameters that can be updated anytime
  58. once the ADC is enabled */
  59. /* Fixed timeout value for ADC calibration. */
  60. /* Values defined to be higher than worst cases: maximum ratio between ADC */
  61. /* and CPU clock frequencies. */
  62. /* Example of profile low frequency : ADC frequency at 46.9kHz (ADC clock */
  63. /* source PLL SAI 12MHz, ADC clock prescaler 256), CPU frequency 64MHz. */
  64. /* Calibration time max = 116 / fADC (refer to datasheet) */
  65. /* = 158 379 CPU cycles */
  66. #define ADC_CALIBRATION_TIMEOUT (158379UL) /*!< ADC calibration time-out value (unit: CPU cycles) */
  67. /**
  68. * @}
  69. */
  70. /* Private macro -------------------------------------------------------------*/
  71. /* Private variables ---------------------------------------------------------*/
  72. /* Private function prototypes -----------------------------------------------*/
  73. /* Exported functions --------------------------------------------------------*/
  74. /** @defgroup ADCEx_Exported_Functions ADC Extended Exported Functions
  75. * @{
  76. */
  77. /** @defgroup ADCEx_Exported_Functions_Group1 Extended Input and Output operation functions
  78. * @brief Extended IO operation functions
  79. *
  80. @verbatim
  81. ===============================================================================
  82. ##### IO operation functions #####
  83. ===============================================================================
  84. [..] This section provides functions allowing to:
  85. (+) Perform the ADC self-calibration for single or differential ending.
  86. (+) Get calibration factors for single or differential ending.
  87. (+) Set calibration factors for single or differential ending.
  88. (+) Start conversion of ADC group injected (not available on devices: STM32WB10xx, STM32WB15xx).
  89. (+) Stop conversion of ADC group injected (not available on devices: STM32WB10xx, STM32WB15xx).
  90. (+) Poll for conversion complete on ADC group injected (not available on devices: STM32WB10xx, STM32WB15xx).
  91. (+) Get result of ADC group injected channel conversion (not available on devices: STM32WB10xx, STM32WB15xx).
  92. (+) Start conversion of ADC group injected and enable interruptions (not available on devices: STM32WB10xx, STM32WB15xx).
  93. (+) Stop conversion of ADC group injected and disable interruptions (not available on devices: STM32WB10xx, STM32WB15xx).
  94. @endverbatim
  95. * @{
  96. */
  97. /**
  98. * @brief Perform an ADC automatic self-calibration
  99. * Calibration prerequisite: ADC must be disabled (execute this
  100. * function before HAL_ADC_Start() or after HAL_ADC_Stop() ).
  101. * @param hadc ADC handle
  102. * @param SingleDiff Selection of single-ended or differential input
  103. * This parameter can be one of the following values:
  104. * @arg @ref ADC_SINGLE_ENDED Channel in mode input single ended
  105. * @arg @ref ADC_DIFFERENTIAL_ENDED Channel in mode input differential ended (1)
  106. *
  107. * (1) On STM32WB serie, parameter not available on devices: STM32WB10xx, STM32WB15xx.
  108. * @retval HAL status
  109. */
  110. HAL_StatusTypeDef HAL_ADCEx_Calibration_Start(ADC_HandleTypeDef *hadc, uint32_t SingleDiff)
  111. {
  112. #if defined (ADC_SUPPORT_2_5_MSPS)
  113. UNUSED(SingleDiff);
  114. #endif
  115. HAL_StatusTypeDef tmp_hal_status;
  116. __IO uint32_t wait_loop_index = 0UL;
  117. /* Check the parameters */
  118. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  119. assert_param(IS_ADC_SINGLE_DIFFERENTIAL(SingleDiff));
  120. /* Process locked */
  121. __HAL_LOCK(hadc);
  122. /* Calibration prerequisite: ADC must be disabled. */
  123. /* Disable the ADC (if not already disabled) */
  124. tmp_hal_status = ADC_Disable(hadc);
  125. /* Check if ADC is effectively disabled */
  126. if (tmp_hal_status == HAL_OK)
  127. {
  128. /* Set ADC state */
  129. #if defined (ADC_SUPPORT_2_5_MSPS)
  130. ADC_STATE_CLR_SET(hadc->State,
  131. HAL_ADC_STATE_REG_BUSY,
  132. HAL_ADC_STATE_BUSY_INTERNAL);
  133. #else
  134. ADC_STATE_CLR_SET(hadc->State,
  135. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  136. HAL_ADC_STATE_BUSY_INTERNAL);
  137. #endif /* ADC_SUPPORT_2_5_MSPS */
  138. /* Start ADC calibration in mode single-ended or differential */
  139. #if defined (ADC_SUPPORT_2_5_MSPS)
  140. LL_ADC_StartCalibration(hadc->Instance);
  141. #else
  142. LL_ADC_StartCalibration(hadc->Instance, SingleDiff);
  143. #endif /* ADC_SUPPORT_2_5_MSPS */
  144. /* Wait for calibration completion */
  145. while (LL_ADC_IsCalibrationOnGoing(hadc->Instance) != 0UL)
  146. {
  147. wait_loop_index++;
  148. if (wait_loop_index >= ADC_CALIBRATION_TIMEOUT)
  149. {
  150. /* Update ADC state machine to error */
  151. ADC_STATE_CLR_SET(hadc->State,
  152. HAL_ADC_STATE_BUSY_INTERNAL,
  153. HAL_ADC_STATE_ERROR_INTERNAL);
  154. /* Process unlocked */
  155. __HAL_UNLOCK(hadc);
  156. return HAL_ERROR;
  157. }
  158. }
  159. /* Set ADC state */
  160. ADC_STATE_CLR_SET(hadc->State,
  161. HAL_ADC_STATE_BUSY_INTERNAL,
  162. HAL_ADC_STATE_READY);
  163. }
  164. else
  165. {
  166. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  167. /* Note: No need to update variable "tmp_hal_status" here: already set */
  168. /* to state "HAL_ERROR" by function disabling the ADC. */
  169. }
  170. /* Process unlocked */
  171. __HAL_UNLOCK(hadc);
  172. /* Return function status */
  173. return tmp_hal_status;
  174. }
  175. /**
  176. * @brief Get the calibration factor.
  177. * @param hadc ADC handle.
  178. * @param SingleDiff This parameter can be only:
  179. * @arg @ref ADC_SINGLE_ENDED Channel in mode input single ended
  180. * @arg @ref ADC_DIFFERENTIAL_ENDED Channel in mode input differential ended (1)
  181. *
  182. * (1) On STM32WB serie, parameter not available on devices: STM32WB10xx, STM32WB15xx.
  183. * @retval Calibration value.
  184. */
  185. uint32_t HAL_ADCEx_Calibration_GetValue(ADC_HandleTypeDef *hadc, uint32_t SingleDiff)
  186. {
  187. #if defined (ADC_SUPPORT_2_5_MSPS)
  188. UNUSED(SingleDiff);
  189. #endif
  190. /* Check the parameters */
  191. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  192. assert_param(IS_ADC_SINGLE_DIFFERENTIAL(SingleDiff));
  193. /* Return the selected ADC calibration value */
  194. #if defined (ADC_SUPPORT_2_5_MSPS)
  195. return LL_ADC_GetCalibrationFactor(hadc->Instance);
  196. #else
  197. return LL_ADC_GetCalibrationFactor(hadc->Instance, SingleDiff);
  198. #endif
  199. }
  200. /**
  201. * @brief Set the calibration factor to overwrite automatic conversion result.
  202. * ADC must be enabled and no conversion is ongoing.
  203. * @param hadc ADC handle
  204. * @param SingleDiff This parameter can be only:
  205. * @arg @ref ADC_SINGLE_ENDED Channel in mode input single ended
  206. * @arg @ref ADC_DIFFERENTIAL_ENDED Channel in mode input differential ended (1)
  207. *
  208. * (1) On STM32WB serie, parameter not available on devices: STM32WB10xx, STM32WB15xx.
  209. * @param CalibrationFactor Calibration factor (coded on 7 bits maximum)
  210. * @retval HAL state
  211. */
  212. HAL_StatusTypeDef HAL_ADCEx_Calibration_SetValue(ADC_HandleTypeDef *hadc, uint32_t SingleDiff, uint32_t CalibrationFactor)
  213. {
  214. #if defined (ADC_SUPPORT_2_5_MSPS)
  215. UNUSED(SingleDiff);
  216. #endif
  217. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  218. uint32_t tmp_adc_is_conversion_on_going_regular;
  219. #if defined (ADC_SUPPORT_2_5_MSPS)
  220. /* Feature "ADC group injected" not available on ADC peripheral of this STM32WB device */
  221. #else
  222. uint32_t tmp_adc_is_conversion_on_going_injected;
  223. #endif /* ADC_SUPPORT_2_5_MSPS */
  224. /* Check the parameters */
  225. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  226. assert_param(IS_ADC_SINGLE_DIFFERENTIAL(SingleDiff));
  227. assert_param(IS_ADC_CALFACT(CalibrationFactor));
  228. /* Process locked */
  229. __HAL_LOCK(hadc);
  230. /* Verification of hardware constraints before modifying the calibration */
  231. /* factors register: ADC must be enabled, no conversion on going. */
  232. tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
  233. #if defined (ADC_SUPPORT_2_5_MSPS)
  234. /* Feature "ADC group injected" not available on ADC peripheral of this STM32WB device */
  235. #else
  236. tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
  237. #endif /* ADC_SUPPORT_2_5_MSPS */
  238. if ((LL_ADC_IsEnabled(hadc->Instance) != 0UL)
  239. && (tmp_adc_is_conversion_on_going_regular == 0UL)
  240. #if defined (ADC_SUPPORT_2_5_MSPS)
  241. /* Feature "ADC group injected" not available on ADC peripheral of this STM32WB device */
  242. #else
  243. && (tmp_adc_is_conversion_on_going_injected == 0UL)
  244. #endif /* ADC_SUPPORT_2_5_MSPS */
  245. )
  246. {
  247. /* Set the selected ADC calibration value */
  248. #if defined (ADC_SUPPORT_2_5_MSPS)
  249. LL_ADC_SetCalibrationFactor(hadc->Instance, CalibrationFactor);
  250. #else
  251. LL_ADC_SetCalibrationFactor(hadc->Instance, SingleDiff, CalibrationFactor);
  252. #endif
  253. }
  254. else
  255. {
  256. /* Update ADC state machine */
  257. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  258. /* Update ADC error code */
  259. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  260. /* Update ADC state machine to error */
  261. tmp_hal_status = HAL_ERROR;
  262. }
  263. /* Process unlocked */
  264. __HAL_UNLOCK(hadc);
  265. /* Return function status */
  266. return tmp_hal_status;
  267. }
  268. #if defined (ADC_SUPPORT_2_5_MSPS)
  269. /* Feature "ADC group injected" not available on ADC peripheral of this STM32WB device */
  270. #else
  271. /**
  272. * @brief Enable ADC, start conversion of injected group.
  273. * @note Interruptions enabled in this function: None.
  274. * @param hadc ADC handle.
  275. * @retval HAL status
  276. */
  277. HAL_StatusTypeDef HAL_ADCEx_InjectedStart(ADC_HandleTypeDef *hadc)
  278. {
  279. HAL_StatusTypeDef tmp_hal_status;
  280. uint32_t tmp_config_injected_queue;
  281. /* Check the parameters */
  282. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  283. if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) != 0UL)
  284. {
  285. return HAL_BUSY;
  286. }
  287. else
  288. {
  289. /* In case of software trigger detection enabled, JQDIS must be set
  290. (which can be done only if ADSTART and JADSTART are both cleared).
  291. If JQDIS is not set at that point, returns an error
  292. - since software trigger detection is disabled. User needs to
  293. resort to HAL_ADCEx_DisableInjectedQueue() API to set JQDIS.
  294. - or (if JQDIS is intentionally reset) since JEXTEN = 0 which means
  295. the queue is empty */
  296. tmp_config_injected_queue = READ_BIT(hadc->Instance->CFGR, ADC_CFGR_JQDIS);
  297. if ((READ_BIT(hadc->Instance->JSQR, ADC_JSQR_JEXTEN) == 0UL)
  298. && (tmp_config_injected_queue == 0UL)
  299. )
  300. {
  301. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  302. return HAL_ERROR;
  303. }
  304. /* Process locked */
  305. __HAL_LOCK(hadc);
  306. /* Enable the ADC peripheral */
  307. tmp_hal_status = ADC_Enable(hadc);
  308. /* Start conversion if ADC is effectively enabled */
  309. if (tmp_hal_status == HAL_OK)
  310. {
  311. /* Check if a regular conversion is ongoing */
  312. if ((hadc->State & HAL_ADC_STATE_REG_BUSY) != 0UL)
  313. {
  314. /* Reset ADC error code field related to injected conversions only */
  315. CLEAR_BIT(hadc->ErrorCode, HAL_ADC_ERROR_JQOVF);
  316. }
  317. else
  318. {
  319. /* Set ADC error code to none */
  320. ADC_CLEAR_ERRORCODE(hadc);
  321. }
  322. /* Set ADC state */
  323. /* - Clear state bitfield related to injected group conversion results */
  324. /* - Set state bitfield related to injected operation */
  325. ADC_STATE_CLR_SET(hadc->State,
  326. HAL_ADC_STATE_READY | HAL_ADC_STATE_INJ_EOC,
  327. HAL_ADC_STATE_INJ_BUSY);
  328. /* Clear ADC group injected group conversion flag */
  329. /* (To ensure of no unknown state from potential previous ADC operations) */
  330. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_JEOC | ADC_FLAG_JEOS));
  331. /* Process unlocked */
  332. /* Unlock before starting ADC conversions: in case of potential */
  333. /* interruption, to let the process to ADC IRQ Handler. */
  334. __HAL_UNLOCK(hadc);
  335. /* Enable conversion of injected group, if automatic injected conversion */
  336. /* is disabled. */
  337. /* If software start has been selected, conversion starts immediately. */
  338. /* If external trigger has been selected, conversion will start at next */
  339. /* trigger event. */
  340. if(LL_ADC_INJ_GetTrigAuto(hadc->Instance) == LL_ADC_INJ_TRIG_INDEPENDENT)
  341. {
  342. /* Start ADC group injected conversion */
  343. LL_ADC_INJ_StartConversion(hadc->Instance);
  344. }
  345. }
  346. else
  347. {
  348. /* Process unlocked */
  349. __HAL_UNLOCK(hadc);
  350. }
  351. /* Return function status */
  352. return tmp_hal_status;
  353. }
  354. }
  355. /**
  356. * @brief Stop conversion of injected channels. Disable ADC peripheral if
  357. * no regular conversion is on going.
  358. * @note If ADC must be disabled and if conversion is on going on
  359. * regular group, function HAL_ADC_Stop must be used to stop both
  360. * injected and regular groups, and disable the ADC.
  361. * @note If injected group mode auto-injection is enabled,
  362. * function HAL_ADC_Stop must be used.
  363. * @param hadc ADC handle.
  364. * @retval HAL status
  365. */
  366. HAL_StatusTypeDef HAL_ADCEx_InjectedStop(ADC_HandleTypeDef *hadc)
  367. {
  368. HAL_StatusTypeDef tmp_hal_status;
  369. /* Check the parameters */
  370. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  371. /* Process locked */
  372. __HAL_LOCK(hadc);
  373. /* 1. Stop potential conversion on going on injected group only. */
  374. tmp_hal_status = ADC_ConversionStop(hadc, ADC_INJECTED_GROUP);
  375. /* Disable ADC peripheral if injected conversions are effectively stopped */
  376. /* and if no conversion on regular group is on-going */
  377. if (tmp_hal_status == HAL_OK)
  378. {
  379. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
  380. {
  381. /* 2. Disable the ADC peripheral */
  382. tmp_hal_status = ADC_Disable(hadc);
  383. /* Check if ADC is effectively disabled */
  384. if (tmp_hal_status == HAL_OK)
  385. {
  386. /* Set ADC state */
  387. ADC_STATE_CLR_SET(hadc->State,
  388. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  389. HAL_ADC_STATE_READY);
  390. }
  391. }
  392. /* Conversion on injected group is stopped, but ADC not disabled since */
  393. /* conversion on regular group is still running. */
  394. else
  395. {
  396. /* Set ADC state */
  397. CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  398. }
  399. }
  400. /* Process unlocked */
  401. __HAL_UNLOCK(hadc);
  402. /* Return function status */
  403. return tmp_hal_status;
  404. }
  405. /**
  406. * @brief Wait for injected group conversion to be completed.
  407. * @param hadc ADC handle
  408. * @param Timeout Timeout value in millisecond.
  409. * @note Depending on hadc->Init.EOCSelection, JEOS or JEOC is
  410. * checked and cleared depending on AUTDLY bit status.
  411. * @retval HAL status
  412. */
  413. HAL_StatusTypeDef HAL_ADCEx_InjectedPollForConversion(ADC_HandleTypeDef *hadc, uint32_t Timeout)
  414. {
  415. uint32_t tickstart;
  416. uint32_t tmp_Flag_End;
  417. uint32_t tmp_adc_inj_is_trigger_source_sw_start;
  418. uint32_t tmp_adc_reg_is_trigger_source_sw_start;
  419. uint32_t tmp_cfgr;
  420. /* Check the parameters */
  421. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  422. /* If end of sequence selected */
  423. if (hadc->Init.EOCSelection == ADC_EOC_SEQ_CONV)
  424. {
  425. tmp_Flag_End = ADC_FLAG_JEOS;
  426. }
  427. else /* end of conversion selected */
  428. {
  429. tmp_Flag_End = ADC_FLAG_JEOC;
  430. }
  431. /* Get timeout */
  432. tickstart = HAL_GetTick();
  433. /* Wait until End of Conversion or Sequence flag is raised */
  434. while ((hadc->Instance->ISR & tmp_Flag_End) == 0UL)
  435. {
  436. /* Check if timeout is disabled (set to infinite wait) */
  437. if (Timeout != HAL_MAX_DELAY)
  438. {
  439. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0UL))
  440. {
  441. /* Update ADC state machine to timeout */
  442. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  443. /* Process unlocked */
  444. __HAL_UNLOCK(hadc);
  445. return HAL_TIMEOUT;
  446. }
  447. }
  448. }
  449. /* Retrieve ADC configuration */
  450. tmp_adc_inj_is_trigger_source_sw_start = LL_ADC_INJ_IsTriggerSourceSWStart(hadc->Instance);
  451. tmp_adc_reg_is_trigger_source_sw_start = LL_ADC_REG_IsTriggerSourceSWStart(hadc->Instance);
  452. tmp_cfgr = READ_REG(hadc->Instance->CFGR);
  453. /* Update ADC state machine */
  454. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_EOC);
  455. /* Determine whether any further conversion upcoming on group injected */
  456. /* by external trigger or by automatic injected conversion */
  457. /* from group regular. */
  458. if ((tmp_adc_inj_is_trigger_source_sw_start != 0UL) ||
  459. ((READ_BIT(tmp_cfgr, ADC_CFGR_JAUTO) == 0UL) &&
  460. ((tmp_adc_reg_is_trigger_source_sw_start != 0UL) &&
  461. (READ_BIT(tmp_cfgr, ADC_CFGR_CONT) == 0UL))))
  462. {
  463. /* Check whether end of sequence is reached */
  464. if (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_JEOS))
  465. {
  466. /* Particular case if injected contexts queue is enabled: */
  467. /* when the last context has been fully processed, JSQR is reset */
  468. /* by the hardware. Even if no injected conversion is planned to come */
  469. /* (queue empty, triggers are ignored), it can start again */
  470. /* immediately after setting a new context (JADSTART is still set). */
  471. /* Therefore, state of HAL ADC injected group is kept to busy. */
  472. if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_JQM) == 0UL)
  473. {
  474. /* Set ADC state */
  475. CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  476. if ((hadc->State & HAL_ADC_STATE_REG_BUSY) == 0UL)
  477. {
  478. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  479. }
  480. }
  481. }
  482. }
  483. /* Clear polled flag */
  484. if (tmp_Flag_End == ADC_FLAG_JEOS)
  485. {
  486. /* Clear end of sequence JEOS flag of injected group if low power feature */
  487. /* "LowPowerAutoWait " is disabled, to not interfere with this feature. */
  488. /* For injected groups, no new conversion will start before JEOS is */
  489. /* cleared. */
  490. if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_AUTDLY) == 0UL)
  491. {
  492. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_JEOC | ADC_FLAG_JEOS));
  493. }
  494. }
  495. else
  496. {
  497. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOC);
  498. }
  499. /* Return API HAL status */
  500. return HAL_OK;
  501. }
  502. /**
  503. * @brief Enable ADC, start conversion of injected group with interruption.
  504. * @note Interruptions enabled in this function according to initialization
  505. * setting : JEOC (end of conversion) or JEOS (end of sequence)
  506. * @param hadc ADC handle.
  507. * @retval HAL status.
  508. */
  509. HAL_StatusTypeDef HAL_ADCEx_InjectedStart_IT(ADC_HandleTypeDef *hadc)
  510. {
  511. HAL_StatusTypeDef tmp_hal_status;
  512. uint32_t tmp_config_injected_queue;
  513. /* Check the parameters */
  514. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  515. if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) != 0UL)
  516. {
  517. return HAL_BUSY;
  518. }
  519. else
  520. {
  521. /* In case of software trigger detection enabled, JQDIS must be set
  522. (which can be done only if ADSTART and JADSTART are both cleared).
  523. If JQDIS is not set at that point, returns an error
  524. - since software trigger detection is disabled. User needs to
  525. resort to HAL_ADCEx_DisableInjectedQueue() API to set JQDIS.
  526. - or (if JQDIS is intentionally reset) since JEXTEN = 0 which means
  527. the queue is empty */
  528. tmp_config_injected_queue = READ_BIT(hadc->Instance->CFGR, ADC_CFGR_JQDIS);
  529. if ((READ_BIT(hadc->Instance->JSQR, ADC_JSQR_JEXTEN) == 0UL)
  530. && (tmp_config_injected_queue == 0UL)
  531. )
  532. {
  533. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  534. return HAL_ERROR;
  535. }
  536. /* Process locked */
  537. __HAL_LOCK(hadc);
  538. /* Enable the ADC peripheral */
  539. tmp_hal_status = ADC_Enable(hadc);
  540. /* Start conversion if ADC is effectively enabled */
  541. if (tmp_hal_status == HAL_OK)
  542. {
  543. /* Check if a regular conversion is ongoing */
  544. if ((hadc->State & HAL_ADC_STATE_REG_BUSY) != 0UL)
  545. {
  546. /* Reset ADC error code field related to injected conversions only */
  547. CLEAR_BIT(hadc->ErrorCode, HAL_ADC_ERROR_JQOVF);
  548. }
  549. else
  550. {
  551. /* Set ADC error code to none */
  552. ADC_CLEAR_ERRORCODE(hadc);
  553. }
  554. /* Set ADC state */
  555. /* - Clear state bitfield related to injected group conversion results */
  556. /* - Set state bitfield related to injected operation */
  557. ADC_STATE_CLR_SET(hadc->State,
  558. HAL_ADC_STATE_READY | HAL_ADC_STATE_INJ_EOC,
  559. HAL_ADC_STATE_INJ_BUSY);
  560. /* Clear ADC group injected group conversion flag */
  561. /* (To ensure of no unknown state from potential previous ADC operations) */
  562. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_JEOC | ADC_FLAG_JEOS));
  563. /* Process unlocked */
  564. /* Unlock before starting ADC conversions: in case of potential */
  565. /* interruption, to let the process to ADC IRQ Handler. */
  566. __HAL_UNLOCK(hadc);
  567. /* Enable ADC Injected context queue overflow interrupt if this feature */
  568. /* is enabled. */
  569. if ((hadc->Instance->CFGR & ADC_CFGR_JQM) != 0UL)
  570. {
  571. __HAL_ADC_ENABLE_IT(hadc, ADC_FLAG_JQOVF);
  572. }
  573. /* Enable ADC end of conversion interrupt */
  574. switch (hadc->Init.EOCSelection)
  575. {
  576. case ADC_EOC_SEQ_CONV:
  577. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC);
  578. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOS);
  579. break;
  580. /* case ADC_EOC_SINGLE_CONV */
  581. default:
  582. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOS);
  583. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOC);
  584. break;
  585. }
  586. /* Enable conversion of injected group, if automatic injected conversion */
  587. /* is disabled. */
  588. /* If software start has been selected, conversion starts immediately. */
  589. /* If external trigger has been selected, conversion will start at next */
  590. /* trigger event. */
  591. if(LL_ADC_INJ_GetTrigAuto(hadc->Instance) == LL_ADC_INJ_TRIG_INDEPENDENT)
  592. {
  593. /* Start ADC group injected conversion */
  594. LL_ADC_INJ_StartConversion(hadc->Instance);
  595. }
  596. }
  597. else
  598. {
  599. /* Process unlocked */
  600. __HAL_UNLOCK(hadc);
  601. }
  602. /* Return function status */
  603. return tmp_hal_status;
  604. }
  605. }
  606. /**
  607. * @brief Stop conversion of injected channels, disable interruption of
  608. * end-of-conversion. Disable ADC peripheral if no regular conversion
  609. * is on going.
  610. * @note If ADC must be disabled and if conversion is on going on
  611. * regular group, function HAL_ADC_Stop must be used to stop both
  612. * injected and regular groups, and disable the ADC.
  613. * @note If injected group mode auto-injection is enabled,
  614. * function HAL_ADC_Stop must be used.
  615. * @note In case of auto-injection mode, HAL_ADC_Stop() must be used.
  616. * @param hadc ADC handle
  617. * @retval HAL status
  618. */
  619. HAL_StatusTypeDef HAL_ADCEx_InjectedStop_IT(ADC_HandleTypeDef *hadc)
  620. {
  621. HAL_StatusTypeDef tmp_hal_status;
  622. /* Check the parameters */
  623. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  624. /* Process locked */
  625. __HAL_LOCK(hadc);
  626. /* 1. Stop potential conversion on going on injected group only. */
  627. tmp_hal_status = ADC_ConversionStop(hadc, ADC_INJECTED_GROUP);
  628. /* Disable ADC peripheral if injected conversions are effectively stopped */
  629. /* and if no conversion on the other group (regular group) is intended to */
  630. /* continue. */
  631. if (tmp_hal_status == HAL_OK)
  632. {
  633. /* Disable ADC end of conversion interrupt for injected channels */
  634. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_JEOC | ADC_IT_JEOS | ADC_FLAG_JQOVF));
  635. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
  636. {
  637. /* 2. Disable the ADC peripheral */
  638. tmp_hal_status = ADC_Disable(hadc);
  639. /* Check if ADC is effectively disabled */
  640. if (tmp_hal_status == HAL_OK)
  641. {
  642. /* Set ADC state */
  643. ADC_STATE_CLR_SET(hadc->State,
  644. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  645. HAL_ADC_STATE_READY);
  646. }
  647. }
  648. /* Conversion on injected group is stopped, but ADC not disabled since */
  649. /* conversion on regular group is still running. */
  650. else
  651. {
  652. /* Set ADC state */
  653. CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  654. }
  655. }
  656. /* Process unlocked */
  657. __HAL_UNLOCK(hadc);
  658. /* Return function status */
  659. return tmp_hal_status;
  660. }
  661. #endif /* ADC_SUPPORT_2_5_MSPS */
  662. #if defined (ADC_SUPPORT_2_5_MSPS)
  663. /* Feature "ADC group injected" not available on ADC peripheral of this STM32WB device */
  664. #else
  665. /**
  666. * @brief Get ADC injected group conversion result.
  667. * @note Reading register JDRx automatically clears ADC flag JEOC
  668. * (ADC group injected end of unitary conversion).
  669. * @note This function does not clear ADC flag JEOS
  670. * (ADC group injected end of sequence conversion)
  671. * Occurrence of flag JEOS rising:
  672. * - If sequencer is composed of 1 rank, flag JEOS is equivalent
  673. * to flag JEOC.
  674. * - If sequencer is composed of several ranks, during the scan
  675. * sequence flag JEOC only is raised, at the end of the scan sequence
  676. * both flags JEOC and EOS are raised.
  677. * Flag JEOS must not be cleared by this function because
  678. * it would not be compliant with low power features
  679. * (feature low power auto-wait, not available on all STM32 families).
  680. * To clear this flag, either use function:
  681. * in programming model IT: @ref HAL_ADC_IRQHandler(), in programming
  682. * model polling: @ref HAL_ADCEx_InjectedPollForConversion()
  683. * or @ref __HAL_ADC_CLEAR_FLAG(&hadc, ADC_FLAG_JEOS).
  684. * @param hadc ADC handle
  685. * @param InjectedRank the converted ADC injected rank.
  686. * This parameter can be one of the following values:
  687. * @arg @ref ADC_INJECTED_RANK_1 ADC group injected rank 1
  688. * @arg @ref ADC_INJECTED_RANK_2 ADC group injected rank 2
  689. * @arg @ref ADC_INJECTED_RANK_3 ADC group injected rank 3
  690. * @arg @ref ADC_INJECTED_RANK_4 ADC group injected rank 4
  691. * @retval ADC group injected conversion data
  692. */
  693. uint32_t HAL_ADCEx_InjectedGetValue(ADC_HandleTypeDef *hadc, uint32_t InjectedRank)
  694. {
  695. uint32_t tmp_jdr;
  696. /* Check the parameters */
  697. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  698. assert_param(IS_ADC_INJECTED_RANK(InjectedRank));
  699. /* Get ADC converted value */
  700. switch (InjectedRank)
  701. {
  702. case ADC_INJECTED_RANK_4:
  703. tmp_jdr = hadc->Instance->JDR4;
  704. break;
  705. case ADC_INJECTED_RANK_3:
  706. tmp_jdr = hadc->Instance->JDR3;
  707. break;
  708. case ADC_INJECTED_RANK_2:
  709. tmp_jdr = hadc->Instance->JDR2;
  710. break;
  711. case ADC_INJECTED_RANK_1:
  712. default:
  713. tmp_jdr = hadc->Instance->JDR1;
  714. break;
  715. }
  716. /* Return ADC converted value */
  717. return tmp_jdr;
  718. }
  719. /**
  720. * @brief Injected conversion complete callback in non-blocking mode.
  721. * @param hadc ADC handle
  722. * @retval None
  723. */
  724. __weak void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef *hadc)
  725. {
  726. /* Prevent unused argument(s) compilation warning */
  727. UNUSED(hadc);
  728. /* NOTE : This function should not be modified. When the callback is needed,
  729. function HAL_ADCEx_InjectedConvCpltCallback must be implemented in the user file.
  730. */
  731. }
  732. /**
  733. * @brief Injected context queue overflow callback.
  734. * @note This callback is called if injected context queue is enabled
  735. (parameter "QueueInjectedContext" in injected channel configuration)
  736. and if a new injected context is set when queue is full (maximum 2
  737. contexts).
  738. * @param hadc ADC handle
  739. * @retval None
  740. */
  741. __weak void HAL_ADCEx_InjectedQueueOverflowCallback(ADC_HandleTypeDef *hadc)
  742. {
  743. /* Prevent unused argument(s) compilation warning */
  744. UNUSED(hadc);
  745. /* NOTE : This function should not be modified. When the callback is needed,
  746. function HAL_ADCEx_InjectedQueueOverflowCallback must be implemented in the user file.
  747. */
  748. }
  749. #endif /* ADC_SUPPORT_2_5_MSPS */
  750. /**
  751. * @brief Analog watchdog 2 callback in non-blocking mode.
  752. * @param hadc ADC handle
  753. * @retval None
  754. */
  755. __weak void HAL_ADCEx_LevelOutOfWindow2Callback(ADC_HandleTypeDef *hadc)
  756. {
  757. /* Prevent unused argument(s) compilation warning */
  758. UNUSED(hadc);
  759. /* NOTE : This function should not be modified. When the callback is needed,
  760. function HAL_ADCEx_LevelOutOfWindow2Callback must be implemented in the user file.
  761. */
  762. }
  763. /**
  764. * @brief Analog watchdog 3 callback in non-blocking mode.
  765. * @param hadc ADC handle
  766. * @retval None
  767. */
  768. __weak void HAL_ADCEx_LevelOutOfWindow3Callback(ADC_HandleTypeDef *hadc)
  769. {
  770. /* Prevent unused argument(s) compilation warning */
  771. UNUSED(hadc);
  772. /* NOTE : This function should not be modified. When the callback is needed,
  773. function HAL_ADCEx_LevelOutOfWindow3Callback must be implemented in the user file.
  774. */
  775. }
  776. /**
  777. * @brief End Of Sampling callback in non-blocking mode.
  778. * @param hadc ADC handle
  779. * @retval None
  780. */
  781. __weak void HAL_ADCEx_EndOfSamplingCallback(ADC_HandleTypeDef *hadc)
  782. {
  783. /* Prevent unused argument(s) compilation warning */
  784. UNUSED(hadc);
  785. /* NOTE : This function should not be modified. When the callback is needed,
  786. function HAL_ADCEx_EndOfSamplingCallback must be implemented in the user file.
  787. */
  788. }
  789. #if defined (ADC_SUPPORT_2_5_MSPS)
  790. /* Feature "ADC group injected" not available on ADC peripheral of this STM32WB device */
  791. #else
  792. /**
  793. * @brief Stop ADC conversion of regular group (and injected channels in
  794. * case of auto_injection mode), disable ADC peripheral if no
  795. * conversion is on going on injected group.
  796. * @param hadc ADC handle
  797. * @retval HAL status.
  798. */
  799. HAL_StatusTypeDef HAL_ADCEx_RegularStop(ADC_HandleTypeDef *hadc)
  800. {
  801. HAL_StatusTypeDef tmp_hal_status;
  802. /* Check the parameters */
  803. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  804. /* Process locked */
  805. __HAL_LOCK(hadc);
  806. /* 1. Stop potential regular conversion on going */
  807. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_GROUP);
  808. /* Disable ADC peripheral if regular conversions are effectively stopped
  809. and if no injected conversions are on-going */
  810. if (tmp_hal_status == HAL_OK)
  811. {
  812. /* Clear HAL_ADC_STATE_REG_BUSY bit */
  813. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  814. if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) == 0UL)
  815. {
  816. /* 2. Disable the ADC peripheral */
  817. tmp_hal_status = ADC_Disable(hadc);
  818. /* Check if ADC is effectively disabled */
  819. if (tmp_hal_status == HAL_OK)
  820. {
  821. /* Set ADC state */
  822. ADC_STATE_CLR_SET(hadc->State,
  823. HAL_ADC_STATE_INJ_BUSY,
  824. HAL_ADC_STATE_READY);
  825. }
  826. }
  827. /* Conversion on injected group is stopped, but ADC not disabled since */
  828. /* conversion on regular group is still running. */
  829. else
  830. {
  831. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  832. }
  833. }
  834. /* Process unlocked */
  835. __HAL_UNLOCK(hadc);
  836. /* Return function status */
  837. return tmp_hal_status;
  838. }
  839. /**
  840. * @brief Stop ADC conversion of ADC groups regular and injected,
  841. * disable interrution of end-of-conversion,
  842. * disable ADC peripheral if no conversion is on going
  843. * on injected group.
  844. * @param hadc ADC handle
  845. * @retval HAL status.
  846. */
  847. HAL_StatusTypeDef HAL_ADCEx_RegularStop_IT(ADC_HandleTypeDef *hadc)
  848. {
  849. HAL_StatusTypeDef tmp_hal_status;
  850. /* Check the parameters */
  851. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  852. /* Process locked */
  853. __HAL_LOCK(hadc);
  854. /* 1. Stop potential regular conversion on going */
  855. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_GROUP);
  856. /* Disable ADC peripheral if conversions are effectively stopped
  857. and if no injected conversion is on-going */
  858. if (tmp_hal_status == HAL_OK)
  859. {
  860. /* Clear HAL_ADC_STATE_REG_BUSY bit */
  861. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  862. /* Disable all regular-related interrupts */
  863. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_EOS | ADC_IT_OVR));
  864. /* 2. Disable ADC peripheral if no injected conversions are on-going */
  865. if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) == 0UL)
  866. {
  867. tmp_hal_status = ADC_Disable(hadc);
  868. /* if no issue reported */
  869. if (tmp_hal_status == HAL_OK)
  870. {
  871. /* Set ADC state */
  872. ADC_STATE_CLR_SET(hadc->State,
  873. HAL_ADC_STATE_INJ_BUSY,
  874. HAL_ADC_STATE_READY);
  875. }
  876. }
  877. else
  878. {
  879. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  880. }
  881. }
  882. /* Process unlocked */
  883. __HAL_UNLOCK(hadc);
  884. /* Return function status */
  885. return tmp_hal_status;
  886. }
  887. /**
  888. * @brief Stop ADC conversion of regular group (and injected group in
  889. * case of auto_injection mode), disable ADC DMA transfer, disable
  890. * ADC peripheral if no conversion is on going
  891. * on injected group.
  892. * @param hadc ADC handle
  893. * @retval HAL status.
  894. */
  895. HAL_StatusTypeDef HAL_ADCEx_RegularStop_DMA(ADC_HandleTypeDef *hadc)
  896. {
  897. HAL_StatusTypeDef tmp_hal_status;
  898. /* Check the parameters */
  899. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  900. /* Process locked */
  901. __HAL_LOCK(hadc);
  902. /* 1. Stop potential regular conversion on going */
  903. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_GROUP);
  904. /* Disable ADC peripheral if conversions are effectively stopped
  905. and if no injected conversion is on-going */
  906. if (tmp_hal_status == HAL_OK)
  907. {
  908. /* Clear HAL_ADC_STATE_REG_BUSY bit */
  909. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  910. /* Disable ADC DMA (ADC DMA configuration ADC_CFGR_DMACFG is kept) */
  911. CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_DMAEN);
  912. /* Disable the DMA channel (in case of DMA in circular mode or stop while */
  913. /* while DMA transfer is on going) */
  914. tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
  915. /* Check if DMA channel effectively disabled */
  916. if (tmp_hal_status != HAL_OK)
  917. {
  918. /* Update ADC state machine to error */
  919. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  920. }
  921. /* Disable ADC overrun interrupt */
  922. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
  923. /* 2. Disable the ADC peripheral */
  924. /* Update "tmp_hal_status" only if DMA channel disabling passed, */
  925. /* to keep in memory a potential failing status. */
  926. if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) == 0UL)
  927. {
  928. if (tmp_hal_status == HAL_OK)
  929. {
  930. tmp_hal_status = ADC_Disable(hadc);
  931. }
  932. else
  933. {
  934. (void)ADC_Disable(hadc);
  935. }
  936. /* Check if ADC is effectively disabled */
  937. if (tmp_hal_status == HAL_OK)
  938. {
  939. /* Set ADC state */
  940. ADC_STATE_CLR_SET(hadc->State,
  941. HAL_ADC_STATE_INJ_BUSY,
  942. HAL_ADC_STATE_READY);
  943. }
  944. }
  945. else
  946. {
  947. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  948. }
  949. }
  950. /* Process unlocked */
  951. __HAL_UNLOCK(hadc);
  952. /* Return function status */
  953. return tmp_hal_status;
  954. }
  955. #endif /* ADC_SUPPORT_2_5_MSPS */
  956. /**
  957. * @}
  958. */
  959. #if defined (ADC_SUPPORT_2_5_MSPS)
  960. /* Feature "ADC group injected" not available on ADC peripheral of this STM32WB device */
  961. #else
  962. /** @defgroup ADCEx_Exported_Functions_Group2 ADC Extended Peripheral Control functions
  963. * @brief ADC Extended Peripheral Control functions
  964. *
  965. @verbatim
  966. ===============================================================================
  967. ##### Peripheral Control functions #####
  968. ===============================================================================
  969. [..] This section provides functions allowing to:
  970. (+) Configure channels on injected group
  971. (+) Enable or Disable Injected Queue
  972. (+) Disable ADC voltage regulator
  973. (+) Enter ADC deep-power-down mode
  974. @endverbatim
  975. * @{
  976. */
  977. /**
  978. * @brief Configure a channel to be assigned to ADC group injected.
  979. * @note Possibility to update parameters on the fly:
  980. * This function initializes injected group, following calls to this
  981. * function can be used to reconfigure some parameters of structure
  982. * "ADC_InjectionConfTypeDef" on the fly, without resetting the ADC.
  983. * The setting of these parameters is conditioned to ADC state:
  984. * Refer to comments of structure "ADC_InjectionConfTypeDef".
  985. * @note In case of usage of internal measurement channels:
  986. * Vbat/VrefInt/TempSensor.
  987. * These internal paths can be disabled using function
  988. * HAL_ADC_DeInit().
  989. * @note Caution: For Injected Context Queue use, a context must be fully
  990. * defined before start of injected conversion. All channels are configured
  991. * consecutively for the same ADC instance. Therefore, the number of calls to
  992. * HAL_ADCEx_InjectedConfigChannel() must be equal to the value of parameter
  993. * InjectedNbrOfConversion for each context.
  994. * - Example 1: If 1 context is intended to be used (or if there is no use of the
  995. * Injected Queue Context feature) and if the context contains 3 injected ranks
  996. * (InjectedNbrOfConversion = 3), HAL_ADCEx_InjectedConfigChannel() must be
  997. * called once for each channel (i.e. 3 times) before starting a conversion.
  998. * This function must not be called to configure a 4th injected channel:
  999. * it would start a new context into context queue.
  1000. * - Example 2: If 2 contexts are intended to be used and each of them contains
  1001. * 3 injected ranks (InjectedNbrOfConversion = 3),
  1002. * HAL_ADCEx_InjectedConfigChannel() must be called once for each channel and
  1003. * for each context (3 channels x 2 contexts = 6 calls). Conversion can
  1004. * start once the 1st context is set, that is after the first three
  1005. * HAL_ADCEx_InjectedConfigChannel() calls. The 2nd context can be set on the fly.
  1006. * @param hadc ADC handle
  1007. * @param sConfigInjected Structure of ADC injected group and ADC channel for
  1008. * injected group.
  1009. * @retval HAL status
  1010. */
  1011. HAL_StatusTypeDef HAL_ADCEx_InjectedConfigChannel(ADC_HandleTypeDef *hadc, ADC_InjectionConfTypeDef *sConfigInjected)
  1012. {
  1013. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1014. uint32_t tmpOffsetShifted;
  1015. uint32_t tmp_config_internal_channel;
  1016. uint32_t tmp_adc_is_conversion_on_going_regular;
  1017. uint32_t tmp_adc_is_conversion_on_going_injected;
  1018. __IO uint32_t wait_loop_index = 0;
  1019. uint32_t tmp_JSQR_ContextQueueBeingBuilt = 0U;
  1020. /* Check the parameters */
  1021. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1022. assert_param(IS_ADC_SAMPLE_TIME(sConfigInjected->InjectedSamplingTime));
  1023. assert_param(IS_ADC_SINGLE_DIFFERENTIAL(sConfigInjected->InjectedSingleDiff));
  1024. assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->AutoInjectedConv));
  1025. assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->QueueInjectedContext));
  1026. assert_param(IS_ADC_EXTTRIGINJEC_EDGE(sConfigInjected->ExternalTrigInjecConvEdge));
  1027. assert_param(IS_ADC_EXTTRIGINJEC(hadc, sConfigInjected->ExternalTrigInjecConv));
  1028. assert_param(IS_ADC_OFFSET_NUMBER(sConfigInjected->InjectedOffsetNumber));
  1029. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), sConfigInjected->InjectedOffset));
  1030. assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->InjecOversamplingMode));
  1031. if (hadc->Init.ScanConvMode != ADC_SCAN_DISABLE)
  1032. {
  1033. assert_param(IS_ADC_INJECTED_RANK(sConfigInjected->InjectedRank));
  1034. assert_param(IS_ADC_INJECTED_NB_CONV(sConfigInjected->InjectedNbrOfConversion));
  1035. assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->InjectedDiscontinuousConvMode));
  1036. }
  1037. /* if JOVSE is set, the value of the OFFSETy_EN bit in ADCx_OFRy register is
  1038. ignored (considered as reset) */
  1039. assert_param(!((sConfigInjected->InjectedOffsetNumber != ADC_OFFSET_NONE) && (sConfigInjected->InjecOversamplingMode == ENABLE)));
  1040. /* JDISCEN and JAUTO bits can't be set at the same time */
  1041. assert_param(!((sConfigInjected->InjectedDiscontinuousConvMode == ENABLE) && (sConfigInjected->AutoInjectedConv == ENABLE)));
  1042. /* DISCEN and JAUTO bits can't be set at the same time */
  1043. assert_param(!((hadc->Init.DiscontinuousConvMode == ENABLE) && (sConfigInjected->AutoInjectedConv == ENABLE)));
  1044. /* Verification of channel number */
  1045. if (sConfigInjected->InjectedSingleDiff != ADC_DIFFERENTIAL_ENDED)
  1046. {
  1047. assert_param(IS_ADC_CHANNEL(hadc, sConfigInjected->InjectedChannel));
  1048. }
  1049. else
  1050. {
  1051. assert_param(IS_ADC_DIFF_CHANNEL(hadc, sConfigInjected->InjectedChannel));
  1052. }
  1053. /* Process locked */
  1054. __HAL_LOCK(hadc);
  1055. /* Configuration of injected group sequencer: */
  1056. /* Hardware constraint: Must fully define injected context register JSQR */
  1057. /* before make it entering into injected sequencer queue. */
  1058. /* */
  1059. /* - if scan mode is disabled: */
  1060. /* * Injected channels sequence length is set to 0x00: 1 channel */
  1061. /* converted (channel on injected rank 1) */
  1062. /* Parameter "InjectedNbrOfConversion" is discarded. */
  1063. /* * Injected context register JSQR setting is simple: register is fully */
  1064. /* defined on one call of this function (for injected rank 1) and can */
  1065. /* be entered into queue directly. */
  1066. /* - if scan mode is enabled: */
  1067. /* * Injected channels sequence length is set to parameter */
  1068. /* "InjectedNbrOfConversion". */
  1069. /* * Injected context register JSQR setting more complex: register is */
  1070. /* fully defined over successive calls of this function, for each */
  1071. /* injected channel rank. It is entered into queue only when all */
  1072. /* injected ranks have been set. */
  1073. /* Note: Scan mode is not present by hardware on this device, but used */
  1074. /* by software for alignment over all STM32 devices. */
  1075. if ((hadc->Init.ScanConvMode == ADC_SCAN_DISABLE) ||
  1076. (sConfigInjected->InjectedNbrOfConversion == 1U))
  1077. {
  1078. /* Configuration of context register JSQR: */
  1079. /* - number of ranks in injected group sequencer: fixed to 1st rank */
  1080. /* (scan mode disabled, only rank 1 used) */
  1081. /* - external trigger to start conversion */
  1082. /* - external trigger polarity */
  1083. /* - channel set to rank 1 (scan mode disabled, only rank 1 can be used) */
  1084. if (sConfigInjected->InjectedRank == ADC_INJECTED_RANK_1)
  1085. {
  1086. /* Enable external trigger if trigger selection is different of */
  1087. /* software start. */
  1088. /* Note: This configuration keeps the hardware feature of parameter */
  1089. /* ExternalTrigInjecConvEdge "trigger edge none" equivalent to */
  1090. /* software start. */
  1091. if (sConfigInjected->ExternalTrigInjecConv != ADC_INJECTED_SOFTWARE_START)
  1092. {
  1093. tmp_JSQR_ContextQueueBeingBuilt = (ADC_JSQR_RK(sConfigInjected->InjectedChannel, ADC_INJECTED_RANK_1)
  1094. | (sConfigInjected->ExternalTrigInjecConv & ADC_JSQR_JEXTSEL)
  1095. | sConfigInjected->ExternalTrigInjecConvEdge
  1096. );
  1097. }
  1098. else
  1099. {
  1100. tmp_JSQR_ContextQueueBeingBuilt = (ADC_JSQR_RK(sConfigInjected->InjectedChannel, ADC_INJECTED_RANK_1));
  1101. }
  1102. MODIFY_REG(hadc->Instance->JSQR, ADC_JSQR_FIELDS, tmp_JSQR_ContextQueueBeingBuilt);
  1103. /* For debug and informative reasons, hadc handle saves JSQR setting */
  1104. hadc->InjectionConfig.ContextQueue = tmp_JSQR_ContextQueueBeingBuilt;
  1105. }
  1106. }
  1107. else
  1108. {
  1109. /* Case of scan mode enabled, several channels to set into injected group */
  1110. /* sequencer. */
  1111. /* */
  1112. /* Procedure to define injected context register JSQR over successive */
  1113. /* calls of this function, for each injected channel rank: */
  1114. /* 1. Start new context and set parameters related to all injected */
  1115. /* channels: injected sequence length and trigger. */
  1116. /* if hadc->InjectionConfig.ChannelCount is equal to 0, this is the first */
  1117. /* call of the context under setting */
  1118. if (hadc->InjectionConfig.ChannelCount == 0U)
  1119. {
  1120. /* Initialize number of channels that will be configured on the context */
  1121. /* being built */
  1122. hadc->InjectionConfig.ChannelCount = sConfigInjected->InjectedNbrOfConversion;
  1123. /* Handle hadc saves the context under build up over each HAL_ADCEx_InjectedConfigChannel()
  1124. call, this context will be written in JSQR register at the last call.
  1125. At this point, the context is merely reset */
  1126. hadc->InjectionConfig.ContextQueue = 0x00000000U;
  1127. /* Configuration of context register JSQR: */
  1128. /* - number of ranks in injected group sequencer */
  1129. /* - external trigger to start conversion */
  1130. /* - external trigger polarity */
  1131. /* Enable external trigger if trigger selection is different of */
  1132. /* software start. */
  1133. /* Note: This configuration keeps the hardware feature of parameter */
  1134. /* ExternalTrigInjecConvEdge "trigger edge none" equivalent to */
  1135. /* software start. */
  1136. if (sConfigInjected->ExternalTrigInjecConv != ADC_INJECTED_SOFTWARE_START)
  1137. {
  1138. tmp_JSQR_ContextQueueBeingBuilt = ((sConfigInjected->InjectedNbrOfConversion - 1U)
  1139. | (sConfigInjected->ExternalTrigInjecConv & ADC_JSQR_JEXTSEL)
  1140. | sConfigInjected->ExternalTrigInjecConvEdge
  1141. );
  1142. }
  1143. else
  1144. {
  1145. tmp_JSQR_ContextQueueBeingBuilt = ((sConfigInjected->InjectedNbrOfConversion - 1U));
  1146. }
  1147. }
  1148. /* 2. Continue setting of context under definition with parameter */
  1149. /* related to each channel: channel rank sequence */
  1150. /* Clear the old JSQx bits for the selected rank */
  1151. tmp_JSQR_ContextQueueBeingBuilt &= ~ADC_JSQR_RK(ADC_SQR3_SQ10, sConfigInjected->InjectedRank);
  1152. /* Set the JSQx bits for the selected rank */
  1153. tmp_JSQR_ContextQueueBeingBuilt |= ADC_JSQR_RK(sConfigInjected->InjectedChannel, sConfigInjected->InjectedRank);
  1154. /* Decrease channel count */
  1155. hadc->InjectionConfig.ChannelCount--;
  1156. /* 3. tmp_JSQR_ContextQueueBeingBuilt is fully built for this HAL_ADCEx_InjectedConfigChannel()
  1157. call, aggregate the setting to those already built during the previous
  1158. HAL_ADCEx_InjectedConfigChannel() calls (for the same context of course) */
  1159. hadc->InjectionConfig.ContextQueue |= tmp_JSQR_ContextQueueBeingBuilt;
  1160. /* 4. End of context setting: if this is the last channel set, then write context
  1161. into register JSQR and make it enter into queue */
  1162. if (hadc->InjectionConfig.ChannelCount == 0U)
  1163. {
  1164. MODIFY_REG(hadc->Instance->JSQR, ADC_JSQR_FIELDS, hadc->InjectionConfig.ContextQueue);
  1165. }
  1166. }
  1167. /* Parameters update conditioned to ADC state: */
  1168. /* Parameters that can be updated when ADC is disabled or enabled without */
  1169. /* conversion on going on injected group: */
  1170. /* - Injected context queue: Queue disable (active context is kept) or */
  1171. /* enable (context decremented, up to 2 contexts queued) */
  1172. /* - Injected discontinuous mode: can be enabled only if auto-injected */
  1173. /* mode is disabled. */
  1174. if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) == 0UL)
  1175. {
  1176. /* If auto-injected mode is disabled: no constraint */
  1177. if (sConfigInjected->AutoInjectedConv == DISABLE)
  1178. {
  1179. MODIFY_REG(hadc->Instance->CFGR,
  1180. ADC_CFGR_JQM | ADC_CFGR_JDISCEN,
  1181. ADC_CFGR_INJECT_CONTEXT_QUEUE((uint32_t)sConfigInjected->QueueInjectedContext) |
  1182. ADC_CFGR_INJECT_DISCCONTINUOUS((uint32_t)sConfigInjected->InjectedDiscontinuousConvMode));
  1183. }
  1184. /* If auto-injected mode is enabled: Injected discontinuous setting is */
  1185. /* discarded. */
  1186. else
  1187. {
  1188. MODIFY_REG(hadc->Instance->CFGR,
  1189. ADC_CFGR_JQM | ADC_CFGR_JDISCEN,
  1190. ADC_CFGR_INJECT_CONTEXT_QUEUE((uint32_t)sConfigInjected->QueueInjectedContext));
  1191. }
  1192. }
  1193. /* Parameters update conditioned to ADC state: */
  1194. /* Parameters that can be updated when ADC is disabled or enabled without */
  1195. /* conversion on going on regular and injected groups: */
  1196. /* - Automatic injected conversion: can be enabled if injected group */
  1197. /* external triggers are disabled. */
  1198. /* - Channel sampling time */
  1199. /* - Channel offset */
  1200. tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
  1201. tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
  1202. if ((tmp_adc_is_conversion_on_going_regular == 0UL)
  1203. && (tmp_adc_is_conversion_on_going_injected == 0UL)
  1204. )
  1205. {
  1206. /* If injected group external triggers are disabled (set to injected */
  1207. /* software start): no constraint */
  1208. if ((sConfigInjected->ExternalTrigInjecConv == ADC_INJECTED_SOFTWARE_START)
  1209. || (sConfigInjected->ExternalTrigInjecConvEdge == ADC_EXTERNALTRIGINJECCONV_EDGE_NONE))
  1210. {
  1211. if (sConfigInjected->AutoInjectedConv == ENABLE)
  1212. {
  1213. SET_BIT(hadc->Instance->CFGR, ADC_CFGR_JAUTO);
  1214. }
  1215. else
  1216. {
  1217. CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_JAUTO);
  1218. }
  1219. }
  1220. /* If Automatic injected conversion was intended to be set and could not */
  1221. /* due to injected group external triggers enabled, error is reported. */
  1222. else
  1223. {
  1224. if (sConfigInjected->AutoInjectedConv == ENABLE)
  1225. {
  1226. /* Update ADC state machine to error */
  1227. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1228. tmp_hal_status = HAL_ERROR;
  1229. }
  1230. else
  1231. {
  1232. CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_JAUTO);
  1233. }
  1234. }
  1235. if (sConfigInjected->InjecOversamplingMode == ENABLE)
  1236. {
  1237. assert_param(IS_ADC_OVERSAMPLING_RATIO(sConfigInjected->InjecOversampling.Ratio));
  1238. assert_param(IS_ADC_RIGHT_BIT_SHIFT(sConfigInjected->InjecOversampling.RightBitShift));
  1239. /* JOVSE must be reset in case of triggered regular mode */
  1240. assert_param(!(READ_BIT(hadc->Instance->CFGR2, ADC_CFGR2_ROVSE | ADC_CFGR2_TROVS) == (ADC_CFGR2_ROVSE | ADC_CFGR2_TROVS)));
  1241. /* Configuration of Injected Oversampler: */
  1242. /* - Oversampling Ratio */
  1243. /* - Right bit shift */
  1244. /* Enable OverSampling mode */
  1245. MODIFY_REG(hadc->Instance->CFGR2,
  1246. ADC_CFGR2_JOVSE |
  1247. ADC_CFGR2_OVSR |
  1248. ADC_CFGR2_OVSS,
  1249. ADC_CFGR2_JOVSE |
  1250. sConfigInjected->InjecOversampling.Ratio |
  1251. sConfigInjected->InjecOversampling.RightBitShift
  1252. );
  1253. }
  1254. else
  1255. {
  1256. /* Disable Regular OverSampling */
  1257. CLEAR_BIT(hadc->Instance->CFGR2, ADC_CFGR2_JOVSE);
  1258. }
  1259. /* Set sampling time of the selected ADC channel */
  1260. LL_ADC_SetChannelSamplingTime(hadc->Instance, sConfigInjected->InjectedChannel, sConfigInjected->InjectedSamplingTime);
  1261. /* Configure the offset: offset enable/disable, channel, offset value */
  1262. /* Shift the offset with respect to the selected ADC resolution. */
  1263. /* Offset has to be left-aligned on bit 11, the LSB (right bits) are set to 0 */
  1264. tmpOffsetShifted = ADC_OFFSET_SHIFT_RESOLUTION(hadc, sConfigInjected->InjectedOffset);
  1265. if (sConfigInjected->InjectedOffsetNumber != ADC_OFFSET_NONE)
  1266. {
  1267. /* Set ADC selected offset number */
  1268. LL_ADC_SetOffset(hadc->Instance, sConfigInjected->InjectedOffsetNumber, sConfigInjected->InjectedChannel,
  1269. tmpOffsetShifted);
  1270. }
  1271. else
  1272. {
  1273. /* Scan each offset register to check if the selected channel is targeted. */
  1274. /* If this is the case, the corresponding offset number is disabled. */
  1275. if(__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_1)) == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfigInjected->InjectedChannel))
  1276. {
  1277. LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_1, LL_ADC_OFFSET_DISABLE);
  1278. }
  1279. if(__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_2)) == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfigInjected->InjectedChannel))
  1280. {
  1281. LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_2, LL_ADC_OFFSET_DISABLE);
  1282. }
  1283. if(__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_3)) == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfigInjected->InjectedChannel))
  1284. {
  1285. LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_3, LL_ADC_OFFSET_DISABLE);
  1286. }
  1287. if(__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_4)) == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfigInjected->InjectedChannel))
  1288. {
  1289. LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_4, LL_ADC_OFFSET_DISABLE);
  1290. }
  1291. }
  1292. }
  1293. /* Parameters update conditioned to ADC state: */
  1294. /* Parameters that can be updated only when ADC is disabled: */
  1295. /* - Single or differential mode */
  1296. /* - Internal measurement channels: Vbat/VrefInt/TempSensor */
  1297. if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
  1298. {
  1299. /* Set mode single-ended or differential input of the selected ADC channel */
  1300. LL_ADC_SetChannelSingleDiff(hadc->Instance, sConfigInjected->InjectedChannel, sConfigInjected->InjectedSingleDiff);
  1301. /* Configuration of differential mode */
  1302. /* Note: ADC channel number masked with value "0x1F" to ensure shift value within 32 bits range */
  1303. if (sConfigInjected->InjectedSingleDiff == ADC_DIFFERENTIAL_ENDED)
  1304. {
  1305. /* Set sampling time of the selected ADC channel */
  1306. LL_ADC_SetChannelSamplingTime(hadc->Instance, (uint32_t)(__LL_ADC_DECIMAL_NB_TO_CHANNEL((__LL_ADC_CHANNEL_TO_DECIMAL_NB((uint32_t)sConfigInjected->InjectedChannel) + 1UL) & 0x1FUL)), sConfigInjected->InjectedSamplingTime);
  1307. }
  1308. /* Management of internal measurement channels: Vbat/VrefInt/TempSensor */
  1309. /* internal measurement paths enable: If internal channel selected, */
  1310. /* enable dedicated internal buffers and path. */
  1311. /* Note: these internal measurement paths can be disabled using */
  1312. /* HAL_ADC_DeInit(). */
  1313. if(__LL_ADC_IS_CHANNEL_INTERNAL(sConfigInjected->InjectedChannel))
  1314. {
  1315. /* Configuration of common ADC parameters (continuation) */
  1316. /* Software is allowed to change common parameters only when all ADCs */
  1317. /* of the common group are disabled. */
  1318. if (__LL_ADC_IS_ENABLED_ALL_COMMON_INSTANCE(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) == 0UL)
  1319. {
  1320. tmp_config_internal_channel = LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
  1321. /* If the requested internal measurement path has already been enabled, */
  1322. /* bypass the configuration processing. */
  1323. if ((sConfigInjected->InjectedChannel == ADC_CHANNEL_TEMPSENSOR) && ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_TEMPSENSOR) == 0UL))
  1324. {
  1325. if (ADC_TEMPERATURE_SENSOR_INSTANCE(hadc))
  1326. {
  1327. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_TEMPSENSOR | tmp_config_internal_channel);
  1328. /* Delay for temperature sensor stabilization time */
  1329. /* Wait loop initialization and execution */
  1330. /* Note: Variable divided by 2 to compensate partially */
  1331. /* CPU processing cycles, scaling in us split to not */
  1332. /* exceed 32 bits register capacity and handle low frequency. */
  1333. wait_loop_index = ((LL_ADC_DELAY_TEMPSENSOR_STAB_US / 10UL) * (SystemCoreClock / (100000UL * 2UL)));
  1334. while(wait_loop_index != 0UL)
  1335. {
  1336. wait_loop_index--;
  1337. }
  1338. }
  1339. }
  1340. else if ((sConfigInjected->InjectedChannel == ADC_CHANNEL_VBAT) && ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_VBAT) == 0UL))
  1341. {
  1342. if (ADC_BATTERY_VOLTAGE_INSTANCE(hadc))
  1343. {
  1344. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_VBAT | tmp_config_internal_channel);
  1345. }
  1346. }
  1347. else if ((sConfigInjected->InjectedChannel == ADC_CHANNEL_VREFINT) && ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_VREFINT) == 0UL))
  1348. {
  1349. if (ADC_VREFINT_INSTANCE(hadc))
  1350. {
  1351. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_VREFINT | tmp_config_internal_channel);
  1352. }
  1353. }
  1354. else
  1355. {
  1356. /* nothing to do */
  1357. }
  1358. }
  1359. /* If the requested internal measurement path has already been enabled */
  1360. /* and other ADC of the common group are enabled, internal */
  1361. /* measurement paths cannot be enabled. */
  1362. else
  1363. {
  1364. /* Update ADC state machine to error */
  1365. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1366. tmp_hal_status = HAL_ERROR;
  1367. }
  1368. }
  1369. }
  1370. /* Process unlocked */
  1371. __HAL_UNLOCK(hadc);
  1372. /* Return function status */
  1373. return tmp_hal_status;
  1374. }
  1375. #endif /* ADC_SUPPORT_2_5_MSPS */
  1376. #if defined (ADC_SUPPORT_2_5_MSPS)
  1377. /* Feature "ADC group injected" not available on ADC peripheral of this STM32WB device */
  1378. #else
  1379. /**
  1380. * @brief Enable Injected Queue
  1381. * @note This function resets CFGR register JQDIS bit in order to enable the
  1382. * Injected Queue. JQDIS can be written only when ADSTART and JDSTART
  1383. * are both equal to 0 to ensure that no regular nor injected
  1384. * conversion is ongoing.
  1385. * @param hadc ADC handle
  1386. * @retval HAL status
  1387. */
  1388. HAL_StatusTypeDef HAL_ADCEx_EnableInjectedQueue(ADC_HandleTypeDef *hadc)
  1389. {
  1390. HAL_StatusTypeDef tmp_hal_status;
  1391. uint32_t tmp_adc_is_conversion_on_going_regular;
  1392. uint32_t tmp_adc_is_conversion_on_going_injected;
  1393. /* Check the parameters */
  1394. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1395. tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
  1396. tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
  1397. /* Parameter can be set only if no conversion is on-going */
  1398. if ((tmp_adc_is_conversion_on_going_regular == 0UL)
  1399. && (tmp_adc_is_conversion_on_going_injected == 0UL)
  1400. )
  1401. {
  1402. CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_JQDIS);
  1403. /* Update state, clear previous result related to injected queue overflow */
  1404. CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_JQOVF);
  1405. tmp_hal_status = HAL_OK;
  1406. }
  1407. else
  1408. {
  1409. tmp_hal_status = HAL_ERROR;
  1410. }
  1411. return tmp_hal_status;
  1412. }
  1413. /**
  1414. * @brief Disable Injected Queue
  1415. * @note This function sets CFGR register JQDIS bit in order to disable the
  1416. * Injected Queue. JQDIS can be written only when ADSTART and JDSTART
  1417. * are both equal to 0 to ensure that no regular nor injected
  1418. * conversion is ongoing.
  1419. * @param hadc ADC handle
  1420. * @retval HAL status
  1421. */
  1422. HAL_StatusTypeDef HAL_ADCEx_DisableInjectedQueue(ADC_HandleTypeDef *hadc)
  1423. {
  1424. HAL_StatusTypeDef tmp_hal_status;
  1425. uint32_t tmp_adc_is_conversion_on_going_regular;
  1426. uint32_t tmp_adc_is_conversion_on_going_injected;
  1427. /* Check the parameters */
  1428. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1429. tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
  1430. tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
  1431. /* Parameter can be set only if no conversion is on-going */
  1432. if ((tmp_adc_is_conversion_on_going_regular == 0UL)
  1433. && (tmp_adc_is_conversion_on_going_injected == 0UL)
  1434. )
  1435. {
  1436. LL_ADC_INJ_SetQueueMode(hadc->Instance, LL_ADC_INJ_QUEUE_DISABLE);
  1437. tmp_hal_status = HAL_OK;
  1438. }
  1439. else
  1440. {
  1441. tmp_hal_status = HAL_ERROR;
  1442. }
  1443. return tmp_hal_status;
  1444. }
  1445. #endif /* ADC_SUPPORT_2_5_MSPS */
  1446. /**
  1447. * @brief Disable ADC voltage regulator.
  1448. * @note Disabling voltage regulator allows to save power. This operation can
  1449. * be carried out only when ADC is disabled.
  1450. * @note To enable again the voltage regulator, the user is expected to
  1451. * resort to HAL_ADC_Init() API.
  1452. * @param hadc ADC handle
  1453. * @retval HAL status
  1454. */
  1455. HAL_StatusTypeDef HAL_ADCEx_DisableVoltageRegulator(ADC_HandleTypeDef *hadc)
  1456. {
  1457. HAL_StatusTypeDef tmp_hal_status;
  1458. /* Check the parameters */
  1459. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1460. /* Setting of this feature is conditioned to ADC state: ADC must be ADC disabled */
  1461. if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
  1462. {
  1463. LL_ADC_DisableInternalRegulator(hadc->Instance);
  1464. tmp_hal_status = HAL_OK;
  1465. }
  1466. else
  1467. {
  1468. tmp_hal_status = HAL_ERROR;
  1469. }
  1470. return tmp_hal_status;
  1471. }
  1472. #if defined (ADC_SUPPORT_2_5_MSPS)
  1473. /* Feature " ADC deep power-down" not available on ADC peripheral of this STM32WB device */
  1474. #else
  1475. /**
  1476. * @brief Enter ADC deep power-down mode
  1477. * @note This mode is achieved in setting DEEPPWD bit and allows to save power
  1478. * in reducing leakage currents. It is particularly interesting before
  1479. * entering stop modes.
  1480. * @note Setting DEEPPWD automatically clears ADVREGEN bit and disables the
  1481. * ADC voltage regulator. This means that this API encompasses
  1482. * HAL_ADCEx_DisableVoltageRegulator(). Additionally, the internal
  1483. * calibration is lost.
  1484. * @note To exit the ADC deep-power-down mode, the user is expected to
  1485. * resort to HAL_ADC_Init() API as well as to relaunch a calibration
  1486. * with HAL_ADCEx_Calibration_Start() API or to re-apply a previously
  1487. * saved calibration factor.
  1488. * @param hadc ADC handle
  1489. * @retval HAL status
  1490. */
  1491. HAL_StatusTypeDef HAL_ADCEx_EnterADCDeepPowerDownMode(ADC_HandleTypeDef *hadc)
  1492. {
  1493. HAL_StatusTypeDef tmp_hal_status;
  1494. /* Check the parameters */
  1495. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1496. /* Setting of this feature is conditioned to ADC state: ADC must be ADC disabled */
  1497. if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
  1498. {
  1499. LL_ADC_EnableDeepPowerDown(hadc->Instance);
  1500. tmp_hal_status = HAL_OK;
  1501. }
  1502. else
  1503. {
  1504. tmp_hal_status = HAL_ERROR;
  1505. }
  1506. return tmp_hal_status;
  1507. }
  1508. #endif
  1509. /**
  1510. * @}
  1511. */
  1512. /**
  1513. * @}
  1514. */
  1515. #endif /* HAL_ADC_MODULE_ENABLED */
  1516. /**
  1517. * @}
  1518. */
  1519. /**
  1520. * @}
  1521. */
  1522. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/