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

stm32f4xx_hal_tim.c 207KB

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