stm32g4xx_hal_adc.c 145 KB

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