stm32h5xx_hal_adc.c 146 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774
  1. /**
  2. ******************************************************************************
  3. * @file stm32h5xx_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. * "stm32h5xx_hal_adc_ex.c".
  13. *
  14. ******************************************************************************
  15. * @attention
  16. *
  17. * Copyright (c) 2023 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 (channel wise)
  37. (+) External trigger (timer or EXTI) with configurable polarity
  38. (+) DMA request generation for transfer of conversions data of regular group.
  39. (+) Configurable delay between conversions in Dual interleaved mode.
  40. (+) ADC channels selectable single/differential input.
  41. (+) ADC offset shared on 4 offset instances.
  42. (+) ADC calibration
  43. (+) ADC conversion of regular group.
  44. (+) ADC supply requirements: 1.62 V to 3.6 V.
  45. (+) ADC input range: from Vref- (connected to Vssa) to Vref+ (connected to
  46. Vdda or to an external voltage reference).
  47. ##### How to use this driver #####
  48. ==============================================================================
  49. [..]
  50. *** Configuration of top level parameters related to ADC ***
  51. ============================================================
  52. [..]
  53. (#) Enable the ADC interface
  54. (++) As prerequisite, ADC clock must be configured at RCC top level.
  55. (++) Two clock settings are mandatory:
  56. (+++) ADC clock (core clock, also possibly conversion clock).
  57. (+++) ADC clock (conversions clock).
  58. Two possible clock sources: synchronous clock derived from AHB clock
  59. or asynchronous clock derived from system clock or PLL.
  60. (+++) Example:
  61. Into HAL_ADC_MspInit() (recommended code location) or with
  62. other device clock parameters configuration:
  63. (+++) __HAL_RCC_ADC_CLK_ENABLE(); (mandatory)
  64. RCC_ADCCLKSOURCE_PLL enable: (optional: if asynchronous clock selected)
  65. (+++) RCC_PeriphClkInitTypeDef RCC_PeriphClkInit;
  66. (+++) PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  67. (+++) PeriphClkInit.AdcClockSelection = RCC_ADCCLKSOURCE_PLL;
  68. (+++) HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
  69. (++) ADC clock source and clock prescaler are configured at ADC level with
  70. parameter "ClockPrescaler" using function HAL_ADC_Init().
  71. (#) ADC pins configuration
  72. (++) Enable the clock for the ADC GPIOs
  73. using macro __HAL_RCC_GPIOx_CLK_ENABLE()
  74. (++) Configure these ADC pins in analog mode
  75. using function HAL_GPIO_Init()
  76. (#) Optionally, in case of usage of ADC with interruptions:
  77. (++) Configure the NVIC for ADC
  78. using function HAL_NVIC_EnableIRQ(ADCx_IRQn)
  79. (++) Insert the ADC interruption handler function HAL_ADC_IRQHandler()
  80. into the function of corresponding ADC interruption vector
  81. ADCx_IRQHandler().
  82. (#) Optionally, in case of usage of DMA:
  83. (++) Configure the DMA (DMA channel, mode normal or circular, ...)
  84. using function HAL_DMA_Init().
  85. (++) Configure the NVIC for DMA
  86. using function HAL_NVIC_EnableIRQ(DMAx_Channelx_IRQn)
  87. (++) Insert the ADC interruption handler function HAL_ADC_IRQHandler()
  88. into the function of corresponding DMA interruption vector
  89. DMAx_Channelx_IRQHandler().
  90. *** Configuration of ADC, group regular, channels parameters ***
  91. ================================================================
  92. [..]
  93. (#) Configure the ADC parameters (resolution, data alignment, ...)
  94. and regular group parameters (conversion trigger, sequencer, ...)
  95. using function HAL_ADC_Init().
  96. (#) Configure the channels for regular group parameters (channel number,
  97. channel rank into sequencer, ..., into regular group)
  98. using function HAL_ADC_ConfigChannel().
  99. (#) Optionally, configure the analog watchdog parameters (channels
  100. monitored, thresholds, ...)
  101. using function HAL_ADC_AnalogWDGConfig().
  102. *** Execution of ADC conversions ***
  103. ====================================
  104. [..]
  105. (#) Optionally, perform an automatic ADC calibration to improve the
  106. conversion accuracy
  107. using function HAL_ADCEx_Calibration_Start().
  108. (#) ADC driver can be used among three modes: polling, interruption,
  109. transfer by DMA.
  110. (++) ADC conversion by polling:
  111. (+++) Activate the ADC peripheral and start conversions
  112. using function HAL_ADC_Start()
  113. (+++) Wait for ADC conversion completion
  114. using function HAL_ADC_PollForConversion()
  115. (+++) Retrieve conversion results
  116. using function HAL_ADC_GetValue()
  117. (+++) Stop conversion and disable the ADC peripheral
  118. using function HAL_ADC_Stop()
  119. (++) ADC conversion by interruption:
  120. (+++) Activate the ADC peripheral and start conversions
  121. using function HAL_ADC_Start_IT()
  122. (+++) Wait for ADC conversion completion by call of function
  123. HAL_ADC_ConvCpltCallback()
  124. (this function must be implemented in user program)
  125. (+++) Retrieve conversion results
  126. using function HAL_ADC_GetValue()
  127. (+++) Stop conversion and disable the ADC peripheral
  128. using function HAL_ADC_Stop_IT()
  129. (++) ADC conversion with transfer by DMA:
  130. (+++) Activate the ADC peripheral and start conversions
  131. using function HAL_ADC_Start_DMA()
  132. (+++) Wait for ADC conversion completion by call of function
  133. HAL_ADC_ConvCpltCallback() or HAL_ADC_ConvHalfCpltCallback()
  134. (these functions must be implemented in user program)
  135. (+++) Conversion results are automatically transferred by DMA into
  136. destination variable address.
  137. (+++) Stop conversion and disable the ADC peripheral
  138. using function HAL_ADC_Stop_DMA()
  139. [..]
  140. (@) Callback functions must be implemented in user program:
  141. (+@) HAL_ADC_ErrorCallback()
  142. (+@) HAL_ADC_LevelOutOfWindowCallback() (callback of analog watchdog)
  143. (+@) HAL_ADC_ConvCpltCallback()
  144. (+@) HAL_ADC_ConvHalfCpltCallback
  145. *** Deinitialization of ADC ***
  146. ============================================================
  147. [..]
  148. (#) Disable the ADC interface
  149. (++) ADC clock can be hard reset and disabled at RCC top level.
  150. (++) Hard reset of ADC peripherals
  151. using macro __ADCx_FORCE_RESET(), __ADCx_RELEASE_RESET().
  152. (++) ADC clock disable
  153. using the equivalent macro/functions as configuration step.
  154. (+++) Example:
  155. Into HAL_ADC_MspDeInit() (recommended code location) or with
  156. other device clock parameters configuration:
  157. (+++) RCC_OscInitStructure.OscillatorType = RCC_OSCILLATORTYPE_HSI14;
  158. (+++) RCC_OscInitStructure.HSI14State = RCC_HSI14_OFF; (if not used for system clock)
  159. (+++) HAL_RCC_OscConfig(&RCC_OscInitStructure);
  160. (#) ADC pins configuration
  161. (++) Disable the clock for the ADC GPIOs
  162. using macro __HAL_RCC_GPIOx_CLK_DISABLE()
  163. (#) Optionally, in case of usage of ADC with interruptions:
  164. (++) Disable the NVIC for ADC
  165. using function HAL_NVIC_EnableIRQ(ADCx_IRQn)
  166. (#) Optionally, in case of usage of DMA:
  167. (++) Deinitialize the DMA
  168. using function HAL_DMA_Init().
  169. (++) Disable the NVIC for DMA
  170. using function HAL_NVIC_EnableIRQ(DMAx_Channelx_IRQn)
  171. [..]
  172. *** Callback registration ***
  173. =============================================
  174. [..]
  175. The compilation flag USE_HAL_ADC_REGISTER_CALLBACKS, when set to 1,
  176. allows the user to configure dynamically the driver callbacks.
  177. Use Functions @ref HAL_ADC_RegisterCallback()
  178. to register an interrupt callback.
  179. [..]
  180. Function @ref HAL_ADC_RegisterCallback() allows to register following callbacks:
  181. (+) ConvCpltCallback : ADC conversion complete callback
  182. (+) ConvHalfCpltCallback : ADC conversion DMA half-transfer callback
  183. (+) LevelOutOfWindowCallback : ADC analog watchdog 1 callback
  184. (+) ErrorCallback : ADC error callback
  185. (+) InjectedConvCpltCallback : ADC group injected conversion complete callback
  186. (+) InjectedQueueOverflowCallback : ADC group injected context queue overflow callback
  187. (+) LevelOutOfWindow2Callback : ADC analog watchdog 2 callback
  188. (+) LevelOutOfWindow3Callback : ADC analog watchdog 3 callback
  189. (+) EndOfSamplingCallback : ADC end of sampling callback
  190. (+) MspInitCallback : ADC Msp Init callback
  191. (+) MspDeInitCallback : ADC Msp DeInit callback
  192. This function takes as parameters the HAL peripheral handle, the Callback ID
  193. and a pointer to the user callback function.
  194. [..]
  195. Use function @ref HAL_ADC_UnRegisterCallback to reset a callback to the default
  196. weak function.
  197. [..]
  198. @ref HAL_ADC_UnRegisterCallback takes as parameters the HAL peripheral handle,
  199. and the Callback ID.
  200. This function allows to reset following callbacks:
  201. (+) ConvCpltCallback : ADC conversion complete callback
  202. (+) ConvHalfCpltCallback : ADC conversion DMA half-transfer callback
  203. (+) LevelOutOfWindowCallback : ADC analog watchdog 1 callback
  204. (+) ErrorCallback : ADC error callback
  205. (+) InjectedConvCpltCallback : ADC group injected conversion complete callback
  206. (+) InjectedQueueOverflowCallback : ADC group injected context queue overflow callback
  207. (+) LevelOutOfWindow2Callback : ADC analog watchdog 2 callback
  208. (+) LevelOutOfWindow3Callback : ADC analog watchdog 3 callback
  209. (+) EndOfSamplingCallback : ADC end of sampling callback
  210. (+) MspInitCallback : ADC Msp Init callback
  211. (+) MspDeInitCallback : ADC Msp DeInit callback
  212. [..]
  213. By default, after the @ref HAL_ADC_Init() and when the state is @ref HAL_ADC_STATE_RESET
  214. all callbacks are set to the corresponding weak functions:
  215. examples @ref HAL_ADC_ConvCpltCallback(), @ref HAL_ADC_ErrorCallback().
  216. Exception done for MspInit and MspDeInit functions that are
  217. reset to the legacy weak functions in the @ref HAL_ADC_Init()/ @ref HAL_ADC_DeInit() only when
  218. these callbacks are null (not registered beforehand).
  219. [..]
  220. If MspInit or MspDeInit are not null, the @ref HAL_ADC_Init()/ @ref HAL_ADC_DeInit()
  221. keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
  222. [..]
  223. Callbacks can be registered/unregistered in @ref HAL_ADC_STATE_READY state only.
  224. Exception done MspInit/MspDeInit functions that can be registered/unregistered
  225. in @ref HAL_ADC_STATE_READY or @ref HAL_ADC_STATE_RESET state,
  226. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  227. [..]
  228. Then, the user first registers the MspInit/MspDeInit user callbacks
  229. using @ref HAL_ADC_RegisterCallback() before calling @ref HAL_ADC_DeInit()
  230. or @ref HAL_ADC_Init() function.
  231. [..]
  232. When the compilation flag USE_HAL_ADC_REGISTER_CALLBACKS is set to 0 or
  233. not defined, the callback registration feature is not available and all callbacks
  234. are set to the corresponding weak functions.
  235. @endverbatim
  236. ******************************************************************************
  237. */
  238. /* Includes ------------------------------------------------------------------*/
  239. #include "stm32h5xx_hal.h"
  240. /** @addtogroup STM32H5xx_HAL_Driver
  241. * @{
  242. */
  243. /** @defgroup ADC ADC
  244. * @brief ADC HAL module driver
  245. * @{
  246. */
  247. #ifdef HAL_ADC_MODULE_ENABLED
  248. /* Private typedef -----------------------------------------------------------*/
  249. /* Private define ------------------------------------------------------------*/
  250. /** @defgroup ADC_Private_Constants ADC Private Constants
  251. * @{
  252. */
  253. #define ADC_CFGR_FIELDS_1 (ADC_CFGR_RES | ADC_CFGR_ALIGN |\
  254. ADC_CFGR_CONT | ADC_CFGR_OVRMOD |\
  255. ADC_CFGR_DISCEN | ADC_CFGR_DISCNUM |\
  256. ADC_CFGR_EXTEN | ADC_CFGR_EXTSEL) /*!< ADC_CFGR fields of parameters that can
  257. be updated when no regular conversion is on-going */
  258. /* Timeout values for ADC operations (enable settling time, */
  259. /* disable settling time, ...). */
  260. /* Values defined to be higher than worst cases: low clock frequency, */
  261. /* maximum prescalers. */
  262. #define ADC_ENABLE_TIMEOUT (2UL) /*!< ADC enable time-out value */
  263. #define ADC_DISABLE_TIMEOUT (2UL) /*!< ADC disable time-out value */
  264. /* Timeout to wait for current conversion on going to be completed. */
  265. /* Timeout fixed to longest ADC conversion possible, for 1 channel: */
  266. /* - maximum sampling time (640.5 adc_clk) */
  267. /* - ADC resolution (Tsar 12 bits= 12.5 adc_clk) */
  268. /* - System clock / ADC clock <= 4096 (hypothesis of maximum clock ratio) */
  269. /* - ADC oversampling ratio 256 */
  270. /* Calculation: 653 * 4096 * 256 CPU clock cycles max */
  271. /* Unit: cycles of CPU clock. */
  272. #define ADC_CONVERSION_TIME_MAX_CPU_CYCLES (653UL * 4096UL * 256UL) /*!< ADC conversion completion time-out value */
  273. /**
  274. * @}
  275. */
  276. /* Private macro -------------------------------------------------------------*/
  277. /* Private variables ---------------------------------------------------------*/
  278. /* Private function prototypes -----------------------------------------------*/
  279. /* Exported functions --------------------------------------------------------*/
  280. /** @defgroup ADC_Exported_Functions ADC Exported Functions
  281. * @{
  282. */
  283. /** @defgroup ADC_Exported_Functions_Group1 Initialization and de-initialization functions
  284. * @brief ADC Initialization and Configuration functions
  285. *
  286. @verbatim
  287. ===============================================================================
  288. ##### Initialization and de-initialization functions #####
  289. ===============================================================================
  290. [..] This section provides functions allowing to:
  291. (+) Initialize and configure the ADC.
  292. (+) De-initialize the ADC.
  293. @endverbatim
  294. * @{
  295. */
  296. /**
  297. * @brief Initialize the ADC peripheral and regular group according to
  298. * parameters specified in structure "ADC_InitTypeDef".
  299. * @note As prerequisite, ADC clock must be configured at RCC top level
  300. * (refer to description of RCC configuration for ADC
  301. * in header of this file).
  302. * @note Possibility to update parameters on the fly:
  303. * This function initializes the ADC MSP (HAL_ADC_MspInit()) only when
  304. * coming from ADC state reset. Following calls to this function can
  305. * be used to reconfigure some parameters of ADC_InitTypeDef
  306. * structure on the fly, without modifying MSP configuration. If ADC
  307. * MSP has to be modified again, HAL_ADC_DeInit() must be called
  308. * before HAL_ADC_Init().
  309. * The setting of these parameters is conditioned to ADC state.
  310. * For parameters constraints, see comments of structure
  311. * "ADC_InitTypeDef".
  312. * @note This function configures the ADC within 2 scopes: scope of entire
  313. * ADC and scope of regular group. For parameters details, see comments
  314. * of structure "ADC_InitTypeDef".
  315. * @note Parameters related to common ADC registers (ADC clock mode) are set
  316. * only if all ADCs are disabled.
  317. * If this is not the case, these common parameters setting are
  318. * bypassed without error reporting: it can be the intended behaviour in
  319. * case of update of a parameter of ADC_InitTypeDef on the fly,
  320. * without disabling the other ADCs.
  321. * @param hadc ADC handle
  322. * @retval HAL status
  323. */
  324. HAL_StatusTypeDef HAL_ADC_Init(ADC_HandleTypeDef *hadc)
  325. {
  326. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  327. uint32_t tmp_cfgr;
  328. uint32_t tmp_adc_is_conversion_on_going_regular;
  329. uint32_t tmp_adc_is_conversion_on_going_injected;
  330. __IO uint32_t wait_loop_index = 0UL;
  331. /* Check ADC handle */
  332. if (hadc == NULL)
  333. {
  334. return HAL_ERROR;
  335. }
  336. /* Check the parameters */
  337. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  338. assert_param(IS_ADC_CLOCKPRESCALER(hadc->Init.ClockPrescaler));
  339. assert_param(IS_ADC_RESOLUTION(hadc->Init.Resolution));
  340. assert_param(IS_ADC_DATA_ALIGN(hadc->Init.DataAlign));
  341. assert_param(IS_ADC_SCAN_MODE(hadc->Init.ScanConvMode));
  342. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
  343. assert_param(IS_ADC_EXTTRIG_EDGE(hadc->Init.ExternalTrigConvEdge));
  344. assert_param(IS_ADC_EXTTRIG(hadc->Init.ExternalTrigConv));
  345. assert_param(IS_ADC_SAMPLINGMODE(hadc->Init.SamplingMode));
  346. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DMAContinuousRequests));
  347. assert_param(IS_ADC_EOC_SELECTION(hadc->Init.EOCSelection));
  348. assert_param(IS_ADC_OVERRUN(hadc->Init.Overrun));
  349. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.LowPowerAutoWait));
  350. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.OversamplingMode));
  351. if (hadc->Init.ScanConvMode != ADC_SCAN_DISABLE)
  352. {
  353. assert_param(IS_ADC_REGULAR_NB_CONV(hadc->Init.NbrOfConversion));
  354. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DiscontinuousConvMode));
  355. if (hadc->Init.DiscontinuousConvMode == ENABLE)
  356. {
  357. assert_param(IS_ADC_REGULAR_DISCONT_NUMBER(hadc->Init.NbrOfDiscConversion));
  358. }
  359. }
  360. /* DISCEN and CONT bits cannot be set at the same time */
  361. assert_param(!((hadc->Init.DiscontinuousConvMode == ENABLE) && (hadc->Init.ContinuousConvMode == ENABLE)));
  362. /* Actions performed only if ADC is coming from state reset: */
  363. /* - Initialization of ADC MSP */
  364. if (hadc->State == HAL_ADC_STATE_RESET)
  365. {
  366. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  367. /* Init the ADC Callback settings */
  368. hadc->ConvCpltCallback = HAL_ADC_ConvCpltCallback; /* Legacy weak callback */
  369. hadc->ConvHalfCpltCallback = HAL_ADC_ConvHalfCpltCallback; /* Legacy weak callback */
  370. hadc->LevelOutOfWindowCallback = HAL_ADC_LevelOutOfWindowCallback; /* Legacy weak callback */
  371. hadc->ErrorCallback = HAL_ADC_ErrorCallback; /* Legacy weak callback */
  372. hadc->InjectedConvCpltCallback = HAL_ADCEx_InjectedConvCpltCallback; /* Legacy weak callback */
  373. hadc->InjectedQueueOverflowCallback = HAL_ADCEx_InjectedQueueOverflowCallback; /* Legacy weak callback */
  374. hadc->LevelOutOfWindow2Callback = HAL_ADCEx_LevelOutOfWindow2Callback; /* Legacy weak callback */
  375. hadc->LevelOutOfWindow3Callback = HAL_ADCEx_LevelOutOfWindow3Callback; /* Legacy weak callback */
  376. hadc->EndOfSamplingCallback = HAL_ADCEx_EndOfSamplingCallback; /* Legacy weak callback */
  377. if (hadc->MspInitCallback == NULL)
  378. {
  379. hadc->MspInitCallback = HAL_ADC_MspInit; /* Legacy weak MspInit */
  380. }
  381. /* Init the low level hardware */
  382. hadc->MspInitCallback(hadc);
  383. #else
  384. /* Init the low level hardware */
  385. HAL_ADC_MspInit(hadc);
  386. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  387. /* Set ADC error code to none */
  388. ADC_CLEAR_ERRORCODE(hadc);
  389. /* Initialize Lock */
  390. hadc->Lock = HAL_UNLOCKED;
  391. }
  392. /* - Exit from deep-power-down mode and ADC voltage regulator enable */
  393. if (LL_ADC_IsDeepPowerDownEnabled(hadc->Instance) != 0UL)
  394. {
  395. /* Disable ADC deep power down mode */
  396. LL_ADC_DisableDeepPowerDown(hadc->Instance);
  397. /* System was in deep power down mode, calibration must
  398. be relaunched or a previously saved calibration factor
  399. re-applied once the ADC voltage regulator is enabled */
  400. }
  401. if (LL_ADC_IsInternalRegulatorEnabled(hadc->Instance) == 0UL)
  402. {
  403. /* Enable ADC internal voltage regulator */
  404. LL_ADC_EnableInternalRegulator(hadc->Instance);
  405. /* Note: Variable divided by 2 to compensate partially */
  406. /* CPU processing cycles, scaling in us split to not */
  407. /* exceed 32 bits register capacity and handle low frequency. */
  408. wait_loop_index = ((LL_ADC_DELAY_INTERNAL_REGUL_STAB_US / 10UL) * ((SystemCoreClock / (100000UL * 2UL)) + 1UL));
  409. while (wait_loop_index != 0UL)
  410. {
  411. wait_loop_index--;
  412. }
  413. }
  414. /* Verification that ADC voltage regulator is correctly enabled, whether */
  415. /* or not ADC is coming from state reset (if any potential problem of */
  416. /* clocking, voltage regulator would not be enabled). */
  417. if (LL_ADC_IsInternalRegulatorEnabled(hadc->Instance) == 0UL)
  418. {
  419. /* Update ADC state machine to error */
  420. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  421. /* Set ADC error code to ADC peripheral internal error */
  422. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  423. tmp_hal_status = HAL_ERROR;
  424. }
  425. /* Configuration of ADC parameters if previous preliminary actions are */
  426. /* correctly completed and if there is no conversion on going on regular */
  427. /* group (ADC may already be enabled at this point if HAL_ADC_Init() is */
  428. /* called to update a parameter on the fly). */
  429. tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
  430. if (((hadc->State & HAL_ADC_STATE_ERROR_INTERNAL) == 0UL)
  431. && (tmp_adc_is_conversion_on_going_regular == 0UL)
  432. )
  433. {
  434. /* Set ADC state */
  435. ADC_STATE_CLR_SET(hadc->State,
  436. HAL_ADC_STATE_REG_BUSY,
  437. HAL_ADC_STATE_BUSY_INTERNAL);
  438. /* Configuration of common ADC parameters */
  439. /* Parameters update conditioned to ADC state: */
  440. /* Parameters that can be updated only when ADC is disabled: */
  441. /* - clock configuration */
  442. if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
  443. {
  444. if (__LL_ADC_IS_ENABLED_ALL_COMMON_INSTANCE(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) == 0UL)
  445. {
  446. /* Reset configuration of ADC common register CCR: */
  447. /* */
  448. /* - ADC clock mode and ACC prescaler (CKMODE and PRESC bits)are set */
  449. /* according to adc->Init.ClockPrescaler. It selects the clock */
  450. /* source and sets the clock division factor. */
  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. /* - when multimode feature is available, multimode-related */
  458. /* parameters: MDMA, DMACFG, DELAY, DUAL (set by API */
  459. /* HAL_ADCEx_MultiModeConfigChannel() ) */
  460. /* - internal measurement paths: Vbat, temperature sensor, Vref */
  461. /* (set into HAL_ADC_ConfigChannel() or */
  462. /* HAL_ADCEx_InjectedConfigChannel() ) */
  463. LL_ADC_SetCommonClock(__LL_ADC_COMMON_INSTANCE(hadc->Instance), hadc->Init.ClockPrescaler);
  464. }
  465. }
  466. /* Configuration of ADC: */
  467. /* - resolution Init.Resolution */
  468. /* - data alignment Init.DataAlign */
  469. /* - external trigger to start conversion Init.ExternalTrigConv */
  470. /* - external trigger polarity Init.ExternalTrigConvEdge */
  471. /* - continuous conversion mode Init.ContinuousConvMode */
  472. /* - overrun Init.Overrun */
  473. /* - discontinuous mode Init.DiscontinuousConvMode */
  474. /* - discontinuous mode channel count Init.NbrOfDiscConversion */
  475. tmp_cfgr = (ADC_CFGR_CONTINUOUS((uint32_t)hadc->Init.ContinuousConvMode) |
  476. hadc->Init.Overrun |
  477. hadc->Init.DataAlign |
  478. hadc->Init.Resolution |
  479. ADC_CFGR_REG_DISCONTINUOUS((uint32_t)hadc->Init.DiscontinuousConvMode));
  480. if (hadc->Init.DiscontinuousConvMode == ENABLE)
  481. {
  482. tmp_cfgr |= ADC_CFGR_DISCONTINUOUS_NUM(hadc->Init.NbrOfDiscConversion);
  483. }
  484. /* Enable external trigger if trigger selection is different of software */
  485. /* start. */
  486. /* Note: This configuration keeps the hardware feature of parameter */
  487. /* ExternalTrigConvEdge "trigger edge none" equivalent to */
  488. /* software start. */
  489. if (hadc->Init.ExternalTrigConv != ADC_SOFTWARE_START)
  490. {
  491. tmp_cfgr |= ((hadc->Init.ExternalTrigConv & ADC_CFGR_EXTSEL)
  492. | hadc->Init.ExternalTrigConvEdge
  493. );
  494. }
  495. /* Update Configuration Register CFGR */
  496. MODIFY_REG(hadc->Instance->CFGR, ADC_CFGR_FIELDS_1, tmp_cfgr);
  497. /* Configuration of sampling mode */
  498. MODIFY_REG(hadc->Instance->CFGR2, ADC_CFGR2_BULB | ADC_CFGR2_SMPTRIG, hadc->Init.SamplingMode);
  499. /* Parameters update conditioned to ADC state: */
  500. /* Parameters that can be updated when ADC is disabled or enabled without */
  501. /* conversion on going on regular and injected groups: */
  502. /* - DMA continuous request Init.DMAContinuousRequests */
  503. /* - LowPowerAutoWait feature Init.LowPowerAutoWait */
  504. /* - Oversampling parameters Init.Oversampling */
  505. tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
  506. if ((tmp_adc_is_conversion_on_going_regular == 0UL)
  507. && (tmp_adc_is_conversion_on_going_injected == 0UL)
  508. )
  509. {
  510. tmp_cfgr = (
  511. ADC_CFGR_AUTOWAIT((uint32_t)hadc->Init.LowPowerAutoWait) |
  512. ADC_CFGR_DMACONTREQ((uint32_t)hadc->Init.DMAContinuousRequests));
  513. MODIFY_REG(hadc->Instance->CFGR, ADC_CFGR_FIELDS_2, tmp_cfgr);
  514. if (hadc->Init.OversamplingMode == ENABLE)
  515. {
  516. assert_param(IS_ADC_OVERSAMPLING_RATIO(hadc->Init.Oversampling.Ratio));
  517. assert_param(IS_ADC_RIGHT_BIT_SHIFT(hadc->Init.Oversampling.RightBitShift));
  518. assert_param(IS_ADC_TRIGGERED_OVERSAMPLING_MODE(hadc->Init.Oversampling.TriggeredMode));
  519. assert_param(IS_ADC_REGOVERSAMPLING_MODE(hadc->Init.Oversampling.OversamplingStopReset));
  520. /* Configuration of Oversampler: */
  521. /* - Oversampling Ratio */
  522. /* - Right bit shift */
  523. /* - Triggered mode */
  524. /* - Oversampling mode (continued/resumed) */
  525. MODIFY_REG(hadc->Instance->CFGR2,
  526. ADC_CFGR2_OVSR |
  527. ADC_CFGR2_OVSS |
  528. ADC_CFGR2_TROVS |
  529. ADC_CFGR2_ROVSM,
  530. ADC_CFGR2_ROVSE |
  531. hadc->Init.Oversampling.Ratio |
  532. hadc->Init.Oversampling.RightBitShift |
  533. hadc->Init.Oversampling.TriggeredMode |
  534. hadc->Init.Oversampling.OversamplingStopReset
  535. );
  536. }
  537. else
  538. {
  539. /* Disable ADC oversampling scope on ADC group regular */
  540. CLEAR_BIT(hadc->Instance->CFGR2, ADC_CFGR2_ROVSE);
  541. }
  542. }
  543. /* Configuration of regular group sequencer: */
  544. /* - if scan mode is disabled, regular channels sequence length is set to */
  545. /* 0x00: 1 channel converted (channel on regular rank 1) */
  546. /* Parameter "NbrOfConversion" is discarded. */
  547. /* Note: Scan mode is not present by hardware on this device, but */
  548. /* emulated by software for alignment over all STM32 devices. */
  549. /* - if scan mode is enabled, regular channels sequence length is set to */
  550. /* parameter "NbrOfConversion". */
  551. if (hadc->Init.ScanConvMode == ADC_SCAN_ENABLE)
  552. {
  553. /* Set number of ranks in regular group sequencer */
  554. MODIFY_REG(hadc->Instance->SQR1, ADC_SQR1_L, (hadc->Init.NbrOfConversion - (uint8_t)1));
  555. }
  556. else
  557. {
  558. CLEAR_BIT(hadc->Instance->SQR1, ADC_SQR1_L);
  559. }
  560. /* Initialize the ADC state */
  561. /* Clear HAL_ADC_STATE_BUSY_INTERNAL bit, set HAL_ADC_STATE_READY bit */
  562. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_BUSY_INTERNAL, HAL_ADC_STATE_READY);
  563. }
  564. else
  565. {
  566. /* Update ADC state machine to error */
  567. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  568. tmp_hal_status = HAL_ERROR;
  569. }
  570. /* Return function status */
  571. return tmp_hal_status;
  572. }
  573. /**
  574. * @brief Deinitialize the ADC peripheral registers to their default reset
  575. * values, with deinitialization of the ADC MSP.
  576. * @note For devices with several ADCs: reset of ADC common registers is done
  577. * only if all ADCs sharing the same common group are disabled.
  578. * (function "HAL_ADC_MspDeInit()" is also called under the same conditions:
  579. * all ADC instances use the same core clock at RCC level, disabling
  580. * the core clock reset all ADC instances).
  581. * If this is not the case, reset of these common parameters reset is
  582. * bypassed without error reporting: it can be the intended behavior in
  583. * case of reset of a single ADC while the other ADCs sharing the same
  584. * common group is still running.
  585. * @note By default, HAL_ADC_DeInit() set ADC in mode deep power-down:
  586. * this saves more power by reducing leakage currents
  587. * and is particularly interesting before entering MCU low-power modes.
  588. * @param hadc ADC handle
  589. * @retval HAL status
  590. */
  591. HAL_StatusTypeDef HAL_ADC_DeInit(ADC_HandleTypeDef *hadc)
  592. {
  593. HAL_StatusTypeDef tmp_hal_status;
  594. /* Check ADC handle */
  595. if (hadc == NULL)
  596. {
  597. return HAL_ERROR;
  598. }
  599. /* Check the parameters */
  600. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  601. /* Set ADC state */
  602. SET_BIT(hadc->State, HAL_ADC_STATE_BUSY_INTERNAL);
  603. /* Stop potential conversion on going */
  604. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_INJECTED_GROUP);
  605. /* Disable ADC peripheral if conversions are effectively stopped */
  606. /* Flush register JSQR: reset the queue sequencer when injected */
  607. /* queue sequencer is enabled and ADC disabled. */
  608. /* The software and hardware triggers of the injected sequence are both */
  609. /* internally disabled just after the completion of the last valid */
  610. /* injected sequence. */
  611. SET_BIT(hadc->Instance->CFGR, ADC_CFGR_JQM);
  612. /* Disable ADC peripheral if conversions are effectively stopped */
  613. if (tmp_hal_status == HAL_OK)
  614. {
  615. /* Disable the ADC peripheral */
  616. tmp_hal_status = ADC_Disable(hadc);
  617. /* Check if ADC is effectively disabled */
  618. if (tmp_hal_status == HAL_OK)
  619. {
  620. /* Change ADC state */
  621. hadc->State = HAL_ADC_STATE_READY;
  622. }
  623. }
  624. /* Note: HAL ADC deInit is done independently of ADC conversion stop */
  625. /* and disable return status. In case of status fail, attempt to */
  626. /* perform deinitialization anyway and it is up user code in */
  627. /* in HAL_ADC_MspDeInit() to reset the ADC peripheral using */
  628. /* system RCC hard reset. */
  629. /* ========== Reset ADC registers ========== */
  630. /* Reset register IER */
  631. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_AWD3 | ADC_IT_AWD2 | ADC_IT_AWD1 |
  632. ADC_IT_JQOVF | ADC_IT_OVR |
  633. ADC_IT_JEOS | ADC_IT_JEOC |
  634. ADC_IT_EOS | ADC_IT_EOC |
  635. ADC_IT_EOSMP | ADC_IT_RDY));
  636. /* Reset register ISR */
  637. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_AWD3 | ADC_FLAG_AWD2 | ADC_FLAG_AWD1 |
  638. ADC_FLAG_JQOVF | ADC_FLAG_OVR |
  639. ADC_FLAG_JEOS | ADC_FLAG_JEOC |
  640. ADC_FLAG_EOS | ADC_FLAG_EOC |
  641. ADC_FLAG_EOSMP | ADC_FLAG_RDY));
  642. /* Reset register CR */
  643. /* Bits ADC_CR_JADSTP, ADC_CR_ADSTP, ADC_CR_JADSTART, ADC_CR_ADSTART,
  644. ADC_CR_ADCAL, ADC_CR_ADDIS and ADC_CR_ADEN are in access mode "read-set":
  645. no direct reset applicable.
  646. Update CR register to reset value where doable by software */
  647. CLEAR_BIT(hadc->Instance->CR, ADC_CR_ADVREGEN | ADC_CR_ADCALDIF);
  648. SET_BIT(hadc->Instance->CR, ADC_CR_DEEPPWD);
  649. /* Reset register CFGR */
  650. CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_FIELDS);
  651. SET_BIT(hadc->Instance->CFGR, ADC_CFGR_JQDIS);
  652. /* Reset register CFGR2 */
  653. CLEAR_BIT(hadc->Instance->CFGR2, ADC_CFGR2_ROVSM | ADC_CFGR2_TROVS | ADC_CFGR2_OVSS |
  654. ADC_CFGR2_OVSR | ADC_CFGR2_JOVSE | ADC_CFGR2_ROVSE);
  655. /* Reset register SMPR1 */
  656. CLEAR_BIT(hadc->Instance->SMPR1, ADC_SMPR1_FIELDS);
  657. /* Reset register SMPR2 */
  658. CLEAR_BIT(hadc->Instance->SMPR2, ADC_SMPR2_SMP18 | ADC_SMPR2_SMP17 | ADC_SMPR2_SMP16 |
  659. ADC_SMPR2_SMP15 | ADC_SMPR2_SMP14 | ADC_SMPR2_SMP13 |
  660. ADC_SMPR2_SMP12 | ADC_SMPR2_SMP11 | ADC_SMPR2_SMP10);
  661. /* Reset register TR1 */
  662. CLEAR_BIT(hadc->Instance->TR1, ADC_TR1_HT1 | ADC_TR1_LT1);
  663. /* Reset register TR2 */
  664. CLEAR_BIT(hadc->Instance->TR2, ADC_TR2_HT2 | ADC_TR2_LT2);
  665. /* Reset register TR3 */
  666. CLEAR_BIT(hadc->Instance->TR3, ADC_TR3_HT3 | ADC_TR3_LT3);
  667. /* Reset register SQR1 */
  668. CLEAR_BIT(hadc->Instance->SQR1, ADC_SQR1_SQ4 | ADC_SQR1_SQ3 | ADC_SQR1_SQ2 |
  669. ADC_SQR1_SQ1 | ADC_SQR1_L);
  670. /* Reset register SQR2 */
  671. CLEAR_BIT(hadc->Instance->SQR2, ADC_SQR2_SQ9 | ADC_SQR2_SQ8 | ADC_SQR2_SQ7 |
  672. ADC_SQR2_SQ6 | ADC_SQR2_SQ5);
  673. /* Reset register SQR3 */
  674. CLEAR_BIT(hadc->Instance->SQR3, ADC_SQR3_SQ14 | ADC_SQR3_SQ13 | ADC_SQR3_SQ12 |
  675. ADC_SQR3_SQ11 | ADC_SQR3_SQ10);
  676. /* Reset register SQR4 */
  677. CLEAR_BIT(hadc->Instance->SQR4, ADC_SQR4_SQ16 | ADC_SQR4_SQ15);
  678. /* Register JSQR was reset when the ADC was disabled */
  679. /* Reset register DR */
  680. /* bits in access mode read only, no direct reset applicable*/
  681. /* Reset register OFR1 */
  682. CLEAR_BIT(hadc->Instance->OFR1, ADC_OFR1_OFFSET1_EN | ADC_OFR1_OFFSET1_CH | ADC_OFR1_OFFSET1);
  683. /* Reset register OFR2 */
  684. CLEAR_BIT(hadc->Instance->OFR2, ADC_OFR2_OFFSET2_EN | ADC_OFR2_OFFSET2_CH | ADC_OFR2_OFFSET2);
  685. /* Reset register OFR3 */
  686. CLEAR_BIT(hadc->Instance->OFR3, ADC_OFR3_OFFSET3_EN | ADC_OFR3_OFFSET3_CH | ADC_OFR3_OFFSET3);
  687. /* Reset register OFR4 */
  688. CLEAR_BIT(hadc->Instance->OFR4, ADC_OFR4_OFFSET4_EN | ADC_OFR4_OFFSET4_CH | ADC_OFR4_OFFSET4);
  689. /* Reset registers JDR1, JDR2, JDR3, JDR4 */
  690. /* bits in access mode read only, no direct reset applicable*/
  691. /* Reset register AWD2CR */
  692. CLEAR_BIT(hadc->Instance->AWD2CR, ADC_AWD2CR_AWD2CH);
  693. /* Reset register AWD3CR */
  694. CLEAR_BIT(hadc->Instance->AWD3CR, ADC_AWD3CR_AWD3CH);
  695. /* Reset register DIFSEL */
  696. CLEAR_BIT(hadc->Instance->DIFSEL, ADC_DIFSEL_DIFSEL);
  697. /* Reset register CALFACT */
  698. CLEAR_BIT(hadc->Instance->CALFACT, ADC_CALFACT_CALFACT_D | ADC_CALFACT_CALFACT_S);
  699. /* ========== Reset common ADC registers ========== */
  700. /* Software is allowed to change common parameters only when all the other
  701. ADCs are disabled. */
  702. if (__LL_ADC_IS_ENABLED_ALL_COMMON_INSTANCE(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) == 0UL)
  703. {
  704. /* Reset configuration of ADC common register CCR:
  705. - clock mode: CKMODE, PRESCEN
  706. - multimode related parameters (when this feature is available): MDMA,
  707. DMACFG, DELAY, DUAL (set by HAL_ADCEx_MultiModeConfigChannel() API)
  708. - internal measurement paths: Vbat, temperature sensor, Vref (set into
  709. HAL_ADC_ConfigChannel() or HAL_ADCEx_InjectedConfigChannel() )
  710. */
  711. ADC_CLEAR_COMMON_CONTROL_REGISTER(hadc);
  712. /* ========== Hard reset ADC peripheral ========== */
  713. /* Performs a global reset of the entire ADC peripherals instances */
  714. /* sharing the same common ADC instance: ADC state is forced to */
  715. /* a similar state as after device power-on. */
  716. /* Note: A possible implementation is to add RCC bus reset of ADC */
  717. /* (for example, using macro */
  718. /* __HAL_RCC_ADC..._FORCE_RESET()/..._RELEASE_RESET()/..._CLK_DISABLE()) */
  719. /* in function "void HAL_ADC_MspDeInit(ADC_HandleTypeDef *hadc)": */
  720. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  721. if (hadc->MspDeInitCallback == NULL)
  722. {
  723. hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit */
  724. }
  725. /* DeInit the low level hardware */
  726. hadc->MspDeInitCallback(hadc);
  727. #else
  728. /* DeInit the low level hardware */
  729. HAL_ADC_MspDeInit(hadc);
  730. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  731. }
  732. /* Set ADC error code to none */
  733. ADC_CLEAR_ERRORCODE(hadc);
  734. /* Reset injected channel configuration parameters */
  735. hadc->InjectionConfig.ContextQueue = 0;
  736. hadc->InjectionConfig.ChannelCount = 0;
  737. /* Set ADC state */
  738. hadc->State = HAL_ADC_STATE_RESET;
  739. /* Process unlocked */
  740. __HAL_UNLOCK(hadc);
  741. /* Return function status */
  742. return tmp_hal_status;
  743. }
  744. /**
  745. * @brief Initialize the ADC MSP.
  746. * @param hadc ADC handle
  747. * @retval None
  748. */
  749. __weak void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc)
  750. {
  751. /* Prevent unused argument(s) compilation warning */
  752. UNUSED(hadc);
  753. /* NOTE : This function should not be modified. When the callback is needed,
  754. function HAL_ADC_MspInit must be implemented in the user file.
  755. */
  756. }
  757. /**
  758. * @brief DeInitialize the ADC MSP.
  759. * @param hadc ADC handle
  760. * @note All ADC instances use the same core clock at RCC level, disabling
  761. * the core clock reset all ADC instances).
  762. * @retval None
  763. */
  764. __weak void HAL_ADC_MspDeInit(ADC_HandleTypeDef *hadc)
  765. {
  766. /* Prevent unused argument(s) compilation warning */
  767. UNUSED(hadc);
  768. /* NOTE : This function should not be modified. When the callback is needed,
  769. function HAL_ADC_MspDeInit must be implemented in the user file.
  770. */
  771. }
  772. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  773. /**
  774. * @brief Register a User ADC Callback
  775. * To be used instead of the weak predefined callback
  776. * @param hadc Pointer to a ADC_HandleTypeDef structure that contains
  777. * the configuration information for the specified ADC.
  778. * @param CallbackID ID of the callback to be registered
  779. * This parameter can be one of the following values:
  780. * @arg @ref HAL_ADC_CONVERSION_COMPLETE_CB_ID ADC conversion complete callback ID
  781. * @arg @ref HAL_ADC_CONVERSION_HALF_CB_ID ADC conversion DMA half-transfer callback ID
  782. * @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID ADC analog watchdog 1 callback ID
  783. * @arg @ref HAL_ADC_ERROR_CB_ID ADC error callback ID
  784. * @arg @ref HAL_ADC_INJ_CONVERSION_COMPLETE_CB_ID ADC group injected conversion complete callback ID
  785. * @arg @ref HAL_ADC_INJ_QUEUE_OVEFLOW_CB_ID ADC group injected context queue overflow callback ID
  786. * @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_2_CB_ID ADC analog watchdog 2 callback ID
  787. * @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_3_CB_ID ADC analog watchdog 3 callback ID
  788. * @arg @ref HAL_ADC_END_OF_SAMPLING_CB_ID ADC end of sampling callback ID
  789. * @arg @ref HAL_ADC_MSPINIT_CB_ID ADC Msp Init callback ID
  790. * @arg @ref HAL_ADC_MSPDEINIT_CB_ID ADC Msp DeInit callback ID
  791. * @arg @ref HAL_ADC_MSPINIT_CB_ID MspInit callback ID
  792. * @arg @ref HAL_ADC_MSPDEINIT_CB_ID MspDeInit callback ID
  793. * @param pCallback pointer to the Callback function
  794. * @retval HAL status
  795. */
  796. HAL_StatusTypeDef HAL_ADC_RegisterCallback(ADC_HandleTypeDef *hadc, HAL_ADC_CallbackIDTypeDef CallbackID,
  797. pADC_CallbackTypeDef pCallback)
  798. {
  799. HAL_StatusTypeDef status = HAL_OK;
  800. if (pCallback == NULL)
  801. {
  802. /* Update the error code */
  803. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  804. return HAL_ERROR;
  805. }
  806. if ((hadc->State & HAL_ADC_STATE_READY) != 0UL)
  807. {
  808. switch (CallbackID)
  809. {
  810. case HAL_ADC_CONVERSION_COMPLETE_CB_ID :
  811. hadc->ConvCpltCallback = pCallback;
  812. break;
  813. case HAL_ADC_CONVERSION_HALF_CB_ID :
  814. hadc->ConvHalfCpltCallback = pCallback;
  815. break;
  816. case HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID :
  817. hadc->LevelOutOfWindowCallback = pCallback;
  818. break;
  819. case HAL_ADC_ERROR_CB_ID :
  820. hadc->ErrorCallback = pCallback;
  821. break;
  822. case HAL_ADC_INJ_CONVERSION_COMPLETE_CB_ID :
  823. hadc->InjectedConvCpltCallback = pCallback;
  824. break;
  825. case HAL_ADC_INJ_QUEUE_OVEFLOW_CB_ID :
  826. hadc->InjectedQueueOverflowCallback = pCallback;
  827. break;
  828. case HAL_ADC_LEVEL_OUT_OF_WINDOW_2_CB_ID :
  829. hadc->LevelOutOfWindow2Callback = pCallback;
  830. break;
  831. case HAL_ADC_LEVEL_OUT_OF_WINDOW_3_CB_ID :
  832. hadc->LevelOutOfWindow3Callback = pCallback;
  833. break;
  834. case HAL_ADC_END_OF_SAMPLING_CB_ID :
  835. hadc->EndOfSamplingCallback = pCallback;
  836. break;
  837. case HAL_ADC_MSPINIT_CB_ID :
  838. hadc->MspInitCallback = pCallback;
  839. break;
  840. case HAL_ADC_MSPDEINIT_CB_ID :
  841. hadc->MspDeInitCallback = pCallback;
  842. break;
  843. default :
  844. /* Update the error code */
  845. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  846. /* Return error status */
  847. status = HAL_ERROR;
  848. break;
  849. }
  850. }
  851. else if (HAL_ADC_STATE_RESET == hadc->State)
  852. {
  853. switch (CallbackID)
  854. {
  855. case HAL_ADC_MSPINIT_CB_ID :
  856. hadc->MspInitCallback = pCallback;
  857. break;
  858. case HAL_ADC_MSPDEINIT_CB_ID :
  859. hadc->MspDeInitCallback = pCallback;
  860. break;
  861. default :
  862. /* Update the error code */
  863. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  864. /* Return error status */
  865. status = HAL_ERROR;
  866. break;
  867. }
  868. }
  869. else
  870. {
  871. /* Update the error code */
  872. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  873. /* Return error status */
  874. status = HAL_ERROR;
  875. }
  876. return status;
  877. }
  878. /**
  879. * @brief Unregister a ADC Callback
  880. * ADC callback is redirected to the weak predefined callback
  881. * @param hadc Pointer to a ADC_HandleTypeDef structure that contains
  882. * the configuration information for the specified ADC.
  883. * @param CallbackID ID of the callback to be unregistered
  884. * This parameter can be one of the following values:
  885. * @arg @ref HAL_ADC_CONVERSION_COMPLETE_CB_ID ADC conversion complete callback ID
  886. * @arg @ref HAL_ADC_CONVERSION_HALF_CB_ID ADC conversion DMA half-transfer callback ID
  887. * @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID ADC analog watchdog 1 callback ID
  888. * @arg @ref HAL_ADC_ERROR_CB_ID ADC error callback ID
  889. * @arg @ref HAL_ADC_INJ_CONVERSION_COMPLETE_CB_ID ADC group injected conversion complete callback ID
  890. * @arg @ref HAL_ADC_INJ_QUEUE_OVEFLOW_CB_ID ADC group injected context queue overflow callback ID
  891. * @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_2_CB_ID ADC analog watchdog 2 callback ID
  892. * @arg @ref HAL_ADC_LEVEL_OUT_OF_WINDOW_3_CB_ID ADC analog watchdog 3 callback ID
  893. * @arg @ref HAL_ADC_END_OF_SAMPLING_CB_ID ADC end of sampling callback ID
  894. * @arg @ref HAL_ADC_MSPINIT_CB_ID ADC Msp Init callback ID
  895. * @arg @ref HAL_ADC_MSPDEINIT_CB_ID ADC Msp DeInit callback ID
  896. * @arg @ref HAL_ADC_MSPINIT_CB_ID MspInit callback ID
  897. * @arg @ref HAL_ADC_MSPDEINIT_CB_ID MspDeInit callback ID
  898. * @retval HAL status
  899. */
  900. HAL_StatusTypeDef HAL_ADC_UnRegisterCallback(ADC_HandleTypeDef *hadc, HAL_ADC_CallbackIDTypeDef CallbackID)
  901. {
  902. HAL_StatusTypeDef status = HAL_OK;
  903. if ((hadc->State & HAL_ADC_STATE_READY) != 0UL)
  904. {
  905. switch (CallbackID)
  906. {
  907. case HAL_ADC_CONVERSION_COMPLETE_CB_ID :
  908. hadc->ConvCpltCallback = HAL_ADC_ConvCpltCallback;
  909. break;
  910. case HAL_ADC_CONVERSION_HALF_CB_ID :
  911. hadc->ConvHalfCpltCallback = HAL_ADC_ConvHalfCpltCallback;
  912. break;
  913. case HAL_ADC_LEVEL_OUT_OF_WINDOW_1_CB_ID :
  914. hadc->LevelOutOfWindowCallback = HAL_ADC_LevelOutOfWindowCallback;
  915. break;
  916. case HAL_ADC_ERROR_CB_ID :
  917. hadc->ErrorCallback = HAL_ADC_ErrorCallback;
  918. break;
  919. case HAL_ADC_INJ_CONVERSION_COMPLETE_CB_ID :
  920. hadc->InjectedConvCpltCallback = HAL_ADCEx_InjectedConvCpltCallback;
  921. break;
  922. case HAL_ADC_INJ_QUEUE_OVEFLOW_CB_ID :
  923. hadc->InjectedQueueOverflowCallback = HAL_ADCEx_InjectedQueueOverflowCallback;
  924. break;
  925. case HAL_ADC_LEVEL_OUT_OF_WINDOW_2_CB_ID :
  926. hadc->LevelOutOfWindow2Callback = HAL_ADCEx_LevelOutOfWindow2Callback;
  927. break;
  928. case HAL_ADC_LEVEL_OUT_OF_WINDOW_3_CB_ID :
  929. hadc->LevelOutOfWindow3Callback = HAL_ADCEx_LevelOutOfWindow3Callback;
  930. break;
  931. case HAL_ADC_END_OF_SAMPLING_CB_ID :
  932. hadc->EndOfSamplingCallback = HAL_ADCEx_EndOfSamplingCallback;
  933. break;
  934. case HAL_ADC_MSPINIT_CB_ID :
  935. hadc->MspInitCallback = HAL_ADC_MspInit; /* Legacy weak MspInit */
  936. break;
  937. case HAL_ADC_MSPDEINIT_CB_ID :
  938. hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit */
  939. break;
  940. default :
  941. /* Update the error code */
  942. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  943. /* Return error status */
  944. status = HAL_ERROR;
  945. break;
  946. }
  947. }
  948. else if (HAL_ADC_STATE_RESET == hadc->State)
  949. {
  950. switch (CallbackID)
  951. {
  952. case HAL_ADC_MSPINIT_CB_ID :
  953. hadc->MspInitCallback = HAL_ADC_MspInit; /* Legacy weak MspInit */
  954. break;
  955. case HAL_ADC_MSPDEINIT_CB_ID :
  956. hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit */
  957. break;
  958. default :
  959. /* Update the error code */
  960. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  961. /* Return error status */
  962. status = HAL_ERROR;
  963. break;
  964. }
  965. }
  966. else
  967. {
  968. /* Update the error code */
  969. hadc->ErrorCode |= HAL_ADC_ERROR_INVALID_CALLBACK;
  970. /* Return error status */
  971. status = HAL_ERROR;
  972. }
  973. return status;
  974. }
  975. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  976. /**
  977. * @}
  978. */
  979. /** @defgroup ADC_Exported_Functions_Group2 ADC Input and Output operation functions
  980. * @brief ADC IO operation functions
  981. *
  982. @verbatim
  983. ===============================================================================
  984. ##### IO operation functions #####
  985. ===============================================================================
  986. [..] This section provides functions allowing to:
  987. (+) Start conversion of regular group.
  988. (+) Stop conversion of regular group.
  989. (+) Poll for conversion complete on regular group.
  990. (+) Poll for conversion event.
  991. (+) Get result of regular channel conversion.
  992. (+) Start conversion of regular group and enable interruptions.
  993. (+) Stop conversion of regular group and disable interruptions.
  994. (+) Handle ADC interrupt request
  995. (+) Start conversion of regular group and enable DMA transfer.
  996. (+) Stop conversion of regular group and disable ADC DMA transfer.
  997. @endverbatim
  998. * @{
  999. */
  1000. /**
  1001. * @brief Enable ADC, start conversion of regular group.
  1002. * @note Interruptions enabled in this function: None.
  1003. * @note Case of multimode enabled (when multimode feature is available):
  1004. * if ADC is Slave, ADC is enabled but conversion is not started,
  1005. * if ADC is master, ADC is enabled and multimode conversion is started.
  1006. * @param hadc ADC handle
  1007. * @retval HAL status
  1008. */
  1009. HAL_StatusTypeDef HAL_ADC_Start(ADC_HandleTypeDef *hadc)
  1010. {
  1011. HAL_StatusTypeDef tmp_hal_status;
  1012. #if defined(ADC_MULTIMODE_SUPPORT)
  1013. const ADC_TypeDef *tmpADC_Master;
  1014. uint32_t tmp_multimode_config = LL_ADC_GetMultimode(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
  1015. #endif /* ADC_MULTIMODE_SUPPORT */
  1016. /* Check the parameters */
  1017. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1018. /* Perform ADC enable and conversion start if no conversion is on going */
  1019. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
  1020. {
  1021. /* Process locked */
  1022. __HAL_LOCK(hadc);
  1023. /* Enable the ADC peripheral */
  1024. tmp_hal_status = ADC_Enable(hadc);
  1025. /* Start conversion if ADC is effectively enabled */
  1026. if (tmp_hal_status == HAL_OK)
  1027. {
  1028. /* Set ADC state */
  1029. /* - Clear state bitfield related to regular group conversion results */
  1030. /* - Set state bitfield related to regular operation */
  1031. ADC_STATE_CLR_SET(hadc->State,
  1032. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP,
  1033. HAL_ADC_STATE_REG_BUSY);
  1034. #if defined(ADC_MULTIMODE_SUPPORT)
  1035. /* Reset HAL_ADC_STATE_MULTIMODE_SLAVE bit
  1036. - if ADC instance is master or if multimode feature is not available
  1037. - if multimode setting is disabled (ADC instance slave in independent mode) */
  1038. if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
  1039. || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
  1040. )
  1041. {
  1042. CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  1043. }
  1044. #endif /* ADC_MULTIMODE_SUPPORT */
  1045. /* Set ADC error code */
  1046. /* Check if a conversion is on going on ADC group injected */
  1047. if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  1048. {
  1049. /* Reset ADC error code fields related to regular conversions only */
  1050. CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA));
  1051. }
  1052. else
  1053. {
  1054. /* Reset all ADC error code fields */
  1055. ADC_CLEAR_ERRORCODE(hadc);
  1056. }
  1057. /* Clear ADC group regular conversion flag and overrun flag */
  1058. /* (To ensure of no unknown state from potential previous ADC operations) */
  1059. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
  1060. /* Process unlocked */
  1061. /* Unlock before starting ADC conversions: in case of potential */
  1062. /* interruption, to let the process to ADC IRQ Handler. */
  1063. __HAL_UNLOCK(hadc);
  1064. /* Enable conversion of regular group. */
  1065. /* If software start has been selected, conversion starts immediately. */
  1066. /* If external trigger has been selected, conversion will start at next */
  1067. /* trigger event. */
  1068. /* Case of multimode enabled (when multimode feature is available): */
  1069. /* - if ADC is slave and dual regular conversions are enabled, ADC is */
  1070. /* enabled only (conversion is not started), */
  1071. /* - if ADC is master, ADC is enabled and conversion is started. */
  1072. #if defined(ADC_MULTIMODE_SUPPORT)
  1073. if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
  1074. || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
  1075. || (tmp_multimode_config == LL_ADC_MULTI_DUAL_INJ_SIMULT)
  1076. || (tmp_multimode_config == LL_ADC_MULTI_DUAL_INJ_ALTERN)
  1077. )
  1078. {
  1079. /* ADC instance is not a multimode slave instance with multimode regular conversions enabled */
  1080. if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_JAUTO) != 0UL)
  1081. {
  1082. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
  1083. }
  1084. /* Start ADC group regular conversion */
  1085. LL_ADC_REG_StartConversion(hadc->Instance);
  1086. }
  1087. else
  1088. {
  1089. /* ADC instance is a multimode slave instance with multimode regular conversions enabled */
  1090. SET_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  1091. /* if Master ADC JAUTO bit is set, update Slave State in setting
  1092. HAL_ADC_STATE_INJ_BUSY bit and in resetting HAL_ADC_STATE_INJ_EOC bit */
  1093. tmpADC_Master = __LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance);
  1094. if (READ_BIT(tmpADC_Master->CFGR, ADC_CFGR_JAUTO) != 0UL)
  1095. {
  1096. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
  1097. }
  1098. }
  1099. #else
  1100. if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_JAUTO) != 0UL)
  1101. {
  1102. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
  1103. }
  1104. /* Start ADC group regular conversion */
  1105. LL_ADC_REG_StartConversion(hadc->Instance);
  1106. #endif /* ADC_MULTIMODE_SUPPORT */
  1107. }
  1108. else
  1109. {
  1110. /* Process unlocked */
  1111. __HAL_UNLOCK(hadc);
  1112. }
  1113. }
  1114. else
  1115. {
  1116. tmp_hal_status = HAL_BUSY;
  1117. }
  1118. /* Return function status */
  1119. return tmp_hal_status;
  1120. }
  1121. /**
  1122. * @brief Stop ADC conversion of regular group (and injected channels in
  1123. * case of auto_injection mode), disable ADC peripheral.
  1124. * @note: ADC peripheral disable is forcing stop of potential
  1125. * conversion on injected group. If injected group is under use, it
  1126. * should be preliminarily stopped using HAL_ADCEx_InjectedStop function.
  1127. * @param hadc ADC handle
  1128. * @retval HAL status.
  1129. */
  1130. HAL_StatusTypeDef HAL_ADC_Stop(ADC_HandleTypeDef *hadc)
  1131. {
  1132. HAL_StatusTypeDef tmp_hal_status;
  1133. /* Check the parameters */
  1134. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1135. /* Process locked */
  1136. __HAL_LOCK(hadc);
  1137. /* 1. Stop potential conversion on going, on ADC groups regular and injected */
  1138. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_INJECTED_GROUP);
  1139. /* Disable ADC peripheral if conversions are effectively stopped */
  1140. if (tmp_hal_status == HAL_OK)
  1141. {
  1142. /* 2. Disable the ADC peripheral */
  1143. tmp_hal_status = ADC_Disable(hadc);
  1144. /* Check if ADC is effectively disabled */
  1145. if (tmp_hal_status == HAL_OK)
  1146. {
  1147. /* Set ADC state */
  1148. ADC_STATE_CLR_SET(hadc->State,
  1149. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  1150. HAL_ADC_STATE_READY);
  1151. }
  1152. }
  1153. /* Process unlocked */
  1154. __HAL_UNLOCK(hadc);
  1155. /* Return function status */
  1156. return tmp_hal_status;
  1157. }
  1158. /**
  1159. * @brief Wait for regular group conversion to be completed.
  1160. * @note ADC conversion flags EOS (end of sequence) and EOC (end of
  1161. * conversion) are cleared by this function, with an exception:
  1162. * if low power feature "LowPowerAutoWait" is enabled, flags are
  1163. * not cleared to not interfere with this feature until data register
  1164. * is read using function HAL_ADC_GetValue().
  1165. * @note This function cannot be used in a particular setup: ADC configured
  1166. * in DMA mode and polling for end of each conversion (ADC init
  1167. * parameter "EOCSelection" set to ADC_EOC_SINGLE_CONV).
  1168. * In this case, DMA resets the flag EOC and polling cannot be
  1169. * performed on each conversion. Nevertheless, polling can still
  1170. * be performed on the complete sequence (ADC init
  1171. * parameter "EOCSelection" set to ADC_EOC_SEQ_CONV).
  1172. * @param hadc ADC handle
  1173. * @param Timeout Timeout value in millisecond.
  1174. * @retval HAL status
  1175. */
  1176. HAL_StatusTypeDef HAL_ADC_PollForConversion(ADC_HandleTypeDef *hadc, uint32_t Timeout)
  1177. {
  1178. uint32_t tickstart;
  1179. uint32_t tmp_Flag_End;
  1180. uint32_t tmp_cfgr;
  1181. #if defined(ADC_MULTIMODE_SUPPORT)
  1182. const ADC_TypeDef *tmpADC_Master;
  1183. uint32_t tmp_multimode_config = LL_ADC_GetMultimode(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
  1184. #endif /* ADC_MULTIMODE_SUPPORT */
  1185. /* Check the parameters */
  1186. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1187. /* If end of conversion selected to end of sequence conversions */
  1188. if (hadc->Init.EOCSelection == ADC_EOC_SEQ_CONV)
  1189. {
  1190. tmp_Flag_End = ADC_FLAG_EOS;
  1191. }
  1192. /* If end of conversion selected to end of unitary conversion */
  1193. else /* ADC_EOC_SINGLE_CONV */
  1194. {
  1195. /* Verification that ADC configuration is compliant with polling for */
  1196. /* each conversion: */
  1197. /* Particular case is ADC configured in DMA mode and ADC sequencer with */
  1198. /* several ranks and polling for end of each conversion. */
  1199. /* For code simplicity sake, this particular case is generalized to */
  1200. /* ADC configured in DMA mode and and polling for end of each conversion. */
  1201. #if defined(ADC_MULTIMODE_SUPPORT)
  1202. if ((tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
  1203. || (tmp_multimode_config == LL_ADC_MULTI_DUAL_INJ_SIMULT)
  1204. || (tmp_multimode_config == LL_ADC_MULTI_DUAL_INJ_ALTERN)
  1205. )
  1206. {
  1207. /* Check ADC DMA mode in independent mode on ADC group regular */
  1208. if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_DMAEN) != 0UL)
  1209. {
  1210. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1211. return HAL_ERROR;
  1212. }
  1213. else
  1214. {
  1215. tmp_Flag_End = (ADC_FLAG_EOC);
  1216. }
  1217. }
  1218. else
  1219. {
  1220. /* Check ADC DMA mode in multimode on ADC group regular */
  1221. if (LL_ADC_GetMultiDMATransfer(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) != LL_ADC_MULTI_REG_DMA_EACH_ADC)
  1222. {
  1223. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1224. return HAL_ERROR;
  1225. }
  1226. else
  1227. {
  1228. tmp_Flag_End = (ADC_FLAG_EOC);
  1229. }
  1230. }
  1231. #else
  1232. /* Check ADC DMA mode */
  1233. if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_DMAEN) != 0UL)
  1234. {
  1235. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1236. return HAL_ERROR;
  1237. }
  1238. else
  1239. {
  1240. tmp_Flag_End = (ADC_FLAG_EOC);
  1241. }
  1242. #endif /* ADC_MULTIMODE_SUPPORT */
  1243. }
  1244. /* Get tick count */
  1245. tickstart = HAL_GetTick();
  1246. /* Wait until End of unitary conversion or sequence conversions flag is raised */
  1247. while ((hadc->Instance->ISR & tmp_Flag_End) == 0UL)
  1248. {
  1249. /* Check if timeout is disabled (set to infinite wait) */
  1250. if (Timeout != HAL_MAX_DELAY)
  1251. {
  1252. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0UL))
  1253. {
  1254. /* New check to avoid false timeout detection in case of preemption */
  1255. if ((hadc->Instance->ISR & tmp_Flag_End) == 0UL)
  1256. {
  1257. /* Update ADC state machine to timeout */
  1258. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  1259. /* Process unlocked */
  1260. __HAL_UNLOCK(hadc);
  1261. return HAL_TIMEOUT;
  1262. }
  1263. }
  1264. }
  1265. }
  1266. /* Update ADC state machine */
  1267. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  1268. /* Determine whether any further conversion upcoming on group regular */
  1269. /* by external trigger, continuous mode or scan sequence on going. */
  1270. if ((LL_ADC_REG_IsTriggerSourceSWStart(hadc->Instance) != 0UL)
  1271. && (hadc->Init.ContinuousConvMode == DISABLE)
  1272. )
  1273. {
  1274. /* Check whether end of sequence is reached */
  1275. if (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOS))
  1276. {
  1277. /* Set ADC state */
  1278. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  1279. if ((hadc->State & HAL_ADC_STATE_INJ_BUSY) == 0UL)
  1280. {
  1281. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  1282. }
  1283. }
  1284. }
  1285. /* Get relevant register CFGR in ADC instance of ADC master or slave */
  1286. /* in function of multimode state (for devices with multimode */
  1287. /* available). */
  1288. #if defined(ADC_MULTIMODE_SUPPORT)
  1289. if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
  1290. || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
  1291. || (tmp_multimode_config == LL_ADC_MULTI_DUAL_INJ_SIMULT)
  1292. || (tmp_multimode_config == LL_ADC_MULTI_DUAL_INJ_ALTERN)
  1293. )
  1294. {
  1295. /* Retrieve handle ADC CFGR register */
  1296. tmp_cfgr = READ_REG(hadc->Instance->CFGR);
  1297. }
  1298. else
  1299. {
  1300. /* Retrieve Master ADC CFGR register */
  1301. tmpADC_Master = __LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance);
  1302. tmp_cfgr = READ_REG(tmpADC_Master->CFGR);
  1303. }
  1304. #else
  1305. /* Retrieve handle ADC CFGR register */
  1306. tmp_cfgr = READ_REG(hadc->Instance->CFGR);
  1307. #endif /* ADC_MULTIMODE_SUPPORT */
  1308. /* Clear polled flag */
  1309. if (tmp_Flag_End == ADC_FLAG_EOS)
  1310. {
  1311. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOS);
  1312. }
  1313. else
  1314. {
  1315. /* Clear end of conversion EOC flag of regular group if low power feature */
  1316. /* "LowPowerAutoWait " is disabled, to not interfere with this feature */
  1317. /* until data register is read using function HAL_ADC_GetValue(). */
  1318. if (READ_BIT(tmp_cfgr, ADC_CFGR_AUTDLY) == 0UL)
  1319. {
  1320. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS));
  1321. }
  1322. }
  1323. /* Return function status */
  1324. return HAL_OK;
  1325. }
  1326. /**
  1327. * @brief Poll for ADC event.
  1328. * @param hadc ADC handle
  1329. * @param EventType the ADC event type.
  1330. * This parameter can be one of the following values:
  1331. * @arg @ref ADC_EOSMP_EVENT ADC End of Sampling event
  1332. * @arg @ref ADC_AWD1_EVENT ADC Analog watchdog 1 event (main analog watchdog, present on
  1333. * all STM32 series)
  1334. * @arg @ref ADC_AWD2_EVENT ADC Analog watchdog 2 event (additional analog watchdog, not present on
  1335. * all STM32 series)
  1336. * @arg @ref ADC_AWD3_EVENT ADC Analog watchdog 3 event (additional analog watchdog, not present on
  1337. * all STM32 series)
  1338. * @arg @ref ADC_OVR_EVENT ADC Overrun event
  1339. * @arg @ref ADC_JQOVF_EVENT ADC Injected context queue overflow event
  1340. * @param Timeout Timeout value in millisecond.
  1341. * @note The relevant flag is cleared if found to be set, except for ADC_FLAG_OVR.
  1342. * Indeed, the latter is reset only if hadc->Init.Overrun field is set
  1343. * to ADC_OVR_DATA_OVERWRITTEN. Otherwise, data register may be potentially overwritten
  1344. * by a new converted data as soon as OVR is cleared.
  1345. * To reset OVR flag once the preserved data is retrieved, the user can resort
  1346. * to macro __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  1347. * @retval HAL status
  1348. */
  1349. HAL_StatusTypeDef HAL_ADC_PollForEvent(ADC_HandleTypeDef *hadc, uint32_t EventType, uint32_t Timeout)
  1350. {
  1351. uint32_t tickstart;
  1352. /* Check the parameters */
  1353. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1354. assert_param(IS_ADC_EVENT_TYPE(EventType));
  1355. /* Get tick count */
  1356. tickstart = HAL_GetTick();
  1357. /* Check selected event flag */
  1358. while (__HAL_ADC_GET_FLAG(hadc, EventType) == 0UL)
  1359. {
  1360. /* Check if timeout is disabled (set to infinite wait) */
  1361. if (Timeout != HAL_MAX_DELAY)
  1362. {
  1363. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0UL))
  1364. {
  1365. /* New check to avoid false timeout detection in case of preemption */
  1366. if (__HAL_ADC_GET_FLAG(hadc, EventType) == 0UL)
  1367. {
  1368. /* Update ADC state machine to timeout */
  1369. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  1370. /* Process unlocked */
  1371. __HAL_UNLOCK(hadc);
  1372. return HAL_TIMEOUT;
  1373. }
  1374. }
  1375. }
  1376. }
  1377. switch (EventType)
  1378. {
  1379. /* End Of Sampling event */
  1380. case ADC_EOSMP_EVENT:
  1381. /* Set ADC state */
  1382. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOSMP);
  1383. /* Clear the End Of Sampling flag */
  1384. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOSMP);
  1385. break;
  1386. /* Analog watchdog (level out of window) event */
  1387. /* Note: In case of several analog watchdog enabled, if needed to know */
  1388. /* which one triggered and on which ADCx, test ADC state of analog watchdog */
  1389. /* flags HAL_ADC_STATE_AWD1/2/3 using function "HAL_ADC_GetState()". */
  1390. /* For example: */
  1391. /* " if ((HAL_ADC_GetState(hadc1) & HAL_ADC_STATE_AWD1) != 0UL) " */
  1392. /* " if ((HAL_ADC_GetState(hadc1) & HAL_ADC_STATE_AWD2) != 0UL) " */
  1393. /* " if ((HAL_ADC_GetState(hadc1) & HAL_ADC_STATE_AWD3) != 0UL) " */
  1394. /* Check analog watchdog 1 flag */
  1395. case ADC_AWD_EVENT:
  1396. /* Set ADC state */
  1397. SET_BIT(hadc->State, HAL_ADC_STATE_AWD1);
  1398. /* Clear ADC analog watchdog flag */
  1399. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD1);
  1400. break;
  1401. /* Check analog watchdog 2 flag */
  1402. case ADC_AWD2_EVENT:
  1403. /* Set ADC state */
  1404. SET_BIT(hadc->State, HAL_ADC_STATE_AWD2);
  1405. /* Clear ADC analog watchdog flag */
  1406. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD2);
  1407. break;
  1408. /* Check analog watchdog 3 flag */
  1409. case ADC_AWD3_EVENT:
  1410. /* Set ADC state */
  1411. SET_BIT(hadc->State, HAL_ADC_STATE_AWD3);
  1412. /* Clear ADC analog watchdog flag */
  1413. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD3);
  1414. break;
  1415. /* Injected context queue overflow event */
  1416. case ADC_JQOVF_EVENT:
  1417. /* Set ADC state */
  1418. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_JQOVF);
  1419. /* Set ADC error code to Injected context queue overflow */
  1420. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_JQOVF);
  1421. /* Clear ADC Injected context queue overflow flag */
  1422. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JQOVF);
  1423. break;
  1424. /* Overrun event */
  1425. default: /* Case ADC_OVR_EVENT */
  1426. /* If overrun is set to overwrite previous data, overrun event is not */
  1427. /* considered as an error. */
  1428. /* (cf ref manual "Managing conversions without using the DMA and without */
  1429. /* overrun ") */
  1430. if (hadc->Init.Overrun == ADC_OVR_DATA_PRESERVED)
  1431. {
  1432. /* Set ADC state */
  1433. SET_BIT(hadc->State, HAL_ADC_STATE_REG_OVR);
  1434. /* Set ADC error code to overrun */
  1435. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_OVR);
  1436. }
  1437. else
  1438. {
  1439. /* Clear ADC Overrun flag only if Overrun is set to ADC_OVR_DATA_OVERWRITTEN
  1440. otherwise, data register is potentially overwritten by new converted data as soon
  1441. as OVR is cleared. */
  1442. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  1443. }
  1444. break;
  1445. }
  1446. /* Return function status */
  1447. return HAL_OK;
  1448. }
  1449. /**
  1450. * @brief Enable ADC, start conversion of regular group with interruption.
  1451. * @note Interruptions enabled in this function according to initialization
  1452. * setting : EOC (end of conversion), EOS (end of sequence),
  1453. * OVR overrun.
  1454. * Each of these interruptions has its dedicated callback function.
  1455. * @note Case of multimode enabled (when multimode feature is available):
  1456. * HAL_ADC_Start_IT() must be called for ADC Slave first, then for
  1457. * ADC Master.
  1458. * For ADC Slave, ADC is enabled only (conversion is not started).
  1459. * For ADC Master, ADC is enabled and multimode conversion is started.
  1460. * @note To guarantee a proper reset of all interruptions once all the needed
  1461. * conversions are obtained, HAL_ADC_Stop_IT() must be called to ensure
  1462. * a correct stop of the IT-based conversions.
  1463. * @note By default, HAL_ADC_Start_IT() does not enable the End Of Sampling
  1464. * interruption. If required (e.g. in case of oversampling with trigger
  1465. * mode), the user must:
  1466. * 1. first clear the EOSMP flag if set with macro __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOSMP)
  1467. * 2. then enable the EOSMP interrupt with macro __HAL_ADC_ENABLE_IT(hadc, ADC_IT_EOSMP)
  1468. * before calling HAL_ADC_Start_IT().
  1469. * @param hadc ADC handle
  1470. * @retval HAL status
  1471. */
  1472. HAL_StatusTypeDef HAL_ADC_Start_IT(ADC_HandleTypeDef *hadc)
  1473. {
  1474. HAL_StatusTypeDef tmp_hal_status;
  1475. #if defined(ADC_MULTIMODE_SUPPORT)
  1476. const ADC_TypeDef *tmpADC_Master;
  1477. uint32_t tmp_multimode_config = LL_ADC_GetMultimode(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
  1478. #endif /* ADC_MULTIMODE_SUPPORT */
  1479. /* Check the parameters */
  1480. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1481. /* Perform ADC enable and conversion start if no conversion is on going */
  1482. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
  1483. {
  1484. /* Process locked */
  1485. __HAL_LOCK(hadc);
  1486. /* Enable the ADC peripheral */
  1487. tmp_hal_status = ADC_Enable(hadc);
  1488. /* Start conversion if ADC is effectively enabled */
  1489. if (tmp_hal_status == HAL_OK)
  1490. {
  1491. /* Set ADC state */
  1492. /* - Clear state bitfield related to regular group conversion results */
  1493. /* - Set state bitfield related to regular operation */
  1494. ADC_STATE_CLR_SET(hadc->State,
  1495. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP,
  1496. HAL_ADC_STATE_REG_BUSY);
  1497. #if defined(ADC_MULTIMODE_SUPPORT)
  1498. /* Reset HAL_ADC_STATE_MULTIMODE_SLAVE bit
  1499. - if ADC instance is master or if multimode feature is not available
  1500. - if multimode setting is disabled (ADC instance slave in independent mode) */
  1501. if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
  1502. || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
  1503. )
  1504. {
  1505. CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  1506. }
  1507. #endif /* ADC_MULTIMODE_SUPPORT */
  1508. /* Set ADC error code */
  1509. /* Check if a conversion is on going on ADC group injected */
  1510. if ((hadc->State & HAL_ADC_STATE_INJ_BUSY) != 0UL)
  1511. {
  1512. /* Reset ADC error code fields related to regular conversions only */
  1513. CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA));
  1514. }
  1515. else
  1516. {
  1517. /* Reset all ADC error code fields */
  1518. ADC_CLEAR_ERRORCODE(hadc);
  1519. }
  1520. /* Clear ADC group regular conversion flag and overrun flag */
  1521. /* (To ensure of no unknown state from potential previous ADC operations) */
  1522. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
  1523. /* Process unlocked */
  1524. /* Unlock before starting ADC conversions: in case of potential */
  1525. /* interruption, to let the process to ADC IRQ Handler. */
  1526. __HAL_UNLOCK(hadc);
  1527. /* Disable all interruptions before enabling the desired ones */
  1528. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_EOS | ADC_IT_OVR));
  1529. /* Enable ADC end of conversion interrupt */
  1530. switch (hadc->Init.EOCSelection)
  1531. {
  1532. case ADC_EOC_SEQ_CONV:
  1533. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_EOS);
  1534. break;
  1535. /* case ADC_EOC_SINGLE_CONV */
  1536. default:
  1537. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_EOC);
  1538. break;
  1539. }
  1540. /* Enable ADC overrun interrupt */
  1541. /* If hadc->Init.Overrun is set to ADC_OVR_DATA_PRESERVED, only then is
  1542. ADC_IT_OVR enabled; otherwise data overwrite is considered as normal
  1543. behavior and no CPU time is lost for a non-processed interruption */
  1544. if (hadc->Init.Overrun == ADC_OVR_DATA_PRESERVED)
  1545. {
  1546. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);
  1547. }
  1548. /* Enable conversion of regular group. */
  1549. /* If software start has been selected, conversion starts immediately. */
  1550. /* If external trigger has been selected, conversion will start at next */
  1551. /* trigger event. */
  1552. /* Case of multimode enabled (when multimode feature is available): */
  1553. /* - if ADC is slave and dual regular conversions are enabled, ADC is */
  1554. /* enabled only (conversion is not started), */
  1555. /* - if ADC is master, ADC is enabled and conversion is started. */
  1556. #if defined(ADC_MULTIMODE_SUPPORT)
  1557. if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
  1558. || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
  1559. || (tmp_multimode_config == LL_ADC_MULTI_DUAL_INJ_SIMULT)
  1560. || (tmp_multimode_config == LL_ADC_MULTI_DUAL_INJ_ALTERN)
  1561. )
  1562. {
  1563. /* ADC instance is not a multimode slave instance with multimode regular conversions enabled */
  1564. if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_JAUTO) != 0UL)
  1565. {
  1566. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
  1567. /* Enable as well injected interruptions in case
  1568. HAL_ADCEx_InjectedStart_IT() has not been called beforehand. This
  1569. allows to start regular and injected conversions when JAUTO is
  1570. set with a single call to HAL_ADC_Start_IT() */
  1571. switch (hadc->Init.EOCSelection)
  1572. {
  1573. case ADC_EOC_SEQ_CONV:
  1574. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC);
  1575. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOS);
  1576. break;
  1577. /* case ADC_EOC_SINGLE_CONV */
  1578. default:
  1579. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOS);
  1580. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOC);
  1581. break;
  1582. }
  1583. }
  1584. /* Start ADC group regular conversion */
  1585. LL_ADC_REG_StartConversion(hadc->Instance);
  1586. }
  1587. else
  1588. {
  1589. /* ADC instance is a multimode slave instance with multimode regular conversions enabled */
  1590. SET_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  1591. /* if Master ADC JAUTO bit is set, Slave injected interruptions
  1592. are enabled nevertheless (for same reason as above) */
  1593. tmpADC_Master = __LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance);
  1594. if (READ_BIT(tmpADC_Master->CFGR, ADC_CFGR_JAUTO) != 0UL)
  1595. {
  1596. /* First, update Slave State in setting HAL_ADC_STATE_INJ_BUSY bit
  1597. and in resetting HAL_ADC_STATE_INJ_EOC bit */
  1598. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
  1599. /* Next, set Slave injected interruptions */
  1600. switch (hadc->Init.EOCSelection)
  1601. {
  1602. case ADC_EOC_SEQ_CONV:
  1603. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC);
  1604. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOS);
  1605. break;
  1606. /* case ADC_EOC_SINGLE_CONV */
  1607. default:
  1608. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOS);
  1609. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOC);
  1610. break;
  1611. }
  1612. }
  1613. }
  1614. #else
  1615. /* ADC instance is not a multimode slave instance with multimode regular conversions enabled */
  1616. if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_JAUTO) != 0UL)
  1617. {
  1618. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
  1619. /* Enable as well injected interruptions in case
  1620. HAL_ADCEx_InjectedStart_IT() has not been called beforehand. This
  1621. allows to start regular and injected conversions when JAUTO is
  1622. set with a single call to HAL_ADC_Start_IT() */
  1623. switch (hadc->Init.EOCSelection)
  1624. {
  1625. case ADC_EOC_SEQ_CONV:
  1626. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC);
  1627. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOS);
  1628. break;
  1629. /* case ADC_EOC_SINGLE_CONV */
  1630. default:
  1631. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOS);
  1632. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOC);
  1633. break;
  1634. }
  1635. }
  1636. /* Start ADC group regular conversion */
  1637. LL_ADC_REG_StartConversion(hadc->Instance);
  1638. #endif /* ADC_MULTIMODE_SUPPORT */
  1639. }
  1640. else
  1641. {
  1642. /* Process unlocked */
  1643. __HAL_UNLOCK(hadc);
  1644. }
  1645. }
  1646. else
  1647. {
  1648. tmp_hal_status = HAL_BUSY;
  1649. }
  1650. /* Return function status */
  1651. return tmp_hal_status;
  1652. }
  1653. /**
  1654. * @brief Stop ADC conversion of regular group (and injected group in
  1655. * case of auto_injection mode), disable interrution of
  1656. * end-of-conversion, disable ADC peripheral.
  1657. * @param hadc ADC handle
  1658. * @retval HAL status.
  1659. */
  1660. HAL_StatusTypeDef HAL_ADC_Stop_IT(ADC_HandleTypeDef *hadc)
  1661. {
  1662. HAL_StatusTypeDef tmp_hal_status;
  1663. /* Check the parameters */
  1664. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1665. /* Process locked */
  1666. __HAL_LOCK(hadc);
  1667. /* 1. Stop potential conversion on going, on ADC groups regular and injected */
  1668. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_INJECTED_GROUP);
  1669. /* Disable ADC peripheral if conversions are effectively stopped */
  1670. if (tmp_hal_status == HAL_OK)
  1671. {
  1672. /* Disable ADC end of conversion interrupt for regular group */
  1673. /* Disable ADC overrun interrupt */
  1674. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_EOS | ADC_IT_OVR));
  1675. /* 2. Disable the ADC peripheral */
  1676. tmp_hal_status = ADC_Disable(hadc);
  1677. /* Check if ADC is effectively disabled */
  1678. if (tmp_hal_status == HAL_OK)
  1679. {
  1680. /* Set ADC state */
  1681. ADC_STATE_CLR_SET(hadc->State,
  1682. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  1683. HAL_ADC_STATE_READY);
  1684. }
  1685. }
  1686. /* Process unlocked */
  1687. __HAL_UNLOCK(hadc);
  1688. /* Return function status */
  1689. return tmp_hal_status;
  1690. }
  1691. /**
  1692. * @brief Enable ADC, start conversion of regular group and transfer result through DMA.
  1693. * @note Interruptions enabled in this function:
  1694. * overrun (if applicable), DMA half transfer, DMA transfer complete.
  1695. * Each of these interruptions has its dedicated callback function.
  1696. * @note Case of multimode enabled (when multimode feature is available): HAL_ADC_Start_DMA()
  1697. * is designed for single-ADC mode only. For multimode, the dedicated
  1698. * HAL_ADCEx_MultiModeStart_DMA() function must be used.
  1699. * @param hadc ADC handle
  1700. * @param pData Destination Buffer address.
  1701. * @param Length Number of data to be transferred from ADC peripheral to memory
  1702. * @retval HAL status.
  1703. */
  1704. HAL_StatusTypeDef HAL_ADC_Start_DMA(ADC_HandleTypeDef *hadc, uint32_t *pData, uint32_t Length)
  1705. {
  1706. HAL_StatusTypeDef tmp_hal_status;
  1707. #if defined(ADC_MULTIMODE_SUPPORT)
  1708. uint32_t tmp_multimode_config = LL_ADC_GetMultimode(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
  1709. #endif /* ADC_MULTIMODE_SUPPORT */
  1710. uint32_t length_bytes;
  1711. DMA_NodeConfTypeDef node_conf;
  1712. /* Check the parameters */
  1713. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1714. /* Perform ADC enable and conversion start if no conversion is on going */
  1715. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
  1716. {
  1717. /* Process locked */
  1718. __HAL_LOCK(hadc);
  1719. #if defined(ADC_MULTIMODE_SUPPORT)
  1720. /* Ensure that multimode regular conversions are not enabled. */
  1721. /* Otherwise, dedicated API HAL_ADCEx_MultiModeStart_DMA() must be used. */
  1722. if ((tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
  1723. || (tmp_multimode_config == LL_ADC_MULTI_DUAL_INJ_SIMULT)
  1724. || (tmp_multimode_config == LL_ADC_MULTI_DUAL_INJ_ALTERN)
  1725. )
  1726. #endif /* ADC_MULTIMODE_SUPPORT */
  1727. {
  1728. /* Enable the ADC peripheral */
  1729. tmp_hal_status = ADC_Enable(hadc);
  1730. /* Start conversion if ADC is effectively enabled */
  1731. if (tmp_hal_status == HAL_OK)
  1732. {
  1733. /* Set ADC state */
  1734. /* - Clear state bitfield related to regular group conversion results */
  1735. /* - Set state bitfield related to regular operation */
  1736. ADC_STATE_CLR_SET(hadc->State,
  1737. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP,
  1738. HAL_ADC_STATE_REG_BUSY);
  1739. #if defined(ADC_MULTIMODE_SUPPORT)
  1740. /* Reset HAL_ADC_STATE_MULTIMODE_SLAVE bit
  1741. - if ADC instance is master or if multimode feature is not available
  1742. - if multimode setting is disabled (ADC instance slave in independent mode) */
  1743. if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
  1744. || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
  1745. )
  1746. {
  1747. CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  1748. }
  1749. #endif /* ADC_MULTIMODE_SUPPORT */
  1750. /* Check if a conversion is on going on ADC group injected */
  1751. if ((hadc->State & HAL_ADC_STATE_INJ_BUSY) != 0UL)
  1752. {
  1753. /* Reset ADC error code fields related to regular conversions only */
  1754. CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA));
  1755. }
  1756. else
  1757. {
  1758. /* Reset all ADC error code fields */
  1759. ADC_CLEAR_ERRORCODE(hadc);
  1760. }
  1761. /* Set the DMA transfer complete callback */
  1762. hadc->DMA_Handle->XferCpltCallback = ADC_DMAConvCplt;
  1763. /* Set the DMA half transfer complete callback */
  1764. hadc->DMA_Handle->XferHalfCpltCallback = ADC_DMAHalfConvCplt;
  1765. /* Set the DMA error callback */
  1766. hadc->DMA_Handle->XferErrorCallback = ADC_DMAError;
  1767. /* Manage ADC and DMA start: ADC overrun interruption, DMA start, */
  1768. /* ADC start (in case of SW start): */
  1769. /* Clear regular group conversion flag and overrun flag */
  1770. /* (To ensure of no unknown state from potential previous ADC */
  1771. /* operations) */
  1772. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
  1773. /* Process unlocked */
  1774. /* Unlock before starting ADC conversions: in case of potential */
  1775. /* interruption, to let the process to ADC IRQ Handler. */
  1776. __HAL_UNLOCK(hadc);
  1777. /* With DMA, overrun event is always considered as an error even if
  1778. hadc->Init.Overrun is set to ADC_OVR_DATA_OVERWRITTEN. Therefore,
  1779. ADC_IT_OVR is enabled. */
  1780. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);
  1781. /* Enable ADC DMA mode */
  1782. SET_BIT(hadc->Instance->CFGR, ADC_CFGR_DMAEN);
  1783. /* Check linkedlist mode */
  1784. if ((hadc->DMA_Handle->Mode & DMA_LINKEDLIST) == DMA_LINKEDLIST)
  1785. {
  1786. if ((hadc->DMA_Handle->LinkedListQueue != NULL) && (hadc->DMA_Handle->LinkedListQueue->Head != NULL))
  1787. {
  1788. /* Length should be converted to number of bytes */
  1789. if (HAL_DMAEx_List_GetNodeConfig(&node_conf, hadc->DMA_Handle->LinkedListQueue->Head) != HAL_OK)
  1790. {
  1791. return HAL_ERROR;
  1792. }
  1793. /* Length should be converted to number of bytes */
  1794. if (node_conf.Init.SrcDataWidth == DMA_SRC_DATAWIDTH_WORD)
  1795. {
  1796. /* Word -> Bytes */
  1797. length_bytes = Length * 4U;
  1798. }
  1799. else if (node_conf.Init.SrcDataWidth == DMA_SRC_DATAWIDTH_HALFWORD)
  1800. {
  1801. /* Halfword -> Bytes */
  1802. length_bytes = Length * 2U;
  1803. }
  1804. else /* Bytes */
  1805. {
  1806. /* Same size already expressed in Bytes */
  1807. length_bytes = Length;
  1808. }
  1809. hadc->DMA_Handle->LinkedListQueue->Head->LinkRegisters[NODE_CBR1_DEFAULT_OFFSET] = (uint32_t)length_bytes;
  1810. hadc->DMA_Handle->LinkedListQueue->Head->LinkRegisters[NODE_CSAR_DEFAULT_OFFSET] = \
  1811. (uint32_t)&hadc->Instance->DR;
  1812. hadc->DMA_Handle->LinkedListQueue->Head->LinkRegisters[NODE_CDAR_DEFAULT_OFFSET] = (uint32_t)pData;
  1813. tmp_hal_status = HAL_DMAEx_List_Start_IT(hadc->DMA_Handle);
  1814. }
  1815. else
  1816. {
  1817. return HAL_ERROR;
  1818. }
  1819. }
  1820. else
  1821. {
  1822. /* Length should be converted to number of bytes */
  1823. if (hadc->DMA_Handle->Init.SrcDataWidth == DMA_SRC_DATAWIDTH_WORD)
  1824. {
  1825. /* Word -> Bytes */
  1826. length_bytes = Length * 4U;
  1827. }
  1828. else if (hadc->DMA_Handle->Init.SrcDataWidth == DMA_SRC_DATAWIDTH_HALFWORD)
  1829. {
  1830. /* Halfword -> Bytes */
  1831. length_bytes = Length * 2U;
  1832. }
  1833. else /* Bytes */
  1834. {
  1835. /* Same size already expressed in Bytes */
  1836. length_bytes = Length;
  1837. }
  1838. /* Start the DMA channel */
  1839. tmp_hal_status = HAL_DMA_Start_IT(hadc->DMA_Handle, (uint32_t)&hadc->Instance->DR, (uint32_t)pData, \
  1840. length_bytes);
  1841. }
  1842. /* Enable conversion of regular group. */
  1843. /* If software start has been selected, conversion starts immediately. */
  1844. /* If external trigger has been selected, conversion will start at next */
  1845. /* trigger event. */
  1846. /* Start ADC group regular conversion */
  1847. LL_ADC_REG_StartConversion(hadc->Instance);
  1848. }
  1849. else
  1850. {
  1851. /* Process unlocked */
  1852. __HAL_UNLOCK(hadc);
  1853. }
  1854. }
  1855. #if defined(ADC_MULTIMODE_SUPPORT)
  1856. else
  1857. {
  1858. tmp_hal_status = HAL_ERROR;
  1859. /* Process unlocked */
  1860. __HAL_UNLOCK(hadc);
  1861. }
  1862. #endif /* ADC_MULTIMODE_SUPPORT */
  1863. }
  1864. else
  1865. {
  1866. tmp_hal_status = HAL_BUSY;
  1867. }
  1868. /* Return function status */
  1869. return tmp_hal_status;
  1870. }
  1871. /**
  1872. * @brief Stop ADC conversion of regular group (and injected group in
  1873. * case of auto_injection mode), disable ADC DMA transfer, disable
  1874. * ADC peripheral.
  1875. * @note: ADC peripheral disable is forcing stop of potential
  1876. * conversion on ADC group injected. If ADC group injected is under use, it
  1877. * should be preliminarily stopped using HAL_ADCEx_InjectedStop function.
  1878. * @note Case of multimode enabled (when multimode feature is available):
  1879. * HAL_ADC_Stop_DMA() function is dedicated to single-ADC mode only.
  1880. * For multimode, the dedicated HAL_ADCEx_MultiModeStop_DMA() API must be used.
  1881. * @param hadc ADC handle
  1882. * @retval HAL status.
  1883. */
  1884. HAL_StatusTypeDef HAL_ADC_Stop_DMA(ADC_HandleTypeDef *hadc)
  1885. {
  1886. HAL_StatusTypeDef tmp_hal_status;
  1887. /* Check the parameters */
  1888. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1889. /* Process locked */
  1890. __HAL_LOCK(hadc);
  1891. /* 1. Stop potential ADC group regular conversion on going */
  1892. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_INJECTED_GROUP);
  1893. /* Disable ADC peripheral if conversions are effectively stopped */
  1894. if (tmp_hal_status == HAL_OK)
  1895. {
  1896. /* Disable ADC DMA (ADC DMA configuration of continuous requests is kept) */
  1897. CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_DMAEN);
  1898. /* Disable the DMA channel (in case of DMA in circular mode or stop */
  1899. /* while DMA transfer is on going) */
  1900. if (hadc->DMA_Handle->State == HAL_DMA_STATE_BUSY)
  1901. {
  1902. tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
  1903. /* Check if DMA channel effectively disabled */
  1904. if (tmp_hal_status != HAL_OK)
  1905. {
  1906. /* Update ADC state machine to error */
  1907. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  1908. }
  1909. }
  1910. /* Disable ADC overrun interrupt */
  1911. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
  1912. /* 2. Disable the ADC peripheral */
  1913. /* Update "tmp_hal_status" only if DMA channel disabling passed, */
  1914. /* to keep in memory a potential failing status. */
  1915. if (tmp_hal_status == HAL_OK)
  1916. {
  1917. tmp_hal_status = ADC_Disable(hadc);
  1918. }
  1919. else
  1920. {
  1921. (void)ADC_Disable(hadc);
  1922. }
  1923. /* Check if ADC is effectively disabled */
  1924. if (tmp_hal_status == HAL_OK)
  1925. {
  1926. /* Set ADC state */
  1927. ADC_STATE_CLR_SET(hadc->State,
  1928. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  1929. HAL_ADC_STATE_READY);
  1930. }
  1931. }
  1932. /* Process unlocked */
  1933. __HAL_UNLOCK(hadc);
  1934. /* Return function status */
  1935. return tmp_hal_status;
  1936. }
  1937. /**
  1938. * @brief Get ADC regular group conversion result.
  1939. * @note Reading register DR automatically clears ADC flag EOC
  1940. * (ADC group regular end of unitary conversion).
  1941. * @note This function does not clear ADC flag EOS
  1942. * (ADC group regular end of sequence conversion).
  1943. * Occurrence of flag EOS rising:
  1944. * - If sequencer is composed of 1 rank, flag EOS is equivalent
  1945. * to flag EOC.
  1946. * - If sequencer is composed of several ranks, during the scan
  1947. * sequence flag EOC only is raised, at the end of the scan sequence
  1948. * both flags EOC and EOS are raised.
  1949. * To clear this flag, either use function:
  1950. * in programming model IT: @ref HAL_ADC_IRQHandler(), in programming
  1951. * model polling: @ref HAL_ADC_PollForConversion()
  1952. * or @ref __HAL_ADC_CLEAR_FLAG(&hadc, ADC_FLAG_EOS).
  1953. * @param hadc ADC handle
  1954. * @retval ADC group regular conversion data
  1955. */
  1956. uint32_t HAL_ADC_GetValue(const ADC_HandleTypeDef *hadc)
  1957. {
  1958. /* Check the parameters */
  1959. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1960. /* Note: EOC flag is not cleared here by software because automatically */
  1961. /* cleared by hardware when reading register DR. */
  1962. /* Return ADC converted value */
  1963. return hadc->Instance->DR;
  1964. }
  1965. /**
  1966. * @brief Start ADC conversion sampling phase of regular group
  1967. * @note: This function should only be called to start sampling when
  1968. * - @ref ADC_SAMPLING_MODE_TRIGGER_CONTROLED sampling
  1969. * mode has been selected
  1970. * - @ref ADC_SOFTWARE_START has been selected as trigger source
  1971. * @param hadc ADC handle
  1972. * @retval HAL status.
  1973. */
  1974. HAL_StatusTypeDef HAL_ADC_StartSampling(ADC_HandleTypeDef *hadc)
  1975. {
  1976. /* Check the parameters */
  1977. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1978. /* Start sampling */
  1979. SET_BIT(hadc->Instance->CFGR2, ADC_CFGR2_SWTRIG);
  1980. /* Return function status */
  1981. return HAL_OK;
  1982. }
  1983. /**
  1984. * @brief Stop ADC conversion sampling phase of regular group and start conversion
  1985. * @note: This function should only be called to stop sampling when
  1986. * - @ref ADC_SAMPLING_MODE_TRIGGER_CONTROLED sampling
  1987. * mode has been selected
  1988. * - @ref ADC_SOFTWARE_START has been selected as trigger source
  1989. * - after sampling has been started using @ref HAL_ADC_StartSampling.
  1990. * @param hadc ADC handle
  1991. * @retval HAL status.
  1992. */
  1993. HAL_StatusTypeDef HAL_ADC_StopSampling(ADC_HandleTypeDef *hadc)
  1994. {
  1995. /* Check the parameters */
  1996. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1997. /* Start sampling */
  1998. CLEAR_BIT(hadc->Instance->CFGR2, ADC_CFGR2_SWTRIG);
  1999. /* Return function status */
  2000. return HAL_OK;
  2001. }
  2002. /**
  2003. * @brief Handle ADC interrupt request.
  2004. * @param hadc ADC handle
  2005. * @retval None
  2006. */
  2007. void HAL_ADC_IRQHandler(ADC_HandleTypeDef *hadc)
  2008. {
  2009. uint32_t overrun_error = 0UL; /* flag set if overrun occurrence has to be considered as an error */
  2010. uint32_t tmp_isr = hadc->Instance->ISR;
  2011. uint32_t tmp_ier = hadc->Instance->IER;
  2012. uint32_t tmp_adc_inj_is_trigger_source_sw_start;
  2013. uint32_t tmp_adc_reg_is_trigger_source_sw_start;
  2014. uint32_t tmp_cfgr;
  2015. #if defined(ADC_MULTIMODE_SUPPORT)
  2016. const ADC_TypeDef *tmpADC_Master;
  2017. uint32_t tmp_multimode_config = LL_ADC_GetMultimode(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
  2018. #endif /* ADC_MULTIMODE_SUPPORT */
  2019. /* Check the parameters */
  2020. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2021. assert_param(IS_ADC_EOC_SELECTION(hadc->Init.EOCSelection));
  2022. /* ========== Check End of Sampling flag for ADC group regular ========== */
  2023. if (((tmp_isr & ADC_FLAG_EOSMP) == ADC_FLAG_EOSMP) && ((tmp_ier & ADC_IT_EOSMP) == ADC_IT_EOSMP))
  2024. {
  2025. /* Update state machine on end of sampling status if not in error state */
  2026. if ((hadc->State & HAL_ADC_STATE_ERROR_INTERNAL) == 0UL)
  2027. {
  2028. /* Set ADC state */
  2029. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOSMP);
  2030. }
  2031. /* End Of Sampling callback */
  2032. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2033. hadc->EndOfSamplingCallback(hadc);
  2034. #else
  2035. HAL_ADCEx_EndOfSamplingCallback(hadc);
  2036. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2037. /* Clear regular group conversion flag */
  2038. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOSMP);
  2039. }
  2040. /* ====== Check ADC group regular end of unitary conversion sequence conversions ===== */
  2041. if ((((tmp_isr & ADC_FLAG_EOC) == ADC_FLAG_EOC) && ((tmp_ier & ADC_IT_EOC) == ADC_IT_EOC)) ||
  2042. (((tmp_isr & ADC_FLAG_EOS) == ADC_FLAG_EOS) && ((tmp_ier & ADC_IT_EOS) == ADC_IT_EOS)))
  2043. {
  2044. /* Update state machine on conversion status if not in error state */
  2045. if ((hadc->State & HAL_ADC_STATE_ERROR_INTERNAL) == 0UL)
  2046. {
  2047. /* Set ADC state */
  2048. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  2049. }
  2050. /* Determine whether any further conversion upcoming on group regular */
  2051. /* by external trigger, continuous mode or scan sequence on going */
  2052. /* to disable interruption. */
  2053. if (LL_ADC_REG_IsTriggerSourceSWStart(hadc->Instance) != 0UL)
  2054. {
  2055. /* Get relevant register CFGR in ADC instance of ADC master or slave */
  2056. /* in function of multimode state (for devices with multimode */
  2057. /* available). */
  2058. #if defined(ADC_MULTIMODE_SUPPORT)
  2059. if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
  2060. || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
  2061. || (tmp_multimode_config == LL_ADC_MULTI_DUAL_INJ_SIMULT)
  2062. || (tmp_multimode_config == LL_ADC_MULTI_DUAL_INJ_ALTERN)
  2063. )
  2064. {
  2065. /* check CONT bit directly in handle ADC CFGR register */
  2066. tmp_cfgr = READ_REG(hadc->Instance->CFGR);
  2067. }
  2068. else
  2069. {
  2070. /* else need to check Master ADC CONT bit */
  2071. tmpADC_Master = __LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance);
  2072. tmp_cfgr = READ_REG(tmpADC_Master->CFGR);
  2073. }
  2074. #else
  2075. tmp_cfgr = READ_REG(hadc->Instance->CFGR);
  2076. #endif /* ADC_MULTIMODE_SUPPORT */
  2077. /* Carry on if continuous mode is disabled */
  2078. if (READ_BIT(tmp_cfgr, ADC_CFGR_CONT) != ADC_CFGR_CONT)
  2079. {
  2080. /* If End of Sequence is reached, disable interrupts */
  2081. if (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOS))
  2082. {
  2083. /* Allowed to modify bits ADC_IT_EOC/ADC_IT_EOS only if bit */
  2084. /* ADSTART==0 (no conversion on going) */
  2085. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
  2086. {
  2087. /* Disable ADC end of sequence conversion interrupt */
  2088. /* Note: Overrun interrupt was enabled with EOC interrupt in */
  2089. /* HAL_Start_IT(), but is not disabled here because can be used */
  2090. /* by overrun IRQ process below. */
  2091. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC | ADC_IT_EOS);
  2092. /* Set ADC state */
  2093. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  2094. if ((hadc->State & HAL_ADC_STATE_INJ_BUSY) == 0UL)
  2095. {
  2096. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  2097. }
  2098. }
  2099. else
  2100. {
  2101. /* Change ADC state to error state */
  2102. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2103. /* Set ADC error code to ADC peripheral internal error */
  2104. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2105. }
  2106. }
  2107. }
  2108. }
  2109. /* Conversion complete callback */
  2110. /* Note: Into callback function "HAL_ADC_ConvCpltCallback()", */
  2111. /* to determine if conversion has been triggered from EOC or EOS, */
  2112. /* possibility to use: */
  2113. /* " if ( __HAL_ADC_GET_FLAG(&hadc, ADC_FLAG_EOS)) " */
  2114. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2115. hadc->ConvCpltCallback(hadc);
  2116. #else
  2117. HAL_ADC_ConvCpltCallback(hadc);
  2118. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2119. /* Clear regular group conversion flag */
  2120. /* Note: in case of overrun set to ADC_OVR_DATA_PRESERVED, end of */
  2121. /* conversion flags clear induces the release of the preserved data.*/
  2122. /* Therefore, if the preserved data value is needed, it must be */
  2123. /* read preliminarily into HAL_ADC_ConvCpltCallback(). */
  2124. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS));
  2125. }
  2126. /* ====== Check ADC group injected end of unitary conversion sequence conversions ===== */
  2127. if ((((tmp_isr & ADC_FLAG_JEOC) == ADC_FLAG_JEOC) && ((tmp_ier & ADC_IT_JEOC) == ADC_IT_JEOC)) ||
  2128. (((tmp_isr & ADC_FLAG_JEOS) == ADC_FLAG_JEOS) && ((tmp_ier & ADC_IT_JEOS) == ADC_IT_JEOS)))
  2129. {
  2130. /* Update state machine on conversion status if not in error state */
  2131. if ((hadc->State & HAL_ADC_STATE_ERROR_INTERNAL) == 0UL)
  2132. {
  2133. /* Set ADC state */
  2134. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_EOC);
  2135. }
  2136. /* Retrieve ADC configuration */
  2137. tmp_adc_inj_is_trigger_source_sw_start = LL_ADC_INJ_IsTriggerSourceSWStart(hadc->Instance);
  2138. tmp_adc_reg_is_trigger_source_sw_start = LL_ADC_REG_IsTriggerSourceSWStart(hadc->Instance);
  2139. /* Get relevant register CFGR in ADC instance of ADC master or slave */
  2140. /* in function of multimode state (for devices with multimode */
  2141. /* available). */
  2142. #if defined(ADC_MULTIMODE_SUPPORT)
  2143. if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
  2144. || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
  2145. || (tmp_multimode_config == LL_ADC_MULTI_DUAL_REG_SIMULT)
  2146. || (tmp_multimode_config == LL_ADC_MULTI_DUAL_REG_INTERL)
  2147. )
  2148. {
  2149. tmp_cfgr = READ_REG(hadc->Instance->CFGR);
  2150. }
  2151. else
  2152. {
  2153. tmpADC_Master = __LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance);
  2154. tmp_cfgr = READ_REG(tmpADC_Master->CFGR);
  2155. }
  2156. #else
  2157. tmp_cfgr = READ_REG(hadc->Instance->CFGR);
  2158. #endif /* ADC_MULTIMODE_SUPPORT */
  2159. /* Disable interruption if no further conversion upcoming by injected */
  2160. /* external trigger or by automatic injected conversion with regular */
  2161. /* group having no further conversion upcoming (same conditions as */
  2162. /* regular group interruption disabling above), */
  2163. /* and if injected scan sequence is completed. */
  2164. if (tmp_adc_inj_is_trigger_source_sw_start != 0UL)
  2165. {
  2166. if ((READ_BIT(tmp_cfgr, ADC_CFGR_JAUTO) == 0UL) ||
  2167. ((tmp_adc_reg_is_trigger_source_sw_start != 0UL) &&
  2168. (READ_BIT(tmp_cfgr, ADC_CFGR_CONT) == 0UL)))
  2169. {
  2170. /* If End of Sequence is reached, disable interrupts */
  2171. if (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_JEOS))
  2172. {
  2173. /* Particular case if injected contexts queue is enabled: */
  2174. /* when the last context has been fully processed, JSQR is reset */
  2175. /* by the hardware. Even if no injected conversion is planned to come */
  2176. /* (queue empty, triggers are ignored), it can start again */
  2177. /* immediately after setting a new context (JADSTART is still set). */
  2178. /* Therefore, state of HAL ADC injected group is kept to busy. */
  2179. if (READ_BIT(tmp_cfgr, ADC_CFGR_JQM) == 0UL)
  2180. {
  2181. /* Allowed to modify bits ADC_IT_JEOC/ADC_IT_JEOS only if bit */
  2182. /* JADSTART==0 (no conversion on going) */
  2183. if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) == 0UL)
  2184. {
  2185. /* Disable ADC end of sequence conversion interrupt */
  2186. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC | ADC_IT_JEOS);
  2187. /* Set ADC state */
  2188. CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  2189. if ((hadc->State & HAL_ADC_STATE_REG_BUSY) == 0UL)
  2190. {
  2191. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  2192. }
  2193. }
  2194. else
  2195. {
  2196. /* Update ADC state machine to error */
  2197. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2198. /* Set ADC error code to ADC peripheral internal error */
  2199. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2200. }
  2201. }
  2202. }
  2203. }
  2204. }
  2205. /* Injected Conversion complete callback */
  2206. /* Note: HAL_ADCEx_InjectedConvCpltCallback can resort to
  2207. if (__HAL_ADC_GET_FLAG(&hadc, ADC_FLAG_JEOS)) or
  2208. if (__HAL_ADC_GET_FLAG(&hadc, ADC_FLAG_JEOC)) to determine whether
  2209. interruption has been triggered by end of conversion or end of
  2210. sequence. */
  2211. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2212. hadc->InjectedConvCpltCallback(hadc);
  2213. #else
  2214. HAL_ADCEx_InjectedConvCpltCallback(hadc);
  2215. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2216. /* Clear injected group conversion flag */
  2217. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOC | ADC_FLAG_JEOS);
  2218. }
  2219. /* ========== Check Analog watchdog 1 flag ========== */
  2220. if (((tmp_isr & ADC_FLAG_AWD1) == ADC_FLAG_AWD1) && ((tmp_ier & ADC_IT_AWD1) == ADC_IT_AWD1))
  2221. {
  2222. /* Set ADC state */
  2223. SET_BIT(hadc->State, HAL_ADC_STATE_AWD1);
  2224. /* Level out of window 1 callback */
  2225. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2226. hadc->LevelOutOfWindowCallback(hadc);
  2227. #else
  2228. HAL_ADC_LevelOutOfWindowCallback(hadc);
  2229. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2230. /* Clear ADC analog watchdog flag */
  2231. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD1);
  2232. }
  2233. /* ========== Check analog watchdog 2 flag ========== */
  2234. if (((tmp_isr & ADC_FLAG_AWD2) == ADC_FLAG_AWD2) && ((tmp_ier & ADC_IT_AWD2) == ADC_IT_AWD2))
  2235. {
  2236. /* Set ADC state */
  2237. SET_BIT(hadc->State, HAL_ADC_STATE_AWD2);
  2238. /* Level out of window 2 callback */
  2239. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2240. hadc->LevelOutOfWindow2Callback(hadc);
  2241. #else
  2242. HAL_ADCEx_LevelOutOfWindow2Callback(hadc);
  2243. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2244. /* Clear ADC analog watchdog flag */
  2245. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD2);
  2246. }
  2247. /* ========== Check analog watchdog 3 flag ========== */
  2248. if (((tmp_isr & ADC_FLAG_AWD3) == ADC_FLAG_AWD3) && ((tmp_ier & ADC_IT_AWD3) == ADC_IT_AWD3))
  2249. {
  2250. /* Set ADC state */
  2251. SET_BIT(hadc->State, HAL_ADC_STATE_AWD3);
  2252. /* Level out of window 3 callback */
  2253. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2254. hadc->LevelOutOfWindow3Callback(hadc);
  2255. #else
  2256. HAL_ADCEx_LevelOutOfWindow3Callback(hadc);
  2257. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2258. /* Clear ADC analog watchdog flag */
  2259. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD3);
  2260. }
  2261. /* ========== Check Overrun flag ========== */
  2262. if (((tmp_isr & ADC_FLAG_OVR) == ADC_FLAG_OVR) && ((tmp_ier & ADC_IT_OVR) == ADC_IT_OVR))
  2263. {
  2264. /* If overrun is set to overwrite previous data (default setting), */
  2265. /* overrun event is not considered as an error. */
  2266. /* (cf ref manual "Managing conversions without using the DMA and without */
  2267. /* overrun ") */
  2268. /* Exception for usage with DMA overrun event always considered as an */
  2269. /* error. */
  2270. if (hadc->Init.Overrun == ADC_OVR_DATA_PRESERVED)
  2271. {
  2272. overrun_error = 1UL;
  2273. }
  2274. else
  2275. {
  2276. /* Check DMA configuration */
  2277. #if defined(ADC_MULTIMODE_SUPPORT)
  2278. if (tmp_multimode_config != LL_ADC_MULTI_INDEPENDENT)
  2279. {
  2280. /* Multimode (when feature is available) is enabled,
  2281. Common Control Register MDMA bits must be checked. */
  2282. if (LL_ADC_GetMultiDMATransfer(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) != LL_ADC_MULTI_REG_DMA_EACH_ADC)
  2283. {
  2284. overrun_error = 1UL;
  2285. }
  2286. }
  2287. else
  2288. #endif /* ADC_MULTIMODE_SUPPORT */
  2289. {
  2290. /* Multimode not set or feature not available or ADC independent */
  2291. if ((hadc->Instance->CFGR & ADC_CFGR_DMAEN) != 0UL)
  2292. {
  2293. overrun_error = 1UL;
  2294. }
  2295. }
  2296. }
  2297. if (overrun_error == 1UL)
  2298. {
  2299. /* Change ADC state to error state */
  2300. SET_BIT(hadc->State, HAL_ADC_STATE_REG_OVR);
  2301. /* Set ADC error code to overrun */
  2302. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_OVR);
  2303. /* Error callback */
  2304. /* Note: In case of overrun, ADC conversion data is preserved until */
  2305. /* flag OVR is reset. */
  2306. /* Therefore, old ADC conversion data can be retrieved in */
  2307. /* function "HAL_ADC_ErrorCallback()". */
  2308. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2309. hadc->ErrorCallback(hadc);
  2310. #else
  2311. HAL_ADC_ErrorCallback(hadc);
  2312. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2313. }
  2314. /* Clear ADC overrun flag */
  2315. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  2316. }
  2317. /* ========== Check Injected context queue overflow flag ========== */
  2318. if (((tmp_isr & ADC_FLAG_JQOVF) == ADC_FLAG_JQOVF) && ((tmp_ier & ADC_IT_JQOVF) == ADC_IT_JQOVF))
  2319. {
  2320. /* Change ADC state to overrun state */
  2321. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_JQOVF);
  2322. /* Set ADC error code to Injected context queue overflow */
  2323. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_JQOVF);
  2324. /* Clear the Injected context queue overflow flag */
  2325. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JQOVF);
  2326. /* Injected context queue overflow callback */
  2327. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2328. hadc->InjectedQueueOverflowCallback(hadc);
  2329. #else
  2330. HAL_ADCEx_InjectedQueueOverflowCallback(hadc);
  2331. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2332. }
  2333. }
  2334. /**
  2335. * @brief Conversion complete callback in non-blocking mode.
  2336. * @param hadc ADC handle
  2337. * @retval None
  2338. */
  2339. __weak void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
  2340. {
  2341. /* Prevent unused argument(s) compilation warning */
  2342. UNUSED(hadc);
  2343. /* NOTE : This function should not be modified. When the callback is needed,
  2344. function HAL_ADC_ConvCpltCallback must be implemented in the user file.
  2345. */
  2346. }
  2347. /**
  2348. * @brief Conversion DMA half-transfer callback in non-blocking mode.
  2349. * @param hadc ADC handle
  2350. * @retval None
  2351. */
  2352. __weak void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef *hadc)
  2353. {
  2354. /* Prevent unused argument(s) compilation warning */
  2355. UNUSED(hadc);
  2356. /* NOTE : This function should not be modified. When the callback is needed,
  2357. function HAL_ADC_ConvHalfCpltCallback must be implemented in the user file.
  2358. */
  2359. }
  2360. /**
  2361. * @brief Analog watchdog 1 callback in non-blocking mode.
  2362. * @param hadc ADC handle
  2363. * @retval None
  2364. */
  2365. __weak void HAL_ADC_LevelOutOfWindowCallback(ADC_HandleTypeDef *hadc)
  2366. {
  2367. /* Prevent unused argument(s) compilation warning */
  2368. UNUSED(hadc);
  2369. /* NOTE : This function should not be modified. When the callback is needed,
  2370. function HAL_ADC_LevelOutOfWindowCallback must be implemented in the user file.
  2371. */
  2372. }
  2373. /**
  2374. * @brief ADC error callback in non-blocking mode
  2375. * (ADC conversion with interruption or transfer by DMA).
  2376. * @note In case of error due to overrun when using ADC with DMA transfer
  2377. * (HAL ADC handle parameter "ErrorCode" to state "HAL_ADC_ERROR_OVR"):
  2378. * - Reinitialize the DMA using function "HAL_ADC_Stop_DMA()".
  2379. * - If needed, restart a new ADC conversion using function
  2380. * "HAL_ADC_Start_DMA()"
  2381. * (this function is also clearing overrun flag)
  2382. * @param hadc ADC handle
  2383. * @retval None
  2384. */
  2385. __weak void HAL_ADC_ErrorCallback(ADC_HandleTypeDef *hadc)
  2386. {
  2387. /* Prevent unused argument(s) compilation warning */
  2388. UNUSED(hadc);
  2389. /* NOTE : This function should not be modified. When the callback is needed,
  2390. function HAL_ADC_ErrorCallback must be implemented in the user file.
  2391. */
  2392. }
  2393. /**
  2394. * @}
  2395. */
  2396. /** @defgroup ADC_Exported_Functions_Group3 Peripheral Control functions
  2397. * @brief Peripheral Control functions
  2398. *
  2399. @verbatim
  2400. ===============================================================================
  2401. ##### Peripheral Control functions #####
  2402. ===============================================================================
  2403. [..] This section provides functions allowing to:
  2404. (+) Configure channels on regular group
  2405. (+) Configure the analog watchdog
  2406. @endverbatim
  2407. * @{
  2408. */
  2409. /**
  2410. * @brief Configure a channel to be assigned to ADC group regular.
  2411. * @note In case of usage of internal measurement channels:
  2412. * Vbat/VrefInt/TempSensor.
  2413. * These internal paths can be disabled using function
  2414. * HAL_ADC_DeInit().
  2415. * @note Possibility to update parameters on the fly:
  2416. * This function initializes channel into ADC group regular,
  2417. * following calls to this function can be used to reconfigure
  2418. * some parameters of structure "ADC_ChannelConfTypeDef" on the fly,
  2419. * without resetting the ADC.
  2420. * The setting of these parameters is conditioned to ADC state:
  2421. * Refer to comments of structure "ADC_ChannelConfTypeDef".
  2422. * @param hadc ADC handle
  2423. * @param pConfig Structure of ADC channel assigned to ADC group regular.
  2424. * @retval HAL status
  2425. */
  2426. HAL_StatusTypeDef HAL_ADC_ConfigChannel(ADC_HandleTypeDef *hadc, const ADC_ChannelConfTypeDef *pConfig)
  2427. {
  2428. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  2429. uint32_t tmpOffsetShifted;
  2430. uint32_t tmp_config_internal_channel;
  2431. __IO uint32_t wait_loop_index = 0UL;
  2432. uint32_t tmp_adc_is_conversion_on_going_regular;
  2433. uint32_t tmp_adc_is_conversion_on_going_injected;
  2434. /* Check the parameters */
  2435. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2436. assert_param(IS_ADC_REGULAR_RANK(pConfig->Rank));
  2437. assert_param(IS_ADC_SAMPLE_TIME(pConfig->SamplingTime));
  2438. assert_param(IS_ADC_SINGLE_DIFFERENTIAL(pConfig->SingleDiff));
  2439. assert_param(IS_ADC_OFFSET_NUMBER(pConfig->OffsetNumber));
  2440. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), pConfig->Offset));
  2441. /* if ROVSE is set, the value of the OFFSETy_EN bit in ADCx_OFRy register is
  2442. ignored (considered as reset) */
  2443. assert_param(!((pConfig->OffsetNumber != ADC_OFFSET_NONE) && (hadc->Init.OversamplingMode == ENABLE)));
  2444. /* Verification of channel number */
  2445. if (pConfig->SingleDiff != ADC_DIFFERENTIAL_ENDED)
  2446. {
  2447. assert_param(IS_ADC_CHANNEL(hadc, pConfig->Channel));
  2448. }
  2449. else
  2450. {
  2451. assert_param(IS_ADC_DIFF_CHANNEL(hadc, pConfig->Channel));
  2452. }
  2453. /* Process locked */
  2454. __HAL_LOCK(hadc);
  2455. /* Parameters update conditioned to ADC state: */
  2456. /* Parameters that can be updated when ADC is disabled or enabled without */
  2457. /* conversion on going on regular group: */
  2458. /* - Channel number */
  2459. /* - Channel rank */
  2460. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
  2461. {
  2462. if ((pConfig->Channel == ADC_CHANNEL_0)
  2463. || ((pConfig->Channel == ADC_CHANNEL_1) && (pConfig->SingleDiff == ADC_DIFFERENTIAL_ENDED)))
  2464. {
  2465. LL_ADC_EnableChannel0_GPIO(hadc->Instance);
  2466. }
  2467. /* Set ADC group regular sequence: channel on the selected scan sequence rank */
  2468. LL_ADC_REG_SetSequencerRanks(hadc->Instance, pConfig->Rank, pConfig->Channel);
  2469. /* Parameters update conditioned to ADC state: */
  2470. /* Parameters that can be updated when ADC is disabled or enabled without */
  2471. /* conversion on going on regular group: */
  2472. /* - Channel sampling time */
  2473. /* - Channel offset */
  2474. tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
  2475. tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
  2476. if ((tmp_adc_is_conversion_on_going_regular == 0UL)
  2477. && (tmp_adc_is_conversion_on_going_injected == 0UL)
  2478. )
  2479. {
  2480. /* Manage specific case of sampling time 3.5 cycles replacing 2.5 cyles */
  2481. if (pConfig->SamplingTime == ADC_SAMPLETIME_3CYCLES_5)
  2482. {
  2483. /* Set sampling time of the selected ADC channel */
  2484. LL_ADC_SetChannelSamplingTime(hadc->Instance, pConfig->Channel, LL_ADC_SAMPLINGTIME_2CYCLES_5);
  2485. /* Set ADC sampling time common configuration */
  2486. LL_ADC_SetSamplingTimeCommonConfig(hadc->Instance, LL_ADC_SAMPLINGTIME_COMMON_3C5_REPL_2C5);
  2487. }
  2488. else
  2489. {
  2490. /* Set sampling time of the selected ADC channel */
  2491. LL_ADC_SetChannelSamplingTime(hadc->Instance, pConfig->Channel, pConfig->SamplingTime);
  2492. /* Set ADC sampling time common configuration */
  2493. LL_ADC_SetSamplingTimeCommonConfig(hadc->Instance, LL_ADC_SAMPLINGTIME_COMMON_DEFAULT);
  2494. }
  2495. /* Configure the offset: offset enable/disable, channel, offset value */
  2496. /* Shift the offset with respect to the selected ADC resolution. */
  2497. /* Offset has to be left-aligned on bit 11, the LSB (right bits) are set to 0 */
  2498. tmpOffsetShifted = ADC_OFFSET_SHIFT_RESOLUTION(hadc, (uint32_t)pConfig->Offset);
  2499. if (pConfig->OffsetNumber != ADC_OFFSET_NONE)
  2500. {
  2501. /* Set ADC selected offset number */
  2502. LL_ADC_SetOffset(hadc->Instance, pConfig->OffsetNumber, pConfig->Channel, tmpOffsetShifted);
  2503. assert_param(IS_ADC_OFFSET_SIGN(pConfig->OffsetSign));
  2504. assert_param(IS_FUNCTIONAL_STATE(pConfig->OffsetSaturation));
  2505. /* Set ADC selected offset sign & saturation */
  2506. LL_ADC_SetOffsetSign(hadc->Instance, pConfig->OffsetNumber, pConfig->OffsetSign);
  2507. LL_ADC_SetOffsetSaturation(hadc->Instance, pConfig->OffsetNumber,
  2508. (pConfig->OffsetSaturation == ENABLE) ?
  2509. LL_ADC_OFFSET_SATURATION_ENABLE : LL_ADC_OFFSET_SATURATION_DISABLE);
  2510. }
  2511. else
  2512. {
  2513. /* Scan each offset register to check if the selected channel is targeted. */
  2514. /* If this is the case, the corresponding offset number is disabled. */
  2515. if (__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_1))
  2516. == __LL_ADC_CHANNEL_TO_DECIMAL_NB(pConfig->Channel))
  2517. {
  2518. LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_1, LL_ADC_OFFSET_DISABLE);
  2519. }
  2520. if (__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_2))
  2521. == __LL_ADC_CHANNEL_TO_DECIMAL_NB(pConfig->Channel))
  2522. {
  2523. LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_2, LL_ADC_OFFSET_DISABLE);
  2524. }
  2525. if (__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_3))
  2526. == __LL_ADC_CHANNEL_TO_DECIMAL_NB(pConfig->Channel))
  2527. {
  2528. LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_3, LL_ADC_OFFSET_DISABLE);
  2529. }
  2530. if (__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_4))
  2531. == __LL_ADC_CHANNEL_TO_DECIMAL_NB(pConfig->Channel))
  2532. {
  2533. LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_4, LL_ADC_OFFSET_DISABLE);
  2534. }
  2535. }
  2536. }
  2537. /* Parameters update conditioned to ADC state: */
  2538. /* Parameters that can be updated only when ADC is disabled: */
  2539. /* - Single or differential mode */
  2540. if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
  2541. {
  2542. /* Set mode single-ended or differential input of the selected ADC channel */
  2543. LL_ADC_SetChannelSingleDiff(hadc->Instance, pConfig->Channel, pConfig->SingleDiff);
  2544. /* Configuration of differential mode */
  2545. if (pConfig->SingleDiff == ADC_DIFFERENTIAL_ENDED)
  2546. {
  2547. /* Set sampling time of the selected ADC channel */
  2548. /* Note: ADC channel number masked with value "0x1F" to ensure shift value within 32 bits range */
  2549. LL_ADC_SetChannelSamplingTime(hadc->Instance,
  2550. (uint32_t)(__LL_ADC_DECIMAL_NB_TO_CHANNEL(
  2551. (__LL_ADC_CHANNEL_TO_DECIMAL_NB((uint32_t)pConfig->Channel)
  2552. + 1UL) & 0x1FUL)),
  2553. pConfig->SamplingTime);
  2554. }
  2555. }
  2556. /* Management of internal measurement channels: Vbat/VrefInt/TempSensor. */
  2557. /* If internal channel selected, enable dedicated internal buffers and */
  2558. /* paths. */
  2559. /* Note: these internal measurement paths can be disabled using */
  2560. /* HAL_ADC_DeInit(). */
  2561. if (__LL_ADC_IS_CHANNEL_INTERNAL(pConfig->Channel))
  2562. {
  2563. tmp_config_internal_channel = LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
  2564. /* If the requested internal measurement path has already been enabled, */
  2565. /* bypass the configuration processing. */
  2566. if ((pConfig->Channel == ADC_CHANNEL_TEMPSENSOR)
  2567. && ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_TEMPSENSOR) == 0UL))
  2568. {
  2569. if (ADC_TEMPERATURE_SENSOR_INSTANCE(hadc))
  2570. {
  2571. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance),
  2572. LL_ADC_PATH_INTERNAL_TEMPSENSOR | tmp_config_internal_channel);
  2573. /* Delay for temperature sensor stabilization time */
  2574. /* Wait loop initialization and execution */
  2575. /* Note: Variable divided by 2 to compensate partially */
  2576. /* CPU processing cycles, scaling in us split to not */
  2577. /* exceed 32 bits register capacity and handle low frequency. */
  2578. wait_loop_index = ((LL_ADC_DELAY_TEMPSENSOR_STAB_US / 10UL) * ((SystemCoreClock / (100000UL * 2UL)) + 1UL));
  2579. while (wait_loop_index != 0UL)
  2580. {
  2581. wait_loop_index--;
  2582. }
  2583. }
  2584. }
  2585. else if ((pConfig->Channel == ADC_CHANNEL_VBAT)
  2586. && ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_VBAT) == 0UL))
  2587. {
  2588. if (ADC_BATTERY_VOLTAGE_INSTANCE(hadc))
  2589. {
  2590. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance),
  2591. LL_ADC_PATH_INTERNAL_VBAT | tmp_config_internal_channel);
  2592. }
  2593. }
  2594. else if ((pConfig->Channel == ADC_CHANNEL_VREFINT)
  2595. && ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_VREFINT) == 0UL))
  2596. {
  2597. if (ADC_VREFINT_INSTANCE(hadc))
  2598. {
  2599. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance),
  2600. LL_ADC_PATH_INTERNAL_VREFINT | tmp_config_internal_channel);
  2601. }
  2602. }
  2603. else if (pConfig->Channel == ADC_CHANNEL_VDDCORE)
  2604. {
  2605. if (ADC_VDDCORE_INSTANCE(hadc))
  2606. {
  2607. LL_ADC_EnableChannelVDDcore(hadc->Instance);
  2608. }
  2609. }
  2610. else
  2611. {
  2612. /* nothing to do */
  2613. }
  2614. }
  2615. }
  2616. /* If a conversion is on going on regular group, no update on regular */
  2617. /* channel could be done on neither of the channel configuration structure */
  2618. /* parameters. */
  2619. else
  2620. {
  2621. /* Update ADC state machine to error */
  2622. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  2623. tmp_hal_status = HAL_ERROR;
  2624. }
  2625. /* Process unlocked */
  2626. __HAL_UNLOCK(hadc);
  2627. /* Return function status */
  2628. return tmp_hal_status;
  2629. }
  2630. /**
  2631. * @brief Configure the analog watchdog.
  2632. * @note Possibility to update parameters on the fly:
  2633. * This function initializes the selected analog watchdog, successive
  2634. * calls to this function can be used to reconfigure some parameters
  2635. * of structure "ADC_AnalogWDGConfTypeDef" on the fly, without resetting
  2636. * the ADC.
  2637. * The setting of these parameters is conditioned to ADC state.
  2638. * For parameters constraints, see comments of structure
  2639. * "ADC_AnalogWDGConfTypeDef".
  2640. * @note On this STM32 series, analog watchdog thresholds can be modified
  2641. * while ADC conversion is on going.
  2642. * In this case, some constraints must be taken into account:
  2643. * the programmed threshold values are effective from the next
  2644. * ADC EOC (end of unitary conversion).
  2645. * Considering that registers write delay may happen due to
  2646. * bus activity, this might cause an uncertainty on the
  2647. * effective timing of the new programmed threshold values.
  2648. * @param hadc ADC handle
  2649. * @param pAnalogWDGConfig Structure of ADC analog watchdog configuration
  2650. * @retval HAL status
  2651. */
  2652. HAL_StatusTypeDef HAL_ADC_AnalogWDGConfig(ADC_HandleTypeDef *hadc, const ADC_AnalogWDGConfTypeDef *pAnalogWDGConfig)
  2653. {
  2654. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  2655. uint32_t tmp_awd_high_threshold_shifted;
  2656. uint32_t tmp_awd_low_threshold_shifted;
  2657. uint32_t tmp_adc_is_conversion_on_going_regular;
  2658. uint32_t tmp_adc_is_conversion_on_going_injected;
  2659. /* Check the parameters */
  2660. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2661. assert_param(IS_ADC_ANALOG_WATCHDOG_NUMBER(pAnalogWDGConfig->WatchdogNumber));
  2662. assert_param(IS_ADC_ANALOG_WATCHDOG_MODE(pAnalogWDGConfig->WatchdogMode));
  2663. assert_param(IS_ADC_ANALOG_WATCHDOG_FILTERING_MODE(pAnalogWDGConfig->FilteringConfig));
  2664. assert_param(IS_FUNCTIONAL_STATE(pAnalogWDGConfig->ITMode));
  2665. if ((pAnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_REG) ||
  2666. (pAnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_INJEC) ||
  2667. (pAnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_REGINJEC))
  2668. {
  2669. assert_param(IS_ADC_CHANNEL(hadc, pAnalogWDGConfig->Channel));
  2670. }
  2671. /* Verify thresholds range */
  2672. if (hadc->Init.OversamplingMode == ENABLE)
  2673. {
  2674. /* Case of oversampling enabled: depending on ratio and shift configuration,
  2675. analog watchdog thresholds can be higher than ADC resolution.
  2676. Verify if thresholds are within maximum thresholds range. */
  2677. assert_param(IS_ADC_RANGE(ADC_RESOLUTION_12B, pAnalogWDGConfig->HighThreshold));
  2678. assert_param(IS_ADC_RANGE(ADC_RESOLUTION_12B, pAnalogWDGConfig->LowThreshold));
  2679. }
  2680. else
  2681. {
  2682. /* Verify if thresholds are within the selected ADC resolution */
  2683. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), pAnalogWDGConfig->HighThreshold));
  2684. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), pAnalogWDGConfig->LowThreshold));
  2685. }
  2686. /* Process locked */
  2687. __HAL_LOCK(hadc);
  2688. /* Parameters update conditioned to ADC state: */
  2689. /* Parameters that can be updated when ADC is disabled or enabled without */
  2690. /* conversion on going on ADC groups regular and injected: */
  2691. /* - Analog watchdog channels */
  2692. tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
  2693. tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
  2694. if ((tmp_adc_is_conversion_on_going_regular == 0UL)
  2695. && (tmp_adc_is_conversion_on_going_injected == 0UL)
  2696. )
  2697. {
  2698. /* Analog watchdog configuration */
  2699. if (pAnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_1)
  2700. {
  2701. /* Configuration of analog watchdog: */
  2702. /* - Set the analog watchdog enable mode: one or overall group of */
  2703. /* channels, on groups regular and-or injected. */
  2704. switch (pAnalogWDGConfig->WatchdogMode)
  2705. {
  2706. case ADC_ANALOGWATCHDOG_SINGLE_REG:
  2707. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1,
  2708. __LL_ADC_ANALOGWD_CHANNEL_GROUP(pAnalogWDGConfig->Channel,
  2709. LL_ADC_GROUP_REGULAR));
  2710. break;
  2711. case ADC_ANALOGWATCHDOG_SINGLE_INJEC:
  2712. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1,
  2713. __LL_ADC_ANALOGWD_CHANNEL_GROUP(pAnalogWDGConfig->Channel,
  2714. LL_ADC_GROUP_INJECTED));
  2715. break;
  2716. case ADC_ANALOGWATCHDOG_SINGLE_REGINJEC:
  2717. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1,
  2718. __LL_ADC_ANALOGWD_CHANNEL_GROUP(pAnalogWDGConfig->Channel,
  2719. LL_ADC_GROUP_REGULAR_INJECTED));
  2720. break;
  2721. case ADC_ANALOGWATCHDOG_ALL_REG:
  2722. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, LL_ADC_AWD_ALL_CHANNELS_REG);
  2723. break;
  2724. case ADC_ANALOGWATCHDOG_ALL_INJEC:
  2725. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, LL_ADC_AWD_ALL_CHANNELS_INJ);
  2726. break;
  2727. case ADC_ANALOGWATCHDOG_ALL_REGINJEC:
  2728. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, LL_ADC_AWD_ALL_CHANNELS_REG_INJ);
  2729. break;
  2730. default: /* ADC_ANALOGWATCHDOG_NONE */
  2731. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, LL_ADC_AWD_DISABLE);
  2732. break;
  2733. }
  2734. /* Set the filtering configuration */
  2735. MODIFY_REG(hadc->Instance->TR1,
  2736. ADC_TR1_AWDFILT,
  2737. pAnalogWDGConfig->FilteringConfig);
  2738. /* Update state, clear previous result related to AWD1 */
  2739. CLEAR_BIT(hadc->State, HAL_ADC_STATE_AWD1);
  2740. /* Clear flag ADC analog watchdog */
  2741. /* Note: Flag cleared Clear the ADC Analog watchdog flag to be ready */
  2742. /* to use for HAL_ADC_IRQHandler() or HAL_ADC_PollForEvent() */
  2743. /* (in case left enabled by previous ADC operations). */
  2744. LL_ADC_ClearFlag_AWD1(hadc->Instance);
  2745. /* Configure ADC analog watchdog interrupt */
  2746. if (pAnalogWDGConfig->ITMode == ENABLE)
  2747. {
  2748. LL_ADC_EnableIT_AWD1(hadc->Instance);
  2749. }
  2750. else
  2751. {
  2752. LL_ADC_DisableIT_AWD1(hadc->Instance);
  2753. }
  2754. }
  2755. /* Case of ADC_ANALOGWATCHDOG_2 or ADC_ANALOGWATCHDOG_3 */
  2756. else
  2757. {
  2758. switch (pAnalogWDGConfig->WatchdogMode)
  2759. {
  2760. case ADC_ANALOGWATCHDOG_SINGLE_REG:
  2761. case ADC_ANALOGWATCHDOG_SINGLE_INJEC:
  2762. case ADC_ANALOGWATCHDOG_SINGLE_REGINJEC:
  2763. /* Update AWD by bitfield to keep the possibility to monitor */
  2764. /* several channels by successive calls of this function. */
  2765. if (pAnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_2)
  2766. {
  2767. SET_BIT(hadc->Instance->AWD2CR,
  2768. (1UL << (__LL_ADC_CHANNEL_TO_DECIMAL_NB(pAnalogWDGConfig->Channel) & 0x1FUL)));
  2769. }
  2770. else
  2771. {
  2772. SET_BIT(hadc->Instance->AWD3CR,
  2773. (1UL << (__LL_ADC_CHANNEL_TO_DECIMAL_NB(pAnalogWDGConfig->Channel) & 0x1FUL)));
  2774. }
  2775. break;
  2776. case ADC_ANALOGWATCHDOG_ALL_REG:
  2777. case ADC_ANALOGWATCHDOG_ALL_INJEC:
  2778. case ADC_ANALOGWATCHDOG_ALL_REGINJEC:
  2779. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance,
  2780. pAnalogWDGConfig->WatchdogNumber, LL_ADC_AWD_ALL_CHANNELS_REG_INJ);
  2781. break;
  2782. default: /* ADC_ANALOGWATCHDOG_NONE */
  2783. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, pAnalogWDGConfig->WatchdogNumber, LL_ADC_AWD_DISABLE);
  2784. break;
  2785. }
  2786. if (pAnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_2)
  2787. {
  2788. /* Update state, clear previous result related to AWD2 */
  2789. CLEAR_BIT(hadc->State, HAL_ADC_STATE_AWD2);
  2790. /* Clear flag ADC analog watchdog */
  2791. /* Note: Flag cleared Clear the ADC Analog watchdog flag to be ready */
  2792. /* to use for HAL_ADC_IRQHandler() or HAL_ADC_PollForEvent() */
  2793. /* (in case left enabled by previous ADC operations). */
  2794. LL_ADC_ClearFlag_AWD2(hadc->Instance);
  2795. /* Configure ADC analog watchdog interrupt */
  2796. if (pAnalogWDGConfig->ITMode == ENABLE)
  2797. {
  2798. LL_ADC_EnableIT_AWD2(hadc->Instance);
  2799. }
  2800. else
  2801. {
  2802. LL_ADC_DisableIT_AWD2(hadc->Instance);
  2803. }
  2804. }
  2805. /* (pAnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_3) */
  2806. else
  2807. {
  2808. /* Update state, clear previous result related to AWD3 */
  2809. CLEAR_BIT(hadc->State, HAL_ADC_STATE_AWD3);
  2810. /* Clear flag ADC analog watchdog */
  2811. /* Note: Flag cleared Clear the ADC Analog watchdog flag to be ready */
  2812. /* to use for HAL_ADC_IRQHandler() or HAL_ADC_PollForEvent() */
  2813. /* (in case left enabled by previous ADC operations). */
  2814. LL_ADC_ClearFlag_AWD3(hadc->Instance);
  2815. /* Configure ADC analog watchdog interrupt */
  2816. if (pAnalogWDGConfig->ITMode == ENABLE)
  2817. {
  2818. LL_ADC_EnableIT_AWD3(hadc->Instance);
  2819. }
  2820. else
  2821. {
  2822. LL_ADC_DisableIT_AWD3(hadc->Instance);
  2823. }
  2824. }
  2825. }
  2826. }
  2827. /* Analog watchdog thresholds configuration */
  2828. if (pAnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_1)
  2829. {
  2830. /* Shift the offset with respect to the selected ADC resolution: */
  2831. /* Thresholds have to be left-aligned on bit 11, the LSB (right bits) */
  2832. /* are set to 0. */
  2833. tmp_awd_high_threshold_shifted = ADC_AWD1THRESHOLD_SHIFT_RESOLUTION(hadc, pAnalogWDGConfig->HighThreshold);
  2834. tmp_awd_low_threshold_shifted = ADC_AWD1THRESHOLD_SHIFT_RESOLUTION(hadc, pAnalogWDGConfig->LowThreshold);
  2835. }
  2836. /* Case of ADC_ANALOGWATCHDOG_2 and ADC_ANALOGWATCHDOG_3 */
  2837. else
  2838. {
  2839. /* Shift the offset with respect to the selected ADC resolution: */
  2840. /* Thresholds have to be left-aligned on bit 7, the LSB (right bits) */
  2841. /* are set to 0. */
  2842. tmp_awd_high_threshold_shifted = ADC_AWD23THRESHOLD_SHIFT_RESOLUTION(hadc, pAnalogWDGConfig->HighThreshold);
  2843. tmp_awd_low_threshold_shifted = ADC_AWD23THRESHOLD_SHIFT_RESOLUTION(hadc, pAnalogWDGConfig->LowThreshold);
  2844. }
  2845. /* Set ADC analog watchdog thresholds value of both thresholds high and low */
  2846. LL_ADC_ConfigAnalogWDThresholds(hadc->Instance, pAnalogWDGConfig->WatchdogNumber, tmp_awd_high_threshold_shifted,
  2847. tmp_awd_low_threshold_shifted);
  2848. /* Process unlocked */
  2849. __HAL_UNLOCK(hadc);
  2850. /* Return function status */
  2851. return tmp_hal_status;
  2852. }
  2853. /**
  2854. * @}
  2855. */
  2856. /** @defgroup ADC_Exported_Functions_Group4 Peripheral State functions
  2857. * @brief ADC Peripheral State functions
  2858. *
  2859. @verbatim
  2860. ===============================================================================
  2861. ##### Peripheral state and errors functions #####
  2862. ===============================================================================
  2863. [..]
  2864. This subsection provides functions to get in run-time the status of the
  2865. peripheral.
  2866. (+) Check the ADC state
  2867. (+) Check the ADC error code
  2868. @endverbatim
  2869. * @{
  2870. */
  2871. /**
  2872. * @brief Return the ADC handle state.
  2873. * @note ADC state machine is managed by bitfields, ADC status must be
  2874. * compared with states bits.
  2875. * For example:
  2876. * " if ((HAL_ADC_GetState(hadc1) & HAL_ADC_STATE_REG_BUSY) != 0UL) "
  2877. * " if ((HAL_ADC_GetState(hadc1) & HAL_ADC_STATE_AWD1) != 0UL) "
  2878. * @param hadc ADC handle
  2879. * @retval ADC handle state (bitfield on 32 bits)
  2880. */
  2881. uint32_t HAL_ADC_GetState(const ADC_HandleTypeDef *hadc)
  2882. {
  2883. /* Check the parameters */
  2884. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2885. /* Return ADC handle state */
  2886. return hadc->State;
  2887. }
  2888. /**
  2889. * @brief Return the ADC error code.
  2890. * @param hadc ADC handle
  2891. * @retval ADC error code (bitfield on 32 bits)
  2892. */
  2893. uint32_t HAL_ADC_GetError(const ADC_HandleTypeDef *hadc)
  2894. {
  2895. /* Check the parameters */
  2896. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2897. return hadc->ErrorCode;
  2898. }
  2899. /**
  2900. * @}
  2901. */
  2902. /**
  2903. * @}
  2904. */
  2905. /** @defgroup ADC_Private_Functions ADC Private Functions
  2906. * @{
  2907. */
  2908. /**
  2909. * @brief Stop ADC conversion.
  2910. * @param hadc ADC handle
  2911. * @param ConversionGroup ADC group regular and/or injected.
  2912. * This parameter can be one of the following values:
  2913. * @arg @ref ADC_REGULAR_GROUP ADC regular conversion type.
  2914. * @arg @ref ADC_INJECTED_GROUP ADC injected conversion type.
  2915. * @arg @ref ADC_REGULAR_INJECTED_GROUP ADC regular and injected conversion type.
  2916. * @retval HAL status.
  2917. */
  2918. HAL_StatusTypeDef ADC_ConversionStop(ADC_HandleTypeDef *hadc, uint32_t ConversionGroup)
  2919. {
  2920. uint32_t tickstart;
  2921. uint32_t Conversion_Timeout_CPU_cycles = 0UL;
  2922. uint32_t conversion_group_reassigned = ConversionGroup;
  2923. uint32_t tmp_ADC_CR_ADSTART_JADSTART;
  2924. uint32_t tmp_adc_is_conversion_on_going_regular;
  2925. uint32_t tmp_adc_is_conversion_on_going_injected;
  2926. /* Check the parameters */
  2927. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2928. assert_param(IS_ADC_CONVERSION_GROUP(ConversionGroup));
  2929. /* Verification if ADC is not already stopped (on regular and injected */
  2930. /* groups) to bypass this function if not needed. */
  2931. tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
  2932. tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
  2933. if ((tmp_adc_is_conversion_on_going_regular != 0UL)
  2934. || (tmp_adc_is_conversion_on_going_injected != 0UL)
  2935. )
  2936. {
  2937. /* Particular case of continuous auto-injection mode combined with */
  2938. /* auto-delay mode. */
  2939. /* In auto-injection mode, regular group stop ADC_CR_ADSTP is used (not */
  2940. /* injected group stop ADC_CR_JADSTP). */
  2941. /* Procedure to be followed: Wait until JEOS=1, clear JEOS, set ADSTP=1 */
  2942. /* (see reference manual). */
  2943. if (((hadc->Instance->CFGR & ADC_CFGR_JAUTO) != 0UL)
  2944. && (hadc->Init.ContinuousConvMode == ENABLE)
  2945. && (hadc->Init.LowPowerAutoWait == ENABLE)
  2946. )
  2947. {
  2948. /* Use stop of regular group */
  2949. conversion_group_reassigned = ADC_REGULAR_GROUP;
  2950. /* Wait until JEOS=1 (maximum Timeout: 4 injected conversions) */
  2951. while (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_JEOS) == 0UL)
  2952. {
  2953. if (Conversion_Timeout_CPU_cycles >= (ADC_CONVERSION_TIME_MAX_CPU_CYCLES * 4UL))
  2954. {
  2955. /* Update ADC state machine to error */
  2956. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2957. /* Set ADC error code to ADC peripheral internal error */
  2958. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2959. return HAL_ERROR;
  2960. }
  2961. Conversion_Timeout_CPU_cycles ++;
  2962. }
  2963. /* Clear JEOS */
  2964. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOS);
  2965. }
  2966. /* Stop potential conversion on going on ADC group regular */
  2967. if (conversion_group_reassigned != ADC_INJECTED_GROUP)
  2968. {
  2969. /* Software is allowed to set ADSTP only when ADSTART=1 and ADDIS=0 */
  2970. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) != 0UL)
  2971. {
  2972. if (LL_ADC_IsDisableOngoing(hadc->Instance) == 0UL)
  2973. {
  2974. /* Stop ADC group regular conversion */
  2975. LL_ADC_REG_StopConversion(hadc->Instance);
  2976. }
  2977. }
  2978. }
  2979. /* Stop potential conversion on going on ADC group injected */
  2980. if (conversion_group_reassigned != ADC_REGULAR_GROUP)
  2981. {
  2982. /* Software is allowed to set JADSTP only when JADSTART=1 and ADDIS=0 */
  2983. if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) != 0UL)
  2984. {
  2985. if (LL_ADC_IsDisableOngoing(hadc->Instance) == 0UL)
  2986. {
  2987. /* Stop ADC group injected conversion */
  2988. LL_ADC_INJ_StopConversion(hadc->Instance);
  2989. }
  2990. }
  2991. }
  2992. /* Selection of start and stop bits with respect to the regular or injected group */
  2993. switch (conversion_group_reassigned)
  2994. {
  2995. case ADC_REGULAR_INJECTED_GROUP:
  2996. tmp_ADC_CR_ADSTART_JADSTART = (ADC_CR_ADSTART | ADC_CR_JADSTART);
  2997. break;
  2998. case ADC_INJECTED_GROUP:
  2999. tmp_ADC_CR_ADSTART_JADSTART = ADC_CR_JADSTART;
  3000. break;
  3001. /* Case ADC_REGULAR_GROUP only*/
  3002. default:
  3003. tmp_ADC_CR_ADSTART_JADSTART = ADC_CR_ADSTART;
  3004. break;
  3005. }
  3006. /* Wait for conversion effectively stopped */
  3007. tickstart = HAL_GetTick();
  3008. while ((hadc->Instance->CR & tmp_ADC_CR_ADSTART_JADSTART) != 0UL)
  3009. {
  3010. if ((HAL_GetTick() - tickstart) > ADC_STOP_CONVERSION_TIMEOUT)
  3011. {
  3012. /* New check to avoid false timeout detection in case of preemption */
  3013. if ((hadc->Instance->CR & tmp_ADC_CR_ADSTART_JADSTART) != 0UL)
  3014. {
  3015. /* Update ADC state machine to error */
  3016. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  3017. /* Set ADC error code to ADC peripheral internal error */
  3018. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  3019. return HAL_ERROR;
  3020. }
  3021. }
  3022. }
  3023. }
  3024. /* Return HAL status */
  3025. return HAL_OK;
  3026. }
  3027. /**
  3028. * @brief Enable the selected ADC.
  3029. * @note Prerequisite condition to use this function: ADC must be disabled
  3030. * and voltage regulator must be enabled (done into HAL_ADC_Init()).
  3031. * @param hadc ADC handle
  3032. * @retval HAL status.
  3033. */
  3034. HAL_StatusTypeDef ADC_Enable(ADC_HandleTypeDef *hadc)
  3035. {
  3036. uint32_t tickstart;
  3037. __IO uint32_t wait_loop_index = 0UL;
  3038. /* ADC enable and wait for ADC ready (in case of ADC is disabled or */
  3039. /* enabling phase not yet completed: flag ADC ready not yet set). */
  3040. /* Timeout implemented to not be stuck if ADC cannot be enabled (possible */
  3041. /* causes: ADC clock not running, ...). */
  3042. if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
  3043. {
  3044. /* Check if conditions to enable the ADC are fulfilled */
  3045. if ((hadc->Instance->CR & (ADC_CR_ADCAL | ADC_CR_JADSTP | ADC_CR_ADSTP | ADC_CR_JADSTART | ADC_CR_ADSTART
  3046. | ADC_CR_ADDIS | ADC_CR_ADEN)) != 0UL)
  3047. {
  3048. /* Update ADC state machine to error */
  3049. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  3050. /* Set ADC error code to ADC peripheral internal error */
  3051. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  3052. return HAL_ERROR;
  3053. }
  3054. /* Enable the ADC peripheral */
  3055. LL_ADC_Enable(hadc->Instance);
  3056. if ((LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance))
  3057. & LL_ADC_PATH_INTERNAL_TEMPSENSOR) != 0UL)
  3058. {
  3059. /* Delay for temperature sensor buffer stabilization time */
  3060. /* Note: Value LL_ADC_DELAY_TEMPSENSOR_STAB_US used instead of */
  3061. /* LL_ADC_DELAY_TEMPSENSOR_BUFFER_STAB_US because needed */
  3062. /* in case of ADC enable after a system wake up */
  3063. /* from low power mode. */
  3064. /* Wait loop initialization and execution */
  3065. /* Note: Variable divided by 2 to compensate partially */
  3066. /* CPU processing cycles, scaling in us split to not */
  3067. /* exceed 32 bits register capacity and handle low frequency. */
  3068. wait_loop_index = ((LL_ADC_DELAY_TEMPSENSOR_STAB_US / 10UL) * ((SystemCoreClock / (100000UL * 2UL)) + 1UL));
  3069. while (wait_loop_index != 0UL)
  3070. {
  3071. wait_loop_index--;
  3072. }
  3073. }
  3074. /* Wait for ADC effectively enabled */
  3075. tickstart = HAL_GetTick();
  3076. while (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_RDY) == 0UL)
  3077. {
  3078. /* If ADEN bit is set less than 4 ADC clock cycles after the ADCAL bit
  3079. has been cleared (after a calibration), ADEN bit is reset by the
  3080. calibration logic.
  3081. The workaround is to continue setting ADEN until ADRDY is becomes 1.
  3082. Additionally, ADC_ENABLE_TIMEOUT is defined to encompass this
  3083. 4 ADC clock cycle duration */
  3084. /* Note: Test of ADC enabled required due to hardware constraint to */
  3085. /* not enable ADC if already enabled. */
  3086. if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
  3087. {
  3088. LL_ADC_Enable(hadc->Instance);
  3089. }
  3090. if ((HAL_GetTick() - tickstart) > ADC_ENABLE_TIMEOUT)
  3091. {
  3092. /* New check to avoid false timeout detection in case of preemption */
  3093. if (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_RDY) == 0UL)
  3094. {
  3095. /* Update ADC state machine to error */
  3096. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  3097. /* Set ADC error code to ADC peripheral internal error */
  3098. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  3099. return HAL_ERROR;
  3100. }
  3101. }
  3102. }
  3103. }
  3104. /* Return HAL status */
  3105. return HAL_OK;
  3106. }
  3107. /**
  3108. * @brief Disable the selected ADC.
  3109. * @note Prerequisite condition to use this function: ADC conversions must be
  3110. * stopped.
  3111. * @param hadc ADC handle
  3112. * @retval HAL status.
  3113. */
  3114. HAL_StatusTypeDef ADC_Disable(ADC_HandleTypeDef *hadc)
  3115. {
  3116. uint32_t tickstart;
  3117. const uint32_t tmp_adc_is_disable_on_going = LL_ADC_IsDisableOngoing(hadc->Instance);
  3118. /* Verification if ADC is not already disabled: */
  3119. /* Note: forbidden to disable ADC (set bit ADC_CR_ADDIS) if ADC is already */
  3120. /* disabled. */
  3121. if ((LL_ADC_IsEnabled(hadc->Instance) != 0UL)
  3122. && (tmp_adc_is_disable_on_going == 0UL)
  3123. )
  3124. {
  3125. /* Check if conditions to disable the ADC are fulfilled */
  3126. if ((hadc->Instance->CR & (ADC_CR_JADSTART | ADC_CR_ADSTART | ADC_CR_ADEN)) == ADC_CR_ADEN)
  3127. {
  3128. /* Disable the ADC peripheral */
  3129. LL_ADC_Disable(hadc->Instance);
  3130. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOSMP | ADC_FLAG_RDY));
  3131. }
  3132. else
  3133. {
  3134. /* Update ADC state machine to error */
  3135. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  3136. /* Set ADC error code to ADC peripheral internal error */
  3137. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  3138. return HAL_ERROR;
  3139. }
  3140. /* Wait for ADC effectively disabled */
  3141. /* Get tick count */
  3142. tickstart = HAL_GetTick();
  3143. while ((hadc->Instance->CR & ADC_CR_ADEN) != 0UL)
  3144. {
  3145. if ((HAL_GetTick() - tickstart) > ADC_DISABLE_TIMEOUT)
  3146. {
  3147. /* New check to avoid false timeout detection in case of preemption */
  3148. if ((hadc->Instance->CR & ADC_CR_ADEN) != 0UL)
  3149. {
  3150. /* Update ADC state machine to error */
  3151. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  3152. /* Set ADC error code to ADC peripheral internal error */
  3153. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  3154. return HAL_ERROR;
  3155. }
  3156. }
  3157. }
  3158. }
  3159. /* Return HAL status */
  3160. return HAL_OK;
  3161. }
  3162. /**
  3163. * @brief DMA transfer complete callback.
  3164. * @param hdma pointer to DMA handle.
  3165. * @retval None
  3166. */
  3167. void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma)
  3168. {
  3169. /* Retrieve ADC handle corresponding to current DMA handle */
  3170. ADC_HandleTypeDef *hadc = (ADC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  3171. /* Update state machine on conversion status if not in error state */
  3172. if ((hadc->State & (HAL_ADC_STATE_ERROR_INTERNAL | HAL_ADC_STATE_ERROR_DMA)) == 0UL)
  3173. {
  3174. /* Set ADC state */
  3175. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  3176. /* Determine whether any further conversion upcoming on group regular */
  3177. /* by external trigger, continuous mode or scan sequence on going */
  3178. /* to disable interruption. */
  3179. /* Is it the end of the regular sequence ? */
  3180. if ((hadc->Instance->ISR & ADC_FLAG_EOS) != 0UL)
  3181. {
  3182. /* Are conversions software-triggered ? */
  3183. if (LL_ADC_REG_IsTriggerSourceSWStart(hadc->Instance) != 0UL)
  3184. {
  3185. /* Is CONT bit set ? */
  3186. if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_CONT) == 0UL)
  3187. {
  3188. /* CONT bit is not set, no more conversions expected */
  3189. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  3190. if ((hadc->State & HAL_ADC_STATE_INJ_BUSY) == 0UL)
  3191. {
  3192. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  3193. }
  3194. }
  3195. }
  3196. }
  3197. else
  3198. {
  3199. /* DMA End of Transfer interrupt was triggered but conversions sequence
  3200. is not over. If DMACFG is set to 0, conversions are stopped. */
  3201. if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_DMACFG) == 0UL)
  3202. {
  3203. /* DMACFG bit is not set, conversions are stopped. */
  3204. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  3205. if ((hadc->State & HAL_ADC_STATE_INJ_BUSY) == 0UL)
  3206. {
  3207. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  3208. }
  3209. }
  3210. }
  3211. /* Conversion complete callback */
  3212. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  3213. hadc->ConvCpltCallback(hadc);
  3214. #else
  3215. HAL_ADC_ConvCpltCallback(hadc);
  3216. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  3217. }
  3218. else /* DMA and-or internal error occurred */
  3219. {
  3220. if ((hadc->State & HAL_ADC_STATE_ERROR_INTERNAL) != 0UL)
  3221. {
  3222. /* Call HAL ADC Error Callback function */
  3223. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  3224. hadc->ErrorCallback(hadc);
  3225. #else
  3226. HAL_ADC_ErrorCallback(hadc);
  3227. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  3228. }
  3229. else
  3230. {
  3231. /* Call ADC DMA error callback */
  3232. hadc->DMA_Handle->XferErrorCallback(hdma);
  3233. }
  3234. }
  3235. }
  3236. /**
  3237. * @brief DMA half transfer complete callback.
  3238. * @param hdma pointer to DMA handle.
  3239. * @retval None
  3240. */
  3241. void ADC_DMAHalfConvCplt(DMA_HandleTypeDef *hdma)
  3242. {
  3243. /* Retrieve ADC handle corresponding to current DMA handle */
  3244. ADC_HandleTypeDef *hadc = (ADC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  3245. /* Half conversion callback */
  3246. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  3247. hadc->ConvHalfCpltCallback(hadc);
  3248. #else
  3249. HAL_ADC_ConvHalfCpltCallback(hadc);
  3250. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  3251. }
  3252. /**
  3253. * @brief DMA error callback.
  3254. * @param hdma pointer to DMA handle.
  3255. * @retval None
  3256. */
  3257. void ADC_DMAError(DMA_HandleTypeDef *hdma)
  3258. {
  3259. /* Retrieve ADC handle corresponding to current DMA handle */
  3260. ADC_HandleTypeDef *hadc = (ADC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  3261. /* Set ADC state */
  3262. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  3263. /* Set ADC error code to DMA error */
  3264. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_DMA);
  3265. /* Error callback */
  3266. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  3267. hadc->ErrorCallback(hadc);
  3268. #else
  3269. HAL_ADC_ErrorCallback(hadc);
  3270. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  3271. }
  3272. /**
  3273. * @}
  3274. */
  3275. #endif /* HAL_ADC_MODULE_ENABLED */
  3276. /**
  3277. * @}
  3278. */
  3279. /**
  3280. * @}
  3281. */