stm32l4xx_hal_tim.c 247 KB

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