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.
 
 
 

2316 lines
91 KiB

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