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.
 
 
 

2493 lines
95 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32h7xx_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 Convertor (ADC)
  7. * peripheral:
  8. * + Operation functions
  9. * ++ Start, stop, get result of conversions of ADC group injected,
  10. * using 2 possible modes: polling, interruption.
  11. * ++ Calibration
  12. * +++ ADC automatic self-calibration
  13. * +++ Calibration factors get or set
  14. * ++ Multimode feature when available
  15. * + Control functions
  16. * ++ Channels configuration on ADC group injected
  17. * + State functions
  18. * ++ ADC group injected contexts queue management
  19. * Other functions (generic functions) are available in file
  20. * "stm32h7xx_hal_adc.c".
  21. *
  22. @verbatim
  23. [..]
  24. (@) Sections "ADC peripheral features" and "How to use this driver" are
  25. available in file of generic functions "stm32h7xx_hal_adc.c".
  26. [..]
  27. @endverbatim
  28. ******************************************************************************
  29. * @attention
  30. *
  31. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  32. * All rights reserved.</center></h2>
  33. *
  34. * This software component is licensed by ST under BSD 3-Clause license,
  35. * the "License"; You may not use this file except in compliance with the
  36. * License. You may obtain a copy of the License at:
  37. * opensource.org/licenses/BSD-3-Clause
  38. *
  39. ******************************************************************************
  40. */
  41. /* Includes ------------------------------------------------------------------*/
  42. #include "stm32h7xx_hal.h"
  43. /** @addtogroup STM32H7xx_HAL_Driver
  44. * @{
  45. */
  46. /** @defgroup ADCEx ADCEx
  47. * @brief ADC Extended HAL module driver
  48. * @{
  49. */
  50. #ifdef HAL_ADC_MODULE_ENABLED
  51. /* Private typedef -----------------------------------------------------------*/
  52. /* Private define ------------------------------------------------------------*/
  53. /** @defgroup ADCEx_Private_Constants ADC Extended Private Constants
  54. * @{
  55. */
  56. #define ADC_JSQR_FIELDS ((ADC_JSQR_JL | ADC_JSQR_JEXTSEL | ADC_JSQR_JEXTEN |\
  57. ADC_JSQR_JSQ1 | ADC_JSQR_JSQ2 |\
  58. ADC_JSQR_JSQ3 | ADC_JSQR_JSQ4 )) /*!< ADC_JSQR fields of parameters that can be updated anytime
  59. once the ADC is enabled */
  60. /* Fixed timeout value for ADC calibration. */
  61. /* Fixed timeout value for ADC calibration. */
  62. /* Values defined to be higher than worst cases: low clock frequency, */
  63. /* maximum prescalers. */
  64. /* Ex of profile low frequency : f_ADC at 0.125 Mhz (minimum value */
  65. /* according to Data sheet), calibration_time MAX = 165010 / f_ADC */
  66. /* 165010 / 125000 = 1.32s */
  67. /* At maximum CPU speed (480 MHz), this means */
  68. /* 1.32 * 480 MHz = 633600000 CPU cycles */
  69. #define ADC_CALIBRATION_TIMEOUT (633600000U) /*!< ADC calibration time-out value */
  70. /**
  71. * @}
  72. */
  73. /* Private macro -------------------------------------------------------------*/
  74. /* Private variables ---------------------------------------------------------*/
  75. /* Private function prototypes -----------------------------------------------*/
  76. /* Exported functions --------------------------------------------------------*/
  77. /** @defgroup ADCEx_Exported_Functions ADC Extended Exported Functions
  78. * @{
  79. */
  80. /** @defgroup ADCEx_Exported_Functions_Group1 Extended Input and Output operation functions
  81. * @brief Extended IO operation functions
  82. *
  83. @verbatim
  84. ===============================================================================
  85. ##### IO operation functions #####
  86. ===============================================================================
  87. [..] This section provides functions allowing to:
  88. (+) Perform the ADC self-calibration for single or differential ending.
  89. (+) Get calibration factors for single or differential ending.
  90. (+) Set calibration factors for single or differential ending.
  91. (+) Start conversion of ADC group injected.
  92. (+) Stop conversion of ADC group injected.
  93. (+) Poll for conversion complete on ADC group injected.
  94. (+) Get result of ADC group injected channel conversion.
  95. (+) Start conversion of ADC group injected and enable interruptions.
  96. (+) Stop conversion of ADC group injected and disable interruptions.
  97. (+) When multimode feature is available, start multimode and enable DMA transfer.
  98. (+) Stop multimode and disable ADC DMA transfer.
  99. (+) Get result of multimode conversion.
  100. @endverbatim
  101. * @{
  102. */
  103. /**
  104. * @brief Perform an ADC automatic self-calibration
  105. * Calibration prerequisite: ADC must be disabled (execute this
  106. * function before HAL_ADC_Start() or after HAL_ADC_Stop() ).
  107. * @param hadc ADC handle
  108. * @param CalibrationMode Selection of calibration offset or
  109. * linear calibration offset.
  110. * @arg ADC_CALIB_OFFSET Channel in mode calibration offset
  111. * @arg ADC_CALIB_OFFSET_LINEARITY Channel in mode linear calibration offset
  112. * @param SingleDiff Selection of single-ended or differential input
  113. * This parameter can be one of the following values:
  114. * @arg @ref ADC_SINGLE_ENDED Channel in mode input single ended
  115. * @arg @ref ADC_DIFFERENTIAL_ENDED Channel in mode input differential ended
  116. * @retval HAL status
  117. */
  118. HAL_StatusTypeDef HAL_ADCEx_Calibration_Start(ADC_HandleTypeDef *hadc, uint32_t CalibrationMode, uint32_t SingleDiff)
  119. {
  120. HAL_StatusTypeDef tmp_hal_status;
  121. __IO uint32_t wait_loop_index = 0UL;
  122. /* Check the parameters */
  123. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  124. assert_param(IS_ADC_SINGLE_DIFFERENTIAL(SingleDiff));
  125. /* Process locked */
  126. __HAL_LOCK(hadc);
  127. /* Calibration prerequisite: ADC must be disabled. */
  128. /* Disable the ADC (if not already disabled) */
  129. tmp_hal_status = ADC_Disable(hadc);
  130. /* Check if ADC is effectively disabled */
  131. if (tmp_hal_status == HAL_OK)
  132. {
  133. /* Set ADC state */
  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. /* Start ADC calibration in mode single-ended or differential */
  138. LL_ADC_StartCalibration(hadc->Instance , CalibrationMode, SingleDiff );
  139. /* Wait for calibration completion */
  140. while (LL_ADC_IsCalibrationOnGoing(hadc->Instance) != 0UL)
  141. {
  142. wait_loop_index++;
  143. if (wait_loop_index >= ADC_CALIBRATION_TIMEOUT)
  144. {
  145. /* Update ADC state machine to error */
  146. ADC_STATE_CLR_SET(hadc->State,
  147. HAL_ADC_STATE_BUSY_INTERNAL,
  148. HAL_ADC_STATE_ERROR_INTERNAL);
  149. /* Process unlocked */
  150. __HAL_UNLOCK(hadc);
  151. return HAL_ERROR;
  152. }
  153. }
  154. /* Set ADC state */
  155. ADC_STATE_CLR_SET(hadc->State,
  156. HAL_ADC_STATE_BUSY_INTERNAL,
  157. HAL_ADC_STATE_READY);
  158. }
  159. else
  160. {
  161. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  162. /* Note: No need to update variable "tmp_hal_status" here: already set */
  163. /* to state "HAL_ERROR" by function disabling the ADC. */
  164. }
  165. /* Process unlocked */
  166. __HAL_UNLOCK(hadc);
  167. /* Return function status */
  168. return tmp_hal_status;
  169. }
  170. /**
  171. * @brief Get the calibration factor.
  172. * @param hadc ADC handle.
  173. * @param SingleDiff This parameter can be only:
  174. * @arg @ref ADC_SINGLE_ENDED Channel in mode input single ended
  175. * @arg @ref ADC_DIFFERENTIAL_ENDED Channel in mode input differential ended
  176. * @retval Calibration value.
  177. */
  178. uint32_t HAL_ADCEx_Calibration_GetValue(ADC_HandleTypeDef *hadc, uint32_t SingleDiff)
  179. {
  180. /* Check the parameters */
  181. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  182. assert_param(IS_ADC_SINGLE_DIFFERENTIAL(SingleDiff));
  183. /* Return the selected ADC calibration value */
  184. return LL_ADC_GetCalibrationOffsetFactor(hadc->Instance, SingleDiff);
  185. }
  186. /**
  187. * @brief Get the calibration factor from automatic conversion result
  188. * @param hadc ADC handle
  189. * @param LinearCalib_Buffer: Linear calibration factor
  190. * @retval HAL state
  191. */
  192. HAL_StatusTypeDef HAL_ADCEx_LinearCalibration_GetValue(ADC_HandleTypeDef* hadc, uint32_t* LinearCalib_Buffer)
  193. {
  194. uint32_t cnt;
  195. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  196. uint32_t temp_REG_IsConversionOngoing = 0UL;
  197. /* Check the parameters */
  198. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  199. /* Enable the ADC ADEN = 1 to be able to read the linear calibration factor */
  200. if(LL_ADC_IsEnabled(hadc->Instance) == 0UL)
  201. {
  202. tmp_hal_status = ADC_Enable(hadc);
  203. }
  204. if (tmp_hal_status == HAL_OK)
  205. {
  206. if(LL_ADC_REG_IsConversionOngoing(hadc->Instance) != 0UL)
  207. {
  208. LL_ADC_REG_StopConversion(hadc->Instance);
  209. temp_REG_IsConversionOngoing = 1UL;
  210. }
  211. for(cnt = ADC_LINEAR_CALIB_REG_COUNT; cnt > 0UL; cnt--)
  212. {
  213. LinearCalib_Buffer[cnt-1U]=LL_ADC_GetCalibrationLinearFactor(hadc->Instance, ADC_CR_LINCALRDYW6 >> (ADC_LINEAR_CALIB_REG_COUNT-cnt));
  214. }
  215. if(temp_REG_IsConversionOngoing != 0UL)
  216. {
  217. LL_ADC_REG_StartConversion(hadc->Instance);
  218. }
  219. }
  220. return tmp_hal_status;
  221. }
  222. /**
  223. * @brief Set the calibration factor to overwrite automatic conversion result.
  224. * ADC must be enabled and no conversion is ongoing.
  225. * @param hadc ADC handle
  226. * @param SingleDiff This parameter can be only:
  227. * @arg @ref ADC_SINGLE_ENDED Channel in mode input single ended
  228. * @arg @ref ADC_DIFFERENTIAL_ENDED Channel in mode input differential ended
  229. * @param CalibrationFactor Calibration factor (coded on 7 bits maximum)
  230. * @retval HAL state
  231. */
  232. HAL_StatusTypeDef HAL_ADCEx_Calibration_SetValue(ADC_HandleTypeDef *hadc, uint32_t SingleDiff, uint32_t CalibrationFactor)
  233. {
  234. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  235. uint32_t tmp_adc_is_conversion_on_going_regular;
  236. uint32_t tmp_adc_is_conversion_on_going_injected;
  237. /* Check the parameters */
  238. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  239. assert_param(IS_ADC_SINGLE_DIFFERENTIAL(SingleDiff));
  240. assert_param(IS_ADC_CALFACT(CalibrationFactor));
  241. /* Process locked */
  242. __HAL_LOCK(hadc);
  243. /* Verification of hardware constraints before modifying the calibration */
  244. /* factors register: ADC must be enabled, no conversion on going. */
  245. tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
  246. tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
  247. if ((LL_ADC_IsEnabled(hadc->Instance) != 0UL)
  248. && (tmp_adc_is_conversion_on_going_regular == 0UL)
  249. && (tmp_adc_is_conversion_on_going_injected == 0UL)
  250. )
  251. {
  252. /* Set the selected ADC calibration value */
  253. LL_ADC_SetCalibrationOffsetFactor(hadc->Instance, SingleDiff, CalibrationFactor);
  254. }
  255. else
  256. {
  257. /* Update ADC state machine */
  258. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  259. /* Update ADC error code */
  260. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  261. /* Update ADC state machine to error */
  262. tmp_hal_status = HAL_ERROR;
  263. }
  264. /* Process unlocked */
  265. __HAL_UNLOCK(hadc);
  266. /* Return function status */
  267. return tmp_hal_status;
  268. }
  269. /**
  270. * @brief Set the linear calibration factor
  271. * @param hadc ADC handle
  272. * @param LinearCalib_Buffer: Linear calibration factor
  273. * @retval HAL state
  274. */
  275. HAL_StatusTypeDef HAL_ADCEx_LinearCalibration_SetValue(ADC_HandleTypeDef *hadc, uint32_t* LinearCalib_Buffer)
  276. {
  277. uint32_t cnt;
  278. __IO uint32_t wait_loop_index = 0;
  279. uint32_t temp_REG_IsConversionOngoing = 0UL;
  280. /* Check the parameters */
  281. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  282. /* - Exit from deep-power-down mode and ADC voltage regulator enable */
  283. /* Exit deep power down mode if still in that state */
  284. if (HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_DEEPPWD))
  285. {
  286. /* Exit deep power down mode */
  287. CLEAR_BIT(hadc->Instance->CR, ADC_CR_DEEPPWD);
  288. /* System was in deep power down mode, calibration must
  289. be relaunched or a previously saved calibration factor
  290. re-applied once the ADC voltage regulator is enabled */
  291. }
  292. if (HAL_IS_BIT_CLR(hadc->Instance->CR, ADC_CR_ADVREGEN))
  293. {
  294. /* Enable ADC internal voltage regulator */
  295. SET_BIT(hadc->Instance->CR, ADC_CR_ADVREGEN);
  296. /* Delay for ADC stabilization time */
  297. /* Wait loop initialization and execution */
  298. /* Note: Variable divided by 2 to compensate partially */
  299. /* CPU processing cycles. */
  300. wait_loop_index = (ADC_STAB_DELAY_US * (SystemCoreClock / (1000000UL * 2UL)));
  301. while(wait_loop_index != 0UL)
  302. {
  303. wait_loop_index--;
  304. }
  305. }
  306. /* Verification that ADC voltage regulator is correctly enabled, whether */
  307. /* or not ADC is coming from state reset (if any potential problem of */
  308. /* clocking, voltage regulator would not be enabled). */
  309. if (HAL_IS_BIT_CLR(hadc->Instance->CR, ADC_CR_ADVREGEN))
  310. {
  311. /* Update ADC state machine to error */
  312. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  313. /* Set ADC error code to ADC peripheral internal error */
  314. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  315. return HAL_ERROR;
  316. }
  317. /* Enable the ADC peripheral */
  318. if(LL_ADC_IsEnabled(hadc->Instance) == 0UL) /* Enable the ADC if it is disabled */
  319. {
  320. if (ADC_Enable(hadc) != HAL_OK)
  321. {
  322. return HAL_ERROR;
  323. }
  324. else
  325. {
  326. for(cnt = ADC_LINEAR_CALIB_REG_COUNT; cnt > 0UL ; cnt--)
  327. {
  328. LL_ADC_SetCalibrationLinearFactor(hadc->Instance, ADC_CR_LINCALRDYW6 >> (ADC_LINEAR_CALIB_REG_COUNT-cnt), LinearCalib_Buffer[cnt-1U]);
  329. }
  330. (void)ADC_Disable(hadc);
  331. }
  332. }else /* ADC is already enabled, so no need to enable it but need to stop conversion */
  333. {
  334. if(LL_ADC_REG_IsConversionOngoing(hadc->Instance) != 0UL)
  335. {
  336. LL_ADC_REG_StopConversion(hadc->Instance);
  337. temp_REG_IsConversionOngoing = 1UL;
  338. }
  339. for(cnt = ADC_LINEAR_CALIB_REG_COUNT; cnt > 0UL ; cnt--)
  340. {
  341. LL_ADC_SetCalibrationLinearFactor(hadc->Instance, ADC_CR_LINCALRDYW6 >> (ADC_LINEAR_CALIB_REG_COUNT-cnt), LinearCalib_Buffer[cnt-1U]);
  342. }
  343. if(temp_REG_IsConversionOngoing != 0UL)
  344. {
  345. LL_ADC_REG_StartConversion(hadc->Instance);
  346. }
  347. }
  348. return HAL_OK;
  349. }
  350. /**
  351. * @brief Load the calibration factor from engi bytes
  352. * @param hadc ADC handle
  353. * @retval HAL state
  354. */
  355. HAL_StatusTypeDef HAL_ADCEx_LinearCalibration_FactorLoad(ADC_HandleTypeDef *hadc)
  356. {
  357. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  358. uint32_t cnt, FactorOffset;
  359. uint32_t LinearCalib_Buffer[ADC_LINEAR_CALIB_REG_COUNT];
  360. /* Linearity calibration is retrieved from engi bytes
  361. read values from registers and put them to the CALFACT2 register */
  362. /* If needed linearity calibration can be done in runtime using
  363. LL_ADC_GetCalibrationLinearFactor() */
  364. if(hadc->Instance == ADC1)
  365. {
  366. FactorOffset = 0UL;
  367. }else if(hadc->Instance == ADC2)
  368. {
  369. FactorOffset = 8UL;
  370. }else /*Case ADC3*/
  371. {
  372. FactorOffset = 16UL;
  373. }
  374. for (cnt = 0UL; cnt < ADC_LINEAR_CALIB_REG_COUNT; cnt++)
  375. {
  376. LinearCalib_Buffer[cnt] = *(uint32_t*)(ADC_LINEAR_CALIB_REG_1_ADDR + FactorOffset + cnt);
  377. }
  378. if (HAL_ADCEx_LinearCalibration_SetValue(hadc,(uint32_t*)LinearCalib_Buffer) != HAL_OK)
  379. {
  380. tmp_hal_status = HAL_ERROR;
  381. }
  382. return tmp_hal_status;
  383. }
  384. /**
  385. * @brief Enable ADC, start conversion of injected group.
  386. * @note Interruptions enabled in this function: None.
  387. * @note Case of multimode enabled when multimode feature is available:
  388. * HAL_ADCEx_InjectedStart() API must be called for ADC slave first,
  389. * then for ADC master.
  390. * For ADC slave, ADC is enabled only (conversion is not started).
  391. * For ADC master, ADC is enabled and multimode conversion is started.
  392. * @param hadc ADC handle.
  393. * @retval HAL status
  394. */
  395. HAL_StatusTypeDef HAL_ADCEx_InjectedStart(ADC_HandleTypeDef *hadc)
  396. {
  397. HAL_StatusTypeDef tmp_hal_status;
  398. uint32_t tmp_config_injected_queue;
  399. uint32_t tmp_multimode_config = LL_ADC_GetMultimode(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
  400. /* Check the parameters */
  401. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  402. if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) != 0UL)
  403. {
  404. return HAL_BUSY;
  405. }
  406. else
  407. {
  408. /* In case of software trigger detection enabled, JQDIS must be set
  409. (which can be done only if ADSTART and JADSTART are both cleared).
  410. If JQDIS is not set at that point, returns an error
  411. - since software trigger detection is disabled. User needs to
  412. resort to HAL_ADCEx_DisableInjectedQueue() API to set JQDIS.
  413. - or (if JQDIS is intentionally reset) since JEXTEN = 0 which means
  414. the queue is empty */
  415. tmp_config_injected_queue = READ_BIT(hadc->Instance->CFGR, ADC_CFGR_JQDIS);
  416. if ((READ_BIT(hadc->Instance->JSQR, ADC_JSQR_JEXTEN) == 0UL)
  417. && (tmp_config_injected_queue == 0UL)
  418. )
  419. {
  420. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  421. return HAL_ERROR;
  422. }
  423. /* Process locked */
  424. __HAL_LOCK(hadc);
  425. /* Enable the ADC peripheral */
  426. tmp_hal_status = ADC_Enable(hadc);
  427. /* Start conversion if ADC is effectively enabled */
  428. if (tmp_hal_status == HAL_OK)
  429. {
  430. /* Check if a regular conversion is ongoing */
  431. if ((hadc->State & HAL_ADC_STATE_REG_BUSY) != 0UL)
  432. {
  433. /* Reset ADC error code field related to injected conversions only */
  434. CLEAR_BIT(hadc->ErrorCode, HAL_ADC_ERROR_JQOVF);
  435. }
  436. else
  437. {
  438. /* Set ADC error code to none */
  439. ADC_CLEAR_ERRORCODE(hadc);
  440. }
  441. /* Set ADC state */
  442. /* - Clear state bitfield related to injected group conversion results */
  443. /* - Set state bitfield related to injected operation */
  444. ADC_STATE_CLR_SET(hadc->State,
  445. HAL_ADC_STATE_READY | HAL_ADC_STATE_INJ_EOC,
  446. HAL_ADC_STATE_INJ_BUSY);
  447. /* Reset HAL_ADC_STATE_MULTIMODE_SLAVE bit
  448. - if ADC instance is master or if multimode feature is not available
  449. - if multimode setting is disabled (ADC instance slave in independent mode) */
  450. if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
  451. || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
  452. )
  453. {
  454. CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  455. }
  456. /* Clear ADC group injected group conversion flag */
  457. /* (To ensure of no unknown state from potential previous ADC operations) */
  458. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_JEOC | ADC_FLAG_JEOS));
  459. /* Process unlocked */
  460. /* Unlock before starting ADC conversions: in case of potential */
  461. /* interruption, to let the process to ADC IRQ Handler. */
  462. __HAL_UNLOCK(hadc);
  463. /* Enable conversion of injected group, if automatic injected conversion */
  464. /* is disabled. */
  465. /* If software start has been selected, conversion starts immediately. */
  466. /* If external trigger has been selected, conversion will start at next */
  467. /* trigger event. */
  468. /* Case of multimode enabled (when multimode feature is available): */
  469. /* if ADC is slave, */
  470. /* - ADC is enabled only (conversion is not started), */
  471. /* - if multimode only concerns regular conversion, ADC is enabled */
  472. /* and conversion is started. */
  473. /* If ADC is master or independent, */
  474. /* - ADC is enabled and conversion is started. */
  475. if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
  476. || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
  477. || (tmp_multimode_config == LL_ADC_MULTI_DUAL_REG_SIMULT)
  478. || (tmp_multimode_config == LL_ADC_MULTI_DUAL_REG_INTERL)
  479. )
  480. {
  481. /* ADC instance is not a multimode slave instance with multimode injected conversions enabled */
  482. if (LL_ADC_INJ_GetTrigAuto(hadc->Instance) == LL_ADC_INJ_TRIG_INDEPENDENT)
  483. {
  484. LL_ADC_INJ_StartConversion(hadc->Instance);
  485. }
  486. }
  487. else
  488. {
  489. /* ADC instance is not a multimode slave instance with multimode injected conversions enabled */
  490. SET_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  491. }
  492. }
  493. else
  494. {
  495. /* Process unlocked */
  496. __HAL_UNLOCK(hadc);
  497. }
  498. /* Return function status */
  499. return tmp_hal_status;
  500. }
  501. }
  502. /**
  503. * @brief Stop conversion of injected channels. Disable ADC peripheral if
  504. * no regular conversion is on going.
  505. * @note If ADC must be disabled and if conversion is on going on
  506. * regular group, function HAL_ADC_Stop must be used to stop both
  507. * injected and regular groups, and disable the ADC.
  508. * @note If injected group mode auto-injection is enabled,
  509. * function HAL_ADC_Stop must be used.
  510. * @note In case of multimode enabled (when multimode feature is available),
  511. * HAL_ADCEx_InjectedStop() must be called for ADC master first, then for ADC slave.
  512. * For ADC master, conversion is stopped and ADC is disabled.
  513. * For ADC slave, ADC is disabled only (conversion stop of ADC master
  514. * has already stopped conversion of ADC slave).
  515. * @param hadc ADC handle.
  516. * @retval HAL status
  517. */
  518. HAL_StatusTypeDef HAL_ADCEx_InjectedStop(ADC_HandleTypeDef *hadc)
  519. {
  520. HAL_StatusTypeDef tmp_hal_status;
  521. /* Check the parameters */
  522. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  523. /* Process locked */
  524. __HAL_LOCK(hadc);
  525. /* 1. Stop potential conversion on going on injected group only. */
  526. tmp_hal_status = ADC_ConversionStop(hadc, ADC_INJECTED_GROUP);
  527. /* Disable ADC peripheral if injected conversions are effectively stopped */
  528. /* and if no conversion on regular group is on-going */
  529. if (tmp_hal_status == HAL_OK)
  530. {
  531. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
  532. {
  533. /* 2. Disable the ADC peripheral */
  534. tmp_hal_status = ADC_Disable(hadc);
  535. /* Check if ADC is effectively disabled */
  536. if (tmp_hal_status == HAL_OK)
  537. {
  538. /* Set ADC state */
  539. ADC_STATE_CLR_SET(hadc->State,
  540. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  541. HAL_ADC_STATE_READY);
  542. }
  543. }
  544. /* Conversion on injected group is stopped, but ADC not disabled since */
  545. /* conversion on regular group is still running. */
  546. else
  547. {
  548. /* Set ADC state */
  549. CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  550. }
  551. }
  552. /* Process unlocked */
  553. __HAL_UNLOCK(hadc);
  554. /* Return function status */
  555. return tmp_hal_status;
  556. }
  557. /**
  558. * @brief Wait for injected group conversion to be completed.
  559. * @param hadc ADC handle
  560. * @param Timeout Timeout value in millisecond.
  561. * @note Depending on hadc->Init.EOCSelection, JEOS or JEOC is
  562. * checked and cleared depending on AUTDLY bit status.
  563. * @retval HAL status
  564. */
  565. HAL_StatusTypeDef HAL_ADCEx_InjectedPollForConversion(ADC_HandleTypeDef *hadc, uint32_t Timeout)
  566. {
  567. uint32_t tickstart;
  568. uint32_t tmp_Flag_End;
  569. uint32_t tmp_adc_inj_is_trigger_source_sw_start;
  570. uint32_t tmp_adc_reg_is_trigger_source_sw_start;
  571. uint32_t tmp_cfgr;
  572. const ADC_TypeDef *tmpADC_Master;
  573. uint32_t tmp_multimode_config = LL_ADC_GetMultimode(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
  574. /* Check the parameters */
  575. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  576. /* If end of sequence selected */
  577. if (hadc->Init.EOCSelection == ADC_EOC_SEQ_CONV)
  578. {
  579. tmp_Flag_End = ADC_FLAG_JEOS;
  580. }
  581. else /* end of conversion selected */
  582. {
  583. tmp_Flag_End = ADC_FLAG_JEOC;
  584. }
  585. /* Get timeout */
  586. tickstart = HAL_GetTick();
  587. /* Wait until End of Conversion or Sequence flag is raised */
  588. while ((hadc->Instance->ISR & tmp_Flag_End) == 0UL)
  589. {
  590. /* Check if timeout is disabled (set to infinite wait) */
  591. if (Timeout != HAL_MAX_DELAY)
  592. {
  593. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0UL))
  594. {
  595. /* Update ADC state machine to timeout */
  596. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  597. /* Process unlocked */
  598. __HAL_UNLOCK(hadc);
  599. return HAL_TIMEOUT;
  600. }
  601. }
  602. }
  603. /* Retrieve ADC configuration */
  604. tmp_adc_inj_is_trigger_source_sw_start = LL_ADC_INJ_IsTriggerSourceSWStart(hadc->Instance);
  605. tmp_adc_reg_is_trigger_source_sw_start = LL_ADC_REG_IsTriggerSourceSWStart(hadc->Instance);
  606. /* Get relevant register CFGR in ADC instance of ADC master or slave */
  607. /* in function of multimode state (for devices with multimode */
  608. /* available). */
  609. if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
  610. || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
  611. || (tmp_multimode_config == LL_ADC_MULTI_DUAL_REG_SIMULT)
  612. || (tmp_multimode_config == LL_ADC_MULTI_DUAL_REG_INTERL)
  613. )
  614. {
  615. tmp_cfgr = READ_REG(hadc->Instance->CFGR);
  616. }
  617. else
  618. {
  619. tmpADC_Master = __LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance);
  620. tmp_cfgr = READ_REG(tmpADC_Master->CFGR);
  621. }
  622. /* Update ADC state machine */
  623. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_EOC);
  624. /* Determine whether any further conversion upcoming on group injected */
  625. /* by external trigger or by automatic injected conversion */
  626. /* from group regular. */
  627. if ((tmp_adc_inj_is_trigger_source_sw_start != 0UL) ||
  628. ((READ_BIT(tmp_cfgr, ADC_CFGR_JAUTO) == 0UL) &&
  629. ((tmp_adc_reg_is_trigger_source_sw_start != 0UL) &&
  630. (READ_BIT(tmp_cfgr, ADC_CFGR_CONT) == 0UL))))
  631. {
  632. /* Check whether end of sequence is reached */
  633. if (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_JEOS))
  634. {
  635. /* Particular case if injected contexts queue is enabled: */
  636. /* when the last context has been fully processed, JSQR is reset */
  637. /* by the hardware. Even if no injected conversion is planned to come */
  638. /* (queue empty, triggers are ignored), it can start again */
  639. /* immediately after setting a new context (JADSTART is still set). */
  640. /* Therefore, state of HAL ADC injected group is kept to busy. */
  641. if (READ_BIT(tmp_cfgr, ADC_CFGR_JQM) == 0UL)
  642. {
  643. /* Set ADC state */
  644. CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  645. if ((hadc->State & HAL_ADC_STATE_REG_BUSY) == 0UL)
  646. {
  647. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  648. }
  649. }
  650. }
  651. }
  652. /* Clear polled flag */
  653. if (tmp_Flag_End == ADC_FLAG_JEOS)
  654. {
  655. /* Clear end of sequence JEOS flag of injected group if low power feature */
  656. /* "LowPowerAutoWait " is disabled, to not interfere with this feature. */
  657. /* For injected groups, no new conversion will start before JEOS is */
  658. /* cleared. */
  659. if (READ_BIT(tmp_cfgr, ADC_CFGR_AUTDLY) == 0UL)
  660. {
  661. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_JEOC | ADC_FLAG_JEOS));
  662. }
  663. }
  664. else
  665. {
  666. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOC);
  667. }
  668. /* Return API HAL status */
  669. return HAL_OK;
  670. }
  671. /**
  672. * @brief Enable ADC, start conversion of injected group with interruption.
  673. * @note Interruptions enabled in this function according to initialization
  674. * setting : JEOC (end of conversion) or JEOS (end of sequence)
  675. * @note Case of multimode enabled (when multimode feature is enabled):
  676. * HAL_ADCEx_InjectedStart_IT() API must be called for ADC slave first,
  677. * then for ADC master.
  678. * For ADC slave, ADC is enabled only (conversion is not started).
  679. * For ADC master, ADC is enabled and multimode conversion is started.
  680. * @param hadc ADC handle.
  681. * @retval HAL status.
  682. */
  683. HAL_StatusTypeDef HAL_ADCEx_InjectedStart_IT(ADC_HandleTypeDef *hadc)
  684. {
  685. HAL_StatusTypeDef tmp_hal_status;
  686. uint32_t tmp_config_injected_queue;
  687. uint32_t tmp_multimode_config = LL_ADC_GetMultimode(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
  688. /* Check the parameters */
  689. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  690. if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) != 0UL)
  691. {
  692. return HAL_BUSY;
  693. }
  694. else
  695. {
  696. /* In case of software trigger detection enabled, JQDIS must be set
  697. (which can be done only if ADSTART and JADSTART are both cleared).
  698. If JQDIS is not set at that point, returns an error
  699. - since software trigger detection is disabled. User needs to
  700. resort to HAL_ADCEx_DisableInjectedQueue() API to set JQDIS.
  701. - or (if JQDIS is intentionally reset) since JEXTEN = 0 which means
  702. the queue is empty */
  703. tmp_config_injected_queue = READ_BIT(hadc->Instance->CFGR, ADC_CFGR_JQDIS);
  704. if ((READ_BIT(hadc->Instance->JSQR, ADC_JSQR_JEXTEN) == 0UL)
  705. && (tmp_config_injected_queue == 0UL)
  706. )
  707. {
  708. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  709. return HAL_ERROR;
  710. }
  711. /* Process locked */
  712. __HAL_LOCK(hadc);
  713. /* Enable the ADC peripheral */
  714. tmp_hal_status = ADC_Enable(hadc);
  715. /* Start conversion if ADC is effectively enabled */
  716. if (tmp_hal_status == HAL_OK)
  717. {
  718. /* Check if a regular conversion is ongoing */
  719. if ((hadc->State & HAL_ADC_STATE_REG_BUSY) != 0UL)
  720. {
  721. /* Reset ADC error code field related to injected conversions only */
  722. CLEAR_BIT(hadc->ErrorCode, HAL_ADC_ERROR_JQOVF);
  723. }
  724. else
  725. {
  726. /* Set ADC error code to none */
  727. ADC_CLEAR_ERRORCODE(hadc);
  728. }
  729. /* Set ADC state */
  730. /* - Clear state bitfield related to injected group conversion results */
  731. /* - Set state bitfield related to injected operation */
  732. ADC_STATE_CLR_SET(hadc->State,
  733. HAL_ADC_STATE_READY | HAL_ADC_STATE_INJ_EOC,
  734. HAL_ADC_STATE_INJ_BUSY);
  735. /* Reset HAL_ADC_STATE_MULTIMODE_SLAVE bit
  736. - if ADC instance is master or if multimode feature is not available
  737. - if multimode setting is disabled (ADC instance slave in independent mode) */
  738. if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
  739. || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
  740. )
  741. {
  742. CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  743. }
  744. /* Clear ADC group injected group conversion flag */
  745. /* (To ensure of no unknown state from potential previous ADC operations) */
  746. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_JEOC | ADC_FLAG_JEOS));
  747. /* Process unlocked */
  748. /* Unlock before starting ADC conversions: in case of potential */
  749. /* interruption, to let the process to ADC IRQ Handler. */
  750. __HAL_UNLOCK(hadc);
  751. /* Enable ADC Injected context queue overflow interrupt if this feature */
  752. /* is enabled. */
  753. if ((hadc->Instance->CFGR & ADC_CFGR_JQM) != 0UL)
  754. {
  755. __HAL_ADC_ENABLE_IT(hadc, ADC_FLAG_JQOVF);
  756. }
  757. /* Enable ADC end of conversion interrupt */
  758. switch (hadc->Init.EOCSelection)
  759. {
  760. case ADC_EOC_SEQ_CONV:
  761. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC);
  762. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOS);
  763. break;
  764. /* case ADC_EOC_SINGLE_CONV */
  765. default:
  766. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOS);
  767. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOC);
  768. break;
  769. }
  770. /* Enable conversion of injected group, if automatic injected conversion */
  771. /* is disabled. */
  772. /* If software start has been selected, conversion starts immediately. */
  773. /* If external trigger has been selected, conversion will start at next */
  774. /* trigger event. */
  775. /* Case of multimode enabled (when multimode feature is available): */
  776. /* if ADC is slave, */
  777. /* - ADC is enabled only (conversion is not started), */
  778. /* - if multimode only concerns regular conversion, ADC is enabled */
  779. /* and conversion is started. */
  780. /* If ADC is master or independent, */
  781. /* - ADC is enabled and conversion is started. */
  782. if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
  783. || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
  784. || (tmp_multimode_config == LL_ADC_MULTI_DUAL_REG_SIMULT)
  785. || (tmp_multimode_config == LL_ADC_MULTI_DUAL_REG_INTERL)
  786. )
  787. {
  788. /* ADC instance is not a multimode slave instance with multimode injected conversions enabled */
  789. if (LL_ADC_INJ_GetTrigAuto(hadc->Instance) == LL_ADC_INJ_TRIG_INDEPENDENT)
  790. {
  791. LL_ADC_INJ_StartConversion(hadc->Instance);
  792. }
  793. }
  794. else
  795. {
  796. /* ADC instance is not a multimode slave instance with multimode injected conversions enabled */
  797. SET_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  798. }
  799. }
  800. else
  801. {
  802. /* Process unlocked */
  803. __HAL_UNLOCK(hadc);
  804. }
  805. /* Return function status */
  806. return tmp_hal_status;
  807. }
  808. }
  809. /**
  810. * @brief Stop conversion of injected channels, disable interruption of
  811. * end-of-conversion. Disable ADC peripheral if no regular conversion
  812. * is on going.
  813. * @note If ADC must be disabled and if conversion is on going on
  814. * regular group, function HAL_ADC_Stop must be used to stop both
  815. * injected and regular groups, and disable the ADC.
  816. * @note If injected group mode auto-injection is enabled,
  817. * function HAL_ADC_Stop must be used.
  818. * @note Case of multimode enabled (when multimode feature is available):
  819. * HAL_ADCEx_InjectedStop_IT() API must be called for ADC master first,
  820. * then for ADC slave.
  821. * For ADC master, conversion is stopped and ADC is disabled.
  822. * For ADC slave, ADC is disabled only (conversion stop of ADC master
  823. * has already stopped conversion of ADC slave).
  824. * @note In case of auto-injection mode, HAL_ADC_Stop() must be used.
  825. * @param hadc ADC handle
  826. * @retval HAL status
  827. */
  828. HAL_StatusTypeDef HAL_ADCEx_InjectedStop_IT(ADC_HandleTypeDef *hadc)
  829. {
  830. HAL_StatusTypeDef tmp_hal_status;
  831. /* Check the parameters */
  832. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  833. /* Process locked */
  834. __HAL_LOCK(hadc);
  835. /* 1. Stop potential conversion on going on injected group only. */
  836. tmp_hal_status = ADC_ConversionStop(hadc, ADC_INJECTED_GROUP);
  837. /* Disable ADC peripheral if injected conversions are effectively stopped */
  838. /* and if no conversion on the other group (regular group) is intended to */
  839. /* continue. */
  840. if (tmp_hal_status == HAL_OK)
  841. {
  842. /* Disable ADC end of conversion interrupt for injected channels */
  843. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_JEOC | ADC_IT_JEOS | ADC_FLAG_JQOVF));
  844. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
  845. {
  846. /* 2. Disable the ADC peripheral */
  847. tmp_hal_status = ADC_Disable(hadc);
  848. /* Check if ADC is effectively disabled */
  849. if (tmp_hal_status == HAL_OK)
  850. {
  851. /* Set ADC state */
  852. ADC_STATE_CLR_SET(hadc->State,
  853. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  854. HAL_ADC_STATE_READY);
  855. }
  856. }
  857. /* Conversion on injected group is stopped, but ADC not disabled since */
  858. /* conversion on regular group is still running. */
  859. else
  860. {
  861. /* Set ADC state */
  862. CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  863. }
  864. }
  865. /* Process unlocked */
  866. __HAL_UNLOCK(hadc);
  867. /* Return function status */
  868. return tmp_hal_status;
  869. }
  870. /**
  871. * @brief Enable ADC, start MultiMode conversion and transfer regular results through DMA.
  872. * @note Multimode must have been previously configured using
  873. * HAL_ADCEx_MultiModeConfigChannel() function.
  874. * Interruptions enabled in this function:
  875. * overrun, DMA half transfer, DMA transfer complete.
  876. * Each of these interruptions has its dedicated callback function.
  877. * @note State field of Slave ADC handle is not updated in this configuration:
  878. * user should not rely on it for information related to Slave regular
  879. * conversions.
  880. * @param hadc ADC handle of ADC master (handle of ADC slave must not be used)
  881. * @param pData Destination Buffer address.
  882. * @param Length Length of data to be transferred from ADC peripheral to memory (in bytes).
  883. * @retval HAL status
  884. */
  885. HAL_StatusTypeDef HAL_ADCEx_MultiModeStart_DMA(ADC_HandleTypeDef *hadc, uint32_t *pData, uint32_t Length)
  886. {
  887. HAL_StatusTypeDef tmp_hal_status;
  888. ADC_HandleTypeDef tmphadcSlave;
  889. ADC_Common_TypeDef *tmpADC_Common;
  890. /* Check the parameters */
  891. assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
  892. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
  893. assert_param(IS_ADC_EXTTRIG_EDGE(hadc->Init.ExternalTrigConvEdge));
  894. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) != 0UL)
  895. {
  896. return HAL_BUSY;
  897. }
  898. else
  899. {
  900. /* Process locked */
  901. __HAL_LOCK(hadc);
  902. /* Set a temporary handle of the ADC slave associated to the ADC master */
  903. ADC_MULTI_SLAVE(hadc, &tmphadcSlave);
  904. if (tmphadcSlave.Instance == NULL)
  905. {
  906. /* Set ADC state */
  907. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  908. /* Process unlocked */
  909. __HAL_UNLOCK(hadc);
  910. return HAL_ERROR;
  911. }
  912. /* Enable the ADC peripherals: master and slave (in case if not already */
  913. /* enabled previously) */
  914. tmp_hal_status = ADC_Enable(hadc);
  915. if (tmp_hal_status == HAL_OK)
  916. {
  917. tmp_hal_status = ADC_Enable(&tmphadcSlave);
  918. }
  919. /* Start multimode conversion of ADCs pair */
  920. if (tmp_hal_status == HAL_OK)
  921. {
  922. /* Set ADC state */
  923. ADC_STATE_CLR_SET(hadc->State,
  924. (HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP),
  925. HAL_ADC_STATE_REG_BUSY);
  926. /* Set ADC error code to none */
  927. ADC_CLEAR_ERRORCODE(hadc);
  928. /* Set the DMA transfer complete callback */
  929. hadc->DMA_Handle->XferCpltCallback = ADC_DMAConvCplt;
  930. /* Set the DMA half transfer complete callback */
  931. hadc->DMA_Handle->XferHalfCpltCallback = ADC_DMAHalfConvCplt;
  932. /* Set the DMA error callback */
  933. hadc->DMA_Handle->XferErrorCallback = ADC_DMAError ;
  934. /* Pointer to the common control register */
  935. tmpADC_Common = __LL_ADC_COMMON_INSTANCE(hadc->Instance);
  936. /* Manage ADC and DMA start: ADC overrun interruption, DMA start, ADC */
  937. /* start (in case of SW start): */
  938. /* Clear regular group conversion flag and overrun flag */
  939. /* (To ensure of no unknown state from potential previous ADC operations) */
  940. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
  941. /* Process unlocked */
  942. /* Unlock before starting ADC conversions: in case of potential */
  943. /* interruption, to let the process to ADC IRQ Handler. */
  944. __HAL_UNLOCK(hadc);
  945. /* Enable ADC overrun interrupt */
  946. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);
  947. /* Start the DMA channel */
  948. tmp_hal_status = HAL_DMA_Start_IT(hadc->DMA_Handle, (uint32_t)&tmpADC_Common->CDR, (uint32_t)pData, Length);
  949. /* Enable conversion of regular group. */
  950. /* If software start has been selected, conversion starts immediately. */
  951. /* If external trigger has been selected, conversion will start at next */
  952. /* trigger event. */
  953. /* Start ADC group regular conversion */
  954. LL_ADC_REG_StartConversion(hadc->Instance);
  955. }
  956. else
  957. {
  958. /* Process unlocked */
  959. __HAL_UNLOCK(hadc);
  960. }
  961. /* Return function status */
  962. return tmp_hal_status;
  963. }
  964. }
  965. /**
  966. * @brief Stop multimode ADC conversion, disable ADC DMA transfer, disable ADC peripheral.
  967. * @note Multimode is kept enabled after this function. MultiMode DMA bits
  968. * (MDMA and DMACFG bits of common CCR register) are maintained. To disable
  969. * Multimode (set with HAL_ADCEx_MultiModeConfigChannel()), ADC must be
  970. * reinitialized using HAL_ADC_Init() or HAL_ADC_DeInit(), or the user can
  971. * resort to HAL_ADCEx_DisableMultiMode() API.
  972. * @note In case of DMA configured in circular mode, function
  973. * HAL_ADC_Stop_DMA() must be called after this function with handle of
  974. * ADC slave, to properly disable the DMA channel.
  975. * @param hadc ADC handle of ADC master (handle of ADC slave must not be used)
  976. * @retval HAL status
  977. */
  978. HAL_StatusTypeDef HAL_ADCEx_MultiModeStop_DMA(ADC_HandleTypeDef *hadc)
  979. {
  980. HAL_StatusTypeDef tmp_hal_status;
  981. uint32_t tickstart;
  982. ADC_HandleTypeDef tmphadcSlave;
  983. uint32_t tmphadcSlave_conversion_on_going;
  984. HAL_StatusTypeDef tmphadcSlave_disable_status;
  985. /* Check the parameters */
  986. assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
  987. /* Process locked */
  988. __HAL_LOCK(hadc);
  989. /* 1. Stop potential multimode conversion on going, on regular and injected groups */
  990. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_INJECTED_GROUP);
  991. /* Disable ADC peripheral if conversions are effectively stopped */
  992. if (tmp_hal_status == HAL_OK)
  993. {
  994. /* Set a temporary handle of the ADC slave associated to the ADC master */
  995. ADC_MULTI_SLAVE(hadc, &tmphadcSlave);
  996. if (tmphadcSlave.Instance == NULL)
  997. {
  998. /* Update ADC state machine to error */
  999. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1000. /* Process unlocked */
  1001. __HAL_UNLOCK(hadc);
  1002. return HAL_ERROR;
  1003. }
  1004. /* Procedure to disable the ADC peripheral: wait for conversions */
  1005. /* effectively stopped (ADC master and ADC slave), then disable ADC */
  1006. /* 1. Wait for ADC conversion completion for ADC master and ADC slave */
  1007. tickstart = HAL_GetTick();
  1008. tmphadcSlave_conversion_on_going = LL_ADC_REG_IsConversionOngoing((&tmphadcSlave)->Instance);
  1009. while ((LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 1UL)
  1010. || (tmphadcSlave_conversion_on_going == 1UL)
  1011. )
  1012. {
  1013. if ((HAL_GetTick() - tickstart) > ADC_STOP_CONVERSION_TIMEOUT)
  1014. {
  1015. /* Update ADC state machine to error */
  1016. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  1017. /* Process unlocked */
  1018. __HAL_UNLOCK(hadc);
  1019. return HAL_ERROR;
  1020. }
  1021. tmphadcSlave_conversion_on_going = LL_ADC_REG_IsConversionOngoing((&tmphadcSlave)->Instance);
  1022. }
  1023. /* Disable the DMA channel (in case of DMA in circular mode or stop */
  1024. /* while DMA transfer is on going) */
  1025. /* Note: DMA channel of ADC slave should be stopped after this function */
  1026. /* with HAL_ADC_Stop_DMA() API. */
  1027. tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
  1028. /* Check if DMA channel effectively disabled */
  1029. if (tmp_hal_status == HAL_ERROR)
  1030. {
  1031. /* Update ADC state machine to error */
  1032. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  1033. }
  1034. /* Disable ADC overrun interrupt */
  1035. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
  1036. /* 2. Disable the ADC peripherals: master and slave */
  1037. /* Update "tmp_hal_status" only if DMA channel disabling passed, to keep in */
  1038. /* memory a potential failing status. */
  1039. if (tmp_hal_status == HAL_OK)
  1040. {
  1041. tmphadcSlave_disable_status = ADC_Disable(&tmphadcSlave);
  1042. if ((ADC_Disable(hadc) == HAL_OK) &&
  1043. (tmphadcSlave_disable_status == HAL_OK))
  1044. {
  1045. tmp_hal_status = HAL_OK;
  1046. }
  1047. }
  1048. else
  1049. {
  1050. /* In case of error, attempt to disable ADC master and slave without status assert */
  1051. (void) ADC_Disable(hadc);
  1052. (void) ADC_Disable(&tmphadcSlave);
  1053. }
  1054. /* Set ADC state (ADC master) */
  1055. ADC_STATE_CLR_SET(hadc->State,
  1056. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  1057. HAL_ADC_STATE_READY);
  1058. }
  1059. /* Process unlocked */
  1060. __HAL_UNLOCK(hadc);
  1061. /* Return function status */
  1062. return tmp_hal_status;
  1063. }
  1064. /**
  1065. * @brief Return the last ADC Master and Slave regular conversions results when in multimode configuration.
  1066. * @param hadc ADC handle of ADC Master (handle of ADC Slave must not be used)
  1067. * @retval The converted data values.
  1068. */
  1069. uint32_t HAL_ADCEx_MultiModeGetValue(ADC_HandleTypeDef *hadc)
  1070. {
  1071. const ADC_Common_TypeDef *tmpADC_Common;
  1072. /* Check the parameters */
  1073. assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
  1074. /* Prevent unused argument(s) compilation warning if no assert_param check */
  1075. /* and possible no usage in __LL_ADC_COMMON_INSTANCE() below */
  1076. UNUSED(hadc);
  1077. /* Pointer to the common control register */
  1078. tmpADC_Common = __LL_ADC_COMMON_INSTANCE(hadc->Instance);
  1079. /* Return the multi mode conversion value */
  1080. return tmpADC_Common->CDR;
  1081. }
  1082. /**
  1083. * @brief Get ADC injected group conversion result.
  1084. * @note Reading register JDRx automatically clears ADC flag JEOC
  1085. * (ADC group injected end of unitary conversion).
  1086. * @note This function does not clear ADC flag JEOS
  1087. * (ADC group injected end of sequence conversion)
  1088. * Occurrence of flag JEOS rising:
  1089. * - If sequencer is composed of 1 rank, flag JEOS is equivalent
  1090. * to flag JEOC.
  1091. * - If sequencer is composed of several ranks, during the scan
  1092. * sequence flag JEOC only is raised, at the end of the scan sequence
  1093. * both flags JEOC and EOS are raised.
  1094. * Flag JEOS must not be cleared by this function because
  1095. * it would not be compliant with low power features
  1096. * (feature low power auto-wait, not available on all STM32 families).
  1097. * To clear this flag, either use function:
  1098. * in programming model IT: @ref HAL_ADC_IRQHandler(), in programming
  1099. * model polling: @ref HAL_ADCEx_InjectedPollForConversion()
  1100. * or @ref __HAL_ADC_CLEAR_FLAG(&hadc, ADC_FLAG_JEOS).
  1101. * @param hadc ADC handle
  1102. * @param InjectedRank the converted ADC injected rank.
  1103. * This parameter can be one of the following values:
  1104. * @arg @ref ADC_INJECTED_RANK_1 ADC group injected rank 1
  1105. * @arg @ref ADC_INJECTED_RANK_2 ADC group injected rank 2
  1106. * @arg @ref ADC_INJECTED_RANK_3 ADC group injected rank 3
  1107. * @arg @ref ADC_INJECTED_RANK_4 ADC group injected rank 4
  1108. * @retval ADC group injected conversion data
  1109. */
  1110. uint32_t HAL_ADCEx_InjectedGetValue(ADC_HandleTypeDef *hadc, uint32_t InjectedRank)
  1111. {
  1112. uint32_t tmp_jdr;
  1113. /* Check the parameters */
  1114. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1115. assert_param(IS_ADC_INJECTED_RANK(InjectedRank));
  1116. /* Get ADC converted value */
  1117. switch (InjectedRank)
  1118. {
  1119. case ADC_INJECTED_RANK_4:
  1120. tmp_jdr = hadc->Instance->JDR4;
  1121. break;
  1122. case ADC_INJECTED_RANK_3:
  1123. tmp_jdr = hadc->Instance->JDR3;
  1124. break;
  1125. case ADC_INJECTED_RANK_2:
  1126. tmp_jdr = hadc->Instance->JDR2;
  1127. break;
  1128. case ADC_INJECTED_RANK_1:
  1129. default:
  1130. tmp_jdr = hadc->Instance->JDR1;
  1131. break;
  1132. }
  1133. /* Return ADC converted value */
  1134. return tmp_jdr;
  1135. }
  1136. /**
  1137. * @brief Injected conversion complete callback in non-blocking mode.
  1138. * @param hadc ADC handle
  1139. * @retval None
  1140. */
  1141. __weak void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef *hadc)
  1142. {
  1143. /* Prevent unused argument(s) compilation warning */
  1144. UNUSED(hadc);
  1145. /* NOTE : This function should not be modified. When the callback is needed,
  1146. function HAL_ADCEx_InjectedConvCpltCallback must be implemented in the user file.
  1147. */
  1148. }
  1149. /**
  1150. * @brief Injected context queue overflow callback.
  1151. * @note This callback is called if injected context queue is enabled
  1152. (parameter "QueueInjectedContext" in injected channel configuration)
  1153. and if a new injected context is set when queue is full (maximum 2
  1154. contexts).
  1155. * @param hadc ADC handle
  1156. * @retval None
  1157. */
  1158. __weak void HAL_ADCEx_InjectedQueueOverflowCallback(ADC_HandleTypeDef *hadc)
  1159. {
  1160. /* Prevent unused argument(s) compilation warning */
  1161. UNUSED(hadc);
  1162. /* NOTE : This function should not be modified. When the callback is needed,
  1163. function HAL_ADCEx_InjectedQueueOverflowCallback must be implemented in the user file.
  1164. */
  1165. }
  1166. /**
  1167. * @brief Analog watchdog 2 callback in non-blocking mode.
  1168. * @param hadc ADC handle
  1169. * @retval None
  1170. */
  1171. __weak void HAL_ADCEx_LevelOutOfWindow2Callback(ADC_HandleTypeDef *hadc)
  1172. {
  1173. /* Prevent unused argument(s) compilation warning */
  1174. UNUSED(hadc);
  1175. /* NOTE : This function should not be modified. When the callback is needed,
  1176. function HAL_ADCEx_LevelOutOfWindow2Callback must be implemented in the user file.
  1177. */
  1178. }
  1179. /**
  1180. * @brief Analog watchdog 3 callback in non-blocking mode.
  1181. * @param hadc ADC handle
  1182. * @retval None
  1183. */
  1184. __weak void HAL_ADCEx_LevelOutOfWindow3Callback(ADC_HandleTypeDef *hadc)
  1185. {
  1186. /* Prevent unused argument(s) compilation warning */
  1187. UNUSED(hadc);
  1188. /* NOTE : This function should not be modified. When the callback is needed,
  1189. function HAL_ADCEx_LevelOutOfWindow3Callback must be implemented in the user file.
  1190. */
  1191. }
  1192. /**
  1193. * @brief End Of Sampling callback in non-blocking mode.
  1194. * @param hadc ADC handle
  1195. * @retval None
  1196. */
  1197. __weak void HAL_ADCEx_EndOfSamplingCallback(ADC_HandleTypeDef *hadc)
  1198. {
  1199. /* Prevent unused argument(s) compilation warning */
  1200. UNUSED(hadc);
  1201. /* NOTE : This function should not be modified. When the callback is needed,
  1202. function HAL_ADCEx_EndOfSamplingCallback must be implemented in the user file.
  1203. */
  1204. }
  1205. /**
  1206. * @brief Stop ADC conversion of regular group (and injected channels in
  1207. * case of auto_injection mode), disable ADC peripheral if no
  1208. * conversion is on going on injected group.
  1209. * @param hadc ADC handle
  1210. * @retval HAL status.
  1211. */
  1212. HAL_StatusTypeDef HAL_ADCEx_RegularStop(ADC_HandleTypeDef *hadc)
  1213. {
  1214. HAL_StatusTypeDef tmp_hal_status;
  1215. /* Check the parameters */
  1216. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1217. /* Process locked */
  1218. __HAL_LOCK(hadc);
  1219. /* 1. Stop potential regular conversion on going */
  1220. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_GROUP);
  1221. /* Disable ADC peripheral if regular conversions are effectively stopped
  1222. and if no injected conversions are on-going */
  1223. if (tmp_hal_status == HAL_OK)
  1224. {
  1225. /* Clear HAL_ADC_STATE_REG_BUSY bit */
  1226. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  1227. if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) == 0UL)
  1228. {
  1229. /* 2. Disable the ADC peripheral */
  1230. tmp_hal_status = ADC_Disable(hadc);
  1231. /* Check if ADC is effectively disabled */
  1232. if (tmp_hal_status == HAL_OK)
  1233. {
  1234. /* Set ADC state */
  1235. ADC_STATE_CLR_SET(hadc->State,
  1236. HAL_ADC_STATE_INJ_BUSY,
  1237. HAL_ADC_STATE_READY);
  1238. }
  1239. }
  1240. /* Conversion on injected group is stopped, but ADC not disabled since */
  1241. /* conversion on regular group is still running. */
  1242. else
  1243. {
  1244. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  1245. }
  1246. }
  1247. /* Process unlocked */
  1248. __HAL_UNLOCK(hadc);
  1249. /* Return function status */
  1250. return tmp_hal_status;
  1251. }
  1252. /**
  1253. * @brief Stop ADC conversion of ADC groups regular and injected,
  1254. * disable interrution of end-of-conversion,
  1255. * disable ADC peripheral if no conversion is on going
  1256. * on injected group.
  1257. * @param hadc ADC handle
  1258. * @retval HAL status.
  1259. */
  1260. HAL_StatusTypeDef HAL_ADCEx_RegularStop_IT(ADC_HandleTypeDef *hadc)
  1261. {
  1262. HAL_StatusTypeDef tmp_hal_status;
  1263. /* Check the parameters */
  1264. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1265. /* Process locked */
  1266. __HAL_LOCK(hadc);
  1267. /* 1. Stop potential regular conversion on going */
  1268. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_GROUP);
  1269. /* Disable ADC peripheral if conversions are effectively stopped
  1270. and if no injected conversion is on-going */
  1271. if (tmp_hal_status == HAL_OK)
  1272. {
  1273. /* Clear HAL_ADC_STATE_REG_BUSY bit */
  1274. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  1275. /* Disable all regular-related interrupts */
  1276. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_EOS | ADC_IT_OVR));
  1277. /* 2. Disable ADC peripheral if no injected conversions are on-going */
  1278. if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) == 0UL)
  1279. {
  1280. tmp_hal_status = ADC_Disable(hadc);
  1281. /* if no issue reported */
  1282. if (tmp_hal_status == HAL_OK)
  1283. {
  1284. /* Set ADC state */
  1285. ADC_STATE_CLR_SET(hadc->State,
  1286. HAL_ADC_STATE_INJ_BUSY,
  1287. HAL_ADC_STATE_READY);
  1288. }
  1289. }
  1290. else
  1291. {
  1292. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  1293. }
  1294. }
  1295. /* Process unlocked */
  1296. __HAL_UNLOCK(hadc);
  1297. /* Return function status */
  1298. return tmp_hal_status;
  1299. }
  1300. /**
  1301. * @brief Stop ADC conversion of regular group (and injected group in
  1302. * case of auto_injection mode), disable ADC DMA transfer, disable
  1303. * ADC peripheral if no conversion is on going
  1304. * on injected group.
  1305. * @note HAL_ADCEx_RegularStop_DMA() function is dedicated to single-ADC mode only.
  1306. * For multimode (when multimode feature is available),
  1307. * HAL_ADCEx_RegularMultiModeStop_DMA() API must be used.
  1308. * @param hadc ADC handle
  1309. * @retval HAL status.
  1310. */
  1311. HAL_StatusTypeDef HAL_ADCEx_RegularStop_DMA(ADC_HandleTypeDef *hadc)
  1312. {
  1313. HAL_StatusTypeDef tmp_hal_status;
  1314. /* Check the parameters */
  1315. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1316. /* Process locked */
  1317. __HAL_LOCK(hadc);
  1318. /* 1. Stop potential regular conversion on going */
  1319. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_GROUP);
  1320. /* Disable ADC peripheral if conversions are effectively stopped
  1321. and if no injected conversion is on-going */
  1322. if (tmp_hal_status == HAL_OK)
  1323. {
  1324. /* Clear HAL_ADC_STATE_REG_BUSY bit */
  1325. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  1326. /* Disable ADC DMA (ADC DMA configuration ADC_CFGR_DMACFG is kept) */
  1327. MODIFY_REG(hadc->Instance->CFGR, ADC_CFGR_DMNGT_0 |ADC_CFGR_DMNGT_1, 0UL);
  1328. /* Disable the DMA channel (in case of DMA in circular mode or stop while */
  1329. /* while DMA transfer is on going) */
  1330. tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
  1331. /* Check if DMA channel effectively disabled */
  1332. if (tmp_hal_status != HAL_OK)
  1333. {
  1334. /* Update ADC state machine to error */
  1335. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  1336. }
  1337. /* Disable ADC overrun interrupt */
  1338. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
  1339. /* 2. Disable the ADC peripheral */
  1340. /* Update "tmp_hal_status" only if DMA channel disabling passed, */
  1341. /* to keep in memory a potential failing status. */
  1342. if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) == 0UL)
  1343. {
  1344. if (tmp_hal_status == HAL_OK)
  1345. {
  1346. tmp_hal_status = ADC_Disable(hadc);
  1347. }
  1348. else
  1349. {
  1350. (void)ADC_Disable(hadc);
  1351. }
  1352. /* Check if ADC is effectively disabled */
  1353. if (tmp_hal_status == HAL_OK)
  1354. {
  1355. /* Set ADC state */
  1356. ADC_STATE_CLR_SET(hadc->State,
  1357. HAL_ADC_STATE_INJ_BUSY,
  1358. HAL_ADC_STATE_READY);
  1359. }
  1360. }
  1361. else
  1362. {
  1363. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  1364. }
  1365. }
  1366. /* Process unlocked */
  1367. __HAL_UNLOCK(hadc);
  1368. /* Return function status */
  1369. return tmp_hal_status;
  1370. }
  1371. /**
  1372. * @brief Stop DMA-based multimode ADC conversion, disable ADC DMA transfer, disable ADC peripheral if no injected conversion is on-going.
  1373. * @note Multimode is kept enabled after this function. Multimode DMA bits
  1374. * (MDMA and DMACFG bits of common CCR register) are maintained. To disable
  1375. * multimode (set with HAL_ADCEx_MultiModeConfigChannel()), ADC must be
  1376. * reinitialized using HAL_ADC_Init() or HAL_ADC_DeInit(), or the user can
  1377. * resort to HAL_ADCEx_DisableMultiMode() API.
  1378. * @note In case of DMA configured in circular mode, function
  1379. * HAL_ADCEx_RegularStop_DMA() must be called after this function with handle of
  1380. * ADC slave, to properly disable the DMA channel.
  1381. * @param hadc ADC handle of ADC master (handle of ADC slave must not be used)
  1382. * @retval HAL status
  1383. */
  1384. HAL_StatusTypeDef HAL_ADCEx_RegularMultiModeStop_DMA(ADC_HandleTypeDef *hadc)
  1385. {
  1386. HAL_StatusTypeDef tmp_hal_status;
  1387. uint32_t tickstart;
  1388. ADC_HandleTypeDef tmphadcSlave;
  1389. uint32_t tmphadcSlave_conversion_on_going;
  1390. /* Check the parameters */
  1391. assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
  1392. /* Process locked */
  1393. __HAL_LOCK(hadc);
  1394. /* 1. Stop potential multimode conversion on going, on regular groups */
  1395. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_GROUP);
  1396. /* Disable ADC peripheral if conversions are effectively stopped */
  1397. if (tmp_hal_status == HAL_OK)
  1398. {
  1399. /* Clear HAL_ADC_STATE_REG_BUSY bit */
  1400. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  1401. /* Set a temporary handle of the ADC slave associated to the ADC master */
  1402. ADC_MULTI_SLAVE(hadc, &tmphadcSlave);
  1403. if (tmphadcSlave.Instance == NULL)
  1404. {
  1405. /* Update ADC state machine to error */
  1406. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1407. /* Process unlocked */
  1408. __HAL_UNLOCK(hadc);
  1409. return HAL_ERROR;
  1410. }
  1411. /* Procedure to disable the ADC peripheral: wait for conversions */
  1412. /* effectively stopped (ADC master and ADC slave), then disable ADC */
  1413. /* 1. Wait for ADC conversion completion for ADC master and ADC slave */
  1414. tickstart = HAL_GetTick();
  1415. tmphadcSlave_conversion_on_going = LL_ADC_REG_IsConversionOngoing((&tmphadcSlave)->Instance);
  1416. while ((LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 1UL)
  1417. || (tmphadcSlave_conversion_on_going == 1UL)
  1418. )
  1419. {
  1420. if ((HAL_GetTick() - tickstart) > ADC_STOP_CONVERSION_TIMEOUT)
  1421. {
  1422. /* Update ADC state machine to error */
  1423. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  1424. /* Process unlocked */
  1425. __HAL_UNLOCK(hadc);
  1426. return HAL_ERROR;
  1427. }
  1428. tmphadcSlave_conversion_on_going = LL_ADC_REG_IsConversionOngoing((&tmphadcSlave)->Instance);
  1429. }
  1430. /* Disable the DMA channel (in case of DMA in circular mode or stop */
  1431. /* while DMA transfer is on going) */
  1432. /* Note: DMA channel of ADC slave should be stopped after this function */
  1433. /* with HAL_ADCEx_RegularStop_DMA() API. */
  1434. tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
  1435. /* Check if DMA channel effectively disabled */
  1436. if (tmp_hal_status != HAL_OK)
  1437. {
  1438. /* Update ADC state machine to error */
  1439. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  1440. }
  1441. /* Disable ADC overrun interrupt */
  1442. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
  1443. /* 2. Disable the ADC peripherals: master and slave if no injected */
  1444. /* conversion is on-going. */
  1445. /* Update "tmp_hal_status" only if DMA channel disabling passed, to keep in */
  1446. /* memory a potential failing status. */
  1447. if (tmp_hal_status == HAL_OK)
  1448. {
  1449. if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) == 0UL)
  1450. {
  1451. tmp_hal_status = ADC_Disable(hadc);
  1452. if (tmp_hal_status == HAL_OK)
  1453. {
  1454. if (LL_ADC_INJ_IsConversionOngoing((&tmphadcSlave)->Instance) == 0UL)
  1455. {
  1456. tmp_hal_status = ADC_Disable(&tmphadcSlave);
  1457. }
  1458. }
  1459. }
  1460. if (tmp_hal_status == HAL_OK)
  1461. {
  1462. /* Both Master and Slave ADC's could be disabled. Update Master State */
  1463. /* Clear HAL_ADC_STATE_INJ_BUSY bit, set HAL_ADC_STATE_READY bit */
  1464. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_BUSY, HAL_ADC_STATE_READY);
  1465. }
  1466. else
  1467. {
  1468. /* injected (Master or Slave) conversions are still on-going,
  1469. no Master State change */
  1470. }
  1471. }
  1472. }
  1473. /* Process unlocked */
  1474. __HAL_UNLOCK(hadc);
  1475. /* Return function status */
  1476. return tmp_hal_status;
  1477. }
  1478. /**
  1479. * @}
  1480. */
  1481. /** @defgroup ADCEx_Exported_Functions_Group2 ADC Extended Peripheral Control functions
  1482. * @brief ADC Extended Peripheral Control functions
  1483. *
  1484. @verbatim
  1485. ===============================================================================
  1486. ##### Peripheral Control functions #####
  1487. ===============================================================================
  1488. [..] This section provides functions allowing to:
  1489. (+) Configure channels on injected group
  1490. (+) Configure multimode when multimode feature is available
  1491. (+) Enable or Disable Injected Queue
  1492. (+) Disable ADC voltage regulator
  1493. (+) Enter ADC deep-power-down mode
  1494. @endverbatim
  1495. * @{
  1496. */
  1497. /**
  1498. * @brief Configure a channel to be assigned to ADC group injected.
  1499. * @note Possibility to update parameters on the fly:
  1500. * This function initializes injected group, following calls to this
  1501. * function can be used to reconfigure some parameters of structure
  1502. * "ADC_InjectionConfTypeDef" on the fly, without resetting the ADC.
  1503. * The setting of these parameters is conditioned to ADC state:
  1504. * Refer to comments of structure "ADC_InjectionConfTypeDef".
  1505. * @note In case of usage of internal measurement channels:
  1506. * Vbat/VrefInt/TempSensor.
  1507. * These internal paths can be disabled using function
  1508. * HAL_ADC_DeInit().
  1509. * @note Caution: For Injected Context Queue use, a context must be fully
  1510. * defined before start of injected conversion. All channels are configured
  1511. * consecutively for the same ADC instance. Therefore, the number of calls to
  1512. * HAL_ADCEx_InjectedConfigChannel() must be equal to the value of parameter
  1513. * InjectedNbrOfConversion for each context.
  1514. * - Example 1: If 1 context is intended to be used (or if there is no use of the
  1515. * Injected Queue Context feature) and if the context contains 3 injected ranks
  1516. * (InjectedNbrOfConversion = 3), HAL_ADCEx_InjectedConfigChannel() must be
  1517. * called once for each channel (i.e. 3 times) before starting a conversion.
  1518. * This function must not be called to configure a 4th injected channel:
  1519. * it would start a new context into context queue.
  1520. * - Example 2: If 2 contexts are intended to be used and each of them contains
  1521. * 3 injected ranks (InjectedNbrOfConversion = 3),
  1522. * HAL_ADCEx_InjectedConfigChannel() must be called once for each channel and
  1523. * for each context (3 channels x 2 contexts = 6 calls). Conversion can
  1524. * start once the 1st context is set, that is after the first three
  1525. * HAL_ADCEx_InjectedConfigChannel() calls. The 2nd context can be set on the fly.
  1526. * @param hadc ADC handle
  1527. * @param sConfigInjected Structure of ADC injected group and ADC channel for
  1528. * injected group.
  1529. * @retval HAL status
  1530. */
  1531. HAL_StatusTypeDef HAL_ADCEx_InjectedConfigChannel(ADC_HandleTypeDef *hadc, ADC_InjectionConfTypeDef *sConfigInjected)
  1532. {
  1533. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1534. uint32_t tmpOffsetShifted;
  1535. uint32_t tmp_config_internal_channel;
  1536. uint32_t tmp_adc_is_conversion_on_going_regular;
  1537. uint32_t tmp_adc_is_conversion_on_going_injected;
  1538. __IO uint32_t wait_loop_index = 0;
  1539. uint32_t tmp_JSQR_ContextQueueBeingBuilt = 0U;
  1540. /* Check the parameters */
  1541. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1542. assert_param(IS_ADC_SAMPLE_TIME(sConfigInjected->InjectedSamplingTime));
  1543. assert_param(IS_ADC_SINGLE_DIFFERENTIAL(sConfigInjected->InjectedSingleDiff));
  1544. assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->AutoInjectedConv));
  1545. assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->QueueInjectedContext));
  1546. assert_param(IS_ADC_EXTTRIGINJEC_EDGE(sConfigInjected->ExternalTrigInjecConvEdge));
  1547. assert_param(IS_ADC_EXTTRIGINJEC(sConfigInjected->ExternalTrigInjecConv));
  1548. assert_param(IS_ADC_OFFSET_NUMBER(sConfigInjected->InjectedOffsetNumber));
  1549. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), sConfigInjected->InjectedOffset));
  1550. assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->InjecOversamplingMode));
  1551. if (hadc->Init.ScanConvMode != ADC_SCAN_DISABLE)
  1552. {
  1553. assert_param(IS_ADC_INJECTED_RANK(sConfigInjected->InjectedRank));
  1554. assert_param(IS_ADC_INJECTED_NB_CONV(sConfigInjected->InjectedNbrOfConversion));
  1555. assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->InjectedDiscontinuousConvMode));
  1556. }
  1557. /* Check offset range according to oversampling setting */
  1558. if (hadc->Init.OversamplingMode == ENABLE)
  1559. {
  1560. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), sConfigInjected->InjectedOffset/(hadc->Init.Oversampling.Ratio+1U)));
  1561. }
  1562. else
  1563. {
  1564. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), sConfigInjected->InjectedOffset));
  1565. }
  1566. /* JDISCEN and JAUTO bits can't be set at the same time */
  1567. assert_param(!((sConfigInjected->InjectedDiscontinuousConvMode == ENABLE) && (sConfigInjected->AutoInjectedConv == ENABLE)));
  1568. /* DISCEN and JAUTO bits can't be set at the same time */
  1569. assert_param(!((hadc->Init.DiscontinuousConvMode == ENABLE) && (sConfigInjected->AutoInjectedConv == ENABLE)));
  1570. /* Verification of channel number */
  1571. if (sConfigInjected->InjectedSingleDiff != ADC_DIFFERENTIAL_ENDED)
  1572. {
  1573. assert_param(IS_ADC_CHANNEL(sConfigInjected->InjectedChannel));
  1574. }
  1575. else
  1576. {
  1577. if (hadc->Instance == ADC1)
  1578. {
  1579. assert_param(IS_ADC1_DIFF_CHANNEL(sConfigInjected->InjectedChannel));
  1580. }
  1581. if (hadc->Instance == ADC2)
  1582. {
  1583. assert_param(IS_ADC2_DIFF_CHANNEL(sConfigInjected->InjectedChannel));
  1584. }
  1585. #if defined (ADC3)
  1586. if (hadc->Instance == ADC3)
  1587. {
  1588. assert_param(IS_ADC3_DIFF_CHANNEL(sConfigInjected->InjectedChannel));
  1589. }
  1590. #endif
  1591. }
  1592. /* Process locked */
  1593. __HAL_LOCK(hadc);
  1594. /* Configuration of injected group sequencer: */
  1595. /* Hardware constraint: Must fully define injected context register JSQR */
  1596. /* before make it entering into injected sequencer queue. */
  1597. /* */
  1598. /* - if scan mode is disabled: */
  1599. /* * Injected channels sequence length is set to 0x00: 1 channel */
  1600. /* converted (channel on injected rank 1) */
  1601. /* Parameter "InjectedNbrOfConversion" is discarded. */
  1602. /* * Injected context register JSQR setting is simple: register is fully */
  1603. /* defined on one call of this function (for injected rank 1) and can */
  1604. /* be entered into queue directly. */
  1605. /* - if scan mode is enabled: */
  1606. /* * Injected channels sequence length is set to parameter */
  1607. /* "InjectedNbrOfConversion". */
  1608. /* * Injected context register JSQR setting more complex: register is */
  1609. /* fully defined over successive calls of this function, for each */
  1610. /* injected channel rank. It is entered into queue only when all */
  1611. /* injected ranks have been set. */
  1612. /* Note: Scan mode is not present by hardware on this device, but used */
  1613. /* by software for alignment over all STM32 devices. */
  1614. if ((hadc->Init.ScanConvMode == ADC_SCAN_DISABLE) ||
  1615. (sConfigInjected->InjectedNbrOfConversion == 1U))
  1616. {
  1617. /* Configuration of context register JSQR: */
  1618. /* - number of ranks in injected group sequencer: fixed to 1st rank */
  1619. /* (scan mode disabled, only rank 1 used) */
  1620. /* - external trigger to start conversion */
  1621. /* - external trigger polarity */
  1622. /* - channel set to rank 1 (scan mode disabled, only rank 1 can be used) */
  1623. if (sConfigInjected->InjectedRank == ADC_INJECTED_RANK_1)
  1624. {
  1625. /* Enable external trigger if trigger selection is different of */
  1626. /* software start. */
  1627. /* Note: This configuration keeps the hardware feature of parameter */
  1628. /* ExternalTrigInjecConvEdge "trigger edge none" equivalent to */
  1629. /* software start. */
  1630. if (sConfigInjected->ExternalTrigInjecConv != ADC_INJECTED_SOFTWARE_START)
  1631. {
  1632. tmp_JSQR_ContextQueueBeingBuilt = (ADC_JSQR_RK(sConfigInjected->InjectedChannel, ADC_INJECTED_RANK_1)
  1633. | (sConfigInjected->ExternalTrigInjecConv & ADC_JSQR_JEXTSEL)
  1634. | sConfigInjected->ExternalTrigInjecConvEdge
  1635. );
  1636. }
  1637. else
  1638. {
  1639. tmp_JSQR_ContextQueueBeingBuilt = (ADC_JSQR_RK(sConfigInjected->InjectedChannel, ADC_INJECTED_RANK_1));
  1640. }
  1641. MODIFY_REG(hadc->Instance->JSQR, ADC_JSQR_FIELDS, tmp_JSQR_ContextQueueBeingBuilt);
  1642. /* For debug and informative reasons, hadc handle saves JSQR setting */
  1643. hadc->InjectionConfig.ContextQueue = tmp_JSQR_ContextQueueBeingBuilt;
  1644. }
  1645. }
  1646. else
  1647. {
  1648. /* Case of scan mode enabled, several channels to set into injected group */
  1649. /* sequencer. */
  1650. /* */
  1651. /* Procedure to define injected context register JSQR over successive */
  1652. /* calls of this function, for each injected channel rank: */
  1653. /* 1. Start new context and set parameters related to all injected */
  1654. /* channels: injected sequence length and trigger. */
  1655. /* if hadc->InjectionConfig.ChannelCount is equal to 0, this is the first */
  1656. /* call of the context under setting */
  1657. if (hadc->InjectionConfig.ChannelCount == 0U)
  1658. {
  1659. /* Initialize number of channels that will be configured on the context */
  1660. /* being built */
  1661. hadc->InjectionConfig.ChannelCount = sConfigInjected->InjectedNbrOfConversion;
  1662. /* Handle hadc saves the context under build up over each HAL_ADCEx_InjectedConfigChannel()
  1663. call, this context will be written in JSQR register at the last call.
  1664. At this point, the context is merely reset */
  1665. hadc->InjectionConfig.ContextQueue = 0x00000000U;
  1666. /* Configuration of context register JSQR: */
  1667. /* - number of ranks in injected group sequencer */
  1668. /* - external trigger to start conversion */
  1669. /* - external trigger polarity */
  1670. /* Enable external trigger if trigger selection is different of */
  1671. /* software start. */
  1672. /* Note: This configuration keeps the hardware feature of parameter */
  1673. /* ExternalTrigInjecConvEdge "trigger edge none" equivalent to */
  1674. /* software start. */
  1675. if (sConfigInjected->ExternalTrigInjecConv != ADC_INJECTED_SOFTWARE_START)
  1676. {
  1677. tmp_JSQR_ContextQueueBeingBuilt = ((sConfigInjected->InjectedNbrOfConversion - 1U)
  1678. | (sConfigInjected->ExternalTrigInjecConv & ADC_JSQR_JEXTSEL)
  1679. | sConfigInjected->ExternalTrigInjecConvEdge
  1680. );
  1681. }
  1682. else
  1683. {
  1684. tmp_JSQR_ContextQueueBeingBuilt = ((sConfigInjected->InjectedNbrOfConversion - 1U));
  1685. }
  1686. }
  1687. /* 2. Continue setting of context under definition with parameter */
  1688. /* related to each channel: channel rank sequence */
  1689. /* Clear the old JSQx bits for the selected rank */
  1690. tmp_JSQR_ContextQueueBeingBuilt &= ~ADC_JSQR_RK(ADC_SQR3_SQ10, sConfigInjected->InjectedRank);
  1691. /* Set the JSQx bits for the selected rank */
  1692. tmp_JSQR_ContextQueueBeingBuilt |= ADC_JSQR_RK(sConfigInjected->InjectedChannel, sConfigInjected->InjectedRank);
  1693. /* Decrease channel count */
  1694. hadc->InjectionConfig.ChannelCount--;
  1695. /* 3. tmp_JSQR_ContextQueueBeingBuilt is fully built for this HAL_ADCEx_InjectedConfigChannel()
  1696. call, aggregate the setting to those already built during the previous
  1697. HAL_ADCEx_InjectedConfigChannel() calls (for the same context of course) */
  1698. hadc->InjectionConfig.ContextQueue |= tmp_JSQR_ContextQueueBeingBuilt;
  1699. /* 4. End of context setting: if this is the last channel set, then write context
  1700. into register JSQR and make it enter into queue */
  1701. if (hadc->InjectionConfig.ChannelCount == 0U)
  1702. {
  1703. MODIFY_REG(hadc->Instance->JSQR, ADC_JSQR_FIELDS, hadc->InjectionConfig.ContextQueue);
  1704. }
  1705. }
  1706. /* Parameters update conditioned to ADC state: */
  1707. /* Parameters that can be updated when ADC is disabled or enabled without */
  1708. /* conversion on going on injected group: */
  1709. /* - Injected context queue: Queue disable (active context is kept) or */
  1710. /* enable (context decremented, up to 2 contexts queued) */
  1711. /* - Injected discontinuous mode: can be enabled only if auto-injected */
  1712. /* mode is disabled. */
  1713. if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) == 0UL)
  1714. {
  1715. /* ADC channels preselection */
  1716. hadc->Instance->PCSEL |= (1UL << (__LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfigInjected->InjectedChannel) & 0x1FUL));
  1717. /* If auto-injected mode is disabled: no constraint */
  1718. if (sConfigInjected->AutoInjectedConv == DISABLE)
  1719. {
  1720. MODIFY_REG(hadc->Instance->CFGR,
  1721. ADC_CFGR_JQM | ADC_CFGR_JDISCEN,
  1722. ADC_CFGR_INJECT_CONTEXT_QUEUE((uint32_t)sConfigInjected->QueueInjectedContext) |
  1723. ADC_CFGR_INJECT_DISCCONTINUOUS((uint32_t)sConfigInjected->InjectedDiscontinuousConvMode));
  1724. }
  1725. /* If auto-injected mode is enabled: Injected discontinuous setting is */
  1726. /* discarded. */
  1727. else
  1728. {
  1729. MODIFY_REG(hadc->Instance->CFGR,
  1730. ADC_CFGR_JQM | ADC_CFGR_JDISCEN,
  1731. ADC_CFGR_INJECT_CONTEXT_QUEUE((uint32_t)sConfigInjected->QueueInjectedContext));
  1732. }
  1733. }
  1734. /* Parameters update conditioned to ADC state: */
  1735. /* Parameters that can be updated when ADC is disabled or enabled without */
  1736. /* conversion on going on regular and injected groups: */
  1737. /* - Automatic injected conversion: can be enabled if injected group */
  1738. /* external triggers are disabled. */
  1739. /* - Channel sampling time */
  1740. /* - Channel offset */
  1741. tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
  1742. tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
  1743. if ((tmp_adc_is_conversion_on_going_regular == 0UL)
  1744. && (tmp_adc_is_conversion_on_going_injected == 0UL)
  1745. )
  1746. {
  1747. /* If injected group external triggers are disabled (set to injected */
  1748. /* software start): no constraint */
  1749. if ((sConfigInjected->ExternalTrigInjecConv == ADC_INJECTED_SOFTWARE_START)
  1750. || (sConfigInjected->ExternalTrigInjecConvEdge == ADC_EXTERNALTRIGINJECCONV_EDGE_NONE))
  1751. {
  1752. if (sConfigInjected->AutoInjectedConv == ENABLE)
  1753. {
  1754. SET_BIT(hadc->Instance->CFGR, ADC_CFGR_JAUTO);
  1755. }
  1756. else
  1757. {
  1758. CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_JAUTO);
  1759. }
  1760. }
  1761. /* If Automatic injected conversion was intended to be set and could not */
  1762. /* due to injected group external triggers enabled, error is reported. */
  1763. else
  1764. {
  1765. if (sConfigInjected->AutoInjectedConv == ENABLE)
  1766. {
  1767. /* Update ADC state machine to error */
  1768. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1769. tmp_hal_status = HAL_ERROR;
  1770. }
  1771. else
  1772. {
  1773. CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_JAUTO);
  1774. }
  1775. }
  1776. if (sConfigInjected->InjecOversamplingMode == ENABLE)
  1777. {
  1778. assert_param(IS_ADC_OVERSAMPLING_RATIO(sConfigInjected->InjecOversampling.Ratio));
  1779. assert_param(IS_ADC_RIGHT_BIT_SHIFT(sConfigInjected->InjecOversampling.RightBitShift));
  1780. /* JOVSE must be reset in case of triggered regular mode */
  1781. assert_param(!(READ_BIT(hadc->Instance->CFGR2, ADC_CFGR2_ROVSE | ADC_CFGR2_TROVS) == (ADC_CFGR2_ROVSE | ADC_CFGR2_TROVS)));
  1782. /* Configuration of Injected Oversampler: */
  1783. /* - Oversampling Ratio */
  1784. /* - Right bit shift */
  1785. /* Enable OverSampling mode */
  1786. MODIFY_REG(hadc->Instance->CFGR2,
  1787. ADC_CFGR2_JOVSE |
  1788. ADC_CFGR2_OVSR |
  1789. ADC_CFGR2_OVSS,
  1790. ADC_CFGR2_JOVSE |
  1791. ((sConfigInjected->InjecOversampling.Ratio - 1UL) << ADC_CFGR2_OVSR_Pos) |
  1792. sConfigInjected->InjecOversampling.RightBitShift
  1793. );
  1794. }
  1795. else
  1796. {
  1797. /* Disable Regular OverSampling */
  1798. CLEAR_BIT(hadc->Instance->CFGR2, ADC_CFGR2_JOVSE);
  1799. }
  1800. /* Set sampling time of the selected ADC channel */
  1801. LL_ADC_SetChannelSamplingTime(hadc->Instance, sConfigInjected->InjectedChannel, sConfigInjected->InjectedSamplingTime);
  1802. /* Configure the offset: offset enable/disable, channel, offset value */
  1803. /* Shift the offset with respect to the selected ADC resolution. */
  1804. /* Offset has to be left-aligned on bit 11, the LSB (right bits) are set to 0 */
  1805. tmpOffsetShifted = ADC_OFFSET_SHIFT_RESOLUTION(hadc, sConfigInjected->InjectedOffset);
  1806. if (sConfigInjected->InjectedOffsetNumber != ADC_OFFSET_NONE)
  1807. {
  1808. /* Set ADC selected offset number */
  1809. LL_ADC_SetOffset(hadc->Instance, sConfigInjected->InjectedOffsetNumber, sConfigInjected->InjectedChannel,
  1810. tmpOffsetShifted);
  1811. /* Set ADC selected offset signed saturation */
  1812. LL_ADC_SetOffsetSignedSaturation(hadc->Instance, sConfigInjected->InjectedOffsetNumber, (sConfigInjected->InjectedOffsetSignedSaturation == ENABLE) ? LL_ADC_OFFSET_SIGNED_SATURATION_ENABLE : LL_ADC_OFFSET_SIGNED_SATURATION_DISABLE);
  1813. /* Set ADC selected offset right shift */
  1814. LL_ADC_SetDataRightShift(hadc->Instance, sConfigInjected->InjectedOffsetNumber, (sConfigInjected->InjectedOffsetRightShift == (uint32_t)ENABLE) ? LL_ADC_OFFSET_RSHIFT_ENABLE : LL_ADC_OFFSET_RSHIFT_DISABLE);
  1815. }
  1816. else
  1817. {
  1818. /* Scan each offset register to check if the selected channel is targeted. */
  1819. /* If this is the case, the corresponding offset number is disabled. */
  1820. if(__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_1)) == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfigInjected->InjectedChannel))
  1821. {
  1822. LL_ADC_SetOffset(hadc->Instance, LL_ADC_OFFSET_1, sConfigInjected->InjectedChannel, LL_ADC_OFFSET_SIGNED_SATURATION_DISABLE);
  1823. }
  1824. if(__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_2)) == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfigInjected->InjectedChannel))
  1825. {
  1826. LL_ADC_SetOffset(hadc->Instance, LL_ADC_OFFSET_2, sConfigInjected->InjectedChannel, LL_ADC_OFFSET_SIGNED_SATURATION_DISABLE);
  1827. }
  1828. if(__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_3)) == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfigInjected->InjectedChannel))
  1829. {
  1830. LL_ADC_SetOffset(hadc->Instance, LL_ADC_OFFSET_4, sConfigInjected->InjectedChannel, LL_ADC_OFFSET_SIGNED_SATURATION_DISABLE);
  1831. }
  1832. if(__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_4)) == __LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfigInjected->InjectedChannel))
  1833. {
  1834. LL_ADC_SetOffset(hadc->Instance, LL_ADC_OFFSET_4, sConfigInjected->InjectedChannel, LL_ADC_OFFSET_SIGNED_SATURATION_DISABLE);
  1835. }
  1836. }
  1837. }
  1838. /* Parameters update conditioned to ADC state: */
  1839. /* Parameters that can be updated only when ADC is disabled: */
  1840. /* - Single or differential mode */
  1841. /* - Internal measurement channels: Vbat/VrefInt/TempSensor */
  1842. if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
  1843. {
  1844. /* Set mode single-ended or differential input of the selected ADC channel */
  1845. LL_ADC_SetChannelSingleDiff(hadc->Instance, sConfigInjected->InjectedChannel, sConfigInjected->InjectedSingleDiff);
  1846. /* Configuration of differential mode */
  1847. /* Note: ADC channel number masked with value "0x1F" to ensure shift value within 32 bits range */
  1848. if (sConfigInjected->InjectedSingleDiff == ADC_DIFFERENTIAL_ENDED)
  1849. {
  1850. /* Set sampling time of the selected ADC channel */
  1851. 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);
  1852. }
  1853. /* Management of internal measurement channels: Vbat/VrefInt/TempSensor */
  1854. /* internal measurement paths enable: If internal channel selected, */
  1855. /* enable dedicated internal buffers and path. */
  1856. /* Note: these internal measurement paths can be disabled using */
  1857. /* HAL_ADC_DeInit(). */
  1858. if(__LL_ADC_IS_CHANNEL_INTERNAL(sConfigInjected->InjectedChannel))
  1859. {
  1860. /* Configuration of common ADC parameters (continuation) */
  1861. /* Software is allowed to change common parameters only when all ADCs */
  1862. /* of the common group are disabled. */
  1863. if (__LL_ADC_IS_ENABLED_ALL_COMMON_INSTANCE(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) == 0UL)
  1864. {
  1865. tmp_config_internal_channel = LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
  1866. /* If the requested internal measurement path has already been enabled, */
  1867. /* bypass the configuration processing. */
  1868. if ((sConfigInjected->InjectedChannel == ADC_CHANNEL_TEMPSENSOR) && ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_TEMPSENSOR) == 0UL))
  1869. {
  1870. if (ADC_TEMPERATURE_SENSOR_INSTANCE(hadc))
  1871. {
  1872. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_TEMPSENSOR | tmp_config_internal_channel);
  1873. /* Delay for temperature sensor stabilization time */
  1874. /* Wait loop initialization and execution */
  1875. /* Note: Variable divided by 2 to compensate partially */
  1876. /* CPU processing cycles, scaling in us split to not */
  1877. /* exceed 32 bits register capacity and handle low frequency. */
  1878. wait_loop_index = ((LL_ADC_DELAY_TEMPSENSOR_STAB_US / 10UL) * (SystemCoreClock / (100000UL * 2UL)));
  1879. while(wait_loop_index != 0UL)
  1880. {
  1881. wait_loop_index--;
  1882. }
  1883. }
  1884. }
  1885. else if ((sConfigInjected->InjectedChannel == ADC_CHANNEL_VBAT) && ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_VBAT) == 0UL))
  1886. {
  1887. if (ADC_BATTERY_VOLTAGE_INSTANCE(hadc))
  1888. {
  1889. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_VBAT | tmp_config_internal_channel);
  1890. }
  1891. }
  1892. else if ((sConfigInjected->InjectedChannel == ADC_CHANNEL_VREFINT) && ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_VREFINT) == 0UL))
  1893. {
  1894. if (ADC_VREFINT_INSTANCE(hadc))
  1895. {
  1896. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_VREFINT | tmp_config_internal_channel);
  1897. }
  1898. }
  1899. else
  1900. {
  1901. /* nothing to do */
  1902. }
  1903. }
  1904. /* If the requested internal measurement path has already been enabled */
  1905. /* and other ADC of the common group are enabled, internal */
  1906. /* measurement paths cannot be enabled. */
  1907. else
  1908. {
  1909. /* Update ADC state machine to error */
  1910. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1911. tmp_hal_status = HAL_ERROR;
  1912. }
  1913. }
  1914. }
  1915. /* Process unlocked */
  1916. __HAL_UNLOCK(hadc);
  1917. /* Return function status */
  1918. return tmp_hal_status;
  1919. }
  1920. /**
  1921. * @brief Enable ADC multimode and configure multimode parameters
  1922. * @note Possibility to update parameters on the fly:
  1923. * This function initializes multimode parameters, following
  1924. * calls to this function can be used to reconfigure some parameters
  1925. * of structure "ADC_MultiModeTypeDef" on the fly, without resetting
  1926. * the ADCs.
  1927. * The setting of these parameters is conditioned to ADC state.
  1928. * For parameters constraints, see comments of structure
  1929. * "ADC_MultiModeTypeDef".
  1930. * @note To move back configuration from multimode to single mode, ADC must
  1931. * be reset (using function HAL_ADC_Init() ).
  1932. * @param hadc Master ADC handle
  1933. * @param multimode Structure of ADC multimode configuration
  1934. * @retval HAL status
  1935. */
  1936. HAL_StatusTypeDef HAL_ADCEx_MultiModeConfigChannel(ADC_HandleTypeDef *hadc, ADC_MultiModeTypeDef *multimode)
  1937. {
  1938. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1939. ADC_Common_TypeDef *tmpADC_Common;
  1940. ADC_HandleTypeDef tmphadcSlave;
  1941. uint32_t tmphadcSlave_conversion_on_going;
  1942. /* Check the parameters */
  1943. assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
  1944. assert_param(IS_ADC_MULTIMODE(multimode->Mode));
  1945. if (multimode->Mode != ADC_MODE_INDEPENDENT)
  1946. {
  1947. assert_param(IS_ADC_DUAL_DATA_MODE(multimode->DualModeData));
  1948. assert_param(IS_ADC_SAMPLING_DELAY(multimode->TwoSamplingDelay));
  1949. }
  1950. /* Process locked */
  1951. __HAL_LOCK(hadc);
  1952. ADC_MULTI_SLAVE(hadc, &tmphadcSlave);
  1953. if (tmphadcSlave.Instance == NULL)
  1954. {
  1955. /* Update ADC state machine to error */
  1956. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1957. /* Process unlocked */
  1958. __HAL_UNLOCK(hadc);
  1959. return HAL_ERROR;
  1960. }
  1961. /* Parameters update conditioned to ADC state: */
  1962. /* Parameters that can be updated when ADC is disabled or enabled without */
  1963. /* conversion on going on regular group: */
  1964. /* - Multimode DATA Format configuration */
  1965. tmphadcSlave_conversion_on_going = LL_ADC_REG_IsConversionOngoing((&tmphadcSlave)->Instance);
  1966. if ((LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
  1967. && (tmphadcSlave_conversion_on_going == 0UL))
  1968. {
  1969. /* Pointer to the common control register */
  1970. tmpADC_Common = __LL_ADC_COMMON_INSTANCE(hadc->Instance);
  1971. /* If multimode is selected, configure all multimode parameters. */
  1972. /* Otherwise, reset multimode parameters (can be used in case of */
  1973. /* transition from multimode to independent mode). */
  1974. if (multimode->Mode != ADC_MODE_INDEPENDENT)
  1975. {
  1976. MODIFY_REG(tmpADC_Common->CCR, ADC_CCR_DAMDF, multimode->DualModeData);
  1977. /* Parameters that can be updated only when ADC is disabled: */
  1978. /* - Multimode mode selection */
  1979. /* - Multimode delay */
  1980. /* Note: Delay range depends on selected resolution: */
  1981. /* from 1 to 9 clock cycles for 16 bits */
  1982. /* from 1 to 9 clock cycles for 14 bits, */
  1983. /* from 1 to 8 clock cycles for 12 bits */
  1984. /* from 1 to 6 clock cycles for 10 and 8 bits */
  1985. /* If a higher delay is selected, it will be clipped to maximum delay */
  1986. /* range */
  1987. if (__LL_ADC_IS_ENABLED_ALL_COMMON_INSTANCE(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) == 0UL)
  1988. {
  1989. MODIFY_REG(tmpADC_Common->CCR,
  1990. ADC_CCR_DUAL |
  1991. ADC_CCR_DELAY,
  1992. multimode->Mode |
  1993. multimode->TwoSamplingDelay
  1994. );
  1995. }
  1996. }
  1997. else /* ADC_MODE_INDEPENDENT */
  1998. {
  1999. CLEAR_BIT(tmpADC_Common->CCR, ADC_CCR_DAMDF);
  2000. /* Parameters that can be updated only when ADC is disabled: */
  2001. /* - Multimode mode selection */
  2002. /* - Multimode delay */
  2003. if (__LL_ADC_IS_ENABLED_ALL_COMMON_INSTANCE(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) == 0UL)
  2004. {
  2005. CLEAR_BIT(tmpADC_Common->CCR, ADC_CCR_DUAL | ADC_CCR_DELAY);
  2006. }
  2007. }
  2008. }
  2009. /* If one of the ADC sharing the same common group is enabled, no update */
  2010. /* could be done on neither of the multimode structure parameters. */
  2011. else
  2012. {
  2013. /* Update ADC state machine to error */
  2014. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  2015. tmp_hal_status = HAL_ERROR;
  2016. }
  2017. /* Process unlocked */
  2018. __HAL_UNLOCK(hadc);
  2019. /* Return function status */
  2020. return tmp_hal_status;
  2021. }
  2022. /**
  2023. * @brief Enable Injected Queue
  2024. * @note This function resets CFGR register JQDIS bit in order to enable the
  2025. * Injected Queue. JQDIS can be written only when ADSTART and JDSTART
  2026. * are both equal to 0 to ensure that no regular nor injected
  2027. * conversion is ongoing.
  2028. * @param hadc ADC handle
  2029. * @retval HAL status
  2030. */
  2031. HAL_StatusTypeDef HAL_ADCEx_EnableInjectedQueue(ADC_HandleTypeDef *hadc)
  2032. {
  2033. HAL_StatusTypeDef tmp_hal_status;
  2034. uint32_t tmp_adc_is_conversion_on_going_regular;
  2035. uint32_t tmp_adc_is_conversion_on_going_injected;
  2036. /* Check the parameters */
  2037. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2038. tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
  2039. tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
  2040. /* Parameter can be set only if no conversion is on-going */
  2041. if ((tmp_adc_is_conversion_on_going_regular == 0UL)
  2042. && (tmp_adc_is_conversion_on_going_injected == 0UL)
  2043. )
  2044. {
  2045. CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_JQDIS);
  2046. /* Update state, clear previous result related to injected queue overflow */
  2047. CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_JQOVF);
  2048. tmp_hal_status = HAL_OK;
  2049. }
  2050. else
  2051. {
  2052. tmp_hal_status = HAL_ERROR;
  2053. }
  2054. return tmp_hal_status;
  2055. }
  2056. /**
  2057. * @brief Disable Injected Queue
  2058. * @note This function sets CFGR register JQDIS bit in order to disable the
  2059. * Injected Queue. JQDIS can be written only when ADSTART and JDSTART
  2060. * are both equal to 0 to ensure that no regular nor injected
  2061. * conversion is ongoing.
  2062. * @param hadc ADC handle
  2063. * @retval HAL status
  2064. */
  2065. HAL_StatusTypeDef HAL_ADCEx_DisableInjectedQueue(ADC_HandleTypeDef *hadc)
  2066. {
  2067. HAL_StatusTypeDef tmp_hal_status;
  2068. uint32_t tmp_adc_is_conversion_on_going_regular;
  2069. uint32_t tmp_adc_is_conversion_on_going_injected;
  2070. /* Check the parameters */
  2071. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2072. tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
  2073. tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
  2074. /* Parameter can be set only if no conversion is on-going */
  2075. if ((tmp_adc_is_conversion_on_going_regular == 0UL)
  2076. && (tmp_adc_is_conversion_on_going_injected == 0UL)
  2077. )
  2078. {
  2079. LL_ADC_INJ_SetQueueMode(hadc->Instance, LL_ADC_INJ_QUEUE_DISABLE);
  2080. tmp_hal_status = HAL_OK;
  2081. }
  2082. else
  2083. {
  2084. tmp_hal_status = HAL_ERROR;
  2085. }
  2086. return tmp_hal_status;
  2087. }
  2088. /**
  2089. * @brief Disable ADC voltage regulator.
  2090. * @note Disabling voltage regulator allows to save power. This operation can
  2091. * be carried out only when ADC is disabled.
  2092. * @note To enable again the voltage regulator, the user is expected to
  2093. * resort to HAL_ADC_Init() API.
  2094. * @param hadc ADC handle
  2095. * @retval HAL status
  2096. */
  2097. HAL_StatusTypeDef HAL_ADCEx_DisableVoltageRegulator(ADC_HandleTypeDef *hadc)
  2098. {
  2099. HAL_StatusTypeDef tmp_hal_status;
  2100. /* Check the parameters */
  2101. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2102. /* Setting of this feature is conditioned to ADC state: ADC must be ADC disabled */
  2103. if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
  2104. {
  2105. LL_ADC_DisableInternalRegulator(hadc->Instance);
  2106. tmp_hal_status = HAL_OK;
  2107. }
  2108. else
  2109. {
  2110. tmp_hal_status = HAL_ERROR;
  2111. }
  2112. return tmp_hal_status;
  2113. }
  2114. /**
  2115. * @brief Enter ADC deep-power-down mode
  2116. * @note This mode is achieved in setting DEEPPWD bit and allows to save power
  2117. * in reducing leakage currents. It is particularly interesting before
  2118. * entering stop modes.
  2119. * @note Setting DEEPPWD automatically clears ADVREGEN bit and disables the
  2120. * ADC voltage regulator. This means that this API encompasses
  2121. * HAL_ADCEx_DisableVoltageRegulator(). Additionally, the internal
  2122. * calibration is lost.
  2123. * @note To exit the ADC deep-power-down mode, the user is expected to
  2124. * resort to HAL_ADC_Init() API as well as to relaunch a calibration
  2125. * with HAL_ADCEx_Calibration_Start() API or to re-apply a previously
  2126. * saved calibration factor.
  2127. * @param hadc ADC handle
  2128. * @retval HAL status
  2129. */
  2130. HAL_StatusTypeDef HAL_ADCEx_EnterADCDeepPowerDownMode(ADC_HandleTypeDef *hadc)
  2131. {
  2132. HAL_StatusTypeDef tmp_hal_status;
  2133. /* Check the parameters */
  2134. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2135. /* Setting of this feature is conditioned to ADC state: ADC must be ADC disabled */
  2136. if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
  2137. {
  2138. LL_ADC_EnableDeepPowerDown(hadc->Instance);
  2139. tmp_hal_status = HAL_OK;
  2140. }
  2141. else
  2142. {
  2143. tmp_hal_status = HAL_ERROR;
  2144. }
  2145. return tmp_hal_status;
  2146. }
  2147. /**
  2148. * @}
  2149. */
  2150. /**
  2151. * @}
  2152. */
  2153. #endif /* HAL_ADC_MODULE_ENABLED */
  2154. /**
  2155. * @}
  2156. */
  2157. /**
  2158. * @}
  2159. */
  2160. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/