stm32f4xx_hal_tim.c 236 KB

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