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.
 
 
 

2383 lines
92 KiB

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