stm32g0xx_hal_adc.c 116 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987
  1. /**
  2. ******************************************************************************
  3. * @file stm32g0xx_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 Converter (ADC)
  7. * peripheral:
  8. * + Initialization and de-initialization functions
  9. * + Peripheral Control functions
  10. * + Peripheral State functions
  11. * Other functions (extended functions) are available in file
  12. * "stm32g0xx_hal_adc_ex.c".
  13. *
  14. ******************************************************************************
  15. * @attention
  16. *
  17. * Copyright (c) 2018 STMicroelectronics.
  18. * All rights reserved.
  19. *
  20. * This software is licensed under terms that can be found in the LICENSE file
  21. * in the root directory of this software component.
  22. * If no LICENSE file comes with this software, it is provided AS-IS.
  23. *
  24. ******************************************************************************
  25. @verbatim
  26. ==============================================================================
  27. ##### ADC peripheral features #####
  28. ==============================================================================
  29. [..]
  30. (+) 12-bit, 10-bit, 8-bit or 6-bit configurable resolution.
  31. (+) Interrupt generation at the end of regular conversion and in case of
  32. analog watchdog or overrun events.
  33. (+) Single and continuous conversion modes.
  34. (+) Scan mode for conversion of several channels sequentially.
  35. (+) Data alignment with in-built data coherency.
  36. (+) Programmable sampling time (common to group of channels)
  37. (+) External trigger (timer or EXTI) with configurable polarity
  38. (+) DMA request generation for transfer of conversions data of regular group.
  39. (+) ADC calibration
  40. (+) ADC conversion of regular group.
  41. (+) ADC supply requirements: 1.62 V to 3.6 V.
  42. (+) ADC input range: from Vref- (connected to Vssa) to Vref+ (connected to
  43. Vdda or to an external voltage reference).
  44. ##### How to use this driver #####
  45. ==============================================================================
  46. [..]
  47. *** Configuration of top level parameters related to ADC ***
  48. ============================================================
  49. [..]
  50. (#) Enable the ADC interface
  51. (++) As prerequisite, ADC clock must be configured at RCC top level.
  52. Caution: On STM32G0, ADC clock frequency max is 35MHz (refer
  53. to device datasheet).
  54. Therefore, ADC clock source from RCC and ADC clock
  55. prescaler must be configured to remain below
  56. this maximum frequency.
  57. (++) Two clock settings are mandatory:
  58. (+++) ADC clock (core clock, also possibly conversion clock).
  59. (+++) ADC clock (conversions clock).
  60. Four possible clock sources: synchronous clock from APB clock (same as ADC core clock)
  61. or asynchronous clock from RCC level: SYSCLK, HSI16, PLLPCLK.
  62. (+++) Example:
  63. Into HAL_ADC_MspInit() (recommended code location) or with
  64. other device clock parameters configuration:
  65. (+++) __HAL_RCC_ADC_CLK_ENABLE(); (mandatory: core clock)
  66. (++) ADC clock source and clock prescaler are configured at ADC level with
  67. parameter "ClockPrescaler" using function HAL_ADC_Init().
  68. (#) ADC pins configuration
  69. (++) Enable the clock for the ADC GPIOs
  70. using macro __HAL_RCC_GPIOx_CLK_ENABLE()
  71. (++) Configure these ADC pins in analog mode
  72. using function HAL_GPIO_Init()
  73. (#) Optionally, in case of usage of ADC with interruptions:
  74. (++) Configure the NVIC for ADC
  75. using function HAL_NVIC_EnableIRQ(ADCx_IRQn)
  76. (++) Insert the ADC interruption handler function HAL_ADC_IRQHandler()
  77. into the function of corresponding ADC interruption vector
  78. ADCx_IRQHandler().
  79. (#) Optionally, in case of usage of DMA:
  80. (++) Configure the DMA (DMA channel, mode normal or circular, ...)
  81. using function HAL_DMA_Init().
  82. (++) Configure the NVIC for DMA
  83. using function HAL_NVIC_EnableIRQ(DMAx_Channelx_IRQn)
  84. (++) Insert the ADC interruption handler function HAL_ADC_IRQHandler()
  85. into the function of corresponding DMA interruption vector
  86. DMAx_Channelx_IRQHandler().
  87. *** Configuration of ADC, group regular, channels parameters ***
  88. ================================================================
  89. [..]
  90. (#) Configure the ADC parameters (resolution, data alignment, ...)
  91. and regular group parameters (conversion trigger, sequencer, ...)
  92. using function HAL_ADC_Init().
  93. (#) Configure the channels for regular group parameters (channel number,
  94. channel rank into sequencer, ..., into regular group)
  95. using function HAL_ADC_ConfigChannel().
  96. (#) Optionally, configure the analog watchdog parameters (channels
  97. monitored, thresholds, ...)
  98. using function HAL_ADC_AnalogWDGConfig().
  99. *** Execution of ADC conversions ***
  100. ====================================
  101. [..]
  102. (#) Optionally, perform an automatic ADC calibration to improve the
  103. conversion accuracy
  104. using function HAL_ADCEx_Calibration_Start().
  105. (#) ADC driver can be used among three modes: polling, interruption,
  106. transfer by DMA.
  107. (++) ADC conversion by polling:
  108. (+++) Activate the ADC peripheral and start conversions
  109. using function HAL_ADC_Start()
  110. (+++) Wait for ADC conversion completion
  111. using function HAL_ADC_PollForConversion()
  112. (+++) Retrieve conversion results
  113. using function HAL_ADC_GetValue()
  114. (+++) Stop conversion and disable the ADC peripheral
  115. using function HAL_ADC_Stop()
  116. (++) ADC conversion by interruption:
  117. (+++) Activate the ADC peripheral and start conversions
  118. using function HAL_ADC_Start_IT()
  119. (+++) Wait for ADC conversion completion by call of function
  120. HAL_ADC_ConvCpltCallback()
  121. (this function must be implemented in user program)
  122. (+++) Retrieve conversion results
  123. using function HAL_ADC_GetValue()
  124. (+++) Stop conversion and disable the ADC peripheral
  125. using function HAL_ADC_Stop_IT()
  126. (++) ADC conversion with transfer by DMA:
  127. (+++) Activate the ADC peripheral and start conversions
  128. using function HAL_ADC_Start_DMA()
  129. (+++) Wait for ADC conversion completion by call of function
  130. HAL_ADC_ConvCpltCallback() or HAL_ADC_ConvHalfCpltCallback()
  131. (these functions must be implemented in user program)
  132. (+++) Conversion results are automatically transferred by DMA into
  133. destination variable address.
  134. (+++) Stop conversion and disable the ADC peripheral
  135. using function HAL_ADC_Stop_DMA()
  136. [..]
  137. (@) Callback functions must be implemented in user program:
  138. (+@) HAL_ADC_ErrorCallback()
  139. (+@) HAL_ADC_LevelOutOfWindowCallback() (callback of analog watchdog)
  140. (+@) HAL_ADC_ConvCpltCallback()
  141. (+@) HAL_ADC_ConvHalfCpltCallback
  142. *** Deinitialization of ADC ***
  143. ============================================================
  144. [..]
  145. (#) Disable the ADC interface
  146. (++) ADC clock can be hard reset and disabled at RCC top level.
  147. (++) Hard reset of ADC peripherals
  148. using macro __ADCx_FORCE_RESET(), __ADCx_RELEASE_RESET().
  149. (++) ADC clock disable
  150. using the equivalent macro/functions as configuration step.
  151. (+++) Example:
  152. Into HAL_ADC_MspDeInit() (recommended code location) or with
  153. other device clock parameters configuration:
  154. (+++) RCC_OscInitStructure.OscillatorType = RCC_OSCILLATORTYPE_HSI14;
  155. (+++) RCC_OscInitStructure.HSI14State = RCC_HSI14_OFF; (if not used for system clock)
  156. (+++) HAL_RCC_OscConfig(&RCC_OscInitStructure);
  157. (#) ADC pins configuration
  158. (++) Disable the clock for the ADC GPIOs
  159. using macro __HAL_RCC_GPIOx_CLK_DISABLE()
  160. (#) Optionally, in case of usage of ADC with interruptions:
  161. (++) Disable the NVIC for ADC
  162. using function HAL_NVIC_EnableIRQ(ADCx_IRQn)
  163. (#) Optionally, in case of usage of DMA:
  164. (++) Deinitialize the DMA
  165. using function HAL_DMA_Init().
  166. (++) Disable the NVIC for DMA
  167. using function HAL_NVIC_EnableIRQ(DMAx_Channelx_IRQn)
  168. [..]
  169. *** Callback registration ***
  170. =============================================
  171. [..]
  172. The compilation flag USE_HAL_ADC_REGISTER_CALLBACKS, when set to 1,
  173. allows the user to configure dynamically the driver callbacks.
  174. Use Functions HAL_ADC_RegisterCallback()
  175. to register an interrupt callback.
  176. [..]
  177. Function HAL_ADC_RegisterCallback() allows to register following callbacks:
  178. (+) ConvCpltCallback : ADC conversion complete callback
  179. (+) ConvHalfCpltCallback : ADC conversion DMA half-transfer callback
  180. (+) LevelOutOfWindowCallback : ADC analog watchdog 1 callback
  181. (+) ErrorCallback : ADC error callback
  182. (+) LevelOutOfWindow2Callback : ADC analog watchdog 2 callback
  183. (+) LevelOutOfWindow3Callback : ADC analog watchdog 3 callback
  184. (+) EndOfSamplingCallback : ADC end of sampling callback
  185. (+) MspInitCallback : ADC Msp Init callback
  186. (+) MspDeInitCallback : ADC Msp DeInit callback
  187. This function takes as parameters the HAL peripheral handle, the Callback ID
  188. and a pointer to the user callback function.
  189. [..]
  190. Use function HAL_ADC_UnRegisterCallback to reset a callback to the default
  191. weak function.
  192. [..]
  193. HAL_ADC_UnRegisterCallback takes as parameters the HAL peripheral handle,
  194. and the Callback ID.
  195. This function allows to reset following callbacks:
  196. (+) ConvCpltCallback : ADC conversion complete callback
  197. (+) ConvHalfCpltCallback : ADC conversion DMA half-transfer callback
  198. (+) LevelOutOfWindowCallback : ADC analog watchdog 1 callback
  199. (+) ErrorCallback : ADC error callback
  200. (+) LevelOutOfWindow2Callback : ADC analog watchdog 2 callback
  201. (+) LevelOutOfWindow3Callback : ADC analog watchdog 3 callback
  202. (+) EndOfSamplingCallback : ADC end of sampling callback
  203. (+) MspInitCallback : ADC Msp Init callback
  204. (+) MspDeInitCallback : ADC Msp DeInit callback
  205. [..]
  206. By default, after the HAL_ADC_Init() and when the state is HAL_ADC_STATE_RESET
  207. all callbacks are set to the corresponding weak functions:
  208. examples HAL_ADC_ConvCpltCallback(), HAL_ADC_ErrorCallback().
  209. Exception done for MspInit and MspDeInit functions that are
  210. reset to the legacy weak functions in the HAL_ADC_Init()/ HAL_ADC_DeInit() only when
  211. these callbacks are null (not registered beforehand).
  212. [..]
  213. If MspInit or MspDeInit are not null, the HAL_ADC_Init()/ HAL_ADC_DeInit()
  214. keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
  215. [..]
  216. Callbacks can be registered/unregistered in HAL_ADC_STATE_READY state only.
  217. Exception done MspInit/MspDeInit functions that can be registered/unregistered
  218. in HAL_ADC_STATE_READY or HAL_ADC_STATE_RESET state,
  219. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  220. [..]
  221. Then, the user first registers the MspInit/MspDeInit user callbacks
  222. using HAL_ADC_RegisterCallback() before calling HAL_ADC_DeInit()
  223. or HAL_ADC_Init() function.
  224. [..]
  225. When the compilation flag USE_HAL_ADC_REGISTER_CALLBACKS is set to 0 or
  226. not defined, the callback registration feature is not available and all callbacks
  227. are set to the corresponding weak functions.
  228. @endverbatim
  229. ******************************************************************************
  230. */
  231. /* Includes ------------------------------------------------------------------*/
  232. #include "stm32g0xx_hal.h"
  233. /** @addtogroup STM32G0xx_HAL_Driver
  234. * @{
  235. */
  236. /** @defgroup ADC ADC
  237. * @brief ADC HAL module driver
  238. * @{
  239. */
  240. #ifdef HAL_ADC_MODULE_ENABLED
  241. /* Private typedef -----------------------------------------------------------*/
  242. /* Private define ------------------------------------------------------------*/
  243. /** @defgroup ADC_Private_Constants ADC Private Constants
  244. * @{
  245. */
  246. /* Fixed timeout values for ADC calibration, enable settling time, disable */
  247. /* settling time. */
  248. /* Values defined to be higher than worst cases: low clock frequency, */
  249. /* maximum prescaler. */
  250. /* Ex of profile low frequency : Clock source at 0.1 MHz, ADC clock */
  251. /* prescaler 4, sampling time 7.5 ADC clock cycles, resolution 12 bits. */
  252. /* Unit: ms */
  253. #define ADC_ENABLE_TIMEOUT (2UL)
  254. #define ADC_DISABLE_TIMEOUT (2UL)
  255. #define ADC_STOP_CONVERSION_TIMEOUT (2UL)
  256. #define ADC_CHANNEL_CONF_RDY_TIMEOUT (1UL)
  257. /* Register CHSELR bits corresponding to ranks 2 to 8 . */
  258. #define ADC_CHSELR_SQ2_TO_SQ8 (ADC_CHSELR_SQ2 | ADC_CHSELR_SQ3 | ADC_CHSELR_SQ4 | \
  259. ADC_CHSELR_SQ5 | ADC_CHSELR_SQ6 | ADC_CHSELR_SQ7 | ADC_CHSELR_SQ8)
  260. /**
  261. * @}
  262. */
  263. /* Private macro -------------------------------------------------------------*/
  264. /* Private variables ---------------------------------------------------------*/
  265. /* Private function prototypes -----------------------------------------------*/
  266. /** @defgroup ADC_Private_Functions ADC Private Functions
  267. * @{
  268. */
  269. static void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma);
  270. static void ADC_DMAHalfConvCplt(DMA_HandleTypeDef *hdma);
  271. static void ADC_DMAError(DMA_HandleTypeDef *hdma);
  272. /**
  273. * @}
  274. */
  275. /* Exported functions ---------------------------------------------------------*/
  276. /** @defgroup ADC_Exported_Functions ADC Exported Functions
  277. * @{
  278. */
  279. /** @defgroup ADC_Exported_Functions_Group1 Initialization and de-initialization functions
  280. * @brief ADC Initialization and Configuration functions
  281. *
  282. @verbatim
  283. ===============================================================================
  284. ##### Initialization and de-initialization functions #####
  285. ===============================================================================
  286. [..] This section provides functions allowing to:
  287. (+) Initialize and configure the ADC.
  288. (+) De-initialize the ADC.
  289. @endverbatim
  290. * @{
  291. */
  292. /**
  293. * @brief Initialize the ADC peripheral and regular group according to
  294. * parameters specified in structure "ADC_InitTypeDef".
  295. * @note As prerequisite, ADC clock must be configured at RCC top level
  296. * (refer to description of RCC configuration for ADC
  297. * in header of this file).
  298. * @note Possibility to update parameters on the fly:
  299. * This function initializes the ADC MSP (HAL_ADC_MspInit()) only when
  300. * coming from ADC state reset. Following calls to this function can
  301. * be used to reconfigure some parameters of ADC_InitTypeDef
  302. * structure on the fly, without modifying MSP configuration. If ADC
  303. * MSP has to be modified again, HAL_ADC_DeInit() must be called
  304. * before HAL_ADC_Init().
  305. * The setting of these parameters is conditioned to ADC state.
  306. * For parameters constraints, see comments of structure
  307. * "ADC_InitTypeDef".
  308. * @note This function configures the ADC within 2 scopes: scope of entire
  309. * ADC and scope of regular group. For parameters details, see comments
  310. * of structure "ADC_InitTypeDef".
  311. * @param hadc ADC handle
  312. * @retval HAL status
  313. */
  314. HAL_StatusTypeDef HAL_ADC_Init(ADC_HandleTypeDef *hadc)
  315. {
  316. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  317. uint32_t tmp_cfgr1 = 0UL;
  318. uint32_t tmp_cfgr2 = 0UL;
  319. uint32_t tmp_adc_reg_is_conversion_on_going;
  320. __IO uint32_t wait_loop_index = 0UL;
  321. /* Check ADC handle */
  322. if (hadc == NULL)
  323. {
  324. return HAL_ERROR;
  325. }
  326. /* Check the parameters */
  327. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  328. assert_param(IS_ADC_CLOCKPRESCALER(hadc->Init.ClockPrescaler));
  329. assert_param(IS_ADC_RESOLUTION(hadc->Init.Resolution));
  330. assert_param(IS_ADC_DATA_ALIGN(hadc->Init.DataAlign));
  331. assert_param(IS_ADC_SCAN_MODE(hadc->Init.ScanConvMode));
  332. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
  333. assert_param(IS_ADC_EXTTRIG_EDGE(hadc->Init.ExternalTrigConvEdge));
  334. assert_param(IS_ADC_EXTTRIG(hadc->Init.ExternalTrigConv));
  335. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DMAContinuousRequests));
  336. assert_param(IS_ADC_EOC_SELECTION(hadc->Init.EOCSelection));
  337. assert_param(IS_ADC_OVERRUN(hadc->Init.Overrun));
  338. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.LowPowerAutoWait));
  339. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.LowPowerAutoPowerOff));
  340. assert_param(IS_ADC_SAMPLE_TIME(hadc->Init.SamplingTimeCommon1));
  341. assert_param(IS_ADC_SAMPLE_TIME(hadc->Init.SamplingTimeCommon2));
  342. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.OversamplingMode));
  343. if (hadc->Init.OversamplingMode == ENABLE)
  344. {
  345. assert_param(IS_ADC_OVERSAMPLING_RATIO(hadc->Init.Oversampling.Ratio));
  346. assert_param(IS_ADC_RIGHT_BIT_SHIFT(hadc->Init.Oversampling.RightBitShift));
  347. assert_param(IS_ADC_TRIGGERED_OVERSAMPLING_MODE(hadc->Init.Oversampling.TriggeredMode));
  348. }
  349. assert_param(IS_ADC_TRIGGER_FREQ(hadc->Init.TriggerFrequencyMode));
  350. if (hadc->Init.ScanConvMode != ADC_SCAN_DISABLE)
  351. {
  352. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DiscontinuousConvMode));
  353. if (hadc->Init.ScanConvMode == ADC_SCAN_ENABLE)
  354. {
  355. assert_param(IS_ADC_REGULAR_NB_CONV(hadc->Init.NbrOfConversion));
  356. }
  357. }
  358. /* ADC group regular discontinuous mode can be enabled only if */
  359. /* continuous mode is disabled. */
  360. assert_param(!((hadc->Init.DiscontinuousConvMode == ENABLE) && (hadc->Init.ContinuousConvMode == ENABLE)));
  361. /* Actions performed only if ADC is coming from state reset: */
  362. /* - Initialization of ADC MSP */
  363. if (hadc->State == HAL_ADC_STATE_RESET)
  364. {
  365. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  366. /* Init the ADC Callback settings */
  367. hadc->ConvCpltCallback = HAL_ADC_ConvCpltCallback; /* Legacy weak callback */
  368. hadc->ConvHalfCpltCallback = HAL_ADC_ConvHalfCpltCallback; /* Legacy weak callback */
  369. hadc->LevelOutOfWindowCallback = HAL_ADC_LevelOutOfWindowCallback; /* Legacy weak callback */
  370. hadc->ErrorCallback = HAL_ADC_ErrorCallback; /* Legacy weak callback */
  371. hadc->LevelOutOfWindow2Callback = HAL_ADCEx_LevelOutOfWindow2Callback; /* Legacy weak callback */
  372. hadc->LevelOutOfWindow3Callback = HAL_ADCEx_LevelOutOfWindow3Callback; /* Legacy weak callback */
  373. hadc->EndOfSamplingCallback = HAL_ADCEx_EndOfSamplingCallback; /* Legacy weak callback */
  374. if (hadc->MspInitCallback == NULL)
  375. {
  376. hadc->MspInitCallback = HAL_ADC_MspInit; /* Legacy weak MspInit */
  377. }
  378. /* Init the low level hardware */
  379. hadc->MspInitCallback(hadc);
  380. #else
  381. /* Init the low level hardware */
  382. HAL_ADC_MspInit(hadc);
  383. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  384. /* Set ADC error code to none */
  385. ADC_CLEAR_ERRORCODE(hadc);
  386. /* Initialize Lock */
  387. hadc->Lock = HAL_UNLOCKED;
  388. }
  389. if (LL_ADC_IsInternalRegulatorEnabled(hadc->Instance) == 0UL)
  390. {
  391. /* Enable ADC internal voltage regulator */
  392. LL_ADC_EnableInternalRegulator(hadc->Instance);
  393. /* Delay for ADC stabilization time */
  394. /* Wait loop initialization and execution */
  395. /* Note: Variable divided by 2 to compensate partially */
  396. /* CPU processing cycles, scaling in us split to not */
  397. /* exceed 32 bits register capacity and handle low frequency. */
  398. wait_loop_index = ((LL_ADC_DELAY_INTERNAL_REGUL_STAB_US / 10UL) * ((SystemCoreClock / (100000UL * 2UL)) + 1UL));
  399. while (wait_loop_index != 0UL)
  400. {
  401. wait_loop_index--;
  402. }
  403. }
  404. /* Verification that ADC voltage regulator is correctly enabled, whether */
  405. /* or not ADC is coming from state reset (if any potential problem of */
  406. /* clocking, voltage regulator would not be enabled). */
  407. if (LL_ADC_IsInternalRegulatorEnabled(hadc->Instance) == 0UL)
  408. {
  409. /* Update ADC state machine to error */
  410. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  411. /* Set ADC error code to ADC peripheral internal error */
  412. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  413. tmp_hal_status = HAL_ERROR;
  414. }
  415. /* Configuration of ADC parameters if previous preliminary actions are */
  416. /* correctly completed and if there is no conversion on going on regular */
  417. /* group (ADC may already be enabled at this point if HAL_ADC_Init() is */
  418. /* called to update a parameter on the fly). */
  419. tmp_adc_reg_is_conversion_on_going = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
  420. if (((hadc->State & HAL_ADC_STATE_ERROR_INTERNAL) == 0UL)
  421. && (tmp_adc_reg_is_conversion_on_going == 0UL)
  422. )
  423. {
  424. /* Set ADC state */
  425. ADC_STATE_CLR_SET(hadc->State,
  426. HAL_ADC_STATE_REG_BUSY,
  427. HAL_ADC_STATE_BUSY_INTERNAL);
  428. /* Configuration of common ADC parameters */
  429. /* Parameters update conditioned to ADC state: */
  430. /* Parameters that can be updated only when ADC is disabled: */
  431. /* - Internal voltage regulator (no parameter in HAL ADC init structure) */
  432. /* - Clock configuration */
  433. /* - ADC resolution */
  434. /* - Oversampling */
  435. /* - discontinuous mode */
  436. /* - LowPowerAutoWait mode */
  437. /* - LowPowerAutoPowerOff mode */
  438. /* - continuous conversion mode */
  439. /* - overrun */
  440. /* - external trigger to start conversion */
  441. /* - external trigger polarity */
  442. /* - data alignment */
  443. /* - resolution */
  444. /* - scan direction */
  445. /* - DMA continuous request */
  446. /* - Trigger frequency mode */
  447. /* Note: If low power mode AutoPowerOff is enabled, ADC enable */
  448. /* and disable phases are performed automatically by hardware */
  449. /* (in this case, flag ADC_FLAG_RDY is not set). */
  450. if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
  451. {
  452. /* Some parameters of this register are not reset, since they are set */
  453. /* by other functions and must be kept in case of usage of this */
  454. /* function on the fly (update of a parameter of ADC_InitTypeDef */
  455. /* without needing to reconfigure all other ADC groups/channels */
  456. /* parameters): */
  457. /* - internal measurement paths (VrefInt, ...) */
  458. /* (set into HAL_ADC_ConfigChannel() ) */
  459. tmp_cfgr1 |= (hadc->Init.Resolution |
  460. ADC_CFGR1_AUTOWAIT((uint32_t)hadc->Init.LowPowerAutoWait) |
  461. ADC_CFGR1_AUTOOFF((uint32_t)hadc->Init.LowPowerAutoPowerOff) |
  462. ADC_CFGR1_CONTINUOUS((uint32_t)hadc->Init.ContinuousConvMode) |
  463. ADC_CFGR1_OVERRUN(hadc->Init.Overrun) |
  464. hadc->Init.DataAlign |
  465. ADC_SCAN_SEQ_MODE(hadc->Init.ScanConvMode) |
  466. ADC_CFGR1_DMACONTREQ((uint32_t)hadc->Init.DMAContinuousRequests));
  467. /* Update setting of discontinuous mode only if continuous mode is disabled */
  468. if (hadc->Init.DiscontinuousConvMode == ENABLE)
  469. {
  470. if (hadc->Init.ContinuousConvMode == DISABLE)
  471. {
  472. /* Enable the selected ADC group regular discontinuous mode */
  473. tmp_cfgr1 |= ADC_CFGR1_DISCEN;
  474. }
  475. else
  476. {
  477. /* ADC regular group discontinuous was intended to be enabled, */
  478. /* but ADC regular group modes continuous and sequencer discontinuous */
  479. /* cannot be enabled simultaneously. */
  480. /* Update ADC state machine to error */
  481. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  482. /* Set ADC error code to ADC peripheral internal error */
  483. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  484. }
  485. }
  486. /* Enable external trigger if trigger selection is different of software */
  487. /* start. */
  488. /* Note: This configuration keeps the hardware feature of parameter */
  489. /* ExternalTrigConvEdge "trigger edge none" equivalent to */
  490. /* software start. */
  491. if (hadc->Init.ExternalTrigConv != ADC_SOFTWARE_START)
  492. {
  493. tmp_cfgr1 |= ((hadc->Init.ExternalTrigConv & ADC_CFGR1_EXTSEL) |
  494. hadc->Init.ExternalTrigConvEdge);
  495. }
  496. /* Update ADC configuration register with previous settings */
  497. MODIFY_REG(hadc->Instance->CFGR1,
  498. ADC_CFGR1_RES |
  499. ADC_CFGR1_DISCEN |
  500. ADC_CFGR1_CHSELRMOD |
  501. ADC_CFGR1_AUTOFF |
  502. ADC_CFGR1_WAIT |
  503. ADC_CFGR1_CONT |
  504. ADC_CFGR1_OVRMOD |
  505. ADC_CFGR1_EXTSEL |
  506. ADC_CFGR1_EXTEN |
  507. ADC_CFGR1_ALIGN |
  508. ADC_CFGR1_SCANDIR |
  509. ADC_CFGR1_DMACFG,
  510. tmp_cfgr1);
  511. tmp_cfgr2 |= ((hadc->Init.ClockPrescaler & ADC_CFGR2_CKMODE) |
  512. hadc->Init.TriggerFrequencyMode
  513. );
  514. if (hadc->Init.OversamplingMode == ENABLE)
  515. {
  516. tmp_cfgr2 |= (ADC_CFGR2_OVSE |
  517. (hadc->Init.ClockPrescaler & ADC_CFGR2_CKMODE) |
  518. hadc->Init.Oversampling.Ratio |
  519. hadc->Init.Oversampling.RightBitShift |
  520. hadc->Init.Oversampling.TriggeredMode
  521. );
  522. }
  523. MODIFY_REG(hadc->Instance->CFGR2,
  524. ADC_CFGR2_CKMODE |
  525. ADC_CFGR2_LFTRIG |
  526. ADC_CFGR2_OVSE |
  527. ADC_CFGR2_OVSR |
  528. ADC_CFGR2_OVSS |
  529. ADC_CFGR2_TOVS,
  530. tmp_cfgr2);
  531. /* Configuration of ADC clock mode: asynchronous clock source */
  532. /* with selectable prescaler. */
  533. if (((hadc->Init.ClockPrescaler) != ADC_CLOCK_SYNC_PCLK_DIV1) &&
  534. ((hadc->Init.ClockPrescaler) != ADC_CLOCK_SYNC_PCLK_DIV2) &&
  535. ((hadc->Init.ClockPrescaler) != ADC_CLOCK_SYNC_PCLK_DIV4))
  536. {
  537. MODIFY_REG(ADC1_COMMON->CCR,
  538. ADC_CCR_PRESC,
  539. hadc->Init.ClockPrescaler & ADC_CCR_PRESC);
  540. }
  541. }
  542. /* Channel sampling time configuration */
  543. LL_ADC_SetSamplingTimeCommonChannels(hadc->Instance, LL_ADC_SAMPLINGTIME_COMMON_1, hadc->Init.SamplingTimeCommon1);
  544. LL_ADC_SetSamplingTimeCommonChannels(hadc->Instance, LL_ADC_SAMPLINGTIME_COMMON_2, hadc->Init.SamplingTimeCommon2);
  545. /* Configuration of regular group sequencer: */
  546. /* - if scan mode is disabled, regular channels sequence length is set to */
  547. /* 0x00: 1 channel converted (channel on regular rank 1) */
  548. /* Parameter "NbrOfConversion" is discarded. */
  549. /* Note: Scan mode is not present by hardware on this device, but */
  550. /* emulated by software for alignment over all STM32 devices. */
  551. /* - if scan mode is enabled, regular channels sequence length is set to */
  552. /* parameter "NbrOfConversion". */
  553. /* Channels must be configured into each rank using function */
  554. /* "HAL_ADC_ConfigChannel()". */
  555. if (hadc->Init.ScanConvMode == ADC_SCAN_DISABLE)
  556. {
  557. /* Set sequencer scan length by clearing ranks above rank 1 */
  558. /* and do not modify rank 1 value. */
  559. SET_BIT(hadc->Instance->CHSELR,
  560. ADC_CHSELR_SQ2_TO_SQ8);
  561. }
  562. else if (hadc->Init.ScanConvMode == ADC_SCAN_ENABLE)
  563. {
  564. /* Set ADC group regular sequencer: */
  565. /* - Set ADC group regular sequencer to value memorized */
  566. /* in HAL ADC handle */
  567. /* Note: This value maybe be initialized at a unknown value, */
  568. /* therefore after the first call of "HAL_ADC_Init()", */
  569. /* each rank corresponding to parameter "NbrOfConversion" */
  570. /* must be set using "HAL_ADC_ConfigChannel()". */
  571. /* - Set sequencer scan length by clearing ranks above maximum rank */
  572. /* and do not modify other ranks value. */
  573. MODIFY_REG(hadc->Instance->CHSELR,
  574. ADC_CHSELR_SQ_ALL,
  575. (ADC_CHSELR_SQ2_TO_SQ8 << (((hadc->Init.NbrOfConversion - 1UL) * ADC_REGULAR_RANK_2) & 0x1FUL))
  576. | (hadc->ADCGroupRegularSequencerRanks)
  577. );
  578. }
  579. else
  580. {
  581. /* Nothing to do */
  582. }
  583. /* Check back that ADC registers have effectively been configured to */
  584. /* ensure of no potential problem of ADC core peripheral clocking. */
  585. if (LL_ADC_GetSamplingTimeCommonChannels(hadc->Instance, LL_ADC_SAMPLINGTIME_COMMON_1)
  586. == hadc->Init.SamplingTimeCommon1)
  587. {
  588. /* Set ADC error code to none */
  589. ADC_CLEAR_ERRORCODE(hadc);
  590. /* Set the ADC state */
  591. ADC_STATE_CLR_SET(hadc->State,
  592. HAL_ADC_STATE_BUSY_INTERNAL,
  593. HAL_ADC_STATE_READY);
  594. }
  595. else
  596. {
  597. /* Update ADC state machine to error */
  598. ADC_STATE_CLR_SET(hadc->State,
  599. HAL_ADC_STATE_BUSY_INTERNAL,
  600. HAL_ADC_STATE_ERROR_INTERNAL);
  601. /* Set ADC error code to ADC peripheral internal error */
  602. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  603. tmp_hal_status = HAL_ERROR;
  604. }
  605. }
  606. else
  607. {
  608. /* Update ADC state machine to error */
  609. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  610. tmp_hal_status = HAL_ERROR;
  611. }
  612. return tmp_hal_status;
  613. }
  614. /**
  615. * @brief Deinitialize the ADC peripheral registers to their default reset
  616. * values, with deinitialization of the ADC MSP.
  617. * @note For devices with several ADCs: reset of ADC common registers is done
  618. * only if all ADCs sharing the same common group are disabled.
  619. * (function "HAL_ADC_MspDeInit()" is also called under the same conditions:
  620. * all ADC instances use the same core clock at RCC level, disabling
  621. * the core clock reset all ADC instances).
  622. * If this is not the case, reset of these common parameters reset is
  623. * bypassed without error reporting: it can be the intended behavior in
  624. * case of reset of a single ADC while the other ADCs sharing the same
  625. * common group is still running.
  626. * @param hadc ADC handle
  627. * @retval HAL status
  628. */
  629. HAL_StatusTypeDef HAL_ADC_DeInit(ADC_HandleTypeDef *hadc)
  630. {
  631. HAL_StatusTypeDef tmp_hal_status;
  632. /* Check ADC handle */
  633. if (hadc == NULL)
  634. {
  635. return HAL_ERROR;
  636. }
  637. /* Check the parameters */
  638. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  639. /* Set ADC state */
  640. SET_BIT(hadc->State, HAL_ADC_STATE_BUSY_INTERNAL);
  641. /* Stop potential conversion on going, on regular group */
  642. tmp_hal_status = ADC_ConversionStop(hadc);
  643. /* Disable ADC peripheral if conversions are effectively stopped */
  644. if (tmp_hal_status == HAL_OK)
  645. {
  646. /* Disable the ADC peripheral */
  647. tmp_hal_status = ADC_Disable(hadc);
  648. /* Check if ADC is effectively disabled */
  649. if (tmp_hal_status == HAL_OK)
  650. {
  651. /* Change ADC state */
  652. hadc->State = HAL_ADC_STATE_READY;
  653. }
  654. /* Disable ADC internal voltage regulator */
  655. LL_ADC_DisableInternalRegulator(hadc->Instance);
  656. }
  657. /* Note: HAL ADC deInit is done independently of ADC conversion stop */
  658. /* and disable return status. In case of status fail, attempt to */
  659. /* perform deinitialization anyway and it is up user code in */
  660. /* in HAL_ADC_MspDeInit() to reset the ADC peripheral using */
  661. /* system RCC hard reset. */
  662. /* ========== Reset ADC registers ========== */
  663. /* Reset register IER */
  664. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_AWD3 | ADC_IT_AWD2 |
  665. ADC_IT_AWD1 | ADC_IT_OVR |
  666. ADC_IT_EOS | ADC_IT_EOC |
  667. ADC_IT_EOSMP | ADC_IT_RDY));
  668. /* Reset register ISR */
  669. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_AWD3 | ADC_FLAG_AWD2 |
  670. ADC_FLAG_AWD1 | ADC_FLAG_OVR |
  671. ADC_FLAG_EOS | ADC_FLAG_EOC |
  672. ADC_FLAG_EOSMP | ADC_FLAG_RDY));
  673. /* Reset register CR */
  674. /* Bits ADC_CR_ADCAL, ADC_CR_ADSTP, ADC_CR_ADSTART are in access mode */
  675. /* "read-set": no direct reset applicable. */
  676. /* Reset register CFGR1 */
  677. hadc->Instance->CFGR1 &= ~(ADC_CFGR1_AWD1CH | ADC_CFGR1_AWD1EN | ADC_CFGR1_AWD1SGL | ADC_CFGR1_DISCEN |
  678. ADC_CFGR1_CHSELRMOD | ADC_CFGR1_AUTOFF |
  679. ADC_CFGR1_WAIT | ADC_CFGR1_CONT | ADC_CFGR1_OVRMOD |
  680. ADC_CFGR1_EXTEN | ADC_CFGR1_EXTSEL | ADC_CFGR1_ALIGN | ADC_CFGR1_RES |
  681. ADC_CFGR1_SCANDIR | ADC_CFGR1_DMACFG | ADC_CFGR1_DMAEN);
  682. /* Reset register SMPR */
  683. hadc->Instance->SMPR &= ~ADC_SMPR_SMP1;
  684. /* Reset register CHSELR */
  685. hadc->Instance->CHSELR &= ~(ADC_CHSELR_SQ_ALL);
  686. /* Reset register DR */
  687. /* bits in access mode read only, no direct reset applicable */
  688. /* Reset registers AWDxTR */
  689. hadc->Instance->AWD1TR &= ~(ADC_AWD1TR_HT1 | ADC_AWD1TR_LT1);
  690. hadc->Instance->AWD2TR &= ~(ADC_AWD2TR_HT2 | ADC_AWD2TR_LT2);
  691. hadc->Instance->AWD3TR &= ~(ADC_AWD3TR_HT3 | ADC_AWD3TR_LT3);
  692. /* Reset register CFGR2 */
  693. /* Note: CFGR2 reset done at the end of de-initialization due to */
  694. /* clock source reset */
  695. /* Note: Update of ADC clock mode is conditioned to ADC state disabled: */
  696. /* already done above. */
  697. hadc->Instance->CFGR2 &= ~ADC_CFGR2_CKMODE;
  698. /* Reset register CCR */
  699. ADC1_COMMON->CCR &= ~(ADC_CCR_VBATEN | ADC_CCR_TSEN | ADC_CCR_VREFEN | ADC_CCR_PRESC);
  700. /* ========== Hard reset ADC peripheral ========== */
  701. /* Performs a global reset of the entire ADC peripheral: ADC state is */
  702. /* forced to a similar state after device power-on. */
  703. /* Note: A possible implementation is to add RCC bus reset of ADC */
  704. /* (for example, using macro */
  705. /* __HAL_RCC_ADC..._FORCE_RESET()/..._RELEASE_RESET()/..._CLK_DISABLE()) */
  706. /* in function "void HAL_ADC_MspDeInit(ADC_HandleTypeDef *hadc)": */
  707. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  708. if (hadc->MspDeInitCallback == NULL)
  709. {
  710. hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit */
  711. }
  712. /* DeInit the low level hardware */
  713. hadc->MspDeInitCallback(hadc);
  714. #else
  715. /* DeInit the low level hardware */
  716. HAL_ADC_MspDeInit(hadc);
  717. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  718. /* Reset HAL ADC handle variable */
  719. hadc->ADCGroupRegularSequencerRanks = 0x00000000UL;
  720. /* Set ADC error code to none */
  721. ADC_CLEAR_ERRORCODE(hadc);
  722. /* Set ADC state */
  723. hadc->State = HAL_ADC_STATE_RESET;
  724. __HAL_UNLOCK(hadc);
  725. return tmp_hal_status;
  726. }
  727. /**
  728. * @brief Initialize the ADC MSP.
  729. * @param hadc ADC handle
  730. * @retval None
  731. */
  732. __weak void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc)
  733. {
  734. /* Prevent unused argument(s) compilation warning */
  735. UNUSED(hadc);
  736. /* NOTE : This function should not be modified. When the callback is needed,
  737. function HAL_ADC_MspInit must be implemented in the user file.
  738. */
  739. }
  740. /**
  741. * @brief DeInitialize the ADC MSP.
  742. * @param hadc ADC handle
  743. * @note All ADC instances use the same core clock at RCC level, disabling
  744. * the core clock reset all ADC instances).
  745. * @retval None
  746. */
  747. __weak void HAL_ADC_MspDeInit(ADC_HandleTypeDef *hadc)
  748. {
  749. /* Prevent unused argument(s) compilation warning */
  750. UNUSED(hadc);
  751. /* NOTE : This function should not be modified. When the callback is needed,
  752. function HAL_ADC_MspDeInit must be implemented in the user file.
  753. */
  754. }
  755. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  756. /**
  757. * @brief Register a User ADC Callback
  758. * To be used instead of the weak predefined callback
  759. * @param hadc Pointer to a ADC_HandleTypeDef structure that contains
  760. * the configuration information for the specified ADC.
  761. * @param CallbackID ID of the callback to be registered
  762. * This parameter can be one of the following values:
  763. * @arg @ref HAL_ADC_CONVERSION_COMPLETE_CB_ID ADC conversion complete callback ID
  764. * @arg @ref HAL_ADC_CONVERSION_HALF_CB_ID ADC conversion DMA half-transfer callback ID
  765. * @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID ADC analog watchdog 1 callback ID
  766. * @arg @ref HAL_ADC_ERROR_CB_ID ADC error callback ID
  767. * @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_2_CB_ID ADC analog watchdog 2 callback ID
  768. * @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_3_CB_ID ADC analog watchdog 3 callback ID
  769. * @arg @ref HAL_ADC_END_OF_SAMPLING_CB_ID ADC end of sampling callback ID
  770. * @arg @ref HAL_ADC_MSPINIT_CB_ID ADC Msp Init callback ID
  771. * @arg @ref HAL_ADC_MSPDEINIT_CB_ID ADC Msp DeInit callback ID
  772. * @arg @ref HAL_ADC_MSPINIT_CB_ID MspInit callback ID
  773. * @arg @ref HAL_ADC_MSPDEINIT_CB_ID MspDeInit callback ID
  774. * @param pCallback pointer to the Callback function
  775. * @retval HAL status
  776. */
  777. HAL_StatusTypeDef HAL_ADC_RegisterCallback(ADC_HandleTypeDef *hadc, HAL_ADC_CallbackIDTypeDef CallbackID,
  778. pADC_CallbackTypeDef pCallback)
  779. {
  780. HAL_StatusTypeDef status = HAL_OK;
  781. if (pCallback == NULL)
  782. {
  783. /* Update the error code */
  784. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  785. return HAL_ERROR;
  786. }
  787. if ((hadc->State & HAL_ADC_STATE_READY) != 0UL)
  788. {
  789. switch (CallbackID)
  790. {
  791. case HAL_ADC_CONVERSION_COMPLETE_CB_ID :
  792. hadc->ConvCpltCallback = pCallback;
  793. break;
  794. case HAL_ADC_CONVERSION_HALF_CB_ID :
  795. hadc->ConvHalfCpltCallback = pCallback;
  796. break;
  797. case HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID :
  798. hadc->LevelOutOfWindowCallback = pCallback;
  799. break;
  800. case HAL_ADC_ERROR_CB_ID :
  801. hadc->ErrorCallback = pCallback;
  802. break;
  803. case HAL_ADC_LEVEL_OUT_OF_WINDOW_2_CB_ID :
  804. hadc->LevelOutOfWindow2Callback = pCallback;
  805. break;
  806. case HAL_ADC_LEVEL_OUT_OF_WINDOW_3_CB_ID :
  807. hadc->LevelOutOfWindow3Callback = pCallback;
  808. break;
  809. case HAL_ADC_END_OF_SAMPLING_CB_ID :
  810. hadc->EndOfSamplingCallback = pCallback;
  811. break;
  812. case HAL_ADC_MSPINIT_CB_ID :
  813. hadc->MspInitCallback = pCallback;
  814. break;
  815. case HAL_ADC_MSPDEINIT_CB_ID :
  816. hadc->MspDeInitCallback = pCallback;
  817. break;
  818. default :
  819. /* Update the error code */
  820. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  821. /* Return error status */
  822. status = HAL_ERROR;
  823. break;
  824. }
  825. }
  826. else if (HAL_ADC_STATE_RESET == hadc->State)
  827. {
  828. switch (CallbackID)
  829. {
  830. case HAL_ADC_MSPINIT_CB_ID :
  831. hadc->MspInitCallback = pCallback;
  832. break;
  833. case HAL_ADC_MSPDEINIT_CB_ID :
  834. hadc->MspDeInitCallback = pCallback;
  835. break;
  836. default :
  837. /* Update the error code */
  838. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  839. /* Return error status */
  840. status = HAL_ERROR;
  841. break;
  842. }
  843. }
  844. else
  845. {
  846. /* Update the error code */
  847. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  848. /* Return error status */
  849. status = HAL_ERROR;
  850. }
  851. return status;
  852. }
  853. /**
  854. * @brief Unregister a ADC Callback
  855. * ADC callback is redirected to the weak predefined callback
  856. * @param hadc Pointer to a ADC_HandleTypeDef structure that contains
  857. * the configuration information for the specified ADC.
  858. * @param CallbackID ID of the callback to be unregistered
  859. * This parameter can be one of the following values:
  860. * @arg @ref HAL_ADC_CONVERSION_COMPLETE_CB_ID ADC conversion complete callback ID
  861. * @arg @ref HAL_ADC_CONVERSION_HALF_CB_ID ADC conversion DMA half-transfer callback ID
  862. * @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID ADC analog watchdog 1 callback ID
  863. * @arg @ref HAL_ADC_ERROR_CB_ID ADC error callback ID
  864. * @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_2_CB_ID ADC analog watchdog 2 callback ID
  865. * @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_3_CB_ID ADC analog watchdog 3 callback ID
  866. * @arg @ref HAL_ADC_END_OF_SAMPLING_CB_ID ADC end of sampling callback ID
  867. * @arg @ref HAL_ADC_MSPINIT_CB_ID ADC Msp Init callback ID
  868. * @arg @ref HAL_ADC_MSPDEINIT_CB_ID ADC Msp DeInit callback ID
  869. * @arg @ref HAL_ADC_MSPINIT_CB_ID MspInit callback ID
  870. * @arg @ref HAL_ADC_MSPDEINIT_CB_ID MspDeInit callback ID
  871. * @retval HAL status
  872. */
  873. HAL_StatusTypeDef HAL_ADC_UnRegisterCallback(ADC_HandleTypeDef *hadc, HAL_ADC_CallbackIDTypeDef CallbackID)
  874. {
  875. HAL_StatusTypeDef status = HAL_OK;
  876. if ((hadc->State & HAL_ADC_STATE_READY) != 0UL)
  877. {
  878. switch (CallbackID)
  879. {
  880. case HAL_ADC_CONVERSION_COMPLETE_CB_ID :
  881. hadc->ConvCpltCallback = HAL_ADC_ConvCpltCallback;
  882. break;
  883. case HAL_ADC_CONVERSION_HALF_CB_ID :
  884. hadc->ConvHalfCpltCallback = HAL_ADC_ConvHalfCpltCallback;
  885. break;
  886. case HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID :
  887. hadc->LevelOutOfWindowCallback = HAL_ADC_LevelOutOfWindowCallback;
  888. break;
  889. case HAL_ADC_ERROR_CB_ID :
  890. hadc->ErrorCallback = HAL_ADC_ErrorCallback;
  891. break;
  892. case HAL_ADC_LEVEL_OUT_OF_WINDOW_2_CB_ID :
  893. hadc->LevelOutOfWindow2Callback = HAL_ADCEx_LevelOutOfWindow2Callback;
  894. break;
  895. case HAL_ADC_LEVEL_OUT_OF_WINDOW_3_CB_ID :
  896. hadc->LevelOutOfWindow3Callback = HAL_ADCEx_LevelOutOfWindow3Callback;
  897. break;
  898. case HAL_ADC_END_OF_SAMPLING_CB_ID :
  899. hadc->EndOfSamplingCallback = HAL_ADCEx_EndOfSamplingCallback;
  900. break;
  901. case HAL_ADC_MSPINIT_CB_ID :
  902. hadc->MspInitCallback = HAL_ADC_MspInit; /* Legacy weak MspInit */
  903. break;
  904. case HAL_ADC_MSPDEINIT_CB_ID :
  905. hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit */
  906. break;
  907. default :
  908. /* Update the error code */
  909. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  910. /* Return error status */
  911. status = HAL_ERROR;
  912. break;
  913. }
  914. }
  915. else if (HAL_ADC_STATE_RESET == hadc->State)
  916. {
  917. switch (CallbackID)
  918. {
  919. case HAL_ADC_MSPINIT_CB_ID :
  920. hadc->MspInitCallback = HAL_ADC_MspInit; /* Legacy weak MspInit */
  921. break;
  922. case HAL_ADC_MSPDEINIT_CB_ID :
  923. hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit */
  924. break;
  925. default :
  926. /* Update the error code */
  927. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  928. /* Return error status */
  929. status = HAL_ERROR;
  930. break;
  931. }
  932. }
  933. else
  934. {
  935. /* Update the error code */
  936. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  937. /* Return error status */
  938. status = HAL_ERROR;
  939. }
  940. return status;
  941. }
  942. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  943. /**
  944. * @}
  945. */
  946. /** @defgroup ADC_Exported_Functions_Group2 ADC Input and Output operation functions
  947. * @brief ADC IO operation functions
  948. *
  949. @verbatim
  950. ===============================================================================
  951. ##### IO operation functions #####
  952. ===============================================================================
  953. [..] This section provides functions allowing to:
  954. (+) Start conversion of regular group.
  955. (+) Stop conversion of regular group.
  956. (+) Poll for conversion complete on regular group.
  957. (+) Poll for conversion event.
  958. (+) Get result of regular channel conversion.
  959. (+) Start conversion of regular group and enable interruptions.
  960. (+) Stop conversion of regular group and disable interruptions.
  961. (+) Handle ADC interrupt request
  962. (+) Start conversion of regular group and enable DMA transfer.
  963. (+) Stop conversion of regular group and disable ADC DMA transfer.
  964. @endverbatim
  965. * @{
  966. */
  967. /**
  968. * @brief Enable ADC, start conversion of regular group.
  969. * @note Interruptions enabled in this function: None.
  970. * @param hadc ADC handle
  971. * @retval HAL status
  972. */
  973. HAL_StatusTypeDef HAL_ADC_Start(ADC_HandleTypeDef *hadc)
  974. {
  975. HAL_StatusTypeDef tmp_hal_status;
  976. /* Check the parameters */
  977. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  978. /* Perform ADC enable and conversion start if no conversion is on going */
  979. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
  980. {
  981. __HAL_LOCK(hadc);
  982. /* Enable the ADC peripheral */
  983. tmp_hal_status = ADC_Enable(hadc);
  984. /* Start conversion if ADC is effectively enabled */
  985. if (tmp_hal_status == HAL_OK)
  986. {
  987. /* Set ADC state */
  988. /* - Clear state bitfield related to regular group conversion results */
  989. /* - Set state bitfield related to regular operation */
  990. ADC_STATE_CLR_SET(hadc->State,
  991. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP,
  992. HAL_ADC_STATE_REG_BUSY);
  993. /* Set ADC error code */
  994. /* Reset all ADC error code fields */
  995. ADC_CLEAR_ERRORCODE(hadc);
  996. /* Clear ADC group regular conversion flag and overrun flag */
  997. /* (To ensure of no unknown state from potential previous ADC operations) */
  998. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
  999. /* Process unlocked */
  1000. /* Unlock before starting ADC conversions: in case of potential */
  1001. /* interruption, to let the process to ADC IRQ Handler. */
  1002. __HAL_UNLOCK(hadc);
  1003. /* Enable conversion of regular group. */
  1004. /* If software start has been selected, conversion starts immediately. */
  1005. /* If external trigger has been selected, conversion will start at next */
  1006. /* trigger event. */
  1007. /* Start ADC group regular conversion */
  1008. LL_ADC_REG_StartConversion(hadc->Instance);
  1009. }
  1010. else
  1011. {
  1012. __HAL_UNLOCK(hadc);
  1013. }
  1014. }
  1015. else
  1016. {
  1017. tmp_hal_status = HAL_BUSY;
  1018. }
  1019. return tmp_hal_status;
  1020. }
  1021. /**
  1022. * @brief Stop ADC conversion of regular group (and injected channels in
  1023. * case of auto_injection mode), disable ADC peripheral.
  1024. * @note: ADC peripheral disable is forcing stop of potential
  1025. * conversion on injected group. If injected group is under use, it
  1026. * should be preliminarily stopped using HAL_ADCEx_InjectedStop function.
  1027. * @param hadc ADC handle
  1028. * @retval HAL status.
  1029. */
  1030. HAL_StatusTypeDef HAL_ADC_Stop(ADC_HandleTypeDef *hadc)
  1031. {
  1032. HAL_StatusTypeDef tmp_hal_status;
  1033. /* Check the parameters */
  1034. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1035. __HAL_LOCK(hadc);
  1036. /* 1. Stop potential conversion on going, on ADC group regular */
  1037. tmp_hal_status = ADC_ConversionStop(hadc);
  1038. /* Disable ADC peripheral if conversions are effectively stopped */
  1039. if (tmp_hal_status == HAL_OK)
  1040. {
  1041. /* 2. Disable the ADC peripheral */
  1042. tmp_hal_status = ADC_Disable(hadc);
  1043. /* Check if ADC is effectively disabled */
  1044. if (tmp_hal_status == HAL_OK)
  1045. {
  1046. /* Set ADC state */
  1047. ADC_STATE_CLR_SET(hadc->State,
  1048. HAL_ADC_STATE_REG_BUSY,
  1049. HAL_ADC_STATE_READY);
  1050. }
  1051. }
  1052. __HAL_UNLOCK(hadc);
  1053. return tmp_hal_status;
  1054. }
  1055. /**
  1056. * @brief Wait for regular group conversion to be completed.
  1057. * @note ADC conversion flags EOS (end of sequence) and EOC (end of
  1058. * conversion) are cleared by this function, with an exception:
  1059. * if low power feature "LowPowerAutoWait" is enabled, flags are
  1060. * not cleared to not interfere with this feature until data register
  1061. * is read using function HAL_ADC_GetValue().
  1062. * @note This function cannot be used in a particular setup: ADC configured
  1063. * in DMA mode and polling for end of each conversion (ADC init
  1064. * parameter "EOCSelection" set to ADC_EOC_SINGLE_CONV).
  1065. * In this case, DMA resets the flag EOC and polling cannot be
  1066. * performed on each conversion. Nevertheless, polling can still
  1067. * be performed on the complete sequence (ADC init
  1068. * parameter "EOCSelection" set to ADC_EOC_SEQ_CONV).
  1069. * @param hadc ADC handle
  1070. * @param Timeout Timeout value in millisecond.
  1071. * @retval HAL status
  1072. */
  1073. HAL_StatusTypeDef HAL_ADC_PollForConversion(ADC_HandleTypeDef *hadc, uint32_t Timeout)
  1074. {
  1075. uint32_t tickstart;
  1076. uint32_t tmp_flag_end;
  1077. /* Check the parameters */
  1078. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1079. /* If end of conversion selected to end of sequence conversions */
  1080. if (hadc->Init.EOCSelection == ADC_EOC_SEQ_CONV)
  1081. {
  1082. tmp_flag_end = ADC_FLAG_EOS;
  1083. }
  1084. /* If end of conversion selected to end of unitary conversion */
  1085. else /* ADC_EOC_SINGLE_CONV */
  1086. {
  1087. /* Verification that ADC configuration is compliant with polling for */
  1088. /* each conversion: */
  1089. /* Particular case is ADC configured in DMA mode and ADC sequencer with */
  1090. /* several ranks and polling for end of each conversion. */
  1091. /* For code simplicity sake, this particular case is generalized to */
  1092. /* ADC configured in DMA mode and and polling for end of each conversion. */
  1093. if ((hadc->Instance->CFGR1 & ADC_CFGR1_DMAEN) != 0UL)
  1094. {
  1095. /* Update ADC state machine to error */
  1096. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1097. return HAL_ERROR;
  1098. }
  1099. else
  1100. {
  1101. tmp_flag_end = (ADC_FLAG_EOC);
  1102. }
  1103. }
  1104. /* Get tick count */
  1105. tickstart = HAL_GetTick();
  1106. /* Wait until End of unitary conversion or sequence conversions flag is raised */
  1107. while ((hadc->Instance->ISR & tmp_flag_end) == 0UL)
  1108. {
  1109. /* Check if timeout is disabled (set to infinite wait) */
  1110. if (Timeout != HAL_MAX_DELAY)
  1111. {
  1112. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0UL))
  1113. {
  1114. /* New check to avoid false timeout detection in case of preemption */
  1115. if ((hadc->Instance->ISR & tmp_flag_end) == 0UL)
  1116. {
  1117. /* Update ADC state machine to timeout */
  1118. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  1119. __HAL_UNLOCK(hadc);
  1120. return HAL_TIMEOUT;
  1121. }
  1122. }
  1123. }
  1124. }
  1125. /* Update ADC state machine */
  1126. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  1127. /* Determine whether any further conversion upcoming on group regular */
  1128. /* by external trigger, continuous mode or scan sequence on going. */
  1129. if ((LL_ADC_REG_IsTriggerSourceSWStart(hadc->Instance) != 0UL)
  1130. && (hadc->Init.ContinuousConvMode == DISABLE)
  1131. )
  1132. {
  1133. /* Check whether end of sequence is reached */
  1134. if (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOS))
  1135. {
  1136. /* Allowed to modify bits ADC_IT_EOC/ADC_IT_EOS only if bit */
  1137. /* ADSTART==0 (no conversion on going) */
  1138. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
  1139. {
  1140. /* Disable ADC end of single conversion interrupt on group regular */
  1141. /* Note: Overrun interrupt was enabled with EOC interrupt in */
  1142. /* HAL_Start_IT(), but is not disabled here because can be used */
  1143. /* by overrun IRQ process below. */
  1144. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC | ADC_IT_EOS);
  1145. /* Set ADC state */
  1146. ADC_STATE_CLR_SET(hadc->State,
  1147. HAL_ADC_STATE_REG_BUSY,
  1148. HAL_ADC_STATE_READY);
  1149. }
  1150. else
  1151. {
  1152. /* Change ADC state to error state */
  1153. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1154. /* Set ADC error code to ADC peripheral internal error */
  1155. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  1156. }
  1157. }
  1158. }
  1159. /* Clear end of conversion flag of regular group if low power feature */
  1160. /* "LowPowerAutoWait " is disabled, to not interfere with this feature */
  1161. /* until data register is read using function HAL_ADC_GetValue(). */
  1162. if (hadc->Init.LowPowerAutoWait == DISABLE)
  1163. {
  1164. /* Clear regular group conversion flag */
  1165. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS));
  1166. }
  1167. /* Return function status */
  1168. return HAL_OK;
  1169. }
  1170. /**
  1171. * @brief Poll for ADC event.
  1172. * @param hadc ADC handle
  1173. * @param EventType the ADC event type.
  1174. * This parameter can be one of the following values:
  1175. * @arg @ref ADC_EOSMP_EVENT ADC End of Sampling event
  1176. * @arg @ref ADC_AWD1_EVENT ADC Analog watchdog 1 event (main analog watchdog, present on
  1177. * all STM32 series)
  1178. * @arg @ref ADC_AWD2_EVENT ADC Analog watchdog 2 event (additional analog watchdog, not present on
  1179. * all STM32 series)
  1180. * @arg @ref ADC_AWD3_EVENT ADC Analog watchdog 3 event (additional analog watchdog, not present on
  1181. * all STM32 series)
  1182. * @arg @ref ADC_OVR_EVENT ADC Overrun event
  1183. * @param Timeout Timeout value in millisecond.
  1184. * @note The relevant flag is cleared if found to be set, except for ADC_FLAG_OVR.
  1185. * Indeed, the latter is reset only if hadc->Init.Overrun field is set
  1186. * to ADC_OVR_DATA_OVERWRITTEN. Otherwise, data register may be potentially overwritten
  1187. * by a new converted data as soon as OVR is cleared.
  1188. * To reset OVR flag once the preserved data is retrieved, the user can resort
  1189. * to macro __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  1190. * @retval HAL status
  1191. */
  1192. HAL_StatusTypeDef HAL_ADC_PollForEvent(ADC_HandleTypeDef *hadc, uint32_t EventType, uint32_t Timeout)
  1193. {
  1194. uint32_t tickstart;
  1195. /* Check the parameters */
  1196. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1197. assert_param(IS_ADC_EVENT_TYPE(EventType));
  1198. /* Get tick count */
  1199. tickstart = HAL_GetTick();
  1200. /* Check selected event flag */
  1201. while (__HAL_ADC_GET_FLAG(hadc, EventType) == 0UL)
  1202. {
  1203. /* Check if timeout is disabled (set to infinite wait) */
  1204. if (Timeout != HAL_MAX_DELAY)
  1205. {
  1206. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0UL))
  1207. {
  1208. /* New check to avoid false timeout detection in case of preemption */
  1209. if (__HAL_ADC_GET_FLAG(hadc, EventType) == 0UL)
  1210. {
  1211. /* Update ADC state machine to timeout */
  1212. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  1213. __HAL_UNLOCK(hadc);
  1214. return HAL_TIMEOUT;
  1215. }
  1216. }
  1217. }
  1218. }
  1219. switch (EventType)
  1220. {
  1221. /* End Of Sampling event */
  1222. case ADC_EOSMP_EVENT:
  1223. /* Set ADC state */
  1224. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOSMP);
  1225. /* Clear the End Of Sampling flag */
  1226. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOSMP);
  1227. break;
  1228. /* Analog watchdog (level out of window) event */
  1229. /* Note: In case of several analog watchdog enabled, if needed to know */
  1230. /* which one triggered and on which ADCx, test ADC state of analog watchdog */
  1231. /* flags HAL_ADC_STATE_AWD1/2/3 using function "HAL_ADC_GetState()". */
  1232. /* For example: */
  1233. /* " if ((HAL_ADC_GetState(hadc1) & HAL_ADC_STATE_AWD1) != 0UL) " */
  1234. /* " if ((HAL_ADC_GetState(hadc1) & HAL_ADC_STATE_AWD2) != 0UL) " */
  1235. /* " if ((HAL_ADC_GetState(hadc1) & HAL_ADC_STATE_AWD3) != 0UL) " */
  1236. /* Check analog watchdog 1 flag */
  1237. case ADC_AWD_EVENT:
  1238. /* Set ADC state */
  1239. SET_BIT(hadc->State, HAL_ADC_STATE_AWD1);
  1240. /* Clear ADC analog watchdog flag */
  1241. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD1);
  1242. break;
  1243. /* Check analog watchdog 2 flag */
  1244. case ADC_AWD2_EVENT:
  1245. /* Set ADC state */
  1246. SET_BIT(hadc->State, HAL_ADC_STATE_AWD2);
  1247. /* Clear ADC analog watchdog flag */
  1248. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD2);
  1249. break;
  1250. /* Check analog watchdog 3 flag */
  1251. case ADC_AWD3_EVENT:
  1252. /* Set ADC state */
  1253. SET_BIT(hadc->State, HAL_ADC_STATE_AWD3);
  1254. /* Clear ADC analog watchdog flag */
  1255. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD3);
  1256. break;
  1257. /* Overrun event */
  1258. default: /* Case ADC_OVR_EVENT */
  1259. /* If overrun is set to overwrite previous data, overrun event is not */
  1260. /* considered as an error. */
  1261. /* (cf ref manual "Managing conversions without using the DMA and without */
  1262. /* overrun ") */
  1263. if (hadc->Init.Overrun == ADC_OVR_DATA_PRESERVED)
  1264. {
  1265. /* Set ADC state */
  1266. SET_BIT(hadc->State, HAL_ADC_STATE_REG_OVR);
  1267. /* Set ADC error code to overrun */
  1268. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_OVR);
  1269. }
  1270. else
  1271. {
  1272. /* Clear ADC Overrun flag only if Overrun is set to ADC_OVR_DATA_OVERWRITTEN
  1273. otherwise, data register is potentially overwritten by new converted data as soon
  1274. as OVR is cleared. */
  1275. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  1276. }
  1277. break;
  1278. }
  1279. /* Return function status */
  1280. return HAL_OK;
  1281. }
  1282. /**
  1283. * @brief Enable ADC, start conversion of regular group with interruption.
  1284. * @note Interruptions enabled in this function according to initialization
  1285. * setting : EOC (end of conversion), EOS (end of sequence),
  1286. * OVR overrun.
  1287. * Each of these interruptions has its dedicated callback function.
  1288. * @note To guarantee a proper reset of all interruptions once all the needed
  1289. * conversions are obtained, HAL_ADC_Stop_IT() must be called to ensure
  1290. * a correct stop of the IT-based conversions.
  1291. * @note By default, HAL_ADC_Start_IT() does not enable the End Of Sampling
  1292. * interruption. If required (e.g. in case of oversampling with trigger
  1293. * mode), the user must:
  1294. * 1. first clear the EOSMP flag if set with macro __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOSMP)
  1295. * 2. then enable the EOSMP interrupt with macro __HAL_ADC_ENABLE_IT(hadc, ADC_IT_EOSMP)
  1296. * before calling HAL_ADC_Start_IT().
  1297. * @param hadc ADC handle
  1298. * @retval HAL status
  1299. */
  1300. HAL_StatusTypeDef HAL_ADC_Start_IT(ADC_HandleTypeDef *hadc)
  1301. {
  1302. HAL_StatusTypeDef tmp_hal_status;
  1303. /* Check the parameters */
  1304. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1305. /* Perform ADC enable and conversion start if no conversion is on going */
  1306. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
  1307. {
  1308. __HAL_LOCK(hadc);
  1309. /* Enable the ADC peripheral */
  1310. tmp_hal_status = ADC_Enable(hadc);
  1311. /* Start conversion if ADC is effectively enabled */
  1312. if (tmp_hal_status == HAL_OK)
  1313. {
  1314. /* Set ADC state */
  1315. /* - Clear state bitfield related to regular group conversion results */
  1316. /* - Set state bitfield related to regular operation */
  1317. ADC_STATE_CLR_SET(hadc->State,
  1318. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP,
  1319. HAL_ADC_STATE_REG_BUSY);
  1320. /* Set ADC error code */
  1321. /* Reset all ADC error code fields */
  1322. ADC_CLEAR_ERRORCODE(hadc);
  1323. /* Clear ADC group regular conversion flag and overrun flag */
  1324. /* (To ensure of no unknown state from potential previous ADC operations) */
  1325. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
  1326. /* Process unlocked */
  1327. /* Unlock before starting ADC conversions: in case of potential */
  1328. /* interruption, to let the process to ADC IRQ Handler. */
  1329. __HAL_UNLOCK(hadc);
  1330. /* Disable all interruptions before enabling the desired ones */
  1331. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_EOS | ADC_IT_OVR));
  1332. /* Enable ADC end of conversion interrupt */
  1333. switch (hadc->Init.EOCSelection)
  1334. {
  1335. case ADC_EOC_SEQ_CONV:
  1336. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_EOS);
  1337. break;
  1338. /* case ADC_EOC_SINGLE_CONV */
  1339. default:
  1340. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_EOC);
  1341. break;
  1342. }
  1343. /* Enable ADC overrun interrupt */
  1344. /* If hadc->Init.Overrun is set to ADC_OVR_DATA_PRESERVED, only then is
  1345. ADC_IT_OVR enabled; otherwise data overwrite is considered as normal
  1346. behavior and no CPU time is lost for a non-processed interruption */
  1347. if (hadc->Init.Overrun == ADC_OVR_DATA_PRESERVED)
  1348. {
  1349. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);
  1350. }
  1351. /* Enable conversion of regular group. */
  1352. /* If software start has been selected, conversion starts immediately. */
  1353. /* If external trigger has been selected, conversion will start at next */
  1354. /* trigger event. */
  1355. /* Start ADC group regular conversion */
  1356. LL_ADC_REG_StartConversion(hadc->Instance);
  1357. }
  1358. else
  1359. {
  1360. __HAL_UNLOCK(hadc);
  1361. }
  1362. }
  1363. else
  1364. {
  1365. tmp_hal_status = HAL_BUSY;
  1366. }
  1367. return tmp_hal_status;
  1368. }
  1369. /**
  1370. * @brief Stop ADC conversion of regular group (and injected group in
  1371. * case of auto_injection mode), disable interrution of
  1372. * end-of-conversion, disable ADC peripheral.
  1373. * @param hadc ADC handle
  1374. * @retval HAL status.
  1375. */
  1376. HAL_StatusTypeDef HAL_ADC_Stop_IT(ADC_HandleTypeDef *hadc)
  1377. {
  1378. HAL_StatusTypeDef tmp_hal_status;
  1379. /* Check the parameters */
  1380. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1381. __HAL_LOCK(hadc);
  1382. /* 1. Stop potential conversion on going, on ADC group regular */
  1383. tmp_hal_status = ADC_ConversionStop(hadc);
  1384. /* Disable ADC peripheral if conversions are effectively stopped */
  1385. if (tmp_hal_status == HAL_OK)
  1386. {
  1387. /* Disable ADC end of conversion interrupt for regular group */
  1388. /* Disable ADC overrun interrupt */
  1389. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_EOS | ADC_IT_OVR));
  1390. /* 2. Disable the ADC peripheral */
  1391. tmp_hal_status = ADC_Disable(hadc);
  1392. /* Check if ADC is effectively disabled */
  1393. if (tmp_hal_status == HAL_OK)
  1394. {
  1395. /* Set ADC state */
  1396. ADC_STATE_CLR_SET(hadc->State,
  1397. HAL_ADC_STATE_REG_BUSY,
  1398. HAL_ADC_STATE_READY);
  1399. }
  1400. }
  1401. __HAL_UNLOCK(hadc);
  1402. return tmp_hal_status;
  1403. }
  1404. /**
  1405. * @brief Enable ADC, start conversion of regular group and transfer result through DMA.
  1406. * @note Interruptions enabled in this function:
  1407. * overrun (if applicable), DMA half transfer, DMA transfer complete.
  1408. * Each of these interruptions has its dedicated callback function.
  1409. * @param hadc ADC handle
  1410. * @param pData Destination Buffer address.
  1411. * @param Length Number of data to be transferred from ADC peripheral to memory
  1412. * @retval HAL status.
  1413. */
  1414. HAL_StatusTypeDef HAL_ADC_Start_DMA(ADC_HandleTypeDef *hadc, uint32_t *pData, uint32_t Length)
  1415. {
  1416. HAL_StatusTypeDef tmp_hal_status;
  1417. /* Check the parameters */
  1418. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1419. /* Perform ADC enable and conversion start if no conversion is on going */
  1420. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
  1421. {
  1422. __HAL_LOCK(hadc);
  1423. /* Specific case for first call occurrence of this function (DMA transfer */
  1424. /* not activated and ADC disabled), DMA transfer must be activated */
  1425. /* with ADC disabled. */
  1426. if ((hadc->Instance->CFGR1 & ADC_CFGR1_DMAEN) == 0UL)
  1427. {
  1428. if (LL_ADC_IsEnabled(hadc->Instance) != 0UL)
  1429. {
  1430. /* Disable ADC */
  1431. LL_ADC_Disable(hadc->Instance);
  1432. }
  1433. /* Enable ADC DMA mode */
  1434. hadc->Instance->CFGR1 |= ADC_CFGR1_DMAEN;
  1435. }
  1436. /* Enable the ADC peripheral */
  1437. tmp_hal_status = ADC_Enable(hadc);
  1438. /* Start conversion if ADC is effectively enabled */
  1439. if (tmp_hal_status == HAL_OK)
  1440. {
  1441. /* Set ADC state */
  1442. /* - Clear state bitfield related to regular group conversion results */
  1443. /* - Set state bitfield related to regular operation */
  1444. ADC_STATE_CLR_SET(hadc->State,
  1445. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP,
  1446. HAL_ADC_STATE_REG_BUSY);
  1447. /* Set ADC error code */
  1448. /* Reset all ADC error code fields */
  1449. ADC_CLEAR_ERRORCODE(hadc);
  1450. /* Set the DMA transfer complete callback */
  1451. hadc->DMA_Handle->XferCpltCallback = ADC_DMAConvCplt;
  1452. /* Set the DMA half transfer complete callback */
  1453. hadc->DMA_Handle->XferHalfCpltCallback = ADC_DMAHalfConvCplt;
  1454. /* Set the DMA error callback */
  1455. hadc->DMA_Handle->XferErrorCallback = ADC_DMAError;
  1456. /* Manage ADC and DMA start: ADC overrun interruption, DMA start, ADC */
  1457. /* start (in case of SW start): */
  1458. /* Clear regular group conversion flag and overrun flag */
  1459. /* (To ensure of no unknown state from potential previous ADC */
  1460. /* operations) */
  1461. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
  1462. /* Process unlocked */
  1463. /* Unlock before starting ADC conversions: in case of potential */
  1464. /* interruption, to let the process to ADC IRQ Handler. */
  1465. __HAL_UNLOCK(hadc);
  1466. /* Enable ADC overrun interrupt */
  1467. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);
  1468. /* Start the DMA channel */
  1469. tmp_hal_status = HAL_DMA_Start_IT(hadc->DMA_Handle, (uint32_t)&hadc->Instance->DR, (uint32_t)pData, Length);
  1470. /* Enable conversion of regular group. */
  1471. /* If software start has been selected, conversion starts immediately. */
  1472. /* If external trigger has been selected, conversion will start at next */
  1473. /* trigger event. */
  1474. /* Start ADC group regular conversion */
  1475. LL_ADC_REG_StartConversion(hadc->Instance);
  1476. }
  1477. }
  1478. else
  1479. {
  1480. tmp_hal_status = HAL_BUSY;
  1481. }
  1482. return tmp_hal_status;
  1483. }
  1484. /**
  1485. * @brief Stop ADC conversion of regular group (and injected group in
  1486. * case of auto_injection mode), disable ADC DMA transfer, disable
  1487. * ADC peripheral.
  1488. * @param hadc ADC handle
  1489. * @retval HAL status.
  1490. */
  1491. HAL_StatusTypeDef HAL_ADC_Stop_DMA(ADC_HandleTypeDef *hadc)
  1492. {
  1493. HAL_StatusTypeDef tmp_hal_status;
  1494. /* Check the parameters */
  1495. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1496. __HAL_LOCK(hadc);
  1497. /* 1. Stop potential ADC group regular conversion on going */
  1498. tmp_hal_status = ADC_ConversionStop(hadc);
  1499. /* Disable ADC peripheral if conversions are effectively stopped */
  1500. if (tmp_hal_status == HAL_OK)
  1501. {
  1502. /* Disable the DMA channel (in case of DMA in circular mode or stop */
  1503. /* while DMA transfer is on going) */
  1504. if (hadc->DMA_Handle->State == HAL_DMA_STATE_BUSY)
  1505. {
  1506. tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
  1507. /* Check if DMA channel effectively disabled */
  1508. if (tmp_hal_status != HAL_OK)
  1509. {
  1510. /* Update ADC state machine to error */
  1511. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  1512. }
  1513. }
  1514. /* Disable ADC overrun interrupt */
  1515. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
  1516. /* 2. Disable the ADC peripheral */
  1517. /* Update "tmp_hal_status" only if DMA channel disabling passed, */
  1518. /* to keep in memory a potential failing status. */
  1519. if (tmp_hal_status == HAL_OK)
  1520. {
  1521. tmp_hal_status = ADC_Disable(hadc);
  1522. }
  1523. else
  1524. {
  1525. (void)ADC_Disable(hadc);
  1526. }
  1527. /* Check if ADC is effectively disabled */
  1528. if (tmp_hal_status == HAL_OK)
  1529. {
  1530. /* Set ADC state */
  1531. ADC_STATE_CLR_SET(hadc->State,
  1532. HAL_ADC_STATE_REG_BUSY,
  1533. HAL_ADC_STATE_READY);
  1534. }
  1535. /* Disable ADC DMA (ADC DMA configuration of continuous requests is kept) */
  1536. CLEAR_BIT(hadc->Instance->CFGR1, ADC_CFGR1_DMAEN);
  1537. }
  1538. __HAL_UNLOCK(hadc);
  1539. return tmp_hal_status;
  1540. }
  1541. /**
  1542. * @brief Get ADC regular group conversion result.
  1543. * @note Reading register DR automatically clears ADC flag EOC
  1544. * (ADC group regular end of unitary conversion).
  1545. * @note This function does not clear ADC flag EOS
  1546. * (ADC group regular end of sequence conversion).
  1547. * Occurrence of flag EOS rising:
  1548. * - If sequencer is composed of 1 rank, flag EOS is equivalent
  1549. * to flag EOC.
  1550. * - If sequencer is composed of several ranks, during the scan
  1551. * sequence flag EOC only is raised, at the end of the scan sequence
  1552. * both flags EOC and EOS are raised.
  1553. * To clear this flag, either use function:
  1554. * in programming model IT: @ref HAL_ADC_IRQHandler(), in programming
  1555. * model polling: @ref HAL_ADC_PollForConversion()
  1556. * or @ref __HAL_ADC_CLEAR_FLAG(&hadc, ADC_FLAG_EOS).
  1557. * @param hadc ADC handle
  1558. * @retval ADC group regular conversion data
  1559. */
  1560. uint32_t HAL_ADC_GetValue(const ADC_HandleTypeDef *hadc)
  1561. {
  1562. /* Check the parameters */
  1563. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1564. /* Note: EOC flag is not cleared here by software because automatically */
  1565. /* cleared by hardware when reading register DR. */
  1566. /* Return ADC converted value */
  1567. return hadc->Instance->DR;
  1568. }
  1569. /**
  1570. * @brief Handle ADC interrupt request.
  1571. * @param hadc ADC handle
  1572. * @retval None
  1573. */
  1574. void HAL_ADC_IRQHandler(ADC_HandleTypeDef *hadc)
  1575. {
  1576. uint32_t overrun_error = 0UL; /* flag set if overrun occurrence has to be considered as an error */
  1577. uint32_t tmp_isr = hadc->Instance->ISR;
  1578. uint32_t tmp_ier = hadc->Instance->IER;
  1579. /* Check the parameters */
  1580. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1581. assert_param(IS_ADC_EOC_SELECTION(hadc->Init.EOCSelection));
  1582. /* ========== Check End of Sampling flag for ADC group regular ========== */
  1583. if (((tmp_isr & ADC_FLAG_EOSMP) == ADC_FLAG_EOSMP) && ((tmp_ier & ADC_IT_EOSMP) == ADC_IT_EOSMP))
  1584. {
  1585. /* Update state machine on end of sampling status if not in error state */
  1586. if ((hadc->State & HAL_ADC_STATE_ERROR_INTERNAL) == 0UL)
  1587. {
  1588. /* Set ADC state */
  1589. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOSMP);
  1590. }
  1591. /* End Of Sampling callback */
  1592. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  1593. hadc->EndOfSamplingCallback(hadc);
  1594. #else
  1595. HAL_ADCEx_EndOfSamplingCallback(hadc);
  1596. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  1597. /* Clear regular group conversion flag */
  1598. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOSMP);
  1599. }
  1600. /* ====== Check ADC group regular end of unitary conversion sequence conversions ===== */
  1601. if ((((tmp_isr & ADC_FLAG_EOC) == ADC_FLAG_EOC) && ((tmp_ier & ADC_IT_EOC) == ADC_IT_EOC)) ||
  1602. (((tmp_isr & ADC_FLAG_EOS) == ADC_FLAG_EOS) && ((tmp_ier & ADC_IT_EOS) == ADC_IT_EOS)))
  1603. {
  1604. /* Update state machine on conversion status if not in error state */
  1605. if ((hadc->State & HAL_ADC_STATE_ERROR_INTERNAL) == 0UL)
  1606. {
  1607. /* Set ADC state */
  1608. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  1609. }
  1610. /* Determine whether any further conversion upcoming on group regular */
  1611. /* by external trigger, continuous mode or scan sequence on going */
  1612. /* to disable interruption. */
  1613. if ((LL_ADC_REG_IsTriggerSourceSWStart(hadc->Instance) != 0UL)
  1614. && (hadc->Init.ContinuousConvMode == DISABLE)
  1615. )
  1616. {
  1617. /* If End of Sequence is reached, disable interrupts */
  1618. if (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOS))
  1619. {
  1620. /* Allowed to modify bits ADC_IT_EOC/ADC_IT_EOS only if bit */
  1621. /* ADSTART==0 (no conversion on going) */
  1622. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
  1623. {
  1624. /* Disable ADC end of single conversion interrupt on group regular */
  1625. /* Note: Overrun interrupt was enabled with EOC interrupt in */
  1626. /* HAL_Start_IT(), but is not disabled here because can be used */
  1627. /* by overrun IRQ process below. */
  1628. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC | ADC_IT_EOS);
  1629. /* Set ADC state */
  1630. ADC_STATE_CLR_SET(hadc->State,
  1631. HAL_ADC_STATE_REG_BUSY,
  1632. HAL_ADC_STATE_READY);
  1633. }
  1634. else
  1635. {
  1636. /* Change ADC state to error state */
  1637. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1638. /* Set ADC error code to ADC peripheral internal error */
  1639. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  1640. }
  1641. }
  1642. }
  1643. /* Conversion complete callback */
  1644. /* Note: Into callback function "HAL_ADC_ConvCpltCallback()", */
  1645. /* to determine if conversion has been triggered from EOC or EOS, */
  1646. /* possibility to use: */
  1647. /* " if ( __HAL_ADC_GET_FLAG(&hadc, ADC_FLAG_EOS)) " */
  1648. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  1649. hadc->ConvCpltCallback(hadc);
  1650. #else
  1651. HAL_ADC_ConvCpltCallback(hadc);
  1652. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  1653. /* Clear regular group conversion flag */
  1654. /* Note: in case of overrun set to ADC_OVR_DATA_PRESERVED, end of */
  1655. /* conversion flags clear induces the release of the preserved data.*/
  1656. /* Therefore, if the preserved data value is needed, it must be */
  1657. /* read preliminarily into HAL_ADC_ConvCpltCallback(). */
  1658. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS));
  1659. }
  1660. /* ========== Check Analog watchdog 1 flag ========== */
  1661. if (((tmp_isr & ADC_FLAG_AWD1) == ADC_FLAG_AWD1) && ((tmp_ier & ADC_IT_AWD1) == ADC_IT_AWD1))
  1662. {
  1663. /* Set ADC state */
  1664. SET_BIT(hadc->State, HAL_ADC_STATE_AWD1);
  1665. /* Level out of window 1 callback */
  1666. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  1667. hadc->LevelOutOfWindowCallback(hadc);
  1668. #else
  1669. HAL_ADC_LevelOutOfWindowCallback(hadc);
  1670. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  1671. /* Clear ADC analog watchdog flag */
  1672. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD1);
  1673. }
  1674. /* ========== Check analog watchdog 2 flag ========== */
  1675. if (((tmp_isr & ADC_FLAG_AWD2) == ADC_FLAG_AWD2) && ((tmp_ier & ADC_IT_AWD2) == ADC_IT_AWD2))
  1676. {
  1677. /* Set ADC state */
  1678. SET_BIT(hadc->State, HAL_ADC_STATE_AWD2);
  1679. /* Level out of window 2 callback */
  1680. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  1681. hadc->LevelOutOfWindow2Callback(hadc);
  1682. #else
  1683. HAL_ADCEx_LevelOutOfWindow2Callback(hadc);
  1684. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  1685. /* Clear ADC analog watchdog flag */
  1686. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD2);
  1687. }
  1688. /* ========== Check analog watchdog 3 flag ========== */
  1689. if (((tmp_isr & ADC_FLAG_AWD3) == ADC_FLAG_AWD3) && ((tmp_ier & ADC_IT_AWD3) == ADC_IT_AWD3))
  1690. {
  1691. /* Set ADC state */
  1692. SET_BIT(hadc->State, HAL_ADC_STATE_AWD3);
  1693. /* Level out of window 3 callback */
  1694. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  1695. hadc->LevelOutOfWindow3Callback(hadc);
  1696. #else
  1697. HAL_ADCEx_LevelOutOfWindow3Callback(hadc);
  1698. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  1699. /* Clear ADC analog watchdog flag */
  1700. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD3);
  1701. }
  1702. /* ========== Check Overrun flag ========== */
  1703. if (((tmp_isr & ADC_FLAG_OVR) == ADC_FLAG_OVR) && ((tmp_ier & ADC_IT_OVR) == ADC_IT_OVR))
  1704. {
  1705. /* If overrun is set to overwrite previous data (default setting), */
  1706. /* overrun event is not considered as an error. */
  1707. /* (cf ref manual "Managing conversions without using the DMA and without */
  1708. /* overrun ") */
  1709. /* Exception for usage with DMA overrun event always considered as an */
  1710. /* error. */
  1711. if (hadc->Init.Overrun == ADC_OVR_DATA_PRESERVED)
  1712. {
  1713. overrun_error = 1UL;
  1714. }
  1715. else
  1716. {
  1717. /* Check DMA configuration */
  1718. if (LL_ADC_REG_GetDMATransfer(hadc->Instance) != LL_ADC_REG_DMA_TRANSFER_NONE)
  1719. {
  1720. overrun_error = 1UL;
  1721. }
  1722. }
  1723. if (overrun_error == 1UL)
  1724. {
  1725. /* Change ADC state to error state */
  1726. SET_BIT(hadc->State, HAL_ADC_STATE_REG_OVR);
  1727. /* Set ADC error code to overrun */
  1728. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_OVR);
  1729. /* Error callback */
  1730. /* Note: In case of overrun, ADC conversion data is preserved until */
  1731. /* flag OVR is reset. */
  1732. /* Therefore, old ADC conversion data can be retrieved in */
  1733. /* function "HAL_ADC_ErrorCallback()". */
  1734. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  1735. hadc->ErrorCallback(hadc);
  1736. #else
  1737. HAL_ADC_ErrorCallback(hadc);
  1738. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  1739. }
  1740. /* Clear ADC overrun flag */
  1741. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  1742. }
  1743. /* ========== Check channel configuration ready flag ========== */
  1744. if (((tmp_isr & ADC_FLAG_CCRDY) == ADC_FLAG_CCRDY) && ((tmp_ier & ADC_IT_CCRDY) == ADC_IT_CCRDY))
  1745. {
  1746. /* Channel configuration ready callback */
  1747. HAL_ADCEx_ChannelConfigReadyCallback(hadc);
  1748. /* Clear ADC analog watchdog flag */
  1749. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_CCRDY);
  1750. }
  1751. }
  1752. /**
  1753. * @brief Conversion complete callback in non-blocking mode.
  1754. * @param hadc ADC handle
  1755. * @retval None
  1756. */
  1757. __weak void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
  1758. {
  1759. /* Prevent unused argument(s) compilation warning */
  1760. UNUSED(hadc);
  1761. /* NOTE : This function should not be modified. When the callback is needed,
  1762. function HAL_ADC_ConvCpltCallback must be implemented in the user file.
  1763. */
  1764. }
  1765. /**
  1766. * @brief Conversion DMA half-transfer callback in non-blocking mode.
  1767. * @param hadc ADC handle
  1768. * @retval None
  1769. */
  1770. __weak void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef *hadc)
  1771. {
  1772. /* Prevent unused argument(s) compilation warning */
  1773. UNUSED(hadc);
  1774. /* NOTE : This function should not be modified. When the callback is needed,
  1775. function HAL_ADC_ConvHalfCpltCallback must be implemented in the user file.
  1776. */
  1777. }
  1778. /**
  1779. * @brief Analog watchdog 1 callback in non-blocking mode.
  1780. * @param hadc ADC handle
  1781. * @retval None
  1782. */
  1783. __weak void HAL_ADC_LevelOutOfWindowCallback(ADC_HandleTypeDef *hadc)
  1784. {
  1785. /* Prevent unused argument(s) compilation warning */
  1786. UNUSED(hadc);
  1787. /* NOTE : This function should not be modified. When the callback is needed,
  1788. function HAL_ADC_LevelOutOfWindowCallback must be implemented in the user file.
  1789. */
  1790. }
  1791. /**
  1792. * @brief ADC error callback in non-blocking mode
  1793. * (ADC conversion with interruption or transfer by DMA).
  1794. * @note In case of error due to overrun when using ADC with DMA transfer
  1795. * (HAL ADC handle parameter "ErrorCode" to state "HAL_ADC_ERROR_OVR"):
  1796. * - Reinitialize the DMA using function "HAL_ADC_Stop_DMA()".
  1797. * - If needed, restart a new ADC conversion using function
  1798. * "HAL_ADC_Start_DMA()"
  1799. * (this function is also clearing overrun flag)
  1800. * @param hadc ADC handle
  1801. * @retval None
  1802. */
  1803. __weak void HAL_ADC_ErrorCallback(ADC_HandleTypeDef *hadc)
  1804. {
  1805. /* Prevent unused argument(s) compilation warning */
  1806. UNUSED(hadc);
  1807. /* NOTE : This function should not be modified. When the callback is needed,
  1808. function HAL_ADC_ErrorCallback must be implemented in the user file.
  1809. */
  1810. }
  1811. /**
  1812. * @}
  1813. */
  1814. /** @defgroup ADC_Exported_Functions_Group3 Peripheral Control functions
  1815. * @brief Peripheral Control functions
  1816. *
  1817. @verbatim
  1818. ===============================================================================
  1819. ##### Peripheral Control functions #####
  1820. ===============================================================================
  1821. [..] This section provides functions allowing to:
  1822. (+) Configure channels on regular group
  1823. (+) Configure the analog watchdog
  1824. @endverbatim
  1825. * @{
  1826. */
  1827. /**
  1828. * @brief Configure a channel to be assigned to ADC group regular.
  1829. * @note In case of usage of internal measurement channels:
  1830. * Vbat/VrefInt/TempSensor.
  1831. * These internal paths can be disabled using function
  1832. * HAL_ADC_DeInit().
  1833. * @note Possibility to update parameters on the fly:
  1834. * This function initializes channel into ADC group regular,
  1835. * following calls to this function can be used to reconfigure
  1836. * some parameters of structure "ADC_ChannelConfTypeDef" on the fly,
  1837. * without resetting the ADC.
  1838. * The setting of these parameters is conditioned to ADC state:
  1839. * Refer to comments of structure "ADC_ChannelConfTypeDef".
  1840. * @param hadc ADC handle
  1841. * @param pConfig Structure of ADC channel assigned to ADC group regular.
  1842. * @retval HAL status
  1843. */
  1844. HAL_StatusTypeDef HAL_ADC_ConfigChannel(ADC_HandleTypeDef *hadc, const ADC_ChannelConfTypeDef *pConfig)
  1845. {
  1846. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1847. uint32_t tmp_config_internal_channel;
  1848. __IO uint32_t wait_loop_index = 0UL;
  1849. /* Check the parameters */
  1850. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1851. assert_param(IS_ADC_CHANNEL(pConfig->Channel));
  1852. assert_param(IS_ADC_SAMPLING_TIME_COMMON(pConfig->SamplingTime));
  1853. if ((hadc->Init.ScanConvMode == ADC_SCAN_SEQ_FIXED) ||
  1854. (hadc->Init.ScanConvMode == ADC_SCAN_SEQ_FIXED_BACKWARD))
  1855. {
  1856. assert_param(IS_ADC_REGULAR_RANK_SEQ_FIXED(pConfig->Rank));
  1857. }
  1858. else
  1859. {
  1860. assert_param(IS_ADC_REGULAR_NB_CONV(hadc->Init.NbrOfConversion));
  1861. assert_param(IS_ADC_REGULAR_RANK(pConfig->Rank));
  1862. }
  1863. __HAL_LOCK(hadc);
  1864. /* Parameters update conditioned to ADC state: */
  1865. /* Parameters that can be updated when ADC is disabled or enabled without */
  1866. /* conversion on going on regular group: */
  1867. /* - Channel number */
  1868. /* - Channel sampling time */
  1869. /* - Management of internal measurement channels: VrefInt/TempSensor/Vbat */
  1870. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
  1871. {
  1872. /* Configure channel: depending on rank setting, add it or remove it from */
  1873. /* ADC sequencer. */
  1874. /* If sequencer set to not fully configurable with channel rank set to */
  1875. /* none, remove the channel from the sequencer. */
  1876. /* Otherwise (sequencer set to fully configurable or to to not fully */
  1877. /* configurable with channel rank to be set), configure the selected */
  1878. /* channel. */
  1879. if (pConfig->Rank != ADC_RANK_NONE)
  1880. {
  1881. /* Regular sequence configuration */
  1882. /* Note: ADC channel configuration requires few ADC clock cycles */
  1883. /* to be ready. Processing of ADC settings in this function */
  1884. /* induce that a specific wait time is not necessary. */
  1885. /* For more details on ADC channel configuration ready, */
  1886. /* refer to function "LL_ADC_IsActiveFlag_CCRDY()". */
  1887. if ((hadc->Init.ScanConvMode == ADC_SCAN_SEQ_FIXED) ||
  1888. (hadc->Init.ScanConvMode == ADC_SCAN_SEQ_FIXED_BACKWARD))
  1889. {
  1890. /* Sequencer set to not fully configurable: */
  1891. /* Set the channel by enabling the corresponding bitfield. */
  1892. LL_ADC_REG_SetSequencerChAdd(hadc->Instance, pConfig->Channel);
  1893. }
  1894. else
  1895. {
  1896. /* Sequencer set to fully configurable: */
  1897. /* Set the channel by entering it into the selected rank. */
  1898. /* Memorize the channel set into variable in HAL ADC handle */
  1899. MODIFY_REG(hadc->ADCGroupRegularSequencerRanks,
  1900. ADC_CHSELR_SQ1 << (pConfig->Rank & 0x1FUL),
  1901. __LL_ADC_CHANNEL_TO_DECIMAL_NB(pConfig->Channel) << (pConfig->Rank & 0x1FUL));
  1902. /* If the selected rank is below ADC group regular sequencer length, */
  1903. /* apply the configuration in ADC register. */
  1904. /* Note: Otherwise, configuration is not applied. */
  1905. /* To apply it, parameter'NbrOfConversion' must be increased. */
  1906. if (((pConfig->Rank >> 2UL) + 1UL) <= hadc->Init.NbrOfConversion)
  1907. {
  1908. LL_ADC_REG_SetSequencerRanks(hadc->Instance, pConfig->Rank, pConfig->Channel);
  1909. }
  1910. }
  1911. /* Set sampling time of the selected ADC channel */
  1912. LL_ADC_SetChannelSamplingTime(hadc->Instance, pConfig->Channel, pConfig->SamplingTime);
  1913. /* Management of internal measurement channels: VrefInt/TempSensor/Vbat */
  1914. /* internal measurement paths enable: If internal channel selected, */
  1915. /* enable dedicated internal buffers and path. */
  1916. /* Note: these internal measurement paths can be disabled using */
  1917. /* HAL_ADC_DeInit() or removing the channel from sequencer with */
  1918. /* channel configuration parameter "Rank". */
  1919. if (__LL_ADC_IS_CHANNEL_INTERNAL(pConfig->Channel))
  1920. {
  1921. tmp_config_internal_channel = LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
  1922. /* If the requested internal measurement path has already been enabled, */
  1923. /* bypass the configuration processing. */
  1924. if ((pConfig->Channel == ADC_CHANNEL_TEMPSENSOR) &&
  1925. ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_TEMPSENSOR) == 0UL))
  1926. {
  1927. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance),
  1928. LL_ADC_PATH_INTERNAL_TEMPSENSOR | tmp_config_internal_channel);
  1929. /* Delay for temperature sensor stabilization time */
  1930. /* Wait loop initialization and execution */
  1931. /* Note: Variable divided by 2 to compensate partially */
  1932. /* CPU processing cycles, scaling in us split to not */
  1933. /* exceed 32 bits register capacity and handle low frequency. */
  1934. wait_loop_index = ((LL_ADC_DELAY_TEMPSENSOR_STAB_US / 10UL) * ((SystemCoreClock / (100000UL * 2UL)) + 1UL));
  1935. while (wait_loop_index != 0UL)
  1936. {
  1937. wait_loop_index--;
  1938. }
  1939. }
  1940. else if ((pConfig->Channel == ADC_CHANNEL_VBAT)
  1941. && ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_VBAT) == 0UL))
  1942. {
  1943. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance),
  1944. LL_ADC_PATH_INTERNAL_VBAT | tmp_config_internal_channel);
  1945. }
  1946. else if ((pConfig->Channel == ADC_CHANNEL_VREFINT) &&
  1947. ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_VREFINT) == 0UL))
  1948. {
  1949. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance),
  1950. LL_ADC_PATH_INTERNAL_VREFINT | tmp_config_internal_channel);
  1951. }
  1952. else
  1953. {
  1954. /* nothing to do */
  1955. }
  1956. }
  1957. }
  1958. else
  1959. {
  1960. /* Regular sequencer configuration */
  1961. /* Note: Case of sequencer set to fully configurable: */
  1962. /* Sequencer rank cannot be disabled, only affected to */
  1963. /* another channel. */
  1964. /* To remove a rank, use parameter 'NbrOfConversion". */
  1965. if ((hadc->Init.ScanConvMode == ADC_SCAN_SEQ_FIXED) ||
  1966. (hadc->Init.ScanConvMode == ADC_SCAN_SEQ_FIXED_BACKWARD))
  1967. {
  1968. /* Sequencer set to not fully configurable: */
  1969. /* Reset the channel by disabling the corresponding bitfield. */
  1970. LL_ADC_REG_SetSequencerChRem(hadc->Instance, pConfig->Channel);
  1971. }
  1972. /* Management of internal measurement channels: Vbat/VrefInt/TempSensor. */
  1973. /* If internal channel selected, enable dedicated internal buffers and */
  1974. /* paths. */
  1975. if (__LL_ADC_IS_CHANNEL_INTERNAL(pConfig->Channel))
  1976. {
  1977. tmp_config_internal_channel = LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
  1978. if (pConfig->Channel == ADC_CHANNEL_TEMPSENSOR)
  1979. {
  1980. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance),
  1981. ~LL_ADC_PATH_INTERNAL_TEMPSENSOR & tmp_config_internal_channel);
  1982. }
  1983. else if (pConfig->Channel == ADC_CHANNEL_VBAT)
  1984. {
  1985. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance),
  1986. ~LL_ADC_PATH_INTERNAL_VBAT & tmp_config_internal_channel);
  1987. }
  1988. else if (pConfig->Channel == ADC_CHANNEL_VREFINT)
  1989. {
  1990. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance),
  1991. ~LL_ADC_PATH_INTERNAL_VREFINT & tmp_config_internal_channel);
  1992. }
  1993. else
  1994. {
  1995. /* nothing to do */
  1996. }
  1997. }
  1998. }
  1999. }
  2000. /* If a conversion is on going on regular group, no update on regular */
  2001. /* channel could be done on neither of the channel configuration structure */
  2002. /* parameters. */
  2003. else
  2004. {
  2005. /* Update ADC state machine to error */
  2006. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  2007. tmp_hal_status = HAL_ERROR;
  2008. }
  2009. __HAL_UNLOCK(hadc);
  2010. return tmp_hal_status;
  2011. }
  2012. /**
  2013. * @brief Configure the analog watchdog.
  2014. * @note Possibility to update parameters on the fly:
  2015. * This function initializes the selected analog watchdog, successive
  2016. * calls to this function can be used to reconfigure some parameters
  2017. * of structure "ADC_AnalogWDGConfTypeDef" on the fly, without resetting
  2018. * the ADC.
  2019. * The setting of these parameters is conditioned to ADC state.
  2020. * For parameters constraints, see comments of structure
  2021. * "ADC_AnalogWDGConfTypeDef".
  2022. * @note On this STM32 series, analog watchdog thresholds can be modified
  2023. * while ADC conversion is on going.
  2024. * In this case, some constraints must be taken into account:
  2025. * the programmed threshold values are effective from the next
  2026. * ADC EOC (end of unitary conversion).
  2027. * Considering that registers write delay may happen due to
  2028. * bus activity, this might cause an uncertainty on the
  2029. * effective timing of the new programmed threshold values.
  2030. * @param hadc ADC handle
  2031. * @param pAnalogWDGConfig Structure of ADC analog watchdog configuration
  2032. * @retval HAL status
  2033. */
  2034. HAL_StatusTypeDef HAL_ADC_AnalogWDGConfig(ADC_HandleTypeDef *hadc, const ADC_AnalogWDGConfTypeDef *pAnalogWDGConfig)
  2035. {
  2036. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  2037. uint32_t tmp_awd_high_threshold_shifted;
  2038. uint32_t tmp_awd_low_threshold_shifted;
  2039. uint32_t backup_setting_adc_enable_state = 0UL;
  2040. /* Check the parameters */
  2041. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2042. assert_param(IS_ADC_ANALOG_WATCHDOG_NUMBER(pAnalogWDGConfig->WatchdogNumber));
  2043. assert_param(IS_ADC_ANALOG_WATCHDOG_MODE(pAnalogWDGConfig->WatchdogMode));
  2044. assert_param(IS_FUNCTIONAL_STATE(pAnalogWDGConfig->ITMode));
  2045. if (pAnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_REG)
  2046. {
  2047. assert_param(IS_ADC_CHANNEL(pAnalogWDGConfig->Channel));
  2048. }
  2049. /* Verify thresholds range */
  2050. if (hadc->Init.OversamplingMode == ENABLE)
  2051. {
  2052. /* Case of oversampling enabled: depending on ratio and shift configuration,
  2053. analog watchdog thresholds can be higher than ADC resolution.
  2054. Verify if thresholds are within maximum thresholds range. */
  2055. assert_param(IS_ADC_RANGE(ADC_RESOLUTION_12B, pAnalogWDGConfig->HighThreshold));
  2056. assert_param(IS_ADC_RANGE(ADC_RESOLUTION_12B, pAnalogWDGConfig->LowThreshold));
  2057. }
  2058. else
  2059. {
  2060. /* Verify if thresholds are within the selected ADC resolution */
  2061. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), pAnalogWDGConfig->HighThreshold));
  2062. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), pAnalogWDGConfig->LowThreshold));
  2063. }
  2064. __HAL_LOCK(hadc);
  2065. /* Parameters update conditioned to ADC state: */
  2066. /* Parameters that can be updated when ADC is disabled or enabled without */
  2067. /* conversion on going on ADC group regular: */
  2068. /* - Analog watchdog channels */
  2069. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
  2070. {
  2071. /* Analog watchdog configuration */
  2072. if (pAnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_1)
  2073. {
  2074. /* Constraint of ADC on this STM32 series: ADC must be disable
  2075. to modify bitfields of register ADC_CFGR1 */
  2076. if (LL_ADC_IsEnabled(hadc->Instance) != 0UL)
  2077. {
  2078. backup_setting_adc_enable_state = 1UL;
  2079. tmp_hal_status = ADC_Disable(hadc);
  2080. }
  2081. /* Configuration of analog watchdog: */
  2082. /* - Set the analog watchdog enable mode: one or overall group of */
  2083. /* channels. */
  2084. switch (pAnalogWDGConfig->WatchdogMode)
  2085. {
  2086. case ADC_ANALOGWATCHDOG_SINGLE_REG:
  2087. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1,
  2088. __LL_ADC_ANALOGWD_CHANNEL_GROUP(pAnalogWDGConfig->Channel,
  2089. LL_ADC_GROUP_REGULAR));
  2090. break;
  2091. case ADC_ANALOGWATCHDOG_ALL_REG:
  2092. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, LL_ADC_AWD_ALL_CHANNELS_REG);
  2093. break;
  2094. default: /* ADC_ANALOGWATCHDOG_NONE */
  2095. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, LL_ADC_AWD_DISABLE);
  2096. break;
  2097. }
  2098. if (backup_setting_adc_enable_state == 1UL)
  2099. {
  2100. if (tmp_hal_status == HAL_OK)
  2101. {
  2102. tmp_hal_status = ADC_Enable(hadc);
  2103. }
  2104. }
  2105. /* Update state, clear previous result related to AWD1 */
  2106. CLEAR_BIT(hadc->State, HAL_ADC_STATE_AWD1);
  2107. /* Clear flag ADC analog watchdog */
  2108. /* Note: Flag cleared Clear the ADC Analog watchdog flag to be ready */
  2109. /* to use for HAL_ADC_IRQHandler() or HAL_ADC_PollForEvent() */
  2110. /* (in case left enabled by previous ADC operations). */
  2111. LL_ADC_ClearFlag_AWD1(hadc->Instance);
  2112. /* Configure ADC analog watchdog interrupt */
  2113. if (pAnalogWDGConfig->ITMode == ENABLE)
  2114. {
  2115. LL_ADC_EnableIT_AWD1(hadc->Instance);
  2116. }
  2117. else
  2118. {
  2119. LL_ADC_DisableIT_AWD1(hadc->Instance);
  2120. }
  2121. }
  2122. /* Case of ADC_ANALOGWATCHDOG_2 or ADC_ANALOGWATCHDOG_3 */
  2123. else
  2124. {
  2125. switch (pAnalogWDGConfig->WatchdogMode)
  2126. {
  2127. case ADC_ANALOGWATCHDOG_SINGLE_REG:
  2128. /* Update AWD by bitfield to keep the possibility to monitor */
  2129. /* several channels by successive calls of this function. */
  2130. if (pAnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_2)
  2131. {
  2132. SET_BIT(hadc->Instance->AWD2CR, (1UL << __LL_ADC_CHANNEL_TO_DECIMAL_NB(pAnalogWDGConfig->Channel)));
  2133. }
  2134. else
  2135. {
  2136. SET_BIT(hadc->Instance->AWD3CR, (1UL << __LL_ADC_CHANNEL_TO_DECIMAL_NB(pAnalogWDGConfig->Channel)));
  2137. }
  2138. break;
  2139. case ADC_ANALOGWATCHDOG_ALL_REG:
  2140. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance,
  2141. pAnalogWDGConfig->WatchdogNumber,
  2142. LL_ADC_AWD_ALL_CHANNELS_REG);
  2143. break;
  2144. default: /* ADC_ANALOGWATCHDOG_NONE */
  2145. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, pAnalogWDGConfig->WatchdogNumber, LL_ADC_AWD_DISABLE);
  2146. break;
  2147. }
  2148. if (pAnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_2)
  2149. {
  2150. /* Update state, clear previous result related to AWD2 */
  2151. CLEAR_BIT(hadc->State, HAL_ADC_STATE_AWD2);
  2152. /* Clear flag ADC analog watchdog */
  2153. /* Note: Flag cleared Clear the ADC Analog watchdog flag to be ready */
  2154. /* to use for HAL_ADC_IRQHandler() or HAL_ADC_PollForEvent() */
  2155. /* (in case left enabled by previous ADC operations). */
  2156. LL_ADC_ClearFlag_AWD2(hadc->Instance);
  2157. /* Configure ADC analog watchdog interrupt */
  2158. if (pAnalogWDGConfig->ITMode == ENABLE)
  2159. {
  2160. LL_ADC_EnableIT_AWD2(hadc->Instance);
  2161. }
  2162. else
  2163. {
  2164. LL_ADC_DisableIT_AWD2(hadc->Instance);
  2165. }
  2166. }
  2167. /* (pAnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_3) */
  2168. else
  2169. {
  2170. /* Update state, clear previous result related to AWD3 */
  2171. CLEAR_BIT(hadc->State, HAL_ADC_STATE_AWD3);
  2172. /* Clear flag ADC analog watchdog */
  2173. /* Note: Flag cleared Clear the ADC Analog watchdog flag to be ready */
  2174. /* to use for HAL_ADC_IRQHandler() or HAL_ADC_PollForEvent() */
  2175. /* (in case left enabled by previous ADC operations). */
  2176. LL_ADC_ClearFlag_AWD3(hadc->Instance);
  2177. /* Configure ADC analog watchdog interrupt */
  2178. if (pAnalogWDGConfig->ITMode == ENABLE)
  2179. {
  2180. LL_ADC_EnableIT_AWD3(hadc->Instance);
  2181. }
  2182. else
  2183. {
  2184. LL_ADC_DisableIT_AWD3(hadc->Instance);
  2185. }
  2186. }
  2187. }
  2188. }
  2189. /* Analog watchdog thresholds configuration */
  2190. if (pAnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_1)
  2191. {
  2192. /* Shift the offset with respect to the selected ADC resolution: */
  2193. /* Thresholds have to be left-aligned on bit 11, the LSB (right bits) */
  2194. /* are set to 0. */
  2195. tmp_awd_high_threshold_shifted = ADC_AWD1THRESHOLD_SHIFT_RESOLUTION(hadc, pAnalogWDGConfig->HighThreshold);
  2196. tmp_awd_low_threshold_shifted = ADC_AWD1THRESHOLD_SHIFT_RESOLUTION(hadc, pAnalogWDGConfig->LowThreshold);
  2197. }
  2198. /* Case of ADC_ANALOGWATCHDOG_2 and ADC_ANALOGWATCHDOG_3 */
  2199. else
  2200. {
  2201. /* No need to shift the offset with respect to the selected ADC resolution: */
  2202. /* Thresholds have to be left-aligned on bit 11, the LSB (right bits) */
  2203. /* are set to 0. */
  2204. tmp_awd_high_threshold_shifted = pAnalogWDGConfig->HighThreshold;
  2205. tmp_awd_low_threshold_shifted = pAnalogWDGConfig->LowThreshold;
  2206. }
  2207. /* Set ADC analog watchdog thresholds value of both thresholds high and low */
  2208. LL_ADC_ConfigAnalogWDThresholds(hadc->Instance, pAnalogWDGConfig->WatchdogNumber, tmp_awd_high_threshold_shifted,
  2209. tmp_awd_low_threshold_shifted);
  2210. __HAL_UNLOCK(hadc);
  2211. return tmp_hal_status;
  2212. }
  2213. /**
  2214. * @}
  2215. */
  2216. /** @defgroup ADC_Exported_Functions_Group4 Peripheral State functions
  2217. * @brief ADC Peripheral State functions
  2218. *
  2219. @verbatim
  2220. ===============================================================================
  2221. ##### Peripheral state and errors functions #####
  2222. ===============================================================================
  2223. [..]
  2224. This subsection provides functions to get in run-time the status of the
  2225. peripheral.
  2226. (+) Check the ADC state
  2227. (+) Check the ADC error code
  2228. @endverbatim
  2229. * @{
  2230. */
  2231. /**
  2232. * @brief Return the ADC handle state.
  2233. * @note ADC state machine is managed by bitfields, ADC status must be
  2234. * compared with states bits.
  2235. * For example:
  2236. * " if ((HAL_ADC_GetState(hadc1) & HAL_ADC_STATE_REG_BUSY) != 0UL) "
  2237. * " if ((HAL_ADC_GetState(hadc1) & HAL_ADC_STATE_AWD1) != 0UL) "
  2238. * @param hadc ADC handle
  2239. * @retval ADC handle state (bitfield on 32 bits)
  2240. */
  2241. uint32_t HAL_ADC_GetState(const ADC_HandleTypeDef *hadc)
  2242. {
  2243. /* Check the parameters */
  2244. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2245. /* Return ADC handle state */
  2246. return hadc->State;
  2247. }
  2248. /**
  2249. * @brief Return the ADC error code.
  2250. * @param hadc ADC handle
  2251. * @retval ADC error code (bitfield on 32 bits)
  2252. */
  2253. uint32_t HAL_ADC_GetError(const ADC_HandleTypeDef *hadc)
  2254. {
  2255. /* Check the parameters */
  2256. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2257. return hadc->ErrorCode;
  2258. }
  2259. /**
  2260. * @}
  2261. */
  2262. /**
  2263. * @}
  2264. */
  2265. /** @defgroup ADC_Private_Functions ADC Private Functions
  2266. * @{
  2267. */
  2268. /**
  2269. * @brief Stop ADC conversion.
  2270. * @note Prerequisite condition to use this function: ADC conversions must be
  2271. * stopped to disable the ADC.
  2272. * @param hadc ADC handle
  2273. * @retval HAL status.
  2274. */
  2275. HAL_StatusTypeDef ADC_ConversionStop(ADC_HandleTypeDef *hadc)
  2276. {
  2277. uint32_t tickstart;
  2278. /* Check the parameters */
  2279. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2280. /* Verification if ADC is not already stopped on regular group to bypass */
  2281. /* this function if not needed. */
  2282. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) != 0UL)
  2283. {
  2284. /* Stop potential conversion on going on regular group */
  2285. /* Software is allowed to set ADSTP only when ADSTART=1 and ADDIS=0 */
  2286. if (LL_ADC_IsDisableOngoing(hadc->Instance) == 0UL)
  2287. {
  2288. /* Stop ADC group regular conversion */
  2289. LL_ADC_REG_StopConversion(hadc->Instance);
  2290. }
  2291. /* Wait for conversion effectively stopped */
  2292. /* Get tick count */
  2293. tickstart = HAL_GetTick();
  2294. while ((hadc->Instance->CR & ADC_CR_ADSTART) != 0UL)
  2295. {
  2296. if ((HAL_GetTick() - tickstart) > ADC_STOP_CONVERSION_TIMEOUT)
  2297. {
  2298. /* New check to avoid false timeout detection in case of preemption */
  2299. if ((hadc->Instance->CR & ADC_CR_ADSTART) != 0UL)
  2300. {
  2301. /* Update ADC state machine to error */
  2302. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2303. /* Set ADC error code to ADC peripheral internal error */
  2304. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2305. return HAL_ERROR;
  2306. }
  2307. }
  2308. }
  2309. }
  2310. /* Return HAL status */
  2311. return HAL_OK;
  2312. }
  2313. /**
  2314. * @brief Enable the selected ADC.
  2315. * @note Prerequisite condition to use this function: ADC must be disabled
  2316. * and voltage regulator must be enabled (done into HAL_ADC_Init()).
  2317. * @param hadc ADC handle
  2318. * @retval HAL status.
  2319. */
  2320. HAL_StatusTypeDef ADC_Enable(ADC_HandleTypeDef *hadc)
  2321. {
  2322. uint32_t tickstart;
  2323. __IO uint32_t wait_loop_index = 0UL;
  2324. /* ADC enable and wait for ADC ready (in case of ADC is disabled or */
  2325. /* enabling phase not yet completed: flag ADC ready not yet set). */
  2326. /* Timeout implemented to not be stuck if ADC cannot be enabled (possible */
  2327. /* causes: ADC clock not running, ...). */
  2328. if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
  2329. {
  2330. /* Check if conditions to enable the ADC are fulfilled */
  2331. if ((hadc->Instance->CR & (ADC_CR_ADCAL | ADC_CR_ADSTP | ADC_CR_ADSTART | ADC_CR_ADDIS | ADC_CR_ADEN)) != 0UL)
  2332. {
  2333. /* Update ADC state machine to error */
  2334. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2335. /* Set ADC error code to ADC peripheral internal error */
  2336. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2337. return HAL_ERROR;
  2338. }
  2339. /* Enable the ADC peripheral */
  2340. LL_ADC_Enable(hadc->Instance);
  2341. if ((LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) & LL_ADC_PATH_INTERNAL_TEMPSENSOR)
  2342. != 0UL)
  2343. {
  2344. /* Delay for temperature sensor buffer stabilization time */
  2345. /* Wait loop initialization and execution */
  2346. /* Note: Variable divided by 2 to compensate partially */
  2347. /* CPU processing cycles, scaling in us split to not */
  2348. /* exceed 32 bits register capacity and handle low frequency. */
  2349. wait_loop_index = ((LL_ADC_DELAY_TEMPSENSOR_BUFFER_STAB_US / 10UL)
  2350. * ((SystemCoreClock / (100000UL * 2UL)) + 1UL));
  2351. while (wait_loop_index != 0UL)
  2352. {
  2353. wait_loop_index--;
  2354. }
  2355. }
  2356. /* If low power mode AutoPowerOff is enabled, power-on/off phases are */
  2357. /* performed automatically by hardware and flag ADC ready is not set. */
  2358. if (hadc->Init.LowPowerAutoPowerOff != ENABLE)
  2359. {
  2360. /* Wait for ADC effectively enabled */
  2361. tickstart = HAL_GetTick();
  2362. while (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_RDY) == 0UL)
  2363. {
  2364. /* If ADEN bit is set less than 4 ADC clock cycles after the ADCAL bit
  2365. has been cleared (after a calibration), ADEN bit is reset by the
  2366. calibration logic.
  2367. The workaround is to continue setting ADEN until ADRDY is becomes 1.
  2368. Additionally, ADC_ENABLE_TIMEOUT is defined to encompass this
  2369. 4 ADC clock cycle duration */
  2370. /* Note: Test of ADC enabled required due to hardware constraint to */
  2371. /* not enable ADC if already enabled. */
  2372. if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
  2373. {
  2374. LL_ADC_Enable(hadc->Instance);
  2375. }
  2376. if ((HAL_GetTick() - tickstart) > ADC_ENABLE_TIMEOUT)
  2377. {
  2378. /* New check to avoid false timeout detection in case of preemption */
  2379. if (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_RDY) == 0UL)
  2380. {
  2381. /* Update ADC state machine to error */
  2382. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2383. /* Set ADC error code to ADC peripheral internal error */
  2384. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2385. return HAL_ERROR;
  2386. }
  2387. }
  2388. }
  2389. }
  2390. }
  2391. /* Return HAL status */
  2392. return HAL_OK;
  2393. }
  2394. /**
  2395. * @brief Disable the selected ADC.
  2396. * @note Prerequisite condition to use this function: ADC conversions must be
  2397. * stopped.
  2398. * @param hadc ADC handle
  2399. * @retval HAL status.
  2400. */
  2401. HAL_StatusTypeDef ADC_Disable(ADC_HandleTypeDef *hadc)
  2402. {
  2403. uint32_t tickstart;
  2404. const uint32_t tmp_adc_is_disable_on_going = LL_ADC_IsDisableOngoing(hadc->Instance);
  2405. /* Verification if ADC is not already disabled: */
  2406. /* Note: forbidden to disable ADC (set bit ADC_CR_ADDIS) if ADC is already */
  2407. /* disabled. */
  2408. if ((LL_ADC_IsEnabled(hadc->Instance) != 0UL)
  2409. && (tmp_adc_is_disable_on_going == 0UL)
  2410. )
  2411. {
  2412. /* Check if conditions to disable the ADC are fulfilled */
  2413. if ((hadc->Instance->CR & (ADC_CR_ADSTART | ADC_CR_ADEN)) == ADC_CR_ADEN)
  2414. {
  2415. /* Disable the ADC peripheral */
  2416. LL_ADC_Disable(hadc->Instance);
  2417. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOSMP | ADC_FLAG_RDY));
  2418. }
  2419. else
  2420. {
  2421. /* Update ADC state machine to error */
  2422. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2423. /* Set ADC error code to ADC peripheral internal error */
  2424. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2425. return HAL_ERROR;
  2426. }
  2427. /* Wait for ADC effectively disabled */
  2428. /* Get tick count */
  2429. tickstart = HAL_GetTick();
  2430. while ((hadc->Instance->CR & ADC_CR_ADEN) != 0UL)
  2431. {
  2432. if ((HAL_GetTick() - tickstart) > ADC_DISABLE_TIMEOUT)
  2433. {
  2434. /* New check to avoid false timeout detection in case of preemption */
  2435. if ((hadc->Instance->CR & ADC_CR_ADEN) != 0UL)
  2436. {
  2437. /* Update ADC state machine to error */
  2438. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2439. /* Set ADC error code to ADC peripheral internal error */
  2440. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2441. return HAL_ERROR;
  2442. }
  2443. }
  2444. }
  2445. }
  2446. /* Return HAL status */
  2447. return HAL_OK;
  2448. }
  2449. /**
  2450. * @brief DMA transfer complete callback.
  2451. * @param hdma pointer to DMA handle.
  2452. * @retval None
  2453. */
  2454. static void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma)
  2455. {
  2456. /* Retrieve ADC handle corresponding to current DMA handle */
  2457. ADC_HandleTypeDef *hadc = (ADC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2458. /* Update state machine on conversion status if not in error state */
  2459. if ((hadc->State & (HAL_ADC_STATE_ERROR_INTERNAL | HAL_ADC_STATE_ERROR_DMA)) == 0UL)
  2460. {
  2461. /* Set ADC state */
  2462. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  2463. /* Determine whether any further conversion upcoming on group regular */
  2464. /* by external trigger, continuous mode or scan sequence on going */
  2465. /* to disable interruption. */
  2466. if ((LL_ADC_REG_IsTriggerSourceSWStart(hadc->Instance) != 0UL)
  2467. && (hadc->Init.ContinuousConvMode == DISABLE)
  2468. )
  2469. {
  2470. /* If End of Sequence is reached, disable interrupts */
  2471. if (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOS))
  2472. {
  2473. /* Allowed to modify bits ADC_IT_EOC/ADC_IT_EOS only if bit */
  2474. /* ADSTART==0 (no conversion on going) */
  2475. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
  2476. {
  2477. /* Disable ADC end of single conversion interrupt on group regular */
  2478. /* Note: Overrun interrupt was enabled with EOC interrupt in */
  2479. /* HAL_Start_IT(), but is not disabled here because can be used */
  2480. /* by overrun IRQ process below. */
  2481. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC | ADC_IT_EOS);
  2482. /* Set ADC state */
  2483. ADC_STATE_CLR_SET(hadc->State,
  2484. HAL_ADC_STATE_REG_BUSY,
  2485. HAL_ADC_STATE_READY);
  2486. }
  2487. else
  2488. {
  2489. /* Change ADC state to error state */
  2490. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  2491. /* Set ADC error code to ADC peripheral internal error */
  2492. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2493. }
  2494. }
  2495. }
  2496. /* Conversion complete callback */
  2497. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2498. hadc->ConvCpltCallback(hadc);
  2499. #else
  2500. HAL_ADC_ConvCpltCallback(hadc);
  2501. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2502. }
  2503. else /* DMA and-or internal error occurred */
  2504. {
  2505. if ((hadc->State & HAL_ADC_STATE_ERROR_INTERNAL) != 0UL)
  2506. {
  2507. /* Call HAL ADC Error Callback function */
  2508. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2509. hadc->ErrorCallback(hadc);
  2510. #else
  2511. HAL_ADC_ErrorCallback(hadc);
  2512. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2513. }
  2514. else
  2515. {
  2516. /* Call ADC DMA error callback */
  2517. hadc->DMA_Handle->XferErrorCallback(hdma);
  2518. }
  2519. }
  2520. }
  2521. /**
  2522. * @brief DMA half transfer complete callback.
  2523. * @param hdma pointer to DMA handle.
  2524. * @retval None
  2525. */
  2526. static void ADC_DMAHalfConvCplt(DMA_HandleTypeDef *hdma)
  2527. {
  2528. /* Retrieve ADC handle corresponding to current DMA handle */
  2529. ADC_HandleTypeDef *hadc = (ADC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2530. /* Half conversion callback */
  2531. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2532. hadc->ConvHalfCpltCallback(hadc);
  2533. #else
  2534. HAL_ADC_ConvHalfCpltCallback(hadc);
  2535. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2536. }
  2537. /**
  2538. * @brief DMA error callback.
  2539. * @param hdma pointer to DMA handle.
  2540. * @retval None
  2541. */
  2542. static void ADC_DMAError(DMA_HandleTypeDef *hdma)
  2543. {
  2544. /* Retrieve ADC handle corresponding to current DMA handle */
  2545. ADC_HandleTypeDef *hadc = (ADC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  2546. /* Set ADC state */
  2547. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  2548. /* Set ADC error code to DMA error */
  2549. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_DMA);
  2550. /* Error callback */
  2551. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2552. hadc->ErrorCallback(hadc);
  2553. #else
  2554. HAL_ADC_ErrorCallback(hadc);
  2555. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2556. }
  2557. /**
  2558. * @}
  2559. */
  2560. #endif /* HAL_ADC_MODULE_ENABLED */
  2561. /**
  2562. * @}
  2563. */
  2564. /**
  2565. * @}
  2566. */