stm32f7xx_hal_tim.c 246 KB

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