stm32f4xx_hal_tim.c 176 KB

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