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.
 
 
 

2993 lines
115 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_adc.c
  4. * @author MCD Application conversion
  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. * + Initialization and de-initialization functions
  11. * ++ Configuration of ADC
  12. * + Operation functions
  13. * ++ Start, stop, get result of regular conversions of regular
  14. * using 3 possible modes: polling, interruption or DMA.
  15. * + Control functions
  16. * ++ Analog Watchdog configuration
  17. * ++ Channels configuration on regular group
  18. * + State functions
  19. * ++ ADC state machine management
  20. * ++ Interrupts and flags management
  21. *
  22. @verbatim
  23. ==============================================================================
  24. ##### ADC specific features #####
  25. ==============================================================================
  26. [..]
  27. (#) 12-bit, 10-bit, 8-bit or 6-bit configurable resolution.
  28. (#) Interrupt generation at the end of regular conversion and in case of
  29. analog watchdog and overrun events.
  30. (#) Single and continuous conversion modes.
  31. (#) Scan mode for automatic conversion of channel 0 to channel 'n'.
  32. (#) Data alignment with in-built data coherency.
  33. (#) Channel-wise programmable sampling time.
  34. (#) External trigger (timer or EXTI) with configurable polarity for
  35. regular groups.
  36. (#) DMA request generation for transfer of regular group converted data.
  37. (#) Configurable delay between conversions in Dual interleaved mode.
  38. (#) ADC channels selectable single/differential input.
  39. (#) ADC offset on regular groups.
  40. (#) ADC supply requirements: 1.62 V to 3.6 V.
  41. (#) ADC input range: from Vref_ (connected to Vssa) to Vref+ (connected to
  42. Vdda or to an external voltage reference).
  43. ##### How to use this driver #####
  44. ==============================================================================
  45. [..]
  46. (#) Enable the ADC interface
  47. As prerequisite, in HAL_ADC_MspInit(), ADC clock source must be
  48. configured at RCC top level.
  49. Two different clock sources are available:
  50. (++) - the ADC clock can be a specific clock source, coming from the system
  51. clock, the PLLSAI1 or the PLLSAI2 running up to 80MHz.
  52. (++) - or the ADC clock can be derived from the AHB clock of the ADC bus
  53. interface, divided by a programmable factor
  54. (++) For example, in case of PLLSAI2:
  55. (+++) __HAL_RCC_ADC_CLK_ENABLE();
  56. (+++) HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
  57. (+++) where
  58. (+++) PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC
  59. (+++) PeriphClkInit.AdcClockSelection = RCC_ADCCLKSOURCE_PLLSAI2
  60. (#) ADC pins configuration
  61. (++) Enable the clock for the ADC GPIOs using the following function:
  62. __HAL_RCC_GPIOx_CLK_ENABLE();
  63. (++) Configure these ADC pins in analog mode using HAL_GPIO_Init();
  64. (#) Configure the ADC parameters (conversion resolution, data alignment,
  65. continuous mode, ...) using the HAL_ADC_Init() function.
  66. (#) Optionally, perform an automatic ADC calibration to improve the
  67. conversion accuracy using function HAL_ADCEx_Calibration_Start().
  68. (#) Activate the ADC peripheral using one of the start functions:
  69. HAL_ADC_Start(), HAL_ADC_Start_IT(), HAL_ADC_Start_DMA(),
  70. HAL_ADCEx_InjectedStart(), HAL_ADCEx_InjectedStart_IT() or
  71. HAL_ADCEx_MultiModeStart_DMA() when multimode feature is available.
  72. *** Channels to regular group configuration ***
  73. ============================================
  74. [..]
  75. (+) To configure the ADC regular group features, use
  76. HAL_ADC_Init() and HAL_ADC_ConfigChannel() functions.
  77. (+) To activate the continuous mode, use the HAL_ADC_Init() function.
  78. (+) To read the ADC converted values, use the HAL_ADC_GetValue() function.
  79. *** DMA for regular configuration ***
  80. =============================================================
  81. [..]
  82. (+) To enable the DMA mode for regular group, use the
  83. HAL_ADC_Start_DMA() function.
  84. (+) To enable the generation of DMA requests continuously at the end of
  85. the last DMA transfer, resort to DMAContinuousRequests parameter of
  86. ADC handle initialization structure.
  87. @endverbatim
  88. ******************************************************************************
  89. * @attention
  90. *
  91. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  92. *
  93. * Redistribution and use in source and binary forms, with or without modification,
  94. * are permitted provided that the following conditions are met:
  95. * 1. Redistributions of source code must retain the above copyright notice,
  96. * this list of conditions and the following disclaimer.
  97. * 2. Redistributions in binary form must reproduce the above copyright notice,
  98. * this list of conditions and the following disclaimer in the documentation
  99. * and/or other materials provided with the distribution.
  100. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  101. * may be used to endorse or promote products derived from this software
  102. * without specific prior written permission.
  103. *
  104. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  105. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  106. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  107. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  108. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  109. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  110. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  111. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  112. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  113. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  114. *
  115. ******************************************************************************
  116. */
  117. /* Includes ------------------------------------------------------------------*/
  118. #include "stm32l4xx_hal.h"
  119. /** @addtogroup STM32L4xx_HAL_Driver
  120. * @{
  121. */
  122. /** @defgroup ADC ADC
  123. * @brief ADC HAL module driver
  124. * @{
  125. */
  126. #ifdef HAL_ADC_MODULE_ENABLED
  127. /* Private typedef -----------------------------------------------------------*/
  128. /* Private define ------------------------------------------------------------*/
  129. /** @defgroup ADC_Private_Constants ADC Private Constants
  130. * @{
  131. */
  132. #define ADC_CFGR_FIELDS_1 ((uint32_t)(ADC_CFGR_RES | ADC_CFGR_ALIGN |\
  133. ADC_CFGR_CONT | ADC_CFGR_OVRMOD |\
  134. ADC_CFGR_DISCEN | ADC_CFGR_DISCNUM |\
  135. ADC_CFGR_EXTEN | ADC_CFGR_EXTSEL)) /*!< ADC_CFGR fields of parameters that can be updated
  136. when no regular conversion is on-going */
  137. #define ADC_CFGR2_FIELDS ((uint32_t)(ADC_CFGR2_ROVSE | ADC_CFGR2_OVSR |\
  138. ADC_CFGR2_OVSS | ADC_CFGR2_TROVS |\
  139. ADC_CFGR2_ROVSM)) /*!< ADC_CFGR2 fields of parameters that can be updated when no conversion
  140. (neither regular nor injected) is on-going */
  141. #define ADC_CFGR_WD_FIELDS ((uint32_t)(ADC_CFGR_AWD1SGL | ADC_CFGR_JAWD1EN | \
  142. ADC_CFGR_AWD1EN | ADC_CFGR_AWD1CH)) /*!< ADC_CFGR fields of Analog Watchdog parameters that can be updated when no
  143. conversion (neither regular nor injected) is on-going */
  144. #define ADC_OFR_FIELDS ((uint32_t)(ADC_OFR1_OFFSET1 | ADC_OFR1_OFFSET1_CH | ADC_OFR1_OFFSET1_EN)) /*!< ADC_OFR fields of parameters that can be updated when no conversion
  145. (neither regular nor injected) is on-going */
  146. /* Delay to wait before setting ADEN once ADCAL has been reset
  147. must be at least 4 ADC clock cycles.
  148. Assuming lowest ADC clock (140 KHz according to DS), this
  149. 4 ADC clock cycles duration is equal to
  150. 4 / 140,000 = 0.028 ms.
  151. ADC_ENABLE_TIMEOUT set to 2 is a margin large enough to ensure
  152. the 4 ADC clock cycles have elapsed while waiting for ADRDY
  153. to become 1 */
  154. #define ADC_ENABLE_TIMEOUT ((uint32_t) 2) /*!< ADC enable time-out value */
  155. #define ADC_DISABLE_TIMEOUT ((uint32_t) 2) /*!< ADC disable time-out value */
  156. /* Delay for ADC voltage regulator startup time */
  157. /* Maximum delay is 10 microseconds */
  158. /* (refer device RM, parameter Tadcvreg_stup). */
  159. #define ADC_STAB_DELAY_US ((uint32_t) 10) /*!< ADC voltage regulator startup time */
  160. /* Timeout to wait for current conversion on going to be completed. */
  161. /* Timeout fixed to worst case, for 1 channel. */
  162. /* - maximum sampling time (640.5 adc_clk) */
  163. /* - ADC resolution (Tsar 12 bits= 12.5 adc_clk) */
  164. /* - ADC clock with prescaler 256 */
  165. /* 653 * 256 = 167168 clock cycles max */
  166. /* Unit: cycles of CPU clock. */
  167. #define ADC_CONVERSION_TIME_MAX_CPU_CYCLES ((uint32_t) 167168) /*!< ADC conversion completion time-out value */
  168. /**
  169. * @}
  170. */
  171. /* Private macro -------------------------------------------------------------*/
  172. /* Private variables ---------------------------------------------------------*/
  173. /* Private function prototypes -----------------------------------------------*/
  174. /* Exported functions --------------------------------------------------------*/
  175. /** @defgroup ADC_Exported_Functions ADC Exported Functions
  176. * @{
  177. */
  178. /** @defgroup ADC_Exported_Functions_Group1 Initialization and de-initialization functions
  179. * @brief Initialization and Configuration functions
  180. *
  181. @verbatim
  182. ===============================================================================
  183. ##### Initialization and de-initialization functions #####
  184. ===============================================================================
  185. [..] This section provides functions allowing to:
  186. (+) Initialize and configure the ADC.
  187. (+) De-initialize the ADC.
  188. @endverbatim
  189. * @{
  190. */
  191. /**
  192. * @brief Initialize the ADC peripheral and regular group according to
  193. * parameters specified in structure "ADC_InitTypeDef".
  194. * @note As prerequisite, ADC clock must be configured at RCC top level
  195. * depending on possible clock sources: System/PLLSAI1/PLLSAI2 clocks
  196. * or AHB clock.
  197. * @note Possibility to update parameters on the fly:
  198. * this function initializes the ADC MSP (HAL_ADC_MspInit()) only when
  199. * coming from ADC state reset. Following calls to this function can
  200. * be used to reconfigure some parameters of ADC_InitTypeDef
  201. * structure on the fly, without modifying MSP configuration. If ADC
  202. * MSP has to be modified again, HAL_ADC_DeInit() must be called
  203. * before HAL_ADC_Init().
  204. * The setting of these parameters is conditioned by ADC state.
  205. * For parameters constraints, see comments of structure
  206. * "ADC_InitTypeDef".
  207. * @note This function configures the ADC within 2 scopes: scope of entire
  208. * ADC and scope of regular group. For parameters details, see comments
  209. * of structure "ADC_InitTypeDef".
  210. * @note Parameters related to common ADC registers (ADC clock mode) are set
  211. * only if all ADCs are disabled.
  212. * If this is not the case, these common parameters setting are
  213. * bypassed without error reporting: it can be the intended behaviour in
  214. * case of update of a parameter of ADC_InitTypeDef on the fly,
  215. * without disabling the other ADCs.
  216. * @param hadc: ADC handle
  217. * @retval HAL status
  218. */
  219. HAL_StatusTypeDef HAL_ADC_Init(ADC_HandleTypeDef* hadc)
  220. {
  221. HAL_StatusTypeDef tmp_status = HAL_OK;
  222. ADC_Common_TypeDef *tmpADC_Common;
  223. uint32_t tmpCFGR = 0;
  224. uint32_t wait_loop_index = 0;
  225. /* Check ADC handle */
  226. if(hadc == NULL)
  227. {
  228. return HAL_ERROR;
  229. }
  230. /* Check the parameters */
  231. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  232. assert_param(IS_ADC_CLOCKPRESCALER(hadc->Init.ClockPrescaler));
  233. assert_param(IS_ADC_RESOLUTION(hadc->Init.Resolution));
  234. assert_param(IS_ADC_DATA_ALIGN(hadc->Init.DataAlign));
  235. assert_param(IS_ADC_SCAN_MODE(hadc->Init.ScanConvMode));
  236. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
  237. assert_param(IS_ADC_EXTTRIG_EDGE(hadc->Init.ExternalTrigConvEdge));
  238. assert_param(IS_ADC_EXTTRIG(hadc->Init.ExternalTrigConv));
  239. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DMAContinuousRequests));
  240. assert_param(IS_ADC_EOC_SELECTION(hadc->Init.EOCSelection));
  241. assert_param(IS_ADC_OVERRUN(hadc->Init.Overrun));
  242. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.LowPowerAutoWait));
  243. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.OversamplingMode));
  244. if(hadc->Init.ScanConvMode != ADC_SCAN_DISABLE)
  245. {
  246. assert_param(IS_ADC_REGULAR_NB_CONV(hadc->Init.NbrOfConversion));
  247. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DiscontinuousConvMode));
  248. if (hadc->Init.DiscontinuousConvMode == ENABLE)
  249. {
  250. assert_param(IS_ADC_REGULAR_DISCONT_NUMBER(hadc->Init.NbrOfDiscConversion));
  251. }
  252. }
  253. /* DISCEN and CONT bits can't be set at the same time */
  254. assert_param(!((hadc->Init.DiscontinuousConvMode == ENABLE) && (hadc->Init.ContinuousConvMode == ENABLE)));
  255. /* Actions performed only if ADC is coming from state reset: */
  256. /* - Initialization of ADC MSP */
  257. if (hadc->State == HAL_ADC_STATE_RESET)
  258. {
  259. /* Init the low level hardware */
  260. HAL_ADC_MspInit(hadc);
  261. /* Set ADC error code to none */
  262. ADC_CLEAR_ERRORCODE(hadc);
  263. /* Initialize Lock */
  264. hadc->Lock = HAL_UNLOCKED;
  265. }
  266. /* - Exit from deep-power-down mode and ADC voltage regulator enable */
  267. /* Exit deep power down mode if still in that state */
  268. if (HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_DEEPPWD))
  269. {
  270. /* Exit deep power down mode */
  271. CLEAR_BIT(hadc->Instance->CR, ADC_CR_DEEPPWD);
  272. /* System was in deep power down mode, calibration must
  273. be relaunched or a previously saved calibration factor
  274. re-applied once the ADC voltage regulator is enabled */
  275. }
  276. if (HAL_IS_BIT_CLR(hadc->Instance->CR, ADC_CR_ADVREGEN))
  277. {
  278. /* Enable ADC internal voltage regulator then
  279. wait for start-up time */
  280. SET_BIT(hadc->Instance->CR, ADC_CR_ADVREGEN);
  281. wait_loop_index = (ADC_STAB_DELAY_US * (SystemCoreClock / 1000000));
  282. while(wait_loop_index != 0)
  283. {
  284. wait_loop_index--;
  285. }
  286. }
  287. /* Verification that ADC voltage regulator is correctly enabled, whether */
  288. /* or not ADC is coming from state reset (if any potential problem of */
  289. /* clocking, voltage regulator would not be enabled). */
  290. if (HAL_IS_BIT_CLR(hadc->Instance->CR, ADC_CR_ADVREGEN))
  291. {
  292. /* Update ADC state machine to error */
  293. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  294. /* Set ADC error code to ADC IP internal error */
  295. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  296. tmp_status = HAL_ERROR;
  297. }
  298. /* Configuration of ADC parameters if previous preliminary actions are */
  299. /* correctly completed and if there is no conversion on going on regular */
  300. /* group (ADC may already be enabled at this point if HAL_ADC_Init() is */
  301. /* called to update a parameter on the fly). */
  302. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL) &&
  303. (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET) )
  304. {
  305. /* Initialize the ADC state */
  306. SET_BIT(hadc->State, HAL_ADC_STATE_BUSY_INTERNAL);
  307. /* Configuration of common ADC parameters */
  308. /* Pointer to the common control register */
  309. tmpADC_Common = ADC_COMMON_REGISTER(hadc);
  310. /* Parameters update conditioned to ADC state: */
  311. /* Parameters that can be updated only when ADC is disabled: */
  312. /* - clock configuration */
  313. if ((ADC_IS_ENABLE(hadc) == RESET) &&
  314. (ADC_ANY_OTHER_ENABLED(hadc) == RESET) )
  315. {
  316. /* Reset configuration of ADC common register CCR: */
  317. /* */
  318. /* - ADC clock mode and ACC prescaler (CKMODE and PRESC bits)are set */
  319. /* according to adc->Init.ClockPrescaler. It selects the clock */
  320. /* source and sets the clock division factor. */
  321. /* */
  322. /* Some parameters of this register are not reset, since they are set */
  323. /* by other functions and must be kept in case of usage of this */
  324. /* function on the fly (update of a parameter of ADC_InitTypeDef */
  325. /* without needing to reconfigure all other ADC groups/channels */
  326. /* parameters): */
  327. /* - when multimode feature is available, multimode-related */
  328. /* parameters: MDMA, DMACFG, DELAY, DUAL (set by API */
  329. /* HAL_ADCEx_MultiModeConfigChannel() ) */
  330. /* - internal measurement paths: Vbat, temperature sensor, Vref */
  331. /* (set into HAL_ADC_ConfigChannel() or */
  332. /* HAL_ADCEx_InjectedConfigChannel() ) */
  333. MODIFY_REG(tmpADC_Common->CCR, ADC_CCR_PRESC|ADC_CCR_CKMODE, hadc->Init.ClockPrescaler);
  334. }
  335. /* Configuration of ADC: */
  336. /* - resolution Init.Resolution */
  337. /* - data alignment Init.DataAlign */
  338. /* - external trigger to start conversion Init.ExternalTrigConv */
  339. /* - external trigger polarity Init.ExternalTrigConvEdge */
  340. /* - continuous conversion mode Init.ContinuousConvMode */
  341. /* - overrun Init.Overrun */
  342. /* - discontinuous mode Init.DiscontinuousConvMode */
  343. /* - discontinuous mode channel count Init.NbrOfDiscConversion */
  344. tmpCFGR = ( ADC_CFGR_CONTINUOUS(hadc->Init.ContinuousConvMode) |
  345. hadc->Init.Overrun |
  346. hadc->Init.DataAlign |
  347. hadc->Init.Resolution |
  348. ADC_CFGR_REG_DISCONTINUOUS(hadc->Init.DiscontinuousConvMode) |
  349. ADC_CFGR_DISCONTINUOUS_NUM(hadc->Init.NbrOfDiscConversion) );
  350. /* Enable external trigger if trigger selection is different of software */
  351. /* start. */
  352. /* - external trigger to start conversion Init.ExternalTrigConv */
  353. /* - external trigger polarity Init.ExternalTrigConvEdge */
  354. /* Note: parameter ExternalTrigConvEdge set to "trigger edge none" is */
  355. /* equivalent to software start. */
  356. if ((hadc->Init.ExternalTrigConv != ADC_SOFTWARE_START)
  357. && (hadc->Init.ExternalTrigConvEdge != ADC_EXTERNALTRIGCONVEDGE_NONE))
  358. {
  359. tmpCFGR |= ( hadc->Init.ExternalTrigConv | hadc->Init.ExternalTrigConvEdge);
  360. }
  361. /* Update Configuration Register CFGR */
  362. MODIFY_REG(hadc->Instance->CFGR, ADC_CFGR_FIELDS_1, tmpCFGR);
  363. /* Parameters update conditioned to ADC state: */
  364. /* Parameters that can be updated when ADC is disabled or enabled without */
  365. /* conversion on going on regular and injected groups: */
  366. /* - DMA continuous request Init.DMAContinuousRequests */
  367. /* - LowPowerAutoWait feature Init.LowPowerAutoWait */
  368. /* - Oversampling parameters Init.Oversampling */
  369. if (ADC_IS_CONVERSION_ONGOING_REGULAR_INJECTED(hadc) == RESET)
  370. {
  371. tmpCFGR = ( ADC_CFGR_AUTOWAIT(hadc->Init.LowPowerAutoWait) |
  372. ADC_CFGR_DMACONTREQ(hadc->Init.DMAContinuousRequests) );
  373. MODIFY_REG(hadc->Instance->CFGR, ADC_CFGR_FIELDS_2, tmpCFGR);
  374. if (hadc->Init.OversamplingMode == ENABLE)
  375. {
  376. assert_param(IS_ADC_OVERSAMPLING_RATIO(hadc->Init.Oversampling.Ratio));
  377. assert_param(IS_ADC_RIGHT_BIT_SHIFT(hadc->Init.Oversampling.RightBitShift));
  378. assert_param(IS_ADC_TRIGGERED_OVERSAMPLING_MODE(hadc->Init.Oversampling.TriggeredMode));
  379. assert_param(IS_ADC_REGOVERSAMPLING_MODE(hadc->Init.Oversampling.OversamplingStopReset));
  380. if ((hadc->Init.ExternalTrigConv == ADC_SOFTWARE_START)
  381. || (hadc->Init.ExternalTrigConvEdge == ADC_EXTERNALTRIGCONVEDGE_NONE))
  382. {
  383. /* Multi trigger is not applicable to software-triggered conversions */
  384. assert_param((hadc->Init.Oversampling.TriggeredMode == ADC_TRIGGEREDMODE_SINGLE_TRIGGER));
  385. }
  386. /* Configuration of Oversampler: */
  387. /* - Oversampling Ratio */
  388. /* - Right bit shift */
  389. /* - Triggered mode */
  390. /* - Oversampling mode (continued/resumed) */
  391. MODIFY_REG(hadc->Instance->CFGR2, ADC_CFGR2_FIELDS,
  392. ADC_CFGR2_ROVSE |
  393. hadc->Init.Oversampling.Ratio |
  394. hadc->Init.Oversampling.RightBitShift |
  395. hadc->Init.Oversampling.TriggeredMode |
  396. hadc->Init.Oversampling.OversamplingStopReset);
  397. }
  398. else
  399. {
  400. /* Disable Regular OverSampling */
  401. CLEAR_BIT( hadc->Instance->CFGR2, ADC_CFGR2_ROVSE);
  402. }
  403. } /* if (ADC_IS_CONVERSION_ONGOING_REGULAR_INJECTED(hadc) == RESET) */
  404. /* Configuration of regular group sequencer: */
  405. /* - if scan mode is disabled, regular channels sequence length is set to */
  406. /* 0x00: 1 channel converted (channel on regular rank 1) */
  407. /* Parameter "NbrOfConversion" is discarded. */
  408. /* Note: Scan mode is not present by hardware on this device, but */
  409. /* emulated by software for alignment over all STM32 devices. */
  410. /* - if scan mode is enabled, regular channels sequence length is set to */
  411. /* parameter "NbrOfConversion" */
  412. if (hadc->Init.ScanConvMode == ADC_SCAN_ENABLE)
  413. {
  414. /* Set number of ranks in regular group sequencer */
  415. MODIFY_REG(hadc->Instance->SQR1, ADC_SQR1_L, (hadc->Init.NbrOfConversion - (uint8_t)1));
  416. }
  417. else
  418. {
  419. CLEAR_BIT(hadc->Instance->SQR1, ADC_SQR1_L);
  420. }
  421. /* Initialize the ADC state */
  422. /* Clear HAL_ADC_STATE_BUSY_INTERNAL bit, set HAL_ADC_STATE_READY bit */
  423. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_BUSY_INTERNAL, HAL_ADC_STATE_READY);
  424. }
  425. else
  426. {
  427. /* Update ADC state machine to error */
  428. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  429. tmp_status = HAL_ERROR;
  430. } /* if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL) && (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET) ) */
  431. /* Return function status */
  432. return tmp_status;
  433. }
  434. /**
  435. * @brief Deinitialize the ADC peripheral registers to their default reset
  436. * values, with deinitialization of the ADC MSP.
  437. * @note Keep in mind that all ADCs use the same clock: disabling
  438. * the clock will reset all ADCs.
  439. * @note By default, HAL_ADC_DeInit() sets DEEPPWD: this saves more power by
  440. * reducing the leakage currents and is particularly interesting before
  441. * entering STOP 1 or STOP 2 modes.
  442. * @param hadc: ADC handle
  443. * @retval HAL status
  444. */
  445. HAL_StatusTypeDef HAL_ADC_DeInit(ADC_HandleTypeDef* hadc)
  446. {
  447. /* Check ADC handle */
  448. if(hadc == NULL)
  449. {
  450. return HAL_ERROR;
  451. }
  452. /* Check the parameters */
  453. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  454. /* Change ADC state */
  455. SET_BIT(hadc->State, HAL_ADC_STATE_BUSY_INTERNAL);
  456. /* Stop potential conversion on going, on regular and injected groups */
  457. /* No check on ADC_ConversionStop() return status, if the conversion
  458. stop failed, it is up to HAL_ADC_MspDeInit() to reset the ADC IP */
  459. ADC_ConversionStop(hadc, ADC_REGULAR_INJECTED_GROUP);
  460. /* Disable ADC peripheral if conversions are effectively stopped */
  461. /* Flush register JSQR: reset the queue sequencer when injected */
  462. /* queue sequencer is enabled and ADC disabled. */
  463. /* The software and hardware triggers of the injected sequence are both */
  464. /* internally disabled just after the completion of the last valid */
  465. /* injected sequence. */
  466. SET_BIT(hadc->Instance->CFGR, ADC_CFGR_JQM);
  467. /* Disable the ADC peripheral */
  468. /* No check on ADC_Disable() return status, if the ADC disabling process
  469. failed, it is up to HAL_ADC_MspDeInit() to reset the ADC IP */
  470. ADC_Disable(hadc);
  471. /* ========== Reset ADC registers ========== */
  472. /* Reset register IER */
  473. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_AWD3 | ADC_IT_AWD2 | ADC_IT_AWD1 |
  474. ADC_IT_JQOVF | ADC_IT_OVR |
  475. ADC_IT_JEOS | ADC_IT_JEOC |
  476. ADC_IT_EOS | ADC_IT_EOC |
  477. ADC_IT_EOSMP | ADC_IT_RDY ) );
  478. /* Reset register ISR */
  479. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_AWD3 | ADC_FLAG_AWD2 | ADC_FLAG_AWD1 |
  480. ADC_FLAG_JQOVF | ADC_FLAG_OVR |
  481. ADC_FLAG_JEOS | ADC_FLAG_JEOC |
  482. ADC_FLAG_EOS | ADC_FLAG_EOC |
  483. ADC_FLAG_EOSMP | ADC_FLAG_RDY ) );
  484. /* Reset register CR */
  485. /* Bits ADC_CR_JADSTP, ADC_CR_ADSTP, ADC_CR_JADSTART, ADC_CR_ADSTART,
  486. ADC_CR_ADCAL, ADC_CR_ADDIS and ADC_CR_ADEN are in access mode "read-set":
  487. no direct reset applicable.
  488. Update CR register to reset value where doable by software */
  489. CLEAR_BIT(hadc->Instance->CR, ADC_CR_ADVREGEN | ADC_CR_ADCALDIF);
  490. SET_BIT(hadc->Instance->CR, ADC_CR_DEEPPWD);
  491. /* Reset register CFGR */
  492. CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_FIELDS);
  493. SET_BIT(hadc->Instance->CFGR, ADC_CFGR_JQDIS);
  494. /* Reset register CFGR2 */
  495. CLEAR_BIT(hadc->Instance->CFGR2, ADC_CFGR2_ROVSM | ADC_CFGR2_TROVS | ADC_CFGR2_OVSS |
  496. ADC_CFGR2_OVSR | ADC_CFGR2_JOVSE | ADC_CFGR2_ROVSE );
  497. /* Reset register SMPR1 */
  498. CLEAR_BIT(hadc->Instance->SMPR1, ADC_SMPR1_FIELDS);
  499. /* Reset register SMPR2 */
  500. CLEAR_BIT(hadc->Instance->SMPR2, ADC_SMPR2_SMP18 | ADC_SMPR2_SMP17 | ADC_SMPR2_SMP16 |
  501. ADC_SMPR2_SMP15 | ADC_SMPR2_SMP14 | ADC_SMPR2_SMP13 |
  502. ADC_SMPR2_SMP12 | ADC_SMPR2_SMP11 | ADC_SMPR2_SMP10 );
  503. /* Reset register TR1 */
  504. CLEAR_BIT(hadc->Instance->TR1, ADC_TR1_HT1 | ADC_TR1_LT1);
  505. /* Reset register TR2 */
  506. CLEAR_BIT(hadc->Instance->TR2, ADC_TR2_HT2 | ADC_TR2_LT2);
  507. /* Reset register TR3 */
  508. CLEAR_BIT(hadc->Instance->TR3, ADC_TR3_HT3 | ADC_TR3_LT3);
  509. /* Reset register SQR1 */
  510. CLEAR_BIT(hadc->Instance->SQR1, ADC_SQR1_SQ4 | ADC_SQR1_SQ3 | ADC_SQR1_SQ2 |
  511. ADC_SQR1_SQ1 | ADC_SQR1_L);
  512. /* Reset register SQR2 */
  513. CLEAR_BIT(hadc->Instance->SQR2, ADC_SQR2_SQ9 | ADC_SQR2_SQ8 | ADC_SQR2_SQ7 |
  514. ADC_SQR2_SQ6 | ADC_SQR2_SQ5);
  515. /* Reset register SQR3 */
  516. CLEAR_BIT(hadc->Instance->SQR3, ADC_SQR3_SQ14 | ADC_SQR3_SQ13 | ADC_SQR3_SQ12 |
  517. ADC_SQR3_SQ11 | ADC_SQR3_SQ10);
  518. /* Reset register SQR4 */
  519. CLEAR_BIT(hadc->Instance->SQR4, ADC_SQR4_SQ16 | ADC_SQR4_SQ15);
  520. /* Register JSQR was reset when the ADC was disabled */
  521. /* Reset register DR */
  522. /* bits in access mode read only, no direct reset applicable*/
  523. /* Reset register OFR1 */
  524. CLEAR_BIT(hadc->Instance->OFR1, ADC_OFR1_OFFSET1_EN | ADC_OFR1_OFFSET1_CH | ADC_OFR1_OFFSET1);
  525. /* Reset register OFR2 */
  526. CLEAR_BIT(hadc->Instance->OFR2, ADC_OFR2_OFFSET2_EN | ADC_OFR2_OFFSET2_CH | ADC_OFR2_OFFSET2);
  527. /* Reset register OFR3 */
  528. CLEAR_BIT(hadc->Instance->OFR3, ADC_OFR3_OFFSET3_EN | ADC_OFR3_OFFSET3_CH | ADC_OFR3_OFFSET3);
  529. /* Reset register OFR4 */
  530. CLEAR_BIT(hadc->Instance->OFR4, ADC_OFR4_OFFSET4_EN | ADC_OFR4_OFFSET4_CH | ADC_OFR4_OFFSET4);
  531. /* Reset registers JDR1, JDR2, JDR3, JDR4 */
  532. /* bits in access mode read only, no direct reset applicable*/
  533. /* Reset register AWD2CR */
  534. CLEAR_BIT(hadc->Instance->AWD2CR, ADC_AWD2CR_AWD2CH);
  535. /* Reset register AWD3CR */
  536. CLEAR_BIT(hadc->Instance->AWD3CR, ADC_AWD3CR_AWD3CH);
  537. /* Reset register DIFSEL */
  538. CLEAR_BIT(hadc->Instance->DIFSEL, ADC_DIFSEL_DIFSEL);
  539. /* Reset register CALFACT */
  540. CLEAR_BIT(hadc->Instance->CALFACT, ADC_CALFACT_CALFACT_D | ADC_CALFACT_CALFACT_S);
  541. /* ========== Reset common ADC registers ========== */
  542. /* Software is allowed to change common parameters only when all the other
  543. ADCs are disabled. */
  544. if ((ADC_IS_ENABLE(hadc) == RESET) &&
  545. (ADC_ANY_OTHER_ENABLED(hadc) == RESET) )
  546. {
  547. /* Reset configuration of ADC common register CCR:
  548. - clock mode: CKMODE, PRESCEN
  549. - multimode related parameters (when this feature is available): MDMA,
  550. DMACFG, DELAY, DUAL (set by HAL_ADCEx_MultiModeConfigChannel() API)
  551. - internal measurement paths: Vbat, temperature sensor, Vref (set into
  552. HAL_ADC_ConfigChannel() or HAL_ADCEx_InjectedConfigChannel() )
  553. */
  554. ADC_CLEAR_COMMON_CONTROL_REGISTER(hadc);
  555. }
  556. /* DeInit the low level hardware.
  557. For example:
  558. __HAL_RCC_ADC_FORCE_RESET();
  559. __HAL_RCC_ADC_RELEASE_RESET();
  560. __HAL_RCC_ADC_CLK_DISABLE();
  561. Keep in mind that all ADCs use the same clock: disabling
  562. the clock will reset all ADCs.
  563. */
  564. HAL_ADC_MspDeInit(hadc);
  565. /* Set ADC error code to none */
  566. ADC_CLEAR_ERRORCODE(hadc);
  567. /* Reset injected channel configuration parameters */
  568. hadc->InjectionConfig.ContextQueue = 0;
  569. hadc->InjectionConfig.ChannelCount = 0;
  570. /* Change ADC state */
  571. hadc->State = HAL_ADC_STATE_RESET;
  572. /* Process unlocked */
  573. __HAL_UNLOCK(hadc);
  574. /* Return function status */
  575. return HAL_OK;
  576. }
  577. /**
  578. * @brief Initialize the ADC MSP.
  579. * @param hadc: ADC handle
  580. * @retval None
  581. */
  582. __weak void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
  583. {
  584. /* Prevent unused argument(s) compilation warning */
  585. UNUSED(hadc);
  586. /* NOTE : This function should not be modified. When the callback is needed,
  587. function HAL_ADC_MspInit must be implemented in the user file.
  588. */
  589. }
  590. /**
  591. * @brief DeInitialize the ADC MSP.
  592. * @param hadc: ADC handle
  593. * @note All ADCs use the same clock: disabling the clock will reset all ADCs.
  594. * @retval None
  595. */
  596. __weak void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc)
  597. {
  598. /* Prevent unused argument(s) compilation warning */
  599. UNUSED(hadc);
  600. /* NOTE : This function should not be modified. When the callback is needed,
  601. function HAL_ADC_MspDeInit must be implemented in the user file.
  602. */
  603. }
  604. /**
  605. * @}
  606. */
  607. /** @defgroup ADC_Exported_Functions_Group2 Input and Output operation functions
  608. * @brief IO operation functions
  609. *
  610. @verbatim
  611. ===============================================================================
  612. ##### IO operation functions #####
  613. ===============================================================================
  614. [..] This section provides functions allowing to:
  615. (+) Start conversion of regular group.
  616. (+) Stop conversion of regular group.
  617. (+) Poll for conversion complete on regular group.
  618. (+) Poll for conversion event.
  619. (+) Get result of regular channel conversion.
  620. (+) Start conversion of regular group and enable interruptions.
  621. (+) Stop conversion of regular group and disable interruptions.
  622. (+) Handle ADC interrupt request
  623. (+) Start conversion of regular group and enable DMA transfer.
  624. (+) Stop conversion of regular group and disable ADC DMA transfer.
  625. @endverbatim
  626. * @{
  627. */
  628. /**
  629. * @brief Enable ADC, start conversion of regular group.
  630. * @note Interruptions enabled in this function: None.
  631. * @note Case of multimode enabled (when multimode feature is available):
  632. * if ADC is Slave, ADC is enabled but conversion is not started,
  633. * if ADC is master, ADC is enabled and multimode conversion is started.
  634. * @param hadc: ADC handle
  635. * @retval HAL status
  636. */
  637. HAL_StatusTypeDef HAL_ADC_Start(ADC_HandleTypeDef* hadc)
  638. {
  639. ADC_TypeDef *tmpADC_Master;
  640. HAL_StatusTypeDef tmp_status = HAL_OK;
  641. /* Check the parameters */
  642. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  643. /* if a regular conversion is already on-going (i.e. ADSTART is set),
  644. don't restart the conversion. */
  645. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc))
  646. {
  647. return HAL_BUSY;
  648. }
  649. else
  650. {
  651. /* Process locked */
  652. __HAL_LOCK(hadc);
  653. /* Enable the ADC peripheral */
  654. tmp_status = ADC_Enable(hadc);
  655. /* Start conversion if ADC is effectively enabled */
  656. if (tmp_status == HAL_OK)
  657. {
  658. /* State machine update: Check if an injected conversion is ongoing */
  659. if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  660. {
  661. /* Reset ADC error code fields related to regular conversions only */
  662. CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR|HAL_ADC_ERROR_DMA));
  663. }
  664. else
  665. {
  666. /* Set ADC error code to none */
  667. ADC_CLEAR_ERRORCODE(hadc);
  668. }
  669. /* Clear HAL_ADC_STATE_READY and regular conversion results bits, set HAL_ADC_STATE_REG_BUSY bit */
  670. 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);
  671. /* Reset HAL_ADC_STATE_MULTIMODE_SLAVE bit
  672. - by default if ADC is Master or Independent or if multimode feature is not available
  673. - if multimode setting is set to independent mode (no dual regular or injected conversions are configured) */
  674. if (ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc))
  675. {
  676. CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  677. }
  678. /* Clear regular group conversion flag and overrun flag */
  679. /* (To ensure of no unknown state from potential previous ADC operations) */
  680. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
  681. /* Enable conversion of regular group. */
  682. /* If software start has been selected, conversion starts immediately. */
  683. /* If external trigger has been selected, conversion starts at next */
  684. /* trigger event. */
  685. /* Case of multimode enabled (when multimode feature is available): */
  686. /* - if ADC is slave and dual regular conversions are enabled, ADC is */
  687. /* enabled only (conversion is not started), */
  688. /* - if ADC is master, ADC is enabled and conversion is started. */
  689. if (ADC_INDEPENDENT_OR_NONMULTIMODEREGULAR_SLAVE(hadc))
  690. {
  691. /* Multimode feature is not available or ADC Instance is Independent or Master,
  692. or is not Slave ADC with dual regular conversions enabled.
  693. Then, set HAL_ADC_STATE_INJ_BUSY bit and reset HAL_ADC_STATE_INJ_EOC bit if JAUTO is set. */
  694. if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_JAUTO) != RESET)
  695. {
  696. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
  697. }
  698. /* Process unlocked */
  699. __HAL_UNLOCK(hadc);
  700. /* Start ADC */
  701. SET_BIT(hadc->Instance->CR, ADC_CR_ADSTART);
  702. }
  703. else
  704. {
  705. SET_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  706. /* if Master ADC JAUTO bit is set, update Slave State in setting
  707. HAL_ADC_STATE_INJ_BUSY bit and in resetting HAL_ADC_STATE_INJ_EOC bit */
  708. tmpADC_Master = ADC_MASTER_REGISTER(hadc);
  709. if (READ_BIT(tmpADC_Master->CFGR, ADC_CFGR_JAUTO) != RESET)
  710. {
  711. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
  712. } /* if (READ_BIT(tmpADC_Master->CFGR, ADC_CFGR_JAUTO) != RESET) */
  713. /* Process unlocked */
  714. __HAL_UNLOCK(hadc);
  715. } /* if (ADC_INDEPENDENT_OR_NONMULTIMODEREGULAR_SLAVE(hadc)) */
  716. }
  717. else
  718. {
  719. /* Process unlocked */
  720. __HAL_UNLOCK(hadc);
  721. }
  722. /* Return function status */
  723. return tmp_status;
  724. }
  725. }
  726. /**
  727. * @brief Stop ADC conversion of regular and injected groups, disable ADC peripheral.
  728. * @param hadc: ADC handle
  729. * @retval HAL status.
  730. */
  731. HAL_StatusTypeDef HAL_ADC_Stop(ADC_HandleTypeDef* hadc)
  732. {
  733. HAL_StatusTypeDef tmp_status = HAL_OK;
  734. /* Check the parameters */
  735. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  736. /* Process locked */
  737. __HAL_LOCK(hadc);
  738. /* 1. Stop potential regular and injected on-going conversions */
  739. tmp_status = ADC_ConversionStop(hadc, ADC_REGULAR_INJECTED_GROUP);
  740. /* Disable ADC peripheral if conversions are effectively stopped */
  741. if (tmp_status == HAL_OK)
  742. {
  743. /* 2. Disable the ADC peripheral */
  744. tmp_status = ADC_Disable(hadc);
  745. /* Check if ADC is effectively disabled */
  746. if (tmp_status == HAL_OK)
  747. {
  748. /* Change ADC state */
  749. /* Clear HAL_ADC_STATE_REG_BUSY and HAL_ADC_STATE_INJ_BUSY bits, set HAL_ADC_STATE_READY bit */
  750. ADC_STATE_CLR_SET(hadc->State, (HAL_ADC_STATE_REG_BUSY|HAL_ADC_STATE_INJ_BUSY), HAL_ADC_STATE_READY);
  751. }
  752. }
  753. /* Process unlocked */
  754. __HAL_UNLOCK(hadc);
  755. /* Return function status */
  756. return tmp_status;
  757. }
  758. /**
  759. * @brief Wait for regular group conversion to be completed.
  760. * @param hadc: ADC handle
  761. * @param Timeout: Timeout value in millisecond.
  762. * @note Depending on hadc->Init.EOCSelection, EOS or EOC is
  763. * checked and cleared depending on AUTDLY bit status.
  764. * @note HAL_ADC_PollForConversion() returns HAL_ERROR if EOC is polled in a
  765. * DMA-managed conversions configuration: indeed, EOC is immediately
  766. * reset by the DMA reading the DR register when the converted data is
  767. * available. Therefore, EOC is set for a too short period to be
  768. * reliably polled.
  769. * @retval HAL status
  770. */
  771. HAL_StatusTypeDef HAL_ADC_PollForConversion(ADC_HandleTypeDef* hadc, uint32_t Timeout)
  772. {
  773. uint32_t tickstart;
  774. uint32_t tmp_Flag_End = 0x00;
  775. ADC_TypeDef *tmpADC_Master;
  776. uint32_t tmp_cfgr = 0x00;
  777. uint32_t tmp_eos_raised = 0x01; /* by default, assume that EOS is set,
  778. tmp_eos_raised will be corrected
  779. accordingly during API execution */
  780. /* Check the parameters */
  781. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  782. /* If end of sequence selected */
  783. if (hadc->Init.EOCSelection == ADC_EOC_SEQ_CONV)
  784. {
  785. tmp_Flag_End = ADC_FLAG_EOS;
  786. }
  787. else /* end of conversion selected */
  788. {
  789. /* Check that the ADC is not in a DMA-based configuration. Otherwise,
  790. returns an error. */
  791. /* Check whether dual regular conversions are disabled or unavailable. */
  792. if (ADC_IS_DUAL_REGULAR_CONVERSION_ENABLE(hadc) == RESET)
  793. {
  794. /* Check DMAEN bit in handle ADC CFGR register */
  795. if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_DMAEN) != RESET)
  796. {
  797. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  798. return HAL_ERROR;
  799. }
  800. }
  801. else
  802. {
  803. /* Else need to check Common register CCR MDMA bit field. */
  804. if (ADC_MULTIMODE_DMA_ENABLED())
  805. {
  806. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  807. return HAL_ERROR;
  808. }
  809. }
  810. /* no DMA transfer detected, polling ADC_FLAG_EOC is possible */
  811. tmp_Flag_End = ADC_FLAG_EOC;
  812. }
  813. /* Get timeout */
  814. tickstart = HAL_GetTick();
  815. /* Wait until End of Conversion or Sequence flag is raised */
  816. while (HAL_IS_BIT_CLR(hadc->Instance->ISR, tmp_Flag_End))
  817. {
  818. /* Check if timeout is disabled (set to infinite wait) */
  819. if(Timeout != HAL_MAX_DELAY)
  820. {
  821. if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
  822. {
  823. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  824. return HAL_TIMEOUT;
  825. }
  826. }
  827. }
  828. /* Next, to clear the polled flag as well as to update the handle State,
  829. EOS is checked and the relevant configuration register is retrieved. */
  830. /* 1. Check whether or not EOS is set */
  831. if (HAL_IS_BIT_CLR(hadc->Instance->ISR, ADC_FLAG_EOS))
  832. {
  833. tmp_eos_raised = 0;
  834. }
  835. /* 2. Check whether or not hadc is the handle of a Slave ADC with dual
  836. regular conversions enabled. */
  837. if (ADC_INDEPENDENT_OR_NONMULTIMODEREGULAR_SLAVE(hadc))
  838. {
  839. /* Retrieve handle ADC CFGR register */
  840. tmp_cfgr = READ_REG(hadc->Instance->CFGR);
  841. }
  842. else
  843. {
  844. /* Retrieve Master ADC CFGR register */
  845. tmpADC_Master = ADC_MASTER_REGISTER(hadc);
  846. tmp_cfgr = READ_REG(tmpADC_Master->CFGR);
  847. }
  848. /* Clear polled flag */
  849. if (tmp_Flag_End == ADC_FLAG_EOS)
  850. {
  851. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOS);
  852. }
  853. else
  854. {
  855. /* Clear end of conversion EOC flag of regular group if low power feature */
  856. /* "LowPowerAutoWait " is disabled, to not interfere with this feature */
  857. /* until data register is read using function HAL_ADC_GetValue(). */
  858. /* For regular groups, no new conversion will start before EOC is cleared.*/
  859. /* Note that 1. reading DR clears EOC. */
  860. /* 2. in multimode with dual regular conversions enabled (when */
  861. /* multimode feature is available), Master AUTDLY bit is */
  862. /* checked. */
  863. if (READ_BIT (tmp_cfgr, ADC_CFGR_AUTDLY) == RESET)
  864. {
  865. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOC);
  866. }
  867. }
  868. /* Update ADC state machine */
  869. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  870. /* If 1. EOS is set
  871. 2. conversions are software-triggered
  872. 3. CONT bit is reset (that of handle ADC or Master ADC if applicable)
  873. Then regular conversions are over and HAL_ADC_STATE_REG_BUSY can be reset.
  874. 4. additionally, if no injected conversions are on-going, HAL_ADC_STATE_READY
  875. can be set */
  876. if ((tmp_eos_raised)
  877. && (ADC_IS_SOFTWARE_START_REGULAR(hadc))
  878. && (READ_BIT (tmp_cfgr, ADC_CFGR_CONT) == RESET))
  879. {
  880. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  881. /* If no injected conversion on-going, set HAL_ADC_STATE_READY bit */
  882. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  883. {
  884. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  885. }
  886. }
  887. /* Return API HAL status */
  888. return HAL_OK;
  889. }
  890. /**
  891. * @brief Poll for ADC event.
  892. * @param hadc: ADC handle
  893. * @param EventType: the ADC event type.
  894. * This parameter can be one of the following values:
  895. * @arg @ref ADC_EOSMP_EVENT ADC End of Sampling event
  896. * @arg @ref ADC_AWD_EVENT ADC Analog watchdog 1 event
  897. * @arg @ref ADC_AWD2_EVENT ADC Analog watchdog 2 event
  898. * @arg @ref ADC_AWD3_EVENT ADC Analog watchdog 3 event
  899. * @arg @ref ADC_OVR_EVENT ADC Overrun event
  900. * @arg @ref ADC_JQOVF_EVENT ADC Injected context queue overflow event
  901. * @param Timeout: Timeout value in millisecond.
  902. * @note The relevant flag is cleared if found to be set, except for ADC_FLAG_OVR.
  903. * Indeed, the latter is reset only if hadc->Init.Overrun field is set
  904. * to ADC_OVR_DATA_OVERWRITTEN. Otherwise, DR may be potentially overwritten
  905. * by a new converted data as soon as OVR is cleared.
  906. * To reset OVR flag once the preserved data is retrieved, the user can resort
  907. * to macro __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  908. * @retval HAL status
  909. */
  910. HAL_StatusTypeDef HAL_ADC_PollForEvent(ADC_HandleTypeDef* hadc, uint32_t EventType, uint32_t Timeout)
  911. {
  912. uint32_t tickstart;
  913. /* Check the parameters */
  914. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  915. assert_param(IS_ADC_EVENT_TYPE(EventType));
  916. tickstart = HAL_GetTick();
  917. /* Check selected event flag */
  918. while(__HAL_ADC_GET_FLAG(hadc, EventType) == RESET)
  919. {
  920. /* Check if timeout is disabled (set to infinite wait) */
  921. if(Timeout != HAL_MAX_DELAY)
  922. {
  923. if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
  924. {
  925. /* Update ADC state machine to timeout */
  926. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  927. /* Process unlocked */
  928. __HAL_UNLOCK(hadc);
  929. return HAL_TIMEOUT;
  930. }
  931. }
  932. }
  933. switch(EventType)
  934. {
  935. /* End Of Sampling event */
  936. case ADC_EOSMP_EVENT:
  937. /* Change ADC state */
  938. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOSMP);
  939. /* Clear the End Of Sampling flag */
  940. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOSMP);
  941. break;
  942. /* Analog watchdog (level out of window) event */
  943. /* Note: In case of several analog watchdog enabled, if needed to know */
  944. /* which one triggered and on which ADCx, test ADC state of Analog Watchdog */
  945. /* flags HAL_ADC_STATE_AWD/2/3 function. */
  946. /* For example: "if (HAL_ADC_GetState(hadc1) == HAL_ADC_STATE_AWD) " */
  947. /* "if (HAL_ADC_GetState(hadc1) == HAL_ADC_STATE_AWD2)" */
  948. /* "if (HAL_ADC_GetState(hadc1) == HAL_ADC_STATE_AWD3)" */
  949. case ADC_AWD_EVENT:
  950. /* Change ADC state */
  951. SET_BIT(hadc->State, HAL_ADC_STATE_AWD1);
  952. /* Clear ADC analog watchdog flag */
  953. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD1);
  954. break;
  955. /* Check analog watchdog 2 flag */
  956. case ADC_AWD2_EVENT:
  957. /* Change ADC state */
  958. SET_BIT(hadc->State, HAL_ADC_STATE_AWD2);
  959. /* Clear ADC analog watchdog flag */
  960. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD2);
  961. break;
  962. /* Check analog watchdog 3 flag */
  963. case ADC_AWD3_EVENT:
  964. /* Change ADC state */
  965. SET_BIT(hadc->State, HAL_ADC_STATE_AWD3);
  966. /* Clear ADC analog watchdog flag */
  967. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD3);
  968. break;
  969. /* Injected context queue overflow event */
  970. case ADC_JQOVF_EVENT:
  971. /* Change ADC state */
  972. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_JQOVF);
  973. /* Set ADC error code to Injected context queue overflow */
  974. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_JQOVF);
  975. /* Clear ADC Injected context queue overflow flag */
  976. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JQOVF);
  977. break;
  978. /* Overrun event */
  979. default: /* Case ADC_OVR_EVENT */
  980. /* If overrun is set to overwrite previous data, overrun event is not */
  981. /* considered as an error. */
  982. /* (cf ref manual "Managing conversions without using the DMA and without */
  983. /* overrun ") */
  984. if (hadc->Init.Overrun == ADC_OVR_DATA_PRESERVED)
  985. {
  986. /* Change ADC state */
  987. SET_BIT(hadc->State, HAL_ADC_STATE_REG_OVR);
  988. /* Set ADC error code to overrun */
  989. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_OVR);
  990. }
  991. else
  992. {
  993. /* Clear ADC Overrun flag only if Overrun is set to ADC_OVR_DATA_OVERWRITTEN
  994. otherwise, DR is potentially overwritten by new converted data as soon
  995. as OVR is cleared. */
  996. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  997. }
  998. break;
  999. }
  1000. /* Return API HAL status */
  1001. return HAL_OK;
  1002. }
  1003. /**
  1004. * @brief Enable ADC, start conversion of regular group with interruption.
  1005. * @note Interruptions enabled in this function according to initialization
  1006. * setting : EOC (end of conversion), EOS (end of sequence),
  1007. * OVR overrun.
  1008. * Each of these interruptions has its dedicated callback function.
  1009. * @note Case of multimode enabled (when multimode feature is available):
  1010. * HAL_ADC_Start_IT() must be called for ADC Slave first, then for
  1011. * ADC Master.
  1012. * For ADC Slave, ADC is enabled only (conversion is not started).
  1013. * For ADC Master, ADC is enabled and multimode conversion is started.
  1014. * @note To guarantee a proper reset of all interruptions once all the needed
  1015. * conversions are obtained, HAL_ADC_Stop_IT() must be called to ensure
  1016. * a correct stop of the IT-based conversions.
  1017. * @note By default, HAL_ADC_Start_IT() doesn't enable the End Of Sampling
  1018. * interruption. If required (e.g. in case of oversampling with trigger
  1019. * mode), the user must
  1020. * 1. first clear the EOSMP flag if set with macro __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOSMP)
  1021. * 2. then enable the EOSMP interrupt with macro __HAL_ADC_ENABLE_IT(hadc, ADC_IT_EOSMP)
  1022. * before calling HAL_ADC_Start_IT().
  1023. * @param hadc: ADC handle
  1024. * @retval HAL status
  1025. */
  1026. HAL_StatusTypeDef HAL_ADC_Start_IT(ADC_HandleTypeDef* hadc)
  1027. {
  1028. HAL_StatusTypeDef tmp_status = HAL_OK;
  1029. ADC_TypeDef *tmpADC_Master;
  1030. /* Check the parameters */
  1031. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1032. /* if a regular conversion is already on-going (i.e. ADSTART is set),
  1033. don't restart the conversion. */
  1034. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc))
  1035. {
  1036. return HAL_BUSY;
  1037. }
  1038. else
  1039. {
  1040. /* Process locked */
  1041. __HAL_LOCK(hadc);
  1042. /* Enable the ADC peripheral */
  1043. tmp_status = ADC_Enable(hadc);
  1044. /* Start conversion if ADC is effectively enabled */
  1045. if (tmp_status == HAL_OK)
  1046. {
  1047. /* State machine update: Check if an injected conversion is ongoing */
  1048. if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  1049. {
  1050. /* Reset ADC error code fields related to regular conversions only */
  1051. CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR|HAL_ADC_ERROR_DMA));
  1052. }
  1053. else
  1054. {
  1055. /* Set ADC error code to none */
  1056. ADC_CLEAR_ERRORCODE(hadc);
  1057. }
  1058. /* Clear HAL_ADC_STATE_READY and regular conversion results bits, set HAL_ADC_STATE_REG_BUSY bit */
  1059. 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);
  1060. /* Reset HAL_ADC_STATE_MULTIMODE_SLAVE bit
  1061. - by default if ADC is Master or Independent or if multimode feature is not available
  1062. - if MultiMode setting is set to independent mode (no dual regular or injected conversions are configured) */
  1063. if (ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc))
  1064. {
  1065. CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  1066. }
  1067. /* Clear regular group conversion flag and overrun flag */
  1068. /* (To ensure of no unknown state from potential previous ADC operations) */
  1069. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
  1070. /* By default, disable all interruptions before enabling the desired ones */
  1071. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_EOS | ADC_IT_OVR));
  1072. /* Enable required interruptions */
  1073. switch(hadc->Init.EOCSelection)
  1074. {
  1075. case ADC_EOC_SEQ_CONV:
  1076. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_EOS);
  1077. break;
  1078. /* case ADC_EOC_SINGLE_CONV */
  1079. default:
  1080. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_EOC);
  1081. break;
  1082. }
  1083. /* If hadc->Init.Overrun is set to ADC_OVR_DATA_PRESERVED, only then is
  1084. ADC_IT_OVR enabled; otherwise data overwrite is considered as normal
  1085. behavior and no CPU time is lost for a non-processed interruption */
  1086. if (hadc->Init.Overrun == ADC_OVR_DATA_PRESERVED)
  1087. {
  1088. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);
  1089. }
  1090. /* Enable conversion of regular group. */
  1091. /* If software start has been selected, conversion starts immediately. */
  1092. /* If external trigger has been selected, conversion starts at next */
  1093. /* trigger event. */
  1094. /* Case of multimode enabled (when multimode feature is available): */
  1095. /* - if ADC is slave and dual regular conversions are enabled, ADC is */
  1096. /* enabled only (conversion is not started), */
  1097. /* - if ADC is master, ADC is enabled and conversion is started. */
  1098. if (ADC_INDEPENDENT_OR_NONMULTIMODEREGULAR_SLAVE(hadc) )
  1099. {
  1100. /* Multimode feature is not available or ADC Instance is Independent or Master,
  1101. or is not Slave ADC with dual regular conversions enabled.
  1102. Then set HAL_ADC_STATE_INJ_BUSY and reset HAL_ADC_STATE_INJ_EOC if JAUTO is set. */
  1103. if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_JAUTO) != RESET)
  1104. {
  1105. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
  1106. /* Enable as well injected interruptions in case
  1107. HAL_ADCEx_InjectedStart_IT() has not been called beforehand. This
  1108. allows to start regular and injected conversions when JAUTO is
  1109. set with a single call to HAL_ADC_Start_IT() */
  1110. switch(hadc->Init.EOCSelection)
  1111. {
  1112. case ADC_EOC_SEQ_CONV:
  1113. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC);
  1114. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOS);
  1115. break;
  1116. /* case ADC_EOC_SINGLE_CONV */
  1117. default:
  1118. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOS);
  1119. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOC);
  1120. break;
  1121. }
  1122. } /* if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_JAUTO) != RESET) */
  1123. /* Process unlocked */
  1124. __HAL_UNLOCK(hadc);
  1125. /* Start ADC */
  1126. SET_BIT(hadc->Instance->CR, ADC_CR_ADSTART);
  1127. }
  1128. else
  1129. {
  1130. /* hadc is the handle of a Slave ADC with dual regular conversions
  1131. enabled. Therefore, ADC_CR_ADSTART is NOT set */
  1132. SET_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  1133. /* if Master ADC JAUTO bit is set, Slave injected interruptions
  1134. are enabled nevertheless (for same reason as above) */
  1135. tmpADC_Master = ADC_MASTER_REGISTER(hadc);
  1136. if (READ_BIT(tmpADC_Master->CFGR, ADC_CFGR_JAUTO) != RESET)
  1137. {
  1138. /* First, update Slave State in setting HAL_ADC_STATE_INJ_BUSY bit
  1139. and in resetting HAL_ADC_STATE_INJ_EOC bit */
  1140. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
  1141. /* Next, set Slave injected interruptions */
  1142. switch(hadc->Init.EOCSelection)
  1143. {
  1144. case ADC_EOC_SEQ_CONV:
  1145. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC);
  1146. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOS);
  1147. break;
  1148. /* case ADC_EOC_SINGLE_CONV */
  1149. default:
  1150. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOS);
  1151. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOC);
  1152. break;
  1153. }
  1154. } /* if (READ_BIT(tmpADC_Master->CFGR, ADC_CFGR_JAUTO) != RESET) */
  1155. /* Process unlocked */
  1156. __HAL_UNLOCK(hadc);
  1157. } /* if (ADC_INDEPENDENT_OR_NONMULTIMODEREGULAR_SLAVE(hadc) ) */
  1158. } /* if (tmp_status == HAL_OK) */
  1159. else
  1160. {
  1161. /* Process unlocked */
  1162. __HAL_UNLOCK(hadc);
  1163. }
  1164. /* Return function status */
  1165. return tmp_status;
  1166. }
  1167. }
  1168. /**
  1169. * @brief Stop ADC conversion of regular groups when interruptions are enabled.
  1170. * @note Stop as well injected conversions and disable ADC peripheral.
  1171. * @param hadc: ADC handle
  1172. * @retval HAL status.
  1173. */
  1174. HAL_StatusTypeDef HAL_ADC_Stop_IT(ADC_HandleTypeDef* hadc)
  1175. {
  1176. HAL_StatusTypeDef tmp_status = HAL_OK;
  1177. /* Check the parameters */
  1178. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1179. /* Process locked */
  1180. __HAL_LOCK(hadc);
  1181. /* 1. Stop potential regular and injected on-going conversions */
  1182. tmp_status = ADC_ConversionStop(hadc, ADC_REGULAR_INJECTED_GROUP);
  1183. /* Disable ADC peripheral if conversions are effectively stopped */
  1184. if (tmp_status == HAL_OK)
  1185. {
  1186. /* Disable all interrupts */
  1187. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_EOS | ADC_IT_OVR));
  1188. /* 2. Disable the ADC peripheral */
  1189. tmp_status = ADC_Disable(hadc);
  1190. /* Check if ADC is effectively disabled */
  1191. if (tmp_status == HAL_OK)
  1192. {
  1193. /* Change ADC state */
  1194. /* Clear HAL_ADC_STATE_REG_BUSY and HAL_ADC_STATE_INJ_BUSY bits, set HAL_ADC_STATE_READY bit */
  1195. ADC_STATE_CLR_SET(hadc->State, (HAL_ADC_STATE_REG_BUSY|HAL_ADC_STATE_INJ_BUSY), HAL_ADC_STATE_READY);
  1196. }
  1197. }
  1198. /* Process unlocked */
  1199. __HAL_UNLOCK(hadc);
  1200. /* Return function status */
  1201. return tmp_status;
  1202. }
  1203. /**
  1204. * @brief Enable ADC, start conversion of regular group and transfer result through DMA.
  1205. * @note Interruptions enabled in this function:
  1206. * overrun (if applicable), DMA half transfer, DMA transfer complete.
  1207. * Each of these interruptions has its dedicated callback function.
  1208. * @note Case of multimode enabled (when multimode feature is available): HAL_ADC_Start_DMA()
  1209. * is designed for single-ADC mode only. For multimode, the dedicated
  1210. * HAL_ADCEx_MultiModeStart_DMA() function must be used.
  1211. * @param hadc: ADC handle
  1212. * @param pData: Destination Buffer address.
  1213. * @param Length: Length of data to be transferred from ADC peripheral to memory (in bytes)
  1214. * @retval None
  1215. */
  1216. HAL_StatusTypeDef HAL_ADC_Start_DMA(ADC_HandleTypeDef* hadc, uint32_t* pData, uint32_t Length)
  1217. {
  1218. HAL_StatusTypeDef tmp_status = HAL_OK;
  1219. /* Check the parameters */
  1220. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1221. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc))
  1222. {
  1223. return HAL_BUSY;
  1224. }
  1225. else
  1226. {
  1227. /* Process locked */
  1228. __HAL_LOCK(hadc);
  1229. /* Ensure that dual regular conversions are not enabled or unavailable. */
  1230. /* Otherwise, dedicated API HAL_ADCEx_MultiModeStart_DMA() must be used. */
  1231. if (ADC_IS_DUAL_REGULAR_CONVERSION_ENABLE(hadc) == RESET)
  1232. {
  1233. /* Enable the ADC peripheral */
  1234. tmp_status = ADC_Enable(hadc);
  1235. /* Start conversion if ADC is effectively enabled */
  1236. if (tmp_status == HAL_OK)
  1237. {
  1238. /* State machine update: Check if an injected conversion is ongoing */
  1239. if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  1240. {
  1241. /* Reset ADC error code fields related to regular conversions only */
  1242. CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR|HAL_ADC_ERROR_DMA));
  1243. }
  1244. else
  1245. {
  1246. /* Set ADC error code to none */
  1247. ADC_CLEAR_ERRORCODE(hadc);
  1248. }
  1249. /* Clear HAL_ADC_STATE_READY and regular conversion results bits, set HAL_ADC_STATE_REG_BUSY bit */
  1250. 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);
  1251. /* Reset HAL_ADC_STATE_MULTIMODE_SLAVE bit
  1252. - by default if ADC is Master or Independent or if multimode feature is not available
  1253. - if multimode setting is set to independent mode (no dual regular or injected conversions are configured) */
  1254. if (ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc))
  1255. {
  1256. CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  1257. }
  1258. /* Set the DMA transfer complete callback */
  1259. hadc->DMA_Handle->XferCpltCallback = ADC_DMAConvCplt;
  1260. /* Set the DMA half transfer complete callback */
  1261. hadc->DMA_Handle->XferHalfCpltCallback = ADC_DMAHalfConvCplt;
  1262. /* Set the DMA error callback */
  1263. hadc->DMA_Handle->XferErrorCallback = ADC_DMAError;
  1264. /* Manage ADC and DMA start: ADC overrun interruption, DMA start, */
  1265. /* ADC start (in case of SW start): */
  1266. /* Clear regular group conversion flag and overrun flag */
  1267. /* (To ensure of no unknown state from potential previous ADC */
  1268. /* operations) */
  1269. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
  1270. /* With DMA, overrun event is always considered as an error even if
  1271. hadc->Init.Overrun is set to ADC_OVR_DATA_OVERWRITTEN. Therefore,
  1272. ADC_IT_OVR is enabled. */
  1273. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);
  1274. /* Enable ADC DMA mode */
  1275. SET_BIT(hadc->Instance->CFGR, ADC_CFGR_DMAEN);
  1276. /* Start the DMA channel */
  1277. HAL_DMA_Start_IT(hadc->DMA_Handle, (uint32_t)&hadc->Instance->DR, (uint32_t)pData, Length);
  1278. /* Enable conversion of regular group. */
  1279. /* Process unlocked */
  1280. __HAL_UNLOCK(hadc);
  1281. /* If software start has been selected, conversion starts immediately. */
  1282. /* If external trigger has been selected, conversion will start at next */
  1283. /* trigger event. */
  1284. SET_BIT(hadc->Instance->CR, ADC_CR_ADSTART);
  1285. }
  1286. else
  1287. {
  1288. /* Process unlocked */
  1289. __HAL_UNLOCK(hadc);
  1290. } /* if (tmp_status == HAL_OK) */
  1291. }
  1292. else
  1293. {
  1294. tmp_status = HAL_ERROR;
  1295. /* Process unlocked */
  1296. __HAL_UNLOCK(hadc);
  1297. } /* if (ADC_IS_DUAL_REGULAR_CONVERSION_ENABLE(hadc) == RESET) */
  1298. /* Return function status */
  1299. return tmp_status;
  1300. } /* if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc)) */
  1301. }
  1302. /**
  1303. * @brief Stop ADC conversion of regular groups and disable ADC DMA transfer.
  1304. * @note Stop as well injected conversions and disable ADC peripheral.
  1305. * @note Case of multimode enabled (when multimode feature is available):
  1306. * HAL_ADC_Stop_DMA() function is dedicated to single-ADC mode only.
  1307. * For multimode, the dedicated HAL_ADCEx_MultiModeStop_DMA() API must be used.
  1308. * @param hadc: ADC handle
  1309. * @retval HAL status.
  1310. */
  1311. HAL_StatusTypeDef HAL_ADC_Stop_DMA(ADC_HandleTypeDef* hadc)
  1312. {
  1313. HAL_StatusTypeDef tmp_status = HAL_OK;
  1314. /* Check the parameters */
  1315. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1316. /* Process locked */
  1317. __HAL_LOCK(hadc);
  1318. /* 1. Stop potential regular conversion on going */
  1319. tmp_status = ADC_ConversionStop(hadc, ADC_REGULAR_INJECTED_GROUP);
  1320. /* Disable ADC peripheral if conversions are effectively stopped */
  1321. if (tmp_status == HAL_OK)
  1322. {
  1323. /* Disable ADC DMA (ADC DMA configuration ADC_CFGR_DMACFG is kept) */
  1324. CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_DMAEN);
  1325. /* Disable the DMA channel (in case of DMA in circular mode or stop while */
  1326. /* while DMA transfer is on going) */
  1327. tmp_status = HAL_DMA_Abort(hadc->DMA_Handle);
  1328. /* Check if DMA channel effectively disabled */
  1329. if (tmp_status != HAL_OK)
  1330. {
  1331. /* Update ADC state machine to error */
  1332. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  1333. }
  1334. /* Disable ADC overrun interrupt */
  1335. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
  1336. /* 2. Disable the ADC peripheral */
  1337. /* Update "tmp_status" only if DMA channel disabling passed, to keep in */
  1338. /* memory a potential failing status. */
  1339. if (tmp_status == HAL_OK)
  1340. {
  1341. tmp_status = ADC_Disable(hadc);
  1342. }
  1343. else
  1344. {
  1345. ADC_Disable(hadc);
  1346. }
  1347. /* Check if ADC is effectively disabled */
  1348. if (tmp_status == HAL_OK)
  1349. {
  1350. /* Change ADC state */
  1351. /* Clear HAL_ADC_STATE_REG_BUSY and HAL_ADC_STATE_INJ_BUSY bits, set HAL_ADC_STATE_READY bit */
  1352. ADC_STATE_CLR_SET(hadc->State, (HAL_ADC_STATE_REG_BUSY|HAL_ADC_STATE_INJ_BUSY), HAL_ADC_STATE_READY);
  1353. }
  1354. }
  1355. /* Process unlocked */
  1356. __HAL_UNLOCK(hadc);
  1357. /* Return function status */
  1358. return tmp_status;
  1359. }
  1360. /**
  1361. * @brief Get ADC regular group conversion result.
  1362. * @param hadc: ADC handle
  1363. * @note Reading DR register automatically clears EOC flag. To reset EOS flag,
  1364. * the user must resort to the macro
  1365. * __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOS)
  1366. * @retval Converted value
  1367. */
  1368. uint32_t HAL_ADC_GetValue(ADC_HandleTypeDef* hadc)
  1369. {
  1370. /* Check the parameters */
  1371. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1372. /* Return ADC converted value */
  1373. return hadc->Instance->DR;
  1374. }
  1375. /**
  1376. * @brief Handle ADC interrupt request.
  1377. * @param hadc: ADC handle
  1378. * @retval None
  1379. */
  1380. void HAL_ADC_IRQHandler(ADC_HandleTypeDef* hadc)
  1381. {
  1382. uint32_t overrun_error = 0; /* flag set if overrun occurrence has to be considered as an error */
  1383. ADC_TypeDef *tmpADC_Master;
  1384. uint32_t tmp_isr = hadc->Instance->ISR;
  1385. uint32_t tmp_ier = hadc->Instance->IER;
  1386. uint32_t tmp_cfgr = 0x0;
  1387. uint32_t tmp_cfgr_jqm = 0x0;
  1388. /* Check the parameters */
  1389. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1390. assert_param(IS_ADC_EOC_SELECTION(hadc->Init.EOCSelection));
  1391. /* ====== Check End of Sampling flag for regular group ===== */
  1392. if (((tmp_isr & ADC_FLAG_EOSMP) == ADC_FLAG_EOSMP) && ((tmp_ier & ADC_IT_EOSMP) == ADC_IT_EOSMP))
  1393. {
  1394. /* Update state machine on end of sampling status if not in error state */
  1395. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL))
  1396. {
  1397. /* Change ADC state */
  1398. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOSMP);
  1399. }
  1400. /* End Of Sampling callback */
  1401. HAL_ADCEx_EndOfSamplingCallback(hadc);
  1402. /* Clear regular group conversion flag */
  1403. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOSMP );
  1404. }
  1405. /* ====== Check End of Conversion or Sequence flags for regular group ===== */
  1406. if( (((tmp_isr & ADC_FLAG_EOC) == ADC_FLAG_EOC) && ((tmp_ier & ADC_IT_EOC) == ADC_IT_EOC)) ||
  1407. (((tmp_isr & ADC_FLAG_EOS) == ADC_FLAG_EOS) && ((tmp_ier & ADC_IT_EOS) == ADC_IT_EOS)) )
  1408. {
  1409. /* Update state machine on conversion status if not in error state */
  1410. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL))
  1411. {
  1412. /* Change ADC state */
  1413. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  1414. }
  1415. /* Disable interruption if no further conversion upcoming by regular */
  1416. /* external trigger or by continuous mode, */
  1417. /* and if scan sequence if completed. */
  1418. if(ADC_IS_SOFTWARE_START_REGULAR(hadc))
  1419. {
  1420. if (ADC_INDEPENDENT_OR_NONMULTIMODEREGULAR_SLAVE(hadc))
  1421. {
  1422. /* check CONT bit directly in handle ADC CFGR register */
  1423. tmp_cfgr = READ_REG(hadc->Instance->CFGR);
  1424. }
  1425. else
  1426. {
  1427. /* else need to check Master ADC CONT bit */
  1428. tmpADC_Master = ADC_MASTER_REGISTER(hadc);
  1429. tmp_cfgr = READ_REG(tmpADC_Master->CFGR);
  1430. }
  1431. /* Carry on if continuous mode is disabled */
  1432. if (READ_BIT (tmp_cfgr, ADC_CFGR_CONT) != ADC_CFGR_CONT)
  1433. {
  1434. /* If End of Sequence is reached, disable interrupts */
  1435. if( __HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOS) )
  1436. {
  1437. /* Allowed to modify bits ADC_IT_EOC/ADC_IT_EOS only if bit */
  1438. /* ADSTART==0 (no conversion on going) */
  1439. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  1440. {
  1441. /* Disable ADC end of sequence conversion interrupt */
  1442. /* Note: if Overrun interrupt was enabled with EOC or EOS interrupt */
  1443. /* in HAL_Start_IT(), it isn't disabled here because it can be used */
  1444. /* by overrun IRQ process below. */
  1445. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC | ADC_IT_EOS);
  1446. /* Clear HAL_ADC_STATE_REG_BUSY bit */
  1447. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  1448. /* If no injected conversion on-going, set HAL_ADC_STATE_READY bit */
  1449. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  1450. {
  1451. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  1452. }
  1453. }
  1454. else
  1455. {
  1456. /* Change ADC state to error state */
  1457. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  1458. /* Set ADC error code to ADC IP internal error */
  1459. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  1460. }
  1461. }
  1462. } /* if (READ_BIT (tmp_cfgr, ADC_CFGR_CONT) != ADC_CFGR_CONT) */
  1463. } /* if(ADC_IS_SOFTWARE_START_REGULAR(hadc) */
  1464. /* Conversion complete callback */
  1465. /* Note: HAL_ADC_ConvCpltCallback can resort to
  1466. if( __HAL_ADC_GET_FLAG(&hadc, ADC_FLAG_EOS)) or
  1467. if( __HAL_ADC_GET_FLAG(&hadc, ADC_FLAG_EOC)) to determine whether
  1468. interruption has been triggered by end of conversion or end of
  1469. sequence. */
  1470. HAL_ADC_ConvCpltCallback(hadc);
  1471. /* Clear regular group conversion flag */
  1472. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS) );
  1473. }
  1474. /* ========== Check End of Conversion flag for injected group ========== */
  1475. if( (((tmp_isr & ADC_FLAG_JEOC) == ADC_FLAG_JEOC) && ((tmp_ier & ADC_IT_JEOC) == ADC_IT_JEOC)) ||
  1476. (((tmp_isr & ADC_FLAG_JEOS) == ADC_FLAG_JEOS) && ((tmp_ier & ADC_IT_JEOS) == ADC_IT_JEOS)) )
  1477. {
  1478. /* Update state machine on conversion status if not in error state */
  1479. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL))
  1480. {
  1481. /* Change ADC state */
  1482. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_EOC);
  1483. }
  1484. /* Check whether interruptions can be disabled only if
  1485. - injected conversions are software-triggered when injected queue management is disabled
  1486. OR
  1487. - auto-injection is enabled, continuous mode is disabled (CONT = 0)
  1488. and regular conversions are software-triggered */
  1489. /* If End of Sequence is reached, disable interrupts */
  1490. if( __HAL_ADC_GET_FLAG(hadc, ADC_FLAG_JEOS))
  1491. {
  1492. /* First, retrieve proper registers to check */
  1493. /* 1a. Are injected conversions that of a dual Slave ? */
  1494. if (ADC_INDEPENDENT_OR_NONMULTIMODEINJECTED_SLAVE(hadc))
  1495. {
  1496. /* hadc is not the handle of a Slave ADC with dual injected conversions enabled:
  1497. check JQM bit directly in ADC CFGR register */
  1498. tmp_cfgr_jqm = READ_REG(hadc->Instance->CFGR);
  1499. }
  1500. else
  1501. {
  1502. /* hadc is the handle of a Slave ADC with dual injected conversions enabled:
  1503. need to check JQM bit of Master ADC CFGR register */
  1504. tmpADC_Master = ADC_MASTER_REGISTER(hadc);
  1505. tmp_cfgr_jqm = READ_REG(tmpADC_Master->CFGR);
  1506. }
  1507. /* 1b. Is hadc the handle of a Slave ADC with regular conversions enabled? */
  1508. if (ADC_INDEPENDENT_OR_NONMULTIMODEREGULAR_SLAVE(hadc))
  1509. {
  1510. /* hadc is not the handle of a Slave ADC with dual regular conversions enabled:
  1511. check JAUTO and CONT bits directly in ADC CFGR register */
  1512. tmp_cfgr = READ_REG(hadc->Instance->CFGR);
  1513. }
  1514. else
  1515. {
  1516. /* hadc is not the handle of a Slave ADC with dual regular conversions enabled:
  1517. check JAUTO and CONT bits of Master ADC CFGR register */
  1518. tmpADC_Master = ADC_MASTER_REGISTER(hadc);
  1519. tmp_cfgr = READ_REG(tmpADC_Master->CFGR);
  1520. }
  1521. /* Secondly, check whether JEOC and JEOS interruptions can be disabled */
  1522. if ((ADC_IS_SOFTWARE_START_INJECTED(hadc) && (READ_BIT(tmp_cfgr_jqm, ADC_CFGR_JQM) != ADC_CFGR_JQM))
  1523. && (!((READ_BIT(tmp_cfgr, (ADC_CFGR_JAUTO|ADC_CFGR_CONT)) == (ADC_CFGR_JAUTO|ADC_CFGR_CONT)) &&
  1524. (ADC_IS_SOFTWARE_START_REGULAR(hadc)))) )
  1525. {
  1526. /* Allowed to modify bits ADC_IT_JEOC/ADC_IT_JEOS only if bit */
  1527. /* JADSTART==0 (no conversion on going) */
  1528. if (ADC_IS_CONVERSION_ONGOING_INJECTED(hadc) == RESET)
  1529. {
  1530. /* Disable ADC end of sequence conversion interrupt */
  1531. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC | ADC_IT_JEOS);
  1532. /* Clear HAL_ADC_STATE_INJ_BUSY bit */
  1533. CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  1534. /* If no regular conversion on-going, set HAL_ADC_STATE_READY bit */
  1535. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_REG_BUSY))
  1536. {
  1537. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  1538. }
  1539. }
  1540. else
  1541. {
  1542. /* Change ADC state to error state */
  1543. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  1544. /* Set ADC error code to ADC IP internal error */
  1545. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  1546. }
  1547. }
  1548. } /* if( __HAL_ADC_GET_FLAG(hadc, ADC_FLAG_JEOS)) */
  1549. /* Injected Conversion complete callback */
  1550. /* Note: HAL_ADCEx_InjectedConvCpltCallback can resort to
  1551. if( __HAL_ADC_GET_FLAG(&hadc, ADC_FLAG_JEOS)) or
  1552. if( __HAL_ADC_GET_FLAG(&hadc, ADC_FLAG_JEOC)) to determine whether
  1553. interruption has been triggered by end of conversion or end of
  1554. sequence. */
  1555. HAL_ADCEx_InjectedConvCpltCallback(hadc);
  1556. /* Clear injected group conversion flag */
  1557. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOC | ADC_FLAG_JEOS);
  1558. }
  1559. /* ========== Check Analog watchdog flags =================================================== */
  1560. /* ========== Check Analog watchdog 1 flags ========== */
  1561. if (((tmp_isr & ADC_FLAG_AWD1) == ADC_FLAG_AWD1) && ((tmp_ier & ADC_IT_AWD1) == ADC_IT_AWD1))
  1562. {
  1563. /* Change ADC state */
  1564. SET_BIT(hadc->State, HAL_ADC_STATE_AWD1);
  1565. /* Level out of window 1 callback */
  1566. HAL_ADC_LevelOutOfWindowCallback(hadc);
  1567. /* Clear ADC Analog watchdog flag */
  1568. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD1);
  1569. }
  1570. /* ========== Check Analog watchdog 2 flags ========== */
  1571. if (((tmp_isr & ADC_FLAG_AWD2) == ADC_FLAG_AWD2) && ((tmp_ier & ADC_IT_AWD2) == ADC_IT_AWD2))
  1572. {
  1573. /* Change ADC state */
  1574. SET_BIT(hadc->State, HAL_ADC_STATE_AWD2);
  1575. /* Level out of window 2 callback */
  1576. HAL_ADCEx_LevelOutOfWindow2Callback(hadc);
  1577. /* Clear ADC Analog watchdog flag */
  1578. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD2);
  1579. }
  1580. /* ========== Check Analog watchdog 3 flags ========== */
  1581. if (((tmp_isr & ADC_FLAG_AWD3) == ADC_FLAG_AWD3) && ((tmp_ier & ADC_IT_AWD3) == ADC_IT_AWD3))
  1582. {
  1583. /* Change ADC state */
  1584. SET_BIT(hadc->State, HAL_ADC_STATE_AWD3);
  1585. /* Level out of window 3 callback */
  1586. HAL_ADCEx_LevelOutOfWindow3Callback(hadc);
  1587. /* Clear ADC Analog watchdog flag */
  1588. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD3);
  1589. }
  1590. /* ========== Check Overrun flag ========== */
  1591. if (((tmp_isr & ADC_FLAG_OVR) == ADC_FLAG_OVR) && ((tmp_ier & ADC_IT_OVR) == ADC_IT_OVR))
  1592. {
  1593. /* If overrun is set to overwrite previous data (default setting), */
  1594. /* overrun event is not considered as an error. */
  1595. /* (cf ref manual "Managing conversions without using the DMA and without */
  1596. /* overrun ") */
  1597. /* Exception for usage with DMA overrun event always considered as an */
  1598. /* error. */
  1599. if (hadc->Init.Overrun == ADC_OVR_DATA_PRESERVED)
  1600. {
  1601. overrun_error = 1;
  1602. }
  1603. else
  1604. {
  1605. /* check DMA configuration, depending on multimode set or not,
  1606. or whether or not multimode feature is available */
  1607. if (ADC_IS_DUAL_CONVERSION_ENABLE(hadc) == RESET)
  1608. {
  1609. /* Multimode not set or feature not available or ADC independent */
  1610. if (HAL_IS_BIT_SET(hadc->Instance->CFGR, ADC_CFGR_DMAEN))
  1611. {
  1612. overrun_error = 1;
  1613. }
  1614. }
  1615. else
  1616. {
  1617. /* Multimode (when feature is available) is enabled,
  1618. Common Control Register MDMA bits must be checked. */
  1619. if (ADC_MULTIMODE_DMA_ENABLED())
  1620. {
  1621. overrun_error = 1;
  1622. }
  1623. }
  1624. }
  1625. if (overrun_error == 1)
  1626. {
  1627. /* Change ADC state to error state */
  1628. SET_BIT(hadc->State, HAL_ADC_STATE_REG_OVR);
  1629. /* Set ADC error code to overrun */
  1630. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_OVR);
  1631. /* Error callback */
  1632. HAL_ADC_ErrorCallback(hadc);
  1633. }
  1634. /* Clear the Overrun flag, to be done AFTER HAL_ADC_ErrorCallback() since
  1635. old data is preserved until OVR is reset */
  1636. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  1637. }
  1638. /* ========== Check Injected context queue overflow flag ========== */
  1639. if (((tmp_isr & ADC_FLAG_JQOVF) == ADC_FLAG_JQOVF) && ((tmp_ier & ADC_IT_JQOVF) == ADC_IT_JQOVF))
  1640. {
  1641. /* Change ADC state to overrun state */
  1642. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_JQOVF);
  1643. /* Set ADC error code to Injected context queue overflow */
  1644. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_JQOVF);
  1645. /* Clear the Injected context queue overflow flag */
  1646. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JQOVF);
  1647. /* Error callback */
  1648. HAL_ADCEx_InjectedQueueOverflowCallback(hadc);
  1649. }
  1650. }
  1651. /**
  1652. * @brief Conversion complete callback in non-blocking mode.
  1653. * @param hadc: ADC handle
  1654. * @retval None
  1655. */
  1656. __weak void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
  1657. {
  1658. /* Prevent unused argument(s) compilation warning */
  1659. UNUSED(hadc);
  1660. /* NOTE : This function should not be modified. When the callback is needed,
  1661. function HAL_ADC_ConvCpltCallback must be implemented in the user file.
  1662. */
  1663. }
  1664. /**
  1665. * @brief Conversion DMA half-transfer callback in non-blocking mode.
  1666. * @param hadc: ADC handle
  1667. * @retval None
  1668. */
  1669. __weak void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc)
  1670. {
  1671. /* Prevent unused argument(s) compilation warning */
  1672. UNUSED(hadc);
  1673. /* NOTE : This function should not be modified. When the callback is needed,
  1674. function HAL_ADC_ConvHalfCpltCallback must be implemented in the user file.
  1675. */
  1676. }
  1677. /**
  1678. * @brief Analog watchdog 1 callback in non-blocking mode.
  1679. * @param hadc: ADC handle
  1680. * @retval None
  1681. */
  1682. __weak void HAL_ADC_LevelOutOfWindowCallback(ADC_HandleTypeDef* hadc)
  1683. {
  1684. /* Prevent unused argument(s) compilation warning */
  1685. UNUSED(hadc);
  1686. /* NOTE : This function should not be modified. When the callback is needed,
  1687. function HAL_ADC_LevelOutOfWindowCallback must be implemented in the user file.
  1688. */
  1689. }
  1690. /**
  1691. * @brief ADC error callback in non-blocking mode
  1692. * (ADC conversion with interruption or transfer by DMA).
  1693. * @param hadc: ADC handle
  1694. * @retval None
  1695. */
  1696. __weak void HAL_ADC_ErrorCallback(ADC_HandleTypeDef *hadc)
  1697. {
  1698. /* Prevent unused argument(s) compilation warning */
  1699. UNUSED(hadc);
  1700. /* NOTE : This function should not be modified. When the callback is needed,
  1701. function HAL_ADC_ErrorCallback must be implemented in the user file.
  1702. */
  1703. }
  1704. /**
  1705. * @}
  1706. */
  1707. /** @defgroup ADC_Exported_Functions_Group3 Peripheral Control functions
  1708. * @brief Peripheral Control functions
  1709. *
  1710. @verbatim
  1711. ===============================================================================
  1712. ##### Peripheral Control functions #####
  1713. ===============================================================================
  1714. [..] This section provides functions allowing to:
  1715. (+) Configure channels on regular group
  1716. (+) Configure the analog watchdog
  1717. @endverbatim
  1718. * @{
  1719. */
  1720. /**
  1721. * @brief Configure the selected channel to be linked to the regular group.
  1722. * @note In case of usage of internal measurement channels (Vbat / VrefInt /
  1723. * TempSensor), the recommended sampling time is provided by the
  1724. * datasheet.
  1725. * These internal paths can be disabled using function
  1726. * HAL_ADC_DeInit().
  1727. * @note Possibility to update parameters on the fly:
  1728. * HAL_ADC_ConfigChannel() initializes channel into regular group,
  1729. * consecutive calls to this function can be used to reconfigure some
  1730. * parameters of structure "ADC_ChannelConfTypeDef" on the fly, without
  1731. * resetting the ADC.
  1732. * The setting of these parameters is conditioned to ADC state.
  1733. * For parameters constraints, see comments of structure
  1734. * "ADC_ChannelConfTypeDef".
  1735. * @param hadc: ADC handle
  1736. * @param sConfig: Structure ADC channel for regular group.
  1737. * @retval HAL status
  1738. */
  1739. HAL_StatusTypeDef HAL_ADC_ConfigChannel(ADC_HandleTypeDef* hadc, ADC_ChannelConfTypeDef* sConfig)
  1740. {
  1741. HAL_StatusTypeDef tmp_status = HAL_OK;
  1742. ADC_Common_TypeDef *tmpADC_Common;
  1743. uint32_t tmpOffsetShifted;
  1744. __IO uint32_t wait_loop_index = 0;
  1745. /* Check the parameters */
  1746. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1747. assert_param(IS_ADC_REGULAR_RANK(sConfig->Rank));
  1748. assert_param(IS_ADC_SAMPLE_TIME(sConfig->SamplingTime));
  1749. assert_param(IS_ADC_SINGLE_DIFFERENTIAL(sConfig->SingleDiff));
  1750. assert_param(IS_ADC_OFFSET_NUMBER(sConfig->OffsetNumber));
  1751. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), sConfig->Offset));
  1752. /* if ROVSE is set, the value of the OFFSETy_EN bit in ADCx_OFRy register is
  1753. ignored (considered as reset) */
  1754. assert_param(!((sConfig->OffsetNumber != ADC_OFFSET_NONE) && (hadc->Init.OversamplingMode == ENABLE)));
  1755. /* Verification of channel number */
  1756. if (sConfig->SingleDiff != ADC_DIFFERENTIAL_ENDED)
  1757. {
  1758. assert_param(IS_ADC_CHANNEL(hadc, sConfig->Channel));
  1759. }
  1760. else
  1761. {
  1762. assert_param(IS_ADC_DIFF_CHANNEL(hadc, sConfig->Channel));
  1763. }
  1764. /* Process locked */
  1765. __HAL_LOCK(hadc);
  1766. /* Parameters update conditioned to ADC state: */
  1767. /* Parameters that can be updated when ADC is disabled or enabled without */
  1768. /* conversion on going on regular group: */
  1769. /* - Channel number */
  1770. /* - Channel rank */
  1771. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  1772. {
  1773. /* Regular sequence configuration */
  1774. /* Clear the old SQx bits then set the new ones for the selected rank */
  1775. /* For Rank 1 to 4 */
  1776. if (sConfig->Rank < 5)
  1777. {
  1778. MODIFY_REG(hadc->Instance->SQR1,
  1779. ADC_SQR1_RK(ADC_SQR2_SQ5, sConfig->Rank),
  1780. ADC_SQR1_RK(sConfig->Channel, sConfig->Rank));
  1781. }
  1782. /* For Rank 5 to 9 */
  1783. else if (sConfig->Rank < 10)
  1784. {
  1785. MODIFY_REG(hadc->Instance->SQR2,
  1786. ADC_SQR2_RK(ADC_SQR2_SQ5, sConfig->Rank),
  1787. ADC_SQR2_RK(sConfig->Channel, sConfig->Rank));
  1788. }
  1789. /* For Rank 10 to 14 */
  1790. else if (sConfig->Rank < 15)
  1791. {
  1792. MODIFY_REG(hadc->Instance->SQR3,
  1793. ADC_SQR3_RK(ADC_SQR3_SQ10, sConfig->Rank),
  1794. ADC_SQR3_RK(sConfig->Channel, sConfig->Rank));
  1795. }
  1796. /* For Rank 15 to 16 */
  1797. else
  1798. {
  1799. MODIFY_REG(hadc->Instance->SQR4,
  1800. ADC_SQR4_RK(ADC_SQR4_SQ15, sConfig->Rank),
  1801. ADC_SQR4_RK(sConfig->Channel, sConfig->Rank));
  1802. }
  1803. /* Parameters update conditioned to ADC state: */
  1804. /* Parameters that can be updated when ADC is disabled or enabled without */
  1805. /* conversion on going on regular group: */
  1806. /* - Channel sampling time */
  1807. /* - Channel offset */
  1808. if (ADC_IS_CONVERSION_ONGOING_REGULAR_INJECTED(hadc) == RESET)
  1809. {
  1810. /* Channel sampling time configuration */
  1811. /* Clear the old sample time then set the new one for the selected channel */
  1812. /* For channels 10 to 18 */
  1813. if (sConfig->Channel >= ADC_CHANNEL_10)
  1814. {
  1815. ADC_SMPR2_SETTING(hadc, sConfig->SamplingTime, sConfig->Channel);
  1816. }
  1817. else /* For channels 0 to 9 */
  1818. {
  1819. ADC_SMPR1_SETTING(hadc, sConfig->SamplingTime, sConfig->Channel);
  1820. }
  1821. /* Configure the offset: offset enable/disable, channel, offset value */
  1822. /* Shift the offset with respect to the selected ADC resolution. */
  1823. /* Offset has to be left-aligned on bit 11, the LSB (right bits) are set to 0 */
  1824. tmpOffsetShifted = ADC_OFFSET_SHIFT_RESOLUTION(hadc, sConfig->Offset);
  1825. switch (sConfig->OffsetNumber)
  1826. {
  1827. /* Configure offset register i when applicable: */
  1828. /* - Enable offset */
  1829. /* - Set channel number */
  1830. /* - Set offset value */
  1831. case ADC_OFFSET_1:
  1832. MODIFY_REG(hadc->Instance->OFR1,
  1833. ADC_OFR_FIELDS,
  1834. ADC_OFR1_OFFSET1_EN | ADC_OFR_CHANNEL(sConfig->Channel) | tmpOffsetShifted);
  1835. break;
  1836. case ADC_OFFSET_2:
  1837. MODIFY_REG(hadc->Instance->OFR2,
  1838. ADC_OFR_FIELDS,
  1839. ADC_OFR2_OFFSET2_EN | ADC_OFR_CHANNEL(sConfig->Channel) | tmpOffsetShifted);
  1840. break;
  1841. case ADC_OFFSET_3:
  1842. MODIFY_REG(hadc->Instance->OFR3,
  1843. ADC_OFR_FIELDS,
  1844. ADC_OFR3_OFFSET3_EN | ADC_OFR_CHANNEL(sConfig->Channel) | tmpOffsetShifted);
  1845. break;
  1846. case ADC_OFFSET_4:
  1847. MODIFY_REG(hadc->Instance->OFR4,
  1848. ADC_OFR_FIELDS,
  1849. ADC_OFR4_OFFSET4_EN | ADC_OFR_CHANNEL(sConfig->Channel) | tmpOffsetShifted);
  1850. break;
  1851. /* Case ADC_OFFSET_NONE */
  1852. default :
  1853. /* Scan OFR1, OFR2, OFR3, OFR4 to check if the selected channel is enabled.
  1854. If this is the case, offset OFRx is disabled since
  1855. sConfig->OffsetNumber = ADC_OFFSET_NONE. */
  1856. if (((hadc->Instance->OFR1) & ADC_OFR1_OFFSET1_CH) == ADC_OFR_CHANNEL(sConfig->Channel))
  1857. {
  1858. CLEAR_BIT(hadc->Instance->OFR1, ADC_OFR1_OFFSET1_EN);
  1859. }
  1860. if (((hadc->Instance->OFR2) & ADC_OFR2_OFFSET2_CH) == ADC_OFR_CHANNEL(sConfig->Channel))
  1861. {
  1862. CLEAR_BIT(hadc->Instance->OFR2, ADC_OFR2_OFFSET2_EN);
  1863. }
  1864. if (((hadc->Instance->OFR3) & ADC_OFR3_OFFSET3_CH) == ADC_OFR_CHANNEL(sConfig->Channel))
  1865. {
  1866. CLEAR_BIT(hadc->Instance->OFR3, ADC_OFR3_OFFSET3_EN);
  1867. }
  1868. if (((hadc->Instance->OFR4) & ADC_OFR4_OFFSET4_CH) == ADC_OFR_CHANNEL(sConfig->Channel))
  1869. {
  1870. CLEAR_BIT(hadc->Instance->OFR4, ADC_OFR4_OFFSET4_EN);
  1871. }
  1872. break;
  1873. } /* switch (sConfig->OffsetNumber) */
  1874. } /* if (ADC_IS_CONVERSION_ONGOING_REGULAR_INJECTED(hadc) == RESET) */
  1875. /* Parameters update conditioned to ADC state: */
  1876. /* Parameters that can be updated only when ADC is disabled: */
  1877. /* - Single or differential mode */
  1878. /* - Internal measurement channels: Vbat/VrefInt/TempSensor */
  1879. if (ADC_IS_ENABLE(hadc) == RESET)
  1880. {
  1881. /* Configuration of differential mode */
  1882. if (sConfig->SingleDiff != ADC_DIFFERENTIAL_ENDED)
  1883. {
  1884. /* Disable differential mode (default mode: single-ended) */
  1885. CLEAR_BIT(hadc->Instance->DIFSEL, ADC_DIFSEL_CHANNEL(sConfig->Channel));
  1886. }
  1887. else
  1888. {
  1889. /* Enable differential mode */
  1890. SET_BIT(hadc->Instance->DIFSEL, ADC_DIFSEL_CHANNEL(sConfig->Channel));
  1891. /* Sampling time configuration of channel ADC_IN+1 (negative input) */
  1892. /* Clear the old sample time then set the new one for the selected */
  1893. /* channel. */
  1894. /* Starting from channel 9, SMPR2 register must be configured */
  1895. if (sConfig->Channel >= ADC_CHANNEL_9)
  1896. {
  1897. ADC_SMPR2_SETTING(hadc, sConfig->SamplingTime, sConfig->Channel+1);
  1898. }
  1899. else /* For channels 0 to 8, SMPR1 must be configured */
  1900. {
  1901. ADC_SMPR1_SETTING(hadc, sConfig->SamplingTime, sConfig->Channel+1);
  1902. }
  1903. }
  1904. /* Management of internal measurement channels: Vbat/VrefInt/TempSensor. */
  1905. /* If internal channel selected, enable dedicated internal buffers and */
  1906. /* paths. */
  1907. /* Note: these internal measurement paths can be disabled using */
  1908. /* HAL_ADC_DeInit(). */
  1909. /* Configuration of common ADC parameters */
  1910. tmpADC_Common = ADC_COMMON_REGISTER(hadc);
  1911. /* If the requested internal measurement path has already been enabled, */
  1912. /* bypass the configuration processing. */
  1913. if (( (sConfig->Channel == ADC_CHANNEL_TEMPSENSOR) &&
  1914. (HAL_IS_BIT_CLR(tmpADC_Common->CCR, ADC_CCR_TSEN)) ) ||
  1915. ( (sConfig->Channel == ADC_CHANNEL_VBAT) &&
  1916. (HAL_IS_BIT_CLR(tmpADC_Common->CCR, ADC_CCR_VBATEN)) ) ||
  1917. ( (sConfig->Channel == ADC_CHANNEL_VREFINT) &&
  1918. (HAL_IS_BIT_CLR(tmpADC_Common->CCR, ADC_CCR_VREFEN)))
  1919. )
  1920. {
  1921. /* Configuration of common ADC parameters (continuation) */
  1922. /* Software is allowed to change common parameters only when all ADCs */
  1923. /* of the common group are disabled. */
  1924. if ((ADC_IS_ENABLE(hadc) == RESET) &&
  1925. (ADC_ANY_OTHER_ENABLED(hadc) == RESET) )
  1926. {
  1927. if (sConfig->Channel == ADC_CHANNEL_TEMPSENSOR)
  1928. {
  1929. if (ADC_TEMPERATURE_SENSOR_INSTANCE(hadc))
  1930. {
  1931. SET_BIT(tmpADC_Common->CCR, ADC_CCR_TSEN);
  1932. /* Delay for temperature sensor stabilization time */
  1933. /* Compute number of CPU cycles to wait for */
  1934. wait_loop_index = (ADC_TEMPSENSOR_DELAY_US * (SystemCoreClock / 1000000));
  1935. while(wait_loop_index != 0)
  1936. {
  1937. wait_loop_index--;
  1938. }
  1939. }
  1940. }
  1941. else if (sConfig->Channel == ADC_CHANNEL_VBAT)
  1942. {
  1943. if (ADC_BATTERY_VOLTAGE_INSTANCE(hadc))
  1944. {
  1945. SET_BIT(tmpADC_Common->CCR, ADC_CCR_VBATEN);
  1946. }
  1947. }
  1948. else if (sConfig->Channel == ADC_CHANNEL_VREFINT)
  1949. {
  1950. if (ADC_VREFINT_INSTANCE(hadc))
  1951. {
  1952. SET_BIT(tmpADC_Common->CCR, ADC_CCR_VREFEN);
  1953. }
  1954. }
  1955. }
  1956. /* If the requested internal measurement path has already been */
  1957. /* enabled and other ADC of the common group are enabled, internal */
  1958. /* measurement paths cannot be enabled. */
  1959. else
  1960. {
  1961. /* Update ADC state machine to error */
  1962. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1963. tmp_status = HAL_ERROR;
  1964. }
  1965. }
  1966. } /* if (ADC_IS_ENABLE(hadc) == RESET) */
  1967. } /* if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET) */
  1968. /* If a conversion is on going on regular group, no update on regular */
  1969. /* channel could be done on neither of the channel configuration structure */
  1970. /* parameters. */
  1971. else
  1972. {
  1973. /* Update ADC state machine to error */
  1974. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1975. tmp_status = HAL_ERROR;
  1976. }
  1977. /* Process unlocked */
  1978. __HAL_UNLOCK(hadc);
  1979. /* Return function status */
  1980. return tmp_status;
  1981. }
  1982. /**
  1983. * @brief Configure the analog watchdog.
  1984. * @note Possibility to update parameters on the fly:
  1985. * This function initializes the selected analog watchdog, successive
  1986. * calls to this function can be used to reconfigure some parameters
  1987. * of structure "ADC_AnalogWDGConfTypeDef" on the fly, without resetting
  1988. * the ADC, e.g. to set several channels to monitor simultaneously.
  1989. * The setting of these parameters is conditioned to ADC state.
  1990. * For parameters constraints, see comments of structure
  1991. * "ADC_AnalogWDGConfTypeDef".
  1992. * @param hadc: ADC handle
  1993. * @param AnalogWDGConfig: Structure of ADC analog watchdog configuration
  1994. * @retval HAL status
  1995. */
  1996. HAL_StatusTypeDef HAL_ADC_AnalogWDGConfig(ADC_HandleTypeDef* hadc, ADC_AnalogWDGConfTypeDef* AnalogWDGConfig)
  1997. {
  1998. HAL_StatusTypeDef tmp_status = HAL_OK;
  1999. uint32_t tmpAWDHighThresholdShifted;
  2000. uint32_t tmpAWDLowThresholdShifted;
  2001. uint32_t tmpADCFlagAWD2orAWD3;
  2002. uint32_t tmpADCITAWD2orAWD3;
  2003. /* Check the parameters */
  2004. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2005. assert_param(IS_ADC_ANALOG_WATCHDOG_NUMBER(AnalogWDGConfig->WatchdogNumber));
  2006. assert_param(IS_ADC_ANALOG_WATCHDOG_MODE(AnalogWDGConfig->WatchdogMode));
  2007. assert_param(IS_FUNCTIONAL_STATE(AnalogWDGConfig->ITMode));
  2008. if((AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_REG) ||
  2009. (AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_INJEC) ||
  2010. (AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_REGINJEC) )
  2011. {
  2012. assert_param(IS_ADC_CHANNEL(hadc, AnalogWDGConfig->Channel));
  2013. }
  2014. /* Verify if threshold is within the selected ADC resolution */
  2015. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), AnalogWDGConfig->HighThreshold));
  2016. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), AnalogWDGConfig->LowThreshold));
  2017. /* Process locked */
  2018. __HAL_LOCK(hadc);
  2019. /* Parameters update conditioned to ADC state: */
  2020. /* Parameters that can be updated when ADC is disabled or enabled without */
  2021. /* conversion on going on regular and injected groups: */
  2022. /* - Analog watchdog channels */
  2023. /* - Analog watchdog thresholds */
  2024. if (ADC_IS_CONVERSION_ONGOING_REGULAR_INJECTED(hadc) == RESET)
  2025. {
  2026. /* Analog watchdogs configuration */
  2027. if(AnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_1)
  2028. {
  2029. /* Configuration of analog watchdog: */
  2030. /* - Set the analog watchdog enable mode: regular and/or injected */
  2031. /* groups, one or overall group of channels. */
  2032. /* - Set the Analog watchdog channel (is not used if watchdog */
  2033. /* mode "all channels": ADC_CFGR_AWD1SGL=0). */
  2034. MODIFY_REG(hadc->Instance->CFGR, ADC_CFGR_WD_FIELDS,
  2035. AnalogWDGConfig->WatchdogMode | ADC_CFGR_SET_AWD1CH(AnalogWDGConfig->Channel) );
  2036. /* Shift the offset with respect to the selected ADC resolution: */
  2037. /* Thresholds have to be left-aligned on bit 11, the LSB (right bits) */
  2038. /* are set to 0 */
  2039. tmpAWDHighThresholdShifted = ADC_AWD1THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->HighThreshold);
  2040. tmpAWDLowThresholdShifted = ADC_AWD1THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->LowThreshold);
  2041. /* Set the high and low thresholds */
  2042. MODIFY_REG(hadc->Instance->TR1, ADC_TR1_HT1 | ADC_TR1_LT1,
  2043. ADC_TRX_HIGHTHRESHOLD (tmpAWDHighThresholdShifted) | tmpAWDLowThresholdShifted );
  2044. /* Clear the ADC Analog watchdog flag (in case left enabled by */
  2045. /* previous ADC operations) to be ready to use for HAL_ADC_IRQHandler() */
  2046. /* or HAL_ADC_PollForEvent(). */
  2047. __HAL_ADC_CLEAR_FLAG(hadc, ADC_IT_AWD1);
  2048. /* Configure ADC Analog watchdog interrupt */
  2049. if(AnalogWDGConfig->ITMode == ENABLE)
  2050. {
  2051. /* Enable the ADC Analog watchdog interrupt */
  2052. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_AWD1);
  2053. }
  2054. else
  2055. {
  2056. /* Disable the ADC Analog watchdog interrupt */
  2057. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_AWD1);
  2058. }
  2059. /* Update state, clear previous result related to AWD1 */
  2060. CLEAR_BIT(hadc->State, HAL_ADC_STATE_AWD1);
  2061. }
  2062. /* Case of ADC_ANALOGWATCHDOG_2 and ADC_ANALOGWATCHDOG_3 */
  2063. else
  2064. {
  2065. /* Shift the threshold with respect to the selected ADC resolution */
  2066. /* have to be left-aligned on bit 7, the LSB (right bits) are set to 0 */
  2067. tmpAWDHighThresholdShifted = ADC_AWD23THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->HighThreshold);
  2068. tmpAWDLowThresholdShifted = ADC_AWD23THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->LowThreshold);
  2069. if (AnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_2)
  2070. {
  2071. /* Set the Analog watchdog channel or group of channels. This also */
  2072. /* enables the watchdog. */
  2073. /* Note: Conditional register reset, because several channels can be */
  2074. /* set by successive calls of this function. */
  2075. if (AnalogWDGConfig->WatchdogMode != ADC_ANALOGWATCHDOG_NONE)
  2076. {
  2077. SET_BIT(hadc->Instance->AWD2CR, ADC_CFGR_SET_AWD23CR(AnalogWDGConfig->Channel));
  2078. }
  2079. else
  2080. {
  2081. CLEAR_BIT(hadc->Instance->AWD2CR, ADC_AWD2CR_AWD2CH);
  2082. }
  2083. /* Set the high and low thresholds */
  2084. MODIFY_REG(hadc->Instance->TR2, ADC_TR2_HT2 | ADC_TR2_LT2,
  2085. ADC_TRX_HIGHTHRESHOLD (tmpAWDHighThresholdShifted) | tmpAWDLowThresholdShifted );
  2086. /* Set temporary variable to flag and IT of AWD2 or AWD3 for further */
  2087. /* settings. */
  2088. tmpADCFlagAWD2orAWD3 = ADC_FLAG_AWD2;
  2089. tmpADCITAWD2orAWD3 = ADC_IT_AWD2;
  2090. /* Update state, clear previous result related to AWD2 */
  2091. CLEAR_BIT(hadc->State, HAL_ADC_STATE_AWD2);
  2092. }
  2093. /* (AnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_3) */
  2094. else
  2095. {
  2096. /* Set the Analog watchdog channel or group of channels. This also */
  2097. /* enables the watchdog. */
  2098. /* Note: Conditional register reset, because several channels can be */
  2099. /* set by successive calls of this function. */
  2100. if (AnalogWDGConfig->WatchdogMode != ADC_ANALOGWATCHDOG_NONE)
  2101. {
  2102. SET_BIT(hadc->Instance->AWD3CR, ADC_CFGR_SET_AWD23CR(AnalogWDGConfig->Channel));
  2103. }
  2104. else
  2105. {
  2106. CLEAR_BIT(hadc->Instance->AWD3CR, ADC_AWD3CR_AWD3CH);
  2107. }
  2108. /* Set the high and low thresholds */
  2109. MODIFY_REG(hadc->Instance->TR3, ADC_TR3_HT3 | ADC_TR3_LT3,
  2110. ADC_TRX_HIGHTHRESHOLD (tmpAWDHighThresholdShifted) | tmpAWDLowThresholdShifted );
  2111. /* Set temporary variable to flag and IT of AWD2 or AWD3 for further */
  2112. /* settings. */
  2113. tmpADCFlagAWD2orAWD3 = ADC_FLAG_AWD3;
  2114. tmpADCITAWD2orAWD3 = ADC_IT_AWD3;
  2115. /* Update state, clear previous result related to AWD3 */
  2116. CLEAR_BIT(hadc->State, HAL_ADC_STATE_AWD3);
  2117. }
  2118. /* Clear the ADC Analog watchdog flag (in case left enabled by */
  2119. /* previous ADC operations) to be ready to use for HAL_ADC_IRQHandler() */
  2120. /* or HAL_ADC_PollForEvent(). */
  2121. __HAL_ADC_CLEAR_FLAG(hadc, tmpADCFlagAWD2orAWD3);
  2122. /* Configure ADC Analog watchdog interrupt */
  2123. if(AnalogWDGConfig->ITMode == ENABLE)
  2124. {
  2125. __HAL_ADC_ENABLE_IT(hadc, tmpADCITAWD2orAWD3);
  2126. }
  2127. else
  2128. {
  2129. __HAL_ADC_DISABLE_IT(hadc, tmpADCITAWD2orAWD3);
  2130. }
  2131. }
  2132. }
  2133. /* If a conversion is on going on regular or injected groups, no update */
  2134. /* could be done on neither of the AWD configuration structure parameters. */
  2135. else
  2136. {
  2137. /* Update ADC state machine to error */
  2138. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  2139. tmp_status = HAL_ERROR;
  2140. }
  2141. /* Process unlocked */
  2142. __HAL_UNLOCK(hadc);
  2143. /* Return function status */
  2144. return tmp_status;
  2145. }
  2146. /**
  2147. * @}
  2148. */
  2149. /** @defgroup ADC_Exported_Functions_Group4 Peripheral State functions
  2150. * @brief ADC Peripheral State functions
  2151. *
  2152. @verbatim
  2153. ===============================================================================
  2154. ##### Peripheral state and errors functions #####
  2155. ===============================================================================
  2156. [..]
  2157. This subsection provides functions to get in run-time the status of the
  2158. peripheral.
  2159. (+) Check the ADC state
  2160. (+) Check the ADC error code
  2161. @endverbatim
  2162. * @{
  2163. */
  2164. /**
  2165. * @brief Return the ADC handle state.
  2166. * @param hadc: ADC handle
  2167. * @retval HAL state (uint32_t bit-map)
  2168. */
  2169. uint32_t HAL_ADC_GetState(ADC_HandleTypeDef* hadc)
  2170. {
  2171. /* Check the parameters */
  2172. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2173. /* Return ADC handle state */
  2174. return hadc->State;
  2175. }
  2176. /**
  2177. * @brief Return the ADC error code.
  2178. * @param hadc: ADC handle
  2179. * @retval ADC Error Code (uint32_t bit-map)
  2180. */
  2181. uint32_t HAL_ADC_GetError(ADC_HandleTypeDef *hadc)
  2182. {
  2183. /* Check the parameters */
  2184. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2185. return hadc->ErrorCode;
  2186. }
  2187. /**
  2188. * @}
  2189. */
  2190. /**
  2191. * @}
  2192. */
  2193. /** @defgroup ADC_Private_Functions ADC Private Functions
  2194. * @{
  2195. */
  2196. /**
  2197. * @brief Stop ADC conversion.
  2198. * @param hadc: ADC handle
  2199. * @param ConversionGroup: ADC group regular and/or injected.
  2200. * This parameter can be one of the following values:
  2201. * @arg @ref ADC_REGULAR_GROUP ADC regular conversion type.
  2202. * @arg @ref ADC_INJECTED_GROUP ADC injected conversion type.
  2203. * @arg @ref ADC_REGULAR_INJECTED_GROUP ADC regular and injected conversion type.
  2204. * @retval HAL status.
  2205. */
  2206. HAL_StatusTypeDef ADC_ConversionStop(ADC_HandleTypeDef* hadc, uint32_t ConversionGroup)
  2207. {
  2208. uint32_t tmp_ADC_CR_ADSTART_JADSTART = 0;
  2209. uint32_t tickstart = 0;
  2210. uint32_t Conversion_Timeout_CPU_cycles = 0;
  2211. /* Check the parameters */
  2212. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2213. assert_param(IS_ADC_CONVERSION_GROUP(ConversionGroup));
  2214. /* Verification if ADC is not already stopped (on regular and injected */
  2215. /* groups) to bypass this function if not needed. */
  2216. if (ADC_IS_CONVERSION_ONGOING_REGULAR_INJECTED(hadc))
  2217. {
  2218. /* Particular case of continuous auto-injection mode combined with */
  2219. /* auto-delay mode. */
  2220. /* In auto-injection mode, regular group stop ADC_CR_ADSTP is used (not */
  2221. /* injected group stop ADC_CR_JADSTP). */
  2222. /* Procedure to be followed: Wait until JEOS=1, clear JEOS, set ADSTP=1 */
  2223. /* (see reference manual). */
  2224. if ((HAL_IS_BIT_SET(hadc->Instance->CFGR, ADC_CFGR_JAUTO))
  2225. && (hadc->Init.ContinuousConvMode==ENABLE)
  2226. && (hadc->Init.LowPowerAutoWait==ENABLE))
  2227. {
  2228. /* Use stop of regular group */
  2229. ConversionGroup = ADC_REGULAR_GROUP;
  2230. /* Wait until JEOS=1 (maximum Timeout: 4 injected conversions) */
  2231. while(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_JEOS) == RESET)
  2232. {
  2233. if (Conversion_Timeout_CPU_cycles >= (ADC_CONVERSION_TIME_MAX_CPU_CYCLES *4))
  2234. {
  2235. /* Update ADC state machine to error */
  2236. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2237. /* Set ADC error code to ADC IP internal error */
  2238. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2239. return HAL_ERROR;
  2240. }
  2241. Conversion_Timeout_CPU_cycles ++;
  2242. }
  2243. /* Clear JEOS */
  2244. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOS);
  2245. }
  2246. /* Stop potential conversion on going on regular group */
  2247. if (ConversionGroup != ADC_INJECTED_GROUP)
  2248. {
  2249. /* Software is allowed to set ADSTP only when ADSTART=1 and ADDIS=0 */
  2250. if (HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADSTART) &&
  2251. HAL_IS_BIT_CLR(hadc->Instance->CR, ADC_CR_ADDIS) )
  2252. {
  2253. /* Stop conversions on regular group */
  2254. SET_BIT(hadc->Instance->CR, ADC_CR_ADSTP);
  2255. }
  2256. }
  2257. /* Stop potential conversion on going on injected group */
  2258. if (ConversionGroup != ADC_REGULAR_GROUP)
  2259. {
  2260. /* Software is allowed to set JADSTP only when JADSTART=1 and ADDIS=0 */
  2261. if (HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_JADSTART) &&
  2262. HAL_IS_BIT_CLR(hadc->Instance->CR, ADC_CR_ADDIS) )
  2263. {
  2264. /* Stop conversions on injected group */
  2265. SET_BIT(hadc->Instance->CR, ADC_CR_JADSTP);
  2266. }
  2267. }
  2268. /* Selection of start and stop bits with respect to the regular or injected group */
  2269. switch(ConversionGroup)
  2270. {
  2271. case ADC_REGULAR_INJECTED_GROUP:
  2272. tmp_ADC_CR_ADSTART_JADSTART = (ADC_CR_ADSTART | ADC_CR_JADSTART);
  2273. break;
  2274. case ADC_INJECTED_GROUP:
  2275. tmp_ADC_CR_ADSTART_JADSTART = ADC_CR_JADSTART;
  2276. break;
  2277. /* Case ADC_REGULAR_GROUP only*/
  2278. default:
  2279. tmp_ADC_CR_ADSTART_JADSTART = ADC_CR_ADSTART;
  2280. break;
  2281. }
  2282. /* Wait for conversion effectively stopped */
  2283. tickstart = HAL_GetTick();
  2284. while((hadc->Instance->CR & tmp_ADC_CR_ADSTART_JADSTART) != RESET)
  2285. {
  2286. if((HAL_GetTick()-tickstart) > ADC_STOP_CONVERSION_TIMEOUT)
  2287. {
  2288. /* Update ADC state machine to error */
  2289. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2290. /* Set ADC error code to ADC IP internal error */
  2291. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2292. return HAL_ERROR;
  2293. }
  2294. }
  2295. } /* if (ADC_IS_CONVERSION_ONGOING_REGULAR_INJECTED(hadc)) */
  2296. /* Return HAL status */
  2297. return HAL_OK;
  2298. }
  2299. /**
  2300. * @brief Enable the selected ADC.
  2301. * @note Prerequisite condition to use this function: ADC must be disabled
  2302. * and voltage regulator must be enabled (done into HAL_ADC_Init()).
  2303. * @param hadc: ADC handle
  2304. * @retval HAL status.
  2305. */
  2306. HAL_StatusTypeDef ADC_Enable(ADC_HandleTypeDef* hadc)
  2307. {
  2308. uint32_t tickstart = 0;
  2309. /* ADC enable and wait for ADC ready (in case of ADC is disabled or */
  2310. /* enabling phase not yet completed: flag ADC ready not set yet). */
  2311. /* Timeout implemented not to be stuck if ADC cannot be enabled (possible */
  2312. /* causes: ADC clock not running, ...). */
  2313. if (ADC_IS_ENABLE(hadc) == RESET)
  2314. {
  2315. /* Check if conditions to enable the ADC are fulfilled */
  2316. if (ADC_ENABLING_CONDITIONS(hadc) == RESET)
  2317. {
  2318. /* Update ADC state machine to error */
  2319. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2320. /* Set ADC error code to ADC IP internal error */
  2321. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2322. return HAL_ERROR;
  2323. }
  2324. /* Enable the ADC peripheral */
  2325. ADC_ENABLE(hadc);
  2326. /* Wait for ADC effectively enabled */
  2327. tickstart = HAL_GetTick();
  2328. while(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_RDY) == RESET)
  2329. {
  2330. /* If ADEN bit is set less than 4 ADC clock cycles after the ADCAL bit
  2331. has been cleared (after a calibration), ADEN bit is reset by the
  2332. calibration logic.
  2333. The workaround is to continue setting ADEN until ADRDY is becomes 1.
  2334. Additionally, ADC_ENABLE_TIMEOUT is defined to encompass this
  2335. 4 ADC clock cycle duration */
  2336. ADC_ENABLE(hadc);
  2337. if((HAL_GetTick()-tickstart) > ADC_ENABLE_TIMEOUT)
  2338. {
  2339. /* Update ADC state machine to error */
  2340. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2341. /* Set ADC error code to ADC IP internal error */
  2342. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2343. return HAL_ERROR;
  2344. }
  2345. }
  2346. }
  2347. /* Return HAL status */
  2348. return HAL_OK;
  2349. }
  2350. /**
  2351. * @brief Disable the selected ADC.
  2352. * @note Prerequisite condition to use this function: ADC conversions must be
  2353. * stopped.
  2354. * @param hadc: ADC handle
  2355. * @retval HAL status.
  2356. */
  2357. HAL_StatusTypeDef ADC_Disable(ADC_HandleTypeDef* hadc)
  2358. {
  2359. uint32_t tickstart = 0;
  2360. /* Verification if ADC is not already disabled: */
  2361. /* Note: forbidden to disable ADC (set bit ADC_CR_ADDIS) if ADC is already */
  2362. /* disabled. */
  2363. if (ADC_IS_ENABLE(hadc) != RESET )
  2364. {
  2365. /* Check if conditions to disable the ADC are fulfilled */
  2366. if (ADC_DISABLING_CONDITIONS(hadc) != RESET)
  2367. {
  2368. /* Disable the ADC peripheral */
  2369. ADC_DISABLE(hadc);
  2370. }
  2371. else
  2372. {
  2373. /* Update ADC state machine to error */
  2374. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2375. /* Set ADC error code to ADC IP internal error */
  2376. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2377. return HAL_ERROR;
  2378. }
  2379. /* Wait for ADC effectively disabled */
  2380. tickstart = HAL_GetTick();
  2381. while(HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADEN))
  2382. {
  2383. if((HAL_GetTick()-tickstart) > ADC_DISABLE_TIMEOUT)
  2384. {
  2385. /* Update ADC state machine to error */
  2386. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2387. /* Set ADC error code to ADC IP internal error */
  2388. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2389. return HAL_ERROR;
  2390. }
  2391. }
  2392. }
  2393. /* Return HAL status */
  2394. return HAL_OK;
  2395. }
  2396. /**
  2397. * @brief DMA transfer complete callback.
  2398. * @param hdma: pointer to DMA handle.
  2399. * @retval None
  2400. */
  2401. void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma)
  2402. {
  2403. /* Retrieve ADC handle corresponding to current DMA handle */
  2404. ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  2405. /* Update state machine on conversion status if not in error state */
  2406. if (HAL_IS_BIT_CLR(hadc->State, (HAL_ADC_STATE_ERROR_INTERNAL|HAL_ADC_STATE_ERROR_DMA)))
  2407. {
  2408. /* Update ADC state machine */
  2409. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  2410. /* Is it the end of the regular sequence ? */
  2411. if (HAL_IS_BIT_SET(hadc->Instance->ISR, ADC_FLAG_EOS))
  2412. {
  2413. /* Are conversions software-triggered ? */
  2414. if(ADC_IS_SOFTWARE_START_REGULAR(hadc))
  2415. {
  2416. /* Is CONT bit set ? */
  2417. if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_CONT) == RESET)
  2418. {
  2419. /* CONT bit is not set, no more conversions expected */
  2420. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  2421. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  2422. {
  2423. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  2424. }
  2425. }
  2426. }
  2427. }
  2428. else
  2429. {
  2430. /* DMA End of Transfer interrupt was triggered but conversions sequence
  2431. is not over. If DMACFG is set to 0, conversions are stopped. */
  2432. if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_DMACFG) == RESET)
  2433. {
  2434. /* DMACFG bit is not set, conversions are stopped. */
  2435. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  2436. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  2437. {
  2438. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  2439. }
  2440. }
  2441. }
  2442. /* Conversion complete callback */
  2443. HAL_ADC_ConvCpltCallback(hadc);
  2444. }
  2445. else /* DMA or internal error occurred (or both) */
  2446. {
  2447. /* In case of internal error, */
  2448. if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL))
  2449. {
  2450. /* call Error Callback function */
  2451. HAL_ADC_ErrorCallback(hadc);
  2452. }
  2453. }
  2454. }
  2455. /**
  2456. * @brief DMA half transfer complete callback.
  2457. * @param hdma: pointer to DMA handle.
  2458. * @retval None
  2459. */
  2460. void ADC_DMAHalfConvCplt(DMA_HandleTypeDef *hdma)
  2461. {
  2462. /* Retrieve ADC handle corresponding to current DMA handle */
  2463. ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  2464. /* Half conversion callback */
  2465. HAL_ADC_ConvHalfCpltCallback(hadc);
  2466. }
  2467. /**
  2468. * @brief DMA error callback.
  2469. * @param hdma: pointer to DMA handle.
  2470. * @retval None
  2471. */
  2472. void ADC_DMAError(DMA_HandleTypeDef *hdma)
  2473. {
  2474. /* Retrieve ADC handle corresponding to current DMA handle */
  2475. ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  2476. /* Change ADC state */
  2477. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  2478. /* Set ADC error code to DMA error */
  2479. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_DMA);
  2480. /* Error callback */
  2481. HAL_ADC_ErrorCallback(hadc);
  2482. }
  2483. /**
  2484. * @}
  2485. */
  2486. #endif /* HAL_ADC_MODULE_ENABLED */
  2487. /**
  2488. * @}
  2489. */
  2490. /**
  2491. * @}
  2492. */
  2493. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/