stm32f3xx_hal_adc_ex.c 309 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630
  1. /**
  2. ******************************************************************************
  3. * @file stm32f3xx_hal_adc_ex.c
  4. * @author MCD Application Team
  5. * @brief This file provides firmware functions to manage the following
  6. * functionalities of the Analog to Digital Convertor (ADC)
  7. * peripheral:
  8. * + Peripheral Control functions
  9. * Other functions (generic functions) are available in file
  10. * "stm32f3xx_hal_adc.c".
  11. *
  12. ******************************************************************************
  13. * @attention
  14. *
  15. * Copyright (c) 2016 STMicroelectronics.
  16. * All rights reserved.
  17. *
  18. * This software is licensed under terms that can be found in the LICENSE file
  19. * in the root directory of this software component.
  20. * If no LICENSE file comes with this software, it is provided AS-IS.
  21. *
  22. ******************************************************************************
  23. @verbatim
  24. [..]
  25. (@) Sections "ADC peripheral features" and "How to use this driver" are
  26. available in file of generic functions "stm32f3xx_hal_adc.c".
  27. [..]
  28. @endverbatim
  29. */
  30. /* Includes ------------------------------------------------------------------*/
  31. #include "stm32f3xx_hal.h"
  32. /** @addtogroup STM32F3xx_HAL_Driver
  33. * @{
  34. */
  35. /** @defgroup ADCEx ADCEx
  36. * @brief ADC Extended HAL module driver
  37. * @{
  38. */
  39. #ifdef HAL_ADC_MODULE_ENABLED
  40. /* Private typedef -----------------------------------------------------------*/
  41. /* Private define ------------------------------------------------------------*/
  42. /** @defgroup ADCEx_Private_Constants ADCEx Private Constants
  43. * @{
  44. */
  45. #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
  46. defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
  47. defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
  48. defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
  49. /* Fixed timeout values for ADC calibration, enable settling time, disable */
  50. /* settling time. */
  51. /* Values defined to be higher than worst cases: low clock frequency, */
  52. /* maximum prescalers. */
  53. /* Ex of profile low frequency : Clock source at 0.5 MHz, ADC clock */
  54. /* prescaler 256 (devices STM32F30xx), sampling time 7.5 ADC clock cycles, */
  55. /* resolution 12 bits. */
  56. /* Unit: ms */
  57. #define ADC_CALIBRATION_TIMEOUT ( 10U)
  58. #define ADC_ENABLE_TIMEOUT ( 2U)
  59. #define ADC_DISABLE_TIMEOUT ( 2U)
  60. #define ADC_STOP_CONVERSION_TIMEOUT ( 11U)
  61. /* Timeout to wait for current conversion on going to be completed. */
  62. /* Timeout fixed to worst case, for 1 channel. */
  63. /* - maximum sampling time (601.5 adc_clk) */
  64. /* - ADC resolution (Tsar 12 bits= 12.5 adc_clk) */
  65. /* - ADC clock (from PLL with prescaler 256 (devices STM32F30xx)) */
  66. /* Unit: cycles of CPU clock. */
  67. #define ADC_CONVERSION_TIME_MAX_CPU_CYCLES ( 156928U)
  68. /* Delay for ADC stabilization time (ADC voltage regulator start-up time) */
  69. /* Maximum delay is 10us (refer to device datasheet, param. TADCVREG_STUP). */
  70. /* Unit: us */
  71. #define ADC_STAB_DELAY_US ( 10U)
  72. /* Delay for temperature sensor stabilization time. */
  73. /* Maximum delay is 10us (refer device datasheet, parameter tSTART). */
  74. /* Unit: us */
  75. #define ADC_TEMPSENSOR_DELAY_US ( 10U)
  76. #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
  77. /* STM32F302xC || STM32F303xC || STM32F358xx || */
  78. /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
  79. /* STM32F301x8 || STM32F302x8 || STM32F318xx */
  80. #if defined(STM32F373xC) || defined(STM32F378xx)
  81. /* Timeout values for ADC enable and disable settling time. */
  82. /* Values defined to be higher than worst cases: low clocks freq, */
  83. /* maximum prescaler. */
  84. /* Ex of profile low frequency : Clock source at 0.1 MHz, ADC clock */
  85. /* prescaler 4U, sampling time 12.5 ADC clock cycles, resolution 12 bits. */
  86. /* Unit: ms */
  87. #define ADC_ENABLE_TIMEOUT ( 2U)
  88. #define ADC_DISABLE_TIMEOUT ( 2U)
  89. /* Delay for ADC calibration: */
  90. /* Hardware prerequisite before starting a calibration: the ADC must have */
  91. /* been in power-on state for at least two ADC clock cycles. */
  92. /* Unit: ADC clock cycles */
  93. #define ADC_PRECALIBRATION_DELAY_ADCCLOCKCYCLES ( 2U)
  94. /* Timeout value for ADC calibration */
  95. /* Value defined to be higher than worst cases: low clocks freq, */
  96. /* maximum prescaler. */
  97. /* Ex of profile low frequency : Clock source at 0.1 MHz, ADC clock */
  98. /* prescaler 4U, sampling time 12.5 ADC clock cycles, resolution 12 bits. */
  99. /* Unit: ms */
  100. #define ADC_CALIBRATION_TIMEOUT ( 10U)
  101. /* Delay for ADC stabilization time. */
  102. /* Maximum delay is 1us (refer to device datasheet, parameter tSTAB). */
  103. /* Unit: us */
  104. #define ADC_STAB_DELAY_US ( 1U)
  105. /* Delay for temperature sensor stabilization time. */
  106. /* Maximum delay is 10us (refer to device datasheet, parameter tSTART). */
  107. /* Unit: us */
  108. #define ADC_TEMPSENSOR_DELAY_US ( 10U)
  109. /* Maximum number of CPU cycles corresponding to 1 ADC cycle */
  110. /* Value fixed to worst case: clock prescalers slowing down ADC clock to */
  111. /* minimum frequency */
  112. /* - AHB prescaler: 16 */
  113. /* - ADC prescaler: 8 */
  114. /* Unit: cycles of CPU clock. */
  115. #define ADC_CYCLE_WORST_CASE_CPU_CYCLES ( 128U)
  116. /* ADC conversion cycles (unit: ADC clock cycles) */
  117. /* (selected sampling time + conversion time of 12.5 ADC clock cycles, with */
  118. /* resolution 12 bits) */
  119. #define ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_1CYCLE5 ( 14U)
  120. #define ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_7CYCLES5 ( 20U)
  121. #define ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_13CYCLES5 ( 26U)
  122. #define ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_28CYCLES5 ( 41U)
  123. #define ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_41CYCLES5 ( 54U)
  124. #define ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_55CYCLES5 ( 68U)
  125. #define ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_71CYCLES5 ( 84U)
  126. #define ADC_CONVERSIONCLOCKCYCLES_SAMPLETIME_239CYCLES5 (252U)
  127. #endif /* STM32F373xC || STM32F378xx */
  128. /**
  129. * @}
  130. */
  131. /* Private macro -------------------------------------------------------------*/
  132. /* Private variables ---------------------------------------------------------*/
  133. /* Private function prototypes -----------------------------------------------*/
  134. #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
  135. defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
  136. defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
  137. defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
  138. static HAL_StatusTypeDef ADC_Enable(ADC_HandleTypeDef* hadc);
  139. static HAL_StatusTypeDef ADC_Disable(ADC_HandleTypeDef* hadc);
  140. static HAL_StatusTypeDef ADC_ConversionStop(ADC_HandleTypeDef* hadc, uint32_t ConversionGroup);
  141. #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
  142. /* STM32F302xC || STM32F303xC || STM32F358xx || */
  143. /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
  144. /* STM32F301x8 || STM32F302x8 || STM32F318xx */
  145. #if defined(STM32F373xC) || defined(STM32F378xx)
  146. static HAL_StatusTypeDef ADC_Enable(ADC_HandleTypeDef* hadc);
  147. static HAL_StatusTypeDef ADC_ConversionStop_Disable(ADC_HandleTypeDef* hadc);
  148. #endif /* STM32F373xC || STM32F378xx */
  149. static void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma);
  150. static void ADC_DMAHalfConvCplt(DMA_HandleTypeDef *hdma);
  151. static void ADC_DMAError(DMA_HandleTypeDef *hdma);
  152. /* Exported functions --------------------------------------------------------*/
  153. /** @defgroup ADCEx_Exported_Functions ADCEx Exported Functions
  154. * @{
  155. */
  156. /** @defgroup ADCEx_Exported_Functions_Group1 ADCEx Initialization and de-initialization functions
  157. * @brief ADC Extended Initialization and Configuration functions
  158. *
  159. @verbatim
  160. ===============================================================================
  161. ##### Initialization and de-initialization functions #####
  162. ===============================================================================
  163. [..] This section provides functions allowing to:
  164. (+) Initialize and configure the ADC.
  165. (+) De-initialize the ADC.
  166. @endverbatim
  167. * @{
  168. */
  169. #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
  170. defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
  171. defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
  172. defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
  173. /**
  174. * @brief Initializes the ADC peripheral and regular group according to
  175. * parameters specified in structure "ADC_InitTypeDef".
  176. * @note As prerequisite, ADC clock must be configured at RCC top level
  177. * depending on possible clock sources: AHB clock or PLL clock.
  178. * See commented example code below that can be copied and uncommented
  179. * into HAL_ADC_MspInit().
  180. * @note Possibility to update parameters on the fly:
  181. * This function initializes the ADC MSP (HAL_ADC_MspInit()) only when
  182. * coming from ADC state reset. Following calls to this function can
  183. * be used to reconfigure some parameters of ADC_InitTypeDef
  184. * structure on the fly, without modifying MSP configuration. If ADC
  185. * MSP has to be modified again, HAL_ADC_DeInit() must be called
  186. * before HAL_ADC_Init().
  187. * The setting of these parameters is conditioned by ADC state.
  188. * For parameters constraints, see comments of structure
  189. * "ADC_InitTypeDef".
  190. * @note This function configures the ADC within 2 scopes: scope of entire
  191. * ADC and scope of regular group. For parameters details, see comments
  192. * of structure "ADC_InitTypeDef".
  193. * @note For devices with several ADCs: parameters related to common ADC
  194. * registers (ADC clock mode) are set only if all ADCs sharing the
  195. * same common group are disabled.
  196. * If this is not the case, these common parameters setting are
  197. * bypassed without error reporting: it can be the intended behaviour in
  198. * case of update of a parameter of ADC_InitTypeDef on the fly,
  199. * without disabling the other ADCs sharing the same common group.
  200. * @param hadc ADC handle
  201. * @retval HAL status
  202. */
  203. HAL_StatusTypeDef HAL_ADC_Init(ADC_HandleTypeDef* hadc)
  204. {
  205. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  206. ADC_Common_TypeDef *tmpADC_Common;
  207. ADC_HandleTypeDef tmphadcSharingSameCommonRegister;
  208. uint32_t tmpCFGR = 0U;
  209. __IO uint32_t wait_loop_index = 0U;
  210. /* Check ADC handle */
  211. if(hadc == NULL)
  212. {
  213. return HAL_ERROR;
  214. }
  215. /* Check the parameters */
  216. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  217. assert_param(IS_ADC_CLOCKPRESCALER(hadc->Init.ClockPrescaler));
  218. assert_param(IS_ADC_RESOLUTION(hadc->Init.Resolution));
  219. assert_param(IS_ADC_DATA_ALIGN(hadc->Init.DataAlign));
  220. assert_param(IS_ADC_SCAN_MODE(hadc->Init.ScanConvMode));
  221. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
  222. assert_param(IS_ADC_EXTTRIG_EDGE(hadc->Init.ExternalTrigConvEdge));
  223. assert_param(IS_ADC_EXTTRIG(hadc->Init.ExternalTrigConv));
  224. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DMAContinuousRequests));
  225. assert_param(IS_ADC_EOC_SELECTION(hadc->Init.EOCSelection));
  226. assert_param(IS_ADC_OVERRUN(hadc->Init.Overrun));
  227. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.LowPowerAutoWait));
  228. if(hadc->Init.ScanConvMode != ADC_SCAN_DISABLE)
  229. {
  230. assert_param(IS_ADC_REGULAR_NB_CONV(hadc->Init.NbrOfConversion));
  231. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DiscontinuousConvMode));
  232. if(hadc->Init.DiscontinuousConvMode != DISABLE)
  233. {
  234. assert_param(IS_ADC_REGULAR_DISCONT_NUMBER(hadc->Init.NbrOfDiscConversion));
  235. }
  236. }
  237. /* Configuration of ADC core parameters and ADC MSP related parameters */
  238. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL))
  239. {
  240. /* As prerequisite, into HAL_ADC_MspInit(), ADC clock must be configured */
  241. /* at RCC top level. */
  242. /* Refer to header of this file for more details on clock enabling */
  243. /* procedure. */
  244. /* Actions performed only if ADC is coming from state reset: */
  245. /* - Initialization of ADC MSP */
  246. /* - ADC voltage regulator enable */
  247. if (hadc->State == HAL_ADC_STATE_RESET)
  248. {
  249. /* Initialize ADC error code */
  250. ADC_CLEAR_ERRORCODE(hadc);
  251. /* Initialize HAL ADC API internal variables */
  252. hadc->InjectionConfig.ChannelCount = 0U;
  253. hadc->InjectionConfig.ContextQueue = 0U;
  254. /* Allocate lock resource and initialize it */
  255. hadc->Lock = HAL_UNLOCKED;
  256. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  257. /* Init the ADC Callback settings */
  258. hadc->ConvCpltCallback = HAL_ADC_ConvCpltCallback; /* Legacy weak callback */
  259. hadc->ConvHalfCpltCallback = HAL_ADC_ConvHalfCpltCallback; /* Legacy weak callback */
  260. hadc->LevelOutOfWindowCallback = HAL_ADC_LevelOutOfWindowCallback; /* Legacy weak callback */
  261. hadc->ErrorCallback = HAL_ADC_ErrorCallback; /* Legacy weak callback */
  262. hadc->InjectedConvCpltCallback = HAL_ADCEx_InjectedConvCpltCallback; /* Legacy weak callback */
  263. if (hadc->MspInitCallback == NULL)
  264. {
  265. hadc->MspInitCallback = HAL_ADC_MspInit; /* Legacy weak MspInit */
  266. }
  267. /* Init the low level hardware */
  268. hadc->MspInitCallback(hadc);
  269. #else
  270. /* Init the low level hardware */
  271. HAL_ADC_MspInit(hadc);
  272. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  273. /* Enable voltage regulator (if disabled at this step) */
  274. if (HAL_IS_BIT_CLR(hadc->Instance->CR, ADC_CR_ADVREGEN_0))
  275. {
  276. /* Note: The software must wait for the startup time of the ADC */
  277. /* voltage regulator before launching a calibration or */
  278. /* enabling the ADC. This temporization must be implemented by */
  279. /* software and is equal to 10 us in the worst case */
  280. /* process/temperature/power supply. */
  281. /* Disable the ADC (if not already disabled) */
  282. tmp_hal_status = ADC_Disable(hadc);
  283. /* Check if ADC is effectively disabled */
  284. /* Configuration of ADC parameters if previous preliminary actions */
  285. /* are correctly completed. */
  286. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL) &&
  287. (tmp_hal_status == HAL_OK) )
  288. {
  289. /* Set ADC state */
  290. ADC_STATE_CLR_SET(hadc->State,
  291. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  292. HAL_ADC_STATE_BUSY_INTERNAL);
  293. /* Set the intermediate state before moving the ADC voltage */
  294. /* regulator to state enable. */
  295. CLEAR_BIT(hadc->Instance->CR, (ADC_CR_ADVREGEN_1 | ADC_CR_ADVREGEN_0));
  296. /* Set ADVREGEN bits to 0x01U */
  297. SET_BIT(hadc->Instance->CR, ADC_CR_ADVREGEN_0);
  298. /* Delay for ADC stabilization time. */
  299. /* Compute number of CPU cycles to wait for */
  300. wait_loop_index = (ADC_STAB_DELAY_US * (SystemCoreClock / 1000000U));
  301. while(wait_loop_index != 0U)
  302. {
  303. wait_loop_index--;
  304. }
  305. }
  306. }
  307. }
  308. /* Verification that ADC voltage regulator is correctly enabled, whether */
  309. /* or not ADC is coming from state reset (if any potential problem of */
  310. /* clocking, voltage regulator would not be enabled). */
  311. if (HAL_IS_BIT_CLR(hadc->Instance->CR, ADC_CR_ADVREGEN_0) ||
  312. HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADVREGEN_1) )
  313. {
  314. /* Update ADC state machine to error */
  315. ADC_STATE_CLR_SET(hadc->State,
  316. HAL_ADC_STATE_BUSY_INTERNAL,
  317. HAL_ADC_STATE_ERROR_INTERNAL);
  318. /* Set ADC error code to ADC IP internal error */
  319. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  320. tmp_hal_status = HAL_ERROR;
  321. }
  322. }
  323. /* Configuration of ADC parameters if previous preliminary actions are */
  324. /* correctly completed and if there is no conversion on going on regular */
  325. /* group (ADC may already be enabled at this point if HAL_ADC_Init() is */
  326. /* called to update a parameter on the fly). */
  327. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL) &&
  328. (tmp_hal_status == HAL_OK) &&
  329. (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET) )
  330. {
  331. /* Set ADC state */
  332. ADC_STATE_CLR_SET(hadc->State,
  333. HAL_ADC_STATE_REG_BUSY,
  334. HAL_ADC_STATE_BUSY_INTERNAL);
  335. /* Configuration of common ADC parameters */
  336. /* Pointer to the common control register to which is belonging hadc */
  337. /* (Depending on STM32F3 product, there may be up to 4 ADC and 2 common */
  338. /* control registers) */
  339. tmpADC_Common = ADC_COMMON_REGISTER(hadc);
  340. /* Set handle of the other ADC sharing the same common register */
  341. ADC_COMMON_ADC_OTHER(hadc, &tmphadcSharingSameCommonRegister);
  342. /* Parameters update conditioned to ADC state: */
  343. /* Parameters that can be updated only when ADC is disabled: */
  344. /* - Multimode clock configuration */
  345. if ((ADC_IS_ENABLE(hadc) == RESET) &&
  346. ((tmphadcSharingSameCommonRegister.Instance == NULL) ||
  347. (ADC_IS_ENABLE(&tmphadcSharingSameCommonRegister) == RESET) ) )
  348. {
  349. /* Reset configuration of ADC common register CCR: */
  350. /* - ADC clock mode: CKMODE */
  351. /* Some parameters of this register are not reset, since they are set */
  352. /* by other functions and must be kept in case of usage of this */
  353. /* function on the fly (update of a parameter of ADC_InitTypeDef */
  354. /* without needing to reconfigure all other ADC groups/channels */
  355. /* parameters): */
  356. /* - multimode related parameters: MDMA, DMACFG, DELAY, MULTI (set */
  357. /* into HAL_ADCEx_MultiModeConfigChannel() ) */
  358. /* - internal measurement paths: Vbat, temperature sensor, Vref */
  359. /* (set into HAL_ADC_ConfigChannel() or */
  360. /* HAL_ADCEx_InjectedConfigChannel() ) */
  361. MODIFY_REG(tmpADC_Common->CCR ,
  362. ADC_CCR_CKMODE ,
  363. hadc->Init.ClockPrescaler );
  364. }
  365. /* Configuration of ADC: */
  366. /* - resolution */
  367. /* - data alignment */
  368. /* - external trigger to start conversion */
  369. /* - external trigger polarity */
  370. /* - continuous conversion mode */
  371. /* - overrun */
  372. /* - discontinuous mode */
  373. SET_BIT(tmpCFGR, ADC_CFGR_CONTINUOUS((uint32_t)hadc->Init.ContinuousConvMode) |
  374. ADC_CFGR_OVERRUN(hadc->Init.Overrun) |
  375. hadc->Init.DataAlign |
  376. hadc->Init.Resolution );
  377. /* Enable discontinuous mode only if continuous mode is disabled */
  378. if (hadc->Init.DiscontinuousConvMode == ENABLE)
  379. {
  380. if (hadc->Init.ContinuousConvMode == DISABLE)
  381. {
  382. /* Enable the selected ADC regular discontinuous mode */
  383. /* Set the number of channels to be converted in discontinuous mode */
  384. SET_BIT(tmpCFGR, ADC_CFGR_DISCEN |
  385. ADC_CFGR_DISCONTINUOUS_NUM(hadc->Init.NbrOfDiscConversion) );
  386. }
  387. else
  388. {
  389. /* ADC regular group discontinuous was intended to be enabled, */
  390. /* but ADC regular group modes continuous and sequencer discontinuous */
  391. /* cannot be enabled simultaneously. */
  392. /* Update ADC state machine to error */
  393. ADC_STATE_CLR_SET(hadc->State,
  394. HAL_ADC_STATE_BUSY_INTERNAL,
  395. HAL_ADC_STATE_ERROR_CONFIG);
  396. /* Set ADC error code to ADC IP internal error */
  397. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  398. }
  399. }
  400. /* Enable external trigger if trigger selection is different of software */
  401. /* start. */
  402. /* Note: This configuration keeps the hardware feature of parameter */
  403. /* ExternalTrigConvEdge "trigger edge none" equivalent to */
  404. /* software start. */
  405. if (hadc->Init.ExternalTrigConv != ADC_SOFTWARE_START)
  406. {
  407. SET_BIT(tmpCFGR, ADC_CFGR_EXTSEL_SET(hadc, hadc->Init.ExternalTrigConv) |
  408. hadc->Init.ExternalTrigConvEdge );
  409. }
  410. /* Parameters update conditioned to ADC state: */
  411. /* Parameters that can be updated when ADC is disabled or enabled without */
  412. /* conversion on going on regular and injected groups: */
  413. /* - DMA continuous request */
  414. /* - LowPowerAutoWait feature */
  415. if (ADC_IS_CONVERSION_ONGOING_REGULAR_INJECTED(hadc) == RESET)
  416. {
  417. CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_AUTDLY |
  418. ADC_CFGR_DMACFG );
  419. SET_BIT(tmpCFGR, ADC_CFGR_AUTOWAIT((uint32_t)hadc->Init.LowPowerAutoWait) |
  420. ADC_CFGR_DMACONTREQ((uint32_t)hadc->Init.DMAContinuousRequests) );
  421. }
  422. /* Update ADC configuration register with previous settings */
  423. MODIFY_REG(hadc->Instance->CFGR,
  424. ADC_CFGR_DISCNUM |
  425. ADC_CFGR_DISCEN |
  426. ADC_CFGR_CONT |
  427. ADC_CFGR_OVRMOD |
  428. ADC_CFGR_EXTSEL |
  429. ADC_CFGR_EXTEN |
  430. ADC_CFGR_ALIGN |
  431. ADC_CFGR_RES ,
  432. tmpCFGR );
  433. /* Configuration of regular group sequencer: */
  434. /* - if scan mode is disabled, regular channels sequence length is set to */
  435. /* 0x00: 1 channel converted (channel on regular rank 1U) */
  436. /* Parameter "NbrOfConversion" is discarded. */
  437. /* Note: Scan mode is not present by hardware on this device, but */
  438. /* emulated by software for alignment over all STM32 devices. */
  439. /* - if scan mode is enabled, regular channels sequence length is set to */
  440. /* parameter "NbrOfConversion" */
  441. if (hadc->Init.ScanConvMode == ADC_SCAN_ENABLE)
  442. {
  443. /* Set number of ranks in regular group sequencer */
  444. MODIFY_REG(hadc->Instance->SQR1 ,
  445. ADC_SQR1_L ,
  446. (hadc->Init.NbrOfConversion - (uint8_t)1U) );
  447. }
  448. else
  449. {
  450. CLEAR_BIT(hadc->Instance->SQR1, ADC_SQR1_L);
  451. }
  452. /* Set ADC error code to none */
  453. ADC_CLEAR_ERRORCODE(hadc);
  454. /* Set the ADC state */
  455. ADC_STATE_CLR_SET(hadc->State,
  456. HAL_ADC_STATE_BUSY_INTERNAL,
  457. HAL_ADC_STATE_READY);
  458. }
  459. else
  460. {
  461. /* Update ADC state machine to error */
  462. ADC_STATE_CLR_SET(hadc->State,
  463. HAL_ADC_STATE_BUSY_INTERNAL,
  464. HAL_ADC_STATE_ERROR_INTERNAL);
  465. tmp_hal_status = HAL_ERROR;
  466. }
  467. /* Return function status */
  468. return tmp_hal_status;
  469. }
  470. #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
  471. /* STM32F302xC || STM32F303xC || STM32F358xx || */
  472. /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
  473. /* STM32F301x8 || STM32F302x8 || STM32F318xx */
  474. #if defined(STM32F373xC) || defined(STM32F378xx)
  475. /**
  476. * @brief Initializes the ADC peripheral and regular group according to
  477. * parameters specified in structure "ADC_InitTypeDef".
  478. * @note As prerequisite, ADC clock must be configured at RCC top level
  479. * (clock source APB2).
  480. * See commented example code below that can be copied and uncommented
  481. * into HAL_ADC_MspInit().
  482. * @note Possibility to update parameters on the fly:
  483. * This function initializes the ADC MSP (HAL_ADC_MspInit()) only when
  484. * coming from ADC state reset. Following calls to this function can
  485. * be used to reconfigure some parameters of ADC_InitTypeDef
  486. * structure on the fly, without modifying MSP configuration. If ADC
  487. * MSP has to be modified again, HAL_ADC_DeInit() must be called
  488. * before HAL_ADC_Init().
  489. * The setting of these parameters is conditioned to ADC state.
  490. * For parameters constraints, see comments of structure
  491. * "ADC_InitTypeDef".
  492. * @note This function configures the ADC within 2 scopes: scope of entire
  493. * ADC and scope of regular group. For parameters details, see comments
  494. * of structure "ADC_InitTypeDef".
  495. * @param hadc ADC handle
  496. * @retval HAL status
  497. */
  498. HAL_StatusTypeDef HAL_ADC_Init(ADC_HandleTypeDef* hadc)
  499. {
  500. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  501. uint32_t tmp_cr1 = 0U;
  502. uint32_t tmp_cr2 = 0U;
  503. uint32_t tmp_sqr1 = 0U;
  504. /* Check ADC handle */
  505. if(hadc == NULL)
  506. {
  507. return HAL_ERROR;
  508. }
  509. /* Check the parameters */
  510. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  511. assert_param(IS_ADC_DATA_ALIGN(hadc->Init.DataAlign));
  512. assert_param(IS_ADC_SCAN_MODE(hadc->Init.ScanConvMode));
  513. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
  514. assert_param(IS_ADC_EXTTRIG(hadc->Init.ExternalTrigConv));
  515. if(hadc->Init.ScanConvMode != ADC_SCAN_DISABLE)
  516. {
  517. assert_param(IS_ADC_REGULAR_NB_CONV(hadc->Init.NbrOfConversion));
  518. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DiscontinuousConvMode));
  519. if(hadc->Init.DiscontinuousConvMode != DISABLE)
  520. {
  521. assert_param(IS_ADC_REGULAR_DISCONT_NUMBER(hadc->Init.NbrOfDiscConversion));
  522. }
  523. }
  524. /* As prerequisite, into HAL_ADC_MspInit(), ADC clock must be configured */
  525. /* at RCC top level. */
  526. /* Refer to header of this file for more details on clock enabling */
  527. /* procedure. */
  528. /* Actions performed only if ADC is coming from state reset: */
  529. /* - Initialization of ADC MSP */
  530. if (hadc->State == HAL_ADC_STATE_RESET)
  531. {
  532. /* Initialize ADC error code */
  533. ADC_CLEAR_ERRORCODE(hadc);
  534. /* Allocate lock resource and initialize it */
  535. hadc->Lock = HAL_UNLOCKED;
  536. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  537. /* Init the ADC Callback settings */
  538. hadc->ConvCpltCallback = HAL_ADC_ConvCpltCallback; /* Legacy weak callback */
  539. hadc->ConvHalfCpltCallback = HAL_ADC_ConvHalfCpltCallback; /* Legacy weak callback */
  540. hadc->LevelOutOfWindowCallback = HAL_ADC_LevelOutOfWindowCallback; /* Legacy weak callback */
  541. hadc->ErrorCallback = HAL_ADC_ErrorCallback; /* Legacy weak callback */
  542. hadc->InjectedConvCpltCallback = HAL_ADCEx_InjectedConvCpltCallback; /* Legacy weak callback */
  543. if (hadc->MspInitCallback == NULL)
  544. {
  545. hadc->MspInitCallback = HAL_ADC_MspInit; /* Legacy weak MspInit */
  546. }
  547. /* Init the low level hardware */
  548. hadc->MspInitCallback(hadc);
  549. #else
  550. /* Init the low level hardware */
  551. HAL_ADC_MspInit(hadc);
  552. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  553. }
  554. /* Stop potential conversion on going, on regular and injected groups */
  555. /* Disable ADC peripheral */
  556. /* Note: In case of ADC already enabled, precaution to not launch an */
  557. /* unwanted conversion while modifying register CR2 by writing 1 to */
  558. /* bit ADON. */
  559. tmp_hal_status = ADC_ConversionStop_Disable(hadc);
  560. /* Configuration of ADC parameters if previous preliminary actions are */
  561. /* correctly completed. */
  562. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL) &&
  563. (tmp_hal_status == HAL_OK) )
  564. {
  565. /* Set ADC state */
  566. ADC_STATE_CLR_SET(hadc->State,
  567. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  568. HAL_ADC_STATE_BUSY_INTERNAL);
  569. /* Set ADC parameters */
  570. /* Configuration of ADC: */
  571. /* - data alignment */
  572. /* - external trigger to start conversion */
  573. /* - external trigger polarity (always set to 1U, because needed for all */
  574. /* triggers: external trigger of SW start) */
  575. /* - continuous conversion mode */
  576. /* Note: External trigger polarity (ADC_CR2_EXTTRIG) is set into */
  577. /* HAL_ADC_Start_xxx functions because if set in this function, */
  578. /* a conversion on injected group would start a conversion also on */
  579. /* regular group after ADC enabling. */
  580. tmp_cr2 |= (hadc->Init.DataAlign |
  581. hadc->Init.ExternalTrigConv |
  582. ADC_CR2_CONTINUOUS((uint32_t)hadc->Init.ContinuousConvMode) );
  583. /* Configuration of ADC: */
  584. /* - scan mode */
  585. /* - discontinuous mode disable/enable */
  586. /* - discontinuous mode number of conversions */
  587. tmp_cr1 |= (ADC_CR1_SCAN_SET(hadc->Init.ScanConvMode));
  588. /* Enable discontinuous mode only if continuous mode is disabled */
  589. /* Note: If parameter "Init.ScanConvMode" is set to disable, parameter */
  590. /* discontinuous is set anyway, but will have no effect on ADC HW. */
  591. if (hadc->Init.DiscontinuousConvMode == ENABLE)
  592. {
  593. if (hadc->Init.ContinuousConvMode == DISABLE)
  594. {
  595. /* Enable the selected ADC regular discontinuous mode */
  596. /* Set the number of channels to be converted in discontinuous mode */
  597. tmp_cr1 |= (ADC_CR1_DISCEN |
  598. ADC_CR1_DISCONTINUOUS_NUM(hadc->Init.NbrOfDiscConversion) );
  599. }
  600. else
  601. {
  602. /* ADC regular group discontinuous was intended to be enabled, */
  603. /* but ADC regular group modes continuous and sequencer discontinuous */
  604. /* cannot be enabled simultaneously. */
  605. /* Update ADC state machine to error */
  606. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  607. /* Set ADC error code to ADC IP internal error */
  608. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  609. }
  610. }
  611. /* Update ADC configuration register CR1 with previous settings */
  612. MODIFY_REG(hadc->Instance->CR1,
  613. ADC_CR1_SCAN |
  614. ADC_CR1_DISCEN |
  615. ADC_CR1_DISCNUM ,
  616. tmp_cr1 );
  617. /* Update ADC configuration register CR2 with previous settings */
  618. MODIFY_REG(hadc->Instance->CR2,
  619. ADC_CR2_ALIGN |
  620. ADC_CR2_EXTSEL |
  621. ADC_CR2_EXTTRIG |
  622. ADC_CR2_CONT ,
  623. tmp_cr2 );
  624. /* Configuration of regular group sequencer: */
  625. /* - if scan mode is disabled, regular channels sequence length is set to */
  626. /* 0x00: 1 channel converted (channel on regular rank 1U) */
  627. /* Parameter "NbrOfConversion" is discarded. */
  628. /* Note: Scan mode is present by hardware on this device and, if */
  629. /* disabled, discards automatically nb of conversions. Anyway, nb of */
  630. /* conversions is forced to 0x00 for alignment over all STM32 devices. */
  631. /* - if scan mode is enabled, regular channels sequence length is set to */
  632. /* parameter "NbrOfConversion" */
  633. if (ADC_CR1_SCAN_SET(hadc->Init.ScanConvMode) == ADC_SCAN_ENABLE)
  634. {
  635. tmp_sqr1 = ADC_SQR1_L_SHIFT(hadc->Init.NbrOfConversion);
  636. }
  637. MODIFY_REG(hadc->Instance->SQR1,
  638. ADC_SQR1_L ,
  639. tmp_sqr1 );
  640. /* Check back that ADC registers have effectively been configured to */
  641. /* ensure of no potential problem of ADC core IP clocking. */
  642. /* Check through register CR2 (excluding bits set in other functions: */
  643. /* execution control bits (ADON, JSWSTART, SWSTART), regular group bits */
  644. /* (DMA), injected group bits (JEXTTRIG and JEXTSEL), channel internal */
  645. /* measurement path bit (TSVREFE). */
  646. if (READ_BIT(hadc->Instance->CR2, ~(ADC_CR2_ADON | ADC_CR2_DMA |
  647. ADC_CR2_SWSTART | ADC_CR2_JSWSTART |
  648. ADC_CR2_JEXTTRIG | ADC_CR2_JEXTSEL |
  649. ADC_CR2_TSVREFE ))
  650. == tmp_cr2)
  651. {
  652. /* Set ADC error code to none */
  653. ADC_CLEAR_ERRORCODE(hadc);
  654. /* Set the ADC state */
  655. ADC_STATE_CLR_SET(hadc->State,
  656. HAL_ADC_STATE_BUSY_INTERNAL,
  657. HAL_ADC_STATE_READY);
  658. }
  659. else
  660. {
  661. /* Update ADC state machine to error */
  662. ADC_STATE_CLR_SET(hadc->State,
  663. HAL_ADC_STATE_BUSY_INTERNAL,
  664. HAL_ADC_STATE_ERROR_INTERNAL);
  665. /* Set ADC error code to ADC IP internal error */
  666. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  667. tmp_hal_status = HAL_ERROR;
  668. }
  669. }
  670. else
  671. {
  672. /* Update ADC state machine to error */
  673. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  674. tmp_hal_status = HAL_ERROR;
  675. }
  676. /* Return function status */
  677. return tmp_hal_status;
  678. }
  679. #endif /* STM32F373xC || STM32F378xx */
  680. #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
  681. defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
  682. defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
  683. defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
  684. /**
  685. * @brief Deinitialize the ADC peripheral registers to their default reset
  686. * values, with deinitialization of the ADC MSP.
  687. * @note For devices with several ADCs: reset of ADC common registers is done
  688. * only if all ADCs sharing the same common group are disabled.
  689. * If this is not the case, reset of these common parameters reset is
  690. * bypassed without error reporting: it can be the intended behaviour in
  691. * case of reset of a single ADC while the other ADCs sharing the same
  692. * common group is still running.
  693. * @note For devices with several ADCs: Global reset of all ADCs sharing a
  694. * common group is possible.
  695. * As this function is intended to reset a single ADC, to not impact
  696. * other ADCs, instructions for global reset of multiple ADCs have been
  697. * let commented below.
  698. * If needed, the example code can be copied and uncommented into
  699. * function HAL_ADC_MspDeInit().
  700. * @param hadc ADC handle
  701. * @retval HAL status
  702. */
  703. HAL_StatusTypeDef HAL_ADC_DeInit(ADC_HandleTypeDef* hadc)
  704. {
  705. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  706. ADC_Common_TypeDef *tmpADC_Common;
  707. ADC_HandleTypeDef tmphadcSharingSameCommonRegister;
  708. /* Check ADC handle */
  709. if(hadc == NULL)
  710. {
  711. return HAL_ERROR;
  712. }
  713. /* Check the parameters */
  714. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  715. /* Set ADC state */
  716. SET_BIT(hadc->State, HAL_ADC_STATE_BUSY_INTERNAL);
  717. /* Stop potential conversion on going, on regular and injected groups */
  718. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_INJECTED_GROUP);
  719. /* Disable ADC peripheral if conversions are effectively stopped */
  720. if (tmp_hal_status == HAL_OK)
  721. {
  722. /* Flush register JSQR: queue sequencer reset when injected queue */
  723. /* sequencer is enabled and ADC disabled. */
  724. /* Enable injected queue sequencer after injected conversion stop */
  725. SET_BIT(hadc->Instance->CFGR, ADC_CFGR_JQM);
  726. /* Disable the ADC peripheral */
  727. tmp_hal_status = ADC_Disable(hadc);
  728. /* Check if ADC is effectively disabled */
  729. if (tmp_hal_status == HAL_OK)
  730. {
  731. /* Change ADC state */
  732. hadc->State = HAL_ADC_STATE_READY;
  733. }
  734. else
  735. {
  736. tmp_hal_status = HAL_ERROR;
  737. }
  738. }
  739. /* Configuration of ADC parameters if previous preliminary actions are */
  740. /* correctly completed. */
  741. if (tmp_hal_status == HAL_OK)
  742. {
  743. /* ========== Reset ADC registers ========== */
  744. /* Reset register IER */
  745. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_AWD3 | ADC_IT_AWD2 | ADC_IT_AWD1 |
  746. ADC_IT_JQOVF | ADC_IT_OVR |
  747. ADC_IT_JEOS | ADC_IT_JEOC |
  748. ADC_IT_EOS | ADC_IT_EOC |
  749. ADC_IT_EOSMP | ADC_IT_RDY ) );
  750. /* Reset register ISR */
  751. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_AWD3 | ADC_FLAG_AWD2 | ADC_FLAG_AWD1 |
  752. ADC_FLAG_JQOVF | ADC_FLAG_OVR |
  753. ADC_FLAG_JEOS | ADC_FLAG_JEOC |
  754. ADC_FLAG_EOS | ADC_FLAG_EOC |
  755. ADC_FLAG_EOSMP | ADC_FLAG_RDY ) );
  756. /* Reset register CR */
  757. /* Bits ADC_CR_JADSTP, ADC_CR_ADSTP, ADC_CR_JADSTART, ADC_CR_ADSTART are */
  758. /* in access mode "read-set": no direct reset applicable. */
  759. /* Reset Calibration mode to default setting (single ended): */
  760. /* Disable voltage regulator: */
  761. /* Note: Voltage regulator disable is conditioned to ADC state disabled: */
  762. /* already done above. */
  763. /* Note: Voltage regulator disable is intended for power saving. */
  764. /* Sequence to disable voltage regulator: */
  765. /* 1. Set the intermediate state before moving the ADC voltage regulator */
  766. /* to disable state. */
  767. CLEAR_BIT(hadc->Instance->CR, ADC_CR_ADVREGEN_1 | ADC_CR_ADVREGEN_0 | ADC_CR_ADCALDIF);
  768. /* 2. Set ADVREGEN bits to 0x10U */
  769. SET_BIT(hadc->Instance->CR, ADC_CR_ADVREGEN_1);
  770. /* Reset register CFGR */
  771. CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_AWD1CH | ADC_CFGR_JAUTO | ADC_CFGR_JAWD1EN |
  772. ADC_CFGR_AWD1EN | ADC_CFGR_AWD1SGL | ADC_CFGR_JQM |
  773. ADC_CFGR_JDISCEN | ADC_CFGR_DISCNUM | ADC_CFGR_DISCEN |
  774. ADC_CFGR_AUTDLY | ADC_CFGR_CONT | ADC_CFGR_OVRMOD |
  775. ADC_CFGR_EXTEN | ADC_CFGR_EXTSEL | ADC_CFGR_ALIGN |
  776. ADC_CFGR_RES | ADC_CFGR_DMACFG | ADC_CFGR_DMAEN );
  777. /* Reset register SMPR1 */
  778. CLEAR_BIT(hadc->Instance->SMPR1, ADC_SMPR1_SMP9 | ADC_SMPR1_SMP8 | ADC_SMPR1_SMP7 |
  779. ADC_SMPR1_SMP6 | ADC_SMPR1_SMP5 | ADC_SMPR1_SMP4 |
  780. ADC_SMPR1_SMP3 | ADC_SMPR1_SMP2 | ADC_SMPR1_SMP1 );
  781. /* Reset register SMPR2 */
  782. CLEAR_BIT(hadc->Instance->SMPR2, ADC_SMPR2_SMP18 | ADC_SMPR2_SMP17 | ADC_SMPR2_SMP16 |
  783. ADC_SMPR2_SMP15 | ADC_SMPR2_SMP14 | ADC_SMPR2_SMP13 |
  784. ADC_SMPR2_SMP12 | ADC_SMPR2_SMP11 | ADC_SMPR2_SMP10 );
  785. /* Reset register TR1 */
  786. CLEAR_BIT(hadc->Instance->TR1, ADC_TR1_HT1 | ADC_TR1_LT1);
  787. /* Reset register TR2 */
  788. CLEAR_BIT(hadc->Instance->TR2, ADC_TR2_HT2 | ADC_TR2_LT2);
  789. /* Reset register TR3 */
  790. CLEAR_BIT(hadc->Instance->TR3, ADC_TR3_HT3 | ADC_TR3_LT3);
  791. /* Reset register SQR1 */
  792. CLEAR_BIT(hadc->Instance->SQR1, ADC_SQR1_SQ4 | ADC_SQR1_SQ3 | ADC_SQR1_SQ2 |
  793. ADC_SQR1_SQ1 | ADC_SQR1_L);
  794. /* Reset register SQR2 */
  795. CLEAR_BIT(hadc->Instance->SQR2, ADC_SQR2_SQ9 | ADC_SQR2_SQ8 | ADC_SQR2_SQ7 |
  796. ADC_SQR2_SQ6 | ADC_SQR2_SQ5);
  797. /* Reset register SQR3 */
  798. CLEAR_BIT(hadc->Instance->SQR3, ADC_SQR3_SQ14 | ADC_SQR3_SQ13 | ADC_SQR3_SQ12 |
  799. ADC_SQR3_SQ11 | ADC_SQR3_SQ10);
  800. /* Reset register SQR4 */
  801. CLEAR_BIT(hadc->Instance->SQR4, ADC_SQR4_SQ16 | ADC_SQR4_SQ15);
  802. /* Reset register DR */
  803. /* bits in access mode read only, no direct reset applicable*/
  804. /* Reset register OFR1 */
  805. CLEAR_BIT(hadc->Instance->OFR1, ADC_OFR1_OFFSET1_EN | ADC_OFR1_OFFSET1_CH | ADC_OFR1_OFFSET1);
  806. /* Reset register OFR2 */
  807. CLEAR_BIT(hadc->Instance->OFR2, ADC_OFR2_OFFSET2_EN | ADC_OFR2_OFFSET2_CH | ADC_OFR2_OFFSET2);
  808. /* Reset register OFR3 */
  809. CLEAR_BIT(hadc->Instance->OFR3, ADC_OFR3_OFFSET3_EN | ADC_OFR3_OFFSET3_CH | ADC_OFR3_OFFSET3);
  810. /* Reset register OFR4 */
  811. CLEAR_BIT(hadc->Instance->OFR4, ADC_OFR4_OFFSET4_EN | ADC_OFR4_OFFSET4_CH | ADC_OFR4_OFFSET4);
  812. /* Reset registers JDR1, JDR2, JDR3, JDR4 */
  813. /* bits in access mode read only, no direct reset applicable*/
  814. /* Reset register AWD2CR */
  815. CLEAR_BIT(hadc->Instance->AWD2CR, ADC_AWD2CR_AWD2CH);
  816. /* Reset register AWD3CR */
  817. CLEAR_BIT(hadc->Instance->AWD3CR, ADC_AWD3CR_AWD3CH);
  818. /* Reset register DIFSEL */
  819. CLEAR_BIT(hadc->Instance->DIFSEL, ADC_DIFSEL_DIFSEL);
  820. /* Reset register CALFACT */
  821. CLEAR_BIT(hadc->Instance->CALFACT, ADC_CALFACT_CALFACT_D | ADC_CALFACT_CALFACT_S);
  822. /* ========== Reset common ADC registers ========== */
  823. /* Pointer to the common control register to which is belonging hadc */
  824. /* (Depending on STM32F3 product, there may be up to 4 ADC and 2 common */
  825. /* control registers) */
  826. tmpADC_Common = ADC_COMMON_REGISTER(hadc);
  827. /* Set handle of the other ADC sharing the same common register */
  828. ADC_COMMON_ADC_OTHER(hadc, &tmphadcSharingSameCommonRegister);
  829. /* Software is allowed to change common parameters only when all ADCs of */
  830. /* the common group are disabled. */
  831. if ((ADC_IS_ENABLE(hadc) == RESET) &&
  832. ( (tmphadcSharingSameCommonRegister.Instance == NULL) ||
  833. (ADC_IS_ENABLE(&tmphadcSharingSameCommonRegister) == RESET) ) )
  834. {
  835. /* Reset configuration of ADC common register CCR:
  836. - clock mode: CKMODE
  837. - multimode related parameters: MDMA, DMACFG, DELAY, MULTI (set into
  838. HAL_ADCEx_MultiModeConfigChannel() )
  839. - internal measurement paths: Vbat, temperature sensor, Vref (set into
  840. HAL_ADC_ConfigChannel() or HAL_ADCEx_InjectedConfigChannel() )
  841. */
  842. CLEAR_BIT(tmpADC_Common->CCR, ADC_CCR_CKMODE |
  843. ADC_CCR_VBATEN |
  844. ADC_CCR_TSEN |
  845. ADC_CCR_VREFEN |
  846. ADC_CCR_MDMA |
  847. ADC_CCR_DMACFG |
  848. ADC_CCR_DELAY |
  849. ADC_CCR_MULTI );
  850. /* Other ADC common registers (CSR, CDR) are in access mode read only,
  851. no direct reset applicable */
  852. }
  853. /* ========== Hard reset and clock disable of ADC peripheral ========== */
  854. /* Into HAL_ADC_MspDeInit(), ADC clock can be hard reset and disabled */
  855. /* at RCC top level. */
  856. /* Refer to header of this file for more details on clock disabling */
  857. /* procedure. */
  858. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  859. if (hadc->MspDeInitCallback == NULL)
  860. {
  861. hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit */
  862. }
  863. /* DeInit the low level hardware */
  864. hadc->MspDeInitCallback(hadc);
  865. #else
  866. /* DeInit the low level hardware */
  867. HAL_ADC_MspDeInit(hadc);
  868. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  869. /* Set ADC error code to none */
  870. ADC_CLEAR_ERRORCODE(hadc);
  871. /* Set ADC state */
  872. hadc->State = HAL_ADC_STATE_RESET;
  873. }
  874. /* Process unlocked */
  875. __HAL_UNLOCK(hadc);
  876. /* Return function status */
  877. return tmp_hal_status;
  878. }
  879. #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
  880. /* STM32F302xC || STM32F303xC || STM32F358xx || */
  881. /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
  882. /* STM32F301x8 || STM32F302x8 || STM32F318xx */
  883. #if defined(STM32F373xC) || defined(STM32F378xx)
  884. /**
  885. * @brief Deinitialize the ADC peripheral registers to its default reset values.
  886. * @param hadc ADC handle
  887. * @retval HAL status
  888. */
  889. HAL_StatusTypeDef HAL_ADC_DeInit(ADC_HandleTypeDef* hadc)
  890. {
  891. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  892. /* Check ADC handle */
  893. if(hadc == NULL)
  894. {
  895. return HAL_ERROR;
  896. }
  897. /* Check the parameters */
  898. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  899. /* Set ADC state */
  900. SET_BIT(hadc->State, HAL_ADC_STATE_BUSY_INTERNAL);
  901. /* Stop potential conversion on going, on regular and injected groups */
  902. /* Disable ADC peripheral */
  903. tmp_hal_status = ADC_ConversionStop_Disable(hadc);
  904. /* Configuration of ADC parameters if previous preliminary actions are */
  905. /* correctly completed. */
  906. if (tmp_hal_status == HAL_OK)
  907. {
  908. /* ========== Reset ADC registers ========== */
  909. /* Reset register SR */
  910. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_AWD | ADC_FLAG_JEOC | ADC_FLAG_EOC |
  911. ADC_FLAG_JSTRT | ADC_FLAG_STRT));
  912. /* Reset register CR1 */
  913. CLEAR_BIT(hadc->Instance->CR1, (ADC_CR1_AWDEN | ADC_CR1_JAWDEN | ADC_CR1_DISCNUM |
  914. ADC_CR1_JDISCEN | ADC_CR1_DISCEN | ADC_CR1_JAUTO |
  915. ADC_CR1_AWDSGL | ADC_CR1_SCAN | ADC_CR1_JEOCIE |
  916. ADC_CR1_AWDIE | ADC_CR1_EOCIE | ADC_CR1_AWDCH ));
  917. /* Reset register CR2 */
  918. CLEAR_BIT(hadc->Instance->CR2, (ADC_CR2_TSVREFE | ADC_CR2_SWSTART | ADC_CR2_JSWSTART |
  919. ADC_CR2_EXTTRIG | ADC_CR2_EXTSEL | ADC_CR2_JEXTTRIG |
  920. ADC_CR2_JEXTSEL | ADC_CR2_ALIGN | ADC_CR2_DMA |
  921. ADC_CR2_RSTCAL | ADC_CR2_CAL | ADC_CR2_CONT |
  922. ADC_CR2_ADON ));
  923. /* Reset register SMPR1 */
  924. CLEAR_BIT(hadc->Instance->SMPR1, (ADC_SMPR1_SMP18 | ADC_SMPR1_SMP17 | ADC_SMPR1_SMP15 |
  925. ADC_SMPR1_SMP15 | ADC_SMPR1_SMP14 | ADC_SMPR1_SMP13 |
  926. ADC_SMPR1_SMP12 | ADC_SMPR1_SMP11 | ADC_SMPR1_SMP10 ));
  927. /* Reset register SMPR2 */
  928. CLEAR_BIT(hadc->Instance->SMPR2, (ADC_SMPR2_SMP9 | ADC_SMPR2_SMP8 | ADC_SMPR2_SMP7 |
  929. ADC_SMPR2_SMP6 | ADC_SMPR2_SMP5 | ADC_SMPR2_SMP4 |
  930. ADC_SMPR2_SMP3 | ADC_SMPR2_SMP2 | ADC_SMPR2_SMP1 |
  931. ADC_SMPR2_SMP0 ));
  932. /* Reset register JOFR1 */
  933. CLEAR_BIT(hadc->Instance->JOFR1, ADC_JOFR1_JOFFSET1);
  934. /* Reset register JOFR2 */
  935. CLEAR_BIT(hadc->Instance->JOFR2, ADC_JOFR2_JOFFSET2);
  936. /* Reset register JOFR3 */
  937. CLEAR_BIT(hadc->Instance->JOFR3, ADC_JOFR3_JOFFSET3);
  938. /* Reset register JOFR4 */
  939. CLEAR_BIT(hadc->Instance->JOFR4, ADC_JOFR4_JOFFSET4);
  940. /* Reset register HTR */
  941. CLEAR_BIT(hadc->Instance->HTR, ADC_HTR_HT);
  942. /* Reset register LTR */
  943. CLEAR_BIT(hadc->Instance->LTR, ADC_LTR_LT);
  944. /* Reset register SQR1 */
  945. CLEAR_BIT(hadc->Instance->SQR1, ADC_SQR1_L |
  946. ADC_SQR1_SQ16 | ADC_SQR1_SQ15 |
  947. ADC_SQR1_SQ14 | ADC_SQR1_SQ13 );
  948. /* Reset register SQR1 */
  949. CLEAR_BIT(hadc->Instance->SQR1, ADC_SQR1_L |
  950. ADC_SQR1_SQ16 | ADC_SQR1_SQ15 |
  951. ADC_SQR1_SQ14 | ADC_SQR1_SQ13 );
  952. /* Reset register SQR2 */
  953. CLEAR_BIT(hadc->Instance->SQR2, ADC_SQR2_SQ12 | ADC_SQR2_SQ11 | ADC_SQR2_SQ10 |
  954. ADC_SQR2_SQ9 | ADC_SQR2_SQ8 | ADC_SQR2_SQ7 );
  955. /* Reset register SQR3 */
  956. CLEAR_BIT(hadc->Instance->SQR3, ADC_SQR3_SQ6 | ADC_SQR3_SQ5 | ADC_SQR3_SQ4 |
  957. ADC_SQR3_SQ3 | ADC_SQR3_SQ2 | ADC_SQR3_SQ1 );
  958. /* Reset register JSQR */
  959. CLEAR_BIT(hadc->Instance->JSQR, ADC_JSQR_JL |
  960. ADC_JSQR_JSQ4 | ADC_JSQR_JSQ3 |
  961. ADC_JSQR_JSQ2 | ADC_JSQR_JSQ1 );
  962. /* Reset register JSQR */
  963. CLEAR_BIT(hadc->Instance->JSQR, ADC_JSQR_JL |
  964. ADC_JSQR_JSQ4 | ADC_JSQR_JSQ3 |
  965. ADC_JSQR_JSQ2 | ADC_JSQR_JSQ1 );
  966. /* Reset register DR */
  967. /* bits in access mode read only, no direct reset applicable*/
  968. /* Reset registers JDR1, JDR2, JDR3, JDR4 */
  969. /* bits in access mode read only, no direct reset applicable*/
  970. /* Reset VBAT measurement path, in case of enabled before by selecting */
  971. /* channel ADC_CHANNEL_VBAT. */
  972. SYSCFG->CFGR1 &= ~(SYSCFG_CFGR1_VBAT);
  973. /* ========== Hard reset ADC peripheral ========== */
  974. /* Performs a global reset of the entire ADC peripheral: ADC state is */
  975. /* forced to a similar state after device power-on. */
  976. /* If needed, copy-paste and uncomment the following reset code into */
  977. /* function "void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)": */
  978. /* */
  979. /* __HAL_RCC_ADC1_FORCE_RESET() */
  980. /* __HAL_RCC_ADC1_RELEASE_RESET() */
  981. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  982. if (hadc->MspDeInitCallback == NULL)
  983. {
  984. hadc->MspDeInitCallback = HAL_ADC_MspDeInit; /* Legacy weak MspDeInit */
  985. }
  986. /* DeInit the low level hardware */
  987. hadc->MspDeInitCallback(hadc);
  988. #else
  989. /* DeInit the low level hardware */
  990. HAL_ADC_MspDeInit(hadc);
  991. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  992. /* Set ADC error code to none */
  993. ADC_CLEAR_ERRORCODE(hadc);
  994. /* Set ADC state */
  995. hadc->State = HAL_ADC_STATE_RESET;
  996. }
  997. /* Process unlocked */
  998. __HAL_UNLOCK(hadc);
  999. /* Return function status */
  1000. return tmp_hal_status;
  1001. }
  1002. #endif /* STM32F373xC || STM32F378xx */
  1003. /**
  1004. * @}
  1005. */
  1006. /** @defgroup ADCEx_Exported_Functions_Group2 ADCEx Input and Output operation functions
  1007. * @brief ADC Extended IO operation functions
  1008. *
  1009. @verbatim
  1010. ===============================================================================
  1011. ##### IO operation functions #####
  1012. ===============================================================================
  1013. [..] This section provides functions allowing to:
  1014. (+) Start conversion of regular group.
  1015. (+) Stop conversion of regular group.
  1016. (+) Poll for conversion complete on regular group.
  1017. (+) Poll for conversion event.
  1018. (+) Get result of regular channel conversion.
  1019. (+) Start conversion of regular group and enable interruptions.
  1020. (+) Stop conversion of regular group and disable interruptions.
  1021. (+) Handle ADC interrupt request
  1022. (+) Start conversion of regular group and enable DMA transfer.
  1023. (+) Stop conversion of regular group and disable ADC DMA transfer.
  1024. (+) Start conversion of injected group.
  1025. (+) Stop conversion of injected group.
  1026. (+) Poll for conversion complete on injected group.
  1027. (+) Get result of injected channel conversion.
  1028. (+) Start conversion of injected group and enable interruptions.
  1029. (+) Stop conversion of injected group and disable interruptions.
  1030. (+) Start multimode and enable DMA transfer.
  1031. (+) Stop multimode and disable ADC DMA transfer.
  1032. (+) Get result of multimode conversion.
  1033. (+) Perform the ADC self-calibration for single or differential ending.
  1034. (+) Get calibration factors for single or differential ending.
  1035. (+) Set calibration factors for single or differential ending.
  1036. @endverbatim
  1037. * @{
  1038. */
  1039. #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
  1040. defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
  1041. defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
  1042. defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
  1043. /**
  1044. * @brief Enables ADC, starts conversion of regular group.
  1045. * Interruptions enabled in this function: None.
  1046. * @note Case of multimode enabled (for devices with several ADCs):
  1047. * if ADC is slave, ADC is enabled only (conversion is not started).
  1048. * if ADC is master, ADC is enabled and multimode conversion is started.
  1049. * @param hadc ADC handle
  1050. * @retval HAL status
  1051. */
  1052. HAL_StatusTypeDef HAL_ADC_Start(ADC_HandleTypeDef* hadc)
  1053. {
  1054. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1055. /* Check the parameters */
  1056. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1057. /* Perform ADC enable and conversion start if no conversion is on going */
  1058. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  1059. {
  1060. /* Process locked */
  1061. __HAL_LOCK(hadc);
  1062. /* Enable the ADC peripheral */
  1063. tmp_hal_status = ADC_Enable(hadc);
  1064. /* Start conversion if ADC is effectively enabled */
  1065. if (tmp_hal_status == HAL_OK)
  1066. {
  1067. /* Set ADC state */
  1068. /* - Clear state bitfield related to regular group conversion results */
  1069. /* - Set state bitfield related to regular operation */
  1070. ADC_STATE_CLR_SET(hadc->State,
  1071. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP,
  1072. HAL_ADC_STATE_REG_BUSY);
  1073. /* Set group injected state (from auto-injection) and multimode state */
  1074. /* for all cases of multimode: independent mode, multimode ADC master */
  1075. /* or multimode ADC slave (for devices with several ADCs): */
  1076. if (ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc))
  1077. {
  1078. /* Set ADC state (ADC independent or master) */
  1079. CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  1080. /* If conversions on group regular are also triggering group injected,*/
  1081. /* update ADC state. */
  1082. if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_JAUTO) != RESET)
  1083. {
  1084. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
  1085. }
  1086. }
  1087. else
  1088. {
  1089. /* Set ADC state (ADC slave) */
  1090. SET_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  1091. /* If conversions on group regular are also triggering group injected,*/
  1092. /* update ADC state. */
  1093. if (ADC_MULTIMODE_AUTO_INJECTED(hadc))
  1094. {
  1095. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
  1096. }
  1097. }
  1098. /* State machine update: Check if an injected conversion is ongoing */
  1099. if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  1100. {
  1101. /* Reset ADC error code fields related to conversions on group regular*/
  1102. CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA));
  1103. }
  1104. else
  1105. {
  1106. /* Reset ADC all error code fields */
  1107. ADC_CLEAR_ERRORCODE(hadc);
  1108. }
  1109. /* Process unlocked */
  1110. /* Unlock before starting ADC conversions: in case of potential */
  1111. /* interruption, to let the process to ADC IRQ Handler. */
  1112. __HAL_UNLOCK(hadc);
  1113. /* Clear regular group conversion flag and overrun flag */
  1114. /* (To ensure of no unknown state from potential previous ADC */
  1115. /* operations) */
  1116. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
  1117. /* Enable conversion of regular group. */
  1118. /* If software start has been selected, conversion starts immediately. */
  1119. /* If external trigger has been selected, conversion will start at next */
  1120. /* trigger event. */
  1121. /* Case of multimode enabled (for devices with several ADCs): */
  1122. /* - if ADC is slave, ADC is enabled only (conversion is not started). */
  1123. /* - if ADC is master, ADC is enabled and conversion is started. */
  1124. if (ADC_NONMULTIMODE_REG_OR_MULTIMODEMASTER(hadc))
  1125. {
  1126. SET_BIT(hadc->Instance->CR, ADC_CR_ADSTART);
  1127. }
  1128. }
  1129. else
  1130. {
  1131. /* Process unlocked */
  1132. __HAL_UNLOCK(hadc);
  1133. }
  1134. }
  1135. else
  1136. {
  1137. tmp_hal_status = HAL_BUSY;
  1138. }
  1139. /* Return function status */
  1140. return tmp_hal_status;
  1141. }
  1142. #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
  1143. /* STM32F302xC || STM32F303xC || STM32F358xx || */
  1144. /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
  1145. /* STM32F301x8 || STM32F302x8 || STM32F318xx */
  1146. #if defined(STM32F373xC) || defined(STM32F378xx)
  1147. /**
  1148. * @brief Enables ADC, starts conversion of regular group.
  1149. * Interruptions enabled in this function: None.
  1150. * @param hadc ADC handle
  1151. * @retval HAL status
  1152. */
  1153. HAL_StatusTypeDef HAL_ADC_Start(ADC_HandleTypeDef* hadc)
  1154. {
  1155. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1156. /* Check the parameters */
  1157. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1158. /* Process locked */
  1159. __HAL_LOCK(hadc);
  1160. /* Enable the ADC peripheral */
  1161. tmp_hal_status = ADC_Enable(hadc);
  1162. /* Start conversion if ADC is effectively enabled */
  1163. if (tmp_hal_status == HAL_OK)
  1164. {
  1165. /* Set ADC state */
  1166. /* - Clear state bitfield related to regular group conversion results */
  1167. /* - Set state bitfield related to regular operation */
  1168. ADC_STATE_CLR_SET(hadc->State,
  1169. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC,
  1170. HAL_ADC_STATE_REG_BUSY);
  1171. /* Set group injected state (from auto-injection) */
  1172. /* If conversions on group regular are also triggering group injected, */
  1173. /* update ADC state. */
  1174. if (READ_BIT(hadc->Instance->CR1, ADC_CR1_JAUTO) != RESET)
  1175. {
  1176. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
  1177. }
  1178. /* State machine update: Check if an injected conversion is ongoing */
  1179. if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  1180. {
  1181. /* Reset ADC error code fields related to conversions on group regular */
  1182. CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA));
  1183. }
  1184. else
  1185. {
  1186. /* Reset ADC all error code fields */
  1187. ADC_CLEAR_ERRORCODE(hadc);
  1188. }
  1189. /* Process unlocked */
  1190. /* Unlock before starting ADC conversions: in case of potential */
  1191. /* interruption, to let the process to ADC IRQ Handler. */
  1192. __HAL_UNLOCK(hadc);
  1193. /* Clear regular group conversion flag and overrun flag */
  1194. /* (To ensure of no unknown state from potential previous ADC operations) */
  1195. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOC);
  1196. /* Enable conversion of regular group. */
  1197. /* If software start has been selected, conversion starts immediately. */
  1198. /* If external trigger has been selected, conversion will start at next */
  1199. /* trigger event. */
  1200. /* Note: Alternate trigger for single conversion could be to force an */
  1201. /* additional set of bit ADON "hadc->Instance->CR2 |= ADC_CR2_ADON;"*/
  1202. if (ADC_IS_SOFTWARE_START_REGULAR(hadc))
  1203. {
  1204. /* Start ADC conversion on regular group with SW start */
  1205. SET_BIT(hadc->Instance->CR2, (ADC_CR2_SWSTART | ADC_CR2_EXTTRIG));
  1206. }
  1207. else
  1208. {
  1209. /* Start ADC conversion on regular group with external trigger */
  1210. SET_BIT(hadc->Instance->CR2, ADC_CR2_EXTTRIG);
  1211. }
  1212. }
  1213. /* Return function status */
  1214. return tmp_hal_status;
  1215. }
  1216. #endif /* STM32F373xC || STM32F378xx */
  1217. #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
  1218. defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
  1219. defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
  1220. defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
  1221. /**
  1222. * @brief Stop ADC conversion of both groups regular and injected,
  1223. * disable ADC peripheral.
  1224. * @note ADC peripheral disable is forcing interruption of potential
  1225. * conversion on injected group. If injected group is under use,
  1226. * it should be preliminarily stopped using function
  1227. * @ref HAL_ADCEx_InjectedStop().
  1228. * To stop ADC conversion only on ADC group regular
  1229. * while letting ADC group injected conversions running,
  1230. * use function @ref HAL_ADCEx_RegularStop().
  1231. * @param hadc ADC handle
  1232. * @retval HAL status.
  1233. */
  1234. HAL_StatusTypeDef HAL_ADC_Stop(ADC_HandleTypeDef* hadc)
  1235. {
  1236. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1237. /* Check the parameters */
  1238. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1239. /* Process locked */
  1240. __HAL_LOCK(hadc);
  1241. /* 1. Stop potential conversion on going, on regular and injected groups */
  1242. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_INJECTED_GROUP);
  1243. /* Disable ADC peripheral if conversions are effectively stopped */
  1244. if (tmp_hal_status == HAL_OK)
  1245. {
  1246. /* 2. Disable the ADC peripheral */
  1247. tmp_hal_status = ADC_Disable(hadc);
  1248. /* Check if ADC is effectively disabled */
  1249. if (tmp_hal_status == HAL_OK)
  1250. {
  1251. /* Set ADC state */
  1252. ADC_STATE_CLR_SET(hadc->State,
  1253. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  1254. HAL_ADC_STATE_READY);
  1255. }
  1256. }
  1257. /* Process unlocked */
  1258. __HAL_UNLOCK(hadc);
  1259. /* Return function status */
  1260. return tmp_hal_status;
  1261. }
  1262. #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
  1263. /* STM32F302xC || STM32F303xC || STM32F358xx || */
  1264. /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
  1265. /* STM32F301x8 || STM32F302x8 || STM32F318xx */
  1266. #if defined(STM32F373xC) || defined(STM32F378xx)
  1267. /**
  1268. * @brief Stop ADC conversion of regular group (and injected channels in
  1269. * case of auto_injection mode), disable ADC peripheral.
  1270. * @note ADC peripheral disable is forcing interruption of potential
  1271. * conversion on injected group. If injected group is under use, it
  1272. * should be preliminarily stopped using HAL_ADCEx_InjectedStop function.
  1273. * @param hadc ADC handle
  1274. * @retval HAL status.
  1275. */
  1276. HAL_StatusTypeDef HAL_ADC_Stop(ADC_HandleTypeDef* hadc)
  1277. {
  1278. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1279. /* Check the parameters */
  1280. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1281. /* Process locked */
  1282. __HAL_LOCK(hadc);
  1283. /* Stop potential conversion on going, on regular and injected groups */
  1284. /* Disable ADC peripheral */
  1285. tmp_hal_status = ADC_ConversionStop_Disable(hadc);
  1286. /* Check if ADC is effectively disabled */
  1287. if (tmp_hal_status == HAL_OK)
  1288. {
  1289. /* Set ADC state */
  1290. ADC_STATE_CLR_SET(hadc->State,
  1291. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  1292. HAL_ADC_STATE_READY);
  1293. }
  1294. /* Process unlocked */
  1295. __HAL_UNLOCK(hadc);
  1296. /* Return function status */
  1297. return tmp_hal_status;
  1298. }
  1299. #endif /* STM32F373xC || STM32F378xx */
  1300. #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
  1301. defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
  1302. defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
  1303. defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
  1304. /**
  1305. * @brief Wait for regular group conversion to be completed.
  1306. * @note ADC conversion flags EOS (end of sequence) and EOC (end of
  1307. * conversion) are cleared by this function, with an exception:
  1308. * if low power feature "LowPowerAutoWait" is enabled, flags are
  1309. * not cleared to not interfere with this feature until data register
  1310. * is read using function HAL_ADC_GetValue().
  1311. * @note This function cannot be used in a particular setup: ADC configured
  1312. * in DMA mode and polling for end of each conversion (ADC init
  1313. * parameter "EOCSelection" set to ADC_EOC_SINGLE_CONV).
  1314. * In this case, DMA resets the flag EOC and polling cannot be
  1315. * performed on each conversion. Nevertheless, polling can still
  1316. * be performed on the complete sequence (ADC init
  1317. * parameter "EOCSelection" set to ADC_EOC_SEQ_CONV).
  1318. * @param hadc ADC handle
  1319. * @param Timeout Timeout value in millisecond.
  1320. * @note Depending on init parameter "EOCSelection", flags EOS or EOC is
  1321. * checked and cleared depending on autodelay status (bit AUTDLY).
  1322. * @retval HAL status
  1323. */
  1324. HAL_StatusTypeDef HAL_ADC_PollForConversion(ADC_HandleTypeDef* hadc, uint32_t Timeout)
  1325. {
  1326. uint32_t tickstart;
  1327. uint32_t tmp_Flag_EOC;
  1328. ADC_Common_TypeDef *tmpADC_Common;
  1329. uint32_t tmp_cfgr = 0x0U;
  1330. /* Check the parameters */
  1331. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1332. /* If end of conversion selected to end of sequence */
  1333. if (hadc->Init.EOCSelection == ADC_EOC_SEQ_CONV)
  1334. {
  1335. tmp_Flag_EOC = ADC_FLAG_EOS;
  1336. }
  1337. /* If end of conversion selected to end of each conversion */
  1338. else /* ADC_EOC_SINGLE_CONV */
  1339. {
  1340. /* Verification that ADC configuration is compliant with polling for */
  1341. /* each conversion: */
  1342. /* Particular case is ADC configured in DMA mode and ADC sequencer with */
  1343. /* several ranks and polling for end of each conversion. */
  1344. /* For code simplicity sake, this particular case is generalized to */
  1345. /* ADC configured in DMA mode and and polling for end of each conversion. */
  1346. /* Pointer to the common control register to which is belonging hadc */
  1347. /* (Depending on STM32F3 product, there may have up to 4 ADC and 2 common */
  1348. /* control registers) */
  1349. tmpADC_Common = ADC_COMMON_REGISTER(hadc);
  1350. /* Check DMA configuration, depending on MultiMode set or not */
  1351. if (READ_BIT(tmpADC_Common->CCR, ADC_CCR_MULTI) == ADC_MODE_INDEPENDENT)
  1352. {
  1353. if (HAL_IS_BIT_SET(hadc->Instance->CFGR, ADC_CFGR_DMAEN))
  1354. {
  1355. /* Update ADC state machine to error */
  1356. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1357. /* Process unlocked */
  1358. __HAL_UNLOCK(hadc);
  1359. return HAL_ERROR;
  1360. }
  1361. }
  1362. else
  1363. {
  1364. /* MultiMode is enabled, Common Control Register MDMA bits must be checked */
  1365. if (READ_BIT(tmpADC_Common->CCR, ADC_CCR_MDMA) != RESET)
  1366. {
  1367. /* Update ADC state machine to error */
  1368. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1369. /* Process unlocked */
  1370. __HAL_UNLOCK(hadc);
  1371. return HAL_ERROR;
  1372. }
  1373. }
  1374. tmp_Flag_EOC = (ADC_FLAG_EOC | ADC_FLAG_EOS);
  1375. }
  1376. /* Get relevant register CFGR in ADC instance of ADC master or slave */
  1377. /* in function of multimode state (for devices with multimode */
  1378. /* available). */
  1379. if(ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc))
  1380. {
  1381. tmp_cfgr = READ_REG(hadc->Instance->CFGR);
  1382. }
  1383. else
  1384. {
  1385. tmp_cfgr = READ_REG(ADC_MASTER_INSTANCE(hadc)->CFGR);
  1386. }
  1387. /* Get tick count */
  1388. tickstart = HAL_GetTick();
  1389. /* Wait until End of Conversion or End of Sequence flag is raised */
  1390. while(HAL_IS_BIT_CLR(hadc->Instance->ISR, tmp_Flag_EOC))
  1391. {
  1392. /* Check if timeout is disabled (set to infinite wait) */
  1393. if(Timeout != HAL_MAX_DELAY)
  1394. {
  1395. if((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
  1396. {
  1397. /* New check to avoid false timeout detection in case of preemption */
  1398. if(HAL_IS_BIT_CLR(hadc->Instance->ISR, tmp_Flag_EOC))
  1399. {
  1400. /* Update ADC state machine to timeout */
  1401. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  1402. /* Process unlocked */
  1403. __HAL_UNLOCK(hadc);
  1404. return HAL_TIMEOUT;
  1405. }
  1406. }
  1407. }
  1408. }
  1409. /* Update ADC state machine */
  1410. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  1411. /* Determine whether any further conversion upcoming on group regular */
  1412. /* by external trigger, continuous mode or scan sequence on going. */
  1413. if(ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  1414. (READ_BIT (tmp_cfgr, ADC_CFGR_CONT) == RESET) )
  1415. {
  1416. /* If End of Sequence is reached, disable interrupts */
  1417. if( __HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOS) )
  1418. {
  1419. /* Allowed to modify bits ADC_IT_EOC/ADC_IT_EOS only if bit */
  1420. /* ADSTART==0 (no conversion on going) */
  1421. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  1422. {
  1423. /* Set ADC state */
  1424. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  1425. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  1426. {
  1427. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  1428. }
  1429. }
  1430. else
  1431. {
  1432. /* Change ADC state to error state */
  1433. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1434. /* Set ADC error code to ADC IP internal error */
  1435. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  1436. }
  1437. }
  1438. }
  1439. /* Clear end of conversion flag of regular group if low power feature */
  1440. /* "LowPowerAutoWait " is disabled, to not interfere with this feature */
  1441. /* until data register is read using function HAL_ADC_GetValue(). */
  1442. if (READ_BIT (tmp_cfgr, ADC_CFGR_AUTDLY) == RESET)
  1443. {
  1444. /* Clear regular group conversion flag */
  1445. /* (EOC or EOS depending on HAL ADC initialization parameter) */
  1446. __HAL_ADC_CLEAR_FLAG(hadc, tmp_Flag_EOC);
  1447. }
  1448. /* Return ADC state */
  1449. return HAL_OK;
  1450. }
  1451. #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
  1452. /* STM32F302xC || STM32F303xC || STM32F358xx || */
  1453. /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
  1454. /* STM32F301x8 || STM32F302x8 || STM32F318xx */
  1455. #if defined(STM32F373xC) || defined(STM32F378xx)
  1456. /**
  1457. * @brief Wait for regular group conversion to be completed.
  1458. * @note This function cannot be used in a particular setup: ADC configured
  1459. * in DMA mode.
  1460. * In this case, DMA resets the flag EOC and polling cannot be
  1461. * performed on each conversion.
  1462. * @note On STM32F37x devices, limitation in case of sequencer enabled
  1463. * (several ranks selected): polling cannot be done on each
  1464. * conversion inside the sequence. In this case, polling is replaced by
  1465. * wait for maximum conversion time.
  1466. * @param hadc ADC handle
  1467. * @param Timeout Timeout value in millisecond.
  1468. * @retval HAL status
  1469. */
  1470. HAL_StatusTypeDef HAL_ADC_PollForConversion(ADC_HandleTypeDef* hadc, uint32_t Timeout)
  1471. {
  1472. uint32_t tickstart;
  1473. /* Variables for polling in case of scan mode enabled */
  1474. uint32_t Conversion_Timeout_CPU_cycles_max = 0U;
  1475. uint32_t Conversion_Timeout_CPU_cycles = 0U;
  1476. /* Check the parameters */
  1477. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1478. /* Verification that ADC configuration is compliant with polling for */
  1479. /* each conversion: */
  1480. /* Particular case is ADC configured in DMA mode */
  1481. if (HAL_IS_BIT_SET(hadc->Instance->CR2, ADC_CR2_DMA))
  1482. {
  1483. /* Update ADC state machine to error */
  1484. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  1485. /* Process unlocked */
  1486. __HAL_UNLOCK(hadc);
  1487. return HAL_ERROR;
  1488. }
  1489. /* Get tick count */
  1490. tickstart = HAL_GetTick();
  1491. /* Polling for end of conversion: differentiation if single/sequence */
  1492. /* conversion. */
  1493. /* - If single conversion for regular group (Scan mode disabled or enabled */
  1494. /* with NbrOfConversion =1U), flag EOC is used to determine the */
  1495. /* conversion completion. */
  1496. /* - If sequence conversion for regular group (scan mode enabled and */
  1497. /* NbrOfConversion >=2U), flag EOC is set only at the end of the */
  1498. /* sequence. */
  1499. /* To poll for each conversion, the maximum conversion time is computed */
  1500. /* from ADC conversion time (selected sampling time + conversion time of */
  1501. /* 12.5 ADC clock cycles) and APB2/ADC clock prescalers (depending on */
  1502. /* settings, conversion time range can be from 28 to 32256 CPU cycles). */
  1503. /* As flag EOC is not set after each conversion, no timeout status can */
  1504. /* be set. */
  1505. if (HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_SCAN) &&
  1506. HAL_IS_BIT_CLR(hadc->Instance->SQR1, ADC_SQR1_L) )
  1507. {
  1508. /* Wait until End of Conversion flag is raised */
  1509. while(HAL_IS_BIT_CLR(hadc->Instance->SR, ADC_FLAG_EOC))
  1510. {
  1511. /* Check if timeout is disabled (set to infinite wait) */
  1512. if(Timeout != HAL_MAX_DELAY)
  1513. {
  1514. if((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
  1515. {
  1516. /* New check to avoid false timeout detection in case of preemption */
  1517. if(HAL_IS_BIT_CLR(hadc->Instance->SR, ADC_FLAG_EOC))
  1518. {
  1519. /* Update ADC state machine to timeout */
  1520. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  1521. /* Process unlocked */
  1522. __HAL_UNLOCK(hadc);
  1523. return HAL_TIMEOUT;
  1524. }
  1525. }
  1526. }
  1527. }
  1528. }
  1529. else
  1530. {
  1531. /* Replace polling by wait for maximum conversion time */
  1532. /* Calculation of CPU cycles corresponding to ADC conversion cycles. */
  1533. /* Retrieve ADC clock prescaler and ADC maximum conversion cycles on all */
  1534. /* channels. */
  1535. Conversion_Timeout_CPU_cycles_max = ADC_CLOCK_PRESCALER_RANGE() ;
  1536. Conversion_Timeout_CPU_cycles_max *= ADC_CONVCYCLES_MAX_RANGE(hadc);
  1537. /* Poll with maximum conversion time */
  1538. while(Conversion_Timeout_CPU_cycles < Conversion_Timeout_CPU_cycles_max)
  1539. {
  1540. /* Check if timeout is disabled (set to infinite wait) */
  1541. if(Timeout != HAL_MAX_DELAY)
  1542. {
  1543. if((Timeout == 0U) || ((HAL_GetTick() - tickstart ) > Timeout))
  1544. {
  1545. /* New check to avoid false timeout detection in case of preemption */
  1546. if(Conversion_Timeout_CPU_cycles < Conversion_Timeout_CPU_cycles_max)
  1547. {
  1548. /* Update ADC state machine to timeout */
  1549. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  1550. /* Process unlocked */
  1551. __HAL_UNLOCK(hadc);
  1552. return HAL_TIMEOUT;
  1553. }
  1554. }
  1555. }
  1556. Conversion_Timeout_CPU_cycles ++;
  1557. }
  1558. }
  1559. /* Clear regular group conversion flag */
  1560. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_STRT | ADC_FLAG_EOC);
  1561. /* Update ADC state machine */
  1562. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  1563. /* Determine whether any further conversion upcoming on group regular */
  1564. /* by external trigger, continuous mode or scan sequence on going. */
  1565. /* Note: On STM32F37x devices, in case of sequencer enabled */
  1566. /* (several ranks selected), end of conversion flag is raised */
  1567. /* at the end of the sequence. */
  1568. if(ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  1569. (hadc->Init.ContinuousConvMode == DISABLE) )
  1570. {
  1571. /* Set ADC state */
  1572. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  1573. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  1574. {
  1575. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  1576. }
  1577. }
  1578. /* Return ADC state */
  1579. return HAL_OK;
  1580. }
  1581. #endif /* STM32F373xC || STM32F378xx */
  1582. #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
  1583. defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
  1584. defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
  1585. defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
  1586. /**
  1587. * @brief Poll for conversion event.
  1588. * @param hadc ADC handle
  1589. * @param EventType the ADC event type.
  1590. * This parameter can be one of the following values:
  1591. * @arg ADC_AWD1_EVENT: ADC Analog watchdog 1 event (main analog watchdog, present on all STM32 devices)
  1592. * @arg ADC_AWD2_EVENT: ADC Analog watchdog 2 event (additional analog watchdog, not present on all STM32 families)
  1593. * @arg ADC_AWD3_EVENT: ADC Analog watchdog 3 event (additional analog watchdog, not present on all STM32 families)
  1594. * @arg ADC_OVR_EVENT: ADC Overrun event
  1595. * @arg ADC_JQOVF_EVENT: ADC Injected context queue overflow event
  1596. * @param Timeout Timeout value in millisecond.
  1597. * @retval HAL status
  1598. */
  1599. HAL_StatusTypeDef HAL_ADC_PollForEvent(ADC_HandleTypeDef* hadc, uint32_t EventType, uint32_t Timeout)
  1600. {
  1601. uint32_t tickstart;
  1602. /* Check the parameters */
  1603. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1604. assert_param(IS_ADC_EVENT_TYPE(EventType));
  1605. /* Get start tick count */
  1606. tickstart = HAL_GetTick();
  1607. /* Check selected event flag */
  1608. while(__HAL_ADC_GET_FLAG(hadc, EventType) == RESET)
  1609. {
  1610. /* Check if timeout is disabled (set to infinite wait) */
  1611. if(Timeout != HAL_MAX_DELAY)
  1612. {
  1613. if((Timeout == 0U) || ((HAL_GetTick() - tickstart ) > Timeout))
  1614. {
  1615. /* New check to avoid false timeout detection in case of preemption */
  1616. if(__HAL_ADC_GET_FLAG(hadc, EventType) == RESET)
  1617. {
  1618. /* Update ADC state machine to timeout */
  1619. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  1620. /* Process unlocked */
  1621. __HAL_UNLOCK(hadc);
  1622. return HAL_TIMEOUT;
  1623. }
  1624. }
  1625. }
  1626. }
  1627. switch(EventType)
  1628. {
  1629. /* Analog watchdog (level out of window) event */
  1630. /* Note: In case of several analog watchdog enabled, if needed to know */
  1631. /* which one triggered and on which ADCx, test ADC state of analog watchdog */
  1632. /* flags HAL_ADC_STATE_AWD1/2U/3 using function "HAL_ADC_GetState()". */
  1633. /* For example: */
  1634. /* " if (HAL_IS_BIT_SET(HAL_ADC_GetState(hadc1), HAL_ADC_STATE_AWD1)) " */
  1635. /* " if (HAL_IS_BIT_SET(HAL_ADC_GetState(hadc1), HAL_ADC_STATE_AWD2)) " */
  1636. /* " if (HAL_IS_BIT_SET(HAL_ADC_GetState(hadc1), HAL_ADC_STATE_AWD3)) " */
  1637. /* Check analog watchdog 1 flag */
  1638. case ADC_AWD_EVENT:
  1639. /* Set ADC state */
  1640. SET_BIT(hadc->State, HAL_ADC_STATE_AWD1);
  1641. /* Clear ADC analog watchdog flag */
  1642. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD1);
  1643. break;
  1644. /* Check analog watchdog 2 flag */
  1645. case ADC_AWD2_EVENT:
  1646. /* Set ADC state */
  1647. SET_BIT(hadc->State, HAL_ADC_STATE_AWD2);
  1648. /* Clear ADC analog watchdog flag */
  1649. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD2);
  1650. break;
  1651. /* Check analog watchdog 3 flag */
  1652. case ADC_AWD3_EVENT:
  1653. /* Set ADC state */
  1654. SET_BIT(hadc->State, HAL_ADC_STATE_AWD3);
  1655. /* Clear ADC analog watchdog flag */
  1656. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD3);
  1657. break;
  1658. /* Injected context queue overflow event */
  1659. case ADC_JQOVF_EVENT:
  1660. /* Set ADC state */
  1661. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_JQOVF);
  1662. /* Set ADC error code to Injected context queue overflow */
  1663. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_JQOVF);
  1664. /* Clear ADC Injected context queue overflow flag */
  1665. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JQOVF);
  1666. break;
  1667. /* Overrun event */
  1668. default: /* Case ADC_OVR_EVENT */
  1669. /* If overrun is set to overwrite previous data, overrun event is not */
  1670. /* considered as an error. */
  1671. /* (cf ref manual "Managing conversions without using the DMA and without */
  1672. /* overrun ") */
  1673. if (hadc->Init.Overrun == ADC_OVR_DATA_PRESERVED)
  1674. {
  1675. /* Set ADC state */
  1676. SET_BIT(hadc->State, HAL_ADC_STATE_REG_OVR);
  1677. /* Set ADC error code to overrun */
  1678. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_OVR);
  1679. }
  1680. /* Clear ADC Overrun flag */
  1681. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  1682. break;
  1683. }
  1684. /* Return ADC state */
  1685. return HAL_OK;
  1686. }
  1687. #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
  1688. /* STM32F302xC || STM32F303xC || STM32F358xx || */
  1689. /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
  1690. /* STM32F301x8 || STM32F302x8 || STM32F318xx */
  1691. #if defined(STM32F373xC) || defined(STM32F378xx)
  1692. /**
  1693. * @brief Poll for conversion event.
  1694. * @param hadc ADC handle
  1695. * @param EventType the ADC event type.
  1696. * This parameter can be one of the following values:
  1697. * @arg ADC_AWD_EVENT: ADC Analog watchdog event.
  1698. * @param Timeout Timeout value in millisecond.
  1699. * @retval HAL status
  1700. */
  1701. HAL_StatusTypeDef HAL_ADC_PollForEvent(ADC_HandleTypeDef* hadc, uint32_t EventType, uint32_t Timeout)
  1702. {
  1703. uint32_t tickstart;
  1704. /* Check the parameters */
  1705. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1706. assert_param(IS_ADC_EVENT_TYPE(EventType));
  1707. tickstart = HAL_GetTick();
  1708. /* Check selected event flag */
  1709. while(__HAL_ADC_GET_FLAG(hadc, EventType) == RESET)
  1710. {
  1711. /* Check if timeout is disabled (set to infinite wait) */
  1712. if(Timeout != HAL_MAX_DELAY)
  1713. {
  1714. if((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
  1715. {
  1716. /* New check to avoid false timeout detection in case of preemption */
  1717. if(__HAL_ADC_GET_FLAG(hadc, EventType) == RESET)
  1718. {
  1719. /* Update ADC state machine to timeout */
  1720. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  1721. /* Process unlocked */
  1722. __HAL_UNLOCK(hadc);
  1723. return HAL_ERROR;
  1724. }
  1725. }
  1726. }
  1727. }
  1728. /* Analog watchdog (level out of window) event */
  1729. /* Set ADC state */
  1730. SET_BIT(hadc->State, HAL_ADC_STATE_AWD1);
  1731. /* Clear ADC analog watchdog flag */
  1732. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD);
  1733. /* Return ADC state */
  1734. return HAL_OK;
  1735. }
  1736. #endif /* STM32F373xC || STM32F378xx */
  1737. #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
  1738. defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
  1739. defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
  1740. defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
  1741. /**
  1742. * @brief Enables ADC, starts conversion of regular group with interruption.
  1743. * Interruptions enabled in this function:
  1744. * - EOC (end of conversion of regular group) or EOS (end of
  1745. * sequence of regular group) depending on ADC initialization
  1746. * parameter "EOCSelection"
  1747. * - overrun, depending on ADC initialization parameter "Overrun"
  1748. * Each of these interruptions has its dedicated callback function.
  1749. * @note Case of multimode enabled (for devices with several ADCs): This
  1750. * function must be called for ADC slave first, then ADC master.
  1751. * For ADC slave, ADC is enabled only (conversion is not started).
  1752. * For ADC master, ADC is enabled and multimode conversion is started.
  1753. * @param hadc ADC handle
  1754. * @retval HAL status
  1755. */
  1756. HAL_StatusTypeDef HAL_ADC_Start_IT(ADC_HandleTypeDef* hadc)
  1757. {
  1758. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1759. /* Check the parameters */
  1760. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1761. /* Perform ADC enable and conversion start if no conversion is on going */
  1762. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  1763. {
  1764. /* Process locked */
  1765. __HAL_LOCK(hadc);
  1766. /* Enable the ADC peripheral */
  1767. tmp_hal_status = ADC_Enable(hadc);
  1768. /* Start conversion if ADC is effectively enabled */
  1769. if (tmp_hal_status == HAL_OK)
  1770. {
  1771. /* Set ADC state */
  1772. /* - Clear state bitfield related to regular group conversion results */
  1773. /* - Set state bitfield related to regular operation */
  1774. ADC_STATE_CLR_SET(hadc->State,
  1775. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP,
  1776. HAL_ADC_STATE_REG_BUSY);
  1777. /* Set group injected state (from auto-injection) and multimode state */
  1778. /* for all cases of multimode: independent mode, multimode ADC master */
  1779. /* or multimode ADC slave (for devices with several ADCs): */
  1780. if (ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc))
  1781. {
  1782. /* Set ADC state (ADC independent or master) */
  1783. CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  1784. /* If conversions on group regular are also triggering group injected,*/
  1785. /* update ADC state. */
  1786. if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_JAUTO) != RESET)
  1787. {
  1788. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
  1789. }
  1790. }
  1791. else
  1792. {
  1793. /* Set ADC state (ADC slave) */
  1794. SET_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  1795. /* If conversions on group regular are also triggering group injected,*/
  1796. /* update ADC state. */
  1797. if (ADC_MULTIMODE_AUTO_INJECTED(hadc))
  1798. {
  1799. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
  1800. }
  1801. }
  1802. /* State machine update: Check if an injected conversion is ongoing */
  1803. if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  1804. {
  1805. /* Reset ADC error code fields related to conversions on group regular*/
  1806. CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA));
  1807. }
  1808. else
  1809. {
  1810. /* Reset ADC all error code fields */
  1811. ADC_CLEAR_ERRORCODE(hadc);
  1812. }
  1813. /* Process unlocked */
  1814. /* Unlock before starting ADC conversions: in case of potential */
  1815. /* interruption, to let the process to ADC IRQ Handler. */
  1816. __HAL_UNLOCK(hadc);
  1817. /* Clear regular group conversion flag and overrun flag */
  1818. /* (To ensure of no unknown state from potential previous ADC */
  1819. /* operations) */
  1820. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
  1821. /* Enable ADC end of conversion interrupt */
  1822. /* Enable ADC overrun interrupt */
  1823. switch(hadc->Init.EOCSelection)
  1824. {
  1825. case ADC_EOC_SEQ_CONV:
  1826. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC);
  1827. __HAL_ADC_ENABLE_IT(hadc, (ADC_IT_EOS));
  1828. break;
  1829. /* case ADC_EOC_SINGLE_CONV */
  1830. default:
  1831. __HAL_ADC_ENABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_EOS));
  1832. break;
  1833. }
  1834. /* If overrun is set to overwrite previous data (default setting), */
  1835. /* overrun interrupt is not activated (overrun event is not considered */
  1836. /* as an error). */
  1837. /* (cf ref manual "Managing conversions without using the DMA and */
  1838. /* without overrun ") */
  1839. if (hadc->Init.Overrun == ADC_OVR_DATA_PRESERVED)
  1840. {
  1841. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
  1842. }
  1843. /* Enable conversion of regular group. */
  1844. /* If software start has been selected, conversion starts immediately. */
  1845. /* If external trigger has been selected, conversion will start at next */
  1846. /* trigger event. */
  1847. /* Case of multimode enabled (for devices with several ADCs): */
  1848. /* - if ADC is slave, ADC is enabled only (conversion is not started). */
  1849. /* - if ADC is master, ADC is enabled and conversion is started. */
  1850. if (ADC_NONMULTIMODE_REG_OR_MULTIMODEMASTER(hadc))
  1851. {
  1852. SET_BIT(hadc->Instance->CR, ADC_CR_ADSTART);
  1853. }
  1854. }
  1855. else
  1856. {
  1857. /* Process unlocked */
  1858. __HAL_UNLOCK(hadc);
  1859. }
  1860. }
  1861. else
  1862. {
  1863. tmp_hal_status = HAL_BUSY;
  1864. }
  1865. /* Return function status */
  1866. return tmp_hal_status;
  1867. }
  1868. #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
  1869. /* STM32F302xC || STM32F303xC || STM32F358xx || */
  1870. /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
  1871. /* STM32F301x8 || STM32F302x8 || STM32F318xx */
  1872. #if defined(STM32F373xC) || defined(STM32F378xx)
  1873. /**
  1874. * @brief Enables ADC, starts conversion of regular group with interruption.
  1875. * Interruptions enabled in this function:
  1876. * - EOC (end of conversion of regular group)
  1877. * Each of these interruptions has its dedicated callback function.
  1878. * @param hadc ADC handle
  1879. * @retval HAL status
  1880. */
  1881. HAL_StatusTypeDef HAL_ADC_Start_IT(ADC_HandleTypeDef* hadc)
  1882. {
  1883. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1884. /* Check the parameters */
  1885. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1886. /* Process locked */
  1887. __HAL_LOCK(hadc);
  1888. /* Enable the ADC peripheral */
  1889. tmp_hal_status = ADC_Enable(hadc);
  1890. /* Start conversion if ADC is effectively enabled */
  1891. if (tmp_hal_status == HAL_OK)
  1892. {
  1893. /* Set ADC state */
  1894. /* - Clear state bitfield related to regular group conversion results */
  1895. /* - Set state bitfield related to regular operation */
  1896. ADC_STATE_CLR_SET(hadc->State,
  1897. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC,
  1898. HAL_ADC_STATE_REG_BUSY);
  1899. /* Set group injected state (from auto-injection) */
  1900. /* If conversions on group regular are also triggering group injected, */
  1901. /* update ADC state. */
  1902. if (READ_BIT(hadc->Instance->CR1, ADC_CR1_JAUTO) != RESET)
  1903. {
  1904. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
  1905. }
  1906. /* State machine update: Check if an injected conversion is ongoing */
  1907. if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  1908. {
  1909. /* Reset ADC error code fields related to conversions on group regular */
  1910. CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA));
  1911. }
  1912. else
  1913. {
  1914. /* Reset ADC all error code fields */
  1915. ADC_CLEAR_ERRORCODE(hadc);
  1916. }
  1917. /* Process unlocked */
  1918. /* Unlock before starting ADC conversions: in case of potential */
  1919. /* interruption, to let the process to ADC IRQ Handler. */
  1920. __HAL_UNLOCK(hadc);
  1921. /* Clear regular group conversion flag and overrun flag */
  1922. /* (To ensure of no unknown state from potential previous ADC operations) */
  1923. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOC);
  1924. /* Enable end of conversion interrupt for regular group */
  1925. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_EOC);
  1926. /* Enable conversion of regular group. */
  1927. /* If software start has been selected, conversion starts immediately. */
  1928. /* If external trigger has been selected, conversion will start at next */
  1929. /* trigger event. */
  1930. if (ADC_IS_SOFTWARE_START_REGULAR(hadc))
  1931. {
  1932. /* Start ADC conversion on regular group with SW start */
  1933. SET_BIT(hadc->Instance->CR2, (ADC_CR2_SWSTART | ADC_CR2_EXTTRIG));
  1934. }
  1935. else
  1936. {
  1937. /* Start ADC conversion on regular group with external trigger */
  1938. SET_BIT(hadc->Instance->CR2, ADC_CR2_EXTTRIG);
  1939. }
  1940. }
  1941. /* Return function status */
  1942. return tmp_hal_status;
  1943. }
  1944. #endif /* STM32F373xC || STM32F378xx */
  1945. #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
  1946. defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
  1947. defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
  1948. defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
  1949. /**
  1950. * @brief Stop ADC conversion of both groups regular and injected,
  1951. * disable ADC peripheral.
  1952. * Interruptions disabled in this function:
  1953. * - EOC (end of conversion of regular group) and EOS (end of
  1954. * sequence of regular group)
  1955. * - overrun
  1956. * @note ADC peripheral disable is forcing interruption of potential
  1957. * conversion on injected group. If injected group is under use,
  1958. * it should be preliminarily stopped using function
  1959. * @ref HAL_ADCEx_InjectedStop().
  1960. * To stop ADC conversion only on ADC group regular
  1961. * while letting ADC group injected conversions running,
  1962. * use function @ref HAL_ADCEx_RegularStop_IT().
  1963. * @param hadc ADC handle
  1964. * @retval HAL status.
  1965. */
  1966. HAL_StatusTypeDef HAL_ADC_Stop_IT(ADC_HandleTypeDef* hadc)
  1967. {
  1968. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  1969. /* Check the parameters */
  1970. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  1971. /* Process locked */
  1972. __HAL_LOCK(hadc);
  1973. /* 1. Stop potential conversion on going, on regular and injected groups */
  1974. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_INJECTED_GROUP);
  1975. /* Disable ADC peripheral if conversions are effectively stopped */
  1976. if (tmp_hal_status == HAL_OK)
  1977. {
  1978. /* Disable ADC end of conversion interrupt for regular group */
  1979. /* Disable ADC overrun interrupt */
  1980. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_EOS | ADC_IT_OVR));
  1981. /* 2. Disable the ADC peripheral */
  1982. tmp_hal_status = ADC_Disable(hadc);
  1983. /* Check if ADC is effectively disabled */
  1984. if (tmp_hal_status == HAL_OK)
  1985. {
  1986. /* Set ADC state */
  1987. ADC_STATE_CLR_SET(hadc->State,
  1988. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  1989. HAL_ADC_STATE_READY);
  1990. }
  1991. }
  1992. /* Process unlocked */
  1993. __HAL_UNLOCK(hadc);
  1994. /* Return function status */
  1995. return tmp_hal_status;
  1996. }
  1997. #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
  1998. /* STM32F302xC || STM32F303xC || STM32F358xx || */
  1999. /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
  2000. /* STM32F301x8 || STM32F302x8 || STM32F318xx */
  2001. #if defined(STM32F373xC) || defined(STM32F378xx)
  2002. /**
  2003. * @brief Stop ADC conversion of regular group (and injected group in
  2004. * case of auto_injection mode), disable interrution of
  2005. * end-of-conversion, disable ADC peripheral.
  2006. * @param hadc ADC handle
  2007. * @retval None
  2008. */
  2009. HAL_StatusTypeDef HAL_ADC_Stop_IT(ADC_HandleTypeDef* hadc)
  2010. {
  2011. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  2012. /* Check the parameters */
  2013. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2014. /* Process locked */
  2015. __HAL_LOCK(hadc);
  2016. /* Stop potential conversion on going, on regular and injected groups */
  2017. /* Disable ADC peripheral */
  2018. tmp_hal_status = ADC_ConversionStop_Disable(hadc);
  2019. /* Check if ADC is effectively disabled */
  2020. if (tmp_hal_status == HAL_OK)
  2021. {
  2022. /* Disable ADC end of conversion interrupt for regular group */
  2023. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC);
  2024. /* Set ADC state */
  2025. ADC_STATE_CLR_SET(hadc->State,
  2026. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  2027. HAL_ADC_STATE_READY);
  2028. }
  2029. /* Process unlocked */
  2030. __HAL_UNLOCK(hadc);
  2031. /* Return function status */
  2032. return tmp_hal_status;
  2033. }
  2034. #endif /* STM32F373xC || STM32F378xx */
  2035. #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
  2036. defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
  2037. defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
  2038. defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
  2039. /**
  2040. * @brief Enables ADC, starts conversion of regular group and transfers result
  2041. * through DMA.
  2042. * Interruptions enabled in this function:
  2043. * - DMA transfer complete
  2044. * - DMA half transfer
  2045. * - overrun
  2046. * Each of these interruptions has its dedicated callback function.
  2047. * @note Case of multimode enabled (for devices with several ADCs): This
  2048. * function is for single-ADC mode only. For multimode, use the
  2049. * dedicated MultimodeStart function.
  2050. * @param hadc ADC handle
  2051. * @param pData The destination Buffer address.
  2052. * @param Length The length of data to be transferred from ADC peripheral to memory.
  2053. * @retval None
  2054. */
  2055. HAL_StatusTypeDef HAL_ADC_Start_DMA(ADC_HandleTypeDef* hadc, uint32_t* pData, uint32_t Length)
  2056. {
  2057. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  2058. /* Check the parameters */
  2059. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2060. /* Perform ADC enable and conversion start if no conversion is on going */
  2061. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  2062. {
  2063. /* Process locked */
  2064. __HAL_LOCK(hadc);
  2065. /* Verification if multimode is disabled (for devices with several ADC) */
  2066. /* If multimode is enabled, dedicated function multimode conversion */
  2067. /* start DMA must be used. */
  2068. if(ADC_COMMON_CCR_MULTI(hadc) == RESET)
  2069. {
  2070. /* Enable the ADC peripheral */
  2071. tmp_hal_status = ADC_Enable(hadc);
  2072. /* Start conversion if ADC is effectively enabled */
  2073. if (tmp_hal_status == HAL_OK)
  2074. {
  2075. /* Set ADC state */
  2076. /* - Clear state bitfield related to regular group conversion results */
  2077. /* - Set state bitfield related to regular operation */
  2078. ADC_STATE_CLR_SET(hadc->State,
  2079. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP,
  2080. HAL_ADC_STATE_REG_BUSY);
  2081. /* Set group injected state (from auto-injection) and multimode state */
  2082. /* for all cases of multimode: independent mode, multimode ADC master */
  2083. /* or multimode ADC slave (for devices with several ADCs): */
  2084. if (ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc))
  2085. {
  2086. /* Set ADC state (ADC independent or master) */
  2087. CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  2088. /* If conversions on group regular are also triggering group injected,*/
  2089. /* update ADC state. */
  2090. if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_JAUTO) != RESET)
  2091. {
  2092. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
  2093. }
  2094. }
  2095. else
  2096. {
  2097. /* Set ADC state (ADC slave) */
  2098. SET_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  2099. /* If conversions on group regular are also triggering group injected,*/
  2100. /* update ADC state. */
  2101. if (ADC_MULTIMODE_AUTO_INJECTED(hadc))
  2102. {
  2103. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
  2104. }
  2105. }
  2106. /* State machine update: Check if an injected conversion is ongoing */
  2107. if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  2108. {
  2109. /* Reset ADC error code fields related to conversions on group regular*/
  2110. CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA));
  2111. }
  2112. else
  2113. {
  2114. /* Reset ADC all error code fields */
  2115. ADC_CLEAR_ERRORCODE(hadc);
  2116. }
  2117. /* Process unlocked */
  2118. /* Unlock before starting ADC conversions: in case of potential */
  2119. /* interruption, to let the process to ADC IRQ Handler. */
  2120. __HAL_UNLOCK(hadc);
  2121. /* Set the DMA transfer complete callback */
  2122. hadc->DMA_Handle->XferCpltCallback = ADC_DMAConvCplt;
  2123. /* Set the DMA half transfer complete callback */
  2124. hadc->DMA_Handle->XferHalfCpltCallback = ADC_DMAHalfConvCplt;
  2125. /* Set the DMA error callback */
  2126. hadc->DMA_Handle->XferErrorCallback = ADC_DMAError;
  2127. /* Manage ADC and DMA start: ADC overrun interruption, DMA start, ADC */
  2128. /* start (in case of SW start): */
  2129. /* Clear regular group conversion flag and overrun flag */
  2130. /* (To ensure of no unknown state from potential previous ADC */
  2131. /* operations) */
  2132. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
  2133. /* Enable ADC overrun interrupt */
  2134. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);
  2135. /* Enable ADC DMA mode */
  2136. SET_BIT(hadc->Instance->CFGR, ADC_CFGR_DMAEN);
  2137. /* Start the DMA channel */
  2138. HAL_DMA_Start_IT(hadc->DMA_Handle, (uint32_t)&hadc->Instance->DR, (uint32_t)pData, Length);
  2139. /* Enable conversion of regular group. */
  2140. /* If software start has been selected, conversion starts immediately.*/
  2141. /* If external trigger has been selected, conversion will start at */
  2142. /* next trigger event. */
  2143. SET_BIT(hadc->Instance->CR, ADC_CR_ADSTART);
  2144. }
  2145. else
  2146. {
  2147. /* Process unlocked */
  2148. __HAL_UNLOCK(hadc);
  2149. }
  2150. }
  2151. else
  2152. {
  2153. tmp_hal_status = HAL_ERROR;
  2154. /* Process unlocked */
  2155. __HAL_UNLOCK(hadc);
  2156. }
  2157. }
  2158. else
  2159. {
  2160. tmp_hal_status = HAL_BUSY;
  2161. }
  2162. /* Return function status */
  2163. return tmp_hal_status;
  2164. }
  2165. #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
  2166. /* STM32F302xC || STM32F303xC || STM32F358xx || */
  2167. /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
  2168. /* STM32F301x8 || STM32F302x8 || STM32F318xx */
  2169. #if defined(STM32F373xC) || defined(STM32F378xx)
  2170. /**
  2171. * @brief Enables ADC, starts conversion of regular group and transfers result
  2172. * through DMA.
  2173. * Interruptions enabled in this function:
  2174. * - DMA transfer complete
  2175. * - DMA half transfer
  2176. * Each of these interruptions has its dedicated callback function.
  2177. * @note For devices with several ADCs: This function is for single-ADC mode
  2178. * only. For multimode, use the dedicated MultimodeStart function.
  2179. * @param hadc ADC handle
  2180. * @param pData The destination Buffer address.
  2181. * @param Length The length of data to be transferred from ADC peripheral to memory.
  2182. * @retval None
  2183. */
  2184. HAL_StatusTypeDef HAL_ADC_Start_DMA(ADC_HandleTypeDef* hadc, uint32_t* pData, uint32_t Length)
  2185. {
  2186. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  2187. /* Check the parameters */
  2188. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2189. /* Process locked */
  2190. __HAL_LOCK(hadc);
  2191. /* Enable the ADC peripheral */
  2192. tmp_hal_status = ADC_Enable(hadc);
  2193. /* Start conversion if ADC is effectively enabled */
  2194. if (tmp_hal_status == HAL_OK)
  2195. {
  2196. /* Set ADC state */
  2197. /* - Clear state bitfield related to regular group conversion results */
  2198. /* - Set state bitfield related to regular operation */
  2199. ADC_STATE_CLR_SET(hadc->State,
  2200. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC,
  2201. HAL_ADC_STATE_REG_BUSY);
  2202. /* Set group injected state (from auto-injection) */
  2203. /* If conversions on group regular are also triggering group injected, */
  2204. /* update ADC state. */
  2205. if (READ_BIT(hadc->Instance->CR1, ADC_CR1_JAUTO) != RESET)
  2206. {
  2207. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
  2208. }
  2209. /* State machine update: Check if an injected conversion is ongoing */
  2210. if (HAL_IS_BIT_SET(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  2211. {
  2212. /* Reset ADC error code fields related to conversions on group regular */
  2213. CLEAR_BIT(hadc->ErrorCode, (HAL_ADC_ERROR_OVR | HAL_ADC_ERROR_DMA));
  2214. }
  2215. else
  2216. {
  2217. /* Reset ADC all error code fields */
  2218. ADC_CLEAR_ERRORCODE(hadc);
  2219. }
  2220. /* Process unlocked */
  2221. /* Unlock before starting ADC conversions: in case of potential */
  2222. /* interruption, to let the process to ADC IRQ Handler. */
  2223. __HAL_UNLOCK(hadc);
  2224. /* Set the DMA transfer complete callback */
  2225. hadc->DMA_Handle->XferCpltCallback = ADC_DMAConvCplt;
  2226. /* Set the DMA half transfer complete callback */
  2227. hadc->DMA_Handle->XferHalfCpltCallback = ADC_DMAHalfConvCplt;
  2228. /* Set the DMA error callback */
  2229. hadc->DMA_Handle->XferErrorCallback = ADC_DMAError;
  2230. /* Manage ADC and DMA start: ADC overrun interruption, DMA start, ADC */
  2231. /* start (in case of SW start): */
  2232. /* Clear regular group conversion flag and overrun flag */
  2233. /* (To ensure of no unknown state from potential previous ADC operations) */
  2234. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_EOC);
  2235. /* Enable ADC DMA mode */
  2236. hadc->Instance->CR2 |= ADC_CR2_DMA;
  2237. /* Start the DMA channel */
  2238. HAL_DMA_Start_IT(hadc->DMA_Handle, (uint32_t)&hadc->Instance->DR, (uint32_t)pData, Length);
  2239. /* Enable conversion of regular group. */
  2240. /* If software start has been selected, conversion starts immediately. */
  2241. /* If external trigger has been selected, conversion will start at next */
  2242. /* trigger event. */
  2243. /* Note: Alternate trigger for single conversion could be to force an */
  2244. /* additional set of bit ADON "hadc->Instance->CR2 |= ADC_CR2_ADON;"*/
  2245. if (ADC_IS_SOFTWARE_START_REGULAR(hadc))
  2246. {
  2247. /* Start ADC conversion on regular group with SW start */
  2248. SET_BIT(hadc->Instance->CR2, (ADC_CR2_SWSTART | ADC_CR2_EXTTRIG));
  2249. }
  2250. else
  2251. {
  2252. /* Start ADC conversion on regular group with external trigger */
  2253. SET_BIT(hadc->Instance->CR2, ADC_CR2_EXTTRIG);
  2254. }
  2255. }
  2256. /* Return function status */
  2257. return tmp_hal_status;
  2258. }
  2259. #endif /* STM32F373xC || STM32F378xx */
  2260. #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
  2261. defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
  2262. defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
  2263. defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
  2264. /**
  2265. * @brief Stop ADC conversion of both groups regular and injected,
  2266. * disable ADC DMA transfer, disable ADC peripheral.
  2267. * Interruptions disabled in this function:
  2268. * - DMA transfer complete
  2269. * - DMA half transfer
  2270. * - overrun
  2271. * @note ADC peripheral disable is forcing interruption of potential
  2272. * conversion on injected group. If injected group is under use,
  2273. * it should be preliminarily stopped using function
  2274. * @ref HAL_ADCEx_InjectedStop().
  2275. * To stop ADC conversion only on ADC group regular
  2276. * while letting ADC group injected conversions running,
  2277. * use function @ref HAL_ADCEx_RegularStop_DMA().
  2278. * @note Case of multimode enabled (for devices with several ADCs): This
  2279. * function is for single-ADC mode only. For multimode, use the
  2280. * dedicated MultimodeStop function.
  2281. * @param hadc ADC handle
  2282. * @retval HAL status.
  2283. */
  2284. HAL_StatusTypeDef HAL_ADC_Stop_DMA(ADC_HandleTypeDef* hadc)
  2285. {
  2286. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  2287. /* Check the parameters */
  2288. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2289. /* Process locked */
  2290. __HAL_LOCK(hadc);
  2291. /* 1. Stop potential conversion on going, on regular and injected groups */
  2292. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_INJECTED_GROUP);
  2293. /* Disable ADC peripheral if conversions are effectively stopped */
  2294. if (tmp_hal_status == HAL_OK)
  2295. {
  2296. /* Disable ADC DMA (ADC DMA configuration ADC_CFGR_DMACFG is kept) */
  2297. CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_DMAEN);
  2298. /* Disable the DMA channel (in case of DMA in circular mode or stop while */
  2299. /* while DMA transfer is on going) */
  2300. if (hadc->DMA_Handle->State == HAL_DMA_STATE_BUSY)
  2301. {
  2302. tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
  2303. /* Check if DMA channel effectively disabled */
  2304. if (tmp_hal_status != HAL_OK)
  2305. {
  2306. /* Update ADC state machine to error */
  2307. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  2308. }
  2309. }
  2310. /* Disable ADC overrun interrupt */
  2311. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
  2312. /* 2. Disable the ADC peripheral */
  2313. /* Update "tmp_hal_status" only if DMA channel disabling passed, */
  2314. /* to retain a potential failing status. */
  2315. if (tmp_hal_status == HAL_OK)
  2316. {
  2317. tmp_hal_status = ADC_Disable(hadc);
  2318. }
  2319. else
  2320. {
  2321. ADC_Disable(hadc);
  2322. }
  2323. /* Check if ADC is effectively disabled */
  2324. if (tmp_hal_status == HAL_OK)
  2325. {
  2326. /* Set ADC state */
  2327. ADC_STATE_CLR_SET(hadc->State,
  2328. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  2329. HAL_ADC_STATE_READY);
  2330. }
  2331. }
  2332. /* Process unlocked */
  2333. __HAL_UNLOCK(hadc);
  2334. /* Return function status */
  2335. return tmp_hal_status;
  2336. }
  2337. #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
  2338. /* STM32F302xC || STM32F303xC || STM32F358xx || */
  2339. /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
  2340. /* STM32F301x8 || STM32F302x8 || STM32F318xx */
  2341. #if defined(STM32F373xC) || defined(STM32F378xx)
  2342. /**
  2343. * @brief Stop ADC conversion of regular group (and injected group in
  2344. * case of auto_injection mode), disable ADC DMA transfer, disable
  2345. * ADC peripheral.
  2346. * @note ADC peripheral disable is forcing interruption of potential
  2347. * conversion on injected group. If injected group is under use, it
  2348. * should be preliminarily stopped using HAL_ADCEx_InjectedStop function.
  2349. * @note For devices with several ADCs: This function is for single-ADC mode
  2350. * only. For multimode, use the dedicated MultimodeStop function.
  2351. * @param hadc ADC handle
  2352. * @retval HAL status.
  2353. */
  2354. HAL_StatusTypeDef HAL_ADC_Stop_DMA(ADC_HandleTypeDef* hadc)
  2355. {
  2356. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  2357. /* Check the parameters */
  2358. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2359. /* Process locked */
  2360. __HAL_LOCK(hadc);
  2361. /* Stop potential conversion on going, on regular and injected groups */
  2362. /* Disable ADC peripheral */
  2363. tmp_hal_status = ADC_ConversionStop_Disable(hadc);
  2364. /* Check if ADC is effectively disabled */
  2365. if (tmp_hal_status == HAL_OK)
  2366. {
  2367. /* Disable ADC DMA mode */
  2368. hadc->Instance->CR2 &= ~ADC_CR2_DMA;
  2369. /* Disable the DMA channel (in case of DMA in circular mode or stop while */
  2370. /* while DMA transfer is on going) */
  2371. tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
  2372. /* Check if DMA channel effectively disabled */
  2373. if (tmp_hal_status == HAL_OK)
  2374. {
  2375. /* Set ADC state */
  2376. ADC_STATE_CLR_SET(hadc->State,
  2377. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  2378. HAL_ADC_STATE_READY);
  2379. }
  2380. else
  2381. {
  2382. /* Update ADC state machine to error */
  2383. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  2384. }
  2385. }
  2386. /* Process unlocked */
  2387. __HAL_UNLOCK(hadc);
  2388. /* Return function status */
  2389. return tmp_hal_status;
  2390. }
  2391. #endif /* STM32F373xC || STM32F378xx */
  2392. #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
  2393. defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
  2394. defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
  2395. defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
  2396. /**
  2397. * @brief Get ADC regular group conversion result.
  2398. * @note Reading register DR automatically clears ADC flag EOC
  2399. * (ADC group regular end of unitary conversion).
  2400. * @note This function does not clear ADC flag EOS
  2401. * (ADC group regular end of sequence conversion).
  2402. * Occurrence of flag EOS rising:
  2403. * - If sequencer is composed of 1 rank, flag EOS is equivalent
  2404. * to flag EOC.
  2405. * - If sequencer is composed of several ranks, during the scan
  2406. * sequence flag EOC only is raised, at the end of the scan sequence
  2407. * both flags EOC and EOS are raised.
  2408. * To clear this flag, either use function:
  2409. * in programming model IT: @ref HAL_ADC_IRQHandler(), in programming
  2410. * model polling: @ref HAL_ADC_PollForConversion()
  2411. * or @ref __HAL_ADC_CLEAR_FLAG(&hadc, ADC_FLAG_EOS).
  2412. * @param hadc ADC handle
  2413. * @retval ADC group regular conversion data
  2414. */
  2415. uint32_t HAL_ADC_GetValue(ADC_HandleTypeDef* hadc)
  2416. {
  2417. /* Check the parameters */
  2418. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2419. /* Note: ADC flag EOC is not cleared here by software because */
  2420. /* automatically cleared by hardware when reading register DR. */
  2421. /* Return ADC converted value */
  2422. return hadc->Instance->DR;
  2423. }
  2424. #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
  2425. /* STM32F302xC || STM32F303xC || STM32F358xx || */
  2426. /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
  2427. /* STM32F301x8 || STM32F302x8 || STM32F318xx */
  2428. #if defined(STM32F373xC) || defined(STM32F378xx)
  2429. /**
  2430. * @brief Get ADC regular group conversion result.
  2431. * @note Reading register DR automatically clears ADC flag EOC
  2432. * (ADC group regular end of unitary conversion).
  2433. * @note This function does not clear ADC flag EOS
  2434. * (ADC group regular end of sequence conversion).
  2435. * Occurrence of flag EOS rising:
  2436. * - If sequencer is composed of 1 rank, flag EOS is equivalent
  2437. * to flag EOC.
  2438. * - If sequencer is composed of several ranks, during the scan
  2439. * sequence flag EOC only is raised, at the end of the scan sequence
  2440. * both flags EOC and EOS are raised.
  2441. * To clear this flag, either use function:
  2442. * in programming model IT: @ref HAL_ADC_IRQHandler(), in programming
  2443. * model polling: @ref HAL_ADC_PollForConversion()
  2444. * or @ref __HAL_ADC_CLEAR_FLAG(&hadc, ADC_FLAG_EOS).
  2445. * @param hadc ADC handle
  2446. * @retval ADC group regular conversion data
  2447. */
  2448. uint32_t HAL_ADC_GetValue(ADC_HandleTypeDef* hadc)
  2449. {
  2450. /* Check the parameters */
  2451. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2452. /* Note: EOC flag is not cleared here by software because automatically */
  2453. /* cleared by hardware when reading register DR. */
  2454. /* Return ADC converted value */
  2455. return hadc->Instance->DR;
  2456. }
  2457. #endif /* STM32F373xC || STM32F378xx */
  2458. #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
  2459. defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
  2460. defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
  2461. defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
  2462. /**
  2463. * @brief Handles ADC interrupt request.
  2464. * @param hadc ADC handle
  2465. * @retval None
  2466. */
  2467. void HAL_ADC_IRQHandler(ADC_HandleTypeDef* hadc)
  2468. {
  2469. uint32_t overrun_error = 0U; /* flag set if overrun occurrence has to be considered as an error */
  2470. ADC_Common_TypeDef *tmpADC_Common;
  2471. uint32_t tmp_cfgr = 0x0U;
  2472. uint32_t tmp_cfgr_jqm = 0x0U;
  2473. uint32_t tmp_isr = hadc->Instance->ISR;
  2474. uint32_t tmp_ier = hadc->Instance->IER;
  2475. /* Check the parameters */
  2476. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2477. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
  2478. assert_param(IS_ADC_EOC_SELECTION(hadc->Init.EOCSelection));
  2479. /* ========== Check End of Conversion flag for regular group ========== */
  2480. if( (((tmp_isr & ADC_FLAG_EOC) == ADC_FLAG_EOC) && ((tmp_ier & ADC_IT_EOC) == ADC_IT_EOC)) ||
  2481. (((tmp_isr & ADC_FLAG_EOS) == ADC_FLAG_EOS) && ((tmp_ier & ADC_IT_EOS) == ADC_IT_EOS)) )
  2482. {
  2483. /* Update state machine on conversion status if not in error state */
  2484. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL))
  2485. {
  2486. /* Set ADC state */
  2487. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  2488. }
  2489. /* Get relevant register CFGR in ADC instance of ADC master or slave */
  2490. /* in function of multimode state (for devices with multimode */
  2491. /* available). */
  2492. if (ADC_NONMULTIMODE_REG_OR_MULTIMODEMASTER(hadc))
  2493. {
  2494. tmp_cfgr = READ_REG(hadc->Instance->CFGR);
  2495. }
  2496. else
  2497. {
  2498. tmp_cfgr = READ_REG(ADC_MASTER_INSTANCE(hadc)->CFGR);
  2499. }
  2500. /* Disable interruption if no further conversion upcoming by regular */
  2501. /* external trigger or by continuous mode, */
  2502. /* and if scan sequence if completed. */
  2503. if(ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  2504. (READ_BIT(tmp_cfgr, ADC_CFGR_CONT) == RESET) )
  2505. {
  2506. /* If End of Sequence is reached, disable interrupts */
  2507. if((tmp_isr & ADC_FLAG_EOS) == ADC_FLAG_EOS)
  2508. {
  2509. /* Allowed to modify bits ADC_IT_EOC/ADC_IT_EOS only if bit */
  2510. /* ADSTART==0 (no conversion on going) */
  2511. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  2512. {
  2513. /* Disable ADC end of sequence conversion interrupt */
  2514. /* Note: Overrun interrupt was enabled with EOC interrupt in */
  2515. /* HAL_Start_IT(), but is not disabled here because can be used */
  2516. /* by overrun IRQ process below. */
  2517. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC | ADC_IT_EOS);
  2518. /* Set ADC state */
  2519. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  2520. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  2521. {
  2522. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  2523. }
  2524. }
  2525. else
  2526. {
  2527. /* Update ADC state machine to error */
  2528. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2529. /* Set ADC error code to ADC IP internal error */
  2530. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2531. }
  2532. }
  2533. }
  2534. /* Conversion complete callback */
  2535. /* Note: into callback, to determine if conversion has been triggered */
  2536. /* from EOC or EOS, possibility to use: */
  2537. /* " if( __HAL_ADC_GET_FLAG(&hadc, ADC_FLAG_EOS)) " */
  2538. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2539. hadc->ConvCpltCallback(hadc);
  2540. #else
  2541. HAL_ADC_ConvCpltCallback(hadc);
  2542. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2543. /* Clear regular group conversion flag */
  2544. /* Note: in case of overrun set to ADC_OVR_DATA_PRESERVED, end of */
  2545. /* conversion flags clear induces the release of the preserved */
  2546. /* data. */
  2547. /* Therefore, if the preserved data value is needed, it must be */
  2548. /* read preliminarily into HAL_ADC_ConvCpltCallback(). */
  2549. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS) );
  2550. }
  2551. /* ========== Check End of Conversion flag for injected group ========== */
  2552. if( (((tmp_isr & ADC_FLAG_JEOC) == ADC_FLAG_JEOC) && ((tmp_ier & ADC_IT_JEOC) == ADC_IT_JEOC)) ||
  2553. (((tmp_isr & ADC_FLAG_JEOS) == ADC_FLAG_JEOS) && ((tmp_ier & ADC_IT_JEOS) == ADC_IT_JEOS)) )
  2554. {
  2555. /* Set ADC state */
  2556. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_EOC);
  2557. /* Get relevant register CFGR in ADC instance of ADC master or slave */
  2558. /* in function of multimode state (for devices with multimode */
  2559. /* available). */
  2560. if (ADC_NONMULTIMODE_REG_OR_MULTIMODEMASTER(hadc))
  2561. {
  2562. tmp_cfgr = READ_REG(hadc->Instance->CFGR);
  2563. }
  2564. else
  2565. {
  2566. tmp_cfgr = READ_REG(ADC_MASTER_INSTANCE(hadc)->CFGR);
  2567. }
  2568. /* Disable interruption if no further conversion upcoming by injected */
  2569. /* external trigger or by automatic injected conversion with regular */
  2570. /* group having no further conversion upcoming (same conditions as */
  2571. /* regular group interruption disabling above), */
  2572. /* and if injected scan sequence is completed. */
  2573. if(ADC_IS_SOFTWARE_START_INJECTED(hadc))
  2574. {
  2575. if((READ_BIT (tmp_cfgr, ADC_CFGR_JAUTO) == RESET) ||
  2576. (ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  2577. (READ_BIT (tmp_cfgr, ADC_CFGR_CONT) == RESET) ) )
  2578. {
  2579. /* If End of Sequence is reached, disable interrupts */
  2580. if((tmp_isr & ADC_FLAG_JEOS) == ADC_FLAG_JEOS)
  2581. {
  2582. /* Get relevant register CFGR in ADC instance of ADC master or slave */
  2583. /* in function of multimode state (for devices with multimode */
  2584. /* available). */
  2585. if (ADC_NONMULTIMODE_INJ_OR_MULTIMODEMASTER(hadc))
  2586. {
  2587. tmp_cfgr_jqm = READ_REG(hadc->Instance->CFGR);
  2588. }
  2589. else
  2590. {
  2591. tmp_cfgr_jqm = READ_REG(ADC_MASTER_INSTANCE(hadc)->CFGR);
  2592. }
  2593. /* Particular case if injected contexts queue is enabled: */
  2594. /* when the last context has been fully processed, JSQR is reset */
  2595. /* by the hardware. Even if no injected conversion is planned to come */
  2596. /* (queue empty, triggers are ignored), it can start again */
  2597. /* immediately after setting a new context (JADSTART is still set). */
  2598. /* Therefore, state of HAL ADC injected group is kept to busy. */
  2599. if(READ_BIT(tmp_cfgr_jqm, ADC_CFGR_JQM) == RESET)
  2600. {
  2601. /* Allowed to modify bits ADC_IT_JEOC/ADC_IT_JEOS only if bit */
  2602. /* JADSTART==0 (no conversion on going) */
  2603. if (ADC_IS_CONVERSION_ONGOING_INJECTED(hadc) == RESET)
  2604. {
  2605. /* Disable ADC end of sequence conversion interrupt */
  2606. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC | ADC_IT_JEOS);
  2607. /* Set ADC state */
  2608. CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  2609. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_REG_BUSY))
  2610. {
  2611. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  2612. }
  2613. }
  2614. else
  2615. {
  2616. /* Update ADC state machine to error */
  2617. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  2618. /* Set ADC error code to ADC IP internal error */
  2619. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  2620. }
  2621. }
  2622. }
  2623. }
  2624. }
  2625. /* Conversion complete callback */
  2626. /* Note: into callback, to determine if conversion has been triggered */
  2627. /* from JEOC or JEOS, possibility to use: */
  2628. /* " if( __HAL_ADC_GET_FLAG(&hadc, ADC_FLAG_JEOS)) " */
  2629. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2630. hadc->InjectedConvCpltCallback(hadc);
  2631. #else
  2632. HAL_ADCEx_InjectedConvCpltCallback(hadc);
  2633. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2634. /* Clear injected group conversion flag */
  2635. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOC | ADC_FLAG_JEOS);
  2636. }
  2637. /* ========== Check analog watchdog 1 flag ========== */
  2638. if(((tmp_isr & ADC_FLAG_AWD1) == ADC_FLAG_AWD1) && ((tmp_ier & ADC_IT_AWD1) == ADC_IT_AWD1))
  2639. {
  2640. /* Set ADC state */
  2641. SET_BIT(hadc->State, HAL_ADC_STATE_AWD1);
  2642. /* Level out of window 1 callback */
  2643. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2644. hadc->LevelOutOfWindowCallback(hadc);
  2645. #else
  2646. HAL_ADC_LevelOutOfWindowCallback(hadc);
  2647. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2648. /* Clear ADC analog watchdog flag */
  2649. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD1);
  2650. }
  2651. /* ========== Check analog watchdog 2 flag ========== */
  2652. if(((tmp_isr & ADC_FLAG_AWD2) == ADC_FLAG_AWD2) && ((tmp_ier & ADC_IT_AWD2) == ADC_IT_AWD2))
  2653. {
  2654. /* Set ADC state */
  2655. SET_BIT(hadc->State, HAL_ADC_STATE_AWD2);
  2656. /* Level out of window 2 callback */
  2657. HAL_ADCEx_LevelOutOfWindow2Callback(hadc);
  2658. /* Clear ADC analog watchdog flag */
  2659. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD2);
  2660. }
  2661. /* ========== Check analog watchdog 3 flag ========== */
  2662. if(((tmp_isr & ADC_FLAG_AWD3) == ADC_FLAG_AWD3) && ((tmp_ier & ADC_IT_AWD3) == ADC_IT_AWD3))
  2663. {
  2664. /* Set ADC state */
  2665. SET_BIT(hadc->State, HAL_ADC_STATE_AWD3);
  2666. /* Level out of window 3 callback */
  2667. HAL_ADCEx_LevelOutOfWindow3Callback(hadc);
  2668. /* Clear ADC analog watchdog flag */
  2669. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD3);
  2670. }
  2671. /* ========== Check Overrun flag ========== */
  2672. if(((tmp_isr & ADC_FLAG_OVR) == ADC_FLAG_OVR) && ((tmp_ier & ADC_IT_OVR) == ADC_IT_OVR))
  2673. {
  2674. /* If overrun is set to overwrite previous data (default setting), */
  2675. /* overrun event is not considered as an error. */
  2676. /* (cf ref manual "Managing conversions without using the DMA and */
  2677. /* without overrun ") */
  2678. /* Exception for usage with DMA overrun event always considered as an */
  2679. /* error. */
  2680. if (hadc->Init.Overrun == ADC_OVR_DATA_PRESERVED)
  2681. {
  2682. overrun_error = 1U;
  2683. }
  2684. else
  2685. {
  2686. /* Pointer to the common control register to which is belonging hadc */
  2687. /* (Depending on STM32F3 product, there may be up to 4 ADC and 2 common */
  2688. /* control registers) */
  2689. tmpADC_Common = ADC_COMMON_REGISTER(hadc);
  2690. /* Check DMA configuration, depending on MultiMode set or not */
  2691. if (READ_BIT(tmpADC_Common->CCR, ADC_CCR_MULTI) == ADC_MODE_INDEPENDENT)
  2692. {
  2693. if (HAL_IS_BIT_SET(hadc->Instance->CFGR, ADC_CFGR_DMAEN))
  2694. {
  2695. overrun_error = 1U;
  2696. }
  2697. }
  2698. else
  2699. {
  2700. /* MultiMode is enabled, Common Control Register MDMA bits must be checked */
  2701. if (READ_BIT(tmpADC_Common->CCR, ADC_CCR_MDMA) != RESET)
  2702. {
  2703. overrun_error = 1U;
  2704. }
  2705. }
  2706. }
  2707. if (overrun_error == 1U)
  2708. {
  2709. /* Update ADC state machine to error */
  2710. SET_BIT(hadc->State, HAL_ADC_STATE_REG_OVR);
  2711. /* Set ADC error code to ADC IP internal error */
  2712. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_OVR);
  2713. /* Error callback */
  2714. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2715. hadc->ErrorCallback(hadc);
  2716. #else
  2717. HAL_ADC_ErrorCallback(hadc);
  2718. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2719. }
  2720. /* Clear the Overrun flag */
  2721. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_OVR);
  2722. }
  2723. /* ========== Check Injected context queue overflow flag ========== */
  2724. if(((tmp_isr & ADC_FLAG_JQOVF) == ADC_FLAG_JQOVF) && ((tmp_ier & ADC_IT_JQOVF) == ADC_IT_JQOVF))
  2725. {
  2726. /* Update ADC state machine to error */
  2727. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_JQOVF);
  2728. /* Set ADC error code to ADC IP internal error */
  2729. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_JQOVF);
  2730. /* Clear the Injected context queue overflow flag */
  2731. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JQOVF);
  2732. /* Error callback */
  2733. HAL_ADCEx_InjectedQueueOverflowCallback(hadc);
  2734. }
  2735. }
  2736. #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
  2737. /* STM32F302xC || STM32F303xC || STM32F358xx || */
  2738. /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
  2739. /* STM32F301x8 || STM32F302x8 || STM32F318xx */
  2740. #if defined(STM32F373xC) || defined(STM32F378xx)
  2741. /**
  2742. * @brief Handles ADC interrupt request
  2743. * @param hadc ADC handle
  2744. * @retval None
  2745. */
  2746. void HAL_ADC_IRQHandler(ADC_HandleTypeDef* hadc)
  2747. {
  2748. uint32_t tmp_sr = hadc->Instance->SR;
  2749. uint32_t tmp_cr1 = hadc->Instance->CR1;
  2750. /* Check the parameters */
  2751. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2752. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
  2753. assert_param(IS_ADC_REGULAR_NB_CONV(hadc->Init.NbrOfConversion));
  2754. /* ========== Check End of Conversion flag for regular group ========== */
  2755. if((tmp_cr1 & ADC_IT_EOC) == ADC_IT_EOC)
  2756. {
  2757. if((tmp_sr & ADC_FLAG_EOC) == ADC_FLAG_EOC)
  2758. {
  2759. /* Update state machine on conversion status if not in error state */
  2760. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL))
  2761. {
  2762. /* Set ADC state */
  2763. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  2764. }
  2765. /* Determine whether any further conversion upcoming on group regular */
  2766. /* by external trigger, continuous mode or scan sequence on going. */
  2767. /* Note: On STM32F37x devices, in case of sequencer enabled */
  2768. /* (several ranks selected), end of conversion flag is raised */
  2769. /* at the end of the sequence. */
  2770. if(ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  2771. (hadc->Init.ContinuousConvMode == DISABLE) )
  2772. {
  2773. /* Disable ADC end of single conversion interrupt */
  2774. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_EOC);
  2775. /* Set ADC state */
  2776. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  2777. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  2778. {
  2779. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  2780. }
  2781. }
  2782. /* Conversion complete callback */
  2783. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2784. hadc->ConvCpltCallback(hadc);
  2785. #else
  2786. HAL_ADC_ConvCpltCallback(hadc);
  2787. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2788. /* Clear regular group conversion flag */
  2789. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_STRT | ADC_FLAG_EOC);
  2790. }
  2791. }
  2792. /* ========== Check End of Conversion flag for injected group ========== */
  2793. if((tmp_cr1 & ADC_IT_JEOC) == ADC_IT_JEOC)
  2794. {
  2795. if((tmp_sr & ADC_FLAG_JEOC) == ADC_FLAG_JEOC)
  2796. {
  2797. /* Update state machine on conversion status if not in error state */
  2798. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL))
  2799. {
  2800. /* Set ADC state */
  2801. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_EOC);
  2802. }
  2803. /* Determine whether any further conversion upcoming on group injected */
  2804. /* by external trigger, scan sequence on going or by automatic injected */
  2805. /* conversion from group regular (same conditions as group regular */
  2806. /* interruption disabling above). */
  2807. /* Note: On STM32F37x devices, in case of sequencer enabled */
  2808. /* (several ranks selected), end of conversion flag is raised */
  2809. /* at the end of the sequence. */
  2810. if(ADC_IS_SOFTWARE_START_INJECTED(hadc) ||
  2811. (HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO) &&
  2812. (ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  2813. (hadc->Init.ContinuousConvMode == DISABLE) ) ) )
  2814. {
  2815. /* Disable ADC end of single conversion interrupt */
  2816. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC);
  2817. /* Set ADC state */
  2818. CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  2819. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_REG_BUSY))
  2820. {
  2821. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  2822. }
  2823. }
  2824. /* Conversion complete callback */
  2825. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2826. hadc->InjectedConvCpltCallback(hadc);
  2827. #else
  2828. HAL_ADCEx_InjectedConvCpltCallback(hadc);
  2829. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2830. /* Clear injected group conversion flag */
  2831. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_JSTRT | ADC_FLAG_JEOC));
  2832. }
  2833. }
  2834. /* ========== Check Analog watchdog flags ========== */
  2835. if((tmp_cr1 & ADC_IT_AWD) == ADC_IT_AWD)
  2836. {
  2837. if((tmp_sr & ADC_FLAG_AWD) == ADC_FLAG_AWD)
  2838. {
  2839. /* Set ADC state */
  2840. SET_BIT(hadc->State, HAL_ADC_STATE_AWD1);
  2841. /* Level out of window callback */
  2842. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  2843. hadc->LevelOutOfWindowCallback(hadc);
  2844. #else
  2845. HAL_ADC_LevelOutOfWindowCallback(hadc);
  2846. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  2847. /* Clear the ADC analog watchdog flag */
  2848. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_AWD);
  2849. }
  2850. }
  2851. }
  2852. #endif /* STM32F373xC || STM32F378xx */
  2853. #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
  2854. defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
  2855. defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
  2856. defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
  2857. /**
  2858. * @brief Perform an ADC automatic self-calibration
  2859. * Calibration prerequisite: ADC must be disabled (execute this
  2860. * function before HAL_ADC_Start() or after HAL_ADC_Stop() ).
  2861. * @param hadc ADC handle
  2862. * @param SingleDiff Selection of single-ended or differential input
  2863. * This parameter can be one of the following values:
  2864. * @arg ADC_SINGLE_ENDED: Channel in mode input single ended
  2865. * @arg ADC_DIFFERENTIAL_ENDED: Channel in mode input differential ended
  2866. * @retval HAL status
  2867. */
  2868. HAL_StatusTypeDef HAL_ADCEx_Calibration_Start(ADC_HandleTypeDef* hadc, uint32_t SingleDiff)
  2869. {
  2870. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  2871. uint32_t tickstart;
  2872. /* Check the parameters */
  2873. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2874. assert_param(IS_ADC_SINGLE_DIFFERENTIAL(SingleDiff));
  2875. /* Process locked */
  2876. __HAL_LOCK(hadc);
  2877. /* Calibration prerequisite: ADC must be disabled. */
  2878. /* Disable the ADC (if not already disabled) */
  2879. tmp_hal_status = ADC_Disable(hadc);
  2880. /* Check if ADC is effectively disabled */
  2881. if (tmp_hal_status == HAL_OK)
  2882. {
  2883. /* Change ADC state */
  2884. hadc->State = HAL_ADC_STATE_READY;
  2885. /* Select calibration mode single ended or differential ended */
  2886. hadc->Instance->CR &= (~ADC_CR_ADCALDIF);
  2887. if (SingleDiff == ADC_DIFFERENTIAL_ENDED)
  2888. {
  2889. hadc->Instance->CR |= ADC_CR_ADCALDIF;
  2890. }
  2891. /* Start ADC calibration */
  2892. hadc->Instance->CR |= ADC_CR_ADCAL;
  2893. tickstart = HAL_GetTick();
  2894. /* Wait for calibration completion */
  2895. while(HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADCAL))
  2896. {
  2897. if((HAL_GetTick() - tickstart) > ADC_CALIBRATION_TIMEOUT)
  2898. {
  2899. /* New check to avoid false timeout detection in case of preemption */
  2900. if(HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADCAL))
  2901. {
  2902. /* Update ADC state machine to error */
  2903. ADC_STATE_CLR_SET(hadc->State,
  2904. HAL_ADC_STATE_BUSY_INTERNAL,
  2905. HAL_ADC_STATE_ERROR_INTERNAL);
  2906. /* Process unlocked */
  2907. __HAL_UNLOCK(hadc);
  2908. return HAL_ERROR;
  2909. }
  2910. }
  2911. }
  2912. /* Set ADC state */
  2913. ADC_STATE_CLR_SET(hadc->State,
  2914. HAL_ADC_STATE_BUSY_INTERNAL,
  2915. HAL_ADC_STATE_READY);
  2916. }
  2917. /* Process unlocked */
  2918. __HAL_UNLOCK(hadc);
  2919. /* Return function status */
  2920. return tmp_hal_status;
  2921. }
  2922. #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
  2923. /* STM32F302xC || STM32F303xC || STM32F358xx || */
  2924. /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
  2925. /* STM32F301x8 || STM32F302x8 || STM32F318xx */
  2926. #if defined(STM32F373xC) || defined(STM32F378xx)
  2927. /**
  2928. * @brief Perform an ADC automatic self-calibration
  2929. * Calibration prerequisite: ADC must be disabled (execute this
  2930. * function before HAL_ADC_Start() or after HAL_ADC_Stop() ).
  2931. * During calibration process, ADC is enabled. ADC is let enabled at
  2932. * the completion of this function.
  2933. * @param hadc ADC handle
  2934. * @retval HAL status
  2935. */
  2936. HAL_StatusTypeDef HAL_ADCEx_Calibration_Start(ADC_HandleTypeDef* hadc)
  2937. {
  2938. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  2939. uint32_t tickstart;
  2940. __IO uint32_t wait_loop_index = 0U;
  2941. /* Check the parameters */
  2942. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  2943. /* Process locked */
  2944. __HAL_LOCK(hadc);
  2945. /* 1. Calibration prerequisite: */
  2946. /* - ADC must be disabled for at least two ADC clock cycles in disable */
  2947. /* mode before ADC enable */
  2948. /* Stop potential conversion on going, on regular and injected groups */
  2949. /* Disable ADC peripheral */
  2950. tmp_hal_status = ADC_ConversionStop_Disable(hadc);
  2951. /* Check if ADC is effectively disabled */
  2952. if (tmp_hal_status == HAL_OK)
  2953. {
  2954. /* Set ADC state */
  2955. ADC_STATE_CLR_SET(hadc->State,
  2956. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  2957. HAL_ADC_STATE_BUSY_INTERNAL);
  2958. /* Wait two ADC clock cycles */
  2959. while(wait_loop_index < ADC_CYCLE_WORST_CASE_CPU_CYCLES *2U)
  2960. {
  2961. wait_loop_index++;
  2962. }
  2963. /* 2. Enable the ADC peripheral */
  2964. ADC_Enable(hadc);
  2965. /* 3. Resets ADC calibration registers */
  2966. SET_BIT(hadc->Instance->CR2, ADC_CR2_RSTCAL);
  2967. tickstart = HAL_GetTick();
  2968. /* Wait for calibration reset completion */
  2969. while(HAL_IS_BIT_SET(hadc->Instance->CR2, ADC_CR2_RSTCAL))
  2970. {
  2971. if((HAL_GetTick() - tickstart) > ADC_CALIBRATION_TIMEOUT)
  2972. {
  2973. /* New check to avoid false timeout detection in case of preemption */
  2974. if(HAL_IS_BIT_SET(hadc->Instance->CR2, ADC_CR2_RSTCAL))
  2975. {
  2976. /* Update ADC state machine to error */
  2977. ADC_STATE_CLR_SET(hadc->State,
  2978. HAL_ADC_STATE_BUSY_INTERNAL,
  2979. HAL_ADC_STATE_ERROR_INTERNAL);
  2980. /* Process unlocked */
  2981. __HAL_UNLOCK(hadc);
  2982. return HAL_ERROR;
  2983. }
  2984. }
  2985. }
  2986. /* 4. Start ADC calibration */
  2987. SET_BIT(hadc->Instance->CR2, ADC_CR2_CAL);
  2988. tickstart = HAL_GetTick();
  2989. /* Wait for calibration completion */
  2990. while(HAL_IS_BIT_SET(hadc->Instance->CR2, ADC_CR2_CAL))
  2991. {
  2992. if((HAL_GetTick() - tickstart) > ADC_CALIBRATION_TIMEOUT)
  2993. {
  2994. /* New check to avoid false timeout detection in case of preemption */
  2995. if(HAL_IS_BIT_SET(hadc->Instance->CR2, ADC_CR2_CAL))
  2996. {
  2997. /* Update ADC state machine to error */
  2998. ADC_STATE_CLR_SET(hadc->State,
  2999. HAL_ADC_STATE_BUSY_INTERNAL,
  3000. HAL_ADC_STATE_ERROR_INTERNAL);
  3001. /* Process unlocked */
  3002. __HAL_UNLOCK(hadc);
  3003. return HAL_ERROR;
  3004. }
  3005. }
  3006. }
  3007. /* Set ADC state */
  3008. ADC_STATE_CLR_SET(hadc->State,
  3009. HAL_ADC_STATE_BUSY_INTERNAL,
  3010. HAL_ADC_STATE_READY);
  3011. }
  3012. /* Process unlocked */
  3013. __HAL_UNLOCK(hadc);
  3014. /* Return function status */
  3015. return tmp_hal_status;
  3016. }
  3017. #endif /* STM32F373xC || STM32F378xx */
  3018. #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
  3019. defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
  3020. defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
  3021. defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
  3022. /**
  3023. * @brief Get the calibration factor from automatic conversion result
  3024. * @param hadc ADC handle
  3025. * @param SingleDiff Selection of single-ended or differential input
  3026. * This parameter can be one of the following values:
  3027. * @arg ADC_SINGLE_ENDED: Channel in mode input single ended
  3028. * @arg ADC_DIFFERENTIAL_ENDED: Channel in mode input differential ended
  3029. * @retval Converted value
  3030. */
  3031. uint32_t HAL_ADCEx_Calibration_GetValue(ADC_HandleTypeDef* hadc, uint32_t SingleDiff)
  3032. {
  3033. /* Check the parameters */
  3034. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  3035. assert_param(IS_ADC_SINGLE_DIFFERENTIAL(SingleDiff));
  3036. /* Return the selected ADC calibration value */
  3037. if (SingleDiff == ADC_DIFFERENTIAL_ENDED)
  3038. {
  3039. return ADC_CALFACT_DIFF_GET(hadc->Instance->CALFACT);
  3040. }
  3041. else
  3042. {
  3043. return ((hadc->Instance->CALFACT) & ADC_CALFACT_CALFACT_S);
  3044. }
  3045. }
  3046. #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
  3047. /* STM32F302xC || STM32F303xC || STM32F358xx || */
  3048. /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
  3049. /* STM32F301x8 || STM32F302x8 || STM32F318xx */
  3050. #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
  3051. defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
  3052. defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
  3053. defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
  3054. /**
  3055. * @brief Set the calibration factor to overwrite automatic conversion result. ADC must be enabled and no conversion on going.
  3056. * @param hadc ADC handle
  3057. * @param SingleDiff Selection of single-ended or differential input
  3058. * This parameter can be one of the following values:
  3059. * @arg ADC_SINGLE_ENDED: Channel in mode input single ended
  3060. * @arg ADC_DIFFERENTIAL_ENDED: Channel in mode input differential ended
  3061. * @param CalibrationFactor Calibration factor (coded on 7 bits maximum)
  3062. * @retval HAL state
  3063. */
  3064. HAL_StatusTypeDef HAL_ADCEx_Calibration_SetValue(ADC_HandleTypeDef* hadc, uint32_t SingleDiff, uint32_t CalibrationFactor)
  3065. {
  3066. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  3067. /* Check the parameters */
  3068. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  3069. assert_param(IS_ADC_SINGLE_DIFFERENTIAL(SingleDiff));
  3070. assert_param(IS_ADC_CALFACT(CalibrationFactor));
  3071. /* Process locked */
  3072. __HAL_LOCK(hadc);
  3073. /* Verification of hardware constraints before modifying the calibration */
  3074. /* factors register: ADC must be enabled, no conversion on going. */
  3075. if ( (ADC_IS_ENABLE(hadc) != RESET) &&
  3076. (ADC_IS_CONVERSION_ONGOING_REGULAR_INJECTED(hadc) == RESET) )
  3077. {
  3078. /* Set the selected ADC calibration value */
  3079. if (SingleDiff == ADC_DIFFERENTIAL_ENDED)
  3080. {
  3081. MODIFY_REG(hadc->Instance->CALFACT ,
  3082. ADC_CALFACT_CALFACT_D ,
  3083. ADC_CALFACT_DIFF_SET(CalibrationFactor) );
  3084. }
  3085. else
  3086. {
  3087. MODIFY_REG(hadc->Instance->CALFACT,
  3088. ADC_CALFACT_CALFACT_S ,
  3089. CalibrationFactor );
  3090. }
  3091. }
  3092. else
  3093. {
  3094. /* Update ADC state machine to error */
  3095. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  3096. /* Set ADC error code to ADC IP internal error */
  3097. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  3098. }
  3099. /* Process unlocked */
  3100. __HAL_UNLOCK(hadc);
  3101. /* Return function status */
  3102. return tmp_hal_status;
  3103. }
  3104. #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
  3105. /* STM32F302xC || STM32F303xC || STM32F358xx || */
  3106. /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
  3107. /* STM32F301x8 || STM32F302x8 || STM32F318xx */
  3108. #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
  3109. defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
  3110. defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
  3111. defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
  3112. /**
  3113. * @brief Enables ADC, starts conversion of injected group.
  3114. * Interruptions enabled in this function: None.
  3115. * @note Case of multimode enabled (for devices with several ADCs): This
  3116. * function must be called for ADC slave first, then ADC master.
  3117. * For ADC slave, ADC is enabled only (conversion is not started).
  3118. * For ADC master, ADC is enabled and multimode conversion is started.
  3119. * @param hadc ADC handle
  3120. * @retval HAL status
  3121. */
  3122. HAL_StatusTypeDef HAL_ADCEx_InjectedStart(ADC_HandleTypeDef* hadc)
  3123. {
  3124. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  3125. /* Check the parameters */
  3126. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  3127. /* Perform ADC enable and conversion start if no conversion is on going */
  3128. if (ADC_IS_CONVERSION_ONGOING_INJECTED(hadc) == RESET)
  3129. {
  3130. /* Process locked */
  3131. __HAL_LOCK(hadc);
  3132. /* Enable the ADC peripheral */
  3133. tmp_hal_status = ADC_Enable(hadc);
  3134. /* Start conversion if ADC is effectively enabled */
  3135. if (tmp_hal_status == HAL_OK)
  3136. {
  3137. /* Set ADC state */
  3138. /* - Clear state bitfield related to injected group conversion results */
  3139. /* - Set state bitfield related to injected operation */
  3140. ADC_STATE_CLR_SET(hadc->State,
  3141. HAL_ADC_STATE_READY | HAL_ADC_STATE_INJ_EOC,
  3142. HAL_ADC_STATE_INJ_BUSY);
  3143. /* Case of independent mode or multimode(for devices with several ADCs):*/
  3144. /* Set multimode state. */
  3145. if (ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc))
  3146. {
  3147. CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  3148. }
  3149. else
  3150. {
  3151. SET_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  3152. }
  3153. /* Check if a regular conversion is ongoing */
  3154. /* Note: On this device, there is no ADC error code fields related to */
  3155. /* conversions on group injected only. In case of conversion on */
  3156. /* going on group regular, no error code is reset. */
  3157. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_REG_BUSY))
  3158. {
  3159. /* Reset ADC all error code fields */
  3160. ADC_CLEAR_ERRORCODE(hadc);
  3161. }
  3162. /* Process unlocked */
  3163. /* Unlock before starting ADC conversions: in case of potential */
  3164. /* interruption, to let the process to ADC IRQ Handler. */
  3165. __HAL_UNLOCK(hadc);
  3166. /* Clear injected group conversion flag */
  3167. /* (To ensure of no unknown state from potential previous ADC */
  3168. /* operations) */
  3169. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_JEOC | ADC_FLAG_JEOS));
  3170. /* Enable conversion of injected group, if automatic injected */
  3171. /* conversion is disabled. */
  3172. /* If software start has been selected, conversion starts immediately. */
  3173. /* If external trigger has been selected, conversion will start at next */
  3174. /* trigger event. */
  3175. /* Case of multimode enabled (for devices with several ADCs): */
  3176. /* - if ADC is slave, ADC is enabled only (conversion is not started). */
  3177. /* - if ADC is master, ADC is enabled and conversion is started. */
  3178. if (HAL_IS_BIT_CLR(hadc->Instance->CFGR, ADC_CFGR_JAUTO) &&
  3179. ADC_NONMULTIMODE_INJ_OR_MULTIMODEMASTER(hadc) )
  3180. {
  3181. SET_BIT(hadc->Instance->CR, ADC_CR_JADSTART);
  3182. }
  3183. }
  3184. else
  3185. {
  3186. /* Process unlocked */
  3187. __HAL_UNLOCK(hadc);
  3188. }
  3189. }
  3190. else
  3191. {
  3192. tmp_hal_status = HAL_BUSY;
  3193. }
  3194. /* Return function status */
  3195. return tmp_hal_status;
  3196. }
  3197. #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
  3198. /* STM32F302xC || STM32F303xC || STM32F358xx || */
  3199. /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
  3200. /* STM32F301x8 || STM32F302x8 || STM32F318xx */
  3201. #if defined(STM32F373xC) || defined(STM32F378xx)
  3202. /**
  3203. * @brief Enables ADC, starts conversion of injected group.
  3204. * Interruptions enabled in this function: None.
  3205. * @param hadc ADC handle
  3206. * @retval HAL status
  3207. */
  3208. HAL_StatusTypeDef HAL_ADCEx_InjectedStart(ADC_HandleTypeDef* hadc)
  3209. {
  3210. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  3211. /* Check the parameters */
  3212. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  3213. /* Process locked */
  3214. __HAL_LOCK(hadc);
  3215. /* Enable the ADC peripheral */
  3216. tmp_hal_status = ADC_Enable(hadc);
  3217. /* Start conversion if ADC is effectively enabled */
  3218. if (tmp_hal_status == HAL_OK)
  3219. {
  3220. /* Set ADC state */
  3221. /* - Clear state bitfield related to injected group conversion results */
  3222. /* - Set state bitfield related to injected operation */
  3223. ADC_STATE_CLR_SET(hadc->State,
  3224. HAL_ADC_STATE_READY | HAL_ADC_STATE_INJ_EOC,
  3225. HAL_ADC_STATE_INJ_BUSY);
  3226. /* Check if a regular conversion is ongoing */
  3227. /* Note: On this device, there is no ADC error code fields related to */
  3228. /* conversions on group injected only. In case of conversion on */
  3229. /* going on group regular, no error code is reset. */
  3230. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_REG_BUSY))
  3231. {
  3232. /* Reset ADC all error code fields */
  3233. ADC_CLEAR_ERRORCODE(hadc);
  3234. }
  3235. /* Process unlocked */
  3236. /* Unlock before starting ADC conversions: in case of potential */
  3237. /* interruption, to let the process to ADC IRQ Handler. */
  3238. __HAL_UNLOCK(hadc);
  3239. /* Clear injected group conversion flag */
  3240. /* (To ensure of no unknown state from potential previous ADC operations) */
  3241. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOC);
  3242. /* Enable conversion of injected group. */
  3243. /* If software start has been selected, conversion starts immediately. */
  3244. /* If external trigger has been selected, conversion will start at next */
  3245. /* trigger event. */
  3246. /* If external trigger has been selected, conversion will start at next */
  3247. /* trigger event. */
  3248. /* If automatic injected conversion is enabled, conversion will start */
  3249. /* after next regular group conversion. */
  3250. if (ADC_IS_SOFTWARE_START_INJECTED(hadc) &&
  3251. HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO) )
  3252. {
  3253. /* Start ADC conversion on injected group with SW start */
  3254. SET_BIT(hadc->Instance->CR2, (ADC_CR2_JSWSTART | ADC_CR2_JEXTTRIG));
  3255. }
  3256. else
  3257. {
  3258. /* Start ADC conversion on injected group with external trigger */
  3259. SET_BIT(hadc->Instance->CR2, ADC_CR2_JEXTTRIG);
  3260. }
  3261. }
  3262. /* Return function status */
  3263. return tmp_hal_status;
  3264. }
  3265. #endif /* STM32F373xC || STM32F378xx */
  3266. #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
  3267. defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
  3268. defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
  3269. defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
  3270. /**
  3271. * @brief Stop ADC group injected conversion (potential conversion on going
  3272. * on ADC group regular is not impacted), disable ADC peripheral
  3273. * if no conversion is on going on group regular.
  3274. * @note To stop ADC conversion of both groups regular and injected and to
  3275. * to disable ADC peripheral, instead of using 2 functions
  3276. * @ref HAL_ADCEx_RegularStop() and @ref HAL_ADCEx_InjectedStop(),
  3277. * use function @ref HAL_ADC_Stop().
  3278. * @note If injected group mode auto-injection is enabled,
  3279. * function HAL_ADC_Stop must be used.
  3280. * @note Case of multimode enabled (for devices with several ADCs): This
  3281. * function must be called for ADC master first, then ADC slave.
  3282. * For ADC master, conversion is stopped and ADC is disabled.
  3283. * For ADC slave, ADC is disabled only (conversion stop of ADC master
  3284. * has already stopped conversion of ADC slave).
  3285. * @note In case of auto-injection mode, HAL_ADC_Stop must be used.
  3286. * @param hadc ADC handle
  3287. * @retval None
  3288. */
  3289. HAL_StatusTypeDef HAL_ADCEx_InjectedStop(ADC_HandleTypeDef* hadc)
  3290. {
  3291. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  3292. /* Check the parameters */
  3293. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  3294. /* Process locked */
  3295. __HAL_LOCK(hadc);
  3296. /* Stop potential ADC conversion on going and disable ADC peripheral */
  3297. /* conditioned to: */
  3298. /* - In case of auto-injection mode, HAL_ADC_Stop must be used. */
  3299. /* - For ADC injected group conversion stop: */
  3300. /* On this STM32 family, conversion on the other group */
  3301. /* (group regular) can continue (groups regular and injected */
  3302. /* conversion stop commands are independent) */
  3303. /* - For ADC disable: */
  3304. /* No conversion on the other group (group regular) must be intended to */
  3305. /* continue (groups regular and injected are both impacted by */
  3306. /* ADC disable) */
  3307. if(HAL_IS_BIT_CLR(hadc->Instance->CFGR, ADC_CFGR_JAUTO))
  3308. {
  3309. /* 1. Stop potential conversion on going on injected group only. */
  3310. tmp_hal_status = ADC_ConversionStop(hadc, ADC_INJECTED_GROUP);
  3311. /* Disable ADC peripheral if conversion on ADC group injected is */
  3312. /* effectively stopped and if no conversion on the other group */
  3313. /* (ADC group regular) is intended to continue. */
  3314. if (tmp_hal_status == HAL_OK)
  3315. {
  3316. if((ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET) &&
  3317. ((hadc->State & HAL_ADC_STATE_REG_BUSY) == RESET) )
  3318. {
  3319. /* 2. Disable the ADC peripheral */
  3320. tmp_hal_status = ADC_Disable(hadc);
  3321. /* Check if ADC is effectively disabled */
  3322. if (tmp_hal_status == HAL_OK)
  3323. {
  3324. /* Set ADC state */
  3325. ADC_STATE_CLR_SET(hadc->State,
  3326. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  3327. HAL_ADC_STATE_READY);
  3328. }
  3329. }
  3330. /* Conversion on ADC group injected group is stopped, but ADC is not */
  3331. /* disabled since conversion on ADC group regular is still on going. */
  3332. else
  3333. {
  3334. /* Set ADC state */
  3335. CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  3336. }
  3337. }
  3338. }
  3339. else
  3340. {
  3341. /* Update ADC state machine to error */
  3342. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  3343. tmp_hal_status = HAL_ERROR;
  3344. }
  3345. /* Process unlocked */
  3346. __HAL_UNLOCK(hadc);
  3347. /* Return function status */
  3348. return tmp_hal_status;
  3349. }
  3350. #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
  3351. /* STM32F302xC || STM32F303xC || STM32F358xx || */
  3352. /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
  3353. /* STM32F301x8 || STM32F302x8 || STM32F318xx */
  3354. #if defined(STM32F373xC) || defined(STM32F378xx)
  3355. /**
  3356. * @brief Stop conversion of injected channels. Disable ADC peripheral if
  3357. * no regular conversion is on going.
  3358. * @note If ADC must be disabled and if conversion is on going on
  3359. * regular group, function HAL_ADC_Stop must be used to stop both
  3360. * injected and regular groups, and disable the ADC.
  3361. * @note In case of auto-injection mode, HAL_ADC_Stop must be used.
  3362. * @param hadc ADC handle
  3363. * @retval None
  3364. */
  3365. HAL_StatusTypeDef HAL_ADCEx_InjectedStop(ADC_HandleTypeDef* hadc)
  3366. {
  3367. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  3368. /* Check the parameters */
  3369. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  3370. /* Process locked */
  3371. __HAL_LOCK(hadc);
  3372. /* Stop potential conversion and disable ADC peripheral */
  3373. /* Conditioned to: */
  3374. /* - No conversion on the other group (regular group) is intended to */
  3375. /* continue (injected and regular groups stop conversion and ADC disable */
  3376. /* are common) */
  3377. /* - In case of auto-injection mode, HAL_ADC_Stop must be used. */
  3378. if(((hadc->State & HAL_ADC_STATE_REG_BUSY) == RESET) &&
  3379. HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO) )
  3380. {
  3381. /* Stop potential conversion on going, on regular and injected groups */
  3382. /* Disable ADC peripheral */
  3383. tmp_hal_status = ADC_ConversionStop_Disable(hadc);
  3384. /* Check if ADC is effectively disabled */
  3385. if (tmp_hal_status == HAL_OK)
  3386. {
  3387. /* Set ADC state */
  3388. ADC_STATE_CLR_SET(hadc->State,
  3389. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  3390. HAL_ADC_STATE_READY);
  3391. }
  3392. }
  3393. else
  3394. {
  3395. /* Update ADC state machine to error */
  3396. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  3397. tmp_hal_status = HAL_ERROR;
  3398. }
  3399. /* Process unlocked */
  3400. __HAL_UNLOCK(hadc);
  3401. /* Return function status */
  3402. return tmp_hal_status;
  3403. }
  3404. #endif /* STM32F373xC || STM32F378xx */
  3405. #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
  3406. defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
  3407. defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
  3408. defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
  3409. /**
  3410. * @brief Wait for injected group conversion to be completed.
  3411. * @param hadc ADC handle
  3412. * @param Timeout Timeout value in millisecond.
  3413. * @retval HAL status
  3414. */
  3415. HAL_StatusTypeDef HAL_ADCEx_InjectedPollForConversion(ADC_HandleTypeDef* hadc, uint32_t Timeout)
  3416. {
  3417. uint32_t tickstart;
  3418. uint32_t tmp_Flag_EOC;
  3419. uint32_t tmp_cfgr = 0x00000000U;
  3420. /* Check the parameters */
  3421. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  3422. /* If end of conversion selected to end of sequence */
  3423. if (hadc->Init.EOCSelection == ADC_EOC_SEQ_CONV)
  3424. {
  3425. tmp_Flag_EOC = ADC_FLAG_JEOS;
  3426. }
  3427. /* If end of conversion selected to end of each conversion */
  3428. else /* ADC_EOC_SINGLE_CONV */
  3429. {
  3430. tmp_Flag_EOC = (ADC_FLAG_JEOC | ADC_FLAG_JEOS);
  3431. }
  3432. /* Get relevant register CFGR in ADC instance of ADC master or slave */
  3433. /* in function of multimode state (for devices with multimode */
  3434. /* available). */
  3435. if (ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc))
  3436. {
  3437. tmp_cfgr = READ_REG(hadc->Instance->CFGR);
  3438. }
  3439. else
  3440. {
  3441. tmp_cfgr = READ_REG(ADC_MASTER_INSTANCE(hadc)->CFGR);
  3442. }
  3443. /* Get tick count */
  3444. tickstart = HAL_GetTick();
  3445. /* Wait until End of Conversion flag is raised */
  3446. while(HAL_IS_BIT_CLR(hadc->Instance->ISR, tmp_Flag_EOC))
  3447. {
  3448. /* Check if timeout is disabled (set to infinite wait) */
  3449. if(Timeout != HAL_MAX_DELAY)
  3450. {
  3451. if((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
  3452. {
  3453. /* New check to avoid false timeout detection in case of preemption */
  3454. if(HAL_IS_BIT_CLR(hadc->Instance->ISR, tmp_Flag_EOC))
  3455. {
  3456. /* Update ADC state machine to timeout */
  3457. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  3458. /* Process unlocked */
  3459. __HAL_UNLOCK(hadc);
  3460. return HAL_TIMEOUT;
  3461. }
  3462. }
  3463. }
  3464. }
  3465. /* Update ADC state machine */
  3466. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_EOC);
  3467. /* Determine whether any further conversion upcoming on group injected */
  3468. /* by external trigger or by automatic injected conversion */
  3469. /* from group regular. */
  3470. if(ADC_IS_SOFTWARE_START_INJECTED(hadc) ||
  3471. ((READ_BIT (tmp_cfgr, ADC_CFGR_JAUTO) == RESET) &&
  3472. (ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  3473. (READ_BIT (tmp_cfgr, ADC_CFGR_CONT) == RESET) ) ) )
  3474. {
  3475. /* Set ADC state */
  3476. CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  3477. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_REG_BUSY))
  3478. {
  3479. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  3480. }
  3481. }
  3482. /* Clear end of conversion flag of injected group if low power feature */
  3483. /* "Auto Wait" is disabled, to not interfere with this feature until data */
  3484. /* register is read using function HAL_ADC_GetValue(). */
  3485. if (READ_BIT (tmp_cfgr, ADC_CFGR_AUTDLY) == RESET)
  3486. {
  3487. /* Clear injected group conversion flag */
  3488. /* (JEOC or JEOS depending on HAL ADC initialization parameter) */
  3489. __HAL_ADC_CLEAR_FLAG(hadc, tmp_Flag_EOC);
  3490. }
  3491. /* Return ADC state */
  3492. return HAL_OK;
  3493. }
  3494. #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
  3495. /* STM32F302xC || STM32F303xC || STM32F358xx || */
  3496. /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
  3497. /* STM32F301x8 || STM32F302x8 || STM32F318xx */
  3498. #if defined(STM32F373xC) || defined(STM32F378xx)
  3499. /**
  3500. * @brief Wait for injected group conversion to be completed.
  3501. * @param hadc ADC handle
  3502. * @param Timeout Timeout value in millisecond.
  3503. * @retval HAL status
  3504. */
  3505. HAL_StatusTypeDef HAL_ADCEx_InjectedPollForConversion(ADC_HandleTypeDef* hadc, uint32_t Timeout)
  3506. {
  3507. uint32_t tickstart = 0U;
  3508. /* Variables for polling in case of scan mode enabled */
  3509. uint32_t Conversion_Timeout_CPU_cycles_max =0U;
  3510. uint32_t Conversion_Timeout_CPU_cycles =0U;
  3511. /* Check the parameters */
  3512. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  3513. /* Get tick count */
  3514. tickstart = HAL_GetTick();
  3515. /* Polling for end of conversion: differentiation if single/sequence */
  3516. /* conversion. */
  3517. /* For injected group, flag JEOC is set only at the end of the sequence, */
  3518. /* not for each conversion within the sequence. */
  3519. /* - If single conversion for injected group (scan mode disabled or */
  3520. /* InjectedNbrOfConversion ==1U), flag JEOC is used to determine the */
  3521. /* conversion completion. */
  3522. /* - If sequence conversion for injected group (scan mode enabled and */
  3523. /* InjectedNbrOfConversion >=2U), flag JEOC is set only at the end of the */
  3524. /* sequence. */
  3525. /* To poll for each conversion, the maximum conversion time is computed */
  3526. /* from ADC conversion time (selected sampling time + conversion time of */
  3527. /* 12.5 ADC clock cycles) and APB2/ADC clock prescalers (depending on */
  3528. /* settings, conversion time range can be from 28 to 32256 CPU cycles). */
  3529. /* As flag JEOC is not set after each conversion, no timeout status can */
  3530. /* be set. */
  3531. if ((hadc->Instance->JSQR & ADC_JSQR_JL) == RESET)
  3532. {
  3533. /* Wait until End of Conversion flag is raised */
  3534. while(HAL_IS_BIT_CLR(hadc->Instance->SR, ADC_FLAG_JEOC))
  3535. {
  3536. /* Check if timeout is disabled (set to infinite wait) */
  3537. if(Timeout != HAL_MAX_DELAY)
  3538. {
  3539. if((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
  3540. {
  3541. /* New check to avoid false timeout detection in case of preemption */
  3542. if(HAL_IS_BIT_CLR(hadc->Instance->SR, ADC_FLAG_JEOC))
  3543. {
  3544. /* Update ADC state machine to timeout */
  3545. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  3546. /* Process unlocked */
  3547. __HAL_UNLOCK(hadc);
  3548. return HAL_TIMEOUT;
  3549. }
  3550. }
  3551. }
  3552. }
  3553. }
  3554. else
  3555. {
  3556. /* Replace polling by wait for maximum conversion time */
  3557. /* Calculation of CPU cycles corresponding to ADC conversion cycles. */
  3558. /* Retrieve ADC clock prescaler and ADC maximum conversion cycles on all */
  3559. /* channels. */
  3560. Conversion_Timeout_CPU_cycles_max = ADC_CLOCK_PRESCALER_RANGE();
  3561. Conversion_Timeout_CPU_cycles_max *= ADC_CONVCYCLES_MAX_RANGE(hadc);
  3562. /* Poll with maximum conversion time */
  3563. while(Conversion_Timeout_CPU_cycles < Conversion_Timeout_CPU_cycles_max)
  3564. {
  3565. /* Check if timeout is disabled (set to infinite wait) */
  3566. if(Timeout != HAL_MAX_DELAY)
  3567. {
  3568. if((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
  3569. {
  3570. /* New check to avoid false timeout detection in case of preemption */
  3571. if(Conversion_Timeout_CPU_cycles < Conversion_Timeout_CPU_cycles_max)
  3572. {
  3573. /* Update ADC state machine to timeout */
  3574. SET_BIT(hadc->State, HAL_ADC_STATE_TIMEOUT);
  3575. /* Process unlocked */
  3576. __HAL_UNLOCK(hadc);
  3577. return HAL_TIMEOUT;
  3578. }
  3579. }
  3580. }
  3581. Conversion_Timeout_CPU_cycles ++;
  3582. }
  3583. }
  3584. /* Clear injected group conversion flag (and regular conversion flag raised simultaneously) */
  3585. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JSTRT | ADC_FLAG_JEOC | ADC_FLAG_EOC);
  3586. /* Update ADC state machine */
  3587. SET_BIT(hadc->State, HAL_ADC_STATE_INJ_EOC);
  3588. /* Determine whether any further conversion upcoming on group injected */
  3589. /* by external trigger or by automatic injected conversion */
  3590. /* from group regular. */
  3591. if(ADC_IS_SOFTWARE_START_INJECTED(hadc) ||
  3592. (HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO) &&
  3593. (ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  3594. (hadc->Init.ContinuousConvMode == DISABLE) ) ) )
  3595. {
  3596. /* Set ADC state */
  3597. CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  3598. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_REG_BUSY))
  3599. {
  3600. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  3601. }
  3602. }
  3603. /* Return ADC state */
  3604. return HAL_OK;
  3605. }
  3606. #endif /* STM32F373xC || STM32F378xx */
  3607. #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
  3608. defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
  3609. defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
  3610. defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
  3611. /**
  3612. * @brief Enables ADC, starts conversion of injected group with interruption.
  3613. * Interruptions enabled in this function:
  3614. * - JEOC (end of conversion of injected group) or JEOS (end of
  3615. * sequence of injected group) depending on ADC initialization
  3616. * parameter "EOCSelection"
  3617. * Each of these interruptions has its dedicated callback function.
  3618. * @note Case of multimode enabled (for devices with several ADCs): This
  3619. * function must be called for ADC slave first, then ADC master.
  3620. * For ADC slave, ADC is enabled only (conversion is not started).
  3621. * For ADC master, ADC is enabled and multimode conversion is started.
  3622. * @param hadc ADC handle
  3623. * @retval HAL status.
  3624. */
  3625. HAL_StatusTypeDef HAL_ADCEx_InjectedStart_IT(ADC_HandleTypeDef* hadc)
  3626. {
  3627. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  3628. /* Check the parameters */
  3629. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  3630. /* Perform ADC enable and conversion start if no conversion is on going */
  3631. if (ADC_IS_CONVERSION_ONGOING_INJECTED(hadc) == RESET)
  3632. {
  3633. /* Process locked */
  3634. __HAL_LOCK(hadc);
  3635. /* Enable the ADC peripheral */
  3636. tmp_hal_status = ADC_Enable(hadc);
  3637. /* Start conversion if ADC is effectively enabled */
  3638. /* Start conversion if ADC is effectively enabled */
  3639. if (tmp_hal_status == HAL_OK)
  3640. {
  3641. /* Set ADC state */
  3642. /* - Clear state bitfield related to injected group conversion results */
  3643. /* - Set state bitfield related to injected operation */
  3644. ADC_STATE_CLR_SET(hadc->State,
  3645. HAL_ADC_STATE_READY | HAL_ADC_STATE_INJ_EOC,
  3646. HAL_ADC_STATE_INJ_BUSY);
  3647. /* Case of independent mode or multimode(for devices with several ADCs):*/
  3648. /* Set multimode state. */
  3649. if (ADC_NONMULTIMODE_OR_MULTIMODEMASTER(hadc))
  3650. {
  3651. CLEAR_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  3652. }
  3653. else
  3654. {
  3655. SET_BIT(hadc->State, HAL_ADC_STATE_MULTIMODE_SLAVE);
  3656. }
  3657. /* Check if a regular conversion is ongoing */
  3658. /* Note: On this device, there is no ADC error code fields related to */
  3659. /* conversions on group injected only. In case of conversion on */
  3660. /* going on group regular, no error code is reset. */
  3661. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_REG_BUSY))
  3662. {
  3663. /* Reset ADC all error code fields */
  3664. ADC_CLEAR_ERRORCODE(hadc);
  3665. }
  3666. /* Process unlocked */
  3667. /* Unlock before starting ADC conversions: in case of potential */
  3668. /* interruption, to let the process to ADC IRQ Handler. */
  3669. __HAL_UNLOCK(hadc);
  3670. /* Clear injected group conversion flag */
  3671. /* (To ensure of no unknown state from potential previous ADC */
  3672. /* operations) */
  3673. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_JEOC | ADC_FLAG_JEOS));
  3674. /* Enable ADC Injected context queue overflow interrupt if this feature */
  3675. /* is enabled. */
  3676. if ((hadc->Instance->CFGR & ADC_CFGR_JQM) != RESET)
  3677. {
  3678. __HAL_ADC_ENABLE_IT(hadc, ADC_FLAG_JQOVF);
  3679. }
  3680. /* Enable ADC end of conversion interrupt */
  3681. switch(hadc->Init.EOCSelection)
  3682. {
  3683. case ADC_EOC_SEQ_CONV:
  3684. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC);
  3685. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOS);
  3686. break;
  3687. /* case ADC_EOC_SINGLE_CONV */
  3688. default:
  3689. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOC | ADC_IT_JEOS);
  3690. break;
  3691. }
  3692. /* Enable conversion of injected group, if automatic injected */
  3693. /* conversion is disabled. */
  3694. /* If software start has been selected, conversion starts immediately. */
  3695. /* If external trigger has been selected, conversion will start at next */
  3696. /* trigger event. */
  3697. /* Case of multimode enabled (for devices with several ADCs): */
  3698. /* - if ADC is slave, ADC is enabled only (conversion is not started). */
  3699. /* - if ADC is master, ADC is enabled and conversion is started. */
  3700. if (HAL_IS_BIT_CLR(hadc->Instance->CFGR, ADC_CFGR_JAUTO) &&
  3701. ADC_NONMULTIMODE_INJ_OR_MULTIMODEMASTER(hadc) )
  3702. {
  3703. SET_BIT(hadc->Instance->CR, ADC_CR_JADSTART);
  3704. }
  3705. }
  3706. else
  3707. {
  3708. /* Process unlocked */
  3709. __HAL_UNLOCK(hadc);
  3710. }
  3711. }
  3712. else
  3713. {
  3714. tmp_hal_status = HAL_BUSY;
  3715. }
  3716. /* Return function status */
  3717. return tmp_hal_status;
  3718. }
  3719. #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
  3720. /* STM32F302xC || STM32F303xC || STM32F358xx || */
  3721. /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
  3722. /* STM32F301x8 || STM32F302x8 || STM32F318xx */
  3723. #if defined(STM32F373xC) || defined(STM32F378xx)
  3724. /**
  3725. * @brief Enables ADC, starts conversion of injected group with interruption.
  3726. * Interruptions enabled in this function:
  3727. * - JEOC (end of conversion of injected group)
  3728. * Each of these interruptions has its dedicated callback function.
  3729. * @param hadc ADC handle
  3730. * @retval HAL status.
  3731. */
  3732. HAL_StatusTypeDef HAL_ADCEx_InjectedStart_IT(ADC_HandleTypeDef* hadc)
  3733. {
  3734. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  3735. /* Check the parameters */
  3736. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  3737. /* Process locked */
  3738. __HAL_LOCK(hadc);
  3739. /* Enable the ADC peripheral */
  3740. tmp_hal_status = ADC_Enable(hadc);
  3741. /* Start conversion if ADC is effectively enabled */
  3742. if (tmp_hal_status == HAL_OK)
  3743. {
  3744. /* Set ADC state */
  3745. /* - Clear state bitfield related to injected group conversion results */
  3746. /* - Set state bitfield related to injected operation */
  3747. ADC_STATE_CLR_SET(hadc->State,
  3748. HAL_ADC_STATE_READY | HAL_ADC_STATE_INJ_EOC,
  3749. HAL_ADC_STATE_INJ_BUSY);
  3750. /* Check if a regular conversion is ongoing */
  3751. /* Note: On this device, there is no ADC error code fields related to */
  3752. /* conversions on group injected only. In case of conversion on */
  3753. /* going on group regular, no error code is reset. */
  3754. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_REG_BUSY))
  3755. {
  3756. /* Reset ADC all error code fields */
  3757. ADC_CLEAR_ERRORCODE(hadc);
  3758. }
  3759. /* Process unlocked */
  3760. /* Unlock before starting ADC conversions: in case of potential */
  3761. /* interruption, to let the process to ADC IRQ Handler. */
  3762. __HAL_UNLOCK(hadc);
  3763. /* Set ADC error code to none */
  3764. ADC_CLEAR_ERRORCODE(hadc);
  3765. /* Clear injected group conversion flag */
  3766. /* (To ensure of no unknown state from potential previous ADC operations) */
  3767. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOC);
  3768. /* Enable end of conversion interrupt for injected channels */
  3769. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_JEOC);
  3770. /* Enable conversion of injected group. */
  3771. /* If software start has been selected, conversion starts immediately. */
  3772. /* If external trigger has been selected, conversion will start at next */
  3773. /* trigger event. */
  3774. /* If external trigger has been selected, conversion will start at next */
  3775. /* trigger event. */
  3776. /* If automatic injected conversion is enabled, conversion will start */
  3777. /* after next regular group conversion. */
  3778. if (ADC_IS_SOFTWARE_START_INJECTED(hadc) &&
  3779. HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO) )
  3780. {
  3781. /* Start ADC conversion on injected group with SW start */
  3782. SET_BIT(hadc->Instance->CR2, (ADC_CR2_JSWSTART | ADC_CR2_JEXTTRIG));
  3783. }
  3784. else
  3785. {
  3786. /* Start ADC conversion on injected group with external trigger */
  3787. SET_BIT(hadc->Instance->CR2, ADC_CR2_JEXTTRIG);
  3788. }
  3789. }
  3790. /* Return function status */
  3791. return tmp_hal_status;
  3792. }
  3793. #endif /* STM32F373xC || STM32F378xx */
  3794. #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
  3795. defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
  3796. defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
  3797. defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
  3798. /**
  3799. * @brief Stop ADC group injected conversion (potential conversion on going
  3800. * on ADC group regular is not impacted), disable ADC peripheral
  3801. * if no conversion is on going on group regular.
  3802. * Interruptions disabled in this function:
  3803. * - JEOC (end of conversion of injected group) and JEOS (end of
  3804. * sequence of injected group)
  3805. * @note To stop ADC conversion of both groups regular and injected and to
  3806. * to disable ADC peripheral, instead of using 2 functions
  3807. * @ref HAL_ADCEx_RegularStop() and @ref HAL_ADCEx_InjectedStop(),
  3808. * use function @ref HAL_ADC_Stop().
  3809. * @note If injected group mode auto-injection is enabled,
  3810. * function HAL_ADC_Stop must be used.
  3811. * @note Case of multimode enabled (for devices with several ADCs): This
  3812. * function must be called for ADC master first, then ADC slave.
  3813. * For ADC master, conversion is stopped and ADC is disabled.
  3814. * For ADC slave, ADC is disabled only (conversion stop of ADC master
  3815. * has already stopped conversion of ADC slave).
  3816. * @note In case of auto-injection mode, HAL_ADC_Stop must be used.
  3817. * @param hadc ADC handle
  3818. * @retval None
  3819. */
  3820. HAL_StatusTypeDef HAL_ADCEx_InjectedStop_IT(ADC_HandleTypeDef* hadc)
  3821. {
  3822. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  3823. /* Check the parameters */
  3824. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  3825. /* Process locked */
  3826. __HAL_LOCK(hadc);
  3827. /* Stop potential ADC conversion on going and disable ADC peripheral */
  3828. /* conditioned to: */
  3829. /* - In case of auto-injection mode, HAL_ADC_Stop must be used. */
  3830. /* - For ADC injected group conversion stop: */
  3831. /* On this STM32 family, conversion on the other group */
  3832. /* (group regular) can continue (groups regular and injected */
  3833. /* conversion stop commands are independent) */
  3834. /* - For ADC disable: */
  3835. /* No conversion on the other group (group regular) must be intended to */
  3836. /* continue (groups regular and injected are both impacted by */
  3837. /* ADC disable) */
  3838. if(HAL_IS_BIT_CLR(hadc->Instance->CFGR, ADC_CFGR_JAUTO))
  3839. {
  3840. /* 1. Stop potential conversion on going on injected group only. */
  3841. tmp_hal_status = ADC_ConversionStop(hadc, ADC_INJECTED_GROUP);
  3842. /* Disable ADC peripheral if conversion on ADC group injected is */
  3843. /* effectively stopped and if no conversion on the other group */
  3844. /* (ADC group regular) is intended to continue. */
  3845. if (tmp_hal_status == HAL_OK)
  3846. {
  3847. /* Disable ADC end of conversion interrupt for injected channels */
  3848. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_JEOC | ADC_IT_JEOS | ADC_IT_JQOVF));
  3849. if((ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET) &&
  3850. ((hadc->State & HAL_ADC_STATE_REG_BUSY) == RESET) )
  3851. {
  3852. /* 2. Disable the ADC peripheral */
  3853. tmp_hal_status = ADC_Disable(hadc);
  3854. /* Check if ADC is effectively disabled */
  3855. if (tmp_hal_status == HAL_OK)
  3856. {
  3857. /* Set ADC state */
  3858. ADC_STATE_CLR_SET(hadc->State,
  3859. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  3860. HAL_ADC_STATE_READY);
  3861. }
  3862. }
  3863. /* Conversion on ADC group injected group is stopped, but ADC is not */
  3864. /* disabled since conversion on ADC group regular is still on going. */
  3865. else
  3866. {
  3867. /* Set ADC state */
  3868. CLEAR_BIT(hadc->State, HAL_ADC_STATE_INJ_BUSY);
  3869. }
  3870. }
  3871. }
  3872. else
  3873. {
  3874. /* Update ADC state machine to error */
  3875. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  3876. tmp_hal_status = HAL_ERROR;
  3877. }
  3878. /* Process unlocked */
  3879. __HAL_UNLOCK(hadc);
  3880. /* Return function status */
  3881. return tmp_hal_status;
  3882. }
  3883. #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
  3884. /* STM32F302xC || STM32F303xC || STM32F358xx || */
  3885. /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
  3886. /* STM32F301x8 || STM32F302x8 || STM32F318xx */
  3887. #if defined(STM32F373xC) || defined(STM32F378xx)
  3888. /**
  3889. * @brief Stop conversion of injected channels, disable interruption of
  3890. * end-of-conversion. Disable ADC peripheral if no regular conversion
  3891. * is on going.
  3892. * @note If ADC must be disabled and if conversion is on going on
  3893. * regular group, function HAL_ADC_Stop must be used to stop both
  3894. * injected and regular groups, and disable the ADC.
  3895. * @param hadc ADC handle
  3896. * @retval None
  3897. */
  3898. HAL_StatusTypeDef HAL_ADCEx_InjectedStop_IT(ADC_HandleTypeDef* hadc)
  3899. {
  3900. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  3901. /* Check the parameters */
  3902. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  3903. /* Process locked */
  3904. __HAL_LOCK(hadc);
  3905. /* Stop potential conversion and disable ADC peripheral */
  3906. /* Conditioned to: */
  3907. /* - No conversion on the other group (regular group) is intended to */
  3908. /* continue (injected and regular groups stop conversion and ADC disable */
  3909. /* are common) */
  3910. /* - In case of auto-injection mode, HAL_ADC_Stop must be used. */
  3911. if(((hadc->State & HAL_ADC_STATE_REG_BUSY) == RESET) &&
  3912. HAL_IS_BIT_CLR(hadc->Instance->CR1, ADC_CR1_JAUTO) )
  3913. {
  3914. /* Stop potential conversion on going, on regular and injected groups */
  3915. /* Disable ADC peripheral */
  3916. tmp_hal_status = ADC_ConversionStop_Disable(hadc);
  3917. /* Check if ADC is effectively disabled */
  3918. if (tmp_hal_status == HAL_OK)
  3919. {
  3920. /* Disable ADC end of conversion interrupt for injected channels */
  3921. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_JEOC);
  3922. /* Set ADC state */
  3923. ADC_STATE_CLR_SET(hadc->State,
  3924. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  3925. HAL_ADC_STATE_READY);
  3926. }
  3927. }
  3928. else
  3929. {
  3930. /* Update ADC state machine to error */
  3931. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  3932. tmp_hal_status = HAL_ERROR;
  3933. }
  3934. /* Process unlocked */
  3935. __HAL_UNLOCK(hadc);
  3936. /* Return function status */
  3937. return tmp_hal_status;
  3938. }
  3939. #endif /* STM32F373xC || STM32F378xx */
  3940. #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
  3941. defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
  3942. defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx)
  3943. /**
  3944. * @brief With ADC configured in multimode, for ADC master:
  3945. * Enables ADC, starts conversion of regular group and transfers result
  3946. * through DMA.
  3947. * Multimode must have been previously configured using
  3948. * HAL_ADCEx_MultiModeConfigChannel() function.
  3949. * Interruptions enabled in this function:
  3950. * - DMA transfer complete
  3951. * - DMA half transfer
  3952. * - overrun
  3953. * Each of these interruptions has its dedicated callback function.
  3954. * @note ADC slave must be preliminarily enabled using single-mode
  3955. * HAL_ADC_Start() function.
  3956. * @param hadc ADC handle of ADC master (handle of ADC slave must not be used)
  3957. * @param pData The destination Buffer address.
  3958. * @param Length The length of data to be transferred from ADC peripheral to memory.
  3959. * @retval None
  3960. */
  3961. HAL_StatusTypeDef HAL_ADCEx_MultiModeStart_DMA(ADC_HandleTypeDef* hadc, uint32_t* pData, uint32_t Length)
  3962. {
  3963. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  3964. ADC_HandleTypeDef tmphadcSlave = {0};
  3965. ADC_Common_TypeDef *tmpADC_Common;
  3966. /* Check the parameters */
  3967. assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
  3968. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
  3969. assert_param(IS_ADC_EXTTRIG_EDGE(hadc->Init.ExternalTrigConvEdge));
  3970. assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DMAContinuousRequests));
  3971. /* Process locked */
  3972. __HAL_LOCK(hadc);
  3973. /* Perform ADC enable and conversion start if no conversion is on going */
  3974. /* (check on ADC master only) */
  3975. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  3976. {
  3977. /* Set a temporary handle of the ADC slave associated to the ADC master */
  3978. /* (Depending on STM32F3 product, there may be up to 2 ADC slaves) */
  3979. ADC_MULTI_SLAVE(hadc, &tmphadcSlave);
  3980. if (tmphadcSlave.Instance == NULL)
  3981. {
  3982. /* Update ADC state machine to error */
  3983. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  3984. /* Process unlocked */
  3985. __HAL_UNLOCK(hadc);
  3986. return HAL_ERROR;
  3987. }
  3988. /* Enable the ADC peripherals: master and slave (in case if not already */
  3989. /* enabled previously) */
  3990. tmp_hal_status = ADC_Enable(hadc);
  3991. if (tmp_hal_status == HAL_OK)
  3992. {
  3993. tmp_hal_status = ADC_Enable(&tmphadcSlave);
  3994. }
  3995. /* Start conversion all ADCs of multimode are effectively enabled */
  3996. if (tmp_hal_status == HAL_OK)
  3997. {
  3998. /* Set ADC state (ADC master) */
  3999. /* - Clear state bitfield related to regular group conversion results */
  4000. /* - Set state bitfield related to regular operation */
  4001. ADC_STATE_CLR_SET(hadc->State,
  4002. HAL_ADC_STATE_READY | HAL_ADC_STATE_REG_EOC | HAL_ADC_STATE_REG_OVR | HAL_ADC_STATE_REG_EOSMP | HAL_ADC_STATE_MULTIMODE_SLAVE,
  4003. HAL_ADC_STATE_REG_BUSY);
  4004. /* If conversions on group regular are also triggering group injected, */
  4005. /* update ADC state. */
  4006. if (READ_BIT(hadc->Instance->CFGR, ADC_CFGR_JAUTO) != RESET)
  4007. {
  4008. ADC_STATE_CLR_SET(hadc->State, HAL_ADC_STATE_INJ_EOC, HAL_ADC_STATE_INJ_BUSY);
  4009. }
  4010. /* Process unlocked */
  4011. /* Unlock before starting ADC conversions: in case of potential */
  4012. /* interruption, to let the process to ADC IRQ Handler. */
  4013. __HAL_UNLOCK(hadc);
  4014. /* Set ADC error code to none */
  4015. ADC_CLEAR_ERRORCODE(hadc);
  4016. /* Set the DMA transfer complete callback */
  4017. hadc->DMA_Handle->XferCpltCallback = ADC_DMAConvCplt;
  4018. /* Set the DMA half transfer complete callback */
  4019. hadc->DMA_Handle->XferHalfCpltCallback = ADC_DMAHalfConvCplt;
  4020. /* Set the DMA error callback */
  4021. hadc->DMA_Handle->XferErrorCallback = ADC_DMAError ;
  4022. /* Pointer to the common control register to which is belonging hadc */
  4023. /* (Depending on STM32F3 product, there may be up to 4 ADC and 2 common */
  4024. /* control registers) */
  4025. tmpADC_Common = ADC_COMMON_REGISTER(hadc);
  4026. /* Manage ADC and DMA start: ADC overrun interruption, DMA start, ADC */
  4027. /* start (in case of SW start): */
  4028. /* Clear regular group conversion flag and overrun flag */
  4029. /* (To ensure of no unknown state from potential previous ADC operations) */
  4030. __HAL_ADC_CLEAR_FLAG(hadc, (ADC_FLAG_EOC | ADC_FLAG_EOS | ADC_FLAG_OVR));
  4031. /* Enable ADC overrun interrupt */
  4032. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);
  4033. /* Start the DMA channel */
  4034. HAL_DMA_Start_IT(hadc->DMA_Handle, (uint32_t)&tmpADC_Common->CDR, (uint32_t)pData, Length);
  4035. /* Enable conversion of regular group. */
  4036. /* If software start has been selected, conversion starts immediately. */
  4037. /* If external trigger has been selected, conversion will start at next */
  4038. /* trigger event. */
  4039. SET_BIT(hadc->Instance->CR, ADC_CR_ADSTART);
  4040. }
  4041. else
  4042. {
  4043. /* Process unlocked */
  4044. __HAL_UNLOCK(hadc);
  4045. }
  4046. }
  4047. else
  4048. {
  4049. tmp_hal_status = HAL_BUSY;
  4050. }
  4051. /* Return function status */
  4052. return tmp_hal_status;
  4053. }
  4054. /**
  4055. * @brief With ADC configured in multimode, for ADC master:
  4056. * Stop ADC group regular conversion (potential conversion on going
  4057. * on ADC group injected is not impacted),
  4058. * disable ADC DMA transfer, disable ADC peripheral
  4059. * if no conversion is on going on group injected.
  4060. * Interruptions disabled in this function:
  4061. * - DMA transfer complete
  4062. * - DMA half transfer
  4063. * - overrun
  4064. * @note In case of auto-injection mode, this function also stop conversion
  4065. * on ADC group injected.
  4066. * @note Multimode is kept enabled after this function. To disable multimode
  4067. * (set with HAL_ADCEx_MultiModeConfigChannel() ), ADC must be
  4068. * reinitialized using HAL_ADC_Init() or HAL_ADC_ReInit().
  4069. * @note In case of DMA configured in circular mode, function
  4070. * HAL_ADC_Stop_DMA must be called after this function with handle of
  4071. * ADC slave, to properly disable the DMA channel of ADC slave.
  4072. * @param hadc ADC handle of ADC master (handle of ADC slave must not be used)
  4073. * @retval None
  4074. */
  4075. HAL_StatusTypeDef HAL_ADCEx_MultiModeStop_DMA(ADC_HandleTypeDef* hadc)
  4076. {
  4077. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  4078. uint32_t tickstart;
  4079. ADC_HandleTypeDef tmphadcSlave = {0};
  4080. /* Check the parameters */
  4081. assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
  4082. /* Process locked */
  4083. __HAL_LOCK(hadc);
  4084. /* 1. Stop potential multimode conversion on going, on regular and */
  4085. /* injected groups. */
  4086. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_INJECTED_GROUP);
  4087. /* Disable ADC peripheral if conversions are effectively stopped */
  4088. if (tmp_hal_status == HAL_OK)
  4089. {
  4090. /* Set a temporary handle of the ADC slave associated to the ADC master */
  4091. /* (Depending on STM32F3 product, there may be up to 2 ADC slaves) */
  4092. ADC_MULTI_SLAVE(hadc, &tmphadcSlave);
  4093. if (tmphadcSlave.Instance == NULL)
  4094. {
  4095. /* Update ADC state machine (ADC master) to error */
  4096. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  4097. /* Process unlocked */
  4098. __HAL_UNLOCK(hadc);
  4099. return HAL_ERROR;
  4100. }
  4101. /* Procedure to disable the ADC peripheral: wait for conversions */
  4102. /* effectively stopped (ADC master and ADC slave), then disable ADC */
  4103. /* 1. Wait until ADSTP=0 for ADC master and ADC slave */
  4104. tickstart = HAL_GetTick();
  4105. while(ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) ||
  4106. ADC_IS_CONVERSION_ONGOING_REGULAR(&tmphadcSlave) )
  4107. {
  4108. if((HAL_GetTick() - tickstart) > ADC_STOP_CONVERSION_TIMEOUT)
  4109. {
  4110. /* New check to avoid false timeout detection in case of preemption */
  4111. if(ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) ||
  4112. ADC_IS_CONVERSION_ONGOING_REGULAR(&tmphadcSlave) )
  4113. {
  4114. /* Update ADC state machine (ADC master) to error */
  4115. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  4116. /* Process unlocked */
  4117. __HAL_UNLOCK(hadc);
  4118. return HAL_ERROR;
  4119. }
  4120. }
  4121. }
  4122. /* Disable the DMA channel (in case of DMA in circular mode or stop while */
  4123. /* while DMA transfer is on going) */
  4124. /* Note: In case of ADC slave using its own DMA channel (multimode */
  4125. /* parameter "DMAAccessMode" set to disabled): */
  4126. /* DMA channel of ADC slave should stopped after this function with */
  4127. /* function HAL_ADC_Stop_DMA. */
  4128. tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
  4129. /* Check if DMA channel effectively disabled */
  4130. if (tmp_hal_status != HAL_OK)
  4131. {
  4132. /* Update ADC state machine to error */
  4133. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  4134. }
  4135. /* Disable ADC overrun interrupt */
  4136. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
  4137. /* 2. Disable the ADC peripherals: master and slave */
  4138. /* Update "tmp_hal_status" only if DMA channel disabling passed, */
  4139. /* to retain a potential failing status. */
  4140. if (tmp_hal_status == HAL_OK)
  4141. {
  4142. /* Check if ADC are effectively disabled */
  4143. if ((ADC_Disable(hadc) != HAL_ERROR) &&
  4144. (ADC_Disable(&tmphadcSlave) != HAL_ERROR) )
  4145. {
  4146. tmp_hal_status = HAL_OK;
  4147. /* Change ADC state (ADC master) */
  4148. ADC_STATE_CLR_SET(hadc->State,
  4149. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  4150. HAL_ADC_STATE_READY);
  4151. }
  4152. }
  4153. else
  4154. {
  4155. /* In case of error, attempt to disable ADC instances anyway */
  4156. ADC_Disable(hadc);
  4157. ADC_Disable(&tmphadcSlave);
  4158. /* Update ADC state machine (ADC master) to error */
  4159. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  4160. }
  4161. }
  4162. /* Process unlocked */
  4163. __HAL_UNLOCK(hadc);
  4164. /* Return function status */
  4165. return tmp_hal_status;
  4166. }
  4167. /**
  4168. * @brief Returns the last ADC Master&Slave regular conversions results data
  4169. * in the selected multi mode.
  4170. * @note Reading register CDR does not clear flag ADC flag EOC
  4171. * (ADC group regular end of unitary conversion),
  4172. * as it is the case for independent mode data register.
  4173. * @param hadc ADC handle of ADC master (handle of ADC slave must not be used)
  4174. * @retval The converted data value.
  4175. */
  4176. uint32_t HAL_ADCEx_MultiModeGetValue(ADC_HandleTypeDef* hadc)
  4177. {
  4178. ADC_Common_TypeDef *tmpADC_Common;
  4179. /* Check the parameters */
  4180. assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
  4181. /* Prevent unused argument(s) compilation warning */
  4182. UNUSED(hadc);
  4183. /* Pointer to the common control register to which is belonging hadc */
  4184. /* (Depending on STM32F3 product, there may be up to 4 ADC and 2 common */
  4185. /* control registers) */
  4186. tmpADC_Common = ADC_COMMON_REGISTER(hadc);
  4187. /* Return the multi mode conversion value */
  4188. return tmpADC_Common->CDR;
  4189. }
  4190. #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
  4191. /* STM32F302xC || STM32F303xC || STM32F358xx || */
  4192. /* STM32F303x8 || STM32F334x8 || STM32F328xx */
  4193. #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
  4194. defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
  4195. defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
  4196. defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
  4197. /**
  4198. * @brief Get ADC injected group conversion result.
  4199. * @note Reading register JDRx automatically clears ADC flag JEOC
  4200. * (ADC group injected end of unitary conversion).
  4201. * @note This function does not clear ADC flag JEOS
  4202. * (ADC group injected end of sequence conversion)
  4203. * Occurrence of flag JEOS rising:
  4204. * - If sequencer is composed of 1 rank, flag JEOS is equivalent
  4205. * to flag JEOC.
  4206. * - If sequencer is composed of several ranks, during the scan
  4207. * sequence flag JEOC only is raised, at the end of the scan sequence
  4208. * both flags JEOC and EOS are raised.
  4209. * Flag JEOS must not be cleared by this function because
  4210. * it would not be compliant with low power features
  4211. * (feature low power auto-wait, not available on all STM32 families).
  4212. * To clear this flag, either use function:
  4213. * in programming model IT: @ref HAL_ADC_IRQHandler(), in programming
  4214. * model polling: @ref HAL_ADCEx_InjectedPollForConversion()
  4215. * or @ref __HAL_ADC_CLEAR_FLAG(&hadc, ADC_FLAG_JEOS).
  4216. * @param hadc ADC handle
  4217. * @param InjectedRank the converted ADC injected rank.
  4218. * This parameter can be one of the following values:
  4219. * @arg ADC_INJECTED_RANK_1: Injected Channel1 selected
  4220. * @arg ADC_INJECTED_RANK_2: Injected Channel2 selected
  4221. * @arg ADC_INJECTED_RANK_3: Injected Channel3 selected
  4222. * @arg ADC_INJECTED_RANK_4: Injected Channel4 selected
  4223. * @retval ADC group injected conversion data
  4224. */
  4225. uint32_t HAL_ADCEx_InjectedGetValue(ADC_HandleTypeDef* hadc, uint32_t InjectedRank)
  4226. {
  4227. uint32_t tmp_jdr = 0U;
  4228. /* Check the parameters */
  4229. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  4230. assert_param(IS_ADC_INJECTED_RANK(InjectedRank));
  4231. /* Note: ADC flag JEOC is not cleared here by software because */
  4232. /* automatically cleared by hardware when reading register JDRx. */
  4233. /* Get ADC converted value */
  4234. switch(InjectedRank)
  4235. {
  4236. case ADC_INJECTED_RANK_4:
  4237. tmp_jdr = hadc->Instance->JDR4;
  4238. break;
  4239. case ADC_INJECTED_RANK_3:
  4240. tmp_jdr = hadc->Instance->JDR3;
  4241. break;
  4242. case ADC_INJECTED_RANK_2:
  4243. tmp_jdr = hadc->Instance->JDR2;
  4244. break;
  4245. case ADC_INJECTED_RANK_1:
  4246. default:
  4247. tmp_jdr = hadc->Instance->JDR1;
  4248. break;
  4249. }
  4250. /* Return ADC converted value */
  4251. return tmp_jdr;
  4252. }
  4253. #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
  4254. /* STM32F302xC || STM32F303xC || STM32F358xx || */
  4255. /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
  4256. /* STM32F301x8 || STM32F302x8 || STM32F318xx */
  4257. #if defined(STM32F373xC) || defined(STM32F378xx)
  4258. /**
  4259. * @brief Get ADC injected group conversion result.
  4260. * @note Reading register JDRx automatically clears ADC flag JEOC
  4261. * (ADC group injected end of unitary conversion).
  4262. * @note This function does not clear ADC flag JEOS
  4263. * (ADC group injected end of sequence conversion)
  4264. * Occurrence of flag JEOS rising:
  4265. * - If sequencer is composed of 1 rank, flag JEOS is equivalent
  4266. * to flag JEOC.
  4267. * - If sequencer is composed of several ranks, during the scan
  4268. * sequence flag JEOC only is raised, at the end of the scan sequence
  4269. * both flags JEOC and EOS are raised.
  4270. * Flag JEOS must not be cleared by this function because
  4271. * it would not be compliant with low power features
  4272. * (feature low power auto-wait, not available on all STM32 families).
  4273. * To clear this flag, either use function:
  4274. * in programming model IT: @ref HAL_ADC_IRQHandler(), in programming
  4275. * model polling: @ref HAL_ADCEx_InjectedPollForConversion()
  4276. * or @ref __HAL_ADC_CLEAR_FLAG(&hadc, ADC_FLAG_JEOS).
  4277. * @param hadc ADC handle
  4278. * @param InjectedRank the converted ADC injected rank.
  4279. * This parameter can be one of the following values:
  4280. * @arg ADC_INJECTED_RANK_1: Injected Channel1 selected
  4281. * @arg ADC_INJECTED_RANK_2: Injected Channel2 selected
  4282. * @arg ADC_INJECTED_RANK_3: Injected Channel3 selected
  4283. * @arg ADC_INJECTED_RANK_4: Injected Channel4 selected
  4284. * @retval ADC group injected conversion data
  4285. */
  4286. uint32_t HAL_ADCEx_InjectedGetValue(ADC_HandleTypeDef* hadc, uint32_t InjectedRank)
  4287. {
  4288. uint32_t tmp_jdr = 0U;
  4289. /* Check the parameters */
  4290. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  4291. assert_param(IS_ADC_INJECTED_RANK(InjectedRank));
  4292. /* Get ADC converted value */
  4293. switch(InjectedRank)
  4294. {
  4295. case ADC_INJECTED_RANK_4:
  4296. tmp_jdr = hadc->Instance->JDR4;
  4297. break;
  4298. case ADC_INJECTED_RANK_3:
  4299. tmp_jdr = hadc->Instance->JDR3;
  4300. break;
  4301. case ADC_INJECTED_RANK_2:
  4302. tmp_jdr = hadc->Instance->JDR2;
  4303. break;
  4304. case ADC_INJECTED_RANK_1:
  4305. default:
  4306. tmp_jdr = hadc->Instance->JDR1;
  4307. break;
  4308. }
  4309. /* Return ADC converted value */
  4310. return tmp_jdr;
  4311. }
  4312. #endif /* STM32F373xC || STM32F378xx */
  4313. #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
  4314. defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
  4315. defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
  4316. defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
  4317. /**
  4318. * @brief Stop ADC group regular conversion (potential conversion on going
  4319. * on ADC group injected is not impacted), disable ADC peripheral
  4320. * if no conversion is on going on group injected.
  4321. * @note To stop ADC conversion of both groups regular and injected and to
  4322. * to disable ADC peripheral, instead of using 2 functions
  4323. * @ref HAL_ADCEx_RegularStop() and @ref HAL_ADCEx_InjectedStop(),
  4324. * use function @ref HAL_ADC_Stop().
  4325. * @note In case of auto-injection mode, this function also stop conversion
  4326. * on ADC group injected.
  4327. * @param hadc ADC handle
  4328. * @retval HAL status.
  4329. */
  4330. HAL_StatusTypeDef HAL_ADCEx_RegularStop(ADC_HandleTypeDef* hadc)
  4331. {
  4332. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  4333. /* Check the parameters */
  4334. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  4335. /* Process locked */
  4336. __HAL_LOCK(hadc);
  4337. /* Stop potential ADC conversion on going and disable ADC peripheral */
  4338. /* conditioned to: */
  4339. /* - For ADC regular group conversion stop: */
  4340. /* On this STM32 family, conversion on the other group */
  4341. /* (group injected) can continue (groups regular and injected */
  4342. /* conversion stop commands are independent) */
  4343. /* - For ADC disable: */
  4344. /* No conversion on the other group (group injected) must be intended to */
  4345. /* continue (groups regular and injected are both impacted by */
  4346. /* ADC disable) */
  4347. /* 1. Stop potential conversion on going, on regular group only */
  4348. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_GROUP);
  4349. /* Disable ADC peripheral if conversion on ADC group regular is */
  4350. /* effectively stopped and if no conversion on the other group */
  4351. /* (ADC group injected) is intended to continue. */
  4352. if((ADC_IS_CONVERSION_ONGOING_INJECTED(hadc) == RESET) &&
  4353. ((hadc->State & HAL_ADC_STATE_INJ_BUSY) == RESET) )
  4354. {
  4355. /* 2. Disable the ADC peripheral */
  4356. tmp_hal_status = ADC_Disable(hadc);
  4357. /* Check if ADC is effectively disabled */
  4358. if (tmp_hal_status == HAL_OK)
  4359. {
  4360. /* Set ADC state */
  4361. ADC_STATE_CLR_SET(hadc->State,
  4362. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  4363. HAL_ADC_STATE_READY);
  4364. }
  4365. }
  4366. /* Conversion on ADC group regular group is stopped, but ADC is not */
  4367. /* disabled since conversion on ADC group injected is still on going. */
  4368. else
  4369. {
  4370. /* Set ADC state */
  4371. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  4372. }
  4373. /* Process unlocked */
  4374. __HAL_UNLOCK(hadc);
  4375. /* Return function status */
  4376. return tmp_hal_status;
  4377. }
  4378. /**
  4379. * @brief Stop ADC group regular conversion (potential conversion on going
  4380. * on ADC group injected is not impacted), disable ADC peripheral
  4381. * if no conversion is on going on group injected.
  4382. * Interruptions disabled in this function:
  4383. * - EOC (end of conversion of regular group) and EOS (end of
  4384. * sequence of regular group)
  4385. * - overrun
  4386. * @note To stop ADC conversion of both groups regular and injected and to
  4387. * to disable ADC peripheral, instead of using 2 functions
  4388. * @ref HAL_ADCEx_RegularStop() and @ref HAL_ADCEx_InjectedStop(),
  4389. * use function @ref HAL_ADC_Stop().
  4390. * @note In case of auto-injection mode, this function also stop conversion
  4391. * on ADC group injected.
  4392. * @param hadc ADC handle
  4393. * @retval HAL status.
  4394. */
  4395. HAL_StatusTypeDef HAL_ADCEx_RegularStop_IT(ADC_HandleTypeDef* hadc)
  4396. {
  4397. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  4398. /* Check the parameters */
  4399. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  4400. /* Process locked */
  4401. __HAL_LOCK(hadc);
  4402. /* Stop potential ADC conversion on going and disable ADC peripheral */
  4403. /* conditioned to: */
  4404. /* - For ADC regular group conversion stop: */
  4405. /* On this STM32 family, conversion on the other group */
  4406. /* (group injected) can continue (groups regular and injected */
  4407. /* conversion stop commands are independent) */
  4408. /* - For ADC disable: */
  4409. /* No conversion on the other group (group injected) must be intended to */
  4410. /* continue (groups regular and injected are both impacted by */
  4411. /* ADC disable) */
  4412. /* 1. Stop potential conversion on going, on regular group only */
  4413. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_GROUP);
  4414. /* Disable ADC peripheral if conversion on ADC group regular is */
  4415. /* effectively stopped and if no conversion on the other group */
  4416. /* (ADC group injected) is intended to continue. */
  4417. if((ADC_IS_CONVERSION_ONGOING_INJECTED(hadc) == RESET) &&
  4418. ((hadc->State & HAL_ADC_STATE_INJ_BUSY) == RESET) )
  4419. {
  4420. /* Disable ADC end of conversion interrupt for regular group */
  4421. /* Disable ADC overrun interrupt */
  4422. __HAL_ADC_DISABLE_IT(hadc, (ADC_IT_EOC | ADC_IT_EOS | ADC_IT_OVR));
  4423. /* 2. Disable the ADC peripheral */
  4424. tmp_hal_status = ADC_Disable(hadc);
  4425. /* Check if ADC is effectively disabled */
  4426. if (tmp_hal_status == HAL_OK)
  4427. {
  4428. /* Set ADC state */
  4429. ADC_STATE_CLR_SET(hadc->State,
  4430. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  4431. HAL_ADC_STATE_READY);
  4432. }
  4433. }
  4434. /* Conversion on ADC group regular group is stopped, but ADC is not */
  4435. /* disabled since conversion on ADC group injected is still on going. */
  4436. else
  4437. {
  4438. /* Set ADC state */
  4439. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  4440. }
  4441. /* Process unlocked */
  4442. __HAL_UNLOCK(hadc);
  4443. /* Return function status */
  4444. return tmp_hal_status;
  4445. }
  4446. /**
  4447. * @brief Stop ADC group regular conversion (potential conversion on going
  4448. * on ADC group injected is not impacted),
  4449. * disable ADC DMA transfer, disable ADC peripheral
  4450. * if no conversion is on going on group injected.
  4451. * Interruptions disabled in this function:
  4452. * - DMA transfer complete
  4453. * - DMA half transfer
  4454. * - overrun
  4455. * @note To stop ADC conversion of both groups regular and injected and to
  4456. * to disable ADC peripheral, instead of using 2 functions
  4457. * @ref HAL_ADCEx_RegularStop() and @ref HAL_ADCEx_InjectedStop(),
  4458. * use function @ref HAL_ADC_Stop().
  4459. * @note Case of multimode enabled (for devices with several ADCs): This
  4460. * function is for single-ADC mode only. For multimode, use the
  4461. * dedicated MultimodeStop function.
  4462. * @param hadc ADC handle
  4463. * @retval HAL status.
  4464. */
  4465. HAL_StatusTypeDef HAL_ADCEx_RegularStop_DMA(ADC_HandleTypeDef* hadc)
  4466. {
  4467. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  4468. /* Check the parameters */
  4469. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  4470. /* Process locked */
  4471. __HAL_LOCK(hadc);
  4472. /* Stop potential ADC conversion on going and disable ADC peripheral */
  4473. /* conditioned to: */
  4474. /* - For ADC regular group conversion stop: */
  4475. /* On this STM32 family, conversion on the other group */
  4476. /* (group injected) can continue (groups regular and injected */
  4477. /* conversion stop commands are independent) */
  4478. /* - For ADC disable: */
  4479. /* No conversion on the other group (group injected) must be intended to */
  4480. /* continue (groups regular and injected are both impacted by */
  4481. /* ADC disable) */
  4482. /* 1. Stop potential conversion on going, on regular group only */
  4483. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_GROUP);
  4484. /* Disable ADC peripheral if conversion on ADC group regular is */
  4485. /* effectively stopped and if no conversion on the other group */
  4486. /* (ADC group injected) is intended to continue. */
  4487. if((ADC_IS_CONVERSION_ONGOING_INJECTED(hadc) == RESET) &&
  4488. ((hadc->State & HAL_ADC_STATE_INJ_BUSY) == RESET) )
  4489. {
  4490. /* Disable ADC DMA (ADC DMA configuration ADC_CFGR_DMACFG is kept) */
  4491. CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_DMAEN);
  4492. /* Disable the DMA channel (in case of DMA in circular mode or stop while */
  4493. /* while DMA transfer is on going) */
  4494. tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
  4495. /* Check if DMA channel effectively disabled */
  4496. if (tmp_hal_status != HAL_OK)
  4497. {
  4498. /* Update ADC state machine to error */
  4499. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  4500. }
  4501. /* Disable ADC overrun interrupt */
  4502. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
  4503. /* 2. Disable the ADC peripheral */
  4504. /* Update "tmp_hal_status" only if DMA channel disabling passed, */
  4505. /* to retain a potential failing status. */
  4506. if (tmp_hal_status == HAL_OK)
  4507. {
  4508. tmp_hal_status = ADC_Disable(hadc);
  4509. }
  4510. else
  4511. {
  4512. ADC_Disable(hadc);
  4513. }
  4514. /* Check if ADC is effectively disabled */
  4515. if (tmp_hal_status == HAL_OK)
  4516. {
  4517. /* Set ADC state */
  4518. ADC_STATE_CLR_SET(hadc->State,
  4519. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  4520. HAL_ADC_STATE_READY);
  4521. }
  4522. }
  4523. /* Conversion on ADC group regular group is stopped, but ADC is not */
  4524. /* disabled since conversion on ADC group injected is still on going. */
  4525. else
  4526. {
  4527. /* Set ADC state */
  4528. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  4529. }
  4530. /* Process unlocked */
  4531. __HAL_UNLOCK(hadc);
  4532. /* Return function status */
  4533. return tmp_hal_status;
  4534. }
  4535. #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
  4536. defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
  4537. defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx)
  4538. /**
  4539. * @brief With ADC configured in multimode, for ADC master:
  4540. * Stop ADC group regular conversion (potential conversion on going
  4541. * on ADC group injected is not impacted),
  4542. * disable ADC DMA transfer, disable ADC peripheral
  4543. * if no conversion is on going on group injected.
  4544. * Interruptions disabled in this function:
  4545. * - DMA transfer complete
  4546. * - DMA half transfer
  4547. * - overrun
  4548. * @note To stop ADC conversion of both groups regular and injected and to
  4549. * to disable ADC peripheral, instead of using 2 functions
  4550. * @ref HAL_ADCEx_RegularMultiModeStop_DMA() and
  4551. * @ref HAL_ADCEx_InjectedStop(), use function
  4552. * @ref HAL_ADCEx_MultiModeStop_DMA.
  4553. * @note In case of auto-injection mode, this function also stop conversion
  4554. * on ADC group injected.
  4555. * @note Multimode is kept enabled after this function. To disable multimode
  4556. * (set with HAL_ADCEx_MultiModeConfigChannel() ), ADC must be
  4557. * reinitialized using HAL_ADC_Init() or HAL_ADC_ReInit().
  4558. * @note In case of DMA configured in circular mode, function
  4559. * HAL_ADC_Stop_DMA must be called after this function with handle of
  4560. * ADC slave, to properly disable the DMA channel of ADC slave.
  4561. * @param hadc ADC handle of ADC master (handle of ADC slave must not be used)
  4562. * @retval None
  4563. */
  4564. HAL_StatusTypeDef HAL_ADCEx_RegularMultiModeStop_DMA(ADC_HandleTypeDef* hadc)
  4565. {
  4566. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  4567. uint32_t tickstart;
  4568. ADC_HandleTypeDef tmphadcSlave = {0};
  4569. /* Check the parameters */
  4570. assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
  4571. /* Process locked */
  4572. __HAL_LOCK(hadc);
  4573. /* Stop potential ADC conversion on going and disable ADC peripheral */
  4574. /* conditioned to: */
  4575. /* - For ADC regular group conversion stop: */
  4576. /* On this STM32 family, conversion on the other group */
  4577. /* (group injected) can continue (groups regular and injected */
  4578. /* conversion stop commands are independent) */
  4579. /* - For ADC disable: */
  4580. /* No conversion on the other group (group injected) must be intended to */
  4581. /* continue (groups regular and injected are both impacted by */
  4582. /* ADC disable) */
  4583. /* 1. Stop potential conversion on going, on regular group only */
  4584. tmp_hal_status = ADC_ConversionStop(hadc, ADC_REGULAR_GROUP);
  4585. /* Disable ADC peripheral if conversion on ADC group regular is */
  4586. /* effectively stopped and if no conversion on the other group */
  4587. /* (ADC group injected) is intended to continue. */
  4588. if((ADC_IS_CONVERSION_ONGOING_INJECTED(hadc) == RESET) &&
  4589. ((hadc->State & HAL_ADC_STATE_INJ_BUSY) == RESET) )
  4590. {
  4591. /* Set a temporary handle of the ADC slave associated to the ADC master */
  4592. /* (Depending on STM32F3 product, there may be up to 2 ADC slaves) */
  4593. ADC_MULTI_SLAVE(hadc, &tmphadcSlave);
  4594. if (tmphadcSlave.Instance == NULL)
  4595. {
  4596. /* Update ADC state machine (ADC master) to error */
  4597. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  4598. /* Process unlocked */
  4599. __HAL_UNLOCK(hadc);
  4600. return HAL_ERROR;
  4601. }
  4602. /* Procedure to disable the ADC peripheral: wait for conversions */
  4603. /* effectively stopped (ADC master and ADC slave), then disable ADC */
  4604. /* 1. Wait until ADSTP=0 for ADC master and ADC slave*/
  4605. tickstart = HAL_GetTick();
  4606. while(ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) ||
  4607. ADC_IS_CONVERSION_ONGOING_REGULAR(&tmphadcSlave) )
  4608. {
  4609. if((HAL_GetTick() - tickstart) > ADC_STOP_CONVERSION_TIMEOUT)
  4610. {
  4611. /* New check to avoid false timeout detection in case of preemption */
  4612. if(ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) ||
  4613. ADC_IS_CONVERSION_ONGOING_REGULAR(&tmphadcSlave) )
  4614. {
  4615. /* Update ADC state machine (ADC master) to error */
  4616. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  4617. /* Process unlocked */
  4618. __HAL_UNLOCK(hadc);
  4619. return HAL_ERROR;
  4620. }
  4621. }
  4622. }
  4623. /* Disable the DMA channel (in case of DMA in circular mode or stop while */
  4624. /* while DMA transfer is on going) */
  4625. /* Note: In case of ADC slave using its own DMA channel (multimode */
  4626. /* parameter "DMAAccessMode" set to disabled): */
  4627. /* DMA channel of ADC slave should stopped after this function with */
  4628. /* function HAL_ADC_Stop_DMA. */
  4629. tmp_hal_status = HAL_DMA_Abort(hadc->DMA_Handle);
  4630. /* Check if DMA channel effectively disabled */
  4631. if (tmp_hal_status != HAL_OK)
  4632. {
  4633. /* Update ADC state machine to error */
  4634. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  4635. }
  4636. /* Disable ADC overrun interrupt */
  4637. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_OVR);
  4638. /* 2. Disable the ADC peripherals: master and slave */
  4639. /* Update "tmp_hal_status" only if DMA channel disabling passed, */
  4640. /* to retain a potential failing status. */
  4641. if (tmp_hal_status == HAL_OK)
  4642. {
  4643. /* Check if ADC are effectively disabled */
  4644. if ((ADC_Disable(hadc) != HAL_ERROR) &&
  4645. (ADC_Disable(&tmphadcSlave) != HAL_ERROR) )
  4646. {
  4647. tmp_hal_status = HAL_OK;
  4648. /* Change ADC state (ADC master) */
  4649. ADC_STATE_CLR_SET(hadc->State,
  4650. HAL_ADC_STATE_REG_BUSY | HAL_ADC_STATE_INJ_BUSY,
  4651. HAL_ADC_STATE_READY);
  4652. }
  4653. }
  4654. else
  4655. {
  4656. /* In case of error, attempt to disable ADC instances anyway */
  4657. ADC_Disable(hadc);
  4658. ADC_Disable(&tmphadcSlave);
  4659. /* Update ADC state machine (ADC master) to error */
  4660. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  4661. }
  4662. }
  4663. /* Conversion on ADC group regular group is stopped, but ADC is not */
  4664. /* disabled since conversion on ADC group injected is still on going. */
  4665. else
  4666. {
  4667. /* Set ADC state */
  4668. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  4669. }
  4670. /* Process unlocked */
  4671. __HAL_UNLOCK(hadc);
  4672. /* Return function status */
  4673. return tmp_hal_status;
  4674. }
  4675. #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
  4676. /* STM32F302xC || STM32F303xC || STM32F358xx || */
  4677. /* STM32F303x8 || STM32F334x8 || STM32F328xx */
  4678. #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
  4679. /* STM32F302xC || STM32F303xC || STM32F358xx || */
  4680. /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
  4681. /* STM32F301x8 || STM32F302x8 || STM32F318xx */
  4682. /**
  4683. * @brief Injected conversion complete callback in non blocking mode
  4684. * @param hadc ADC handle
  4685. * @retval None
  4686. */
  4687. __weak void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef* hadc)
  4688. {
  4689. /* Prevent unused argument(s) compilation warning */
  4690. UNUSED(hadc);
  4691. /* NOTE : This function Should not be modified, when the callback is needed,
  4692. the HAL_ADCEx_InjectedConvCpltCallback could be implemented in the user file
  4693. */
  4694. }
  4695. #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
  4696. defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
  4697. defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
  4698. defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
  4699. /**
  4700. * @brief Injected context queue overflow flag callback.
  4701. * @note This callback is called if injected context queue is enabled
  4702. (parameter "QueueInjectedContext" in injected channel configuration)
  4703. and if a new injected context is set when queue is full (maximum 2
  4704. contexts).
  4705. * @param hadc ADC handle
  4706. * @retval None
  4707. */
  4708. __weak void HAL_ADCEx_InjectedQueueOverflowCallback(ADC_HandleTypeDef* hadc)
  4709. {
  4710. /* Prevent unused argument(s) compilation warning */
  4711. UNUSED(hadc);
  4712. /* NOTE : This function should not be modified. When the callback is needed,
  4713. function HAL_ADCEx_InjectedQueueOverflowCallback must be implemented
  4714. in the user file.
  4715. */
  4716. }
  4717. /**
  4718. * @brief Analog watchdog 2 callback in non blocking mode.
  4719. * @param hadc ADC handle
  4720. * @retval None
  4721. */
  4722. __weak void HAL_ADCEx_LevelOutOfWindow2Callback(ADC_HandleTypeDef* hadc)
  4723. {
  4724. /* Prevent unused argument(s) compilation warning */
  4725. UNUSED(hadc);
  4726. /* NOTE : This function should not be modified. When the callback is needed,
  4727. function HAL_ADC_LevelOoutOfWindow2Callback must be implemented in the user file.
  4728. */
  4729. }
  4730. /**
  4731. * @brief Analog watchdog 3 callback in non blocking mode.
  4732. * @param hadc ADC handle
  4733. * @retval None
  4734. */
  4735. __weak void HAL_ADCEx_LevelOutOfWindow3Callback(ADC_HandleTypeDef* hadc)
  4736. {
  4737. /* Prevent unused argument(s) compilation warning */
  4738. UNUSED(hadc);
  4739. /* NOTE : This function should not be modified. When the callback is needed,
  4740. function HAL_ADC_LevelOoutOfWindow3Callback must be implemented in the user file.
  4741. */
  4742. }
  4743. #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
  4744. /* STM32F302xC || STM32F303xC || STM32F358xx || */
  4745. /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
  4746. /* STM32F301x8 || STM32F302x8 || STM32F318xx */
  4747. /**
  4748. * @}
  4749. */
  4750. /** @defgroup ADCEx_Exported_Functions_Group3 ADCEx Peripheral Control functions
  4751. * @brief ADC Extended Peripheral Control functions
  4752. *
  4753. @verbatim
  4754. ===============================================================================
  4755. ##### Peripheral Control functions #####
  4756. ===============================================================================
  4757. [..] This section provides functions allowing to:
  4758. (+) Configure channels on regular group
  4759. (+) Configure channels on injected group
  4760. (+) Configure multimode
  4761. (+) Configure the analog watchdog
  4762. @endverbatim
  4763. * @{
  4764. */
  4765. #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
  4766. defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
  4767. defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
  4768. defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
  4769. /**
  4770. * @brief Configures the the selected channel to be linked to the regular
  4771. * group.
  4772. * @note In case of usage of internal measurement channels:
  4773. * Vbat/VrefInt/TempSensor.
  4774. * The recommended sampling time is at least:
  4775. * - For devices STM32F37x: 17.1us for temperature sensor
  4776. * - For the other STM32F3 devices: 2.2us for each of channels
  4777. * Vbat/VrefInt/TempSensor.
  4778. * These internal paths can be be disabled using function
  4779. * HAL_ADC_DeInit().
  4780. * @note Possibility to update parameters on the fly:
  4781. * This function initializes channel into regular group, following
  4782. * calls to this function can be used to reconfigure some parameters
  4783. * of structure "ADC_ChannelConfTypeDef" on the fly, without resetting
  4784. * the ADC.
  4785. * The setting of these parameters is conditioned to ADC state.
  4786. * For parameters constraints, see comments of structure
  4787. * "ADC_ChannelConfTypeDef".
  4788. * @param hadc ADC handle
  4789. * @param sConfig Structure ADC channel for regular group.
  4790. * @retval HAL status
  4791. */
  4792. HAL_StatusTypeDef HAL_ADC_ConfigChannel(ADC_HandleTypeDef* hadc, ADC_ChannelConfTypeDef* sConfig)
  4793. {
  4794. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  4795. ADC_Common_TypeDef *tmpADC_Common;
  4796. ADC_HandleTypeDef tmphadcSharingSameCommonRegister;
  4797. uint32_t tmpOffsetShifted;
  4798. __IO uint32_t wait_loop_index = 0U;
  4799. /* Check the parameters */
  4800. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  4801. assert_param(IS_ADC_REGULAR_RANK(sConfig->Rank));
  4802. assert_param(IS_ADC_SAMPLE_TIME(sConfig->SamplingTime));
  4803. assert_param(IS_ADC_SINGLE_DIFFERENTIAL(sConfig->SingleDiff));
  4804. assert_param(IS_ADC_OFFSET_NUMBER(sConfig->OffsetNumber));
  4805. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), sConfig->Offset));
  4806. /* Verification of channel number: Channels 1 to 14 are available in */
  4807. /* differential mode. Channels 15U, 16U, 17U, 18 can be used only in */
  4808. /* single-ended mode. */
  4809. if (sConfig->SingleDiff != ADC_DIFFERENTIAL_ENDED)
  4810. {
  4811. assert_param(IS_ADC_CHANNEL(sConfig->Channel));
  4812. }
  4813. else
  4814. {
  4815. assert_param(IS_ADC_DIFF_CHANNEL(sConfig->Channel));
  4816. }
  4817. /* Process locked */
  4818. __HAL_LOCK(hadc);
  4819. /* Parameters update conditioned to ADC state: */
  4820. /* Parameters that can be updated when ADC is disabled or enabled without */
  4821. /* conversion on going on regular group: */
  4822. /* - Channel number */
  4823. /* - Channel rank */
  4824. if (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  4825. {
  4826. /* Regular sequence configuration */
  4827. /* For Rank 1 to 4U */
  4828. if (sConfig->Rank < 5U)
  4829. {
  4830. MODIFY_REG(hadc->Instance->SQR1,
  4831. ADC_SQR1_RK(ADC_SQR2_SQ5, sConfig->Rank) ,
  4832. ADC_SQR1_RK(sConfig->Channel, sConfig->Rank) );
  4833. }
  4834. /* For Rank 5 to 9U */
  4835. else if (sConfig->Rank < 10U)
  4836. {
  4837. MODIFY_REG(hadc->Instance->SQR2,
  4838. ADC_SQR2_RK(ADC_SQR2_SQ5, sConfig->Rank) ,
  4839. ADC_SQR2_RK(sConfig->Channel, sConfig->Rank) );
  4840. }
  4841. /* For Rank 10 to 14U */
  4842. else if (sConfig->Rank < 15U)
  4843. {
  4844. MODIFY_REG(hadc->Instance->SQR3 ,
  4845. ADC_SQR3_RK(ADC_SQR3_SQ10, sConfig->Rank) ,
  4846. ADC_SQR3_RK(sConfig->Channel, sConfig->Rank) );
  4847. }
  4848. /* For Rank 15 to 16U */
  4849. else
  4850. {
  4851. MODIFY_REG(hadc->Instance->SQR4 ,
  4852. ADC_SQR4_RK(ADC_SQR4_SQ15, sConfig->Rank) ,
  4853. ADC_SQR4_RK(sConfig->Channel, sConfig->Rank) );
  4854. }
  4855. /* Parameters update conditioned to ADC state: */
  4856. /* Parameters that can be updated when ADC is disabled or enabled without */
  4857. /* conversion on going on regular group: */
  4858. /* - Channel sampling time */
  4859. /* - Channel offset */
  4860. if (ADC_IS_CONVERSION_ONGOING_REGULAR_INJECTED(hadc) == RESET)
  4861. {
  4862. /* Channel sampling time configuration */
  4863. /* For channels 10 to 18U */
  4864. if (sConfig->Channel >= ADC_CHANNEL_10)
  4865. {
  4866. MODIFY_REG(hadc->Instance->SMPR2 ,
  4867. ADC_SMPR2(ADC_SMPR2_SMP10, sConfig->Channel) ,
  4868. ADC_SMPR2(sConfig->SamplingTime, sConfig->Channel) );
  4869. }
  4870. else /* For channels 1 to 9U */
  4871. {
  4872. MODIFY_REG(hadc->Instance->SMPR1 ,
  4873. ADC_SMPR1(ADC_SMPR1_SMP0, sConfig->Channel) ,
  4874. ADC_SMPR1(sConfig->SamplingTime, sConfig->Channel) );
  4875. }
  4876. /* Configure the offset: offset enable/disable, channel, offset value */
  4877. /* Shift the offset in function of the selected ADC resolution. */
  4878. /* Offset has to be left-aligned on bit 11U, the LSB (right bits) are set */
  4879. /* to 0. */
  4880. tmpOffsetShifted = ADC_OFFSET_SHIFT_RESOLUTION(hadc, sConfig->Offset);
  4881. /* Configure the selected offset register: */
  4882. /* - Enable offset */
  4883. /* - Set channel number */
  4884. /* - Set offset value */
  4885. switch (sConfig->OffsetNumber)
  4886. {
  4887. case ADC_OFFSET_1:
  4888. /* Configure offset register 1U */
  4889. MODIFY_REG(hadc->Instance->OFR1 ,
  4890. ADC_OFR1_OFFSET1_CH |
  4891. ADC_OFR1_OFFSET1 ,
  4892. ADC_OFR1_OFFSET1_EN |
  4893. ADC_OFR_CHANNEL(sConfig->Channel) |
  4894. tmpOffsetShifted );
  4895. break;
  4896. case ADC_OFFSET_2:
  4897. /* Configure offset register 2U */
  4898. MODIFY_REG(hadc->Instance->OFR2 ,
  4899. ADC_OFR2_OFFSET2_CH |
  4900. ADC_OFR2_OFFSET2 ,
  4901. ADC_OFR2_OFFSET2_EN |
  4902. ADC_OFR_CHANNEL(sConfig->Channel) |
  4903. tmpOffsetShifted );
  4904. break;
  4905. case ADC_OFFSET_3:
  4906. /* Configure offset register 3U */
  4907. MODIFY_REG(hadc->Instance->OFR3 ,
  4908. ADC_OFR3_OFFSET3_CH |
  4909. ADC_OFR3_OFFSET3 ,
  4910. ADC_OFR3_OFFSET3_EN |
  4911. ADC_OFR_CHANNEL(sConfig->Channel) |
  4912. tmpOffsetShifted );
  4913. break;
  4914. case ADC_OFFSET_4:
  4915. /* Configure offset register 4U */
  4916. MODIFY_REG(hadc->Instance->OFR4 ,
  4917. ADC_OFR4_OFFSET4_CH |
  4918. ADC_OFR4_OFFSET4 ,
  4919. ADC_OFR4_OFFSET4_EN |
  4920. ADC_OFR_CHANNEL(sConfig->Channel) |
  4921. tmpOffsetShifted );
  4922. break;
  4923. /* Case ADC_OFFSET_NONE */
  4924. default :
  4925. /* Scan OFR1, OFR2, OFR3, OFR4 to check if the selected channel is */
  4926. /* enabled. If this is the case, offset OFRx is disabled. */
  4927. if (((hadc->Instance->OFR1) & ADC_OFR1_OFFSET1_CH) == ADC_OFR_CHANNEL(sConfig->Channel))
  4928. {
  4929. /* Disable offset OFR1*/
  4930. CLEAR_BIT(hadc->Instance->OFR1, ADC_OFR1_OFFSET1_EN);
  4931. }
  4932. if (((hadc->Instance->OFR2) & ADC_OFR2_OFFSET2_CH) == ADC_OFR_CHANNEL(sConfig->Channel))
  4933. {
  4934. /* Disable offset OFR2*/
  4935. CLEAR_BIT(hadc->Instance->OFR2, ADC_OFR2_OFFSET2_EN);
  4936. }
  4937. if (((hadc->Instance->OFR3) & ADC_OFR3_OFFSET3_CH) == ADC_OFR_CHANNEL(sConfig->Channel))
  4938. {
  4939. /* Disable offset OFR3*/
  4940. CLEAR_BIT(hadc->Instance->OFR3, ADC_OFR3_OFFSET3_EN);
  4941. }
  4942. if (((hadc->Instance->OFR4) & ADC_OFR4_OFFSET4_CH) == ADC_OFR_CHANNEL(sConfig->Channel))
  4943. {
  4944. /* Disable offset OFR4*/
  4945. CLEAR_BIT(hadc->Instance->OFR4, ADC_OFR4_OFFSET4_EN);
  4946. }
  4947. break;
  4948. }
  4949. }
  4950. /* Parameters update conditioned to ADC state: */
  4951. /* Parameters that can be updated only when ADC is disabled: */
  4952. /* - Single or differential mode */
  4953. /* - Internal measurement channels: Vbat/VrefInt/TempSensor */
  4954. if (ADC_IS_ENABLE(hadc) == RESET)
  4955. {
  4956. /* Configuration of differential mode */
  4957. if (sConfig->SingleDiff != ADC_DIFFERENTIAL_ENDED)
  4958. {
  4959. /* Disable differential mode (default mode: single-ended) */
  4960. CLEAR_BIT(hadc->Instance->DIFSEL, ADC_DIFSEL_CHANNEL(sConfig->Channel));
  4961. }
  4962. else
  4963. {
  4964. /* Enable differential mode */
  4965. SET_BIT(hadc->Instance->DIFSEL, ADC_DIFSEL_CHANNEL(sConfig->Channel));
  4966. /* Channel sampling time configuration (channel ADC_INx +1 */
  4967. /* corresponding to differential negative input). */
  4968. /* For channels 10 to 18U */
  4969. if (sConfig->Channel >= ADC_CHANNEL_10)
  4970. {
  4971. MODIFY_REG(hadc->Instance->SMPR2,
  4972. ADC_SMPR2(ADC_SMPR2_SMP10, sConfig->Channel +1U) ,
  4973. ADC_SMPR2(sConfig->SamplingTime, sConfig->Channel +1U) );
  4974. }
  4975. else /* For channels 1 to 9U */
  4976. {
  4977. MODIFY_REG(hadc->Instance->SMPR1,
  4978. ADC_SMPR1(ADC_SMPR1_SMP0, sConfig->Channel +1U) ,
  4979. ADC_SMPR1(sConfig->SamplingTime, sConfig->Channel +1U) );
  4980. }
  4981. }
  4982. /* Management of internal measurement channels: VrefInt/TempSensor/Vbat */
  4983. /* internal measurement paths enable: If internal channel selected, */
  4984. /* enable dedicated internal buffers and path. */
  4985. /* Note: these internal measurement paths can be disabled using */
  4986. /* HAL_ADC_DeInit(). */
  4987. /* Configuration of common ADC parameters */
  4988. /* Pointer to the common control register to which is belonging hadc */
  4989. /* (Depending on STM32F3 product, there may be up to 4 ADC and 2 common */
  4990. /* control registers) */
  4991. tmpADC_Common = ADC_COMMON_REGISTER(hadc);
  4992. /* If the requested internal measurement path has already been enabled, */
  4993. /* bypass the configuration processing. */
  4994. if (( (sConfig->Channel == ADC_CHANNEL_TEMPSENSOR) &&
  4995. (HAL_IS_BIT_CLR(tmpADC_Common->CCR, ADC_CCR_TSEN)) ) ||
  4996. ( (sConfig->Channel == ADC_CHANNEL_VBAT) &&
  4997. (HAL_IS_BIT_CLR(tmpADC_Common->CCR, ADC_CCR_VBATEN)) ) ||
  4998. ( (sConfig->Channel == ADC_CHANNEL_VREFINT) &&
  4999. (HAL_IS_BIT_CLR(tmpADC_Common->CCR, ADC_CCR_VREFEN)))
  5000. )
  5001. {
  5002. /* Configuration of common ADC parameters (continuation) */
  5003. /* Set handle of the other ADC sharing the same common register */
  5004. ADC_COMMON_ADC_OTHER(hadc, &tmphadcSharingSameCommonRegister);
  5005. /* Software is allowed to change common parameters only when all ADCs */
  5006. /* of the common group are disabled. */
  5007. if ((ADC_IS_ENABLE(hadc) == RESET) &&
  5008. ( (tmphadcSharingSameCommonRegister.Instance == NULL) ||
  5009. (ADC_IS_ENABLE(&tmphadcSharingSameCommonRegister) == RESET) ) )
  5010. {
  5011. /* If Channel_16 is selected, enable Temp. sensor measurement path */
  5012. /* Note: Temp. sensor internal channels available on ADC1 only */
  5013. if ((sConfig->Channel == ADC_CHANNEL_TEMPSENSOR) && (hadc->Instance == ADC1))
  5014. {
  5015. SET_BIT(tmpADC_Common->CCR, ADC_CCR_TSEN);
  5016. /* Delay for temperature sensor stabilization time */
  5017. /* Compute number of CPU cycles to wait for */
  5018. wait_loop_index = (ADC_TEMPSENSOR_DELAY_US * (SystemCoreClock / 1000000U));
  5019. while(wait_loop_index != 0U)
  5020. {
  5021. wait_loop_index--;
  5022. }
  5023. }
  5024. /* If Channel_17 is selected, enable VBAT measurement path */
  5025. /* Note: VBAT internal channels available on ADC1 only */
  5026. else if ((sConfig->Channel == ADC_CHANNEL_VBAT) && (hadc->Instance == ADC1))
  5027. {
  5028. SET_BIT(tmpADC_Common->CCR, ADC_CCR_VBATEN);
  5029. }
  5030. /* If Channel_18 is selected, enable VREFINT measurement path */
  5031. /* Note: VrefInt internal channels available on all ADCs, but only */
  5032. /* one ADC is allowed to be connected to VrefInt at the same */
  5033. /* time. */
  5034. else if (sConfig->Channel == ADC_CHANNEL_VREFINT)
  5035. {
  5036. SET_BIT(tmpADC_Common->CCR, ADC_CCR_VREFEN);
  5037. }
  5038. }
  5039. /* If the requested internal measurement path has already been */
  5040. /* enabled and other ADC of the common group are enabled, internal */
  5041. /* measurement paths cannot be enabled. */
  5042. else
  5043. {
  5044. /* Update ADC state machine to error */
  5045. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  5046. tmp_hal_status = HAL_ERROR;
  5047. }
  5048. }
  5049. }
  5050. }
  5051. /* If a conversion is on going on regular group, no update on regular */
  5052. /* channel could be done on neither of the channel configuration structure */
  5053. /* parameters. */
  5054. else
  5055. {
  5056. /* Update ADC state machine to error */
  5057. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  5058. tmp_hal_status = HAL_ERROR;
  5059. }
  5060. /* Process unlocked */
  5061. __HAL_UNLOCK(hadc);
  5062. /* Return function status */
  5063. return tmp_hal_status;
  5064. }
  5065. #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
  5066. /* STM32F302xC || STM32F303xC || STM32F358xx || */
  5067. /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
  5068. /* STM32F301x8 || STM32F302x8 || STM32F318xx */
  5069. #if defined(STM32F373xC) || defined(STM32F378xx)
  5070. /**
  5071. * @brief Configures the the selected channel to be linked to the regular
  5072. * group.
  5073. * @note In case of usage of internal measurement channels:
  5074. * Vbat/VrefInt/TempSensor.
  5075. * The recommended sampling time is at least:
  5076. * - For devices STM32F37x: 17.1us for temperature sensor
  5077. * - For the other STM32F3 devices: 2.2us for each of channels
  5078. * Vbat/VrefInt/TempSensor.
  5079. * These internal paths can be be disabled using function
  5080. * HAL_ADC_DeInit().
  5081. * @note Possibility to update parameters on the fly:
  5082. * This function initializes channel into regular group, following
  5083. * calls to this function can be used to reconfigure some parameters
  5084. * of structure "ADC_ChannelConfTypeDef" on the fly, without resetting
  5085. * the ADC.
  5086. * The setting of these parameters is conditioned to ADC state.
  5087. * For parameters constraints, see comments of structure
  5088. * "ADC_ChannelConfTypeDef".
  5089. * @param hadc ADC handle
  5090. * @param sConfig Structure of ADC channel for regular group.
  5091. * @retval HAL status
  5092. */
  5093. HAL_StatusTypeDef HAL_ADC_ConfigChannel(ADC_HandleTypeDef* hadc, ADC_ChannelConfTypeDef* sConfig)
  5094. {
  5095. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  5096. __IO uint32_t wait_loop_index = 0U;
  5097. /* Check the parameters */
  5098. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  5099. assert_param(IS_ADC_CHANNEL(sConfig->Channel));
  5100. assert_param(IS_ADC_REGULAR_RANK(sConfig->Rank));
  5101. assert_param(IS_ADC_SAMPLE_TIME(sConfig->SamplingTime));
  5102. /* Process locked */
  5103. __HAL_LOCK(hadc);
  5104. /* Regular sequence configuration */
  5105. /* For Rank 1 to 6U */
  5106. if (sConfig->Rank < 7U)
  5107. {
  5108. MODIFY_REG(hadc->Instance->SQR3 ,
  5109. ADC_SQR3_RK(ADC_SQR3_SQ1, sConfig->Rank) ,
  5110. ADC_SQR3_RK(sConfig->Channel, sConfig->Rank) );
  5111. }
  5112. /* For Rank 7 to 12U */
  5113. else if (sConfig->Rank < 13U)
  5114. {
  5115. MODIFY_REG(hadc->Instance->SQR2 ,
  5116. ADC_SQR2_RK(ADC_SQR2_SQ7, sConfig->Rank) ,
  5117. ADC_SQR2_RK(sConfig->Channel, sConfig->Rank) );
  5118. }
  5119. /* For Rank 13 to 16U */
  5120. else
  5121. {
  5122. MODIFY_REG(hadc->Instance->SQR1 ,
  5123. ADC_SQR1_RK(ADC_SQR1_SQ13, sConfig->Rank) ,
  5124. ADC_SQR1_RK(sConfig->Channel, sConfig->Rank) );
  5125. }
  5126. /* Channel sampling time configuration */
  5127. /* For channels 10 to 18U */
  5128. if (sConfig->Channel > ADC_CHANNEL_10)
  5129. {
  5130. MODIFY_REG(hadc->Instance->SMPR1 ,
  5131. ADC_SMPR1(ADC_SMPR1_SMP10, sConfig->Channel) ,
  5132. ADC_SMPR1(sConfig->SamplingTime, sConfig->Channel) );
  5133. }
  5134. else /* For channels 0 to 9U */
  5135. {
  5136. MODIFY_REG(hadc->Instance->SMPR2 ,
  5137. ADC_SMPR2(ADC_SMPR2_SMP0, sConfig->Channel) ,
  5138. ADC_SMPR2(sConfig->SamplingTime, sConfig->Channel) );
  5139. }
  5140. /* If ADC1 Channel_16 or Channel_17 is selected, enable Temperature sensor */
  5141. /* and VREFINT measurement path. */
  5142. if ((sConfig->Channel == ADC_CHANNEL_TEMPSENSOR) ||
  5143. (sConfig->Channel == ADC_CHANNEL_VREFINT) )
  5144. {
  5145. SET_BIT(hadc->Instance->CR2, ADC_CR2_TSVREFE);
  5146. if ((sConfig->Channel == ADC_CHANNEL_TEMPSENSOR))
  5147. {
  5148. /* Delay for temperature sensor stabilization time */
  5149. /* Compute number of CPU cycles to wait for */
  5150. wait_loop_index = (ADC_TEMPSENSOR_DELAY_US * (SystemCoreClock / 1000000U));
  5151. while(wait_loop_index != 0U)
  5152. {
  5153. wait_loop_index--;
  5154. }
  5155. }
  5156. }
  5157. /* if ADC1 Channel_18 is selected, enable VBAT measurement path */
  5158. else if (sConfig->Channel == ADC_CHANNEL_VBAT)
  5159. {
  5160. SET_BIT(SYSCFG->CFGR1, SYSCFG_CFGR1_VBAT);
  5161. }
  5162. /* Process unlocked */
  5163. __HAL_UNLOCK(hadc);
  5164. /* Return function status */
  5165. return tmp_hal_status;
  5166. }
  5167. #endif /* STM32F373xC || STM32F378xx */
  5168. #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
  5169. defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
  5170. defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
  5171. defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
  5172. /**
  5173. * @brief Configures the ADC injected group and the selected channel to be
  5174. * linked to the injected group.
  5175. * @note Possibility to update parameters on the fly:
  5176. * This function initializes injected group, following calls to this
  5177. * function can be used to reconfigure some parameters of structure
  5178. * "ADC_InjectionConfTypeDef" on the fly, without resetting the ADC.
  5179. * The setting of these parameters is conditioned to ADC state.
  5180. * For parameters constraints, see comments of structure
  5181. * "ADC_InjectionConfTypeDef".
  5182. * @note In case of usage of internal measurement channels:
  5183. * Vbat/VrefInt/TempSensor.
  5184. * The recommended sampling time is at least:
  5185. * - For devices STM32F37x: 17.1us for temperature sensor
  5186. * - For the other STM32F3 devices: 2.2us for each of channels
  5187. * Vbat/VrefInt/TempSensor.
  5188. * These internal paths can be be disabled using function
  5189. * HAL_ADC_DeInit().
  5190. * @note To reset injected sequencer, function HAL_ADCEx_InjectedStop() can
  5191. * be used.
  5192. * @note Caution: For Injected Context Queue use: a context must be fully
  5193. * defined before start of injected conversion: all channels configured
  5194. * consecutively for the same ADC instance. Therefore, Number of calls of
  5195. * HAL_ADCEx_InjectedConfigChannel() must correspond to value of parameter
  5196. * InjectedNbrOfConversion for each context.
  5197. * - Example 1: If 1 context intended to be used (or not use of this feature:
  5198. * QueueInjectedContext=DISABLE) and usage of the 3 first injected ranks
  5199. * (InjectedNbrOfConversion=3), HAL_ADCEx_InjectedConfigChannel() must be
  5200. * called once for each channel (3 times) before launching a conversion.
  5201. * This function must not be called to configure the 4th injected channel:
  5202. * it would start a new context into context queue.
  5203. * - Example 2: If 2 contexts intended to be used and usage of the 3 first
  5204. * injected ranks (InjectedNbrOfConversion=3),
  5205. * HAL_ADCEx_InjectedConfigChannel() must be called once for each channel and
  5206. * for each context (3 channels x 2 contexts = 6 calls). Conversion can
  5207. * start once the 1st context is set. The 2nd context can be set on the fly.
  5208. * @param hadc ADC handle
  5209. * @param sConfigInjected Structure of ADC injected group and ADC channel for
  5210. * injected group.
  5211. * @retval None
  5212. */
  5213. HAL_StatusTypeDef HAL_ADCEx_InjectedConfigChannel(ADC_HandleTypeDef* hadc, ADC_InjectionConfTypeDef* sConfigInjected)
  5214. {
  5215. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  5216. ADC_Common_TypeDef *tmpADC_Common;
  5217. ADC_HandleTypeDef tmphadcSharingSameCommonRegister;
  5218. uint32_t tmpOffsetShifted;
  5219. __IO uint32_t wait_loop_index = 0U;
  5220. /* Injected context queue feature: temporary JSQR variables defined in */
  5221. /* static to be passed over calls of this function */
  5222. uint32_t tmp_JSQR_ContextQueueBeingBuilt = 0U;
  5223. /* Check the parameters */
  5224. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  5225. assert_param(IS_ADC_SAMPLE_TIME(sConfigInjected->InjectedSamplingTime));
  5226. assert_param(IS_ADC_SINGLE_DIFFERENTIAL(sConfigInjected->InjectedSingleDiff));
  5227. assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->AutoInjectedConv));
  5228. assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->QueueInjectedContext));
  5229. assert_param(IS_ADC_EXTTRIGINJEC_EDGE(sConfigInjected->ExternalTrigInjecConvEdge));
  5230. assert_param(IS_ADC_EXTTRIGINJEC(sConfigInjected->ExternalTrigInjecConv));
  5231. assert_param(IS_ADC_OFFSET_NUMBER(sConfigInjected->InjectedOffsetNumber));
  5232. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), sConfigInjected->InjectedOffset));
  5233. if(hadc->Init.ScanConvMode != ADC_SCAN_DISABLE)
  5234. {
  5235. assert_param(IS_ADC_INJECTED_RANK(sConfigInjected->InjectedRank));
  5236. assert_param(IS_ADC_INJECTED_NB_CONV(sConfigInjected->InjectedNbrOfConversion));
  5237. assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->InjectedDiscontinuousConvMode));
  5238. }
  5239. /* Verification of channel number: Channels 1 to 14 are available in */
  5240. /* differential mode. Channels 15U, 16U, 17U, 18 can be used only in */
  5241. /* single-ended mode. */
  5242. if (sConfigInjected->InjectedSingleDiff != ADC_DIFFERENTIAL_ENDED)
  5243. {
  5244. assert_param(IS_ADC_CHANNEL(sConfigInjected->InjectedChannel));
  5245. }
  5246. else
  5247. {
  5248. assert_param(IS_ADC_DIFF_CHANNEL(sConfigInjected->InjectedChannel));
  5249. }
  5250. /* Process locked */
  5251. __HAL_LOCK(hadc);
  5252. /* Configuration of Injected group sequencer. */
  5253. /* Hardware constraint: Must fully define injected context register JSQR */
  5254. /* before make it entering into injected sequencer queue. */
  5255. /* */
  5256. /* - if scan mode is disabled: */
  5257. /* * Injected channels sequence length is set to 0x00: 1 channel */
  5258. /* converted (channel on injected rank 1U) */
  5259. /* Parameter "InjectedNbrOfConversion" is discarded. */
  5260. /* * Injected context register JSQR setting is simple: register is fully */
  5261. /* defined on one call of this function (for injected rank 1U) and can */
  5262. /* be entered into queue directly. */
  5263. /* - if scan mode is enabled: */
  5264. /* * Injected channels sequence length is set to parameter */
  5265. /* "InjectedNbrOfConversion". */
  5266. /* * Injected context register JSQR setting more complex: register is */
  5267. /* fully defined over successive calls of this function, for each */
  5268. /* injected channel rank. It is entered into queue only when all */
  5269. /* injected ranks have been set. */
  5270. /* Note: Scan mode is not present by hardware on this device, but used */
  5271. /* by software for alignment over all STM32 devices. */
  5272. if ((hadc->Init.ScanConvMode == ADC_SCAN_DISABLE) ||
  5273. (sConfigInjected->InjectedNbrOfConversion == 1U) )
  5274. {
  5275. /* Configuration of context register JSQR: */
  5276. /* - number of ranks in injected group sequencer: fixed to 1st rank */
  5277. /* (scan mode disabled, only rank 1 used) */
  5278. /* - external trigger to start conversion */
  5279. /* - external trigger polarity */
  5280. /* - channel set to rank 1 (scan mode disabled, only rank 1 used) */
  5281. if (sConfigInjected->InjectedRank == ADC_INJECTED_RANK_1)
  5282. {
  5283. /* Enable external trigger if trigger selection is different of */
  5284. /* software start. */
  5285. /* Note: This configuration keeps the hardware feature of parameter */
  5286. /* ExternalTrigInjecConvEdge "trigger edge none" equivalent to */
  5287. /* software start. */
  5288. if (sConfigInjected->ExternalTrigInjecConv != ADC_INJECTED_SOFTWARE_START)
  5289. {
  5290. SET_BIT(tmp_JSQR_ContextQueueBeingBuilt, ADC_JSQR_RK(sConfigInjected->InjectedChannel, ADC_INJECTED_RANK_1) |
  5291. ADC_JSQR_JEXTSEL_SET(hadc, sConfigInjected->ExternalTrigInjecConv) |
  5292. sConfigInjected->ExternalTrigInjecConvEdge );
  5293. }
  5294. else
  5295. {
  5296. SET_BIT(tmp_JSQR_ContextQueueBeingBuilt, ADC_JSQR_RK(sConfigInjected->InjectedChannel, ADC_INJECTED_RANK_1) );
  5297. }
  5298. /* Update ADC register JSQR */
  5299. MODIFY_REG(hadc->Instance->JSQR ,
  5300. ADC_JSQR_JSQ4 |
  5301. ADC_JSQR_JSQ3 |
  5302. ADC_JSQR_JSQ2 |
  5303. ADC_JSQR_JSQ1 |
  5304. ADC_JSQR_JEXTEN |
  5305. ADC_JSQR_JEXTSEL |
  5306. ADC_JSQR_JL ,
  5307. tmp_JSQR_ContextQueueBeingBuilt );
  5308. /* For debug and informative reasons, hadc handle saves JSQR setting */
  5309. hadc->InjectionConfig.ContextQueue = tmp_JSQR_ContextQueueBeingBuilt;
  5310. }
  5311. /* If another injected rank than rank1 was intended to be set, and could */
  5312. /* not due to ScanConvMode disabled, error is reported. */
  5313. else
  5314. {
  5315. /* Update ADC state machine to error */
  5316. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  5317. tmp_hal_status = HAL_ERROR;
  5318. }
  5319. }
  5320. else
  5321. {
  5322. /* Case of scan mode enabled, several channels to set into injected group */
  5323. /* sequencer. */
  5324. /* Procedure to define injected context register JSQR over successive */
  5325. /* calls of this function, for each injected channel rank: */
  5326. /* 1. Start new context and set parameters related to all injected */
  5327. /* channels: injected sequence length and trigger */
  5328. if (hadc->InjectionConfig.ChannelCount == 0U)
  5329. {
  5330. /* Initialize number of channels that will be configured on the context */
  5331. /* being built */
  5332. hadc->InjectionConfig.ChannelCount = sConfigInjected->InjectedNbrOfConversion;
  5333. /* Initialize value that will be set into register JSQR */
  5334. hadc->InjectionConfig.ContextQueue = 0x00000000U;
  5335. /* Configuration of context register JSQR: */
  5336. /* - number of ranks in injected group sequencer */
  5337. /* - external trigger to start conversion */
  5338. /* - external trigger polarity */
  5339. /* Enable external trigger if trigger selection is different of */
  5340. /* software start. */
  5341. /* Note: This configuration keeps the hardware feature of parameter */
  5342. /* ExternalTrigInjecConvEdge "trigger edge none" equivalent to */
  5343. /* software start. */
  5344. if (sConfigInjected->ExternalTrigInjecConv != ADC_INJECTED_SOFTWARE_START)
  5345. {
  5346. SET_BIT(hadc->InjectionConfig.ContextQueue, (sConfigInjected->InjectedNbrOfConversion - 1U) |
  5347. ADC_JSQR_JEXTSEL_SET(hadc, sConfigInjected->ExternalTrigInjecConv) |
  5348. sConfigInjected->ExternalTrigInjecConvEdge );
  5349. }
  5350. else
  5351. {
  5352. SET_BIT(hadc->InjectionConfig.ContextQueue, (sConfigInjected->InjectedNbrOfConversion - 1U) );
  5353. }
  5354. }
  5355. /* 2. Continue setting of context under definition with parameter */
  5356. /* related to each channel: channel rank sequence */
  5357. /* Set the JSQx bits for the selected rank */
  5358. MODIFY_REG(hadc->InjectionConfig.ContextQueue ,
  5359. ADC_JSQR_RK(ADC_SQR3_SQ10, sConfigInjected->InjectedRank) ,
  5360. ADC_JSQR_RK(sConfigInjected->InjectedChannel, sConfigInjected->InjectedRank) );
  5361. /* Decrease channel count after setting into temporary JSQR variable */
  5362. hadc->InjectionConfig.ChannelCount --;
  5363. /* 3. End of context setting: If last channel set, then write context */
  5364. /* into register JSQR and make it enter into queue */
  5365. if (hadc->InjectionConfig.ChannelCount == 0U)
  5366. {
  5367. /* Update ADC register JSQR */
  5368. MODIFY_REG(hadc->Instance->JSQR ,
  5369. ADC_JSQR_JSQ4 |
  5370. ADC_JSQR_JSQ3 |
  5371. ADC_JSQR_JSQ2 |
  5372. ADC_JSQR_JSQ1 |
  5373. ADC_JSQR_JEXTEN |
  5374. ADC_JSQR_JEXTSEL |
  5375. ADC_JSQR_JL ,
  5376. hadc->InjectionConfig.ContextQueue );
  5377. }
  5378. }
  5379. /* Parameters update conditioned to ADC state: */
  5380. /* Parameters that can be updated when ADC is disabled or enabled without */
  5381. /* conversion on going on injected group: */
  5382. /* - Injected context queue: Queue disable (active context is kept) or */
  5383. /* enable (context decremented, up to 2 contexts queued) */
  5384. /* - Injected discontinuous mode: can be enabled only if auto-injected */
  5385. /* mode is disabled. */
  5386. if (ADC_IS_CONVERSION_ONGOING_INJECTED(hadc) == RESET)
  5387. {
  5388. /* If auto-injected mode is disabled: no constraint */
  5389. if (sConfigInjected->AutoInjectedConv == DISABLE)
  5390. {
  5391. MODIFY_REG(hadc->Instance->CFGR ,
  5392. ADC_CFGR_JQM |
  5393. ADC_CFGR_JDISCEN ,
  5394. ADC_CFGR_INJECT_CONTEXT_QUEUE((uint32_t)sConfigInjected->QueueInjectedContext) |
  5395. ADC_CFGR_INJECT_DISCCONTINUOUS((uint32_t)sConfigInjected->InjectedDiscontinuousConvMode) );
  5396. }
  5397. /* If auto-injected mode is enabled: Injected discontinuous setting is */
  5398. /* discarded. */
  5399. else
  5400. {
  5401. MODIFY_REG(hadc->Instance->CFGR ,
  5402. ADC_CFGR_JQM |
  5403. ADC_CFGR_JDISCEN ,
  5404. ADC_CFGR_INJECT_CONTEXT_QUEUE((uint32_t)sConfigInjected->QueueInjectedContext) );
  5405. /* If injected discontinuous mode was intended to be set and could not */
  5406. /* due to auto-injected enabled, error is reported. */
  5407. if (sConfigInjected->InjectedDiscontinuousConvMode == ENABLE)
  5408. {
  5409. /* Update ADC state machine to error */
  5410. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  5411. tmp_hal_status = HAL_ERROR;
  5412. }
  5413. }
  5414. }
  5415. /* Parameters update conditioned to ADC state: */
  5416. /* Parameters that can be updated when ADC is disabled or enabled without */
  5417. /* conversion on going on regular and injected groups: */
  5418. /* - Automatic injected conversion: can be enabled if injected group */
  5419. /* external triggers are disabled. */
  5420. /* - Channel sampling time */
  5421. /* - Channel offset */
  5422. if (ADC_IS_CONVERSION_ONGOING_REGULAR_INJECTED(hadc) == RESET)
  5423. {
  5424. /* If injected group external triggers are disabled (set to injected */
  5425. /* software start): no constraint */
  5426. if (sConfigInjected->ExternalTrigInjecConv == ADC_INJECTED_SOFTWARE_START)
  5427. {
  5428. MODIFY_REG(hadc->Instance->CFGR ,
  5429. ADC_CFGR_JAUTO ,
  5430. ADC_CFGR_INJECT_AUTO_CONVERSION((uint32_t)sConfigInjected->AutoInjectedConv) );
  5431. }
  5432. /* If Automatic injected conversion was intended to be set and could not */
  5433. /* due to injected group external triggers enabled, error is reported. */
  5434. else
  5435. {
  5436. /* Disable Automatic injected conversion */
  5437. CLEAR_BIT(hadc->Instance->CFGR, ADC_CFGR_JAUTO);
  5438. if (sConfigInjected->AutoInjectedConv == ENABLE)
  5439. {
  5440. /* Update ADC state machine to error */
  5441. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  5442. tmp_hal_status = HAL_ERROR;
  5443. }
  5444. }
  5445. /* Channel sampling time configuration */
  5446. /* For channels 10 to 18U */
  5447. if (sConfigInjected->InjectedChannel >= ADC_CHANNEL_10)
  5448. {
  5449. MODIFY_REG(hadc->Instance->SMPR2 ,
  5450. ADC_SMPR2(ADC_SMPR2_SMP10, sConfigInjected->InjectedChannel) ,
  5451. ADC_SMPR2(sConfigInjected->InjectedSamplingTime, sConfigInjected->InjectedChannel) );
  5452. }
  5453. else /* For channels 1 to 9U */
  5454. {
  5455. MODIFY_REG(hadc->Instance->SMPR1 ,
  5456. ADC_SMPR1(ADC_SMPR1_SMP0, sConfigInjected->InjectedChannel) ,
  5457. ADC_SMPR1(sConfigInjected->InjectedSamplingTime, sConfigInjected->InjectedChannel) );
  5458. }
  5459. /* Configure the offset: offset enable/disable, channel, offset value */
  5460. /* Shift the offset in function of the selected ADC resolution. */
  5461. /* Offset has to be left-aligned on bit 11U, the LSB (right bits) are set */
  5462. /* to 0. */
  5463. tmpOffsetShifted = ADC_OFFSET_SHIFT_RESOLUTION(hadc, sConfigInjected->InjectedOffset);
  5464. /* Configure the selected offset register: */
  5465. /* - Enable offset */
  5466. /* - Set channel number */
  5467. /* - Set offset value */
  5468. switch (sConfigInjected->InjectedOffsetNumber)
  5469. {
  5470. case ADC_OFFSET_1:
  5471. /* Configure offset register 1U */
  5472. MODIFY_REG(hadc->Instance->OFR1 ,
  5473. ADC_OFR1_OFFSET1_CH |
  5474. ADC_OFR1_OFFSET1 ,
  5475. ADC_OFR1_OFFSET1_EN |
  5476. ADC_OFR_CHANNEL(sConfigInjected->InjectedChannel) |
  5477. tmpOffsetShifted );
  5478. break;
  5479. case ADC_OFFSET_2:
  5480. /* Configure offset register 2U */
  5481. MODIFY_REG(hadc->Instance->OFR2 ,
  5482. ADC_OFR2_OFFSET2_CH |
  5483. ADC_OFR2_OFFSET2 ,
  5484. ADC_OFR2_OFFSET2_EN |
  5485. ADC_OFR_CHANNEL(sConfigInjected->InjectedChannel) |
  5486. tmpOffsetShifted );
  5487. break;
  5488. case ADC_OFFSET_3:
  5489. /* Configure offset register 3U */
  5490. MODIFY_REG(hadc->Instance->OFR3 ,
  5491. ADC_OFR3_OFFSET3_CH |
  5492. ADC_OFR3_OFFSET3 ,
  5493. ADC_OFR3_OFFSET3_EN |
  5494. ADC_OFR_CHANNEL(sConfigInjected->InjectedChannel) |
  5495. tmpOffsetShifted );
  5496. break;
  5497. case ADC_OFFSET_4:
  5498. /* Configure offset register 4U */
  5499. MODIFY_REG(hadc->Instance->OFR4 ,
  5500. ADC_OFR4_OFFSET4_CH |
  5501. ADC_OFR4_OFFSET4 ,
  5502. ADC_OFR4_OFFSET4_EN |
  5503. ADC_OFR_CHANNEL(sConfigInjected->InjectedChannel) |
  5504. tmpOffsetShifted );
  5505. break;
  5506. /* Case ADC_OFFSET_NONE */
  5507. default :
  5508. /* Scan OFR1, OFR2, OFR3, OFR4 to check if the selected channel is */
  5509. /* enabled. If this is the case, offset OFRx is disabled. */
  5510. if (((hadc->Instance->OFR1) & ADC_OFR1_OFFSET1_CH) == ADC_OFR_CHANNEL(sConfigInjected->InjectedChannel))
  5511. {
  5512. /* Disable offset OFR1*/
  5513. CLEAR_BIT(hadc->Instance->OFR1, ADC_OFR1_OFFSET1_EN);
  5514. }
  5515. if (((hadc->Instance->OFR2) & ADC_OFR2_OFFSET2_CH) == ADC_OFR_CHANNEL(sConfigInjected->InjectedChannel))
  5516. {
  5517. /* Disable offset OFR2*/
  5518. CLEAR_BIT(hadc->Instance->OFR2, ADC_OFR2_OFFSET2_EN);
  5519. }
  5520. if (((hadc->Instance->OFR3) & ADC_OFR3_OFFSET3_CH) == ADC_OFR_CHANNEL(sConfigInjected->InjectedChannel))
  5521. {
  5522. /* Disable offset OFR3*/
  5523. CLEAR_BIT(hadc->Instance->OFR3, ADC_OFR3_OFFSET3_EN);
  5524. }
  5525. if (((hadc->Instance->OFR4) & ADC_OFR4_OFFSET4_CH) == ADC_OFR_CHANNEL(sConfigInjected->InjectedChannel))
  5526. {
  5527. /* Disable offset OFR4*/
  5528. CLEAR_BIT(hadc->Instance->OFR4, ADC_OFR4_OFFSET4_EN);
  5529. }
  5530. break;
  5531. }
  5532. }
  5533. /* Parameters update conditioned to ADC state: */
  5534. /* Parameters that can be updated only when ADC is disabled: */
  5535. /* - Single or differential mode */
  5536. /* - Internal measurement channels: Vbat/VrefInt/TempSensor */
  5537. if (ADC_IS_ENABLE(hadc) == RESET)
  5538. {
  5539. /* Configuration of differential mode */
  5540. if (sConfigInjected->InjectedSingleDiff != ADC_DIFFERENTIAL_ENDED)
  5541. {
  5542. /* Disable differential mode (default mode: single-ended) */
  5543. CLEAR_BIT(hadc->Instance->DIFSEL, ADC_DIFSEL_CHANNEL(sConfigInjected->InjectedChannel));
  5544. }
  5545. else
  5546. {
  5547. /* Enable differential mode */
  5548. SET_BIT(hadc->Instance->DIFSEL, ADC_DIFSEL_CHANNEL(sConfigInjected->InjectedChannel));
  5549. /* Channel sampling time configuration (channel ADC_INx +1 */
  5550. /* corresponding to differential negative input). */
  5551. /* For channels 10 to 18U */
  5552. if (sConfigInjected->InjectedChannel >= ADC_CHANNEL_10)
  5553. {
  5554. MODIFY_REG(hadc->Instance->SMPR2,
  5555. ADC_SMPR2(ADC_SMPR2_SMP10, sConfigInjected->InjectedChannel +1U),
  5556. ADC_SMPR2(sConfigInjected->InjectedSamplingTime, sConfigInjected->InjectedChannel +1U) );
  5557. }
  5558. else /* For channels 1 to 9U */
  5559. {
  5560. MODIFY_REG(hadc->Instance->SMPR1,
  5561. ADC_SMPR1(ADC_SMPR1_SMP0, sConfigInjected->InjectedChannel +1U),
  5562. ADC_SMPR1(sConfigInjected->InjectedSamplingTime, sConfigInjected->InjectedChannel +1U) );
  5563. }
  5564. }
  5565. /* Management of internal measurement channels: VrefInt/TempSensor/Vbat */
  5566. /* internal measurement paths enable: If internal channel selected, */
  5567. /* enable dedicated internal buffers and path. */
  5568. /* Note: these internal measurement paths can be disabled using */
  5569. /* HAL_ADC_deInit(). */
  5570. /* Configuration of common ADC parameters */
  5571. /* Pointer to the common control register to which is belonging hadc */
  5572. /* (Depending on STM32F3 product, there may be up to 4 ADC and 2 common */
  5573. /* control registers) */
  5574. tmpADC_Common = ADC_COMMON_REGISTER(hadc);
  5575. /* If the requested internal measurement path has already been enabled, */
  5576. /* bypass the configuration processing. */
  5577. if (( (sConfigInjected->InjectedChannel == ADC_CHANNEL_TEMPSENSOR) &&
  5578. (HAL_IS_BIT_CLR(tmpADC_Common->CCR, ADC_CCR_TSEN)) ) ||
  5579. ( (sConfigInjected->InjectedChannel == ADC_CHANNEL_VBAT) &&
  5580. (HAL_IS_BIT_CLR(tmpADC_Common->CCR, ADC_CCR_VBATEN)) ) ||
  5581. ( (sConfigInjected->InjectedChannel == ADC_CHANNEL_VREFINT) &&
  5582. (HAL_IS_BIT_CLR(tmpADC_Common->CCR, ADC_CCR_VREFEN)))
  5583. )
  5584. {
  5585. /* Configuration of common ADC parameters (continuation) */
  5586. /* Set handle of the other ADC sharing the same common register */
  5587. ADC_COMMON_ADC_OTHER(hadc, &tmphadcSharingSameCommonRegister);
  5588. /* Software is allowed to change common parameters only when all ADCs */
  5589. /* of the common group are disabled. */
  5590. if ((ADC_IS_ENABLE(hadc) == RESET) &&
  5591. ( (tmphadcSharingSameCommonRegister.Instance == NULL) ||
  5592. (ADC_IS_ENABLE(&tmphadcSharingSameCommonRegister) == RESET) ) )
  5593. {
  5594. /* If Channel_16 is selected, enable Temp. sensor measurement path */
  5595. /* Note: Temp. sensor internal channels available on ADC1 only */
  5596. if ((sConfigInjected->InjectedChannel == ADC_CHANNEL_TEMPSENSOR) && (hadc->Instance == ADC1))
  5597. {
  5598. SET_BIT(tmpADC_Common->CCR, ADC_CCR_TSEN);
  5599. /* Delay for temperature sensor stabilization time */
  5600. /* Compute number of CPU cycles to wait for */
  5601. wait_loop_index = (ADC_TEMPSENSOR_DELAY_US * (SystemCoreClock / 1000000U));
  5602. while(wait_loop_index != 0U)
  5603. {
  5604. wait_loop_index--;
  5605. }
  5606. }
  5607. /* If Channel_17 is selected, enable VBAT measurement path */
  5608. /* Note: VBAT internal channels available on ADC1 only */
  5609. else if ((sConfigInjected->InjectedChannel == ADC_CHANNEL_VBAT) && (hadc->Instance == ADC1))
  5610. {
  5611. SET_BIT(tmpADC_Common->CCR, ADC_CCR_VBATEN);
  5612. }
  5613. /* If Channel_18 is selected, enable VREFINT measurement path */
  5614. /* Note: VrefInt internal channels available on all ADCs, but only */
  5615. /* one ADC is allowed to be connected to VrefInt at the same */
  5616. /* time. */
  5617. else if (sConfigInjected->InjectedChannel == ADC_CHANNEL_VREFINT)
  5618. {
  5619. SET_BIT(tmpADC_Common->CCR, ADC_CCR_VREFEN);
  5620. }
  5621. }
  5622. /* If the requested internal measurement path has already been enabled */
  5623. /* and other ADC of the common group are enabled, internal */
  5624. /* measurement paths cannot be enabled. */
  5625. else
  5626. {
  5627. /* Update ADC state machine to error */
  5628. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  5629. tmp_hal_status = HAL_ERROR;
  5630. }
  5631. }
  5632. }
  5633. /* Process unlocked */
  5634. __HAL_UNLOCK(hadc);
  5635. /* Return function status */
  5636. return tmp_hal_status;
  5637. }
  5638. #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
  5639. /* STM32F302xC || STM32F303xC || STM32F358xx || */
  5640. /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
  5641. /* STM32F301x8 || STM32F302x8 || STM32F318xx */
  5642. #if defined(STM32F373xC) || defined(STM32F378xx)
  5643. /**
  5644. * @brief Configures the ADC injected group and the selected channel to be
  5645. * linked to the injected group.
  5646. * @note Possibility to update parameters on the fly:
  5647. * This function initializes injected group, following calls to this
  5648. * function can be used to reconfigure some parameters of structure
  5649. * "ADC_InjectionConfTypeDef" on the fly, without resetting the ADC.
  5650. * The setting of these parameters is conditioned to ADC state:
  5651. * this function must be called when ADC is not under conversion.
  5652. * @note In case of usage of internal measurement channels:
  5653. * Vbat/VrefInt/TempSensor.
  5654. * The recommended sampling time is at least:
  5655. * - For devices STM32F37x: 17.1us for temperature sensor
  5656. * - For the other STM32F3 devices: 2.2us for each of channels
  5657. * Vbat/VrefInt/TempSensor.
  5658. * These internal paths can be be disabled using function
  5659. * HAL_ADC_DeInit().
  5660. * @param hadc ADC handle
  5661. * @param sConfigInjected Structure of ADC injected group and ADC channel for
  5662. * injected group.
  5663. * @retval None
  5664. */
  5665. HAL_StatusTypeDef HAL_ADCEx_InjectedConfigChannel(ADC_HandleTypeDef* hadc, ADC_InjectionConfTypeDef* sConfigInjected)
  5666. {
  5667. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  5668. __IO uint32_t wait_loop_index = 0U;
  5669. /* Check the parameters */
  5670. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  5671. assert_param(IS_ADC_CHANNEL(sConfigInjected->InjectedChannel));
  5672. assert_param(IS_ADC_SAMPLE_TIME(sConfigInjected->InjectedSamplingTime));
  5673. assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->AutoInjectedConv));
  5674. assert_param(IS_ADC_EXTTRIGINJEC(sConfigInjected->ExternalTrigInjecConv));
  5675. assert_param(IS_ADC_RANGE(sConfigInjected->InjectedOffset));
  5676. if(hadc->Init.ScanConvMode != ADC_SCAN_DISABLE)
  5677. {
  5678. assert_param(IS_ADC_INJECTED_RANK(sConfigInjected->InjectedRank));
  5679. assert_param(IS_ADC_INJECTED_NB_CONV(sConfigInjected->InjectedNbrOfConversion));
  5680. assert_param(IS_FUNCTIONAL_STATE(sConfigInjected->InjectedDiscontinuousConvMode));
  5681. }
  5682. /* Process locked */
  5683. __HAL_LOCK(hadc);
  5684. /* Configuration of injected group sequencer: */
  5685. /* - if scan mode is disabled, injected channels sequence length is set to */
  5686. /* 0x00: 1 channel converted (channel on regular rank 1U) */
  5687. /* Parameter "InjectedNbrOfConversion" is discarded. */
  5688. /* Note: Scan mode is present by hardware on this device and, if */
  5689. /* disabled, discards automatically nb of conversions. Anyway, nb of */
  5690. /* conversions is forced to 0x00 for alignment over all STM32 devices. */
  5691. /* - if scan mode is enabled, injected channels sequence length is set to */
  5692. /* parameter "InjectedNbrOfConversion". */
  5693. if (hadc->Init.ScanConvMode == ADC_SCAN_DISABLE)
  5694. {
  5695. if (sConfigInjected->InjectedRank == ADC_INJECTED_RANK_1)
  5696. {
  5697. /* Clear the old SQx bits for all injected ranks */
  5698. MODIFY_REG(hadc->Instance->JSQR ,
  5699. ADC_JSQR_JL |
  5700. ADC_JSQR_JSQ4 |
  5701. ADC_JSQR_JSQ3 |
  5702. ADC_JSQR_JSQ2 |
  5703. ADC_JSQR_JSQ1 ,
  5704. ADC_JSQR_RK_JL(sConfigInjected->InjectedChannel,
  5705. ADC_INJECTED_RANK_1,
  5706. 0x01U) );
  5707. }
  5708. /* If another injected rank than rank1 was intended to be set, and could */
  5709. /* not due to ScanConvMode disabled, error is reported. */
  5710. else
  5711. {
  5712. /* Update ADC state machine to error */
  5713. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  5714. tmp_hal_status = HAL_ERROR;
  5715. }
  5716. }
  5717. else
  5718. {
  5719. /* Since injected channels rank conv. order depends on total number of */
  5720. /* injected conversions, selected rank must be below or equal to total */
  5721. /* number of injected conversions to be updated. */
  5722. if (sConfigInjected->InjectedRank <= sConfigInjected->InjectedNbrOfConversion)
  5723. {
  5724. /* Clear the old SQx bits for the selected rank */
  5725. /* Set the SQx bits for the selected rank */
  5726. MODIFY_REG(hadc->Instance->JSQR ,
  5727. ADC_JSQR_JL |
  5728. ADC_JSQR_RK_JL(ADC_JSQR_JSQ1,
  5729. sConfigInjected->InjectedRank,
  5730. sConfigInjected->InjectedNbrOfConversion) ,
  5731. ADC_JSQR_JL_SHIFT(sConfigInjected->InjectedNbrOfConversion) |
  5732. ADC_JSQR_RK_JL(sConfigInjected->InjectedChannel,
  5733. sConfigInjected->InjectedRank,
  5734. sConfigInjected->InjectedNbrOfConversion) );
  5735. }
  5736. else
  5737. {
  5738. /* Clear the old SQx bits for the selected rank */
  5739. MODIFY_REG(hadc->Instance->JSQR ,
  5740. ADC_JSQR_JL |
  5741. ADC_JSQR_RK_JL(ADC_JSQR_JSQ1,
  5742. sConfigInjected->InjectedRank,
  5743. sConfigInjected->InjectedNbrOfConversion) ,
  5744. 0x00000000 );
  5745. }
  5746. }
  5747. /* Configuration of injected group */
  5748. /* Parameters update conditioned to ADC state: */
  5749. /* Parameters that can be updated only when ADC is disabled: */
  5750. /* - external trigger to start conversion */
  5751. /* Parameters update not conditioned to ADC state: */
  5752. /* - Automatic injected conversion */
  5753. /* - Injected discontinuous mode */
  5754. /* Note: In case of ADC already enabled, caution to not launch an unwanted */
  5755. /* conversion while modifying register CR2 by writing 1 to bit ADON. */
  5756. if (ADC_IS_ENABLE(hadc) == RESET)
  5757. {
  5758. MODIFY_REG(hadc->Instance->CR2 ,
  5759. ADC_CR2_JEXTSEL |
  5760. ADC_CR2_ADON ,
  5761. sConfigInjected->ExternalTrigInjecConv );
  5762. }
  5763. /* Configuration of injected group */
  5764. /* - Automatic injected conversion */
  5765. /* - Injected discontinuous mode */
  5766. /* Automatic injected conversion can be enabled if injected group */
  5767. /* external triggers are disabled. */
  5768. if (sConfigInjected->AutoInjectedConv == ENABLE)
  5769. {
  5770. if (sConfigInjected->ExternalTrigInjecConv == ADC_INJECTED_SOFTWARE_START)
  5771. {
  5772. SET_BIT(hadc->Instance->CR1, ADC_CR1_JAUTO);
  5773. }
  5774. else
  5775. {
  5776. /* Update ADC state machine to error */
  5777. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  5778. tmp_hal_status = HAL_ERROR;
  5779. }
  5780. }
  5781. /* Injected discontinuous can be enabled only if auto-injected mode is */
  5782. /* disabled. */
  5783. if (sConfigInjected->InjectedDiscontinuousConvMode == ENABLE)
  5784. {
  5785. if (sConfigInjected->AutoInjectedConv == DISABLE)
  5786. {
  5787. SET_BIT(hadc->Instance->CR1, ADC_CR1_JDISCEN);
  5788. }
  5789. else
  5790. {
  5791. /* Update ADC state machine to error */
  5792. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  5793. tmp_hal_status = HAL_ERROR;
  5794. }
  5795. }
  5796. /* InjectedChannel sampling time configuration */
  5797. /* For channels 10 to 18 */
  5798. if (sConfigInjected->InjectedChannel > ADC_CHANNEL_10)
  5799. {
  5800. MODIFY_REG(hadc->Instance->SMPR1,
  5801. ADC_SMPR1(ADC_SMPR1_SMP10, sConfigInjected->InjectedChannel),
  5802. ADC_SMPR1(sConfigInjected->InjectedSamplingTime, sConfigInjected->InjectedChannel) );
  5803. }
  5804. else /* For channels 1 to 9 */
  5805. {
  5806. MODIFY_REG(hadc->Instance->SMPR2,
  5807. ADC_SMPR2(ADC_SMPR2_SMP0, sConfigInjected->InjectedChannel),
  5808. ADC_SMPR2(sConfigInjected->InjectedSamplingTime, sConfigInjected->InjectedChannel) );
  5809. }
  5810. /* Configure the offset: offset enable/disable, InjectedChannel, offset value */
  5811. switch(sConfigInjected->InjectedRank)
  5812. {
  5813. case 1:
  5814. /* Set injected channel 1 offset */
  5815. MODIFY_REG(hadc->Instance->JOFR1,
  5816. ADC_JOFR1_JOFFSET1,
  5817. sConfigInjected->InjectedOffset);
  5818. break;
  5819. case 2:
  5820. /* Set injected channel 2 offset */
  5821. MODIFY_REG(hadc->Instance->JOFR2,
  5822. ADC_JOFR2_JOFFSET2,
  5823. sConfigInjected->InjectedOffset);
  5824. break;
  5825. case 3:
  5826. /* Set injected channel 3 offset */
  5827. MODIFY_REG(hadc->Instance->JOFR3,
  5828. ADC_JOFR3_JOFFSET3,
  5829. sConfigInjected->InjectedOffset);
  5830. break;
  5831. case 4:
  5832. default:
  5833. MODIFY_REG(hadc->Instance->JOFR4,
  5834. ADC_JOFR4_JOFFSET4,
  5835. sConfigInjected->InjectedOffset);
  5836. break;
  5837. }
  5838. /* If ADC1 Channel_16 or Channel_17 is selected, enable Temperature sensor */
  5839. /* and VREFINT measurement path. */
  5840. if ((sConfigInjected->InjectedChannel == ADC_CHANNEL_TEMPSENSOR) ||
  5841. (sConfigInjected->InjectedChannel == ADC_CHANNEL_VREFINT) )
  5842. {
  5843. if (READ_BIT(hadc->Instance->CR2, ADC_CR2_TSVREFE) == RESET)
  5844. {
  5845. SET_BIT(hadc->Instance->CR2, ADC_CR2_TSVREFE);
  5846. if ((sConfigInjected->InjectedChannel == ADC_CHANNEL_TEMPSENSOR))
  5847. {
  5848. /* Delay for temperature sensor stabilization time */
  5849. /* Compute number of CPU cycles to wait for */
  5850. wait_loop_index = (ADC_TEMPSENSOR_DELAY_US * (SystemCoreClock / 1000000U));
  5851. while(wait_loop_index != 0U)
  5852. {
  5853. wait_loop_index--;
  5854. }
  5855. }
  5856. }
  5857. }
  5858. /* if ADC1 Channel_18 is selected, enable VBAT measurement path */
  5859. else if (sConfigInjected->InjectedChannel == ADC_CHANNEL_VBAT)
  5860. {
  5861. SET_BIT(SYSCFG->CFGR1, SYSCFG_CFGR1_VBAT);
  5862. }
  5863. /* Process unlocked */
  5864. __HAL_UNLOCK(hadc);
  5865. /* Return function status */
  5866. return tmp_hal_status;
  5867. }
  5868. #endif /* STM32F373xC || STM32F378xx */
  5869. #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
  5870. defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
  5871. defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
  5872. defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
  5873. /**
  5874. * @brief Configures the analog watchdog.
  5875. * @note Possibility to update parameters on the fly:
  5876. * This function initializes the selected analog watchdog, following
  5877. * calls to this function can be used to reconfigure some parameters
  5878. * of structure "ADC_AnalogWDGConfTypeDef" on the fly, without resetting
  5879. * the ADC.
  5880. * The setting of these parameters is conditioned to ADC state.
  5881. * For parameters constraints, see comments of structure
  5882. * "ADC_AnalogWDGConfTypeDef".
  5883. * @param hadc ADC handle
  5884. * @param AnalogWDGConfig Structure of ADC analog watchdog configuration
  5885. * @retval HAL status
  5886. */
  5887. HAL_StatusTypeDef HAL_ADC_AnalogWDGConfig(ADC_HandleTypeDef* hadc, ADC_AnalogWDGConfTypeDef* AnalogWDGConfig)
  5888. {
  5889. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  5890. uint32_t tmpAWDHighThresholdShifted;
  5891. uint32_t tmpAWDLowThresholdShifted;
  5892. uint32_t tmpADCFlagAWD2orAWD3;
  5893. uint32_t tmpADCITAWD2orAWD3;
  5894. /* Check the parameters */
  5895. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  5896. assert_param(IS_ADC_ANALOG_WATCHDOG_NUMBER(AnalogWDGConfig->WatchdogNumber));
  5897. assert_param(IS_ADC_ANALOG_WATCHDOG_MODE(AnalogWDGConfig->WatchdogMode));
  5898. assert_param(IS_FUNCTIONAL_STATE(AnalogWDGConfig->ITMode));
  5899. /* Verify if threshold is within the selected ADC resolution */
  5900. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), AnalogWDGConfig->HighThreshold));
  5901. assert_param(IS_ADC_RANGE(ADC_GET_RESOLUTION(hadc), AnalogWDGConfig->LowThreshold));
  5902. if((AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_REG) ||
  5903. (AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_INJEC) ||
  5904. (AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_REGINJEC) )
  5905. {
  5906. assert_param(IS_ADC_CHANNEL(AnalogWDGConfig->Channel));
  5907. }
  5908. /* Process locked */
  5909. __HAL_LOCK(hadc);
  5910. /* Parameters update conditioned to ADC state: */
  5911. /* Parameters that can be updated when ADC is disabled or enabled without */
  5912. /* conversion on going on regular and injected groups: */
  5913. /* - Analog watchdog channels */
  5914. /* - Analog watchdog thresholds */
  5915. if (ADC_IS_CONVERSION_ONGOING_REGULAR_INJECTED(hadc) == RESET)
  5916. {
  5917. /* Analog watchdogs configuration */
  5918. if(AnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_1)
  5919. {
  5920. /* Configuration of analog watchdog: */
  5921. /* - Set the analog watchdog enable mode: regular and/or injected */
  5922. /* groups, one or overall group of channels. */
  5923. /* - Set the Analog watchdog channel (is not used if watchdog */
  5924. /* mode "all channels": ADC_CFGR_AWD1SGL=0U). */
  5925. MODIFY_REG(hadc->Instance->CFGR ,
  5926. ADC_CFGR_AWD1SGL |
  5927. ADC_CFGR_JAWD1EN |
  5928. ADC_CFGR_AWD1EN |
  5929. ADC_CFGR_AWD1CH ,
  5930. AnalogWDGConfig->WatchdogMode |
  5931. ADC_CFGR_AWD1CH_SHIFT(AnalogWDGConfig->Channel) );
  5932. /* Shift the offset in function of the selected ADC resolution: */
  5933. /* Thresholds have to be left-aligned on bit 11U, the LSB (right bits) */
  5934. /* are set to 0 */
  5935. tmpAWDHighThresholdShifted = ADC_AWD1THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->HighThreshold);
  5936. tmpAWDLowThresholdShifted = ADC_AWD1THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->LowThreshold);
  5937. /* Set the high and low thresholds */
  5938. MODIFY_REG(hadc->Instance->TR1 ,
  5939. ADC_TR1_HT1 |
  5940. ADC_TR1_LT1 ,
  5941. ADC_TRX_HIGHTHRESHOLD(tmpAWDHighThresholdShifted) |
  5942. tmpAWDLowThresholdShifted );
  5943. /* Clear the ADC Analog watchdog flag (in case of left enabled by */
  5944. /* previous ADC operations) to be ready to use for HAL_ADC_IRQHandler() */
  5945. /* or HAL_ADC_PollForEvent(). */
  5946. __HAL_ADC_CLEAR_FLAG(hadc, ADC_IT_AWD1);
  5947. /* Configure ADC Analog watchdog interrupt */
  5948. if(AnalogWDGConfig->ITMode == ENABLE)
  5949. {
  5950. /* Enable the ADC Analog watchdog interrupt */
  5951. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_AWD1);
  5952. }
  5953. else
  5954. {
  5955. /* Disable the ADC Analog watchdog interrupt */
  5956. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_AWD1);
  5957. }
  5958. }
  5959. /* Case of ADC_ANALOGWATCHDOG_2 and ADC_ANALOGWATCHDOG_3 */
  5960. else
  5961. {
  5962. /* Shift the threshold in function of the selected ADC resolution */
  5963. /* have to be left-aligned on bit 7U, the LSB (right bits) are set to 0 */
  5964. tmpAWDHighThresholdShifted = ADC_AWD23THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->HighThreshold);
  5965. tmpAWDLowThresholdShifted = ADC_AWD23THRESHOLD_SHIFT_RESOLUTION(hadc, AnalogWDGConfig->LowThreshold);
  5966. if (AnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_2)
  5967. {
  5968. /* Set the Analog watchdog channel or group of channels. This also */
  5969. /* enables the watchdog. */
  5970. /* Note: Conditional register reset, because several channels can be */
  5971. /* set by successive calls of this function. */
  5972. if (AnalogWDGConfig->WatchdogMode != ADC_ANALOGWATCHDOG_NONE)
  5973. {
  5974. /* Set the high and low thresholds */
  5975. MODIFY_REG(hadc->Instance->TR2 ,
  5976. ADC_TR2_HT2 |
  5977. ADC_TR2_LT2 ,
  5978. ADC_TRX_HIGHTHRESHOLD(tmpAWDHighThresholdShifted) |
  5979. tmpAWDLowThresholdShifted );
  5980. SET_BIT(hadc->Instance->AWD2CR, ADC_CFGR_AWD23CR(AnalogWDGConfig->Channel));
  5981. }
  5982. else
  5983. {
  5984. CLEAR_BIT(hadc->Instance->TR2, ADC_TR2_HT2 | ADC_TR2_LT2);
  5985. CLEAR_BIT(hadc->Instance->AWD2CR, ADC_AWD2CR_AWD2CH);
  5986. }
  5987. /* Set temporary variable to flag and IT of AWD2 or AWD3 for further */
  5988. /* settings. */
  5989. tmpADCFlagAWD2orAWD3 = ADC_FLAG_AWD2;
  5990. tmpADCITAWD2orAWD3 = ADC_IT_AWD2;
  5991. }
  5992. /* (AnalogWDGConfig->WatchdogNumber == ADC_ANALOGWATCHDOG_3) */
  5993. else
  5994. {
  5995. /* Set the Analog watchdog channel or group of channels. This also */
  5996. /* enables the watchdog. */
  5997. /* Note: Conditional register reset, because several channels can be */
  5998. /* set by successive calls of this function. */
  5999. if (AnalogWDGConfig->WatchdogMode != ADC_ANALOGWATCHDOG_NONE)
  6000. {
  6001. /* Set the high and low thresholds */
  6002. MODIFY_REG(hadc->Instance->TR3 ,
  6003. ADC_TR3_HT3 |
  6004. ADC_TR3_LT3 ,
  6005. ADC_TRX_HIGHTHRESHOLD(tmpAWDHighThresholdShifted) |
  6006. tmpAWDLowThresholdShifted );
  6007. SET_BIT(hadc->Instance->AWD3CR, ADC_CFGR_AWD23CR(AnalogWDGConfig->Channel));
  6008. }
  6009. else
  6010. {
  6011. CLEAR_BIT(hadc->Instance->TR3, ADC_TR3_HT3 | ADC_TR3_LT3);
  6012. CLEAR_BIT(hadc->Instance->AWD3CR, ADC_AWD3CR_AWD3CH);
  6013. }
  6014. /* Set temporary variable to flag and IT of AWD2 or AWD3 for further */
  6015. /* settings. */
  6016. tmpADCFlagAWD2orAWD3 = ADC_FLAG_AWD3;
  6017. tmpADCITAWD2orAWD3 = ADC_IT_AWD3;
  6018. }
  6019. /* Clear the ADC Analog watchdog flag (in case of left enabled by */
  6020. /* previous ADC operations) to be ready to use for HAL_ADC_IRQHandler() */
  6021. /* or HAL_ADC_PollForEvent(). */
  6022. __HAL_ADC_CLEAR_FLAG(hadc, tmpADCFlagAWD2orAWD3);
  6023. /* Configure ADC Analog watchdog interrupt */
  6024. if(AnalogWDGConfig->ITMode == ENABLE)
  6025. {
  6026. __HAL_ADC_ENABLE_IT(hadc, tmpADCITAWD2orAWD3);
  6027. }
  6028. else
  6029. {
  6030. __HAL_ADC_DISABLE_IT(hadc, tmpADCITAWD2orAWD3);
  6031. }
  6032. }
  6033. }
  6034. /* If a conversion is on going on regular or injected groups, no update */
  6035. /* could be done on neither of the AWD configuration structure parameters. */
  6036. else
  6037. {
  6038. /* Update ADC state machine to error */
  6039. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  6040. tmp_hal_status = HAL_ERROR;
  6041. }
  6042. /* Process unlocked */
  6043. __HAL_UNLOCK(hadc);
  6044. /* Return function status */
  6045. return tmp_hal_status;
  6046. }
  6047. #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
  6048. /* STM32F302xC || STM32F303xC || STM32F358xx || */
  6049. /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
  6050. /* STM32F301x8 || STM32F302x8 || STM32F318xx */
  6051. #if defined(STM32F373xC) || defined(STM32F378xx)
  6052. /**
  6053. * @brief Configures the analog watchdog.
  6054. * @note Analog watchdog thresholds can be modified while ADC conversion
  6055. * is on going.
  6056. * In this case, some constraints must be taken into account:
  6057. * the programmed threshold values are effective from the next
  6058. * ADC EOC (end of unitary conversion).
  6059. * Considering that registers write delay may happen due to
  6060. * bus activity, this might cause an uncertainty on the
  6061. * effective timing of the new programmed threshold values.
  6062. * @param hadc ADC handle
  6063. * @param AnalogWDGConfig Structure of ADC analog watchdog configuration
  6064. * @retval HAL status
  6065. */
  6066. HAL_StatusTypeDef HAL_ADC_AnalogWDGConfig(ADC_HandleTypeDef* hadc, ADC_AnalogWDGConfTypeDef* AnalogWDGConfig)
  6067. {
  6068. /* Check the parameters */
  6069. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  6070. assert_param(IS_ADC_ANALOG_WATCHDOG_MODE(AnalogWDGConfig->WatchdogMode));
  6071. assert_param(IS_FUNCTIONAL_STATE(AnalogWDGConfig->ITMode));
  6072. assert_param(IS_ADC_RANGE(AnalogWDGConfig->HighThreshold));
  6073. assert_param(IS_ADC_RANGE(AnalogWDGConfig->LowThreshold));
  6074. if((AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_REG) ||
  6075. (AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_INJEC) ||
  6076. (AnalogWDGConfig->WatchdogMode == ADC_ANALOGWATCHDOG_SINGLE_REGINJEC) )
  6077. {
  6078. assert_param(IS_ADC_CHANNEL(AnalogWDGConfig->Channel));
  6079. }
  6080. /* Process locked */
  6081. __HAL_LOCK(hadc);
  6082. /* Analog watchdog configuration */
  6083. /* Configure ADC Analog watchdog interrupt */
  6084. if(AnalogWDGConfig->ITMode == ENABLE)
  6085. {
  6086. /* Enable the ADC Analog watchdog interrupt */
  6087. __HAL_ADC_ENABLE_IT(hadc, ADC_IT_AWD);
  6088. }
  6089. else
  6090. {
  6091. /* Disable the ADC Analog watchdog interrupt */
  6092. __HAL_ADC_DISABLE_IT(hadc, ADC_IT_AWD);
  6093. }
  6094. /* Configuration of analog watchdog: */
  6095. /* - Set the analog watchdog enable mode: regular and/or injected groups, */
  6096. /* one or all channels. */
  6097. /* - Set the Analog watchdog channel (is not used if watchdog */
  6098. /* mode "all channels": ADC_CFGR_AWD1SGL=0U). */
  6099. MODIFY_REG(hadc->Instance->CR1 ,
  6100. ADC_CR1_AWDSGL |
  6101. ADC_CR1_JAWDEN |
  6102. ADC_CR1_AWDEN |
  6103. ADC_CR1_AWDCH ,
  6104. AnalogWDGConfig->WatchdogMode |
  6105. AnalogWDGConfig->Channel );
  6106. /* Set the high threshold */
  6107. WRITE_REG(hadc->Instance->HTR, AnalogWDGConfig->HighThreshold);
  6108. /* Set the low threshold */
  6109. WRITE_REG(hadc->Instance->LTR, AnalogWDGConfig->LowThreshold);
  6110. /* Process unlocked */
  6111. __HAL_UNLOCK(hadc);
  6112. /* Return function status */
  6113. return HAL_OK;
  6114. }
  6115. #endif /* STM32F373xC || STM32F378xx */
  6116. #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
  6117. defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
  6118. defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx)
  6119. /**
  6120. * @brief Enable ADC multimode and configure multimode parameters
  6121. * @note Possibility to update parameters on the fly:
  6122. * This function initializes multimode parameters, following
  6123. * calls to this function can be used to reconfigure some parameters
  6124. * of structure "ADC_MultiModeTypeDef" on the fly, without resetting
  6125. * the ADCs (both ADCs of the common group).
  6126. * The setting of these parameters is conditioned to ADC state.
  6127. * For parameters constraints, see comments of structure
  6128. * "ADC_MultiModeTypeDef".
  6129. * @note To change back configuration from multimode to single mode, ADC must
  6130. * be reset (using function HAL_ADC_Init() ).
  6131. * @param hadc ADC handle
  6132. * @param multimode Structure of ADC multimode configuration
  6133. * @retval HAL status
  6134. */
  6135. HAL_StatusTypeDef HAL_ADCEx_MultiModeConfigChannel(ADC_HandleTypeDef* hadc, ADC_MultiModeTypeDef* multimode)
  6136. {
  6137. HAL_StatusTypeDef tmp_hal_status = HAL_OK;
  6138. ADC_Common_TypeDef *tmpADC_Common;
  6139. ADC_HandleTypeDef tmphadcSharingSameCommonRegister;
  6140. /* Check the parameters */
  6141. assert_param(IS_ADC_MULTIMODE_MASTER_INSTANCE(hadc->Instance));
  6142. assert_param(IS_ADC_MODE(multimode->Mode));
  6143. if(multimode->Mode != ADC_MODE_INDEPENDENT)
  6144. {
  6145. assert_param(IS_ADC_DMA_ACCESS_MODE(multimode->DMAAccessMode));
  6146. assert_param(IS_ADC_SAMPLING_DELAY(multimode->TwoSamplingDelay));
  6147. }
  6148. /* Set handle of the other ADC sharing the same common register */
  6149. ADC_COMMON_ADC_OTHER(hadc, &tmphadcSharingSameCommonRegister);
  6150. if (tmphadcSharingSameCommonRegister.Instance == NULL)
  6151. {
  6152. /* Return function status */
  6153. return HAL_ERROR;
  6154. }
  6155. /* Process locked */
  6156. __HAL_LOCK(hadc);
  6157. /* Parameters update conditioned to ADC state: */
  6158. /* Parameters that can be updated when ADC is disabled or enabled without */
  6159. /* conversion on going on regular group: */
  6160. /* - Multimode DMA configuration */
  6161. /* - Multimode DMA mode */
  6162. if ( (ADC_IS_CONVERSION_ONGOING_REGULAR(hadc) == RESET)
  6163. && (ADC_IS_CONVERSION_ONGOING_REGULAR(&tmphadcSharingSameCommonRegister) == RESET) )
  6164. {
  6165. /* Pointer to the common control register to which is belonging hadc */
  6166. /* (Depending on STM32F3 product, there may have up to 4 ADC and 2 common */
  6167. /* control registers) */
  6168. tmpADC_Common = ADC_COMMON_REGISTER(hadc);
  6169. /* If multimode is selected, configure all multimode parameters. */
  6170. /* Otherwise, reset multimode parameters (can be used in case of */
  6171. /* transition from multimode to independent mode). */
  6172. if(multimode->Mode != ADC_MODE_INDEPENDENT)
  6173. {
  6174. /* Configuration of ADC common group ADC1&ADC2, ADC3&ADC4 if available */
  6175. /* (ADC2, ADC3, ADC4 availability depends on STM32 product) */
  6176. /* - DMA access mode */
  6177. MODIFY_REG(tmpADC_Common->CCR ,
  6178. ADC_CCR_MDMA |
  6179. ADC_CCR_DMACFG ,
  6180. multimode->DMAAccessMode |
  6181. ADC_CCR_MULTI_DMACONTREQ((uint32_t)hadc->Init.DMAContinuousRequests) );
  6182. /* Parameters that can be updated only when ADC is disabled: */
  6183. /* - Multimode mode selection */
  6184. /* - Set delay between two sampling phases */
  6185. /* Note: Delay range depends on selected resolution: */
  6186. /* from 1 to 12 clock cycles for 12 bits */
  6187. /* from 1 to 10 clock cycles for 10 bits, */
  6188. /* from 1 to 8 clock cycles for 8 bits */
  6189. /* from 1 to 6 clock cycles for 6 bits */
  6190. /* If a higher delay is selected, it will be clamped to maximum delay */
  6191. /* range */
  6192. /* Note: If ADC is not in the appropriate state to modify these */
  6193. /* parameters, their setting is bypassed without error reporting */
  6194. /* (as it can be the expected behaviour in case of intended action */
  6195. /* to update parameter above (which fulfills the ADC state */
  6196. /* condition: no conversion on going on group regular) */
  6197. /* on the fly). */
  6198. if ((ADC_IS_ENABLE(hadc) == RESET) &&
  6199. (ADC_IS_ENABLE(&tmphadcSharingSameCommonRegister) == RESET) )
  6200. {
  6201. MODIFY_REG(tmpADC_Common->CCR ,
  6202. ADC_CCR_MULTI |
  6203. ADC_CCR_DELAY ,
  6204. multimode->Mode |
  6205. multimode->TwoSamplingDelay );
  6206. }
  6207. }
  6208. else /* ADC_MODE_INDEPENDENT */
  6209. {
  6210. CLEAR_BIT(tmpADC_Common->CCR, ADC_CCR_MDMA | ADC_CCR_DMACFG);
  6211. /* Parameters that can be updated only when ADC is disabled: */
  6212. /* - Multimode mode selection */
  6213. /* - Multimode delay */
  6214. if ((ADC_IS_ENABLE(hadc) == RESET) &&
  6215. (ADC_IS_ENABLE(&tmphadcSharingSameCommonRegister) == RESET) )
  6216. {
  6217. CLEAR_BIT(tmpADC_Common->CCR, ADC_CCR_MULTI | ADC_CCR_DELAY);
  6218. }
  6219. }
  6220. }
  6221. /* If one of the ADC sharing the same common group is enabled, no update */
  6222. /* could be done on neither of the multimode structure parameters. */
  6223. else
  6224. {
  6225. /* Update ADC state machine to error */
  6226. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
  6227. tmp_hal_status = HAL_ERROR;
  6228. }
  6229. /* Process unlocked */
  6230. __HAL_UNLOCK(hadc);
  6231. /* Return function status */
  6232. return tmp_hal_status;
  6233. }
  6234. #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
  6235. /* STM32F302xC || STM32F303xC || STM32F358xx || */
  6236. /* STM32F303x8 || STM32F328xx || STM32F334x8 */
  6237. /**
  6238. * @}
  6239. */
  6240. /**
  6241. * @}
  6242. */
  6243. /** @defgroup ADCEx_Private_Functions ADCEx Private Functions
  6244. * @{
  6245. */
  6246. /**
  6247. * @brief DMA transfer complete callback.
  6248. * @param hdma pointer to DMA handle.
  6249. * @retval None
  6250. */
  6251. static void ADC_DMAConvCplt(DMA_HandleTypeDef *hdma)
  6252. {
  6253. /* Retrieve ADC handle corresponding to current DMA handle */
  6254. ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  6255. /* Update state machine on conversion status if not in error state */
  6256. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL | HAL_ADC_STATE_ERROR_DMA))
  6257. {
  6258. /* Update ADC state machine */
  6259. SET_BIT(hadc->State, HAL_ADC_STATE_REG_EOC);
  6260. /* Determine whether any further conversion upcoming on group regular */
  6261. /* by external trigger, continuous mode or scan sequence on going. */
  6262. /* Note: On STM32F3 devices, in case of sequencer enabled */
  6263. /* (several ranks selected), end of conversion flag is raised */
  6264. /* at the end of the sequence. */
  6265. if(ADC_IS_SOFTWARE_START_REGULAR(hadc) &&
  6266. (hadc->Init.ContinuousConvMode == DISABLE) )
  6267. {
  6268. /* Set ADC state */
  6269. CLEAR_BIT(hadc->State, HAL_ADC_STATE_REG_BUSY);
  6270. if (HAL_IS_BIT_CLR(hadc->State, HAL_ADC_STATE_INJ_BUSY))
  6271. {
  6272. SET_BIT(hadc->State, HAL_ADC_STATE_READY);
  6273. }
  6274. }
  6275. /* Conversion complete callback */
  6276. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  6277. hadc->ConvCpltCallback(hadc);
  6278. #else
  6279. HAL_ADC_ConvCpltCallback(hadc);
  6280. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  6281. }
  6282. else
  6283. {
  6284. /* Call DMA error callback */
  6285. hadc->DMA_Handle->XferErrorCallback(hdma);
  6286. }
  6287. }
  6288. /**
  6289. * @brief DMA half transfer complete callback.
  6290. * @param hdma pointer to DMA handle.
  6291. * @retval None
  6292. */
  6293. static void ADC_DMAHalfConvCplt(DMA_HandleTypeDef *hdma)
  6294. {
  6295. /* Retrieve ADC handle corresponding to current DMA handle */
  6296. ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  6297. /* Half conversion callback */
  6298. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  6299. hadc->ConvHalfCpltCallback(hadc);
  6300. #else
  6301. HAL_ADC_ConvHalfCpltCallback(hadc);
  6302. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  6303. }
  6304. /**
  6305. * @brief DMA error callback
  6306. * @param hdma pointer to DMA handle.
  6307. * @retval None
  6308. */
  6309. static void ADC_DMAError(DMA_HandleTypeDef *hdma)
  6310. {
  6311. /* Retrieve ADC handle corresponding to current DMA handle */
  6312. ADC_HandleTypeDef* hadc = ( ADC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  6313. /* Set ADC state */
  6314. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_DMA);
  6315. /* Set ADC error code to DMA error */
  6316. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_DMA);
  6317. /* Error callback */
  6318. #if (USE_HAL_ADC_REGISTER_CALLBACKS == 1)
  6319. hadc->ErrorCallback(hadc);
  6320. #else
  6321. HAL_ADC_ErrorCallback(hadc);
  6322. #endif /* USE_HAL_ADC_REGISTER_CALLBACKS */
  6323. }
  6324. #if defined(STM32F302xE) || defined(STM32F303xE) || defined(STM32F398xx) || \
  6325. defined(STM32F302xC) || defined(STM32F303xC) || defined(STM32F358xx) || \
  6326. defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
  6327. defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
  6328. /**
  6329. * @brief Enable the selected ADC.
  6330. * @note Prerequisite condition to use this function: ADC must be disabled
  6331. * and voltage regulator must be enabled (done into HAL_ADC_Init()).
  6332. * @param hadc ADC handle
  6333. * @retval HAL status.
  6334. */
  6335. static HAL_StatusTypeDef ADC_Enable(ADC_HandleTypeDef* hadc)
  6336. {
  6337. uint32_t tickstart = 0U;
  6338. /* ADC enable and wait for ADC ready (in case of ADC is disabled or */
  6339. /* enabling phase not yet completed: flag ADC ready not yet set). */
  6340. /* Timeout implemented to not be stuck if ADC cannot be enabled (possible */
  6341. /* causes: ADC clock not running, ...). */
  6342. if (ADC_IS_ENABLE(hadc) == RESET)
  6343. {
  6344. /* Check if conditions to enable the ADC are fulfilled */
  6345. if (ADC_ENABLING_CONDITIONS(hadc) == RESET)
  6346. {
  6347. /* Update ADC state machine to error */
  6348. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  6349. /* Set ADC error code to ADC IP internal error */
  6350. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  6351. return HAL_ERROR;
  6352. }
  6353. /* Enable the ADC peripheral */
  6354. __HAL_ADC_ENABLE(hadc);
  6355. /* Wait for ADC effectively enabled */
  6356. tickstart = HAL_GetTick();
  6357. while(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_RDY) == RESET)
  6358. {
  6359. if((HAL_GetTick() - tickstart) > ADC_ENABLE_TIMEOUT)
  6360. {
  6361. /* New check to avoid false timeout detection in case of preemption */
  6362. if(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_RDY) == RESET)
  6363. {
  6364. /* Update ADC state machine to error */
  6365. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  6366. /* Set ADC error code to ADC IP internal error */
  6367. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  6368. return HAL_ERROR;
  6369. }
  6370. }
  6371. }
  6372. }
  6373. /* Return HAL status */
  6374. return HAL_OK;
  6375. }
  6376. /**
  6377. * @brief Disable the selected ADC.
  6378. * @note Prerequisite condition to use this function: ADC conversions must be
  6379. * stopped.
  6380. * @param hadc ADC handle
  6381. * @retval HAL status.
  6382. */
  6383. static HAL_StatusTypeDef ADC_Disable(ADC_HandleTypeDef* hadc)
  6384. {
  6385. uint32_t tickstart = 0U;
  6386. /* Verification if ADC is not already disabled: */
  6387. /* Note: forbidden to disable ADC (set bit ADC_CR_ADDIS) if ADC is already */
  6388. /* disabled. */
  6389. if (ADC_IS_ENABLE(hadc) != RESET )
  6390. {
  6391. /* Check if conditions to disable the ADC are fulfilled */
  6392. if (ADC_DISABLING_CONDITIONS(hadc) != RESET)
  6393. {
  6394. /* Disable the ADC peripheral */
  6395. __HAL_ADC_DISABLE(hadc);
  6396. }
  6397. else
  6398. {
  6399. /* Update ADC state machine to error */
  6400. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  6401. /* Set ADC error code to ADC IP internal error */
  6402. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  6403. return HAL_ERROR;
  6404. }
  6405. /* Wait for ADC effectively disabled */
  6406. tickstart = HAL_GetTick();
  6407. while(HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADEN))
  6408. {
  6409. if((HAL_GetTick() - tickstart) > ADC_DISABLE_TIMEOUT)
  6410. {
  6411. /* New check to avoid false timeout detection in case of preemption */
  6412. if(HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADEN))
  6413. {
  6414. /* Update ADC state machine to error */
  6415. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  6416. /* Set ADC error code to ADC IP internal error */
  6417. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  6418. return HAL_ERROR;
  6419. }
  6420. }
  6421. }
  6422. }
  6423. /* Return HAL status */
  6424. return HAL_OK;
  6425. }
  6426. /**
  6427. * @brief Stop ADC conversion.
  6428. * @param hadc ADC handle
  6429. * @param ConversionGroup ADC group regular and/or injected.
  6430. * This parameter can be one of the following values:
  6431. * @arg ADC_REGULAR_GROUP: ADC regular conversion type.
  6432. * @arg ADC_INJECTED_GROUP: ADC injected conversion type.
  6433. * @arg ADC_REGULAR_INJECTED_GROUP: ADC regular and injected conversion type.
  6434. * @retval HAL status.
  6435. */
  6436. static HAL_StatusTypeDef ADC_ConversionStop(ADC_HandleTypeDef* hadc, uint32_t ConversionGroup)
  6437. {
  6438. uint32_t tmp_ADC_CR_ADSTART_JADSTART = 0U;
  6439. uint32_t tickstart = 0U;
  6440. uint32_t Conversion_Timeout_CPU_cycles = 0U;
  6441. /* Check the parameters */
  6442. assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
  6443. assert_param(IS_ADC_CONVERSION_GROUP(ConversionGroup));
  6444. /* Verification if ADC is not already stopped (on regular and injected */
  6445. /* groups) to bypass this function if not needed. */
  6446. if (ADC_IS_CONVERSION_ONGOING_REGULAR_INJECTED(hadc))
  6447. {
  6448. /* Particular case of continuous auto-injection mode combined with */
  6449. /* auto-delay mode. */
  6450. /* In auto-injection mode, regular group stop ADC_CR_ADSTP is used (not */
  6451. /* injected group stop ADC_CR_JADSTP). */
  6452. /* Procedure to be followed: Wait until JEOS=1U, clear JEOS, set ADSTP=1 */
  6453. /* (see reference manual). */
  6454. if ((HAL_IS_BIT_SET(hadc->Instance->CFGR, ADC_CFGR_JAUTO)) &&
  6455. (hadc->Init.ContinuousConvMode==ENABLE) &&
  6456. (hadc->Init.LowPowerAutoWait==ENABLE) )
  6457. {
  6458. /* Use stop of regular group */
  6459. ConversionGroup = ADC_REGULAR_GROUP;
  6460. /* Wait until JEOS=1 (maximum Timeout: 4 injected conversions) */
  6461. while(__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_JEOS) == RESET)
  6462. {
  6463. if (Conversion_Timeout_CPU_cycles >= (ADC_CONVERSION_TIME_MAX_CPU_CYCLES *4U))
  6464. {
  6465. /* Update ADC state machine to error */
  6466. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  6467. /* Set ADC error code to ADC IP internal error */
  6468. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  6469. return HAL_ERROR;
  6470. }
  6471. Conversion_Timeout_CPU_cycles ++;
  6472. }
  6473. /* Clear JEOS */
  6474. __HAL_ADC_CLEAR_FLAG(hadc, ADC_FLAG_JEOS);
  6475. }
  6476. /* Stop potential conversion on going on regular group */
  6477. if (ConversionGroup != ADC_INJECTED_GROUP)
  6478. {
  6479. /* Software is allowed to set ADSTP only when ADSTART=1 and ADDIS=0U */
  6480. if (HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_ADSTART) &&
  6481. HAL_IS_BIT_CLR(hadc->Instance->CR, ADC_CR_ADDIS) )
  6482. {
  6483. /* Stop conversions on regular group */
  6484. hadc->Instance->CR |= ADC_CR_ADSTP;
  6485. }
  6486. }
  6487. /* Stop potential conversion on going on injected group */
  6488. if (ConversionGroup != ADC_REGULAR_GROUP)
  6489. {
  6490. /* Software is allowed to set JADSTP only when JADSTART=1 and ADDIS=0U */
  6491. if (HAL_IS_BIT_SET(hadc->Instance->CR, ADC_CR_JADSTART) &&
  6492. HAL_IS_BIT_CLR(hadc->Instance->CR, ADC_CR_ADDIS) )
  6493. {
  6494. /* Stop conversions on injected group */
  6495. hadc->Instance->CR |= ADC_CR_JADSTP;
  6496. }
  6497. }
  6498. /* Selection of start and stop bits in function of regular or injected group */
  6499. switch(ConversionGroup)
  6500. {
  6501. case ADC_REGULAR_INJECTED_GROUP:
  6502. tmp_ADC_CR_ADSTART_JADSTART = (ADC_CR_ADSTART | ADC_CR_JADSTART);
  6503. break;
  6504. case ADC_INJECTED_GROUP:
  6505. tmp_ADC_CR_ADSTART_JADSTART = ADC_CR_JADSTART;
  6506. break;
  6507. /* Case ADC_REGULAR_GROUP */
  6508. default:
  6509. tmp_ADC_CR_ADSTART_JADSTART = ADC_CR_ADSTART;
  6510. break;
  6511. }
  6512. /* Wait for conversion effectively stopped */
  6513. tickstart = HAL_GetTick();
  6514. while((hadc->Instance->CR & tmp_ADC_CR_ADSTART_JADSTART) != RESET)
  6515. {
  6516. if((HAL_GetTick() - tickstart) > ADC_STOP_CONVERSION_TIMEOUT)
  6517. {
  6518. /* New check to avoid false timeout detection in case of preemption */
  6519. if((hadc->Instance->CR & tmp_ADC_CR_ADSTART_JADSTART) != RESET)
  6520. {
  6521. /* Update ADC state machine to error */
  6522. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  6523. /* Set ADC error code to ADC IP internal error */
  6524. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  6525. return HAL_ERROR;
  6526. }
  6527. }
  6528. }
  6529. }
  6530. /* Return HAL status */
  6531. return HAL_OK;
  6532. }
  6533. #endif /* STM32F302xE || STM32F303xE || STM32F398xx || */
  6534. /* STM32F302xC || STM32F303xC || STM32F358xx || */
  6535. /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
  6536. /* STM32F301x8 || STM32F302x8 || STM32F318xx */
  6537. #if defined(STM32F373xC) || defined(STM32F378xx)
  6538. /**
  6539. * @brief Enable the selected ADC.
  6540. * @note Prerequisite condition to use this function: ADC must be disabled
  6541. * and voltage regulator must be enabled (done into HAL_ADC_Init()).
  6542. * @param hadc ADC handle
  6543. * @retval HAL status.
  6544. */
  6545. static HAL_StatusTypeDef ADC_Enable(ADC_HandleTypeDef* hadc)
  6546. {
  6547. uint32_t tickstart = 0U;
  6548. __IO uint32_t wait_loop_index = 0U;
  6549. /* ADC enable and wait for ADC ready (in case of ADC is disabled or */
  6550. /* enabling phase not yet completed: flag ADC ready not yet set). */
  6551. /* Timeout implemented to not be stuck if ADC cannot be enabled (possible */
  6552. /* causes: ADC clock not running, ...). */
  6553. if (ADC_IS_ENABLE(hadc) == RESET)
  6554. {
  6555. /* Enable the Peripheral */
  6556. __HAL_ADC_ENABLE(hadc);
  6557. /* Delay for ADC stabilization time */
  6558. /* Compute number of CPU cycles to wait for */
  6559. wait_loop_index = (ADC_STAB_DELAY_US * (SystemCoreClock / 1000000U));
  6560. while(wait_loop_index != 0U)
  6561. {
  6562. wait_loop_index--;
  6563. }
  6564. /* Get tick count */
  6565. tickstart = HAL_GetTick();
  6566. /* Wait for ADC effectively enabled */
  6567. while(ADC_IS_ENABLE(hadc) == RESET)
  6568. {
  6569. if((HAL_GetTick() - tickstart) > ADC_ENABLE_TIMEOUT)
  6570. {
  6571. /* New check to avoid false timeout detection in case of preemption */
  6572. if(ADC_IS_ENABLE(hadc) == RESET)
  6573. {
  6574. /* Update ADC state machine to error */
  6575. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  6576. /* Set ADC error code to ADC IP internal error */
  6577. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  6578. /* Process unlocked */
  6579. __HAL_UNLOCK(hadc);
  6580. return HAL_ERROR;
  6581. }
  6582. }
  6583. }
  6584. }
  6585. /* Return HAL status */
  6586. return HAL_OK;
  6587. }
  6588. /**
  6589. * @brief Stop ADC conversion and disable the selected ADC
  6590. * @param hadc ADC handle
  6591. * @retval HAL status.
  6592. */
  6593. static HAL_StatusTypeDef ADC_ConversionStop_Disable(ADC_HandleTypeDef* hadc)
  6594. {
  6595. uint32_t tickstart = 0U;
  6596. /* Verification if ADC is not already disabled: */
  6597. if (ADC_IS_ENABLE(hadc) != RESET)
  6598. {
  6599. /* Disable the ADC peripheral */
  6600. __HAL_ADC_DISABLE(hadc);
  6601. /* Get tick count */
  6602. tickstart = HAL_GetTick();
  6603. /* Wait for ADC effectively disabled */
  6604. while(ADC_IS_ENABLE(hadc) != RESET)
  6605. {
  6606. if((HAL_GetTick() - tickstart) > ADC_DISABLE_TIMEOUT)
  6607. {
  6608. /* New check to avoid false timeout detection in case of preemption */
  6609. if(ADC_IS_ENABLE(hadc) != RESET)
  6610. {
  6611. /* Update ADC state machine to error */
  6612. SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_INTERNAL);
  6613. /* Set ADC error code to ADC IP internal error */
  6614. SET_BIT(hadc->ErrorCode, HAL_ADC_ERROR_INTERNAL);
  6615. return HAL_ERROR;
  6616. }
  6617. }
  6618. }
  6619. }
  6620. /* Return HAL status */
  6621. return HAL_OK;
  6622. }
  6623. #endif /* STM32F373xC || STM32F378xx */
  6624. /**
  6625. * @}
  6626. */
  6627. #endif /* HAL_ADC_MODULE_ENABLED */
  6628. /**
  6629. * @}
  6630. */
  6631. /**
  6632. * @}
  6633. */