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.
 
 
 

2540 lines
97 KiB

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