stm32f4xx_hal_tim.c 169 KB

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