stm32l0xx_hal_tim.c 221 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185
  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. uint32_t itsource = htim->Instance->DIER;
  3126. uint32_t itflag = htim->Instance->SR;
  3127. /* Capture compare 1 event */
  3128. if ((itflag & (TIM_FLAG_CC1)) == (TIM_FLAG_CC1))
  3129. {
  3130. if ((itsource & (TIM_IT_CC1)) == (TIM_IT_CC1))
  3131. {
  3132. {
  3133. __HAL_TIM_CLEAR_FLAG(htim, TIM_FLAG_CC1);
  3134. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
  3135. /* Input capture event */
  3136. if ((htim->Instance->CCMR1 & TIM_CCMR1_CC1S) != 0x00U)
  3137. {
  3138. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  3139. htim->IC_CaptureCallback(htim);
  3140. #else
  3141. HAL_TIM_IC_CaptureCallback(htim);
  3142. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  3143. }
  3144. /* Output compare event */
  3145. else
  3146. {
  3147. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  3148. htim->OC_DelayElapsedCallback(htim);
  3149. htim->PWM_PulseFinishedCallback(htim);
  3150. #else
  3151. HAL_TIM_OC_DelayElapsedCallback(htim);
  3152. HAL_TIM_PWM_PulseFinishedCallback(htim);
  3153. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  3154. }
  3155. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  3156. }
  3157. }
  3158. }
  3159. /* Capture compare 2 event */
  3160. if ((itflag & (TIM_FLAG_CC2)) == (TIM_FLAG_CC2))
  3161. {
  3162. if ((itsource & (TIM_IT_CC2)) == (TIM_IT_CC2))
  3163. {
  3164. __HAL_TIM_CLEAR_FLAG(htim, TIM_FLAG_CC2);
  3165. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
  3166. /* Input capture event */
  3167. if ((htim->Instance->CCMR1 & TIM_CCMR1_CC2S) != 0x00U)
  3168. {
  3169. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  3170. htim->IC_CaptureCallback(htim);
  3171. #else
  3172. HAL_TIM_IC_CaptureCallback(htim);
  3173. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  3174. }
  3175. /* Output compare event */
  3176. else
  3177. {
  3178. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  3179. htim->OC_DelayElapsedCallback(htim);
  3180. htim->PWM_PulseFinishedCallback(htim);
  3181. #else
  3182. HAL_TIM_OC_DelayElapsedCallback(htim);
  3183. HAL_TIM_PWM_PulseFinishedCallback(htim);
  3184. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  3185. }
  3186. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  3187. }
  3188. }
  3189. /* Capture compare 3 event */
  3190. if ((itflag & (TIM_FLAG_CC3)) == (TIM_FLAG_CC3))
  3191. {
  3192. if ((itsource & (TIM_IT_CC3)) == (TIM_IT_CC3))
  3193. {
  3194. __HAL_TIM_CLEAR_FLAG(htim, TIM_FLAG_CC3);
  3195. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
  3196. /* Input capture event */
  3197. if ((htim->Instance->CCMR2 & TIM_CCMR2_CC3S) != 0x00U)
  3198. {
  3199. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  3200. htim->IC_CaptureCallback(htim);
  3201. #else
  3202. HAL_TIM_IC_CaptureCallback(htim);
  3203. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  3204. }
  3205. /* Output compare event */
  3206. else
  3207. {
  3208. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  3209. htim->OC_DelayElapsedCallback(htim);
  3210. htim->PWM_PulseFinishedCallback(htim);
  3211. #else
  3212. HAL_TIM_OC_DelayElapsedCallback(htim);
  3213. HAL_TIM_PWM_PulseFinishedCallback(htim);
  3214. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  3215. }
  3216. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  3217. }
  3218. }
  3219. /* Capture compare 4 event */
  3220. if ((itflag & (TIM_FLAG_CC4)) == (TIM_FLAG_CC4))
  3221. {
  3222. if ((itsource & (TIM_IT_CC4)) == (TIM_IT_CC4))
  3223. {
  3224. __HAL_TIM_CLEAR_FLAG(htim, TIM_FLAG_CC4);
  3225. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
  3226. /* Input capture event */
  3227. if ((htim->Instance->CCMR2 & TIM_CCMR2_CC4S) != 0x00U)
  3228. {
  3229. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  3230. htim->IC_CaptureCallback(htim);
  3231. #else
  3232. HAL_TIM_IC_CaptureCallback(htim);
  3233. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  3234. }
  3235. /* Output compare event */
  3236. else
  3237. {
  3238. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  3239. htim->OC_DelayElapsedCallback(htim);
  3240. htim->PWM_PulseFinishedCallback(htim);
  3241. #else
  3242. HAL_TIM_OC_DelayElapsedCallback(htim);
  3243. HAL_TIM_PWM_PulseFinishedCallback(htim);
  3244. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  3245. }
  3246. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  3247. }
  3248. }
  3249. /* TIM Update event */
  3250. if ((itflag & (TIM_FLAG_UPDATE)) == (TIM_FLAG_UPDATE))
  3251. {
  3252. if ((itsource & (TIM_IT_UPDATE)) == (TIM_IT_UPDATE))
  3253. {
  3254. __HAL_TIM_CLEAR_FLAG(htim, TIM_FLAG_UPDATE);
  3255. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  3256. htim->PeriodElapsedCallback(htim);
  3257. #else
  3258. HAL_TIM_PeriodElapsedCallback(htim);
  3259. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  3260. }
  3261. }
  3262. /* TIM Trigger detection event */
  3263. if ((itflag & (TIM_FLAG_TRIGGER)) == (TIM_FLAG_TRIGGER))
  3264. {
  3265. if ((itsource & (TIM_IT_TRIGGER)) == (TIM_IT_TRIGGER))
  3266. {
  3267. __HAL_TIM_CLEAR_FLAG(htim, TIM_FLAG_TRIGGER);
  3268. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  3269. htim->TriggerCallback(htim);
  3270. #else
  3271. HAL_TIM_TriggerCallback(htim);
  3272. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  3273. }
  3274. }
  3275. }
  3276. /**
  3277. * @}
  3278. */
  3279. /** @defgroup TIM_Exported_Functions_Group8 TIM Peripheral Control functions
  3280. * @brief TIM Peripheral Control functions
  3281. *
  3282. @verbatim
  3283. ==============================================================================
  3284. ##### Peripheral Control functions #####
  3285. ==============================================================================
  3286. [..]
  3287. This section provides functions allowing to:
  3288. (+) Configure The Input Output channels for OC, PWM, IC or One Pulse mode.
  3289. (+) Configure External Clock source.
  3290. (+) Configure Master and the Slave synchronization.
  3291. (+) Configure the DMA Burst Mode.
  3292. @endverbatim
  3293. * @{
  3294. */
  3295. /**
  3296. * @brief Initializes the TIM Output Compare Channels according to the specified
  3297. * parameters in the TIM_OC_InitTypeDef.
  3298. * @param htim TIM Output Compare handle
  3299. * @param sConfig TIM Output Compare configuration structure
  3300. * @param Channel TIM Channels to configure
  3301. * This parameter can be one of the following values:
  3302. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  3303. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  3304. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  3305. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  3306. * @retval HAL status
  3307. */
  3308. HAL_StatusTypeDef HAL_TIM_OC_ConfigChannel(TIM_HandleTypeDef *htim,
  3309. const TIM_OC_InitTypeDef *sConfig,
  3310. uint32_t Channel)
  3311. {
  3312. HAL_StatusTypeDef status = HAL_OK;
  3313. /* Check the parameters */
  3314. assert_param(IS_TIM_CHANNELS(Channel));
  3315. assert_param(IS_TIM_OC_MODE(sConfig->OCMode));
  3316. assert_param(IS_TIM_OC_POLARITY(sConfig->OCPolarity));
  3317. /* Process Locked */
  3318. __HAL_LOCK(htim);
  3319. switch (Channel)
  3320. {
  3321. case TIM_CHANNEL_1:
  3322. {
  3323. /* Check the parameters */
  3324. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  3325. /* Configure the TIM Channel 1 in Output Compare */
  3326. TIM_OC1_SetConfig(htim->Instance, sConfig);
  3327. break;
  3328. }
  3329. case TIM_CHANNEL_2:
  3330. {
  3331. /* Check the parameters */
  3332. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  3333. /* Configure the TIM Channel 2 in Output Compare */
  3334. TIM_OC2_SetConfig(htim->Instance, sConfig);
  3335. break;
  3336. }
  3337. case TIM_CHANNEL_3:
  3338. {
  3339. /* Check the parameters */
  3340. assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
  3341. /* Configure the TIM Channel 3 in Output Compare */
  3342. TIM_OC3_SetConfig(htim->Instance, sConfig);
  3343. break;
  3344. }
  3345. case TIM_CHANNEL_4:
  3346. {
  3347. /* Check the parameters */
  3348. assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
  3349. /* Configure the TIM Channel 4 in Output Compare */
  3350. TIM_OC4_SetConfig(htim->Instance, sConfig);
  3351. break;
  3352. }
  3353. default:
  3354. status = HAL_ERROR;
  3355. break;
  3356. }
  3357. __HAL_UNLOCK(htim);
  3358. return status;
  3359. }
  3360. /**
  3361. * @brief Initializes the TIM Input Capture Channels according to the specified
  3362. * parameters in the TIM_IC_InitTypeDef.
  3363. * @param htim TIM IC handle
  3364. * @param sConfig TIM Input Capture configuration structure
  3365. * @param Channel TIM Channel to configure
  3366. * This parameter can be one of the following values:
  3367. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  3368. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  3369. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  3370. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  3371. * @retval HAL status
  3372. */
  3373. HAL_StatusTypeDef HAL_TIM_IC_ConfigChannel(TIM_HandleTypeDef *htim, const TIM_IC_InitTypeDef *sConfig, uint32_t Channel)
  3374. {
  3375. HAL_StatusTypeDef status = HAL_OK;
  3376. /* Check the parameters */
  3377. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  3378. assert_param(IS_TIM_IC_POLARITY(sConfig->ICPolarity));
  3379. assert_param(IS_TIM_IC_SELECTION(sConfig->ICSelection));
  3380. assert_param(IS_TIM_IC_PRESCALER(sConfig->ICPrescaler));
  3381. assert_param(IS_TIM_IC_FILTER(sConfig->ICFilter));
  3382. /* Process Locked */
  3383. __HAL_LOCK(htim);
  3384. if (Channel == TIM_CHANNEL_1)
  3385. {
  3386. /* TI1 Configuration */
  3387. TIM_TI1_SetConfig(htim->Instance,
  3388. sConfig->ICPolarity,
  3389. sConfig->ICSelection,
  3390. sConfig->ICFilter);
  3391. /* Reset the IC1PSC Bits */
  3392. htim->Instance->CCMR1 &= ~TIM_CCMR1_IC1PSC;
  3393. /* Set the IC1PSC value */
  3394. htim->Instance->CCMR1 |= sConfig->ICPrescaler;
  3395. }
  3396. else if (Channel == TIM_CHANNEL_2)
  3397. {
  3398. /* TI2 Configuration */
  3399. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  3400. TIM_TI2_SetConfig(htim->Instance,
  3401. sConfig->ICPolarity,
  3402. sConfig->ICSelection,
  3403. sConfig->ICFilter);
  3404. /* Reset the IC2PSC Bits */
  3405. htim->Instance->CCMR1 &= ~TIM_CCMR1_IC2PSC;
  3406. /* Set the IC2PSC value */
  3407. htim->Instance->CCMR1 |= (sConfig->ICPrescaler << 8U);
  3408. }
  3409. else if (Channel == TIM_CHANNEL_3)
  3410. {
  3411. /* TI3 Configuration */
  3412. assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
  3413. TIM_TI3_SetConfig(htim->Instance,
  3414. sConfig->ICPolarity,
  3415. sConfig->ICSelection,
  3416. sConfig->ICFilter);
  3417. /* Reset the IC3PSC Bits */
  3418. htim->Instance->CCMR2 &= ~TIM_CCMR2_IC3PSC;
  3419. /* Set the IC3PSC value */
  3420. htim->Instance->CCMR2 |= sConfig->ICPrescaler;
  3421. }
  3422. else if (Channel == TIM_CHANNEL_4)
  3423. {
  3424. /* TI4 Configuration */
  3425. assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
  3426. TIM_TI4_SetConfig(htim->Instance,
  3427. sConfig->ICPolarity,
  3428. sConfig->ICSelection,
  3429. sConfig->ICFilter);
  3430. /* Reset the IC4PSC Bits */
  3431. htim->Instance->CCMR2 &= ~TIM_CCMR2_IC4PSC;
  3432. /* Set the IC4PSC value */
  3433. htim->Instance->CCMR2 |= (sConfig->ICPrescaler << 8U);
  3434. }
  3435. else
  3436. {
  3437. status = HAL_ERROR;
  3438. }
  3439. __HAL_UNLOCK(htim);
  3440. return status;
  3441. }
  3442. /**
  3443. * @brief Initializes the TIM PWM channels according to the specified
  3444. * parameters in the TIM_OC_InitTypeDef.
  3445. * @param htim TIM PWM handle
  3446. * @param sConfig TIM PWM configuration structure
  3447. * @param Channel TIM Channels to be configured
  3448. * This parameter can be one of the following values:
  3449. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  3450. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  3451. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  3452. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  3453. * @retval HAL status
  3454. */
  3455. HAL_StatusTypeDef HAL_TIM_PWM_ConfigChannel(TIM_HandleTypeDef *htim,
  3456. const TIM_OC_InitTypeDef *sConfig,
  3457. uint32_t Channel)
  3458. {
  3459. HAL_StatusTypeDef status = HAL_OK;
  3460. /* Check the parameters */
  3461. assert_param(IS_TIM_CHANNELS(Channel));
  3462. assert_param(IS_TIM_PWM_MODE(sConfig->OCMode));
  3463. assert_param(IS_TIM_OC_POLARITY(sConfig->OCPolarity));
  3464. assert_param(IS_TIM_FAST_STATE(sConfig->OCFastMode));
  3465. /* Process Locked */
  3466. __HAL_LOCK(htim);
  3467. switch (Channel)
  3468. {
  3469. case TIM_CHANNEL_1:
  3470. {
  3471. /* Check the parameters */
  3472. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  3473. /* Configure the Channel 1 in PWM mode */
  3474. TIM_OC1_SetConfig(htim->Instance, sConfig);
  3475. /* Set the Preload enable bit for channel1 */
  3476. htim->Instance->CCMR1 |= TIM_CCMR1_OC1PE;
  3477. /* Configure the Output Fast mode */
  3478. htim->Instance->CCMR1 &= ~TIM_CCMR1_OC1FE;
  3479. htim->Instance->CCMR1 |= sConfig->OCFastMode;
  3480. break;
  3481. }
  3482. case TIM_CHANNEL_2:
  3483. {
  3484. /* Check the parameters */
  3485. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  3486. /* Configure the Channel 2 in PWM mode */
  3487. TIM_OC2_SetConfig(htim->Instance, sConfig);
  3488. /* Set the Preload enable bit for channel2 */
  3489. htim->Instance->CCMR1 |= TIM_CCMR1_OC2PE;
  3490. /* Configure the Output Fast mode */
  3491. htim->Instance->CCMR1 &= ~TIM_CCMR1_OC2FE;
  3492. htim->Instance->CCMR1 |= sConfig->OCFastMode << 8U;
  3493. break;
  3494. }
  3495. case TIM_CHANNEL_3:
  3496. {
  3497. /* Check the parameters */
  3498. assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
  3499. /* Configure the Channel 3 in PWM mode */
  3500. TIM_OC3_SetConfig(htim->Instance, sConfig);
  3501. /* Set the Preload enable bit for channel3 */
  3502. htim->Instance->CCMR2 |= TIM_CCMR2_OC3PE;
  3503. /* Configure the Output Fast mode */
  3504. htim->Instance->CCMR2 &= ~TIM_CCMR2_OC3FE;
  3505. htim->Instance->CCMR2 |= sConfig->OCFastMode;
  3506. break;
  3507. }
  3508. case TIM_CHANNEL_4:
  3509. {
  3510. /* Check the parameters */
  3511. assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
  3512. /* Configure the Channel 4 in PWM mode */
  3513. TIM_OC4_SetConfig(htim->Instance, sConfig);
  3514. /* Set the Preload enable bit for channel4 */
  3515. htim->Instance->CCMR2 |= TIM_CCMR2_OC4PE;
  3516. /* Configure the Output Fast mode */
  3517. htim->Instance->CCMR2 &= ~TIM_CCMR2_OC4FE;
  3518. htim->Instance->CCMR2 |= sConfig->OCFastMode << 8U;
  3519. break;
  3520. }
  3521. default:
  3522. status = HAL_ERROR;
  3523. break;
  3524. }
  3525. __HAL_UNLOCK(htim);
  3526. return status;
  3527. }
  3528. /**
  3529. * @brief Initializes the TIM One Pulse Channels according to the specified
  3530. * parameters in the TIM_OnePulse_InitTypeDef.
  3531. * @param htim TIM One Pulse handle
  3532. * @param sConfig TIM One Pulse configuration structure
  3533. * @param OutputChannel TIM output channel to configure
  3534. * This parameter can be one of the following values:
  3535. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  3536. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  3537. * @param InputChannel TIM input Channel to configure
  3538. * This parameter can be one of the following values:
  3539. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  3540. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  3541. * @note To output a waveform with a minimum delay user can enable the fast
  3542. * mode by calling the @ref __HAL_TIM_ENABLE_OCxFAST macro. Then CCx
  3543. * output is forced in response to the edge detection on TIx input,
  3544. * without taking in account the comparison.
  3545. * @retval HAL status
  3546. */
  3547. HAL_StatusTypeDef HAL_TIM_OnePulse_ConfigChannel(TIM_HandleTypeDef *htim, TIM_OnePulse_InitTypeDef *sConfig,
  3548. uint32_t OutputChannel, uint32_t InputChannel)
  3549. {
  3550. HAL_StatusTypeDef status = HAL_OK;
  3551. TIM_OC_InitTypeDef temp1;
  3552. /* Check the parameters */
  3553. assert_param(IS_TIM_OPM_CHANNELS(OutputChannel));
  3554. assert_param(IS_TIM_OPM_CHANNELS(InputChannel));
  3555. if (OutputChannel != InputChannel)
  3556. {
  3557. /* Process Locked */
  3558. __HAL_LOCK(htim);
  3559. htim->State = HAL_TIM_STATE_BUSY;
  3560. /* Extract the Output compare configuration from sConfig structure */
  3561. temp1.OCMode = sConfig->OCMode;
  3562. temp1.Pulse = sConfig->Pulse;
  3563. temp1.OCPolarity = sConfig->OCPolarity;
  3564. switch (OutputChannel)
  3565. {
  3566. case TIM_CHANNEL_1:
  3567. {
  3568. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  3569. TIM_OC1_SetConfig(htim->Instance, &temp1);
  3570. break;
  3571. }
  3572. case TIM_CHANNEL_2:
  3573. {
  3574. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  3575. TIM_OC2_SetConfig(htim->Instance, &temp1);
  3576. break;
  3577. }
  3578. default:
  3579. status = HAL_ERROR;
  3580. break;
  3581. }
  3582. if (status == HAL_OK)
  3583. {
  3584. switch (InputChannel)
  3585. {
  3586. case TIM_CHANNEL_1:
  3587. {
  3588. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  3589. TIM_TI1_SetConfig(htim->Instance, sConfig->ICPolarity,
  3590. sConfig->ICSelection, sConfig->ICFilter);
  3591. /* Reset the IC1PSC Bits */
  3592. htim->Instance->CCMR1 &= ~TIM_CCMR1_IC1PSC;
  3593. /* Select the Trigger source */
  3594. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  3595. htim->Instance->SMCR |= TIM_TS_TI1FP1;
  3596. /* Select the Slave Mode */
  3597. htim->Instance->SMCR &= ~TIM_SMCR_SMS;
  3598. htim->Instance->SMCR |= TIM_SLAVEMODE_TRIGGER;
  3599. break;
  3600. }
  3601. case TIM_CHANNEL_2:
  3602. {
  3603. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  3604. TIM_TI2_SetConfig(htim->Instance, sConfig->ICPolarity,
  3605. sConfig->ICSelection, sConfig->ICFilter);
  3606. /* Reset the IC2PSC Bits */
  3607. htim->Instance->CCMR1 &= ~TIM_CCMR1_IC2PSC;
  3608. /* Select the Trigger source */
  3609. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  3610. htim->Instance->SMCR |= TIM_TS_TI2FP2;
  3611. /* Select the Slave Mode */
  3612. htim->Instance->SMCR &= ~TIM_SMCR_SMS;
  3613. htim->Instance->SMCR |= TIM_SLAVEMODE_TRIGGER;
  3614. break;
  3615. }
  3616. default:
  3617. status = HAL_ERROR;
  3618. break;
  3619. }
  3620. }
  3621. htim->State = HAL_TIM_STATE_READY;
  3622. __HAL_UNLOCK(htim);
  3623. return status;
  3624. }
  3625. else
  3626. {
  3627. return HAL_ERROR;
  3628. }
  3629. }
  3630. /**
  3631. * @brief Configure the DMA Burst to transfer Data from the memory to the TIM peripheral
  3632. * @param htim TIM handle
  3633. * @param BurstBaseAddress TIM Base address from where the DMA will start the Data write
  3634. * This parameter can be one of the following values:
  3635. * @arg TIM_DMABASE_CR1
  3636. * @arg TIM_DMABASE_CR2
  3637. * @arg TIM_DMABASE_SMCR
  3638. * @arg TIM_DMABASE_DIER
  3639. * @arg TIM_DMABASE_SR
  3640. * @arg TIM_DMABASE_EGR
  3641. * @arg TIM_DMABASE_CCMR1
  3642. * @arg TIM_DMABASE_CCMR2
  3643. * @arg TIM_DMABASE_CCER
  3644. * @arg TIM_DMABASE_CNT
  3645. * @arg TIM_DMABASE_PSC
  3646. * @arg TIM_DMABASE_ARR
  3647. * @arg TIM_DMABASE_CCR1
  3648. * @arg TIM_DMABASE_CCR2
  3649. * @arg TIM_DMABASE_CCR3
  3650. * @arg TIM_DMABASE_CCR4
  3651. * @arg TIM_DMABASE_OR
  3652. * @param BurstRequestSrc TIM DMA Request sources
  3653. * This parameter can be one of the following values:
  3654. * @arg TIM_DMA_UPDATE: TIM update Interrupt source
  3655. * @arg TIM_DMA_CC1: TIM Capture Compare 1 DMA source
  3656. * @arg TIM_DMA_CC2: TIM Capture Compare 2 DMA source
  3657. * @arg TIM_DMA_CC3: TIM Capture Compare 3 DMA source
  3658. * @arg TIM_DMA_CC4: TIM Capture Compare 4 DMA source
  3659. * @arg TIM_DMA_TRIGGER: TIM Trigger DMA source
  3660. * @param BurstBuffer The Buffer address.
  3661. * @param BurstLength DMA Burst length. This parameter can be one value
  3662. * between: TIM_DMABURSTLENGTH_1TRANSFER and TIM_DMABURSTLENGTH_18TRANSFERS.
  3663. * @note This function should be used only when BurstLength is equal to DMA data transfer length.
  3664. * @retval HAL status
  3665. */
  3666. HAL_StatusTypeDef HAL_TIM_DMABurst_WriteStart(TIM_HandleTypeDef *htim, uint32_t BurstBaseAddress,
  3667. uint32_t BurstRequestSrc, const uint32_t *BurstBuffer,
  3668. uint32_t BurstLength)
  3669. {
  3670. HAL_StatusTypeDef status;
  3671. status = HAL_TIM_DMABurst_MultiWriteStart(htim, BurstBaseAddress, BurstRequestSrc, BurstBuffer, BurstLength,
  3672. ((BurstLength) >> 8U) + 1U);
  3673. return status;
  3674. }
  3675. /**
  3676. * @brief Configure the DMA Burst to transfer multiple Data from the memory to the TIM peripheral
  3677. * @param htim TIM handle
  3678. * @param BurstBaseAddress TIM Base address from where the DMA will start the Data write
  3679. * This parameter can be one of the following values:
  3680. * @arg TIM_DMABASE_CR1
  3681. * @arg TIM_DMABASE_CR2
  3682. * @arg TIM_DMABASE_SMCR
  3683. * @arg TIM_DMABASE_DIER
  3684. * @arg TIM_DMABASE_SR
  3685. * @arg TIM_DMABASE_EGR
  3686. * @arg TIM_DMABASE_CCMR1
  3687. * @arg TIM_DMABASE_CCMR2
  3688. * @arg TIM_DMABASE_CCER
  3689. * @arg TIM_DMABASE_CNT
  3690. * @arg TIM_DMABASE_PSC
  3691. * @arg TIM_DMABASE_ARR
  3692. * @arg TIM_DMABASE_CCR1
  3693. * @arg TIM_DMABASE_CCR2
  3694. * @arg TIM_DMABASE_CCR3
  3695. * @arg TIM_DMABASE_CCR4
  3696. * @arg TIM_DMABASE_OR
  3697. * @param BurstRequestSrc TIM DMA Request sources
  3698. * This parameter can be one of the following values:
  3699. * @arg TIM_DMA_UPDATE: TIM update Interrupt source
  3700. * @arg TIM_DMA_CC1: TIM Capture Compare 1 DMA source
  3701. * @arg TIM_DMA_CC2: TIM Capture Compare 2 DMA source
  3702. * @arg TIM_DMA_CC3: TIM Capture Compare 3 DMA source
  3703. * @arg TIM_DMA_CC4: TIM Capture Compare 4 DMA source
  3704. * @arg TIM_DMA_TRIGGER: TIM Trigger DMA source
  3705. * @param BurstBuffer The Buffer address.
  3706. * @param BurstLength DMA Burst length. This parameter can be one value
  3707. * between: TIM_DMABURSTLENGTH_1TRANSFER and TIM_DMABURSTLENGTH_18TRANSFERS.
  3708. * @param DataLength Data length. This parameter can be one value
  3709. * between 1 and 0xFFFF.
  3710. * @retval HAL status
  3711. */
  3712. HAL_StatusTypeDef HAL_TIM_DMABurst_MultiWriteStart(TIM_HandleTypeDef *htim, uint32_t BurstBaseAddress,
  3713. uint32_t BurstRequestSrc, const uint32_t *BurstBuffer,
  3714. uint32_t BurstLength, uint32_t DataLength)
  3715. {
  3716. HAL_StatusTypeDef status = HAL_OK;
  3717. /* Check the parameters */
  3718. assert_param(IS_TIM_DMABURST_INSTANCE(htim->Instance));
  3719. assert_param(IS_TIM_DMA_BASE(BurstBaseAddress));
  3720. assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc));
  3721. assert_param(IS_TIM_DMA_LENGTH(BurstLength));
  3722. assert_param(IS_TIM_DMA_DATA_LENGTH(DataLength));
  3723. if (htim->DMABurstState == HAL_DMA_BURST_STATE_BUSY)
  3724. {
  3725. return HAL_BUSY;
  3726. }
  3727. else if (htim->DMABurstState == HAL_DMA_BURST_STATE_READY)
  3728. {
  3729. if ((BurstBuffer == NULL) && (BurstLength > 0U))
  3730. {
  3731. return HAL_ERROR;
  3732. }
  3733. else
  3734. {
  3735. htim->DMABurstState = HAL_DMA_BURST_STATE_BUSY;
  3736. }
  3737. }
  3738. else
  3739. {
  3740. /* nothing to do */
  3741. }
  3742. switch (BurstRequestSrc)
  3743. {
  3744. case TIM_DMA_UPDATE:
  3745. {
  3746. /* Set the DMA Period elapsed callbacks */
  3747. htim->hdma[TIM_DMA_ID_UPDATE]->XferCpltCallback = TIM_DMAPeriodElapsedCplt;
  3748. htim->hdma[TIM_DMA_ID_UPDATE]->XferHalfCpltCallback = TIM_DMAPeriodElapsedHalfCplt;
  3749. /* Set the DMA error callback */
  3750. htim->hdma[TIM_DMA_ID_UPDATE]->XferErrorCallback = TIM_DMAError ;
  3751. /* Enable the DMA channel */
  3752. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_UPDATE], (uint32_t)BurstBuffer,
  3753. (uint32_t)&htim->Instance->DMAR, DataLength) != HAL_OK)
  3754. {
  3755. /* Return error status */
  3756. return HAL_ERROR;
  3757. }
  3758. break;
  3759. }
  3760. case TIM_DMA_CC1:
  3761. {
  3762. /* Set the DMA compare callbacks */
  3763. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseCplt;
  3764. htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  3765. /* Set the DMA error callback */
  3766. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  3767. /* Enable the DMA channel */
  3768. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)BurstBuffer,
  3769. (uint32_t)&htim->Instance->DMAR, DataLength) != HAL_OK)
  3770. {
  3771. /* Return error status */
  3772. return HAL_ERROR;
  3773. }
  3774. break;
  3775. }
  3776. case TIM_DMA_CC2:
  3777. {
  3778. /* Set the DMA compare callbacks */
  3779. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseCplt;
  3780. htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  3781. /* Set the DMA error callback */
  3782. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
  3783. /* Enable the DMA channel */
  3784. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)BurstBuffer,
  3785. (uint32_t)&htim->Instance->DMAR, DataLength) != HAL_OK)
  3786. {
  3787. /* Return error status */
  3788. return HAL_ERROR;
  3789. }
  3790. break;
  3791. }
  3792. case TIM_DMA_CC3:
  3793. {
  3794. /* Set the DMA compare callbacks */
  3795. htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseCplt;
  3796. htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  3797. /* Set the DMA error callback */
  3798. htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
  3799. /* Enable the DMA channel */
  3800. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)BurstBuffer,
  3801. (uint32_t)&htim->Instance->DMAR, DataLength) != HAL_OK)
  3802. {
  3803. /* Return error status */
  3804. return HAL_ERROR;
  3805. }
  3806. break;
  3807. }
  3808. case TIM_DMA_CC4:
  3809. {
  3810. /* Set the DMA compare callbacks */
  3811. htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMADelayPulseCplt;
  3812. htim->hdma[TIM_DMA_ID_CC4]->XferHalfCpltCallback = TIM_DMADelayPulseHalfCplt;
  3813. /* Set the DMA error callback */
  3814. htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
  3815. /* Enable the DMA channel */
  3816. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)BurstBuffer,
  3817. (uint32_t)&htim->Instance->DMAR, DataLength) != HAL_OK)
  3818. {
  3819. /* Return error status */
  3820. return HAL_ERROR;
  3821. }
  3822. break;
  3823. }
  3824. case TIM_DMA_TRIGGER:
  3825. {
  3826. /* Set the DMA trigger callbacks */
  3827. htim->hdma[TIM_DMA_ID_TRIGGER]->XferCpltCallback = TIM_DMATriggerCplt;
  3828. htim->hdma[TIM_DMA_ID_TRIGGER]->XferHalfCpltCallback = TIM_DMATriggerHalfCplt;
  3829. /* Set the DMA error callback */
  3830. htim->hdma[TIM_DMA_ID_TRIGGER]->XferErrorCallback = TIM_DMAError ;
  3831. /* Enable the DMA channel */
  3832. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_TRIGGER], (uint32_t)BurstBuffer,
  3833. (uint32_t)&htim->Instance->DMAR, DataLength) != HAL_OK)
  3834. {
  3835. /* Return error status */
  3836. return HAL_ERROR;
  3837. }
  3838. break;
  3839. }
  3840. default:
  3841. status = HAL_ERROR;
  3842. break;
  3843. }
  3844. if (status == HAL_OK)
  3845. {
  3846. /* Configure the DMA Burst Mode */
  3847. htim->Instance->DCR = (BurstBaseAddress | BurstLength);
  3848. /* Enable the TIM DMA Request */
  3849. __HAL_TIM_ENABLE_DMA(htim, BurstRequestSrc);
  3850. }
  3851. /* Return function status */
  3852. return status;
  3853. }
  3854. /**
  3855. * @brief Stops the TIM DMA Burst mode
  3856. * @param htim TIM handle
  3857. * @param BurstRequestSrc TIM DMA Request sources to disable
  3858. * @retval HAL status
  3859. */
  3860. HAL_StatusTypeDef HAL_TIM_DMABurst_WriteStop(TIM_HandleTypeDef *htim, uint32_t BurstRequestSrc)
  3861. {
  3862. HAL_StatusTypeDef status = HAL_OK;
  3863. /* Check the parameters */
  3864. assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc));
  3865. /* Abort the DMA transfer (at least disable the DMA channel) */
  3866. switch (BurstRequestSrc)
  3867. {
  3868. case TIM_DMA_UPDATE:
  3869. {
  3870. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_UPDATE]);
  3871. break;
  3872. }
  3873. case TIM_DMA_CC1:
  3874. {
  3875. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
  3876. break;
  3877. }
  3878. case TIM_DMA_CC2:
  3879. {
  3880. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
  3881. break;
  3882. }
  3883. case TIM_DMA_CC3:
  3884. {
  3885. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
  3886. break;
  3887. }
  3888. case TIM_DMA_CC4:
  3889. {
  3890. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC4]);
  3891. break;
  3892. }
  3893. case TIM_DMA_TRIGGER:
  3894. {
  3895. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_TRIGGER]);
  3896. break;
  3897. }
  3898. default:
  3899. status = HAL_ERROR;
  3900. break;
  3901. }
  3902. if (status == HAL_OK)
  3903. {
  3904. /* Disable the TIM Update DMA request */
  3905. __HAL_TIM_DISABLE_DMA(htim, BurstRequestSrc);
  3906. /* Change the DMA burst operation state */
  3907. htim->DMABurstState = HAL_DMA_BURST_STATE_READY;
  3908. }
  3909. /* Return function status */
  3910. return status;
  3911. }
  3912. /**
  3913. * @brief Configure the DMA Burst to transfer Data from the TIM peripheral to the memory
  3914. * @param htim TIM handle
  3915. * @param BurstBaseAddress TIM Base address from where the DMA will start the Data read
  3916. * This parameter can be one of the following values:
  3917. * @arg TIM_DMABASE_CR1
  3918. * @arg TIM_DMABASE_CR2
  3919. * @arg TIM_DMABASE_SMCR
  3920. * @arg TIM_DMABASE_DIER
  3921. * @arg TIM_DMABASE_SR
  3922. * @arg TIM_DMABASE_EGR
  3923. * @arg TIM_DMABASE_CCMR1
  3924. * @arg TIM_DMABASE_CCMR2
  3925. * @arg TIM_DMABASE_CCER
  3926. * @arg TIM_DMABASE_CNT
  3927. * @arg TIM_DMABASE_PSC
  3928. * @arg TIM_DMABASE_ARR
  3929. * @arg TIM_DMABASE_CCR1
  3930. * @arg TIM_DMABASE_CCR2
  3931. * @arg TIM_DMABASE_CCR3
  3932. * @arg TIM_DMABASE_CCR4
  3933. * @arg TIM_DMABASE_OR
  3934. * @param BurstRequestSrc TIM DMA Request sources
  3935. * This parameter can be one of the following values:
  3936. * @arg TIM_DMA_UPDATE: TIM update Interrupt source
  3937. * @arg TIM_DMA_CC1: TIM Capture Compare 1 DMA source
  3938. * @arg TIM_DMA_CC2: TIM Capture Compare 2 DMA source
  3939. * @arg TIM_DMA_CC3: TIM Capture Compare 3 DMA source
  3940. * @arg TIM_DMA_CC4: TIM Capture Compare 4 DMA source
  3941. * @arg TIM_DMA_TRIGGER: TIM Trigger DMA source
  3942. * @param BurstBuffer The Buffer address.
  3943. * @param BurstLength DMA Burst length. This parameter can be one value
  3944. * between: TIM_DMABURSTLENGTH_1TRANSFER and TIM_DMABURSTLENGTH_18TRANSFERS.
  3945. * @note This function should be used only when BurstLength is equal to DMA data transfer length.
  3946. * @retval HAL status
  3947. */
  3948. HAL_StatusTypeDef HAL_TIM_DMABurst_ReadStart(TIM_HandleTypeDef *htim, uint32_t BurstBaseAddress,
  3949. uint32_t BurstRequestSrc, uint32_t *BurstBuffer, uint32_t BurstLength)
  3950. {
  3951. HAL_StatusTypeDef status;
  3952. status = HAL_TIM_DMABurst_MultiReadStart(htim, BurstBaseAddress, BurstRequestSrc, BurstBuffer, BurstLength,
  3953. ((BurstLength) >> 8U) + 1U);
  3954. return status;
  3955. }
  3956. /**
  3957. * @brief Configure the DMA Burst to transfer Data from the TIM peripheral to the memory
  3958. * @param htim TIM handle
  3959. * @param BurstBaseAddress TIM Base address from where the DMA will start the Data read
  3960. * This parameter can be one of the following values:
  3961. * @arg TIM_DMABASE_CR1
  3962. * @arg TIM_DMABASE_CR2
  3963. * @arg TIM_DMABASE_SMCR
  3964. * @arg TIM_DMABASE_DIER
  3965. * @arg TIM_DMABASE_SR
  3966. * @arg TIM_DMABASE_EGR
  3967. * @arg TIM_DMABASE_CCMR1
  3968. * @arg TIM_DMABASE_CCMR2
  3969. * @arg TIM_DMABASE_CCER
  3970. * @arg TIM_DMABASE_CNT
  3971. * @arg TIM_DMABASE_PSC
  3972. * @arg TIM_DMABASE_ARR
  3973. * @arg TIM_DMABASE_CCR1
  3974. * @arg TIM_DMABASE_CCR2
  3975. * @arg TIM_DMABASE_CCR3
  3976. * @arg TIM_DMABASE_CCR4
  3977. * @arg TIM_DMABASE_OR
  3978. * @param BurstRequestSrc TIM DMA Request sources
  3979. * This parameter can be one of the following values:
  3980. * @arg TIM_DMA_UPDATE: TIM update Interrupt source
  3981. * @arg TIM_DMA_CC1: TIM Capture Compare 1 DMA source
  3982. * @arg TIM_DMA_CC2: TIM Capture Compare 2 DMA source
  3983. * @arg TIM_DMA_CC3: TIM Capture Compare 3 DMA source
  3984. * @arg TIM_DMA_CC4: TIM Capture Compare 4 DMA source
  3985. * @arg TIM_DMA_TRIGGER: TIM Trigger DMA source
  3986. * @param BurstBuffer The Buffer address.
  3987. * @param BurstLength DMA Burst length. This parameter can be one value
  3988. * between: TIM_DMABURSTLENGTH_1TRANSFER and TIM_DMABURSTLENGTH_18TRANSFERS.
  3989. * @param DataLength Data length. This parameter can be one value
  3990. * between 1 and 0xFFFF.
  3991. * @retval HAL status
  3992. */
  3993. HAL_StatusTypeDef HAL_TIM_DMABurst_MultiReadStart(TIM_HandleTypeDef *htim, uint32_t BurstBaseAddress,
  3994. uint32_t BurstRequestSrc, uint32_t *BurstBuffer,
  3995. uint32_t BurstLength, uint32_t DataLength)
  3996. {
  3997. HAL_StatusTypeDef status = HAL_OK;
  3998. /* Check the parameters */
  3999. assert_param(IS_TIM_DMABURST_INSTANCE(htim->Instance));
  4000. assert_param(IS_TIM_DMA_BASE(BurstBaseAddress));
  4001. assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc));
  4002. assert_param(IS_TIM_DMA_LENGTH(BurstLength));
  4003. assert_param(IS_TIM_DMA_DATA_LENGTH(DataLength));
  4004. if (htim->DMABurstState == HAL_DMA_BURST_STATE_BUSY)
  4005. {
  4006. return HAL_BUSY;
  4007. }
  4008. else if (htim->DMABurstState == HAL_DMA_BURST_STATE_READY)
  4009. {
  4010. if ((BurstBuffer == NULL) && (BurstLength > 0U))
  4011. {
  4012. return HAL_ERROR;
  4013. }
  4014. else
  4015. {
  4016. htim->DMABurstState = HAL_DMA_BURST_STATE_BUSY;
  4017. }
  4018. }
  4019. else
  4020. {
  4021. /* nothing to do */
  4022. }
  4023. switch (BurstRequestSrc)
  4024. {
  4025. case TIM_DMA_UPDATE:
  4026. {
  4027. /* Set the DMA Period elapsed callbacks */
  4028. htim->hdma[TIM_DMA_ID_UPDATE]->XferCpltCallback = TIM_DMAPeriodElapsedCplt;
  4029. htim->hdma[TIM_DMA_ID_UPDATE]->XferHalfCpltCallback = TIM_DMAPeriodElapsedHalfCplt;
  4030. /* Set the DMA error callback */
  4031. htim->hdma[TIM_DMA_ID_UPDATE]->XferErrorCallback = TIM_DMAError ;
  4032. /* Enable the DMA channel */
  4033. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_UPDATE], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer,
  4034. DataLength) != HAL_OK)
  4035. {
  4036. /* Return error status */
  4037. return HAL_ERROR;
  4038. }
  4039. break;
  4040. }
  4041. case TIM_DMA_CC1:
  4042. {
  4043. /* Set the DMA capture callbacks */
  4044. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMACaptureCplt;
  4045. htim->hdma[TIM_DMA_ID_CC1]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  4046. /* Set the DMA error callback */
  4047. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  4048. /* Enable the DMA channel */
  4049. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer,
  4050. DataLength) != HAL_OK)
  4051. {
  4052. /* Return error status */
  4053. return HAL_ERROR;
  4054. }
  4055. break;
  4056. }
  4057. case TIM_DMA_CC2:
  4058. {
  4059. /* Set the DMA capture callbacks */
  4060. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMACaptureCplt;
  4061. htim->hdma[TIM_DMA_ID_CC2]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  4062. /* Set the DMA error callback */
  4063. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
  4064. /* Enable the DMA channel */
  4065. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer,
  4066. DataLength) != HAL_OK)
  4067. {
  4068. /* Return error status */
  4069. return HAL_ERROR;
  4070. }
  4071. break;
  4072. }
  4073. case TIM_DMA_CC3:
  4074. {
  4075. /* Set the DMA capture callbacks */
  4076. htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMACaptureCplt;
  4077. htim->hdma[TIM_DMA_ID_CC3]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  4078. /* Set the DMA error callback */
  4079. htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
  4080. /* Enable the DMA channel */
  4081. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer,
  4082. DataLength) != HAL_OK)
  4083. {
  4084. /* Return error status */
  4085. return HAL_ERROR;
  4086. }
  4087. break;
  4088. }
  4089. case TIM_DMA_CC4:
  4090. {
  4091. /* Set the DMA capture callbacks */
  4092. htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMACaptureCplt;
  4093. htim->hdma[TIM_DMA_ID_CC4]->XferHalfCpltCallback = TIM_DMACaptureHalfCplt;
  4094. /* Set the DMA error callback */
  4095. htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
  4096. /* Enable the DMA channel */
  4097. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer,
  4098. DataLength) != HAL_OK)
  4099. {
  4100. /* Return error status */
  4101. return HAL_ERROR;
  4102. }
  4103. break;
  4104. }
  4105. case TIM_DMA_TRIGGER:
  4106. {
  4107. /* Set the DMA trigger callbacks */
  4108. htim->hdma[TIM_DMA_ID_TRIGGER]->XferCpltCallback = TIM_DMATriggerCplt;
  4109. htim->hdma[TIM_DMA_ID_TRIGGER]->XferHalfCpltCallback = TIM_DMATriggerHalfCplt;
  4110. /* Set the DMA error callback */
  4111. htim->hdma[TIM_DMA_ID_TRIGGER]->XferErrorCallback = TIM_DMAError ;
  4112. /* Enable the DMA channel */
  4113. if (HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_TRIGGER], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer,
  4114. DataLength) != HAL_OK)
  4115. {
  4116. /* Return error status */
  4117. return HAL_ERROR;
  4118. }
  4119. break;
  4120. }
  4121. default:
  4122. status = HAL_ERROR;
  4123. break;
  4124. }
  4125. if (status == HAL_OK)
  4126. {
  4127. /* Configure the DMA Burst Mode */
  4128. htim->Instance->DCR = (BurstBaseAddress | BurstLength);
  4129. /* Enable the TIM DMA Request */
  4130. __HAL_TIM_ENABLE_DMA(htim, BurstRequestSrc);
  4131. }
  4132. /* Return function status */
  4133. return status;
  4134. }
  4135. /**
  4136. * @brief Stop the DMA burst reading
  4137. * @param htim TIM handle
  4138. * @param BurstRequestSrc TIM DMA Request sources to disable.
  4139. * @retval HAL status
  4140. */
  4141. HAL_StatusTypeDef HAL_TIM_DMABurst_ReadStop(TIM_HandleTypeDef *htim, uint32_t BurstRequestSrc)
  4142. {
  4143. HAL_StatusTypeDef status = HAL_OK;
  4144. /* Check the parameters */
  4145. assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc));
  4146. /* Abort the DMA transfer (at least disable the DMA channel) */
  4147. switch (BurstRequestSrc)
  4148. {
  4149. case TIM_DMA_UPDATE:
  4150. {
  4151. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_UPDATE]);
  4152. break;
  4153. }
  4154. case TIM_DMA_CC1:
  4155. {
  4156. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC1]);
  4157. break;
  4158. }
  4159. case TIM_DMA_CC2:
  4160. {
  4161. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC2]);
  4162. break;
  4163. }
  4164. case TIM_DMA_CC3:
  4165. {
  4166. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC3]);
  4167. break;
  4168. }
  4169. case TIM_DMA_CC4:
  4170. {
  4171. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_CC4]);
  4172. break;
  4173. }
  4174. case TIM_DMA_TRIGGER:
  4175. {
  4176. (void)HAL_DMA_Abort_IT(htim->hdma[TIM_DMA_ID_TRIGGER]);
  4177. break;
  4178. }
  4179. default:
  4180. status = HAL_ERROR;
  4181. break;
  4182. }
  4183. if (status == HAL_OK)
  4184. {
  4185. /* Disable the TIM Update DMA request */
  4186. __HAL_TIM_DISABLE_DMA(htim, BurstRequestSrc);
  4187. /* Change the DMA burst operation state */
  4188. htim->DMABurstState = HAL_DMA_BURST_STATE_READY;
  4189. }
  4190. /* Return function status */
  4191. return status;
  4192. }
  4193. /**
  4194. * @brief Generate a software event
  4195. * @param htim TIM handle
  4196. * @param EventSource specifies the event source.
  4197. * This parameter can be one of the following values:
  4198. * @arg TIM_EVENTSOURCE_UPDATE: Timer update Event source
  4199. * @arg TIM_EVENTSOURCE_CC1: Timer Capture Compare 1 Event source
  4200. * @arg TIM_EVENTSOURCE_CC2: Timer Capture Compare 2 Event source
  4201. * @arg TIM_EVENTSOURCE_CC3: Timer Capture Compare 3 Event source
  4202. * @arg TIM_EVENTSOURCE_CC4: Timer Capture Compare 4 Event source
  4203. * @arg TIM_EVENTSOURCE_TRIGGER: Timer Trigger Event source
  4204. * @note Basic timers can only generate an update event.
  4205. * @retval HAL status
  4206. */
  4207. HAL_StatusTypeDef HAL_TIM_GenerateEvent(TIM_HandleTypeDef *htim, uint32_t EventSource)
  4208. {
  4209. /* Check the parameters */
  4210. assert_param(IS_TIM_INSTANCE(htim->Instance));
  4211. assert_param(IS_TIM_EVENT_SOURCE(EventSource));
  4212. /* Process Locked */
  4213. __HAL_LOCK(htim);
  4214. /* Change the TIM state */
  4215. htim->State = HAL_TIM_STATE_BUSY;
  4216. /* Set the event sources */
  4217. htim->Instance->EGR = EventSource;
  4218. /* Change the TIM state */
  4219. htim->State = HAL_TIM_STATE_READY;
  4220. __HAL_UNLOCK(htim);
  4221. /* Return function status */
  4222. return HAL_OK;
  4223. }
  4224. /**
  4225. * @brief Configures the OCRef clear feature
  4226. * @param htim TIM handle
  4227. * @param sClearInputConfig pointer to a TIM_ClearInputConfigTypeDef structure that
  4228. * contains the OCREF clear feature and parameters for the TIM peripheral.
  4229. * @param Channel specifies the TIM Channel
  4230. * This parameter can be one of the following values:
  4231. * @arg TIM_CHANNEL_1: TIM Channel 1
  4232. * @arg TIM_CHANNEL_2: TIM Channel 2
  4233. * @arg TIM_CHANNEL_3: TIM Channel 3
  4234. * @arg TIM_CHANNEL_4: TIM Channel 4
  4235. * @retval HAL status
  4236. */
  4237. HAL_StatusTypeDef HAL_TIM_ConfigOCrefClear(TIM_HandleTypeDef *htim,
  4238. const TIM_ClearInputConfigTypeDef *sClearInputConfig,
  4239. uint32_t Channel)
  4240. {
  4241. HAL_StatusTypeDef status = HAL_OK;
  4242. /* Check the parameters */
  4243. assert_param(IS_TIM_OCXREF_CLEAR_INSTANCE(htim->Instance));
  4244. assert_param(IS_TIM_CLEARINPUT_SOURCE(sClearInputConfig->ClearInputSource));
  4245. /* Process Locked */
  4246. __HAL_LOCK(htim);
  4247. htim->State = HAL_TIM_STATE_BUSY;
  4248. switch (sClearInputConfig->ClearInputSource)
  4249. {
  4250. case TIM_CLEARINPUTSOURCE_NONE:
  4251. {
  4252. /* Clear the OCREF clear selection bit and the the ETR Bits */
  4253. CLEAR_BIT(htim->Instance->SMCR, (TIM_SMCR_ETF | TIM_SMCR_ETPS | TIM_SMCR_ECE | TIM_SMCR_ETP));
  4254. break;
  4255. }
  4256. case TIM_CLEARINPUTSOURCE_ETR:
  4257. {
  4258. /* Check the parameters */
  4259. assert_param(IS_TIM_CLEARINPUT_POLARITY(sClearInputConfig->ClearInputPolarity));
  4260. assert_param(IS_TIM_CLEARINPUT_PRESCALER(sClearInputConfig->ClearInputPrescaler));
  4261. assert_param(IS_TIM_CLEARINPUT_FILTER(sClearInputConfig->ClearInputFilter));
  4262. /* When OCRef clear feature is used with ETR source, ETR prescaler must be off */
  4263. if (sClearInputConfig->ClearInputPrescaler != TIM_CLEARINPUTPRESCALER_DIV1)
  4264. {
  4265. htim->State = HAL_TIM_STATE_READY;
  4266. __HAL_UNLOCK(htim);
  4267. return HAL_ERROR;
  4268. }
  4269. TIM_ETR_SetConfig(htim->Instance,
  4270. sClearInputConfig->ClearInputPrescaler,
  4271. sClearInputConfig->ClearInputPolarity,
  4272. sClearInputConfig->ClearInputFilter);
  4273. break;
  4274. }
  4275. default:
  4276. status = HAL_ERROR;
  4277. break;
  4278. }
  4279. if (status == HAL_OK)
  4280. {
  4281. switch (Channel)
  4282. {
  4283. case TIM_CHANNEL_1:
  4284. {
  4285. if (sClearInputConfig->ClearInputState != (uint32_t)DISABLE)
  4286. {
  4287. /* Enable the OCREF clear feature for Channel 1 */
  4288. SET_BIT(htim->Instance->CCMR1, TIM_CCMR1_OC1CE);
  4289. }
  4290. else
  4291. {
  4292. /* Disable the OCREF clear feature for Channel 1 */
  4293. CLEAR_BIT(htim->Instance->CCMR1, TIM_CCMR1_OC1CE);
  4294. }
  4295. break;
  4296. }
  4297. case TIM_CHANNEL_2:
  4298. {
  4299. if (sClearInputConfig->ClearInputState != (uint32_t)DISABLE)
  4300. {
  4301. /* Enable the OCREF clear feature for Channel 2 */
  4302. SET_BIT(htim->Instance->CCMR1, TIM_CCMR1_OC2CE);
  4303. }
  4304. else
  4305. {
  4306. /* Disable the OCREF clear feature for Channel 2 */
  4307. CLEAR_BIT(htim->Instance->CCMR1, TIM_CCMR1_OC2CE);
  4308. }
  4309. break;
  4310. }
  4311. case TIM_CHANNEL_3:
  4312. {
  4313. if (sClearInputConfig->ClearInputState != (uint32_t)DISABLE)
  4314. {
  4315. /* Enable the OCREF clear feature for Channel 3 */
  4316. SET_BIT(htim->Instance->CCMR2, TIM_CCMR2_OC3CE);
  4317. }
  4318. else
  4319. {
  4320. /* Disable the OCREF clear feature for Channel 3 */
  4321. CLEAR_BIT(htim->Instance->CCMR2, TIM_CCMR2_OC3CE);
  4322. }
  4323. break;
  4324. }
  4325. case TIM_CHANNEL_4:
  4326. {
  4327. if (sClearInputConfig->ClearInputState != (uint32_t)DISABLE)
  4328. {
  4329. /* Enable the OCREF clear feature for Channel 4 */
  4330. SET_BIT(htim->Instance->CCMR2, TIM_CCMR2_OC4CE);
  4331. }
  4332. else
  4333. {
  4334. /* Disable the OCREF clear feature for Channel 4 */
  4335. CLEAR_BIT(htim->Instance->CCMR2, TIM_CCMR2_OC4CE);
  4336. }
  4337. break;
  4338. }
  4339. default:
  4340. break;
  4341. }
  4342. }
  4343. htim->State = HAL_TIM_STATE_READY;
  4344. __HAL_UNLOCK(htim);
  4345. return status;
  4346. }
  4347. /**
  4348. * @brief Configures the clock source to be used
  4349. * @param htim TIM handle
  4350. * @param sClockSourceConfig pointer to a TIM_ClockConfigTypeDef structure that
  4351. * contains the clock source information for the TIM peripheral.
  4352. * @retval HAL status
  4353. */
  4354. HAL_StatusTypeDef HAL_TIM_ConfigClockSource(TIM_HandleTypeDef *htim, const TIM_ClockConfigTypeDef *sClockSourceConfig)
  4355. {
  4356. HAL_StatusTypeDef status = HAL_OK;
  4357. uint32_t tmpsmcr;
  4358. /* Process Locked */
  4359. __HAL_LOCK(htim);
  4360. htim->State = HAL_TIM_STATE_BUSY;
  4361. /* Check the parameters */
  4362. assert_param(IS_TIM_CLOCKSOURCE(sClockSourceConfig->ClockSource));
  4363. /* Reset the SMS, TS, ECE, ETPS and ETRF bits */
  4364. tmpsmcr = htim->Instance->SMCR;
  4365. tmpsmcr &= ~(TIM_SMCR_SMS | TIM_SMCR_TS);
  4366. tmpsmcr &= ~(TIM_SMCR_ETF | TIM_SMCR_ETPS | TIM_SMCR_ECE | TIM_SMCR_ETP);
  4367. htim->Instance->SMCR = tmpsmcr;
  4368. switch (sClockSourceConfig->ClockSource)
  4369. {
  4370. case TIM_CLOCKSOURCE_INTERNAL:
  4371. {
  4372. assert_param(IS_TIM_INSTANCE(htim->Instance));
  4373. break;
  4374. }
  4375. case TIM_CLOCKSOURCE_ETRMODE1:
  4376. {
  4377. /* Check whether or not the timer instance supports external trigger input mode 1 (ETRF)*/
  4378. assert_param(IS_TIM_CLOCKSOURCE_ETRMODE1_INSTANCE(htim->Instance));
  4379. /* Check ETR input conditioning related parameters */
  4380. assert_param(IS_TIM_CLOCKPRESCALER(sClockSourceConfig->ClockPrescaler));
  4381. assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));
  4382. assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));
  4383. /* Configure the ETR Clock source */
  4384. TIM_ETR_SetConfig(htim->Instance,
  4385. sClockSourceConfig->ClockPrescaler,
  4386. sClockSourceConfig->ClockPolarity,
  4387. sClockSourceConfig->ClockFilter);
  4388. /* Select the External clock mode1 and the ETRF trigger */
  4389. tmpsmcr = htim->Instance->SMCR;
  4390. tmpsmcr |= (TIM_SLAVEMODE_EXTERNAL1 | TIM_CLOCKSOURCE_ETRMODE1);
  4391. /* Write to TIMx SMCR */
  4392. htim->Instance->SMCR = tmpsmcr;
  4393. break;
  4394. }
  4395. case TIM_CLOCKSOURCE_ETRMODE2:
  4396. {
  4397. /* Check whether or not the timer instance supports external trigger input mode 2 (ETRF)*/
  4398. assert_param(IS_TIM_CLOCKSOURCE_ETRMODE2_INSTANCE(htim->Instance));
  4399. /* Check ETR input conditioning related parameters */
  4400. assert_param(IS_TIM_CLOCKPRESCALER(sClockSourceConfig->ClockPrescaler));
  4401. assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));
  4402. assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));
  4403. /* Configure the ETR Clock source */
  4404. TIM_ETR_SetConfig(htim->Instance,
  4405. sClockSourceConfig->ClockPrescaler,
  4406. sClockSourceConfig->ClockPolarity,
  4407. sClockSourceConfig->ClockFilter);
  4408. /* Enable the External clock mode2 */
  4409. htim->Instance->SMCR |= TIM_SMCR_ECE;
  4410. break;
  4411. }
  4412. case TIM_CLOCKSOURCE_TI1:
  4413. {
  4414. /* Check whether or not the timer instance supports external clock mode 1 */
  4415. assert_param(IS_TIM_CLOCKSOURCE_TIX_INSTANCE(htim->Instance));
  4416. /* Check TI1 input conditioning related parameters */
  4417. assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));
  4418. assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));
  4419. TIM_TI1_ConfigInputStage(htim->Instance,
  4420. sClockSourceConfig->ClockPolarity,
  4421. sClockSourceConfig->ClockFilter);
  4422. TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_TI1);
  4423. break;
  4424. }
  4425. case TIM_CLOCKSOURCE_TI2:
  4426. {
  4427. /* Check whether or not the timer instance supports external clock mode 1 (ETRF)*/
  4428. assert_param(IS_TIM_CLOCKSOURCE_TIX_INSTANCE(htim->Instance));
  4429. /* Check TI2 input conditioning related parameters */
  4430. assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));
  4431. assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));
  4432. TIM_TI2_ConfigInputStage(htim->Instance,
  4433. sClockSourceConfig->ClockPolarity,
  4434. sClockSourceConfig->ClockFilter);
  4435. TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_TI2);
  4436. break;
  4437. }
  4438. case TIM_CLOCKSOURCE_TI1ED:
  4439. {
  4440. /* Check whether or not the timer instance supports external clock mode 1 */
  4441. assert_param(IS_TIM_CLOCKSOURCE_TIX_INSTANCE(htim->Instance));
  4442. /* Check TI1 input conditioning related parameters */
  4443. assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));
  4444. assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));
  4445. TIM_TI1_ConfigInputStage(htim->Instance,
  4446. sClockSourceConfig->ClockPolarity,
  4447. sClockSourceConfig->ClockFilter);
  4448. TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_TI1ED);
  4449. break;
  4450. }
  4451. case TIM_CLOCKSOURCE_ITR0:
  4452. case TIM_CLOCKSOURCE_ITR1:
  4453. case TIM_CLOCKSOURCE_ITR2:
  4454. case TIM_CLOCKSOURCE_ITR3:
  4455. {
  4456. /* Check whether or not the timer instance supports internal trigger input */
  4457. assert_param(IS_TIM_CLOCKSOURCE_ITRX_INSTANCE(htim->Instance));
  4458. TIM_ITRx_SetConfig(htim->Instance, sClockSourceConfig->ClockSource);
  4459. break;
  4460. }
  4461. default:
  4462. status = HAL_ERROR;
  4463. break;
  4464. }
  4465. htim->State = HAL_TIM_STATE_READY;
  4466. __HAL_UNLOCK(htim);
  4467. return status;
  4468. }
  4469. /**
  4470. * @brief Selects the signal connected to the TI1 input: direct from CH1_input
  4471. * or a XOR combination between CH1_input, CH2_input & CH3_input
  4472. * @param htim TIM handle.
  4473. * @param TI1_Selection Indicate whether or not channel 1 is connected to the
  4474. * output of a XOR gate.
  4475. * This parameter can be one of the following values:
  4476. * @arg TIM_TI1SELECTION_CH1: The TIMx_CH1 pin is connected to TI1 input
  4477. * @arg TIM_TI1SELECTION_XORCOMBINATION: The TIMx_CH1, CH2 and CH3
  4478. * pins are connected to the TI1 input (XOR combination)
  4479. * @retval HAL status
  4480. */
  4481. HAL_StatusTypeDef HAL_TIM_ConfigTI1Input(TIM_HandleTypeDef *htim, uint32_t TI1_Selection)
  4482. {
  4483. uint32_t tmpcr2;
  4484. /* Check the parameters */
  4485. assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
  4486. assert_param(IS_TIM_TI1SELECTION(TI1_Selection));
  4487. /* Get the TIMx CR2 register value */
  4488. tmpcr2 = htim->Instance->CR2;
  4489. /* Reset the TI1 selection */
  4490. tmpcr2 &= ~TIM_CR2_TI1S;
  4491. /* Set the TI1 selection */
  4492. tmpcr2 |= TI1_Selection;
  4493. /* Write to TIMxCR2 */
  4494. htim->Instance->CR2 = tmpcr2;
  4495. return HAL_OK;
  4496. }
  4497. /**
  4498. * @brief Configures the TIM in Slave mode
  4499. * @param htim TIM handle.
  4500. * @param sSlaveConfig pointer to a TIM_SlaveConfigTypeDef structure that
  4501. * contains the selected trigger (internal trigger input, filtered
  4502. * timer input or external trigger input) and the Slave mode
  4503. * (Disable, Reset, Gated, Trigger, External clock mode 1).
  4504. * @retval HAL status
  4505. */
  4506. HAL_StatusTypeDef HAL_TIM_SlaveConfigSynchro(TIM_HandleTypeDef *htim, const TIM_SlaveConfigTypeDef *sSlaveConfig)
  4507. {
  4508. /* Check the parameters */
  4509. assert_param(IS_TIM_SLAVE_INSTANCE(htim->Instance));
  4510. assert_param(IS_TIM_SLAVE_MODE(sSlaveConfig->SlaveMode));
  4511. assert_param(IS_TIM_TRIGGER_SELECTION(sSlaveConfig->InputTrigger));
  4512. __HAL_LOCK(htim);
  4513. htim->State = HAL_TIM_STATE_BUSY;
  4514. if (TIM_SlaveTimer_SetConfig(htim, sSlaveConfig) != HAL_OK)
  4515. {
  4516. htim->State = HAL_TIM_STATE_READY;
  4517. __HAL_UNLOCK(htim);
  4518. return HAL_ERROR;
  4519. }
  4520. /* Disable Trigger Interrupt */
  4521. __HAL_TIM_DISABLE_IT(htim, TIM_IT_TRIGGER);
  4522. /* Disable Trigger DMA request */
  4523. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_TRIGGER);
  4524. htim->State = HAL_TIM_STATE_READY;
  4525. __HAL_UNLOCK(htim);
  4526. return HAL_OK;
  4527. }
  4528. /**
  4529. * @brief Configures the TIM in Slave mode in interrupt mode
  4530. * @param htim TIM handle.
  4531. * @param sSlaveConfig pointer to a TIM_SlaveConfigTypeDef structure that
  4532. * contains the selected trigger (internal trigger input, filtered
  4533. * timer input or external trigger input) and the Slave mode
  4534. * (Disable, Reset, Gated, Trigger, External clock mode 1).
  4535. * @retval HAL status
  4536. */
  4537. HAL_StatusTypeDef HAL_TIM_SlaveConfigSynchro_IT(TIM_HandleTypeDef *htim,
  4538. const TIM_SlaveConfigTypeDef *sSlaveConfig)
  4539. {
  4540. /* Check the parameters */
  4541. assert_param(IS_TIM_SLAVE_INSTANCE(htim->Instance));
  4542. assert_param(IS_TIM_SLAVE_MODE(sSlaveConfig->SlaveMode));
  4543. assert_param(IS_TIM_TRIGGER_SELECTION(sSlaveConfig->InputTrigger));
  4544. __HAL_LOCK(htim);
  4545. htim->State = HAL_TIM_STATE_BUSY;
  4546. if (TIM_SlaveTimer_SetConfig(htim, sSlaveConfig) != HAL_OK)
  4547. {
  4548. htim->State = HAL_TIM_STATE_READY;
  4549. __HAL_UNLOCK(htim);
  4550. return HAL_ERROR;
  4551. }
  4552. /* Enable Trigger Interrupt */
  4553. __HAL_TIM_ENABLE_IT(htim, TIM_IT_TRIGGER);
  4554. /* Disable Trigger DMA request */
  4555. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_TRIGGER);
  4556. htim->State = HAL_TIM_STATE_READY;
  4557. __HAL_UNLOCK(htim);
  4558. return HAL_OK;
  4559. }
  4560. /**
  4561. * @brief Read the captured value from Capture Compare unit
  4562. * @param htim TIM handle.
  4563. * @param Channel TIM Channels to be enabled
  4564. * This parameter can be one of the following values:
  4565. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  4566. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  4567. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  4568. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  4569. * @retval Captured value
  4570. */
  4571. uint32_t HAL_TIM_ReadCapturedValue(const TIM_HandleTypeDef *htim, uint32_t Channel)
  4572. {
  4573. uint32_t tmpreg = 0U;
  4574. switch (Channel)
  4575. {
  4576. case TIM_CHANNEL_1:
  4577. {
  4578. /* Check the parameters */
  4579. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  4580. /* Return the capture 1 value */
  4581. tmpreg = htim->Instance->CCR1;
  4582. break;
  4583. }
  4584. case TIM_CHANNEL_2:
  4585. {
  4586. /* Check the parameters */
  4587. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  4588. /* Return the capture 2 value */
  4589. tmpreg = htim->Instance->CCR2;
  4590. break;
  4591. }
  4592. case TIM_CHANNEL_3:
  4593. {
  4594. /* Check the parameters */
  4595. assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
  4596. /* Return the capture 3 value */
  4597. tmpreg = htim->Instance->CCR3;
  4598. break;
  4599. }
  4600. case TIM_CHANNEL_4:
  4601. {
  4602. /* Check the parameters */
  4603. assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
  4604. /* Return the capture 4 value */
  4605. tmpreg = htim->Instance->CCR4;
  4606. break;
  4607. }
  4608. default:
  4609. break;
  4610. }
  4611. return tmpreg;
  4612. }
  4613. /**
  4614. * @}
  4615. */
  4616. /** @defgroup TIM_Exported_Functions_Group9 TIM Callbacks functions
  4617. * @brief TIM Callbacks functions
  4618. *
  4619. @verbatim
  4620. ==============================================================================
  4621. ##### TIM Callbacks functions #####
  4622. ==============================================================================
  4623. [..]
  4624. This section provides TIM callback functions:
  4625. (+) TIM Period elapsed callback
  4626. (+) TIM Output Compare callback
  4627. (+) TIM Input capture callback
  4628. (+) TIM Trigger callback
  4629. (+) TIM Error callback
  4630. @endverbatim
  4631. * @{
  4632. */
  4633. /**
  4634. * @brief Period elapsed callback in non-blocking mode
  4635. * @param htim TIM handle
  4636. * @retval None
  4637. */
  4638. __weak void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  4639. {
  4640. /* Prevent unused argument(s) compilation warning */
  4641. UNUSED(htim);
  4642. /* NOTE : This function should not be modified, when the callback is needed,
  4643. the HAL_TIM_PeriodElapsedCallback could be implemented in the user file
  4644. */
  4645. }
  4646. /**
  4647. * @brief Period elapsed half complete callback in non-blocking mode
  4648. * @param htim TIM handle
  4649. * @retval None
  4650. */
  4651. __weak void HAL_TIM_PeriodElapsedHalfCpltCallback(TIM_HandleTypeDef *htim)
  4652. {
  4653. /* Prevent unused argument(s) compilation warning */
  4654. UNUSED(htim);
  4655. /* NOTE : This function should not be modified, when the callback is needed,
  4656. the HAL_TIM_PeriodElapsedHalfCpltCallback could be implemented in the user file
  4657. */
  4658. }
  4659. /**
  4660. * @brief Output Compare callback in non-blocking mode
  4661. * @param htim TIM OC handle
  4662. * @retval None
  4663. */
  4664. __weak void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)
  4665. {
  4666. /* Prevent unused argument(s) compilation warning */
  4667. UNUSED(htim);
  4668. /* NOTE : This function should not be modified, when the callback is needed,
  4669. the HAL_TIM_OC_DelayElapsedCallback could be implemented in the user file
  4670. */
  4671. }
  4672. /**
  4673. * @brief Input Capture callback in non-blocking mode
  4674. * @param htim TIM IC handle
  4675. * @retval None
  4676. */
  4677. __weak void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
  4678. {
  4679. /* Prevent unused argument(s) compilation warning */
  4680. UNUSED(htim);
  4681. /* NOTE : This function should not be modified, when the callback is needed,
  4682. the HAL_TIM_IC_CaptureCallback could be implemented in the user file
  4683. */
  4684. }
  4685. /**
  4686. * @brief Input Capture half complete callback in non-blocking mode
  4687. * @param htim TIM IC handle
  4688. * @retval None
  4689. */
  4690. __weak void HAL_TIM_IC_CaptureHalfCpltCallback(TIM_HandleTypeDef *htim)
  4691. {
  4692. /* Prevent unused argument(s) compilation warning */
  4693. UNUSED(htim);
  4694. /* NOTE : This function should not be modified, when the callback is needed,
  4695. the HAL_TIM_IC_CaptureHalfCpltCallback could be implemented in the user file
  4696. */
  4697. }
  4698. /**
  4699. * @brief PWM Pulse finished callback in non-blocking mode
  4700. * @param htim TIM handle
  4701. * @retval None
  4702. */
  4703. __weak void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
  4704. {
  4705. /* Prevent unused argument(s) compilation warning */
  4706. UNUSED(htim);
  4707. /* NOTE : This function should not be modified, when the callback is needed,
  4708. the HAL_TIM_PWM_PulseFinishedCallback could be implemented in the user file
  4709. */
  4710. }
  4711. /**
  4712. * @brief PWM Pulse finished half complete callback in non-blocking mode
  4713. * @param htim TIM handle
  4714. * @retval None
  4715. */
  4716. __weak void HAL_TIM_PWM_PulseFinishedHalfCpltCallback(TIM_HandleTypeDef *htim)
  4717. {
  4718. /* Prevent unused argument(s) compilation warning */
  4719. UNUSED(htim);
  4720. /* NOTE : This function should not be modified, when the callback is needed,
  4721. the HAL_TIM_PWM_PulseFinishedHalfCpltCallback could be implemented in the user file
  4722. */
  4723. }
  4724. /**
  4725. * @brief Hall Trigger detection callback in non-blocking mode
  4726. * @param htim TIM handle
  4727. * @retval None
  4728. */
  4729. __weak void HAL_TIM_TriggerCallback(TIM_HandleTypeDef *htim)
  4730. {
  4731. /* Prevent unused argument(s) compilation warning */
  4732. UNUSED(htim);
  4733. /* NOTE : This function should not be modified, when the callback is needed,
  4734. the HAL_TIM_TriggerCallback could be implemented in the user file
  4735. */
  4736. }
  4737. /**
  4738. * @brief Hall Trigger detection half complete callback in non-blocking mode
  4739. * @param htim TIM handle
  4740. * @retval None
  4741. */
  4742. __weak void HAL_TIM_TriggerHalfCpltCallback(TIM_HandleTypeDef *htim)
  4743. {
  4744. /* Prevent unused argument(s) compilation warning */
  4745. UNUSED(htim);
  4746. /* NOTE : This function should not be modified, when the callback is needed,
  4747. the HAL_TIM_TriggerHalfCpltCallback could be implemented in the user file
  4748. */
  4749. }
  4750. /**
  4751. * @brief Timer error callback in non-blocking mode
  4752. * @param htim TIM handle
  4753. * @retval None
  4754. */
  4755. __weak void HAL_TIM_ErrorCallback(TIM_HandleTypeDef *htim)
  4756. {
  4757. /* Prevent unused argument(s) compilation warning */
  4758. UNUSED(htim);
  4759. /* NOTE : This function should not be modified, when the callback is needed,
  4760. the HAL_TIM_ErrorCallback could be implemented in the user file
  4761. */
  4762. }
  4763. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  4764. /**
  4765. * @brief Register a User TIM callback to be used instead of the weak predefined callback
  4766. * @param htim tim handle
  4767. * @param CallbackID ID of the callback to be registered
  4768. * This parameter can be one of the following values:
  4769. * @arg @ref HAL_TIM_BASE_MSPINIT_CB_ID Base MspInit Callback ID
  4770. * @arg @ref HAL_TIM_BASE_MSPDEINIT_CB_ID Base MspDeInit Callback ID
  4771. * @arg @ref HAL_TIM_IC_MSPINIT_CB_ID IC MspInit Callback ID
  4772. * @arg @ref HAL_TIM_IC_MSPDEINIT_CB_ID IC MspDeInit Callback ID
  4773. * @arg @ref HAL_TIM_OC_MSPINIT_CB_ID OC MspInit Callback ID
  4774. * @arg @ref HAL_TIM_OC_MSPDEINIT_CB_ID OC MspDeInit Callback ID
  4775. * @arg @ref HAL_TIM_PWM_MSPINIT_CB_ID PWM MspInit Callback ID
  4776. * @arg @ref HAL_TIM_PWM_MSPDEINIT_CB_ID PWM MspDeInit Callback ID
  4777. * @arg @ref HAL_TIM_ONE_PULSE_MSPINIT_CB_ID One Pulse MspInit Callback ID
  4778. * @arg @ref HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID One Pulse MspDeInit Callback ID
  4779. * @arg @ref HAL_TIM_ENCODER_MSPINIT_CB_ID Encoder MspInit Callback ID
  4780. * @arg @ref HAL_TIM_ENCODER_MSPDEINIT_CB_ID Encoder MspDeInit Callback ID
  4781. * @arg @ref HAL_TIM_PERIOD_ELAPSED_CB_ID Period Elapsed Callback ID
  4782. * @arg @ref HAL_TIM_PERIOD_ELAPSED_HALF_CB_ID Period Elapsed half complete Callback ID
  4783. * @arg @ref HAL_TIM_TRIGGER_CB_ID Trigger Callback ID
  4784. * @arg @ref HAL_TIM_TRIGGER_HALF_CB_ID Trigger half complete Callback ID
  4785. * @arg @ref HAL_TIM_IC_CAPTURE_CB_ID Input Capture Callback ID
  4786. * @arg @ref HAL_TIM_IC_CAPTURE_HALF_CB_ID Input Capture half complete Callback ID
  4787. * @arg @ref HAL_TIM_OC_DELAY_ELAPSED_CB_ID Output Compare Delay Elapsed Callback ID
  4788. * @arg @ref HAL_TIM_PWM_PULSE_FINISHED_CB_ID PWM Pulse Finished Callback ID
  4789. * @arg @ref HAL_TIM_PWM_PULSE_FINISHED_HALF_CB_ID PWM Pulse Finished half complete Callback ID
  4790. * @arg @ref HAL_TIM_ERROR_CB_ID Error Callback ID
  4791. * @param pCallback pointer to the callback function
  4792. * @retval status
  4793. */
  4794. HAL_StatusTypeDef HAL_TIM_RegisterCallback(TIM_HandleTypeDef *htim, HAL_TIM_CallbackIDTypeDef CallbackID,
  4795. pTIM_CallbackTypeDef pCallback)
  4796. {
  4797. HAL_StatusTypeDef status = HAL_OK;
  4798. if (pCallback == NULL)
  4799. {
  4800. return HAL_ERROR;
  4801. }
  4802. if (htim->State == HAL_TIM_STATE_READY)
  4803. {
  4804. switch (CallbackID)
  4805. {
  4806. case HAL_TIM_BASE_MSPINIT_CB_ID :
  4807. htim->Base_MspInitCallback = pCallback;
  4808. break;
  4809. case HAL_TIM_BASE_MSPDEINIT_CB_ID :
  4810. htim->Base_MspDeInitCallback = pCallback;
  4811. break;
  4812. case HAL_TIM_IC_MSPINIT_CB_ID :
  4813. htim->IC_MspInitCallback = pCallback;
  4814. break;
  4815. case HAL_TIM_IC_MSPDEINIT_CB_ID :
  4816. htim->IC_MspDeInitCallback = pCallback;
  4817. break;
  4818. case HAL_TIM_OC_MSPINIT_CB_ID :
  4819. htim->OC_MspInitCallback = pCallback;
  4820. break;
  4821. case HAL_TIM_OC_MSPDEINIT_CB_ID :
  4822. htim->OC_MspDeInitCallback = pCallback;
  4823. break;
  4824. case HAL_TIM_PWM_MSPINIT_CB_ID :
  4825. htim->PWM_MspInitCallback = pCallback;
  4826. break;
  4827. case HAL_TIM_PWM_MSPDEINIT_CB_ID :
  4828. htim->PWM_MspDeInitCallback = pCallback;
  4829. break;
  4830. case HAL_TIM_ONE_PULSE_MSPINIT_CB_ID :
  4831. htim->OnePulse_MspInitCallback = pCallback;
  4832. break;
  4833. case HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID :
  4834. htim->OnePulse_MspDeInitCallback = pCallback;
  4835. break;
  4836. case HAL_TIM_ENCODER_MSPINIT_CB_ID :
  4837. htim->Encoder_MspInitCallback = pCallback;
  4838. break;
  4839. case HAL_TIM_ENCODER_MSPDEINIT_CB_ID :
  4840. htim->Encoder_MspDeInitCallback = pCallback;
  4841. break;
  4842. case HAL_TIM_PERIOD_ELAPSED_CB_ID :
  4843. htim->PeriodElapsedCallback = pCallback;
  4844. break;
  4845. case HAL_TIM_PERIOD_ELAPSED_HALF_CB_ID :
  4846. htim->PeriodElapsedHalfCpltCallback = pCallback;
  4847. break;
  4848. case HAL_TIM_TRIGGER_CB_ID :
  4849. htim->TriggerCallback = pCallback;
  4850. break;
  4851. case HAL_TIM_TRIGGER_HALF_CB_ID :
  4852. htim->TriggerHalfCpltCallback = pCallback;
  4853. break;
  4854. case HAL_TIM_IC_CAPTURE_CB_ID :
  4855. htim->IC_CaptureCallback = pCallback;
  4856. break;
  4857. case HAL_TIM_IC_CAPTURE_HALF_CB_ID :
  4858. htim->IC_CaptureHalfCpltCallback = pCallback;
  4859. break;
  4860. case HAL_TIM_OC_DELAY_ELAPSED_CB_ID :
  4861. htim->OC_DelayElapsedCallback = pCallback;
  4862. break;
  4863. case HAL_TIM_PWM_PULSE_FINISHED_CB_ID :
  4864. htim->PWM_PulseFinishedCallback = pCallback;
  4865. break;
  4866. case HAL_TIM_PWM_PULSE_FINISHED_HALF_CB_ID :
  4867. htim->PWM_PulseFinishedHalfCpltCallback = pCallback;
  4868. break;
  4869. case HAL_TIM_ERROR_CB_ID :
  4870. htim->ErrorCallback = pCallback;
  4871. break;
  4872. default :
  4873. /* Return error status */
  4874. status = HAL_ERROR;
  4875. break;
  4876. }
  4877. }
  4878. else if (htim->State == HAL_TIM_STATE_RESET)
  4879. {
  4880. switch (CallbackID)
  4881. {
  4882. case HAL_TIM_BASE_MSPINIT_CB_ID :
  4883. htim->Base_MspInitCallback = pCallback;
  4884. break;
  4885. case HAL_TIM_BASE_MSPDEINIT_CB_ID :
  4886. htim->Base_MspDeInitCallback = pCallback;
  4887. break;
  4888. case HAL_TIM_IC_MSPINIT_CB_ID :
  4889. htim->IC_MspInitCallback = pCallback;
  4890. break;
  4891. case HAL_TIM_IC_MSPDEINIT_CB_ID :
  4892. htim->IC_MspDeInitCallback = pCallback;
  4893. break;
  4894. case HAL_TIM_OC_MSPINIT_CB_ID :
  4895. htim->OC_MspInitCallback = pCallback;
  4896. break;
  4897. case HAL_TIM_OC_MSPDEINIT_CB_ID :
  4898. htim->OC_MspDeInitCallback = pCallback;
  4899. break;
  4900. case HAL_TIM_PWM_MSPINIT_CB_ID :
  4901. htim->PWM_MspInitCallback = pCallback;
  4902. break;
  4903. case HAL_TIM_PWM_MSPDEINIT_CB_ID :
  4904. htim->PWM_MspDeInitCallback = pCallback;
  4905. break;
  4906. case HAL_TIM_ONE_PULSE_MSPINIT_CB_ID :
  4907. htim->OnePulse_MspInitCallback = pCallback;
  4908. break;
  4909. case HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID :
  4910. htim->OnePulse_MspDeInitCallback = pCallback;
  4911. break;
  4912. case HAL_TIM_ENCODER_MSPINIT_CB_ID :
  4913. htim->Encoder_MspInitCallback = pCallback;
  4914. break;
  4915. case HAL_TIM_ENCODER_MSPDEINIT_CB_ID :
  4916. htim->Encoder_MspDeInitCallback = pCallback;
  4917. break;
  4918. default :
  4919. /* Return error status */
  4920. status = HAL_ERROR;
  4921. break;
  4922. }
  4923. }
  4924. else
  4925. {
  4926. /* Return error status */
  4927. status = HAL_ERROR;
  4928. }
  4929. return status;
  4930. }
  4931. /**
  4932. * @brief Unregister a TIM callback
  4933. * TIM callback is redirected to the weak predefined callback
  4934. * @param htim tim handle
  4935. * @param CallbackID ID of the callback to be unregistered
  4936. * This parameter can be one of the following values:
  4937. * @arg @ref HAL_TIM_BASE_MSPINIT_CB_ID Base MspInit Callback ID
  4938. * @arg @ref HAL_TIM_BASE_MSPDEINIT_CB_ID Base MspDeInit Callback ID
  4939. * @arg @ref HAL_TIM_IC_MSPINIT_CB_ID IC MspInit Callback ID
  4940. * @arg @ref HAL_TIM_IC_MSPDEINIT_CB_ID IC MspDeInit Callback ID
  4941. * @arg @ref HAL_TIM_OC_MSPINIT_CB_ID OC MspInit Callback ID
  4942. * @arg @ref HAL_TIM_OC_MSPDEINIT_CB_ID OC MspDeInit Callback ID
  4943. * @arg @ref HAL_TIM_PWM_MSPINIT_CB_ID PWM MspInit Callback ID
  4944. * @arg @ref HAL_TIM_PWM_MSPDEINIT_CB_ID PWM MspDeInit Callback ID
  4945. * @arg @ref HAL_TIM_ONE_PULSE_MSPINIT_CB_ID One Pulse MspInit Callback ID
  4946. * @arg @ref HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID One Pulse MspDeInit Callback ID
  4947. * @arg @ref HAL_TIM_ENCODER_MSPINIT_CB_ID Encoder MspInit Callback ID
  4948. * @arg @ref HAL_TIM_ENCODER_MSPDEINIT_CB_ID Encoder MspDeInit Callback ID
  4949. * @arg @ref HAL_TIM_PERIOD_ELAPSED_CB_ID Period Elapsed Callback ID
  4950. * @arg @ref HAL_TIM_PERIOD_ELAPSED_HALF_CB_ID Period Elapsed half complete Callback ID
  4951. * @arg @ref HAL_TIM_TRIGGER_CB_ID Trigger Callback ID
  4952. * @arg @ref HAL_TIM_TRIGGER_HALF_CB_ID Trigger half complete Callback ID
  4953. * @arg @ref HAL_TIM_IC_CAPTURE_CB_ID Input Capture Callback ID
  4954. * @arg @ref HAL_TIM_IC_CAPTURE_HALF_CB_ID Input Capture half complete Callback ID
  4955. * @arg @ref HAL_TIM_OC_DELAY_ELAPSED_CB_ID Output Compare Delay Elapsed Callback ID
  4956. * @arg @ref HAL_TIM_PWM_PULSE_FINISHED_CB_ID PWM Pulse Finished Callback ID
  4957. * @arg @ref HAL_TIM_PWM_PULSE_FINISHED_HALF_CB_ID PWM Pulse Finished half complete Callback ID
  4958. * @arg @ref HAL_TIM_ERROR_CB_ID Error Callback ID
  4959. * @retval status
  4960. */
  4961. HAL_StatusTypeDef HAL_TIM_UnRegisterCallback(TIM_HandleTypeDef *htim, HAL_TIM_CallbackIDTypeDef CallbackID)
  4962. {
  4963. HAL_StatusTypeDef status = HAL_OK;
  4964. if (htim->State == HAL_TIM_STATE_READY)
  4965. {
  4966. switch (CallbackID)
  4967. {
  4968. case HAL_TIM_BASE_MSPINIT_CB_ID :
  4969. /* Legacy weak Base MspInit Callback */
  4970. htim->Base_MspInitCallback = HAL_TIM_Base_MspInit;
  4971. break;
  4972. case HAL_TIM_BASE_MSPDEINIT_CB_ID :
  4973. /* Legacy weak Base Msp DeInit Callback */
  4974. htim->Base_MspDeInitCallback = HAL_TIM_Base_MspDeInit;
  4975. break;
  4976. case HAL_TIM_IC_MSPINIT_CB_ID :
  4977. /* Legacy weak IC Msp Init Callback */
  4978. htim->IC_MspInitCallback = HAL_TIM_IC_MspInit;
  4979. break;
  4980. case HAL_TIM_IC_MSPDEINIT_CB_ID :
  4981. /* Legacy weak IC Msp DeInit Callback */
  4982. htim->IC_MspDeInitCallback = HAL_TIM_IC_MspDeInit;
  4983. break;
  4984. case HAL_TIM_OC_MSPINIT_CB_ID :
  4985. /* Legacy weak OC Msp Init Callback */
  4986. htim->OC_MspInitCallback = HAL_TIM_OC_MspInit;
  4987. break;
  4988. case HAL_TIM_OC_MSPDEINIT_CB_ID :
  4989. /* Legacy weak OC Msp DeInit Callback */
  4990. htim->OC_MspDeInitCallback = HAL_TIM_OC_MspDeInit;
  4991. break;
  4992. case HAL_TIM_PWM_MSPINIT_CB_ID :
  4993. /* Legacy weak PWM Msp Init Callback */
  4994. htim->PWM_MspInitCallback = HAL_TIM_PWM_MspInit;
  4995. break;
  4996. case HAL_TIM_PWM_MSPDEINIT_CB_ID :
  4997. /* Legacy weak PWM Msp DeInit Callback */
  4998. htim->PWM_MspDeInitCallback = HAL_TIM_PWM_MspDeInit;
  4999. break;
  5000. case HAL_TIM_ONE_PULSE_MSPINIT_CB_ID :
  5001. /* Legacy weak One Pulse Msp Init Callback */
  5002. htim->OnePulse_MspInitCallback = HAL_TIM_OnePulse_MspInit;
  5003. break;
  5004. case HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID :
  5005. /* Legacy weak One Pulse Msp DeInit Callback */
  5006. htim->OnePulse_MspDeInitCallback = HAL_TIM_OnePulse_MspDeInit;
  5007. break;
  5008. case HAL_TIM_ENCODER_MSPINIT_CB_ID :
  5009. /* Legacy weak Encoder Msp Init Callback */
  5010. htim->Encoder_MspInitCallback = HAL_TIM_Encoder_MspInit;
  5011. break;
  5012. case HAL_TIM_ENCODER_MSPDEINIT_CB_ID :
  5013. /* Legacy weak Encoder Msp DeInit Callback */
  5014. htim->Encoder_MspDeInitCallback = HAL_TIM_Encoder_MspDeInit;
  5015. break;
  5016. case HAL_TIM_PERIOD_ELAPSED_CB_ID :
  5017. /* Legacy weak Period Elapsed Callback */
  5018. htim->PeriodElapsedCallback = HAL_TIM_PeriodElapsedCallback;
  5019. break;
  5020. case HAL_TIM_PERIOD_ELAPSED_HALF_CB_ID :
  5021. /* Legacy weak Period Elapsed half complete Callback */
  5022. htim->PeriodElapsedHalfCpltCallback = HAL_TIM_PeriodElapsedHalfCpltCallback;
  5023. break;
  5024. case HAL_TIM_TRIGGER_CB_ID :
  5025. /* Legacy weak Trigger Callback */
  5026. htim->TriggerCallback = HAL_TIM_TriggerCallback;
  5027. break;
  5028. case HAL_TIM_TRIGGER_HALF_CB_ID :
  5029. /* Legacy weak Trigger half complete Callback */
  5030. htim->TriggerHalfCpltCallback = HAL_TIM_TriggerHalfCpltCallback;
  5031. break;
  5032. case HAL_TIM_IC_CAPTURE_CB_ID :
  5033. /* Legacy weak IC Capture Callback */
  5034. htim->IC_CaptureCallback = HAL_TIM_IC_CaptureCallback;
  5035. break;
  5036. case HAL_TIM_IC_CAPTURE_HALF_CB_ID :
  5037. /* Legacy weak IC Capture half complete Callback */
  5038. htim->IC_CaptureHalfCpltCallback = HAL_TIM_IC_CaptureHalfCpltCallback;
  5039. break;
  5040. case HAL_TIM_OC_DELAY_ELAPSED_CB_ID :
  5041. /* Legacy weak OC Delay Elapsed Callback */
  5042. htim->OC_DelayElapsedCallback = HAL_TIM_OC_DelayElapsedCallback;
  5043. break;
  5044. case HAL_TIM_PWM_PULSE_FINISHED_CB_ID :
  5045. /* Legacy weak PWM Pulse Finished Callback */
  5046. htim->PWM_PulseFinishedCallback = HAL_TIM_PWM_PulseFinishedCallback;
  5047. break;
  5048. case HAL_TIM_PWM_PULSE_FINISHED_HALF_CB_ID :
  5049. /* Legacy weak PWM Pulse Finished half complete Callback */
  5050. htim->PWM_PulseFinishedHalfCpltCallback = HAL_TIM_PWM_PulseFinishedHalfCpltCallback;
  5051. break;
  5052. case HAL_TIM_ERROR_CB_ID :
  5053. /* Legacy weak Error Callback */
  5054. htim->ErrorCallback = HAL_TIM_ErrorCallback;
  5055. break;
  5056. default :
  5057. /* Return error status */
  5058. status = HAL_ERROR;
  5059. break;
  5060. }
  5061. }
  5062. else if (htim->State == HAL_TIM_STATE_RESET)
  5063. {
  5064. switch (CallbackID)
  5065. {
  5066. case HAL_TIM_BASE_MSPINIT_CB_ID :
  5067. /* Legacy weak Base MspInit Callback */
  5068. htim->Base_MspInitCallback = HAL_TIM_Base_MspInit;
  5069. break;
  5070. case HAL_TIM_BASE_MSPDEINIT_CB_ID :
  5071. /* Legacy weak Base Msp DeInit Callback */
  5072. htim->Base_MspDeInitCallback = HAL_TIM_Base_MspDeInit;
  5073. break;
  5074. case HAL_TIM_IC_MSPINIT_CB_ID :
  5075. /* Legacy weak IC Msp Init Callback */
  5076. htim->IC_MspInitCallback = HAL_TIM_IC_MspInit;
  5077. break;
  5078. case HAL_TIM_IC_MSPDEINIT_CB_ID :
  5079. /* Legacy weak IC Msp DeInit Callback */
  5080. htim->IC_MspDeInitCallback = HAL_TIM_IC_MspDeInit;
  5081. break;
  5082. case HAL_TIM_OC_MSPINIT_CB_ID :
  5083. /* Legacy weak OC Msp Init Callback */
  5084. htim->OC_MspInitCallback = HAL_TIM_OC_MspInit;
  5085. break;
  5086. case HAL_TIM_OC_MSPDEINIT_CB_ID :
  5087. /* Legacy weak OC Msp DeInit Callback */
  5088. htim->OC_MspDeInitCallback = HAL_TIM_OC_MspDeInit;
  5089. break;
  5090. case HAL_TIM_PWM_MSPINIT_CB_ID :
  5091. /* Legacy weak PWM Msp Init Callback */
  5092. htim->PWM_MspInitCallback = HAL_TIM_PWM_MspInit;
  5093. break;
  5094. case HAL_TIM_PWM_MSPDEINIT_CB_ID :
  5095. /* Legacy weak PWM Msp DeInit Callback */
  5096. htim->PWM_MspDeInitCallback = HAL_TIM_PWM_MspDeInit;
  5097. break;
  5098. case HAL_TIM_ONE_PULSE_MSPINIT_CB_ID :
  5099. /* Legacy weak One Pulse Msp Init Callback */
  5100. htim->OnePulse_MspInitCallback = HAL_TIM_OnePulse_MspInit;
  5101. break;
  5102. case HAL_TIM_ONE_PULSE_MSPDEINIT_CB_ID :
  5103. /* Legacy weak One Pulse Msp DeInit Callback */
  5104. htim->OnePulse_MspDeInitCallback = HAL_TIM_OnePulse_MspDeInit;
  5105. break;
  5106. case HAL_TIM_ENCODER_MSPINIT_CB_ID :
  5107. /* Legacy weak Encoder Msp Init Callback */
  5108. htim->Encoder_MspInitCallback = HAL_TIM_Encoder_MspInit;
  5109. break;
  5110. case HAL_TIM_ENCODER_MSPDEINIT_CB_ID :
  5111. /* Legacy weak Encoder Msp DeInit Callback */
  5112. htim->Encoder_MspDeInitCallback = HAL_TIM_Encoder_MspDeInit;
  5113. break;
  5114. default :
  5115. /* Return error status */
  5116. status = HAL_ERROR;
  5117. break;
  5118. }
  5119. }
  5120. else
  5121. {
  5122. /* Return error status */
  5123. status = HAL_ERROR;
  5124. }
  5125. return status;
  5126. }
  5127. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  5128. /**
  5129. * @}
  5130. */
  5131. /** @defgroup TIM_Exported_Functions_Group10 TIM Peripheral State functions
  5132. * @brief TIM Peripheral State functions
  5133. *
  5134. @verbatim
  5135. ==============================================================================
  5136. ##### Peripheral State functions #####
  5137. ==============================================================================
  5138. [..]
  5139. This subsection permits to get in run-time the status of the peripheral
  5140. and the data flow.
  5141. @endverbatim
  5142. * @{
  5143. */
  5144. /**
  5145. * @brief Return the TIM Base handle state.
  5146. * @param htim TIM Base handle
  5147. * @retval HAL state
  5148. */
  5149. HAL_TIM_StateTypeDef HAL_TIM_Base_GetState(const TIM_HandleTypeDef *htim)
  5150. {
  5151. return htim->State;
  5152. }
  5153. /**
  5154. * @brief Return the TIM OC handle state.
  5155. * @param htim TIM Output Compare handle
  5156. * @retval HAL state
  5157. */
  5158. HAL_TIM_StateTypeDef HAL_TIM_OC_GetState(const TIM_HandleTypeDef *htim)
  5159. {
  5160. return htim->State;
  5161. }
  5162. /**
  5163. * @brief Return the TIM PWM handle state.
  5164. * @param htim TIM handle
  5165. * @retval HAL state
  5166. */
  5167. HAL_TIM_StateTypeDef HAL_TIM_PWM_GetState(const TIM_HandleTypeDef *htim)
  5168. {
  5169. return htim->State;
  5170. }
  5171. /**
  5172. * @brief Return the TIM Input Capture handle state.
  5173. * @param htim TIM IC handle
  5174. * @retval HAL state
  5175. */
  5176. HAL_TIM_StateTypeDef HAL_TIM_IC_GetState(const TIM_HandleTypeDef *htim)
  5177. {
  5178. return htim->State;
  5179. }
  5180. /**
  5181. * @brief Return the TIM One Pulse Mode handle state.
  5182. * @param htim TIM OPM handle
  5183. * @retval HAL state
  5184. */
  5185. HAL_TIM_StateTypeDef HAL_TIM_OnePulse_GetState(const TIM_HandleTypeDef *htim)
  5186. {
  5187. return htim->State;
  5188. }
  5189. /**
  5190. * @brief Return the TIM Encoder Mode handle state.
  5191. * @param htim TIM Encoder Interface handle
  5192. * @retval HAL state
  5193. */
  5194. HAL_TIM_StateTypeDef HAL_TIM_Encoder_GetState(const TIM_HandleTypeDef *htim)
  5195. {
  5196. return htim->State;
  5197. }
  5198. /**
  5199. * @brief Return the TIM Encoder Mode handle state.
  5200. * @param htim TIM handle
  5201. * @retval Active channel
  5202. */
  5203. HAL_TIM_ActiveChannel HAL_TIM_GetActiveChannel(const TIM_HandleTypeDef *htim)
  5204. {
  5205. return htim->Channel;
  5206. }
  5207. /**
  5208. * @brief Return actual state of the TIM channel.
  5209. * @param htim TIM handle
  5210. * @param Channel TIM Channel
  5211. * This parameter can be one of the following values:
  5212. * @arg TIM_CHANNEL_1: TIM Channel 1
  5213. * @arg TIM_CHANNEL_2: TIM Channel 2
  5214. * @arg TIM_CHANNEL_3: TIM Channel 3
  5215. * @arg TIM_CHANNEL_4: TIM Channel 4
  5216. * @arg TIM_CHANNEL_5: TIM Channel 5
  5217. * @arg TIM_CHANNEL_6: TIM Channel 6
  5218. * @retval TIM Channel state
  5219. */
  5220. HAL_TIM_ChannelStateTypeDef HAL_TIM_GetChannelState(const TIM_HandleTypeDef *htim, uint32_t Channel)
  5221. {
  5222. HAL_TIM_ChannelStateTypeDef channel_state;
  5223. /* Check the parameters */
  5224. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  5225. channel_state = TIM_CHANNEL_STATE_GET(htim, Channel);
  5226. return channel_state;
  5227. }
  5228. /**
  5229. * @brief Return actual state of a DMA burst operation.
  5230. * @param htim TIM handle
  5231. * @retval DMA burst state
  5232. */
  5233. HAL_TIM_DMABurstStateTypeDef HAL_TIM_DMABurstState(const TIM_HandleTypeDef *htim)
  5234. {
  5235. /* Check the parameters */
  5236. assert_param(IS_TIM_DMABURST_INSTANCE(htim->Instance));
  5237. return htim->DMABurstState;
  5238. }
  5239. /**
  5240. * @}
  5241. */
  5242. /**
  5243. * @}
  5244. */
  5245. /** @defgroup TIM_Private_Functions TIM Private Functions
  5246. * @{
  5247. */
  5248. /**
  5249. * @brief TIM DMA error callback
  5250. * @param hdma pointer to DMA handle.
  5251. * @retval None
  5252. */
  5253. void TIM_DMAError(DMA_HandleTypeDef *hdma)
  5254. {
  5255. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  5256. if (hdma == htim->hdma[TIM_DMA_ID_CC1])
  5257. {
  5258. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
  5259. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  5260. }
  5261. else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
  5262. {
  5263. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
  5264. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  5265. }
  5266. else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
  5267. {
  5268. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
  5269. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_3, HAL_TIM_CHANNEL_STATE_READY);
  5270. }
  5271. else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
  5272. {
  5273. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
  5274. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_4, HAL_TIM_CHANNEL_STATE_READY);
  5275. }
  5276. else
  5277. {
  5278. htim->State = HAL_TIM_STATE_READY;
  5279. }
  5280. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  5281. htim->ErrorCallback(htim);
  5282. #else
  5283. HAL_TIM_ErrorCallback(htim);
  5284. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  5285. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  5286. }
  5287. /**
  5288. * @brief TIM DMA Delay Pulse complete callback.
  5289. * @param hdma pointer to DMA handle.
  5290. * @retval None
  5291. */
  5292. static void TIM_DMADelayPulseCplt(DMA_HandleTypeDef *hdma)
  5293. {
  5294. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  5295. if (hdma == htim->hdma[TIM_DMA_ID_CC1])
  5296. {
  5297. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
  5298. if (hdma->Init.Mode == DMA_NORMAL)
  5299. {
  5300. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  5301. }
  5302. }
  5303. else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
  5304. {
  5305. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
  5306. if (hdma->Init.Mode == DMA_NORMAL)
  5307. {
  5308. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  5309. }
  5310. }
  5311. else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
  5312. {
  5313. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
  5314. if (hdma->Init.Mode == DMA_NORMAL)
  5315. {
  5316. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_3, HAL_TIM_CHANNEL_STATE_READY);
  5317. }
  5318. }
  5319. else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
  5320. {
  5321. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
  5322. if (hdma->Init.Mode == DMA_NORMAL)
  5323. {
  5324. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_4, HAL_TIM_CHANNEL_STATE_READY);
  5325. }
  5326. }
  5327. else
  5328. {
  5329. /* nothing to do */
  5330. }
  5331. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  5332. htim->PWM_PulseFinishedCallback(htim);
  5333. #else
  5334. HAL_TIM_PWM_PulseFinishedCallback(htim);
  5335. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  5336. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  5337. }
  5338. /**
  5339. * @brief TIM DMA Delay Pulse half complete callback.
  5340. * @param hdma pointer to DMA handle.
  5341. * @retval None
  5342. */
  5343. static void TIM_DMADelayPulseHalfCplt(DMA_HandleTypeDef *hdma)
  5344. {
  5345. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  5346. if (hdma == htim->hdma[TIM_DMA_ID_CC1])
  5347. {
  5348. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
  5349. }
  5350. else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
  5351. {
  5352. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
  5353. }
  5354. else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
  5355. {
  5356. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
  5357. }
  5358. else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
  5359. {
  5360. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
  5361. }
  5362. else
  5363. {
  5364. /* nothing to do */
  5365. }
  5366. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  5367. htim->PWM_PulseFinishedHalfCpltCallback(htim);
  5368. #else
  5369. HAL_TIM_PWM_PulseFinishedHalfCpltCallback(htim);
  5370. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  5371. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  5372. }
  5373. /**
  5374. * @brief TIM DMA Capture complete callback.
  5375. * @param hdma pointer to DMA handle.
  5376. * @retval None
  5377. */
  5378. void TIM_DMACaptureCplt(DMA_HandleTypeDef *hdma)
  5379. {
  5380. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  5381. if (hdma == htim->hdma[TIM_DMA_ID_CC1])
  5382. {
  5383. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
  5384. if (hdma->Init.Mode == DMA_NORMAL)
  5385. {
  5386. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_1, HAL_TIM_CHANNEL_STATE_READY);
  5387. }
  5388. }
  5389. else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
  5390. {
  5391. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
  5392. if (hdma->Init.Mode == DMA_NORMAL)
  5393. {
  5394. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_2, HAL_TIM_CHANNEL_STATE_READY);
  5395. }
  5396. }
  5397. else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
  5398. {
  5399. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
  5400. if (hdma->Init.Mode == DMA_NORMAL)
  5401. {
  5402. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_3, HAL_TIM_CHANNEL_STATE_READY);
  5403. }
  5404. }
  5405. else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
  5406. {
  5407. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
  5408. if (hdma->Init.Mode == DMA_NORMAL)
  5409. {
  5410. TIM_CHANNEL_STATE_SET(htim, TIM_CHANNEL_4, HAL_TIM_CHANNEL_STATE_READY);
  5411. }
  5412. }
  5413. else
  5414. {
  5415. /* nothing to do */
  5416. }
  5417. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  5418. htim->IC_CaptureCallback(htim);
  5419. #else
  5420. HAL_TIM_IC_CaptureCallback(htim);
  5421. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  5422. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  5423. }
  5424. /**
  5425. * @brief TIM DMA Capture half complete callback.
  5426. * @param hdma pointer to DMA handle.
  5427. * @retval None
  5428. */
  5429. void TIM_DMACaptureHalfCplt(DMA_HandleTypeDef *hdma)
  5430. {
  5431. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  5432. if (hdma == htim->hdma[TIM_DMA_ID_CC1])
  5433. {
  5434. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
  5435. }
  5436. else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
  5437. {
  5438. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
  5439. }
  5440. else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
  5441. {
  5442. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
  5443. }
  5444. else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
  5445. {
  5446. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
  5447. }
  5448. else
  5449. {
  5450. /* nothing to do */
  5451. }
  5452. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  5453. htim->IC_CaptureHalfCpltCallback(htim);
  5454. #else
  5455. HAL_TIM_IC_CaptureHalfCpltCallback(htim);
  5456. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  5457. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  5458. }
  5459. /**
  5460. * @brief TIM DMA Period Elapse complete callback.
  5461. * @param hdma pointer to DMA handle.
  5462. * @retval None
  5463. */
  5464. static void TIM_DMAPeriodElapsedCplt(DMA_HandleTypeDef *hdma)
  5465. {
  5466. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  5467. if (htim->hdma[TIM_DMA_ID_UPDATE]->Init.Mode == DMA_NORMAL)
  5468. {
  5469. htim->State = HAL_TIM_STATE_READY;
  5470. }
  5471. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  5472. htim->PeriodElapsedCallback(htim);
  5473. #else
  5474. HAL_TIM_PeriodElapsedCallback(htim);
  5475. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  5476. }
  5477. /**
  5478. * @brief TIM DMA Period Elapse half complete callback.
  5479. * @param hdma pointer to DMA handle.
  5480. * @retval None
  5481. */
  5482. static void TIM_DMAPeriodElapsedHalfCplt(DMA_HandleTypeDef *hdma)
  5483. {
  5484. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  5485. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  5486. htim->PeriodElapsedHalfCpltCallback(htim);
  5487. #else
  5488. HAL_TIM_PeriodElapsedHalfCpltCallback(htim);
  5489. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  5490. }
  5491. /**
  5492. * @brief TIM DMA Trigger callback.
  5493. * @param hdma pointer to DMA handle.
  5494. * @retval None
  5495. */
  5496. static void TIM_DMATriggerCplt(DMA_HandleTypeDef *hdma)
  5497. {
  5498. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  5499. if (htim->hdma[TIM_DMA_ID_TRIGGER]->Init.Mode == DMA_NORMAL)
  5500. {
  5501. htim->State = HAL_TIM_STATE_READY;
  5502. }
  5503. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  5504. htim->TriggerCallback(htim);
  5505. #else
  5506. HAL_TIM_TriggerCallback(htim);
  5507. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  5508. }
  5509. /**
  5510. * @brief TIM DMA Trigger half complete callback.
  5511. * @param hdma pointer to DMA handle.
  5512. * @retval None
  5513. */
  5514. static void TIM_DMATriggerHalfCplt(DMA_HandleTypeDef *hdma)
  5515. {
  5516. TIM_HandleTypeDef *htim = (TIM_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  5517. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  5518. htim->TriggerHalfCpltCallback(htim);
  5519. #else
  5520. HAL_TIM_TriggerHalfCpltCallback(htim);
  5521. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  5522. }
  5523. /**
  5524. * @brief Time Base configuration
  5525. * @param TIMx TIM peripheral
  5526. * @param Structure TIM Base configuration structure
  5527. * @retval None
  5528. */
  5529. static void TIM_Base_SetConfig(TIM_TypeDef *TIMx, const TIM_Base_InitTypeDef *Structure)
  5530. {
  5531. uint32_t tmpcr1;
  5532. tmpcr1 = TIMx->CR1;
  5533. /* Set TIM Time Base Unit parameters ---------------------------------------*/
  5534. if (IS_TIM_COUNTER_MODE_SELECT_INSTANCE(TIMx))
  5535. {
  5536. /* Select the Counter Mode */
  5537. tmpcr1 &= ~(TIM_CR1_DIR | TIM_CR1_CMS);
  5538. tmpcr1 |= Structure->CounterMode;
  5539. }
  5540. if (IS_TIM_CLOCK_DIVISION_INSTANCE(TIMx))
  5541. {
  5542. /* Set the clock division */
  5543. tmpcr1 &= ~TIM_CR1_CKD;
  5544. tmpcr1 |= (uint32_t)Structure->ClockDivision;
  5545. }
  5546. /* Set the auto-reload preload */
  5547. MODIFY_REG(tmpcr1, TIM_CR1_ARPE, Structure->AutoReloadPreload);
  5548. /* Set the Autoreload value */
  5549. TIMx->ARR = (uint32_t)Structure->Period ;
  5550. /* Set the Prescaler value */
  5551. TIMx->PSC = Structure->Prescaler;
  5552. /* Disable Update Event (UEV) with Update Generation (UG)
  5553. by changing Update Request Source (URS) to avoid Update flag (UIF) */
  5554. SET_BIT(TIMx->CR1, TIM_CR1_URS);
  5555. /* Generate an update event to reload the Prescaler
  5556. and the repetition counter (only for advanced timer) value immediately */
  5557. TIMx->EGR = TIM_EGR_UG;
  5558. TIMx->CR1 = tmpcr1;
  5559. }
  5560. /**
  5561. * @brief Timer Output Compare 1 configuration
  5562. * @param TIMx to select the TIM peripheral
  5563. * @param OC_Config The output configuration structure
  5564. * @retval None
  5565. */
  5566. static void TIM_OC1_SetConfig(TIM_TypeDef *TIMx, const TIM_OC_InitTypeDef *OC_Config)
  5567. {
  5568. uint32_t tmpccmrx;
  5569. uint32_t tmpccer;
  5570. uint32_t tmpcr2;
  5571. /* Get the TIMx CCER register value */
  5572. tmpccer = TIMx->CCER;
  5573. /* Disable the Channel 1: Reset the CC1E Bit */
  5574. TIMx->CCER &= ~TIM_CCER_CC1E;
  5575. /* Get the TIMx CR2 register value */
  5576. tmpcr2 = TIMx->CR2;
  5577. /* Get the TIMx CCMR1 register value */
  5578. tmpccmrx = TIMx->CCMR1;
  5579. /* Reset the Output Compare Mode Bits */
  5580. tmpccmrx &= ~TIM_CCMR1_OC1M;
  5581. tmpccmrx &= ~TIM_CCMR1_CC1S;
  5582. /* Select the Output Compare Mode */
  5583. tmpccmrx |= OC_Config->OCMode;
  5584. /* Reset the Output Polarity level */
  5585. tmpccer &= ~TIM_CCER_CC1P;
  5586. /* Set the Output Compare Polarity */
  5587. tmpccer |= OC_Config->OCPolarity;
  5588. /* Write to TIMx CR2 */
  5589. TIMx->CR2 = tmpcr2;
  5590. /* Write to TIMx CCMR1 */
  5591. TIMx->CCMR1 = tmpccmrx;
  5592. /* Set the Capture Compare Register value */
  5593. TIMx->CCR1 = OC_Config->Pulse;
  5594. /* Write to TIMx CCER */
  5595. TIMx->CCER = tmpccer;
  5596. }
  5597. /**
  5598. * @brief Timer Output Compare 2 configuration
  5599. * @param TIMx to select the TIM peripheral
  5600. * @param OC_Config The output configuration structure
  5601. * @retval None
  5602. */
  5603. static void TIM_OC2_SetConfig(TIM_TypeDef *TIMx, const TIM_OC_InitTypeDef *OC_Config)
  5604. {
  5605. uint32_t tmpccmrx;
  5606. uint32_t tmpccer;
  5607. uint32_t tmpcr2;
  5608. /* Get the TIMx CCER register value */
  5609. tmpccer = TIMx->CCER;
  5610. /* Disable the Channel 2: Reset the CC2E Bit */
  5611. TIMx->CCER &= ~TIM_CCER_CC2E;
  5612. /* Get the TIMx CR2 register value */
  5613. tmpcr2 = TIMx->CR2;
  5614. /* Get the TIMx CCMR1 register value */
  5615. tmpccmrx = TIMx->CCMR1;
  5616. /* Reset the Output Compare mode and Capture/Compare selection Bits */
  5617. tmpccmrx &= ~TIM_CCMR1_OC2M;
  5618. tmpccmrx &= ~TIM_CCMR1_CC2S;
  5619. /* Select the Output Compare Mode */
  5620. tmpccmrx |= (OC_Config->OCMode << 8U);
  5621. /* Reset the Output Polarity level */
  5622. tmpccer &= ~TIM_CCER_CC2P;
  5623. /* Set the Output Compare Polarity */
  5624. tmpccer |= (OC_Config->OCPolarity << 4U);
  5625. /* Write to TIMx CR2 */
  5626. TIMx->CR2 = tmpcr2;
  5627. /* Write to TIMx CCMR1 */
  5628. TIMx->CCMR1 = tmpccmrx;
  5629. /* Set the Capture Compare Register value */
  5630. TIMx->CCR2 = OC_Config->Pulse;
  5631. /* Write to TIMx CCER */
  5632. TIMx->CCER = tmpccer;
  5633. }
  5634. /**
  5635. * @brief Timer Output Compare 3 configuration
  5636. * @param TIMx to select the TIM peripheral
  5637. * @param OC_Config The output configuration structure
  5638. * @retval None
  5639. */
  5640. static void TIM_OC3_SetConfig(TIM_TypeDef *TIMx, const TIM_OC_InitTypeDef *OC_Config)
  5641. {
  5642. uint32_t tmpccmrx;
  5643. uint32_t tmpccer;
  5644. uint32_t tmpcr2;
  5645. /* Get the TIMx CCER register value */
  5646. tmpccer = TIMx->CCER;
  5647. /* Disable the Channel 3: Reset the CC2E Bit */
  5648. TIMx->CCER &= ~TIM_CCER_CC3E;
  5649. /* Get the TIMx CR2 register value */
  5650. tmpcr2 = TIMx->CR2;
  5651. /* Get the TIMx CCMR2 register value */
  5652. tmpccmrx = TIMx->CCMR2;
  5653. /* Reset the Output Compare mode and Capture/Compare selection Bits */
  5654. tmpccmrx &= ~TIM_CCMR2_OC3M;
  5655. tmpccmrx &= ~TIM_CCMR2_CC3S;
  5656. /* Select the Output Compare Mode */
  5657. tmpccmrx |= OC_Config->OCMode;
  5658. /* Reset the Output Polarity level */
  5659. tmpccer &= ~TIM_CCER_CC3P;
  5660. /* Set the Output Compare Polarity */
  5661. tmpccer |= (OC_Config->OCPolarity << 8U);
  5662. /* Write to TIMx CR2 */
  5663. TIMx->CR2 = tmpcr2;
  5664. /* Write to TIMx CCMR2 */
  5665. TIMx->CCMR2 = tmpccmrx;
  5666. /* Set the Capture Compare Register value */
  5667. TIMx->CCR3 = OC_Config->Pulse;
  5668. /* Write to TIMx CCER */
  5669. TIMx->CCER = tmpccer;
  5670. }
  5671. /**
  5672. * @brief Timer Output Compare 4 configuration
  5673. * @param TIMx to select the TIM peripheral
  5674. * @param OC_Config The output configuration structure
  5675. * @retval None
  5676. */
  5677. static void TIM_OC4_SetConfig(TIM_TypeDef *TIMx, const TIM_OC_InitTypeDef *OC_Config)
  5678. {
  5679. uint32_t tmpccmrx;
  5680. uint32_t tmpccer;
  5681. uint32_t tmpcr2;
  5682. /* Get the TIMx CCER register value */
  5683. tmpccer = TIMx->CCER;
  5684. /* Disable the Channel 4: Reset the CC4E Bit */
  5685. TIMx->CCER &= ~TIM_CCER_CC4E;
  5686. /* Get the TIMx CR2 register value */
  5687. tmpcr2 = TIMx->CR2;
  5688. /* Get the TIMx CCMR2 register value */
  5689. tmpccmrx = TIMx->CCMR2;
  5690. /* Reset the Output Compare mode and Capture/Compare selection Bits */
  5691. tmpccmrx &= ~TIM_CCMR2_OC4M;
  5692. tmpccmrx &= ~TIM_CCMR2_CC4S;
  5693. /* Select the Output Compare Mode */
  5694. tmpccmrx |= (OC_Config->OCMode << 8U);
  5695. /* Reset the Output Polarity level */
  5696. tmpccer &= ~TIM_CCER_CC4P;
  5697. /* Set the Output Compare Polarity */
  5698. tmpccer |= (OC_Config->OCPolarity << 12U);
  5699. /* Write to TIMx CR2 */
  5700. TIMx->CR2 = tmpcr2;
  5701. /* Write to TIMx CCMR2 */
  5702. TIMx->CCMR2 = tmpccmrx;
  5703. /* Set the Capture Compare Register value */
  5704. TIMx->CCR4 = OC_Config->Pulse;
  5705. /* Write to TIMx CCER */
  5706. TIMx->CCER = tmpccer;
  5707. }
  5708. /**
  5709. * @brief Slave Timer configuration function
  5710. * @param htim TIM handle
  5711. * @param sSlaveConfig Slave timer configuration
  5712. * @retval None
  5713. */
  5714. static HAL_StatusTypeDef TIM_SlaveTimer_SetConfig(TIM_HandleTypeDef *htim,
  5715. const TIM_SlaveConfigTypeDef *sSlaveConfig)
  5716. {
  5717. HAL_StatusTypeDef status = HAL_OK;
  5718. uint32_t tmpsmcr;
  5719. uint32_t tmpccmr1;
  5720. uint32_t tmpccer;
  5721. /* Get the TIMx SMCR register value */
  5722. tmpsmcr = htim->Instance->SMCR;
  5723. /* Reset the Trigger Selection Bits */
  5724. tmpsmcr &= ~TIM_SMCR_TS;
  5725. /* Set the Input Trigger source */
  5726. tmpsmcr |= sSlaveConfig->InputTrigger;
  5727. /* Reset the slave mode Bits */
  5728. tmpsmcr &= ~TIM_SMCR_SMS;
  5729. /* Set the slave mode */
  5730. tmpsmcr |= sSlaveConfig->SlaveMode;
  5731. /* Write to TIMx SMCR */
  5732. htim->Instance->SMCR = tmpsmcr;
  5733. /* Configure the trigger prescaler, filter, and polarity */
  5734. switch (sSlaveConfig->InputTrigger)
  5735. {
  5736. case TIM_TS_ETRF:
  5737. {
  5738. /* Check the parameters */
  5739. assert_param(IS_TIM_CLOCKSOURCE_ETRMODE1_INSTANCE(htim->Instance));
  5740. assert_param(IS_TIM_TRIGGERPRESCALER(sSlaveConfig->TriggerPrescaler));
  5741. assert_param(IS_TIM_TRIGGERPOLARITY(sSlaveConfig->TriggerPolarity));
  5742. assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));
  5743. /* Configure the ETR Trigger source */
  5744. TIM_ETR_SetConfig(htim->Instance,
  5745. sSlaveConfig->TriggerPrescaler,
  5746. sSlaveConfig->TriggerPolarity,
  5747. sSlaveConfig->TriggerFilter);
  5748. break;
  5749. }
  5750. case TIM_TS_TI1F_ED:
  5751. {
  5752. /* Check the parameters */
  5753. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  5754. assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));
  5755. if (sSlaveConfig->SlaveMode == TIM_SLAVEMODE_GATED)
  5756. {
  5757. return HAL_ERROR;
  5758. }
  5759. /* Disable the Channel 1: Reset the CC1E Bit */
  5760. tmpccer = htim->Instance->CCER;
  5761. htim->Instance->CCER &= ~TIM_CCER_CC1E;
  5762. tmpccmr1 = htim->Instance->CCMR1;
  5763. /* Set the filter */
  5764. tmpccmr1 &= ~TIM_CCMR1_IC1F;
  5765. tmpccmr1 |= ((sSlaveConfig->TriggerFilter) << 4U);
  5766. /* Write to TIMx CCMR1 and CCER registers */
  5767. htim->Instance->CCMR1 = tmpccmr1;
  5768. htim->Instance->CCER = tmpccer;
  5769. break;
  5770. }
  5771. case TIM_TS_TI1FP1:
  5772. {
  5773. /* Check the parameters */
  5774. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  5775. assert_param(IS_TIM_TRIGGERPOLARITY(sSlaveConfig->TriggerPolarity));
  5776. assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));
  5777. /* Configure TI1 Filter and Polarity */
  5778. TIM_TI1_ConfigInputStage(htim->Instance,
  5779. sSlaveConfig->TriggerPolarity,
  5780. sSlaveConfig->TriggerFilter);
  5781. break;
  5782. }
  5783. case TIM_TS_TI2FP2:
  5784. {
  5785. /* Check the parameters */
  5786. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  5787. assert_param(IS_TIM_TRIGGERPOLARITY(sSlaveConfig->TriggerPolarity));
  5788. assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));
  5789. /* Configure TI2 Filter and Polarity */
  5790. TIM_TI2_ConfigInputStage(htim->Instance,
  5791. sSlaveConfig->TriggerPolarity,
  5792. sSlaveConfig->TriggerFilter);
  5793. break;
  5794. }
  5795. case TIM_TS_ITR0:
  5796. case TIM_TS_ITR1:
  5797. case TIM_TS_ITR2:
  5798. case TIM_TS_ITR3:
  5799. {
  5800. /* Check the parameter */
  5801. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  5802. break;
  5803. }
  5804. default:
  5805. status = HAL_ERROR;
  5806. break;
  5807. }
  5808. return status;
  5809. }
  5810. /**
  5811. * @brief Configure the TI1 as Input.
  5812. * @param TIMx to select the TIM peripheral.
  5813. * @param TIM_ICPolarity The Input Polarity.
  5814. * This parameter can be one of the following values:
  5815. * @arg TIM_ICPOLARITY_RISING
  5816. * @arg TIM_ICPOLARITY_FALLING
  5817. * @arg TIM_ICPOLARITY_BOTHEDGE
  5818. * @param TIM_ICSelection specifies the input to be used.
  5819. * This parameter can be one of the following values:
  5820. * @arg TIM_ICSELECTION_DIRECTTI: TIM Input 1 is selected to be connected to IC1.
  5821. * @arg TIM_ICSELECTION_INDIRECTTI: TIM Input 1 is selected to be connected to IC2.
  5822. * @arg TIM_ICSELECTION_TRC: TIM Input 1 is selected to be connected to TRC.
  5823. * @param TIM_ICFilter Specifies the Input Capture Filter.
  5824. * This parameter must be a value between 0x00 and 0x0F.
  5825. * @retval None
  5826. * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI2FP1
  5827. * (on channel2 path) is used as the input signal. Therefore CCMR1 must be
  5828. * protected against un-initialized filter and polarity values.
  5829. */
  5830. static void TIM_TI1_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
  5831. uint32_t TIM_ICFilter)
  5832. {
  5833. uint32_t tmpccmr1;
  5834. uint32_t tmpccer;
  5835. /* Disable the Channel 1: Reset the CC1E Bit */
  5836. tmpccer = TIMx->CCER;
  5837. TIMx->CCER &= ~TIM_CCER_CC1E;
  5838. tmpccmr1 = TIMx->CCMR1;
  5839. /* Select the Input */
  5840. if (IS_TIM_CC2_INSTANCE(TIMx) != RESET)
  5841. {
  5842. tmpccmr1 &= ~TIM_CCMR1_CC1S;
  5843. tmpccmr1 |= TIM_ICSelection;
  5844. }
  5845. else
  5846. {
  5847. tmpccmr1 |= TIM_CCMR1_CC1S_0;
  5848. }
  5849. /* Set the filter */
  5850. tmpccmr1 &= ~TIM_CCMR1_IC1F;
  5851. tmpccmr1 |= ((TIM_ICFilter << 4U) & TIM_CCMR1_IC1F);
  5852. /* Select the Polarity and set the CC1E Bit */
  5853. tmpccer &= ~(TIM_CCER_CC1P | TIM_CCER_CC1NP);
  5854. tmpccer |= (TIM_ICPolarity & (TIM_CCER_CC1P | TIM_CCER_CC1NP));
  5855. /* Write to TIMx CCMR1 and CCER registers */
  5856. TIMx->CCMR1 = tmpccmr1;
  5857. TIMx->CCER = tmpccer;
  5858. }
  5859. /**
  5860. * @brief Configure the Polarity and Filter for TI1.
  5861. * @param TIMx to select the TIM peripheral.
  5862. * @param TIM_ICPolarity The Input Polarity.
  5863. * This parameter can be one of the following values:
  5864. * @arg TIM_ICPOLARITY_RISING
  5865. * @arg TIM_ICPOLARITY_FALLING
  5866. * @arg TIM_ICPOLARITY_BOTHEDGE
  5867. * @param TIM_ICFilter Specifies the Input Capture Filter.
  5868. * This parameter must be a value between 0x00 and 0x0F.
  5869. * @retval None
  5870. */
  5871. static void TIM_TI1_ConfigInputStage(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICFilter)
  5872. {
  5873. uint32_t tmpccmr1;
  5874. uint32_t tmpccer;
  5875. /* Disable the Channel 1: Reset the CC1E Bit */
  5876. tmpccer = TIMx->CCER;
  5877. TIMx->CCER &= ~TIM_CCER_CC1E;
  5878. tmpccmr1 = TIMx->CCMR1;
  5879. /* Set the filter */
  5880. tmpccmr1 &= ~TIM_CCMR1_IC1F;
  5881. tmpccmr1 |= (TIM_ICFilter << 4U);
  5882. /* Select the Polarity and set the CC1E Bit */
  5883. tmpccer &= ~(TIM_CCER_CC1P | TIM_CCER_CC1NP);
  5884. tmpccer |= TIM_ICPolarity;
  5885. /* Write to TIMx CCMR1 and CCER registers */
  5886. TIMx->CCMR1 = tmpccmr1;
  5887. TIMx->CCER = tmpccer;
  5888. }
  5889. /**
  5890. * @brief Configure the TI2 as Input.
  5891. * @param TIMx to select the TIM peripheral
  5892. * @param TIM_ICPolarity The Input Polarity.
  5893. * This parameter can be one of the following values:
  5894. * @arg TIM_ICPOLARITY_RISING
  5895. * @arg TIM_ICPOLARITY_FALLING
  5896. * @arg TIM_ICPOLARITY_BOTHEDGE
  5897. * @param TIM_ICSelection specifies the input to be used.
  5898. * This parameter can be one of the following values:
  5899. * @arg TIM_ICSELECTION_DIRECTTI: TIM Input 2 is selected to be connected to IC2.
  5900. * @arg TIM_ICSELECTION_INDIRECTTI: TIM Input 2 is selected to be connected to IC1.
  5901. * @arg TIM_ICSELECTION_TRC: TIM Input 2 is selected to be connected to TRC.
  5902. * @param TIM_ICFilter Specifies the Input Capture Filter.
  5903. * This parameter must be a value between 0x00 and 0x0F.
  5904. * @retval None
  5905. * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI1FP2
  5906. * (on channel1 path) is used as the input signal. Therefore CCMR1 must be
  5907. * protected against un-initialized filter and polarity values.
  5908. */
  5909. static void TIM_TI2_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
  5910. uint32_t TIM_ICFilter)
  5911. {
  5912. uint32_t tmpccmr1;
  5913. uint32_t tmpccer;
  5914. /* Disable the Channel 2: Reset the CC2E Bit */
  5915. tmpccer = TIMx->CCER;
  5916. TIMx->CCER &= ~TIM_CCER_CC2E;
  5917. tmpccmr1 = TIMx->CCMR1;
  5918. /* Select the Input */
  5919. tmpccmr1 &= ~TIM_CCMR1_CC2S;
  5920. tmpccmr1 |= (TIM_ICSelection << 8U);
  5921. /* Set the filter */
  5922. tmpccmr1 &= ~TIM_CCMR1_IC2F;
  5923. tmpccmr1 |= ((TIM_ICFilter << 12U) & TIM_CCMR1_IC2F);
  5924. /* Select the Polarity and set the CC2E Bit */
  5925. tmpccer &= ~(TIM_CCER_CC2P | TIM_CCER_CC2NP);
  5926. tmpccer |= ((TIM_ICPolarity << 4U) & (TIM_CCER_CC2P | TIM_CCER_CC2NP));
  5927. /* Write to TIMx CCMR1 and CCER registers */
  5928. TIMx->CCMR1 = tmpccmr1 ;
  5929. TIMx->CCER = tmpccer;
  5930. }
  5931. /**
  5932. * @brief Configure the Polarity and Filter for TI2.
  5933. * @param TIMx to select the TIM peripheral.
  5934. * @param TIM_ICPolarity The Input Polarity.
  5935. * This parameter can be one of the following values:
  5936. * @arg TIM_ICPOLARITY_RISING
  5937. * @arg TIM_ICPOLARITY_FALLING
  5938. * @arg TIM_ICPOLARITY_BOTHEDGE
  5939. * @param TIM_ICFilter Specifies the Input Capture Filter.
  5940. * This parameter must be a value between 0x00 and 0x0F.
  5941. * @retval None
  5942. */
  5943. static void TIM_TI2_ConfigInputStage(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICFilter)
  5944. {
  5945. uint32_t tmpccmr1;
  5946. uint32_t tmpccer;
  5947. /* Disable the Channel 2: Reset the CC2E Bit */
  5948. tmpccer = TIMx->CCER;
  5949. TIMx->CCER &= ~TIM_CCER_CC2E;
  5950. tmpccmr1 = TIMx->CCMR1;
  5951. /* Set the filter */
  5952. tmpccmr1 &= ~TIM_CCMR1_IC2F;
  5953. tmpccmr1 |= (TIM_ICFilter << 12U);
  5954. /* Select the Polarity and set the CC2E Bit */
  5955. tmpccer &= ~(TIM_CCER_CC2P | TIM_CCER_CC2NP);
  5956. tmpccer |= (TIM_ICPolarity << 4U);
  5957. /* Write to TIMx CCMR1 and CCER registers */
  5958. TIMx->CCMR1 = tmpccmr1 ;
  5959. TIMx->CCER = tmpccer;
  5960. }
  5961. /**
  5962. * @brief Configure the TI3 as Input.
  5963. * @param TIMx to select the TIM peripheral
  5964. * @param TIM_ICPolarity The Input Polarity.
  5965. * This parameter can be one of the following values:
  5966. * @arg TIM_ICPOLARITY_RISING
  5967. * @arg TIM_ICPOLARITY_FALLING
  5968. * @arg TIM_ICPOLARITY_BOTHEDGE
  5969. * @param TIM_ICSelection specifies the input to be used.
  5970. * This parameter can be one of the following values:
  5971. * @arg TIM_ICSELECTION_DIRECTTI: TIM Input 3 is selected to be connected to IC3.
  5972. * @arg TIM_ICSELECTION_INDIRECTTI: TIM Input 3 is selected to be connected to IC4.
  5973. * @arg TIM_ICSELECTION_TRC: TIM Input 3 is selected to be connected to TRC.
  5974. * @param TIM_ICFilter Specifies the Input Capture Filter.
  5975. * This parameter must be a value between 0x00 and 0x0F.
  5976. * @retval None
  5977. * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI3FP4
  5978. * (on channel1 path) is used as the input signal. Therefore CCMR2 must be
  5979. * protected against un-initialized filter and polarity values.
  5980. */
  5981. static void TIM_TI3_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
  5982. uint32_t TIM_ICFilter)
  5983. {
  5984. uint32_t tmpccmr2;
  5985. uint32_t tmpccer;
  5986. /* Disable the Channel 3: Reset the CC3E Bit */
  5987. tmpccer = TIMx->CCER;
  5988. TIMx->CCER &= ~TIM_CCER_CC3E;
  5989. tmpccmr2 = TIMx->CCMR2;
  5990. /* Select the Input */
  5991. tmpccmr2 &= ~TIM_CCMR2_CC3S;
  5992. tmpccmr2 |= TIM_ICSelection;
  5993. /* Set the filter */
  5994. tmpccmr2 &= ~TIM_CCMR2_IC3F;
  5995. tmpccmr2 |= ((TIM_ICFilter << 4U) & TIM_CCMR2_IC3F);
  5996. /* Select the Polarity and set the CC3E Bit */
  5997. tmpccer &= ~(TIM_CCER_CC3P | TIM_CCER_CC3NP);
  5998. tmpccer |= ((TIM_ICPolarity << 8U) & (TIM_CCER_CC3P | TIM_CCER_CC3NP));
  5999. /* Write to TIMx CCMR2 and CCER registers */
  6000. TIMx->CCMR2 = tmpccmr2;
  6001. TIMx->CCER = tmpccer;
  6002. }
  6003. /**
  6004. * @brief Configure the TI4 as Input.
  6005. * @param TIMx to select the TIM peripheral
  6006. * @param TIM_ICPolarity The Input Polarity.
  6007. * This parameter can be one of the following values:
  6008. * @arg TIM_ICPOLARITY_RISING
  6009. * @arg TIM_ICPOLARITY_FALLING
  6010. * @arg TIM_ICPOLARITY_BOTHEDGE
  6011. * @param TIM_ICSelection specifies the input to be used.
  6012. * This parameter can be one of the following values:
  6013. * @arg TIM_ICSELECTION_DIRECTTI: TIM Input 4 is selected to be connected to IC4.
  6014. * @arg TIM_ICSELECTION_INDIRECTTI: TIM Input 4 is selected to be connected to IC3.
  6015. * @arg TIM_ICSELECTION_TRC: TIM Input 4 is selected to be connected to TRC.
  6016. * @param TIM_ICFilter Specifies the Input Capture Filter.
  6017. * This parameter must be a value between 0x00 and 0x0F.
  6018. * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI4FP3
  6019. * (on channel1 path) is used as the input signal. Therefore CCMR2 must be
  6020. * protected against un-initialized filter and polarity values.
  6021. * @retval None
  6022. */
  6023. static void TIM_TI4_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
  6024. uint32_t TIM_ICFilter)
  6025. {
  6026. uint32_t tmpccmr2;
  6027. uint32_t tmpccer;
  6028. /* Disable the Channel 4: Reset the CC4E Bit */
  6029. tmpccer = TIMx->CCER;
  6030. TIMx->CCER &= ~TIM_CCER_CC4E;
  6031. tmpccmr2 = TIMx->CCMR2;
  6032. /* Select the Input */
  6033. tmpccmr2 &= ~TIM_CCMR2_CC4S;
  6034. tmpccmr2 |= (TIM_ICSelection << 8U);
  6035. /* Set the filter */
  6036. tmpccmr2 &= ~TIM_CCMR2_IC4F;
  6037. tmpccmr2 |= ((TIM_ICFilter << 12U) & TIM_CCMR2_IC4F);
  6038. /* Select the Polarity and set the CC4E Bit */
  6039. tmpccer &= ~(TIM_CCER_CC4P | TIM_CCER_CC4NP);
  6040. tmpccer |= ((TIM_ICPolarity << 12U) & (TIM_CCER_CC4P | TIM_CCER_CC4NP));
  6041. /* Write to TIMx CCMR2 and CCER registers */
  6042. TIMx->CCMR2 = tmpccmr2;
  6043. TIMx->CCER = tmpccer ;
  6044. }
  6045. /**
  6046. * @brief Selects the Input Trigger source
  6047. * @param TIMx to select the TIM peripheral
  6048. * @param InputTriggerSource The Input Trigger source.
  6049. * This parameter can be one of the following values:
  6050. * @arg TIM_TS_ITR0: Internal Trigger 0
  6051. * @arg TIM_TS_ITR1: Internal Trigger 1
  6052. * @arg TIM_TS_ITR2: Internal Trigger 2
  6053. * @arg TIM_TS_ITR3: Internal Trigger 3
  6054. * @arg TIM_TS_TI1F_ED: TI1 Edge Detector
  6055. * @arg TIM_TS_TI1FP1: Filtered Timer Input 1
  6056. * @arg TIM_TS_TI2FP2: Filtered Timer Input 2
  6057. * @arg TIM_TS_ETRF: External Trigger input
  6058. * @retval None
  6059. */
  6060. static void TIM_ITRx_SetConfig(TIM_TypeDef *TIMx, uint32_t InputTriggerSource)
  6061. {
  6062. uint32_t tmpsmcr;
  6063. /* Get the TIMx SMCR register value */
  6064. tmpsmcr = TIMx->SMCR;
  6065. /* Reset the TS Bits */
  6066. tmpsmcr &= ~TIM_SMCR_TS;
  6067. /* Set the Input Trigger source and the slave mode*/
  6068. tmpsmcr |= (InputTriggerSource | TIM_SLAVEMODE_EXTERNAL1);
  6069. /* Write to TIMx SMCR */
  6070. TIMx->SMCR = tmpsmcr;
  6071. }
  6072. /**
  6073. * @brief Configures the TIMx External Trigger (ETR).
  6074. * @param TIMx to select the TIM peripheral
  6075. * @param TIM_ExtTRGPrescaler The external Trigger Prescaler.
  6076. * This parameter can be one of the following values:
  6077. * @arg TIM_ETRPRESCALER_DIV1: ETRP Prescaler OFF.
  6078. * @arg TIM_ETRPRESCALER_DIV2: ETRP frequency divided by 2.
  6079. * @arg TIM_ETRPRESCALER_DIV4: ETRP frequency divided by 4.
  6080. * @arg TIM_ETRPRESCALER_DIV8: ETRP frequency divided by 8.
  6081. * @param TIM_ExtTRGPolarity The external Trigger Polarity.
  6082. * This parameter can be one of the following values:
  6083. * @arg TIM_ETRPOLARITY_INVERTED: active low or falling edge active.
  6084. * @arg TIM_ETRPOLARITY_NONINVERTED: active high or rising edge active.
  6085. * @param ExtTRGFilter External Trigger Filter.
  6086. * This parameter must be a value between 0x00 and 0x0F
  6087. * @retval None
  6088. */
  6089. static void TIM_ETR_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ExtTRGPrescaler,
  6090. uint32_t TIM_ExtTRGPolarity, uint32_t ExtTRGFilter)
  6091. {
  6092. uint32_t tmpsmcr;
  6093. tmpsmcr = TIMx->SMCR;
  6094. /* Reset the ETR Bits */
  6095. tmpsmcr &= ~(TIM_SMCR_ETF | TIM_SMCR_ETPS | TIM_SMCR_ECE | TIM_SMCR_ETP);
  6096. /* Set the Prescaler, the Filter value and the Polarity */
  6097. tmpsmcr |= (uint32_t)(TIM_ExtTRGPrescaler | (TIM_ExtTRGPolarity | (ExtTRGFilter << 8U)));
  6098. /* Write to TIMx SMCR */
  6099. TIMx->SMCR = tmpsmcr;
  6100. }
  6101. /**
  6102. * @brief Enables or disables the TIM Capture Compare Channel x.
  6103. * @param TIMx to select the TIM peripheral
  6104. * @param Channel specifies the TIM Channel
  6105. * This parameter can be one of the following values:
  6106. * @arg TIM_CHANNEL_1: TIM Channel 1
  6107. * @arg TIM_CHANNEL_2: TIM Channel 2
  6108. * @arg TIM_CHANNEL_3: TIM Channel 3
  6109. * @arg TIM_CHANNEL_4: TIM Channel 4
  6110. * @param ChannelState specifies the TIM Channel CCxE bit new state.
  6111. * This parameter can be: TIM_CCx_ENABLE or TIM_CCx_DISABLE.
  6112. * @retval None
  6113. */
  6114. static void TIM_CCxChannelCmd(TIM_TypeDef *TIMx, uint32_t Channel, uint32_t ChannelState)
  6115. {
  6116. uint32_t tmp;
  6117. /* Check the parameters */
  6118. assert_param(IS_TIM_CC1_INSTANCE(TIMx));
  6119. assert_param(IS_TIM_CHANNELS(Channel));
  6120. tmp = TIM_CCER_CC1E << (Channel & 0x1FU); /* 0x1FU = 31 bits max shift */
  6121. /* Reset the CCxE Bit */
  6122. TIMx->CCER &= ~tmp;
  6123. /* Set or reset the CCxE Bit */
  6124. TIMx->CCER |= (uint32_t)(ChannelState << (Channel & 0x1FU)); /* 0x1FU = 31 bits max shift */
  6125. }
  6126. #if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
  6127. /**
  6128. * @brief Reset interrupt callbacks to the legacy weak callbacks.
  6129. * @param htim pointer to a TIM_HandleTypeDef structure that contains
  6130. * the configuration information for TIM module.
  6131. * @retval None
  6132. */
  6133. void TIM_ResetCallback(TIM_HandleTypeDef *htim)
  6134. {
  6135. /* Reset the TIM callback to the legacy weak callbacks */
  6136. htim->PeriodElapsedCallback = HAL_TIM_PeriodElapsedCallback;
  6137. htim->PeriodElapsedHalfCpltCallback = HAL_TIM_PeriodElapsedHalfCpltCallback;
  6138. htim->TriggerCallback = HAL_TIM_TriggerCallback;
  6139. htim->TriggerHalfCpltCallback = HAL_TIM_TriggerHalfCpltCallback;
  6140. htim->IC_CaptureCallback = HAL_TIM_IC_CaptureCallback;
  6141. htim->IC_CaptureHalfCpltCallback = HAL_TIM_IC_CaptureHalfCpltCallback;
  6142. htim->OC_DelayElapsedCallback = HAL_TIM_OC_DelayElapsedCallback;
  6143. htim->PWM_PulseFinishedCallback = HAL_TIM_PWM_PulseFinishedCallback;
  6144. htim->PWM_PulseFinishedHalfCpltCallback = HAL_TIM_PWM_PulseFinishedHalfCpltCallback;
  6145. htim->ErrorCallback = HAL_TIM_ErrorCallback;
  6146. }
  6147. #endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
  6148. /**
  6149. * @}
  6150. */
  6151. #endif /* HAL_TIM_MODULE_ENABLED */
  6152. /**
  6153. * @}
  6154. */
  6155. /**
  6156. * @}
  6157. */