stm32l1xx_hal_tim.c 154 KB

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