stm32h7xx_hal_tim.c 247 KB

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