stm32l4xx_hal_tim.c 247 KB

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