stm32f1xx_hal_tim.c 160 KB

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