stm32f1xx_hal_tim.c 235 KB

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