stm32f4xx_hal_adc.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_adc.c
  4. * @author MCD Application Team
  5. * @version V1.6.0
  6. * @date 04-November-2016
  7. * @brief This file provides firmware functions to manage the following
  8. * functionalities of the Analog to Digital Convertor (ADC) peripheral:
  9. * + Initialization and de-initialization functions
  10. * + IO operation functions
  11. * + State and errors functions
  12. *
  13. @verbatim
  14. ==============================================================================
  15. ##### ADC Peripheral features #####
  16. ==============================================================================
  17. [..]
  18. (#) 12-bit, 10-bit, 8-bit or 6-bit configurable resolution.
  19. (#) Interrupt generation at the end of conversion, end of injected conversion,
  20. and in case of analog watchdog or overrun events
  21. (#) Single and continuous conversion modes.
  22. (#) Scan mode for automatic conversion of channel 0 to channel x.
  23. (#) Data alignment with in-built data coherency.
  24. (#) Channel-wise programmable sampling time.
  25. (#) External trigger option with configurable polarity for both regular and
  26. injected conversion.
  27. (#) Dual/Triple mode (on devices with 2 ADCs or more).
  28. (#) Configurable DMA data storage in Dual/Triple ADC mode.
  29. (#) Configurable delay between conversions in Dual/Triple interleaved mode.
  30. (#) ADC conversion type (refer to the datasheets).
  31. (#) ADC supply requirements: 2.4 V to 3.6 V at full speed and down to 1.8 V at
  32. slower speed.
  33. (#) ADC input range: VREF(minus) = VIN = VREF(plus).
  34. (#) DMA request generation during regular channel conversion.
  35. ##### How to use this driver #####
  36. ==============================================================================
  37. [..]
  38. (#)Initialize the ADC low level resources by implementing the HAL_ADC_MspInit():
  39. (##) Enable the ADC interface clock using __HAL_RCC_ADC_CLK_ENABLE()
  40. (##) ADC pins configuration
  41. (+++) Enable the clock for the ADC GPIOs using the following function:
  42. __HAL_RCC_GPIOx_CLK_ENABLE()
  43. (+++) Configure these ADC pins in analog mode using HAL_GPIO_Init()
  44. (##) In case of using interrupts (e.g. HAL_ADC_Start_IT())
  45. (+++) Configure the ADC interrupt priority using HAL_NVIC_SetPriority()
  46. (+++) Enable the ADC IRQ handler using HAL_NVIC_EnableIRQ()
  47. (+++) In ADC IRQ handler, call HAL_ADC_IRQHandler()
  48. (##) In case of using DMA to control data transfer (e.g. HAL_ADC_Start_DMA())
  49. (+++) Enable the DMAx interface clock using __HAL_RCC_DMAx_CLK_ENABLE()
  50. (+++) Configure and enable two DMA streams stream for managing data
  51. transfer from peripheral to memory (output stream)
  52. (+++) Associate the initialized DMA handle to the CRYP DMA handle
  53. using __HAL_LINKDMA()
  54. (+++) Configure the priority and enable the NVIC for the transfer complete
  55. interrupt on the two DMA Streams. The output stream should have higher
  56. priority than the input stream.
  57. *** Configuration of ADC, groups regular/injected, channels parameters ***
  58. ==============================================================================
  59. [..]
  60. (#) Configure the ADC parameters (resolution, data alignment, ...)
  61. and regular group parameters (conversion trigger, sequencer, ...)
  62. using function HAL_ADC_Init().
  63. (#) Configure the channels for regular group parameters (channel number,
  64. channel rank into sequencer, ..., into regular group)
  65. using function HAL_ADC_ConfigChannel().
  66. (#) Optionally, configure the injected group parameters (conversion trigger,
  67. sequencer, ..., of injected group)
  68. and the channels for injected group parameters (channel number,
  69. channel rank into sequencer, ..., into injected group)
  70. using function HAL_ADCEx_InjectedConfigChannel().
  71. (#) Optionally, configure the analog watchdog parameters (channels
  72. monitored, thresholds, ...) using function HAL_ADC_AnalogWDGConfig().
  73. (#) Optionally, for devices with several ADC instances: configure the
  74. multimode parameters using function HAL_ADCEx_MultiModeConfigChannel().
  75. *** Execution of ADC conversions ***
  76. ==============================================================================
  77. [..]
  78. (#) ADC driver can be used among three modes: polling, interruption,
  79. transfer by DMA.
  80. *** Polling mode IO operation ***
  81. =================================
  82. [..]
  83. (+) Start the ADC peripheral using HAL_ADC_Start()
  84. (+) Wait for end of conversion using HAL_ADC_PollForConversion(), at this stage
  85. user can specify the value of timeout according to his end application
  86. (+) To read the ADC converted values, use the HAL_ADC_GetValue() function.
  87. (+) Stop the ADC peripheral using HAL_ADC_Stop()
  88. *** Interrupt mode IO operation ***
  89. ===================================
  90. [..]
  91. (+) Start the ADC peripheral using HAL_ADC_Start_IT()
  92. (+) Use HAL_ADC_IRQHandler() called under ADC_IRQHandler() Interrupt subroutine
  93. (+) At ADC end of conversion HAL_ADC_ConvCpltCallback() function is executed and user can
  94. add his own code by customization of function pointer HAL_ADC_ConvCpltCallback
  95. (+) In case of ADC Error, HAL_ADC_ErrorCallback() function is executed and user can
  96. add his own code by customization of function pointer HAL_ADC_ErrorCallback
  97. (+) Stop the ADC peripheral using HAL_ADC_Stop_IT()
  98. *** DMA mode IO operation ***
  99. ==============================
  100. [..]
  101. (+) Start the ADC peripheral using HAL_ADC_Start_DMA(), at this stage the user specify the length
  102. of data to be transferred at each end of conversion
  103. (+) At The end of data transfer by HAL_ADC_ConvCpltCallback() function is executed and user can
  104. add his own code by customization of function pointer HAL_ADC_ConvCpltCallback
  105. (+) In case of transfer Error, HAL_ADC_ErrorCallback() function is executed and user can
  106. add his own code by customization of function pointer HAL_ADC_ErrorCallback
  107. (+) Stop the ADC peripheral using HAL_ADC_Stop_DMA()
  108. *** ADC HAL driver macros list ***
  109. =============================================
  110. [..]
  111. Below the list of most used macros in ADC HAL driver.
  112. (+) __HAL_ADC_ENABLE : Enable the ADC peripheral
  113. (+) __HAL_ADC_DISABLE : Disable the ADC peripheral
  114. (+) __HAL_ADC_ENABLE_IT: Enable the ADC end of conversion interrupt
  115. (+) __HAL_ADC_DISABLE_IT: Disable the ADC end of conversion interrupt
  116. (+) __HAL_ADC_GET_IT_SOURCE: Check if the specified ADC interrupt source is enabled or disabled
  117. (+) __HAL_ADC_CLEAR_FLAG: Clear the ADC's pending flags
  118. (+) __HAL_ADC_GET_FLAG: Get the selected ADC's flag status
  119. (+) ADC_GET_RESOLUTION: Return resolution bits in CR1 register
  120. [..]
  121. (@) You can refer to the ADC HAL driver header file for more useful macros
  122. *** Deinitialization of ADC ***
  123. ==============================================================================
  124. [..]
  125. (#) Disable the ADC interface
  126. (++) ADC clock can be hard reset and disabled at RCC top level.
  127. (++) Hard reset of ADC peripherals
  128. using macro __HAL_RCC_ADC_FORCE_RESET(), __HAL_RCC_ADC_RELEASE_RESET().
  129. (++) ADC clock disable using the equivalent macro/functions as configuration step.
  130. (+++) Example:
  131. Into HAL_ADC_MspDeInit() (recommended code location) or with
  132. other device clock parameters configuration:
  133. (+++) HAL_RCC_GetOscConfig(&RCC_OscInitStructure);
  134. (+++) RCC_OscInitStructure.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  135. (+++) RCC_OscInitStructure.HSIState = RCC_HSI_OFF; (if not used for system clock)
  136. (+++) HAL_RCC_OscConfig(&RCC_OscInitStructure);
  137. (#) ADC pins configuration
  138. (++) Disable the clock for the ADC GPIOs using macro __HAL_RCC_GPIOx_CLK_DISABLE()
  139. (#) Optionally, in case of usage of ADC with interruptions:
  140. (++) Disable the NVIC for ADC using function HAL_NVIC_DisableIRQ(ADCx_IRQn)
  141. (#) Optionally, in case of usage of DMA:
  142. (++) Deinitialize the DMA using function HAL_DMA_DeInit().
  143. (++) Disable the NVIC for DMA using function HAL_NVIC_DisableIRQ(DMAx_Channelx_IRQn)
  144. @endverbatim
  145. ******************************************************************************
  146. * @attention
  147. *
  148. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  149. *
  150. * Redistribution and use in source and binary forms, with or without modification,
  151. * are permitted provided that the following conditions are met:
  152. * 1. Redistributions of source code must retain the above copyright notice,
  153. * this list of conditions and the following disclaimer.
  154. * 2. Redistributions in binary form must reproduce the above copyright notice,
  155. * this list of conditions and the following disclaimer in the documentation
  156. * and/or other materials provided with the distribution.
  157. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  158. * may be used to endorse or promote products derived from this software
  159. * without specific prior written permission.
  160. *
  161. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  162. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  163. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  164. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  165. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  166. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  167. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  168. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  169. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  170. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  171. *
  172. ******************************************************************************
  173. */
  174. /* Includes ------------------------------------------------------------------*/
  175. #include "stm32f4xx_hal.h"
  176. /** @addtogroup STM32F4xx_HAL_Driver
  177. * @{
  178. */
  179. /** @defgroup ADC ADC
  180. * @brief ADC driver modules
  181. * @{
  182. */
  183. #ifdef HAL_ADC_MODULE_ENABLED
  184. /* Private typedef -----------------------------------------------------------*/
  185. /* Private define ------------------------------------------------------------*/
  186. /* Private macro -------------------------------------------------------------*/
  187. /* Private variables ---------------------------------------------------------*/
  188. /** @addtogroup ADC_Private_Functions
  189. * @{
  190. */
  191. /* Private function prototypes -----------------------------------------------*/
  192. static void ADC_Init(ADC_HandleTypeDef* hadc);
  193. static void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma);
  194. static void ADC_DMAError(DMA_HandleTypeDef *hdma);
  195. static void ADC_DMAHalfConvCplt(DMA_HandleTypeDef *hdma);
  196. /**
  197. * @}
  198. */
  199. /* Exported functions --------------------------------------------------------*/
  200. /** @defgroup ADC_Exported_Functions ADC Exported Functions
  201. * @{
  202. */
  203. /** @defgroup ADC_Exported_Functions_Group1 Initialization and de-initialization functions
  204. * @brief Initialization and Configuration functions
  205. *
  206. @verbatim
  207. ===============================================================================
  208. ##### Initialization and de-initialization functions #####
  209. ===============================================================================
  210. [..] This section provides functions allowing to:
  211. (+) Initialize and configure the ADC.
  212. (+) De-initialize the ADC.
  213. @endverbatim
  214. * @{
  215. */
  216. /**
  217. * @brief Initializes the ADCx peripheral according to the specified parameters
  218. * in the ADC_InitStruct and initializes the ADC MSP.
  219. *
  220. * @note This function is used to configure the global features of the ADC (
  221. * ClockPrescaler, Resolution, Data Alignment and number of conversion), however,
  222. * the rest of the configuration parameters are specific to the regular
  223. * channels group (scan mode activation, continuous mode activation,
  224. * External trigger source and edge, DMA continuous request after the
  225. * last transfer and End of conversion selection).
  226. *
  227. * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
  228. * the configuration information for the specified ADC.
  229. * @retval HAL status
  230. */
  231. HAL_StatusTypeDef HAL_ADC_Init(ADC_HandleTypeDef* hadc)
  232. {
  233. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  234. /* Check ADC handle */
  235. if(hadc == NULL)
  236. {
  237. return HAL_ERROR;
  238. }
  239. /* Check the parameters */
  240. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  241. assert_param(IS_ADC_CLOCKPRESCALER(hadc->Init.ClockPrescaler));
  242. assert_param(IS_ADC_RESOLUTION(hadc->Init.Resolution));
  243. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ScanConvMode));
  244. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
  245. assert_param(IS_ADC_EXT_TRIG(hadc->Init.ExternalTrigConv));
  246. assert_param(IS_ADC_DATA_ALIGN(hadc->Init.DataAlign));
  247. assert_param(IS_ADC_REGULAR_LENGTH(hadc->Init.NbrOfConversion));
  248. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DMAContinuousRequests));
  249. assert_param(IS_ADC_EOCSelection(hadc->Init.EOCSelection));
  250. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DiscontinuousConvMode));
  251. if(hadc->Init.ExternalTrigConv != ADC_SOFTWARE_START)
  252. {
  253. assert_param(IS_ADC_EXT_TRIG_EDGE(hadc->Init.ExternalTrigConvEdge));
  254. }
  255. if(hadc->State == HAL_ADC_STATE_RESET)
  256. {
  257. /* Initialize ADC error code */
  258. ADC_CLEAR_ERRORCODE(hadc);
  259. /* Allocate lock resource and initialize it */
  260. hadc->Lock = HAL_UNLOCKED;
  261. /* Init the low level hardware */
  262. HAL_ADC_MspInit(hadc);
  263. }
  264. /* Configuration of ADC parameters if previous preliminary actions are */
  265. /* correctly completed. */
  266. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL))
  267. {
  268. /* Set ADC state */
  269. ADC_STATE_CLR_SET(hadc->State,
  270. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  271. HAL_ADC_STATE_BUSY_INTERNAL);
  272. /* Set ADC parameters */
  273. ADC_Init(hadc);
  274. /* Set ADC error code to none */
  275. ADC_CLEAR_ERRORCODE(hadc);
  276. /* Set the ADC state */
  277. ADC_STATE_CLR_SET(hadc->State,
  278. HAL_ADC_STATE_BUSY_INTERNAL,
  279. HAL_ADC_STATE_READY);
  280. }
  281. else
  282. {
  283. tmp_hal_status = HAL_ERROR;
  284. }
  285. /* Release Lock */
  286. __HAL_UNLOCK(hadc);
  287. /* Return function status */
  288. return tmp_hal_status;
  289. }
  290. /**
  291. * @brief Deinitializes the ADCx peripheral registers to their default reset values.
  292. * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
  293. * the configuration information for the specified ADC.
  294. * @retval HAL status
  295. */
  296. HAL_StatusTypeDef HAL_ADC_DeInit(ADC_HandleTypeDef* hadc)
  297. {
  298. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  299. /* Check ADC handle */
  300. if(hadc == NULL)
  301. {
  302. return HAL_ERROR;
  303. }
  304. /* Check the parameters */
  305. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  306. /* Set ADC state */
  307. SET_BIT(hadc->State, HAL_ADC_STATE_BUSY_INTERNAL);
  308. /* Stop potential conversion on going, on regular and injected groups */
  309. /* Disable ADC peripheral */
  310. __HAL_ADC_DISABLE(hadc);
  311. /* Configuration of ADC parameters if previous preliminary actions are */
  312. /* correctly completed. */
  313. if(HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_ADON))
  314. {
  315. /* DeInit the low level hardware */
  316. HAL_ADC_MspDeInit(hadc);
  317. /* Set ADC error code to none */
  318. ADC_CLEAR_ERRORCODE(hadc);
  319. /* Set ADC state */
  320. hadc->State = HAL_ADC_STATE_RESET;
  321. }
  322. /* Process unlocked */
  323. __HAL_UNLOCK(hadc);
  324. /* Return function status */
  325. return tmp_hal_status;
  326. }
  327. /**
  328. * @brief Initializes the ADC MSP.
  329. * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
  330. * the configuration information for the specified ADC.
  331. * @retval None
  332. */
  333. __weak void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
  334. {
  335. /* Prevent unused argument(s) compilation warning */
  336. UNUSED(hadc);
  337. /* NOTE : This function Should not be modified, when the callback is needed,
  338. the HAL_ADC_MspInit could be implemented in the user file
  339. */
  340. }
  341. /**
  342. * @brief DeInitializes the ADC MSP.
  343. * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
  344. * the configuration information for the specified ADC.
  345. * @retval None
  346. */
  347. __weak void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc)
  348. {
  349. /* Prevent unused argument(s) compilation warning */
  350. UNUSED(hadc);
  351. /* NOTE : This function Should not be modified, when the callback is needed,
  352. the HAL_ADC_MspDeInit could be implemented in the user file
  353. */
  354. }
  355. /**
  356. * @}
  357. */
  358. /** @defgroup ADC_Exported_Functions_Group2 IO operation functions
  359. * @brief IO operation functions
  360. *
  361. @verbatim
  362. ===============================================================================
  363. ##### IO operation functions #####
  364. ===============================================================================
  365. [..] This section provides functions allowing to:
  366. (+) Start conversion of regular channel.
  367. (+) Stop conversion of regular channel.
  368. (+) Start conversion of regular channel and enable interrupt.
  369. (+) Stop conversion of regular channel and disable interrupt.
  370. (+) Start conversion of regular channel and enable DMA transfer.
  371. (+) Stop conversion of regular channel and disable DMA transfer.
  372. (+) Handle ADC interrupt request.
  373. @endverbatim
  374. * @{
  375. */
  376. /**
  377. * @brief Enables ADC and starts conversion of the regular channels.
  378. * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
  379. * the configuration information for the specified ADC.
  380. * @retval HAL status
  381. */
  382. HAL_StatusTypeDef HAL_ADC_Start(ADC_HandleTypeDef* hadc)
  383. {
  384. __IO uint32_t counter = 0U;
  385. /* Check the parameters */
  386. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
  387. assert_param(IS_ADC_EXT_TRIG_EDGE(hadc->Init.ExternalTrigConvEdge));
  388. /* Process locked */
  389. __HAL_LOCK(hadc);
  390. /* Enable the ADC peripheral */
  391. /* Check if ADC peripheral is disabled in order to enable it and wait during
  392. Tstab time the ADC's stabilization */
  393. if((hadc->Instance->CR2 & ADC_CR2_ADON) != ADC_CR2_ADON)
  394. {
  395. /* Enable the Peripheral */
  396. __HAL_ADC_ENABLE(hadc);
  397. /* Delay for ADC stabilization time */
  398. /* Compute number of CPU cycles to wait for */
  399. counter = (ADC_STAB_DELAY_US * (SystemCoreClock / 1000000U));
  400. while(counter != 0U)
  401. {
  402. counter--;
  403. }
  404. }
  405. /* Start conversion if ADC is effectively enabled */
  406. if(HAL_IS_BIT_SET(hadc->Instance->CR2, ADC_CR2_ADON))
  407. {
  408. /* Set ADC state */
  409. /* - Clear state bitfield related to regular group conversion results */
  410. /* - Set state bitfield related to regular group operation */
  411. ADC_STATE_CLR_SET(hadc->State,
  412. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR,
  413. HAL_ADC_STATE_REG_BUSY);
  414. /* If conversions on group regular are also triggering group injected, */
  415. /* update ADC state. */
  416. if (READ_BIT(hadc->Instance->CR1, ADC_CR1_JAUTO) != RESET)
  417. {
  418. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
  419. }
  420. /* State machine update: Check if an injected conversion is ongoing */
  421. if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  422. {
  423. /* Reset ADC error code fields related to conversions on group regular */
  424. CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA));
  425. }
  426. else
  427. {
  428. /* Reset ADC all error code fields */
  429. ADC_CLEAR_ERRORCODE(hadc);
  430. }
  431. /* Process unlocked */
  432. /* Unlock before starting ADC conversions: in case of potential */
  433. /* interruption, to let the process to ADC IRQ Handler. */
  434. __HAL_UNLOCK(hadc);
  435. /* Clear regular group conversion flag and overrun flag */
  436. /* (To ensure of no unknown state from potential previous ADC operations) */
  437. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOC | ADC_FLAG_OVR);
  438. /* Check if Multimode enabled */
  439. if(HAL_IS_BIT_CLR(ADC->CCR, ADC_CCR_MULTI))
  440. {
  441. /* if no external trigger present enable software conversion of regular channels */
  442. if((hadc->Instance->CR2 & ADC_CR2_EXTEN) == RESET)
  443. {
  444. /* Enable the selected ADC software conversion for regular group */
  445. hadc->Instance->CR2 |= (uint32_t)ADC_CR2_SWSTART;
  446. }
  447. }
  448. else
  449. {
  450. /* if instance of handle correspond to ADC1 and no external trigger present enable software conversion of regular channels */
  451. if((hadc->Instance == ADC1) && ((hadc->Instance->CR2 & ADC_CR2_EXTEN) == RESET))
  452. {
  453. /* Enable the selected ADC software conversion for regular group */
  454. hadc->Instance->CR2 |= (uint32_t)ADC_CR2_SWSTART;
  455. }
  456. }
  457. }
  458. /* Return function status */
  459. return HAL_OK;
  460. }
  461. /**
  462. * @brief Disables ADC and stop conversion of regular channels.
  463. *
  464. * @note Caution: This function will stop also injected channels.
  465. *
  466. * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
  467. * the configuration information for the specified ADC.
  468. *
  469. * @retval HAL status.
  470. */
  471. HAL_StatusTypeDef HAL_ADC_Stop(ADC_HandleTypeDef* hadc)
  472. {
  473. /* Check the parameters */
  474. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  475. /* Process locked */
  476. __HAL_LOCK(hadc);
  477. /* Stop potential conversion on going, on regular and injected groups */
  478. /* Disable ADC peripheral */
  479. __HAL_ADC_DISABLE(hadc);
  480. /* Check if ADC is effectively disabled */
  481. if(HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_ADON))
  482. {
  483. /* Set ADC state */
  484. ADC_STATE_CLR_SET(hadc->State,
  485. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  486. HAL_ADC_STATE_READY);
  487. }
  488. /* Process unlocked */
  489. __HAL_UNLOCK(hadc);
  490. /* Return function status */
  491. return HAL_OK;
  492. }
  493. /**
  494. * @brief Poll for regular conversion complete
  495. * @note ADC conversion flags EOS (end of sequence) and EOC (end of
  496. * conversion) are cleared by this function.
  497. * @note This function cannot be used in a particular setup: ADC configured
  498. * in DMA mode and polling for end of each conversion (ADC init
  499. * parameter "EOCSelection" set to ADC_EOC_SINGLE_CONV).
  500. * In this case, DMA resets the flag EOC and polling cannot be
  501. * performed on each conversion. Nevertheless, polling can still
  502. * be performed on the complete sequence.
  503. * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
  504. * the configuration information for the specified ADC.
  505. * @param Timeout: Timeout value in millisecond.
  506. * @retval HAL status
  507. */
  508. HAL_StatusTypeDef HAL_ADC_PollForConversion(ADC_HandleTypeDef* hadc, uint32_t Timeout)
  509. {
  510. uint32_t tickstart = 0U;
  511. /* Verification that ADC configuration is compliant with polling for */
  512. /* each conversion: */
  513. /* Particular case is ADC configured in DMA mode and ADC sequencer with */
  514. /* several ranks and polling for end of each conversion. */
  515. /* For code simplicity sake, this particular case is generalized to */
  516. /* ADC configured in DMA mode and polling for end of each conversion. */
  517. if (HAL_IS_BIT_SET(hadc->Instance->CR2, ADC_CR2_EOCS) &&
  518. HAL_IS_BIT_SET(hadc->Instance->CR2, ADC_CR2_DMA) )
  519. {
  520. /* Update ADC state machine to error */
  521. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  522. /* Process unlocked */
  523. __HAL_UNLOCK(hadc);
  524. return HAL_ERROR;
  525. }
  526. /* Get tick */
  527. tickstart = HAL_GetTick();
  528. /* Check End of conversion flag */
  529. while(!(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOC)))
  530. {
  531. /* Check if timeout is disabled (set to infinite wait) */
  532. if(Timeout != HAL_MAX_DELAY)
  533. {
  534. if((Timeout == 0U) || ((HAL_GetTick() - tickstart ) > Timeout))
  535. {
  536. /* Update ADC state machine to timeout */
  537. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  538. /* Process unlocked */
  539. __HAL_UNLOCK(hadc);
  540. return HAL_TIMEOUT;
  541. }
  542. }
  543. }
  544. /* Clear regular group conversion flag */
  545. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_STRT | ADC_FLAG_EOC);
  546. /* Update ADC state machine */
  547. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  548. /* Determine whether any further conversion upcoming on group regular */
  549. /* by external trigger, continuous mode or scan sequence on going. */
  550. /* Note: On STM32F4, there is no independent flag of end of sequence. */
  551. /* The test of scan sequence on going is done either with scan */
  552. /* sequence disabled or with end of conversion flag set to */
  553. /* of end of sequence. */
  554. if(ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  555. (hadc->Init.ContinuousConvMode == DISABLE) &&
  556. (HAL_IS_BIT_CLR(hadc->Instance->SQR1, ADC_SQR1_L) ||
  557. HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_EOCS) ) )
  558. {
  559. /* Set ADC state */
  560. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  561. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  562. {
  563. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  564. }
  565. }
  566. /* Return ADC state */
  567. return HAL_OK;
  568. }
  569. /**
  570. * @brief Poll for conversion event
  571. * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
  572. * the configuration information for the specified ADC.
  573. * @param EventType: the ADC event type.
  574. * This parameter can be one of the following values:
  575. * @arg ADC_AWD_EVENT: ADC Analog watch Dog event.
  576. * @arg ADC_OVR_EVENT: ADC Overrun event.
  577. * @param Timeout: Timeout value in millisecond.
  578. * @retval HAL status
  579. */
  580. HAL_StatusTypeDef HAL_ADC_PollForEvent(ADC_HandleTypeDef* hadc, uint32_t EventType, uint32_t Timeout)
  581. {
  582. uint32_t tickstart = 0U;
  583. /* Check the parameters */
  584. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  585. assert_param(IS_ADC_EVENT_TYPE(EventType));
  586. /* Get tick */
  587. tickstart = HAL_GetTick();
  588. /* Check selected event flag */
  589. while(!(__HAL_ADC_GET_FLAG(hadc,EventType)))
  590. {
  591. /* Check for the Timeout */
  592. if(Timeout != HAL_MAX_DELAY)
  593. {
  594. if((Timeout == 0U) || ((HAL_GetTick() - tickstart ) > Timeout))
  595. {
  596. /* Update ADC state machine to timeout */
  597. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  598. /* Process unlocked */
  599. __HAL_UNLOCK(hadc);
  600. return HAL_TIMEOUT;
  601. }
  602. }
  603. }
  604. /* Analog watchdog (level out of window) event */
  605. if(EventType == ADC_AWD_EVENT)
  606. {
  607. /* Set ADC state */
  608. SET_BIT(hadc->State, HAL_ADC_STATE_AWD1);
  609. /* Clear ADC analog watchdog flag */
  610. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD);
  611. }
  612. /* Overrun event */
  613. else
  614. {
  615. /* Set ADC state */
  616. SET_BIT(hadc->State, HAL_ADC_STATE_REG_OVR);
  617. /* Set ADC error code to overrun */
  618. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_OVR);
  619. /* Clear ADC overrun flag */
  620. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  621. }
  622. /* Return ADC state */
  623. return HAL_OK;
  624. }
  625. /**
  626. * @brief Enables the interrupt and starts ADC conversion of regular channels.
  627. * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
  628. * the configuration information for the specified ADC.
  629. * @retval HAL status.
  630. */
  631. HAL_StatusTypeDef HAL_ADC_Start_IT(ADC_HandleTypeDef* hadc)
  632. {
  633. __IO uint32_t counter = 0U;
  634. /* Check the parameters */
  635. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
  636. assert_param(IS_ADC_EXT_TRIG_EDGE(hadc->Init.ExternalTrigConvEdge));
  637. /* Process locked */
  638. __HAL_LOCK(hadc);
  639. /* Enable the ADC peripheral */
  640. /* Check if ADC peripheral is disabled in order to enable it and wait during
  641. Tstab time the ADC's stabilization */
  642. if((hadc->Instance->CR2 & ADC_CR2_ADON) != ADC_CR2_ADON)
  643. {
  644. /* Enable the Peripheral */
  645. __HAL_ADC_ENABLE(hadc);
  646. /* Delay for ADC stabilization time */
  647. /* Compute number of CPU cycles to wait for */
  648. counter = (ADC_STAB_DELAY_US * (SystemCoreClock / 1000000U));
  649. while(counter != 0U)
  650. {
  651. counter--;
  652. }
  653. }
  654. /* Start conversion if ADC is effectively enabled */
  655. if(HAL_IS_BIT_SET(hadc->Instance->CR2, ADC_CR2_ADON))
  656. {
  657. /* Set ADC state */
  658. /* - Clear state bitfield related to regular group conversion results */
  659. /* - Set state bitfield related to regular group operation */
  660. ADC_STATE_CLR_SET(hadc->State,
  661. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR,
  662. HAL_ADC_STATE_REG_BUSY);
  663. /* If conversions on group regular are also triggering group injected, */
  664. /* update ADC state. */
  665. if (READ_BIT(hadc->Instance->CR1, ADC_CR1_JAUTO) != RESET)
  666. {
  667. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
  668. }
  669. /* State machine update: Check if an injected conversion is ongoing */
  670. if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  671. {
  672. /* Reset ADC error code fields related to conversions on group regular */
  673. CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA));
  674. }
  675. else
  676. {
  677. /* Reset ADC all error code fields */
  678. ADC_CLEAR_ERRORCODE(hadc);
  679. }
  680. /* Process unlocked */
  681. /* Unlock before starting ADC conversions: in case of potential */
  682. /* interruption, to let the process to ADC IRQ Handler. */
  683. __HAL_UNLOCK(hadc);
  684. /* Clear regular group conversion flag and overrun flag */
  685. /* (To ensure of no unknown state from potential previous ADC operations) */
  686. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOC | ADC_FLAG_OVR);
  687. /* Enable end of conversion interrupt for regular group */
  688. __HAL_ADC_ENABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_OVR));
  689. /* Check if Multimode enabled */
  690. if(HAL_IS_BIT_CLR(ADC->CCR, ADC_CCR_MULTI))
  691. {
  692. /* if no external trigger present enable software conversion of regular channels */
  693. if((hadc->Instance->CR2 & ADC_CR2_EXTEN) == RESET)
  694. {
  695. /* Enable the selected ADC software conversion for regular group */
  696. hadc->Instance->CR2 |= (uint32_t)ADC_CR2_SWSTART;
  697. }
  698. }
  699. else
  700. {
  701. /* if instance of handle correspond to ADC1 and no external trigger present enable software conversion of regular channels */
  702. if((hadc->Instance == ADC1) && ((hadc->Instance->CR2 & ADC_CR2_EXTEN) == RESET))
  703. {
  704. /* Enable the selected ADC software conversion for regular group */
  705. hadc->Instance->CR2 |= (uint32_t)ADC_CR2_SWSTART;
  706. }
  707. }
  708. }
  709. /* Return function status */
  710. return HAL_OK;
  711. }
  712. /**
  713. * @brief Disables the interrupt and stop ADC conversion of regular channels.
  714. *
  715. * @note Caution: This function will stop also injected channels.
  716. *
  717. * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
  718. * the configuration information for the specified ADC.
  719. * @retval HAL status.
  720. */
  721. HAL_StatusTypeDef HAL_ADC_Stop_IT(ADC_HandleTypeDef* hadc)
  722. {
  723. /* Check the parameters */
  724. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  725. /* Process locked */
  726. __HAL_LOCK(hadc);
  727. /* Stop potential conversion on going, on regular and injected groups */
  728. /* Disable ADC peripheral */
  729. __HAL_ADC_DISABLE(hadc);
  730. /* Check if ADC is effectively disabled */
  731. if(HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_ADON))
  732. {
  733. /* Disable ADC end of conversion interrupt for regular group */
  734. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_OVR));
  735. /* Set ADC state */
  736. ADC_STATE_CLR_SET(hadc->State,
  737. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  738. HAL_ADC_STATE_READY);
  739. }
  740. /* Process unlocked */
  741. __HAL_UNLOCK(hadc);
  742. /* Return function status */
  743. return HAL_OK;
  744. }
  745. /**
  746. * @brief Handles ADC interrupt request
  747. * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
  748. * the configuration information for the specified ADC.
  749. * @retval None
  750. */
  751. void HAL_ADC_IRQHandler(ADC_HandleTypeDef* hadc)
  752. {
  753. uint32_t tmp1 = 0U, tmp2 = 0U;
  754. /* Check the parameters */
  755. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
  756. assert_param(IS_ADC_REGULAR_LENGTH(hadc->Init.NbrOfConversion));
  757. assert_param(IS_ADC_EOCSelection(hadc->Init.EOCSelection));
  758. tmp1 = __HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOC);
  759. tmp2 = __HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_EOC);
  760. /* Check End of conversion flag for regular channels */
  761. if(tmp1 && tmp2)
  762. {
  763. /* Update state machine on conversion status if not in error state */
  764. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL))
  765. {
  766. /* Set ADC state */
  767. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  768. }
  769. /* Determine whether any further conversion upcoming on group regular */
  770. /* by external trigger, continuous mode or scan sequence on going. */
  771. /* Note: On STM32F4, there is no independent flag of end of sequence. */
  772. /* The test of scan sequence on going is done either with scan */
  773. /* sequence disabled or with end of conversion flag set to */
  774. /* of end of sequence. */
  775. if(ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  776. (hadc->Init.ContinuousConvMode == DISABLE) &&
  777. (HAL_IS_BIT_CLR(hadc->Instance->SQR1, ADC_SQR1_L) ||
  778. HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_EOCS) ) )
  779. {
  780. /* Disable ADC end of single conversion interrupt on group regular */
  781. /* Note: Overrun interrupt was enabled with EOC interrupt in */
  782. /* HAL_ADC_Start_IT(), but is not disabled here because can be used */
  783. /* by overrun IRQ process below. */
  784. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC);
  785. /* Set ADC state */
  786. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  787. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  788. {
  789. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  790. }
  791. }
  792. /* Conversion complete callback */
  793. HAL_ADC_ConvCpltCallback(hadc);
  794. /* Clear regular group conversion flag */
  795. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_STRT | ADC_FLAG_EOC);
  796. }
  797. tmp1 = __HAL_ADC_GET_FLAG(hadc, ADC_FLAG_JEOC);
  798. tmp2 = __HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_JEOC);
  799. /* Check End of conversion flag for injected channels */
  800. if(tmp1 && tmp2)
  801. {
  802. /* Update state machine on conversion status if not in error state */
  803. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL))
  804. {
  805. /* Set ADC state */
  806. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_EOC);
  807. }
  808. /* Determine whether any further conversion upcoming on group injected */
  809. /* by external trigger, scan sequence on going or by automatic injected */
  810. /* conversion from group regular (same conditions as group regular */
  811. /* interruption disabling above). */
  812. if(ADC_IS_SOFTWARE_START_INJECTED(hadc) &&
  813. (HAL_IS_BIT_CLR(hadc->Instance->JSQR, ADC_JSQR_JL) ||
  814. HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_EOCS) ) &&
  815. (HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO) &&
  816. (ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  817. (hadc->Init.ContinuousConvMode == DISABLE) ) ) )
  818. {
  819. /* Disable ADC end of single conversion interrupt on group injected */
  820. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC);
  821. /* Set ADC state */
  822. CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  823. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_REG_BUSY))
  824. {
  825. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  826. }
  827. }
  828. /* Conversion complete callback */
  829. HAL_ADCEx_InjectedConvCpltCallback(hadc);
  830. /* Clear injected group conversion flag */
  831. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_JSTRT | ADC_FLAG_JEOC));
  832. }
  833. tmp1 = __HAL_ADC_GET_FLAG(hadc, ADC_FLAG_AWD);
  834. tmp2 = __HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_AWD);
  835. /* Check Analog watchdog flag */
  836. if(tmp1 && tmp2)
  837. {
  838. if(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_AWD))
  839. {
  840. /* Set ADC state */
  841. SET_BIT(hadc->State, HAL_ADC_STATE_AWD1);
  842. /* Level out of window callback */
  843. HAL_ADC_LevelOutOfWindowCallback(hadc);
  844. /* Clear the ADC analog watchdog flag */
  845. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD);
  846. }
  847. }
  848. tmp1 = __HAL_ADC_GET_FLAG(hadc, ADC_FLAG_OVR);
  849. tmp2 = __HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_OVR);
  850. /* Check Overrun flag */
  851. if(tmp1 && tmp2)
  852. {
  853. /* Note: On STM32F4, ADC overrun can be set through other parameters */
  854. /* refer to description of parameter "EOCSelection" for more */
  855. /* details. */
  856. /* Set ADC error code to overrun */
  857. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_OVR);
  858. /* Clear ADC overrun flag */
  859. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  860. /* Error callback */
  861. HAL_ADC_ErrorCallback(hadc);
  862. /* Clear the Overrun flag */
  863. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  864. }
  865. }
  866. /**
  867. * @brief Enables ADC DMA request after last transfer (Single-ADC mode) and enables ADC peripheral
  868. * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
  869. * the configuration information for the specified ADC.
  870. * @param pData: The destination Buffer address.
  871. * @param Length: The length of data to be transferred from ADC peripheral to memory.
  872. * @retval HAL status
  873. */
  874. HAL_StatusTypeDef HAL_ADC_Start_DMA(ADC_HandleTypeDef* hadc, uint32_t* pData, uint32_t Length)
  875. {
  876. __IO uint32_t counter = 0U;
  877. /* Check the parameters */
  878. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
  879. assert_param(IS_ADC_EXT_TRIG_EDGE(hadc->Init.ExternalTrigConvEdge));
  880. /* Process locked */
  881. __HAL_LOCK(hadc);
  882. /* Enable the ADC peripheral */
  883. /* Check if ADC peripheral is disabled in order to enable it and wait during
  884. Tstab time the ADC's stabilization */
  885. if((hadc->Instance->CR2 & ADC_CR2_ADON) != ADC_CR2_ADON)
  886. {
  887. /* Enable the Peripheral */
  888. __HAL_ADC_ENABLE(hadc);
  889. /* Delay for ADC stabilization time */
  890. /* Compute number of CPU cycles to wait for */
  891. counter = (ADC_STAB_DELAY_US * (SystemCoreClock / 1000000U));
  892. while(counter != 0U)
  893. {
  894. counter--;
  895. }
  896. }
  897. /* Start conversion if ADC is effectively enabled */
  898. if(HAL_IS_BIT_SET(hadc->Instance->CR2, ADC_CR2_ADON))
  899. {
  900. /* Set ADC state */
  901. /* - Clear state bitfield related to regular group conversion results */
  902. /* - Set state bitfield related to regular group operation */
  903. ADC_STATE_CLR_SET(hadc->State,
  904. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR,
  905. HAL_ADC_STATE_REG_BUSY);
  906. /* If conversions on group regular are also triggering group injected, */
  907. /* update ADC state. */
  908. if (READ_BIT(hadc->Instance->CR1, ADC_CR1_JAUTO) != RESET)
  909. {
  910. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
  911. }
  912. /* State machine update: Check if an injected conversion is ongoing */
  913. if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  914. {
  915. /* Reset ADC error code fields related to conversions on group regular */
  916. CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA));
  917. }
  918. else
  919. {
  920. /* Reset ADC all error code fields */
  921. ADC_CLEAR_ERRORCODE(hadc);
  922. }
  923. /* Process unlocked */
  924. /* Unlock before starting ADC conversions: in case of potential */
  925. /* interruption, to let the process to ADC IRQ Handler. */
  926. __HAL_UNLOCK(hadc);
  927. /* Set the DMA transfer complete callback */
  928. hadc->DMA_Handle->XferCpltCallback = ADC_DMAConvCplt;
  929. /* Set the DMA half transfer complete callback */
  930. hadc->DMA_Handle->XferHalfCpltCallback = ADC_DMAHalfConvCplt;
  931. /* Set the DMA error callback */
  932. hadc->DMA_Handle->XferErrorCallback = ADC_DMAError;
  933. /* Manage ADC and DMA start: ADC overrun interruption, DMA start, ADC */
  934. /* start (in case of SW start): */
  935. /* Clear regular group conversion flag and overrun flag */
  936. /* (To ensure of no unknown state from potential previous ADC operations) */
  937. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOC | ADC_FLAG_OVR);
  938. /* Enable ADC overrun interrupt */
  939. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);
  940. /* Enable ADC DMA mode */
  941. hadc->Instance->CR2 |= ADC_CR2_DMA;
  942. /* Start the DMA channel */
  943. HAL_DMA_Start_IT(hadc->DMA_Handle, (uint32_t)&hadc->Instance->DR, (uint32_t)pData, Length);
  944. /* Check if Multimode enabled */
  945. if(HAL_IS_BIT_CLR(ADC->CCR, ADC_CCR_MULTI))
  946. {
  947. /* if no external trigger present enable software conversion of regular channels */
  948. if((hadc->Instance->CR2 & ADC_CR2_EXTEN) == RESET)
  949. {
  950. /* Enable the selected ADC software conversion for regular group */
  951. hadc->Instance->CR2 |= (uint32_t)ADC_CR2_SWSTART;
  952. }
  953. }
  954. else
  955. {
  956. /* if instance of handle correspond to ADC1 and no external trigger present enable software conversion of regular channels */
  957. if((hadc->Instance == ADC1) && ((hadc->Instance->CR2 & ADC_CR2_EXTEN) == RESET))
  958. {
  959. /* Enable the selected ADC software conversion for regular group */
  960. hadc->Instance->CR2 |= (uint32_t)ADC_CR2_SWSTART;
  961. }
  962. }
  963. }
  964. /* Return function status */
  965. return HAL_OK;
  966. }
  967. /**
  968. * @brief Disables ADC DMA (Single-ADC mode) and disables ADC peripheral
  969. * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
  970. * the configuration information for the specified ADC.
  971. * @retval HAL status
  972. */
  973. HAL_StatusTypeDef HAL_ADC_Stop_DMA(ADC_HandleTypeDef* hadc)
  974. {
  975. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  976. /* Check the parameters */
  977. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  978. /* Process locked */
  979. __HAL_LOCK(hadc);
  980. /* Stop potential conversion on going, on regular and injected groups */
  981. /* Disable ADC peripheral */
  982. __HAL_ADC_DISABLE(hadc);
  983. /* Check if ADC is effectively disabled */
  984. if(HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_ADON))
  985. {
  986. /* Disable the selected ADC DMA mode */
  987. hadc->Instance->CR2 &= ~ADC_CR2_DMA;
  988. /* Disable the DMA channel (in case of DMA in circular mode or stop while */
  989. /* DMA transfer is on going) */
  990. tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
  991. /* Disable ADC overrun interrupt */
  992. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
  993. /* Set ADC state */
  994. ADC_STATE_CLR_SET(hadc->State,
  995. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  996. HAL_ADC_STATE_READY);
  997. }
  998. /* Process unlocked */
  999. __HAL_UNLOCK(hadc);
  1000. /* Return function status */
  1001. return tmp_hal_status;
  1002. }
  1003. /**
  1004. * @brief Gets the converted value from data register of regular channel.
  1005. * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
  1006. * the configuration information for the specified ADC.
  1007. * @retval Converted value
  1008. */
  1009. uint32_t HAL_ADC_GetValue(ADC_HandleTypeDef* hadc)
  1010. {
  1011. /* Return the selected ADC converted value */
  1012. return hadc->Instance->DR;
  1013. }
  1014. /**
  1015. * @brief Regular conversion complete callback in non blocking mode
  1016. * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
  1017. * the configuration information for the specified ADC.
  1018. * @retval None
  1019. */
  1020. __weak void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
  1021. {
  1022. /* Prevent unused argument(s) compilation warning */
  1023. UNUSED(hadc);
  1024. /* NOTE : This function Should not be modified, when the callback is needed,
  1025. the HAL_ADC_ConvCpltCallback could be implemented in the user file
  1026. */
  1027. }
  1028. /**
  1029. * @brief Regular conversion half DMA transfer callback in non blocking mode
  1030. * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
  1031. * the configuration information for the specified ADC.
  1032. * @retval None
  1033. */
  1034. __weak void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc)
  1035. {
  1036. /* Prevent unused argument(s) compilation warning */
  1037. UNUSED(hadc);
  1038. /* NOTE : This function Should not be modified, when the callback is needed,
  1039. the HAL_ADC_ConvHalfCpltCallback could be implemented in the user file
  1040. */
  1041. }
  1042. /**
  1043. * @brief Analog watchdog callback in non blocking mode
  1044. * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
  1045. * the configuration information for the specified ADC.
  1046. * @retval None
  1047. */
  1048. __weak void HAL_ADC_LevelOutOfWindowCallback(ADC_HandleTypeDef* hadc)
  1049. {
  1050. /* Prevent unused argument(s) compilation warning */
  1051. UNUSED(hadc);
  1052. /* NOTE : This function Should not be modified, when the callback is needed,
  1053. the HAL_ADC_LevelOoutOfWindowCallback could be implemented in the user file
  1054. */
  1055. }
  1056. /**
  1057. * @brief Error ADC callback.
  1058. * @note In case of error due to overrun when using ADC with DMA transfer
  1059. * (HAL ADC handle paramater "ErrorCode" to state "HAL_ADC_ERROR_OVR"):
  1060. * - Reinitialize the DMA using function "HAL_ADC_Stop_DMA()".
  1061. * - If needed, restart a new ADC conversion using function
  1062. * "HAL_ADC_Start_DMA()"
  1063. * (this function is also clearing overrun flag)
  1064. * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
  1065. * the configuration information for the specified ADC.
  1066. * @retval None
  1067. */
  1068. __weak void HAL_ADC_ErrorCallback(ADC_HandleTypeDef *hadc)
  1069. {
  1070. /* Prevent unused argument(s) compilation warning */
  1071. UNUSED(hadc);
  1072. /* NOTE : This function Should not be modified, when the callback is needed,
  1073. the HAL_ADC_ErrorCallback could be implemented in the user file
  1074. */
  1075. }
  1076. /**
  1077. * @}
  1078. */
  1079. /** @defgroup ADC_Exported_Functions_Group3 Peripheral Control functions
  1080. * @brief Peripheral Control functions
  1081. *
  1082. @verbatim
  1083. ===============================================================================
  1084. ##### Peripheral Control functions #####
  1085. ===============================================================================
  1086. [..] This section provides functions allowing to:
  1087. (+) Configure regular channels.
  1088. (+) Configure injected channels.
  1089. (+) Configure multimode.
  1090. (+) Configure the analog watch dog.
  1091. @endverbatim
  1092. * @{
  1093. */
  1094. /**
  1095. * @brief Configures for the selected ADC regular channel its corresponding
  1096. * rank in the sequencer and its sample time.
  1097. * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
  1098. * the configuration information for the specified ADC.
  1099. * @param sConfig: ADC configuration structure.
  1100. * @retval HAL status
  1101. */
  1102. HAL_StatusTypeDef HAL_ADC_ConfigChannel(ADC_HandleTypeDef* hadc, ADC_ChannelConfTypeDef* sConfig)
  1103. {
  1104. __IO uint32_t counter = 0U;
  1105. /* Check the parameters */
  1106. assert_param(IS_ADC_CHANNEL(sConfig->Channel));
  1107. assert_param(IS_ADC_REGULAR_RANK(sConfig->Rank));
  1108. assert_param(IS_ADC_SAMPLE_TIME(sConfig->SamplingTime));
  1109. /* Process locked */
  1110. __HAL_LOCK(hadc);
  1111. /* if ADC_Channel_10 ... ADC_Channel_18 is selected */
  1112. if (sConfig->Channel > ADC_CHANNEL_9)
  1113. {
  1114. /* Clear the old sample time */
  1115. hadc->Instance->SMPR1 &= ~ADC_SMPR1(ADC_SMPR1_SMP10, sConfig->Channel);
  1116. /* Set the new sample time */
  1117. hadc->Instance->SMPR1 |= ADC_SMPR1(sConfig->SamplingTime, sConfig->Channel);
  1118. }
  1119. else /* ADC_Channel include in ADC_Channel_[0..9] */
  1120. {
  1121. /* Clear the old sample time */
  1122. hadc->Instance->SMPR2 &= ~ADC_SMPR2(ADC_SMPR2_SMP0, sConfig->Channel);
  1123. /* Set the new sample time */
  1124. hadc->Instance->SMPR2 |= ADC_SMPR2(sConfig->SamplingTime, sConfig->Channel);
  1125. }
  1126. /* For Rank 1 to 6 */
  1127. if (sConfig->Rank < 7U)
  1128. {
  1129. /* Clear the old SQx bits for the selected rank */
  1130. hadc->Instance->SQR3 &= ~ADC_SQR3_RK(ADC_SQR3_SQ1, sConfig->Rank);
  1131. /* Set the SQx bits for the selected rank */
  1132. hadc->Instance->SQR3 |= ADC_SQR3_RK(sConfig->Channel, sConfig->Rank);
  1133. }
  1134. /* For Rank 7 to 12 */
  1135. else if (sConfig->Rank < 13U)
  1136. {
  1137. /* Clear the old SQx bits for the selected rank */
  1138. hadc->Instance->SQR2 &= ~ADC_SQR2_RK(ADC_SQR2_SQ7, sConfig->Rank);
  1139. /* Set the SQx bits for the selected rank */
  1140. hadc->Instance->SQR2 |= ADC_SQR2_RK(sConfig->Channel, sConfig->Rank);
  1141. }
  1142. /* For Rank 13 to 16 */
  1143. else
  1144. {
  1145. /* Clear the old SQx bits for the selected rank */
  1146. hadc->Instance->SQR1 &= ~ADC_SQR1_RK(ADC_SQR1_SQ13, sConfig->Rank);
  1147. /* Set the SQx bits for the selected rank */
  1148. hadc->Instance->SQR1 |= ADC_SQR1_RK(sConfig->Channel, sConfig->Rank);
  1149. }
  1150. /* if ADC1 Channel_18 is selected enable VBAT Channel */
  1151. if ((hadc->Instance == ADC1) && (sConfig->Channel == ADC_CHANNEL_VBAT))
  1152. {
  1153. /* Enable the VBAT channel*/
  1154. ADC->CCR |= ADC_CCR_VBATE;
  1155. }
  1156. /* if ADC1 Channel_16 or Channel_17 is selected enable TSVREFE Channel(Temperature sensor and VREFINT) */
  1157. if ((hadc->Instance == ADC1) && ((sConfig->Channel == ADC_CHANNEL_TEMPSENSOR) || (sConfig->Channel == ADC_CHANNEL_VREFINT)))
  1158. {
  1159. /* Enable the TSVREFE channel*/
  1160. ADC->CCR |= ADC_CCR_TSVREFE;
  1161. if((sConfig->Channel == ADC_CHANNEL_TEMPSENSOR))
  1162. {
  1163. /* Delay for temperature sensor stabilization time */
  1164. /* Compute number of CPU cycles to wait for */
  1165. counter = (ADC_TEMPSENSOR_DELAY_US * (SystemCoreClock / 1000000U));
  1166. while(counter != 0U)
  1167. {
  1168. counter--;
  1169. }
  1170. }
  1171. }
  1172. /* Process unlocked */
  1173. __HAL_UNLOCK(hadc);
  1174. /* Return function status */
  1175. return HAL_OK;
  1176. }
  1177. /**
  1178. * @brief Configures the analog watchdog.
  1179. * @note Analog watchdog thresholds can be modified while ADC conversion
  1180. * is on going.
  1181. * In this case, some constraints must be taken into account:
  1182. * The programmed threshold values are effective from the next
  1183. * ADC EOC (end of unitary conversion).
  1184. * Considering that registers write delay may happen due to
  1185. * bus activity, this might cause an uncertainty on the
  1186. * effective timing of the new programmed threshold values.
  1187. * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
  1188. * the configuration information for the specified ADC.
  1189. * @param AnalogWDGConfig : pointer to an ADC_AnalogWDGConfTypeDef structure
  1190. * that contains the configuration information of ADC analog watchdog.
  1191. * @retval HAL status
  1192. */
  1193. HAL_StatusTypeDef HAL_ADC_AnalogWDGConfig(ADC_HandleTypeDef* hadc, ADC_AnalogWDGConfTypeDef* AnalogWDGConfig)
  1194. {
  1195. #ifdef USE_FULL_ASSERT
  1196. uint32_t tmp = 0U;
  1197. #endif /* USE_FULL_ASSERT */
  1198. /* Check the parameters */
  1199. assert_param(IS_ADC_ANALOG_WATCHDOG(AnalogWDGConfig->WatchdogMode));
  1200. assert_param(IS_ADC_CHANNEL(AnalogWDGConfig->Channel));
  1201. assert_param(IS_FUNCTIONAL_STATE(AnalogWDGConfig->ITMode));
  1202. #ifdef USE_FULL_ASSERT
  1203. tmp = ADC_GET_RESOLUTION(hadc);
  1204. assert_param(IS_ADC_RANGE(tmp, AnalogWDGConfig->HighThreshold));
  1205. assert_param(IS_ADC_RANGE(tmp, AnalogWDGConfig->LowThreshold));
  1206. #endif /* USE_FULL_ASSERT */
  1207. /* Process locked */
  1208. __HAL_LOCK(hadc);
  1209. if(AnalogWDGConfig->ITMode == ENABLE)
  1210. {
  1211. /* Enable the ADC Analog watchdog interrupt */
  1212. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_AWD);
  1213. }
  1214. else
  1215. {
  1216. /* Disable the ADC Analog watchdog interrupt */
  1217. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_AWD);
  1218. }
  1219. /* Clear AWDEN, JAWDEN and AWDSGL bits */
  1220. hadc->Instance->CR1 &= ~(ADC_CR1_AWDSGL | ADC_CR1_JAWDEN | ADC_CR1_AWDEN);
  1221. /* Set the analog watchdog enable mode */
  1222. hadc->Instance->CR1 |= AnalogWDGConfig->WatchdogMode;
  1223. /* Set the high threshold */
  1224. hadc->Instance->HTR = AnalogWDGConfig->HighThreshold;
  1225. /* Set the low threshold */
  1226. hadc->Instance->LTR = AnalogWDGConfig->LowThreshold;
  1227. /* Clear the Analog watchdog channel select bits */
  1228. hadc->Instance->CR1 &= ~ADC_CR1_AWDCH;
  1229. /* Set the Analog watchdog channel */
  1230. hadc->Instance->CR1 |= (uint32_t)((uint16_t)(AnalogWDGConfig->Channel));
  1231. /* Process unlocked */
  1232. __HAL_UNLOCK(hadc);
  1233. /* Return function status */
  1234. return HAL_OK;
  1235. }
  1236. /**
  1237. * @}
  1238. */
  1239. /** @defgroup ADC_Exported_Functions_Group4 ADC Peripheral State functions
  1240. * @brief ADC Peripheral State functions
  1241. *
  1242. @verbatim
  1243. ===============================================================================
  1244. ##### Peripheral State and errors functions #####
  1245. ===============================================================================
  1246. [..]
  1247. This subsection provides functions allowing to
  1248. (+) Check the ADC state
  1249. (+) Check the ADC Error
  1250. @endverbatim
  1251. * @{
  1252. */
  1253. /**
  1254. * @brief return the ADC state
  1255. * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
  1256. * the configuration information for the specified ADC.
  1257. * @retval HAL state
  1258. */
  1259. uint32_t HAL_ADC_GetState(ADC_HandleTypeDef* hadc)
  1260. {
  1261. /* Return ADC state */
  1262. return hadc->State;
  1263. }
  1264. /**
  1265. * @brief Return the ADC error code
  1266. * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
  1267. * the configuration information for the specified ADC.
  1268. * @retval ADC Error Code
  1269. */
  1270. uint32_t HAL_ADC_GetError(ADC_HandleTypeDef *hadc)
  1271. {
  1272. return hadc->ErrorCode;
  1273. }
  1274. /**
  1275. * @}
  1276. */
  1277. /** @addtogroup ADC_Private_Functions
  1278. * @{
  1279. */
  1280. /**
  1281. * @brief Initializes the ADCx peripheral according to the specified parameters
  1282. * in the ADC_InitStruct without initializing the ADC MSP.
  1283. * @param hadc: pointer to a ADC_HandleTypeDef structure that contains
  1284. * the configuration information for the specified ADC.
  1285. * @retval None
  1286. */
  1287. static void ADC_Init(ADC_HandleTypeDef* hadc)
  1288. {
  1289. /* Set ADC parameters */
  1290. /* Set the ADC clock prescaler */
  1291. ADC->CCR &= ~(ADC_CCR_ADCPRE);
  1292. ADC->CCR |= hadc->Init.ClockPrescaler;
  1293. /* Set ADC scan mode */
  1294. hadc->Instance->CR1 &= ~(ADC_CR1_SCAN);
  1295. hadc->Instance->CR1 |= ADC_CR1_SCANCONV(hadc->Init.ScanConvMode);
  1296. /* Set ADC resolution */
  1297. hadc->Instance->CR1 &= ~(ADC_CR1_RES);
  1298. hadc->Instance->CR1 |= hadc->Init.Resolution;
  1299. /* Set ADC data alignment */
  1300. hadc->Instance->CR2 &= ~(ADC_CR2_ALIGN);
  1301. hadc->Instance->CR2 |= hadc->Init.DataAlign;
  1302. /* Enable external trigger if trigger selection is different of software */
  1303. /* start. */
  1304. /* Note: This configuration keeps the hardware feature of parameter */
  1305. /* ExternalTrigConvEdge "trigger edge none" equivalent to */
  1306. /* software start. */
  1307. if(hadc->Init.ExternalTrigConv != ADC_SOFTWARE_START)
  1308. {
  1309. /* Select external trigger to start conversion */
  1310. hadc->Instance->CR2 &= ~(ADC_CR2_EXTSEL);
  1311. hadc->Instance->CR2 |= hadc->Init.ExternalTrigConv;
  1312. /* Select external trigger polarity */
  1313. hadc->Instance->CR2 &= ~(ADC_CR2_EXTEN);
  1314. hadc->Instance->CR2 |= hadc->Init.ExternalTrigConvEdge;
  1315. }
  1316. else
  1317. {
  1318. /* Reset the external trigger */
  1319. hadc->Instance->CR2 &= ~(ADC_CR2_EXTSEL);
  1320. hadc->Instance->CR2 &= ~(ADC_CR2_EXTEN);
  1321. }
  1322. /* Enable or disable ADC continuous conversion mode */
  1323. hadc->Instance->CR2 &= ~(ADC_CR2_CONT);
  1324. hadc->Instance->CR2 |= ADC_CR2_CONTINUOUS(hadc->Init.ContinuousConvMode);
  1325. if(hadc->Init.DiscontinuousConvMode != DISABLE)
  1326. {
  1327. assert_param(IS_ADC_REGULAR_DISC_NUMBER(hadc->Init.NbrOfDiscConversion));
  1328. /* Enable the selected ADC regular discontinuous mode */
  1329. hadc->Instance->CR1 |= (uint32_t)ADC_CR1_DISCEN;
  1330. /* Set the number of channels to be converted in discontinuous mode */
  1331. hadc->Instance->CR1 &= ~(ADC_CR1_DISCNUM);
  1332. hadc->Instance->CR1 |= ADC_CR1_DISCONTINUOUS(hadc->Init.NbrOfDiscConversion);
  1333. }
  1334. else
  1335. {
  1336. /* Disable the selected ADC regular discontinuous mode */
  1337. hadc->Instance->CR1 &= ~(ADC_CR1_DISCEN);
  1338. }
  1339. /* Set ADC number of conversion */
  1340. hadc->Instance->SQR1 &= ~(ADC_SQR1_L);
  1341. hadc->Instance->SQR1 |= ADC_SQR1(hadc->Init.NbrOfConversion);
  1342. /* Enable or disable ADC DMA continuous request */
  1343. hadc->Instance->CR2 &= ~(ADC_CR2_DDS);
  1344. hadc->Instance->CR2 |= ADC_CR2_DMAContReq(hadc->Init.DMAContinuousRequests);
  1345. /* Enable or disable ADC end of conversion selection */
  1346. hadc->Instance->CR2 &= ~(ADC_CR2_EOCS);
  1347. hadc->Instance->CR2 |= ADC_CR2_EOCSelection(hadc->Init.EOCSelection);
  1348. }
  1349. /**
  1350. * @brief DMA transfer complete callback.
  1351. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1352. * the configuration information for the specified DMA module.
  1353. * @retval None
  1354. */
  1355. static void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma)
  1356. {
  1357. /* Retrieve ADC handle corresponding to current DMA handle */
  1358. ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1359. /* Update state machine on conversion status if not in error state */
  1360. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL | HAL_ADC_STATE_ERROR_DMA))
  1361. {
  1362. /* Update ADC state machine */
  1363. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  1364. /* Determine whether any further conversion upcoming on group regular */
  1365. /* by external trigger, continuous mode or scan sequence on going. */
  1366. /* Note: On STM32F4, there is no independent flag of end of sequence. */
  1367. /* The test of scan sequence on going is done either with scan */
  1368. /* sequence disabled or with end of conversion flag set to */
  1369. /* of end of sequence. */
  1370. if(ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  1371. (hadc->Init.ContinuousConvMode == DISABLE) &&
  1372. (HAL_IS_BIT_CLR(hadc->Instance->SQR1, ADC_SQR1_L) ||
  1373. HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_EOCS) ) )
  1374. {
  1375. /* Disable ADC end of single conversion interrupt on group regular */
  1376. /* Note: Overrun interrupt was enabled with EOC interrupt in */
  1377. /* HAL_ADC_Start_IT(), but is not disabled here because can be used */
  1378. /* by overrun IRQ process below. */
  1379. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC);
  1380. /* Set ADC state */
  1381. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  1382. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  1383. {
  1384. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  1385. }
  1386. }
  1387. /* Conversion complete callback */
  1388. HAL_ADC_ConvCpltCallback(hadc);
  1389. }
  1390. else
  1391. {
  1392. /* Call DMA error callback */
  1393. hadc->DMA_Handle->XferErrorCallback(hdma);
  1394. }
  1395. }
  1396. /**
  1397. * @brief DMA half transfer complete callback.
  1398. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1399. * the configuration information for the specified DMA module.
  1400. * @retval None
  1401. */
  1402. static void ADC_DMAHalfConvCplt(DMA_HandleTypeDef *hdma)
  1403. {
  1404. ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1405. /* Conversion complete callback */
  1406. HAL_ADC_ConvHalfCpltCallback(hadc);
  1407. }
  1408. /**
  1409. * @brief DMA error callback
  1410. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1411. * the configuration information for the specified DMA module.
  1412. * @retval None
  1413. */
  1414. static void ADC_DMAError(DMA_HandleTypeDef *hdma)
  1415. {
  1416. ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1417. hadc->State= HAL_ADC_STATE_ERROR_DMA;
  1418. /* Set ADC error code to DMA error */
  1419. hadc->ErrorCode |= HAL_ADC_ERROR_DMA;
  1420. HAL_ADC_ErrorCallback(hadc);
  1421. }
  1422. /**
  1423. * @}
  1424. */
  1425. /**
  1426. * @}
  1427. */
  1428. #endif /* HAL_ADC_MODULE_ENABLED */
  1429. /**
  1430. * @}
  1431. */
  1432. /**
  1433. * @}
  1434. */
  1435. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/