stm32f7xx_hal_tim.c 201 KB

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