stm32f0xx_hal_tim.c 238 KB

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