stm32l0xx_hal_tim.c 227 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173
  1. /**
  2. ******************************************************************************
  3. * @file stm32l0xx_hal_tim.c
  4. * @author MCD Application Team
  5. * @brief TIM HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Timer (TIM) peripheral:
  8. * + TIM Time Base Initialization
  9. * + TIM Time Base Start
  10. * + TIM Time Base Start Interruption
  11. * + TIM Time Base Start DMA
  12. * + TIM Output Compare/PWM Initialization
  13. * + TIM Output Compare/PWM Channel Configuration
  14. * + TIM Output Compare/PWM Start
  15. * + TIM Output Compare/PWM Start Interruption
  16. * + TIM Output Compare/PWM Start DMA
  17. * + TIM Input Capture Initialization
  18. * + TIM Input Capture Channel Configuration
  19. * + TIM Input Capture Start
  20. * + TIM Input Capture Start Interruption
  21. * + TIM Input Capture Start DMA
  22. * + TIM One Pulse Initialization
  23. * + TIM One Pulse Channel Configuration
  24. * + TIM One Pulse Start
  25. * + TIM Encoder Interface Initialization
  26. * + TIM Encoder Interface Start
  27. * + TIM Encoder Interface Start Interruption
  28. * + TIM Encoder Interface Start DMA
  29. * + Commutation Event configuration with Interruption and DMA
  30. * + TIM OCRef clear configuration
  31. * + TIM External Clock configuration
  32. ******************************************************************************
  33. * @attention
  34. *
  35. * Copyright (c) 2016 STMicroelectronics.
  36. * All rights reserved.
  37. *
  38. * This software is licensed under terms that can be found in the LICENSE file
  39. * in the root directory of this software component.
  40. * If no LICENSE file comes with this software, it is provided AS-IS.
  41. *
  42. ******************************************************************************
  43. @verbatim
  44. ==============================================================================
  45. ##### TIMER Generic features #####
  46. ==============================================================================
  47. [..] The Timer features include:
  48. (#) 16-bit up, down, up/down auto-reload counter.
  49. (#) 16-bit programmable prescaler allowing dividing (also on the fly) the
  50. counter clock frequency either by any factor between 1 and 65536.
  51. (#) Up to 4 independent channels for:
  52. (++) Input Capture
  53. (++) Output Compare
  54. (++) PWM generation (Edge and Center-aligned Mode)
  55. (++) One-pulse mode output
  56. (#) Synchronization circuit to control the timer with external signals and to interconnect
  57. several timers together.
  58. (#) Supports incremental encoder for positioning purposes
  59. ##### How to use this driver #####
  60. ==============================================================================
  61. [..]
  62. (#) Initialize the TIM low level resources by implementing the following functions
  63. depending on the selected feature:
  64. (++) Time Base : HAL_TIM_Base_MspInit()
  65. (++) Input Capture : HAL_TIM_IC_MspInit()
  66. (++) Output Compare : HAL_TIM_OC_MspInit()
  67. (++) PWM generation : HAL_TIM_PWM_MspInit()
  68. (++) One-pulse mode output : HAL_TIM_OnePulse_MspInit()
  69. (++) Encoder mode output : HAL_TIM_Encoder_MspInit()
  70. (#) Initialize the TIM low level resources :
  71. (##) Enable the TIM interface clock using __HAL_RCC_TIMx_CLK_ENABLE();
  72. (##) TIM pins configuration
  73. (+++) Enable the clock for the TIM GPIOs using the following function:
  74. __HAL_RCC_GPIOx_CLK_ENABLE();
  75. (+++) Configure these TIM pins in Alternate function mode using HAL_GPIO_Init();
  76. (#) The external Clock can be configured, if needed (the default clock is the
  77. internal clock from the APBx), using the following function:
  78. HAL_TIM_ConfigClockSource, the clock configuration should be done before
  79. any start function.
  80. (#) Configure the TIM in the desired functioning mode using one of the
  81. Initialization function of this driver:
  82. (++) HAL_TIM_Base_Init: to use the Timer to generate a simple time base
  83. (++) HAL_TIM_OC_Init and HAL_TIM_OC_ConfigChannel: to use the Timer to generate an
  84. Output Compare signal.
  85. (++) HAL_TIM_PWM_Init and HAL_TIM_PWM_ConfigChannel: to use the Timer to generate a
  86. PWM signal.
  87. (++) HAL_TIM_IC_Init and HAL_TIM_IC_ConfigChannel: to use the Timer to measure an
  88. external signal.
  89. (++) HAL_TIM_OnePulse_Init and HAL_TIM_OnePulse_ConfigChannel: to use the Timer
  90. in One Pulse Mode.
  91. (++) HAL_TIM_Encoder_Init: to use the Timer Encoder Interface.
  92. (#) Activate the TIM peripheral using one of the start functions depending from the feature used:
  93. (++) Time Base : HAL_TIM_Base_Start(), HAL_TIM_Base_Start_DMA(), HAL_TIM_Base_Start_IT()
  94. (++) Input Capture : HAL_TIM_IC_Start(), HAL_TIM_IC_Start_DMA(), HAL_TIM_IC_Start_IT()
  95. (++) Output Compare : HAL_TIM_OC_Start(), HAL_TIM_OC_Start_DMA(), HAL_TIM_OC_Start_IT()
  96. (++) PWM generation : HAL_TIM_PWM_Start(), HAL_TIM_PWM_Start_DMA(), HAL_TIM_PWM_Start_IT()
  97. (++) One-pulse mode output : HAL_TIM_OnePulse_Start(), HAL_TIM_OnePulse_Start_IT()
  98. (++) Encoder mode output : HAL_TIM_Encoder_Start(), HAL_TIM_Encoder_Start_DMA(), HAL_TIM_Encoder_Start_IT().
  99. (#) The DMA Burst is managed with the two following functions:
  100. HAL_TIM_DMABurst_WriteStart()
  101. HAL_TIM_DMABurst_ReadStart()
  102. *** Callback registration ***
  103. =============================================
  104. [..]
  105. The compilation define USE_HAL_TIM_REGISTER_CALLBACKS when set to 1
  106. allows the user to configure dynamically the driver callbacks.
  107. [..]
  108. Use Function HAL_TIM_RegisterCallback() to register a callback.
  109. HAL_TIM_RegisterCallback() takes as parameters the HAL peripheral handle,
  110. the Callback ID and a pointer to the user callback function.
  111. [..]
  112. Use function HAL_TIM_UnRegisterCallback() to reset a callback to the default
  113. weak function.
  114. HAL_TIM_UnRegisterCallback takes as parameters the HAL peripheral handle,
  115. and the Callback ID.
  116. [..]
  117. These functions allow to register/unregister following callbacks:
  118. (+) Base_MspInitCallback : TIM Base Msp Init Callback.
  119. (+) Base_MspDeInitCallback : TIM Base Msp DeInit Callback.
  120. (+) IC_MspInitCallback : TIM IC Msp Init Callback.
  121. (+) IC_MspDeInitCallback : TIM IC Msp DeInit Callback.
  122. (+) OC_MspInitCallback : TIM OC Msp Init Callback.
  123. (+) OC_MspDeInitCallback : TIM OC Msp DeInit Callback.
  124. (+) PWM_MspInitCallback : TIM PWM Msp Init Callback.
  125. (+) PWM_MspDeInitCallback : TIM PWM Msp DeInit Callback.
  126. (+) OnePulse_MspInitCallback : TIM One Pulse Msp Init Callback.
  127. (+) OnePulse_MspDeInitCallback : TIM One Pulse Msp DeInit Callback.
  128. (+) Encoder_MspInitCallback : TIM Encoder Msp Init Callback.
  129. (+) Encoder_MspDeInitCallback : TIM Encoder Msp DeInit Callback.
  130. (+) PeriodElapsedCallback : TIM Period Elapsed Callback.
  131. (+) PeriodElapsedHalfCpltCallback : TIM Period Elapsed half complete Callback.
  132. (+) TriggerCallback : TIM Trigger Callback.
  133. (+) TriggerHalfCpltCallback : TIM Trigger half complete Callback.
  134. (+) IC_CaptureCallback : TIM Input Capture Callback.
  135. (+) IC_CaptureHalfCpltCallback : TIM Input Capture half complete Callback.
  136. (+) OC_DelayElapsedCallback : TIM Output Compare Delay Elapsed Callback.
  137. (+) PWM_PulseFinishedCallback : TIM PWM Pulse Finished Callback.
  138. (+) PWM_PulseFinishedHalfCpltCallback : TIM PWM Pulse Finished half complete Callback.
  139. (+) ErrorCallback : TIM Error Callback.
  140. [..]
  141. By default, after the Init and when the state is HAL_TIM_STATE_RESET
  142. all interrupt callbacks are set to the corresponding weak functions:
  143. examples HAL_TIM_TriggerCallback(), HAL_TIM_ErrorCallback().
  144. [..]
  145. Exception done for MspInit and MspDeInit functions that are reset to the legacy weak
  146. functionalities in the Init / DeInit only when these callbacks are null
  147. (not registered beforehand). If not, MspInit or MspDeInit are not null, the Init / DeInit
  148. keep and use the user MspInit / MspDeInit callbacks(registered beforehand)
  149. [..]
  150. Callbacks can be registered / unregistered in HAL_TIM_STATE_READY state only.
  151. Exception done MspInit / MspDeInit that can be registered / unregistered
  152. in HAL_TIM_STATE_READY or HAL_TIM_STATE_RESET state,
  153. thus registered(user) MspInit / DeInit callbacks can be used during the Init / DeInit.
  154. In that case first register the MspInit/MspDeInit user callbacks
  155. using HAL_TIM_RegisterCallback() before calling DeInit or Init function.
  156. [..]
  157. When The compilation define USE_HAL_TIM_REGISTER_CALLBACKS is set to 0 or
  158. not defined, the callback registration feature is not available and all callbacks
  159. are set to the corresponding weak functions.
  160. @endverbatim
  161. ******************************************************************************
  162. */
  163. /* Includes ------------------------------------------------------------------*/
  164. #include "stm32l0xx_hal.h"
  165. /** @addtogroup STM32L0xx_HAL_Driver
  166. * @{
  167. */
  168. /** @defgroup TIM TIM
  169. * @brief TIM HAL module driver
  170. * @{
  171. */
  172. #ifdef HAL_TIM_MODULE_ENABLED
  173. /* Private typedef -----------------------------------------------------------*/
  174. /* Private define ------------------------------------------------------------*/
  175. /* Private macros ------------------------------------------------------------*/
  176. /* Private variables ---------------------------------------------------------*/
  177. /* Private function prototypes -----------------------------------------------*/
  178. /** @addtogroup TIM_Private_Functions
  179. * @{
  180. */
  181. static void TIM_Base_SetConfig(TIM_TypeDef *TIMx, const TIM_Base_InitTypeDef *Structure);
  182. static void TIM_OC1_SetConfig(TIM_TypeDef *TIMx, const TIM_OC_InitTypeDef *OC_Config);
  183. static void TIM_OC2_SetConfig(TIM_TypeDef *TIMx, const TIM_OC_InitTypeDef *OC_Config);
  184. static void TIM_OC3_SetConfig(TIM_TypeDef *TIMx, const TIM_OC_InitTypeDef *OC_Config);
  185. static void TIM_OC4_SetConfig(TIM_TypeDef *TIMx, const TIM_OC_InitTypeDef *OC_Config);
  186. static void TIM_TI1_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
  187. uint32_t TIM_ICFilter);
  188. static void TIM_TI1_ConfigInputStage(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICFilter);
  189. static void TIM_TI2_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
  190. uint32_t TIM_ICFilter);
  191. static void TIM_TI2_ConfigInputStage(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICFilter);
  192. static void TIM_TI3_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
  193. uint32_t TIM_ICFilter);
  194. static void TIM_TI4_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
  195. uint32_t TIM_ICFilter);
  196. static void TIM_ITRx_SetConfig(TIM_TypeDef *TIMx, uint32_t InputTriggerSource);
  197. static void TIM_ETR_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ExtTRGPrescaler,
  198. uint32_t TIM_ExtTRGPolarity, uint32_t ExtTRGFilter);
  199. static void TIM_CCxChannelCmd(TIM_TypeDef *TIMx, uint32_t Channel, uint32_t ChannelState);
  200. static void TIM_DMAPeriodElapsedCplt(DMA_HandleTypeDef *hdma);
  201. static void TIM_DMAPeriodElapsedHalfCplt(DMA_HandleTypeDef *hdma);
  202. static void TIM_DMADelayPulseCplt(DMA_HandleTypeDef *hdma);
  203. static void TIM_DMADelayPulseHalfCplt(DMA_HandleTypeDef *hdma);
  204. static void TIM_DMATriggerCplt(DMA_HandleTypeDef *hdma);
  205. static void TIM_DMATriggerHalfCplt(DMA_HandleTypeDef *hdma);
  206. static HAL_StatusTypeDef TIM_SlaveTimer_SetConfig(TIM_HandleTypeDef *htim,
  207. const TIM_SlaveConfigTypeDef *sSlaveConfig);
  208. /**
  209. * @}
  210. */
  211. /* Exported functions --------------------------------------------------------*/
  212. /** @defgroup TIM_Exported_Functions TIM Exported Functions
  213. * @{
  214. */
  215. /** @defgroup TIM_Exported_Functions_Group1 TIM Time Base functions
  216. * @brief Time Base functions
  217. *
  218. @verbatim
  219. ==============================================================================
  220. ##### Time Base functions #####
  221. ==============================================================================
  222. [..]
  223. This section provides functions allowing to:
  224. (+) Initialize and configure the TIM base.
  225. (+) De-initialize the TIM base.
  226. (+) Start the Time Base.
  227. (+) Stop the Time Base.
  228. (+) Start the Time Base and enable interrupt.
  229. (+) Stop the Time Base and disable interrupt.
  230. (+) Start the Time Base and enable DMA transfer.
  231. (+) Stop the Time Base and disable DMA transfer.
  232. @endverbatim
  233. * @{
  234. */
  235. /**
  236. * @brief Initializes the TIM Time base Unit according to the specified
  237. * parameters in the TIM_HandleTypeDef and initialize the associated handle.
  238. * @note Switching from Center Aligned counter mode to Edge counter mode (or reverse)
  239. * requires a timer reset to avoid unexpected direction
  240. * due to DIR bit readonly in center aligned mode.
  241. * Ex: call @ref HAL_TIM_Base_DeInit() before HAL_TIM_Base_Init()
  242. * @param htim TIM Base handle
  243. * @retval HAL status
  244. */
  245. HAL_StatusTypeDef HAL_TIM_Base_Init(TIM_HandleTypeDef *htim)
  246. {
  247. /* Check the TIM handle allocation */
  248. if (htim == NULL)
  249. {
  250. return HAL_ERROR;
  251. }
  252. /* Check the parameters */
  253. assert_param(IS_TIM_INSTANCE(htim->Instance));
  254. assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
  255. assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
  256. assert_param(IS_TIM_PERIOD(htim->Init.Period));
  257. assert_param(IS_TIM_PRESCALER(htim->Init.Prescaler));
  258. assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
  259. if (htim->State == HAL_TIM_STATE_RESET)
  260. {
  261. /* Allocate lock resource and initialize it */
  262. htim->Lock = HAL_UNLOCKED;
  263. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  264. /* Reset interrupt callbacks to legacy weak callbacks */
  265. TIM_ResetCallback(htim);
  266. if (htim->Base_MspInitCallback == NULL)
  267. {
  268. htim->Base_MspInitCallback = HAL_TIM_Base_MspInit;
  269. }
  270. /* Init the low level hardware : GPIO, CLOCK, NVIC */
  271. htim->Base_MspInitCallback(htim);
  272. #else
  273. /* Init the low level hardware : GPIO, CLOCK, NVIC */
  274. HAL_TIM_Base_MspInit(htim);
  275. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  276. }
  277. /* Set the TIM state */
  278. htim->State = HAL_TIM_STATE_BUSY;
  279. /* Set the Time Base configuration */
  280. TIM_Base_SetConfig(htim->Instance, &htim->Init);
  281. /* Initialize the DMA burst operation state */
  282. htim->DMABurstState = HAL_DMA_BURST_STATE_READY;
  283. /* Initialize the TIM channels state */
  284. TIM_CHANNEL_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_READY);
  285. /* Initialize the TIM state*/
  286. htim->State = HAL_TIM_STATE_READY;
  287. return HAL_OK;
  288. }
  289. /**
  290. * @brief DeInitializes the TIM Base peripheral
  291. * @param htim TIM Base handle
  292. * @retval HAL status
  293. */
  294. HAL_StatusTypeDef HAL_TIM_Base_DeInit(TIM_HandleTypeDef *htim)
  295. {
  296. /* Check the parameters */
  297. assert_param(IS_TIM_INSTANCE(htim->Instance));
  298. htim->State = HAL_TIM_STATE_BUSY;
  299. /* Disable the TIM Peripheral Clock */
  300. __HAL_TIM_DISABLE(htim);
  301. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  302. if (htim->Base_MspDeInitCallback == NULL)
  303. {
  304. htim->Base_MspDeInitCallback = HAL_TIM_Base_MspDeInit;
  305. }
  306. /* DeInit the low level hardware */
  307. htim->Base_MspDeInitCallback(htim);
  308. #else
  309. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  310. HAL_TIM_Base_MspDeInit(htim);
  311. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  312. /* Change the DMA burst operation state */
  313. htim->DMABurstState = HAL_DMA_BURST_STATE_RESET;
  314. /* Change the TIM channels state */
  315. TIM_CHANNEL_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_RESET);
  316. /* Change TIM state */
  317. htim->State = HAL_TIM_STATE_RESET;
  318. /* Release Lock */
  319. __HAL_UNLOCK(htim);
  320. return HAL_OK;
  321. }
  322. /**
  323. * @brief Initializes the TIM Base MSP.
  324. * @param htim TIM Base handle
  325. * @retval None
  326. */
  327. __weak void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim)
  328. {
  329. /* Prevent unused argument(s) compilation warning */
  330. UNUSED(htim);
  331. /* NOTE : This function should not be modified, when the callback is needed,
  332. the HAL_TIM_Base_MspInit could be implemented in the user file
  333. */
  334. }
  335. /**
  336. * @brief DeInitializes TIM Base MSP.
  337. * @param htim TIM Base handle
  338. * @retval None
  339. */
  340. __weak void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef *htim)
  341. {
  342. /* Prevent unused argument(s) compilation warning */
  343. UNUSED(htim);
  344. /* NOTE : This function should not be modified, when the callback is needed,
  345. the HAL_TIM_Base_MspDeInit could be implemented in the user file
  346. */
  347. }
  348. /**
  349. * @brief Starts the TIM Base generation.
  350. * @param htim TIM Base handle
  351. * @retval HAL status
  352. */
  353. HAL_StatusTypeDef HAL_TIM_Base_Start(TIM_HandleTypeDef *htim)
  354. {
  355. uint32_t tmpsmcr;
  356. /* Check the parameters */
  357. assert_param(IS_TIM_INSTANCE(htim->Instance));
  358. /* Check the TIM state */
  359. if (htim->State != HAL_TIM_STATE_READY)
  360. {
  361. return HAL_ERROR;
  362. }
  363. /* Set the TIM state */
  364. htim->State = HAL_TIM_STATE_BUSY;
  365. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  366. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  367. {
  368. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  369. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  370. {
  371. __HAL_TIM_ENABLE(htim);
  372. }
  373. }
  374. else
  375. {
  376. __HAL_TIM_ENABLE(htim);
  377. }
  378. /* Return function status */
  379. return HAL_OK;
  380. }
  381. /**
  382. * @brief Stops the TIM Base generation.
  383. * @param htim TIM Base handle
  384. * @retval HAL status
  385. */
  386. HAL_StatusTypeDef HAL_TIM_Base_Stop(TIM_HandleTypeDef *htim)
  387. {
  388. /* Check the parameters */
  389. assert_param(IS_TIM_INSTANCE(htim->Instance));
  390. /* Disable the Peripheral */
  391. __HAL_TIM_DISABLE(htim);
  392. /* Set the TIM state */
  393. htim->State = HAL_TIM_STATE_READY;
  394. /* Return function status */
  395. return HAL_OK;
  396. }
  397. /**
  398. * @brief Starts the TIM Base generation in interrupt mode.
  399. * @param htim TIM Base handle
  400. * @retval HAL status
  401. */
  402. HAL_StatusTypeDef HAL_TIM_Base_Start_IT(TIM_HandleTypeDef *htim)
  403. {
  404. uint32_t tmpsmcr;
  405. /* Check the parameters */
  406. assert_param(IS_TIM_INSTANCE(htim->Instance));
  407. /* Check the TIM state */
  408. if (htim->State != HAL_TIM_STATE_READY)
  409. {
  410. return HAL_ERROR;
  411. }
  412. /* Set the TIM state */
  413. htim->State = HAL_TIM_STATE_BUSY;
  414. /* Enable the TIM Update interrupt */
  415. __HAL_TIM_ENABLE_IT(htim, TIM_IT_UPDATE);
  416. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  417. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  418. {
  419. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  420. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  421. {
  422. __HAL_TIM_ENABLE(htim);
  423. }
  424. }
  425. else
  426. {
  427. __HAL_TIM_ENABLE(htim);
  428. }
  429. /* Return function status */
  430. return HAL_OK;
  431. }
  432. /**
  433. * @brief Stops the TIM Base generation in interrupt mode.
  434. * @param htim TIM Base handle
  435. * @retval HAL status
  436. */
  437. HAL_StatusTypeDef HAL_TIM_Base_Stop_IT(TIM_HandleTypeDef *htim)
  438. {
  439. /* Check the parameters */
  440. assert_param(IS_TIM_INSTANCE(htim->Instance));
  441. /* Disable the TIM Update interrupt */
  442. __HAL_TIM_DISABLE_IT(htim, TIM_IT_UPDATE);
  443. /* Disable the Peripheral */
  444. __HAL_TIM_DISABLE(htim);
  445. /* Set the TIM state */
  446. htim->State = HAL_TIM_STATE_READY;
  447. /* Return function status */
  448. return HAL_OK;
  449. }
  450. /**
  451. * @brief Starts the TIM Base generation in DMA mode.
  452. * @param htim TIM Base handle
  453. * @param pData The source Buffer address.
  454. * @param Length The length of data to be transferred from memory to peripheral.
  455. * @retval HAL status
  456. */
  457. HAL_StatusTypeDef HAL_TIM_Base_Start_DMA(TIM_HandleTypeDef *htim, const uint32_t *pData, uint16_t Length)
  458. {
  459. uint32_t tmpsmcr;
  460. /* Check the parameters */
  461. assert_param(IS_TIM_DMA_INSTANCE(htim->Instance));
  462. /* Set the TIM state */
  463. if (htim->State == HAL_TIM_STATE_BUSY)
  464. {
  465. return HAL_BUSY;
  466. }
  467. else if (htim->State == HAL_TIM_STATE_READY)
  468. {
  469. if ((pData == NULL) || (Length == 0U))
  470. {
  471. return HAL_ERROR;
  472. }
  473. else
  474. {
  475. htim->State = HAL_TIM_STATE_BUSY;
  476. }
  477. }
  478. else
  479. {
  480. return HAL_ERROR;
  481. }
  482. /* Set the DMA Period elapsed callbacks */
  483. htim->hdma[TIM_DMA_ID_UPDATE]->XferCpltCallback = TIM_DMAPeriodElapsedCplt;
  484. htim->hdma[TIM_DMA_ID_UPDATE]->XferHalfCpltCallback = TIM_DMAPeriodElapsedHalfCplt;
  485. /* Set the DMA error callback */
  486. htim->hdma[TIM_DMA_ID_UPDATE]->XferErrorCallback = TIM_DMAError ;
  487. /* Enable the DMA channel */
  488. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_UPDATE], (uint32_t)pData, (uint32_t)&htim->Instance->ARR,
  489. Length) != HAL_OK)
  490. {
  491. /* Return error status */
  492. return HAL_ERROR;
  493. }
  494. /* Enable the TIM Update DMA request */
  495. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_UPDATE);
  496. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  497. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  498. {
  499. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  500. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  501. {
  502. __HAL_TIM_ENABLE(htim);
  503. }
  504. }
  505. else
  506. {
  507. __HAL_TIM_ENABLE(htim);
  508. }
  509. /* Return function status */
  510. return HAL_OK;
  511. }
  512. /**
  513. * @brief Stops the TIM Base generation in DMA mode.
  514. * @param htim TIM Base handle
  515. * @retval HAL status
  516. */
  517. HAL_StatusTypeDef HAL_TIM_Base_Stop_DMA(TIM_HandleTypeDef *htim)
  518. {
  519. /* Check the parameters */
  520. assert_param(IS_TIM_DMA_INSTANCE(htim->Instance));
  521. /* Disable the TIM Update DMA request */
  522. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_UPDATE);
  523. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_UPDATE]);
  524. /* Disable the Peripheral */
  525. __HAL_TIM_DISABLE(htim);
  526. /* Set the TIM state */
  527. htim->State = HAL_TIM_STATE_READY;
  528. /* Return function status */
  529. return HAL_OK;
  530. }
  531. /**
  532. * @}
  533. */
  534. /** @defgroup TIM_Exported_Functions_Group2 TIM Output Compare functions
  535. * @brief TIM Output Compare functions
  536. *
  537. @verbatim
  538. ==============================================================================
  539. ##### TIM Output Compare functions #####
  540. ==============================================================================
  541. [..]
  542. This section provides functions allowing to:
  543. (+) Initialize and configure the TIM Output Compare.
  544. (+) De-initialize the TIM Output Compare.
  545. (+) Start the TIM Output Compare.
  546. (+) Stop the TIM Output Compare.
  547. (+) Start the TIM Output Compare and enable interrupt.
  548. (+) Stop the TIM Output Compare and disable interrupt.
  549. (+) Start the TIM Output Compare and enable DMA transfer.
  550. (+) Stop the TIM Output Compare and disable DMA transfer.
  551. @endverbatim
  552. * @{
  553. */
  554. /**
  555. * @brief Initializes the TIM Output Compare according to the specified
  556. * parameters in the TIM_HandleTypeDef and initializes the associated handle.
  557. * @note Switching from Center Aligned counter mode to Edge counter mode (or reverse)
  558. * requires a timer reset to avoid unexpected direction
  559. * due to DIR bit readonly in center aligned mode.
  560. * Ex: call @ref HAL_TIM_OC_DeInit() before HAL_TIM_OC_Init()
  561. * @param htim TIM Output Compare handle
  562. * @retval HAL status
  563. */
  564. HAL_StatusTypeDef HAL_TIM_OC_Init(TIM_HandleTypeDef *htim)
  565. {
  566. /* Check the TIM handle allocation */
  567. if (htim == NULL)
  568. {
  569. return HAL_ERROR;
  570. }
  571. /* Check the parameters */
  572. assert_param(IS_TIM_INSTANCE(htim->Instance));
  573. assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
  574. assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
  575. assert_param(IS_TIM_PERIOD(htim->Init.Period));
  576. assert_param(IS_TIM_PRESCALER(htim->Init.Prescaler));
  577. assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
  578. if (htim->State == HAL_TIM_STATE_RESET)
  579. {
  580. /* Allocate lock resource and initialize it */
  581. htim->Lock = HAL_UNLOCKED;
  582. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  583. /* Reset interrupt callbacks to legacy weak callbacks */
  584. TIM_ResetCallback(htim);
  585. if (htim->OC_MspInitCallback == NULL)
  586. {
  587. htim->OC_MspInitCallback = HAL_TIM_OC_MspInit;
  588. }
  589. /* Init the low level hardware : GPIO, CLOCK, NVIC */
  590. htim->OC_MspInitCallback(htim);
  591. #else
  592. /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
  593. HAL_TIM_OC_MspInit(htim);
  594. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  595. }
  596. /* Set the TIM state */
  597. htim->State = HAL_TIM_STATE_BUSY;
  598. /* Init the base time for the Output Compare */
  599. TIM_Base_SetConfig(htim->Instance, &htim->Init);
  600. /* Initialize the DMA burst operation state */
  601. htim->DMABurstState = HAL_DMA_BURST_STATE_READY;
  602. /* Initialize the TIM channels state */
  603. TIM_CHANNEL_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_READY);
  604. /* Initialize the TIM state*/
  605. htim->State = HAL_TIM_STATE_READY;
  606. return HAL_OK;
  607. }
  608. /**
  609. * @brief DeInitializes the TIM peripheral
  610. * @param htim TIM Output Compare handle
  611. * @retval HAL status
  612. */
  613. HAL_StatusTypeDef HAL_TIM_OC_DeInit(TIM_HandleTypeDef *htim)
  614. {
  615. /* Check the parameters */
  616. assert_param(IS_TIM_INSTANCE(htim->Instance));
  617. htim->State = HAL_TIM_STATE_BUSY;
  618. /* Disable the TIM Peripheral Clock */
  619. __HAL_TIM_DISABLE(htim);
  620. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  621. if (htim->OC_MspDeInitCallback == NULL)
  622. {
  623. htim->OC_MspDeInitCallback = HAL_TIM_OC_MspDeInit;
  624. }
  625. /* DeInit the low level hardware */
  626. htim->OC_MspDeInitCallback(htim);
  627. #else
  628. /* DeInit the low level hardware: GPIO, CLOCK, NVIC and DMA */
  629. HAL_TIM_OC_MspDeInit(htim);
  630. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  631. /* Change the DMA burst operation state */
  632. htim->DMABurstState = HAL_DMA_BURST_STATE_RESET;
  633. /* Change the TIM channels state */
  634. TIM_CHANNEL_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_RESET);
  635. /* Change TIM state */
  636. htim->State = HAL_TIM_STATE_RESET;
  637. /* Release Lock */
  638. __HAL_UNLOCK(htim);
  639. return HAL_OK;
  640. }
  641. /**
  642. * @brief Initializes the TIM Output Compare MSP.
  643. * @param htim TIM Output Compare handle
  644. * @retval None
  645. */
  646. __weak void HAL_TIM_OC_MspInit(TIM_HandleTypeDef *htim)
  647. {
  648. /* Prevent unused argument(s) compilation warning */
  649. UNUSED(htim);
  650. /* NOTE : This function should not be modified, when the callback is needed,
  651. the HAL_TIM_OC_MspInit could be implemented in the user file
  652. */
  653. }
  654. /**
  655. * @brief DeInitializes TIM Output Compare MSP.
  656. * @param htim TIM Output Compare handle
  657. * @retval None
  658. */
  659. __weak void HAL_TIM_OC_MspDeInit(TIM_HandleTypeDef *htim)
  660. {
  661. /* Prevent unused argument(s) compilation warning */
  662. UNUSED(htim);
  663. /* NOTE : This function should not be modified, when the callback is needed,
  664. the HAL_TIM_OC_MspDeInit could be implemented in the user file
  665. */
  666. }
  667. /**
  668. * @brief Starts the TIM Output Compare signal generation.
  669. * @param htim TIM Output Compare handle
  670. * @param Channel TIM Channel to be enabled
  671. * This parameter can be one of the following values:
  672. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  673. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  674. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  675. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  676. * @retval HAL status
  677. */
  678. HAL_StatusTypeDef HAL_TIM_OC_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
  679. {
  680. uint32_t tmpsmcr;
  681. /* Check the parameters */
  682. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  683. /* Check the TIM channel state */
  684. if (TIM_CHANNEL_STATE_GET(htim, Channel) != HAL_TIM_CHANNEL_STATE_READY)
  685. {
  686. return HAL_ERROR;
  687. }
  688. /* Set the TIM channel state */
  689. TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
  690. /* Enable the Output compare channel */
  691. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
  692. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  693. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  694. {
  695. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  696. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  697. {
  698. __HAL_TIM_ENABLE(htim);
  699. }
  700. }
  701. else
  702. {
  703. __HAL_TIM_ENABLE(htim);
  704. }
  705. /* Return function status */
  706. return HAL_OK;
  707. }
  708. /**
  709. * @brief Stops the TIM Output Compare signal generation.
  710. * @param htim TIM Output Compare handle
  711. * @param Channel TIM Channel to be disabled
  712. * This parameter can be one of the following values:
  713. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  714. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  715. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  716. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  717. * @retval HAL status
  718. */
  719. HAL_StatusTypeDef HAL_TIM_OC_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
  720. {
  721. /* Check the parameters */
  722. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  723. /* Disable the Output compare channel */
  724. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
  725. /* Disable the Peripheral */
  726. __HAL_TIM_DISABLE(htim);
  727. /* Set the TIM channel state */
  728. TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
  729. /* Return function status */
  730. return HAL_OK;
  731. }
  732. /**
  733. * @brief Starts the TIM Output Compare signal generation in interrupt mode.
  734. * @param htim TIM Output Compare handle
  735. * @param Channel TIM Channel to be enabled
  736. * This parameter can be one of the following values:
  737. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  738. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  739. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  740. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  741. * @retval HAL status
  742. */
  743. HAL_StatusTypeDef HAL_TIM_OC_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  744. {
  745. HAL_StatusTypeDef status = HAL_OK;
  746. uint32_t tmpsmcr;
  747. /* Check the parameters */
  748. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  749. /* Check the TIM channel state */
  750. if (TIM_CHANNEL_STATE_GET(htim, Channel) != HAL_TIM_CHANNEL_STATE_READY)
  751. {
  752. return HAL_ERROR;
  753. }
  754. /* Set the TIM channel state */
  755. TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
  756. switch (Channel)
  757. {
  758. case TIM_CHANNEL_1:
  759. {
  760. /* Enable the TIM Capture/Compare 1 interrupt */
  761. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  762. break;
  763. }
  764. case TIM_CHANNEL_2:
  765. {
  766. /* Enable the TIM Capture/Compare 2 interrupt */
  767. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  768. break;
  769. }
  770. case TIM_CHANNEL_3:
  771. {
  772. /* Enable the TIM Capture/Compare 3 interrupt */
  773. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
  774. break;
  775. }
  776. case TIM_CHANNEL_4:
  777. {
  778. /* Enable the TIM Capture/Compare 4 interrupt */
  779. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
  780. break;
  781. }
  782. default:
  783. status = HAL_ERROR;
  784. break;
  785. }
  786. if (status == HAL_OK)
  787. {
  788. /* Enable the Output compare channel */
  789. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
  790. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  791. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  792. {
  793. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  794. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  795. {
  796. __HAL_TIM_ENABLE(htim);
  797. }
  798. }
  799. else
  800. {
  801. __HAL_TIM_ENABLE(htim);
  802. }
  803. }
  804. /* Return function status */
  805. return status;
  806. }
  807. /**
  808. * @brief Stops the TIM Output Compare signal generation in interrupt mode.
  809. * @param htim TIM Output Compare handle
  810. * @param Channel TIM Channel to be disabled
  811. * This parameter can be one of the following values:
  812. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  813. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  814. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  815. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  816. * @retval HAL status
  817. */
  818. HAL_StatusTypeDef HAL_TIM_OC_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  819. {
  820. HAL_StatusTypeDef status = HAL_OK;
  821. /* Check the parameters */
  822. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  823. switch (Channel)
  824. {
  825. case TIM_CHANNEL_1:
  826. {
  827. /* Disable the TIM Capture/Compare 1 interrupt */
  828. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  829. break;
  830. }
  831. case TIM_CHANNEL_2:
  832. {
  833. /* Disable the TIM Capture/Compare 2 interrupt */
  834. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  835. break;
  836. }
  837. case TIM_CHANNEL_3:
  838. {
  839. /* Disable the TIM Capture/Compare 3 interrupt */
  840. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
  841. break;
  842. }
  843. case TIM_CHANNEL_4:
  844. {
  845. /* Disable the TIM Capture/Compare 4 interrupt */
  846. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
  847. break;
  848. }
  849. default:
  850. status = HAL_ERROR;
  851. break;
  852. }
  853. if (status == HAL_OK)
  854. {
  855. /* Disable the Output compare channel */
  856. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
  857. /* Disable the Peripheral */
  858. __HAL_TIM_DISABLE(htim);
  859. /* Set the TIM channel state */
  860. TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
  861. }
  862. /* Return function status */
  863. return status;
  864. }
  865. /**
  866. * @brief Starts the TIM Output Compare signal generation in DMA mode.
  867. * @param htim TIM Output Compare handle
  868. * @param Channel TIM Channel to be enabled
  869. * This parameter can be one of the following values:
  870. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  871. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  872. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  873. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  874. * @param pData The source Buffer address.
  875. * @param Length The length of data to be transferred from memory to TIM peripheral
  876. * @retval HAL status
  877. */
  878. HAL_StatusTypeDef HAL_TIM_OC_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, const uint32_t *pData,
  879. uint16_t Length)
  880. {
  881. HAL_StatusTypeDef status = HAL_OK;
  882. uint32_t tmpsmcr;
  883. /* Check the parameters */
  884. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  885. /* Set the TIM channel state */
  886. if (TIM_CHANNEL_STATE_GET(htim, Channel) == HAL_TIM_CHANNEL_STATE_BUSY)
  887. {
  888. return HAL_BUSY;
  889. }
  890. else if (TIM_CHANNEL_STATE_GET(htim, Channel) == HAL_TIM_CHANNEL_STATE_READY)
  891. {
  892. if ((pData == NULL) || (Length == 0U))
  893. {
  894. return HAL_ERROR;
  895. }
  896. else
  897. {
  898. TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
  899. }
  900. }
  901. else
  902. {
  903. return HAL_ERROR;
  904. }
  905. switch (Channel)
  906. {
  907. case TIM_CHANNEL_1:
  908. {
  909. /* Set the DMA compare callbacks */
  910. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseCplt;
  911. htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  912. /* Set the DMA error callback */
  913. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  914. /* Enable the DMA channel */
  915. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1,
  916. Length) != HAL_OK)
  917. {
  918. /* Return error status */
  919. return HAL_ERROR;
  920. }
  921. /* Enable the TIM Capture/Compare 1 DMA request */
  922. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  923. break;
  924. }
  925. case TIM_CHANNEL_2:
  926. {
  927. /* Set the DMA compare callbacks */
  928. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseCplt;
  929. htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  930. /* Set the DMA error callback */
  931. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
  932. /* Enable the DMA channel */
  933. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2,
  934. Length) != HAL_OK)
  935. {
  936. /* Return error status */
  937. return HAL_ERROR;
  938. }
  939. /* Enable the TIM Capture/Compare 2 DMA request */
  940. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
  941. break;
  942. }
  943. case TIM_CHANNEL_3:
  944. {
  945. /* Set the DMA compare callbacks */
  946. htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseCplt;
  947. htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  948. /* Set the DMA error callback */
  949. htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
  950. /* Enable the DMA channel */
  951. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3,
  952. Length) != HAL_OK)
  953. {
  954. /* Return error status */
  955. return HAL_ERROR;
  956. }
  957. /* Enable the TIM Capture/Compare 3 DMA request */
  958. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
  959. break;
  960. }
  961. case TIM_CHANNEL_4:
  962. {
  963. /* Set the DMA compare callbacks */
  964. htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMADelayPulseCplt;
  965. htim->hdma[TIM_DMA_ID_CC4]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  966. /* Set the DMA error callback */
  967. htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
  968. /* Enable the DMA channel */
  969. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)pData, (uint32_t)&htim->Instance->CCR4,
  970. Length) != HAL_OK)
  971. {
  972. /* Return error status */
  973. return HAL_ERROR;
  974. }
  975. /* Enable the TIM Capture/Compare 4 DMA request */
  976. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);
  977. break;
  978. }
  979. default:
  980. status = HAL_ERROR;
  981. break;
  982. }
  983. if (status == HAL_OK)
  984. {
  985. /* Enable the Output compare channel */
  986. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
  987. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  988. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  989. {
  990. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  991. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  992. {
  993. __HAL_TIM_ENABLE(htim);
  994. }
  995. }
  996. else
  997. {
  998. __HAL_TIM_ENABLE(htim);
  999. }
  1000. }
  1001. /* Return function status */
  1002. return status;
  1003. }
  1004. /**
  1005. * @brief Stops the TIM Output Compare signal generation in DMA mode.
  1006. * @param htim TIM Output Compare handle
  1007. * @param Channel TIM Channel to be disabled
  1008. * This parameter can be one of the following values:
  1009. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1010. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1011. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1012. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1013. * @retval HAL status
  1014. */
  1015. HAL_StatusTypeDef HAL_TIM_OC_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
  1016. {
  1017. HAL_StatusTypeDef status = HAL_OK;
  1018. /* Check the parameters */
  1019. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1020. switch (Channel)
  1021. {
  1022. case TIM_CHANNEL_1:
  1023. {
  1024. /* Disable the TIM Capture/Compare 1 DMA request */
  1025. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  1026. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
  1027. break;
  1028. }
  1029. case TIM_CHANNEL_2:
  1030. {
  1031. /* Disable the TIM Capture/Compare 2 DMA request */
  1032. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
  1033. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
  1034. break;
  1035. }
  1036. case TIM_CHANNEL_3:
  1037. {
  1038. /* Disable the TIM Capture/Compare 3 DMA request */
  1039. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
  1040. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
  1041. break;
  1042. }
  1043. case TIM_CHANNEL_4:
  1044. {
  1045. /* Disable the TIM Capture/Compare 4 interrupt */
  1046. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);
  1047. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC4]);
  1048. break;
  1049. }
  1050. default:
  1051. status = HAL_ERROR;
  1052. break;
  1053. }
  1054. if (status == HAL_OK)
  1055. {
  1056. /* Disable the Output compare channel */
  1057. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
  1058. /* Disable the Peripheral */
  1059. __HAL_TIM_DISABLE(htim);
  1060. /* Set the TIM channel state */
  1061. TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
  1062. }
  1063. /* Return function status */
  1064. return status;
  1065. }
  1066. /**
  1067. * @}
  1068. */
  1069. /** @defgroup TIM_Exported_Functions_Group3 TIM PWM functions
  1070. * @brief TIM PWM functions
  1071. *
  1072. @verbatim
  1073. ==============================================================================
  1074. ##### TIM PWM functions #####
  1075. ==============================================================================
  1076. [..]
  1077. This section provides functions allowing to:
  1078. (+) Initialize and configure the TIM PWM.
  1079. (+) De-initialize the TIM PWM.
  1080. (+) Start the TIM PWM.
  1081. (+) Stop the TIM PWM.
  1082. (+) Start the TIM PWM and enable interrupt.
  1083. (+) Stop the TIM PWM and disable interrupt.
  1084. (+) Start the TIM PWM and enable DMA transfer.
  1085. (+) Stop the TIM PWM and disable DMA transfer.
  1086. @endverbatim
  1087. * @{
  1088. */
  1089. /**
  1090. * @brief Initializes the TIM PWM Time Base according to the specified
  1091. * parameters in the TIM_HandleTypeDef and initializes the associated handle.
  1092. * @note Switching from Center Aligned counter mode to Edge counter mode (or reverse)
  1093. * requires a timer reset to avoid unexpected direction
  1094. * due to DIR bit readonly in center aligned mode.
  1095. * Ex: call @ref HAL_TIM_PWM_DeInit() before HAL_TIM_PWM_Init()
  1096. * @param htim TIM PWM handle
  1097. * @retval HAL status
  1098. */
  1099. HAL_StatusTypeDef HAL_TIM_PWM_Init(TIM_HandleTypeDef *htim)
  1100. {
  1101. /* Check the TIM handle allocation */
  1102. if (htim == NULL)
  1103. {
  1104. return HAL_ERROR;
  1105. }
  1106. /* Check the parameters */
  1107. assert_param(IS_TIM_INSTANCE(htim->Instance));
  1108. assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
  1109. assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
  1110. assert_param(IS_TIM_PERIOD(htim->Init.Period));
  1111. assert_param(IS_TIM_PRESCALER(htim->Init.Prescaler));
  1112. assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
  1113. if (htim->State == HAL_TIM_STATE_RESET)
  1114. {
  1115. /* Allocate lock resource and initialize it */
  1116. htim->Lock = HAL_UNLOCKED;
  1117. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  1118. /* Reset interrupt callbacks to legacy weak callbacks */
  1119. TIM_ResetCallback(htim);
  1120. if (htim->PWM_MspInitCallback == NULL)
  1121. {
  1122. htim->PWM_MspInitCallback = HAL_TIM_PWM_MspInit;
  1123. }
  1124. /* Init the low level hardware : GPIO, CLOCK, NVIC */
  1125. htim->PWM_MspInitCallback(htim);
  1126. #else
  1127. /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
  1128. HAL_TIM_PWM_MspInit(htim);
  1129. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  1130. }
  1131. /* Set the TIM state */
  1132. htim->State = HAL_TIM_STATE_BUSY;
  1133. /* Init the base time for the PWM */
  1134. TIM_Base_SetConfig(htim->Instance, &htim->Init);
  1135. /* Initialize the DMA burst operation state */
  1136. htim->DMABurstState = HAL_DMA_BURST_STATE_READY;
  1137. /* Initialize the TIM channels state */
  1138. TIM_CHANNEL_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_READY);
  1139. /* Initialize the TIM state*/
  1140. htim->State = HAL_TIM_STATE_READY;
  1141. return HAL_OK;
  1142. }
  1143. /**
  1144. * @brief DeInitializes the TIM peripheral
  1145. * @param htim TIM PWM handle
  1146. * @retval HAL status
  1147. */
  1148. HAL_StatusTypeDef HAL_TIM_PWM_DeInit(TIM_HandleTypeDef *htim)
  1149. {
  1150. /* Check the parameters */
  1151. assert_param(IS_TIM_INSTANCE(htim->Instance));
  1152. htim->State = HAL_TIM_STATE_BUSY;
  1153. /* Disable the TIM Peripheral Clock */
  1154. __HAL_TIM_DISABLE(htim);
  1155. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  1156. if (htim->PWM_MspDeInitCallback == NULL)
  1157. {
  1158. htim->PWM_MspDeInitCallback = HAL_TIM_PWM_MspDeInit;
  1159. }
  1160. /* DeInit the low level hardware */
  1161. htim->PWM_MspDeInitCallback(htim);
  1162. #else
  1163. /* DeInit the low level hardware: GPIO, CLOCK, NVIC and DMA */
  1164. HAL_TIM_PWM_MspDeInit(htim);
  1165. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  1166. /* Change the DMA burst operation state */
  1167. htim->DMABurstState = HAL_DMA_BURST_STATE_RESET;
  1168. /* Change the TIM channels state */
  1169. TIM_CHANNEL_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_RESET);
  1170. /* Change TIM state */
  1171. htim->State = HAL_TIM_STATE_RESET;
  1172. /* Release Lock */
  1173. __HAL_UNLOCK(htim);
  1174. return HAL_OK;
  1175. }
  1176. /**
  1177. * @brief Initializes the TIM PWM MSP.
  1178. * @param htim TIM PWM handle
  1179. * @retval None
  1180. */
  1181. __weak void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef *htim)
  1182. {
  1183. /* Prevent unused argument(s) compilation warning */
  1184. UNUSED(htim);
  1185. /* NOTE : This function should not be modified, when the callback is needed,
  1186. the HAL_TIM_PWM_MspInit could be implemented in the user file
  1187. */
  1188. }
  1189. /**
  1190. * @brief DeInitializes TIM PWM MSP.
  1191. * @param htim TIM PWM handle
  1192. * @retval None
  1193. */
  1194. __weak void HAL_TIM_PWM_MspDeInit(TIM_HandleTypeDef *htim)
  1195. {
  1196. /* Prevent unused argument(s) compilation warning */
  1197. UNUSED(htim);
  1198. /* NOTE : This function should not be modified, when the callback is needed,
  1199. the HAL_TIM_PWM_MspDeInit could be implemented in the user file
  1200. */
  1201. }
  1202. /**
  1203. * @brief Starts the PWM signal generation.
  1204. * @param htim TIM handle
  1205. * @param Channel TIM Channels to be enabled
  1206. * This parameter can be one of the following values:
  1207. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1208. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1209. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1210. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1211. * @retval HAL status
  1212. */
  1213. HAL_StatusTypeDef HAL_TIM_PWM_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
  1214. {
  1215. uint32_t tmpsmcr;
  1216. /* Check the parameters */
  1217. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1218. /* Check the TIM channel state */
  1219. if (TIM_CHANNEL_STATE_GET(htim, Channel) != HAL_TIM_CHANNEL_STATE_READY)
  1220. {
  1221. return HAL_ERROR;
  1222. }
  1223. /* Set the TIM channel state */
  1224. TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
  1225. /* Enable the Capture compare channel */
  1226. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
  1227. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  1228. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  1229. {
  1230. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  1231. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  1232. {
  1233. __HAL_TIM_ENABLE(htim);
  1234. }
  1235. }
  1236. else
  1237. {
  1238. __HAL_TIM_ENABLE(htim);
  1239. }
  1240. /* Return function status */
  1241. return HAL_OK;
  1242. }
  1243. /**
  1244. * @brief Stops the PWM signal generation.
  1245. * @param htim TIM PWM handle
  1246. * @param Channel TIM Channels to be disabled
  1247. * This parameter can be one of the following values:
  1248. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1249. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1250. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1251. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1252. * @retval HAL status
  1253. */
  1254. HAL_StatusTypeDef HAL_TIM_PWM_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
  1255. {
  1256. /* Check the parameters */
  1257. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1258. /* Disable the Capture compare channel */
  1259. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
  1260. /* Disable the Peripheral */
  1261. __HAL_TIM_DISABLE(htim);
  1262. /* Set the TIM channel state */
  1263. TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
  1264. /* Return function status */
  1265. return HAL_OK;
  1266. }
  1267. /**
  1268. * @brief Starts the PWM signal generation in interrupt mode.
  1269. * @param htim TIM PWM handle
  1270. * @param Channel TIM Channel to be enabled
  1271. * This parameter can be one of the following values:
  1272. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1273. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1274. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1275. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1276. * @retval HAL status
  1277. */
  1278. HAL_StatusTypeDef HAL_TIM_PWM_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  1279. {
  1280. HAL_StatusTypeDef status = HAL_OK;
  1281. uint32_t tmpsmcr;
  1282. /* Check the parameters */
  1283. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1284. /* Check the TIM channel state */
  1285. if (TIM_CHANNEL_STATE_GET(htim, Channel) != HAL_TIM_CHANNEL_STATE_READY)
  1286. {
  1287. return HAL_ERROR;
  1288. }
  1289. /* Set the TIM channel state */
  1290. TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
  1291. switch (Channel)
  1292. {
  1293. case TIM_CHANNEL_1:
  1294. {
  1295. /* Enable the TIM Capture/Compare 1 interrupt */
  1296. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  1297. break;
  1298. }
  1299. case TIM_CHANNEL_2:
  1300. {
  1301. /* Enable the TIM Capture/Compare 2 interrupt */
  1302. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  1303. break;
  1304. }
  1305. case TIM_CHANNEL_3:
  1306. {
  1307. /* Enable the TIM Capture/Compare 3 interrupt */
  1308. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
  1309. break;
  1310. }
  1311. case TIM_CHANNEL_4:
  1312. {
  1313. /* Enable the TIM Capture/Compare 4 interrupt */
  1314. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
  1315. break;
  1316. }
  1317. default:
  1318. status = HAL_ERROR;
  1319. break;
  1320. }
  1321. if (status == HAL_OK)
  1322. {
  1323. /* Enable the Capture compare channel */
  1324. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
  1325. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  1326. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  1327. {
  1328. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  1329. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  1330. {
  1331. __HAL_TIM_ENABLE(htim);
  1332. }
  1333. }
  1334. else
  1335. {
  1336. __HAL_TIM_ENABLE(htim);
  1337. }
  1338. }
  1339. /* Return function status */
  1340. return status;
  1341. }
  1342. /**
  1343. * @brief Stops the PWM signal generation in interrupt mode.
  1344. * @param htim TIM PWM handle
  1345. * @param Channel TIM Channels to be disabled
  1346. * This parameter can be one of the following values:
  1347. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1348. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1349. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1350. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1351. * @retval HAL status
  1352. */
  1353. HAL_StatusTypeDef HAL_TIM_PWM_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  1354. {
  1355. HAL_StatusTypeDef status = HAL_OK;
  1356. /* Check the parameters */
  1357. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1358. switch (Channel)
  1359. {
  1360. case TIM_CHANNEL_1:
  1361. {
  1362. /* Disable the TIM Capture/Compare 1 interrupt */
  1363. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  1364. break;
  1365. }
  1366. case TIM_CHANNEL_2:
  1367. {
  1368. /* Disable the TIM Capture/Compare 2 interrupt */
  1369. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  1370. break;
  1371. }
  1372. case TIM_CHANNEL_3:
  1373. {
  1374. /* Disable the TIM Capture/Compare 3 interrupt */
  1375. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
  1376. break;
  1377. }
  1378. case TIM_CHANNEL_4:
  1379. {
  1380. /* Disable the TIM Capture/Compare 4 interrupt */
  1381. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
  1382. break;
  1383. }
  1384. default:
  1385. status = HAL_ERROR;
  1386. break;
  1387. }
  1388. if (status == HAL_OK)
  1389. {
  1390. /* Disable the Capture compare channel */
  1391. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
  1392. /* Disable the Peripheral */
  1393. __HAL_TIM_DISABLE(htim);
  1394. /* Set the TIM channel state */
  1395. TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
  1396. }
  1397. /* Return function status */
  1398. return status;
  1399. }
  1400. /**
  1401. * @brief Starts the TIM PWM signal generation in DMA mode.
  1402. * @param htim TIM PWM handle
  1403. * @param Channel TIM Channels to be enabled
  1404. * This parameter can be one of the following values:
  1405. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1406. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1407. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1408. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1409. * @param pData The source Buffer address.
  1410. * @param Length The length of data to be transferred from memory to TIM peripheral
  1411. * @retval HAL status
  1412. */
  1413. HAL_StatusTypeDef HAL_TIM_PWM_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, const uint32_t *pData,
  1414. uint16_t Length)
  1415. {
  1416. HAL_StatusTypeDef status = HAL_OK;
  1417. uint32_t tmpsmcr;
  1418. /* Check the parameters */
  1419. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1420. /* Set the TIM channel state */
  1421. if (TIM_CHANNEL_STATE_GET(htim, Channel) == HAL_TIM_CHANNEL_STATE_BUSY)
  1422. {
  1423. return HAL_BUSY;
  1424. }
  1425. else if (TIM_CHANNEL_STATE_GET(htim, Channel) == HAL_TIM_CHANNEL_STATE_READY)
  1426. {
  1427. if ((pData == NULL) || (Length == 0U))
  1428. {
  1429. return HAL_ERROR;
  1430. }
  1431. else
  1432. {
  1433. TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
  1434. }
  1435. }
  1436. else
  1437. {
  1438. return HAL_ERROR;
  1439. }
  1440. switch (Channel)
  1441. {
  1442. case TIM_CHANNEL_1:
  1443. {
  1444. /* Set the DMA compare callbacks */
  1445. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseCplt;
  1446. htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  1447. /* Set the DMA error callback */
  1448. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  1449. /* Enable the DMA channel */
  1450. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1,
  1451. Length) != HAL_OK)
  1452. {
  1453. /* Return error status */
  1454. return HAL_ERROR;
  1455. }
  1456. /* Enable the TIM Capture/Compare 1 DMA request */
  1457. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  1458. break;
  1459. }
  1460. case TIM_CHANNEL_2:
  1461. {
  1462. /* Set the DMA compare callbacks */
  1463. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseCplt;
  1464. htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  1465. /* Set the DMA error callback */
  1466. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
  1467. /* Enable the DMA channel */
  1468. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2,
  1469. Length) != HAL_OK)
  1470. {
  1471. /* Return error status */
  1472. return HAL_ERROR;
  1473. }
  1474. /* Enable the TIM Capture/Compare 2 DMA request */
  1475. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
  1476. break;
  1477. }
  1478. case TIM_CHANNEL_3:
  1479. {
  1480. /* Set the DMA compare callbacks */
  1481. htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseCplt;
  1482. htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  1483. /* Set the DMA error callback */
  1484. htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
  1485. /* Enable the DMA channel */
  1486. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3,
  1487. Length) != HAL_OK)
  1488. {
  1489. /* Return error status */
  1490. return HAL_ERROR;
  1491. }
  1492. /* Enable the TIM Output Capture/Compare 3 request */
  1493. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
  1494. break;
  1495. }
  1496. case TIM_CHANNEL_4:
  1497. {
  1498. /* Set the DMA compare callbacks */
  1499. htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMADelayPulseCplt;
  1500. htim->hdma[TIM_DMA_ID_CC4]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  1501. /* Set the DMA error callback */
  1502. htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
  1503. /* Enable the DMA channel */
  1504. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)pData, (uint32_t)&htim->Instance->CCR4,
  1505. Length) != HAL_OK)
  1506. {
  1507. /* Return error status */
  1508. return HAL_ERROR;
  1509. }
  1510. /* Enable the TIM Capture/Compare 4 DMA request */
  1511. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);
  1512. break;
  1513. }
  1514. default:
  1515. status = HAL_ERROR;
  1516. break;
  1517. }
  1518. if (status == HAL_OK)
  1519. {
  1520. /* Enable the Capture compare channel */
  1521. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
  1522. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  1523. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  1524. {
  1525. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  1526. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  1527. {
  1528. __HAL_TIM_ENABLE(htim);
  1529. }
  1530. }
  1531. else
  1532. {
  1533. __HAL_TIM_ENABLE(htim);
  1534. }
  1535. }
  1536. /* Return function status */
  1537. return status;
  1538. }
  1539. /**
  1540. * @brief Stops the TIM PWM signal generation in DMA mode.
  1541. * @param htim TIM PWM handle
  1542. * @param Channel TIM Channels to be disabled
  1543. * This parameter can be one of the following values:
  1544. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1545. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1546. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1547. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1548. * @retval HAL status
  1549. */
  1550. HAL_StatusTypeDef HAL_TIM_PWM_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
  1551. {
  1552. HAL_StatusTypeDef status = HAL_OK;
  1553. /* Check the parameters */
  1554. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1555. switch (Channel)
  1556. {
  1557. case TIM_CHANNEL_1:
  1558. {
  1559. /* Disable the TIM Capture/Compare 1 DMA request */
  1560. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  1561. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
  1562. break;
  1563. }
  1564. case TIM_CHANNEL_2:
  1565. {
  1566. /* Disable the TIM Capture/Compare 2 DMA request */
  1567. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
  1568. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
  1569. break;
  1570. }
  1571. case TIM_CHANNEL_3:
  1572. {
  1573. /* Disable the TIM Capture/Compare 3 DMA request */
  1574. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
  1575. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
  1576. break;
  1577. }
  1578. case TIM_CHANNEL_4:
  1579. {
  1580. /* Disable the TIM Capture/Compare 4 interrupt */
  1581. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);
  1582. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC4]);
  1583. break;
  1584. }
  1585. default:
  1586. status = HAL_ERROR;
  1587. break;
  1588. }
  1589. if (status == HAL_OK)
  1590. {
  1591. /* Disable the Capture compare channel */
  1592. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
  1593. /* Disable the Peripheral */
  1594. __HAL_TIM_DISABLE(htim);
  1595. /* Set the TIM channel state */
  1596. TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
  1597. }
  1598. /* Return function status */
  1599. return status;
  1600. }
  1601. /**
  1602. * @}
  1603. */
  1604. /** @defgroup TIM_Exported_Functions_Group4 TIM Input Capture functions
  1605. * @brief TIM Input Capture functions
  1606. *
  1607. @verbatim
  1608. ==============================================================================
  1609. ##### TIM Input Capture functions #####
  1610. ==============================================================================
  1611. [..]
  1612. This section provides functions allowing to:
  1613. (+) Initialize and configure the TIM Input Capture.
  1614. (+) De-initialize the TIM Input Capture.
  1615. (+) Start the TIM Input Capture.
  1616. (+) Stop the TIM Input Capture.
  1617. (+) Start the TIM Input Capture and enable interrupt.
  1618. (+) Stop the TIM Input Capture and disable interrupt.
  1619. (+) Start the TIM Input Capture and enable DMA transfer.
  1620. (+) Stop the TIM Input Capture and disable DMA transfer.
  1621. @endverbatim
  1622. * @{
  1623. */
  1624. /**
  1625. * @brief Initializes the TIM Input Capture Time base according to the specified
  1626. * parameters in the TIM_HandleTypeDef and initializes the associated handle.
  1627. * @note Switching from Center Aligned counter mode to Edge counter mode (or reverse)
  1628. * requires a timer reset to avoid unexpected direction
  1629. * due to DIR bit readonly in center aligned mode.
  1630. * Ex: call @ref HAL_TIM_IC_DeInit() before HAL_TIM_IC_Init()
  1631. * @param htim TIM Input Capture handle
  1632. * @retval HAL status
  1633. */
  1634. HAL_StatusTypeDef HAL_TIM_IC_Init(TIM_HandleTypeDef *htim)
  1635. {
  1636. /* Check the TIM handle allocation */
  1637. if (htim == NULL)
  1638. {
  1639. return HAL_ERROR;
  1640. }
  1641. /* Check the parameters */
  1642. assert_param(IS_TIM_INSTANCE(htim->Instance));
  1643. assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
  1644. assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
  1645. assert_param(IS_TIM_PERIOD(htim->Init.Period));
  1646. assert_param(IS_TIM_PRESCALER(htim->Init.Prescaler));
  1647. assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
  1648. if (htim->State == HAL_TIM_STATE_RESET)
  1649. {
  1650. /* Allocate lock resource and initialize it */
  1651. htim->Lock = HAL_UNLOCKED;
  1652. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  1653. /* Reset interrupt callbacks to legacy weak callbacks */
  1654. TIM_ResetCallback(htim);
  1655. if (htim->IC_MspInitCallback == NULL)
  1656. {
  1657. htim->IC_MspInitCallback = HAL_TIM_IC_MspInit;
  1658. }
  1659. /* Init the low level hardware : GPIO, CLOCK, NVIC */
  1660. htim->IC_MspInitCallback(htim);
  1661. #else
  1662. /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
  1663. HAL_TIM_IC_MspInit(htim);
  1664. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  1665. }
  1666. /* Set the TIM state */
  1667. htim->State = HAL_TIM_STATE_BUSY;
  1668. /* Init the base time for the input capture */
  1669. TIM_Base_SetConfig(htim->Instance, &htim->Init);
  1670. /* Initialize the DMA burst operation state */
  1671. htim->DMABurstState = HAL_DMA_BURST_STATE_READY;
  1672. /* Initialize the TIM channels state */
  1673. TIM_CHANNEL_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_READY);
  1674. /* Initialize the TIM state*/
  1675. htim->State = HAL_TIM_STATE_READY;
  1676. return HAL_OK;
  1677. }
  1678. /**
  1679. * @brief DeInitializes the TIM peripheral
  1680. * @param htim TIM Input Capture handle
  1681. * @retval HAL status
  1682. */
  1683. HAL_StatusTypeDef HAL_TIM_IC_DeInit(TIM_HandleTypeDef *htim)
  1684. {
  1685. /* Check the parameters */
  1686. assert_param(IS_TIM_INSTANCE(htim->Instance));
  1687. htim->State = HAL_TIM_STATE_BUSY;
  1688. /* Disable the TIM Peripheral Clock */
  1689. __HAL_TIM_DISABLE(htim);
  1690. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  1691. if (htim->IC_MspDeInitCallback == NULL)
  1692. {
  1693. htim->IC_MspDeInitCallback = HAL_TIM_IC_MspDeInit;
  1694. }
  1695. /* DeInit the low level hardware */
  1696. htim->IC_MspDeInitCallback(htim);
  1697. #else
  1698. /* DeInit the low level hardware: GPIO, CLOCK, NVIC and DMA */
  1699. HAL_TIM_IC_MspDeInit(htim);
  1700. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  1701. /* Change the DMA burst operation state */
  1702. htim->DMABurstState = HAL_DMA_BURST_STATE_RESET;
  1703. /* Change the TIM channels state */
  1704. TIM_CHANNEL_STATE_SET_ALL(htim, HAL_TIM_CHANNEL_STATE_RESET);
  1705. /* Change TIM state */
  1706. htim->State = HAL_TIM_STATE_RESET;
  1707. /* Release Lock */
  1708. __HAL_UNLOCK(htim);
  1709. return HAL_OK;
  1710. }
  1711. /**
  1712. * @brief Initializes the TIM Input Capture MSP.
  1713. * @param htim TIM Input Capture handle
  1714. * @retval None
  1715. */
  1716. __weak void HAL_TIM_IC_MspInit(TIM_HandleTypeDef *htim)
  1717. {
  1718. /* Prevent unused argument(s) compilation warning */
  1719. UNUSED(htim);
  1720. /* NOTE : This function should not be modified, when the callback is needed,
  1721. the HAL_TIM_IC_MspInit could be implemented in the user file
  1722. */
  1723. }
  1724. /**
  1725. * @brief DeInitializes TIM Input Capture MSP.
  1726. * @param htim TIM handle
  1727. * @retval None
  1728. */
  1729. __weak void HAL_TIM_IC_MspDeInit(TIM_HandleTypeDef *htim)
  1730. {
  1731. /* Prevent unused argument(s) compilation warning */
  1732. UNUSED(htim);
  1733. /* NOTE : This function should not be modified, when the callback is needed,
  1734. the HAL_TIM_IC_MspDeInit could be implemented in the user file
  1735. */
  1736. }
  1737. /**
  1738. * @brief Starts the TIM Input Capture measurement.
  1739. * @param htim TIM Input Capture handle
  1740. * @param Channel TIM Channels to be enabled
  1741. * This parameter can be one of the following values:
  1742. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1743. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1744. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1745. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1746. * @retval HAL status
  1747. */
  1748. HAL_StatusTypeDef HAL_TIM_IC_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
  1749. {
  1750. uint32_t tmpsmcr;
  1751. HAL_TIM_ChannelStateTypeDef channel_state = TIM_CHANNEL_STATE_GET(htim, Channel);
  1752. /* Check the parameters */
  1753. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1754. /* Check the TIM channel state */
  1755. if (channel_state != HAL_TIM_CHANNEL_STATE_READY)
  1756. {
  1757. return HAL_ERROR;
  1758. }
  1759. /* Set the TIM channel state */
  1760. TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
  1761. /* Enable the Input Capture channel */
  1762. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
  1763. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  1764. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  1765. {
  1766. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  1767. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  1768. {
  1769. __HAL_TIM_ENABLE(htim);
  1770. }
  1771. }
  1772. else
  1773. {
  1774. __HAL_TIM_ENABLE(htim);
  1775. }
  1776. /* Return function status */
  1777. return HAL_OK;
  1778. }
  1779. /**
  1780. * @brief Stops the TIM Input Capture measurement.
  1781. * @param htim TIM Input Capture handle
  1782. * @param Channel TIM Channels to be disabled
  1783. * This parameter can be one of the following values:
  1784. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1785. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1786. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1787. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1788. * @retval HAL status
  1789. */
  1790. HAL_StatusTypeDef HAL_TIM_IC_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
  1791. {
  1792. /* Check the parameters */
  1793. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1794. /* Disable the Input Capture channel */
  1795. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
  1796. /* Disable the Peripheral */
  1797. __HAL_TIM_DISABLE(htim);
  1798. /* Set the TIM channel state */
  1799. TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
  1800. /* Return function status */
  1801. return HAL_OK;
  1802. }
  1803. /**
  1804. * @brief Starts the TIM Input Capture measurement in interrupt mode.
  1805. * @param htim TIM Input Capture handle
  1806. * @param Channel TIM Channels to be enabled
  1807. * This parameter can be one of the following values:
  1808. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1809. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1810. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1811. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1812. * @retval HAL status
  1813. */
  1814. HAL_StatusTypeDef HAL_TIM_IC_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  1815. {
  1816. HAL_StatusTypeDef status = HAL_OK;
  1817. uint32_t tmpsmcr;
  1818. HAL_TIM_ChannelStateTypeDef channel_state = TIM_CHANNEL_STATE_GET(htim, Channel);
  1819. /* Check the parameters */
  1820. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1821. /* Check the TIM channel state */
  1822. if (channel_state != HAL_TIM_CHANNEL_STATE_READY)
  1823. {
  1824. return HAL_ERROR;
  1825. }
  1826. /* Set the TIM channel state */
  1827. TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
  1828. switch (Channel)
  1829. {
  1830. case TIM_CHANNEL_1:
  1831. {
  1832. /* Enable the TIM Capture/Compare 1 interrupt */
  1833. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  1834. break;
  1835. }
  1836. case TIM_CHANNEL_2:
  1837. {
  1838. /* Enable the TIM Capture/Compare 2 interrupt */
  1839. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  1840. break;
  1841. }
  1842. case TIM_CHANNEL_3:
  1843. {
  1844. /* Enable the TIM Capture/Compare 3 interrupt */
  1845. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
  1846. break;
  1847. }
  1848. case TIM_CHANNEL_4:
  1849. {
  1850. /* Enable the TIM Capture/Compare 4 interrupt */
  1851. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
  1852. break;
  1853. }
  1854. default:
  1855. status = HAL_ERROR;
  1856. break;
  1857. }
  1858. if (status == HAL_OK)
  1859. {
  1860. /* Enable the Input Capture channel */
  1861. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
  1862. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  1863. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  1864. {
  1865. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  1866. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  1867. {
  1868. __HAL_TIM_ENABLE(htim);
  1869. }
  1870. }
  1871. else
  1872. {
  1873. __HAL_TIM_ENABLE(htim);
  1874. }
  1875. }
  1876. /* Return function status */
  1877. return status;
  1878. }
  1879. /**
  1880. * @brief Stops the TIM Input Capture measurement in interrupt mode.
  1881. * @param htim TIM Input Capture handle
  1882. * @param Channel TIM Channels to be disabled
  1883. * This parameter can be one of the following values:
  1884. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1885. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1886. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1887. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1888. * @retval HAL status
  1889. */
  1890. HAL_StatusTypeDef HAL_TIM_IC_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  1891. {
  1892. HAL_StatusTypeDef status = HAL_OK;
  1893. /* Check the parameters */
  1894. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1895. switch (Channel)
  1896. {
  1897. case TIM_CHANNEL_1:
  1898. {
  1899. /* Disable the TIM Capture/Compare 1 interrupt */
  1900. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  1901. break;
  1902. }
  1903. case TIM_CHANNEL_2:
  1904. {
  1905. /* Disable the TIM Capture/Compare 2 interrupt */
  1906. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  1907. break;
  1908. }
  1909. case TIM_CHANNEL_3:
  1910. {
  1911. /* Disable the TIM Capture/Compare 3 interrupt */
  1912. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
  1913. break;
  1914. }
  1915. case TIM_CHANNEL_4:
  1916. {
  1917. /* Disable the TIM Capture/Compare 4 interrupt */
  1918. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
  1919. break;
  1920. }
  1921. default:
  1922. status = HAL_ERROR;
  1923. break;
  1924. }
  1925. if (status == HAL_OK)
  1926. {
  1927. /* Disable the Input Capture channel */
  1928. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
  1929. /* Disable the Peripheral */
  1930. __HAL_TIM_DISABLE(htim);
  1931. /* Set the TIM channel state */
  1932. TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
  1933. }
  1934. /* Return function status */
  1935. return status;
  1936. }
  1937. /**
  1938. * @brief Starts the TIM Input Capture measurement in DMA mode.
  1939. * @param htim TIM Input Capture handle
  1940. * @param Channel TIM Channels to be enabled
  1941. * This parameter can be one of the following values:
  1942. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1943. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1944. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1945. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1946. * @param pData The destination Buffer address.
  1947. * @param Length The length of data to be transferred from TIM peripheral to memory.
  1948. * @retval HAL status
  1949. */
  1950. HAL_StatusTypeDef HAL_TIM_IC_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)
  1951. {
  1952. HAL_StatusTypeDef status = HAL_OK;
  1953. uint32_t tmpsmcr;
  1954. HAL_TIM_ChannelStateTypeDef channel_state = TIM_CHANNEL_STATE_GET(htim, Channel);
  1955. /* Check the parameters */
  1956. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1957. assert_param(IS_TIM_DMA_CC_INSTANCE(htim->Instance));
  1958. /* Set the TIM channel state */
  1959. if (channel_state == HAL_TIM_CHANNEL_STATE_BUSY)
  1960. {
  1961. return HAL_BUSY;
  1962. }
  1963. if (channel_state == HAL_TIM_CHANNEL_STATE_READY)
  1964. {
  1965. if ((pData == NULL) || (Length == 0U))
  1966. {
  1967. return HAL_ERROR;
  1968. }
  1969. else
  1970. {
  1971. TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_BUSY);
  1972. }
  1973. }
  1974. else
  1975. {
  1976. return HAL_ERROR;
  1977. }
  1978. /* Enable the Input Capture channel */
  1979. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
  1980. switch (Channel)
  1981. {
  1982. case TIM_CHANNEL_1:
  1983. {
  1984. /* Set the DMA capture callbacks */
  1985. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMACaptureCplt;
  1986. htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  1987. /* Set the DMA error callback */
  1988. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  1989. /* Enable the DMA channel */
  1990. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->CCR1, (uint32_t)pData,
  1991. Length) != HAL_OK)
  1992. {
  1993. /* Return error status */
  1994. return HAL_ERROR;
  1995. }
  1996. /* Enable the TIM Capture/Compare 1 DMA request */
  1997. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  1998. break;
  1999. }
  2000. case TIM_CHANNEL_2:
  2001. {
  2002. /* Set the DMA capture callbacks */
  2003. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMACaptureCplt;
  2004. htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  2005. /* Set the DMA error callback */
  2006. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
  2007. /* Enable the DMA channel */
  2008. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)&htim->Instance->CCR2, (uint32_t)pData,
  2009. Length) != HAL_OK)
  2010. {
  2011. /* Return error status */
  2012. return HAL_ERROR;
  2013. }
  2014. /* Enable the TIM Capture/Compare 2 DMA request */
  2015. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
  2016. break;
  2017. }
  2018. case TIM_CHANNEL_3:
  2019. {
  2020. /* Set the DMA capture callbacks */
  2021. htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMACaptureCplt;
  2022. htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  2023. /* Set the DMA error callback */
  2024. htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
  2025. /* Enable the DMA channel */
  2026. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)&htim->Instance->CCR3, (uint32_t)pData,
  2027. Length) != HAL_OK)
  2028. {
  2029. /* Return error status */
  2030. return HAL_ERROR;
  2031. }
  2032. /* Enable the TIM Capture/Compare 3 DMA request */
  2033. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
  2034. break;
  2035. }
  2036. case TIM_CHANNEL_4:
  2037. {
  2038. /* Set the DMA capture callbacks */
  2039. htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMACaptureCplt;
  2040. htim->hdma[TIM_DMA_ID_CC4]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  2041. /* Set the DMA error callback */
  2042. htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
  2043. /* Enable the DMA channel */
  2044. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)&htim->Instance->CCR4, (uint32_t)pData,
  2045. Length) != HAL_OK)
  2046. {
  2047. /* Return error status */
  2048. return HAL_ERROR;
  2049. }
  2050. /* Enable the TIM Capture/Compare 4 DMA request */
  2051. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);
  2052. break;
  2053. }
  2054. default:
  2055. status = HAL_ERROR;
  2056. break;
  2057. }
  2058. /* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
  2059. if (IS_TIM_SLAVE_INSTANCE(htim->Instance))
  2060. {
  2061. tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
  2062. if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
  2063. {
  2064. __HAL_TIM_ENABLE(htim);
  2065. }
  2066. }
  2067. else
  2068. {
  2069. __HAL_TIM_ENABLE(htim);
  2070. }
  2071. /* Return function status */
  2072. return status;
  2073. }
  2074. /**
  2075. * @brief Stops the TIM Input Capture measurement in DMA mode.
  2076. * @param htim TIM Input Capture handle
  2077. * @param Channel TIM Channels to be disabled
  2078. * This parameter can be one of the following values:
  2079. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  2080. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  2081. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  2082. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  2083. * @retval HAL status
  2084. */
  2085. HAL_StatusTypeDef HAL_TIM_IC_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
  2086. {
  2087. HAL_StatusTypeDef status = HAL_OK;
  2088. /* Check the parameters */
  2089. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  2090. assert_param(IS_TIM_DMA_CC_INSTANCE(htim->Instance));
  2091. /* Disable the Input Capture channel */
  2092. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
  2093. switch (Channel)
  2094. {
  2095. case TIM_CHANNEL_1:
  2096. {
  2097. /* Disable the TIM Capture/Compare 1 DMA request */
  2098. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  2099. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
  2100. break;
  2101. }
  2102. case TIM_CHANNEL_2:
  2103. {
  2104. /* Disable the TIM Capture/Compare 2 DMA request */
  2105. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
  2106. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
  2107. break;
  2108. }
  2109. case TIM_CHANNEL_3:
  2110. {
  2111. /* Disable the TIM Capture/Compare 3 DMA request */
  2112. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
  2113. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
  2114. break;
  2115. }
  2116. case TIM_CHANNEL_4:
  2117. {
  2118. /* Disable the TIM Capture/Compare 4 DMA request */
  2119. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);
  2120. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC4]);
  2121. break;
  2122. }
  2123. default:
  2124. status = HAL_ERROR;
  2125. break;
  2126. }
  2127. if (status == HAL_OK)
  2128. {
  2129. /* Disable the Peripheral */
  2130. __HAL_TIM_DISABLE(htim);
  2131. /* Set the TIM channel state */
  2132. TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
  2133. }
  2134. /* Return function status */
  2135. return status;
  2136. }
  2137. /**
  2138. * @}
  2139. */
  2140. /** @defgroup TIM_Exported_Functions_Group5 TIM One Pulse functions
  2141. * @brief TIM One Pulse functions
  2142. *
  2143. @verbatim
  2144. ==============================================================================
  2145. ##### TIM One Pulse functions #####
  2146. ==============================================================================
  2147. [..]
  2148. This section provides functions allowing to:
  2149. (+) Initialize and configure the TIM One Pulse.
  2150. (+) De-initialize the TIM One Pulse.
  2151. (+) Start the TIM One Pulse.
  2152. (+) Stop the TIM One Pulse.
  2153. (+) Start the TIM One Pulse and enable interrupt.
  2154. (+) Stop the TIM One Pulse and disable interrupt.
  2155. (+) Start the TIM One Pulse and enable DMA transfer.
  2156. (+) Stop the TIM One Pulse and disable DMA transfer.
  2157. @endverbatim
  2158. * @{
  2159. */
  2160. /**
  2161. * @brief Initializes the TIM One Pulse Time Base according to the specified
  2162. * parameters in the TIM_HandleTypeDef and initializes the associated handle.
  2163. * @note Switching from Center Aligned counter mode to Edge counter mode (or reverse)
  2164. * requires a timer reset to avoid unexpected direction
  2165. * due to DIR bit readonly in center aligned mode.
  2166. * Ex: call @ref HAL_TIM_OnePulse_DeInit() before HAL_TIM_OnePulse_Init()
  2167. * @note When the timer instance is initialized in One Pulse mode, timer
  2168. * channels 1 and channel 2 are reserved and cannot be used for other
  2169. * purpose.
  2170. * @param htim TIM One Pulse handle
  2171. * @param OnePulseMode Select the One pulse mode.
  2172. * This parameter can be one of the following values:
  2173. * @arg TIM_OPMODE_SINGLE: Only one pulse will be generated.
  2174. * @arg TIM_OPMODE_REPETITIVE: Repetitive pulses will be generated.
  2175. * @retval HAL status
  2176. */
  2177. HAL_StatusTypeDef HAL_TIM_OnePulse_Init(TIM_HandleTypeDef *htim, uint32_t OnePulseMode)
  2178. {
  2179. /* Check the TIM handle allocation */
  2180. if (htim == NULL)
  2181. {
  2182. return HAL_ERROR;
  2183. }
  2184. /* Check the parameters */
  2185. assert_param(IS_TIM_INSTANCE(htim->Instance));
  2186. assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
  2187. assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
  2188. assert_param(IS_TIM_OPM_MODE(OnePulseMode));
  2189. assert_param(IS_TIM_PERIOD(htim->Init.Period));
  2190. assert_param(IS_TIM_PRESCALER(htim->Init.Prescaler));
  2191. assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
  2192. if (htim->State == HAL_TIM_STATE_RESET)
  2193. {
  2194. /* Allocate lock resource and initialize it */
  2195. htim->Lock = HAL_UNLOCKED;
  2196. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2197. /* Reset interrupt callbacks to legacy weak callbacks */
  2198. TIM_ResetCallback(htim);
  2199. if (htim->OnePulse_MspInitCallback == NULL)
  2200. {
  2201. htim->OnePulse_MspInitCallback = HAL_TIM_OnePulse_MspInit;
  2202. }
  2203. /* Init the low level hardware : GPIO, CLOCK, NVIC */
  2204. htim->OnePulse_MspInitCallback(htim);
  2205. #else
  2206. /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
  2207. HAL_TIM_OnePulse_MspInit(htim);
  2208. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2209. }
  2210. /* Set the TIM state */
  2211. htim->State = HAL_TIM_STATE_BUSY;
  2212. /* Configure the Time base in the One Pulse Mode */
  2213. TIM_Base_SetConfig(htim->Instance, &htim->Init);
  2214. /* Reset the OPM Bit */
  2215. htim->Instance->CR1 &= ~TIM_CR1_OPM;
  2216. /* Configure the OPM Mode */
  2217. htim->Instance->CR1 |= OnePulseMode;
  2218. /* Initialize the DMA burst operation state */
  2219. htim->DMABurstState = HAL_DMA_BURST_STATE_READY;
  2220. /* Initialize the TIM channels state */
  2221. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  2222. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  2223. /* Initialize the TIM state*/
  2224. htim->State = HAL_TIM_STATE_READY;
  2225. return HAL_OK;
  2226. }
  2227. /**
  2228. * @brief DeInitializes the TIM One Pulse
  2229. * @param htim TIM One Pulse handle
  2230. * @retval HAL status
  2231. */
  2232. HAL_StatusTypeDef HAL_TIM_OnePulse_DeInit(TIM_HandleTypeDef *htim)
  2233. {
  2234. /* Check the parameters */
  2235. assert_param(IS_TIM_INSTANCE(htim->Instance));
  2236. htim->State = HAL_TIM_STATE_BUSY;
  2237. /* Disable the TIM Peripheral Clock */
  2238. __HAL_TIM_DISABLE(htim);
  2239. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2240. if (htim->OnePulse_MspDeInitCallback == NULL)
  2241. {
  2242. htim->OnePulse_MspDeInitCallback = HAL_TIM_OnePulse_MspDeInit;
  2243. }
  2244. /* DeInit the low level hardware */
  2245. htim->OnePulse_MspDeInitCallback(htim);
  2246. #else
  2247. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  2248. HAL_TIM_OnePulse_MspDeInit(htim);
  2249. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2250. /* Change the DMA burst operation state */
  2251. htim->DMABurstState = HAL_DMA_BURST_STATE_RESET;
  2252. /* Set the TIM channel state */
  2253. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_RESET);
  2254. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_RESET);
  2255. /* Change TIM state */
  2256. htim->State = HAL_TIM_STATE_RESET;
  2257. /* Release Lock */
  2258. __HAL_UNLOCK(htim);
  2259. return HAL_OK;
  2260. }
  2261. /**
  2262. * @brief Initializes the TIM One Pulse MSP.
  2263. * @param htim TIM One Pulse handle
  2264. * @retval None
  2265. */
  2266. __weak void HAL_TIM_OnePulse_MspInit(TIM_HandleTypeDef *htim)
  2267. {
  2268. /* Prevent unused argument(s) compilation warning */
  2269. UNUSED(htim);
  2270. /* NOTE : This function should not be modified, when the callback is needed,
  2271. the HAL_TIM_OnePulse_MspInit could be implemented in the user file
  2272. */
  2273. }
  2274. /**
  2275. * @brief DeInitializes TIM One Pulse MSP.
  2276. * @param htim TIM One Pulse handle
  2277. * @retval None
  2278. */
  2279. __weak void HAL_TIM_OnePulse_MspDeInit(TIM_HandleTypeDef *htim)
  2280. {
  2281. /* Prevent unused argument(s) compilation warning */
  2282. UNUSED(htim);
  2283. /* NOTE : This function should not be modified, when the callback is needed,
  2284. the HAL_TIM_OnePulse_MspDeInit could be implemented in the user file
  2285. */
  2286. }
  2287. /**
  2288. * @brief Starts the TIM One Pulse signal generation.
  2289. * @note Though OutputChannel parameter is deprecated and ignored by the function
  2290. * it has been kept to avoid HAL_TIM API compatibility break.
  2291. * @note The pulse output channel is determined when calling
  2292. * @ref HAL_TIM_OnePulse_ConfigChannel().
  2293. * @param htim TIM One Pulse handle
  2294. * @param OutputChannel See note above
  2295. * @retval HAL status
  2296. */
  2297. HAL_StatusTypeDef HAL_TIM_OnePulse_Start(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  2298. {
  2299. HAL_TIM_ChannelStateTypeDef channel_1_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_1);
  2300. HAL_TIM_ChannelStateTypeDef channel_2_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_2);
  2301. /* Prevent unused argument(s) compilation warning */
  2302. UNUSED(OutputChannel);
  2303. /* Check the TIM channels state */
  2304. if ((channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
  2305. || (channel_2_state != HAL_TIM_CHANNEL_STATE_READY))
  2306. {
  2307. return HAL_ERROR;
  2308. }
  2309. /* Set the TIM channels state */
  2310. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
  2311. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
  2312. /* Enable the Capture compare and the Input Capture channels
  2313. (in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)
  2314. if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and
  2315. if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output
  2316. whatever the combination, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be enabled together
  2317. No need to enable the counter, it's enabled automatically by hardware
  2318. (the counter starts in response to a stimulus and generate a pulse */
  2319. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  2320. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
  2321. /* Return function status */
  2322. return HAL_OK;
  2323. }
  2324. /**
  2325. * @brief Stops the TIM One Pulse signal generation.
  2326. * @note Though OutputChannel parameter is deprecated and ignored by the function
  2327. * it has been kept to avoid HAL_TIM API compatibility break.
  2328. * @note The pulse output channel is determined when calling
  2329. * @ref HAL_TIM_OnePulse_ConfigChannel().
  2330. * @param htim TIM One Pulse handle
  2331. * @param OutputChannel See note above
  2332. * @retval HAL status
  2333. */
  2334. HAL_StatusTypeDef HAL_TIM_OnePulse_Stop(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  2335. {
  2336. /* Prevent unused argument(s) compilation warning */
  2337. UNUSED(OutputChannel);
  2338. /* Disable the Capture compare and the Input Capture channels
  2339. (in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)
  2340. if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and
  2341. if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output
  2342. whatever the combination, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be disabled together */
  2343. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  2344. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
  2345. /* Disable the Peripheral */
  2346. __HAL_TIM_DISABLE(htim);
  2347. /* Set the TIM channels state */
  2348. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  2349. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  2350. /* Return function status */
  2351. return HAL_OK;
  2352. }
  2353. /**
  2354. * @brief Starts the TIM One Pulse signal generation in interrupt mode.
  2355. * @note Though OutputChannel parameter is deprecated and ignored by the function
  2356. * it has been kept to avoid HAL_TIM API compatibility break.
  2357. * @note The pulse output channel is determined when calling
  2358. * @ref HAL_TIM_OnePulse_ConfigChannel().
  2359. * @param htim TIM One Pulse handle
  2360. * @param OutputChannel See note above
  2361. * @retval HAL status
  2362. */
  2363. HAL_StatusTypeDef HAL_TIM_OnePulse_Start_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  2364. {
  2365. HAL_TIM_ChannelStateTypeDef channel_1_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_1);
  2366. HAL_TIM_ChannelStateTypeDef channel_2_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_2);
  2367. /* Prevent unused argument(s) compilation warning */
  2368. UNUSED(OutputChannel);
  2369. /* Check the TIM channels state */
  2370. if ((channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
  2371. || (channel_2_state != HAL_TIM_CHANNEL_STATE_READY))
  2372. {
  2373. return HAL_ERROR;
  2374. }
  2375. /* Set the TIM channels state */
  2376. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
  2377. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
  2378. /* Enable the Capture compare and the Input Capture channels
  2379. (in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)
  2380. if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and
  2381. if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output
  2382. whatever the combination, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be enabled together
  2383. No need to enable the counter, it's enabled automatically by hardware
  2384. (the counter starts in response to a stimulus and generate a pulse */
  2385. /* Enable the TIM Capture/Compare 1 interrupt */
  2386. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  2387. /* Enable the TIM Capture/Compare 2 interrupt */
  2388. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  2389. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  2390. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
  2391. /* Return function status */
  2392. return HAL_OK;
  2393. }
  2394. /**
  2395. * @brief Stops the TIM One Pulse signal generation in interrupt mode.
  2396. * @note Though OutputChannel parameter is deprecated and ignored by the function
  2397. * it has been kept to avoid HAL_TIM API compatibility break.
  2398. * @note The pulse output channel is determined when calling
  2399. * @ref HAL_TIM_OnePulse_ConfigChannel().
  2400. * @param htim TIM One Pulse handle
  2401. * @param OutputChannel See note above
  2402. * @retval HAL status
  2403. */
  2404. HAL_StatusTypeDef HAL_TIM_OnePulse_Stop_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  2405. {
  2406. /* Prevent unused argument(s) compilation warning */
  2407. UNUSED(OutputChannel);
  2408. /* Disable the TIM Capture/Compare 1 interrupt */
  2409. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  2410. /* Disable the TIM Capture/Compare 2 interrupt */
  2411. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  2412. /* Disable the Capture compare and the Input Capture channels
  2413. (in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)
  2414. if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and
  2415. if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output
  2416. whatever the combination, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be disabled together */
  2417. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  2418. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
  2419. /* Disable the Peripheral */
  2420. __HAL_TIM_DISABLE(htim);
  2421. /* Set the TIM channels state */
  2422. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  2423. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  2424. /* Return function status */
  2425. return HAL_OK;
  2426. }
  2427. /**
  2428. * @}
  2429. */
  2430. /** @defgroup TIM_Exported_Functions_Group6 TIM Encoder functions
  2431. * @brief TIM Encoder functions
  2432. *
  2433. @verbatim
  2434. ==============================================================================
  2435. ##### TIM Encoder functions #####
  2436. ==============================================================================
  2437. [..]
  2438. This section provides functions allowing to:
  2439. (+) Initialize and configure the TIM Encoder.
  2440. (+) De-initialize the TIM Encoder.
  2441. (+) Start the TIM Encoder.
  2442. (+) Stop the TIM Encoder.
  2443. (+) Start the TIM Encoder and enable interrupt.
  2444. (+) Stop the TIM Encoder and disable interrupt.
  2445. (+) Start the TIM Encoder and enable DMA transfer.
  2446. (+) Stop the TIM Encoder and disable DMA transfer.
  2447. @endverbatim
  2448. * @{
  2449. */
  2450. /**
  2451. * @brief Initializes the TIM Encoder Interface and initialize the associated handle.
  2452. * @note Switching from Center Aligned counter mode to Edge counter mode (or reverse)
  2453. * requires a timer reset to avoid unexpected direction
  2454. * due to DIR bit readonly in center aligned mode.
  2455. * Ex: call @ref HAL_TIM_Encoder_DeInit() before HAL_TIM_Encoder_Init()
  2456. * @note Encoder mode and External clock mode 2 are not compatible and must not be selected together
  2457. * Ex: A call for @ref HAL_TIM_Encoder_Init will erase the settings of @ref HAL_TIM_ConfigClockSource
  2458. * using TIM_CLOCKSOURCE_ETRMODE2 and vice versa
  2459. * @note When the timer instance is initialized in Encoder mode, timer
  2460. * channels 1 and channel 2 are reserved and cannot be used for other
  2461. * purpose.
  2462. * @param htim TIM Encoder Interface handle
  2463. * @param sConfig TIM Encoder Interface configuration structure
  2464. * @retval HAL status
  2465. */
  2466. HAL_StatusTypeDef HAL_TIM_Encoder_Init(TIM_HandleTypeDef *htim, const TIM_Encoder_InitTypeDef *sConfig)
  2467. {
  2468. uint32_t tmpsmcr;
  2469. uint32_t tmpccmr1;
  2470. uint32_t tmpccer;
  2471. /* Check the TIM handle allocation */
  2472. if (htim == NULL)
  2473. {
  2474. return HAL_ERROR;
  2475. }
  2476. /* Check the parameters */
  2477. assert_param(IS_TIM_ENCODER_INTERFACE_INSTANCE(htim->Instance));
  2478. assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
  2479. assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
  2480. assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
  2481. assert_param(IS_TIM_ENCODER_MODE(sConfig->EncoderMode));
  2482. assert_param(IS_TIM_IC_SELECTION(sConfig->IC1Selection));
  2483. assert_param(IS_TIM_IC_SELECTION(sConfig->IC2Selection));
  2484. assert_param(IS_TIM_ENCODERINPUT_POLARITY(sConfig->IC1Polarity));
  2485. assert_param(IS_TIM_ENCODERINPUT_POLARITY(sConfig->IC2Polarity));
  2486. assert_param(IS_TIM_IC_PRESCALER(sConfig->IC1Prescaler));
  2487. assert_param(IS_TIM_IC_PRESCALER(sConfig->IC2Prescaler));
  2488. assert_param(IS_TIM_IC_FILTER(sConfig->IC1Filter));
  2489. assert_param(IS_TIM_IC_FILTER(sConfig->IC2Filter));
  2490. assert_param(IS_TIM_PERIOD(htim->Init.Period));
  2491. assert_param(IS_TIM_PRESCALER(htim->Init.Prescaler));
  2492. if (htim->State == HAL_TIM_STATE_RESET)
  2493. {
  2494. /* Allocate lock resource and initialize it */
  2495. htim->Lock = HAL_UNLOCKED;
  2496. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2497. /* Reset interrupt callbacks to legacy weak callbacks */
  2498. TIM_ResetCallback(htim);
  2499. if (htim->Encoder_MspInitCallback == NULL)
  2500. {
  2501. htim->Encoder_MspInitCallback = HAL_TIM_Encoder_MspInit;
  2502. }
  2503. /* Init the low level hardware : GPIO, CLOCK, NVIC */
  2504. htim->Encoder_MspInitCallback(htim);
  2505. #else
  2506. /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
  2507. HAL_TIM_Encoder_MspInit(htim);
  2508. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2509. }
  2510. /* Set the TIM state */
  2511. htim->State = HAL_TIM_STATE_BUSY;
  2512. /* Reset the SMS and ECE bits */
  2513. htim->Instance->SMCR &= ~(TIM_SMCR_SMS | TIM_SMCR_ECE);
  2514. /* Configure the Time base in the Encoder Mode */
  2515. TIM_Base_SetConfig(htim->Instance, &htim->Init);
  2516. /* Get the TIMx SMCR register value */
  2517. tmpsmcr = htim->Instance->SMCR;
  2518. /* Get the TIMx CCMR1 register value */
  2519. tmpccmr1 = htim->Instance->CCMR1;
  2520. /* Get the TIMx CCER register value */
  2521. tmpccer = htim->Instance->CCER;
  2522. /* Set the encoder Mode */
  2523. tmpsmcr |= sConfig->EncoderMode;
  2524. /* Select the Capture Compare 1 and the Capture Compare 2 as input */
  2525. tmpccmr1 &= ~(TIM_CCMR1_CC1S | TIM_CCMR1_CC2S);
  2526. tmpccmr1 |= (sConfig->IC1Selection | (sConfig->IC2Selection << 8U));
  2527. /* Set the Capture Compare 1 and the Capture Compare 2 prescalers and filters */
  2528. tmpccmr1 &= ~(TIM_CCMR1_IC1PSC | TIM_CCMR1_IC2PSC);
  2529. tmpccmr1 &= ~(TIM_CCMR1_IC1F | TIM_CCMR1_IC2F);
  2530. tmpccmr1 |= sConfig->IC1Prescaler | (sConfig->IC2Prescaler << 8U);
  2531. tmpccmr1 |= (sConfig->IC1Filter << 4U) | (sConfig->IC2Filter << 12U);
  2532. /* Set the TI1 and the TI2 Polarities */
  2533. tmpccer &= ~(TIM_CCER_CC1P | TIM_CCER_CC2P);
  2534. tmpccer &= ~(TIM_CCER_CC1NP | TIM_CCER_CC2NP);
  2535. tmpccer |= sConfig->IC1Polarity | (sConfig->IC2Polarity << 4U);
  2536. /* Write to TIMx SMCR */
  2537. htim->Instance->SMCR = tmpsmcr;
  2538. /* Write to TIMx CCMR1 */
  2539. htim->Instance->CCMR1 = tmpccmr1;
  2540. /* Write to TIMx CCER */
  2541. htim->Instance->CCER = tmpccer;
  2542. /* Initialize the DMA burst operation state */
  2543. htim->DMABurstState = HAL_DMA_BURST_STATE_READY;
  2544. /* Set the TIM channels state */
  2545. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  2546. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  2547. /* Initialize the TIM state*/
  2548. htim->State = HAL_TIM_STATE_READY;
  2549. return HAL_OK;
  2550. }
  2551. /**
  2552. * @brief DeInitializes the TIM Encoder interface
  2553. * @param htim TIM Encoder Interface handle
  2554. * @retval HAL status
  2555. */
  2556. HAL_StatusTypeDef HAL_TIM_Encoder_DeInit(TIM_HandleTypeDef *htim)
  2557. {
  2558. /* Check the parameters */
  2559. assert_param(IS_TIM_INSTANCE(htim->Instance));
  2560. htim->State = HAL_TIM_STATE_BUSY;
  2561. /* Disable the TIM Peripheral Clock */
  2562. __HAL_TIM_DISABLE(htim);
  2563. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  2564. if (htim->Encoder_MspDeInitCallback == NULL)
  2565. {
  2566. htim->Encoder_MspDeInitCallback = HAL_TIM_Encoder_MspDeInit;
  2567. }
  2568. /* DeInit the low level hardware */
  2569. htim->Encoder_MspDeInitCallback(htim);
  2570. #else
  2571. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  2572. HAL_TIM_Encoder_MspDeInit(htim);
  2573. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  2574. /* Change the DMA burst operation state */
  2575. htim->DMABurstState = HAL_DMA_BURST_STATE_RESET;
  2576. /* Set the TIM channels state */
  2577. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_RESET);
  2578. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_RESET);
  2579. /* Change TIM state */
  2580. htim->State = HAL_TIM_STATE_RESET;
  2581. /* Release Lock */
  2582. __HAL_UNLOCK(htim);
  2583. return HAL_OK;
  2584. }
  2585. /**
  2586. * @brief Initializes the TIM Encoder Interface MSP.
  2587. * @param htim TIM Encoder Interface handle
  2588. * @retval None
  2589. */
  2590. __weak void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef *htim)
  2591. {
  2592. /* Prevent unused argument(s) compilation warning */
  2593. UNUSED(htim);
  2594. /* NOTE : This function should not be modified, when the callback is needed,
  2595. the HAL_TIM_Encoder_MspInit could be implemented in the user file
  2596. */
  2597. }
  2598. /**
  2599. * @brief DeInitializes TIM Encoder Interface MSP.
  2600. * @param htim TIM Encoder Interface handle
  2601. * @retval None
  2602. */
  2603. __weak void HAL_TIM_Encoder_MspDeInit(TIM_HandleTypeDef *htim)
  2604. {
  2605. /* Prevent unused argument(s) compilation warning */
  2606. UNUSED(htim);
  2607. /* NOTE : This function should not be modified, when the callback is needed,
  2608. the HAL_TIM_Encoder_MspDeInit could be implemented in the user file
  2609. */
  2610. }
  2611. /**
  2612. * @brief Starts the TIM Encoder Interface.
  2613. * @param htim TIM Encoder Interface handle
  2614. * @param Channel TIM Channels to be enabled
  2615. * This parameter can be one of the following values:
  2616. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  2617. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  2618. * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
  2619. * @retval HAL status
  2620. */
  2621. HAL_StatusTypeDef HAL_TIM_Encoder_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
  2622. {
  2623. HAL_TIM_ChannelStateTypeDef channel_1_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_1);
  2624. HAL_TIM_ChannelStateTypeDef channel_2_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_2);
  2625. /* Check the parameters */
  2626. assert_param(IS_TIM_ENCODER_INTERFACE_INSTANCE(htim->Instance));
  2627. /* Set the TIM channel(s) state */
  2628. if (Channel == TIM_CHANNEL_1)
  2629. {
  2630. if (channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
  2631. {
  2632. return HAL_ERROR;
  2633. }
  2634. else
  2635. {
  2636. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
  2637. }
  2638. }
  2639. else if (Channel == TIM_CHANNEL_2)
  2640. {
  2641. if (channel_2_state != HAL_TIM_CHANNEL_STATE_READY)
  2642. {
  2643. return HAL_ERROR;
  2644. }
  2645. else
  2646. {
  2647. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
  2648. }
  2649. }
  2650. else
  2651. {
  2652. if ((channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
  2653. || (channel_2_state != HAL_TIM_CHANNEL_STATE_READY))
  2654. {
  2655. return HAL_ERROR;
  2656. }
  2657. else
  2658. {
  2659. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
  2660. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
  2661. }
  2662. }
  2663. /* Enable the encoder interface channels */
  2664. switch (Channel)
  2665. {
  2666. case TIM_CHANNEL_1:
  2667. {
  2668. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  2669. break;
  2670. }
  2671. case TIM_CHANNEL_2:
  2672. {
  2673. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
  2674. break;
  2675. }
  2676. default :
  2677. {
  2678. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  2679. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
  2680. break;
  2681. }
  2682. }
  2683. /* Enable the Peripheral */
  2684. __HAL_TIM_ENABLE(htim);
  2685. /* Return function status */
  2686. return HAL_OK;
  2687. }
  2688. /**
  2689. * @brief Stops the TIM Encoder Interface.
  2690. * @param htim TIM Encoder Interface handle
  2691. * @param Channel TIM Channels to be disabled
  2692. * This parameter can be one of the following values:
  2693. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  2694. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  2695. * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
  2696. * @retval HAL status
  2697. */
  2698. HAL_StatusTypeDef HAL_TIM_Encoder_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
  2699. {
  2700. /* Check the parameters */
  2701. assert_param(IS_TIM_ENCODER_INTERFACE_INSTANCE(htim->Instance));
  2702. /* Disable the Input Capture channels 1 and 2
  2703. (in the EncoderInterface the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2) */
  2704. switch (Channel)
  2705. {
  2706. case TIM_CHANNEL_1:
  2707. {
  2708. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  2709. break;
  2710. }
  2711. case TIM_CHANNEL_2:
  2712. {
  2713. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
  2714. break;
  2715. }
  2716. default :
  2717. {
  2718. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  2719. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
  2720. break;
  2721. }
  2722. }
  2723. /* Disable the Peripheral */
  2724. __HAL_TIM_DISABLE(htim);
  2725. /* Set the TIM channel(s) state */
  2726. if ((Channel == TIM_CHANNEL_1) || (Channel == TIM_CHANNEL_2))
  2727. {
  2728. TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
  2729. }
  2730. else
  2731. {
  2732. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  2733. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  2734. }
  2735. /* Return function status */
  2736. return HAL_OK;
  2737. }
  2738. /**
  2739. * @brief Starts the TIM Encoder Interface in interrupt mode.
  2740. * @param htim TIM Encoder Interface handle
  2741. * @param Channel TIM Channels to be enabled
  2742. * This parameter can be one of the following values:
  2743. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  2744. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  2745. * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
  2746. * @retval HAL status
  2747. */
  2748. HAL_StatusTypeDef HAL_TIM_Encoder_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  2749. {
  2750. HAL_TIM_ChannelStateTypeDef channel_1_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_1);
  2751. HAL_TIM_ChannelStateTypeDef channel_2_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_2);
  2752. /* Check the parameters */
  2753. assert_param(IS_TIM_ENCODER_INTERFACE_INSTANCE(htim->Instance));
  2754. /* Set the TIM channel(s) state */
  2755. if (Channel == TIM_CHANNEL_1)
  2756. {
  2757. if (channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
  2758. {
  2759. return HAL_ERROR;
  2760. }
  2761. else
  2762. {
  2763. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
  2764. }
  2765. }
  2766. else if (Channel == TIM_CHANNEL_2)
  2767. {
  2768. if (channel_2_state != HAL_TIM_CHANNEL_STATE_READY)
  2769. {
  2770. return HAL_ERROR;
  2771. }
  2772. else
  2773. {
  2774. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
  2775. }
  2776. }
  2777. else
  2778. {
  2779. if ((channel_1_state != HAL_TIM_CHANNEL_STATE_READY)
  2780. || (channel_2_state != HAL_TIM_CHANNEL_STATE_READY))
  2781. {
  2782. return HAL_ERROR;
  2783. }
  2784. else
  2785. {
  2786. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
  2787. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
  2788. }
  2789. }
  2790. /* Enable the encoder interface channels */
  2791. /* Enable the capture compare Interrupts 1 and/or 2 */
  2792. switch (Channel)
  2793. {
  2794. case TIM_CHANNEL_1:
  2795. {
  2796. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  2797. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  2798. break;
  2799. }
  2800. case TIM_CHANNEL_2:
  2801. {
  2802. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
  2803. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  2804. break;
  2805. }
  2806. default :
  2807. {
  2808. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  2809. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
  2810. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  2811. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  2812. break;
  2813. }
  2814. }
  2815. /* Enable the Peripheral */
  2816. __HAL_TIM_ENABLE(htim);
  2817. /* Return function status */
  2818. return HAL_OK;
  2819. }
  2820. /**
  2821. * @brief Stops the TIM Encoder Interface in interrupt mode.
  2822. * @param htim TIM Encoder Interface handle
  2823. * @param Channel TIM Channels to be disabled
  2824. * This parameter can be one of the following values:
  2825. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  2826. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  2827. * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
  2828. * @retval HAL status
  2829. */
  2830. HAL_StatusTypeDef HAL_TIM_Encoder_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  2831. {
  2832. /* Check the parameters */
  2833. assert_param(IS_TIM_ENCODER_INTERFACE_INSTANCE(htim->Instance));
  2834. /* Disable the Input Capture channels 1 and 2
  2835. (in the EncoderInterface the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2) */
  2836. if (Channel == TIM_CHANNEL_1)
  2837. {
  2838. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  2839. /* Disable the capture compare Interrupts 1 */
  2840. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  2841. }
  2842. else if (Channel == TIM_CHANNEL_2)
  2843. {
  2844. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
  2845. /* Disable the capture compare Interrupts 2 */
  2846. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  2847. }
  2848. else
  2849. {
  2850. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  2851. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
  2852. /* Disable the capture compare Interrupts 1 and 2 */
  2853. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  2854. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  2855. }
  2856. /* Disable the Peripheral */
  2857. __HAL_TIM_DISABLE(htim);
  2858. /* Set the TIM channel(s) state */
  2859. if ((Channel == TIM_CHANNEL_1) || (Channel == TIM_CHANNEL_2))
  2860. {
  2861. TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
  2862. }
  2863. else
  2864. {
  2865. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  2866. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  2867. }
  2868. /* Return function status */
  2869. return HAL_OK;
  2870. }
  2871. /**
  2872. * @brief Starts the TIM Encoder Interface in DMA mode.
  2873. * @param htim TIM Encoder Interface handle
  2874. * @param Channel TIM Channels to be enabled
  2875. * This parameter can be one of the following values:
  2876. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  2877. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  2878. * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
  2879. * @param pData1 The destination Buffer address for IC1.
  2880. * @param pData2 The destination Buffer address for IC2.
  2881. * @param Length The length of data to be transferred from TIM peripheral to memory.
  2882. * @retval HAL status
  2883. */
  2884. HAL_StatusTypeDef HAL_TIM_Encoder_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData1,
  2885. uint32_t *pData2, uint16_t Length)
  2886. {
  2887. HAL_TIM_ChannelStateTypeDef channel_1_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_1);
  2888. HAL_TIM_ChannelStateTypeDef channel_2_state = TIM_CHANNEL_STATE_GET(htim, TIM_CHANNEL_2);
  2889. /* Check the parameters */
  2890. assert_param(IS_TIM_ENCODER_INTERFACE_INSTANCE(htim->Instance));
  2891. /* Set the TIM channel(s) state */
  2892. if (Channel == TIM_CHANNEL_1)
  2893. {
  2894. if (channel_1_state == HAL_TIM_CHANNEL_STATE_BUSY)
  2895. {
  2896. return HAL_BUSY;
  2897. }
  2898. else if (channel_1_state == HAL_TIM_CHANNEL_STATE_READY)
  2899. {
  2900. if ((pData1 == NULL) || (Length == 0U))
  2901. {
  2902. return HAL_ERROR;
  2903. }
  2904. else
  2905. {
  2906. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
  2907. }
  2908. }
  2909. else
  2910. {
  2911. return HAL_ERROR;
  2912. }
  2913. }
  2914. else if (Channel == TIM_CHANNEL_2)
  2915. {
  2916. if (channel_2_state == HAL_TIM_CHANNEL_STATE_BUSY)
  2917. {
  2918. return HAL_BUSY;
  2919. }
  2920. else if (channel_2_state == HAL_TIM_CHANNEL_STATE_READY)
  2921. {
  2922. if ((pData2 == NULL) || (Length == 0U))
  2923. {
  2924. return HAL_ERROR;
  2925. }
  2926. else
  2927. {
  2928. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
  2929. }
  2930. }
  2931. else
  2932. {
  2933. return HAL_ERROR;
  2934. }
  2935. }
  2936. else
  2937. {
  2938. if ((channel_1_state == HAL_TIM_CHANNEL_STATE_BUSY)
  2939. || (channel_2_state == HAL_TIM_CHANNEL_STATE_BUSY))
  2940. {
  2941. return HAL_BUSY;
  2942. }
  2943. else if ((channel_1_state == HAL_TIM_CHANNEL_STATE_READY)
  2944. && (channel_2_state == HAL_TIM_CHANNEL_STATE_READY))
  2945. {
  2946. if ((((pData1 == NULL) || (pData2 == NULL))) || (Length == 0U))
  2947. {
  2948. return HAL_ERROR;
  2949. }
  2950. else
  2951. {
  2952. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_BUSY);
  2953. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_BUSY);
  2954. }
  2955. }
  2956. else
  2957. {
  2958. return HAL_ERROR;
  2959. }
  2960. }
  2961. switch (Channel)
  2962. {
  2963. case TIM_CHANNEL_1:
  2964. {
  2965. /* Set the DMA capture callbacks */
  2966. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMACaptureCplt;
  2967. htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  2968. /* Set the DMA error callback */
  2969. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  2970. /* Enable the DMA channel */
  2971. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->CCR1, (uint32_t)pData1,
  2972. Length) != HAL_OK)
  2973. {
  2974. /* Return error status */
  2975. return HAL_ERROR;
  2976. }
  2977. /* Enable the TIM Input Capture DMA request */
  2978. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  2979. /* Enable the Capture compare channel */
  2980. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  2981. /* Enable the Peripheral */
  2982. __HAL_TIM_ENABLE(htim);
  2983. break;
  2984. }
  2985. case TIM_CHANNEL_2:
  2986. {
  2987. /* Set the DMA capture callbacks */
  2988. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMACaptureCplt;
  2989. htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  2990. /* Set the DMA error callback */
  2991. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError;
  2992. /* Enable the DMA channel */
  2993. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)&htim->Instance->CCR2, (uint32_t)pData2,
  2994. Length) != HAL_OK)
  2995. {
  2996. /* Return error status */
  2997. return HAL_ERROR;
  2998. }
  2999. /* Enable the TIM Input Capture DMA request */
  3000. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
  3001. /* Enable the Capture compare channel */
  3002. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
  3003. /* Enable the Peripheral */
  3004. __HAL_TIM_ENABLE(htim);
  3005. break;
  3006. }
  3007. default:
  3008. {
  3009. /* Set the DMA capture callbacks */
  3010. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMACaptureCplt;
  3011. htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  3012. /* Set the DMA error callback */
  3013. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  3014. /* Enable the DMA channel */
  3015. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->CCR1, (uint32_t)pData1,
  3016. Length) != HAL_OK)
  3017. {
  3018. /* Return error status */
  3019. return HAL_ERROR;
  3020. }
  3021. /* Set the DMA capture callbacks */
  3022. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMACaptureCplt;
  3023. htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  3024. /* Set the DMA error callback */
  3025. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
  3026. /* Enable the DMA channel */
  3027. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)&htim->Instance->CCR2, (uint32_t)pData2,
  3028. Length) != HAL_OK)
  3029. {
  3030. /* Return error status */
  3031. return HAL_ERROR;
  3032. }
  3033. /* Enable the TIM Input Capture DMA request */
  3034. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  3035. /* Enable the TIM Input Capture DMA request */
  3036. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
  3037. /* Enable the Capture compare channel */
  3038. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  3039. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
  3040. /* Enable the Peripheral */
  3041. __HAL_TIM_ENABLE(htim);
  3042. break;
  3043. }
  3044. }
  3045. /* Return function status */
  3046. return HAL_OK;
  3047. }
  3048. /**
  3049. * @brief Stops the TIM Encoder Interface in DMA mode.
  3050. * @param htim TIM Encoder Interface handle
  3051. * @param Channel TIM Channels to be enabled
  3052. * This parameter can be one of the following values:
  3053. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  3054. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  3055. * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
  3056. * @retval HAL status
  3057. */
  3058. HAL_StatusTypeDef HAL_TIM_Encoder_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
  3059. {
  3060. /* Check the parameters */
  3061. assert_param(IS_TIM_ENCODER_INTERFACE_INSTANCE(htim->Instance));
  3062. /* Disable the Input Capture channels 1 and 2
  3063. (in the EncoderInterface the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2) */
  3064. if (Channel == TIM_CHANNEL_1)
  3065. {
  3066. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  3067. /* Disable the capture compare DMA Request 1 */
  3068. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  3069. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
  3070. }
  3071. else if (Channel == TIM_CHANNEL_2)
  3072. {
  3073. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
  3074. /* Disable the capture compare DMA Request 2 */
  3075. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
  3076. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
  3077. }
  3078. else
  3079. {
  3080. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  3081. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
  3082. /* Disable the capture compare DMA Request 1 and 2 */
  3083. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  3084. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
  3085. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
  3086. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
  3087. }
  3088. /* Disable the Peripheral */
  3089. __HAL_TIM_DISABLE(htim);
  3090. /* Set the TIM channel(s) state */
  3091. if ((Channel == TIM_CHANNEL_1) || (Channel == TIM_CHANNEL_2))
  3092. {
  3093. TIM_CHANNEL_STATE_SET(htim, Channel, HAL_TIM_CHANNEL_STATE_READY);
  3094. }
  3095. else
  3096. {
  3097. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  3098. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  3099. }
  3100. /* Return function status */
  3101. return HAL_OK;
  3102. }
  3103. /**
  3104. * @}
  3105. */
  3106. /** @defgroup TIM_Exported_Functions_Group7 TIM IRQ handler management
  3107. * @brief TIM IRQ handler management
  3108. *
  3109. @verbatim
  3110. ==============================================================================
  3111. ##### IRQ handler management #####
  3112. ==============================================================================
  3113. [..]
  3114. This section provides Timer IRQ handler function.
  3115. @endverbatim
  3116. * @{
  3117. */
  3118. /**
  3119. * @brief This function handles TIM interrupts requests.
  3120. * @param htim TIM handle
  3121. * @retval None
  3122. */
  3123. void HAL_TIM_IRQHandler(TIM_HandleTypeDef *htim)
  3124. {
  3125. /* Capture compare 1 event */
  3126. if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC1) != RESET)
  3127. {
  3128. if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC1) != RESET)
  3129. {
  3130. {
  3131. __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC1);
  3132. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
  3133. /* Input capture event */
  3134. if ((htim->Instance->CCMR1 & TIM_CCMR1_CC1S) != 0x00U)
  3135. {
  3136. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  3137. htim->IC_CaptureCallback(htim);
  3138. #else
  3139. HAL_TIM_IC_CaptureCallback(htim);
  3140. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  3141. }
  3142. /* Output compare event */
  3143. else
  3144. {
  3145. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  3146. htim->OC_DelayElapsedCallback(htim);
  3147. htim->PWM_PulseFinishedCallback(htim);
  3148. #else
  3149. HAL_TIM_OC_DelayElapsedCallback(htim);
  3150. HAL_TIM_PWM_PulseFinishedCallback(htim);
  3151. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  3152. }
  3153. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  3154. }
  3155. }
  3156. }
  3157. /* Capture compare 2 event */
  3158. if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC2) != RESET)
  3159. {
  3160. if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC2) != RESET)
  3161. {
  3162. __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC2);
  3163. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
  3164. /* Input capture event */
  3165. if ((htim->Instance->CCMR1 & TIM_CCMR1_CC2S) != 0x00U)
  3166. {
  3167. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  3168. htim->IC_CaptureCallback(htim);
  3169. #else
  3170. HAL_TIM_IC_CaptureCallback(htim);
  3171. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  3172. }
  3173. /* Output compare event */
  3174. else
  3175. {
  3176. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  3177. htim->OC_DelayElapsedCallback(htim);
  3178. htim->PWM_PulseFinishedCallback(htim);
  3179. #else
  3180. HAL_TIM_OC_DelayElapsedCallback(htim);
  3181. HAL_TIM_PWM_PulseFinishedCallback(htim);
  3182. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  3183. }
  3184. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  3185. }
  3186. }
  3187. /* Capture compare 3 event */
  3188. if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC3) != RESET)
  3189. {
  3190. if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC3) != RESET)
  3191. {
  3192. __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC3);
  3193. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
  3194. /* Input capture event */
  3195. if ((htim->Instance->CCMR2 & TIM_CCMR2_CC3S) != 0x00U)
  3196. {
  3197. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  3198. htim->IC_CaptureCallback(htim);
  3199. #else
  3200. HAL_TIM_IC_CaptureCallback(htim);
  3201. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  3202. }
  3203. /* Output compare event */
  3204. else
  3205. {
  3206. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  3207. htim->OC_DelayElapsedCallback(htim);
  3208. htim->PWM_PulseFinishedCallback(htim);
  3209. #else
  3210. HAL_TIM_OC_DelayElapsedCallback(htim);
  3211. HAL_TIM_PWM_PulseFinishedCallback(htim);
  3212. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  3213. }
  3214. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  3215. }
  3216. }
  3217. /* Capture compare 4 event */
  3218. if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC4) != RESET)
  3219. {
  3220. if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC4) != RESET)
  3221. {
  3222. __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC4);
  3223. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
  3224. /* Input capture event */
  3225. if ((htim->Instance->CCMR2 & TIM_CCMR2_CC4S) != 0x00U)
  3226. {
  3227. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  3228. htim->IC_CaptureCallback(htim);
  3229. #else
  3230. HAL_TIM_IC_CaptureCallback(htim);
  3231. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  3232. }
  3233. /* Output compare event */
  3234. else
  3235. {
  3236. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  3237. htim->OC_DelayElapsedCallback(htim);
  3238. htim->PWM_PulseFinishedCallback(htim);
  3239. #else
  3240. HAL_TIM_OC_DelayElapsedCallback(htim);
  3241. HAL_TIM_PWM_PulseFinishedCallback(htim);
  3242. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  3243. }
  3244. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  3245. }
  3246. }
  3247. /* TIM Update event */
  3248. if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_UPDATE) != RESET)
  3249. {
  3250. if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_UPDATE) != RESET)
  3251. {
  3252. __HAL_TIM_CLEAR_IT(htim, TIM_IT_UPDATE);
  3253. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  3254. htim->PeriodElapsedCallback(htim);
  3255. #else
  3256. HAL_TIM_PeriodElapsedCallback(htim);
  3257. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  3258. }
  3259. }
  3260. /* TIM Trigger detection event */
  3261. if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_TRIGGER) != RESET)
  3262. {
  3263. if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_TRIGGER) != RESET)
  3264. {
  3265. __HAL_TIM_CLEAR_IT(htim, TIM_IT_TRIGGER);
  3266. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  3267. htim->TriggerCallback(htim);
  3268. #else
  3269. HAL_TIM_TriggerCallback(htim);
  3270. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  3271. }
  3272. }
  3273. }
  3274. /**
  3275. * @}
  3276. */
  3277. /** @defgroup TIM_Exported_Functions_Group8 TIM Peripheral Control functions
  3278. * @brief TIM Peripheral Control functions
  3279. *
  3280. @verbatim
  3281. ==============================================================================
  3282. ##### Peripheral Control functions #####
  3283. ==============================================================================
  3284. [..]
  3285. This section provides functions allowing to:
  3286. (+) Configure The Input Output channels for OC, PWM, IC or One Pulse mode.
  3287. (+) Configure External Clock source.
  3288. (+) Configure Master and the Slave synchronization.
  3289. (+) Configure the DMA Burst Mode.
  3290. @endverbatim
  3291. * @{
  3292. */
  3293. /**
  3294. * @brief Initializes the TIM Output Compare Channels according to the specified
  3295. * parameters in the TIM_OC_InitTypeDef.
  3296. * @param htim TIM Output Compare handle
  3297. * @param sConfig TIM Output Compare configuration structure
  3298. * @param Channel TIM Channels to configure
  3299. * This parameter can be one of the following values:
  3300. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  3301. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  3302. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  3303. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  3304. * @retval HAL status
  3305. */
  3306. HAL_StatusTypeDef HAL_TIM_OC_ConfigChannel(TIM_HandleTypeDef *htim,
  3307. const TIM_OC_InitTypeDef *sConfig,
  3308. uint32_t Channel)
  3309. {
  3310. HAL_StatusTypeDef status = HAL_OK;
  3311. /* Check the parameters */
  3312. assert_param(IS_TIM_CHANNELS(Channel));
  3313. assert_param(IS_TIM_OC_MODE(sConfig->OCMode));
  3314. assert_param(IS_TIM_OC_POLARITY(sConfig->OCPolarity));
  3315. /* Process Locked */
  3316. __HAL_LOCK(htim);
  3317. switch (Channel)
  3318. {
  3319. case TIM_CHANNEL_1:
  3320. {
  3321. /* Check the parameters */
  3322. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  3323. /* Configure the TIM Channel 1 in Output Compare */
  3324. TIM_OC1_SetConfig(htim->Instance, sConfig);
  3325. break;
  3326. }
  3327. case TIM_CHANNEL_2:
  3328. {
  3329. /* Check the parameters */
  3330. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  3331. /* Configure the TIM Channel 2 in Output Compare */
  3332. TIM_OC2_SetConfig(htim->Instance, sConfig);
  3333. break;
  3334. }
  3335. case TIM_CHANNEL_3:
  3336. {
  3337. /* Check the parameters */
  3338. assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
  3339. /* Configure the TIM Channel 3 in Output Compare */
  3340. TIM_OC3_SetConfig(htim->Instance, sConfig);
  3341. break;
  3342. }
  3343. case TIM_CHANNEL_4:
  3344. {
  3345. /* Check the parameters */
  3346. assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
  3347. /* Configure the TIM Channel 4 in Output Compare */
  3348. TIM_OC4_SetConfig(htim->Instance, sConfig);
  3349. break;
  3350. }
  3351. default:
  3352. status = HAL_ERROR;
  3353. break;
  3354. }
  3355. __HAL_UNLOCK(htim);
  3356. return status;
  3357. }
  3358. /**
  3359. * @brief Initializes the TIM Input Capture Channels according to the specified
  3360. * parameters in the TIM_IC_InitTypeDef.
  3361. * @param htim TIM IC handle
  3362. * @param sConfig TIM Input Capture configuration structure
  3363. * @param Channel TIM Channel to configure
  3364. * This parameter can be one of the following values:
  3365. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  3366. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  3367. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  3368. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  3369. * @retval HAL status
  3370. */
  3371. HAL_StatusTypeDef HAL_TIM_IC_ConfigChannel(TIM_HandleTypeDef *htim, const TIM_IC_InitTypeDef *sConfig, uint32_t Channel)
  3372. {
  3373. HAL_StatusTypeDef status = HAL_OK;
  3374. /* Check the parameters */
  3375. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  3376. assert_param(IS_TIM_IC_POLARITY(sConfig->ICPolarity));
  3377. assert_param(IS_TIM_IC_SELECTION(sConfig->ICSelection));
  3378. assert_param(IS_TIM_IC_PRESCALER(sConfig->ICPrescaler));
  3379. assert_param(IS_TIM_IC_FILTER(sConfig->ICFilter));
  3380. /* Process Locked */
  3381. __HAL_LOCK(htim);
  3382. if (Channel == TIM_CHANNEL_1)
  3383. {
  3384. /* TI1 Configuration */
  3385. TIM_TI1_SetConfig(htim->Instance,
  3386. sConfig->ICPolarity,
  3387. sConfig->ICSelection,
  3388. sConfig->ICFilter);
  3389. /* Reset the IC1PSC Bits */
  3390. htim->Instance->CCMR1 &= ~TIM_CCMR1_IC1PSC;
  3391. /* Set the IC1PSC value */
  3392. htim->Instance->CCMR1 |= sConfig->ICPrescaler;
  3393. }
  3394. else if (Channel == TIM_CHANNEL_2)
  3395. {
  3396. /* TI2 Configuration */
  3397. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  3398. TIM_TI2_SetConfig(htim->Instance,
  3399. sConfig->ICPolarity,
  3400. sConfig->ICSelection,
  3401. sConfig->ICFilter);
  3402. /* Reset the IC2PSC Bits */
  3403. htim->Instance->CCMR1 &= ~TIM_CCMR1_IC2PSC;
  3404. /* Set the IC2PSC value */
  3405. htim->Instance->CCMR1 |= (sConfig->ICPrescaler << 8U);
  3406. }
  3407. else if (Channel == TIM_CHANNEL_3)
  3408. {
  3409. /* TI3 Configuration */
  3410. assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
  3411. TIM_TI3_SetConfig(htim->Instance,
  3412. sConfig->ICPolarity,
  3413. sConfig->ICSelection,
  3414. sConfig->ICFilter);
  3415. /* Reset the IC3PSC Bits */
  3416. htim->Instance->CCMR2 &= ~TIM_CCMR2_IC3PSC;
  3417. /* Set the IC3PSC value */
  3418. htim->Instance->CCMR2 |= sConfig->ICPrescaler;
  3419. }
  3420. else if (Channel == TIM_CHANNEL_4)
  3421. {
  3422. /* TI4 Configuration */
  3423. assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
  3424. TIM_TI4_SetConfig(htim->Instance,
  3425. sConfig->ICPolarity,
  3426. sConfig->ICSelection,
  3427. sConfig->ICFilter);
  3428. /* Reset the IC4PSC Bits */
  3429. htim->Instance->CCMR2 &= ~TIM_CCMR2_IC4PSC;
  3430. /* Set the IC4PSC value */
  3431. htim->Instance->CCMR2 |= (sConfig->ICPrescaler << 8U);
  3432. }
  3433. else
  3434. {
  3435. status = HAL_ERROR;
  3436. }
  3437. __HAL_UNLOCK(htim);
  3438. return status;
  3439. }
  3440. /**
  3441. * @brief Initializes the TIM PWM channels according to the specified
  3442. * parameters in the TIM_OC_InitTypeDef.
  3443. * @param htim TIM PWM handle
  3444. * @param sConfig TIM PWM configuration structure
  3445. * @param Channel TIM Channels to be configured
  3446. * This parameter can be one of the following values:
  3447. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  3448. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  3449. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  3450. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  3451. * @retval HAL status
  3452. */
  3453. HAL_StatusTypeDef HAL_TIM_PWM_ConfigChannel(TIM_HandleTypeDef *htim,
  3454. const TIM_OC_InitTypeDef *sConfig,
  3455. uint32_t Channel)
  3456. {
  3457. HAL_StatusTypeDef status = HAL_OK;
  3458. /* Check the parameters */
  3459. assert_param(IS_TIM_CHANNELS(Channel));
  3460. assert_param(IS_TIM_PWM_MODE(sConfig->OCMode));
  3461. assert_param(IS_TIM_OC_POLARITY(sConfig->OCPolarity));
  3462. assert_param(IS_TIM_FAST_STATE(sConfig->OCFastMode));
  3463. /* Process Locked */
  3464. __HAL_LOCK(htim);
  3465. switch (Channel)
  3466. {
  3467. case TIM_CHANNEL_1:
  3468. {
  3469. /* Check the parameters */
  3470. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  3471. /* Configure the Channel 1 in PWM mode */
  3472. TIM_OC1_SetConfig(htim->Instance, sConfig);
  3473. /* Set the Preload enable bit for channel1 */
  3474. htim->Instance->CCMR1 |= TIM_CCMR1_OC1PE;
  3475. /* Configure the Output Fast mode */
  3476. htim->Instance->CCMR1 &= ~TIM_CCMR1_OC1FE;
  3477. htim->Instance->CCMR1 |= sConfig->OCFastMode;
  3478. break;
  3479. }
  3480. case TIM_CHANNEL_2:
  3481. {
  3482. /* Check the parameters */
  3483. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  3484. /* Configure the Channel 2 in PWM mode */
  3485. TIM_OC2_SetConfig(htim->Instance, sConfig);
  3486. /* Set the Preload enable bit for channel2 */
  3487. htim->Instance->CCMR1 |= TIM_CCMR1_OC2PE;
  3488. /* Configure the Output Fast mode */
  3489. htim->Instance->CCMR1 &= ~TIM_CCMR1_OC2FE;
  3490. htim->Instance->CCMR1 |= sConfig->OCFastMode << 8U;
  3491. break;
  3492. }
  3493. case TIM_CHANNEL_3:
  3494. {
  3495. /* Check the parameters */
  3496. assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
  3497. /* Configure the Channel 3 in PWM mode */
  3498. TIM_OC3_SetConfig(htim->Instance, sConfig);
  3499. /* Set the Preload enable bit for channel3 */
  3500. htim->Instance->CCMR2 |= TIM_CCMR2_OC3PE;
  3501. /* Configure the Output Fast mode */
  3502. htim->Instance->CCMR2 &= ~TIM_CCMR2_OC3FE;
  3503. htim->Instance->CCMR2 |= sConfig->OCFastMode;
  3504. break;
  3505. }
  3506. case TIM_CHANNEL_4:
  3507. {
  3508. /* Check the parameters */
  3509. assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
  3510. /* Configure the Channel 4 in PWM mode */
  3511. TIM_OC4_SetConfig(htim->Instance, sConfig);
  3512. /* Set the Preload enable bit for channel4 */
  3513. htim->Instance->CCMR2 |= TIM_CCMR2_OC4PE;
  3514. /* Configure the Output Fast mode */
  3515. htim->Instance->CCMR2 &= ~TIM_CCMR2_OC4FE;
  3516. htim->Instance->CCMR2 |= sConfig->OCFastMode << 8U;
  3517. break;
  3518. }
  3519. default:
  3520. status = HAL_ERROR;
  3521. break;
  3522. }
  3523. __HAL_UNLOCK(htim);
  3524. return status;
  3525. }
  3526. /**
  3527. * @brief Initializes the TIM One Pulse Channels according to the specified
  3528. * parameters in the TIM_OnePulse_InitTypeDef.
  3529. * @param htim TIM One Pulse handle
  3530. * @param sConfig TIM One Pulse configuration structure
  3531. * @param OutputChannel TIM output channel to configure
  3532. * This parameter can be one of the following values:
  3533. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  3534. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  3535. * @param InputChannel TIM input Channel to configure
  3536. * This parameter can be one of the following values:
  3537. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  3538. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  3539. * @note To output a waveform with a minimum delay user can enable the fast
  3540. * mode by calling the @ref __HAL_TIM_ENABLE_OCxFAST macro. Then CCx
  3541. * output is forced in response to the edge detection on TIx input,
  3542. * without taking in account the comparison.
  3543. * @retval HAL status
  3544. */
  3545. HAL_StatusTypeDef HAL_TIM_OnePulse_ConfigChannel(TIM_HandleTypeDef *htim, TIM_OnePulse_InitTypeDef *sConfig,
  3546. uint32_t OutputChannel, uint32_t InputChannel)
  3547. {
  3548. HAL_StatusTypeDef status = HAL_OK;
  3549. TIM_OC_InitTypeDef temp1;
  3550. /* Check the parameters */
  3551. assert_param(IS_TIM_OPM_CHANNELS(OutputChannel));
  3552. assert_param(IS_TIM_OPM_CHANNELS(InputChannel));
  3553. if (OutputChannel != InputChannel)
  3554. {
  3555. /* Process Locked */
  3556. __HAL_LOCK(htim);
  3557. htim->State = HAL_TIM_STATE_BUSY;
  3558. /* Extract the Output compare configuration from sConfig structure */
  3559. temp1.OCMode = sConfig->OCMode;
  3560. temp1.Pulse = sConfig->Pulse;
  3561. temp1.OCPolarity = sConfig->OCPolarity;
  3562. switch (OutputChannel)
  3563. {
  3564. case TIM_CHANNEL_1:
  3565. {
  3566. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  3567. TIM_OC1_SetConfig(htim->Instance, &temp1);
  3568. break;
  3569. }
  3570. case TIM_CHANNEL_2:
  3571. {
  3572. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  3573. TIM_OC2_SetConfig(htim->Instance, &temp1);
  3574. break;
  3575. }
  3576. default:
  3577. status = HAL_ERROR;
  3578. break;
  3579. }
  3580. if (status == HAL_OK)
  3581. {
  3582. switch (InputChannel)
  3583. {
  3584. case TIM_CHANNEL_1:
  3585. {
  3586. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  3587. TIM_TI1_SetConfig(htim->Instance, sConfig->ICPolarity,
  3588. sConfig->ICSelection, sConfig->ICFilter);
  3589. /* Reset the IC1PSC Bits */
  3590. htim->Instance->CCMR1 &= ~TIM_CCMR1_IC1PSC;
  3591. /* Select the Trigger source */
  3592. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  3593. htim->Instance->SMCR |= TIM_TS_TI1FP1;
  3594. /* Select the Slave Mode */
  3595. htim->Instance->SMCR &= ~TIM_SMCR_SMS;
  3596. htim->Instance->SMCR |= TIM_SLAVEMODE_TRIGGER;
  3597. break;
  3598. }
  3599. case TIM_CHANNEL_2:
  3600. {
  3601. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  3602. TIM_TI2_SetConfig(htim->Instance, sConfig->ICPolarity,
  3603. sConfig->ICSelection, sConfig->ICFilter);
  3604. /* Reset the IC2PSC Bits */
  3605. htim->Instance->CCMR1 &= ~TIM_CCMR1_IC2PSC;
  3606. /* Select the Trigger source */
  3607. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  3608. htim->Instance->SMCR |= TIM_TS_TI2FP2;
  3609. /* Select the Slave Mode */
  3610. htim->Instance->SMCR &= ~TIM_SMCR_SMS;
  3611. htim->Instance->SMCR |= TIM_SLAVEMODE_TRIGGER;
  3612. break;
  3613. }
  3614. default:
  3615. status = HAL_ERROR;
  3616. break;
  3617. }
  3618. }
  3619. htim->State = HAL_TIM_STATE_READY;
  3620. __HAL_UNLOCK(htim);
  3621. return status;
  3622. }
  3623. else
  3624. {
  3625. return HAL_ERROR;
  3626. }
  3627. }
  3628. /**
  3629. * @brief Configure the DMA Burst to transfer Data from the memory to the TIM peripheral
  3630. * @param htim TIM handle
  3631. * @param BurstBaseAddress TIM Base address from where the DMA will start the Data write
  3632. * This parameter can be one of the following values:
  3633. * @arg TIM_DMABASE_CR1
  3634. * @arg TIM_DMABASE_CR2
  3635. * @arg TIM_DMABASE_SMCR
  3636. * @arg TIM_DMABASE_DIER
  3637. * @arg TIM_DMABASE_SR
  3638. * @arg TIM_DMABASE_EGR
  3639. * @arg TIM_DMABASE_CCMR1
  3640. * @arg TIM_DMABASE_CCMR2
  3641. * @arg TIM_DMABASE_CCER
  3642. * @arg TIM_DMABASE_CNT
  3643. * @arg TIM_DMABASE_PSC
  3644. * @arg TIM_DMABASE_ARR
  3645. * @arg TIM_DMABASE_CCR1
  3646. * @arg TIM_DMABASE_CCR2
  3647. * @arg TIM_DMABASE_CCR3
  3648. * @arg TIM_DMABASE_CCR4
  3649. * @arg TIM_DMABASE_OR
  3650. * @param BurstRequestSrc TIM DMA Request sources
  3651. * This parameter can be one of the following values:
  3652. * @arg TIM_DMA_UPDATE: TIM update Interrupt source
  3653. * @arg TIM_DMA_CC1: TIM Capture Compare 1 DMA source
  3654. * @arg TIM_DMA_CC2: TIM Capture Compare 2 DMA source
  3655. * @arg TIM_DMA_CC3: TIM Capture Compare 3 DMA source
  3656. * @arg TIM_DMA_CC4: TIM Capture Compare 4 DMA source
  3657. * @arg TIM_DMA_TRIGGER: TIM Trigger DMA source
  3658. * @param BurstBuffer The Buffer address.
  3659. * @param BurstLength DMA Burst length. This parameter can be one value
  3660. * between: TIM_DMABURSTLENGTH_1TRANSFER and TIM_DMABURSTLENGTH_18TRANSFERS.
  3661. * @note This function should be used only when BurstLength is equal to DMA data transfer length.
  3662. * @retval HAL status
  3663. */
  3664. HAL_StatusTypeDef HAL_TIM_DMABurst_WriteStart(TIM_HandleTypeDef *htim, uint32_t BurstBaseAddress,
  3665. uint32_t BurstRequestSrc, const uint32_t *BurstBuffer, uint32_t BurstLength)
  3666. {
  3667. HAL_StatusTypeDef status;
  3668. status = HAL_TIM_DMABurst_MultiWriteStart(htim, BurstBaseAddress, BurstRequestSrc, BurstBuffer, BurstLength,
  3669. ((BurstLength) >> 8U) + 1U);
  3670. return status;
  3671. }
  3672. /**
  3673. * @brief Configure the DMA Burst to transfer multiple Data from the memory to the TIM peripheral
  3674. * @param htim TIM handle
  3675. * @param BurstBaseAddress TIM Base address from where the DMA will start the Data write
  3676. * This parameter can be one of the following values:
  3677. * @arg TIM_DMABASE_CR1
  3678. * @arg TIM_DMABASE_CR2
  3679. * @arg TIM_DMABASE_SMCR
  3680. * @arg TIM_DMABASE_DIER
  3681. * @arg TIM_DMABASE_SR
  3682. * @arg TIM_DMABASE_EGR
  3683. * @arg TIM_DMABASE_CCMR1
  3684. * @arg TIM_DMABASE_CCMR2
  3685. * @arg TIM_DMABASE_CCER
  3686. * @arg TIM_DMABASE_CNT
  3687. * @arg TIM_DMABASE_PSC
  3688. * @arg TIM_DMABASE_ARR
  3689. * @arg TIM_DMABASE_CCR1
  3690. * @arg TIM_DMABASE_CCR2
  3691. * @arg TIM_DMABASE_CCR3
  3692. * @arg TIM_DMABASE_CCR4
  3693. * @arg TIM_DMABASE_OR
  3694. * @param BurstRequestSrc TIM DMA Request sources
  3695. * This parameter can be one of the following values:
  3696. * @arg TIM_DMA_UPDATE: TIM update Interrupt source
  3697. * @arg TIM_DMA_CC1: TIM Capture Compare 1 DMA source
  3698. * @arg TIM_DMA_CC2: TIM Capture Compare 2 DMA source
  3699. * @arg TIM_DMA_CC3: TIM Capture Compare 3 DMA source
  3700. * @arg TIM_DMA_CC4: TIM Capture Compare 4 DMA source
  3701. * @arg TIM_DMA_TRIGGER: TIM Trigger DMA source
  3702. * @param BurstBuffer The Buffer address.
  3703. * @param BurstLength DMA Burst length. This parameter can be one value
  3704. * between: TIM_DMABURSTLENGTH_1TRANSFER and TIM_DMABURSTLENGTH_18TRANSFERS.
  3705. * @param DataLength Data length. This parameter can be one value
  3706. * between 1 and 0xFFFF.
  3707. * @retval HAL status
  3708. */
  3709. HAL_StatusTypeDef HAL_TIM_DMABurst_MultiWriteStart(TIM_HandleTypeDef *htim, uint32_t BurstBaseAddress,
  3710. uint32_t BurstRequestSrc, const uint32_t *BurstBuffer,
  3711. uint32_t BurstLength, uint32_t DataLength)
  3712. {
  3713. HAL_StatusTypeDef status = HAL_OK;
  3714. /* Check the parameters */
  3715. assert_param(IS_TIM_DMABURST_INSTANCE(htim->Instance));
  3716. assert_param(IS_TIM_DMA_BASE(BurstBaseAddress));
  3717. assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc));
  3718. assert_param(IS_TIM_DMA_LENGTH(BurstLength));
  3719. assert_param(IS_TIM_DMA_DATA_LENGTH(DataLength));
  3720. if (htim->DMABurstState == HAL_DMA_BURST_STATE_BUSY)
  3721. {
  3722. return HAL_BUSY;
  3723. }
  3724. else if (htim->DMABurstState == HAL_DMA_BURST_STATE_READY)
  3725. {
  3726. if ((BurstBuffer == NULL) && (BurstLength > 0U))
  3727. {
  3728. return HAL_ERROR;
  3729. }
  3730. else
  3731. {
  3732. htim->DMABurstState = HAL_DMA_BURST_STATE_BUSY;
  3733. }
  3734. }
  3735. else
  3736. {
  3737. /* nothing to do */
  3738. }
  3739. switch (BurstRequestSrc)
  3740. {
  3741. case TIM_DMA_UPDATE:
  3742. {
  3743. /* Set the DMA Period elapsed callbacks */
  3744. htim->hdma[TIM_DMA_ID_UPDATE]->XferCpltCallback = TIM_DMAPeriodElapsedCplt;
  3745. htim->hdma[TIM_DMA_ID_UPDATE]->XferHalfCpltCallback = TIM_DMAPeriodElapsedHalfCplt;
  3746. /* Set the DMA error callback */
  3747. htim->hdma[TIM_DMA_ID_UPDATE]->XferErrorCallback = TIM_DMAError ;
  3748. /* Enable the DMA channel */
  3749. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_UPDATE], (uint32_t)BurstBuffer,
  3750. (uint32_t)&htim->Instance->DMAR, DataLength) != HAL_OK)
  3751. {
  3752. /* Return error status */
  3753. return HAL_ERROR;
  3754. }
  3755. break;
  3756. }
  3757. case TIM_DMA_CC1:
  3758. {
  3759. /* Set the DMA compare callbacks */
  3760. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseCplt;
  3761. htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  3762. /* Set the DMA error callback */
  3763. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  3764. /* Enable the DMA channel */
  3765. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)BurstBuffer,
  3766. (uint32_t)&htim->Instance->DMAR, DataLength) != HAL_OK)
  3767. {
  3768. /* Return error status */
  3769. return HAL_ERROR;
  3770. }
  3771. break;
  3772. }
  3773. case TIM_DMA_CC2:
  3774. {
  3775. /* Set the DMA compare callbacks */
  3776. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseCplt;
  3777. htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  3778. /* Set the DMA error callback */
  3779. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
  3780. /* Enable the DMA channel */
  3781. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)BurstBuffer,
  3782. (uint32_t)&htim->Instance->DMAR, DataLength) != HAL_OK)
  3783. {
  3784. /* Return error status */
  3785. return HAL_ERROR;
  3786. }
  3787. break;
  3788. }
  3789. case TIM_DMA_CC3:
  3790. {
  3791. /* Set the DMA compare callbacks */
  3792. htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseCplt;
  3793. htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  3794. /* Set the DMA error callback */
  3795. htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
  3796. /* Enable the DMA channel */
  3797. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)BurstBuffer,
  3798. (uint32_t)&htim->Instance->DMAR, DataLength) != HAL_OK)
  3799. {
  3800. /* Return error status */
  3801. return HAL_ERROR;
  3802. }
  3803. break;
  3804. }
  3805. case TIM_DMA_CC4:
  3806. {
  3807. /* Set the DMA compare callbacks */
  3808. htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMADelayPulseCplt;
  3809. htim->hdma[TIM_DMA_ID_CC4]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  3810. /* Set the DMA error callback */
  3811. htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
  3812. /* Enable the DMA channel */
  3813. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)BurstBuffer,
  3814. (uint32_t)&htim->Instance->DMAR, DataLength) != HAL_OK)
  3815. {
  3816. /* Return error status */
  3817. return HAL_ERROR;
  3818. }
  3819. break;
  3820. }
  3821. case TIM_DMA_TRIGGER:
  3822. {
  3823. /* Set the DMA trigger callbacks */
  3824. htim->hdma[TIM_DMA_ID_TRIGGER]->XferCpltCallback = TIM_DMATriggerCplt;
  3825. htim->hdma[TIM_DMA_ID_TRIGGER]->XferHalfCpltCallback = TIM_DMATriggerHalfCplt;
  3826. /* Set the DMA error callback */
  3827. htim->hdma[TIM_DMA_ID_TRIGGER]->XferErrorCallback = TIM_DMAError ;
  3828. /* Enable the DMA channel */
  3829. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_TRIGGER], (uint32_t)BurstBuffer,
  3830. (uint32_t)&htim->Instance->DMAR, DataLength) != HAL_OK)
  3831. {
  3832. /* Return error status */
  3833. return HAL_ERROR;
  3834. }
  3835. break;
  3836. }
  3837. default:
  3838. status = HAL_ERROR;
  3839. break;
  3840. }
  3841. if (status == HAL_OK)
  3842. {
  3843. /* Configure the DMA Burst Mode */
  3844. htim->Instance->DCR = (BurstBaseAddress | BurstLength);
  3845. /* Enable the TIM DMA Request */
  3846. __HAL_TIM_ENABLE_DMA(htim, BurstRequestSrc);
  3847. }
  3848. /* Return function status */
  3849. return status;
  3850. }
  3851. /**
  3852. * @brief Stops the TIM DMA Burst mode
  3853. * @param htim TIM handle
  3854. * @param BurstRequestSrc TIM DMA Request sources to disable
  3855. * @retval HAL status
  3856. */
  3857. HAL_StatusTypeDef HAL_TIM_DMABurst_WriteStop(TIM_HandleTypeDef *htim, uint32_t BurstRequestSrc)
  3858. {
  3859. HAL_StatusTypeDef status = HAL_OK;
  3860. /* Check the parameters */
  3861. assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc));
  3862. /* Abort the DMA transfer (at least disable the DMA channel) */
  3863. switch (BurstRequestSrc)
  3864. {
  3865. case TIM_DMA_UPDATE:
  3866. {
  3867. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_UPDATE]);
  3868. break;
  3869. }
  3870. case TIM_DMA_CC1:
  3871. {
  3872. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
  3873. break;
  3874. }
  3875. case TIM_DMA_CC2:
  3876. {
  3877. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
  3878. break;
  3879. }
  3880. case TIM_DMA_CC3:
  3881. {
  3882. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
  3883. break;
  3884. }
  3885. case TIM_DMA_CC4:
  3886. {
  3887. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC4]);
  3888. break;
  3889. }
  3890. case TIM_DMA_TRIGGER:
  3891. {
  3892. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_TRIGGER]);
  3893. break;
  3894. }
  3895. default:
  3896. status = HAL_ERROR;
  3897. break;
  3898. }
  3899. if (status == HAL_OK)
  3900. {
  3901. /* Disable the TIM Update DMA request */
  3902. __HAL_TIM_DISABLE_DMA(htim, BurstRequestSrc);
  3903. /* Change the DMA burst operation state */
  3904. htim->DMABurstState = HAL_DMA_BURST_STATE_READY;
  3905. }
  3906. /* Return function status */
  3907. return status;
  3908. }
  3909. /**
  3910. * @brief Configure the DMA Burst to transfer Data from the TIM peripheral to the memory
  3911. * @param htim TIM handle
  3912. * @param BurstBaseAddress TIM Base address from where the DMA will start the Data read
  3913. * This parameter can be one of the following values:
  3914. * @arg TIM_DMABASE_CR1
  3915. * @arg TIM_DMABASE_CR2
  3916. * @arg TIM_DMABASE_SMCR
  3917. * @arg TIM_DMABASE_DIER
  3918. * @arg TIM_DMABASE_SR
  3919. * @arg TIM_DMABASE_EGR
  3920. * @arg TIM_DMABASE_CCMR1
  3921. * @arg TIM_DMABASE_CCMR2
  3922. * @arg TIM_DMABASE_CCER
  3923. * @arg TIM_DMABASE_CNT
  3924. * @arg TIM_DMABASE_PSC
  3925. * @arg TIM_DMABASE_ARR
  3926. * @arg TIM_DMABASE_CCR1
  3927. * @arg TIM_DMABASE_CCR2
  3928. * @arg TIM_DMABASE_CCR3
  3929. * @arg TIM_DMABASE_CCR4
  3930. * @arg TIM_DMABASE_OR
  3931. * @param BurstRequestSrc TIM DMA Request sources
  3932. * This parameter can be one of the following values:
  3933. * @arg TIM_DMA_UPDATE: TIM update Interrupt source
  3934. * @arg TIM_DMA_CC1: TIM Capture Compare 1 DMA source
  3935. * @arg TIM_DMA_CC2: TIM Capture Compare 2 DMA source
  3936. * @arg TIM_DMA_CC3: TIM Capture Compare 3 DMA source
  3937. * @arg TIM_DMA_CC4: TIM Capture Compare 4 DMA source
  3938. * @arg TIM_DMA_TRIGGER: TIM Trigger DMA source
  3939. * @param BurstBuffer The Buffer address.
  3940. * @param BurstLength DMA Burst length. This parameter can be one value
  3941. * between: TIM_DMABURSTLENGTH_1TRANSFER and TIM_DMABURSTLENGTH_18TRANSFERS.
  3942. * @note This function should be used only when BurstLength is equal to DMA data transfer length.
  3943. * @retval HAL status
  3944. */
  3945. HAL_StatusTypeDef HAL_TIM_DMABurst_ReadStart(TIM_HandleTypeDef *htim, uint32_t BurstBaseAddress,
  3946. uint32_t BurstRequestSrc, uint32_t *BurstBuffer, uint32_t BurstLength)
  3947. {
  3948. HAL_StatusTypeDef status;
  3949. status = HAL_TIM_DMABurst_MultiReadStart(htim, BurstBaseAddress, BurstRequestSrc, BurstBuffer, BurstLength,
  3950. ((BurstLength) >> 8U) + 1U);
  3951. return status;
  3952. }
  3953. /**
  3954. * @brief Configure the DMA Burst to transfer Data from the TIM peripheral to the memory
  3955. * @param htim TIM handle
  3956. * @param BurstBaseAddress TIM Base address from where the DMA will start the Data read
  3957. * This parameter can be one of the following values:
  3958. * @arg TIM_DMABASE_CR1
  3959. * @arg TIM_DMABASE_CR2
  3960. * @arg TIM_DMABASE_SMCR
  3961. * @arg TIM_DMABASE_DIER
  3962. * @arg TIM_DMABASE_SR
  3963. * @arg TIM_DMABASE_EGR
  3964. * @arg TIM_DMABASE_CCMR1
  3965. * @arg TIM_DMABASE_CCMR2
  3966. * @arg TIM_DMABASE_CCER
  3967. * @arg TIM_DMABASE_CNT
  3968. * @arg TIM_DMABASE_PSC
  3969. * @arg TIM_DMABASE_ARR
  3970. * @arg TIM_DMABASE_CCR1
  3971. * @arg TIM_DMABASE_CCR2
  3972. * @arg TIM_DMABASE_CCR3
  3973. * @arg TIM_DMABASE_CCR4
  3974. * @arg TIM_DMABASE_OR
  3975. * @param BurstRequestSrc TIM DMA Request sources
  3976. * This parameter can be one of the following values:
  3977. * @arg TIM_DMA_UPDATE: TIM update Interrupt source
  3978. * @arg TIM_DMA_CC1: TIM Capture Compare 1 DMA source
  3979. * @arg TIM_DMA_CC2: TIM Capture Compare 2 DMA source
  3980. * @arg TIM_DMA_CC3: TIM Capture Compare 3 DMA source
  3981. * @arg TIM_DMA_CC4: TIM Capture Compare 4 DMA source
  3982. * @arg TIM_DMA_TRIGGER: TIM Trigger DMA source
  3983. * @param BurstBuffer The Buffer address.
  3984. * @param BurstLength DMA Burst length. This parameter can be one value
  3985. * between: TIM_DMABURSTLENGTH_1TRANSFER and TIM_DMABURSTLENGTH_18TRANSFERS.
  3986. * @param DataLength Data length. This parameter can be one value
  3987. * between 1 and 0xFFFF.
  3988. * @retval HAL status
  3989. */
  3990. HAL_StatusTypeDef HAL_TIM_DMABurst_MultiReadStart(TIM_HandleTypeDef *htim, uint32_t BurstBaseAddress,
  3991. uint32_t BurstRequestSrc, uint32_t *BurstBuffer,
  3992. uint32_t BurstLength, uint32_t DataLength)
  3993. {
  3994. HAL_StatusTypeDef status = HAL_OK;
  3995. /* Check the parameters */
  3996. assert_param(IS_TIM_DMABURST_INSTANCE(htim->Instance));
  3997. assert_param(IS_TIM_DMA_BASE(BurstBaseAddress));
  3998. assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc));
  3999. assert_param(IS_TIM_DMA_LENGTH(BurstLength));
  4000. assert_param(IS_TIM_DMA_DATA_LENGTH(DataLength));
  4001. if (htim->DMABurstState == HAL_DMA_BURST_STATE_BUSY)
  4002. {
  4003. return HAL_BUSY;
  4004. }
  4005. else if (htim->DMABurstState == HAL_DMA_BURST_STATE_READY)
  4006. {
  4007. if ((BurstBuffer == NULL) && (BurstLength > 0U))
  4008. {
  4009. return HAL_ERROR;
  4010. }
  4011. else
  4012. {
  4013. htim->DMABurstState = HAL_DMA_BURST_STATE_BUSY;
  4014. }
  4015. }
  4016. else
  4017. {
  4018. /* nothing to do */
  4019. }
  4020. switch (BurstRequestSrc)
  4021. {
  4022. case TIM_DMA_UPDATE:
  4023. {
  4024. /* Set the DMA Period elapsed callbacks */
  4025. htim->hdma[TIM_DMA_ID_UPDATE]->XferCpltCallback = TIM_DMAPeriodElapsedCplt;
  4026. htim->hdma[TIM_DMA_ID_UPDATE]->XferHalfCpltCallback = TIM_DMAPeriodElapsedHalfCplt;
  4027. /* Set the DMA error callback */
  4028. htim->hdma[TIM_DMA_ID_UPDATE]->XferErrorCallback = TIM_DMAError ;
  4029. /* Enable the DMA channel */
  4030. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_UPDATE], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer,
  4031. DataLength) != HAL_OK)
  4032. {
  4033. /* Return error status */
  4034. return HAL_ERROR;
  4035. }
  4036. break;
  4037. }
  4038. case TIM_DMA_CC1:
  4039. {
  4040. /* Set the DMA capture callbacks */
  4041. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMACaptureCplt;
  4042. htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  4043. /* Set the DMA error callback */
  4044. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  4045. /* Enable the DMA channel */
  4046. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer,
  4047. DataLength) != HAL_OK)
  4048. {
  4049. /* Return error status */
  4050. return HAL_ERROR;
  4051. }
  4052. break;
  4053. }
  4054. case TIM_DMA_CC2:
  4055. {
  4056. /* Set the DMA capture callbacks */
  4057. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMACaptureCplt;
  4058. htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  4059. /* Set the DMA error callback */
  4060. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
  4061. /* Enable the DMA channel */
  4062. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer,
  4063. DataLength) != HAL_OK)
  4064. {
  4065. /* Return error status */
  4066. return HAL_ERROR;
  4067. }
  4068. break;
  4069. }
  4070. case TIM_DMA_CC3:
  4071. {
  4072. /* Set the DMA capture callbacks */
  4073. htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMACaptureCplt;
  4074. htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  4075. /* Set the DMA error callback */
  4076. htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
  4077. /* Enable the DMA channel */
  4078. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer,
  4079. DataLength) != HAL_OK)
  4080. {
  4081. /* Return error status */
  4082. return HAL_ERROR;
  4083. }
  4084. break;
  4085. }
  4086. case TIM_DMA_CC4:
  4087. {
  4088. /* Set the DMA capture callbacks */
  4089. htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMACaptureCplt;
  4090. htim->hdma[TIM_DMA_ID_CC4]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  4091. /* Set the DMA error callback */
  4092. htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
  4093. /* Enable the DMA channel */
  4094. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer,
  4095. DataLength) != HAL_OK)
  4096. {
  4097. /* Return error status */
  4098. return HAL_ERROR;
  4099. }
  4100. break;
  4101. }
  4102. case TIM_DMA_TRIGGER:
  4103. {
  4104. /* Set the DMA trigger callbacks */
  4105. htim->hdma[TIM_DMA_ID_TRIGGER]->XferCpltCallback = TIM_DMATriggerCplt;
  4106. htim->hdma[TIM_DMA_ID_TRIGGER]->XferHalfCpltCallback = TIM_DMATriggerHalfCplt;
  4107. /* Set the DMA error callback */
  4108. htim->hdma[TIM_DMA_ID_TRIGGER]->XferErrorCallback = TIM_DMAError ;
  4109. /* Enable the DMA channel */
  4110. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_TRIGGER], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer,
  4111. DataLength) != HAL_OK)
  4112. {
  4113. /* Return error status */
  4114. return HAL_ERROR;
  4115. }
  4116. break;
  4117. }
  4118. default:
  4119. status = HAL_ERROR;
  4120. break;
  4121. }
  4122. if (status == HAL_OK)
  4123. {
  4124. /* Configure the DMA Burst Mode */
  4125. htim->Instance->DCR = (BurstBaseAddress | BurstLength);
  4126. /* Enable the TIM DMA Request */
  4127. __HAL_TIM_ENABLE_DMA(htim, BurstRequestSrc);
  4128. }
  4129. /* Return function status */
  4130. return status;
  4131. }
  4132. /**
  4133. * @brief Stop the DMA burst reading
  4134. * @param htim TIM handle
  4135. * @param BurstRequestSrc TIM DMA Request sources to disable.
  4136. * @retval HAL status
  4137. */
  4138. HAL_StatusTypeDef HAL_TIM_DMABurst_ReadStop(TIM_HandleTypeDef *htim, uint32_t BurstRequestSrc)
  4139. {
  4140. HAL_StatusTypeDef status = HAL_OK;
  4141. /* Check the parameters */
  4142. assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc));
  4143. /* Abort the DMA transfer (at least disable the DMA channel) */
  4144. switch (BurstRequestSrc)
  4145. {
  4146. case TIM_DMA_UPDATE:
  4147. {
  4148. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_UPDATE]);
  4149. break;
  4150. }
  4151. case TIM_DMA_CC1:
  4152. {
  4153. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
  4154. break;
  4155. }
  4156. case TIM_DMA_CC2:
  4157. {
  4158. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
  4159. break;
  4160. }
  4161. case TIM_DMA_CC3:
  4162. {
  4163. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
  4164. break;
  4165. }
  4166. case TIM_DMA_CC4:
  4167. {
  4168. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC4]);
  4169. break;
  4170. }
  4171. case TIM_DMA_TRIGGER:
  4172. {
  4173. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_TRIGGER]);
  4174. break;
  4175. }
  4176. default:
  4177. status = HAL_ERROR;
  4178. break;
  4179. }
  4180. if (status == HAL_OK)
  4181. {
  4182. /* Disable the TIM Update DMA request */
  4183. __HAL_TIM_DISABLE_DMA(htim, BurstRequestSrc);
  4184. /* Change the DMA burst operation state */
  4185. htim->DMABurstState = HAL_DMA_BURST_STATE_READY;
  4186. }
  4187. /* Return function status */
  4188. return status;
  4189. }
  4190. /**
  4191. * @brief Generate a software event
  4192. * @param htim TIM handle
  4193. * @param EventSource specifies the event source.
  4194. * This parameter can be one of the following values:
  4195. * @arg TIM_EVENTSOURCE_UPDATE: Timer update Event source
  4196. * @arg TIM_EVENTSOURCE_CC1: Timer Capture Compare 1 Event source
  4197. * @arg TIM_EVENTSOURCE_CC2: Timer Capture Compare 2 Event source
  4198. * @arg TIM_EVENTSOURCE_CC3: Timer Capture Compare 3 Event source
  4199. * @arg TIM_EVENTSOURCE_CC4: Timer Capture Compare 4 Event source
  4200. * @arg TIM_EVENTSOURCE_TRIGGER: Timer Trigger Event source
  4201. * @note Basic timers can only generate an update event.
  4202. * @retval HAL status
  4203. */
  4204. HAL_StatusTypeDef HAL_TIM_GenerateEvent(TIM_HandleTypeDef *htim, uint32_t EventSource)
  4205. {
  4206. /* Check the parameters */
  4207. assert_param(IS_TIM_INSTANCE(htim->Instance));
  4208. assert_param(IS_TIM_EVENT_SOURCE(EventSource));
  4209. /* Process Locked */
  4210. __HAL_LOCK(htim);
  4211. /* Change the TIM state */
  4212. htim->State = HAL_TIM_STATE_BUSY;
  4213. /* Set the event sources */
  4214. htim->Instance->EGR = EventSource;
  4215. /* Change the TIM state */
  4216. htim->State = HAL_TIM_STATE_READY;
  4217. __HAL_UNLOCK(htim);
  4218. /* Return function status */
  4219. return HAL_OK;
  4220. }
  4221. /**
  4222. * @brief Configures the OCRef clear feature
  4223. * @param htim TIM handle
  4224. * @param sClearInputConfig pointer to a TIM_ClearInputConfigTypeDef structure that
  4225. * contains the OCREF clear feature and parameters for the TIM peripheral.
  4226. * @param Channel specifies the TIM Channel
  4227. * This parameter can be one of the following values:
  4228. * @arg TIM_CHANNEL_1: TIM Channel 1
  4229. * @arg TIM_CHANNEL_2: TIM Channel 2
  4230. * @arg TIM_CHANNEL_3: TIM Channel 3
  4231. * @arg TIM_CHANNEL_4: TIM Channel 4
  4232. * @retval HAL status
  4233. */
  4234. HAL_StatusTypeDef HAL_TIM_ConfigOCrefClear(TIM_HandleTypeDef *htim,
  4235. const TIM_ClearInputConfigTypeDef *sClearInputConfig,
  4236. uint32_t Channel)
  4237. {
  4238. HAL_StatusTypeDef status = HAL_OK;
  4239. /* Check the parameters */
  4240. assert_param(IS_TIM_OCXREF_CLEAR_INSTANCE(htim->Instance));
  4241. assert_param(IS_TIM_CLEARINPUT_SOURCE(sClearInputConfig->ClearInputSource));
  4242. /* Process Locked */
  4243. __HAL_LOCK(htim);
  4244. htim->State = HAL_TIM_STATE_BUSY;
  4245. switch (sClearInputConfig->ClearInputSource)
  4246. {
  4247. case TIM_CLEARINPUTSOURCE_NONE:
  4248. {
  4249. /* Clear the OCREF clear selection bit and the the ETR Bits */
  4250. CLEAR_BIT(htim->Instance->SMCR, (TIM_SMCR_ETF | TIM_SMCR_ETPS | TIM_SMCR_ECE | TIM_SMCR_ETP));
  4251. break;
  4252. }
  4253. case TIM_CLEARINPUTSOURCE_ETR:
  4254. {
  4255. /* Check the parameters */
  4256. assert_param(IS_TIM_CLEARINPUT_POLARITY(sClearInputConfig->ClearInputPolarity));
  4257. assert_param(IS_TIM_CLEARINPUT_PRESCALER(sClearInputConfig->ClearInputPrescaler));
  4258. assert_param(IS_TIM_CLEARINPUT_FILTER(sClearInputConfig->ClearInputFilter));
  4259. /* When OCRef clear feature is used with ETR source, ETR prescaler must be off */
  4260. if (sClearInputConfig->ClearInputPrescaler != TIM_CLEARINPUTPRESCALER_DIV1)
  4261. {
  4262. htim->State = HAL_TIM_STATE_READY;
  4263. __HAL_UNLOCK(htim);
  4264. return HAL_ERROR;
  4265. }
  4266. TIM_ETR_SetConfig(htim->Instance,
  4267. sClearInputConfig->ClearInputPrescaler,
  4268. sClearInputConfig->ClearInputPolarity,
  4269. sClearInputConfig->ClearInputFilter);
  4270. break;
  4271. }
  4272. default:
  4273. status = HAL_ERROR;
  4274. break;
  4275. }
  4276. if (status == HAL_OK)
  4277. {
  4278. switch (Channel)
  4279. {
  4280. case TIM_CHANNEL_1:
  4281. {
  4282. if (sClearInputConfig->ClearInputState != (uint32_t)DISABLE)
  4283. {
  4284. /* Enable the OCREF clear feature for Channel 1 */
  4285. SET_BIT(htim->Instance->CCMR1, TIM_CCMR1_OC1CE);
  4286. }
  4287. else
  4288. {
  4289. /* Disable the OCREF clear feature for Channel 1 */
  4290. CLEAR_BIT(htim->Instance->CCMR1, TIM_CCMR1_OC1CE);
  4291. }
  4292. break;
  4293. }
  4294. case TIM_CHANNEL_2:
  4295. {
  4296. if (sClearInputConfig->ClearInputState != (uint32_t)DISABLE)
  4297. {
  4298. /* Enable the OCREF clear feature for Channel 2 */
  4299. SET_BIT(htim->Instance->CCMR1, TIM_CCMR1_OC2CE);
  4300. }
  4301. else
  4302. {
  4303. /* Disable the OCREF clear feature for Channel 2 */
  4304. CLEAR_BIT(htim->Instance->CCMR1, TIM_CCMR1_OC2CE);
  4305. }
  4306. break;
  4307. }
  4308. case TIM_CHANNEL_3:
  4309. {
  4310. if (sClearInputConfig->ClearInputState != (uint32_t)DISABLE)
  4311. {
  4312. /* Enable the OCREF clear feature for Channel 3 */
  4313. SET_BIT(htim->Instance->CCMR2, TIM_CCMR2_OC3CE);
  4314. }
  4315. else
  4316. {
  4317. /* Disable the OCREF clear feature for Channel 3 */
  4318. CLEAR_BIT(htim->Instance->CCMR2, TIM_CCMR2_OC3CE);
  4319. }
  4320. break;
  4321. }
  4322. case TIM_CHANNEL_4:
  4323. {
  4324. if (sClearInputConfig->ClearInputState != (uint32_t)DISABLE)
  4325. {
  4326. /* Enable the OCREF clear feature for Channel 4 */
  4327. SET_BIT(htim->Instance->CCMR2, TIM_CCMR2_OC4CE);
  4328. }
  4329. else
  4330. {
  4331. /* Disable the OCREF clear feature for Channel 4 */
  4332. CLEAR_BIT(htim->Instance->CCMR2, TIM_CCMR2_OC4CE);
  4333. }
  4334. break;
  4335. }
  4336. default:
  4337. break;
  4338. }
  4339. }
  4340. htim->State = HAL_TIM_STATE_READY;
  4341. __HAL_UNLOCK(htim);
  4342. return status;
  4343. }
  4344. /**
  4345. * @brief Configures the clock source to be used
  4346. * @param htim TIM handle
  4347. * @param sClockSourceConfig pointer to a TIM_ClockConfigTypeDef structure that
  4348. * contains the clock source information for the TIM peripheral.
  4349. * @retval HAL status
  4350. */
  4351. HAL_StatusTypeDef HAL_TIM_ConfigClockSource(TIM_HandleTypeDef *htim, const TIM_ClockConfigTypeDef *sClockSourceConfig)
  4352. {
  4353. HAL_StatusTypeDef status = HAL_OK;
  4354. uint32_t tmpsmcr;
  4355. /* Process Locked */
  4356. __HAL_LOCK(htim);
  4357. htim->State = HAL_TIM_STATE_BUSY;
  4358. /* Check the parameters */
  4359. assert_param(IS_TIM_CLOCKSOURCE(sClockSourceConfig->ClockSource));
  4360. /* Reset the SMS, TS, ECE, ETPS and ETRF bits */
  4361. tmpsmcr = htim->Instance->SMCR;
  4362. tmpsmcr &= ~(TIM_SMCR_SMS | TIM_SMCR_TS);
  4363. tmpsmcr &= ~(TIM_SMCR_ETF | TIM_SMCR_ETPS | TIM_SMCR_ECE | TIM_SMCR_ETP);
  4364. htim->Instance->SMCR = tmpsmcr;
  4365. switch (sClockSourceConfig->ClockSource)
  4366. {
  4367. case TIM_CLOCKSOURCE_INTERNAL:
  4368. {
  4369. assert_param(IS_TIM_INSTANCE(htim->Instance));
  4370. break;
  4371. }
  4372. case TIM_CLOCKSOURCE_ETRMODE1:
  4373. {
  4374. /* Check whether or not the timer instance supports external trigger input mode 1 (ETRF)*/
  4375. assert_param(IS_TIM_CLOCKSOURCE_ETRMODE1_INSTANCE(htim->Instance));
  4376. /* Check ETR input conditioning related parameters */
  4377. assert_param(IS_TIM_CLOCKPRESCALER(sClockSourceConfig->ClockPrescaler));
  4378. assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));
  4379. assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));
  4380. /* Configure the ETR Clock source */
  4381. TIM_ETR_SetConfig(htim->Instance,
  4382. sClockSourceConfig->ClockPrescaler,
  4383. sClockSourceConfig->ClockPolarity,
  4384. sClockSourceConfig->ClockFilter);
  4385. /* Select the External clock mode1 and the ETRF trigger */
  4386. tmpsmcr = htim->Instance->SMCR;
  4387. tmpsmcr |= (TIM_SLAVEMODE_EXTERNAL1 | TIM_CLOCKSOURCE_ETRMODE1);
  4388. /* Write to TIMx SMCR */
  4389. htim->Instance->SMCR = tmpsmcr;
  4390. break;
  4391. }
  4392. case TIM_CLOCKSOURCE_ETRMODE2:
  4393. {
  4394. /* Check whether or not the timer instance supports external trigger input mode 2 (ETRF)*/
  4395. assert_param(IS_TIM_CLOCKSOURCE_ETRMODE2_INSTANCE(htim->Instance));
  4396. /* Check ETR input conditioning related parameters */
  4397. assert_param(IS_TIM_CLOCKPRESCALER(sClockSourceConfig->ClockPrescaler));
  4398. assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));
  4399. assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));
  4400. /* Configure the ETR Clock source */
  4401. TIM_ETR_SetConfig(htim->Instance,
  4402. sClockSourceConfig->ClockPrescaler,
  4403. sClockSourceConfig->ClockPolarity,
  4404. sClockSourceConfig->ClockFilter);
  4405. /* Enable the External clock mode2 */
  4406. htim->Instance->SMCR |= TIM_SMCR_ECE;
  4407. break;
  4408. }
  4409. case TIM_CLOCKSOURCE_TI1:
  4410. {
  4411. /* Check whether or not the timer instance supports external clock mode 1 */
  4412. assert_param(IS_TIM_CLOCKSOURCE_TIX_INSTANCE(htim->Instance));
  4413. /* Check TI1 input conditioning related parameters */
  4414. assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));
  4415. assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));
  4416. TIM_TI1_ConfigInputStage(htim->Instance,
  4417. sClockSourceConfig->ClockPolarity,
  4418. sClockSourceConfig->ClockFilter);
  4419. TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_TI1);
  4420. break;
  4421. }
  4422. case TIM_CLOCKSOURCE_TI2:
  4423. {
  4424. /* Check whether or not the timer instance supports external clock mode 1 (ETRF)*/
  4425. assert_param(IS_TIM_CLOCKSOURCE_TIX_INSTANCE(htim->Instance));
  4426. /* Check TI2 input conditioning related parameters */
  4427. assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));
  4428. assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));
  4429. TIM_TI2_ConfigInputStage(htim->Instance,
  4430. sClockSourceConfig->ClockPolarity,
  4431. sClockSourceConfig->ClockFilter);
  4432. TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_TI2);
  4433. break;
  4434. }
  4435. case TIM_CLOCKSOURCE_TI1ED:
  4436. {
  4437. /* Check whether or not the timer instance supports external clock mode 1 */
  4438. assert_param(IS_TIM_CLOCKSOURCE_TIX_INSTANCE(htim->Instance));
  4439. /* Check TI1 input conditioning related parameters */
  4440. assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));
  4441. assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));
  4442. TIM_TI1_ConfigInputStage(htim->Instance,
  4443. sClockSourceConfig->ClockPolarity,
  4444. sClockSourceConfig->ClockFilter);
  4445. TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_TI1ED);
  4446. break;
  4447. }
  4448. case TIM_CLOCKSOURCE_ITR0:
  4449. case TIM_CLOCKSOURCE_ITR1:
  4450. case TIM_CLOCKSOURCE_ITR2:
  4451. case TIM_CLOCKSOURCE_ITR3:
  4452. {
  4453. /* Check whether or not the timer instance supports internal trigger input */
  4454. assert_param(IS_TIM_CLOCKSOURCE_ITRX_INSTANCE(htim->Instance));
  4455. TIM_ITRx_SetConfig(htim->Instance, sClockSourceConfig->ClockSource);
  4456. break;
  4457. }
  4458. default:
  4459. status = HAL_ERROR;
  4460. break;
  4461. }
  4462. htim->State = HAL_TIM_STATE_READY;
  4463. __HAL_UNLOCK(htim);
  4464. return status;
  4465. }
  4466. /**
  4467. * @brief Selects the signal connected to the TI1 input: direct from CH1_input
  4468. * or a XOR combination between CH1_input, CH2_input & CH3_input
  4469. * @param htim TIM handle.
  4470. * @param TI1_Selection Indicate whether or not channel 1 is connected to the
  4471. * output of a XOR gate.
  4472. * This parameter can be one of the following values:
  4473. * @arg TIM_TI1SELECTION_CH1: The TIMx_CH1 pin is connected to TI1 input
  4474. * @arg TIM_TI1SELECTION_XORCOMBINATION: The TIMx_CH1, CH2 and CH3
  4475. * pins are connected to the TI1 input (XOR combination)
  4476. * @retval HAL status
  4477. */
  4478. HAL_StatusTypeDef HAL_TIM_ConfigTI1Input(TIM_HandleTypeDef *htim, uint32_t TI1_Selection)
  4479. {
  4480. uint32_t tmpcr2;
  4481. /* Check the parameters */
  4482. assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
  4483. assert_param(IS_TIM_TI1SELECTION(TI1_Selection));
  4484. /* Get the TIMx CR2 register value */
  4485. tmpcr2 = htim->Instance->CR2;
  4486. /* Reset the TI1 selection */
  4487. tmpcr2 &= ~TIM_CR2_TI1S;
  4488. /* Set the TI1 selection */
  4489. tmpcr2 |= TI1_Selection;
  4490. /* Write to TIMxCR2 */
  4491. htim->Instance->CR2 = tmpcr2;
  4492. return HAL_OK;
  4493. }
  4494. /**
  4495. * @brief Configures the TIM in Slave mode
  4496. * @param htim TIM handle.
  4497. * @param sSlaveConfig pointer to a TIM_SlaveConfigTypeDef structure that
  4498. * contains the selected trigger (internal trigger input, filtered
  4499. * timer input or external trigger input) and the Slave mode
  4500. * (Disable, Reset, Gated, Trigger, External clock mode 1).
  4501. * @retval HAL status
  4502. */
  4503. HAL_StatusTypeDef HAL_TIM_SlaveConfigSynchro(TIM_HandleTypeDef *htim, const TIM_SlaveConfigTypeDef *sSlaveConfig)
  4504. {
  4505. /* Check the parameters */
  4506. assert_param(IS_TIM_SLAVE_INSTANCE(htim->Instance));
  4507. assert_param(IS_TIM_SLAVE_MODE(sSlaveConfig->SlaveMode));
  4508. assert_param(IS_TIM_TRIGGER_SELECTION(sSlaveConfig->InputTrigger));
  4509. __HAL_LOCK(htim);
  4510. htim->State = HAL_TIM_STATE_BUSY;
  4511. if (TIM_SlaveTimer_SetConfig(htim, sSlaveConfig) != HAL_OK)
  4512. {
  4513. htim->State = HAL_TIM_STATE_READY;
  4514. __HAL_UNLOCK(htim);
  4515. return HAL_ERROR;
  4516. }
  4517. /* Disable Trigger Interrupt */
  4518. __HAL_TIM_DISABLE_IT(htim, TIM_IT_TRIGGER);
  4519. /* Disable Trigger DMA request */
  4520. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_TRIGGER);
  4521. htim->State = HAL_TIM_STATE_READY;
  4522. __HAL_UNLOCK(htim);
  4523. return HAL_OK;
  4524. }
  4525. /**
  4526. * @brief Configures the TIM in Slave mode in interrupt mode
  4527. * @param htim TIM handle.
  4528. * @param sSlaveConfig pointer to a TIM_SlaveConfigTypeDef structure that
  4529. * contains the selected trigger (internal trigger input, filtered
  4530. * timer input or external trigger input) and the Slave mode
  4531. * (Disable, Reset, Gated, Trigger, External clock mode 1).
  4532. * @retval HAL status
  4533. */
  4534. HAL_StatusTypeDef HAL_TIM_SlaveConfigSynchro_IT(TIM_HandleTypeDef *htim,
  4535. const TIM_SlaveConfigTypeDef *sSlaveConfig)
  4536. {
  4537. /* Check the parameters */
  4538. assert_param(IS_TIM_SLAVE_INSTANCE(htim->Instance));
  4539. assert_param(IS_TIM_SLAVE_MODE(sSlaveConfig->SlaveMode));
  4540. assert_param(IS_TIM_TRIGGER_SELECTION(sSlaveConfig->InputTrigger));
  4541. __HAL_LOCK(htim);
  4542. htim->State = HAL_TIM_STATE_BUSY;
  4543. if (TIM_SlaveTimer_SetConfig(htim, sSlaveConfig) != HAL_OK)
  4544. {
  4545. htim->State = HAL_TIM_STATE_READY;
  4546. __HAL_UNLOCK(htim);
  4547. return HAL_ERROR;
  4548. }
  4549. /* Enable Trigger Interrupt */
  4550. __HAL_TIM_ENABLE_IT(htim, TIM_IT_TRIGGER);
  4551. /* Disable Trigger DMA request */
  4552. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_TRIGGER);
  4553. htim->State = HAL_TIM_STATE_READY;
  4554. __HAL_UNLOCK(htim);
  4555. return HAL_OK;
  4556. }
  4557. /**
  4558. * @brief Read the captured value from Capture Compare unit
  4559. * @param htim TIM handle.
  4560. * @param Channel TIM Channels to be enabled
  4561. * This parameter can be one of the following values:
  4562. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  4563. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  4564. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  4565. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  4566. * @retval Captured value
  4567. */
  4568. uint32_t HAL_TIM_ReadCapturedValue(const TIM_HandleTypeDef *htim, uint32_t Channel)
  4569. {
  4570. uint32_t tmpreg = 0U;
  4571. switch (Channel)
  4572. {
  4573. case TIM_CHANNEL_1:
  4574. {
  4575. /* Check the parameters */
  4576. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  4577. /* Return the capture 1 value */
  4578. tmpreg = htim->Instance->CCR1;
  4579. break;
  4580. }
  4581. case TIM_CHANNEL_2:
  4582. {
  4583. /* Check the parameters */
  4584. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  4585. /* Return the capture 2 value */
  4586. tmpreg = htim->Instance->CCR2;
  4587. break;
  4588. }
  4589. case TIM_CHANNEL_3:
  4590. {
  4591. /* Check the parameters */
  4592. assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
  4593. /* Return the capture 3 value */
  4594. tmpreg = htim->Instance->CCR3;
  4595. break;
  4596. }
  4597. case TIM_CHANNEL_4:
  4598. {
  4599. /* Check the parameters */
  4600. assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
  4601. /* Return the capture 4 value */
  4602. tmpreg = htim->Instance->CCR4;
  4603. break;
  4604. }
  4605. default:
  4606. break;
  4607. }
  4608. return tmpreg;
  4609. }
  4610. /**
  4611. * @}
  4612. */
  4613. /** @defgroup TIM_Exported_Functions_Group9 TIM Callbacks functions
  4614. * @brief TIM Callbacks functions
  4615. *
  4616. @verbatim
  4617. ==============================================================================
  4618. ##### TIM Callbacks functions #####
  4619. ==============================================================================
  4620. [..]
  4621. This section provides TIM callback functions:
  4622. (+) TIM Period elapsed callback
  4623. (+) TIM Output Compare callback
  4624. (+) TIM Input capture callback
  4625. (+) TIM Trigger callback
  4626. (+) TIM Error callback
  4627. @endverbatim
  4628. * @{
  4629. */
  4630. /**
  4631. * @brief Period elapsed callback in non-blocking mode
  4632. * @param htim TIM handle
  4633. * @retval None
  4634. */
  4635. __weak void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  4636. {
  4637. /* Prevent unused argument(s) compilation warning */
  4638. UNUSED(htim);
  4639. /* NOTE : This function should not be modified, when the callback is needed,
  4640. the HAL_TIM_PeriodElapsedCallback could be implemented in the user file
  4641. */
  4642. }
  4643. /**
  4644. * @brief Period elapsed half complete callback in non-blocking mode
  4645. * @param htim TIM handle
  4646. * @retval None
  4647. */
  4648. __weak void HAL_TIM_PeriodElapsedHalfCpltCallback(TIM_HandleTypeDef *htim)
  4649. {
  4650. /* Prevent unused argument(s) compilation warning */
  4651. UNUSED(htim);
  4652. /* NOTE : This function should not be modified, when the callback is needed,
  4653. the HAL_TIM_PeriodElapsedHalfCpltCallback could be implemented in the user file
  4654. */
  4655. }
  4656. /**
  4657. * @brief Output Compare callback in non-blocking mode
  4658. * @param htim TIM OC handle
  4659. * @retval None
  4660. */
  4661. __weak void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)
  4662. {
  4663. /* Prevent unused argument(s) compilation warning */
  4664. UNUSED(htim);
  4665. /* NOTE : This function should not be modified, when the callback is needed,
  4666. the HAL_TIM_OC_DelayElapsedCallback could be implemented in the user file
  4667. */
  4668. }
  4669. /**
  4670. * @brief Input Capture callback in non-blocking mode
  4671. * @param htim TIM IC handle
  4672. * @retval None
  4673. */
  4674. __weak void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
  4675. {
  4676. /* Prevent unused argument(s) compilation warning */
  4677. UNUSED(htim);
  4678. /* NOTE : This function should not be modified, when the callback is needed,
  4679. the HAL_TIM_IC_CaptureCallback could be implemented in the user file
  4680. */
  4681. }
  4682. /**
  4683. * @brief Input Capture half complete callback in non-blocking mode
  4684. * @param htim TIM IC handle
  4685. * @retval None
  4686. */
  4687. __weak void HAL_TIM_IC_CaptureHalfCpltCallback(TIM_HandleTypeDef *htim)
  4688. {
  4689. /* Prevent unused argument(s) compilation warning */
  4690. UNUSED(htim);
  4691. /* NOTE : This function should not be modified, when the callback is needed,
  4692. the HAL_TIM_IC_CaptureHalfCpltCallback could be implemented in the user file
  4693. */
  4694. }
  4695. /**
  4696. * @brief PWM Pulse finished callback in non-blocking mode
  4697. * @param htim TIM handle
  4698. * @retval None
  4699. */
  4700. __weak void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
  4701. {
  4702. /* Prevent unused argument(s) compilation warning */
  4703. UNUSED(htim);
  4704. /* NOTE : This function should not be modified, when the callback is needed,
  4705. the HAL_TIM_PWM_PulseFinishedCallback could be implemented in the user file
  4706. */
  4707. }
  4708. /**
  4709. * @brief PWM Pulse finished half complete callback in non-blocking mode
  4710. * @param htim TIM handle
  4711. * @retval None
  4712. */
  4713. __weak void HAL_TIM_PWM_PulseFinishedHalfCpltCallback(TIM_HandleTypeDef *htim)
  4714. {
  4715. /* Prevent unused argument(s) compilation warning */
  4716. UNUSED(htim);
  4717. /* NOTE : This function should not be modified, when the callback is needed,
  4718. the HAL_TIM_PWM_PulseFinishedHalfCpltCallback could be implemented in the user file
  4719. */
  4720. }
  4721. /**
  4722. * @brief Hall Trigger detection callback in non-blocking mode
  4723. * @param htim TIM handle
  4724. * @retval None
  4725. */
  4726. __weak void HAL_TIM_TriggerCallback(TIM_HandleTypeDef *htim)
  4727. {
  4728. /* Prevent unused argument(s) compilation warning */
  4729. UNUSED(htim);
  4730. /* NOTE : This function should not be modified, when the callback is needed,
  4731. the HAL_TIM_TriggerCallback could be implemented in the user file
  4732. */
  4733. }
  4734. /**
  4735. * @brief Hall Trigger detection half complete callback in non-blocking mode
  4736. * @param htim TIM handle
  4737. * @retval None
  4738. */
  4739. __weak void HAL_TIM_TriggerHalfCpltCallback(TIM_HandleTypeDef *htim)
  4740. {
  4741. /* Prevent unused argument(s) compilation warning */
  4742. UNUSED(htim);
  4743. /* NOTE : This function should not be modified, when the callback is needed,
  4744. the HAL_TIM_TriggerHalfCpltCallback could be implemented in the user file
  4745. */
  4746. }
  4747. /**
  4748. * @brief Timer error callback in non-blocking mode
  4749. * @param htim TIM handle
  4750. * @retval None
  4751. */
  4752. __weak void HAL_TIM_ErrorCallback(TIM_HandleTypeDef *htim)
  4753. {
  4754. /* Prevent unused argument(s) compilation warning */
  4755. UNUSED(htim);
  4756. /* NOTE : This function should not be modified, when the callback is needed,
  4757. the HAL_TIM_ErrorCallback could be implemented in the user file
  4758. */
  4759. }
  4760. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  4761. /**
  4762. * @brief Register a User TIM callback to be used instead of the weak predefined callback
  4763. * @param htim tim handle
  4764. * @param CallbackID ID of the callback to be registered
  4765. * This parameter can be one of the following values:
  4766. * @arg @ref HAL_TIM_BASE_MSPINIT_CB_ID Base MspInit Callback ID
  4767. * @arg @ref HAL_TIM_BASE_MSPDEINIT_CB_ID Base MspDeInit Callback ID
  4768. * @arg @ref HAL_TIM_IC_MSPINIT_CB_ID IC MspInit Callback ID
  4769. * @arg @ref HAL_TIM_IC_MSPDEINIT_CB_ID IC MspDeInit Callback ID
  4770. * @arg @ref HAL_TIM_OC_MSPINIT_CB_ID OC MspInit Callback ID
  4771. * @arg @ref HAL_TIM_OC_MSPDEINIT_CB_ID OC MspDeInit Callback ID
  4772. * @arg @ref HAL_TIM_PWM_MSPINIT_CB_ID PWM MspInit Callback ID
  4773. * @arg @ref HAL_TIM_PWM_MSPDEINIT_CB_ID PWM MspDeInit Callback ID
  4774. * @arg @ref HAL_TIM_ONE_PULSE_MSPINIT_CB_ID One Pulse MspInit Callback ID
  4775. * @arg @ref HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID One Pulse MspDeInit Callback ID
  4776. * @arg @ref HAL_TIM_ENCODER_MSPINIT_CB_ID Encoder MspInit Callback ID
  4777. * @arg @ref HAL_TIM_ENCODER_MSPDEINIT_CB_ID Encoder MspDeInit Callback ID
  4778. * @arg @ref HAL_TIM_PERIOD_ELAPSED_CB_ID Period Elapsed Callback ID
  4779. * @arg @ref HAL_TIM_PERIOD_ELAPSED_HALF_CB_ID Period Elapsed half complete Callback ID
  4780. * @arg @ref HAL_TIM_TRIGGER_CB_ID Trigger Callback ID
  4781. * @arg @ref HAL_TIM_TRIGGER_HALF_CB_ID Trigger half complete Callback ID
  4782. * @arg @ref HAL_TIM_IC_CAPTURE_CB_ID Input Capture Callback ID
  4783. * @arg @ref HAL_TIM_IC_CAPTURE_HALF_CB_ID Input Capture half complete Callback ID
  4784. * @arg @ref HAL_TIM_OC_DELAY_ELAPSED_CB_ID Output Compare Delay Elapsed Callback ID
  4785. * @arg @ref HAL_TIM_PWM_PULSE_FINISHED_CB_ID PWM Pulse Finished Callback ID
  4786. * @arg @ref HAL_TIM_PWM_PULSE_FINISHED_HALF_CB_ID PWM Pulse Finished half complete Callback ID
  4787. * @arg @ref HAL_TIM_ERROR_CB_ID Error Callback ID
  4788. * @param pCallback pointer to the callback function
  4789. * @retval status
  4790. */
  4791. HAL_StatusTypeDef HAL_TIM_RegisterCallback(TIM_HandleTypeDef *htim, HAL_TIM_CallbackIDTypeDef CallbackID,
  4792. pTIM_CallbackTypeDef pCallback)
  4793. {
  4794. HAL_StatusTypeDef status = HAL_OK;
  4795. if (pCallback == NULL)
  4796. {
  4797. return HAL_ERROR;
  4798. }
  4799. if (htim->State == HAL_TIM_STATE_READY)
  4800. {
  4801. switch (CallbackID)
  4802. {
  4803. case HAL_TIM_BASE_MSPINIT_CB_ID :
  4804. htim->Base_MspInitCallback = pCallback;
  4805. break;
  4806. case HAL_TIM_BASE_MSPDEINIT_CB_ID :
  4807. htim->Base_MspDeInitCallback = pCallback;
  4808. break;
  4809. case HAL_TIM_IC_MSPINIT_CB_ID :
  4810. htim->IC_MspInitCallback = pCallback;
  4811. break;
  4812. case HAL_TIM_IC_MSPDEINIT_CB_ID :
  4813. htim->IC_MspDeInitCallback = pCallback;
  4814. break;
  4815. case HAL_TIM_OC_MSPINIT_CB_ID :
  4816. htim->OC_MspInitCallback = pCallback;
  4817. break;
  4818. case HAL_TIM_OC_MSPDEINIT_CB_ID :
  4819. htim->OC_MspDeInitCallback = pCallback;
  4820. break;
  4821. case HAL_TIM_PWM_MSPINIT_CB_ID :
  4822. htim->PWM_MspInitCallback = pCallback;
  4823. break;
  4824. case HAL_TIM_PWM_MSPDEINIT_CB_ID :
  4825. htim->PWM_MspDeInitCallback = pCallback;
  4826. break;
  4827. case HAL_TIM_ONE_PULSE_MSPINIT_CB_ID :
  4828. htim->OnePulse_MspInitCallback = pCallback;
  4829. break;
  4830. case HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID :
  4831. htim->OnePulse_MspDeInitCallback = pCallback;
  4832. break;
  4833. case HAL_TIM_ENCODER_MSPINIT_CB_ID :
  4834. htim->Encoder_MspInitCallback = pCallback;
  4835. break;
  4836. case HAL_TIM_ENCODER_MSPDEINIT_CB_ID :
  4837. htim->Encoder_MspDeInitCallback = pCallback;
  4838. break;
  4839. case HAL_TIM_PERIOD_ELAPSED_CB_ID :
  4840. htim->PeriodElapsedCallback = pCallback;
  4841. break;
  4842. case HAL_TIM_PERIOD_ELAPSED_HALF_CB_ID :
  4843. htim->PeriodElapsedHalfCpltCallback = pCallback;
  4844. break;
  4845. case HAL_TIM_TRIGGER_CB_ID :
  4846. htim->TriggerCallback = pCallback;
  4847. break;
  4848. case HAL_TIM_TRIGGER_HALF_CB_ID :
  4849. htim->TriggerHalfCpltCallback = pCallback;
  4850. break;
  4851. case HAL_TIM_IC_CAPTURE_CB_ID :
  4852. htim->IC_CaptureCallback = pCallback;
  4853. break;
  4854. case HAL_TIM_IC_CAPTURE_HALF_CB_ID :
  4855. htim->IC_CaptureHalfCpltCallback = pCallback;
  4856. break;
  4857. case HAL_TIM_OC_DELAY_ELAPSED_CB_ID :
  4858. htim->OC_DelayElapsedCallback = pCallback;
  4859. break;
  4860. case HAL_TIM_PWM_PULSE_FINISHED_CB_ID :
  4861. htim->PWM_PulseFinishedCallback = pCallback;
  4862. break;
  4863. case HAL_TIM_PWM_PULSE_FINISHED_HALF_CB_ID :
  4864. htim->PWM_PulseFinishedHalfCpltCallback = pCallback;
  4865. break;
  4866. case HAL_TIM_ERROR_CB_ID :
  4867. htim->ErrorCallback = pCallback;
  4868. break;
  4869. default :
  4870. /* Return error status */
  4871. status = HAL_ERROR;
  4872. break;
  4873. }
  4874. }
  4875. else if (htim->State == HAL_TIM_STATE_RESET)
  4876. {
  4877. switch (CallbackID)
  4878. {
  4879. case HAL_TIM_BASE_MSPINIT_CB_ID :
  4880. htim->Base_MspInitCallback = pCallback;
  4881. break;
  4882. case HAL_TIM_BASE_MSPDEINIT_CB_ID :
  4883. htim->Base_MspDeInitCallback = pCallback;
  4884. break;
  4885. case HAL_TIM_IC_MSPINIT_CB_ID :
  4886. htim->IC_MspInitCallback = pCallback;
  4887. break;
  4888. case HAL_TIM_IC_MSPDEINIT_CB_ID :
  4889. htim->IC_MspDeInitCallback = pCallback;
  4890. break;
  4891. case HAL_TIM_OC_MSPINIT_CB_ID :
  4892. htim->OC_MspInitCallback = pCallback;
  4893. break;
  4894. case HAL_TIM_OC_MSPDEINIT_CB_ID :
  4895. htim->OC_MspDeInitCallback = pCallback;
  4896. break;
  4897. case HAL_TIM_PWM_MSPINIT_CB_ID :
  4898. htim->PWM_MspInitCallback = pCallback;
  4899. break;
  4900. case HAL_TIM_PWM_MSPDEINIT_CB_ID :
  4901. htim->PWM_MspDeInitCallback = pCallback;
  4902. break;
  4903. case HAL_TIM_ONE_PULSE_MSPINIT_CB_ID :
  4904. htim->OnePulse_MspInitCallback = pCallback;
  4905. break;
  4906. case HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID :
  4907. htim->OnePulse_MspDeInitCallback = pCallback;
  4908. break;
  4909. case HAL_TIM_ENCODER_MSPINIT_CB_ID :
  4910. htim->Encoder_MspInitCallback = pCallback;
  4911. break;
  4912. case HAL_TIM_ENCODER_MSPDEINIT_CB_ID :
  4913. htim->Encoder_MspDeInitCallback = pCallback;
  4914. break;
  4915. default :
  4916. /* Return error status */
  4917. status = HAL_ERROR;
  4918. break;
  4919. }
  4920. }
  4921. else
  4922. {
  4923. /* Return error status */
  4924. status = HAL_ERROR;
  4925. }
  4926. return status;
  4927. }
  4928. /**
  4929. * @brief Unregister a TIM callback
  4930. * TIM callback is redirected to the weak predefined callback
  4931. * @param htim tim handle
  4932. * @param CallbackID ID of the callback to be unregistered
  4933. * This parameter can be one of the following values:
  4934. * @arg @ref HAL_TIM_BASE_MSPINIT_CB_ID Base MspInit Callback ID
  4935. * @arg @ref HAL_TIM_BASE_MSPDEINIT_CB_ID Base MspDeInit Callback ID
  4936. * @arg @ref HAL_TIM_IC_MSPINIT_CB_ID IC MspInit Callback ID
  4937. * @arg @ref HAL_TIM_IC_MSPDEINIT_CB_ID IC MspDeInit Callback ID
  4938. * @arg @ref HAL_TIM_OC_MSPINIT_CB_ID OC MspInit Callback ID
  4939. * @arg @ref HAL_TIM_OC_MSPDEINIT_CB_ID OC MspDeInit Callback ID
  4940. * @arg @ref HAL_TIM_PWM_MSPINIT_CB_ID PWM MspInit Callback ID
  4941. * @arg @ref HAL_TIM_PWM_MSPDEINIT_CB_ID PWM MspDeInit Callback ID
  4942. * @arg @ref HAL_TIM_ONE_PULSE_MSPINIT_CB_ID One Pulse MspInit Callback ID
  4943. * @arg @ref HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID One Pulse MspDeInit Callback ID
  4944. * @arg @ref HAL_TIM_ENCODER_MSPINIT_CB_ID Encoder MspInit Callback ID
  4945. * @arg @ref HAL_TIM_ENCODER_MSPDEINIT_CB_ID Encoder MspDeInit Callback ID
  4946. * @arg @ref HAL_TIM_PERIOD_ELAPSED_CB_ID Period Elapsed Callback ID
  4947. * @arg @ref HAL_TIM_PERIOD_ELAPSED_HALF_CB_ID Period Elapsed half complete Callback ID
  4948. * @arg @ref HAL_TIM_TRIGGER_CB_ID Trigger Callback ID
  4949. * @arg @ref HAL_TIM_TRIGGER_HALF_CB_ID Trigger half complete Callback ID
  4950. * @arg @ref HAL_TIM_IC_CAPTURE_CB_ID Input Capture Callback ID
  4951. * @arg @ref HAL_TIM_IC_CAPTURE_HALF_CB_ID Input Capture half complete Callback ID
  4952. * @arg @ref HAL_TIM_OC_DELAY_ELAPSED_CB_ID Output Compare Delay Elapsed Callback ID
  4953. * @arg @ref HAL_TIM_PWM_PULSE_FINISHED_CB_ID PWM Pulse Finished Callback ID
  4954. * @arg @ref HAL_TIM_PWM_PULSE_FINISHED_HALF_CB_ID PWM Pulse Finished half complete Callback ID
  4955. * @arg @ref HAL_TIM_ERROR_CB_ID Error Callback ID
  4956. * @retval status
  4957. */
  4958. HAL_StatusTypeDef HAL_TIM_UnRegisterCallback(TIM_HandleTypeDef *htim, HAL_TIM_CallbackIDTypeDef CallbackID)
  4959. {
  4960. HAL_StatusTypeDef status = HAL_OK;
  4961. if (htim->State == HAL_TIM_STATE_READY)
  4962. {
  4963. switch (CallbackID)
  4964. {
  4965. case HAL_TIM_BASE_MSPINIT_CB_ID :
  4966. /* Legacy weak Base MspInit Callback */
  4967. htim->Base_MspInitCallback = HAL_TIM_Base_MspInit;
  4968. break;
  4969. case HAL_TIM_BASE_MSPDEINIT_CB_ID :
  4970. /* Legacy weak Base Msp DeInit Callback */
  4971. htim->Base_MspDeInitCallback = HAL_TIM_Base_MspDeInit;
  4972. break;
  4973. case HAL_TIM_IC_MSPINIT_CB_ID :
  4974. /* Legacy weak IC Msp Init Callback */
  4975. htim->IC_MspInitCallback = HAL_TIM_IC_MspInit;
  4976. break;
  4977. case HAL_TIM_IC_MSPDEINIT_CB_ID :
  4978. /* Legacy weak IC Msp DeInit Callback */
  4979. htim->IC_MspDeInitCallback = HAL_TIM_IC_MspDeInit;
  4980. break;
  4981. case HAL_TIM_OC_MSPINIT_CB_ID :
  4982. /* Legacy weak OC Msp Init Callback */
  4983. htim->OC_MspInitCallback = HAL_TIM_OC_MspInit;
  4984. break;
  4985. case HAL_TIM_OC_MSPDEINIT_CB_ID :
  4986. /* Legacy weak OC Msp DeInit Callback */
  4987. htim->OC_MspDeInitCallback = HAL_TIM_OC_MspDeInit;
  4988. break;
  4989. case HAL_TIM_PWM_MSPINIT_CB_ID :
  4990. /* Legacy weak PWM Msp Init Callback */
  4991. htim->PWM_MspInitCallback = HAL_TIM_PWM_MspInit;
  4992. break;
  4993. case HAL_TIM_PWM_MSPDEINIT_CB_ID :
  4994. /* Legacy weak PWM Msp DeInit Callback */
  4995. htim->PWM_MspDeInitCallback = HAL_TIM_PWM_MspDeInit;
  4996. break;
  4997. case HAL_TIM_ONE_PULSE_MSPINIT_CB_ID :
  4998. /* Legacy weak One Pulse Msp Init Callback */
  4999. htim->OnePulse_MspInitCallback = HAL_TIM_OnePulse_MspInit;
  5000. break;
  5001. case HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID :
  5002. /* Legacy weak One Pulse Msp DeInit Callback */
  5003. htim->OnePulse_MspDeInitCallback = HAL_TIM_OnePulse_MspDeInit;
  5004. break;
  5005. case HAL_TIM_ENCODER_MSPINIT_CB_ID :
  5006. /* Legacy weak Encoder Msp Init Callback */
  5007. htim->Encoder_MspInitCallback = HAL_TIM_Encoder_MspInit;
  5008. break;
  5009. case HAL_TIM_ENCODER_MSPDEINIT_CB_ID :
  5010. /* Legacy weak Encoder Msp DeInit Callback */
  5011. htim->Encoder_MspDeInitCallback = HAL_TIM_Encoder_MspDeInit;
  5012. break;
  5013. case HAL_TIM_PERIOD_ELAPSED_CB_ID :
  5014. /* Legacy weak Period Elapsed Callback */
  5015. htim->PeriodElapsedCallback = HAL_TIM_PeriodElapsedCallback;
  5016. break;
  5017. case HAL_TIM_PERIOD_ELAPSED_HALF_CB_ID :
  5018. /* Legacy weak Period Elapsed half complete Callback */
  5019. htim->PeriodElapsedHalfCpltCallback = HAL_TIM_PeriodElapsedHalfCpltCallback;
  5020. break;
  5021. case HAL_TIM_TRIGGER_CB_ID :
  5022. /* Legacy weak Trigger Callback */
  5023. htim->TriggerCallback = HAL_TIM_TriggerCallback;
  5024. break;
  5025. case HAL_TIM_TRIGGER_HALF_CB_ID :
  5026. /* Legacy weak Trigger half complete Callback */
  5027. htim->TriggerHalfCpltCallback = HAL_TIM_TriggerHalfCpltCallback;
  5028. break;
  5029. case HAL_TIM_IC_CAPTURE_CB_ID :
  5030. /* Legacy weak IC Capture Callback */
  5031. htim->IC_CaptureCallback = HAL_TIM_IC_CaptureCallback;
  5032. break;
  5033. case HAL_TIM_IC_CAPTURE_HALF_CB_ID :
  5034. /* Legacy weak IC Capture half complete Callback */
  5035. htim->IC_CaptureHalfCpltCallback = HAL_TIM_IC_CaptureHalfCpltCallback;
  5036. break;
  5037. case HAL_TIM_OC_DELAY_ELAPSED_CB_ID :
  5038. /* Legacy weak OC Delay Elapsed Callback */
  5039. htim->OC_DelayElapsedCallback = HAL_TIM_OC_DelayElapsedCallback;
  5040. break;
  5041. case HAL_TIM_PWM_PULSE_FINISHED_CB_ID :
  5042. /* Legacy weak PWM Pulse Finished Callback */
  5043. htim->PWM_PulseFinishedCallback = HAL_TIM_PWM_PulseFinishedCallback;
  5044. break;
  5045. case HAL_TIM_PWM_PULSE_FINISHED_HALF_CB_ID :
  5046. /* Legacy weak PWM Pulse Finished half complete Callback */
  5047. htim->PWM_PulseFinishedHalfCpltCallback = HAL_TIM_PWM_PulseFinishedHalfCpltCallback;
  5048. break;
  5049. case HAL_TIM_ERROR_CB_ID :
  5050. /* Legacy weak Error Callback */
  5051. htim->ErrorCallback = HAL_TIM_ErrorCallback;
  5052. break;
  5053. default :
  5054. /* Return error status */
  5055. status = HAL_ERROR;
  5056. break;
  5057. }
  5058. }
  5059. else if (htim->State == HAL_TIM_STATE_RESET)
  5060. {
  5061. switch (CallbackID)
  5062. {
  5063. case HAL_TIM_BASE_MSPINIT_CB_ID :
  5064. /* Legacy weak Base MspInit Callback */
  5065. htim->Base_MspInitCallback = HAL_TIM_Base_MspInit;
  5066. break;
  5067. case HAL_TIM_BASE_MSPDEINIT_CB_ID :
  5068. /* Legacy weak Base Msp DeInit Callback */
  5069. htim->Base_MspDeInitCallback = HAL_TIM_Base_MspDeInit;
  5070. break;
  5071. case HAL_TIM_IC_MSPINIT_CB_ID :
  5072. /* Legacy weak IC Msp Init Callback */
  5073. htim->IC_MspInitCallback = HAL_TIM_IC_MspInit;
  5074. break;
  5075. case HAL_TIM_IC_MSPDEINIT_CB_ID :
  5076. /* Legacy weak IC Msp DeInit Callback */
  5077. htim->IC_MspDeInitCallback = HAL_TIM_IC_MspDeInit;
  5078. break;
  5079. case HAL_TIM_OC_MSPINIT_CB_ID :
  5080. /* Legacy weak OC Msp Init Callback */
  5081. htim->OC_MspInitCallback = HAL_TIM_OC_MspInit;
  5082. break;
  5083. case HAL_TIM_OC_MSPDEINIT_CB_ID :
  5084. /* Legacy weak OC Msp DeInit Callback */
  5085. htim->OC_MspDeInitCallback = HAL_TIM_OC_MspDeInit;
  5086. break;
  5087. case HAL_TIM_PWM_MSPINIT_CB_ID :
  5088. /* Legacy weak PWM Msp Init Callback */
  5089. htim->PWM_MspInitCallback = HAL_TIM_PWM_MspInit;
  5090. break;
  5091. case HAL_TIM_PWM_MSPDEINIT_CB_ID :
  5092. /* Legacy weak PWM Msp DeInit Callback */
  5093. htim->PWM_MspDeInitCallback = HAL_TIM_PWM_MspDeInit;
  5094. break;
  5095. case HAL_TIM_ONE_PULSE_MSPINIT_CB_ID :
  5096. /* Legacy weak One Pulse Msp Init Callback */
  5097. htim->OnePulse_MspInitCallback = HAL_TIM_OnePulse_MspInit;
  5098. break;
  5099. case HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID :
  5100. /* Legacy weak One Pulse Msp DeInit Callback */
  5101. htim->OnePulse_MspDeInitCallback = HAL_TIM_OnePulse_MspDeInit;
  5102. break;
  5103. case HAL_TIM_ENCODER_MSPINIT_CB_ID :
  5104. /* Legacy weak Encoder Msp Init Callback */
  5105. htim->Encoder_MspInitCallback = HAL_TIM_Encoder_MspInit;
  5106. break;
  5107. case HAL_TIM_ENCODER_MSPDEINIT_CB_ID :
  5108. /* Legacy weak Encoder Msp DeInit Callback */
  5109. htim->Encoder_MspDeInitCallback = HAL_TIM_Encoder_MspDeInit;
  5110. break;
  5111. default :
  5112. /* Return error status */
  5113. status = HAL_ERROR;
  5114. break;
  5115. }
  5116. }
  5117. else
  5118. {
  5119. /* Return error status */
  5120. status = HAL_ERROR;
  5121. }
  5122. return status;
  5123. }
  5124. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  5125. /**
  5126. * @}
  5127. */
  5128. /** @defgroup TIM_Exported_Functions_Group10 TIM Peripheral State functions
  5129. * @brief TIM Peripheral State functions
  5130. *
  5131. @verbatim
  5132. ==============================================================================
  5133. ##### Peripheral State functions #####
  5134. ==============================================================================
  5135. [..]
  5136. This subsection permits to get in run-time the status of the peripheral
  5137. and the data flow.
  5138. @endverbatim
  5139. * @{
  5140. */
  5141. /**
  5142. * @brief Return the TIM Base handle state.
  5143. * @param htim TIM Base handle
  5144. * @retval HAL state
  5145. */
  5146. HAL_TIM_StateTypeDef HAL_TIM_Base_GetState(const TIM_HandleTypeDef *htim)
  5147. {
  5148. return htim->State;
  5149. }
  5150. /**
  5151. * @brief Return the TIM OC handle state.
  5152. * @param htim TIM Output Compare handle
  5153. * @retval HAL state
  5154. */
  5155. HAL_TIM_StateTypeDef HAL_TIM_OC_GetState(const TIM_HandleTypeDef *htim)
  5156. {
  5157. return htim->State;
  5158. }
  5159. /**
  5160. * @brief Return the TIM PWM handle state.
  5161. * @param htim TIM handle
  5162. * @retval HAL state
  5163. */
  5164. HAL_TIM_StateTypeDef HAL_TIM_PWM_GetState(const TIM_HandleTypeDef *htim)
  5165. {
  5166. return htim->State;
  5167. }
  5168. /**
  5169. * @brief Return the TIM Input Capture handle state.
  5170. * @param htim TIM IC handle
  5171. * @retval HAL state
  5172. */
  5173. HAL_TIM_StateTypeDef HAL_TIM_IC_GetState(const TIM_HandleTypeDef *htim)
  5174. {
  5175. return htim->State;
  5176. }
  5177. /**
  5178. * @brief Return the TIM One Pulse Mode handle state.
  5179. * @param htim TIM OPM handle
  5180. * @retval HAL state
  5181. */
  5182. HAL_TIM_StateTypeDef HAL_TIM_OnePulse_GetState(const TIM_HandleTypeDef *htim)
  5183. {
  5184. return htim->State;
  5185. }
  5186. /**
  5187. * @brief Return the TIM Encoder Mode handle state.
  5188. * @param htim TIM Encoder Interface handle
  5189. * @retval HAL state
  5190. */
  5191. HAL_TIM_StateTypeDef HAL_TIM_Encoder_GetState(const TIM_HandleTypeDef *htim)
  5192. {
  5193. return htim->State;
  5194. }
  5195. /**
  5196. * @brief Return the TIM Encoder Mode handle state.
  5197. * @param htim TIM handle
  5198. * @retval Active channel
  5199. */
  5200. HAL_TIM_ActiveChannel HAL_TIM_GetActiveChannel(const TIM_HandleTypeDef *htim)
  5201. {
  5202. return htim->Channel;
  5203. }
  5204. /**
  5205. * @brief Return actual state of the TIM channel.
  5206. * @param htim TIM handle
  5207. * @param Channel TIM Channel
  5208. * This parameter can be one of the following values:
  5209. * @arg TIM_CHANNEL_1: TIM Channel 1
  5210. * @arg TIM_CHANNEL_2: TIM Channel 2
  5211. * @arg TIM_CHANNEL_3: TIM Channel 3
  5212. * @arg TIM_CHANNEL_4: TIM Channel 4
  5213. * @arg TIM_CHANNEL_5: TIM Channel 5
  5214. * @arg TIM_CHANNEL_6: TIM Channel 6
  5215. * @retval TIM Channel state
  5216. */
  5217. HAL_TIM_ChannelStateTypeDef HAL_TIM_GetChannelState(const TIM_HandleTypeDef *htim, uint32_t Channel)
  5218. {
  5219. HAL_TIM_ChannelStateTypeDef channel_state;
  5220. /* Check the parameters */
  5221. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  5222. channel_state = TIM_CHANNEL_STATE_GET(htim, Channel);
  5223. return channel_state;
  5224. }
  5225. /**
  5226. * @brief Return actual state of a DMA burst operation.
  5227. * @param htim TIM handle
  5228. * @retval DMA burst state
  5229. */
  5230. HAL_TIM_DMABurstStateTypeDef HAL_TIM_DMABurstState(const TIM_HandleTypeDef *htim)
  5231. {
  5232. /* Check the parameters */
  5233. assert_param(IS_TIM_DMABURST_INSTANCE(htim->Instance));
  5234. return htim->DMABurstState;
  5235. }
  5236. /**
  5237. * @}
  5238. */
  5239. /**
  5240. * @}
  5241. */
  5242. /** @defgroup TIM_Private_Functions TIM Private Functions
  5243. * @{
  5244. */
  5245. /**
  5246. * @brief TIM DMA error callback
  5247. * @param hdma pointer to DMA handle.
  5248. * @retval None
  5249. */
  5250. void TIM_DMAError(DMA_HandleTypeDef *hdma)
  5251. {
  5252. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  5253. if (hdma == htim->hdma[TIM_DMA_ID_CC1])
  5254. {
  5255. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
  5256. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  5257. }
  5258. else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
  5259. {
  5260. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
  5261. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  5262. }
  5263. else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
  5264. {
  5265. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
  5266. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_3, HAL_TIM_CHANNEL_STATE_READY);
  5267. }
  5268. else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
  5269. {
  5270. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
  5271. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_4, HAL_TIM_CHANNEL_STATE_READY);
  5272. }
  5273. else
  5274. {
  5275. htim->State = HAL_TIM_STATE_READY;
  5276. }
  5277. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  5278. htim->ErrorCallback(htim);
  5279. #else
  5280. HAL_TIM_ErrorCallback(htim);
  5281. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  5282. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  5283. }
  5284. /**
  5285. * @brief TIM DMA Delay Pulse complete callback.
  5286. * @param hdma pointer to DMA handle.
  5287. * @retval None
  5288. */
  5289. static void TIM_DMADelayPulseCplt(DMA_HandleTypeDef *hdma)
  5290. {
  5291. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  5292. if (hdma == htim->hdma[TIM_DMA_ID_CC1])
  5293. {
  5294. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
  5295. if (hdma->Init.Mode == DMA_NORMAL)
  5296. {
  5297. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  5298. }
  5299. }
  5300. else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
  5301. {
  5302. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
  5303. if (hdma->Init.Mode == DMA_NORMAL)
  5304. {
  5305. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  5306. }
  5307. }
  5308. else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
  5309. {
  5310. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
  5311. if (hdma->Init.Mode == DMA_NORMAL)
  5312. {
  5313. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_3, HAL_TIM_CHANNEL_STATE_READY);
  5314. }
  5315. }
  5316. else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
  5317. {
  5318. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
  5319. if (hdma->Init.Mode == DMA_NORMAL)
  5320. {
  5321. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_4, HAL_TIM_CHANNEL_STATE_READY);
  5322. }
  5323. }
  5324. else
  5325. {
  5326. /* nothing to do */
  5327. }
  5328. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  5329. htim->PWM_PulseFinishedCallback(htim);
  5330. #else
  5331. HAL_TIM_PWM_PulseFinishedCallback(htim);
  5332. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  5333. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  5334. }
  5335. /**
  5336. * @brief TIM DMA Delay Pulse half complete callback.
  5337. * @param hdma pointer to DMA handle.
  5338. * @retval None
  5339. */
  5340. static void TIM_DMADelayPulseHalfCplt(DMA_HandleTypeDef *hdma)
  5341. {
  5342. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  5343. if (hdma == htim->hdma[TIM_DMA_ID_CC1])
  5344. {
  5345. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
  5346. }
  5347. else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
  5348. {
  5349. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
  5350. }
  5351. else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
  5352. {
  5353. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
  5354. }
  5355. else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
  5356. {
  5357. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
  5358. }
  5359. else
  5360. {
  5361. /* nothing to do */
  5362. }
  5363. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  5364. htim->PWM_PulseFinishedHalfCpltCallback(htim);
  5365. #else
  5366. HAL_TIM_PWM_PulseFinishedHalfCpltCallback(htim);
  5367. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  5368. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  5369. }
  5370. /**
  5371. * @brief TIM DMA Capture complete callback.
  5372. * @param hdma pointer to DMA handle.
  5373. * @retval None
  5374. */
  5375. void TIM_DMACaptureCplt(DMA_HandleTypeDef *hdma)
  5376. {
  5377. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  5378. if (hdma == htim->hdma[TIM_DMA_ID_CC1])
  5379. {
  5380. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
  5381. if (hdma->Init.Mode == DMA_NORMAL)
  5382. {
  5383. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  5384. }
  5385. }
  5386. else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
  5387. {
  5388. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
  5389. if (hdma->Init.Mode == DMA_NORMAL)
  5390. {
  5391. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  5392. }
  5393. }
  5394. else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
  5395. {
  5396. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
  5397. if (hdma->Init.Mode == DMA_NORMAL)
  5398. {
  5399. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_3, HAL_TIM_CHANNEL_STATE_READY);
  5400. }
  5401. }
  5402. else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
  5403. {
  5404. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
  5405. if (hdma->Init.Mode == DMA_NORMAL)
  5406. {
  5407. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_4, HAL_TIM_CHANNEL_STATE_READY);
  5408. }
  5409. }
  5410. else
  5411. {
  5412. /* nothing to do */
  5413. }
  5414. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  5415. htim->IC_CaptureCallback(htim);
  5416. #else
  5417. HAL_TIM_IC_CaptureCallback(htim);
  5418. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  5419. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  5420. }
  5421. /**
  5422. * @brief TIM DMA Capture half complete callback.
  5423. * @param hdma pointer to DMA handle.
  5424. * @retval None
  5425. */
  5426. void TIM_DMACaptureHalfCplt(DMA_HandleTypeDef *hdma)
  5427. {
  5428. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  5429. if (hdma == htim->hdma[TIM_DMA_ID_CC1])
  5430. {
  5431. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
  5432. }
  5433. else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
  5434. {
  5435. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
  5436. }
  5437. else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
  5438. {
  5439. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
  5440. }
  5441. else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
  5442. {
  5443. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
  5444. }
  5445. else
  5446. {
  5447. /* nothing to do */
  5448. }
  5449. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  5450. htim->IC_CaptureHalfCpltCallback(htim);
  5451. #else
  5452. HAL_TIM_IC_CaptureHalfCpltCallback(htim);
  5453. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  5454. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  5455. }
  5456. /**
  5457. * @brief TIM DMA Period Elapse complete callback.
  5458. * @param hdma pointer to DMA handle.
  5459. * @retval None
  5460. */
  5461. static void TIM_DMAPeriodElapsedCplt(DMA_HandleTypeDef *hdma)
  5462. {
  5463. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  5464. if (htim->hdma[TIM_DMA_ID_UPDATE]->Init.Mode == DMA_NORMAL)
  5465. {
  5466. htim->State = HAL_TIM_STATE_READY;
  5467. }
  5468. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  5469. htim->PeriodElapsedCallback(htim);
  5470. #else
  5471. HAL_TIM_PeriodElapsedCallback(htim);
  5472. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  5473. }
  5474. /**
  5475. * @brief TIM DMA Period Elapse half complete callback.
  5476. * @param hdma pointer to DMA handle.
  5477. * @retval None
  5478. */
  5479. static void TIM_DMAPeriodElapsedHalfCplt(DMA_HandleTypeDef *hdma)
  5480. {
  5481. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  5482. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  5483. htim->PeriodElapsedHalfCpltCallback(htim);
  5484. #else
  5485. HAL_TIM_PeriodElapsedHalfCpltCallback(htim);
  5486. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  5487. }
  5488. /**
  5489. * @brief TIM DMA Trigger callback.
  5490. * @param hdma pointer to DMA handle.
  5491. * @retval None
  5492. */
  5493. static void TIM_DMATriggerCplt(DMA_HandleTypeDef *hdma)
  5494. {
  5495. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  5496. if (htim->hdma[TIM_DMA_ID_TRIGGER]->Init.Mode == DMA_NORMAL)
  5497. {
  5498. htim->State = HAL_TIM_STATE_READY;
  5499. }
  5500. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  5501. htim->TriggerCallback(htim);
  5502. #else
  5503. HAL_TIM_TriggerCallback(htim);
  5504. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  5505. }
  5506. /**
  5507. * @brief TIM DMA Trigger half complete callback.
  5508. * @param hdma pointer to DMA handle.
  5509. * @retval None
  5510. */
  5511. static void TIM_DMATriggerHalfCplt(DMA_HandleTypeDef *hdma)
  5512. {
  5513. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  5514. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  5515. htim->TriggerHalfCpltCallback(htim);
  5516. #else
  5517. HAL_TIM_TriggerHalfCpltCallback(htim);
  5518. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  5519. }
  5520. /**
  5521. * @brief Time Base configuration
  5522. * @param TIMx TIM peripheral
  5523. * @param Structure TIM Base configuration structure
  5524. * @retval None
  5525. */
  5526. static void TIM_Base_SetConfig(TIM_TypeDef *TIMx, const TIM_Base_InitTypeDef *Structure)
  5527. {
  5528. uint32_t tmpcr1;
  5529. tmpcr1 = TIMx->CR1;
  5530. /* Set TIM Time Base Unit parameters ---------------------------------------*/
  5531. if (IS_TIM_COUNTER_MODE_SELECT_INSTANCE(TIMx))
  5532. {
  5533. /* Select the Counter Mode */
  5534. tmpcr1 &= ~(TIM_CR1_DIR | TIM_CR1_CMS);
  5535. tmpcr1 |= Structure->CounterMode;
  5536. }
  5537. if (IS_TIM_CLOCK_DIVISION_INSTANCE(TIMx))
  5538. {
  5539. /* Set the clock division */
  5540. tmpcr1 &= ~TIM_CR1_CKD;
  5541. tmpcr1 |= (uint32_t)Structure->ClockDivision;
  5542. }
  5543. /* Set the auto-reload preload */
  5544. MODIFY_REG(tmpcr1, TIM_CR1_ARPE, Structure->AutoReloadPreload);
  5545. TIMx->CR1 = tmpcr1;
  5546. /* Set the Autoreload value */
  5547. TIMx->ARR = (uint32_t)Structure->Period ;
  5548. /* Set the Prescaler value */
  5549. TIMx->PSC = Structure->Prescaler;
  5550. /* Generate an update event to reload the Prescaler
  5551. and the repetition counter (only for advanced timer) value immediately */
  5552. TIMx->EGR = TIM_EGR_UG;
  5553. }
  5554. /**
  5555. * @brief Timer Output Compare 1 configuration
  5556. * @param TIMx to select the TIM peripheral
  5557. * @param OC_Config The output configuration structure
  5558. * @retval None
  5559. */
  5560. static void TIM_OC1_SetConfig(TIM_TypeDef *TIMx, const TIM_OC_InitTypeDef *OC_Config)
  5561. {
  5562. uint32_t tmpccmrx;
  5563. uint32_t tmpccer;
  5564. uint32_t tmpcr2;
  5565. /* Disable the Channel 1: Reset the CC1E Bit */
  5566. TIMx->CCER &= ~TIM_CCER_CC1E;
  5567. /* Get the TIMx CCER register value */
  5568. tmpccer = TIMx->CCER;
  5569. /* Get the TIMx CR2 register value */
  5570. tmpcr2 = TIMx->CR2;
  5571. /* Get the TIMx CCMR1 register value */
  5572. tmpccmrx = TIMx->CCMR1;
  5573. /* Reset the Output Compare Mode Bits */
  5574. tmpccmrx &= ~TIM_CCMR1_OC1M;
  5575. tmpccmrx &= ~TIM_CCMR1_CC1S;
  5576. /* Select the Output Compare Mode */
  5577. tmpccmrx |= OC_Config->OCMode;
  5578. /* Reset the Output Polarity level */
  5579. tmpccer &= ~TIM_CCER_CC1P;
  5580. /* Set the Output Compare Polarity */
  5581. tmpccer |= OC_Config->OCPolarity;
  5582. /* Write to TIMx CR2 */
  5583. TIMx->CR2 = tmpcr2;
  5584. /* Write to TIMx CCMR1 */
  5585. TIMx->CCMR1 = tmpccmrx;
  5586. /* Set the Capture Compare Register value */
  5587. TIMx->CCR1 = OC_Config->Pulse;
  5588. /* Write to TIMx CCER */
  5589. TIMx->CCER = tmpccer;
  5590. }
  5591. /**
  5592. * @brief Timer Output Compare 2 configuration
  5593. * @param TIMx to select the TIM peripheral
  5594. * @param OC_Config The output configuration structure
  5595. * @retval None
  5596. */
  5597. static void TIM_OC2_SetConfig(TIM_TypeDef *TIMx, const TIM_OC_InitTypeDef *OC_Config)
  5598. {
  5599. uint32_t tmpccmrx;
  5600. uint32_t tmpccer;
  5601. uint32_t tmpcr2;
  5602. /* Disable the Channel 2: Reset the CC2E Bit */
  5603. TIMx->CCER &= ~TIM_CCER_CC2E;
  5604. /* Get the TIMx CCER register value */
  5605. tmpccer = TIMx->CCER;
  5606. /* Get the TIMx CR2 register value */
  5607. tmpcr2 = TIMx->CR2;
  5608. /* Get the TIMx CCMR1 register value */
  5609. tmpccmrx = TIMx->CCMR1;
  5610. /* Reset the Output Compare mode and Capture/Compare selection Bits */
  5611. tmpccmrx &= ~TIM_CCMR1_OC2M;
  5612. tmpccmrx &= ~TIM_CCMR1_CC2S;
  5613. /* Select the Output Compare Mode */
  5614. tmpccmrx |= (OC_Config->OCMode << 8U);
  5615. /* Reset the Output Polarity level */
  5616. tmpccer &= ~TIM_CCER_CC2P;
  5617. /* Set the Output Compare Polarity */
  5618. tmpccer |= (OC_Config->OCPolarity << 4U);
  5619. /* Write to TIMx CR2 */
  5620. TIMx->CR2 = tmpcr2;
  5621. /* Write to TIMx CCMR1 */
  5622. TIMx->CCMR1 = tmpccmrx;
  5623. /* Set the Capture Compare Register value */
  5624. TIMx->CCR2 = OC_Config->Pulse;
  5625. /* Write to TIMx CCER */
  5626. TIMx->CCER = tmpccer;
  5627. }
  5628. /**
  5629. * @brief Timer Output Compare 3 configuration
  5630. * @param TIMx to select the TIM peripheral
  5631. * @param OC_Config The output configuration structure
  5632. * @retval None
  5633. */
  5634. static void TIM_OC3_SetConfig(TIM_TypeDef *TIMx, const TIM_OC_InitTypeDef *OC_Config)
  5635. {
  5636. uint32_t tmpccmrx;
  5637. uint32_t tmpccer;
  5638. uint32_t tmpcr2;
  5639. /* Disable the Channel 3: Reset the CC2E Bit */
  5640. TIMx->CCER &= ~TIM_CCER_CC3E;
  5641. /* Get the TIMx CCER register value */
  5642. tmpccer = TIMx->CCER;
  5643. /* Get the TIMx CR2 register value */
  5644. tmpcr2 = TIMx->CR2;
  5645. /* Get the TIMx CCMR2 register value */
  5646. tmpccmrx = TIMx->CCMR2;
  5647. /* Reset the Output Compare mode and Capture/Compare selection Bits */
  5648. tmpccmrx &= ~TIM_CCMR2_OC3M;
  5649. tmpccmrx &= ~TIM_CCMR2_CC3S;
  5650. /* Select the Output Compare Mode */
  5651. tmpccmrx |= OC_Config->OCMode;
  5652. /* Reset the Output Polarity level */
  5653. tmpccer &= ~TIM_CCER_CC3P;
  5654. /* Set the Output Compare Polarity */
  5655. tmpccer |= (OC_Config->OCPolarity << 8U);
  5656. /* Write to TIMx CR2 */
  5657. TIMx->CR2 = tmpcr2;
  5658. /* Write to TIMx CCMR2 */
  5659. TIMx->CCMR2 = tmpccmrx;
  5660. /* Set the Capture Compare Register value */
  5661. TIMx->CCR3 = OC_Config->Pulse;
  5662. /* Write to TIMx CCER */
  5663. TIMx->CCER = tmpccer;
  5664. }
  5665. /**
  5666. * @brief Timer Output Compare 4 configuration
  5667. * @param TIMx to select the TIM peripheral
  5668. * @param OC_Config The output configuration structure
  5669. * @retval None
  5670. */
  5671. static void TIM_OC4_SetConfig(TIM_TypeDef *TIMx, const TIM_OC_InitTypeDef *OC_Config)
  5672. {
  5673. uint32_t tmpccmrx;
  5674. uint32_t tmpccer;
  5675. uint32_t tmpcr2;
  5676. /* Disable the Channel 4: Reset the CC4E Bit */
  5677. TIMx->CCER &= ~TIM_CCER_CC4E;
  5678. /* Get the TIMx CCER register value */
  5679. tmpccer = TIMx->CCER;
  5680. /* Get the TIMx CR2 register value */
  5681. tmpcr2 = TIMx->CR2;
  5682. /* Get the TIMx CCMR2 register value */
  5683. tmpccmrx = TIMx->CCMR2;
  5684. /* Reset the Output Compare mode and Capture/Compare selection Bits */
  5685. tmpccmrx &= ~TIM_CCMR2_OC4M;
  5686. tmpccmrx &= ~TIM_CCMR2_CC4S;
  5687. /* Select the Output Compare Mode */
  5688. tmpccmrx |= (OC_Config->OCMode << 8U);
  5689. /* Reset the Output Polarity level */
  5690. tmpccer &= ~TIM_CCER_CC4P;
  5691. /* Set the Output Compare Polarity */
  5692. tmpccer |= (OC_Config->OCPolarity << 12U);
  5693. /* Write to TIMx CR2 */
  5694. TIMx->CR2 = tmpcr2;
  5695. /* Write to TIMx CCMR2 */
  5696. TIMx->CCMR2 = tmpccmrx;
  5697. /* Set the Capture Compare Register value */
  5698. TIMx->CCR4 = OC_Config->Pulse;
  5699. /* Write to TIMx CCER */
  5700. TIMx->CCER = tmpccer;
  5701. }
  5702. /**
  5703. * @brief Slave Timer configuration function
  5704. * @param htim TIM handle
  5705. * @param sSlaveConfig Slave timer configuration
  5706. * @retval None
  5707. */
  5708. static HAL_StatusTypeDef TIM_SlaveTimer_SetConfig(TIM_HandleTypeDef *htim,
  5709. const TIM_SlaveConfigTypeDef *sSlaveConfig)
  5710. {
  5711. HAL_StatusTypeDef status = HAL_OK;
  5712. uint32_t tmpsmcr;
  5713. uint32_t tmpccmr1;
  5714. uint32_t tmpccer;
  5715. /* Get the TIMx SMCR register value */
  5716. tmpsmcr = htim->Instance->SMCR;
  5717. /* Reset the Trigger Selection Bits */
  5718. tmpsmcr &= ~TIM_SMCR_TS;
  5719. /* Set the Input Trigger source */
  5720. tmpsmcr |= sSlaveConfig->InputTrigger;
  5721. /* Reset the slave mode Bits */
  5722. tmpsmcr &= ~TIM_SMCR_SMS;
  5723. /* Set the slave mode */
  5724. tmpsmcr |= sSlaveConfig->SlaveMode;
  5725. /* Write to TIMx SMCR */
  5726. htim->Instance->SMCR = tmpsmcr;
  5727. /* Configure the trigger prescaler, filter, and polarity */
  5728. switch (sSlaveConfig->InputTrigger)
  5729. {
  5730. case TIM_TS_ETRF:
  5731. {
  5732. /* Check the parameters */
  5733. assert_param(IS_TIM_CLOCKSOURCE_ETRMODE1_INSTANCE(htim->Instance));
  5734. assert_param(IS_TIM_TRIGGERPRESCALER(sSlaveConfig->TriggerPrescaler));
  5735. assert_param(IS_TIM_TRIGGERPOLARITY(sSlaveConfig->TriggerPolarity));
  5736. assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));
  5737. /* Configure the ETR Trigger source */
  5738. TIM_ETR_SetConfig(htim->Instance,
  5739. sSlaveConfig->TriggerPrescaler,
  5740. sSlaveConfig->TriggerPolarity,
  5741. sSlaveConfig->TriggerFilter);
  5742. break;
  5743. }
  5744. case TIM_TS_TI1F_ED:
  5745. {
  5746. /* Check the parameters */
  5747. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  5748. assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));
  5749. if (sSlaveConfig->SlaveMode == TIM_SLAVEMODE_GATED)
  5750. {
  5751. return HAL_ERROR;
  5752. }
  5753. /* Disable the Channel 1: Reset the CC1E Bit */
  5754. tmpccer = htim->Instance->CCER;
  5755. htim->Instance->CCER &= ~TIM_CCER_CC1E;
  5756. tmpccmr1 = htim->Instance->CCMR1;
  5757. /* Set the filter */
  5758. tmpccmr1 &= ~TIM_CCMR1_IC1F;
  5759. tmpccmr1 |= ((sSlaveConfig->TriggerFilter) << 4U);
  5760. /* Write to TIMx CCMR1 and CCER registers */
  5761. htim->Instance->CCMR1 = tmpccmr1;
  5762. htim->Instance->CCER = tmpccer;
  5763. break;
  5764. }
  5765. case TIM_TS_TI1FP1:
  5766. {
  5767. /* Check the parameters */
  5768. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  5769. assert_param(IS_TIM_TRIGGERPOLARITY(sSlaveConfig->TriggerPolarity));
  5770. assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));
  5771. /* Configure TI1 Filter and Polarity */
  5772. TIM_TI1_ConfigInputStage(htim->Instance,
  5773. sSlaveConfig->TriggerPolarity,
  5774. sSlaveConfig->TriggerFilter);
  5775. break;
  5776. }
  5777. case TIM_TS_TI2FP2:
  5778. {
  5779. /* Check the parameters */
  5780. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  5781. assert_param(IS_TIM_TRIGGERPOLARITY(sSlaveConfig->TriggerPolarity));
  5782. assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));
  5783. /* Configure TI2 Filter and Polarity */
  5784. TIM_TI2_ConfigInputStage(htim->Instance,
  5785. sSlaveConfig->TriggerPolarity,
  5786. sSlaveConfig->TriggerFilter);
  5787. break;
  5788. }
  5789. case TIM_TS_ITR0:
  5790. case TIM_TS_ITR1:
  5791. case TIM_TS_ITR2:
  5792. case TIM_TS_ITR3:
  5793. {
  5794. /* Check the parameter */
  5795. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  5796. break;
  5797. }
  5798. default:
  5799. status = HAL_ERROR;
  5800. break;
  5801. }
  5802. return status;
  5803. }
  5804. /**
  5805. * @brief Configure the TI1 as Input.
  5806. * @param TIMx to select the TIM peripheral.
  5807. * @param TIM_ICPolarity The Input Polarity.
  5808. * This parameter can be one of the following values:
  5809. * @arg TIM_ICPOLARITY_RISING
  5810. * @arg TIM_ICPOLARITY_FALLING
  5811. * @arg TIM_ICPOLARITY_BOTHEDGE
  5812. * @param TIM_ICSelection specifies the input to be used.
  5813. * This parameter can be one of the following values:
  5814. * @arg TIM_ICSELECTION_DIRECTTI: TIM Input 1 is selected to be connected to IC1.
  5815. * @arg TIM_ICSELECTION_INDIRECTTI: TIM Input 1 is selected to be connected to IC2.
  5816. * @arg TIM_ICSELECTION_TRC: TIM Input 1 is selected to be connected to TRC.
  5817. * @param TIM_ICFilter Specifies the Input Capture Filter.
  5818. * This parameter must be a value between 0x00 and 0x0F.
  5819. * @retval None
  5820. * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI2FP1
  5821. * (on channel2 path) is used as the input signal. Therefore CCMR1 must be
  5822. * protected against un-initialized filter and polarity values.
  5823. */
  5824. static void TIM_TI1_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
  5825. uint32_t TIM_ICFilter)
  5826. {
  5827. uint32_t tmpccmr1;
  5828. uint32_t tmpccer;
  5829. /* Disable the Channel 1: Reset the CC1E Bit */
  5830. TIMx->CCER &= ~TIM_CCER_CC1E;
  5831. tmpccmr1 = TIMx->CCMR1;
  5832. tmpccer = TIMx->CCER;
  5833. /* Select the Input */
  5834. if (IS_TIM_CC2_INSTANCE(TIMx) != RESET)
  5835. {
  5836. tmpccmr1 &= ~TIM_CCMR1_CC1S;
  5837. tmpccmr1 |= TIM_ICSelection;
  5838. }
  5839. else
  5840. {
  5841. tmpccmr1 |= TIM_CCMR1_CC1S_0;
  5842. }
  5843. /* Set the filter */
  5844. tmpccmr1 &= ~TIM_CCMR1_IC1F;
  5845. tmpccmr1 |= ((TIM_ICFilter << 4U) & TIM_CCMR1_IC1F);
  5846. /* Select the Polarity and set the CC1E Bit */
  5847. tmpccer &= ~(TIM_CCER_CC1P | TIM_CCER_CC1NP);
  5848. tmpccer |= (TIM_ICPolarity & (TIM_CCER_CC1P | TIM_CCER_CC1NP));
  5849. /* Write to TIMx CCMR1 and CCER registers */
  5850. TIMx->CCMR1 = tmpccmr1;
  5851. TIMx->CCER = tmpccer;
  5852. }
  5853. /**
  5854. * @brief Configure the Polarity and Filter for TI1.
  5855. * @param TIMx to select the TIM peripheral.
  5856. * @param TIM_ICPolarity The Input Polarity.
  5857. * This parameter can be one of the following values:
  5858. * @arg TIM_ICPOLARITY_RISING
  5859. * @arg TIM_ICPOLARITY_FALLING
  5860. * @arg TIM_ICPOLARITY_BOTHEDGE
  5861. * @param TIM_ICFilter Specifies the Input Capture Filter.
  5862. * This parameter must be a value between 0x00 and 0x0F.
  5863. * @retval None
  5864. */
  5865. static void TIM_TI1_ConfigInputStage(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICFilter)
  5866. {
  5867. uint32_t tmpccmr1;
  5868. uint32_t tmpccer;
  5869. /* Disable the Channel 1: Reset the CC1E Bit */
  5870. tmpccer = TIMx->CCER;
  5871. TIMx->CCER &= ~TIM_CCER_CC1E;
  5872. tmpccmr1 = TIMx->CCMR1;
  5873. /* Set the filter */
  5874. tmpccmr1 &= ~TIM_CCMR1_IC1F;
  5875. tmpccmr1 |= (TIM_ICFilter << 4U);
  5876. /* Select the Polarity and set the CC1E Bit */
  5877. tmpccer &= ~(TIM_CCER_CC1P | TIM_CCER_CC1NP);
  5878. tmpccer |= TIM_ICPolarity;
  5879. /* Write to TIMx CCMR1 and CCER registers */
  5880. TIMx->CCMR1 = tmpccmr1;
  5881. TIMx->CCER = tmpccer;
  5882. }
  5883. /**
  5884. * @brief Configure the TI2 as Input.
  5885. * @param TIMx to select the TIM peripheral
  5886. * @param TIM_ICPolarity The Input Polarity.
  5887. * This parameter can be one of the following values:
  5888. * @arg TIM_ICPOLARITY_RISING
  5889. * @arg TIM_ICPOLARITY_FALLING
  5890. * @arg TIM_ICPOLARITY_BOTHEDGE
  5891. * @param TIM_ICSelection specifies the input to be used.
  5892. * This parameter can be one of the following values:
  5893. * @arg TIM_ICSELECTION_DIRECTTI: TIM Input 2 is selected to be connected to IC2.
  5894. * @arg TIM_ICSELECTION_INDIRECTTI: TIM Input 2 is selected to be connected to IC1.
  5895. * @arg TIM_ICSELECTION_TRC: TIM Input 2 is selected to be connected to TRC.
  5896. * @param TIM_ICFilter Specifies the Input Capture Filter.
  5897. * This parameter must be a value between 0x00 and 0x0F.
  5898. * @retval None
  5899. * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI1FP2
  5900. * (on channel1 path) is used as the input signal. Therefore CCMR1 must be
  5901. * protected against un-initialized filter and polarity values.
  5902. */
  5903. static void TIM_TI2_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
  5904. uint32_t TIM_ICFilter)
  5905. {
  5906. uint32_t tmpccmr1;
  5907. uint32_t tmpccer;
  5908. /* Disable the Channel 2: Reset the CC2E Bit */
  5909. TIMx->CCER &= ~TIM_CCER_CC2E;
  5910. tmpccmr1 = TIMx->CCMR1;
  5911. tmpccer = TIMx->CCER;
  5912. /* Select the Input */
  5913. tmpccmr1 &= ~TIM_CCMR1_CC2S;
  5914. tmpccmr1 |= (TIM_ICSelection << 8U);
  5915. /* Set the filter */
  5916. tmpccmr1 &= ~TIM_CCMR1_IC2F;
  5917. tmpccmr1 |= ((TIM_ICFilter << 12U) & TIM_CCMR1_IC2F);
  5918. /* Select the Polarity and set the CC2E Bit */
  5919. tmpccer &= ~(TIM_CCER_CC2P | TIM_CCER_CC2NP);
  5920. tmpccer |= ((TIM_ICPolarity << 4U) & (TIM_CCER_CC2P | TIM_CCER_CC2NP));
  5921. /* Write to TIMx CCMR1 and CCER registers */
  5922. TIMx->CCMR1 = tmpccmr1 ;
  5923. TIMx->CCER = tmpccer;
  5924. }
  5925. /**
  5926. * @brief Configure the Polarity and Filter for TI2.
  5927. * @param TIMx to select the TIM peripheral.
  5928. * @param TIM_ICPolarity The Input Polarity.
  5929. * This parameter can be one of the following values:
  5930. * @arg TIM_ICPOLARITY_RISING
  5931. * @arg TIM_ICPOLARITY_FALLING
  5932. * @arg TIM_ICPOLARITY_BOTHEDGE
  5933. * @param TIM_ICFilter Specifies the Input Capture Filter.
  5934. * This parameter must be a value between 0x00 and 0x0F.
  5935. * @retval None
  5936. */
  5937. static void TIM_TI2_ConfigInputStage(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICFilter)
  5938. {
  5939. uint32_t tmpccmr1;
  5940. uint32_t tmpccer;
  5941. /* Disable the Channel 2: Reset the CC2E Bit */
  5942. TIMx->CCER &= ~TIM_CCER_CC2E;
  5943. tmpccmr1 = TIMx->CCMR1;
  5944. tmpccer = TIMx->CCER;
  5945. /* Set the filter */
  5946. tmpccmr1 &= ~TIM_CCMR1_IC2F;
  5947. tmpccmr1 |= (TIM_ICFilter << 12U);
  5948. /* Select the Polarity and set the CC2E Bit */
  5949. tmpccer &= ~(TIM_CCER_CC2P | TIM_CCER_CC2NP);
  5950. tmpccer |= (TIM_ICPolarity << 4U);
  5951. /* Write to TIMx CCMR1 and CCER registers */
  5952. TIMx->CCMR1 = tmpccmr1 ;
  5953. TIMx->CCER = tmpccer;
  5954. }
  5955. /**
  5956. * @brief Configure the TI3 as Input.
  5957. * @param TIMx to select the TIM peripheral
  5958. * @param TIM_ICPolarity The Input Polarity.
  5959. * This parameter can be one of the following values:
  5960. * @arg TIM_ICPOLARITY_RISING
  5961. * @arg TIM_ICPOLARITY_FALLING
  5962. * @arg TIM_ICPOLARITY_BOTHEDGE
  5963. * @param TIM_ICSelection specifies the input to be used.
  5964. * This parameter can be one of the following values:
  5965. * @arg TIM_ICSELECTION_DIRECTTI: TIM Input 3 is selected to be connected to IC3.
  5966. * @arg TIM_ICSELECTION_INDIRECTTI: TIM Input 3 is selected to be connected to IC4.
  5967. * @arg TIM_ICSELECTION_TRC: TIM Input 3 is selected to be connected to TRC.
  5968. * @param TIM_ICFilter Specifies the Input Capture Filter.
  5969. * This parameter must be a value between 0x00 and 0x0F.
  5970. * @retval None
  5971. * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI3FP4
  5972. * (on channel1 path) is used as the input signal. Therefore CCMR2 must be
  5973. * protected against un-initialized filter and polarity values.
  5974. */
  5975. static void TIM_TI3_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
  5976. uint32_t TIM_ICFilter)
  5977. {
  5978. uint32_t tmpccmr2;
  5979. uint32_t tmpccer;
  5980. /* Disable the Channel 3: Reset the CC3E Bit */
  5981. TIMx->CCER &= ~TIM_CCER_CC3E;
  5982. tmpccmr2 = TIMx->CCMR2;
  5983. tmpccer = TIMx->CCER;
  5984. /* Select the Input */
  5985. tmpccmr2 &= ~TIM_CCMR2_CC3S;
  5986. tmpccmr2 |= TIM_ICSelection;
  5987. /* Set the filter */
  5988. tmpccmr2 &= ~TIM_CCMR2_IC3F;
  5989. tmpccmr2 |= ((TIM_ICFilter << 4U) & TIM_CCMR2_IC3F);
  5990. /* Select the Polarity and set the CC3E Bit */
  5991. tmpccer &= ~(TIM_CCER_CC3P | TIM_CCER_CC3NP);
  5992. tmpccer |= ((TIM_ICPolarity << 8U) & (TIM_CCER_CC3P | TIM_CCER_CC3NP));
  5993. /* Write to TIMx CCMR2 and CCER registers */
  5994. TIMx->CCMR2 = tmpccmr2;
  5995. TIMx->CCER = tmpccer;
  5996. }
  5997. /**
  5998. * @brief Configure the TI4 as Input.
  5999. * @param TIMx to select the TIM peripheral
  6000. * @param TIM_ICPolarity The Input Polarity.
  6001. * This parameter can be one of the following values:
  6002. * @arg TIM_ICPOLARITY_RISING
  6003. * @arg TIM_ICPOLARITY_FALLING
  6004. * @arg TIM_ICPOLARITY_BOTHEDGE
  6005. * @param TIM_ICSelection specifies the input to be used.
  6006. * This parameter can be one of the following values:
  6007. * @arg TIM_ICSELECTION_DIRECTTI: TIM Input 4 is selected to be connected to IC4.
  6008. * @arg TIM_ICSELECTION_INDIRECTTI: TIM Input 4 is selected to be connected to IC3.
  6009. * @arg TIM_ICSELECTION_TRC: TIM Input 4 is selected to be connected to TRC.
  6010. * @param TIM_ICFilter Specifies the Input Capture Filter.
  6011. * This parameter must be a value between 0x00 and 0x0F.
  6012. * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI4FP3
  6013. * (on channel1 path) is used as the input signal. Therefore CCMR2 must be
  6014. * protected against un-initialized filter and polarity values.
  6015. * @retval None
  6016. */
  6017. static void TIM_TI4_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
  6018. uint32_t TIM_ICFilter)
  6019. {
  6020. uint32_t tmpccmr2;
  6021. uint32_t tmpccer;
  6022. /* Disable the Channel 4: Reset the CC4E Bit */
  6023. TIMx->CCER &= ~TIM_CCER_CC4E;
  6024. tmpccmr2 = TIMx->CCMR2;
  6025. tmpccer = TIMx->CCER;
  6026. /* Select the Input */
  6027. tmpccmr2 &= ~TIM_CCMR2_CC4S;
  6028. tmpccmr2 |= (TIM_ICSelection << 8U);
  6029. /* Set the filter */
  6030. tmpccmr2 &= ~TIM_CCMR2_IC4F;
  6031. tmpccmr2 |= ((TIM_ICFilter << 12U) & TIM_CCMR2_IC4F);
  6032. /* Select the Polarity and set the CC4E Bit */
  6033. tmpccer &= ~(TIM_CCER_CC4P | TIM_CCER_CC4NP);
  6034. tmpccer |= ((TIM_ICPolarity << 12U) & (TIM_CCER_CC4P | TIM_CCER_CC4NP));
  6035. /* Write to TIMx CCMR2 and CCER registers */
  6036. TIMx->CCMR2 = tmpccmr2;
  6037. TIMx->CCER = tmpccer ;
  6038. }
  6039. /**
  6040. * @brief Selects the Input Trigger source
  6041. * @param TIMx to select the TIM peripheral
  6042. * @param InputTriggerSource The Input Trigger source.
  6043. * This parameter can be one of the following values:
  6044. * @arg TIM_TS_ITR0: Internal Trigger 0
  6045. * @arg TIM_TS_ITR1: Internal Trigger 1
  6046. * @arg TIM_TS_ITR2: Internal Trigger 2
  6047. * @arg TIM_TS_ITR3: Internal Trigger 3
  6048. * @arg TIM_TS_TI1F_ED: TI1 Edge Detector
  6049. * @arg TIM_TS_TI1FP1: Filtered Timer Input 1
  6050. * @arg TIM_TS_TI2FP2: Filtered Timer Input 2
  6051. * @arg TIM_TS_ETRF: External Trigger input
  6052. * @retval None
  6053. */
  6054. static void TIM_ITRx_SetConfig(TIM_TypeDef *TIMx, uint32_t InputTriggerSource)
  6055. {
  6056. uint32_t tmpsmcr;
  6057. /* Get the TIMx SMCR register value */
  6058. tmpsmcr = TIMx->SMCR;
  6059. /* Reset the TS Bits */
  6060. tmpsmcr &= ~TIM_SMCR_TS;
  6061. /* Set the Input Trigger source and the slave mode*/
  6062. tmpsmcr |= (InputTriggerSource | TIM_SLAVEMODE_EXTERNAL1);
  6063. /* Write to TIMx SMCR */
  6064. TIMx->SMCR = tmpsmcr;
  6065. }
  6066. /**
  6067. * @brief Configures the TIMx External Trigger (ETR).
  6068. * @param TIMx to select the TIM peripheral
  6069. * @param TIM_ExtTRGPrescaler The external Trigger Prescaler.
  6070. * This parameter can be one of the following values:
  6071. * @arg TIM_ETRPRESCALER_DIV1: ETRP Prescaler OFF.
  6072. * @arg TIM_ETRPRESCALER_DIV2: ETRP frequency divided by 2.
  6073. * @arg TIM_ETRPRESCALER_DIV4: ETRP frequency divided by 4.
  6074. * @arg TIM_ETRPRESCALER_DIV8: ETRP frequency divided by 8.
  6075. * @param TIM_ExtTRGPolarity The external Trigger Polarity.
  6076. * This parameter can be one of the following values:
  6077. * @arg TIM_ETRPOLARITY_INVERTED: active low or falling edge active.
  6078. * @arg TIM_ETRPOLARITY_NONINVERTED: active high or rising edge active.
  6079. * @param ExtTRGFilter External Trigger Filter.
  6080. * This parameter must be a value between 0x00 and 0x0F
  6081. * @retval None
  6082. */
  6083. static void TIM_ETR_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ExtTRGPrescaler,
  6084. uint32_t TIM_ExtTRGPolarity, uint32_t ExtTRGFilter)
  6085. {
  6086. uint32_t tmpsmcr;
  6087. tmpsmcr = TIMx->SMCR;
  6088. /* Reset the ETR Bits */
  6089. tmpsmcr &= ~(TIM_SMCR_ETF | TIM_SMCR_ETPS | TIM_SMCR_ECE | TIM_SMCR_ETP);
  6090. /* Set the Prescaler, the Filter value and the Polarity */
  6091. tmpsmcr |= (uint32_t)(TIM_ExtTRGPrescaler | (TIM_ExtTRGPolarity | (ExtTRGFilter << 8U)));
  6092. /* Write to TIMx SMCR */
  6093. TIMx->SMCR = tmpsmcr;
  6094. }
  6095. /**
  6096. * @brief Enables or disables the TIM Capture Compare Channel x.
  6097. * @param TIMx to select the TIM peripheral
  6098. * @param Channel specifies the TIM Channel
  6099. * This parameter can be one of the following values:
  6100. * @arg TIM_CHANNEL_1: TIM Channel 1
  6101. * @arg TIM_CHANNEL_2: TIM Channel 2
  6102. * @arg TIM_CHANNEL_3: TIM Channel 3
  6103. * @arg TIM_CHANNEL_4: TIM Channel 4
  6104. * @param ChannelState specifies the TIM Channel CCxE bit new state.
  6105. * This parameter can be: TIM_CCx_ENABLE or TIM_CCx_DISABLE.
  6106. * @retval None
  6107. */
  6108. static void TIM_CCxChannelCmd(TIM_TypeDef *TIMx, uint32_t Channel, uint32_t ChannelState)
  6109. {
  6110. uint32_t tmp;
  6111. /* Check the parameters */
  6112. assert_param(IS_TIM_CC1_INSTANCE(TIMx));
  6113. assert_param(IS_TIM_CHANNELS(Channel));
  6114. tmp = TIM_CCER_CC1E << (Channel & 0x1FU); /* 0x1FU = 31 bits max shift */
  6115. /* Reset the CCxE Bit */
  6116. TIMx->CCER &= ~tmp;
  6117. /* Set or reset the CCxE Bit */
  6118. TIMx->CCER |= (uint32_t)(ChannelState << (Channel & 0x1FU)); /* 0x1FU = 31 bits max shift */
  6119. }
  6120. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  6121. /**
  6122. * @brief Reset interrupt callbacks to the legacy weak callbacks.
  6123. * @param htim pointer to a TIM_HandleTypeDef structure that contains
  6124. * the configuration information for TIM module.
  6125. * @retval None
  6126. */
  6127. void TIM_ResetCallback(TIM_HandleTypeDef *htim)
  6128. {
  6129. /* Reset the TIM callback to the legacy weak callbacks */
  6130. htim->PeriodElapsedCallback = HAL_TIM_PeriodElapsedCallback;
  6131. htim->PeriodElapsedHalfCpltCallback = HAL_TIM_PeriodElapsedHalfCpltCallback;
  6132. htim->TriggerCallback = HAL_TIM_TriggerCallback;
  6133. htim->TriggerHalfCpltCallback = HAL_TIM_TriggerHalfCpltCallback;
  6134. htim->IC_CaptureCallback = HAL_TIM_IC_CaptureCallback;
  6135. htim->IC_CaptureHalfCpltCallback = HAL_TIM_IC_CaptureHalfCpltCallback;
  6136. htim->OC_DelayElapsedCallback = HAL_TIM_OC_DelayElapsedCallback;
  6137. htim->PWM_PulseFinishedCallback = HAL_TIM_PWM_PulseFinishedCallback;
  6138. htim->PWM_PulseFinishedHalfCpltCallback = HAL_TIM_PWM_PulseFinishedHalfCpltCallback;
  6139. htim->ErrorCallback = HAL_TIM_ErrorCallback;
  6140. }
  6141. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  6142. /**
  6143. * @}
  6144. */
  6145. #endif /* HAL_TIM_MODULE_ENABLED */
  6146. /**
  6147. * @}
  6148. */
  6149. /**
  6150. * @}
  6151. */