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.
 
 
 
 
 
 

2384 lines
87 KiB

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