Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
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.
 
 
 
 
 
 

2438 lines
90 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_hal_adc.c
  4. * @author MCD Application Team
  5. * @brief This file provides firmware functions to manage the following
  6. * functionalities of the Analog to Digital Convertor (ADC)
  7. * peripheral:
  8. * + Initialization and de-initialization functions
  9. * ++ Initialization and Configuration of ADC
  10. * + Operation functions
  11. * ++ Start, stop, get result of conversions of regular
  12. * group, using 3 possible modes: polling, interruption or DMA.
  13. * + Control functions
  14. * ++ Channels configuration on regular group
  15. * ++ Channels configuration on injected group
  16. * ++ Analog Watchdog configuration
  17. * + State functions
  18. * ++ ADC state machine management
  19. * ++ Interrupts and flags management
  20. * Other functions (extended functions) are available in file
  21. * "stm32f1xx_hal_adc_ex.c".
  22. *
  23. @verbatim
  24. ==============================================================================
  25. ##### ADC peripheral features #####
  26. ==============================================================================
  27. [..]
  28. (+) 12-bit resolution
  29. (+) Interrupt generation at the end of regular conversion, end of injected
  30. conversion, and in case of analog watchdog or overrun events.
  31. (+) Single and continuous conversion modes.
  32. (+) Scan mode for conversion of several channels sequentially.
  33. (+) Data alignment with in-built data coherency.
  34. (+) Programmable sampling time (channel wise)
  35. (+) ADC conversion of regular group and injected group.
  36. (+) External trigger (timer or EXTI)
  37. for both regular and injected groups.
  38. (+) DMA request generation for transfer of conversions data of regular group.
  39. (+) Multimode Dual mode (available on devices with 2 ADCs or more).
  40. (+) Configurable DMA data storage in Multimode Dual mode (available on devices
  41. with 2 DCs or more).
  42. (+) Configurable delay between conversions in Dual interleaved mode (available
  43. on devices with 2 DCs or more).
  44. (+) ADC calibration
  45. (+) ADC supply requirements: 2.4 V to 3.6 V at full speed and down to 1.8 V at
  46. slower speed.
  47. (+) ADC input range: from Vref- (connected to Vssa) to Vref+ (connected to
  48. Vdda or to an external voltage reference).
  49. ##### How to use this driver #####
  50. ==============================================================================
  51. [..]
  52. *** Configuration of top level parameters related to ADC ***
  53. ============================================================
  54. [..]
  55. (#) Enable the ADC interface
  56. (++) As prerequisite, ADC clock must be configured at RCC top level.
  57. Caution: On STM32F1, ADC clock frequency max is 14MHz (refer
  58. to device datasheet).
  59. Therefore, ADC clock prescaler must be configured in
  60. function of ADC clock source frequency to remain below
  61. this maximum frequency.
  62. (++) One clock setting is mandatory:
  63. ADC clock (core clock, also possibly conversion clock).
  64. (+++) Example:
  65. Into HAL_ADC_MspInit() (recommended code location) or with
  66. other device clock parameters configuration:
  67. (+++) RCC_PeriphCLKInitTypeDef PeriphClkInit;
  68. (+++) __ADC1_CLK_ENABLE();
  69. (+++) PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  70. (+++) PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV2;
  71. (+++) HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
  72. (#) ADC pins configuration
  73. (++) Enable the clock for the ADC GPIOs
  74. using macro __HAL_RCC_GPIOx_CLK_ENABLE()
  75. (++) Configure these ADC pins in analog mode
  76. using function HAL_GPIO_Init()
  77. (#) Optionally, in case of usage of ADC with interruptions:
  78. (++) Configure the NVIC for ADC
  79. using function HAL_NVIC_EnableIRQ(ADCx_IRQn)
  80. (++) Insert the ADC interruption handler function HAL_ADC_IRQHandler()
  81. into the function of corresponding ADC interruption vector
  82. ADCx_IRQHandler().
  83. (#) Optionally, in case of usage of DMA:
  84. (++) Configure the DMA (DMA channel, mode normal or circular, ...)
  85. using function HAL_DMA_Init().
  86. (++) Configure the NVIC for DMA
  87. using function HAL_NVIC_EnableIRQ(DMAx_Channelx_IRQn)
  88. (++) Insert the ADC interruption handler function HAL_ADC_IRQHandler()
  89. into the function of corresponding DMA interruption vector
  90. DMAx_Channelx_IRQHandler().
  91. *** Configuration of ADC, groups regular/injected, channels parameters ***
  92. ==========================================================================
  93. [..]
  94. (#) Configure the ADC parameters (resolution, data alignment, ...)
  95. and regular group parameters (conversion trigger, sequencer, ...)
  96. using function HAL_ADC_Init().
  97. (#) Configure the channels for regular group parameters (channel number,
  98. channel rank into sequencer, ..., into regular group)
  99. using function HAL_ADC_ConfigChannel().
  100. (#) Optionally, configure the injected group parameters (conversion trigger,
  101. sequencer, ..., of injected group)
  102. and the channels for injected group parameters (channel number,
  103. channel rank into sequencer, ..., into injected group)
  104. using function HAL_ADCEx_InjectedConfigChannel().
  105. (#) Optionally, configure the analog watchdog parameters (channels
  106. monitored, thresholds, ...)
  107. using function HAL_ADC_AnalogWDGConfig().
  108. (#) Optionally, for devices with several ADC instances: configure the
  109. multimode parameters
  110. using function HAL_ADCEx_MultiModeConfigChannel().
  111. *** Execution of ADC conversions ***
  112. ====================================
  113. [..]
  114. (#) Optionally, perform an automatic ADC calibration to improve the
  115. conversion accuracy
  116. using function HAL_ADCEx_Calibration_Start().
  117. (#) ADC driver can be used among three modes: polling, interruption,
  118. transfer by DMA.
  119. (++) ADC conversion by polling:
  120. (+++) Activate the ADC peripheral and start conversions
  121. using function HAL_ADC_Start()
  122. (+++) Wait for ADC conversion completion
  123. using function HAL_ADC_PollForConversion()
  124. (or for injected group: HAL_ADCEx_InjectedPollForConversion() )
  125. (+++) Retrieve conversion results
  126. using function HAL_ADC_GetValue()
  127. (or for injected group: HAL_ADCEx_InjectedGetValue() )
  128. (+++) Stop conversion and disable the ADC peripheral
  129. using function HAL_ADC_Stop()
  130. (++) ADC conversion by interruption:
  131. (+++) Activate the ADC peripheral and start conversions
  132. using function HAL_ADC_Start_IT()
  133. (+++) Wait for ADC conversion completion by call of function
  134. HAL_ADC_ConvCpltCallback()
  135. (this function must be implemented in user program)
  136. (or for injected group: HAL_ADCEx_InjectedConvCpltCallback() )
  137. (+++) Retrieve conversion results
  138. using function HAL_ADC_GetValue()
  139. (or for injected group: HAL_ADCEx_InjectedGetValue() )
  140. (+++) Stop conversion and disable the ADC peripheral
  141. using function HAL_ADC_Stop_IT()
  142. (++) ADC conversion with transfer by DMA:
  143. (+++) Activate the ADC peripheral and start conversions
  144. using function HAL_ADC_Start_DMA()
  145. (+++) Wait for ADC conversion completion by call of function
  146. HAL_ADC_ConvCpltCallback() or HAL_ADC_ConvHalfCpltCallback()
  147. (these functions must be implemented in user program)
  148. (+++) Conversion results are automatically transferred by DMA into
  149. destination variable address.
  150. (+++) Stop conversion and disable the ADC peripheral
  151. using function HAL_ADC_Stop_DMA()
  152. (++) For devices with several ADCs: ADC multimode conversion
  153. with transfer by DMA:
  154. (+++) Activate the ADC peripheral (slave) and start conversions
  155. using function HAL_ADC_Start()
  156. (+++) Activate the ADC peripheral (master) and start conversions
  157. using function HAL_ADCEx_MultiModeStart_DMA()
  158. (+++) Wait for ADC conversion completion by call of function
  159. HAL_ADC_ConvCpltCallback() or HAL_ADC_ConvHalfCpltCallback()
  160. (these functions must be implemented in user program)
  161. (+++) Conversion results are automatically transferred by DMA into
  162. destination variable address.
  163. (+++) Stop conversion and disable the ADC peripheral (master)
  164. using function HAL_ADCEx_MultiModeStop_DMA()
  165. (+++) Stop conversion and disable the ADC peripheral (slave)
  166. using function HAL_ADC_Stop_IT()
  167. [..]
  168. (@) Callback functions must be implemented in user program:
  169. (+@) HAL_ADC_ErrorCallback()
  170. (+@) HAL_ADC_LevelOutOfWindowCallback() (callback of analog watchdog)
  171. (+@) HAL_ADC_ConvCpltCallback()
  172. (+@) HAL_ADC_ConvHalfCpltCallback
  173. (+@) HAL_ADCEx_InjectedConvCpltCallback()
  174. *** Deinitialization of ADC ***
  175. ============================================================
  176. [..]
  177. (#) Disable the ADC interface
  178. (++) ADC clock can be hard reset and disabled at RCC top level.
  179. (++) Hard reset of ADC peripherals
  180. using macro __ADCx_FORCE_RESET(), __ADCx_RELEASE_RESET().
  181. (++) ADC clock disable
  182. using the equivalent macro/functions as configuration step.
  183. (+++) Example:
  184. Into HAL_ADC_MspDeInit() (recommended code location) or with
  185. other device clock parameters configuration:
  186. (+++) PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC
  187. (+++) PeriphClkInit.AdcClockSelection = RCC_ADCPLLCLK2_OFF
  188. (+++) HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit)
  189. (#) ADC pins configuration
  190. (++) Disable the clock for the ADC GPIOs
  191. using macro __HAL_RCC_GPIOx_CLK_DISABLE()
  192. (#) Optionally, in case of usage of ADC with interruptions:
  193. (++) Disable the NVIC for ADC
  194. using function HAL_NVIC_EnableIRQ(ADCx_IRQn)
  195. (#) Optionally, in case of usage of DMA:
  196. (++) Deinitialize the DMA
  197. using function HAL_DMA_Init().
  198. (++) Disable the NVIC for DMA
  199. using function HAL_NVIC_EnableIRQ(DMAx_Channelx_IRQn)
  200. [..]
  201. *** Callback registration ***
  202. =============================================
  203. [..]
  204. The compilation flag USE_HAL_ADC_REGISTER_CALLBACKS, when set to 1,
  205. allows the user to configure dynamically the driver callbacks.
  206. Use Functions @ref HAL_ADC_RegisterCallback()
  207. to register an interrupt callback.
  208. [..]
  209. Function @ref HAL_ADC_RegisterCallback() allows to register following callbacks:
  210. (+) ConvCpltCallback : ADC conversion complete callback
  211. (+) ConvHalfCpltCallback : ADC conversion DMA half-transfer callback
  212. (+) LevelOutOfWindowCallback : ADC analog watchdog 1 callback
  213. (+) ErrorCallback : ADC error callback
  214. (+) InjectedConvCpltCallback : ADC group injected conversion complete callback
  215. (+) MspInitCallback : ADC Msp Init callback
  216. (+) MspDeInitCallback : ADC Msp DeInit callback
  217. This function takes as parameters the HAL peripheral handle, the Callback ID
  218. and a pointer to the user callback function.
  219. [..]
  220. Use function @ref HAL_ADC_UnRegisterCallback to reset a callback to the default
  221. weak function.
  222. [..]
  223. @ref HAL_ADC_UnRegisterCallback takes as parameters the HAL peripheral handle,
  224. and the Callback ID.
  225. This function allows to reset following callbacks:
  226. (+) ConvCpltCallback : ADC conversion complete callback
  227. (+) ConvHalfCpltCallback : ADC conversion DMA half-transfer callback
  228. (+) LevelOutOfWindowCallback : ADC analog watchdog 1 callback
  229. (+) ErrorCallback : ADC error callback
  230. (+) InjectedConvCpltCallback : ADC group injected conversion complete callback
  231. (+) MspInitCallback : ADC Msp Init callback
  232. (+) MspDeInitCallback : ADC Msp DeInit callback
  233. [..]
  234. By default, after the @ref HAL_ADC_Init() and when the state is @ref HAL_ADC_STATE_RESET
  235. all callbacks are set to the corresponding weak functions:
  236. examples @ref HAL_ADC_ConvCpltCallback(), @ref HAL_ADC_ErrorCallback().
  237. Exception done for MspInit and MspDeInit functions that are
  238. reset to the legacy weak functions in the @ref HAL_ADC_Init()/ @ref HAL_ADC_DeInit() only when
  239. these callbacks are null (not registered beforehand).
  240. [..]
  241. If MspInit or MspDeInit are not null, the @ref HAL_ADC_Init()/ @ref HAL_ADC_DeInit()
  242. keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
  243. [..]
  244. Callbacks can be registered/unregistered in @ref HAL_ADC_STATE_READY state only.
  245. Exception done MspInit/MspDeInit functions that can be registered/unregistered
  246. in @ref HAL_ADC_STATE_READY or @ref HAL_ADC_STATE_RESET state,
  247. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  248. [..]
  249. Then, the user first registers the MspInit/MspDeInit user callbacks
  250. using @ref HAL_ADC_RegisterCallback() before calling @ref HAL_ADC_DeInit()
  251. or @ref HAL_ADC_Init() function.
  252. [..]
  253. When the compilation flag USE_HAL_ADC_REGISTER_CALLBACKS is set to 0 or
  254. not defined, the callback registration feature is not available and all callbacks
  255. are set to the corresponding weak functions.
  256. @endverbatim
  257. ******************************************************************************
  258. * @attention
  259. *
  260. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  261. * All rights reserved.</center></h2>
  262. *
  263. * This software component is licensed by ST under BSD 3-Clause license,
  264. * the "License"; You may not use this file except in compliance with the
  265. * License. You may obtain a copy of the License at:
  266. * opensource.org/licenses/BSD-3-Clause
  267. *
  268. ******************************************************************************
  269. */
  270. /* Includes ------------------------------------------------------------------*/
  271. #include "stm32f1xx_hal.h"
  272. /** @addtogroup STM32F1xx_HAL_Driver
  273. * @{
  274. */
  275. /** @defgroup ADC ADC
  276. * @brief ADC HAL module driver
  277. * @{
  278. */
  279. #ifdef HAL_ADC_MODULE_ENABLED
  280. /* Private typedef -----------------------------------------------------------*/
  281. /* Private define ------------------------------------------------------------*/
  282. /** @defgroup ADC_Private_Constants ADC Private Constants
  283. * @{
  284. */
  285. /* Timeout values for ADC enable and disable settling time. */
  286. /* Values defined to be higher than worst cases: low clocks freq, */
  287. /* maximum prescaler. */
  288. /* Ex of profile low frequency : Clock source at 0.1 MHz, ADC clock */
  289. /* prescaler 4, sampling time 12.5 ADC clock cycles, resolution 12 bits. */
  290. /* Unit: ms */
  291. #define ADC_ENABLE_TIMEOUT 2U
  292. #define ADC_DISABLE_TIMEOUT 2U
  293. /* Delay for ADC stabilization time. */
  294. /* Maximum delay is 1us (refer to device datasheet, parameter tSTAB). */
  295. /* Unit: us */
  296. #define ADC_STAB_DELAY_US 1U
  297. /* Delay for temperature sensor stabilization time. */
  298. /* Maximum delay is 10us (refer to device datasheet, parameter tSTART). */
  299. /* Unit: us */
  300. #define ADC_TEMPSENSOR_DELAY_US 10U
  301. /**
  302. * @}
  303. */
  304. /* Private macro -------------------------------------------------------------*/
  305. /* Private variables ---------------------------------------------------------*/
  306. /* Private function prototypes -----------------------------------------------*/
  307. /** @defgroup ADC_Private_Functions ADC Private Functions
  308. * @{
  309. */
  310. /**
  311. * @}
  312. */
  313. /* Exported functions --------------------------------------------------------*/
  314. /** @defgroup ADC_Exported_Functions ADC Exported Functions
  315. * @{
  316. */
  317. /** @defgroup ADC_Exported_Functions_Group1 Initialization/de-initialization functions
  318. * @brief Initialization and Configuration functions
  319. *
  320. @verbatim
  321. ===============================================================================
  322. ##### Initialization and de-initialization functions #####
  323. ===============================================================================
  324. [..] This section provides functions allowing to:
  325. (+) Initialize and configure the ADC.
  326. (+) De-initialize the ADC.
  327. @endverbatim
  328. * @{
  329. */
  330. /**
  331. * @brief Initializes the ADC peripheral and regular group according to
  332. * parameters specified in structure "ADC_InitTypeDef".
  333. * @note As prerequisite, ADC clock must be configured at RCC top level
  334. * (clock source APB2).
  335. * See commented example code below that can be copied and uncommented
  336. * into HAL_ADC_MspInit().
  337. * @note Possibility to update parameters on the fly:
  338. * This function initializes the ADC MSP (HAL_ADC_MspInit()) only when
  339. * coming from ADC state reset. Following calls to this function can
  340. * be used to reconfigure some parameters of ADC_InitTypeDef
  341. * structure on the fly, without modifying MSP configuration. If ADC
  342. * MSP has to be modified again, HAL_ADC_DeInit() must be called
  343. * before HAL_ADC_Init().
  344. * The setting of these parameters is conditioned to ADC state.
  345. * For parameters constraints, see comments of structure
  346. * "ADC_InitTypeDef".
  347. * @note This function configures the ADC within 2 scopes: scope of entire
  348. * ADC and scope of regular group. For parameters details, see comments
  349. * of structure "ADC_InitTypeDef".
  350. * @param hadc: ADC handle
  351. * @retval HAL status
  352. */
  353. HAL_StatusTypeDef HAL_ADC_Init(ADC_HandleTypeDef* hadc)
  354. {
  355. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  356. uint32_t tmp_cr1 = 0U;
  357. uint32_t tmp_cr2 = 0U;
  358. uint32_t tmp_sqr1 = 0U;
  359. /* Check ADC handle */
  360. if(hadc == NULL)
  361. {
  362. return HAL_ERROR;
  363. }
  364. /* Check the parameters */
  365. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  366. assert_param(IS_ADC_DATA_ALIGN(hadc->Init.DataAlign));
  367. assert_param(IS_ADC_SCAN_MODE(hadc->Init.ScanConvMode));
  368. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
  369. assert_param(IS_ADC_EXTTRIG(hadc->Init.ExternalTrigConv));
  370. if(hadc->Init.ScanConvMode != ADC_SCAN_DISABLE)
  371. {
  372. assert_param(IS_ADC_REGULAR_NB_CONV(hadc->Init.NbrOfConversion));
  373. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DiscontinuousConvMode));
  374. if(hadc->Init.DiscontinuousConvMode != DISABLE)
  375. {
  376. assert_param(IS_ADC_REGULAR_DISCONT_NUMBER(hadc->Init.NbrOfDiscConversion));
  377. }
  378. }
  379. /* As prerequisite, into HAL_ADC_MspInit(), ADC clock must be configured */
  380. /* at RCC top level. */
  381. /* Refer to header of this file for more details on clock enabling */
  382. /* procedure. */
  383. /* Actions performed only if ADC is coming from state reset: */
  384. /* - Initialization of ADC MSP */
  385. if (hadc->State == HAL_ADC_STATE_RESET)
  386. {
  387. /* Initialize ADC error code */
  388. ADC_CLEAR_ERRORCODE(hadc);
  389. /* Allocate lock resource and initialize it */
  390. hadc->Lock = HAL_UNLOCKED;
  391. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  392. /* Init the ADC Callback settings */
  393. hadc->ConvCpltCallback = HAL_ADC_ConvCpltCallback; /* Legacy weak callback */
  394. hadc->ConvHalfCpltCallback = HAL_ADC_ConvHalfCpltCallback; /* Legacy weak callback */
  395. hadc->LevelOutOfWindowCallback = HAL_ADC_LevelOutOfWindowCallback; /* Legacy weak callback */
  396. hadc->ErrorCallback = HAL_ADC_ErrorCallback; /* Legacy weak callback */
  397. hadc->InjectedConvCpltCallback = HAL_ADCEx_InjectedConvCpltCallback; /* Legacy weak callback */
  398. if (hadc->MspInitCallback == NULL)
  399. {
  400. hadc->MspInitCallback = HAL_ADC_MspInit; /* Legacy weak MspInit */
  401. }
  402. /* Init the low level hardware */
  403. hadc->MspInitCallback(hadc);
  404. #else
  405. /* Init the low level hardware */
  406. HAL_ADC_MspInit(hadc);
  407. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  408. }
  409. /* Stop potential conversion on going, on regular and injected groups */
  410. /* Disable ADC peripheral */
  411. /* Note: In case of ADC already enabled, precaution to not launch an */
  412. /* unwanted conversion while modifying register CR2 by writing 1 to */
  413. /* bit ADON. */
  414. tmp_hal_status = ADC_ConversionStop_Disable(hadc);
  415. /* Configuration of ADC parameters if previous preliminary actions are */
  416. /* correctly completed. */
  417. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL) &&
  418. (tmp_hal_status == HAL_OK) )
  419. {
  420. /* Set ADC state */
  421. ADC_STATE_CLR_SET(hadc->State,
  422. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  423. HAL_ADC_STATE_BUSY_INTERNAL);
  424. /* Set ADC parameters */
  425. /* Configuration of ADC: */
  426. /* - data alignment */
  427. /* - external trigger to start conversion */
  428. /* - external trigger polarity (always set to 1, because needed for all */
  429. /* triggers: external trigger of SW start) */
  430. /* - continuous conversion mode */
  431. /* Note: External trigger polarity (ADC_CR2_EXTTRIG) is set into */
  432. /* HAL_ADC_Start_xxx functions because if set in this function, */
  433. /* a conversion on injected group would start a conversion also on */
  434. /* regular group after ADC enabling. */
  435. tmp_cr2 |= (hadc->Init.DataAlign |
  436. ADC_CFGR_EXTSEL(hadc, hadc->Init.ExternalTrigConv) |
  437. ADC_CR2_CONTINUOUS((uint32_t)hadc->Init.ContinuousConvMode) );
  438. /* Configuration of ADC: */
  439. /* - scan mode */
  440. /* - discontinuous mode disable/enable */
  441. /* - discontinuous mode number of conversions */
  442. tmp_cr1 |= (ADC_CR1_SCAN_SET(hadc->Init.ScanConvMode));
  443. /* Enable discontinuous mode only if continuous mode is disabled */
  444. /* Note: If parameter "Init.ScanConvMode" is set to disable, parameter */
  445. /* discontinuous is set anyway, but will have no effect on ADC HW. */
  446. if (hadc->Init.DiscontinuousConvMode == ENABLE)
  447. {
  448. if (hadc->Init.ContinuousConvMode == DISABLE)
  449. {
  450. /* Enable the selected ADC regular discontinuous mode */
  451. /* Set the number of channels to be converted in discontinuous mode */
  452. SET_BIT(tmp_cr1, ADC_CR1_DISCEN |
  453. ADC_CR1_DISCONTINUOUS_NUM(hadc->Init.NbrOfDiscConversion) );
  454. }
  455. else
  456. {
  457. /* ADC regular group settings continuous and sequencer discontinuous*/
  458. /* cannot be enabled simultaneously. */
  459. /* Update ADC state machine to error */
  460. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  461. /* Set ADC error code to ADC IP internal error */
  462. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  463. }
  464. }
  465. /* Update ADC configuration register CR1 with previous settings */
  466. MODIFY_REG(hadc->Instance->CR1,
  467. ADC_CR1_SCAN |
  468. ADC_CR1_DISCEN |
  469. ADC_CR1_DISCNUM ,
  470. tmp_cr1 );
  471. /* Update ADC configuration register CR2 with previous settings */
  472. MODIFY_REG(hadc->Instance->CR2,
  473. ADC_CR2_ALIGN |
  474. ADC_CR2_EXTSEL |
  475. ADC_CR2_EXTTRIG |
  476. ADC_CR2_CONT ,
  477. tmp_cr2 );
  478. /* Configuration of regular group sequencer: */
  479. /* - if scan mode is disabled, regular channels sequence length is set to */
  480. /* 0x00: 1 channel converted (channel on regular rank 1) */
  481. /* Parameter "NbrOfConversion" is discarded. */
  482. /* Note: Scan mode is present by hardware on this device and, if */
  483. /* disabled, discards automatically nb of conversions. Anyway, nb of */
  484. /* conversions is forced to 0x00 for alignment over all STM32 devices. */
  485. /* - if scan mode is enabled, regular channels sequence length is set to */
  486. /* parameter "NbrOfConversion" */
  487. if (ADC_CR1_SCAN_SET(hadc->Init.ScanConvMode) == ADC_SCAN_ENABLE)
  488. {
  489. tmp_sqr1 = ADC_SQR1_L_SHIFT(hadc->Init.NbrOfConversion);
  490. }
  491. MODIFY_REG(hadc->Instance->SQR1,
  492. ADC_SQR1_L ,
  493. tmp_sqr1 );
  494. /* Check back that ADC registers have effectively been configured to */
  495. /* ensure of no potential problem of ADC core IP clocking. */
  496. /* Check through register CR2 (excluding bits set in other functions: */
  497. /* execution control bits (ADON, JSWSTART, SWSTART), regular group bits */
  498. /* (DMA), injected group bits (JEXTTRIG and JEXTSEL), channel internal */
  499. /* measurement path bit (TSVREFE). */
  500. if (READ_BIT(hadc->Instance->CR2, ~(ADC_CR2_ADON | ADC_CR2_DMA |
  501. ADC_CR2_SWSTART | ADC_CR2_JSWSTART |
  502. ADC_CR2_JEXTTRIG | ADC_CR2_JEXTSEL |
  503. ADC_CR2_TSVREFE ))
  504. == tmp_cr2)
  505. {
  506. /* Set ADC error code to none */
  507. ADC_CLEAR_ERRORCODE(hadc);
  508. /* Set the ADC state */
  509. ADC_STATE_CLR_SET(hadc->State,
  510. HAL_ADC_STATE_BUSY_INTERNAL,
  511. HAL_ADC_STATE_READY);
  512. }
  513. else
  514. {
  515. /* Update ADC state machine to error */
  516. ADC_STATE_CLR_SET(hadc->State,
  517. HAL_ADC_STATE_BUSY_INTERNAL,
  518. HAL_ADC_STATE_ERROR_INTERNAL);
  519. /* Set ADC error code to ADC IP internal error */
  520. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  521. tmp_hal_status = HAL_ERROR;
  522. }
  523. }
  524. else
  525. {
  526. /* Update ADC state machine to error */
  527. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  528. tmp_hal_status = HAL_ERROR;
  529. }
  530. /* Return function status */
  531. return tmp_hal_status;
  532. }
  533. /**
  534. * @brief Deinitialize the ADC peripheral registers to their default reset
  535. * values, with deinitialization of the ADC MSP.
  536. * If needed, the example code can be copied and uncommented into
  537. * function HAL_ADC_MspDeInit().
  538. * @param hadc: ADC handle
  539. * @retval HAL status
  540. */
  541. HAL_StatusTypeDef HAL_ADC_DeInit(ADC_HandleTypeDef* hadc)
  542. {
  543. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  544. /* Check ADC handle */
  545. if(hadc == NULL)
  546. {
  547. return HAL_ERROR;
  548. }
  549. /* Check the parameters */
  550. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  551. /* Set ADC state */
  552. SET_BIT(hadc->State, HAL_ADC_STATE_BUSY_INTERNAL);
  553. /* Stop potential conversion on going, on regular and injected groups */
  554. /* Disable ADC peripheral */
  555. tmp_hal_status = ADC_ConversionStop_Disable(hadc);
  556. /* Configuration of ADC parameters if previous preliminary actions are */
  557. /* correctly completed. */
  558. if (tmp_hal_status == HAL_OK)
  559. {
  560. /* ========== Reset ADC registers ========== */
  561. /* Reset register SR */
  562. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_AWD | ADC_FLAG_JEOC | ADC_FLAG_EOC |
  563. ADC_FLAG_JSTRT | ADC_FLAG_STRT));
  564. /* Reset register CR1 */
  565. CLEAR_BIT(hadc->Instance->CR1, (ADC_CR1_AWDEN | ADC_CR1_JAWDEN | ADC_CR1_DISCNUM |
  566. ADC_CR1_JDISCEN | ADC_CR1_DISCEN | ADC_CR1_JAUTO |
  567. ADC_CR1_AWDSGL | ADC_CR1_SCAN | ADC_CR1_JEOCIE |
  568. ADC_CR1_AWDIE | ADC_CR1_EOCIE | ADC_CR1_AWDCH ));
  569. /* Reset register CR2 */
  570. CLEAR_BIT(hadc->Instance->CR2, (ADC_CR2_TSVREFE | ADC_CR2_SWSTART | ADC_CR2_JSWSTART |
  571. ADC_CR2_EXTTRIG | ADC_CR2_EXTSEL | ADC_CR2_JEXTTRIG |
  572. ADC_CR2_JEXTSEL | ADC_CR2_ALIGN | ADC_CR2_DMA |
  573. ADC_CR2_RSTCAL | ADC_CR2_CAL | ADC_CR2_CONT |
  574. ADC_CR2_ADON ));
  575. /* Reset register SMPR1 */
  576. CLEAR_BIT(hadc->Instance->SMPR1, (ADC_SMPR1_SMP17 | ADC_SMPR1_SMP16 | ADC_SMPR1_SMP15 |
  577. ADC_SMPR1_SMP14 | ADC_SMPR1_SMP13 | ADC_SMPR1_SMP12 |
  578. ADC_SMPR1_SMP11 | ADC_SMPR1_SMP10 ));
  579. /* Reset register SMPR2 */
  580. CLEAR_BIT(hadc->Instance->SMPR2, (ADC_SMPR2_SMP9 | ADC_SMPR2_SMP8 | ADC_SMPR2_SMP7 |
  581. ADC_SMPR2_SMP6 | ADC_SMPR2_SMP5 | ADC_SMPR2_SMP4 |
  582. ADC_SMPR2_SMP3 | ADC_SMPR2_SMP2 | ADC_SMPR2_SMP1 |
  583. ADC_SMPR2_SMP0 ));
  584. /* Reset register JOFR1 */
  585. CLEAR_BIT(hadc->Instance->JOFR1, ADC_JOFR1_JOFFSET1);
  586. /* Reset register JOFR2 */
  587. CLEAR_BIT(hadc->Instance->JOFR2, ADC_JOFR2_JOFFSET2);
  588. /* Reset register JOFR3 */
  589. CLEAR_BIT(hadc->Instance->JOFR3, ADC_JOFR3_JOFFSET3);
  590. /* Reset register JOFR4 */
  591. CLEAR_BIT(hadc->Instance->JOFR4, ADC_JOFR4_JOFFSET4);
  592. /* Reset register HTR */
  593. CLEAR_BIT(hadc->Instance->HTR, ADC_HTR_HT);
  594. /* Reset register LTR */
  595. CLEAR_BIT(hadc->Instance->LTR, ADC_LTR_LT);
  596. /* Reset register SQR1 */
  597. CLEAR_BIT(hadc->Instance->SQR1, ADC_SQR1_L |
  598. ADC_SQR1_SQ16 | ADC_SQR1_SQ15 |
  599. ADC_SQR1_SQ14 | ADC_SQR1_SQ13 );
  600. /* Reset register SQR1 */
  601. CLEAR_BIT(hadc->Instance->SQR1, ADC_SQR1_L |
  602. ADC_SQR1_SQ16 | ADC_SQR1_SQ15 |
  603. ADC_SQR1_SQ14 | ADC_SQR1_SQ13 );
  604. /* Reset register SQR2 */
  605. CLEAR_BIT(hadc->Instance->SQR2, ADC_SQR2_SQ12 | ADC_SQR2_SQ11 | ADC_SQR2_SQ10 |
  606. ADC_SQR2_SQ9 | ADC_SQR2_SQ8 | ADC_SQR2_SQ7 );
  607. /* Reset register SQR3 */
  608. CLEAR_BIT(hadc->Instance->SQR3, ADC_SQR3_SQ6 | ADC_SQR3_SQ5 | ADC_SQR3_SQ4 |
  609. ADC_SQR3_SQ3 | ADC_SQR3_SQ2 | ADC_SQR3_SQ1 );
  610. /* Reset register JSQR */
  611. CLEAR_BIT(hadc->Instance->JSQR, ADC_JSQR_JL |
  612. ADC_JSQR_JSQ4 | ADC_JSQR_JSQ3 |
  613. ADC_JSQR_JSQ2 | ADC_JSQR_JSQ1 );
  614. /* Reset register JSQR */
  615. CLEAR_BIT(hadc->Instance->JSQR, ADC_JSQR_JL |
  616. ADC_JSQR_JSQ4 | ADC_JSQR_JSQ3 |
  617. ADC_JSQR_JSQ2 | ADC_JSQR_JSQ1 );
  618. /* Reset register DR */
  619. /* bits in access mode read only, no direct reset applicable*/
  620. /* Reset registers JDR1, JDR2, JDR3, JDR4 */
  621. /* bits in access mode read only, no direct reset applicable*/
  622. /* ========== Hard reset ADC peripheral ========== */
  623. /* Performs a global reset of the entire ADC peripheral: ADC state is */
  624. /* forced to a similar state after device power-on. */
  625. /* If needed, copy-paste and uncomment the following reset code into */
  626. /* function "void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)": */
  627. /* */
  628. /* __HAL_RCC_ADC1_FORCE_RESET() */
  629. /* __HAL_RCC_ADC1_RELEASE_RESET() */
  630. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  631. if (hadc->MspDeInitCallback == NULL)
  632. {
  633. hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit */
  634. }
  635. /* DeInit the low level hardware */
  636. hadc->MspDeInitCallback(hadc);
  637. #else
  638. /* DeInit the low level hardware */
  639. HAL_ADC_MspDeInit(hadc);
  640. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  641. /* Set ADC error code to none */
  642. ADC_CLEAR_ERRORCODE(hadc);
  643. /* Set ADC state */
  644. hadc->State = HAL_ADC_STATE_RESET;
  645. }
  646. /* Process unlocked */
  647. __HAL_UNLOCK(hadc);
  648. /* Return function status */
  649. return tmp_hal_status;
  650. }
  651. /**
  652. * @brief Initializes the ADC MSP.
  653. * @param hadc: ADC handle
  654. * @retval None
  655. */
  656. __weak void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
  657. {
  658. /* Prevent unused argument(s) compilation warning */
  659. UNUSED(hadc);
  660. /* NOTE : This function should not be modified. When the callback is needed,
  661. function HAL_ADC_MspInit must be implemented in the user file.
  662. */
  663. }
  664. /**
  665. * @brief DeInitializes the ADC MSP.
  666. * @param hadc: ADC handle
  667. * @retval None
  668. */
  669. __weak void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc)
  670. {
  671. /* Prevent unused argument(s) compilation warning */
  672. UNUSED(hadc);
  673. /* NOTE : This function should not be modified. When the callback is needed,
  674. function HAL_ADC_MspDeInit must be implemented in the user file.
  675. */
  676. }
  677. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  678. /**
  679. * @brief Register a User ADC Callback
  680. * To be used instead of the weak predefined callback
  681. * @param hadc Pointer to a ADC_HandleTypeDef structure that contains
  682. * the configuration information for the specified ADC.
  683. * @param CallbackID ID of the callback to be registered
  684. * This parameter can be one of the following values:
  685. * @arg @ref HAL_ADC_CONVERSION_COMPLETE_CB_ID ADC conversion complete callback ID
  686. * @arg @ref HAL_ADC_CONVERSION_HALF_CB_ID ADC conversion complete callback ID
  687. * @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID ADC analog watchdog 1 callback ID
  688. * @arg @ref HAL_ADC_ERROR_CB_ID ADC error callback ID
  689. * @arg @ref HAL_ADC_INJ_CONVERSION_COMPLETE_CB_ID ADC group injected conversion complete callback ID
  690. * @arg @ref HAL_ADC_MSPINIT_CB_ID ADC Msp Init callback ID
  691. * @arg @ref HAL_ADC_MSPDEINIT_CB_ID ADC Msp DeInit callback ID
  692. * @arg @ref HAL_ADC_MSPINIT_CB_ID MspInit callback ID
  693. * @arg @ref HAL_ADC_MSPDEINIT_CB_ID MspDeInit callback ID
  694. * @param pCallback pointer to the Callback function
  695. * @retval HAL status
  696. */
  697. HAL_StatusTypeDef HAL_ADC_RegisterCallback(ADC_HandleTypeDef *hadc, HAL_ADC_CallbackIDTypeDef CallbackID, pADC_CallbackTypeDef pCallback)
  698. {
  699. HAL_StatusTypeDef status = HAL_OK;
  700. if (pCallback == NULL)
  701. {
  702. /* Update the error code */
  703. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  704. return HAL_ERROR;
  705. }
  706. if ((hadc->State & HAL_ADC_STATE_READY) != 0)
  707. {
  708. switch (CallbackID)
  709. {
  710. case HAL_ADC_CONVERSION_COMPLETE_CB_ID :
  711. hadc->ConvCpltCallback = pCallback;
  712. break;
  713. case HAL_ADC_CONVERSION_HALF_CB_ID :
  714. hadc->ConvHalfCpltCallback = pCallback;
  715. break;
  716. case HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID :
  717. hadc->LevelOutOfWindowCallback = pCallback;
  718. break;
  719. case HAL_ADC_ERROR_CB_ID :
  720. hadc->ErrorCallback = pCallback;
  721. break;
  722. case HAL_ADC_INJ_CONVERSION_COMPLETE_CB_ID :
  723. hadc->InjectedConvCpltCallback = pCallback;
  724. break;
  725. case HAL_ADC_MSPINIT_CB_ID :
  726. hadc->MspInitCallback = pCallback;
  727. break;
  728. case HAL_ADC_MSPDEINIT_CB_ID :
  729. hadc->MspDeInitCallback = pCallback;
  730. break;
  731. default :
  732. /* Update the error code */
  733. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  734. /* Return error status */
  735. status = HAL_ERROR;
  736. break;
  737. }
  738. }
  739. else if (HAL_ADC_STATE_RESET == hadc->State)
  740. {
  741. switch (CallbackID)
  742. {
  743. case HAL_ADC_MSPINIT_CB_ID :
  744. hadc->MspInitCallback = pCallback;
  745. break;
  746. case HAL_ADC_MSPDEINIT_CB_ID :
  747. hadc->MspDeInitCallback = pCallback;
  748. break;
  749. default :
  750. /* Update the error code */
  751. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  752. /* Return error status */
  753. status = HAL_ERROR;
  754. break;
  755. }
  756. }
  757. else
  758. {
  759. /* Update the error code */
  760. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  761. /* Return error status */
  762. status = HAL_ERROR;
  763. }
  764. return status;
  765. }
  766. /**
  767. * @brief Unregister a ADC Callback
  768. * ADC callback is redirected to the weak predefined callback
  769. * @param hadc Pointer to a ADC_HandleTypeDef structure that contains
  770. * the configuration information for the specified ADC.
  771. * @param CallbackID ID of the callback to be unregistered
  772. * This parameter can be one of the following values:
  773. * @arg @ref HAL_ADC_CONVERSION_COMPLETE_CB_ID ADC conversion complete callback ID
  774. * @arg @ref HAL_ADC_CONVERSION_HALF_CB_ID ADC conversion complete callback ID
  775. * @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID ADC analog watchdog 1 callback ID
  776. * @arg @ref HAL_ADC_ERROR_CB_ID ADC error callback ID
  777. * @arg @ref HAL_ADC_INJ_CONVERSION_COMPLETE_CB_ID ADC group injected conversion complete callback ID
  778. * @arg @ref HAL_ADC_MSPINIT_CB_ID ADC Msp Init callback ID
  779. * @arg @ref HAL_ADC_MSPDEINIT_CB_ID ADC Msp DeInit callback ID
  780. * @arg @ref HAL_ADC_MSPINIT_CB_ID MspInit callback ID
  781. * @arg @ref HAL_ADC_MSPDEINIT_CB_ID MspDeInit callback ID
  782. * @retval HAL status
  783. */
  784. HAL_StatusTypeDef HAL_ADC_UnRegisterCallback(ADC_HandleTypeDef *hadc, HAL_ADC_CallbackIDTypeDef CallbackID)
  785. {
  786. HAL_StatusTypeDef status = HAL_OK;
  787. if ((hadc->State & HAL_ADC_STATE_READY) != 0)
  788. {
  789. switch (CallbackID)
  790. {
  791. case HAL_ADC_CONVERSION_COMPLETE_CB_ID :
  792. hadc->ConvCpltCallback = HAL_ADC_ConvCpltCallback;
  793. break;
  794. case HAL_ADC_CONVERSION_HALF_CB_ID :
  795. hadc->ConvHalfCpltCallback = HAL_ADC_ConvHalfCpltCallback;
  796. break;
  797. case HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID :
  798. hadc->LevelOutOfWindowCallback = HAL_ADC_LevelOutOfWindowCallback;
  799. break;
  800. case HAL_ADC_ERROR_CB_ID :
  801. hadc->ErrorCallback = HAL_ADC_ErrorCallback;
  802. break;
  803. case HAL_ADC_INJ_CONVERSION_COMPLETE_CB_ID :
  804. hadc->InjectedConvCpltCallback = HAL_ADCEx_InjectedConvCpltCallback;
  805. break;
  806. case HAL_ADC_MSPINIT_CB_ID :
  807. hadc->MspInitCallback = HAL_ADC_MspInit; /* Legacy weak MspInit */
  808. break;
  809. case HAL_ADC_MSPDEINIT_CB_ID :
  810. hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit */
  811. break;
  812. default :
  813. /* Update the error code */
  814. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  815. /* Return error status */
  816. status = HAL_ERROR;
  817. break;
  818. }
  819. }
  820. else if (HAL_ADC_STATE_RESET == hadc->State)
  821. {
  822. switch (CallbackID)
  823. {
  824. case HAL_ADC_MSPINIT_CB_ID :
  825. hadc->MspInitCallback = HAL_ADC_MspInit; /* Legacy weak MspInit */
  826. break;
  827. case HAL_ADC_MSPDEINIT_CB_ID :
  828. hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit */
  829. break;
  830. default :
  831. /* Update the error code */
  832. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  833. /* Return error status */
  834. status = HAL_ERROR;
  835. break;
  836. }
  837. }
  838. else
  839. {
  840. /* Update the error code */
  841. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  842. /* Return error status */
  843. status = HAL_ERROR;
  844. }
  845. return status;
  846. }
  847. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  848. /**
  849. * @}
  850. */
  851. /** @defgroup ADC_Exported_Functions_Group2 IO operation functions
  852. * @brief Input and Output operation functions
  853. *
  854. @verbatim
  855. ===============================================================================
  856. ##### IO operation functions #####
  857. ===============================================================================
  858. [..] This section provides functions allowing to:
  859. (+) Start conversion of regular group.
  860. (+) Stop conversion of regular group.
  861. (+) Poll for conversion complete on regular group.
  862. (+) Poll for conversion event.
  863. (+) Get result of regular channel conversion.
  864. (+) Start conversion of regular group and enable interruptions.
  865. (+) Stop conversion of regular group and disable interruptions.
  866. (+) Handle ADC interrupt request
  867. (+) Start conversion of regular group and enable DMA transfer.
  868. (+) Stop conversion of regular group and disable ADC DMA transfer.
  869. @endverbatim
  870. * @{
  871. */
  872. /**
  873. * @brief Enables ADC, starts conversion of regular group.
  874. * Interruptions enabled in this function: None.
  875. * @param hadc: ADC handle
  876. * @retval HAL status
  877. */
  878. HAL_StatusTypeDef HAL_ADC_Start(ADC_HandleTypeDef* hadc)
  879. {
  880. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  881. /* Check the parameters */
  882. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  883. /* Process locked */
  884. __HAL_LOCK(hadc);
  885. /* Enable the ADC peripheral */
  886. tmp_hal_status = ADC_Enable(hadc);
  887. /* Start conversion if ADC is effectively enabled */
  888. if (tmp_hal_status == HAL_OK)
  889. {
  890. /* Set ADC state */
  891. /* - Clear state bitfield related to regular group conversion results */
  892. /* - Set state bitfield related to regular operation */
  893. ADC_STATE_CLR_SET(hadc->State,
  894. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC,
  895. HAL_ADC_STATE_REG_BUSY);
  896. /* Set group injected state (from auto-injection) and multimode state */
  897. /* for all cases of multimode: independent mode, multimode ADC master */
  898. /* or multimode ADC slave (for devices with several ADCs): */
  899. if (ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc))
  900. {
  901. /* Set ADC state (ADC independent or master) */
  902. CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  903. /* If conversions on group regular are also triggering group injected, */
  904. /* update ADC state. */
  905. if (READ_BIT(hadc->Instance->CR1, ADC_CR1_JAUTO) != RESET)
  906. {
  907. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
  908. }
  909. }
  910. else
  911. {
  912. /* Set ADC state (ADC slave) */
  913. SET_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  914. /* If conversions on group regular are also triggering group injected, */
  915. /* update ADC state. */
  916. if (ADC_MULTIMODE_AUTO_INJECTED(hadc))
  917. {
  918. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
  919. }
  920. }
  921. /* State machine update: Check if an injected conversion is ongoing */
  922. if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  923. {
  924. /* Reset ADC error code fields related to conversions on group regular */
  925. CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA));
  926. }
  927. else
  928. {
  929. /* Reset ADC all error code fields */
  930. ADC_CLEAR_ERRORCODE(hadc);
  931. }
  932. /* Process unlocked */
  933. /* Unlock before starting ADC conversions: in case of potential */
  934. /* interruption, to let the process to ADC IRQ Handler. */
  935. __HAL_UNLOCK(hadc);
  936. /* Clear regular group conversion flag */
  937. /* (To ensure of no unknown state from potential previous ADC operations) */
  938. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOC);
  939. /* Enable conversion of regular group. */
  940. /* If software start has been selected, conversion starts immediately. */
  941. /* If external trigger has been selected, conversion will start at next */
  942. /* trigger event. */
  943. /* Case of multimode enabled: */
  944. /* - if ADC is slave, ADC is enabled only (conversion is not started). */
  945. /* - if ADC is master, ADC is enabled and conversion is started. */
  946. /* If ADC is master, ADC is enabled and conversion is started. */
  947. /* Note: Alternate trigger for single conversion could be to force an */
  948. /* additional set of bit ADON "hadc->Instance->CR2 |= ADC_CR2_ADON;"*/
  949. if (ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  950. ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc) )
  951. {
  952. /* Start ADC conversion on regular group with SW start */
  953. SET_BIT(hadc->Instance->CR2, (ADC_CR2_SWSTART | ADC_CR2_EXTTRIG));
  954. }
  955. else
  956. {
  957. /* Start ADC conversion on regular group with external trigger */
  958. SET_BIT(hadc->Instance->CR2, ADC_CR2_EXTTRIG);
  959. }
  960. }
  961. else
  962. {
  963. /* Process unlocked */
  964. __HAL_UNLOCK(hadc);
  965. }
  966. /* Return function status */
  967. return tmp_hal_status;
  968. }
  969. /**
  970. * @brief Stop ADC conversion of regular group (and injected channels in
  971. * case of auto_injection mode), disable ADC peripheral.
  972. * @note: ADC peripheral disable is forcing stop of potential
  973. * conversion on injected group. If injected group is under use, it
  974. * should be preliminarily stopped using HAL_ADCEx_InjectedStop function.
  975. * @param hadc: ADC handle
  976. * @retval HAL status.
  977. */
  978. HAL_StatusTypeDef HAL_ADC_Stop(ADC_HandleTypeDef* hadc)
  979. {
  980. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  981. /* Check the parameters */
  982. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  983. /* Process locked */
  984. __HAL_LOCK(hadc);
  985. /* Stop potential conversion on going, on regular and injected groups */
  986. /* Disable ADC peripheral */
  987. tmp_hal_status = ADC_ConversionStop_Disable(hadc);
  988. /* Check if ADC is effectively disabled */
  989. if (tmp_hal_status == HAL_OK)
  990. {
  991. /* Set ADC state */
  992. ADC_STATE_CLR_SET(hadc->State,
  993. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  994. HAL_ADC_STATE_READY);
  995. }
  996. /* Process unlocked */
  997. __HAL_UNLOCK(hadc);
  998. /* Return function status */
  999. return tmp_hal_status;
  1000. }
  1001. /**
  1002. * @brief Wait for regular group conversion to be completed.
  1003. * @note This function cannot be used in a particular setup: ADC configured
  1004. * in DMA mode.
  1005. * In this case, DMA resets the flag EOC and polling cannot be
  1006. * performed on each conversion.
  1007. * @note On STM32F1 devices, limitation in case of sequencer enabled
  1008. * (several ranks selected): polling cannot be done on each
  1009. * conversion inside the sequence. In this case, polling is replaced by
  1010. * wait for maximum conversion time.
  1011. * @param hadc: ADC handle
  1012. * @param Timeout: Timeout value in millisecond.
  1013. * @retval HAL status
  1014. */
  1015. HAL_StatusTypeDef HAL_ADC_PollForConversion(ADC_HandleTypeDef* hadc, uint32_t Timeout)
  1016. {
  1017. uint32_t tickstart = 0U;
  1018. /* Variables for polling in case of scan mode enabled and polling for each */
  1019. /* conversion. */
  1020. __IO uint32_t Conversion_Timeout_CPU_cycles = 0U;
  1021. uint32_t Conversion_Timeout_CPU_cycles_max = 0U;
  1022. /* Check the parameters */
  1023. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1024. /* Get tick count */
  1025. tickstart = HAL_GetTick();
  1026. /* Verification that ADC configuration is compliant with polling for */
  1027. /* each conversion: */
  1028. /* Particular case is ADC configured in DMA mode */
  1029. if (HAL_IS_BIT_SET(hadc->Instance->CR2, ADC_CR2_DMA))
  1030. {
  1031. /* Update ADC state machine to error */
  1032. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1033. /* Process unlocked */
  1034. __HAL_UNLOCK(hadc);
  1035. return HAL_ERROR;
  1036. }
  1037. /* Polling for end of conversion: differentiation if single/sequence */
  1038. /* conversion. */
  1039. /* - If single conversion for regular group (Scan mode disabled or enabled */
  1040. /* with NbrOfConversion =1), flag EOC is used to determine the */
  1041. /* conversion completion. */
  1042. /* - If sequence conversion for regular group (scan mode enabled and */
  1043. /* NbrOfConversion >=2), flag EOC is set only at the end of the */
  1044. /* sequence. */
  1045. /* To poll for each conversion, the maximum conversion time is computed */
  1046. /* from ADC conversion time (selected sampling time + conversion time of */
  1047. /* 12.5 ADC clock cycles) and APB2/ADC clock prescalers (depending on */
  1048. /* settings, conversion time range can be from 28 to 32256 CPU cycles). */
  1049. /* As flag EOC is not set after each conversion, no timeout status can */
  1050. /* be set. */
  1051. if (HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_SCAN) &&
  1052. HAL_IS_BIT_CLR(hadc->Instance->SQR1, ADC_SQR1_L) )
  1053. {
  1054. /* Wait until End of Conversion flag is raised */
  1055. while(HAL_IS_BIT_CLR(hadc->Instance->SR, ADC_FLAG_EOC))
  1056. {
  1057. /* Check if timeout is disabled (set to infinite wait) */
  1058. if(Timeout != HAL_MAX_DELAY)
  1059. {
  1060. if((Timeout == 0U) || ((HAL_GetTick() - tickstart ) > Timeout))
  1061. {
  1062. /* New check to avoid false timeout detection in case of preemption */
  1063. if(HAL_IS_BIT_CLR(hadc->Instance->SR, ADC_FLAG_EOC))
  1064. {
  1065. /* Update ADC state machine to timeout */
  1066. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  1067. /* Process unlocked */
  1068. __HAL_UNLOCK(hadc);
  1069. return HAL_TIMEOUT;
  1070. }
  1071. }
  1072. }
  1073. }
  1074. }
  1075. else
  1076. {
  1077. /* Replace polling by wait for maximum conversion time */
  1078. /* - Computation of CPU clock cycles corresponding to ADC clock cycles */
  1079. /* and ADC maximum conversion cycles on all channels. */
  1080. /* - Wait for the expected ADC clock cycles delay */
  1081. Conversion_Timeout_CPU_cycles_max = ((SystemCoreClock
  1082. / HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_ADC))
  1083. * ADC_CONVCYCLES_MAX_RANGE(hadc) );
  1084. while(Conversion_Timeout_CPU_cycles < Conversion_Timeout_CPU_cycles_max)
  1085. {
  1086. /* Check if timeout is disabled (set to infinite wait) */
  1087. if(Timeout != HAL_MAX_DELAY)
  1088. {
  1089. if((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
  1090. {
  1091. /* New check to avoid false timeout detection in case of preemption */
  1092. if(Conversion_Timeout_CPU_cycles < Conversion_Timeout_CPU_cycles_max)
  1093. {
  1094. /* Update ADC state machine to timeout */
  1095. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  1096. /* Process unlocked */
  1097. __HAL_UNLOCK(hadc);
  1098. return HAL_TIMEOUT;
  1099. }
  1100. }
  1101. }
  1102. Conversion_Timeout_CPU_cycles ++;
  1103. }
  1104. }
  1105. /* Clear regular group conversion flag */
  1106. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_STRT | ADC_FLAG_EOC);
  1107. /* Update ADC state machine */
  1108. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  1109. /* Determine whether any further conversion upcoming on group regular */
  1110. /* by external trigger, continuous mode or scan sequence on going. */
  1111. /* Note: On STM32F1 devices, in case of sequencer enabled */
  1112. /* (several ranks selected), end of conversion flag is raised */
  1113. /* at the end of the sequence. */
  1114. if(ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  1115. (hadc->Init.ContinuousConvMode == DISABLE) )
  1116. {
  1117. /* Set ADC state */
  1118. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  1119. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  1120. {
  1121. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  1122. }
  1123. }
  1124. /* Return ADC state */
  1125. return HAL_OK;
  1126. }
  1127. /**
  1128. * @brief Poll for conversion event.
  1129. * @param hadc: ADC handle
  1130. * @param EventType: the ADC event type.
  1131. * This parameter can be one of the following values:
  1132. * @arg ADC_AWD_EVENT: ADC Analog watchdog event.
  1133. * @param Timeout: Timeout value in millisecond.
  1134. * @retval HAL status
  1135. */
  1136. HAL_StatusTypeDef HAL_ADC_PollForEvent(ADC_HandleTypeDef* hadc, uint32_t EventType, uint32_t Timeout)
  1137. {
  1138. uint32_t tickstart = 0U;
  1139. /* Check the parameters */
  1140. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1141. assert_param(IS_ADC_EVENT_TYPE(EventType));
  1142. /* Get tick count */
  1143. tickstart = HAL_GetTick();
  1144. /* Check selected event flag */
  1145. while(__HAL_ADC_GET_FLAG(hadc, EventType) == RESET)
  1146. {
  1147. /* Check if timeout is disabled (set to infinite wait) */
  1148. if(Timeout != HAL_MAX_DELAY)
  1149. {
  1150. if((Timeout == 0U) || ((HAL_GetTick() - tickstart ) > Timeout))
  1151. {
  1152. /* New check to avoid false timeout detection in case of preemption */
  1153. if(__HAL_ADC_GET_FLAG(hadc, EventType) == RESET)
  1154. {
  1155. /* Update ADC state machine to timeout */
  1156. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  1157. /* Process unlocked */
  1158. __HAL_UNLOCK(hadc);
  1159. return HAL_TIMEOUT;
  1160. }
  1161. }
  1162. }
  1163. }
  1164. /* Analog watchdog (level out of window) event */
  1165. /* Set ADC state */
  1166. SET_BIT(hadc->State, HAL_ADC_STATE_AWD1);
  1167. /* Clear ADC analog watchdog flag */
  1168. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD);
  1169. /* Return ADC state */
  1170. return HAL_OK;
  1171. }
  1172. /**
  1173. * @brief Enables ADC, starts conversion of regular group with interruption.
  1174. * Interruptions enabled in this function:
  1175. * - EOC (end of conversion of regular group)
  1176. * Each of these interruptions has its dedicated callback function.
  1177. * @param hadc: ADC handle
  1178. * @retval HAL status
  1179. */
  1180. HAL_StatusTypeDef HAL_ADC_Start_IT(ADC_HandleTypeDef* hadc)
  1181. {
  1182. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1183. /* Check the parameters */
  1184. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1185. /* Process locked */
  1186. __HAL_LOCK(hadc);
  1187. /* Enable the ADC peripheral */
  1188. tmp_hal_status = ADC_Enable(hadc);
  1189. /* Start conversion if ADC is effectively enabled */
  1190. if (tmp_hal_status == HAL_OK)
  1191. {
  1192. /* Set ADC state */
  1193. /* - Clear state bitfield related to regular group conversion results */
  1194. /* - Set state bitfield related to regular operation */
  1195. ADC_STATE_CLR_SET(hadc->State,
  1196. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP,
  1197. HAL_ADC_STATE_REG_BUSY);
  1198. /* Set group injected state (from auto-injection) and multimode state */
  1199. /* for all cases of multimode: independent mode, multimode ADC master */
  1200. /* or multimode ADC slave (for devices with several ADCs): */
  1201. if (ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc))
  1202. {
  1203. /* Set ADC state (ADC independent or master) */
  1204. CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  1205. /* If conversions on group regular are also triggering group injected, */
  1206. /* update ADC state. */
  1207. if (READ_BIT(hadc->Instance->CR1, ADC_CR1_JAUTO) != RESET)
  1208. {
  1209. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
  1210. }
  1211. }
  1212. else
  1213. {
  1214. /* Set ADC state (ADC slave) */
  1215. SET_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  1216. /* If conversions on group regular are also triggering group injected, */
  1217. /* update ADC state. */
  1218. if (ADC_MULTIMODE_AUTO_INJECTED(hadc))
  1219. {
  1220. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
  1221. }
  1222. }
  1223. /* State machine update: Check if an injected conversion is ongoing */
  1224. if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  1225. {
  1226. /* Reset ADC error code fields related to conversions on group regular */
  1227. CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA));
  1228. }
  1229. else
  1230. {
  1231. /* Reset ADC all error code fields */
  1232. ADC_CLEAR_ERRORCODE(hadc);
  1233. }
  1234. /* Process unlocked */
  1235. /* Unlock before starting ADC conversions: in case of potential */
  1236. /* interruption, to let the process to ADC IRQ Handler. */
  1237. __HAL_UNLOCK(hadc);
  1238. /* Clear regular group conversion flag and overrun flag */
  1239. /* (To ensure of no unknown state from potential previous ADC operations) */
  1240. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOC);
  1241. /* Enable end of conversion interrupt for regular group */
  1242. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_EOC);
  1243. /* Enable conversion of regular group. */
  1244. /* If software start has been selected, conversion starts immediately. */
  1245. /* If external trigger has been selected, conversion will start at next */
  1246. /* trigger event. */
  1247. /* Case of multimode enabled: */
  1248. /* - if ADC is slave, ADC is enabled only (conversion is not started). */
  1249. /* - if ADC is master, ADC is enabled and conversion is started. */
  1250. if (ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  1251. ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc) )
  1252. {
  1253. /* Start ADC conversion on regular group with SW start */
  1254. SET_BIT(hadc->Instance->CR2, (ADC_CR2_SWSTART | ADC_CR2_EXTTRIG));
  1255. }
  1256. else
  1257. {
  1258. /* Start ADC conversion on regular group with external trigger */
  1259. SET_BIT(hadc->Instance->CR2, ADC_CR2_EXTTRIG);
  1260. }
  1261. }
  1262. else
  1263. {
  1264. /* Process unlocked */
  1265. __HAL_UNLOCK(hadc);
  1266. }
  1267. /* Return function status */
  1268. return tmp_hal_status;
  1269. }
  1270. /**
  1271. * @brief Stop ADC conversion of regular group (and injected group in
  1272. * case of auto_injection mode), disable interrution of
  1273. * end-of-conversion, disable ADC peripheral.
  1274. * @param hadc: ADC handle
  1275. * @retval None
  1276. */
  1277. HAL_StatusTypeDef HAL_ADC_Stop_IT(ADC_HandleTypeDef* hadc)
  1278. {
  1279. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1280. /* Check the parameters */
  1281. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1282. /* Process locked */
  1283. __HAL_LOCK(hadc);
  1284. /* Stop potential conversion on going, on regular and injected groups */
  1285. /* Disable ADC peripheral */
  1286. tmp_hal_status = ADC_ConversionStop_Disable(hadc);
  1287. /* Check if ADC is effectively disabled */
  1288. if (tmp_hal_status == HAL_OK)
  1289. {
  1290. /* Disable ADC end of conversion interrupt for regular group */
  1291. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC);
  1292. /* Set ADC state */
  1293. ADC_STATE_CLR_SET(hadc->State,
  1294. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  1295. HAL_ADC_STATE_READY);
  1296. }
  1297. /* Process unlocked */
  1298. __HAL_UNLOCK(hadc);
  1299. /* Return function status */
  1300. return tmp_hal_status;
  1301. }
  1302. /**
  1303. * @brief Enables ADC, starts conversion of regular group and transfers result
  1304. * through DMA.
  1305. * Interruptions enabled in this function:
  1306. * - DMA transfer complete
  1307. * - DMA half transfer
  1308. * Each of these interruptions has its dedicated callback function.
  1309. * @note For devices with several ADCs: This function is for single-ADC mode
  1310. * only. For multimode, use the dedicated MultimodeStart function.
  1311. * @note On STM32F1 devices, only ADC1 and ADC3 (ADC availability depending
  1312. * on devices) have DMA capability.
  1313. * ADC2 converted data can be transferred in dual ADC mode using DMA
  1314. * of ADC1 (ADC master in multimode).
  1315. * In case of using ADC1 with DMA on a device featuring 2 ADC
  1316. * instances: ADC1 conversion register DR contains ADC1 conversion
  1317. * result (ADC1 register DR bits 0 to 11) and, additionally, ADC2 last
  1318. * conversion result (ADC1 register DR bits 16 to 27). Therefore, to
  1319. * have DMA transferring the conversion results of ADC1 only, DMA must
  1320. * be configured to transfer size: half word.
  1321. * @param hadc: ADC handle
  1322. * @param pData: The destination Buffer address.
  1323. * @param Length: The length of data to be transferred from ADC peripheral to memory.
  1324. * @retval None
  1325. */
  1326. HAL_StatusTypeDef HAL_ADC_Start_DMA(ADC_HandleTypeDef* hadc, uint32_t* pData, uint32_t Length)
  1327. {
  1328. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1329. /* Check the parameters */
  1330. assert_param(IS_ADC_DMA_CAPABILITY_INSTANCE(hadc->Instance));
  1331. /* Verification if multimode is disabled (for devices with several ADC) */
  1332. /* If multimode is enabled, dedicated function multimode conversion */
  1333. /* start DMA must be used. */
  1334. if(ADC_MULTIMODE_IS_ENABLE(hadc) == RESET)
  1335. {
  1336. /* Process locked */
  1337. __HAL_LOCK(hadc);
  1338. /* Enable the ADC peripheral */
  1339. tmp_hal_status = ADC_Enable(hadc);
  1340. /* Start conversion if ADC is effectively enabled */
  1341. if (tmp_hal_status == HAL_OK)
  1342. {
  1343. /* Set ADC state */
  1344. /* - Clear state bitfield related to regular group conversion results */
  1345. /* - Set state bitfield related to regular operation */
  1346. ADC_STATE_CLR_SET(hadc->State,
  1347. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP,
  1348. HAL_ADC_STATE_REG_BUSY);
  1349. /* Set group injected state (from auto-injection) and multimode state */
  1350. /* for all cases of multimode: independent mode, multimode ADC master */
  1351. /* or multimode ADC slave (for devices with several ADCs): */
  1352. if (ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc))
  1353. {
  1354. /* Set ADC state (ADC independent or master) */
  1355. CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  1356. /* If conversions on group regular are also triggering group injected, */
  1357. /* update ADC state. */
  1358. if (READ_BIT(hadc->Instance->CR1, ADC_CR1_JAUTO) != RESET)
  1359. {
  1360. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
  1361. }
  1362. }
  1363. else
  1364. {
  1365. /* Set ADC state (ADC slave) */
  1366. SET_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  1367. /* If conversions on group regular are also triggering group injected, */
  1368. /* update ADC state. */
  1369. if (ADC_MULTIMODE_AUTO_INJECTED(hadc))
  1370. {
  1371. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
  1372. }
  1373. }
  1374. /* State machine update: Check if an injected conversion is ongoing */
  1375. if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  1376. {
  1377. /* Reset ADC error code fields related to conversions on group regular */
  1378. CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA));
  1379. }
  1380. else
  1381. {
  1382. /* Reset ADC all error code fields */
  1383. ADC_CLEAR_ERRORCODE(hadc);
  1384. }
  1385. /* Process unlocked */
  1386. /* Unlock before starting ADC conversions: in case of potential */
  1387. /* interruption, to let the process to ADC IRQ Handler. */
  1388. __HAL_UNLOCK(hadc);
  1389. /* Set the DMA transfer complete callback */
  1390. hadc->DMA_Handle->XferCpltCallback = ADC_DMAConvCplt;
  1391. /* Set the DMA half transfer complete callback */
  1392. hadc->DMA_Handle->XferHalfCpltCallback = ADC_DMAHalfConvCplt;
  1393. /* Set the DMA error callback */
  1394. hadc->DMA_Handle->XferErrorCallback = ADC_DMAError;
  1395. /* Manage ADC and DMA start: ADC overrun interruption, DMA start, ADC */
  1396. /* start (in case of SW start): */
  1397. /* Clear regular group conversion flag and overrun flag */
  1398. /* (To ensure of no unknown state from potential previous ADC */
  1399. /* operations) */
  1400. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOC);
  1401. /* Enable ADC DMA mode */
  1402. SET_BIT(hadc->Instance->CR2, ADC_CR2_DMA);
  1403. /* Start the DMA channel */
  1404. HAL_DMA_Start_IT(hadc->DMA_Handle, (uint32_t)&hadc->Instance->DR, (uint32_t)pData, Length);
  1405. /* Enable conversion of regular group. */
  1406. /* If software start has been selected, conversion starts immediately. */
  1407. /* If external trigger has been selected, conversion will start at next */
  1408. /* trigger event. */
  1409. if (ADC_IS_SOFTWARE_START_REGULAR(hadc))
  1410. {
  1411. /* Start ADC conversion on regular group with SW start */
  1412. SET_BIT(hadc->Instance->CR2, (ADC_CR2_SWSTART | ADC_CR2_EXTTRIG));
  1413. }
  1414. else
  1415. {
  1416. /* Start ADC conversion on regular group with external trigger */
  1417. SET_BIT(hadc->Instance->CR2, ADC_CR2_EXTTRIG);
  1418. }
  1419. }
  1420. else
  1421. {
  1422. /* Process unlocked */
  1423. __HAL_UNLOCK(hadc);
  1424. }
  1425. }
  1426. else
  1427. {
  1428. tmp_hal_status = HAL_ERROR;
  1429. }
  1430. /* Return function status */
  1431. return tmp_hal_status;
  1432. }
  1433. /**
  1434. * @brief Stop ADC conversion of regular group (and injected group in
  1435. * case of auto_injection mode), disable ADC DMA transfer, disable
  1436. * ADC peripheral.
  1437. * @note: ADC peripheral disable is forcing stop of potential
  1438. * conversion on injected group. If injected group is under use, it
  1439. * should be preliminarily stopped using HAL_ADCEx_InjectedStop function.
  1440. * @note For devices with several ADCs: This function is for single-ADC mode
  1441. * only. For multimode, use the dedicated MultimodeStop function.
  1442. * @note On STM32F1 devices, only ADC1 and ADC3 (ADC availability depending
  1443. * on devices) have DMA capability.
  1444. * @param hadc: ADC handle
  1445. * @retval HAL status.
  1446. */
  1447. HAL_StatusTypeDef HAL_ADC_Stop_DMA(ADC_HandleTypeDef* hadc)
  1448. {
  1449. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1450. /* Check the parameters */
  1451. assert_param(IS_ADC_DMA_CAPABILITY_INSTANCE(hadc->Instance));
  1452. /* Process locked */
  1453. __HAL_LOCK(hadc);
  1454. /* Stop potential conversion on going, on regular and injected groups */
  1455. /* Disable ADC peripheral */
  1456. tmp_hal_status = ADC_ConversionStop_Disable(hadc);
  1457. /* Check if ADC is effectively disabled */
  1458. if (tmp_hal_status == HAL_OK)
  1459. {
  1460. /* Disable ADC DMA mode */
  1461. CLEAR_BIT(hadc->Instance->CR2, ADC_CR2_DMA);
  1462. /* Disable the DMA channel (in case of DMA in circular mode or stop while */
  1463. /* DMA transfer is on going) */
  1464. if (hadc->DMA_Handle->State == HAL_DMA_STATE_BUSY)
  1465. {
  1466. tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
  1467. /* Check if DMA channel effectively disabled */
  1468. if (tmp_hal_status == HAL_OK)
  1469. {
  1470. /* Set ADC state */
  1471. ADC_STATE_CLR_SET(hadc->State,
  1472. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  1473. HAL_ADC_STATE_READY);
  1474. }
  1475. else
  1476. {
  1477. /* Update ADC state machine to error */
  1478. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  1479. }
  1480. }
  1481. }
  1482. /* Process unlocked */
  1483. __HAL_UNLOCK(hadc);
  1484. /* Return function status */
  1485. return tmp_hal_status;
  1486. }
  1487. /**
  1488. * @brief Get ADC regular group conversion result.
  1489. * @note Reading register DR automatically clears ADC flag EOC
  1490. * (ADC group regular end of unitary conversion).
  1491. * @note This function does not clear ADC flag EOS
  1492. * (ADC group regular end of sequence conversion).
  1493. * Occurrence of flag EOS rising:
  1494. * - If sequencer is composed of 1 rank, flag EOS is equivalent
  1495. * to flag EOC.
  1496. * - If sequencer is composed of several ranks, during the scan
  1497. * sequence flag EOC only is raised, at the end of the scan sequence
  1498. * both flags EOC and EOS are raised.
  1499. * To clear this flag, either use function:
  1500. * in programming model IT: @ref HAL_ADC_IRQHandler(), in programming
  1501. * model polling: @ref HAL_ADC_PollForConversion()
  1502. * or @ref __HAL_ADC_CLEAR_FLAG(&hadc, ADC_FLAG_EOS).
  1503. * @param hadc: ADC handle
  1504. * @retval ADC group regular conversion data
  1505. */
  1506. uint32_t HAL_ADC_GetValue(ADC_HandleTypeDef* hadc)
  1507. {
  1508. /* Check the parameters */
  1509. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1510. /* Note: EOC flag is not cleared here by software because automatically */
  1511. /* cleared by hardware when reading register DR. */
  1512. /* Return ADC converted value */
  1513. return hadc->Instance->DR;
  1514. }
  1515. /**
  1516. * @brief Handles ADC interrupt request
  1517. * @param hadc: ADC handle
  1518. * @retval None
  1519. */
  1520. void HAL_ADC_IRQHandler(ADC_HandleTypeDef* hadc)
  1521. {
  1522. /* Check the parameters */
  1523. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1524. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
  1525. assert_param(IS_ADC_REGULAR_NB_CONV(hadc->Init.NbrOfConversion));
  1526. /* ========== Check End of Conversion flag for regular group ========== */
  1527. if(__HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_EOC))
  1528. {
  1529. if(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOC) )
  1530. {
  1531. /* Update state machine on conversion status if not in error state */
  1532. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL))
  1533. {
  1534. /* Set ADC state */
  1535. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  1536. }
  1537. /* Determine whether any further conversion upcoming on group regular */
  1538. /* by external trigger, continuous mode or scan sequence on going. */
  1539. /* Note: On STM32F1 devices, in case of sequencer enabled */
  1540. /* (several ranks selected), end of conversion flag is raised */
  1541. /* at the end of the sequence. */
  1542. if(ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  1543. (hadc->Init.ContinuousConvMode == DISABLE) )
  1544. {
  1545. /* Disable ADC end of conversion interrupt on group regular */
  1546. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC);
  1547. /* Set ADC state */
  1548. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  1549. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  1550. {
  1551. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  1552. }
  1553. }
  1554. /* Conversion complete callback */
  1555. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  1556. hadc->ConvCpltCallback(hadc);
  1557. #else
  1558. HAL_ADC_ConvCpltCallback(hadc);
  1559. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  1560. /* Clear regular group conversion flag */
  1561. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_STRT | ADC_FLAG_EOC);
  1562. }
  1563. }
  1564. /* ========== Check End of Conversion flag for injected group ========== */
  1565. if(__HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_JEOC))
  1566. {
  1567. if(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_JEOC))
  1568. {
  1569. /* Update state machine on conversion status if not in error state */
  1570. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL))
  1571. {
  1572. /* Set ADC state */
  1573. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_EOC);
  1574. }
  1575. /* Determine whether any further conversion upcoming on group injected */
  1576. /* by external trigger, scan sequence on going or by automatic injected */
  1577. /* conversion from group regular (same conditions as group regular */
  1578. /* interruption disabling above). */
  1579. /* Note: On STM32F1 devices, in case of sequencer enabled */
  1580. /* (several ranks selected), end of conversion flag is raised */
  1581. /* at the end of the sequence. */
  1582. if(ADC_IS_SOFTWARE_START_INJECTED(hadc) ||
  1583. (HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO) &&
  1584. (ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  1585. (hadc->Init.ContinuousConvMode == DISABLE) ) ) )
  1586. {
  1587. /* Disable ADC end of conversion interrupt on group injected */
  1588. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC);
  1589. /* Set ADC state */
  1590. CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  1591. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_REG_BUSY))
  1592. {
  1593. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  1594. }
  1595. }
  1596. /* Conversion complete callback */
  1597. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  1598. hadc->InjectedConvCpltCallback(hadc);
  1599. #else
  1600. HAL_ADCEx_InjectedConvCpltCallback(hadc);
  1601. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  1602. /* Clear injected group conversion flag */
  1603. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_JSTRT | ADC_FLAG_JEOC));
  1604. }
  1605. }
  1606. /* ========== Check Analog watchdog flags ========== */
  1607. if(__HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_AWD))
  1608. {
  1609. if(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_AWD))
  1610. {
  1611. /* Set ADC state */
  1612. SET_BIT(hadc->State, HAL_ADC_STATE_AWD1);
  1613. /* Level out of window callback */
  1614. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  1615. hadc->LevelOutOfWindowCallback(hadc);
  1616. #else
  1617. HAL_ADC_LevelOutOfWindowCallback(hadc);
  1618. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  1619. /* Clear the ADC analog watchdog flag */
  1620. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD);
  1621. }
  1622. }
  1623. }
  1624. /**
  1625. * @brief Conversion complete callback in non blocking mode
  1626. * @param hadc: ADC handle
  1627. * @retval None
  1628. */
  1629. __weak void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
  1630. {
  1631. /* Prevent unused argument(s) compilation warning */
  1632. UNUSED(hadc);
  1633. /* NOTE : This function should not be modified. When the callback is needed,
  1634. function HAL_ADC_ConvCpltCallback must be implemented in the user file.
  1635. */
  1636. }
  1637. /**
  1638. * @brief Conversion DMA half-transfer callback in non blocking mode
  1639. * @param hadc: ADC handle
  1640. * @retval None
  1641. */
  1642. __weak void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc)
  1643. {
  1644. /* Prevent unused argument(s) compilation warning */
  1645. UNUSED(hadc);
  1646. /* NOTE : This function should not be modified. When the callback is needed,
  1647. function HAL_ADC_ConvHalfCpltCallback must be implemented in the user file.
  1648. */
  1649. }
  1650. /**
  1651. * @brief Analog watchdog callback in non blocking mode.
  1652. * @param hadc: ADC handle
  1653. * @retval None
  1654. */
  1655. __weak void HAL_ADC_LevelOutOfWindowCallback(ADC_HandleTypeDef* hadc)
  1656. {
  1657. /* Prevent unused argument(s) compilation warning */
  1658. UNUSED(hadc);
  1659. /* NOTE : This function should not be modified. When the callback is needed,
  1660. function HAL_ADC_LevelOutOfWindowCallback must be implemented in the user file.
  1661. */
  1662. }
  1663. /**
  1664. * @brief ADC error callback in non blocking mode
  1665. * (ADC conversion with interruption or transfer by DMA)
  1666. * @param hadc: ADC handle
  1667. * @retval None
  1668. */
  1669. __weak void HAL_ADC_ErrorCallback(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_ErrorCallback must be implemented in the user file.
  1675. */
  1676. }
  1677. /**
  1678. * @}
  1679. */
  1680. /** @defgroup ADC_Exported_Functions_Group3 Peripheral Control functions
  1681. * @brief Peripheral Control functions
  1682. *
  1683. @verbatim
  1684. ===============================================================================
  1685. ##### Peripheral Control functions #####
  1686. ===============================================================================
  1687. [..] This section provides functions allowing to:
  1688. (+) Configure channels on regular group
  1689. (+) Configure the analog watchdog
  1690. @endverbatim
  1691. * @{
  1692. */
  1693. /**
  1694. * @brief Configures the the selected channel to be linked to the regular
  1695. * group.
  1696. * @note In case of usage of internal measurement channels:
  1697. * Vbat/VrefInt/TempSensor.
  1698. * These internal paths can be be disabled using function
  1699. * HAL_ADC_DeInit().
  1700. * @note Possibility to update parameters on the fly:
  1701. * This function initializes channel into regular group, following
  1702. * calls to this function can be used to reconfigure some parameters
  1703. * of structure "ADC_ChannelConfTypeDef" on the fly, without reseting
  1704. * the ADC.
  1705. * The setting of these parameters is conditioned to ADC state.
  1706. * For parameters constraints, see comments of structure
  1707. * "ADC_ChannelConfTypeDef".
  1708. * @param hadc: ADC handle
  1709. * @param sConfig: Structure of ADC channel for regular group.
  1710. * @retval HAL status
  1711. */
  1712. HAL_StatusTypeDef HAL_ADC_ConfigChannel(ADC_HandleTypeDef* hadc, ADC_ChannelConfTypeDef* sConfig)
  1713. {
  1714. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1715. __IO uint32_t wait_loop_index = 0U;
  1716. /* Check the parameters */
  1717. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1718. assert_param(IS_ADC_CHANNEL(sConfig->Channel));
  1719. assert_param(IS_ADC_REGULAR_RANK(sConfig->Rank));
  1720. assert_param(IS_ADC_SAMPLE_TIME(sConfig->SamplingTime));
  1721. /* Process locked */
  1722. __HAL_LOCK(hadc);
  1723. /* Regular sequence configuration */
  1724. /* For Rank 1 to 6 */
  1725. if (sConfig->Rank < 7U)
  1726. {
  1727. MODIFY_REG(hadc->Instance->SQR3 ,
  1728. ADC_SQR3_RK(ADC_SQR3_SQ1, sConfig->Rank) ,
  1729. ADC_SQR3_RK(sConfig->Channel, sConfig->Rank) );
  1730. }
  1731. /* For Rank 7 to 12 */
  1732. else if (sConfig->Rank < 13U)
  1733. {
  1734. MODIFY_REG(hadc->Instance->SQR2 ,
  1735. ADC_SQR2_RK(ADC_SQR2_SQ7, sConfig->Rank) ,
  1736. ADC_SQR2_RK(sConfig->Channel, sConfig->Rank) );
  1737. }
  1738. /* For Rank 13 to 16 */
  1739. else
  1740. {
  1741. MODIFY_REG(hadc->Instance->SQR1 ,
  1742. ADC_SQR1_RK(ADC_SQR1_SQ13, sConfig->Rank) ,
  1743. ADC_SQR1_RK(sConfig->Channel, sConfig->Rank) );
  1744. }
  1745. /* Channel sampling time configuration */
  1746. /* For channels 10 to 17 */
  1747. if (sConfig->Channel >= ADC_CHANNEL_10)
  1748. {
  1749. MODIFY_REG(hadc->Instance->SMPR1 ,
  1750. ADC_SMPR1(ADC_SMPR1_SMP10, sConfig->Channel) ,
  1751. ADC_SMPR1(sConfig->SamplingTime, sConfig->Channel) );
  1752. }
  1753. else /* For channels 0 to 9 */
  1754. {
  1755. MODIFY_REG(hadc->Instance->SMPR2 ,
  1756. ADC_SMPR2(ADC_SMPR2_SMP0, sConfig->Channel) ,
  1757. ADC_SMPR2(sConfig->SamplingTime, sConfig->Channel) );
  1758. }
  1759. /* If ADC1 Channel_16 or Channel_17 is selected, enable Temperature sensor */
  1760. /* and VREFINT measurement path. */
  1761. if ((sConfig->Channel == ADC_CHANNEL_TEMPSENSOR) ||
  1762. (sConfig->Channel == ADC_CHANNEL_VREFINT) )
  1763. {
  1764. /* For STM32F1 devices with several ADC: Only ADC1 can access internal */
  1765. /* measurement channels (VrefInt/TempSensor). If these channels are */
  1766. /* intended to be set on other ADC instances, an error is reported. */
  1767. if (hadc->Instance == ADC1)
  1768. {
  1769. if (READ_BIT(hadc->Instance->CR2, ADC_CR2_TSVREFE) == RESET)
  1770. {
  1771. SET_BIT(hadc->Instance->CR2, ADC_CR2_TSVREFE);
  1772. if (sConfig->Channel == ADC_CHANNEL_TEMPSENSOR)
  1773. {
  1774. /* Delay for temperature sensor stabilization time */
  1775. /* Compute number of CPU cycles to wait for */
  1776. wait_loop_index = (ADC_TEMPSENSOR_DELAY_US * (SystemCoreClock / 1000000U));
  1777. while(wait_loop_index != 0U)
  1778. {
  1779. wait_loop_index--;
  1780. }
  1781. }
  1782. }
  1783. }
  1784. else
  1785. {
  1786. /* Update ADC state machine to error */
  1787. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1788. tmp_hal_status = HAL_ERROR;
  1789. }
  1790. }
  1791. /* Process unlocked */
  1792. __HAL_UNLOCK(hadc);
  1793. /* Return function status */
  1794. return tmp_hal_status;
  1795. }
  1796. /**
  1797. * @brief Configures the analog watchdog.
  1798. * @note Analog watchdog thresholds can be modified while ADC conversion
  1799. * is on going.
  1800. * In this case, some constraints must be taken into account:
  1801. * the programmed threshold values are effective from the next
  1802. * ADC EOC (end of unitary conversion).
  1803. * Considering that registers write delay may happen due to
  1804. * bus activity, this might cause an uncertainty on the
  1805. * effective timing of the new programmed threshold values.
  1806. * @param hadc: ADC handle
  1807. * @param AnalogWDGConfig: Structure of ADC analog watchdog configuration
  1808. * @retval HAL status
  1809. */
  1810. HAL_StatusTypeDef HAL_ADC_AnalogWDGConfig(ADC_HandleTypeDef* hadc, ADC_AnalogWDGConfTypeDef* AnalogWDGConfig)
  1811. {
  1812. /* Check the parameters */
  1813. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1814. assert_param(IS_ADC_ANALOG_WATCHDOG_MODE(AnalogWDGConfig->WatchdogMode));
  1815. assert_param(IS_FUNCTIONAL_STATE(AnalogWDGConfig->ITMode));
  1816. assert_param(IS_ADC_RANGE(AnalogWDGConfig->HighThreshold));
  1817. assert_param(IS_ADC_RANGE(AnalogWDGConfig->LowThreshold));
  1818. if((AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_REG) ||
  1819. (AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_INJEC) ||
  1820. (AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_REGINJEC) )
  1821. {
  1822. assert_param(IS_ADC_CHANNEL(AnalogWDGConfig->Channel));
  1823. }
  1824. /* Process locked */
  1825. __HAL_LOCK(hadc);
  1826. /* Analog watchdog configuration */
  1827. /* Configure ADC Analog watchdog interrupt */
  1828. if(AnalogWDGConfig->ITMode == ENABLE)
  1829. {
  1830. /* Enable the ADC Analog watchdog interrupt */
  1831. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_AWD);
  1832. }
  1833. else
  1834. {
  1835. /* Disable the ADC Analog watchdog interrupt */
  1836. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_AWD);
  1837. }
  1838. /* Configuration of analog watchdog: */
  1839. /* - Set the analog watchdog enable mode: regular and/or injected groups, */
  1840. /* one or all channels. */
  1841. /* - Set the Analog watchdog channel (is not used if watchdog */
  1842. /* mode "all channels": ADC_CFGR_AWD1SGL=0). */
  1843. MODIFY_REG(hadc->Instance->CR1 ,
  1844. ADC_CR1_AWDSGL |
  1845. ADC_CR1_JAWDEN |
  1846. ADC_CR1_AWDEN |
  1847. ADC_CR1_AWDCH ,
  1848. AnalogWDGConfig->WatchdogMode |
  1849. AnalogWDGConfig->Channel );
  1850. /* Set the high threshold */
  1851. WRITE_REG(hadc->Instance->HTR, AnalogWDGConfig->HighThreshold);
  1852. /* Set the low threshold */
  1853. WRITE_REG(hadc->Instance->LTR, AnalogWDGConfig->LowThreshold);
  1854. /* Process unlocked */
  1855. __HAL_UNLOCK(hadc);
  1856. /* Return function status */
  1857. return HAL_OK;
  1858. }
  1859. /**
  1860. * @}
  1861. */
  1862. /** @defgroup ADC_Exported_Functions_Group4 Peripheral State functions
  1863. * @brief Peripheral State functions
  1864. *
  1865. @verbatim
  1866. ===============================================================================
  1867. ##### Peripheral State and Errors functions #####
  1868. ===============================================================================
  1869. [..]
  1870. This subsection provides functions to get in run-time the status of the
  1871. peripheral.
  1872. (+) Check the ADC state
  1873. (+) Check the ADC error code
  1874. @endverbatim
  1875. * @{
  1876. */
  1877. /**
  1878. * @brief return the ADC state
  1879. * @param hadc: ADC handle
  1880. * @retval HAL state
  1881. */
  1882. uint32_t HAL_ADC_GetState(ADC_HandleTypeDef* hadc)
  1883. {
  1884. /* Return ADC state */
  1885. return hadc->State;
  1886. }
  1887. /**
  1888. * @brief Return the ADC error code
  1889. * @param hadc: ADC handle
  1890. * @retval ADC Error Code
  1891. */
  1892. uint32_t HAL_ADC_GetError(ADC_HandleTypeDef *hadc)
  1893. {
  1894. return hadc->ErrorCode;
  1895. }
  1896. /**
  1897. * @}
  1898. */
  1899. /**
  1900. * @}
  1901. */
  1902. /** @defgroup ADC_Private_Functions ADC Private Functions
  1903. * @{
  1904. */
  1905. /**
  1906. * @brief Enable the selected ADC.
  1907. * @note Prerequisite condition to use this function: ADC must be disabled
  1908. * and voltage regulator must be enabled (done into HAL_ADC_Init()).
  1909. * @param hadc: ADC handle
  1910. * @retval HAL status.
  1911. */
  1912. HAL_StatusTypeDef ADC_Enable(ADC_HandleTypeDef* hadc)
  1913. {
  1914. uint32_t tickstart = 0U;
  1915. __IO uint32_t wait_loop_index = 0U;
  1916. /* ADC enable and wait for ADC ready (in case of ADC is disabled or */
  1917. /* enabling phase not yet completed: flag ADC ready not yet set). */
  1918. /* Timeout implemented to not be stuck if ADC cannot be enabled (possible */
  1919. /* causes: ADC clock not running, ...). */
  1920. if (ADC_IS_ENABLE(hadc) == RESET)
  1921. {
  1922. /* Enable the Peripheral */
  1923. __HAL_ADC_ENABLE(hadc);
  1924. /* Delay for ADC stabilization time */
  1925. /* Compute number of CPU cycles to wait for */
  1926. wait_loop_index = (ADC_STAB_DELAY_US * (SystemCoreClock / 1000000U));
  1927. while(wait_loop_index != 0U)
  1928. {
  1929. wait_loop_index--;
  1930. }
  1931. /* Get tick count */
  1932. tickstart = HAL_GetTick();
  1933. /* Wait for ADC effectively enabled */
  1934. while(ADC_IS_ENABLE(hadc) == RESET)
  1935. {
  1936. if((HAL_GetTick() - tickstart) > ADC_ENABLE_TIMEOUT)
  1937. {
  1938. /* New check to avoid false timeout detection in case of preemption */
  1939. if(ADC_IS_ENABLE(hadc) == RESET)
  1940. {
  1941. /* Update ADC state machine to error */
  1942. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  1943. /* Set ADC error code to ADC IP internal error */
  1944. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  1945. /* Process unlocked */
  1946. __HAL_UNLOCK(hadc);
  1947. return HAL_ERROR;
  1948. }
  1949. }
  1950. }
  1951. }
  1952. /* Return HAL status */
  1953. return HAL_OK;
  1954. }
  1955. /**
  1956. * @brief Stop ADC conversion and disable the selected ADC
  1957. * @note Prerequisite condition to use this function: ADC conversions must be
  1958. * stopped to disable the ADC.
  1959. * @param hadc: ADC handle
  1960. * @retval HAL status.
  1961. */
  1962. HAL_StatusTypeDef ADC_ConversionStop_Disable(ADC_HandleTypeDef* hadc)
  1963. {
  1964. uint32_t tickstart = 0U;
  1965. /* Verification if ADC is not already disabled */
  1966. if (ADC_IS_ENABLE(hadc) != RESET)
  1967. {
  1968. /* Disable the ADC peripheral */
  1969. __HAL_ADC_DISABLE(hadc);
  1970. /* Get tick count */
  1971. tickstart = HAL_GetTick();
  1972. /* Wait for ADC effectively disabled */
  1973. while(ADC_IS_ENABLE(hadc) != RESET)
  1974. {
  1975. if((HAL_GetTick() - tickstart) > ADC_DISABLE_TIMEOUT)
  1976. {
  1977. /* New check to avoid false timeout detection in case of preemption */
  1978. if(ADC_IS_ENABLE(hadc) != RESET)
  1979. {
  1980. /* Update ADC state machine to error */
  1981. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  1982. /* Set ADC error code to ADC IP internal error */
  1983. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  1984. return HAL_ERROR;
  1985. }
  1986. }
  1987. }
  1988. }
  1989. /* Return HAL status */
  1990. return HAL_OK;
  1991. }
  1992. /**
  1993. * @brief DMA transfer complete callback.
  1994. * @param hdma: pointer to DMA handle.
  1995. * @retval None
  1996. */
  1997. void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma)
  1998. {
  1999. /* Retrieve ADC handle corresponding to current DMA handle */
  2000. ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  2001. /* Update state machine on conversion status if not in error state */
  2002. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL | HAL_ADC_STATE_ERROR_DMA))
  2003. {
  2004. /* Update ADC state machine */
  2005. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  2006. /* Determine whether any further conversion upcoming on group regular */
  2007. /* by external trigger, continuous mode or scan sequence on going. */
  2008. /* Note: On STM32F1 devices, in case of sequencer enabled */
  2009. /* (several ranks selected), end of conversion flag is raised */
  2010. /* at the end of the sequence. */
  2011. if(ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  2012. (hadc->Init.ContinuousConvMode == DISABLE) )
  2013. {
  2014. /* Set ADC state */
  2015. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  2016. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  2017. {
  2018. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  2019. }
  2020. }
  2021. /* Conversion complete callback */
  2022. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2023. hadc->ConvCpltCallback(hadc);
  2024. #else
  2025. HAL_ADC_ConvCpltCallback(hadc);
  2026. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2027. }
  2028. else
  2029. {
  2030. /* Call DMA error callback */
  2031. hadc->DMA_Handle->XferErrorCallback(hdma);
  2032. }
  2033. }
  2034. /**
  2035. * @brief DMA half transfer complete callback.
  2036. * @param hdma: pointer to DMA handle.
  2037. * @retval None
  2038. */
  2039. void ADC_DMAHalfConvCplt(DMA_HandleTypeDef *hdma)
  2040. {
  2041. /* Retrieve ADC handle corresponding to current DMA handle */
  2042. ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  2043. /* Half conversion callback */
  2044. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2045. hadc->ConvHalfCpltCallback(hadc);
  2046. #else
  2047. HAL_ADC_ConvHalfCpltCallback(hadc);
  2048. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2049. }
  2050. /**
  2051. * @brief DMA error callback
  2052. * @param hdma: pointer to DMA handle.
  2053. * @retval None
  2054. */
  2055. void ADC_DMAError(DMA_HandleTypeDef *hdma)
  2056. {
  2057. /* Retrieve ADC handle corresponding to current DMA handle */
  2058. ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  2059. /* Set ADC state */
  2060. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  2061. /* Set ADC error code to DMA error */
  2062. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_DMA);
  2063. /* Error callback */
  2064. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2065. hadc->ErrorCallback(hadc);
  2066. #else
  2067. HAL_ADC_ErrorCallback(hadc);
  2068. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2069. }
  2070. /**
  2071. * @}
  2072. */
  2073. #endif /* HAL_ADC_MODULE_ENABLED */
  2074. /**
  2075. * @}
  2076. */
  2077. /**
  2078. * @}
  2079. */
  2080. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/