stm32h5xx_hal_tim.c 258 KB

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