stm32l4xx_hal_tim.c 167 KB

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