stm32h7xx_hal_tim.c 179 KB

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