stm32f1xx_hal_tim.c 235 KB

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