stm32f1xx_hal_tim.c 238 KB

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