stm32f1xx_hal_tim.c 161 KB

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