stm32h7xx_hal_tim.c 254 KB

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