stm32f7xx_hal_tim.c 254 KB

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