stm32f7xx_hal_tim.c 201 KB

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