stm32g4xx_hal_tim.c 254 KB

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