stm32g0xx_hal_tim.c 255 KB

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