stm32f0xx_hal_tim.c 166 KB

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