You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stm32l1xx_hal_tim.c 217KB

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