stm32f0xx_hal_tim.c 165 KB

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