stm32f1xx_hal_tim.c 238 KB

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