py32f002b_hal_tim.c 156 KB

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