stm32l5xx_hal_adc.c 146 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660
  1. /**
  2. ******************************************************************************
  3. * @file stm32l5xx_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. * "stm32l5xx_hal_adc_ex.c".
  13. *
  14. ******************************************************************************
  15. * @attention
  16. *
  17. * Copyright (c) 2019 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 AHB2 clock
  59. or asynchronous clock derived from system clock or PLLSAI1 (output divider R)
  60. running up to 120MHz.
  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 "stm32l5xx_hal.h"
  241. /** @addtogroup STM32L5xx_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(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 ((tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
  1722. || (tmp_multimode_config == LL_ADC_MULTI_DUAL_INJ_SIMULT)
  1723. || (tmp_multimode_config == LL_ADC_MULTI_DUAL_INJ_ALTERN)
  1724. )
  1725. #endif /* ADC_MULTIMODE_SUPPORT */
  1726. {
  1727. /* Enable the ADC peripheral */
  1728. tmp_hal_status = ADC_Enable(hadc);
  1729. /* Start conversion if ADC is effectively enabled */
  1730. if (tmp_hal_status == HAL_OK)
  1731. {
  1732. /* Set ADC state */
  1733. /* - Clear state bitfield related to regular group conversion results */
  1734. /* - Set state bitfield related to regular operation */
  1735. ADC_STATE_CLR_SET(hadc->State,
  1736. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP,
  1737. HAL_ADC_STATE_REG_BUSY);
  1738. #if defined(ADC_MULTIMODE_SUPPORT)
  1739. /* Reset HAL_ADC_STATE_MULTIMODE_SLAVE bit
  1740. - if ADC instance is master or if multimode feature is not available
  1741. - if multimode setting is disabled (ADC instance slave in independent mode) */
  1742. if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
  1743. || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
  1744. )
  1745. {
  1746. CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  1747. }
  1748. #endif /* ADC_MULTIMODE_SUPPORT */
  1749. /* Check if a conversion is on going on ADC group injected */
  1750. if ((hadc->State & HAL_ADC_STATE_INJ_BUSY) != 0UL)
  1751. {
  1752. /* Reset ADC error code fields related to regular conversions only */
  1753. CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA));
  1754. }
  1755. else
  1756. {
  1757. /* Reset all ADC error code fields */
  1758. ADC_CLEAR_ERRORCODE(hadc);
  1759. }
  1760. /* Set the DMA transfer complete callback */
  1761. hadc->DMA_Handle->XferCpltCallback = ADC_DMAConvCplt;
  1762. /* Set the DMA half transfer complete callback */
  1763. hadc->DMA_Handle->XferHalfCpltCallback = ADC_DMAHalfConvCplt;
  1764. /* Set the DMA error callback */
  1765. hadc->DMA_Handle->XferErrorCallback = ADC_DMAError;
  1766. /* Manage ADC and DMA start: ADC overrun interruption, DMA start, */
  1767. /* ADC start (in case of SW start): */
  1768. /* Clear regular group conversion flag and overrun flag */
  1769. /* (To ensure of no unknown state from potential previous ADC */
  1770. /* operations) */
  1771. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
  1772. /* Process unlocked */
  1773. /* Unlock before starting ADC conversions: in case of potential */
  1774. /* interruption, to let the process to ADC IRQ Handler. */
  1775. __HAL_UNLOCK(hadc);
  1776. /* With DMA, overrun event is always considered as an error even if
  1777. hadc->Init.Overrun is set to ADC_OVR_DATA_OVERWRITTEN. Therefore,
  1778. ADC_IT_OVR is enabled. */
  1779. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);
  1780. /* Enable ADC DMA mode */
  1781. SET_BIT(hadc->Instance->CFGR, ADC_CFGR_DMAEN);
  1782. /* Start the DMA channel */
  1783. tmp_hal_status = HAL_DMA_Start_IT(hadc->DMA_Handle, (uint32_t)&hadc->Instance->DR, (uint32_t)pData, Length);
  1784. /* Enable conversion of regular group. */
  1785. /* If software start has been selected, conversion starts immediately. */
  1786. /* If external trigger has been selected, conversion will start at next */
  1787. /* trigger event. */
  1788. /* Start ADC group regular conversion */
  1789. LL_ADC_REG_StartConversion(hadc->Instance);
  1790. }
  1791. else
  1792. {
  1793. /* Process unlocked */
  1794. __HAL_UNLOCK(hadc);
  1795. }
  1796. }
  1797. #if defined(ADC_MULTIMODE_SUPPORT)
  1798. else
  1799. {
  1800. tmp_hal_status = HAL_ERROR;
  1801. /* Process unlocked */
  1802. __HAL_UNLOCK(hadc);
  1803. }
  1804. #endif /* ADC_MULTIMODE_SUPPORT */
  1805. }
  1806. else
  1807. {
  1808. tmp_hal_status = HAL_BUSY;
  1809. }
  1810. /* Return function status */
  1811. return tmp_hal_status;
  1812. }
  1813. /**
  1814. * @brief Stop ADC conversion of regular group (and injected group in
  1815. * case of auto_injection mode), disable ADC DMA transfer, disable
  1816. * ADC peripheral.
  1817. * @note: ADC peripheral disable is forcing stop of potential
  1818. * conversion on ADC group injected. If ADC group injected is under use, it
  1819. * should be preliminarily stopped using HAL_ADCEx_InjectedStop function.
  1820. * @note Case of multimode enabled (when multimode feature is available):
  1821. * HAL_ADC_Stop_DMA() function is dedicated to single-ADC mode only.
  1822. * For multimode, the dedicated HAL_ADCEx_MultiModeStop_DMA() API must be used.
  1823. * @param hadc ADC handle
  1824. * @retval HAL status.
  1825. */
  1826. HAL_StatusTypeDef HAL_ADC_Stop_DMA(ADC_HandleTypeDef *hadc)
  1827. {
  1828. HAL_StatusTypeDef tmp_hal_status;
  1829. /* Check the parameters */
  1830. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1831. /* Process locked */
  1832. __HAL_LOCK(hadc);
  1833. /* 1. Stop potential ADC group regular conversion on going */
  1834. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_INJECTED_GROUP);
  1835. /* Disable ADC peripheral if conversions are effectively stopped */
  1836. if (tmp_hal_status == HAL_OK)
  1837. {
  1838. /* Disable ADC DMA (ADC DMA configuration of continuous requests is kept) */
  1839. CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_DMAEN);
  1840. /* Disable the DMA channel (in case of DMA in circular mode or stop */
  1841. /* while DMA transfer is on going) */
  1842. if (hadc->DMA_Handle->State == HAL_DMA_STATE_BUSY)
  1843. {
  1844. tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
  1845. /* Check if DMA channel effectively disabled */
  1846. if (tmp_hal_status != HAL_OK)
  1847. {
  1848. /* Update ADC state machine to error */
  1849. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  1850. }
  1851. }
  1852. /* Disable ADC overrun interrupt */
  1853. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
  1854. /* 2. Disable the ADC peripheral */
  1855. /* Update "tmp_hal_status" only if DMA channel disabling passed, */
  1856. /* to keep in memory a potential failing status. */
  1857. if (tmp_hal_status == HAL_OK)
  1858. {
  1859. tmp_hal_status = ADC_Disable(hadc);
  1860. }
  1861. else
  1862. {
  1863. (void)ADC_Disable(hadc);
  1864. }
  1865. /* Check if ADC is effectively disabled */
  1866. if (tmp_hal_status == HAL_OK)
  1867. {
  1868. /* Set ADC state */
  1869. ADC_STATE_CLR_SET(hadc->State,
  1870. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  1871. HAL_ADC_STATE_READY);
  1872. }
  1873. }
  1874. /* Process unlocked */
  1875. __HAL_UNLOCK(hadc);
  1876. /* Return function status */
  1877. return tmp_hal_status;
  1878. }
  1879. /**
  1880. * @brief Get ADC regular group conversion result.
  1881. * @note Reading register DR automatically clears ADC flag EOC
  1882. * (ADC group regular end of unitary conversion).
  1883. * @note This function does not clear ADC flag EOS
  1884. * (ADC group regular end of sequence conversion).
  1885. * Occurrence of flag EOS rising:
  1886. * - If sequencer is composed of 1 rank, flag EOS is equivalent
  1887. * to flag EOC.
  1888. * - If sequencer is composed of several ranks, during the scan
  1889. * sequence flag EOC only is raised, at the end of the scan sequence
  1890. * both flags EOC and EOS are raised.
  1891. * To clear this flag, either use function:
  1892. * in programming model IT: @ref HAL_ADC_IRQHandler(), in programming
  1893. * model polling: @ref HAL_ADC_PollForConversion()
  1894. * or @ref __HAL_ADC_CLEAR_FLAG(&hadc, ADC_FLAG_EOS).
  1895. * @param hadc ADC handle
  1896. * @retval ADC group regular conversion data
  1897. */
  1898. uint32_t HAL_ADC_GetValue(const ADC_HandleTypeDef *hadc)
  1899. {
  1900. /* Check the parameters */
  1901. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1902. /* Note: EOC flag is not cleared here by software because automatically */
  1903. /* cleared by hardware when reading register DR. */
  1904. /* Return ADC converted value */
  1905. return hadc->Instance->DR;
  1906. }
  1907. /**
  1908. * @brief Handle ADC interrupt request.
  1909. * @param hadc ADC handle
  1910. * @retval None
  1911. */
  1912. void HAL_ADC_IRQHandler(ADC_HandleTypeDef *hadc)
  1913. {
  1914. uint32_t overrun_error = 0UL; /* flag set if overrun occurrence has to be considered as an error */
  1915. uint32_t tmp_isr = hadc->Instance->ISR;
  1916. uint32_t tmp_ier = hadc->Instance->IER;
  1917. uint32_t tmp_adc_inj_is_trigger_source_sw_start;
  1918. uint32_t tmp_adc_reg_is_trigger_source_sw_start;
  1919. uint32_t tmp_cfgr;
  1920. #if defined(ADC_MULTIMODE_SUPPORT)
  1921. const ADC_TypeDef *tmpADC_Master;
  1922. uint32_t tmp_multimode_config = LL_ADC_GetMultimode(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
  1923. #endif /* ADC_MULTIMODE_SUPPORT */
  1924. /* Check the parameters */
  1925. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1926. assert_param(IS_ADC_EOC_SELECTION(hadc->Init.EOCSelection));
  1927. /* ========== Check End of Sampling flag for ADC group regular ========== */
  1928. if (((tmp_isr & ADC_FLAG_EOSMP) == ADC_FLAG_EOSMP) && ((tmp_ier & ADC_IT_EOSMP) == ADC_IT_EOSMP))
  1929. {
  1930. /* Update state machine on end of sampling status if not in error state */
  1931. if ((hadc->State & HAL_ADC_STATE_ERROR_INTERNAL) == 0UL)
  1932. {
  1933. /* Set ADC state */
  1934. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOSMP);
  1935. }
  1936. /* End Of Sampling callback */
  1937. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  1938. hadc->EndOfSamplingCallback(hadc);
  1939. #else
  1940. HAL_ADCEx_EndOfSamplingCallback(hadc);
  1941. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  1942. /* Clear regular group conversion flag */
  1943. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOSMP);
  1944. }
  1945. /* ====== Check ADC group regular end of unitary conversion sequence conversions ===== */
  1946. if ((((tmp_isr & ADC_FLAG_EOC) == ADC_FLAG_EOC) && ((tmp_ier & ADC_IT_EOC) == ADC_IT_EOC)) ||
  1947. (((tmp_isr & ADC_FLAG_EOS) == ADC_FLAG_EOS) && ((tmp_ier & ADC_IT_EOS) == ADC_IT_EOS)))
  1948. {
  1949. /* Update state machine on conversion status if not in error state */
  1950. if ((hadc->State & HAL_ADC_STATE_ERROR_INTERNAL) == 0UL)
  1951. {
  1952. /* Set ADC state */
  1953. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  1954. }
  1955. /* Determine whether any further conversion upcoming on group regular */
  1956. /* by external trigger, continuous mode or scan sequence on going */
  1957. /* to disable interruption. */
  1958. if (LL_ADC_REG_IsTriggerSourceSWStart(hadc->Instance) != 0UL)
  1959. {
  1960. /* Get relevant register CFGR in ADC instance of ADC master or slave */
  1961. /* in function of multimode state (for devices with multimode */
  1962. /* available). */
  1963. #if defined(ADC_MULTIMODE_SUPPORT)
  1964. if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
  1965. || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
  1966. || (tmp_multimode_config == LL_ADC_MULTI_DUAL_INJ_SIMULT)
  1967. || (tmp_multimode_config == LL_ADC_MULTI_DUAL_INJ_ALTERN)
  1968. )
  1969. {
  1970. /* check CONT bit directly in handle ADC CFGR register */
  1971. tmp_cfgr = READ_REG(hadc->Instance->CFGR);
  1972. }
  1973. else
  1974. {
  1975. /* else need to check Master ADC CONT bit */
  1976. tmpADC_Master = __LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance);
  1977. tmp_cfgr = READ_REG(tmpADC_Master->CFGR);
  1978. }
  1979. #else
  1980. tmp_cfgr = READ_REG(hadc->Instance->CFGR);
  1981. #endif /* ADC_MULTIMODE_SUPPORT */
  1982. /* Carry on if continuous mode is disabled */
  1983. if (READ_BIT(tmp_cfgr, ADC_CFGR_CONT) != ADC_CFGR_CONT)
  1984. {
  1985. /* If End of Sequence is reached, disable interrupts */
  1986. if (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOS))
  1987. {
  1988. /* Allowed to modify bits ADC_IT_EOC/ADC_IT_EOS only if bit */
  1989. /* ADSTART==0 (no conversion on going) */
  1990. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
  1991. {
  1992. /* Disable ADC end of sequence conversion interrupt */
  1993. /* Note: Overrun interrupt was enabled with EOC interrupt in */
  1994. /* HAL_Start_IT(), but is not disabled here because can be used */
  1995. /* by overrun IRQ process below. */
  1996. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC | ADC_IT_EOS);
  1997. /* Set ADC state */
  1998. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  1999. if ((hadc->State & HAL_ADC_STATE_INJ_BUSY) == 0UL)
  2000. {
  2001. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  2002. }
  2003. }
  2004. else
  2005. {
  2006. /* Change ADC state to error state */
  2007. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2008. /* Set ADC error code to ADC peripheral internal error */
  2009. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2010. }
  2011. }
  2012. }
  2013. }
  2014. /* Conversion complete callback */
  2015. /* Note: Into callback function "HAL_ADC_ConvCpltCallback()", */
  2016. /* to determine if conversion has been triggered from EOC or EOS, */
  2017. /* possibility to use: */
  2018. /* " if ( __HAL_ADC_GET_FLAG(&hadc, ADC_FLAG_EOS)) " */
  2019. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2020. hadc->ConvCpltCallback(hadc);
  2021. #else
  2022. HAL_ADC_ConvCpltCallback(hadc);
  2023. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2024. /* Clear regular group conversion flag */
  2025. /* Note: in case of overrun set to ADC_OVR_DATA_PRESERVED, end of */
  2026. /* conversion flags clear induces the release of the preserved data.*/
  2027. /* Therefore, if the preserved data value is needed, it must be */
  2028. /* read preliminarily into HAL_ADC_ConvCpltCallback(). */
  2029. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS));
  2030. }
  2031. /* ====== Check ADC group injected end of unitary conversion sequence conversions ===== */
  2032. if ((((tmp_isr & ADC_FLAG_JEOC) == ADC_FLAG_JEOC) && ((tmp_ier & ADC_IT_JEOC) == ADC_IT_JEOC)) ||
  2033. (((tmp_isr & ADC_FLAG_JEOS) == ADC_FLAG_JEOS) && ((tmp_ier & ADC_IT_JEOS) == ADC_IT_JEOS)))
  2034. {
  2035. /* Update state machine on conversion status if not in error state */
  2036. if ((hadc->State & HAL_ADC_STATE_ERROR_INTERNAL) == 0UL)
  2037. {
  2038. /* Set ADC state */
  2039. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_EOC);
  2040. }
  2041. /* Retrieve ADC configuration */
  2042. tmp_adc_inj_is_trigger_source_sw_start = LL_ADC_INJ_IsTriggerSourceSWStart(hadc->Instance);
  2043. tmp_adc_reg_is_trigger_source_sw_start = LL_ADC_REG_IsTriggerSourceSWStart(hadc->Instance);
  2044. /* Get relevant register CFGR in ADC instance of ADC master or slave */
  2045. /* in function of multimode state (for devices with multimode */
  2046. /* available). */
  2047. #if defined(ADC_MULTIMODE_SUPPORT)
  2048. if ((__LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance) == hadc->Instance)
  2049. || (tmp_multimode_config == LL_ADC_MULTI_INDEPENDENT)
  2050. || (tmp_multimode_config == LL_ADC_MULTI_DUAL_REG_SIMULT)
  2051. || (tmp_multimode_config == LL_ADC_MULTI_DUAL_REG_INTERL)
  2052. )
  2053. {
  2054. tmp_cfgr = READ_REG(hadc->Instance->CFGR);
  2055. }
  2056. else
  2057. {
  2058. tmpADC_Master = __LL_ADC_MULTI_INSTANCE_MASTER(hadc->Instance);
  2059. tmp_cfgr = READ_REG(tmpADC_Master->CFGR);
  2060. }
  2061. #else
  2062. tmp_cfgr = READ_REG(hadc->Instance->CFGR);
  2063. #endif /* ADC_MULTIMODE_SUPPORT */
  2064. /* Disable interruption if no further conversion upcoming by injected */
  2065. /* external trigger or by automatic injected conversion with regular */
  2066. /* group having no further conversion upcoming (same conditions as */
  2067. /* regular group interruption disabling above), */
  2068. /* and if injected scan sequence is completed. */
  2069. if (tmp_adc_inj_is_trigger_source_sw_start != 0UL)
  2070. {
  2071. if ((READ_BIT(tmp_cfgr, ADC_CFGR_JAUTO) == 0UL) ||
  2072. ((tmp_adc_reg_is_trigger_source_sw_start != 0UL) &&
  2073. (READ_BIT(tmp_cfgr, ADC_CFGR_CONT) == 0UL)))
  2074. {
  2075. /* If End of Sequence is reached, disable interrupts */
  2076. if (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_JEOS))
  2077. {
  2078. /* Particular case if injected contexts queue is enabled: */
  2079. /* when the last context has been fully processed, JSQR is reset */
  2080. /* by the hardware. Even if no injected conversion is planned to come */
  2081. /* (queue empty, triggers are ignored), it can start again */
  2082. /* immediately after setting a new context (JADSTART is still set). */
  2083. /* Therefore, state of HAL ADC injected group is kept to busy. */
  2084. if (READ_BIT(tmp_cfgr, ADC_CFGR_JQM) == 0UL)
  2085. {
  2086. /* Allowed to modify bits ADC_IT_JEOC/ADC_IT_JEOS only if bit */
  2087. /* JADSTART==0 (no conversion on going) */
  2088. if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) == 0UL)
  2089. {
  2090. /* Disable ADC end of sequence conversion interrupt */
  2091. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC | ADC_IT_JEOS);
  2092. /* Set ADC state */
  2093. CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  2094. if ((hadc->State & HAL_ADC_STATE_REG_BUSY) == 0UL)
  2095. {
  2096. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  2097. }
  2098. }
  2099. else
  2100. {
  2101. /* Update ADC state machine to error */
  2102. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2103. /* Set ADC error code to ADC peripheral internal error */
  2104. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2105. }
  2106. }
  2107. }
  2108. }
  2109. }
  2110. /* Injected Conversion complete callback */
  2111. /* Note: HAL_ADCEx_InjectedConvCpltCallback can resort to
  2112. if (__HAL_ADC_GET_FLAG(&hadc, ADC_FLAG_JEOS)) or
  2113. if (__HAL_ADC_GET_FLAG(&hadc, ADC_FLAG_JEOC)) to determine whether
  2114. interruption has been triggered by end of conversion or end of
  2115. sequence. */
  2116. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2117. hadc->InjectedConvCpltCallback(hadc);
  2118. #else
  2119. HAL_ADCEx_InjectedConvCpltCallback(hadc);
  2120. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2121. /* Clear injected group conversion flag */
  2122. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOC | ADC_FLAG_JEOS);
  2123. }
  2124. /* ========== Check Analog watchdog 1 flag ========== */
  2125. if (((tmp_isr & ADC_FLAG_AWD1) == ADC_FLAG_AWD1) && ((tmp_ier & ADC_IT_AWD1) == ADC_IT_AWD1))
  2126. {
  2127. /* Set ADC state */
  2128. SET_BIT(hadc->State, HAL_ADC_STATE_AWD1);
  2129. /* Level out of window 1 callback */
  2130. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2131. hadc->LevelOutOfWindowCallback(hadc);
  2132. #else
  2133. HAL_ADC_LevelOutOfWindowCallback(hadc);
  2134. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2135. /* Clear ADC analog watchdog flag */
  2136. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD1);
  2137. }
  2138. /* ========== Check analog watchdog 2 flag ========== */
  2139. if (((tmp_isr & ADC_FLAG_AWD2) == ADC_FLAG_AWD2) && ((tmp_ier & ADC_IT_AWD2) == ADC_IT_AWD2))
  2140. {
  2141. /* Set ADC state */
  2142. SET_BIT(hadc->State, HAL_ADC_STATE_AWD2);
  2143. /* Level out of window 2 callback */
  2144. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2145. hadc->LevelOutOfWindow2Callback(hadc);
  2146. #else
  2147. HAL_ADCEx_LevelOutOfWindow2Callback(hadc);
  2148. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2149. /* Clear ADC analog watchdog flag */
  2150. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD2);
  2151. }
  2152. /* ========== Check analog watchdog 3 flag ========== */
  2153. if (((tmp_isr & ADC_FLAG_AWD3) == ADC_FLAG_AWD3) && ((tmp_ier & ADC_IT_AWD3) == ADC_IT_AWD3))
  2154. {
  2155. /* Set ADC state */
  2156. SET_BIT(hadc->State, HAL_ADC_STATE_AWD3);
  2157. /* Level out of window 3 callback */
  2158. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2159. hadc->LevelOutOfWindow3Callback(hadc);
  2160. #else
  2161. HAL_ADCEx_LevelOutOfWindow3Callback(hadc);
  2162. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2163. /* Clear ADC analog watchdog flag */
  2164. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD3);
  2165. }
  2166. /* ========== Check Overrun flag ========== */
  2167. if (((tmp_isr & ADC_FLAG_OVR) == ADC_FLAG_OVR) && ((tmp_ier & ADC_IT_OVR) == ADC_IT_OVR))
  2168. {
  2169. /* If overrun is set to overwrite previous data (default setting), */
  2170. /* overrun event is not considered as an error. */
  2171. /* (cf ref manual "Managing conversions without using the DMA and without */
  2172. /* overrun ") */
  2173. /* Exception for usage with DMA overrun event always considered as an */
  2174. /* error. */
  2175. if (hadc->Init.Overrun == ADC_OVR_DATA_PRESERVED)
  2176. {
  2177. overrun_error = 1UL;
  2178. }
  2179. else
  2180. {
  2181. /* Check DMA configuration */
  2182. #if defined(ADC_MULTIMODE_SUPPORT)
  2183. if (tmp_multimode_config != LL_ADC_MULTI_INDEPENDENT)
  2184. {
  2185. /* Multimode (when feature is available) is enabled,
  2186. Common Control Register MDMA bits must be checked. */
  2187. if (LL_ADC_GetMultiDMATransfer(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) != LL_ADC_MULTI_REG_DMA_EACH_ADC)
  2188. {
  2189. overrun_error = 1UL;
  2190. }
  2191. }
  2192. else
  2193. #endif /* ADC_MULTIMODE_SUPPORT */
  2194. {
  2195. /* Multimode not set or feature not available or ADC independent */
  2196. if ((hadc->Instance->CFGR & ADC_CFGR_DMAEN) != 0UL)
  2197. {
  2198. overrun_error = 1UL;
  2199. }
  2200. }
  2201. }
  2202. if (overrun_error == 1UL)
  2203. {
  2204. /* Change ADC state to error state */
  2205. SET_BIT(hadc->State, HAL_ADC_STATE_REG_OVR);
  2206. /* Set ADC error code to overrun */
  2207. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_OVR);
  2208. /* Error callback */
  2209. /* Note: In case of overrun, ADC conversion data is preserved until */
  2210. /* flag OVR is reset. */
  2211. /* Therefore, old ADC conversion data can be retrieved in */
  2212. /* function "HAL_ADC_ErrorCallback()". */
  2213. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2214. hadc->ErrorCallback(hadc);
  2215. #else
  2216. HAL_ADC_ErrorCallback(hadc);
  2217. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2218. }
  2219. /* Clear ADC overrun flag */
  2220. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  2221. }
  2222. /* ========== Check Injected context queue overflow flag ========== */
  2223. if (((tmp_isr & ADC_FLAG_JQOVF) == ADC_FLAG_JQOVF) && ((tmp_ier & ADC_IT_JQOVF) == ADC_IT_JQOVF))
  2224. {
  2225. /* Change ADC state to overrun state */
  2226. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_JQOVF);
  2227. /* Set ADC error code to Injected context queue overflow */
  2228. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_JQOVF);
  2229. /* Clear the Injected context queue overflow flag */
  2230. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JQOVF);
  2231. /* Injected context queue overflow callback */
  2232. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2233. hadc->InjectedQueueOverflowCallback(hadc);
  2234. #else
  2235. HAL_ADCEx_InjectedQueueOverflowCallback(hadc);
  2236. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2237. }
  2238. }
  2239. /**
  2240. * @brief Conversion complete callback in non-blocking mode.
  2241. * @param hadc ADC handle
  2242. * @retval None
  2243. */
  2244. __weak void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
  2245. {
  2246. /* Prevent unused argument(s) compilation warning */
  2247. UNUSED(hadc);
  2248. /* NOTE : This function should not be modified. When the callback is needed,
  2249. function HAL_ADC_ConvCpltCallback must be implemented in the user file.
  2250. */
  2251. }
  2252. /**
  2253. * @brief Conversion DMA half-transfer callback in non-blocking mode.
  2254. * @param hadc ADC handle
  2255. * @retval None
  2256. */
  2257. __weak void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef *hadc)
  2258. {
  2259. /* Prevent unused argument(s) compilation warning */
  2260. UNUSED(hadc);
  2261. /* NOTE : This function should not be modified. When the callback is needed,
  2262. function HAL_ADC_ConvHalfCpltCallback must be implemented in the user file.
  2263. */
  2264. }
  2265. /**
  2266. * @brief Analog watchdog 1 callback in non-blocking mode.
  2267. * @param hadc ADC handle
  2268. * @retval None
  2269. */
  2270. __weak void HAL_ADC_LevelOutOfWindowCallback(ADC_HandleTypeDef *hadc)
  2271. {
  2272. /* Prevent unused argument(s) compilation warning */
  2273. UNUSED(hadc);
  2274. /* NOTE : This function should not be modified. When the callback is needed,
  2275. function HAL_ADC_LevelOutOfWindowCallback must be implemented in the user file.
  2276. */
  2277. }
  2278. /**
  2279. * @brief ADC error callback in non-blocking mode
  2280. * (ADC conversion with interruption or transfer by DMA).
  2281. * @note In case of error due to overrun when using ADC with DMA transfer
  2282. * (HAL ADC handle parameter "ErrorCode" to state "HAL_ADC_ERROR_OVR"):
  2283. * - Reinitialize the DMA using function "HAL_ADC_Stop_DMA()".
  2284. * - If needed, restart a new ADC conversion using function
  2285. * "HAL_ADC_Start_DMA()"
  2286. * (this function is also clearing overrun flag)
  2287. * @param hadc ADC handle
  2288. * @retval None
  2289. */
  2290. __weak void HAL_ADC_ErrorCallback(ADC_HandleTypeDef *hadc)
  2291. {
  2292. /* Prevent unused argument(s) compilation warning */
  2293. UNUSED(hadc);
  2294. /* NOTE : This function should not be modified. When the callback is needed,
  2295. function HAL_ADC_ErrorCallback must be implemented in the user file.
  2296. */
  2297. }
  2298. /**
  2299. * @}
  2300. */
  2301. /** @defgroup ADC_Exported_Functions_Group3 Peripheral Control functions
  2302. * @brief Peripheral Control functions
  2303. *
  2304. @verbatim
  2305. ===============================================================================
  2306. ##### Peripheral Control functions #####
  2307. ===============================================================================
  2308. [..] This section provides functions allowing to:
  2309. (+) Configure channels on regular group
  2310. (+) Configure the analog watchdog
  2311. @endverbatim
  2312. * @{
  2313. */
  2314. /**
  2315. * @brief Configure a channel to be assigned to ADC group regular.
  2316. * @note In case of usage of internal measurement channels:
  2317. * Vbat/VrefInt/TempSensor.
  2318. * These internal paths can be disabled using function
  2319. * HAL_ADC_DeInit().
  2320. * @note Possibility to update parameters on the fly:
  2321. * This function initializes channel into ADC group regular,
  2322. * following calls to this function can be used to reconfigure
  2323. * some parameters of structure "ADC_ChannelConfTypeDef" on the fly,
  2324. * without resetting the ADC.
  2325. * The setting of these parameters is conditioned to ADC state:
  2326. * Refer to comments of structure "ADC_ChannelConfTypeDef".
  2327. * @param hadc ADC handle
  2328. * @param pConfig Structure of ADC channel assigned to ADC group regular.
  2329. * @retval HAL status
  2330. */
  2331. HAL_StatusTypeDef HAL_ADC_ConfigChannel(ADC_HandleTypeDef *hadc, const ADC_ChannelConfTypeDef *pConfig)
  2332. {
  2333. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  2334. uint32_t tmpOffsetShifted;
  2335. uint32_t tmp_config_internal_channel;
  2336. __IO uint32_t wait_loop_index = 0UL;
  2337. uint32_t tmp_adc_is_conversion_on_going_regular;
  2338. uint32_t tmp_adc_is_conversion_on_going_injected;
  2339. /* Check the parameters */
  2340. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2341. assert_param(IS_ADC_REGULAR_RANK(pConfig->Rank));
  2342. assert_param(IS_ADC_SAMPLE_TIME(pConfig->SamplingTime));
  2343. assert_param(IS_ADC_SINGLE_DIFFERENTIAL(pConfig->SingleDiff));
  2344. assert_param(IS_ADC_OFFSET_NUMBER(pConfig->OffsetNumber));
  2345. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), pConfig->Offset));
  2346. /* if ROVSE is set, the value of the OFFSETy_EN bit in ADCx_OFRy register is
  2347. ignored (considered as reset) */
  2348. assert_param(!((pConfig->OffsetNumber != ADC_OFFSET_NONE) && (hadc->Init.OversamplingMode == ENABLE)));
  2349. /* Verification of channel number */
  2350. if (pConfig->SingleDiff != ADC_DIFFERENTIAL_ENDED)
  2351. {
  2352. assert_param(IS_ADC_CHANNEL(hadc, pConfig->Channel));
  2353. }
  2354. else
  2355. {
  2356. assert_param(IS_ADC_DIFF_CHANNEL(hadc, pConfig->Channel));
  2357. }
  2358. /* Process locked */
  2359. __HAL_LOCK(hadc);
  2360. /* Parameters update conditioned to ADC state: */
  2361. /* Parameters that can be updated when ADC is disabled or enabled without */
  2362. /* conversion on going on regular group: */
  2363. /* - Channel number */
  2364. /* - Channel rank */
  2365. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) == 0UL)
  2366. {
  2367. /* Set ADC group regular sequence: channel on the selected scan sequence rank */
  2368. LL_ADC_REG_SetSequencerRanks(hadc->Instance, pConfig->Rank, pConfig->Channel);
  2369. /* Parameters update conditioned to ADC state: */
  2370. /* Parameters that can be updated when ADC is disabled or enabled without */
  2371. /* conversion on going on regular group: */
  2372. /* - Channel sampling time */
  2373. /* - Channel offset */
  2374. tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
  2375. tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
  2376. if ((tmp_adc_is_conversion_on_going_regular == 0UL)
  2377. && (tmp_adc_is_conversion_on_going_injected == 0UL)
  2378. )
  2379. {
  2380. /* Manage specific case of sampling time 3.5 cycles replacing 2.5 cyles */
  2381. if (pConfig->SamplingTime == ADC_SAMPLETIME_3CYCLES_5)
  2382. {
  2383. /* Set sampling time of the selected ADC channel */
  2384. LL_ADC_SetChannelSamplingTime(hadc->Instance, pConfig->Channel, LL_ADC_SAMPLINGTIME_2CYCLES_5);
  2385. /* Set ADC sampling time common configuration */
  2386. LL_ADC_SetSamplingTimeCommonConfig(hadc->Instance, LL_ADC_SAMPLINGTIME_COMMON_3C5_REPL_2C5);
  2387. }
  2388. else
  2389. {
  2390. /* Set sampling time of the selected ADC channel */
  2391. LL_ADC_SetChannelSamplingTime(hadc->Instance, pConfig->Channel, pConfig->SamplingTime);
  2392. /* Set ADC sampling time common configuration */
  2393. LL_ADC_SetSamplingTimeCommonConfig(hadc->Instance, LL_ADC_SAMPLINGTIME_COMMON_DEFAULT);
  2394. }
  2395. /* Configure the offset: offset enable/disable, channel, offset value */
  2396. /* Shift the offset with respect to the selected ADC resolution. */
  2397. /* Offset has to be left-aligned on bit 11, the LSB (right bits) are set to 0 */
  2398. tmpOffsetShifted = ADC_OFFSET_SHIFT_RESOLUTION(hadc, (uint32_t)pConfig->Offset);
  2399. if (pConfig->OffsetNumber != ADC_OFFSET_NONE)
  2400. {
  2401. /* Set ADC selected offset number */
  2402. LL_ADC_SetOffset(hadc->Instance, pConfig->OffsetNumber, pConfig->Channel, tmpOffsetShifted);
  2403. }
  2404. else
  2405. {
  2406. /* Scan each offset register to check if the selected channel is targeted. */
  2407. /* If this is the case, the corresponding offset number is disabled. */
  2408. if (__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_1))
  2409. == __LL_ADC_CHANNEL_TO_DECIMAL_NB(pConfig->Channel))
  2410. {
  2411. LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_1, LL_ADC_OFFSET_DISABLE);
  2412. }
  2413. if (__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_2))
  2414. == __LL_ADC_CHANNEL_TO_DECIMAL_NB(pConfig->Channel))
  2415. {
  2416. LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_2, LL_ADC_OFFSET_DISABLE);
  2417. }
  2418. if (__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_3))
  2419. == __LL_ADC_CHANNEL_TO_DECIMAL_NB(pConfig->Channel))
  2420. {
  2421. LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_3, LL_ADC_OFFSET_DISABLE);
  2422. }
  2423. if (__LL_ADC_CHANNEL_TO_DECIMAL_NB(LL_ADC_GetOffsetChannel(hadc->Instance, LL_ADC_OFFSET_4))
  2424. == __LL_ADC_CHANNEL_TO_DECIMAL_NB(pConfig->Channel))
  2425. {
  2426. LL_ADC_SetOffsetState(hadc->Instance, LL_ADC_OFFSET_4, LL_ADC_OFFSET_DISABLE);
  2427. }
  2428. }
  2429. }
  2430. /* Parameters update conditioned to ADC state: */
  2431. /* Parameters that can be updated only when ADC is disabled: */
  2432. /* - Single or differential mode */
  2433. /* - Internal measurement channels: Vbat/VrefInt/TempSensor */
  2434. if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
  2435. {
  2436. /* Set mode single-ended or differential input of the selected ADC channel */
  2437. LL_ADC_SetChannelSingleDiff(hadc->Instance, pConfig->Channel, pConfig->SingleDiff);
  2438. /* Configuration of differential mode */
  2439. if (pConfig->SingleDiff == ADC_DIFFERENTIAL_ENDED)
  2440. {
  2441. /* Set sampling time of the selected ADC channel */
  2442. /* Note: ADC channel number masked with value "0x1F" to ensure shift value within 32 bits range */
  2443. LL_ADC_SetChannelSamplingTime(hadc->Instance,
  2444. (uint32_t)(__LL_ADC_DECIMAL_NB_TO_CHANNEL(
  2445. (__LL_ADC_CHANNEL_TO_DECIMAL_NB((uint32_t)pConfig->Channel)
  2446. + 1UL) & 0x1FUL)),
  2447. pConfig->SamplingTime);
  2448. }
  2449. /* Management of internal measurement channels: Vbat/VrefInt/TempSensor. */
  2450. /* If internal channel selected, enable dedicated internal buffers and */
  2451. /* paths. */
  2452. /* Note: these internal measurement paths can be disabled using */
  2453. /* HAL_ADC_DeInit(). */
  2454. if (__LL_ADC_IS_CHANNEL_INTERNAL(pConfig->Channel))
  2455. {
  2456. /* Configuration of common ADC parameters */
  2457. tmp_config_internal_channel = LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance));
  2458. /* Software is allowed to change common parameters only when all ADCs */
  2459. /* of the common group are disabled. */
  2460. if (__LL_ADC_IS_ENABLED_ALL_COMMON_INSTANCE(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) == 0UL)
  2461. {
  2462. /* If the requested internal measurement path has already been enabled, */
  2463. /* bypass the configuration processing. */
  2464. if ((pConfig->Channel == ADC_CHANNEL_TEMPSENSOR)
  2465. && ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_TEMPSENSOR) == 0UL))
  2466. {
  2467. if (ADC_TEMPERATURE_SENSOR_INSTANCE(hadc))
  2468. {
  2469. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance),
  2470. LL_ADC_PATH_INTERNAL_TEMPSENSOR | tmp_config_internal_channel);
  2471. /* Delay for temperature sensor stabilization time */
  2472. /* Wait loop initialization and execution */
  2473. /* Note: Variable divided by 2 to compensate partially */
  2474. /* CPU processing cycles, scaling in us split to not */
  2475. /* exceed 32 bits register capacity and handle low frequency. */
  2476. wait_loop_index = ((LL_ADC_DELAY_TEMPSENSOR_STAB_US / 10UL)
  2477. * ((SystemCoreClock / (100000UL * 2UL)) + 1UL));
  2478. while(wait_loop_index != 0UL)
  2479. {
  2480. wait_loop_index--;
  2481. }
  2482. }
  2483. }
  2484. else if ((pConfig->Channel == ADC_CHANNEL_VBAT)
  2485. && ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_VBAT) == 0UL))
  2486. {
  2487. if (ADC_BATTERY_VOLTAGE_INSTANCE(hadc))
  2488. {
  2489. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance),
  2490. LL_ADC_PATH_INTERNAL_VBAT | tmp_config_internal_channel);
  2491. }
  2492. }
  2493. else if ((pConfig->Channel == ADC_CHANNEL_VREFINT)
  2494. && ((tmp_config_internal_channel & LL_ADC_PATH_INTERNAL_VREFINT) == 0UL))
  2495. {
  2496. if (ADC_VREFINT_INSTANCE(hadc))
  2497. {
  2498. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance),
  2499. LL_ADC_PATH_INTERNAL_VREFINT | tmp_config_internal_channel);
  2500. }
  2501. }
  2502. else
  2503. {
  2504. /* nothing to do */
  2505. }
  2506. }
  2507. /* If the requested internal measurement path has already been */
  2508. /* enabled and other ADC of the common group are enabled, internal */
  2509. /* measurement paths cannot be enabled. */
  2510. else
  2511. {
  2512. /* Update ADC state machine to error */
  2513. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  2514. tmp_hal_status = HAL_ERROR;
  2515. }
  2516. }
  2517. }
  2518. }
  2519. /* If a conversion is on going on regular group, no update on regular */
  2520. /* channel could be done on neither of the channel configuration structure */
  2521. /* parameters. */
  2522. else
  2523. {
  2524. /* Update ADC state machine to error */
  2525. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  2526. tmp_hal_status = HAL_ERROR;
  2527. }
  2528. /* Process unlocked */
  2529. __HAL_UNLOCK(hadc);
  2530. /* Return function status */
  2531. return tmp_hal_status;
  2532. }
  2533. /**
  2534. * @brief Configure the analog watchdog.
  2535. * @note Possibility to update parameters on the fly:
  2536. * This function initializes the selected analog watchdog, successive
  2537. * calls to this function can be used to reconfigure some parameters
  2538. * of structure "ADC_AnalogWDGConfTypeDef" on the fly, without resetting
  2539. * the ADC.
  2540. * The setting of these parameters is conditioned to ADC state.
  2541. * For parameters constraints, see comments of structure
  2542. * "ADC_AnalogWDGConfTypeDef".
  2543. * @note On this STM32 series, analog watchdog thresholds cannot be modified
  2544. * while ADC conversion is on going.
  2545. * @param hadc ADC handle
  2546. * @param pAnalogWDGConfig Structure of ADC analog watchdog configuration
  2547. * @retval HAL status
  2548. */
  2549. HAL_StatusTypeDef HAL_ADC_AnalogWDGConfig(ADC_HandleTypeDef *hadc, const ADC_AnalogWDGConfTypeDef *pAnalogWDGConfig)
  2550. {
  2551. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  2552. uint32_t tmp_awd_high_threshold_shifted;
  2553. uint32_t tmp_awd_low_threshold_shifted;
  2554. uint32_t tmp_adc_is_conversion_on_going_regular;
  2555. uint32_t tmp_adc_is_conversion_on_going_injected;
  2556. /* Check the parameters */
  2557. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2558. assert_param(IS_ADC_ANALOG_WATCHDOG_NUMBER(pAnalogWDGConfig->WatchdogNumber));
  2559. assert_param(IS_ADC_ANALOG_WATCHDOG_MODE(pAnalogWDGConfig->WatchdogMode));
  2560. assert_param(IS_FUNCTIONAL_STATE(pAnalogWDGConfig->ITMode));
  2561. if ((pAnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_REG) ||
  2562. (pAnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_INJEC) ||
  2563. (pAnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_REGINJEC))
  2564. {
  2565. assert_param(IS_ADC_CHANNEL(hadc, pAnalogWDGConfig->Channel));
  2566. }
  2567. /* Verify thresholds range */
  2568. if (hadc->Init.OversamplingMode == ENABLE)
  2569. {
  2570. /* Case of oversampling enabled: depending on ratio and shift configuration,
  2571. analog watchdog thresholds can be higher than ADC resolution.
  2572. Verify if thresholds are within maximum thresholds range. */
  2573. assert_param(IS_ADC_RANGE(ADC_RESOLUTION_12B, pAnalogWDGConfig->HighThreshold));
  2574. assert_param(IS_ADC_RANGE(ADC_RESOLUTION_12B, pAnalogWDGConfig->LowThreshold));
  2575. }
  2576. else
  2577. {
  2578. /* Verify if thresholds are within the selected ADC resolution */
  2579. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), pAnalogWDGConfig->HighThreshold));
  2580. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), pAnalogWDGConfig->LowThreshold));
  2581. }
  2582. /* Process locked */
  2583. __HAL_LOCK(hadc);
  2584. /* Parameters update conditioned to ADC state: */
  2585. /* Parameters that can be updated when ADC is disabled or enabled without */
  2586. /* conversion on going on ADC groups regular and injected: */
  2587. /* - Analog watchdog channels */
  2588. /* - Analog watchdog thresholds */
  2589. tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
  2590. tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
  2591. if ((tmp_adc_is_conversion_on_going_regular == 0UL)
  2592. && (tmp_adc_is_conversion_on_going_injected == 0UL)
  2593. )
  2594. {
  2595. /* Analog watchdog configuration */
  2596. if (pAnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_1)
  2597. {
  2598. /* Configuration of analog watchdog: */
  2599. /* - Set the analog watchdog enable mode: one or overall group of */
  2600. /* channels, on groups regular and-or injected. */
  2601. switch (pAnalogWDGConfig->WatchdogMode)
  2602. {
  2603. case ADC_ANALOGWATCHDOG_SINGLE_REG:
  2604. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1,
  2605. __LL_ADC_ANALOGWD_CHANNEL_GROUP(pAnalogWDGConfig->Channel,
  2606. LL_ADC_GROUP_REGULAR));
  2607. break;
  2608. case ADC_ANALOGWATCHDOG_SINGLE_INJEC:
  2609. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1,
  2610. __LL_ADC_ANALOGWD_CHANNEL_GROUP(pAnalogWDGConfig->Channel,
  2611. LL_ADC_GROUP_INJECTED));
  2612. break;
  2613. case ADC_ANALOGWATCHDOG_SINGLE_REGINJEC:
  2614. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1,
  2615. __LL_ADC_ANALOGWD_CHANNEL_GROUP(pAnalogWDGConfig->Channel,
  2616. LL_ADC_GROUP_REGULAR_INJECTED));
  2617. break;
  2618. case ADC_ANALOGWATCHDOG_ALL_REG:
  2619. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, LL_ADC_AWD_ALL_CHANNELS_REG);
  2620. break;
  2621. case ADC_ANALOGWATCHDOG_ALL_INJEC:
  2622. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, LL_ADC_AWD_ALL_CHANNELS_INJ);
  2623. break;
  2624. case ADC_ANALOGWATCHDOG_ALL_REGINJEC:
  2625. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, LL_ADC_AWD_ALL_CHANNELS_REG_INJ);
  2626. break;
  2627. default: /* ADC_ANALOGWATCHDOG_NONE */
  2628. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, LL_ADC_AWD1, LL_ADC_AWD_DISABLE);
  2629. break;
  2630. }
  2631. /* Shift the offset in function of the selected ADC resolution: */
  2632. /* Thresholds have to be left-aligned on bit 11, the LSB (right bits) */
  2633. /* are set to 0 */
  2634. tmp_awd_high_threshold_shifted = ADC_AWD1THRESHOLD_SHIFT_RESOLUTION(hadc, pAnalogWDGConfig->HighThreshold);
  2635. tmp_awd_low_threshold_shifted = ADC_AWD1THRESHOLD_SHIFT_RESOLUTION(hadc, pAnalogWDGConfig->LowThreshold);
  2636. /* Set ADC analog watchdog thresholds value of both thresholds high and low */
  2637. LL_ADC_ConfigAnalogWDThresholds(hadc->Instance, pAnalogWDGConfig->WatchdogNumber, tmp_awd_high_threshold_shifted,
  2638. tmp_awd_low_threshold_shifted);
  2639. /* Update state, clear previous result related to AWD1 */
  2640. CLEAR_BIT(hadc->State, HAL_ADC_STATE_AWD1);
  2641. /* Clear flag ADC analog watchdog */
  2642. /* Note: Flag cleared Clear the ADC Analog watchdog flag to be ready */
  2643. /* to use for HAL_ADC_IRQHandler() or HAL_ADC_PollForEvent() */
  2644. /* (in case left enabled by previous ADC operations). */
  2645. LL_ADC_ClearFlag_AWD1(hadc->Instance);
  2646. /* Configure ADC analog watchdog interrupt */
  2647. if (pAnalogWDGConfig->ITMode == ENABLE)
  2648. {
  2649. LL_ADC_EnableIT_AWD1(hadc->Instance);
  2650. }
  2651. else
  2652. {
  2653. LL_ADC_DisableIT_AWD1(hadc->Instance);
  2654. }
  2655. }
  2656. /* Case of ADC_ANALOGWATCHDOG_2 or ADC_ANALOGWATCHDOG_3 */
  2657. else
  2658. {
  2659. switch (pAnalogWDGConfig->WatchdogMode)
  2660. {
  2661. case ADC_ANALOGWATCHDOG_SINGLE_REG:
  2662. case ADC_ANALOGWATCHDOG_SINGLE_INJEC:
  2663. case ADC_ANALOGWATCHDOG_SINGLE_REGINJEC:
  2664. /* Update AWD by bitfield to keep the possibility to monitor */
  2665. /* several channels by successive calls of this function. */
  2666. if (pAnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_2)
  2667. {
  2668. SET_BIT(hadc->Instance->AWD2CR,
  2669. (1UL << (__LL_ADC_CHANNEL_TO_DECIMAL_NB(pAnalogWDGConfig->Channel) & 0x1FUL)));
  2670. }
  2671. else
  2672. {
  2673. SET_BIT(hadc->Instance->AWD3CR,
  2674. (1UL << (__LL_ADC_CHANNEL_TO_DECIMAL_NB(pAnalogWDGConfig->Channel) & 0x1FUL)));
  2675. }
  2676. break;
  2677. case ADC_ANALOGWATCHDOG_ALL_REG:
  2678. case ADC_ANALOGWATCHDOG_ALL_INJEC:
  2679. case ADC_ANALOGWATCHDOG_ALL_REGINJEC:
  2680. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance,
  2681. pAnalogWDGConfig->WatchdogNumber, LL_ADC_AWD_ALL_CHANNELS_REG_INJ);
  2682. break;
  2683. default: /* ADC_ANALOGWATCHDOG_NONE */
  2684. LL_ADC_SetAnalogWDMonitChannels(hadc->Instance, pAnalogWDGConfig->WatchdogNumber, LL_ADC_AWD_DISABLE);
  2685. break;
  2686. }
  2687. /* Shift the thresholds in function of the selected ADC resolution */
  2688. /* have to be left-aligned on bit 7, the LSB (right bits) are set to 0 */
  2689. tmp_awd_high_threshold_shifted = ADC_AWD23THRESHOLD_SHIFT_RESOLUTION(hadc, pAnalogWDGConfig->HighThreshold);
  2690. tmp_awd_low_threshold_shifted = ADC_AWD23THRESHOLD_SHIFT_RESOLUTION(hadc, pAnalogWDGConfig->LowThreshold);
  2691. /* Set ADC analog watchdog thresholds value of both thresholds high and low */
  2692. LL_ADC_ConfigAnalogWDThresholds(hadc->Instance, pAnalogWDGConfig->WatchdogNumber, tmp_awd_high_threshold_shifted,
  2693. tmp_awd_low_threshold_shifted);
  2694. if (pAnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_2)
  2695. {
  2696. /* Update state, clear previous result related to AWD2 */
  2697. CLEAR_BIT(hadc->State, HAL_ADC_STATE_AWD2);
  2698. /* Clear flag ADC analog watchdog */
  2699. /* Note: Flag cleared Clear the ADC Analog watchdog flag to be ready */
  2700. /* to use for HAL_ADC_IRQHandler() or HAL_ADC_PollForEvent() */
  2701. /* (in case left enabled by previous ADC operations). */
  2702. LL_ADC_ClearFlag_AWD2(hadc->Instance);
  2703. /* Configure ADC analog watchdog interrupt */
  2704. if (pAnalogWDGConfig->ITMode == ENABLE)
  2705. {
  2706. LL_ADC_EnableIT_AWD2(hadc->Instance);
  2707. }
  2708. else
  2709. {
  2710. LL_ADC_DisableIT_AWD2(hadc->Instance);
  2711. }
  2712. }
  2713. /* (pAnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_3) */
  2714. else
  2715. {
  2716. /* Update state, clear previous result related to AWD3 */
  2717. CLEAR_BIT(hadc->State, HAL_ADC_STATE_AWD3);
  2718. /* Clear flag ADC analog watchdog */
  2719. /* Note: Flag cleared Clear the ADC Analog watchdog flag to be ready */
  2720. /* to use for HAL_ADC_IRQHandler() or HAL_ADC_PollForEvent() */
  2721. /* (in case left enabled by previous ADC operations). */
  2722. LL_ADC_ClearFlag_AWD3(hadc->Instance);
  2723. /* Configure ADC analog watchdog interrupt */
  2724. if (pAnalogWDGConfig->ITMode == ENABLE)
  2725. {
  2726. LL_ADC_EnableIT_AWD3(hadc->Instance);
  2727. }
  2728. else
  2729. {
  2730. LL_ADC_DisableIT_AWD3(hadc->Instance);
  2731. }
  2732. }
  2733. }
  2734. }
  2735. /* If a conversion is on going on ADC group regular or injected, no update */
  2736. /* could be done on neither of the AWD configuration structure parameters. */
  2737. else
  2738. {
  2739. /* Update ADC state machine to error */
  2740. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  2741. tmp_hal_status = HAL_ERROR;
  2742. }
  2743. /* Process unlocked */
  2744. __HAL_UNLOCK(hadc);
  2745. /* Return function status */
  2746. return tmp_hal_status;
  2747. }
  2748. /**
  2749. * @}
  2750. */
  2751. /** @defgroup ADC_Exported_Functions_Group4 Peripheral State functions
  2752. * @brief ADC Peripheral State functions
  2753. *
  2754. @verbatim
  2755. ===============================================================================
  2756. ##### Peripheral state and errors functions #####
  2757. ===============================================================================
  2758. [..]
  2759. This subsection provides functions to get in run-time the status of the
  2760. peripheral.
  2761. (+) Check the ADC state
  2762. (+) Check the ADC error code
  2763. @endverbatim
  2764. * @{
  2765. */
  2766. /**
  2767. * @brief Return the ADC handle state.
  2768. * @note ADC state machine is managed by bitfields, ADC status must be
  2769. * compared with states bits.
  2770. * For example:
  2771. * " if ((HAL_ADC_GetState(hadc1) & HAL_ADC_STATE_REG_BUSY) != 0UL) "
  2772. * " if ((HAL_ADC_GetState(hadc1) & HAL_ADC_STATE_AWD1) != 0UL) "
  2773. * @param hadc ADC handle
  2774. * @retval ADC handle state (bitfield on 32 bits)
  2775. */
  2776. uint32_t HAL_ADC_GetState(const ADC_HandleTypeDef *hadc)
  2777. {
  2778. /* Check the parameters */
  2779. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2780. /* Return ADC handle state */
  2781. return hadc->State;
  2782. }
  2783. /**
  2784. * @brief Return the ADC error code.
  2785. * @param hadc ADC handle
  2786. * @retval ADC error code (bitfield on 32 bits)
  2787. */
  2788. uint32_t HAL_ADC_GetError(const ADC_HandleTypeDef *hadc)
  2789. {
  2790. /* Check the parameters */
  2791. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2792. return hadc->ErrorCode;
  2793. }
  2794. /**
  2795. * @}
  2796. */
  2797. /**
  2798. * @}
  2799. */
  2800. /** @defgroup ADC_Private_Functions ADC Private Functions
  2801. * @{
  2802. */
  2803. /**
  2804. * @brief Stop ADC conversion.
  2805. * @param hadc ADC handle
  2806. * @param ConversionGroup ADC group regular and/or injected.
  2807. * This parameter can be one of the following values:
  2808. * @arg @ref ADC_REGULAR_GROUP ADC regular conversion type.
  2809. * @arg @ref ADC_INJECTED_GROUP ADC injected conversion type.
  2810. * @arg @ref ADC_REGULAR_INJECTED_GROUP ADC regular and injected conversion type.
  2811. * @retval HAL status.
  2812. */
  2813. HAL_StatusTypeDef ADC_ConversionStop(ADC_HandleTypeDef *hadc, uint32_t ConversionGroup)
  2814. {
  2815. uint32_t tickstart;
  2816. uint32_t Conversion_Timeout_CPU_cycles = 0UL;
  2817. uint32_t conversion_group_reassigned = ConversionGroup;
  2818. uint32_t tmp_ADC_CR_ADSTART_JADSTART;
  2819. uint32_t tmp_adc_is_conversion_on_going_regular;
  2820. uint32_t tmp_adc_is_conversion_on_going_injected;
  2821. /* Check the parameters */
  2822. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2823. assert_param(IS_ADC_CONVERSION_GROUP(ConversionGroup));
  2824. /* Verification if ADC is not already stopped (on regular and injected */
  2825. /* groups) to bypass this function if not needed. */
  2826. tmp_adc_is_conversion_on_going_regular = LL_ADC_REG_IsConversionOngoing(hadc->Instance);
  2827. tmp_adc_is_conversion_on_going_injected = LL_ADC_INJ_IsConversionOngoing(hadc->Instance);
  2828. if ((tmp_adc_is_conversion_on_going_regular != 0UL)
  2829. || (tmp_adc_is_conversion_on_going_injected != 0UL)
  2830. )
  2831. {
  2832. /* Particular case of continuous auto-injection mode combined with */
  2833. /* auto-delay mode. */
  2834. /* In auto-injection mode, regular group stop ADC_CR_ADSTP is used (not */
  2835. /* injected group stop ADC_CR_JADSTP). */
  2836. /* Procedure to be followed: Wait until JEOS=1, clear JEOS, set ADSTP=1 */
  2837. /* (see reference manual). */
  2838. if (((hadc->Instance->CFGR & ADC_CFGR_JAUTO) != 0UL)
  2839. && (hadc->Init.ContinuousConvMode == ENABLE)
  2840. && (hadc->Init.LowPowerAutoWait == ENABLE)
  2841. )
  2842. {
  2843. /* Use stop of regular group */
  2844. conversion_group_reassigned = ADC_REGULAR_GROUP;
  2845. /* Wait until JEOS=1 (maximum Timeout: 4 injected conversions) */
  2846. while (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_JEOS) == 0UL)
  2847. {
  2848. if (Conversion_Timeout_CPU_cycles >= (ADC_CONVERSION_TIME_MAX_CPU_CYCLES * 4UL))
  2849. {
  2850. /* Update ADC state machine to error */
  2851. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2852. /* Set ADC error code to ADC peripheral internal error */
  2853. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2854. return HAL_ERROR;
  2855. }
  2856. Conversion_Timeout_CPU_cycles ++;
  2857. }
  2858. /* Clear JEOS */
  2859. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOS);
  2860. }
  2861. /* Stop potential conversion on going on ADC group regular */
  2862. if (conversion_group_reassigned != ADC_INJECTED_GROUP)
  2863. {
  2864. /* Software is allowed to set ADSTP only when ADSTART=1 and ADDIS=0 */
  2865. if (LL_ADC_REG_IsConversionOngoing(hadc->Instance) != 0UL)
  2866. {
  2867. if (LL_ADC_IsDisableOngoing(hadc->Instance) == 0UL)
  2868. {
  2869. /* Stop ADC group regular conversion */
  2870. LL_ADC_REG_StopConversion(hadc->Instance);
  2871. }
  2872. }
  2873. }
  2874. /* Stop potential conversion on going on ADC group injected */
  2875. if (conversion_group_reassigned != ADC_REGULAR_GROUP)
  2876. {
  2877. /* Software is allowed to set JADSTP only when JADSTART=1 and ADDIS=0 */
  2878. if (LL_ADC_INJ_IsConversionOngoing(hadc->Instance) != 0UL)
  2879. {
  2880. if (LL_ADC_IsDisableOngoing(hadc->Instance) == 0UL)
  2881. {
  2882. /* Stop ADC group injected conversion */
  2883. LL_ADC_INJ_StopConversion(hadc->Instance);
  2884. }
  2885. }
  2886. }
  2887. /* Selection of start and stop bits with respect to the regular or injected group */
  2888. switch (conversion_group_reassigned)
  2889. {
  2890. case ADC_REGULAR_INJECTED_GROUP:
  2891. tmp_ADC_CR_ADSTART_JADSTART = (ADC_CR_ADSTART | ADC_CR_JADSTART);
  2892. break;
  2893. case ADC_INJECTED_GROUP:
  2894. tmp_ADC_CR_ADSTART_JADSTART = ADC_CR_JADSTART;
  2895. break;
  2896. /* Case ADC_REGULAR_GROUP only*/
  2897. default:
  2898. tmp_ADC_CR_ADSTART_JADSTART = ADC_CR_ADSTART;
  2899. break;
  2900. }
  2901. /* Wait for conversion effectively stopped */
  2902. tickstart = HAL_GetTick();
  2903. while ((hadc->Instance->CR & tmp_ADC_CR_ADSTART_JADSTART) != 0UL)
  2904. {
  2905. if ((HAL_GetTick() - tickstart) > ADC_STOP_CONVERSION_TIMEOUT)
  2906. {
  2907. /* New check to avoid false timeout detection in case of preemption */
  2908. if ((hadc->Instance->CR & tmp_ADC_CR_ADSTART_JADSTART) != 0UL)
  2909. {
  2910. /* Update ADC state machine to error */
  2911. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2912. /* Set ADC error code to ADC peripheral internal error */
  2913. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2914. return HAL_ERROR;
  2915. }
  2916. }
  2917. }
  2918. }
  2919. /* Return HAL status */
  2920. return HAL_OK;
  2921. }
  2922. /**
  2923. * @brief Enable the selected ADC.
  2924. * @note Prerequisite condition to use this function: ADC must be disabled
  2925. * and voltage regulator must be enabled (done into HAL_ADC_Init()).
  2926. * @param hadc ADC handle
  2927. * @retval HAL status.
  2928. */
  2929. HAL_StatusTypeDef ADC_Enable(ADC_HandleTypeDef *hadc)
  2930. {
  2931. uint32_t tickstart;
  2932. __IO uint32_t wait_loop_index = 0UL;
  2933. /* ADC enable and wait for ADC ready (in case of ADC is disabled or */
  2934. /* enabling phase not yet completed: flag ADC ready not yet set). */
  2935. /* Timeout implemented to not be stuck if ADC cannot be enabled (possible */
  2936. /* causes: ADC clock not running, ...). */
  2937. if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
  2938. {
  2939. /* Check if conditions to enable the ADC are fulfilled */
  2940. if ((hadc->Instance->CR & (ADC_CR_ADCAL | ADC_CR_JADSTP | ADC_CR_ADSTP | ADC_CR_JADSTART | ADC_CR_ADSTART
  2941. | ADC_CR_ADDIS | ADC_CR_ADEN)) != 0UL)
  2942. {
  2943. /* Update ADC state machine to error */
  2944. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2945. /* Set ADC error code to ADC peripheral internal error */
  2946. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2947. return HAL_ERROR;
  2948. }
  2949. /* Enable the ADC peripheral */
  2950. LL_ADC_Enable(hadc->Instance);
  2951. if ((LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance))
  2952. & LL_ADC_PATH_INTERNAL_TEMPSENSOR) != 0UL)
  2953. {
  2954. /* Delay for temperature sensor buffer stabilization time */
  2955. /* Note: Value LL_ADC_DELAY_TEMPSENSOR_STAB_US used instead of */
  2956. /* LL_ADC_DELAY_TEMPSENSOR_BUFFER_STAB_US because needed */
  2957. /* in case of ADC enable after a system wake up */
  2958. /* from low power mode. */
  2959. /* Wait loop initialization and execution */
  2960. /* Note: Variable divided by 2 to compensate partially */
  2961. /* CPU processing cycles, scaling in us split to not */
  2962. /* exceed 32 bits register capacity and handle low frequency. */
  2963. wait_loop_index = ((LL_ADC_DELAY_TEMPSENSOR_STAB_US / 10UL) * ((SystemCoreClock / (100000UL * 2UL)) + 1UL));
  2964. while (wait_loop_index != 0UL)
  2965. {
  2966. wait_loop_index--;
  2967. }
  2968. }
  2969. /* Wait for ADC effectively enabled */
  2970. tickstart = HAL_GetTick();
  2971. while (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_RDY) == 0UL)
  2972. {
  2973. /* If ADEN bit is set less than 4 ADC clock cycles after the ADCAL bit
  2974. has been cleared (after a calibration), ADEN bit is reset by the
  2975. calibration logic.
  2976. The workaround is to continue setting ADEN until ADRDY is becomes 1.
  2977. Additionally, ADC_ENABLE_TIMEOUT is defined to encompass this
  2978. 4 ADC clock cycle duration */
  2979. /* Note: Test of ADC enabled required due to hardware constraint to */
  2980. /* not enable ADC if already enabled. */
  2981. if (LL_ADC_IsEnabled(hadc->Instance) == 0UL)
  2982. {
  2983. LL_ADC_Enable(hadc->Instance);
  2984. }
  2985. if ((HAL_GetTick() - tickstart) > ADC_ENABLE_TIMEOUT)
  2986. {
  2987. /* New check to avoid false timeout detection in case of preemption */
  2988. if (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_RDY) == 0UL)
  2989. {
  2990. /* Update ADC state machine to error */
  2991. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2992. /* Set ADC error code to ADC peripheral internal error */
  2993. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2994. return HAL_ERROR;
  2995. }
  2996. }
  2997. }
  2998. }
  2999. /* Return HAL status */
  3000. return HAL_OK;
  3001. }
  3002. /**
  3003. * @brief Disable the selected ADC.
  3004. * @note Prerequisite condition to use this function: ADC conversions must be
  3005. * stopped.
  3006. * @param hadc ADC handle
  3007. * @retval HAL status.
  3008. */
  3009. HAL_StatusTypeDef ADC_Disable(ADC_HandleTypeDef *hadc)
  3010. {
  3011. uint32_t tickstart;
  3012. const uint32_t tmp_adc_is_disable_on_going = LL_ADC_IsDisableOngoing(hadc->Instance);
  3013. /* Verification if ADC is not already disabled: */
  3014. /* Note: forbidden to disable ADC (set bit ADC_CR_ADDIS) if ADC is already */
  3015. /* disabled. */
  3016. if ((LL_ADC_IsEnabled(hadc->Instance) != 0UL)
  3017. && (tmp_adc_is_disable_on_going == 0UL)
  3018. )
  3019. {
  3020. /* Check if conditions to disable the ADC are fulfilled */
  3021. if ((hadc->Instance->CR & (ADC_CR_JADSTART | ADC_CR_ADSTART | ADC_CR_ADEN)) == ADC_CR_ADEN)
  3022. {
  3023. /* Disable the ADC peripheral */
  3024. LL_ADC_Disable(hadc->Instance);
  3025. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOSMP | ADC_FLAG_RDY));
  3026. }
  3027. else
  3028. {
  3029. /* Update ADC state machine to error */
  3030. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  3031. /* Set ADC error code to ADC peripheral internal error */
  3032. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  3033. return HAL_ERROR;
  3034. }
  3035. /* Wait for ADC effectively disabled */
  3036. /* Get tick count */
  3037. tickstart = HAL_GetTick();
  3038. while ((hadc->Instance->CR & ADC_CR_ADEN) != 0UL)
  3039. {
  3040. if ((HAL_GetTick() - tickstart) > ADC_DISABLE_TIMEOUT)
  3041. {
  3042. /* New check to avoid false timeout detection in case of preemption */
  3043. if ((hadc->Instance->CR & ADC_CR_ADEN) != 0UL)
  3044. {
  3045. /* Update ADC state machine to error */
  3046. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  3047. /* Set ADC error code to ADC peripheral internal error */
  3048. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  3049. return HAL_ERROR;
  3050. }
  3051. }
  3052. }
  3053. }
  3054. /* Return HAL status */
  3055. return HAL_OK;
  3056. }
  3057. /**
  3058. * @brief DMA transfer complete callback.
  3059. * @param hdma pointer to DMA handle.
  3060. * @retval None
  3061. */
  3062. void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma)
  3063. {
  3064. /* Retrieve ADC handle corresponding to current DMA handle */
  3065. ADC_HandleTypeDef *hadc = (ADC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  3066. /* Update state machine on conversion status if not in error state */
  3067. if ((hadc->State & (HAL_ADC_STATE_ERROR_INTERNAL | HAL_ADC_STATE_ERROR_DMA)) == 0UL)
  3068. {
  3069. /* Set ADC state */
  3070. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  3071. /* Determine whether any further conversion upcoming on group regular */
  3072. /* by external trigger, continuous mode or scan sequence on going */
  3073. /* to disable interruption. */
  3074. /* Is it the end of the regular sequence ? */
  3075. if ((hadc->Instance->ISR & ADC_FLAG_EOS) != 0UL)
  3076. {
  3077. /* Are conversions software-triggered ? */
  3078. if (LL_ADC_REG_IsTriggerSourceSWStart(hadc->Instance) != 0UL)
  3079. {
  3080. /* Is CONT bit set ? */
  3081. if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_CONT) == 0UL)
  3082. {
  3083. /* CONT bit is not set, no more conversions expected */
  3084. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  3085. if ((hadc->State & HAL_ADC_STATE_INJ_BUSY) == 0UL)
  3086. {
  3087. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  3088. }
  3089. }
  3090. }
  3091. }
  3092. else
  3093. {
  3094. /* DMA End of Transfer interrupt was triggered but conversions sequence
  3095. is not over. If DMACFG is set to 0, conversions are stopped. */
  3096. if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_DMACFG) == 0UL)
  3097. {
  3098. /* DMACFG bit is not set, conversions are stopped. */
  3099. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  3100. if ((hadc->State & HAL_ADC_STATE_INJ_BUSY) == 0UL)
  3101. {
  3102. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  3103. }
  3104. }
  3105. }
  3106. /* Conversion complete callback */
  3107. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  3108. hadc->ConvCpltCallback(hadc);
  3109. #else
  3110. HAL_ADC_ConvCpltCallback(hadc);
  3111. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  3112. }
  3113. else /* DMA and-or internal error occurred */
  3114. {
  3115. if ((hadc->State & HAL_ADC_STATE_ERROR_INTERNAL) != 0UL)
  3116. {
  3117. /* Call HAL ADC Error Callback function */
  3118. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  3119. hadc->ErrorCallback(hadc);
  3120. #else
  3121. HAL_ADC_ErrorCallback(hadc);
  3122. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  3123. }
  3124. else
  3125. {
  3126. /* Call ADC DMA error callback */
  3127. hadc->DMA_Handle->XferErrorCallback(hdma);
  3128. }
  3129. }
  3130. }
  3131. /**
  3132. * @brief DMA half transfer complete callback.
  3133. * @param hdma pointer to DMA handle.
  3134. * @retval None
  3135. */
  3136. void ADC_DMAHalfConvCplt(DMA_HandleTypeDef *hdma)
  3137. {
  3138. /* Retrieve ADC handle corresponding to current DMA handle */
  3139. ADC_HandleTypeDef *hadc = (ADC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  3140. /* Half conversion callback */
  3141. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  3142. hadc->ConvHalfCpltCallback(hadc);
  3143. #else
  3144. HAL_ADC_ConvHalfCpltCallback(hadc);
  3145. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  3146. }
  3147. /**
  3148. * @brief DMA error callback.
  3149. * @param hdma pointer to DMA handle.
  3150. * @retval None
  3151. */
  3152. void ADC_DMAError(DMA_HandleTypeDef *hdma)
  3153. {
  3154. /* Retrieve ADC handle corresponding to current DMA handle */
  3155. ADC_HandleTypeDef *hadc = (ADC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  3156. /* Set ADC state */
  3157. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  3158. /* Set ADC error code to DMA error */
  3159. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_DMA);
  3160. /* Error callback */
  3161. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  3162. hadc->ErrorCallback(hadc);
  3163. #else
  3164. HAL_ADC_ErrorCallback(hadc);
  3165. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  3166. }
  3167. /**
  3168. * @}
  3169. */
  3170. #endif /* HAL_ADC_MODULE_ENABLED */
  3171. /**
  3172. * @}
  3173. */
  3174. /**
  3175. * @}
  3176. */