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.

stm32f4xx_hal_adc.c 75KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_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) peripheral:
  7. * + Initialization and de-initialization functions
  8. * + IO operation functions
  9. * + State and errors functions
  10. *
  11. @verbatim
  12. ==============================================================================
  13. ##### ADC Peripheral features #####
  14. ==============================================================================
  15. [..]
  16. (#) 12-bit, 10-bit, 8-bit or 6-bit configurable resolution.
  17. (#) Interrupt generation at the end of conversion, end of injected conversion,
  18. and in case of analog watchdog or overrun events
  19. (#) Single and continuous conversion modes.
  20. (#) Scan mode for automatic conversion of channel 0 to channel x.
  21. (#) Data alignment with in-built data coherency.
  22. (#) Channel-wise programmable sampling time.
  23. (#) External trigger option with configurable polarity for both regular and
  24. injected conversion.
  25. (#) Dual/Triple mode (on devices with 2 ADCs or more).
  26. (#) Configurable DMA data storage in Dual/Triple ADC mode.
  27. (#) Configurable delay between conversions in Dual/Triple interleaved mode.
  28. (#) ADC conversion type (refer to the datasheets).
  29. (#) ADC supply requirements: 2.4 V to 3.6 V at full speed and down to 1.8 V at
  30. slower speed.
  31. (#) ADC input range: VREF(minus) = VIN = VREF(plus).
  32. (#) DMA request generation during regular channel conversion.
  33. ##### How to use this driver #####
  34. ==============================================================================
  35. [..]
  36. (#)Initialize the ADC low level resources by implementing the HAL_ADC_MspInit():
  37. (##) Enable the ADC interface clock using __HAL_RCC_ADC_CLK_ENABLE()
  38. (##) ADC pins configuration
  39. (+++) Enable the clock for the ADC GPIOs using the following function:
  40. __HAL_RCC_GPIOx_CLK_ENABLE()
  41. (+++) Configure these ADC pins in analog mode using HAL_GPIO_Init()
  42. (##) In case of using interrupts (e.g. HAL_ADC_Start_IT())
  43. (+++) Configure the ADC interrupt priority using HAL_NVIC_SetPriority()
  44. (+++) Enable the ADC IRQ handler using HAL_NVIC_EnableIRQ()
  45. (+++) In ADC IRQ handler, call HAL_ADC_IRQHandler()
  46. (##) In case of using DMA to control data transfer (e.g. HAL_ADC_Start_DMA())
  47. (+++) Enable the DMAx interface clock using __HAL_RCC_DMAx_CLK_ENABLE()
  48. (+++) Configure and enable two DMA streams stream for managing data
  49. transfer from peripheral to memory (output stream)
  50. (+++) Associate the initialized DMA handle to the CRYP DMA handle
  51. using __HAL_LINKDMA()
  52. (+++) Configure the priority and enable the NVIC for the transfer complete
  53. interrupt on the two DMA Streams. The output stream should have higher
  54. priority than the input stream.
  55. *** Configuration of ADC, groups regular/injected, channels parameters ***
  56. ==============================================================================
  57. [..]
  58. (#) Configure the ADC parameters (resolution, data alignment, ...)
  59. and regular group parameters (conversion trigger, sequencer, ...)
  60. using function HAL_ADC_Init().
  61. (#) Configure the channels for regular group parameters (channel number,
  62. channel rank into sequencer, ..., into regular group)
  63. using function HAL_ADC_ConfigChannel().
  64. (#) Optionally, configure the injected group parameters (conversion trigger,
  65. sequencer, ..., of injected group)
  66. and the channels for injected group parameters (channel number,
  67. channel rank into sequencer, ..., into injected group)
  68. using function HAL_ADCEx_InjectedConfigChannel().
  69. (#) Optionally, configure the analog watchdog parameters (channels
  70. monitored, thresholds, ...) using function HAL_ADC_AnalogWDGConfig().
  71. (#) Optionally, for devices with several ADC instances: configure the
  72. multimode parameters using function HAL_ADCEx_MultiModeConfigChannel().
  73. *** Execution of ADC conversions ***
  74. ==============================================================================
  75. [..]
  76. (#) ADC driver can be used among three modes: polling, interruption,
  77. transfer by DMA.
  78. *** Polling mode IO operation ***
  79. =================================
  80. [..]
  81. (+) Start the ADC peripheral using HAL_ADC_Start()
  82. (+) Wait for end of conversion using HAL_ADC_PollForConversion(), at this stage
  83. user can specify the value of timeout according to his end application
  84. (+) To read the ADC converted values, use the HAL_ADC_GetValue() function.
  85. (+) Stop the ADC peripheral using HAL_ADC_Stop()
  86. *** Interrupt mode IO operation ***
  87. ===================================
  88. [..]
  89. (+) Start the ADC peripheral using HAL_ADC_Start_IT()
  90. (+) Use HAL_ADC_IRQHandler() called under ADC_IRQHandler() Interrupt subroutine
  91. (+) At ADC end of conversion HAL_ADC_ConvCpltCallback() function is executed and user can
  92. add his own code by customization of function pointer HAL_ADC_ConvCpltCallback
  93. (+) In case of ADC Error, HAL_ADC_ErrorCallback() function is executed and user can
  94. add his own code by customization of function pointer HAL_ADC_ErrorCallback
  95. (+) Stop the ADC peripheral using HAL_ADC_Stop_IT()
  96. *** DMA mode IO operation ***
  97. ==============================
  98. [..]
  99. (+) Start the ADC peripheral using HAL_ADC_Start_DMA(), at this stage the user specify the length
  100. of data to be transferred at each end of conversion
  101. (+) At The end of data transfer by HAL_ADC_ConvCpltCallback() function is executed and user can
  102. add his own code by customization of function pointer HAL_ADC_ConvCpltCallback
  103. (+) In case of transfer Error, HAL_ADC_ErrorCallback() function is executed and user can
  104. add his own code by customization of function pointer HAL_ADC_ErrorCallback
  105. (+) Stop the ADC peripheral using HAL_ADC_Stop_DMA()
  106. *** ADC HAL driver macros list ***
  107. =============================================
  108. [..]
  109. Below the list of most used macros in ADC HAL driver.
  110. (+) __HAL_ADC_ENABLE : Enable the ADC peripheral
  111. (+) __HAL_ADC_DISABLE : Disable the ADC peripheral
  112. (+) __HAL_ADC_ENABLE_IT: Enable the ADC end of conversion interrupt
  113. (+) __HAL_ADC_DISABLE_IT: Disable the ADC end of conversion interrupt
  114. (+) __HAL_ADC_GET_IT_SOURCE: Check if the specified ADC interrupt source is enabled or disabled
  115. (+) __HAL_ADC_CLEAR_FLAG: Clear the ADC's pending flags
  116. (+) __HAL_ADC_GET_FLAG: Get the selected ADC's flag status
  117. (+) ADC_GET_RESOLUTION: Return resolution bits in CR1 register
  118. [..]
  119. (@) You can refer to the ADC HAL driver header file for more useful macros
  120. *** Deinitialization of ADC ***
  121. ==============================================================================
  122. [..]
  123. (#) Disable the ADC interface
  124. (++) ADC clock can be hard reset and disabled at RCC top level.
  125. (++) Hard reset of ADC peripherals
  126. using macro __HAL_RCC_ADC_FORCE_RESET(), __HAL_RCC_ADC_RELEASE_RESET().
  127. (++) ADC clock disable using the equivalent macro/functions as configuration step.
  128. (+++) Example:
  129. Into HAL_ADC_MspDeInit() (recommended code location) or with
  130. other device clock parameters configuration:
  131. (+++) HAL_RCC_GetOscConfig(&RCC_OscInitStructure);
  132. (+++) RCC_OscInitStructure.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  133. (+++) RCC_OscInitStructure.HSIState = RCC_HSI_OFF; (if not used for system clock)
  134. (+++) HAL_RCC_OscConfig(&RCC_OscInitStructure);
  135. (#) ADC pins configuration
  136. (++) Disable the clock for the ADC GPIOs using macro __HAL_RCC_GPIOx_CLK_DISABLE()
  137. (#) Optionally, in case of usage of ADC with interruptions:
  138. (++) Disable the NVIC for ADC using function HAL_NVIC_DisableIRQ(ADCx_IRQn)
  139. (#) Optionally, in case of usage of DMA:
  140. (++) Deinitialize the DMA using function HAL_DMA_DeInit().
  141. (++) Disable the NVIC for DMA using function HAL_NVIC_DisableIRQ(DMAx_Channelx_IRQn)
  142. *** Callback registration ***
  143. ==============================================================================
  144. [..]
  145. The compilation flag USE_HAL_ADC_REGISTER_CALLBACKS, when set to 1,
  146. allows the user to configure dynamically the driver callbacks.
  147. Use Functions @ref HAL_ADC_RegisterCallback()
  148. to register an interrupt callback.
  149. [..]
  150. Function @ref HAL_ADC_RegisterCallback() allows to register following callbacks:
  151. (+) ConvCpltCallback : ADC conversion complete callback
  152. (+) ConvHalfCpltCallback : ADC conversion DMA half-transfer callback
  153. (+) LevelOutOfWindowCallback : ADC analog watchdog 1 callback
  154. (+) ErrorCallback : ADC error callback
  155. (+) InjectedConvCpltCallback : ADC group injected conversion complete callback
  156. (+) InjectedQueueOverflowCallback : ADC group injected context queue overflow callback
  157. (+) LevelOutOfWindow2Callback : ADC analog watchdog 2 callback
  158. (+) LevelOutOfWindow3Callback : ADC analog watchdog 3 callback
  159. (+) EndOfSamplingCallback : ADC end of sampling callback
  160. (+) MspInitCallback : ADC Msp Init callback
  161. (+) MspDeInitCallback : ADC Msp DeInit callback
  162. This function takes as parameters the HAL peripheral handle, the Callback ID
  163. and a pointer to the user callback function.
  164. [..]
  165. Use function @ref HAL_ADC_UnRegisterCallback to reset a callback to the default
  166. weak function.
  167. [..]
  168. @ref HAL_ADC_UnRegisterCallback takes as parameters the HAL peripheral handle,
  169. and the Callback ID.
  170. This function allows to reset following callbacks:
  171. (+) ConvCpltCallback : ADC conversion complete callback
  172. (+) ConvHalfCpltCallback : ADC conversion DMA half-transfer callback
  173. (+) LevelOutOfWindowCallback : ADC analog watchdog 1 callback
  174. (+) ErrorCallback : ADC error callback
  175. (+) InjectedConvCpltCallback : ADC group injected conversion complete callback
  176. (+) InjectedQueueOverflowCallback : ADC group injected context queue overflow callback
  177. (+) LevelOutOfWindow2Callback : ADC analog watchdog 2 callback
  178. (+) LevelOutOfWindow3Callback : ADC analog watchdog 3 callback
  179. (+) EndOfSamplingCallback : ADC end of sampling callback
  180. (+) MspInitCallback : ADC Msp Init callback
  181. (+) MspDeInitCallback : ADC Msp DeInit callback
  182. [..]
  183. By default, after the @ref HAL_ADC_Init() and when the state is @ref HAL_ADC_STATE_RESET
  184. all callbacks are set to the corresponding weak functions:
  185. examples @ref HAL_ADC_ConvCpltCallback(), @ref HAL_ADC_ErrorCallback().
  186. Exception done for MspInit and MspDeInit functions that are
  187. reset to the legacy weak functions in the @ref HAL_ADC_Init()/ @ref HAL_ADC_DeInit() only when
  188. these callbacks are null (not registered beforehand).
  189. [..]
  190. If MspInit or MspDeInit are not null, the @ref HAL_ADC_Init()/ @ref HAL_ADC_DeInit()
  191. keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
  192. [..]
  193. Callbacks can be registered/unregistered in @ref HAL_ADC_STATE_READY state only.
  194. Exception done MspInit/MspDeInit functions that can be registered/unregistered
  195. in @ref HAL_ADC_STATE_READY or @ref HAL_ADC_STATE_RESET state,
  196. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  197. [..]
  198. Then, the user first registers the MspInit/MspDeInit user callbacks
  199. using @ref HAL_ADC_RegisterCallback() before calling @ref HAL_ADC_DeInit()
  200. or @ref HAL_ADC_Init() function.
  201. [..]
  202. When the compilation flag USE_HAL_ADC_REGISTER_CALLBACKS is set to 0 or
  203. not defined, the callback registration feature is not available and all callbacks
  204. are set to the corresponding weak functions.
  205. @endverbatim
  206. ******************************************************************************
  207. * @attention
  208. *
  209. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  210. * All rights reserved.</center></h2>
  211. *
  212. * This software component is licensed by ST under BSD 3-Clause license,
  213. * the "License"; You may not use this file except in compliance with the
  214. * License. You may obtain a copy of the License at:
  215. * opensource.org/licenses/BSD-3-Clause
  216. *
  217. ******************************************************************************
  218. */
  219. /* Includes ------------------------------------------------------------------*/
  220. #include "stm32f4xx_hal.h"
  221. /** @addtogroup STM32F4xx_HAL_Driver
  222. * @{
  223. */
  224. /** @defgroup ADC ADC
  225. * @brief ADC driver modules
  226. * @{
  227. */
  228. #ifdef HAL_ADC_MODULE_ENABLED
  229. /* Private typedef -----------------------------------------------------------*/
  230. /* Private define ------------------------------------------------------------*/
  231. /* Private macro -------------------------------------------------------------*/
  232. /* Private variables ---------------------------------------------------------*/
  233. /** @addtogroup ADC_Private_Functions
  234. * @{
  235. */
  236. /* Private function prototypes -----------------------------------------------*/
  237. static void ADC_Init(ADC_HandleTypeDef* hadc);
  238. static void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma);
  239. static void ADC_DMAError(DMA_HandleTypeDef *hdma);
  240. static void ADC_DMAHalfConvCplt(DMA_HandleTypeDef *hdma);
  241. /**
  242. * @}
  243. */
  244. /* Exported functions --------------------------------------------------------*/
  245. /** @defgroup ADC_Exported_Functions ADC Exported Functions
  246. * @{
  247. */
  248. /** @defgroup ADC_Exported_Functions_Group1 Initialization and de-initialization functions
  249. * @brief Initialization and Configuration functions
  250. *
  251. @verbatim
  252. ===============================================================================
  253. ##### Initialization and de-initialization functions #####
  254. ===============================================================================
  255. [..] This section provides functions allowing to:
  256. (+) Initialize and configure the ADC.
  257. (+) De-initialize the ADC.
  258. @endverbatim
  259. * @{
  260. */
  261. /**
  262. * @brief Initializes the ADCx peripheral according to the specified parameters
  263. * in the ADC_InitStruct and initializes the ADC MSP.
  264. *
  265. * @note This function is used to configure the global features of the ADC (
  266. * ClockPrescaler, Resolution, Data Alignment and number of conversion), however,
  267. * the rest of the configuration parameters are specific to the regular
  268. * channels group (scan mode activation, continuous mode activation,
  269. * External trigger source and edge, DMA continuous request after the
  270. * last transfer and End of conversion selection).
  271. *
  272. * @param hadc pointer to a ADC_HandleTypeDef structure that contains
  273. * the configuration information for the specified ADC.
  274. * @retval HAL status
  275. */
  276. HAL_StatusTypeDef HAL_ADC_Init(ADC_HandleTypeDef* hadc)
  277. {
  278. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  279. /* Check ADC handle */
  280. if(hadc == NULL)
  281. {
  282. return HAL_ERROR;
  283. }
  284. /* Check the parameters */
  285. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  286. assert_param(IS_ADC_CLOCKPRESCALER(hadc->Init.ClockPrescaler));
  287. assert_param(IS_ADC_RESOLUTION(hadc->Init.Resolution));
  288. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ScanConvMode));
  289. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
  290. assert_param(IS_ADC_EXT_TRIG(hadc->Init.ExternalTrigConv));
  291. assert_param(IS_ADC_DATA_ALIGN(hadc->Init.DataAlign));
  292. assert_param(IS_ADC_REGULAR_LENGTH(hadc->Init.NbrOfConversion));
  293. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DMAContinuousRequests));
  294. assert_param(IS_ADC_EOCSelection(hadc->Init.EOCSelection));
  295. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DiscontinuousConvMode));
  296. if(hadc->Init.ExternalTrigConv != ADC_SOFTWARE_START)
  297. {
  298. assert_param(IS_ADC_EXT_TRIG_EDGE(hadc->Init.ExternalTrigConvEdge));
  299. }
  300. if(hadc->State == HAL_ADC_STATE_RESET)
  301. {
  302. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  303. /* Init the ADC Callback settings */
  304. hadc->ConvCpltCallback = HAL_ADC_ConvCpltCallback; /* Legacy weak callback */
  305. hadc->ConvHalfCpltCallback = HAL_ADC_ConvHalfCpltCallback; /* Legacy weak callback */
  306. hadc->LevelOutOfWindowCallback = HAL_ADC_LevelOutOfWindowCallback; /* Legacy weak callback */
  307. hadc->ErrorCallback = HAL_ADC_ErrorCallback; /* Legacy weak callback */
  308. hadc->InjectedConvCpltCallback = HAL_ADCEx_InjectedConvCpltCallback; /* Legacy weak callback */
  309. if (hadc->MspInitCallback == NULL)
  310. {
  311. hadc->MspInitCallback = HAL_ADC_MspInit; /* Legacy weak MspInit */
  312. }
  313. /* Init the low level hardware */
  314. hadc->MspInitCallback(hadc);
  315. #else
  316. /* Init the low level hardware */
  317. HAL_ADC_MspInit(hadc);
  318. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  319. /* Initialize ADC error code */
  320. ADC_CLEAR_ERRORCODE(hadc);
  321. /* Allocate lock resource and initialize it */
  322. hadc->Lock = HAL_UNLOCKED;
  323. }
  324. /* Configuration of ADC parameters if previous preliminary actions are */
  325. /* correctly completed. */
  326. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL))
  327. {
  328. /* Set ADC state */
  329. ADC_STATE_CLR_SET(hadc->State,
  330. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  331. HAL_ADC_STATE_BUSY_INTERNAL);
  332. /* Set ADC parameters */
  333. ADC_Init(hadc);
  334. /* Set ADC error code to none */
  335. ADC_CLEAR_ERRORCODE(hadc);
  336. /* Set the ADC state */
  337. ADC_STATE_CLR_SET(hadc->State,
  338. HAL_ADC_STATE_BUSY_INTERNAL,
  339. HAL_ADC_STATE_READY);
  340. }
  341. else
  342. {
  343. tmp_hal_status = HAL_ERROR;
  344. }
  345. /* Release Lock */
  346. __HAL_UNLOCK(hadc);
  347. /* Return function status */
  348. return tmp_hal_status;
  349. }
  350. /**
  351. * @brief Deinitializes the ADCx peripheral registers to their default reset values.
  352. * @param hadc pointer to a ADC_HandleTypeDef structure that contains
  353. * the configuration information for the specified ADC.
  354. * @retval HAL status
  355. */
  356. HAL_StatusTypeDef HAL_ADC_DeInit(ADC_HandleTypeDef* hadc)
  357. {
  358. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  359. /* Check ADC handle */
  360. if(hadc == NULL)
  361. {
  362. return HAL_ERROR;
  363. }
  364. /* Check the parameters */
  365. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  366. /* Set ADC state */
  367. SET_BIT(hadc->State, HAL_ADC_STATE_BUSY_INTERNAL);
  368. /* Stop potential conversion on going, on regular and injected groups */
  369. /* Disable ADC peripheral */
  370. __HAL_ADC_DISABLE(hadc);
  371. /* Configuration of ADC parameters if previous preliminary actions are */
  372. /* correctly completed. */
  373. if(HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_ADON))
  374. {
  375. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  376. if (hadc->MspDeInitCallback == NULL)
  377. {
  378. hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit */
  379. }
  380. /* DeInit the low level hardware: RCC clock, NVIC */
  381. hadc->MspDeInitCallback(hadc);
  382. #else
  383. /* DeInit the low level hardware: RCC clock, NVIC */
  384. HAL_ADC_MspDeInit(hadc);
  385. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  386. /* Set ADC error code to none */
  387. ADC_CLEAR_ERRORCODE(hadc);
  388. /* Set ADC state */
  389. hadc->State = HAL_ADC_STATE_RESET;
  390. }
  391. /* Process unlocked */
  392. __HAL_UNLOCK(hadc);
  393. /* Return function status */
  394. return tmp_hal_status;
  395. }
  396. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  397. /**
  398. * @brief Register a User ADC Callback
  399. * To be used instead of the weak predefined callback
  400. * @param hadc Pointer to a ADC_HandleTypeDef structure that contains
  401. * the configuration information for the specified ADC.
  402. * @param CallbackID ID of the callback to be registered
  403. * This parameter can be one of the following values:
  404. * @arg @ref HAL_ADC_CONVERSION_COMPLETE_CB_ID ADC conversion complete callback ID
  405. * @arg @ref HAL_ADC_CONVERSION_HALF_CB_ID ADC conversion DMA half-transfer callback ID
  406. * @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID ADC analog watchdog 1 callback ID
  407. * @arg @ref HAL_ADC_ERROR_CB_ID ADC error callback ID
  408. * @arg @ref HAL_ADC_INJ_CONVERSION_COMPLETE_CB_ID ADC group injected conversion complete callback ID
  409. * @arg @ref HAL_ADC_MSPINIT_CB_ID ADC Msp Init callback ID
  410. * @arg @ref HAL_ADC_MSPDEINIT_CB_ID ADC Msp DeInit callback ID
  411. * @param pCallback pointer to the Callback function
  412. * @retval HAL status
  413. */
  414. HAL_StatusTypeDef HAL_ADC_RegisterCallback(ADC_HandleTypeDef *hadc, HAL_ADC_CallbackIDTypeDef CallbackID, pADC_CallbackTypeDef pCallback)
  415. {
  416. HAL_StatusTypeDef status = HAL_OK;
  417. if (pCallback == NULL)
  418. {
  419. /* Update the error code */
  420. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  421. return HAL_ERROR;
  422. }
  423. if ((hadc->State & HAL_ADC_STATE_READY) != 0UL)
  424. {
  425. switch (CallbackID)
  426. {
  427. case HAL_ADC_CONVERSION_COMPLETE_CB_ID :
  428. hadc->ConvCpltCallback = pCallback;
  429. break;
  430. case HAL_ADC_CONVERSION_HALF_CB_ID :
  431. hadc->ConvHalfCpltCallback = pCallback;
  432. break;
  433. case HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID :
  434. hadc->LevelOutOfWindowCallback = pCallback;
  435. break;
  436. case HAL_ADC_ERROR_CB_ID :
  437. hadc->ErrorCallback = pCallback;
  438. break;
  439. case HAL_ADC_INJ_CONVERSION_COMPLETE_CB_ID :
  440. hadc->InjectedConvCpltCallback = pCallback;
  441. break;
  442. case HAL_ADC_MSPINIT_CB_ID :
  443. hadc->MspInitCallback = pCallback;
  444. break;
  445. case HAL_ADC_MSPDEINIT_CB_ID :
  446. hadc->MspDeInitCallback = pCallback;
  447. break;
  448. default :
  449. /* Update the error code */
  450. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  451. /* Return error status */
  452. status = HAL_ERROR;
  453. break;
  454. }
  455. }
  456. else if (HAL_ADC_STATE_RESET == hadc->State)
  457. {
  458. switch (CallbackID)
  459. {
  460. case HAL_ADC_MSPINIT_CB_ID :
  461. hadc->MspInitCallback = pCallback;
  462. break;
  463. case HAL_ADC_MSPDEINIT_CB_ID :
  464. hadc->MspDeInitCallback = pCallback;
  465. break;
  466. default :
  467. /* Update the error code */
  468. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  469. /* Return error status */
  470. status = HAL_ERROR;
  471. break;
  472. }
  473. }
  474. else
  475. {
  476. /* Update the error code */
  477. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  478. /* Return error status */
  479. status = HAL_ERROR;
  480. }
  481. return status;
  482. }
  483. /**
  484. * @brief Unregister a ADC Callback
  485. * ADC callback is redirected to the weak predefined callback
  486. * @param hadc Pointer to a ADC_HandleTypeDef structure that contains
  487. * the configuration information for the specified ADC.
  488. * @param CallbackID ID of the callback to be unregistered
  489. * This parameter can be one of the following values:
  490. * @arg @ref HAL_ADC_CONVERSION_COMPLETE_CB_ID ADC conversion complete callback ID
  491. * @arg @ref HAL_ADC_CONVERSION_HALF_CB_ID ADC conversion DMA half-transfer callback ID
  492. * @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID ADC analog watchdog 1 callback ID
  493. * @arg @ref HAL_ADC_ERROR_CB_ID ADC error callback ID
  494. * @arg @ref HAL_ADC_INJ_CONVERSION_COMPLETE_CB_ID ADC group injected conversion complete callback ID
  495. * @arg @ref HAL_ADC_MSPINIT_CB_ID ADC Msp Init callback ID
  496. * @arg @ref HAL_ADC_MSPDEINIT_CB_ID ADC Msp DeInit callback ID
  497. * @retval HAL status
  498. */
  499. HAL_StatusTypeDef HAL_ADC_UnRegisterCallback(ADC_HandleTypeDef *hadc, HAL_ADC_CallbackIDTypeDef CallbackID)
  500. {
  501. HAL_StatusTypeDef status = HAL_OK;
  502. if ((hadc->State & HAL_ADC_STATE_READY) != 0UL)
  503. {
  504. switch (CallbackID)
  505. {
  506. case HAL_ADC_CONVERSION_COMPLETE_CB_ID :
  507. hadc->ConvCpltCallback = HAL_ADC_ConvCpltCallback;
  508. break;
  509. case HAL_ADC_CONVERSION_HALF_CB_ID :
  510. hadc->ConvHalfCpltCallback = HAL_ADC_ConvHalfCpltCallback;
  511. break;
  512. case HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID :
  513. hadc->LevelOutOfWindowCallback = HAL_ADC_LevelOutOfWindowCallback;
  514. break;
  515. case HAL_ADC_ERROR_CB_ID :
  516. hadc->ErrorCallback = HAL_ADC_ErrorCallback;
  517. break;
  518. case HAL_ADC_INJ_CONVERSION_COMPLETE_CB_ID :
  519. hadc->InjectedConvCpltCallback = HAL_ADCEx_InjectedConvCpltCallback;
  520. break;
  521. case HAL_ADC_MSPINIT_CB_ID :
  522. hadc->MspInitCallback = HAL_ADC_MspInit; /* Legacy weak MspInit */
  523. break;
  524. case HAL_ADC_MSPDEINIT_CB_ID :
  525. hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit */
  526. break;
  527. default :
  528. /* Update the error code */
  529. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  530. /* Return error status */
  531. status = HAL_ERROR;
  532. break;
  533. }
  534. }
  535. else if (HAL_ADC_STATE_RESET == hadc->State)
  536. {
  537. switch (CallbackID)
  538. {
  539. case HAL_ADC_MSPINIT_CB_ID :
  540. hadc->MspInitCallback = HAL_ADC_MspInit; /* Legacy weak MspInit */
  541. break;
  542. case HAL_ADC_MSPDEINIT_CB_ID :
  543. hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit */
  544. break;
  545. default :
  546. /* Update the error code */
  547. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  548. /* Return error status */
  549. status = HAL_ERROR;
  550. break;
  551. }
  552. }
  553. else
  554. {
  555. /* Update the error code */
  556. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  557. /* Return error status */
  558. status = HAL_ERROR;
  559. }
  560. return status;
  561. }
  562. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  563. /**
  564. * @brief Initializes the ADC MSP.
  565. * @param hadc pointer to a ADC_HandleTypeDef structure that contains
  566. * the configuration information for the specified ADC.
  567. * @retval None
  568. */
  569. __weak void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
  570. {
  571. /* Prevent unused argument(s) compilation warning */
  572. UNUSED(hadc);
  573. /* NOTE : This function Should not be modified, when the callback is needed,
  574. the HAL_ADC_MspInit could be implemented in the user file
  575. */
  576. }
  577. /**
  578. * @brief DeInitializes the ADC MSP.
  579. * @param hadc pointer to a ADC_HandleTypeDef structure that contains
  580. * the configuration information for the specified ADC.
  581. * @retval None
  582. */
  583. __weak void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc)
  584. {
  585. /* Prevent unused argument(s) compilation warning */
  586. UNUSED(hadc);
  587. /* NOTE : This function Should not be modified, when the callback is needed,
  588. the HAL_ADC_MspDeInit could be implemented in the user file
  589. */
  590. }
  591. /**
  592. * @}
  593. */
  594. /** @defgroup ADC_Exported_Functions_Group2 IO operation functions
  595. * @brief IO operation functions
  596. *
  597. @verbatim
  598. ===============================================================================
  599. ##### IO operation functions #####
  600. ===============================================================================
  601. [..] This section provides functions allowing to:
  602. (+) Start conversion of regular channel.
  603. (+) Stop conversion of regular channel.
  604. (+) Start conversion of regular channel and enable interrupt.
  605. (+) Stop conversion of regular channel and disable interrupt.
  606. (+) Start conversion of regular channel and enable DMA transfer.
  607. (+) Stop conversion of regular channel and disable DMA transfer.
  608. (+) Handle ADC interrupt request.
  609. @endverbatim
  610. * @{
  611. */
  612. /**
  613. * @brief Enables ADC and starts conversion of the regular channels.
  614. * @param hadc pointer to a ADC_HandleTypeDef structure that contains
  615. * the configuration information for the specified ADC.
  616. * @retval HAL status
  617. */
  618. HAL_StatusTypeDef HAL_ADC_Start(ADC_HandleTypeDef* hadc)
  619. {
  620. __IO uint32_t counter = 0U;
  621. ADC_Common_TypeDef *tmpADC_Common;
  622. /* Check the parameters */
  623. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
  624. assert_param(IS_ADC_EXT_TRIG_EDGE(hadc->Init.ExternalTrigConvEdge));
  625. /* Process locked */
  626. __HAL_LOCK(hadc);
  627. /* Enable the ADC peripheral */
  628. /* Check if ADC peripheral is disabled in order to enable it and wait during
  629. Tstab time the ADC's stabilization */
  630. if((hadc->Instance->CR2 & ADC_CR2_ADON) != ADC_CR2_ADON)
  631. {
  632. /* Enable the Peripheral */
  633. __HAL_ADC_ENABLE(hadc);
  634. /* Delay for ADC stabilization time */
  635. /* Compute number of CPU cycles to wait for */
  636. counter = (ADC_STAB_DELAY_US * (SystemCoreClock / 1000000U));
  637. while(counter != 0U)
  638. {
  639. counter--;
  640. }
  641. }
  642. /* Start conversion if ADC is effectively enabled */
  643. if(HAL_IS_BIT_SET(hadc->Instance->CR2, ADC_CR2_ADON))
  644. {
  645. /* Set ADC state */
  646. /* - Clear state bitfield related to regular group conversion results */
  647. /* - Set state bitfield related to regular group operation */
  648. ADC_STATE_CLR_SET(hadc->State,
  649. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR,
  650. HAL_ADC_STATE_REG_BUSY);
  651. /* If conversions on group regular are also triggering group injected, */
  652. /* update ADC state. */
  653. if (READ_BIT(hadc->Instance->CR1, ADC_CR1_JAUTO) != RESET)
  654. {
  655. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
  656. }
  657. /* State machine update: Check if an injected conversion is ongoing */
  658. if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  659. {
  660. /* Reset ADC error code fields related to conversions on group regular */
  661. CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA));
  662. }
  663. else
  664. {
  665. /* Reset ADC all error code fields */
  666. ADC_CLEAR_ERRORCODE(hadc);
  667. }
  668. /* Process unlocked */
  669. /* Unlock before starting ADC conversions: in case of potential */
  670. /* interruption, to let the process to ADC IRQ Handler. */
  671. __HAL_UNLOCK(hadc);
  672. /* Pointer to the common control register to which is belonging hadc */
  673. /* (Depending on STM32F4 product, there may be up to 3 ADCs and 1 common */
  674. /* control register) */
  675. tmpADC_Common = ADC_COMMON_REGISTER(hadc);
  676. /* Clear regular group conversion flag and overrun flag */
  677. /* (To ensure of no unknown state from potential previous ADC operations) */
  678. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOC | ADC_FLAG_OVR);
  679. /* Check if Multimode enabled */
  680. if(HAL_IS_BIT_CLR(tmpADC_Common->CCR, ADC_CCR_MULTI))
  681. {
  682. #if defined(ADC2) && defined(ADC3)
  683. if((hadc->Instance == ADC1) || ((hadc->Instance == ADC2) && ((ADC->CCR & ADC_CCR_MULTI_Msk) < ADC_CCR_MULTI_0)) \
  684. || ((hadc->Instance == ADC3) && ((ADC->CCR & ADC_CCR_MULTI_Msk) < ADC_CCR_MULTI_4)))
  685. {
  686. #endif /* ADC2 || ADC3 */
  687. /* if no external trigger present enable software conversion of regular channels */
  688. if((hadc->Instance->CR2 & ADC_CR2_EXTEN) == RESET)
  689. {
  690. /* Enable the selected ADC software conversion for regular group */
  691. hadc->Instance->CR2 |= (uint32_t)ADC_CR2_SWSTART;
  692. }
  693. #if defined(ADC2) && defined(ADC3)
  694. }
  695. #endif /* ADC2 || ADC3 */
  696. }
  697. else
  698. {
  699. /* if instance of handle correspond to ADC1 and no external trigger present enable software conversion of regular channels */
  700. if((hadc->Instance == ADC1) && ((hadc->Instance->CR2 & ADC_CR2_EXTEN) == RESET))
  701. {
  702. /* Enable the selected ADC software conversion for regular group */
  703. hadc->Instance->CR2 |= (uint32_t)ADC_CR2_SWSTART;
  704. }
  705. }
  706. }
  707. /* Return function status */
  708. return HAL_OK;
  709. }
  710. /**
  711. * @brief Disables ADC and stop conversion of regular channels.
  712. *
  713. * @note Caution: This function will stop also injected channels.
  714. *
  715. * @param hadc pointer to a ADC_HandleTypeDef structure that contains
  716. * the configuration information for the specified ADC.
  717. *
  718. * @retval HAL status.
  719. */
  720. HAL_StatusTypeDef HAL_ADC_Stop(ADC_HandleTypeDef* hadc)
  721. {
  722. /* Check the parameters */
  723. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  724. /* Process locked */
  725. __HAL_LOCK(hadc);
  726. /* Stop potential conversion on going, on regular and injected groups */
  727. /* Disable ADC peripheral */
  728. __HAL_ADC_DISABLE(hadc);
  729. /* Check if ADC is effectively disabled */
  730. if(HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_ADON))
  731. {
  732. /* Set ADC state */
  733. ADC_STATE_CLR_SET(hadc->State,
  734. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  735. HAL_ADC_STATE_READY);
  736. }
  737. /* Process unlocked */
  738. __HAL_UNLOCK(hadc);
  739. /* Return function status */
  740. return HAL_OK;
  741. }
  742. /**
  743. * @brief Poll for regular conversion complete
  744. * @note ADC conversion flags EOS (end of sequence) and EOC (end of
  745. * conversion) are cleared by this function.
  746. * @note This function cannot be used in a particular setup: ADC configured
  747. * in DMA mode and polling for end of each conversion (ADC init
  748. * parameter "EOCSelection" set to ADC_EOC_SINGLE_CONV).
  749. * In this case, DMA resets the flag EOC and polling cannot be
  750. * performed on each conversion. Nevertheless, polling can still
  751. * be performed on the complete sequence.
  752. * @param hadc pointer to a ADC_HandleTypeDef structure that contains
  753. * the configuration information for the specified ADC.
  754. * @param Timeout Timeout value in millisecond.
  755. * @retval HAL status
  756. */
  757. HAL_StatusTypeDef HAL_ADC_PollForConversion(ADC_HandleTypeDef* hadc, uint32_t Timeout)
  758. {
  759. uint32_t tickstart = 0U;
  760. /* Verification that ADC configuration is compliant with polling for */
  761. /* each conversion: */
  762. /* Particular case is ADC configured in DMA mode and ADC sequencer with */
  763. /* several ranks and polling for end of each conversion. */
  764. /* For code simplicity sake, this particular case is generalized to */
  765. /* ADC configured in DMA mode and polling for end of each conversion. */
  766. if (HAL_IS_BIT_SET(hadc->Instance->CR2, ADC_CR2_EOCS) &&
  767. HAL_IS_BIT_SET(hadc->Instance->CR2, ADC_CR2_DMA) )
  768. {
  769. /* Update ADC state machine to error */
  770. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  771. /* Process unlocked */
  772. __HAL_UNLOCK(hadc);
  773. return HAL_ERROR;
  774. }
  775. /* Get tick */
  776. tickstart = HAL_GetTick();
  777. /* Check End of conversion flag */
  778. while(!(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOC)))
  779. {
  780. /* Check if timeout is disabled (set to infinite wait) */
  781. if(Timeout != HAL_MAX_DELAY)
  782. {
  783. if((Timeout == 0U) || ((HAL_GetTick() - tickstart ) > Timeout))
  784. {
  785. /* Update ADC state machine to timeout */
  786. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  787. /* Process unlocked */
  788. __HAL_UNLOCK(hadc);
  789. return HAL_TIMEOUT;
  790. }
  791. }
  792. }
  793. /* Clear regular group conversion flag */
  794. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_STRT | ADC_FLAG_EOC);
  795. /* Update ADC state machine */
  796. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  797. /* Determine whether any further conversion upcoming on group regular */
  798. /* by external trigger, continuous mode or scan sequence on going. */
  799. /* Note: On STM32F4, there is no independent flag of end of sequence. */
  800. /* The test of scan sequence on going is done either with scan */
  801. /* sequence disabled or with end of conversion flag set to */
  802. /* of end of sequence. */
  803. if(ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  804. (hadc->Init.ContinuousConvMode == DISABLE) &&
  805. (HAL_IS_BIT_CLR(hadc->Instance->SQR1, ADC_SQR1_L) ||
  806. HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_EOCS) ) )
  807. {
  808. /* Set ADC state */
  809. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  810. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  811. {
  812. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  813. }
  814. }
  815. /* Return ADC state */
  816. return HAL_OK;
  817. }
  818. /**
  819. * @brief Poll for conversion event
  820. * @param hadc pointer to a ADC_HandleTypeDef structure that contains
  821. * the configuration information for the specified ADC.
  822. * @param EventType the ADC event type.
  823. * This parameter can be one of the following values:
  824. * @arg ADC_AWD_EVENT: ADC Analog watch Dog event.
  825. * @arg ADC_OVR_EVENT: ADC Overrun event.
  826. * @param Timeout Timeout value in millisecond.
  827. * @retval HAL status
  828. */
  829. HAL_StatusTypeDef HAL_ADC_PollForEvent(ADC_HandleTypeDef* hadc, uint32_t EventType, uint32_t Timeout)
  830. {
  831. uint32_t tickstart = 0U;
  832. /* Check the parameters */
  833. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  834. assert_param(IS_ADC_EVENT_TYPE(EventType));
  835. /* Get tick */
  836. tickstart = HAL_GetTick();
  837. /* Check selected event flag */
  838. while(!(__HAL_ADC_GET_FLAG(hadc,EventType)))
  839. {
  840. /* Check for the Timeout */
  841. if(Timeout != HAL_MAX_DELAY)
  842. {
  843. if((Timeout == 0U) || ((HAL_GetTick() - tickstart ) > Timeout))
  844. {
  845. /* Update ADC state machine to timeout */
  846. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  847. /* Process unlocked */
  848. __HAL_UNLOCK(hadc);
  849. return HAL_TIMEOUT;
  850. }
  851. }
  852. }
  853. /* Analog watchdog (level out of window) event */
  854. if(EventType == ADC_AWD_EVENT)
  855. {
  856. /* Set ADC state */
  857. SET_BIT(hadc->State, HAL_ADC_STATE_AWD1);
  858. /* Clear ADC analog watchdog flag */
  859. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD);
  860. }
  861. /* Overrun event */
  862. else
  863. {
  864. /* Set ADC state */
  865. SET_BIT(hadc->State, HAL_ADC_STATE_REG_OVR);
  866. /* Set ADC error code to overrun */
  867. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_OVR);
  868. /* Clear ADC overrun flag */
  869. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  870. }
  871. /* Return ADC state */
  872. return HAL_OK;
  873. }
  874. /**
  875. * @brief Enables the interrupt and starts ADC conversion of regular channels.
  876. * @param hadc pointer to a ADC_HandleTypeDef structure that contains
  877. * the configuration information for the specified ADC.
  878. * @retval HAL status.
  879. */
  880. HAL_StatusTypeDef HAL_ADC_Start_IT(ADC_HandleTypeDef* hadc)
  881. {
  882. __IO uint32_t counter = 0U;
  883. ADC_Common_TypeDef *tmpADC_Common;
  884. /* Check the parameters */
  885. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
  886. assert_param(IS_ADC_EXT_TRIG_EDGE(hadc->Init.ExternalTrigConvEdge));
  887. /* Process locked */
  888. __HAL_LOCK(hadc);
  889. /* Enable the ADC peripheral */
  890. /* Check if ADC peripheral is disabled in order to enable it and wait during
  891. Tstab time the ADC's stabilization */
  892. if((hadc->Instance->CR2 & ADC_CR2_ADON) != ADC_CR2_ADON)
  893. {
  894. /* Enable the Peripheral */
  895. __HAL_ADC_ENABLE(hadc);
  896. /* Delay for ADC stabilization time */
  897. /* Compute number of CPU cycles to wait for */
  898. counter = (ADC_STAB_DELAY_US * (SystemCoreClock / 1000000U));
  899. while(counter != 0U)
  900. {
  901. counter--;
  902. }
  903. }
  904. /* Start conversion if ADC is effectively enabled */
  905. if(HAL_IS_BIT_SET(hadc->Instance->CR2, ADC_CR2_ADON))
  906. {
  907. /* Set ADC state */
  908. /* - Clear state bitfield related to regular group conversion results */
  909. /* - Set state bitfield related to regular group operation */
  910. ADC_STATE_CLR_SET(hadc->State,
  911. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR,
  912. HAL_ADC_STATE_REG_BUSY);
  913. /* If conversions on group regular are also triggering group injected, */
  914. /* update ADC state. */
  915. if (READ_BIT(hadc->Instance->CR1, ADC_CR1_JAUTO) != RESET)
  916. {
  917. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
  918. }
  919. /* State machine update: Check if an injected conversion is ongoing */
  920. if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  921. {
  922. /* Reset ADC error code fields related to conversions on group regular */
  923. CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA));
  924. }
  925. else
  926. {
  927. /* Reset ADC all error code fields */
  928. ADC_CLEAR_ERRORCODE(hadc);
  929. }
  930. /* Process unlocked */
  931. /* Unlock before starting ADC conversions: in case of potential */
  932. /* interruption, to let the process to ADC IRQ Handler. */
  933. __HAL_UNLOCK(hadc);
  934. /* Pointer to the common control register to which is belonging hadc */
  935. /* (Depending on STM32F4 product, there may be up to 3 ADCs and 1 common */
  936. /* control register) */
  937. tmpADC_Common = ADC_COMMON_REGISTER(hadc);
  938. /* Clear regular group conversion flag and overrun flag */
  939. /* (To ensure of no unknown state from potential previous ADC operations) */
  940. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOC | ADC_FLAG_OVR);
  941. /* Enable end of conversion interrupt for regular group */
  942. __HAL_ADC_ENABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_OVR));
  943. /* Check if Multimode enabled */
  944. if(HAL_IS_BIT_CLR(tmpADC_Common->CCR, ADC_CCR_MULTI))
  945. {
  946. #if defined(ADC2) && defined(ADC3)
  947. if((hadc->Instance == ADC1) || ((hadc->Instance == ADC2) && ((ADC->CCR & ADC_CCR_MULTI_Msk) < ADC_CCR_MULTI_0)) \
  948. || ((hadc->Instance == ADC3) && ((ADC->CCR & ADC_CCR_MULTI_Msk) < ADC_CCR_MULTI_4)))
  949. {
  950. #endif /* ADC2 || ADC3 */
  951. /* if no external trigger present enable software conversion of regular channels */
  952. if((hadc->Instance->CR2 & ADC_CR2_EXTEN) == RESET)
  953. {
  954. /* Enable the selected ADC software conversion for regular group */
  955. hadc->Instance->CR2 |= (uint32_t)ADC_CR2_SWSTART;
  956. }
  957. #if defined(ADC2) && defined(ADC3)
  958. }
  959. #endif /* ADC2 || ADC3 */
  960. }
  961. else
  962. {
  963. /* if instance of handle correspond to ADC1 and no external trigger present enable software conversion of regular channels */
  964. if((hadc->Instance == ADC1) && ((hadc->Instance->CR2 & ADC_CR2_EXTEN) == RESET))
  965. {
  966. /* Enable the selected ADC software conversion for regular group */
  967. hadc->Instance->CR2 |= (uint32_t)ADC_CR2_SWSTART;
  968. }
  969. }
  970. }
  971. /* Return function status */
  972. return HAL_OK;
  973. }
  974. /**
  975. * @brief Disables the interrupt and stop ADC conversion of regular channels.
  976. *
  977. * @note Caution: This function will stop also injected channels.
  978. *
  979. * @param hadc pointer to a ADC_HandleTypeDef structure that contains
  980. * the configuration information for the specified ADC.
  981. * @retval HAL status.
  982. */
  983. HAL_StatusTypeDef HAL_ADC_Stop_IT(ADC_HandleTypeDef* hadc)
  984. {
  985. /* Check the parameters */
  986. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  987. /* Process locked */
  988. __HAL_LOCK(hadc);
  989. /* Stop potential conversion on going, on regular and injected groups */
  990. /* Disable ADC peripheral */
  991. __HAL_ADC_DISABLE(hadc);
  992. /* Check if ADC is effectively disabled */
  993. if(HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_ADON))
  994. {
  995. /* Disable ADC end of conversion interrupt for regular group */
  996. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_OVR));
  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 HAL_OK;
  1006. }
  1007. /**
  1008. * @brief Handles ADC interrupt request
  1009. * @param hadc pointer to a ADC_HandleTypeDef structure that contains
  1010. * the configuration information for the specified ADC.
  1011. * @retval None
  1012. */
  1013. void HAL_ADC_IRQHandler(ADC_HandleTypeDef* hadc)
  1014. {
  1015. uint32_t tmp1 = 0U, tmp2 = 0U;
  1016. /* Check the parameters */
  1017. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
  1018. assert_param(IS_ADC_REGULAR_LENGTH(hadc->Init.NbrOfConversion));
  1019. assert_param(IS_ADC_EOCSelection(hadc->Init.EOCSelection));
  1020. tmp1 = __HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOC);
  1021. tmp2 = __HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_EOC);
  1022. /* Check End of conversion flag for regular channels */
  1023. if(tmp1 && tmp2)
  1024. {
  1025. /* Update state machine on conversion status if not in error state */
  1026. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL))
  1027. {
  1028. /* Set ADC state */
  1029. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  1030. }
  1031. /* Determine whether any further conversion upcoming on group regular */
  1032. /* by external trigger, continuous mode or scan sequence on going. */
  1033. /* Note: On STM32F4, there is no independent flag of end of sequence. */
  1034. /* The test of scan sequence on going is done either with scan */
  1035. /* sequence disabled or with end of conversion flag set to */
  1036. /* of end of sequence. */
  1037. if(ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  1038. (hadc->Init.ContinuousConvMode == DISABLE) &&
  1039. (HAL_IS_BIT_CLR(hadc->Instance->SQR1, ADC_SQR1_L) ||
  1040. HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_EOCS) ) )
  1041. {
  1042. /* Disable ADC end of single conversion interrupt on group regular */
  1043. /* Note: Overrun interrupt was enabled with EOC interrupt in */
  1044. /* HAL_ADC_Start_IT(), but is not disabled here because can be used */
  1045. /* by overrun IRQ process below. */
  1046. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC);
  1047. /* Set ADC state */
  1048. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  1049. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  1050. {
  1051. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  1052. }
  1053. }
  1054. /* Conversion complete callback */
  1055. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  1056. hadc->ConvCpltCallback(hadc);
  1057. #else
  1058. HAL_ADC_ConvCpltCallback(hadc);
  1059. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  1060. /* Clear regular group conversion flag */
  1061. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_STRT | ADC_FLAG_EOC);
  1062. }
  1063. tmp1 = __HAL_ADC_GET_FLAG(hadc, ADC_FLAG_JEOC);
  1064. tmp2 = __HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_JEOC);
  1065. /* Check End of conversion flag for injected channels */
  1066. if(tmp1 && tmp2)
  1067. {
  1068. /* Update state machine on conversion status if not in error state */
  1069. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL))
  1070. {
  1071. /* Set ADC state */
  1072. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_EOC);
  1073. }
  1074. /* Determine whether any further conversion upcoming on group injected */
  1075. /* by external trigger, scan sequence on going or by automatic injected */
  1076. /* conversion from group regular (same conditions as group regular */
  1077. /* interruption disabling above). */
  1078. if(ADC_IS_SOFTWARE_START_INJECTED(hadc) &&
  1079. (HAL_IS_BIT_CLR(hadc->Instance->JSQR, ADC_JSQR_JL) ||
  1080. HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_EOCS) ) &&
  1081. (HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO) &&
  1082. (ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  1083. (hadc->Init.ContinuousConvMode == DISABLE) ) ) )
  1084. {
  1085. /* Disable ADC end of single conversion interrupt on group injected */
  1086. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC);
  1087. /* Set ADC state */
  1088. CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  1089. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_REG_BUSY))
  1090. {
  1091. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  1092. }
  1093. }
  1094. /* Conversion complete callback */
  1095. /* Conversion complete callback */
  1096. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  1097. hadc->InjectedConvCpltCallback(hadc);
  1098. #else
  1099. HAL_ADCEx_InjectedConvCpltCallback(hadc);
  1100. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  1101. /* Clear injected group conversion flag */
  1102. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_JSTRT | ADC_FLAG_JEOC));
  1103. }
  1104. tmp1 = __HAL_ADC_GET_FLAG(hadc, ADC_FLAG_AWD);
  1105. tmp2 = __HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_AWD);
  1106. /* Check Analog watchdog flag */
  1107. if(tmp1 && tmp2)
  1108. {
  1109. if(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_AWD))
  1110. {
  1111. /* Set ADC state */
  1112. SET_BIT(hadc->State, HAL_ADC_STATE_AWD1);
  1113. /* Level out of window callback */
  1114. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  1115. hadc->LevelOutOfWindowCallback(hadc);
  1116. #else
  1117. HAL_ADC_LevelOutOfWindowCallback(hadc);
  1118. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  1119. /* Clear the ADC analog watchdog flag */
  1120. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD);
  1121. }
  1122. }
  1123. tmp1 = __HAL_ADC_GET_FLAG(hadc, ADC_FLAG_OVR);
  1124. tmp2 = __HAL_ADC_GET_IT_SOURCE(hadc, ADC_IT_OVR);
  1125. /* Check Overrun flag */
  1126. if(tmp1 && tmp2)
  1127. {
  1128. /* Note: On STM32F4, ADC overrun can be set through other parameters */
  1129. /* refer to description of parameter "EOCSelection" for more */
  1130. /* details. */
  1131. /* Set ADC error code to overrun */
  1132. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_OVR);
  1133. /* Clear ADC overrun flag */
  1134. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  1135. /* Error callback */
  1136. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  1137. hadc->ErrorCallback(hadc);
  1138. #else
  1139. HAL_ADC_ErrorCallback(hadc);
  1140. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  1141. /* Clear the Overrun flag */
  1142. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  1143. }
  1144. }
  1145. /**
  1146. * @brief Enables ADC DMA request after last transfer (Single-ADC mode) and enables ADC peripheral
  1147. * @param hadc pointer to a ADC_HandleTypeDef structure that contains
  1148. * the configuration information for the specified ADC.
  1149. * @param pData The destination Buffer address.
  1150. * @param Length The length of data to be transferred from ADC peripheral to memory.
  1151. * @retval HAL status
  1152. */
  1153. HAL_StatusTypeDef HAL_ADC_Start_DMA(ADC_HandleTypeDef* hadc, uint32_t* pData, uint32_t Length)
  1154. {
  1155. __IO uint32_t counter = 0U;
  1156. ADC_Common_TypeDef *tmpADC_Common;
  1157. /* Check the parameters */
  1158. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
  1159. assert_param(IS_ADC_EXT_TRIG_EDGE(hadc->Init.ExternalTrigConvEdge));
  1160. /* Process locked */
  1161. __HAL_LOCK(hadc);
  1162. /* Enable the ADC peripheral */
  1163. /* Check if ADC peripheral is disabled in order to enable it and wait during
  1164. Tstab time the ADC's stabilization */
  1165. if((hadc->Instance->CR2 & ADC_CR2_ADON) != ADC_CR2_ADON)
  1166. {
  1167. /* Enable the Peripheral */
  1168. __HAL_ADC_ENABLE(hadc);
  1169. /* Delay for ADC stabilization time */
  1170. /* Compute number of CPU cycles to wait for */
  1171. counter = (ADC_STAB_DELAY_US * (SystemCoreClock / 1000000U));
  1172. while(counter != 0U)
  1173. {
  1174. counter--;
  1175. }
  1176. }
  1177. /* Start conversion if ADC is effectively enabled */
  1178. if(HAL_IS_BIT_SET(hadc->Instance->CR2, ADC_CR2_ADON))
  1179. {
  1180. /* Set ADC state */
  1181. /* - Clear state bitfield related to regular group conversion results */
  1182. /* - Set state bitfield related to regular group operation */
  1183. ADC_STATE_CLR_SET(hadc->State,
  1184. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR,
  1185. HAL_ADC_STATE_REG_BUSY);
  1186. /* If conversions on group regular are also triggering group injected, */
  1187. /* update ADC state. */
  1188. if (READ_BIT(hadc->Instance->CR1, ADC_CR1_JAUTO) != RESET)
  1189. {
  1190. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
  1191. }
  1192. /* State machine update: Check if an injected conversion is ongoing */
  1193. if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  1194. {
  1195. /* Reset ADC error code fields related to conversions on group regular */
  1196. CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA));
  1197. }
  1198. else
  1199. {
  1200. /* Reset ADC all error code fields */
  1201. ADC_CLEAR_ERRORCODE(hadc);
  1202. }
  1203. /* Process unlocked */
  1204. /* Unlock before starting ADC conversions: in case of potential */
  1205. /* interruption, to let the process to ADC IRQ Handler. */
  1206. __HAL_UNLOCK(hadc);
  1207. /* Pointer to the common control register to which is belonging hadc */
  1208. /* (Depending on STM32F4 product, there may be up to 3 ADCs and 1 common */
  1209. /* control register) */
  1210. tmpADC_Common = ADC_COMMON_REGISTER(hadc);
  1211. /* Set the DMA transfer complete callback */
  1212. hadc->DMA_Handle->XferCpltCallback = ADC_DMAConvCplt;
  1213. /* Set the DMA half transfer complete callback */
  1214. hadc->DMA_Handle->XferHalfCpltCallback = ADC_DMAHalfConvCplt;
  1215. /* Set the DMA error callback */
  1216. hadc->DMA_Handle->XferErrorCallback = ADC_DMAError;
  1217. /* Manage ADC and DMA start: ADC overrun interruption, DMA start, ADC */
  1218. /* start (in case of SW start): */
  1219. /* Clear regular group conversion flag and overrun flag */
  1220. /* (To ensure of no unknown state from potential previous ADC operations) */
  1221. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOC | ADC_FLAG_OVR);
  1222. /* Enable ADC overrun interrupt */
  1223. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);
  1224. /* Enable ADC DMA mode */
  1225. hadc->Instance->CR2 |= ADC_CR2_DMA;
  1226. /* Start the DMA channel */
  1227. HAL_DMA_Start_IT(hadc->DMA_Handle, (uint32_t)&hadc->Instance->DR, (uint32_t)pData, Length);
  1228. /* Check if Multimode enabled */
  1229. if(HAL_IS_BIT_CLR(tmpADC_Common->CCR, ADC_CCR_MULTI))
  1230. {
  1231. #if defined(ADC2) && defined(ADC3)
  1232. if((hadc->Instance == ADC1) || ((hadc->Instance == ADC2) && ((ADC->CCR & ADC_CCR_MULTI_Msk) < ADC_CCR_MULTI_0)) \
  1233. || ((hadc->Instance == ADC3) && ((ADC->CCR & ADC_CCR_MULTI_Msk) < ADC_CCR_MULTI_4)))
  1234. {
  1235. #endif /* ADC2 || ADC3 */
  1236. /* if no external trigger present enable software conversion of regular channels */
  1237. if((hadc->Instance->CR2 & ADC_CR2_EXTEN) == RESET)
  1238. {
  1239. /* Enable the selected ADC software conversion for regular group */
  1240. hadc->Instance->CR2 |= (uint32_t)ADC_CR2_SWSTART;
  1241. }
  1242. #if defined(ADC2) && defined(ADC3)
  1243. }
  1244. #endif /* ADC2 || ADC3 */
  1245. }
  1246. else
  1247. {
  1248. /* if instance of handle correspond to ADC1 and no external trigger present enable software conversion of regular channels */
  1249. if((hadc->Instance == ADC1) && ((hadc->Instance->CR2 & ADC_CR2_EXTEN) == RESET))
  1250. {
  1251. /* Enable the selected ADC software conversion for regular group */
  1252. hadc->Instance->CR2 |= (uint32_t)ADC_CR2_SWSTART;
  1253. }
  1254. }
  1255. }
  1256. /* Return function status */
  1257. return HAL_OK;
  1258. }
  1259. /**
  1260. * @brief Disables ADC DMA (Single-ADC mode) and disables ADC peripheral
  1261. * @param hadc pointer to a ADC_HandleTypeDef structure that contains
  1262. * the configuration information for the specified ADC.
  1263. * @retval HAL status
  1264. */
  1265. HAL_StatusTypeDef HAL_ADC_Stop_DMA(ADC_HandleTypeDef* hadc)
  1266. {
  1267. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1268. /* Check the parameters */
  1269. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1270. /* Process locked */
  1271. __HAL_LOCK(hadc);
  1272. /* Stop potential conversion on going, on regular and injected groups */
  1273. /* Disable ADC peripheral */
  1274. __HAL_ADC_DISABLE(hadc);
  1275. /* Check if ADC is effectively disabled */
  1276. if(HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_ADON))
  1277. {
  1278. /* Disable the selected ADC DMA mode */
  1279. hadc->Instance->CR2 &= ~ADC_CR2_DMA;
  1280. /* Disable the DMA channel (in case of DMA in circular mode or stop while */
  1281. /* DMA transfer is on going) */
  1282. tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
  1283. /* Disable ADC overrun interrupt */
  1284. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
  1285. /* Set ADC state */
  1286. ADC_STATE_CLR_SET(hadc->State,
  1287. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  1288. HAL_ADC_STATE_READY);
  1289. }
  1290. /* Process unlocked */
  1291. __HAL_UNLOCK(hadc);
  1292. /* Return function status */
  1293. return tmp_hal_status;
  1294. }
  1295. /**
  1296. * @brief Gets the converted value from data register of regular channel.
  1297. * @param hadc pointer to a ADC_HandleTypeDef structure that contains
  1298. * the configuration information for the specified ADC.
  1299. * @retval Converted value
  1300. */
  1301. uint32_t HAL_ADC_GetValue(ADC_HandleTypeDef* hadc)
  1302. {
  1303. /* Return the selected ADC converted value */
  1304. return hadc->Instance->DR;
  1305. }
  1306. /**
  1307. * @brief Regular conversion complete callback in non blocking mode
  1308. * @param hadc pointer to a ADC_HandleTypeDef structure that contains
  1309. * the configuration information for the specified ADC.
  1310. * @retval None
  1311. */
  1312. __weak void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
  1313. {
  1314. /* Prevent unused argument(s) compilation warning */
  1315. UNUSED(hadc);
  1316. /* NOTE : This function Should not be modified, when the callback is needed,
  1317. the HAL_ADC_ConvCpltCallback could be implemented in the user file
  1318. */
  1319. }
  1320. /**
  1321. * @brief Regular conversion half DMA transfer callback in non blocking mode
  1322. * @param hadc pointer to a ADC_HandleTypeDef structure that contains
  1323. * the configuration information for the specified ADC.
  1324. * @retval None
  1325. */
  1326. __weak void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc)
  1327. {
  1328. /* Prevent unused argument(s) compilation warning */
  1329. UNUSED(hadc);
  1330. /* NOTE : This function Should not be modified, when the callback is needed,
  1331. the HAL_ADC_ConvHalfCpltCallback could be implemented in the user file
  1332. */
  1333. }
  1334. /**
  1335. * @brief Analog watchdog callback in non blocking mode
  1336. * @param hadc pointer to a ADC_HandleTypeDef structure that contains
  1337. * the configuration information for the specified ADC.
  1338. * @retval None
  1339. */
  1340. __weak void HAL_ADC_LevelOutOfWindowCallback(ADC_HandleTypeDef* hadc)
  1341. {
  1342. /* Prevent unused argument(s) compilation warning */
  1343. UNUSED(hadc);
  1344. /* NOTE : This function Should not be modified, when the callback is needed,
  1345. the HAL_ADC_LevelOoutOfWindowCallback could be implemented in the user file
  1346. */
  1347. }
  1348. /**
  1349. * @brief Error ADC callback.
  1350. * @note In case of error due to overrun when using ADC with DMA transfer
  1351. * (HAL ADC handle paramater "ErrorCode" to state "HAL_ADC_ERROR_OVR"):
  1352. * - Reinitialize the DMA using function "HAL_ADC_Stop_DMA()".
  1353. * - If needed, restart a new ADC conversion using function
  1354. * "HAL_ADC_Start_DMA()"
  1355. * (this function is also clearing overrun flag)
  1356. * @param hadc pointer to a ADC_HandleTypeDef structure that contains
  1357. * the configuration information for the specified ADC.
  1358. * @retval None
  1359. */
  1360. __weak void HAL_ADC_ErrorCallback(ADC_HandleTypeDef *hadc)
  1361. {
  1362. /* Prevent unused argument(s) compilation warning */
  1363. UNUSED(hadc);
  1364. /* NOTE : This function Should not be modified, when the callback is needed,
  1365. the HAL_ADC_ErrorCallback could be implemented in the user file
  1366. */
  1367. }
  1368. /**
  1369. * @}
  1370. */
  1371. /** @defgroup ADC_Exported_Functions_Group3 Peripheral Control functions
  1372. * @brief Peripheral Control functions
  1373. *
  1374. @verbatim
  1375. ===============================================================================
  1376. ##### Peripheral Control functions #####
  1377. ===============================================================================
  1378. [..] This section provides functions allowing to:
  1379. (+) Configure regular channels.
  1380. (+) Configure injected channels.
  1381. (+) Configure multimode.
  1382. (+) Configure the analog watch dog.
  1383. @endverbatim
  1384. * @{
  1385. */
  1386. /**
  1387. * @brief Configures for the selected ADC regular channel its corresponding
  1388. * rank in the sequencer and its sample time.
  1389. * @param hadc pointer to a ADC_HandleTypeDef structure that contains
  1390. * the configuration information for the specified ADC.
  1391. * @param sConfig ADC configuration structure.
  1392. * @retval HAL status
  1393. */
  1394. HAL_StatusTypeDef HAL_ADC_ConfigChannel(ADC_HandleTypeDef* hadc, ADC_ChannelConfTypeDef* sConfig)
  1395. {
  1396. __IO uint32_t counter = 0U;
  1397. ADC_Common_TypeDef *tmpADC_Common;
  1398. /* Check the parameters */
  1399. assert_param(IS_ADC_CHANNEL(sConfig->Channel));
  1400. assert_param(IS_ADC_REGULAR_RANK(sConfig->Rank));
  1401. assert_param(IS_ADC_SAMPLE_TIME(sConfig->SamplingTime));
  1402. /* Process locked */
  1403. __HAL_LOCK(hadc);
  1404. /* if ADC_Channel_10 ... ADC_Channel_18 is selected */
  1405. if (sConfig->Channel > ADC_CHANNEL_9)
  1406. {
  1407. /* Clear the old sample time */
  1408. hadc->Instance->SMPR1 &= ~ADC_SMPR1(ADC_SMPR1_SMP10, sConfig->Channel);
  1409. /* Set the new sample time */
  1410. hadc->Instance->SMPR1 |= ADC_SMPR1(sConfig->SamplingTime, sConfig->Channel);
  1411. }
  1412. else /* ADC_Channel include in ADC_Channel_[0..9] */
  1413. {
  1414. /* Clear the old sample time */
  1415. hadc->Instance->SMPR2 &= ~ADC_SMPR2(ADC_SMPR2_SMP0, sConfig->Channel);
  1416. /* Set the new sample time */
  1417. hadc->Instance->SMPR2 |= ADC_SMPR2(sConfig->SamplingTime, sConfig->Channel);
  1418. }
  1419. /* For Rank 1 to 6 */
  1420. if (sConfig->Rank < 7U)
  1421. {
  1422. /* Clear the old SQx bits for the selected rank */
  1423. hadc->Instance->SQR3 &= ~ADC_SQR3_RK(ADC_SQR3_SQ1, sConfig->Rank);
  1424. /* Set the SQx bits for the selected rank */
  1425. hadc->Instance->SQR3 |= ADC_SQR3_RK(sConfig->Channel, sConfig->Rank);
  1426. }
  1427. /* For Rank 7 to 12 */
  1428. else if (sConfig->Rank < 13U)
  1429. {
  1430. /* Clear the old SQx bits for the selected rank */
  1431. hadc->Instance->SQR2 &= ~ADC_SQR2_RK(ADC_SQR2_SQ7, sConfig->Rank);
  1432. /* Set the SQx bits for the selected rank */
  1433. hadc->Instance->SQR2 |= ADC_SQR2_RK(sConfig->Channel, sConfig->Rank);
  1434. }
  1435. /* For Rank 13 to 16 */
  1436. else
  1437. {
  1438. /* Clear the old SQx bits for the selected rank */
  1439. hadc->Instance->SQR1 &= ~ADC_SQR1_RK(ADC_SQR1_SQ13, sConfig->Rank);
  1440. /* Set the SQx bits for the selected rank */
  1441. hadc->Instance->SQR1 |= ADC_SQR1_RK(sConfig->Channel, sConfig->Rank);
  1442. }
  1443. /* Pointer to the common control register to which is belonging hadc */
  1444. /* (Depending on STM32F4 product, there may be up to 3 ADCs and 1 common */
  1445. /* control register) */
  1446. tmpADC_Common = ADC_COMMON_REGISTER(hadc);
  1447. /* if ADC1 Channel_18 is selected for VBAT Channel ennable VBATE */
  1448. if ((hadc->Instance == ADC1) && (sConfig->Channel == ADC_CHANNEL_VBAT))
  1449. {
  1450. /* Disable the TEMPSENSOR channel in case of using board with multiplixed ADC_CHANNEL_VBAT & ADC_CHANNEL_TEMPSENSOR*/
  1451. if ((uint16_t)ADC_CHANNEL_TEMPSENSOR == (uint16_t)ADC_CHANNEL_VBAT)
  1452. {
  1453. tmpADC_Common->CCR &= ~ADC_CCR_TSVREFE;
  1454. }
  1455. /* Enable the VBAT channel*/
  1456. tmpADC_Common->CCR |= ADC_CCR_VBATE;
  1457. }
  1458. /* if ADC1 Channel_16 or Channel_18 is selected for Temperature sensor or
  1459. Channel_17 is selected for VREFINT enable TSVREFE */
  1460. if ((hadc->Instance == ADC1) && ((sConfig->Channel == ADC_CHANNEL_TEMPSENSOR) || (sConfig->Channel == ADC_CHANNEL_VREFINT)))
  1461. {
  1462. /* Disable the VBAT channel in case of using board with multiplixed ADC_CHANNEL_VBAT & ADC_CHANNEL_TEMPSENSOR*/
  1463. if ((uint16_t)ADC_CHANNEL_TEMPSENSOR == (uint16_t)ADC_CHANNEL_VBAT)
  1464. {
  1465. tmpADC_Common->CCR &= ~ADC_CCR_VBATE;
  1466. }
  1467. /* Enable the Temperature sensor and VREFINT channel*/
  1468. tmpADC_Common->CCR |= ADC_CCR_TSVREFE;
  1469. if((sConfig->Channel == ADC_CHANNEL_TEMPSENSOR))
  1470. {
  1471. /* Delay for temperature sensor stabilization time */
  1472. /* Compute number of CPU cycles to wait for */
  1473. counter = (ADC_TEMPSENSOR_DELAY_US * (SystemCoreClock / 1000000U));
  1474. while(counter != 0U)
  1475. {
  1476. counter--;
  1477. }
  1478. }
  1479. }
  1480. /* Process unlocked */
  1481. __HAL_UNLOCK(hadc);
  1482. /* Return function status */
  1483. return HAL_OK;
  1484. }
  1485. /**
  1486. * @brief Configures the analog watchdog.
  1487. * @note Analog watchdog thresholds can be modified while ADC conversion
  1488. * is on going.
  1489. * In this case, some constraints must be taken into account:
  1490. * The programmed threshold values are effective from the next
  1491. * ADC EOC (end of unitary conversion).
  1492. * Considering that registers write delay may happen due to
  1493. * bus activity, this might cause an uncertainty on the
  1494. * effective timing of the new programmed threshold values.
  1495. * @param hadc pointer to a ADC_HandleTypeDef structure that contains
  1496. * the configuration information for the specified ADC.
  1497. * @param AnalogWDGConfig pointer to an ADC_AnalogWDGConfTypeDef structure
  1498. * that contains the configuration information of ADC analog watchdog.
  1499. * @retval HAL status
  1500. */
  1501. HAL_StatusTypeDef HAL_ADC_AnalogWDGConfig(ADC_HandleTypeDef* hadc, ADC_AnalogWDGConfTypeDef* AnalogWDGConfig)
  1502. {
  1503. #ifdef USE_FULL_ASSERT
  1504. uint32_t tmp = 0U;
  1505. #endif /* USE_FULL_ASSERT */
  1506. /* Check the parameters */
  1507. assert_param(IS_ADC_ANALOG_WATCHDOG(AnalogWDGConfig->WatchdogMode));
  1508. assert_param(IS_ADC_CHANNEL(AnalogWDGConfig->Channel));
  1509. assert_param(IS_FUNCTIONAL_STATE(AnalogWDGConfig->ITMode));
  1510. #ifdef USE_FULL_ASSERT
  1511. tmp = ADC_GET_RESOLUTION(hadc);
  1512. assert_param(IS_ADC_RANGE(tmp, AnalogWDGConfig->HighThreshold));
  1513. assert_param(IS_ADC_RANGE(tmp, AnalogWDGConfig->LowThreshold));
  1514. #endif /* USE_FULL_ASSERT */
  1515. /* Process locked */
  1516. __HAL_LOCK(hadc);
  1517. if(AnalogWDGConfig->ITMode == ENABLE)
  1518. {
  1519. /* Enable the ADC Analog watchdog interrupt */
  1520. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_AWD);
  1521. }
  1522. else
  1523. {
  1524. /* Disable the ADC Analog watchdog interrupt */
  1525. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_AWD);
  1526. }
  1527. /* Clear AWDEN, JAWDEN and AWDSGL bits */
  1528. hadc->Instance->CR1 &= ~(ADC_CR1_AWDSGL | ADC_CR1_JAWDEN | ADC_CR1_AWDEN);
  1529. /* Set the analog watchdog enable mode */
  1530. hadc->Instance->CR1 |= AnalogWDGConfig->WatchdogMode;
  1531. /* Set the high threshold */
  1532. hadc->Instance->HTR = AnalogWDGConfig->HighThreshold;
  1533. /* Set the low threshold */
  1534. hadc->Instance->LTR = AnalogWDGConfig->LowThreshold;
  1535. /* Clear the Analog watchdog channel select bits */
  1536. hadc->Instance->CR1 &= ~ADC_CR1_AWDCH;
  1537. /* Set the Analog watchdog channel */
  1538. hadc->Instance->CR1 |= (uint32_t)((uint16_t)(AnalogWDGConfig->Channel));
  1539. /* Process unlocked */
  1540. __HAL_UNLOCK(hadc);
  1541. /* Return function status */
  1542. return HAL_OK;
  1543. }
  1544. /**
  1545. * @}
  1546. */
  1547. /** @defgroup ADC_Exported_Functions_Group4 ADC Peripheral State functions
  1548. * @brief ADC Peripheral State functions
  1549. *
  1550. @verbatim
  1551. ===============================================================================
  1552. ##### Peripheral State and errors functions #####
  1553. ===============================================================================
  1554. [..]
  1555. This subsection provides functions allowing to
  1556. (+) Check the ADC state
  1557. (+) Check the ADC Error
  1558. @endverbatim
  1559. * @{
  1560. */
  1561. /**
  1562. * @brief return the ADC state
  1563. * @param hadc pointer to a ADC_HandleTypeDef structure that contains
  1564. * the configuration information for the specified ADC.
  1565. * @retval HAL state
  1566. */
  1567. uint32_t HAL_ADC_GetState(ADC_HandleTypeDef* hadc)
  1568. {
  1569. /* Return ADC state */
  1570. return hadc->State;
  1571. }
  1572. /**
  1573. * @brief Return the ADC error code
  1574. * @param hadc pointer to a ADC_HandleTypeDef structure that contains
  1575. * the configuration information for the specified ADC.
  1576. * @retval ADC Error Code
  1577. */
  1578. uint32_t HAL_ADC_GetError(ADC_HandleTypeDef *hadc)
  1579. {
  1580. return hadc->ErrorCode;
  1581. }
  1582. /**
  1583. * @}
  1584. */
  1585. /** @addtogroup ADC_Private_Functions
  1586. * @{
  1587. */
  1588. /**
  1589. * @brief Initializes the ADCx peripheral according to the specified parameters
  1590. * in the ADC_InitStruct without initializing the ADC MSP.
  1591. * @param hadc pointer to a ADC_HandleTypeDef structure that contains
  1592. * the configuration information for the specified ADC.
  1593. * @retval None
  1594. */
  1595. static void ADC_Init(ADC_HandleTypeDef* hadc)
  1596. {
  1597. ADC_Common_TypeDef *tmpADC_Common;
  1598. /* Set ADC parameters */
  1599. /* Pointer to the common control register to which is belonging hadc */
  1600. /* (Depending on STM32F4 product, there may be up to 3 ADCs and 1 common */
  1601. /* control register) */
  1602. tmpADC_Common = ADC_COMMON_REGISTER(hadc);
  1603. /* Set the ADC clock prescaler */
  1604. tmpADC_Common->CCR &= ~(ADC_CCR_ADCPRE);
  1605. tmpADC_Common->CCR |= hadc->Init.ClockPrescaler;
  1606. /* Set ADC scan mode */
  1607. hadc->Instance->CR1 &= ~(ADC_CR1_SCAN);
  1608. hadc->Instance->CR1 |= ADC_CR1_SCANCONV(hadc->Init.ScanConvMode);
  1609. /* Set ADC resolution */
  1610. hadc->Instance->CR1 &= ~(ADC_CR1_RES);
  1611. hadc->Instance->CR1 |= hadc->Init.Resolution;
  1612. /* Set ADC data alignment */
  1613. hadc->Instance->CR2 &= ~(ADC_CR2_ALIGN);
  1614. hadc->Instance->CR2 |= hadc->Init.DataAlign;
  1615. /* Enable external trigger if trigger selection is different of software */
  1616. /* start. */
  1617. /* Note: This configuration keeps the hardware feature of parameter */
  1618. /* ExternalTrigConvEdge "trigger edge none" equivalent to */
  1619. /* software start. */
  1620. if(hadc->Init.ExternalTrigConv != ADC_SOFTWARE_START)
  1621. {
  1622. /* Select external trigger to start conversion */
  1623. hadc->Instance->CR2 &= ~(ADC_CR2_EXTSEL);
  1624. hadc->Instance->CR2 |= hadc->Init.ExternalTrigConv;
  1625. /* Select external trigger polarity */
  1626. hadc->Instance->CR2 &= ~(ADC_CR2_EXTEN);
  1627. hadc->Instance->CR2 |= hadc->Init.ExternalTrigConvEdge;
  1628. }
  1629. else
  1630. {
  1631. /* Reset the external trigger */
  1632. hadc->Instance->CR2 &= ~(ADC_CR2_EXTSEL);
  1633. hadc->Instance->CR2 &= ~(ADC_CR2_EXTEN);
  1634. }
  1635. /* Enable or disable ADC continuous conversion mode */
  1636. hadc->Instance->CR2 &= ~(ADC_CR2_CONT);
  1637. hadc->Instance->CR2 |= ADC_CR2_CONTINUOUS((uint32_t)hadc->Init.ContinuousConvMode);
  1638. if(hadc->Init.DiscontinuousConvMode != DISABLE)
  1639. {
  1640. assert_param(IS_ADC_REGULAR_DISC_NUMBER(hadc->Init.NbrOfDiscConversion));
  1641. /* Enable the selected ADC regular discontinuous mode */
  1642. hadc->Instance->CR1 |= (uint32_t)ADC_CR1_DISCEN;
  1643. /* Set the number of channels to be converted in discontinuous mode */
  1644. hadc->Instance->CR1 &= ~(ADC_CR1_DISCNUM);
  1645. hadc->Instance->CR1 |= ADC_CR1_DISCONTINUOUS(hadc->Init.NbrOfDiscConversion);
  1646. }
  1647. else
  1648. {
  1649. /* Disable the selected ADC regular discontinuous mode */
  1650. hadc->Instance->CR1 &= ~(ADC_CR1_DISCEN);
  1651. }
  1652. /* Set ADC number of conversion */
  1653. hadc->Instance->SQR1 &= ~(ADC_SQR1_L);
  1654. hadc->Instance->SQR1 |= ADC_SQR1(hadc->Init.NbrOfConversion);
  1655. /* Enable or disable ADC DMA continuous request */
  1656. hadc->Instance->CR2 &= ~(ADC_CR2_DDS);
  1657. hadc->Instance->CR2 |= ADC_CR2_DMAContReq((uint32_t)hadc->Init.DMAContinuousRequests);
  1658. /* Enable or disable ADC end of conversion selection */
  1659. hadc->Instance->CR2 &= ~(ADC_CR2_EOCS);
  1660. hadc->Instance->CR2 |= ADC_CR2_EOCSelection(hadc->Init.EOCSelection);
  1661. }
  1662. /**
  1663. * @brief DMA transfer complete callback.
  1664. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  1665. * the configuration information for the specified DMA module.
  1666. * @retval None
  1667. */
  1668. static void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma)
  1669. {
  1670. /* Retrieve ADC handle corresponding to current DMA handle */
  1671. ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1672. /* Update state machine on conversion status if not in error state */
  1673. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL | HAL_ADC_STATE_ERROR_DMA))
  1674. {
  1675. /* Update ADC state machine */
  1676. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  1677. /* Determine whether any further conversion upcoming on group regular */
  1678. /* by external trigger, continuous mode or scan sequence on going. */
  1679. /* Note: On STM32F4, there is no independent flag of end of sequence. */
  1680. /* The test of scan sequence on going is done either with scan */
  1681. /* sequence disabled or with end of conversion flag set to */
  1682. /* of end of sequence. */
  1683. if(ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  1684. (hadc->Init.ContinuousConvMode == DISABLE) &&
  1685. (HAL_IS_BIT_CLR(hadc->Instance->SQR1, ADC_SQR1_L) ||
  1686. HAL_IS_BIT_CLR(hadc->Instance->CR2, ADC_CR2_EOCS) ) )
  1687. {
  1688. /* Disable ADC end of single conversion interrupt on group regular */
  1689. /* Note: Overrun interrupt was enabled with EOC interrupt in */
  1690. /* HAL_ADC_Start_IT(), but is not disabled here because can be used */
  1691. /* by overrun IRQ process below. */
  1692. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC);
  1693. /* Set ADC state */
  1694. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  1695. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  1696. {
  1697. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  1698. }
  1699. }
  1700. /* Conversion complete callback */
  1701. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  1702. hadc->ConvCpltCallback(hadc);
  1703. #else
  1704. HAL_ADC_ConvCpltCallback(hadc);
  1705. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  1706. }
  1707. else /* DMA and-or internal error occurred */
  1708. {
  1709. if ((hadc->State & HAL_ADC_STATE_ERROR_INTERNAL) != 0UL)
  1710. {
  1711. /* Call HAL ADC Error Callback function */
  1712. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  1713. hadc->ErrorCallback(hadc);
  1714. #else
  1715. HAL_ADC_ErrorCallback(hadc);
  1716. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  1717. }
  1718. else
  1719. {
  1720. /* Call DMA error callback */
  1721. hadc->DMA_Handle->XferErrorCallback(hdma);
  1722. }
  1723. }
  1724. }
  1725. /**
  1726. * @brief DMA half transfer complete callback.
  1727. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  1728. * the configuration information for the specified DMA module.
  1729. * @retval None
  1730. */
  1731. static void ADC_DMAHalfConvCplt(DMA_HandleTypeDef *hdma)
  1732. {
  1733. ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1734. /* Half conversion callback */
  1735. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  1736. hadc->ConvHalfCpltCallback(hadc);
  1737. #else
  1738. HAL_ADC_ConvHalfCpltCallback(hadc);
  1739. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  1740. }
  1741. /**
  1742. * @brief DMA error callback
  1743. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  1744. * the configuration information for the specified DMA module.
  1745. * @retval None
  1746. */
  1747. static void ADC_DMAError(DMA_HandleTypeDef *hdma)
  1748. {
  1749. ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1750. hadc->State= HAL_ADC_STATE_ERROR_DMA;
  1751. /* Set ADC error code to DMA error */
  1752. hadc->ErrorCode |= HAL_ADC_ERROR_DMA;
  1753. /* Error callback */
  1754. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  1755. hadc->ErrorCallback(hadc);
  1756. #else
  1757. HAL_ADC_ErrorCallback(hadc);
  1758. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  1759. }
  1760. /**
  1761. * @}
  1762. */
  1763. /**
  1764. * @}
  1765. */
  1766. #endif /* HAL_ADC_MODULE_ENABLED */
  1767. /**
  1768. * @}
  1769. */
  1770. /**
  1771. * @}
  1772. */
  1773. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/