stm32f1xx_hal_tim.c 207 KB

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