stm32l4xx_hal_adc.c 143 KB

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