stm32wbxx_hal_adc.c 157 KB

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