stm32wbxx_hal_tim.c 247 KB

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