stm32l4xx_hal_tim.c 174 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_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. * + Time Base Initialization
  9. * + Time Base Start
  10. * + Time Base Start Interruption
  11. * + Time Base Start DMA
  12. * + Time Output Compare/PWM Initialization
  13. * + Time Output Compare/PWM Channel Configuration
  14. * + Time Output Compare/PWM Start
  15. * + Time Output Compare/PWM Start Interruption
  16. * + Time Output Compare/PWM Start DMA
  17. * + Time Input Capture Initialization
  18. * + Time Input Capture Channel Configuration
  19. * + Time Input Capture Start
  20. * + Time Input Capture Start Interruption
  21. * + Time Input Capture Start DMA
  22. * + Time One Pulse Initialization
  23. * + Time One Pulse Channel Configuration
  24. * + Time One Pulse Start
  25. * + Time Encoder Interface Initialization
  26. * + Time Encoder Interface Start
  27. * + Time Encoder Interface Start Interruption
  28. * + Time Encoder Interface Start DMA
  29. * + Commutation Event configuration with Interruption and DMA
  30. * + Time OCRef clear configuration
  31. * + Time External Clock configuration
  32. @verbatim
  33. ==============================================================================
  34. ##### TIMER Generic features #####
  35. ==============================================================================
  36. [..] The Timer features include:
  37. (#) 16-bit up, down, up/down auto-reload counter.
  38. (#) 16-bit programmable prescaler allowing dividing (also on the fly) the
  39. counter clock frequency either by any factor between 1 and 65536.
  40. (#) Up to 4 independent channels for:
  41. (++) Input Capture
  42. (++) Output Compare
  43. (++) PWM generation (Edge and Center-aligned Mode)
  44. (++) One-pulse mode output
  45. ##### How to use this driver #####
  46. ==============================================================================
  47. [..]
  48. (#) Initialize the TIM low level resources by implementing the following functions
  49. depending on the selected feature:
  50. (++) Time Base : HAL_TIM_Base_MspInit()
  51. (++) Input Capture : HAL_TIM_IC_MspInit()
  52. (++) Output Compare : HAL_TIM_OC_MspInit()
  53. (++) PWM generation : HAL_TIM_PWM_MspInit()
  54. (++) One-pulse mode output : HAL_TIM_OnePulse_MspInit()
  55. (++) Encoder mode output : HAL_TIM_Encoder_MspInit()
  56. (#) Initialize the TIM low level resources :
  57. (##) Enable the TIM interface clock using __HAL_RCC_TIMx_CLK_ENABLE();
  58. (##) TIM pins configuration
  59. (+++) Enable the clock for the TIM GPIOs using the following function:
  60. __HAL_RCC_GPIOx_CLK_ENABLE();
  61. (+++) Configure these TIM pins in Alternate function mode using HAL_GPIO_Init();
  62. (#) The external Clock can be configured, if needed (the default clock is the
  63. internal clock from the APBx), using the following function:
  64. HAL_TIM_ConfigClockSource, the clock configuration should be done before
  65. any start function.
  66. (#) Configure the TIM in the desired functioning mode using one of the
  67. Initialization function of this driver:
  68. (++) HAL_TIM_Base_Init: to use the Timer to generate a simple time base
  69. (++) HAL_TIM_OC_Init and HAL_TIM_OC_ConfigChannel: to use the Timer to generate an
  70. Output Compare signal.
  71. (++) HAL_TIM_PWM_Init and HAL_TIM_PWM_ConfigChannel: to use the Timer to generate a
  72. PWM signal.
  73. (++) HAL_TIM_IC_Init and HAL_TIM_IC_ConfigChannel: to use the Timer to measure an
  74. external signal.
  75. (++) HAL_TIM_OnePulse_Init and HAL_TIM_OnePulse_ConfigChannel: to use the Timer
  76. in One Pulse Mode.
  77. (++) HAL_TIM_Encoder_Init: to use the Timer Encoder Interface.
  78. (#) Activate the TIM peripheral using one of the start functions depending from the feature used:
  79. (++) Time Base : HAL_TIM_Base_Start(), HAL_TIM_Base_Start_DMA(), HAL_TIM_Base_Start_IT()
  80. (++) Input Capture : HAL_TIM_IC_Start(), HAL_TIM_IC_Start_DMA(), HAL_TIM_IC_Start_IT()
  81. (++) Output Compare : HAL_TIM_OC_Start(), HAL_TIM_OC_Start_DMA(), HAL_TIM_OC_Start_IT()
  82. (++) PWM generation : HAL_TIM_PWM_Start(), HAL_TIM_PWM_Start_DMA(), HAL_TIM_PWM_Start_IT()
  83. (++) One-pulse mode output : HAL_TIM_OnePulse_Start(), HAL_TIM_OnePulse_Start_IT()
  84. (++) Encoder mode output : HAL_TIM_Encoder_Start(), HAL_TIM_Encoder_Start_DMA(), HAL_TIM_Encoder_Start_IT().
  85. (#) The DMA Burst is managed with the two following functions:
  86. HAL_TIM_DMABurst_WriteStart()
  87. HAL_TIM_DMABurst_ReadStart()
  88. @endverbatim
  89. ******************************************************************************
  90. * @attention
  91. *
  92. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  93. *
  94. * Redistribution and use in source and binary forms, with or without modification,
  95. * are permitted provided that the following conditions are met:
  96. * 1. Redistributions of source code must retain the above copyright notice,
  97. * this list of conditions and the following disclaimer.
  98. * 2. Redistributions in binary form must reproduce the above copyright notice,
  99. * this list of conditions and the following disclaimer in the documentation
  100. * and/or other materials provided with the distribution.
  101. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  102. * may be used to endorse or promote products derived from this software
  103. * without specific prior written permission.
  104. *
  105. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  106. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  107. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  108. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  109. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  110. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  111. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  112. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  113. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  114. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  115. *
  116. ******************************************************************************
  117. */
  118. /* Includes ------------------------------------------------------------------*/
  119. #include "stm32l4xx_hal.h"
  120. /** @addtogroup STM32L4xx_HAL_Driver
  121. * @{
  122. */
  123. /** @defgroup TIM TIM
  124. * @brief TIM HAL module driver
  125. * @{
  126. */
  127. #ifdef HAL_TIM_MODULE_ENABLED
  128. /* Private typedef -----------------------------------------------------------*/
  129. /* Private define ------------------------------------------------------------*/
  130. /* Private macro -------------------------------------------------------------*/
  131. /* Private variables ---------------------------------------------------------*/
  132. /* Private function prototypes -----------------------------------------------*/
  133. static void TIM_OC1_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config);
  134. static void TIM_OC3_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config);
  135. static void TIM_OC4_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config);
  136. static void TIM_OC5_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config);
  137. static void TIM_OC6_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config);
  138. static void TIM_TI1_ConfigInputStage(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICFilter);
  139. static void TIM_TI2_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
  140. uint32_t TIM_ICFilter);
  141. static void TIM_TI2_ConfigInputStage(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICFilter);
  142. static void TIM_TI3_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
  143. uint32_t TIM_ICFilter);
  144. static void TIM_TI4_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
  145. uint32_t TIM_ICFilter);
  146. static void TIM_ITRx_SetConfig(TIM_TypeDef* TIMx, uint16_t InputTriggerSource);
  147. static void TIM_DMAPeriodElapsedCplt(DMA_HandleTypeDef *hdma);
  148. static void TIM_DMATriggerCplt(DMA_HandleTypeDef *hdma);
  149. static void TIM_SlaveTimer_SetConfig(TIM_HandleTypeDef *htim,
  150. TIM_SlaveConfigTypeDef * sSlaveConfig);
  151. /* Exported functions --------------------------------------------------------*/
  152. /** @defgroup TIM_Exported_Functions TIM Exported Functions
  153. * @{
  154. */
  155. /** @defgroup TIM_Exported_Functions_Group1 Time Base functions
  156. * @brief Time Base functions
  157. *
  158. @verbatim
  159. ==============================================================================
  160. ##### Time Base functions #####
  161. ==============================================================================
  162. [..]
  163. This section provides functions allowing to:
  164. (+) Initialize and configure the TIM base.
  165. (+) De-initialize the TIM base.
  166. (+) Start the Time Base.
  167. (+) Stop the Time Base.
  168. (+) Start the Time Base and enable interrupt.
  169. (+) Stop the Time Base and disable interrupt.
  170. (+) Start the Time Base and enable DMA transfer.
  171. (+) Stop the Time Base and disable DMA transfer.
  172. @endverbatim
  173. * @{
  174. */
  175. /**
  176. * @brief Initializes the TIM Time base Unit according to the specified
  177. * parameters in the TIM_HandleTypeDef and initialize the associated handle.
  178. * @note Switching from Center Aligned counter mode to Edge counter mode (or reverse)
  179. * requires a timer reset to avoid unexpected direction
  180. * due to DIR bit readonly in center aligned mode.
  181. * Ex: call @ref HAL_TIM_Base_DeInit() before HAL_TIM_Base_Init()
  182. * @param htim TIM Base handle
  183. * @retval HAL status
  184. */
  185. HAL_StatusTypeDef HAL_TIM_Base_Init(TIM_HandleTypeDef *htim)
  186. {
  187. /* Check the TIM handle allocation */
  188. if(htim == NULL)
  189. {
  190. return HAL_ERROR;
  191. }
  192. /* Check the parameters */
  193. assert_param(IS_TIM_INSTANCE(htim->Instance));
  194. assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
  195. assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
  196. assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
  197. if(htim->State == HAL_TIM_STATE_RESET)
  198. {
  199. /* Allocate lock resource and initialize it */
  200. htim->Lock = HAL_UNLOCKED;
  201. /* Init the low level hardware : GPIO, CLOCK, NVIC */
  202. HAL_TIM_Base_MspInit(htim);
  203. }
  204. /* Set the TIM state */
  205. htim->State= HAL_TIM_STATE_BUSY;
  206. /* Set the Time Base configuration */
  207. TIM_Base_SetConfig(htim->Instance, &htim->Init);
  208. /* Initialize the TIM state*/
  209. htim->State= HAL_TIM_STATE_READY;
  210. return HAL_OK;
  211. }
  212. /**
  213. * @brief DeInitialize the TIM Base peripheral
  214. * @param htim TIM Base handle
  215. * @retval HAL status
  216. */
  217. HAL_StatusTypeDef HAL_TIM_Base_DeInit(TIM_HandleTypeDef *htim)
  218. {
  219. /* Check the parameters */
  220. assert_param(IS_TIM_INSTANCE(htim->Instance));
  221. htim->State = HAL_TIM_STATE_BUSY;
  222. /* Disable the TIM Peripheral Clock */
  223. __HAL_TIM_DISABLE(htim);
  224. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  225. HAL_TIM_Base_MspDeInit(htim);
  226. /* Change TIM state */
  227. htim->State = HAL_TIM_STATE_RESET;
  228. /* Release Lock */
  229. __HAL_UNLOCK(htim);
  230. return HAL_OK;
  231. }
  232. /**
  233. * @brief Initializes the TIM Base MSP.
  234. * @param htim TIM handle
  235. * @retval None
  236. */
  237. __weak void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim)
  238. {
  239. /* Prevent unused argument(s) compilation warning */
  240. UNUSED(htim);
  241. /* NOTE : This function should not be modified, when the callback is needed,
  242. the HAL_TIM_Base_MspInit could be implemented in the user file
  243. */
  244. }
  245. /**
  246. * @brief DeInitialize TIM Base MSP.
  247. * @param htim TIM handle
  248. * @retval None
  249. */
  250. __weak void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef *htim)
  251. {
  252. /* Prevent unused argument(s) compilation warning */
  253. UNUSED(htim);
  254. /* NOTE : This function should not be modified, when the callback is needed,
  255. the HAL_TIM_Base_MspDeInit could be implemented in the user file
  256. */
  257. }
  258. /**
  259. * @brief Starts the TIM Base generation.
  260. * @param htim TIM handle
  261. * @retval HAL status
  262. */
  263. HAL_StatusTypeDef HAL_TIM_Base_Start(TIM_HandleTypeDef *htim)
  264. {
  265. /* Check the parameters */
  266. assert_param(IS_TIM_INSTANCE(htim->Instance));
  267. /* Set the TIM state */
  268. htim->State= HAL_TIM_STATE_BUSY;
  269. /* Enable the Peripheral */
  270. __HAL_TIM_ENABLE(htim);
  271. /* Change the TIM state*/
  272. htim->State= HAL_TIM_STATE_READY;
  273. /* Return function status */
  274. return HAL_OK;
  275. }
  276. /**
  277. * @brief Stops the TIM Base generation.
  278. * @param htim TIM handle
  279. * @retval HAL status
  280. */
  281. HAL_StatusTypeDef HAL_TIM_Base_Stop(TIM_HandleTypeDef *htim)
  282. {
  283. /* Check the parameters */
  284. assert_param(IS_TIM_INSTANCE(htim->Instance));
  285. /* Set the TIM state */
  286. htim->State= HAL_TIM_STATE_BUSY;
  287. /* Disable the Peripheral */
  288. __HAL_TIM_DISABLE(htim);
  289. /* Change the TIM state*/
  290. htim->State= HAL_TIM_STATE_READY;
  291. /* Return function status */
  292. return HAL_OK;
  293. }
  294. /**
  295. * @brief Starts the TIM Base generation in interrupt mode.
  296. * @param htim TIM handle
  297. * @retval HAL status
  298. */
  299. HAL_StatusTypeDef HAL_TIM_Base_Start_IT(TIM_HandleTypeDef *htim)
  300. {
  301. /* Check the parameters */
  302. assert_param(IS_TIM_INSTANCE(htim->Instance));
  303. /* Enable the TIM Update interrupt */
  304. __HAL_TIM_ENABLE_IT(htim, TIM_IT_UPDATE);
  305. /* Enable the Peripheral */
  306. __HAL_TIM_ENABLE(htim);
  307. /* Return function status */
  308. return HAL_OK;
  309. }
  310. /**
  311. * @brief Stops the TIM Base generation in interrupt mode.
  312. * @param htim TIM handle
  313. * @retval HAL status
  314. */
  315. HAL_StatusTypeDef HAL_TIM_Base_Stop_IT(TIM_HandleTypeDef *htim)
  316. {
  317. /* Check the parameters */
  318. assert_param(IS_TIM_INSTANCE(htim->Instance));
  319. /* Disable the TIM Update interrupt */
  320. __HAL_TIM_DISABLE_IT(htim, TIM_IT_UPDATE);
  321. /* Disable the Peripheral */
  322. __HAL_TIM_DISABLE(htim);
  323. /* Return function status */
  324. return HAL_OK;
  325. }
  326. /**
  327. * @brief Starts the TIM Base generation in DMA mode.
  328. * @param htim TIM handle
  329. * @param pData The source Buffer address.
  330. * @param Length The length of data to be transferred from memory to peripheral.
  331. * @retval HAL status
  332. */
  333. HAL_StatusTypeDef HAL_TIM_Base_Start_DMA(TIM_HandleTypeDef *htim, uint32_t *pData, uint16_t Length)
  334. {
  335. /* Check the parameters */
  336. assert_param(IS_TIM_DMA_INSTANCE(htim->Instance));
  337. if((htim->State == HAL_TIM_STATE_BUSY))
  338. {
  339. return HAL_BUSY;
  340. }
  341. else if((htim->State == HAL_TIM_STATE_READY))
  342. {
  343. if((pData == 0 ) && (Length > 0))
  344. {
  345. return HAL_ERROR;
  346. }
  347. else
  348. {
  349. htim->State = HAL_TIM_STATE_BUSY;
  350. }
  351. }
  352. /* Set the DMA Period elapsed callback */
  353. htim->hdma[TIM_DMA_ID_UPDATE]->XferCpltCallback = TIM_DMAPeriodElapsedCplt;
  354. /* Set the DMA error callback */
  355. htim->hdma[TIM_DMA_ID_UPDATE]->XferErrorCallback = TIM_DMAError ;
  356. /* Enable the DMA channel */
  357. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_UPDATE], (uint32_t)pData, (uint32_t)&htim->Instance->ARR, Length);
  358. /* Enable the TIM Update DMA request */
  359. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_UPDATE);
  360. /* Enable the Peripheral */
  361. __HAL_TIM_ENABLE(htim);
  362. /* Return function status */
  363. return HAL_OK;
  364. }
  365. /**
  366. * @brief Stops the TIM Base generation in DMA mode.
  367. * @param htim TIM handle
  368. * @retval HAL status
  369. */
  370. HAL_StatusTypeDef HAL_TIM_Base_Stop_DMA(TIM_HandleTypeDef *htim)
  371. {
  372. /* Check the parameters */
  373. assert_param(IS_TIM_DMA_INSTANCE(htim->Instance));
  374. /* Disable the TIM Update DMA request */
  375. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_UPDATE);
  376. /* Disable the Peripheral */
  377. __HAL_TIM_DISABLE(htim);
  378. /* Change the htim state */
  379. htim->State = HAL_TIM_STATE_READY;
  380. /* Return function status */
  381. return HAL_OK;
  382. }
  383. /**
  384. * @}
  385. */
  386. /** @defgroup TIM_Exported_Functions_Group2 Time Output Compare functions
  387. * @brief Time Output Compare functions
  388. *
  389. @verbatim
  390. ==============================================================================
  391. ##### Time Output Compare functions #####
  392. ==============================================================================
  393. [..]
  394. This section provides functions allowing to:
  395. (+) Initialize and configure the TIM Output Compare.
  396. (+) De-initialize the TIM Output Compare.
  397. (+) Start the Time Output Compare.
  398. (+) Stop the Time Output Compare.
  399. (+) Start the Time Output Compare and enable interrupt.
  400. (+) Stop the Time Output Compare and disable interrupt.
  401. (+) Start the Time Output Compare and enable DMA transfer.
  402. (+) Stop the Time Output Compare and disable DMA transfer.
  403. @endverbatim
  404. * @{
  405. */
  406. /**
  407. * @brief Initializes the TIM Output Compare according to the specified
  408. * parameters in the TIM_HandleTypeDef and initialize the associated handle.
  409. * @note Switching from Center Aligned counter mode to Edge counter mode (or reverse)
  410. * requires a timer reset to avoid unexpected direction
  411. * due to DIR bit readonly in center aligned mode.
  412. * Ex: call @ref HAL_TIM_OC_DeInit() before HAL_TIM_OC_Init()
  413. * @param htim TIM Output Compare handle
  414. * @retval HAL status
  415. */
  416. HAL_StatusTypeDef HAL_TIM_OC_Init(TIM_HandleTypeDef* htim)
  417. {
  418. /* Check the TIM handle allocation */
  419. if(htim == NULL)
  420. {
  421. return HAL_ERROR;
  422. }
  423. /* Check the parameters */
  424. assert_param(IS_TIM_INSTANCE(htim->Instance));
  425. assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
  426. assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
  427. assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
  428. if(htim->State == HAL_TIM_STATE_RESET)
  429. {
  430. /* Allocate lock resource and initialize it */
  431. htim->Lock = HAL_UNLOCKED;
  432. /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
  433. HAL_TIM_OC_MspInit(htim);
  434. }
  435. /* Set the TIM state */
  436. htim->State= HAL_TIM_STATE_BUSY;
  437. /* Init the base time for the Output Compare */
  438. TIM_Base_SetConfig(htim->Instance, &htim->Init);
  439. /* Initialize the TIM state*/
  440. htim->State= HAL_TIM_STATE_READY;
  441. return HAL_OK;
  442. }
  443. /**
  444. * @brief DeInitialize the TIM peripheral
  445. * @param htim TIM Output Compare handle
  446. * @retval HAL status
  447. */
  448. HAL_StatusTypeDef HAL_TIM_OC_DeInit(TIM_HandleTypeDef *htim)
  449. {
  450. /* Check the parameters */
  451. assert_param(IS_TIM_INSTANCE(htim->Instance));
  452. htim->State = HAL_TIM_STATE_BUSY;
  453. /* Disable the TIM Peripheral Clock */
  454. __HAL_TIM_DISABLE(htim);
  455. /* DeInit the low level hardware: GPIO, CLOCK, NVIC and DMA */
  456. HAL_TIM_OC_MspDeInit(htim);
  457. /* Change TIM state */
  458. htim->State = HAL_TIM_STATE_RESET;
  459. /* Release Lock */
  460. __HAL_UNLOCK(htim);
  461. return HAL_OK;
  462. }
  463. /**
  464. * @brief Initializes the TIM Output Compare MSP.
  465. * @param htim TIM handle
  466. * @retval None
  467. */
  468. __weak void HAL_TIM_OC_MspInit(TIM_HandleTypeDef *htim)
  469. {
  470. /* Prevent unused argument(s) compilation warning */
  471. UNUSED(htim);
  472. /* NOTE : This function should not be modified, when the callback is needed,
  473. the HAL_TIM_OC_MspInit could be implemented in the user file
  474. */
  475. }
  476. /**
  477. * @brief DeInitialize TIM Output Compare MSP.
  478. * @param htim TIM handle
  479. * @retval None
  480. */
  481. __weak void HAL_TIM_OC_MspDeInit(TIM_HandleTypeDef *htim)
  482. {
  483. /* Prevent unused argument(s) compilation warning */
  484. UNUSED(htim);
  485. /* NOTE : This function should not be modified, when the callback is needed,
  486. the HAL_TIM_OC_MspDeInit could be implemented in the user file
  487. */
  488. }
  489. /**
  490. * @brief Starts the TIM Output Compare signal generation.
  491. * @param htim TIM Output Compare handle
  492. * @param Channel TIM Channel to be enabled
  493. * This parameter can be one of the following values:
  494. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  495. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  496. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  497. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  498. * @arg TIM_CHANNEL_5: TIM Channel 5 selected
  499. * @arg TIM_CHANNEL_6: TIM Channel 6 selected
  500. * @retval HAL status
  501. */
  502. HAL_StatusTypeDef HAL_TIM_OC_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
  503. {
  504. /* Check the parameters */
  505. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  506. /* Enable the Output compare channel */
  507. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
  508. if(IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  509. {
  510. /* Enable the main output */
  511. __HAL_TIM_MOE_ENABLE(htim);
  512. }
  513. /* Enable the Peripheral */
  514. __HAL_TIM_ENABLE(htim);
  515. /* Return function status */
  516. return HAL_OK;
  517. }
  518. /**
  519. * @brief Stops the TIM Output Compare signal generation.
  520. * @param htim TIM handle
  521. * @param Channel TIM Channel to be disabled
  522. * This parameter can be one of the following values:
  523. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  524. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  525. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  526. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  527. * @arg TIM_CHANNEL_5: TIM Channel 5 selected
  528. * @arg TIM_CHANNEL_6: TIM Channel 6 selected
  529. * @retval HAL status
  530. */
  531. HAL_StatusTypeDef HAL_TIM_OC_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
  532. {
  533. /* Check the parameters */
  534. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  535. /* Disable the Output compare channel */
  536. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
  537. if(IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  538. {
  539. /* Disable the Main Ouput */
  540. __HAL_TIM_MOE_DISABLE(htim);
  541. }
  542. /* Disable the Peripheral */
  543. __HAL_TIM_DISABLE(htim);
  544. /* Return function status */
  545. return HAL_OK;
  546. }
  547. /**
  548. * @brief Starts the TIM Output Compare signal generation in interrupt mode.
  549. * @param htim TIM OC handle
  550. * @param Channel TIM Channel to be enabled
  551. * This parameter can be one of the following values:
  552. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  553. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  554. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  555. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  556. * @arg TIM_CHANNEL_5: TIM Channel 5 selected
  557. * @arg TIM_CHANNEL_6: TIM Channel 6 selected
  558. * @retval HAL status
  559. */
  560. HAL_StatusTypeDef HAL_TIM_OC_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  561. {
  562. /* Check the parameters */
  563. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  564. switch (Channel)
  565. {
  566. case TIM_CHANNEL_1:
  567. {
  568. /* Enable the TIM Capture/Compare 1 interrupt */
  569. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  570. }
  571. break;
  572. case TIM_CHANNEL_2:
  573. {
  574. /* Enable the TIM Capture/Compare 2 interrupt */
  575. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  576. }
  577. break;
  578. case TIM_CHANNEL_3:
  579. {
  580. /* Enable the TIM Capture/Compare 3 interrupt */
  581. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
  582. }
  583. break;
  584. case TIM_CHANNEL_4:
  585. {
  586. /* Enable the TIM Capture/Compare 4 interrupt */
  587. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
  588. }
  589. break;
  590. default:
  591. break;
  592. }
  593. /* Enable the Output compare channel */
  594. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
  595. if(IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  596. {
  597. /* Enable the main output */
  598. __HAL_TIM_MOE_ENABLE(htim);
  599. }
  600. /* Enable the Peripheral */
  601. __HAL_TIM_ENABLE(htim);
  602. /* Return function status */
  603. return HAL_OK;
  604. }
  605. /**
  606. * @brief Stops the TIM Output Compare signal generation in interrupt mode.
  607. * @param htim TIM Output Compare handle
  608. * @param Channel TIM Channel to be disabled
  609. * This parameter can be one of the following values:
  610. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  611. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  612. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  613. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  614. * @arg TIM_CHANNEL_5: TIM Channel 5 selected
  615. * @arg TIM_CHANNEL_6: TIM Channel 6 selected
  616. * @retval HAL status
  617. */
  618. HAL_StatusTypeDef HAL_TIM_OC_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  619. {
  620. /* Check the parameters */
  621. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  622. switch (Channel)
  623. {
  624. case TIM_CHANNEL_1:
  625. {
  626. /* Disable the TIM Capture/Compare 1 interrupt */
  627. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  628. }
  629. break;
  630. case TIM_CHANNEL_2:
  631. {
  632. /* Disable the TIM Capture/Compare 2 interrupt */
  633. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  634. }
  635. break;
  636. case TIM_CHANNEL_3:
  637. {
  638. /* Disable the TIM Capture/Compare 3 interrupt */
  639. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
  640. }
  641. break;
  642. case TIM_CHANNEL_4:
  643. {
  644. /* Disable the TIM Capture/Compare 4 interrupt */
  645. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
  646. }
  647. break;
  648. default:
  649. break;
  650. }
  651. /* Disable the Output compare channel */
  652. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
  653. if(IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  654. {
  655. /* Disable the Main Ouput */
  656. __HAL_TIM_MOE_DISABLE(htim);
  657. }
  658. /* Disable the Peripheral */
  659. __HAL_TIM_DISABLE(htim);
  660. /* Return function status */
  661. return HAL_OK;
  662. }
  663. /**
  664. * @brief Starts the TIM Output Compare signal generation in DMA mode.
  665. * @param htim TIM Output Compare handle
  666. * @param Channel TIM Channel to be enabled
  667. * This parameter can be one of the following values:
  668. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  669. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  670. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  671. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  672. * @arg TIM_CHANNEL_5: TIM Channel 5 selected
  673. * @arg TIM_CHANNEL_6: TIM Channel 6 selected
  674. * @param pData The source Buffer address.
  675. * @param Length The length of data to be transferred from memory to TIM peripheral
  676. * @retval HAL status
  677. */
  678. HAL_StatusTypeDef HAL_TIM_OC_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)
  679. {
  680. /* Check the parameters */
  681. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  682. if((htim->State == HAL_TIM_STATE_BUSY))
  683. {
  684. return HAL_BUSY;
  685. }
  686. else if((htim->State == HAL_TIM_STATE_READY))
  687. {
  688. if(((uint32_t)pData == 0 ) && (Length > 0))
  689. {
  690. return HAL_ERROR;
  691. }
  692. else
  693. {
  694. htim->State = HAL_TIM_STATE_BUSY;
  695. }
  696. }
  697. switch (Channel)
  698. {
  699. case TIM_CHANNEL_1:
  700. {
  701. /* Set the DMA Period elapsed callback */
  702. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseCplt;
  703. /* Set the DMA error callback */
  704. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  705. /* Enable the DMA channel */
  706. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1, Length);
  707. /* Enable the TIM Capture/Compare 1 DMA request */
  708. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  709. }
  710. break;
  711. case TIM_CHANNEL_2:
  712. {
  713. /* Set the DMA Period elapsed callback */
  714. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseCplt;
  715. /* Set the DMA error callback */
  716. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
  717. /* Enable the DMA channel */
  718. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2, Length);
  719. /* Enable the TIM Capture/Compare 2 DMA request */
  720. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
  721. }
  722. break;
  723. case TIM_CHANNEL_3:
  724. {
  725. /* Set the DMA Period elapsed callback */
  726. htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseCplt;
  727. /* Set the DMA error callback */
  728. htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
  729. /* Enable the DMA channel */
  730. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3,Length);
  731. /* Enable the TIM Capture/Compare 3 DMA request */
  732. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
  733. }
  734. break;
  735. case TIM_CHANNEL_4:
  736. {
  737. /* Set the DMA Period elapsed callback */
  738. htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMADelayPulseCplt;
  739. /* Set the DMA error callback */
  740. htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
  741. /* Enable the DMA channel */
  742. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)pData, (uint32_t)&htim->Instance->CCR4, Length);
  743. /* Enable the TIM Capture/Compare 4 DMA request */
  744. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);
  745. }
  746. break;
  747. default:
  748. break;
  749. }
  750. /* Enable the Output compare channel */
  751. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
  752. if(IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  753. {
  754. /* Enable the main output */
  755. __HAL_TIM_MOE_ENABLE(htim);
  756. }
  757. /* Enable the Peripheral */
  758. __HAL_TIM_ENABLE(htim);
  759. /* Return function status */
  760. return HAL_OK;
  761. }
  762. /**
  763. * @brief Stops the TIM Output Compare signal generation in DMA mode.
  764. * @param htim TIM Output Compare handle
  765. * @param Channel TIM Channel to be disabled
  766. * This parameter can be one of the following values:
  767. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  768. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  769. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  770. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  771. * @arg TIM_CHANNEL_5: TIM Channel 5 selected
  772. * @arg TIM_CHANNEL_6: TIM Channel 6 selected
  773. * @retval HAL status
  774. */
  775. HAL_StatusTypeDef HAL_TIM_OC_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
  776. {
  777. /* Check the parameters */
  778. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  779. switch (Channel)
  780. {
  781. case TIM_CHANNEL_1:
  782. {
  783. /* Disable the TIM Capture/Compare 1 DMA request */
  784. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  785. }
  786. break;
  787. case TIM_CHANNEL_2:
  788. {
  789. /* Disable the TIM Capture/Compare 2 DMA request */
  790. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
  791. }
  792. break;
  793. case TIM_CHANNEL_3:
  794. {
  795. /* Disable the TIM Capture/Compare 3 DMA request */
  796. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
  797. }
  798. break;
  799. case TIM_CHANNEL_4:
  800. {
  801. /* Disable the TIM Capture/Compare 4 interrupt */
  802. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);
  803. }
  804. break;
  805. default:
  806. break;
  807. }
  808. /* Disable the Output compare channel */
  809. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
  810. if(IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  811. {
  812. /* Disable the Main Ouput */
  813. __HAL_TIM_MOE_DISABLE(htim);
  814. }
  815. /* Disable the Peripheral */
  816. __HAL_TIM_DISABLE(htim);
  817. /* Change the htim state */
  818. htim->State = HAL_TIM_STATE_READY;
  819. /* Return function status */
  820. return HAL_OK;
  821. }
  822. /**
  823. * @}
  824. */
  825. /** @defgroup TIM_Exported_Functions_Group3 Time PWM functions
  826. * @brief Time PWM functions
  827. *
  828. @verbatim
  829. ==============================================================================
  830. ##### Time PWM functions #####
  831. ==============================================================================
  832. [..]
  833. This section provides functions allowing to:
  834. (+) Initialize and configure the TIM OPWM.
  835. (+) De-initialize the TIM PWM.
  836. (+) Start the Time PWM.
  837. (+) Stop the Time PWM.
  838. (+) Start the Time PWM and enable interrupt.
  839. (+) Stop the Time PWM and disable interrupt.
  840. (+) Start the Time PWM and enable DMA transfer.
  841. (+) Stop the Time PWM and disable DMA transfer.
  842. @endverbatim
  843. * @{
  844. */
  845. /**
  846. * @brief Initializes the TIM PWM Time Base according to the specified
  847. * parameters in the TIM_HandleTypeDef and initialize the associated handle.
  848. * @note Switching from Center Aligned counter mode to Edge counter mode (or reverse)
  849. * requires a timer reset to avoid unexpected direction
  850. * due to DIR bit readonly in center aligned mode.
  851. * Ex: call @ref HAL_TIM_PWM_DeInit() before HAL_TIM_PWM_Init()
  852. * @param htim TIM handle
  853. * @retval HAL status
  854. */
  855. HAL_StatusTypeDef HAL_TIM_PWM_Init(TIM_HandleTypeDef *htim)
  856. {
  857. /* Check the TIM handle allocation */
  858. if(htim == NULL)
  859. {
  860. return HAL_ERROR;
  861. }
  862. /* Check the parameters */
  863. assert_param(IS_TIM_INSTANCE(htim->Instance));
  864. assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
  865. assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
  866. assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
  867. if(htim->State == HAL_TIM_STATE_RESET)
  868. {
  869. /* Allocate lock resource and initialize it */
  870. htim->Lock = HAL_UNLOCKED;
  871. /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
  872. HAL_TIM_PWM_MspInit(htim);
  873. }
  874. /* Set the TIM state */
  875. htim->State= HAL_TIM_STATE_BUSY;
  876. /* Init the base time for the PWM */
  877. TIM_Base_SetConfig(htim->Instance, &htim->Init);
  878. /* Initialize the TIM state*/
  879. htim->State= HAL_TIM_STATE_READY;
  880. return HAL_OK;
  881. }
  882. /**
  883. * @brief DeInitialize the TIM peripheral
  884. * @param htim TIM handle
  885. * @retval HAL status
  886. */
  887. HAL_StatusTypeDef HAL_TIM_PWM_DeInit(TIM_HandleTypeDef *htim)
  888. {
  889. /* Check the parameters */
  890. assert_param(IS_TIM_INSTANCE(htim->Instance));
  891. htim->State = HAL_TIM_STATE_BUSY;
  892. /* Disable the TIM Peripheral Clock */
  893. __HAL_TIM_DISABLE(htim);
  894. /* DeInit the low level hardware: GPIO, CLOCK, NVIC and DMA */
  895. HAL_TIM_PWM_MspDeInit(htim);
  896. /* Change TIM state */
  897. htim->State = HAL_TIM_STATE_RESET;
  898. /* Release Lock */
  899. __HAL_UNLOCK(htim);
  900. return HAL_OK;
  901. }
  902. /**
  903. * @brief Initializes the TIM PWM MSP.
  904. * @param htim TIM handle
  905. * @retval None
  906. */
  907. __weak void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef *htim)
  908. {
  909. /* Prevent unused argument(s) compilation warning */
  910. UNUSED(htim);
  911. /* NOTE : This function should not be modified, when the callback is needed,
  912. the HAL_TIM_PWM_MspInit could be implemented in the user file
  913. */
  914. }
  915. /**
  916. * @brief DeInitialize TIM PWM MSP.
  917. * @param htim TIM handle
  918. * @retval None
  919. */
  920. __weak void HAL_TIM_PWM_MspDeInit(TIM_HandleTypeDef *htim)
  921. {
  922. /* Prevent unused argument(s) compilation warning */
  923. UNUSED(htim);
  924. /* NOTE : This function should not be modified, when the callback is needed,
  925. the HAL_TIM_PWM_MspDeInit could be implemented in the user file
  926. */
  927. }
  928. /**
  929. * @brief Starts the PWM signal generation.
  930. * @param htim TIM handle
  931. * @param Channel TIM Channels to be enabled
  932. * This parameter can be one of the following values:
  933. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  934. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  935. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  936. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  937. * @arg TIM_CHANNEL_5: TIM Channel 5 selected
  938. * @arg TIM_CHANNEL_6: TIM Channel 6 selected
  939. * @retval HAL status
  940. */
  941. HAL_StatusTypeDef HAL_TIM_PWM_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
  942. {
  943. /* Check the parameters */
  944. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  945. /* Enable the Capture compare channel */
  946. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
  947. if(IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  948. {
  949. /* Enable the main output */
  950. __HAL_TIM_MOE_ENABLE(htim);
  951. }
  952. /* Enable the Peripheral */
  953. __HAL_TIM_ENABLE(htim);
  954. /* Return function status */
  955. return HAL_OK;
  956. }
  957. /**
  958. * @brief Stops the PWM signal generation.
  959. * @param htim TIM handle
  960. * @param Channel TIM Channels to be disabled
  961. * This parameter can be one of the following values:
  962. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  963. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  964. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  965. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  966. * @arg TIM_CHANNEL_5: TIM Channel 5 selected
  967. * @arg TIM_CHANNEL_6: TIM Channel 6 selected
  968. * @retval HAL status
  969. */
  970. HAL_StatusTypeDef HAL_TIM_PWM_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
  971. {
  972. /* Check the parameters */
  973. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  974. /* Disable the Capture compare channel */
  975. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
  976. if(IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  977. {
  978. /* Disable the Main Ouput */
  979. __HAL_TIM_MOE_DISABLE(htim);
  980. }
  981. /* Disable the Peripheral */
  982. __HAL_TIM_DISABLE(htim);
  983. /* Change the htim state */
  984. htim->State = HAL_TIM_STATE_READY;
  985. /* Return function status */
  986. return HAL_OK;
  987. }
  988. /**
  989. * @brief Starts the PWM signal generation in interrupt mode.
  990. * @param htim TIM handle
  991. * @param Channel TIM Channel to be enabled
  992. * This parameter can be one of the following values:
  993. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  994. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  995. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  996. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  997. * @retval HAL status
  998. */
  999. HAL_StatusTypeDef HAL_TIM_PWM_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  1000. {
  1001. /* Check the parameters */
  1002. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1003. switch (Channel)
  1004. {
  1005. case TIM_CHANNEL_1:
  1006. {
  1007. /* Enable the TIM Capture/Compare 1 interrupt */
  1008. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  1009. }
  1010. break;
  1011. case TIM_CHANNEL_2:
  1012. {
  1013. /* Enable the TIM Capture/Compare 2 interrupt */
  1014. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  1015. }
  1016. break;
  1017. case TIM_CHANNEL_3:
  1018. {
  1019. /* Enable the TIM Capture/Compare 3 interrupt */
  1020. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
  1021. }
  1022. break;
  1023. case TIM_CHANNEL_4:
  1024. {
  1025. /* Enable the TIM Capture/Compare 4 interrupt */
  1026. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
  1027. }
  1028. break;
  1029. default:
  1030. break;
  1031. }
  1032. /* Enable the Capture compare channel */
  1033. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
  1034. if(IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  1035. {
  1036. /* Enable the main output */
  1037. __HAL_TIM_MOE_ENABLE(htim);
  1038. }
  1039. /* Enable the Peripheral */
  1040. __HAL_TIM_ENABLE(htim);
  1041. /* Return function status */
  1042. return HAL_OK;
  1043. }
  1044. /**
  1045. * @brief Stops the PWM signal generation in interrupt mode.
  1046. * @param htim TIM handle
  1047. * @param Channel TIM Channels to be disabled
  1048. * This parameter can be one of the following values:
  1049. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1050. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1051. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1052. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1053. * @retval HAL status
  1054. */
  1055. HAL_StatusTypeDef HAL_TIM_PWM_Stop_IT (TIM_HandleTypeDef *htim, uint32_t Channel)
  1056. {
  1057. /* Check the parameters */
  1058. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1059. switch (Channel)
  1060. {
  1061. case TIM_CHANNEL_1:
  1062. {
  1063. /* Disable the TIM Capture/Compare 1 interrupt */
  1064. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  1065. }
  1066. break;
  1067. case TIM_CHANNEL_2:
  1068. {
  1069. /* Disable the TIM Capture/Compare 2 interrupt */
  1070. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  1071. }
  1072. break;
  1073. case TIM_CHANNEL_3:
  1074. {
  1075. /* Disable the TIM Capture/Compare 3 interrupt */
  1076. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
  1077. }
  1078. break;
  1079. case TIM_CHANNEL_4:
  1080. {
  1081. /* Disable the TIM Capture/Compare 4 interrupt */
  1082. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
  1083. }
  1084. break;
  1085. default:
  1086. break;
  1087. }
  1088. /* Disable the Capture compare channel */
  1089. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
  1090. if(IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  1091. {
  1092. /* Disable the Main Ouput */
  1093. __HAL_TIM_MOE_DISABLE(htim);
  1094. }
  1095. /* Disable the Peripheral */
  1096. __HAL_TIM_DISABLE(htim);
  1097. /* Return function status */
  1098. return HAL_OK;
  1099. }
  1100. /**
  1101. * @brief Starts the TIM PWM signal generation in DMA mode.
  1102. * @param htim TIM handle
  1103. * @param Channel TIM Channels to be enabled
  1104. * This parameter can be one of the following values:
  1105. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1106. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1107. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1108. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1109. * @param pData The source Buffer address.
  1110. * @param Length The length of data to be transferred from memory to TIM peripheral
  1111. * @retval HAL status
  1112. */
  1113. HAL_StatusTypeDef HAL_TIM_PWM_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)
  1114. {
  1115. /* Check the parameters */
  1116. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1117. if((htim->State == HAL_TIM_STATE_BUSY))
  1118. {
  1119. return HAL_BUSY;
  1120. }
  1121. else if((htim->State == HAL_TIM_STATE_READY))
  1122. {
  1123. if(((uint32_t)pData == 0 ) && (Length > 0))
  1124. {
  1125. return HAL_ERROR;
  1126. }
  1127. else
  1128. {
  1129. htim->State = HAL_TIM_STATE_BUSY;
  1130. }
  1131. }
  1132. switch (Channel)
  1133. {
  1134. case TIM_CHANNEL_1:
  1135. {
  1136. /* Set the DMA Period elapsed callback */
  1137. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseCplt;
  1138. /* Set the DMA error callback */
  1139. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  1140. /* Enable the DMA channel */
  1141. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1, Length);
  1142. /* Enable the TIM Capture/Compare 1 DMA request */
  1143. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  1144. }
  1145. break;
  1146. case TIM_CHANNEL_2:
  1147. {
  1148. /* Set the DMA Period elapsed callback */
  1149. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseCplt;
  1150. /* Set the DMA error callback */
  1151. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
  1152. /* Enable the DMA channel */
  1153. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2, Length);
  1154. /* Enable the TIM Capture/Compare 2 DMA request */
  1155. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
  1156. }
  1157. break;
  1158. case TIM_CHANNEL_3:
  1159. {
  1160. /* Set the DMA Period elapsed callback */
  1161. htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseCplt;
  1162. /* Set the DMA error callback */
  1163. htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
  1164. /* Enable the DMA channel */
  1165. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3,Length);
  1166. /* Enable the TIM Output Capture/Compare 3 request */
  1167. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
  1168. }
  1169. break;
  1170. case TIM_CHANNEL_4:
  1171. {
  1172. /* Set the DMA Period elapsed callback */
  1173. htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMADelayPulseCplt;
  1174. /* Set the DMA error callback */
  1175. htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
  1176. /* Enable the DMA channel */
  1177. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)pData, (uint32_t)&htim->Instance->CCR4, Length);
  1178. /* Enable the TIM Capture/Compare 4 DMA request */
  1179. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);
  1180. }
  1181. break;
  1182. default:
  1183. break;
  1184. }
  1185. /* Enable the Capture compare channel */
  1186. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
  1187. if(IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  1188. {
  1189. /* Enable the main output */
  1190. __HAL_TIM_MOE_ENABLE(htim);
  1191. }
  1192. /* Enable the Peripheral */
  1193. __HAL_TIM_ENABLE(htim);
  1194. /* Return function status */
  1195. return HAL_OK;
  1196. }
  1197. /**
  1198. * @brief Stops the TIM PWM signal generation in DMA mode.
  1199. * @param htim TIM handle
  1200. * @param Channel TIM Channels to be disabled
  1201. * This parameter can be one of the following values:
  1202. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1203. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1204. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1205. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1206. * @retval HAL status
  1207. */
  1208. HAL_StatusTypeDef HAL_TIM_PWM_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
  1209. {
  1210. /* Check the parameters */
  1211. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1212. switch (Channel)
  1213. {
  1214. case TIM_CHANNEL_1:
  1215. {
  1216. /* Disable the TIM Capture/Compare 1 DMA request */
  1217. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  1218. }
  1219. break;
  1220. case TIM_CHANNEL_2:
  1221. {
  1222. /* Disable the TIM Capture/Compare 2 DMA request */
  1223. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
  1224. }
  1225. break;
  1226. case TIM_CHANNEL_3:
  1227. {
  1228. /* Disable the TIM Capture/Compare 3 DMA request */
  1229. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
  1230. }
  1231. break;
  1232. case TIM_CHANNEL_4:
  1233. {
  1234. /* Disable the TIM Capture/Compare 4 interrupt */
  1235. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);
  1236. }
  1237. break;
  1238. default:
  1239. break;
  1240. }
  1241. /* Disable the Capture compare channel */
  1242. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
  1243. if(IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  1244. {
  1245. /* Disable the Main Ouput */
  1246. __HAL_TIM_MOE_DISABLE(htim);
  1247. }
  1248. /* Disable the Peripheral */
  1249. __HAL_TIM_DISABLE(htim);
  1250. /* Change the htim state */
  1251. htim->State = HAL_TIM_STATE_READY;
  1252. /* Return function status */
  1253. return HAL_OK;
  1254. }
  1255. /**
  1256. * @}
  1257. */
  1258. /** @defgroup TIM_Exported_Functions_Group4 Time Input Capture functions
  1259. * @brief Time Input Capture functions
  1260. *
  1261. @verbatim
  1262. ==============================================================================
  1263. ##### Time Input Capture functions #####
  1264. ==============================================================================
  1265. [..]
  1266. This section provides functions allowing to:
  1267. (+) Initialize and configure the TIM Input Capture.
  1268. (+) De-initialize the TIM Input Capture.
  1269. (+) Start the Time Input Capture.
  1270. (+) Stop the Time Input Capture.
  1271. (+) Start the Time Input Capture and enable interrupt.
  1272. (+) Stop the Time Input Capture and disable interrupt.
  1273. (+) Start the Time Input Capture and enable DMA transfer.
  1274. (+) Stop the Time Input Capture and disable DMA transfer.
  1275. @endverbatim
  1276. * @{
  1277. */
  1278. /**
  1279. * @brief Initializes the TIM Input Capture Time base according to the specified
  1280. * parameters in the TIM_HandleTypeDef and initialize the associated handle.
  1281. * @note Switching from Center Aligned counter mode to Edge counter mode (or reverse)
  1282. * requires a timer reset to avoid unexpected direction
  1283. * due to DIR bit readonly in center aligned mode.
  1284. * Ex: call @ref HAL_TIM_IC_DeInit() before HAL_TIM_IC_Init()
  1285. * @param htim TIM Input Capture handle
  1286. * @retval HAL status
  1287. */
  1288. HAL_StatusTypeDef HAL_TIM_IC_Init(TIM_HandleTypeDef *htim)
  1289. {
  1290. /* Check the TIM handle allocation */
  1291. if(htim == NULL)
  1292. {
  1293. return HAL_ERROR;
  1294. }
  1295. /* Check the parameters */
  1296. assert_param(IS_TIM_INSTANCE(htim->Instance));
  1297. assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
  1298. assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
  1299. assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
  1300. if(htim->State == HAL_TIM_STATE_RESET)
  1301. {
  1302. /* Allocate lock resource and initialize it */
  1303. htim->Lock = HAL_UNLOCKED;
  1304. /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
  1305. HAL_TIM_IC_MspInit(htim);
  1306. }
  1307. /* Set the TIM state */
  1308. htim->State= HAL_TIM_STATE_BUSY;
  1309. /* Init the base time for the input capture */
  1310. TIM_Base_SetConfig(htim->Instance, &htim->Init);
  1311. /* Initialize the TIM state*/
  1312. htim->State= HAL_TIM_STATE_READY;
  1313. return HAL_OK;
  1314. }
  1315. /**
  1316. * @brief DeInitialize the TIM peripheral
  1317. * @param htim TIM Input Capture handle
  1318. * @retval HAL status
  1319. */
  1320. HAL_StatusTypeDef HAL_TIM_IC_DeInit(TIM_HandleTypeDef *htim)
  1321. {
  1322. /* Check the parameters */
  1323. assert_param(IS_TIM_INSTANCE(htim->Instance));
  1324. htim->State = HAL_TIM_STATE_BUSY;
  1325. /* Disable the TIM Peripheral Clock */
  1326. __HAL_TIM_DISABLE(htim);
  1327. /* DeInit the low level hardware: GPIO, CLOCK, NVIC and DMA */
  1328. HAL_TIM_IC_MspDeInit(htim);
  1329. /* Change TIM state */
  1330. htim->State = HAL_TIM_STATE_RESET;
  1331. /* Release Lock */
  1332. __HAL_UNLOCK(htim);
  1333. return HAL_OK;
  1334. }
  1335. /**
  1336. * @brief Initializes the TIM INput Capture MSP.
  1337. * @param htim TIM handle
  1338. * @retval None
  1339. */
  1340. __weak void HAL_TIM_IC_MspInit(TIM_HandleTypeDef *htim)
  1341. {
  1342. /* Prevent unused argument(s) compilation warning */
  1343. UNUSED(htim);
  1344. /* NOTE : This function should not be modified, when the callback is needed,
  1345. the HAL_TIM_IC_MspInit could be implemented in the user file
  1346. */
  1347. }
  1348. /**
  1349. * @brief DeInitialize TIM Input Capture MSP.
  1350. * @param htim TIM handle
  1351. * @retval None
  1352. */
  1353. __weak void HAL_TIM_IC_MspDeInit(TIM_HandleTypeDef *htim)
  1354. {
  1355. /* Prevent unused argument(s) compilation warning */
  1356. UNUSED(htim);
  1357. /* NOTE : This function should not be modified, when the callback is needed,
  1358. the HAL_TIM_IC_MspDeInit could be implemented in the user file
  1359. */
  1360. }
  1361. /**
  1362. * @brief Starts the TIM Input Capture measurement.
  1363. * @param htim TIM Input Capture handle
  1364. * @param Channel TIM Channels to be enabled
  1365. * This parameter can be one of the following values:
  1366. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1367. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1368. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1369. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1370. * @retval HAL status
  1371. */
  1372. HAL_StatusTypeDef HAL_TIM_IC_Start (TIM_HandleTypeDef *htim, uint32_t Channel)
  1373. {
  1374. /* Check the parameters */
  1375. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1376. /* Enable the Input Capture channel */
  1377. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
  1378. /* Enable the Peripheral */
  1379. __HAL_TIM_ENABLE(htim);
  1380. /* Return function status */
  1381. return HAL_OK;
  1382. }
  1383. /**
  1384. * @brief Stops the TIM Input Capture measurement.
  1385. * @param htim TIM handle
  1386. * @param Channel TIM Channels to be disabled
  1387. * This parameter can be one of the following values:
  1388. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1389. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1390. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1391. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1392. * @retval HAL status
  1393. */
  1394. HAL_StatusTypeDef HAL_TIM_IC_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
  1395. {
  1396. /* Check the parameters */
  1397. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1398. /* Disable the Input Capture channel */
  1399. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
  1400. /* Disable the Peripheral */
  1401. __HAL_TIM_DISABLE(htim);
  1402. /* Return function status */
  1403. return HAL_OK;
  1404. }
  1405. /**
  1406. * @brief Starts the TIM Input Capture measurement in interrupt mode.
  1407. * @param htim TIM Input Capture handle
  1408. * @param Channel TIM Channels to be enabled
  1409. * This parameter can be one of the following values:
  1410. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1411. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1412. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1413. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1414. * @retval HAL status
  1415. */
  1416. HAL_StatusTypeDef HAL_TIM_IC_Start_IT (TIM_HandleTypeDef *htim, uint32_t Channel)
  1417. {
  1418. /* Check the parameters */
  1419. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1420. switch (Channel)
  1421. {
  1422. case TIM_CHANNEL_1:
  1423. {
  1424. /* Enable the TIM Capture/Compare 1 interrupt */
  1425. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  1426. }
  1427. break;
  1428. case TIM_CHANNEL_2:
  1429. {
  1430. /* Enable the TIM Capture/Compare 2 interrupt */
  1431. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  1432. }
  1433. break;
  1434. case TIM_CHANNEL_3:
  1435. {
  1436. /* Enable the TIM Capture/Compare 3 interrupt */
  1437. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
  1438. }
  1439. break;
  1440. case TIM_CHANNEL_4:
  1441. {
  1442. /* Enable the TIM Capture/Compare 4 interrupt */
  1443. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
  1444. }
  1445. break;
  1446. default:
  1447. break;
  1448. }
  1449. /* Enable the Input Capture channel */
  1450. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
  1451. /* Enable the Peripheral */
  1452. __HAL_TIM_ENABLE(htim);
  1453. /* Return function status */
  1454. return HAL_OK;
  1455. }
  1456. /**
  1457. * @brief Stops the TIM Input Capture measurement in interrupt mode.
  1458. * @param htim TIM handle
  1459. * @param Channel TIM Channels to be disabled
  1460. * This parameter can be one of the following values:
  1461. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1462. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1463. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1464. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1465. * @retval HAL status
  1466. */
  1467. HAL_StatusTypeDef HAL_TIM_IC_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  1468. {
  1469. /* Check the parameters */
  1470. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1471. switch (Channel)
  1472. {
  1473. case TIM_CHANNEL_1:
  1474. {
  1475. /* Disable the TIM Capture/Compare 1 interrupt */
  1476. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  1477. }
  1478. break;
  1479. case TIM_CHANNEL_2:
  1480. {
  1481. /* Disable the TIM Capture/Compare 2 interrupt */
  1482. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  1483. }
  1484. break;
  1485. case TIM_CHANNEL_3:
  1486. {
  1487. /* Disable the TIM Capture/Compare 3 interrupt */
  1488. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
  1489. }
  1490. break;
  1491. case TIM_CHANNEL_4:
  1492. {
  1493. /* Disable the TIM Capture/Compare 4 interrupt */
  1494. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
  1495. }
  1496. break;
  1497. default:
  1498. break;
  1499. }
  1500. /* Disable the Input Capture channel */
  1501. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
  1502. /* Disable the Peripheral */
  1503. __HAL_TIM_DISABLE(htim);
  1504. /* Return function status */
  1505. return HAL_OK;
  1506. }
  1507. /**
  1508. * @brief Starts the TIM Input Capture measurement on in DMA mode.
  1509. * @param htim TIM Input Capture handle
  1510. * @param Channel TIM Channels to be enabled
  1511. * This parameter can be one of the following values:
  1512. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1513. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1514. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1515. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1516. * @param pData The destination Buffer address.
  1517. * @param Length The length of data to be transferred from TIM peripheral to memory.
  1518. * @retval HAL status
  1519. */
  1520. HAL_StatusTypeDef HAL_TIM_IC_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)
  1521. {
  1522. /* Check the parameters */
  1523. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1524. assert_param(IS_TIM_DMA_CC_INSTANCE(htim->Instance));
  1525. if((htim->State == HAL_TIM_STATE_BUSY))
  1526. {
  1527. return HAL_BUSY;
  1528. }
  1529. else if((htim->State == HAL_TIM_STATE_READY))
  1530. {
  1531. if((pData == 0 ) && (Length > 0))
  1532. {
  1533. return HAL_ERROR;
  1534. }
  1535. else
  1536. {
  1537. htim->State = HAL_TIM_STATE_BUSY;
  1538. }
  1539. }
  1540. switch (Channel)
  1541. {
  1542. case TIM_CHANNEL_1:
  1543. {
  1544. /* Set the DMA Period elapsed callback */
  1545. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMACaptureCplt;
  1546. /* Set the DMA error callback */
  1547. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  1548. /* Enable the DMA channel */
  1549. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->CCR1, (uint32_t)pData, Length);
  1550. /* Enable the TIM Capture/Compare 1 DMA request */
  1551. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  1552. }
  1553. break;
  1554. case TIM_CHANNEL_2:
  1555. {
  1556. /* Set the DMA Period elapsed callback */
  1557. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMACaptureCplt;
  1558. /* Set the DMA error callback */
  1559. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
  1560. /* Enable the DMA channel */
  1561. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)&htim->Instance->CCR2, (uint32_t)pData, Length);
  1562. /* Enable the TIM Capture/Compare 2 DMA request */
  1563. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
  1564. }
  1565. break;
  1566. case TIM_CHANNEL_3:
  1567. {
  1568. /* Set the DMA Period elapsed callback */
  1569. htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMACaptureCplt;
  1570. /* Set the DMA error callback */
  1571. htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
  1572. /* Enable the DMA channel */
  1573. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)&htim->Instance->CCR3, (uint32_t)pData, Length);
  1574. /* Enable the TIM Capture/Compare 3 DMA request */
  1575. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
  1576. }
  1577. break;
  1578. case TIM_CHANNEL_4:
  1579. {
  1580. /* Set the DMA Period elapsed callback */
  1581. htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMACaptureCplt;
  1582. /* Set the DMA error callback */
  1583. htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
  1584. /* Enable the DMA channel */
  1585. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)&htim->Instance->CCR4, (uint32_t)pData, Length);
  1586. /* Enable the TIM Capture/Compare 4 DMA request */
  1587. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);
  1588. }
  1589. break;
  1590. default:
  1591. break;
  1592. }
  1593. /* Enable the Input Capture channel */
  1594. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
  1595. /* Enable the Peripheral */
  1596. __HAL_TIM_ENABLE(htim);
  1597. /* Return function status */
  1598. return HAL_OK;
  1599. }
  1600. /**
  1601. * @brief Stops the TIM Input Capture measurement in DMA mode.
  1602. * @param htim TIM Input Capture 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_IC_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
  1612. {
  1613. /* Check the parameters */
  1614. assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
  1615. assert_param(IS_TIM_DMA_CC_INSTANCE(htim->Instance));
  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. }
  1623. break;
  1624. case TIM_CHANNEL_2:
  1625. {
  1626. /* Disable the TIM Capture/Compare 2 DMA request */
  1627. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
  1628. }
  1629. break;
  1630. case TIM_CHANNEL_3:
  1631. {
  1632. /* Disable the TIM Capture/Compare 3 DMA request */
  1633. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
  1634. }
  1635. break;
  1636. case TIM_CHANNEL_4:
  1637. {
  1638. /* Disable the TIM Capture/Compare 4 DMA request */
  1639. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);
  1640. }
  1641. break;
  1642. default:
  1643. break;
  1644. }
  1645. /* Disable the Input Capture channel */
  1646. TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_DISABLE);
  1647. /* Disable the Peripheral */
  1648. __HAL_TIM_DISABLE(htim);
  1649. /* Change the htim state */
  1650. htim->State = HAL_TIM_STATE_READY;
  1651. /* Return function status */
  1652. return HAL_OK;
  1653. }
  1654. /**
  1655. * @}
  1656. */
  1657. /** @defgroup TIM_Exported_Functions_Group5 Time One Pulse functions
  1658. * @brief Time One Pulse functions
  1659. *
  1660. @verbatim
  1661. ==============================================================================
  1662. ##### Time One Pulse functions #####
  1663. ==============================================================================
  1664. [..]
  1665. This section provides functions allowing to:
  1666. (+) Initialize and configure the TIM One Pulse.
  1667. (+) De-initialize the TIM One Pulse.
  1668. (+) Start the Time One Pulse.
  1669. (+) Stop the Time One Pulse.
  1670. (+) Start the Time One Pulse and enable interrupt.
  1671. (+) Stop the Time One Pulse and disable interrupt.
  1672. (+) Start the Time One Pulse and enable DMA transfer.
  1673. (+) Stop the Time One Pulse and disable DMA transfer.
  1674. @endverbatim
  1675. * @{
  1676. */
  1677. /**
  1678. * @brief Initializes the TIM One Pulse Time Base according to the specified
  1679. * parameters in the TIM_HandleTypeDef and initialize the associated handle.
  1680. * @note Switching from Center Aligned counter mode to Edge counter mode (or reverse)
  1681. * requires a timer reset to avoid unexpected direction
  1682. * due to DIR bit readonly in center aligned mode.
  1683. * Ex: call @ref HAL_TIM_OnePulse_DeInit() before HAL_TIM_OnePulse_Init()
  1684. * @param htim TIM OnePulse handle
  1685. * @param OnePulseMode Select the One pulse mode.
  1686. * This parameter can be one of the following values:
  1687. * @arg TIM_OPMODE_SINGLE: Only one pulse will be generated.
  1688. * @arg TIM_OPMODE_REPETITIVE: Repetitive pulses will be generated.
  1689. * @retval HAL status
  1690. */
  1691. HAL_StatusTypeDef HAL_TIM_OnePulse_Init(TIM_HandleTypeDef *htim, uint32_t OnePulseMode)
  1692. {
  1693. /* Check the TIM handle allocation */
  1694. if(htim == NULL)
  1695. {
  1696. return HAL_ERROR;
  1697. }
  1698. /* Check the parameters */
  1699. assert_param(IS_TIM_INSTANCE(htim->Instance));
  1700. assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
  1701. assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
  1702. assert_param(IS_TIM_OPM_MODE(OnePulseMode));
  1703. assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
  1704. if(htim->State == HAL_TIM_STATE_RESET)
  1705. {
  1706. /* Allocate lock resource and initialize it */
  1707. htim->Lock = HAL_UNLOCKED;
  1708. /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
  1709. HAL_TIM_OnePulse_MspInit(htim);
  1710. }
  1711. /* Set the TIM state */
  1712. htim->State= HAL_TIM_STATE_BUSY;
  1713. /* Configure the Time base in the One Pulse Mode */
  1714. TIM_Base_SetConfig(htim->Instance, &htim->Init);
  1715. /* Reset the OPM Bit */
  1716. htim->Instance->CR1 &= ~TIM_CR1_OPM;
  1717. /* Configure the OPM Mode */
  1718. htim->Instance->CR1 |= OnePulseMode;
  1719. /* Initialize the TIM state*/
  1720. htim->State= HAL_TIM_STATE_READY;
  1721. return HAL_OK;
  1722. }
  1723. /**
  1724. * @brief DeInitialize the TIM One Pulse
  1725. * @param htim TIM One Pulse handle
  1726. * @retval HAL status
  1727. */
  1728. HAL_StatusTypeDef HAL_TIM_OnePulse_DeInit(TIM_HandleTypeDef *htim)
  1729. {
  1730. /* Check the parameters */
  1731. assert_param(IS_TIM_INSTANCE(htim->Instance));
  1732. htim->State = HAL_TIM_STATE_BUSY;
  1733. /* Disable the TIM Peripheral Clock */
  1734. __HAL_TIM_DISABLE(htim);
  1735. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  1736. HAL_TIM_OnePulse_MspDeInit(htim);
  1737. /* Change TIM state */
  1738. htim->State = HAL_TIM_STATE_RESET;
  1739. /* Release Lock */
  1740. __HAL_UNLOCK(htim);
  1741. return HAL_OK;
  1742. }
  1743. /**
  1744. * @brief Initializes the TIM One Pulse MSP.
  1745. * @param htim TIM handle
  1746. * @retval None
  1747. */
  1748. __weak void HAL_TIM_OnePulse_MspInit(TIM_HandleTypeDef *htim)
  1749. {
  1750. /* Prevent unused argument(s) compilation warning */
  1751. UNUSED(htim);
  1752. /* NOTE : This function should not be modified, when the callback is needed,
  1753. the HAL_TIM_OnePulse_MspInit could be implemented in the user file
  1754. */
  1755. }
  1756. /**
  1757. * @brief DeInitialize TIM One Pulse MSP.
  1758. * @param htim TIM handle
  1759. * @retval None
  1760. */
  1761. __weak void HAL_TIM_OnePulse_MspDeInit(TIM_HandleTypeDef *htim)
  1762. {
  1763. /* Prevent unused argument(s) compilation warning */
  1764. UNUSED(htim);
  1765. /* NOTE : This function should not be modified, when the callback is needed,
  1766. the HAL_TIM_OnePulse_MspDeInit could be implemented in the user file
  1767. */
  1768. }
  1769. /**
  1770. * @brief Starts the TIM One Pulse signal generation.
  1771. * @param htim TIM One Pulse handle
  1772. * @param OutputChannel TIM Channels to be enabled
  1773. * This parameter can be one of the following values:
  1774. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1775. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1776. * @retval HAL status
  1777. */
  1778. HAL_StatusTypeDef HAL_TIM_OnePulse_Start(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1779. {
  1780. /* Prevent unused argument(s) compilation warning */
  1781. UNUSED(OutputChannel);
  1782. /* Enable the Capture compare and the Input Capture channels
  1783. (in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)
  1784. if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and
  1785. if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output
  1786. in all combinations, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be enabled together
  1787. No need to enable the counter, it's enabled automatically by hardware
  1788. (the counter starts in response to a stimulus and generate a pulse */
  1789. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  1790. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
  1791. if(IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  1792. {
  1793. /* Enable the main output */
  1794. __HAL_TIM_MOE_ENABLE(htim);
  1795. }
  1796. /* Return function status */
  1797. return HAL_OK;
  1798. }
  1799. /**
  1800. * @brief Stops the TIM One Pulse signal generation.
  1801. * @param htim TIM One Pulse handle
  1802. * @param OutputChannel TIM Channels to be disable
  1803. * This parameter can be one of the following values:
  1804. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1805. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1806. * @retval HAL status
  1807. */
  1808. HAL_StatusTypeDef HAL_TIM_OnePulse_Stop(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1809. {
  1810. /* Prevent unused argument(s) compilation warning */
  1811. UNUSED(OutputChannel);
  1812. /* Disable the Capture compare and the Input Capture channels
  1813. (in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)
  1814. if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and
  1815. if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output
  1816. in all combinations, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be disabled together */
  1817. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  1818. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
  1819. if(IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  1820. {
  1821. /* Disable the Main Ouput */
  1822. __HAL_TIM_MOE_DISABLE(htim);
  1823. }
  1824. /* Disable the Peripheral */
  1825. __HAL_TIM_DISABLE(htim);
  1826. /* Return function status */
  1827. return HAL_OK;
  1828. }
  1829. /**
  1830. * @brief Starts the TIM One Pulse signal generation in interrupt mode.
  1831. * @param htim TIM One Pulse handle
  1832. * @param OutputChannel TIM Channels to be enabled
  1833. * This parameter can be one of the following values:
  1834. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1835. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1836. * @retval HAL status
  1837. */
  1838. HAL_StatusTypeDef HAL_TIM_OnePulse_Start_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1839. {
  1840. /* Prevent unused argument(s) compilation warning */
  1841. UNUSED(OutputChannel);
  1842. /* Enable the Capture compare and the Input Capture channels
  1843. (in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)
  1844. if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and
  1845. if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output
  1846. in all combinations, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be enabled together
  1847. No need to enable the counter, it's enabled automatically by hardware
  1848. (the counter starts in response to a stimulus and generate a pulse */
  1849. /* Enable the TIM Capture/Compare 1 interrupt */
  1850. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  1851. /* Enable the TIM Capture/Compare 2 interrupt */
  1852. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  1853. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  1854. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
  1855. if(IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  1856. {
  1857. /* Enable the main output */
  1858. __HAL_TIM_MOE_ENABLE(htim);
  1859. }
  1860. /* Return function status */
  1861. return HAL_OK;
  1862. }
  1863. /**
  1864. * @brief Stops the TIM One Pulse signal generation in interrupt mode.
  1865. * @param htim TIM One Pulse handle
  1866. * @param OutputChannel TIM Channels to be enabled
  1867. * This parameter can be one of the following values:
  1868. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1869. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1870. * @retval HAL status
  1871. */
  1872. HAL_StatusTypeDef HAL_TIM_OnePulse_Stop_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1873. {
  1874. /* Prevent unused argument(s) compilation warning */
  1875. UNUSED(OutputChannel);
  1876. /* Disable the TIM Capture/Compare 1 interrupt */
  1877. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  1878. /* Disable the TIM Capture/Compare 2 interrupt */
  1879. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  1880. /* Disable the Capture compare and the Input Capture channels
  1881. (in the OPM Mode the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2)
  1882. if TIM_CHANNEL_1 is used as output, the TIM_CHANNEL_2 will be used as input and
  1883. if TIM_CHANNEL_1 is used as input, the TIM_CHANNEL_2 will be used as output
  1884. in all combinations, the TIM_CHANNEL_1 and TIM_CHANNEL_2 should be disabled together */
  1885. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  1886. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
  1887. if(IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
  1888. {
  1889. /* Disable the Main Ouput */
  1890. __HAL_TIM_MOE_DISABLE(htim);
  1891. }
  1892. /* Disable the Peripheral */
  1893. __HAL_TIM_DISABLE(htim);
  1894. /* Return function status */
  1895. return HAL_OK;
  1896. }
  1897. /**
  1898. * @}
  1899. */
  1900. /** @defgroup TIM_Exported_Functions_Group6 Time Encoder functions
  1901. * @brief Time Encoder functions
  1902. *
  1903. @verbatim
  1904. ==============================================================================
  1905. ##### Time Encoder functions #####
  1906. ==============================================================================
  1907. [..]
  1908. This section provides functions allowing to:
  1909. (+) Initialize and configure the TIM Encoder.
  1910. (+) De-initialize the TIM Encoder.
  1911. (+) Start the Time Encoder.
  1912. (+) Stop the Time Encoder.
  1913. (+) Start the Time Encoder and enable interrupt.
  1914. (+) Stop the Time Encoder and disable interrupt.
  1915. (+) Start the Time Encoder and enable DMA transfer.
  1916. (+) Stop the Time Encoder and disable DMA transfer.
  1917. @endverbatim
  1918. * @{
  1919. */
  1920. /**
  1921. * @brief Initializes the TIM Encoder Interface and initialize the associated handle.
  1922. * @note Switching from Center Aligned counter mode to Edge counter mode (or reverse)
  1923. * requires a timer reset to avoid unexpected direction
  1924. * due to DIR bit readonly in center aligned mode.
  1925. * Ex: call @ref HAL_TIM_Encoder_DeInit() before HAL_TIM_Encoder_Init()
  1926. * @param htim TIM Encoder Interface handle
  1927. * @param sConfig TIM Encoder Interface configuration structure
  1928. * @retval HAL status
  1929. */
  1930. HAL_StatusTypeDef HAL_TIM_Encoder_Init(TIM_HandleTypeDef *htim, TIM_Encoder_InitTypeDef* sConfig)
  1931. {
  1932. uint32_t tmpsmcr = 0;
  1933. uint32_t tmpccmr1 = 0;
  1934. uint32_t tmpccer = 0;
  1935. /* Check the TIM handle allocation */
  1936. if(htim == NULL)
  1937. {
  1938. return HAL_ERROR;
  1939. }
  1940. /* Check the parameters */
  1941. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  1942. assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
  1943. assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
  1944. assert_param(IS_TIM_AUTORELOAD_PRELOAD(htim->Init.AutoReloadPreload));
  1945. assert_param(IS_TIM_ENCODER_MODE(sConfig->EncoderMode));
  1946. assert_param(IS_TIM_IC_SELECTION(sConfig->IC1Selection));
  1947. assert_param(IS_TIM_IC_SELECTION(sConfig->IC2Selection));
  1948. assert_param(IS_TIM_IC_POLARITY(sConfig->IC1Polarity));
  1949. assert_param(IS_TIM_IC_POLARITY(sConfig->IC2Polarity));
  1950. assert_param(IS_TIM_IC_PRESCALER(sConfig->IC1Prescaler));
  1951. assert_param(IS_TIM_IC_PRESCALER(sConfig->IC2Prescaler));
  1952. assert_param(IS_TIM_IC_FILTER(sConfig->IC1Filter));
  1953. assert_param(IS_TIM_IC_FILTER(sConfig->IC2Filter));
  1954. if(htim->State == HAL_TIM_STATE_RESET)
  1955. {
  1956. /* Allocate lock resource and initialize it */
  1957. htim->Lock = HAL_UNLOCKED;
  1958. /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
  1959. HAL_TIM_Encoder_MspInit(htim);
  1960. }
  1961. /* Set the TIM state */
  1962. htim->State= HAL_TIM_STATE_BUSY;
  1963. /* Reset the SMS bits */
  1964. htim->Instance->SMCR &= ~TIM_SMCR_SMS;
  1965. /* Configure the Time base in the Encoder Mode */
  1966. TIM_Base_SetConfig(htim->Instance, &htim->Init);
  1967. /* Get the TIMx SMCR register value */
  1968. tmpsmcr = htim->Instance->SMCR;
  1969. /* Get the TIMx CCMR1 register value */
  1970. tmpccmr1 = htim->Instance->CCMR1;
  1971. /* Get the TIMx CCER register value */
  1972. tmpccer = htim->Instance->CCER;
  1973. /* Set the encoder Mode */
  1974. tmpsmcr |= sConfig->EncoderMode;
  1975. /* Select the Capture Compare 1 and the Capture Compare 2 as input */
  1976. tmpccmr1 &= ~(TIM_CCMR1_CC1S | TIM_CCMR1_CC2S);
  1977. tmpccmr1 |= (sConfig->IC1Selection | (sConfig->IC2Selection << 8));
  1978. /* Set the Capture Compare 1 and the Capture Compare 2 prescalers and filters */
  1979. tmpccmr1 &= ~(TIM_CCMR1_IC1PSC | TIM_CCMR1_IC2PSC);
  1980. tmpccmr1 &= ~(TIM_CCMR1_IC1F | TIM_CCMR1_IC2F);
  1981. tmpccmr1 |= sConfig->IC1Prescaler | (sConfig->IC2Prescaler << 8);
  1982. tmpccmr1 |= (sConfig->IC1Filter << 4) | (sConfig->IC2Filter << 12);
  1983. /* Set the TI1 and the TI2 Polarities */
  1984. tmpccer &= ~(TIM_CCER_CC1P | TIM_CCER_CC2P);
  1985. tmpccer &= ~(TIM_CCER_CC1NP | TIM_CCER_CC2NP);
  1986. tmpccer |= sConfig->IC1Polarity | (sConfig->IC2Polarity << 4);
  1987. /* Write to TIMx SMCR */
  1988. htim->Instance->SMCR = tmpsmcr;
  1989. /* Write to TIMx CCMR1 */
  1990. htim->Instance->CCMR1 = tmpccmr1;
  1991. /* Write to TIMx CCER */
  1992. htim->Instance->CCER = tmpccer;
  1993. /* Initialize the TIM state*/
  1994. htim->State= HAL_TIM_STATE_READY;
  1995. return HAL_OK;
  1996. }
  1997. /**
  1998. * @brief DeInitialize the TIM Encoder interface
  1999. * @param htim TIM Encoder handle
  2000. * @retval HAL status
  2001. */
  2002. HAL_StatusTypeDef HAL_TIM_Encoder_DeInit(TIM_HandleTypeDef *htim)
  2003. {
  2004. /* Check the parameters */
  2005. assert_param(IS_TIM_INSTANCE(htim->Instance));
  2006. htim->State = HAL_TIM_STATE_BUSY;
  2007. /* Disable the TIM Peripheral Clock */
  2008. __HAL_TIM_DISABLE(htim);
  2009. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  2010. HAL_TIM_Encoder_MspDeInit(htim);
  2011. /* Change TIM state */
  2012. htim->State = HAL_TIM_STATE_RESET;
  2013. /* Release Lock */
  2014. __HAL_UNLOCK(htim);
  2015. return HAL_OK;
  2016. }
  2017. /**
  2018. * @brief Initializes the TIM Encoder Interface MSP.
  2019. * @param htim TIM handle
  2020. * @retval None
  2021. */
  2022. __weak void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef *htim)
  2023. {
  2024. /* Prevent unused argument(s) compilation warning */
  2025. UNUSED(htim);
  2026. /* NOTE : This function should not be modified, when the callback is needed,
  2027. the HAL_TIM_Encoder_MspInit could be implemented in the user file
  2028. */
  2029. }
  2030. /**
  2031. * @brief DeInitialize TIM Encoder Interface MSP.
  2032. * @param htim TIM handle
  2033. * @retval None
  2034. */
  2035. __weak void HAL_TIM_Encoder_MspDeInit(TIM_HandleTypeDef *htim)
  2036. {
  2037. /* Prevent unused argument(s) compilation warning */
  2038. UNUSED(htim);
  2039. /* NOTE : This function should not be modified, when the callback is needed,
  2040. the HAL_TIM_Encoder_MspDeInit could be implemented in the user file
  2041. */
  2042. }
  2043. /**
  2044. * @brief Starts the TIM Encoder Interface.
  2045. * @param htim TIM Encoder Interface handle
  2046. * @param Channel TIM Channels to be enabled
  2047. * This parameter can be one of the following values:
  2048. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  2049. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  2050. * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
  2051. * @retval HAL status
  2052. */
  2053. HAL_StatusTypeDef HAL_TIM_Encoder_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
  2054. {
  2055. /* Check the parameters */
  2056. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  2057. /* Enable the encoder interface channels */
  2058. switch (Channel)
  2059. {
  2060. case TIM_CHANNEL_1:
  2061. {
  2062. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  2063. }
  2064. break;
  2065. case TIM_CHANNEL_2:
  2066. {
  2067. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
  2068. }
  2069. break;
  2070. default :
  2071. {
  2072. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  2073. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
  2074. }
  2075. break;
  2076. }
  2077. /* Enable the Peripheral */
  2078. __HAL_TIM_ENABLE(htim);
  2079. /* Return function status */
  2080. return HAL_OK;
  2081. }
  2082. /**
  2083. * @brief Stops the TIM Encoder Interface.
  2084. * @param htim TIM Encoder Interface handle
  2085. * @param Channel TIM Channels to be disabled
  2086. * This parameter can be one of the following values:
  2087. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  2088. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  2089. * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
  2090. * @retval HAL status
  2091. */
  2092. HAL_StatusTypeDef HAL_TIM_Encoder_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
  2093. {
  2094. /* Check the parameters */
  2095. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  2096. /* Disable the Input Capture channels 1 and 2
  2097. (in the EncoderInterface the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2) */
  2098. switch (Channel)
  2099. {
  2100. case TIM_CHANNEL_1:
  2101. {
  2102. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  2103. }
  2104. break;
  2105. case TIM_CHANNEL_2:
  2106. {
  2107. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
  2108. }
  2109. break;
  2110. default :
  2111. {
  2112. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  2113. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
  2114. }
  2115. break;
  2116. }
  2117. /* Disable the Peripheral */
  2118. __HAL_TIM_DISABLE(htim);
  2119. /* Return function status */
  2120. return HAL_OK;
  2121. }
  2122. /**
  2123. * @brief Starts the TIM Encoder Interface in interrupt mode.
  2124. * @param htim TIM Encoder Interface handle
  2125. * @param Channel TIM Channels to be enabled
  2126. * This parameter can be one of the following values:
  2127. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  2128. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  2129. * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
  2130. * @retval HAL status
  2131. */
  2132. HAL_StatusTypeDef HAL_TIM_Encoder_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  2133. {
  2134. /* Check the parameters */
  2135. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  2136. /* Enable the encoder interface channels */
  2137. /* Enable the capture compare Interrupts 1 and/or 2 */
  2138. switch (Channel)
  2139. {
  2140. case TIM_CHANNEL_1:
  2141. {
  2142. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  2143. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  2144. }
  2145. break;
  2146. case TIM_CHANNEL_2:
  2147. {
  2148. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
  2149. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  2150. }
  2151. break;
  2152. default :
  2153. {
  2154. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  2155. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
  2156. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  2157. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  2158. }
  2159. break;
  2160. }
  2161. /* Enable the Peripheral */
  2162. __HAL_TIM_ENABLE(htim);
  2163. /* Return function status */
  2164. return HAL_OK;
  2165. }
  2166. /**
  2167. * @brief Stops the TIM Encoder Interface in interrupt mode.
  2168. * @param htim TIM Encoder Interface handle
  2169. * @param Channel TIM Channels to be disabled
  2170. * This parameter can be one of the following values:
  2171. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  2172. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  2173. * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
  2174. * @retval HAL status
  2175. */
  2176. HAL_StatusTypeDef HAL_TIM_Encoder_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  2177. {
  2178. /* Check the parameters */
  2179. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  2180. /* Disable the Input Capture channels 1 and 2
  2181. (in the EncoderInterface the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2) */
  2182. if(Channel == TIM_CHANNEL_1)
  2183. {
  2184. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  2185. /* Disable the capture compare Interrupts 1 */
  2186. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  2187. }
  2188. else if(Channel == TIM_CHANNEL_2)
  2189. {
  2190. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
  2191. /* Disable the capture compare Interrupts 2 */
  2192. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  2193. }
  2194. else
  2195. {
  2196. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  2197. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
  2198. /* Disable the capture compare Interrupts 1 and 2 */
  2199. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  2200. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  2201. }
  2202. /* Disable the Peripheral */
  2203. __HAL_TIM_DISABLE(htim);
  2204. /* Change the htim state */
  2205. htim->State = HAL_TIM_STATE_READY;
  2206. /* Return function status */
  2207. return HAL_OK;
  2208. }
  2209. /**
  2210. * @brief Starts the TIM Encoder Interface in DMA mode.
  2211. * @param htim TIM Encoder Interface handle
  2212. * @param Channel TIM Channels to be enabled
  2213. * This parameter can be one of the following values:
  2214. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  2215. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  2216. * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
  2217. * @param pData1 The destination Buffer address for IC1.
  2218. * @param pData2 The destination Buffer address for IC2.
  2219. * @param Length The length of data to be transferred from TIM peripheral to memory.
  2220. * @retval HAL status
  2221. */
  2222. HAL_StatusTypeDef HAL_TIM_Encoder_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData1, uint32_t *pData2, uint16_t Length)
  2223. {
  2224. /* Check the parameters */
  2225. assert_param(IS_TIM_DMA_CC_INSTANCE(htim->Instance));
  2226. if((htim->State == HAL_TIM_STATE_BUSY))
  2227. {
  2228. return HAL_BUSY;
  2229. }
  2230. else if((htim->State == HAL_TIM_STATE_READY))
  2231. {
  2232. if((((pData1 == 0) || (pData2 == 0) )) && (Length > 0))
  2233. {
  2234. return HAL_ERROR;
  2235. }
  2236. else
  2237. {
  2238. htim->State = HAL_TIM_STATE_BUSY;
  2239. }
  2240. }
  2241. switch (Channel)
  2242. {
  2243. case TIM_CHANNEL_1:
  2244. {
  2245. /* Set the DMA Period elapsed callback */
  2246. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMACaptureCplt;
  2247. /* Set the DMA error callback */
  2248. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  2249. /* Enable the DMA channel */
  2250. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->CCR1, (uint32_t )pData1, Length);
  2251. /* Enable the TIM Input Capture DMA request */
  2252. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  2253. /* Enable the Peripheral */
  2254. __HAL_TIM_ENABLE(htim);
  2255. /* Enable the Capture compare channel */
  2256. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  2257. }
  2258. break;
  2259. case TIM_CHANNEL_2:
  2260. {
  2261. /* Set the DMA Period elapsed callback */
  2262. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMACaptureCplt;
  2263. /* Set the DMA error callback */
  2264. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError;
  2265. /* Enable the DMA channel */
  2266. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)&htim->Instance->CCR2, (uint32_t)pData2, Length);
  2267. /* Enable the TIM Input Capture DMA request */
  2268. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
  2269. /* Enable the Peripheral */
  2270. __HAL_TIM_ENABLE(htim);
  2271. /* Enable the Capture compare channel */
  2272. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
  2273. }
  2274. break;
  2275. case TIM_CHANNEL_ALL:
  2276. {
  2277. /* Set the DMA Period elapsed callback */
  2278. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMACaptureCplt;
  2279. /* Set the DMA error callback */
  2280. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  2281. /* Enable the DMA channel */
  2282. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->CCR1, (uint32_t)pData1, Length);
  2283. /* Set the DMA Period elapsed callback */
  2284. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMACaptureCplt;
  2285. /* Set the DMA error callback */
  2286. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
  2287. /* Enable the DMA channel */
  2288. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)&htim->Instance->CCR2, (uint32_t)pData2, Length);
  2289. /* Enable the Peripheral */
  2290. __HAL_TIM_ENABLE(htim);
  2291. /* Enable the Capture compare channel */
  2292. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  2293. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_ENABLE);
  2294. /* Enable the TIM Input Capture DMA request */
  2295. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  2296. /* Enable the TIM Input Capture DMA request */
  2297. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
  2298. }
  2299. break;
  2300. default:
  2301. break;
  2302. }
  2303. /* Return function status */
  2304. return HAL_OK;
  2305. }
  2306. /**
  2307. * @brief Stops the TIM Encoder Interface in DMA mode.
  2308. * @param htim TIM Encoder Interface handle
  2309. * @param Channel TIM Channels to be enabled
  2310. * This parameter can be one of the following values:
  2311. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  2312. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  2313. * @arg TIM_CHANNEL_ALL: TIM Channel 1 and TIM Channel 2 are selected
  2314. * @retval HAL status
  2315. */
  2316. HAL_StatusTypeDef HAL_TIM_Encoder_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
  2317. {
  2318. /* Check the parameters */
  2319. assert_param(IS_TIM_DMA_CC_INSTANCE(htim->Instance));
  2320. /* Disable the Input Capture channels 1 and 2
  2321. (in the EncoderInterface the two possible channels that can be used are TIM_CHANNEL_1 and TIM_CHANNEL_2) */
  2322. if(Channel == TIM_CHANNEL_1)
  2323. {
  2324. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  2325. /* Disable the capture compare DMA Request 1 */
  2326. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  2327. }
  2328. else if(Channel == TIM_CHANNEL_2)
  2329. {
  2330. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
  2331. /* Disable the capture compare DMA Request 2 */
  2332. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
  2333. }
  2334. else
  2335. {
  2336. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  2337. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_2, TIM_CCx_DISABLE);
  2338. /* Disable the capture compare DMA Request 1 and 2 */
  2339. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  2340. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
  2341. }
  2342. /* Disable the Peripheral */
  2343. __HAL_TIM_DISABLE(htim);
  2344. /* Change the htim state */
  2345. htim->State = HAL_TIM_STATE_READY;
  2346. /* Return function status */
  2347. return HAL_OK;
  2348. }
  2349. /**
  2350. * @}
  2351. */
  2352. /** @defgroup TIM_Exported_Functions_Group7 TIM IRQ handler management
  2353. * @brief IRQ handler management
  2354. *
  2355. @verbatim
  2356. ==============================================================================
  2357. ##### IRQ handler management #####
  2358. ==============================================================================
  2359. [..]
  2360. This section provides Timer IRQ handler function.
  2361. @endverbatim
  2362. * @{
  2363. */
  2364. /**
  2365. * @brief This function handles TIM interrupts requests.
  2366. * @param htim TIM handle
  2367. * @retval None
  2368. */
  2369. void HAL_TIM_IRQHandler(TIM_HandleTypeDef *htim)
  2370. {
  2371. /* Capture compare 1 event */
  2372. if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC1) != RESET)
  2373. {
  2374. if(__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC1) !=RESET)
  2375. {
  2376. {
  2377. __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC1);
  2378. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
  2379. /* Input capture event */
  2380. if((htim->Instance->CCMR1 & TIM_CCMR1_CC1S) != 0x00)
  2381. {
  2382. HAL_TIM_IC_CaptureCallback(htim);
  2383. }
  2384. /* Output compare event */
  2385. else
  2386. {
  2387. HAL_TIM_OC_DelayElapsedCallback(htim);
  2388. HAL_TIM_PWM_PulseFinishedCallback(htim);
  2389. }
  2390. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  2391. }
  2392. }
  2393. }
  2394. /* Capture compare 2 event */
  2395. if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC2) != RESET)
  2396. {
  2397. if(__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC2) !=RESET)
  2398. {
  2399. __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC2);
  2400. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
  2401. /* Input capture event */
  2402. if((htim->Instance->CCMR1 & TIM_CCMR1_CC2S) != 0x00)
  2403. {
  2404. HAL_TIM_IC_CaptureCallback(htim);
  2405. }
  2406. /* Output compare event */
  2407. else
  2408. {
  2409. HAL_TIM_OC_DelayElapsedCallback(htim);
  2410. HAL_TIM_PWM_PulseFinishedCallback(htim);
  2411. }
  2412. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  2413. }
  2414. }
  2415. /* Capture compare 3 event */
  2416. if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC3) != RESET)
  2417. {
  2418. if(__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC3) !=RESET)
  2419. {
  2420. __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC3);
  2421. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
  2422. /* Input capture event */
  2423. if((htim->Instance->CCMR2 & TIM_CCMR2_CC3S) != 0x00)
  2424. {
  2425. HAL_TIM_IC_CaptureCallback(htim);
  2426. }
  2427. /* Output compare event */
  2428. else
  2429. {
  2430. HAL_TIM_OC_DelayElapsedCallback(htim);
  2431. HAL_TIM_PWM_PulseFinishedCallback(htim);
  2432. }
  2433. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  2434. }
  2435. }
  2436. /* Capture compare 4 event */
  2437. if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC4) != RESET)
  2438. {
  2439. if(__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC4) !=RESET)
  2440. {
  2441. __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC4);
  2442. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
  2443. /* Input capture event */
  2444. if((htim->Instance->CCMR2 & TIM_CCMR2_CC4S) != 0x00)
  2445. {
  2446. HAL_TIM_IC_CaptureCallback(htim);
  2447. }
  2448. /* Output compare event */
  2449. else
  2450. {
  2451. HAL_TIM_OC_DelayElapsedCallback(htim);
  2452. HAL_TIM_PWM_PulseFinishedCallback(htim);
  2453. }
  2454. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  2455. }
  2456. }
  2457. /* TIM Update event */
  2458. if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_UPDATE) != RESET)
  2459. {
  2460. if(__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_UPDATE) !=RESET)
  2461. {
  2462. __HAL_TIM_CLEAR_IT(htim, TIM_IT_UPDATE);
  2463. HAL_TIM_PeriodElapsedCallback(htim);
  2464. }
  2465. }
  2466. /* TIM Break input event */
  2467. if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_BREAK) != RESET)
  2468. {
  2469. if(__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_BREAK) !=RESET)
  2470. {
  2471. __HAL_TIM_CLEAR_IT(htim, TIM_IT_BREAK);
  2472. HAL_TIMEx_BreakCallback(htim);
  2473. }
  2474. }
  2475. /* TIM Trigger detection event */
  2476. if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_TRIGGER) != RESET)
  2477. {
  2478. if(__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_TRIGGER) !=RESET)
  2479. {
  2480. __HAL_TIM_CLEAR_IT(htim, TIM_IT_TRIGGER);
  2481. HAL_TIM_TriggerCallback(htim);
  2482. }
  2483. }
  2484. /* TIM commutation event */
  2485. if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_COM) != RESET)
  2486. {
  2487. if(__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_COM) !=RESET)
  2488. {
  2489. __HAL_TIM_CLEAR_IT(htim, TIM_FLAG_COM);
  2490. HAL_TIMEx_CommutationCallback(htim);
  2491. }
  2492. }
  2493. }
  2494. /**
  2495. * @}
  2496. */
  2497. /** @defgroup TIM_Exported_Functions_Group8 Peripheral Control functions
  2498. * @brief Peripheral Control functions
  2499. *
  2500. @verbatim
  2501. ==============================================================================
  2502. ##### Peripheral Control functions #####
  2503. ==============================================================================
  2504. [..]
  2505. This section provides functions allowing to:
  2506. (+) Configure The Input Output channels for OC, PWM, IC or One Pulse mode.
  2507. (+) Configure External Clock source.
  2508. (+) Configure Complementary channels, break features and dead time.
  2509. (+) Configure Master and the Slave synchronization.
  2510. (+) Configure the DMA Burst Mode.
  2511. @endverbatim
  2512. * @{
  2513. */
  2514. /**
  2515. * @brief Initializes the TIM Output Compare Channels according to the specified
  2516. * parameters in the TIM_OC_InitTypeDef.
  2517. * @param htim TIM Output Compare handle
  2518. * @param sConfig TIM Output Compare configuration structure
  2519. * @param Channel TIM Channels to configure
  2520. * This parameter can be one of the following values:
  2521. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  2522. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  2523. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  2524. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  2525. * @arg TIM_CHANNEL_5: TIM Channel 5 selected
  2526. * @arg TIM_CHANNEL_6: TIM Channel 6 selected
  2527. * @retval HAL status
  2528. */
  2529. HAL_StatusTypeDef HAL_TIM_OC_ConfigChannel(TIM_HandleTypeDef *htim,
  2530. TIM_OC_InitTypeDef* sConfig,
  2531. uint32_t Channel)
  2532. {
  2533. /* Check the parameters */
  2534. assert_param(IS_TIM_CHANNELS(Channel));
  2535. assert_param(IS_TIM_OC_MODE(sConfig->OCMode));
  2536. assert_param(IS_TIM_OC_POLARITY(sConfig->OCPolarity));
  2537. /* Process Locked */
  2538. __HAL_LOCK(htim);
  2539. htim->State = HAL_TIM_STATE_BUSY;
  2540. switch (Channel)
  2541. {
  2542. case TIM_CHANNEL_1:
  2543. {
  2544. /* Check the parameters */
  2545. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  2546. /* Configure the TIM Channel 1 in Output Compare */
  2547. TIM_OC1_SetConfig(htim->Instance, sConfig);
  2548. }
  2549. break;
  2550. case TIM_CHANNEL_2:
  2551. {
  2552. /* Check the parameters */
  2553. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  2554. /* Configure the TIM Channel 2 in Output Compare */
  2555. TIM_OC2_SetConfig(htim->Instance, sConfig);
  2556. }
  2557. break;
  2558. case TIM_CHANNEL_3:
  2559. {
  2560. /* Check the parameters */
  2561. assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
  2562. /* Configure the TIM Channel 3 in Output Compare */
  2563. TIM_OC3_SetConfig(htim->Instance, sConfig);
  2564. }
  2565. break;
  2566. case TIM_CHANNEL_4:
  2567. {
  2568. /* Check the parameters */
  2569. assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
  2570. /* Configure the TIM Channel 4 in Output Compare */
  2571. TIM_OC4_SetConfig(htim->Instance, sConfig);
  2572. }
  2573. break;
  2574. case TIM_CHANNEL_5:
  2575. {
  2576. /* Check the parameters */
  2577. assert_param(IS_TIM_CC5_INSTANCE(htim->Instance));
  2578. /* Configure the TIM Channel 5 in Output Compare */
  2579. TIM_OC5_SetConfig(htim->Instance, sConfig);
  2580. }
  2581. break;
  2582. case TIM_CHANNEL_6:
  2583. {
  2584. /* Check the parameters */
  2585. assert_param(IS_TIM_CC6_INSTANCE(htim->Instance));
  2586. /* Configure the TIM Channel 6 in Output Compare */
  2587. TIM_OC6_SetConfig(htim->Instance, sConfig);
  2588. }
  2589. break;
  2590. default:
  2591. break;
  2592. }
  2593. htim->State = HAL_TIM_STATE_READY;
  2594. __HAL_UNLOCK(htim);
  2595. return HAL_OK;
  2596. }
  2597. /**
  2598. * @brief Initializes the TIM Input Capture Channels according to the specified
  2599. * parameters in the TIM_IC_InitTypeDef.
  2600. * @param htim TIM IC handle
  2601. * @param sConfig TIM Input Capture configuration structure
  2602. * @param Channel TIM Channels to be enabled
  2603. * This parameter can be one of the following values:
  2604. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  2605. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  2606. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  2607. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  2608. * @retval HAL status
  2609. */
  2610. HAL_StatusTypeDef HAL_TIM_IC_ConfigChannel(TIM_HandleTypeDef *htim, TIM_IC_InitTypeDef* sConfig, uint32_t Channel)
  2611. {
  2612. /* Check the parameters */
  2613. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  2614. assert_param(IS_TIM_IC_POLARITY(sConfig->ICPolarity));
  2615. assert_param(IS_TIM_IC_SELECTION(sConfig->ICSelection));
  2616. assert_param(IS_TIM_IC_PRESCALER(sConfig->ICPrescaler));
  2617. assert_param(IS_TIM_IC_FILTER(sConfig->ICFilter));
  2618. /* Process Locked */
  2619. __HAL_LOCK(htim);
  2620. htim->State = HAL_TIM_STATE_BUSY;
  2621. if (Channel == TIM_CHANNEL_1)
  2622. {
  2623. /* TI1 Configuration */
  2624. TIM_TI1_SetConfig(htim->Instance,
  2625. sConfig->ICPolarity,
  2626. sConfig->ICSelection,
  2627. sConfig->ICFilter);
  2628. /* Reset the IC1PSC Bits */
  2629. htim->Instance->CCMR1 &= ~TIM_CCMR1_IC1PSC;
  2630. /* Set the IC1PSC value */
  2631. htim->Instance->CCMR1 |= sConfig->ICPrescaler;
  2632. }
  2633. else if (Channel == TIM_CHANNEL_2)
  2634. {
  2635. /* TI2 Configuration */
  2636. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  2637. TIM_TI2_SetConfig(htim->Instance,
  2638. sConfig->ICPolarity,
  2639. sConfig->ICSelection,
  2640. sConfig->ICFilter);
  2641. /* Reset the IC2PSC Bits */
  2642. htim->Instance->CCMR1 &= ~TIM_CCMR1_IC2PSC;
  2643. /* Set the IC2PSC value */
  2644. htim->Instance->CCMR1 |= (sConfig->ICPrescaler << 8);
  2645. }
  2646. else if (Channel == TIM_CHANNEL_3)
  2647. {
  2648. /* TI3 Configuration */
  2649. assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
  2650. TIM_TI3_SetConfig(htim->Instance,
  2651. sConfig->ICPolarity,
  2652. sConfig->ICSelection,
  2653. sConfig->ICFilter);
  2654. /* Reset the IC3PSC Bits */
  2655. htim->Instance->CCMR2 &= ~TIM_CCMR2_IC3PSC;
  2656. /* Set the IC3PSC value */
  2657. htim->Instance->CCMR2 |= sConfig->ICPrescaler;
  2658. }
  2659. else
  2660. {
  2661. /* TI4 Configuration */
  2662. assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
  2663. TIM_TI4_SetConfig(htim->Instance,
  2664. sConfig->ICPolarity,
  2665. sConfig->ICSelection,
  2666. sConfig->ICFilter);
  2667. /* Reset the IC4PSC Bits */
  2668. htim->Instance->CCMR2 &= ~TIM_CCMR2_IC4PSC;
  2669. /* Set the IC4PSC value */
  2670. htim->Instance->CCMR2 |= (sConfig->ICPrescaler << 8);
  2671. }
  2672. htim->State = HAL_TIM_STATE_READY;
  2673. __HAL_UNLOCK(htim);
  2674. return HAL_OK;
  2675. }
  2676. /**
  2677. * @brief Initializes the TIM PWM channels according to the specified
  2678. * parameters in the TIM_OC_InitTypeDef.
  2679. * @param htim TIM PWM handle
  2680. * @param sConfig TIM PWM configuration structure
  2681. * @param Channel TIM Channels to be configured
  2682. * This parameter can be one of the following values:
  2683. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  2684. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  2685. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  2686. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  2687. * @arg TIM_CHANNEL_5: TIM Channel 5 selected
  2688. * @arg TIM_CHANNEL_6: TIM Channel 6 selected
  2689. * @retval HAL status
  2690. */
  2691. HAL_StatusTypeDef HAL_TIM_PWM_ConfigChannel(TIM_HandleTypeDef *htim,
  2692. TIM_OC_InitTypeDef* sConfig,
  2693. uint32_t Channel)
  2694. {
  2695. /* Check the parameters */
  2696. assert_param(IS_TIM_CHANNELS(Channel));
  2697. assert_param(IS_TIM_PWM_MODE(sConfig->OCMode));
  2698. assert_param(IS_TIM_OC_POLARITY(sConfig->OCPolarity));
  2699. assert_param(IS_TIM_FAST_STATE(sConfig->OCFastMode));
  2700. /* Process Locked */
  2701. __HAL_LOCK(htim);
  2702. htim->State = HAL_TIM_STATE_BUSY;
  2703. switch (Channel)
  2704. {
  2705. case TIM_CHANNEL_1:
  2706. {
  2707. /* Check the parameters */
  2708. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  2709. /* Configure the Channel 1 in PWM mode */
  2710. TIM_OC1_SetConfig(htim->Instance, sConfig);
  2711. /* Set the Preload enable bit for channel1 */
  2712. htim->Instance->CCMR1 |= TIM_CCMR1_OC1PE;
  2713. /* Configure the Output Fast mode */
  2714. htim->Instance->CCMR1 &= ~TIM_CCMR1_OC1FE;
  2715. htim->Instance->CCMR1 |= sConfig->OCFastMode;
  2716. }
  2717. break;
  2718. case TIM_CHANNEL_2:
  2719. {
  2720. /* Check the parameters */
  2721. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  2722. /* Configure the Channel 2 in PWM mode */
  2723. TIM_OC2_SetConfig(htim->Instance, sConfig);
  2724. /* Set the Preload enable bit for channel2 */
  2725. htim->Instance->CCMR1 |= TIM_CCMR1_OC2PE;
  2726. /* Configure the Output Fast mode */
  2727. htim->Instance->CCMR1 &= ~TIM_CCMR1_OC2FE;
  2728. htim->Instance->CCMR1 |= sConfig->OCFastMode << 8;
  2729. }
  2730. break;
  2731. case TIM_CHANNEL_3:
  2732. {
  2733. /* Check the parameters */
  2734. assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
  2735. /* Configure the Channel 3 in PWM mode */
  2736. TIM_OC3_SetConfig(htim->Instance, sConfig);
  2737. /* Set the Preload enable bit for channel3 */
  2738. htim->Instance->CCMR2 |= TIM_CCMR2_OC3PE;
  2739. /* Configure the Output Fast mode */
  2740. htim->Instance->CCMR2 &= ~TIM_CCMR2_OC3FE;
  2741. htim->Instance->CCMR2 |= sConfig->OCFastMode;
  2742. }
  2743. break;
  2744. case TIM_CHANNEL_4:
  2745. {
  2746. /* Check the parameters */
  2747. assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
  2748. /* Configure the Channel 4 in PWM mode */
  2749. TIM_OC4_SetConfig(htim->Instance, sConfig);
  2750. /* Set the Preload enable bit for channel4 */
  2751. htim->Instance->CCMR2 |= TIM_CCMR2_OC4PE;
  2752. /* Configure the Output Fast mode */
  2753. htim->Instance->CCMR2 &= ~TIM_CCMR2_OC4FE;
  2754. htim->Instance->CCMR2 |= sConfig->OCFastMode << 8;
  2755. }
  2756. break;
  2757. case TIM_CHANNEL_5:
  2758. {
  2759. /* Check the parameters */
  2760. assert_param(IS_TIM_CC5_INSTANCE(htim->Instance));
  2761. /* Configure the Channel 5 in PWM mode */
  2762. TIM_OC5_SetConfig(htim->Instance, sConfig);
  2763. /* Set the Preload enable bit for channel5*/
  2764. htim->Instance->CCMR3 |= TIM_CCMR3_OC5PE;
  2765. /* Configure the Output Fast mode */
  2766. htim->Instance->CCMR3 &= ~TIM_CCMR3_OC5FE;
  2767. htim->Instance->CCMR3 |= sConfig->OCFastMode;
  2768. }
  2769. break;
  2770. case TIM_CHANNEL_6:
  2771. {
  2772. /* Check the parameters */
  2773. assert_param(IS_TIM_CC6_INSTANCE(htim->Instance));
  2774. /* Configure the Channel 5 in PWM mode */
  2775. TIM_OC6_SetConfig(htim->Instance, sConfig);
  2776. /* Set the Preload enable bit for channel6 */
  2777. htim->Instance->CCMR3 |= TIM_CCMR3_OC6PE;
  2778. /* Configure the Output Fast mode */
  2779. htim->Instance->CCMR3 &= ~TIM_CCMR3_OC6FE;
  2780. htim->Instance->CCMR3 |= sConfig->OCFastMode << 8;
  2781. }
  2782. break;
  2783. default:
  2784. break;
  2785. }
  2786. htim->State = HAL_TIM_STATE_READY;
  2787. __HAL_UNLOCK(htim);
  2788. return HAL_OK;
  2789. }
  2790. /**
  2791. * @brief Initializes the TIM One Pulse Channels according to the specified
  2792. * parameters in the TIM_OnePulse_InitTypeDef.
  2793. * @param htim TIM One Pulse handle
  2794. * @param sConfig TIM One Pulse configuration structure
  2795. * @param OutputChannel TIM Channels to be enabled
  2796. * This parameter can be one of the following values:
  2797. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  2798. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  2799. * @param InputChannel TIM Channels to be enabled
  2800. * This parameter can be one of the following values:
  2801. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  2802. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  2803. * @retval HAL status
  2804. */
  2805. HAL_StatusTypeDef HAL_TIM_OnePulse_ConfigChannel(TIM_HandleTypeDef *htim, TIM_OnePulse_InitTypeDef* sConfig, uint32_t OutputChannel, uint32_t InputChannel)
  2806. {
  2807. TIM_OC_InitTypeDef temp1;
  2808. /* Check the parameters */
  2809. assert_param(IS_TIM_OPM_CHANNELS(OutputChannel));
  2810. assert_param(IS_TIM_OPM_CHANNELS(InputChannel));
  2811. if(OutputChannel != InputChannel)
  2812. {
  2813. /* Process Locked */
  2814. __HAL_LOCK(htim);
  2815. htim->State = HAL_TIM_STATE_BUSY;
  2816. /* Extract the Ouput compare configuration from sConfig structure */
  2817. temp1.OCMode = sConfig->OCMode;
  2818. temp1.Pulse = sConfig->Pulse;
  2819. temp1.OCPolarity = sConfig->OCPolarity;
  2820. temp1.OCNPolarity = sConfig->OCNPolarity;
  2821. temp1.OCIdleState = sConfig->OCIdleState;
  2822. temp1.OCNIdleState = sConfig->OCNIdleState;
  2823. switch (OutputChannel)
  2824. {
  2825. case TIM_CHANNEL_1:
  2826. {
  2827. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  2828. TIM_OC1_SetConfig(htim->Instance, &temp1);
  2829. }
  2830. break;
  2831. case TIM_CHANNEL_2:
  2832. {
  2833. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  2834. TIM_OC2_SetConfig(htim->Instance, &temp1);
  2835. }
  2836. break;
  2837. default:
  2838. break;
  2839. }
  2840. switch (InputChannel)
  2841. {
  2842. case TIM_CHANNEL_1:
  2843. {
  2844. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  2845. TIM_TI1_SetConfig(htim->Instance, sConfig->ICPolarity,
  2846. sConfig->ICSelection, sConfig->ICFilter);
  2847. /* Reset the IC1PSC Bits */
  2848. htim->Instance->CCMR1 &= ~TIM_CCMR1_IC1PSC;
  2849. /* Select the Trigger source */
  2850. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  2851. htim->Instance->SMCR |= TIM_TS_TI1FP1;
  2852. /* Select the Slave Mode */
  2853. htim->Instance->SMCR &= ~TIM_SMCR_SMS;
  2854. htim->Instance->SMCR |= TIM_SLAVEMODE_TRIGGER;
  2855. }
  2856. break;
  2857. case TIM_CHANNEL_2:
  2858. {
  2859. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  2860. TIM_TI2_SetConfig(htim->Instance, sConfig->ICPolarity,
  2861. sConfig->ICSelection, sConfig->ICFilter);
  2862. /* Reset the IC2PSC Bits */
  2863. htim->Instance->CCMR1 &= ~TIM_CCMR1_IC2PSC;
  2864. /* Select the Trigger source */
  2865. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  2866. htim->Instance->SMCR |= TIM_TS_TI2FP2;
  2867. /* Select the Slave Mode */
  2868. htim->Instance->SMCR &= ~TIM_SMCR_SMS;
  2869. htim->Instance->SMCR |= TIM_SLAVEMODE_TRIGGER;
  2870. }
  2871. break;
  2872. default:
  2873. break;
  2874. }
  2875. htim->State = HAL_TIM_STATE_READY;
  2876. __HAL_UNLOCK(htim);
  2877. return HAL_OK;
  2878. }
  2879. else
  2880. {
  2881. return HAL_ERROR;
  2882. }
  2883. }
  2884. /**
  2885. * @brief Configure the DMA Burst to transfer Data from the memory to the TIM peripheral
  2886. * @param htim TIM handle
  2887. * @param BurstBaseAddress TIM Base address from when the DMA will starts the Data write
  2888. * This parameters can be on of the following values:
  2889. * @arg TIM_DMABASE_CR1
  2890. * @arg TIM_DMABASE_CR2
  2891. * @arg TIM_DMABASE_SMCR
  2892. * @arg TIM_DMABASE_DIER
  2893. * @arg TIM_DMABASE_SR
  2894. * @arg TIM_DMABASE_EGR
  2895. * @arg TIM_DMABASE_CCMR1
  2896. * @arg TIM_DMABASE_CCMR2
  2897. * @arg TIM_DMABASE_CCER
  2898. * @arg TIM_DMABASE_CNT
  2899. * @arg TIM_DMABASE_PSC
  2900. * @arg TIM_DMABASE_ARR
  2901. * @arg TIM_DMABASE_RCR
  2902. * @arg TIM_DMABASE_CCR1
  2903. * @arg TIM_DMABASE_CCR2
  2904. * @arg TIM_DMABASE_CCR3
  2905. * @arg TIM_DMABASE_CCR4
  2906. * @arg TIM_DMABASE_BDTR
  2907. * @arg TIM_DMABASE_DCR
  2908. * @param BurstRequestSrc TIM DMA Request sources
  2909. * This parameters can be on of the following values:
  2910. * @arg TIM_DMA_UPDATE: TIM update Interrupt source
  2911. * @arg TIM_DMA_CC1: TIM Capture Compare 1 DMA source
  2912. * @arg TIM_DMA_CC2: TIM Capture Compare 2 DMA source
  2913. * @arg TIM_DMA_CC3: TIM Capture Compare 3 DMA source
  2914. * @arg TIM_DMA_CC4: TIM Capture Compare 4 DMA source
  2915. * @arg TIM_DMA_COM: TIM Commutation DMA source
  2916. * @arg TIM_DMA_TRIGGER: TIM Trigger DMA source
  2917. * @param BurstBuffer The Buffer address.
  2918. * @param BurstLength DMA Burst length. This parameter can be one value
  2919. * between: TIM_DMABurstLength_1Transfer and TIM_DMABurstLength_18Transfers.
  2920. * @retval HAL status
  2921. */
  2922. HAL_StatusTypeDef HAL_TIM_DMABurst_WriteStart(TIM_HandleTypeDef *htim, uint32_t BurstBaseAddress, uint32_t BurstRequestSrc,
  2923. uint32_t* BurstBuffer, uint32_t BurstLength)
  2924. {
  2925. /* Check the parameters */
  2926. assert_param(IS_TIM_DMABURST_INSTANCE(htim->Instance));
  2927. assert_param(IS_TIM_DMA_BASE(BurstBaseAddress));
  2928. assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc));
  2929. assert_param(IS_TIM_DMA_LENGTH(BurstLength));
  2930. if((htim->State == HAL_TIM_STATE_BUSY))
  2931. {
  2932. return HAL_BUSY;
  2933. }
  2934. else if((htim->State == HAL_TIM_STATE_READY))
  2935. {
  2936. if((BurstBuffer == 0 ) && (BurstLength > 0))
  2937. {
  2938. return HAL_ERROR;
  2939. }
  2940. else
  2941. {
  2942. htim->State = HAL_TIM_STATE_BUSY;
  2943. }
  2944. }
  2945. switch(BurstRequestSrc)
  2946. {
  2947. case TIM_DMA_UPDATE:
  2948. {
  2949. /* Set the DMA Period elapsed callback */
  2950. htim->hdma[TIM_DMA_ID_UPDATE]->XferCpltCallback = TIM_DMAPeriodElapsedCplt;
  2951. /* Set the DMA error callback */
  2952. htim->hdma[TIM_DMA_ID_UPDATE]->XferErrorCallback = TIM_DMAError ;
  2953. /* Enable the DMA channel */
  2954. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_UPDATE], (uint32_t)BurstBuffer, (uint32_t)&htim->Instance->DMAR, ((BurstLength) >> 8) + 1);
  2955. }
  2956. break;
  2957. case TIM_DMA_CC1:
  2958. {
  2959. /* Set the DMA Period elapsed callback */
  2960. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseCplt;
  2961. /* Set the DMA error callback */
  2962. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  2963. /* Enable the DMA channel */
  2964. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)BurstBuffer, (uint32_t)&htim->Instance->DMAR, ((BurstLength) >> 8) + 1);
  2965. }
  2966. break;
  2967. case TIM_DMA_CC2:
  2968. {
  2969. /* Set the DMA Period elapsed callback */
  2970. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseCplt;
  2971. /* Set the DMA error callback */
  2972. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
  2973. /* Enable the DMA channel */
  2974. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)BurstBuffer, (uint32_t)&htim->Instance->DMAR, ((BurstLength) >> 8) + 1);
  2975. }
  2976. break;
  2977. case TIM_DMA_CC3:
  2978. {
  2979. /* Set the DMA Period elapsed callback */
  2980. htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseCplt;
  2981. /* Set the DMA error callback */
  2982. htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
  2983. /* Enable the DMA channel */
  2984. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)BurstBuffer, (uint32_t)&htim->Instance->DMAR, ((BurstLength) >> 8) + 1);
  2985. }
  2986. break;
  2987. case TIM_DMA_CC4:
  2988. {
  2989. /* Set the DMA Period elapsed callback */
  2990. htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMADelayPulseCplt;
  2991. /* Set the DMA error callback */
  2992. htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
  2993. /* Enable the DMA channel */
  2994. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)BurstBuffer, (uint32_t)&htim->Instance->DMAR, ((BurstLength) >> 8) + 1);
  2995. }
  2996. break;
  2997. case TIM_DMA_COM:
  2998. {
  2999. /* Set the DMA Period elapsed callback */
  3000. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferCpltCallback = TIMEx_DMACommutationCplt;
  3001. /* Set the DMA error callback */
  3002. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferErrorCallback = TIM_DMAError ;
  3003. /* Enable the DMA channel */
  3004. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_COMMUTATION], (uint32_t)BurstBuffer, (uint32_t)&htim->Instance->DMAR, ((BurstLength) >> 8) + 1);
  3005. }
  3006. break;
  3007. case TIM_DMA_TRIGGER:
  3008. {
  3009. /* Set the DMA Period elapsed callback */
  3010. htim->hdma[TIM_DMA_ID_TRIGGER]->XferCpltCallback = TIM_DMATriggerCplt;
  3011. /* Set the DMA error callback */
  3012. htim->hdma[TIM_DMA_ID_TRIGGER]->XferErrorCallback = TIM_DMAError ;
  3013. /* Enable the DMA channel */
  3014. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_TRIGGER], (uint32_t)BurstBuffer, (uint32_t)&htim->Instance->DMAR, ((BurstLength) >> 8) + 1);
  3015. }
  3016. break;
  3017. default:
  3018. break;
  3019. }
  3020. /* configure the DMA Burst Mode */
  3021. htim->Instance->DCR = BurstBaseAddress | BurstLength;
  3022. /* Enable the TIM DMA Request */
  3023. __HAL_TIM_ENABLE_DMA(htim, BurstRequestSrc);
  3024. htim->State = HAL_TIM_STATE_READY;
  3025. /* Return function status */
  3026. return HAL_OK;
  3027. }
  3028. /**
  3029. * @brief Stops the TIM DMA Burst mode
  3030. * @param htim TIM handle
  3031. * @param BurstRequestSrc TIM DMA Request sources to disable
  3032. * @retval HAL status
  3033. */
  3034. HAL_StatusTypeDef HAL_TIM_DMABurst_WriteStop(TIM_HandleTypeDef *htim, uint32_t BurstRequestSrc)
  3035. {
  3036. /* Check the parameters */
  3037. assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc));
  3038. /* Abort the DMA transfer (at least disable the DMA channel) */
  3039. switch(BurstRequestSrc)
  3040. {
  3041. case TIM_DMA_UPDATE:
  3042. {
  3043. HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_UPDATE]);
  3044. }
  3045. break;
  3046. case TIM_DMA_CC1:
  3047. {
  3048. HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_CC1]);
  3049. }
  3050. break;
  3051. case TIM_DMA_CC2:
  3052. {
  3053. HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_CC2]);
  3054. }
  3055. break;
  3056. case TIM_DMA_CC3:
  3057. {
  3058. HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_CC3]);
  3059. }
  3060. break;
  3061. case TIM_DMA_CC4:
  3062. {
  3063. HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_CC4]);
  3064. }
  3065. break;
  3066. case TIM_DMA_COM:
  3067. {
  3068. HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_COMMUTATION]);
  3069. }
  3070. break;
  3071. case TIM_DMA_TRIGGER:
  3072. {
  3073. HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_TRIGGER]);
  3074. }
  3075. break;
  3076. default:
  3077. break;
  3078. }
  3079. /* Disable the TIM Update DMA request */
  3080. __HAL_TIM_DISABLE_DMA(htim, BurstRequestSrc);
  3081. /* Return function status */
  3082. return HAL_OK;
  3083. }
  3084. /**
  3085. * @brief Configure the DMA Burst to transfer Data from the TIM peripheral to the memory
  3086. * @param htim TIM handle
  3087. * @param BurstBaseAddress TIM Base address from when the DMA will starts the Data read
  3088. * This parameters can be on of the following values:
  3089. * @arg TIM_DMABASE_CR1
  3090. * @arg TIM_DMABASE_CR2
  3091. * @arg TIM_DMABASE_SMCR
  3092. * @arg TIM_DMABASE_DIER
  3093. * @arg TIM_DMABASE_SR
  3094. * @arg TIM_DMABASE_EGR
  3095. * @arg TIM_DMABASE_CCMR1
  3096. * @arg TIM_DMABASE_CCMR2
  3097. * @arg TIM_DMABASE_CCER
  3098. * @arg TIM_DMABASE_CNT
  3099. * @arg TIM_DMABASE_PSC
  3100. * @arg TIM_DMABASE_ARR
  3101. * @arg TIM_DMABASE_RCR
  3102. * @arg TIM_DMABASE_CCR1
  3103. * @arg TIM_DMABASE_CCR2
  3104. * @arg TIM_DMABASE_CCR3
  3105. * @arg TIM_DMABASE_CCR4
  3106. * @arg TIM_DMABASE_BDTR
  3107. * @arg TIM_DMABASE_DCR
  3108. * @param BurstRequestSrc TIM DMA Request sources
  3109. * This parameters can be on of the following values:
  3110. * @arg TIM_DMA_UPDATE: TIM update Interrupt source
  3111. * @arg TIM_DMA_CC1: TIM Capture Compare 1 DMA source
  3112. * @arg TIM_DMA_CC2: TIM Capture Compare 2 DMA source
  3113. * @arg TIM_DMA_CC3: TIM Capture Compare 3 DMA source
  3114. * @arg TIM_DMA_CC4: TIM Capture Compare 4 DMA source
  3115. * @arg TIM_DMA_COM: TIM Commutation DMA source
  3116. * @arg TIM_DMA_TRIGGER: TIM Trigger DMA source
  3117. * @param BurstBuffer The Buffer address.
  3118. * @param BurstLength DMA Burst length. This parameter can be one value
  3119. * between: TIM_DMABurstLength_1Transfer and TIM_DMABurstLength_18Transfers.
  3120. * @retval HAL status
  3121. */
  3122. HAL_StatusTypeDef HAL_TIM_DMABurst_ReadStart(TIM_HandleTypeDef *htim, uint32_t BurstBaseAddress, uint32_t BurstRequestSrc,
  3123. uint32_t *BurstBuffer, uint32_t BurstLength)
  3124. {
  3125. /* Check the parameters */
  3126. assert_param(IS_TIM_DMABURST_INSTANCE(htim->Instance));
  3127. assert_param(IS_TIM_DMA_BASE(BurstBaseAddress));
  3128. assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc));
  3129. assert_param(IS_TIM_DMA_LENGTH(BurstLength));
  3130. if((htim->State == HAL_TIM_STATE_BUSY))
  3131. {
  3132. return HAL_BUSY;
  3133. }
  3134. else if((htim->State == HAL_TIM_STATE_READY))
  3135. {
  3136. if((BurstBuffer == 0 ) && (BurstLength > 0))
  3137. {
  3138. return HAL_ERROR;
  3139. }
  3140. else
  3141. {
  3142. htim->State = HAL_TIM_STATE_BUSY;
  3143. }
  3144. }
  3145. switch(BurstRequestSrc)
  3146. {
  3147. case TIM_DMA_UPDATE:
  3148. {
  3149. /* Set the DMA Period elapsed callback */
  3150. htim->hdma[TIM_DMA_ID_UPDATE]->XferCpltCallback = TIM_DMAPeriodElapsedCplt;
  3151. /* Set the DMA error callback */
  3152. htim->hdma[TIM_DMA_ID_UPDATE]->XferErrorCallback = TIM_DMAError ;
  3153. /* Enable the DMA channel */
  3154. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_UPDATE], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer, ((BurstLength) >> 8) + 1);
  3155. }
  3156. break;
  3157. case TIM_DMA_CC1:
  3158. {
  3159. /* Set the DMA Period elapsed callback */
  3160. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMACaptureCplt;
  3161. /* Set the DMA error callback */
  3162. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  3163. /* Enable the DMA channel */
  3164. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer, ((BurstLength) >> 8) + 1);
  3165. }
  3166. break;
  3167. case TIM_DMA_CC2:
  3168. {
  3169. /* Set the DMA Period elapsed callback */
  3170. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMACaptureCplt;
  3171. /* Set the DMA error callback */
  3172. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
  3173. /* Enable the DMA channel */
  3174. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer, ((BurstLength) >> 8) + 1);
  3175. }
  3176. break;
  3177. case TIM_DMA_CC3:
  3178. {
  3179. /* Set the DMA Period elapsed callback */
  3180. htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMACaptureCplt;
  3181. /* Set the DMA error callback */
  3182. htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
  3183. /* Enable the DMA channel */
  3184. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer, ((BurstLength) >> 8) + 1);
  3185. }
  3186. break;
  3187. case TIM_DMA_CC4:
  3188. {
  3189. /* Set the DMA Period elapsed callback */
  3190. htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMACaptureCplt;
  3191. /* Set the DMA error callback */
  3192. htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
  3193. /* Enable the DMA channel */
  3194. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer, ((BurstLength) >> 8) + 1);
  3195. }
  3196. break;
  3197. case TIM_DMA_COM:
  3198. {
  3199. /* Set the DMA Period elapsed callback */
  3200. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferCpltCallback = TIMEx_DMACommutationCplt;
  3201. /* Set the DMA error callback */
  3202. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferErrorCallback = TIM_DMAError ;
  3203. /* Enable the DMA channel */
  3204. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_COMMUTATION], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer, ((BurstLength) >> 8) + 1);
  3205. }
  3206. break;
  3207. case TIM_DMA_TRIGGER:
  3208. {
  3209. /* Set the DMA Period elapsed callback */
  3210. htim->hdma[TIM_DMA_ID_TRIGGER]->XferCpltCallback = TIM_DMATriggerCplt;
  3211. /* Set the DMA error callback */
  3212. htim->hdma[TIM_DMA_ID_TRIGGER]->XferErrorCallback = TIM_DMAError ;
  3213. /* Enable the DMA channel */
  3214. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_TRIGGER], (uint32_t)&htim->Instance->DMAR, (uint32_t)BurstBuffer, ((BurstLength) >> 8) + 1);
  3215. }
  3216. break;
  3217. default:
  3218. break;
  3219. }
  3220. /* configure the DMA Burst Mode */
  3221. htim->Instance->DCR = BurstBaseAddress | BurstLength;
  3222. /* Enable the TIM DMA Request */
  3223. __HAL_TIM_ENABLE_DMA(htim, BurstRequestSrc);
  3224. htim->State = HAL_TIM_STATE_READY;
  3225. /* Return function status */
  3226. return HAL_OK;
  3227. }
  3228. /**
  3229. * @brief Stop the DMA burst reading
  3230. * @param htim TIM handle
  3231. * @param BurstRequestSrc TIM DMA Request sources to disable.
  3232. * @retval HAL status
  3233. */
  3234. HAL_StatusTypeDef HAL_TIM_DMABurst_ReadStop(TIM_HandleTypeDef *htim, uint32_t BurstRequestSrc)
  3235. {
  3236. /* Check the parameters */
  3237. assert_param(IS_TIM_DMA_SOURCE(BurstRequestSrc));
  3238. /* Abort the DMA transfer (at least disable the DMA channel) */
  3239. switch(BurstRequestSrc)
  3240. {
  3241. case TIM_DMA_UPDATE:
  3242. {
  3243. HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_UPDATE]);
  3244. }
  3245. break;
  3246. case TIM_DMA_CC1:
  3247. {
  3248. HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_CC1]);
  3249. }
  3250. break;
  3251. case TIM_DMA_CC2:
  3252. {
  3253. HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_CC2]);
  3254. }
  3255. break;
  3256. case TIM_DMA_CC3:
  3257. {
  3258. HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_CC3]);
  3259. }
  3260. break;
  3261. case TIM_DMA_CC4:
  3262. {
  3263. HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_CC4]);
  3264. }
  3265. break;
  3266. case TIM_DMA_COM:
  3267. {
  3268. HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_COMMUTATION]);
  3269. }
  3270. break;
  3271. case TIM_DMA_TRIGGER:
  3272. {
  3273. HAL_DMA_Abort(htim->hdma[TIM_DMA_ID_TRIGGER]);
  3274. }
  3275. break;
  3276. default:
  3277. break;
  3278. }
  3279. /* Disable the TIM Update DMA request */
  3280. __HAL_TIM_DISABLE_DMA(htim, BurstRequestSrc);
  3281. /* Return function status */
  3282. return HAL_OK;
  3283. }
  3284. /**
  3285. * @brief Generate a software event
  3286. * @param htim TIM handle
  3287. * @param EventSource specifies the event source.
  3288. * This parameter can be one of the following values:
  3289. * @arg TIM_EVENTSOURCE_UPDATE: Timer update Event source
  3290. * @arg TIM_EVENTSOURCE_CC1: Timer Capture Compare 1 Event source
  3291. * @arg TIM_EVENTSOURCE_CC2: Timer Capture Compare 2 Event source
  3292. * @arg TIM_EVENTSOURCE_CC3: Timer Capture Compare 3 Event source
  3293. * @arg TIM_EVENTSOURCE_CC4: Timer Capture Compare 4 Event source
  3294. * @arg TIM_EVENTSOURCE_COM: Timer COM event source
  3295. * @arg TIM_EVENTSOURCE_TRIGGER: Timer Trigger Event source
  3296. * @arg TIM_EVENTSOURCE_BREAK: Timer Break event source
  3297. * @arg TIM_EVENTSOURCE_BREAK2: Timer Break2 event source
  3298. * @retval HAL status
  3299. */
  3300. HAL_StatusTypeDef HAL_TIM_GenerateEvent(TIM_HandleTypeDef *htim, uint32_t EventSource)
  3301. {
  3302. /* Check the parameters */
  3303. assert_param(IS_TIM_INSTANCE(htim->Instance));
  3304. assert_param(IS_TIM_EVENT_SOURCE(EventSource));
  3305. /* Process Locked */
  3306. __HAL_LOCK(htim);
  3307. /* Change the TIM state */
  3308. htim->State = HAL_TIM_STATE_BUSY;
  3309. /* Set the event sources */
  3310. htim->Instance->EGR = EventSource;
  3311. /* Change the TIM state */
  3312. htim->State = HAL_TIM_STATE_READY;
  3313. __HAL_UNLOCK(htim);
  3314. /* Return function status */
  3315. return HAL_OK;
  3316. }
  3317. /**
  3318. * @brief Configures the OCRef clear feature
  3319. * @param htim TIM handle
  3320. * @param sClearInputConfig pointer to a TIM_ClearInputConfigTypeDef structure that
  3321. * contains the OCREF clear feature and parameters for the TIM peripheral.
  3322. * @param Channel specifies the TIM Channel
  3323. * This parameter can be one of the following values:
  3324. * @arg TIM_Channel_1: TIM Channel 1
  3325. * @arg TIM_Channel_2: TIM Channel 2
  3326. * @arg TIM_Channel_3: TIM Channel 3
  3327. * @arg TIM_Channel_4: TIM Channel 4
  3328. * @arg TIM_Channel_5: TIM Channel 5
  3329. * @arg TIM_Channel_6: TIM Channel 6
  3330. * @retval None
  3331. */
  3332. HAL_StatusTypeDef HAL_TIM_ConfigOCrefClear(TIM_HandleTypeDef *htim,
  3333. TIM_ClearInputConfigTypeDef *sClearInputConfig,
  3334. uint32_t Channel)
  3335. {
  3336. uint32_t tmpsmcr = 0;
  3337. /* Check the parameters */
  3338. assert_param(IS_TIM_OCXREF_CLEAR_INSTANCE(htim->Instance));
  3339. assert_param(IS_TIM_CLEARINPUT_SOURCE(sClearInputConfig->ClearInputSource));
  3340. /* Process Locked */
  3341. __HAL_LOCK(htim);
  3342. switch (sClearInputConfig->ClearInputSource)
  3343. {
  3344. case TIM_CLEARINPUTSOURCE_NONE:
  3345. {
  3346. /* Get the TIMx SMCR register value */
  3347. tmpsmcr = htim->Instance->SMCR;
  3348. /* Clear the OCREF clear selection bit */
  3349. tmpsmcr &= ~TIM_SMCR_OCCS;
  3350. /* Clear the ETR Bits */
  3351. tmpsmcr &= ~(TIM_SMCR_ETF | TIM_SMCR_ETPS | TIM_SMCR_ECE | TIM_SMCR_ETP);
  3352. /* Set TIMx_SMCR */
  3353. htim->Instance->SMCR = tmpsmcr;
  3354. }
  3355. break;
  3356. case TIM_CLEARINPUTSOURCE_OCREFCLR:
  3357. {
  3358. /* Clear the OCREF clear selection bit */
  3359. htim->Instance->SMCR &= ~TIM_SMCR_OCCS;
  3360. }
  3361. break;
  3362. case TIM_CLEARINPUTSOURCE_ETR:
  3363. {
  3364. /* Check the parameters */
  3365. assert_param(IS_TIM_CLEARINPUT_POLARITY(sClearInputConfig->ClearInputPolarity));
  3366. assert_param(IS_TIM_CLEARINPUT_PRESCALER(sClearInputConfig->ClearInputPrescaler));
  3367. assert_param(IS_TIM_CLEARINPUT_FILTER(sClearInputConfig->ClearInputFilter));
  3368. TIM_ETR_SetConfig(htim->Instance,
  3369. sClearInputConfig->ClearInputPrescaler,
  3370. sClearInputConfig->ClearInputPolarity,
  3371. sClearInputConfig->ClearInputFilter);
  3372. /* Set the OCREF clear selection bit */
  3373. htim->Instance->SMCR |= TIM_SMCR_OCCS;
  3374. }
  3375. break;
  3376. default:
  3377. break;
  3378. }
  3379. switch (Channel)
  3380. {
  3381. case TIM_CHANNEL_1:
  3382. {
  3383. if(sClearInputConfig->ClearInputState != RESET)
  3384. {
  3385. /* Enable the OCREF clear feature for Channel 1 */
  3386. htim->Instance->CCMR1 |= TIM_CCMR1_OC1CE;
  3387. }
  3388. else
  3389. {
  3390. /* Disable the OCREF clear feature for Channel 1 */
  3391. htim->Instance->CCMR1 &= ~TIM_CCMR1_OC1CE;
  3392. }
  3393. }
  3394. break;
  3395. case TIM_CHANNEL_2:
  3396. {
  3397. if(sClearInputConfig->ClearInputState != RESET)
  3398. {
  3399. /* Enable the OCREF clear feature for Channel 2 */
  3400. htim->Instance->CCMR1 |= TIM_CCMR1_OC2CE;
  3401. }
  3402. else
  3403. {
  3404. /* Disable the OCREF clear feature for Channel 2 */
  3405. htim->Instance->CCMR1 &= ~TIM_CCMR1_OC2CE;
  3406. }
  3407. }
  3408. break;
  3409. case TIM_CHANNEL_3:
  3410. {
  3411. if(sClearInputConfig->ClearInputState != RESET)
  3412. {
  3413. /* Enable the OCREF clear feature for Channel 3 */
  3414. htim->Instance->CCMR2 |= TIM_CCMR2_OC3CE;
  3415. }
  3416. else
  3417. {
  3418. /* Disable the OCREF clear feature for Channel 3 */
  3419. htim->Instance->CCMR2 &= ~TIM_CCMR2_OC3CE;
  3420. }
  3421. }
  3422. break;
  3423. case TIM_CHANNEL_4:
  3424. {
  3425. if(sClearInputConfig->ClearInputState != RESET)
  3426. {
  3427. /* Enable the OCREF clear feature for Channel 4 */
  3428. htim->Instance->CCMR2 |= TIM_CCMR2_OC4CE;
  3429. }
  3430. else
  3431. {
  3432. /* Disable the OCREF clear feature for Channel 4 */
  3433. htim->Instance->CCMR2 &= ~TIM_CCMR2_OC4CE;
  3434. }
  3435. }
  3436. break;
  3437. case TIM_CHANNEL_5:
  3438. {
  3439. if(sClearInputConfig->ClearInputState != RESET)
  3440. {
  3441. /* Enable the OCREF clear feature for Channel 1 */
  3442. htim->Instance->CCMR3 |= TIM_CCMR3_OC5CE;
  3443. }
  3444. else
  3445. {
  3446. /* Disable the OCREF clear feature for Channel 1 */
  3447. htim->Instance->CCMR3 &= ~TIM_CCMR3_OC5CE;
  3448. }
  3449. }
  3450. break;
  3451. case TIM_CHANNEL_6:
  3452. {
  3453. if(sClearInputConfig->ClearInputState != RESET)
  3454. {
  3455. /* Enable the OCREF clear feature for Channel 1 */
  3456. htim->Instance->CCMR3 |= TIM_CCMR3_OC6CE;
  3457. }
  3458. else
  3459. {
  3460. /* Disable the OCREF clear feature for Channel 1 */
  3461. htim->Instance->CCMR3 &= ~TIM_CCMR3_OC6CE;
  3462. }
  3463. }
  3464. break;
  3465. default:
  3466. break;
  3467. }
  3468. __HAL_UNLOCK(htim);
  3469. return HAL_OK;
  3470. }
  3471. /**
  3472. * @brief Configures the clock source to be used
  3473. * @param htim TIM handle
  3474. * @param sClockSourceConfig pointer to a TIM_ClockConfigTypeDef structure that
  3475. * contains the clock source information for the TIM peripheral.
  3476. * @retval HAL status
  3477. */
  3478. HAL_StatusTypeDef HAL_TIM_ConfigClockSource(TIM_HandleTypeDef *htim, TIM_ClockConfigTypeDef * sClockSourceConfig)
  3479. {
  3480. uint32_t tmpsmcr = 0;
  3481. /* Process Locked */
  3482. __HAL_LOCK(htim);
  3483. htim->State = HAL_TIM_STATE_BUSY;
  3484. /* Check the parameters */
  3485. assert_param(IS_TIM_CLOCKSOURCE(sClockSourceConfig->ClockSource));
  3486. /* Reset the SMS, TS, ECE, ETPS and ETRF bits */
  3487. tmpsmcr = htim->Instance->SMCR;
  3488. tmpsmcr &= ~(TIM_SMCR_SMS | TIM_SMCR_TS);
  3489. tmpsmcr &= ~(TIM_SMCR_ETF | TIM_SMCR_ETPS | TIM_SMCR_ECE | TIM_SMCR_ETP);
  3490. htim->Instance->SMCR = tmpsmcr;
  3491. switch (sClockSourceConfig->ClockSource)
  3492. {
  3493. case TIM_CLOCKSOURCE_INTERNAL:
  3494. {
  3495. assert_param(IS_TIM_INSTANCE(htim->Instance));
  3496. /* Disable slave mode to clock the prescaler directly with the internal clock */
  3497. htim->Instance->SMCR &= ~TIM_SMCR_SMS;
  3498. }
  3499. break;
  3500. case TIM_CLOCKSOURCE_ETRMODE1:
  3501. {
  3502. /* Check whether or not the timer instance supports external trigger input mode 1 (ETRF)*/
  3503. assert_param(IS_TIM_CLOCKSOURCE_ETRMODE1_INSTANCE(htim->Instance));
  3504. /* Check ETR input conditioning related parameters */
  3505. assert_param(IS_TIM_CLOCKPRESCALER(sClockSourceConfig->ClockPrescaler));
  3506. assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));
  3507. assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));
  3508. /* Configure the ETR Clock source */
  3509. TIM_ETR_SetConfig(htim->Instance,
  3510. sClockSourceConfig->ClockPrescaler,
  3511. sClockSourceConfig->ClockPolarity,
  3512. sClockSourceConfig->ClockFilter);
  3513. /* Get the TIMx SMCR register value */
  3514. tmpsmcr = htim->Instance->SMCR;
  3515. /* Reset the SMS and TS Bits */
  3516. tmpsmcr &= ~(TIM_SMCR_SMS | TIM_SMCR_TS);
  3517. /* Select the External clock mode1 and the ETRF trigger */
  3518. tmpsmcr |= (TIM_SLAVEMODE_EXTERNAL1 | TIM_CLOCKSOURCE_ETRMODE1);
  3519. /* Write to TIMx SMCR */
  3520. htim->Instance->SMCR = tmpsmcr;
  3521. }
  3522. break;
  3523. case TIM_CLOCKSOURCE_ETRMODE2:
  3524. {
  3525. /* Check whether or not the timer instance supports external trigger input mode 2 (ETRF)*/
  3526. assert_param(IS_TIM_CLOCKSOURCE_ETRMODE2_INSTANCE(htim->Instance));
  3527. /* Check ETR input conditioning related parameters */
  3528. assert_param(IS_TIM_CLOCKPRESCALER(sClockSourceConfig->ClockPrescaler));
  3529. assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));
  3530. assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));
  3531. /* Configure the ETR Clock source */
  3532. TIM_ETR_SetConfig(htim->Instance,
  3533. sClockSourceConfig->ClockPrescaler,
  3534. sClockSourceConfig->ClockPolarity,
  3535. sClockSourceConfig->ClockFilter);
  3536. /* Enable the External clock mode2 */
  3537. htim->Instance->SMCR |= TIM_SMCR_ECE;
  3538. }
  3539. break;
  3540. case TIM_CLOCKSOURCE_TI1:
  3541. {
  3542. /* Check whether or not the timer instance supports external clock mode 1 */
  3543. assert_param(IS_TIM_CLOCKSOURCE_TIX_INSTANCE(htim->Instance));
  3544. /* Check TI1 input conditioning related parameters */
  3545. assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));
  3546. assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));
  3547. TIM_TI1_ConfigInputStage(htim->Instance,
  3548. sClockSourceConfig->ClockPolarity,
  3549. sClockSourceConfig->ClockFilter);
  3550. TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_TI1);
  3551. }
  3552. break;
  3553. case TIM_CLOCKSOURCE_TI2:
  3554. {
  3555. /* Check whether or not the timer instance supports external clock mode 1 (ETRF)*/
  3556. assert_param(IS_TIM_CLOCKSOURCE_TIX_INSTANCE(htim->Instance));
  3557. /* Check TI2 input conditioning related parameters */
  3558. assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));
  3559. assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));
  3560. TIM_TI2_ConfigInputStage(htim->Instance,
  3561. sClockSourceConfig->ClockPolarity,
  3562. sClockSourceConfig->ClockFilter);
  3563. TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_TI2);
  3564. }
  3565. break;
  3566. case TIM_CLOCKSOURCE_TI1ED:
  3567. {
  3568. /* Check whether or not the timer instance supports external clock mode 1 */
  3569. assert_param(IS_TIM_CLOCKSOURCE_TIX_INSTANCE(htim->Instance));
  3570. /* Check TI1 input conditioning related parameters */
  3571. assert_param(IS_TIM_CLOCKPOLARITY(sClockSourceConfig->ClockPolarity));
  3572. assert_param(IS_TIM_CLOCKFILTER(sClockSourceConfig->ClockFilter));
  3573. TIM_TI1_ConfigInputStage(htim->Instance,
  3574. sClockSourceConfig->ClockPolarity,
  3575. sClockSourceConfig->ClockFilter);
  3576. TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_TI1ED);
  3577. }
  3578. break;
  3579. case TIM_CLOCKSOURCE_ITR0:
  3580. {
  3581. /* Check whether or not the timer instance supports internal trigger input */
  3582. assert_param(IS_TIM_CLOCKSOURCE_ITRX_INSTANCE(htim->Instance));
  3583. TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_ITR0);
  3584. }
  3585. break;
  3586. case TIM_CLOCKSOURCE_ITR1:
  3587. {
  3588. /* Check whether or not the timer instance supports internal trigger input */
  3589. assert_param(IS_TIM_CLOCKSOURCE_ITRX_INSTANCE(htim->Instance));
  3590. TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_ITR1);
  3591. }
  3592. break;
  3593. case TIM_CLOCKSOURCE_ITR2:
  3594. {
  3595. /* Check whether or not the timer instance supports internal trigger input */
  3596. assert_param(IS_TIM_CLOCKSOURCE_ITRX_INSTANCE(htim->Instance));
  3597. TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_ITR2);
  3598. }
  3599. break;
  3600. case TIM_CLOCKSOURCE_ITR3:
  3601. {
  3602. /* Check whether or not the timer instance supports internal trigger input */
  3603. assert_param(IS_TIM_CLOCKSOURCE_ITRX_INSTANCE(htim->Instance));
  3604. TIM_ITRx_SetConfig(htim->Instance, TIM_CLOCKSOURCE_ITR3);
  3605. }
  3606. break;
  3607. default:
  3608. break;
  3609. }
  3610. htim->State = HAL_TIM_STATE_READY;
  3611. __HAL_UNLOCK(htim);
  3612. return HAL_OK;
  3613. }
  3614. /**
  3615. * @brief Selects the signal connected to the TI1 input: direct from CH1_input
  3616. * or a XOR combination between CH1_input, CH2_input & CH3_input
  3617. * @param htim TIM handle.
  3618. * @param TI1_Selection Indicate whether or not channel 1 is connected to the
  3619. * output of a XOR gate.
  3620. * This parameter can be one of the following values:
  3621. * @arg TIM_TI1SELECTION_CH1: The TIMx_CH1 pin is connected to TI1 input
  3622. * @arg TIM_TI1SELECTION_XORCOMBINATION: The TIMx_CH1, CH2 and CH3
  3623. * pins are connected to the TI1 input (XOR combination)
  3624. * @retval HAL status
  3625. */
  3626. HAL_StatusTypeDef HAL_TIM_ConfigTI1Input(TIM_HandleTypeDef *htim, uint32_t TI1_Selection)
  3627. {
  3628. uint32_t tmpcr2 = 0;
  3629. /* Check the parameters */
  3630. assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
  3631. assert_param(IS_TIM_TI1SELECTION(TI1_Selection));
  3632. /* Get the TIMx CR2 register value */
  3633. tmpcr2 = htim->Instance->CR2;
  3634. /* Reset the TI1 selection */
  3635. tmpcr2 &= ~TIM_CR2_TI1S;
  3636. /* Set the TI1 selection */
  3637. tmpcr2 |= TI1_Selection;
  3638. /* Write to TIMxCR2 */
  3639. htim->Instance->CR2 = tmpcr2;
  3640. return HAL_OK;
  3641. }
  3642. /**
  3643. * @brief Configures the TIM in Slave mode
  3644. * @param htim TIM handle.
  3645. * @param sSlaveConfig pointer to a TIM_SlaveConfigTypeDef structure that
  3646. * contains the selected trigger (internal trigger input, filtered
  3647. * timer input or external trigger input) and the ) and the Slave
  3648. * mode (Disable, Reset, Gated, Trigger, External clock mode 1).
  3649. * @retval HAL status
  3650. */
  3651. HAL_StatusTypeDef HAL_TIM_SlaveConfigSynchronization(TIM_HandleTypeDef *htim, TIM_SlaveConfigTypeDef * sSlaveConfig)
  3652. {
  3653. /* Check the parameters */
  3654. assert_param(IS_TIM_SLAVE_INSTANCE(htim->Instance));
  3655. assert_param(IS_TIM_SLAVE_MODE(sSlaveConfig->SlaveMode));
  3656. assert_param(IS_TIM_TRIGGER_SELECTION(sSlaveConfig->InputTrigger));
  3657. __HAL_LOCK(htim);
  3658. htim->State = HAL_TIM_STATE_BUSY;
  3659. TIM_SlaveTimer_SetConfig(htim, sSlaveConfig);
  3660. /* Disable Trigger Interrupt */
  3661. __HAL_TIM_DISABLE_IT(htim, TIM_IT_TRIGGER);
  3662. /* Disable Trigger DMA request */
  3663. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_TRIGGER);
  3664. htim->State = HAL_TIM_STATE_READY;
  3665. __HAL_UNLOCK(htim);
  3666. return HAL_OK;
  3667. }
  3668. /**
  3669. * @brief Configures the TIM in Slave mode in interrupt mode
  3670. * @param htim TIM handle.
  3671. * @param sSlaveConfig pointer to a TIM_SlaveConfigTypeDef structure that
  3672. * contains the selected trigger (internal trigger input, filtered
  3673. * timer input or external trigger input) and the ) and the Slave
  3674. * mode (Disable, Reset, Gated, Trigger, External clock mode 1).
  3675. * @retval HAL status
  3676. */
  3677. HAL_StatusTypeDef HAL_TIM_SlaveConfigSynchronization_IT(TIM_HandleTypeDef *htim,
  3678. TIM_SlaveConfigTypeDef * sSlaveConfig)
  3679. {
  3680. /* Check the parameters */
  3681. assert_param(IS_TIM_SLAVE_INSTANCE(htim->Instance));
  3682. assert_param(IS_TIM_SLAVE_MODE(sSlaveConfig->SlaveMode));
  3683. assert_param(IS_TIM_TRIGGER_SELECTION(sSlaveConfig->InputTrigger));
  3684. __HAL_LOCK(htim);
  3685. htim->State = HAL_TIM_STATE_BUSY;
  3686. TIM_SlaveTimer_SetConfig(htim, sSlaveConfig);
  3687. /* Enable Trigger Interrupt */
  3688. __HAL_TIM_ENABLE_IT(htim, TIM_IT_TRIGGER);
  3689. /* Disable Trigger DMA request */
  3690. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_TRIGGER);
  3691. htim->State = HAL_TIM_STATE_READY;
  3692. __HAL_UNLOCK(htim);
  3693. return HAL_OK;
  3694. }
  3695. /**
  3696. * @brief Read the captured value from Capture Compare unit
  3697. * @param htim TIM handle.
  3698. * @param Channel TIM Channels to be enabled
  3699. * This parameter can be one of the following values:
  3700. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  3701. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  3702. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  3703. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  3704. * @retval Captured value
  3705. */
  3706. uint32_t HAL_TIM_ReadCapturedValue(TIM_HandleTypeDef *htim, uint32_t Channel)
  3707. {
  3708. uint32_t tmpreg = 0;
  3709. __HAL_LOCK(htim);
  3710. switch (Channel)
  3711. {
  3712. case TIM_CHANNEL_1:
  3713. {
  3714. /* Check the parameters */
  3715. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  3716. /* Return the capture 1 value */
  3717. tmpreg = htim->Instance->CCR1;
  3718. break;
  3719. }
  3720. case TIM_CHANNEL_2:
  3721. {
  3722. /* Check the parameters */
  3723. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  3724. /* Return the capture 2 value */
  3725. tmpreg = htim->Instance->CCR2;
  3726. break;
  3727. }
  3728. case TIM_CHANNEL_3:
  3729. {
  3730. /* Check the parameters */
  3731. assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
  3732. /* Return the capture 3 value */
  3733. tmpreg = htim->Instance->CCR3;
  3734. break;
  3735. }
  3736. case TIM_CHANNEL_4:
  3737. {
  3738. /* Check the parameters */
  3739. assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
  3740. /* Return the capture 4 value */
  3741. tmpreg = htim->Instance->CCR4;
  3742. break;
  3743. }
  3744. default:
  3745. break;
  3746. }
  3747. __HAL_UNLOCK(htim);
  3748. return tmpreg;
  3749. }
  3750. /**
  3751. * @}
  3752. */
  3753. /** @defgroup TIM_Exported_Functions_Group9 TIM Callbacks functions
  3754. * @brief TIM Callbacks functions
  3755. *
  3756. @verbatim
  3757. ==============================================================================
  3758. ##### TIM Callbacks functions #####
  3759. ==============================================================================
  3760. [..]
  3761. This section provides TIM callback functions:
  3762. (+) Timer Period elapsed callback
  3763. (+) Timer Output Compare callback
  3764. (+) Timer Input capture callback
  3765. (+) Timer Trigger callback
  3766. (+) Timer Error callback
  3767. @endverbatim
  3768. * @{
  3769. */
  3770. /**
  3771. * @brief Period elapsed callback in non-blocking mode
  3772. * @param htim TIM handle
  3773. * @retval None
  3774. */
  3775. __weak void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  3776. {
  3777. /* Prevent unused argument(s) compilation warning */
  3778. UNUSED(htim);
  3779. /* NOTE : This function should not be modified, when the callback is needed,
  3780. the __HAL_TIM_PeriodElapsedCallback could be implemented in the user file
  3781. */
  3782. }
  3783. /**
  3784. * @brief Output Compare callback in non-blocking mode
  3785. * @param htim TIM OC handle
  3786. * @retval None
  3787. */
  3788. __weak void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)
  3789. {
  3790. /* Prevent unused argument(s) compilation warning */
  3791. UNUSED(htim);
  3792. /* NOTE : This function should not be modified, when the callback is needed,
  3793. the __HAL_TIM_OC_DelayElapsedCallback could be implemented in the user file
  3794. */
  3795. }
  3796. /**
  3797. * @brief Input Capture callback in non-blocking mode
  3798. * @param htim TIM IC handle
  3799. * @retval None
  3800. */
  3801. __weak void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
  3802. {
  3803. /* Prevent unused argument(s) compilation warning */
  3804. UNUSED(htim);
  3805. /* NOTE : This function should not be modified, when the callback is needed,
  3806. the __HAL_TIM_IC_CaptureCallback could be implemented in the user file
  3807. */
  3808. }
  3809. /**
  3810. * @brief PWM Pulse finished callback in non-blocking mode
  3811. * @param htim TIM handle
  3812. * @retval None
  3813. */
  3814. __weak void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
  3815. {
  3816. /* Prevent unused argument(s) compilation warning */
  3817. UNUSED(htim);
  3818. /* NOTE : This function should not be modified, when the callback is needed,
  3819. the __HAL_TIM_PWM_PulseFinishedCallback could be implemented in the user file
  3820. */
  3821. }
  3822. /**
  3823. * @brief Hall Trigger detection callback in non-blocking mode
  3824. * @param htim TIM handle
  3825. * @retval None
  3826. */
  3827. __weak void HAL_TIM_TriggerCallback(TIM_HandleTypeDef *htim)
  3828. {
  3829. /* Prevent unused argument(s) compilation warning */
  3830. UNUSED(htim);
  3831. /* NOTE : This function should not be modified, when the callback is needed,
  3832. the HAL_TIM_TriggerCallback could be implemented in the user file
  3833. */
  3834. }
  3835. /**
  3836. * @brief Timer error callback in non-blocking mode
  3837. * @param htim TIM handle
  3838. * @retval None
  3839. */
  3840. __weak void HAL_TIM_ErrorCallback(TIM_HandleTypeDef *htim)
  3841. {
  3842. /* Prevent unused argument(s) compilation warning */
  3843. UNUSED(htim);
  3844. /* NOTE : This function should not be modified, when the callback is needed,
  3845. the HAL_TIM_ErrorCallback could be implemented in the user file
  3846. */
  3847. }
  3848. /**
  3849. * @}
  3850. */
  3851. /** @defgroup TIM_Exported_Functions_Group10 Peripheral State functions
  3852. * @brief Peripheral State functions
  3853. *
  3854. @verbatim
  3855. ==============================================================================
  3856. ##### Peripheral State functions #####
  3857. ==============================================================================
  3858. [..]
  3859. This subsection permits to get in run-time the status of the peripheral
  3860. and the data flow.
  3861. @endverbatim
  3862. * @{
  3863. */
  3864. /**
  3865. * @brief Return the TIM Base handle state.
  3866. * @param htim TIM Base handle
  3867. * @retval HAL state
  3868. */
  3869. HAL_TIM_StateTypeDef HAL_TIM_Base_GetState(TIM_HandleTypeDef *htim)
  3870. {
  3871. return htim->State;
  3872. }
  3873. /**
  3874. * @brief Return the TIM OC handle state.
  3875. * @param htim TIM Ouput Compare handle
  3876. * @retval HAL state
  3877. */
  3878. HAL_TIM_StateTypeDef HAL_TIM_OC_GetState(TIM_HandleTypeDef *htim)
  3879. {
  3880. return htim->State;
  3881. }
  3882. /**
  3883. * @brief Return the TIM PWM handle state.
  3884. * @param htim TIM handle
  3885. * @retval HAL state
  3886. */
  3887. HAL_TIM_StateTypeDef HAL_TIM_PWM_GetState(TIM_HandleTypeDef *htim)
  3888. {
  3889. return htim->State;
  3890. }
  3891. /**
  3892. * @brief Return the TIM Input Capture handle state.
  3893. * @param htim TIM IC handle
  3894. * @retval HAL state
  3895. */
  3896. HAL_TIM_StateTypeDef HAL_TIM_IC_GetState(TIM_HandleTypeDef *htim)
  3897. {
  3898. return htim->State;
  3899. }
  3900. /**
  3901. * @brief Return the TIM One Pulse Mode handle state.
  3902. * @param htim TIM OPM handle
  3903. * @retval HAL state
  3904. */
  3905. HAL_TIM_StateTypeDef HAL_TIM_OnePulse_GetState(TIM_HandleTypeDef *htim)
  3906. {
  3907. return htim->State;
  3908. }
  3909. /**
  3910. * @brief Return the TIM Encoder Mode handle state.
  3911. * @param htim TIM Encoder handle
  3912. * @retval HAL state
  3913. */
  3914. HAL_TIM_StateTypeDef HAL_TIM_Encoder_GetState(TIM_HandleTypeDef *htim)
  3915. {
  3916. return htim->State;
  3917. }
  3918. /**
  3919. * @}
  3920. */
  3921. /**
  3922. * @brief TIM DMA error callback
  3923. * @param hdma pointer to DMA handle.
  3924. * @retval None
  3925. */
  3926. void TIM_DMAError(DMA_HandleTypeDef *hdma)
  3927. {
  3928. TIM_HandleTypeDef* htim = ( TIM_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  3929. htim->State= HAL_TIM_STATE_READY;
  3930. HAL_TIM_ErrorCallback(htim);
  3931. }
  3932. /**
  3933. * @brief TIM DMA Delay Pulse complete callback.
  3934. * @param hdma pointer to DMA handle.
  3935. * @retval None
  3936. */
  3937. void TIM_DMADelayPulseCplt(DMA_HandleTypeDef *hdma)
  3938. {
  3939. TIM_HandleTypeDef* htim = ( TIM_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  3940. htim->State= HAL_TIM_STATE_READY;
  3941. if (hdma == htim->hdma[TIM_DMA_ID_CC1])
  3942. {
  3943. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
  3944. }
  3945. else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
  3946. {
  3947. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
  3948. }
  3949. else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
  3950. {
  3951. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
  3952. }
  3953. else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
  3954. {
  3955. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
  3956. }
  3957. HAL_TIM_PWM_PulseFinishedCallback(htim);
  3958. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  3959. }
  3960. /**
  3961. * @brief TIM DMA Capture complete callback.
  3962. * @param hdma pointer to DMA handle.
  3963. * @retval None
  3964. */
  3965. void TIM_DMACaptureCplt(DMA_HandleTypeDef *hdma)
  3966. {
  3967. TIM_HandleTypeDef* htim = ( TIM_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  3968. htim->State= HAL_TIM_STATE_READY;
  3969. if (hdma == htim->hdma[TIM_DMA_ID_CC1])
  3970. {
  3971. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
  3972. }
  3973. else if (hdma == htim->hdma[TIM_DMA_ID_CC2])
  3974. {
  3975. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_2;
  3976. }
  3977. else if (hdma == htim->hdma[TIM_DMA_ID_CC3])
  3978. {
  3979. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_3;
  3980. }
  3981. else if (hdma == htim->hdma[TIM_DMA_ID_CC4])
  3982. {
  3983. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_4;
  3984. }
  3985. HAL_TIM_IC_CaptureCallback(htim);
  3986. htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
  3987. }
  3988. /**
  3989. * @brief TIM DMA Period Elapse complete callback.
  3990. * @param hdma pointer to DMA handle.
  3991. * @retval None
  3992. */
  3993. static void TIM_DMAPeriodElapsedCplt(DMA_HandleTypeDef *hdma)
  3994. {
  3995. TIM_HandleTypeDef* htim = ( TIM_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  3996. htim->State= HAL_TIM_STATE_READY;
  3997. HAL_TIM_PeriodElapsedCallback(htim);
  3998. }
  3999. /**
  4000. * @brief TIM DMA Trigger callback.
  4001. * @param hdma pointer to DMA handle.
  4002. * @retval None
  4003. */
  4004. static void TIM_DMATriggerCplt(DMA_HandleTypeDef *hdma)
  4005. {
  4006. TIM_HandleTypeDef* htim = ( TIM_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  4007. htim->State= HAL_TIM_STATE_READY;
  4008. HAL_TIM_TriggerCallback(htim);
  4009. }
  4010. /**
  4011. * @brief Time Base configuration
  4012. * @param TIMx TIM peripheral
  4013. * @param Structure TIM Base configuration structure
  4014. * @retval None
  4015. */
  4016. void TIM_Base_SetConfig(TIM_TypeDef *TIMx, TIM_Base_InitTypeDef *Structure)
  4017. {
  4018. uint32_t tmpcr1 = 0;
  4019. tmpcr1 = TIMx->CR1;
  4020. /* Set TIM Time Base Unit parameters ---------------------------------------*/
  4021. if (IS_TIM_COUNTER_MODE_SELECT_INSTANCE(TIMx))
  4022. {
  4023. /* Select the Counter Mode */
  4024. tmpcr1 &= ~(TIM_CR1_DIR | TIM_CR1_CMS);
  4025. tmpcr1 |= Structure->CounterMode;
  4026. }
  4027. if(IS_TIM_CLOCK_DIVISION_INSTANCE(TIMx))
  4028. {
  4029. /* Set the clock division */
  4030. tmpcr1 &= ~TIM_CR1_CKD;
  4031. tmpcr1 |= (uint32_t)Structure->ClockDivision;
  4032. }
  4033. /* Set the auto-reload preload */
  4034. tmpcr1 &= ~TIM_CR1_ARPE;
  4035. tmpcr1 |= (uint32_t)Structure->AutoReloadPreload;
  4036. TIMx->CR1 = tmpcr1;
  4037. /* Set the Autoreload value */
  4038. TIMx->ARR = (uint32_t)Structure->Period ;
  4039. /* Set the Prescaler value */
  4040. TIMx->PSC = (uint32_t)Structure->Prescaler;
  4041. if (IS_TIM_REPETITION_COUNTER_INSTANCE(TIMx))
  4042. {
  4043. /* Set the Repetition Counter value */
  4044. TIMx->RCR = Structure->RepetitionCounter;
  4045. }
  4046. /* Generate an update event to reload the Prescaler
  4047. and the repetition counter(only for TIM1 and TIM8) value immediately */
  4048. TIMx->EGR = TIM_EGR_UG;
  4049. }
  4050. /**
  4051. * @brief Time Ouput Compare 1 configuration
  4052. * @param TIMx to select the TIM peripheral
  4053. * @param OC_Config The ouput configuration structure
  4054. * @retval None
  4055. */
  4056. static void TIM_OC1_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config)
  4057. {
  4058. uint32_t tmpccmrx = 0;
  4059. uint32_t tmpccer = 0;
  4060. uint32_t tmpcr2 = 0;
  4061. /* Disable the Channel 1: Reset the CC1E Bit */
  4062. TIMx->CCER &= ~TIM_CCER_CC1E;
  4063. /* Get the TIMx CCER register value */
  4064. tmpccer = TIMx->CCER;
  4065. /* Get the TIMx CR2 register value */
  4066. tmpcr2 = TIMx->CR2;
  4067. /* Get the TIMx CCMR1 register value */
  4068. tmpccmrx = TIMx->CCMR1;
  4069. /* Reset the Output Compare Mode Bits */
  4070. tmpccmrx &= ~TIM_CCMR1_OC1M;
  4071. tmpccmrx &= ~TIM_CCMR1_CC1S;
  4072. /* Select the Output Compare Mode */
  4073. tmpccmrx |= OC_Config->OCMode;
  4074. /* Reset the Output Polarity level */
  4075. tmpccer &= ~TIM_CCER_CC1P;
  4076. /* Set the Output Compare Polarity */
  4077. tmpccer |= OC_Config->OCPolarity;
  4078. if(IS_TIM_CCXN_INSTANCE(TIMx, TIM_CHANNEL_1))
  4079. {
  4080. /* Check parameters */
  4081. assert_param(IS_TIM_OCN_POLARITY(OC_Config->OCNPolarity));
  4082. /* Reset the Output N Polarity level */
  4083. tmpccer &= ~TIM_CCER_CC1NP;
  4084. /* Set the Output N Polarity */
  4085. tmpccer |= OC_Config->OCNPolarity;
  4086. /* Reset the Output N State */
  4087. tmpccer &= ~TIM_CCER_CC1NE;
  4088. }
  4089. if(IS_TIM_BREAK_INSTANCE(TIMx))
  4090. {
  4091. /* Check parameters */
  4092. assert_param(IS_TIM_OCNIDLE_STATE(OC_Config->OCNIdleState));
  4093. assert_param(IS_TIM_OCIDLE_STATE(OC_Config->OCIdleState));
  4094. /* Reset the Output Compare and Output Compare N IDLE State */
  4095. tmpcr2 &= ~TIM_CR2_OIS1;
  4096. tmpcr2 &= ~TIM_CR2_OIS1N;
  4097. /* Set the Output Idle state */
  4098. tmpcr2 |= OC_Config->OCIdleState;
  4099. /* Set the Output N Idle state */
  4100. tmpcr2 |= OC_Config->OCNIdleState;
  4101. }
  4102. /* Write to TIMx CR2 */
  4103. TIMx->CR2 = tmpcr2;
  4104. /* Write to TIMx CCMR1 */
  4105. TIMx->CCMR1 = tmpccmrx;
  4106. /* Set the Capture Compare Register value */
  4107. TIMx->CCR1 = OC_Config->Pulse;
  4108. /* Write to TIMx CCER */
  4109. TIMx->CCER = tmpccer;
  4110. }
  4111. /**
  4112. * @brief Time Ouput Compare 2 configuration
  4113. * @param TIMx to select the TIM peripheral
  4114. * @param OC_Config The ouput configuration structure
  4115. * @retval None
  4116. */
  4117. void TIM_OC2_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config)
  4118. {
  4119. uint32_t tmpccmrx = 0;
  4120. uint32_t tmpccer = 0;
  4121. uint32_t tmpcr2 = 0;
  4122. /* Disable the Channel 2: Reset the CC2E Bit */
  4123. TIMx->CCER &= ~TIM_CCER_CC2E;
  4124. /* Get the TIMx CCER register value */
  4125. tmpccer = TIMx->CCER;
  4126. /* Get the TIMx CR2 register value */
  4127. tmpcr2 = TIMx->CR2;
  4128. /* Get the TIMx CCMR1 register value */
  4129. tmpccmrx = TIMx->CCMR1;
  4130. /* Reset the Output Compare mode and Capture/Compare selection Bits */
  4131. tmpccmrx &= ~TIM_CCMR1_OC2M;
  4132. tmpccmrx &= ~TIM_CCMR1_CC2S;
  4133. /* Select the Output Compare Mode */
  4134. tmpccmrx |= (OC_Config->OCMode << 8);
  4135. /* Reset the Output Polarity level */
  4136. tmpccer &= ~TIM_CCER_CC2P;
  4137. /* Set the Output Compare Polarity */
  4138. tmpccer |= (OC_Config->OCPolarity << 4);
  4139. if(IS_TIM_CCXN_INSTANCE(TIMx, TIM_CHANNEL_2))
  4140. {
  4141. assert_param(IS_TIM_OCN_POLARITY(OC_Config->OCNPolarity));
  4142. /* Reset the Output N Polarity level */
  4143. tmpccer &= ~TIM_CCER_CC2NP;
  4144. /* Set the Output N Polarity */
  4145. tmpccer |= (OC_Config->OCNPolarity << 4);
  4146. /* Reset the Output N State */
  4147. tmpccer &= ~TIM_CCER_CC2NE;
  4148. }
  4149. if(IS_TIM_BREAK_INSTANCE(TIMx))
  4150. {
  4151. /* Check parameters */
  4152. assert_param(IS_TIM_OCNIDLE_STATE(OC_Config->OCNIdleState));
  4153. assert_param(IS_TIM_OCIDLE_STATE(OC_Config->OCIdleState));
  4154. /* Reset the Output Compare and Output Compare N IDLE State */
  4155. tmpcr2 &= ~TIM_CR2_OIS2;
  4156. tmpcr2 &= ~TIM_CR2_OIS2N;
  4157. /* Set the Output Idle state */
  4158. tmpcr2 |= (OC_Config->OCIdleState << 2);
  4159. /* Set the Output N Idle state */
  4160. tmpcr2 |= (OC_Config->OCNIdleState << 2);
  4161. }
  4162. /* Write to TIMx CR2 */
  4163. TIMx->CR2 = tmpcr2;
  4164. /* Write to TIMx CCMR1 */
  4165. TIMx->CCMR1 = tmpccmrx;
  4166. /* Set the Capture Compare Register value */
  4167. TIMx->CCR2 = OC_Config->Pulse;
  4168. /* Write to TIMx CCER */
  4169. TIMx->CCER = tmpccer;
  4170. }
  4171. /**
  4172. * @brief Time Ouput Compare 3 configuration
  4173. * @param TIMx to select the TIM peripheral
  4174. * @param OC_Config The ouput configuration structure
  4175. * @retval None
  4176. */
  4177. static void TIM_OC3_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config)
  4178. {
  4179. uint32_t tmpccmrx = 0;
  4180. uint32_t tmpccer = 0;
  4181. uint32_t tmpcr2 = 0;
  4182. /* Disable the Channel 3: Reset the CC2E Bit */
  4183. TIMx->CCER &= ~TIM_CCER_CC3E;
  4184. /* Get the TIMx CCER register value */
  4185. tmpccer = TIMx->CCER;
  4186. /* Get the TIMx CR2 register value */
  4187. tmpcr2 = TIMx->CR2;
  4188. /* Get the TIMx CCMR2 register value */
  4189. tmpccmrx = TIMx->CCMR2;
  4190. /* Reset the Output Compare mode and Capture/Compare selection Bits */
  4191. tmpccmrx &= ~TIM_CCMR2_OC3M;
  4192. tmpccmrx &= ~TIM_CCMR2_CC3S;
  4193. /* Select the Output Compare Mode */
  4194. tmpccmrx |= OC_Config->OCMode;
  4195. /* Reset the Output Polarity level */
  4196. tmpccer &= ~TIM_CCER_CC3P;
  4197. /* Set the Output Compare Polarity */
  4198. tmpccer |= (OC_Config->OCPolarity << 8);
  4199. if(IS_TIM_CCXN_INSTANCE(TIMx, TIM_CHANNEL_3))
  4200. {
  4201. assert_param(IS_TIM_OCN_POLARITY(OC_Config->OCNPolarity));
  4202. /* Reset the Output N Polarity level */
  4203. tmpccer &= ~TIM_CCER_CC3NP;
  4204. /* Set the Output N Polarity */
  4205. tmpccer |= (OC_Config->OCNPolarity << 8);
  4206. /* Reset the Output N State */
  4207. tmpccer &= ~TIM_CCER_CC3NE;
  4208. }
  4209. if(IS_TIM_BREAK_INSTANCE(TIMx))
  4210. {
  4211. /* Check parameters */
  4212. assert_param(IS_TIM_OCNIDLE_STATE(OC_Config->OCNIdleState));
  4213. assert_param(IS_TIM_OCIDLE_STATE(OC_Config->OCIdleState));
  4214. /* Reset the Output Compare and Output Compare N IDLE State */
  4215. tmpcr2 &= ~TIM_CR2_OIS3;
  4216. tmpcr2 &= ~TIM_CR2_OIS3N;
  4217. /* Set the Output Idle state */
  4218. tmpcr2 |= (OC_Config->OCIdleState << 4);
  4219. /* Set the Output N Idle state */
  4220. tmpcr2 |= (OC_Config->OCNIdleState << 4);
  4221. }
  4222. /* Write to TIMx CR2 */
  4223. TIMx->CR2 = tmpcr2;
  4224. /* Write to TIMx CCMR2 */
  4225. TIMx->CCMR2 = tmpccmrx;
  4226. /* Set the Capture Compare Register value */
  4227. TIMx->CCR3 = OC_Config->Pulse;
  4228. /* Write to TIMx CCER */
  4229. TIMx->CCER = tmpccer;
  4230. }
  4231. /**
  4232. * @brief Time Ouput Compare 4 configuration
  4233. * @param TIMx to select the TIM peripheral
  4234. * @param OC_Config The ouput configuration structure
  4235. * @retval None
  4236. */
  4237. static void TIM_OC4_SetConfig(TIM_TypeDef *TIMx, TIM_OC_InitTypeDef *OC_Config)
  4238. {
  4239. uint32_t tmpccmrx = 0;
  4240. uint32_t tmpccer = 0;
  4241. uint32_t tmpcr2 = 0;
  4242. /* Disable the Channel 4: Reset the CC4E Bit */
  4243. TIMx->CCER &= ~TIM_CCER_CC4E;
  4244. /* Get the TIMx CCER register value */
  4245. tmpccer = TIMx->CCER;
  4246. /* Get the TIMx CR2 register value */
  4247. tmpcr2 = TIMx->CR2;
  4248. /* Get the TIMx CCMR2 register value */
  4249. tmpccmrx = TIMx->CCMR2;
  4250. /* Reset the Output Compare mode and Capture/Compare selection Bits */
  4251. tmpccmrx &= ~TIM_CCMR2_OC4M;
  4252. tmpccmrx &= ~TIM_CCMR2_CC4S;
  4253. /* Select the Output Compare Mode */
  4254. tmpccmrx |= (OC_Config->OCMode << 8);
  4255. /* Reset the Output Polarity level */
  4256. tmpccer &= ~TIM_CCER_CC4P;
  4257. /* Set the Output Compare Polarity */
  4258. tmpccer |= (OC_Config->OCPolarity << 12);
  4259. if(IS_TIM_BREAK_INSTANCE(TIMx))
  4260. {
  4261. assert_param(IS_TIM_OCIDLE_STATE(OC_Config->OCIdleState));
  4262. /* Reset the Output Compare IDLE State */
  4263. tmpcr2 &= ~TIM_CR2_OIS4;
  4264. /* Set the Output Idle state */
  4265. tmpcr2 |= (OC_Config->OCIdleState << 6);
  4266. }
  4267. /* Write to TIMx CR2 */
  4268. TIMx->CR2 = tmpcr2;
  4269. /* Write to TIMx CCMR2 */
  4270. TIMx->CCMR2 = tmpccmrx;
  4271. /* Set the Capture Compare Register value */
  4272. TIMx->CCR4 = OC_Config->Pulse;
  4273. /* Write to TIMx CCER */
  4274. TIMx->CCER = tmpccer;
  4275. }
  4276. /**
  4277. * @brief Timer Ouput Compare 5 configuration
  4278. * @param TIMx to select the TIM peripheral
  4279. * @param OC_Config The ouput configuration structure
  4280. * @retval None
  4281. */
  4282. static void TIM_OC5_SetConfig(TIM_TypeDef *TIMx,
  4283. TIM_OC_InitTypeDef *OC_Config)
  4284. {
  4285. uint32_t tmpccmrx = 0;
  4286. uint32_t tmpccer = 0;
  4287. uint32_t tmpcr2 = 0;
  4288. /* Disable the output: Reset the CCxE Bit */
  4289. TIMx->CCER &= ~TIM_CCER_CC5E;
  4290. /* Get the TIMx CCER register value */
  4291. tmpccer = TIMx->CCER;
  4292. /* Get the TIMx CR2 register value */
  4293. tmpcr2 = TIMx->CR2;
  4294. /* Get the TIMx CCMR1 register value */
  4295. tmpccmrx = TIMx->CCMR3;
  4296. /* Reset the Output Compare Mode Bits */
  4297. tmpccmrx &= ~(TIM_CCMR3_OC5M);
  4298. /* Select the Output Compare Mode */
  4299. tmpccmrx |= OC_Config->OCMode;
  4300. /* Reset the Output Polarity level */
  4301. tmpccer &= ~TIM_CCER_CC5P;
  4302. /* Set the Output Compare Polarity */
  4303. tmpccer |= (OC_Config->OCPolarity << 16);
  4304. if(IS_TIM_BREAK_INSTANCE(TIMx))
  4305. {
  4306. /* Reset the Output Compare IDLE State */
  4307. tmpcr2 &= ~TIM_CR2_OIS5;
  4308. /* Set the Output Idle state */
  4309. tmpcr2 |= (OC_Config->OCIdleState << 8);
  4310. }
  4311. /* Write to TIMx CR2 */
  4312. TIMx->CR2 = tmpcr2;
  4313. /* Write to TIMx CCMR3 */
  4314. TIMx->CCMR3 = tmpccmrx;
  4315. /* Set the Capture Compare Register value */
  4316. TIMx->CCR5 = OC_Config->Pulse;
  4317. /* Write to TIMx CCER */
  4318. TIMx->CCER = tmpccer;
  4319. }
  4320. /**
  4321. * @brief Timer Ouput Compare 6 configuration
  4322. * @param TIMx to select the TIM peripheral
  4323. * @param OC_Config The ouput configuration structure
  4324. * @retval None
  4325. */
  4326. static void TIM_OC6_SetConfig(TIM_TypeDef *TIMx,
  4327. TIM_OC_InitTypeDef *OC_Config)
  4328. {
  4329. uint32_t tmpccmrx = 0;
  4330. uint32_t tmpccer = 0;
  4331. uint32_t tmpcr2 = 0;
  4332. /* Disable the output: Reset the CCxE Bit */
  4333. TIMx->CCER &= ~TIM_CCER_CC6E;
  4334. /* Get the TIMx CCER register value */
  4335. tmpccer = TIMx->CCER;
  4336. /* Get the TIMx CR2 register value */
  4337. tmpcr2 = TIMx->CR2;
  4338. /* Get the TIMx CCMR1 register value */
  4339. tmpccmrx = TIMx->CCMR3;
  4340. /* Reset the Output Compare Mode Bits */
  4341. tmpccmrx &= ~(TIM_CCMR3_OC6M);
  4342. /* Select the Output Compare Mode */
  4343. tmpccmrx |= (OC_Config->OCMode << 8);
  4344. /* Reset the Output Polarity level */
  4345. tmpccer &= (uint32_t)~TIM_CCER_CC6P;
  4346. /* Set the Output Compare Polarity */
  4347. tmpccer |= (OC_Config->OCPolarity << 20);
  4348. if(IS_TIM_BREAK_INSTANCE(TIMx))
  4349. {
  4350. /* Reset the Output Compare IDLE State */
  4351. tmpcr2 &= ~TIM_CR2_OIS6;
  4352. /* Set the Output Idle state */
  4353. tmpcr2 |= (OC_Config->OCIdleState << 10);
  4354. }
  4355. /* Write to TIMx CR2 */
  4356. TIMx->CR2 = tmpcr2;
  4357. /* Write to TIMx CCMR3 */
  4358. TIMx->CCMR3 = tmpccmrx;
  4359. /* Set the Capture Compare Register value */
  4360. TIMx->CCR6 = OC_Config->Pulse;
  4361. /* Write to TIMx CCER */
  4362. TIMx->CCER = tmpccer;
  4363. }
  4364. static void TIM_SlaveTimer_SetConfig(TIM_HandleTypeDef *htim,
  4365. TIM_SlaveConfigTypeDef * sSlaveConfig)
  4366. {
  4367. uint32_t tmpsmcr = 0;
  4368. uint32_t tmpccmr1 = 0;
  4369. uint32_t tmpccer = 0;
  4370. /* Get the TIMx SMCR register value */
  4371. tmpsmcr = htim->Instance->SMCR;
  4372. /* Reset the Trigger Selection Bits */
  4373. tmpsmcr &= ~TIM_SMCR_TS;
  4374. /* Set the Input Trigger source */
  4375. tmpsmcr |= sSlaveConfig->InputTrigger;
  4376. /* Reset the slave mode Bits */
  4377. tmpsmcr &= ~TIM_SMCR_SMS;
  4378. /* Set the slave mode */
  4379. tmpsmcr |= sSlaveConfig->SlaveMode;
  4380. /* Write to TIMx SMCR */
  4381. htim->Instance->SMCR = tmpsmcr;
  4382. /* Configure the trigger prescaler, filter, and polarity */
  4383. switch (sSlaveConfig->InputTrigger)
  4384. {
  4385. case TIM_TS_ETRF:
  4386. {
  4387. /* Check the parameters */
  4388. assert_param(IS_TIM_CLOCKSOURCE_ETRMODE1_INSTANCE(htim->Instance));
  4389. assert_param(IS_TIM_TRIGGERPRESCALER(sSlaveConfig->TriggerPrescaler));
  4390. assert_param(IS_TIM_TRIGGERPOLARITY(sSlaveConfig->TriggerPolarity));
  4391. assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));
  4392. /* Configure the ETR Trigger source */
  4393. TIM_ETR_SetConfig(htim->Instance,
  4394. sSlaveConfig->TriggerPrescaler,
  4395. sSlaveConfig->TriggerPolarity,
  4396. sSlaveConfig->TriggerFilter);
  4397. }
  4398. break;
  4399. case TIM_TS_TI1F_ED:
  4400. {
  4401. /* Check the parameters */
  4402. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  4403. assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));
  4404. /* Disable the Channel 1: Reset the CC1E Bit */
  4405. tmpccer = htim->Instance->CCER;
  4406. htim->Instance->CCER &= ~TIM_CCER_CC1E;
  4407. tmpccmr1 = htim->Instance->CCMR1;
  4408. /* Set the filter */
  4409. tmpccmr1 &= ~TIM_CCMR1_IC1F;
  4410. tmpccmr1 |= ((sSlaveConfig->TriggerFilter) << 4);
  4411. /* Write to TIMx CCMR1 and CCER registers */
  4412. htim->Instance->CCMR1 = tmpccmr1;
  4413. htim->Instance->CCER = tmpccer;
  4414. }
  4415. break;
  4416. case TIM_TS_TI1FP1:
  4417. {
  4418. /* Check the parameters */
  4419. assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
  4420. assert_param(IS_TIM_TRIGGERPOLARITY(sSlaveConfig->TriggerPolarity));
  4421. assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));
  4422. /* Configure TI1 Filter and Polarity */
  4423. TIM_TI1_ConfigInputStage(htim->Instance,
  4424. sSlaveConfig->TriggerPolarity,
  4425. sSlaveConfig->TriggerFilter);
  4426. }
  4427. break;
  4428. case TIM_TS_TI2FP2:
  4429. {
  4430. /* Check the parameters */
  4431. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  4432. assert_param(IS_TIM_TRIGGERPOLARITY(sSlaveConfig->TriggerPolarity));
  4433. assert_param(IS_TIM_TRIGGERFILTER(sSlaveConfig->TriggerFilter));
  4434. /* Configure TI2 Filter and Polarity */
  4435. TIM_TI2_ConfigInputStage(htim->Instance,
  4436. sSlaveConfig->TriggerPolarity,
  4437. sSlaveConfig->TriggerFilter);
  4438. }
  4439. break;
  4440. case TIM_TS_ITR0:
  4441. {
  4442. /* Check the parameter */
  4443. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  4444. }
  4445. break;
  4446. case TIM_TS_ITR1:
  4447. {
  4448. /* Check the parameter */
  4449. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  4450. }
  4451. break;
  4452. case TIM_TS_ITR2:
  4453. {
  4454. /* Check the parameter */
  4455. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  4456. }
  4457. break;
  4458. case TIM_TS_ITR3:
  4459. {
  4460. /* Check the parameter */
  4461. assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
  4462. }
  4463. break;
  4464. default:
  4465. break;
  4466. }
  4467. }
  4468. /**
  4469. * @brief Configure the TI1 as Input.
  4470. * @param TIMx to select the TIM peripheral.
  4471. * @param TIM_ICPolarity The Input Polarity.
  4472. * This parameter can be one of the following values:
  4473. * @arg TIM_ICPolarity_Rising
  4474. * @arg TIM_ICPolarity_Falling
  4475. * @arg TIM_ICPolarity_BothEdge
  4476. * @param TIM_ICSelection specifies the input to be used.
  4477. * This parameter can be one of the following values:
  4478. * @arg TIM_ICSelection_DirectTI: TIM Input 1 is selected to be connected to IC1.
  4479. * @arg TIM_ICSelection_IndirectTI: TIM Input 1 is selected to be connected to IC2.
  4480. * @arg TIM_ICSelection_TRC: TIM Input 1 is selected to be connected to TRC.
  4481. * @param TIM_ICFilter Specifies the Input Capture Filter.
  4482. * This parameter must be a value between 0x00 and 0x0F.
  4483. * @retval None
  4484. * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI2FP1
  4485. * (on channel2 path) is used as the input signal. Therefore CCMR1 must be
  4486. * protected against un-initialized filter and polarity values.
  4487. */
  4488. void TIM_TI1_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
  4489. uint32_t TIM_ICFilter)
  4490. {
  4491. uint32_t tmpccmr1 = 0;
  4492. uint32_t tmpccer = 0;
  4493. /* Disable the Channel 1: Reset the CC1E Bit */
  4494. TIMx->CCER &= ~TIM_CCER_CC1E;
  4495. tmpccmr1 = TIMx->CCMR1;
  4496. tmpccer = TIMx->CCER;
  4497. /* Select the Input */
  4498. if(IS_TIM_CC2_INSTANCE(TIMx) != RESET)
  4499. {
  4500. tmpccmr1 &= ~TIM_CCMR1_CC1S;
  4501. tmpccmr1 |= TIM_ICSelection;
  4502. }
  4503. else
  4504. {
  4505. tmpccmr1 |= TIM_CCMR1_CC1S_0;
  4506. }
  4507. /* Set the filter */
  4508. tmpccmr1 &= ~TIM_CCMR1_IC1F;
  4509. tmpccmr1 |= ((TIM_ICFilter << 4) & TIM_CCMR1_IC1F);
  4510. /* Select the Polarity and set the CC1E Bit */
  4511. tmpccer &= ~(TIM_CCER_CC1P | TIM_CCER_CC1NP);
  4512. tmpccer |= (TIM_ICPolarity & (TIM_CCER_CC1P | TIM_CCER_CC1NP));
  4513. /* Write to TIMx CCMR1 and CCER registers */
  4514. TIMx->CCMR1 = tmpccmr1;
  4515. TIMx->CCER = tmpccer;
  4516. }
  4517. /**
  4518. * @brief Configure the Polarity and Filter for TI1.
  4519. * @param TIMx to select the TIM peripheral.
  4520. * @param TIM_ICPolarity The Input Polarity.
  4521. * This parameter can be one of the following values:
  4522. * @arg TIM_ICPolarity_Rising
  4523. * @arg TIM_ICPolarity_Falling
  4524. * @arg TIM_ICPolarity_BothEdge
  4525. * @param TIM_ICFilter Specifies the Input Capture Filter.
  4526. * This parameter must be a value between 0x00 and 0x0F.
  4527. * @retval None
  4528. */
  4529. static void TIM_TI1_ConfigInputStage(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICFilter)
  4530. {
  4531. uint32_t tmpccmr1 = 0;
  4532. uint32_t tmpccer = 0;
  4533. /* Disable the Channel 1: Reset the CC1E Bit */
  4534. tmpccer = TIMx->CCER;
  4535. TIMx->CCER &= ~TIM_CCER_CC1E;
  4536. tmpccmr1 = TIMx->CCMR1;
  4537. /* Set the filter */
  4538. tmpccmr1 &= ~TIM_CCMR1_IC1F;
  4539. tmpccmr1 |= (TIM_ICFilter << 4);
  4540. /* Select the Polarity and set the CC1E Bit */
  4541. tmpccer &= ~(TIM_CCER_CC1P | TIM_CCER_CC1NP);
  4542. tmpccer |= TIM_ICPolarity;
  4543. /* Write to TIMx CCMR1 and CCER registers */
  4544. TIMx->CCMR1 = tmpccmr1;
  4545. TIMx->CCER = tmpccer;
  4546. }
  4547. /**
  4548. * @brief Configure the TI2 as Input.
  4549. * @param TIMx to select the TIM peripheral
  4550. * @param TIM_ICPolarity The Input Polarity.
  4551. * This parameter can be one of the following values:
  4552. * @arg TIM_ICPolarity_Rising
  4553. * @arg TIM_ICPolarity_Falling
  4554. * @arg TIM_ICPolarity_BothEdge
  4555. * @param TIM_ICSelection specifies the input to be used.
  4556. * This parameter can be one of the following values:
  4557. * @arg TIM_ICSelection_DirectTI: TIM Input 2 is selected to be connected to IC2.
  4558. * @arg TIM_ICSelection_IndirectTI: TIM Input 2 is selected to be connected to IC1.
  4559. * @arg TIM_ICSelection_TRC: TIM Input 2 is selected to be connected to TRC.
  4560. * @param TIM_ICFilter Specifies the Input Capture Filter.
  4561. * This parameter must be a value between 0x00 and 0x0F.
  4562. * @retval None
  4563. * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI1FP2
  4564. * (on channel1 path) is used as the input signal. Therefore CCMR1 must be
  4565. * protected against un-initialized filter and polarity values.
  4566. */
  4567. static void TIM_TI2_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
  4568. uint32_t TIM_ICFilter)
  4569. {
  4570. uint32_t tmpccmr1 = 0;
  4571. uint32_t tmpccer = 0;
  4572. /* Disable the Channel 2: Reset the CC2E Bit */
  4573. TIMx->CCER &= ~TIM_CCER_CC2E;
  4574. tmpccmr1 = TIMx->CCMR1;
  4575. tmpccer = TIMx->CCER;
  4576. /* Select the Input */
  4577. tmpccmr1 &= ~TIM_CCMR1_CC2S;
  4578. tmpccmr1 |= (TIM_ICSelection << 8);
  4579. /* Set the filter */
  4580. tmpccmr1 &= ~TIM_CCMR1_IC2F;
  4581. tmpccmr1 |= ((TIM_ICFilter << 12) & TIM_CCMR1_IC2F);
  4582. /* Select the Polarity and set the CC2E Bit */
  4583. tmpccer &= ~(TIM_CCER_CC2P | TIM_CCER_CC2NP);
  4584. tmpccer |= ((TIM_ICPolarity << 4) & (TIM_CCER_CC2P | TIM_CCER_CC2NP));
  4585. /* Write to TIMx CCMR1 and CCER registers */
  4586. TIMx->CCMR1 = tmpccmr1 ;
  4587. TIMx->CCER = tmpccer;
  4588. }
  4589. /**
  4590. * @brief Configure the Polarity and Filter for TI2.
  4591. * @param TIMx to select the TIM peripheral.
  4592. * @param TIM_ICPolarity The Input Polarity.
  4593. * This parameter can be one of the following values:
  4594. * @arg TIM_ICPolarity_Rising
  4595. * @arg TIM_ICPolarity_Falling
  4596. * @arg TIM_ICPolarity_BothEdge
  4597. * @param TIM_ICFilter Specifies the Input Capture Filter.
  4598. * This parameter must be a value between 0x00 and 0x0F.
  4599. * @retval None
  4600. */
  4601. static void TIM_TI2_ConfigInputStage(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICFilter)
  4602. {
  4603. uint32_t tmpccmr1 = 0;
  4604. uint32_t tmpccer = 0;
  4605. /* Disable the Channel 2: Reset the CC2E Bit */
  4606. TIMx->CCER &= ~TIM_CCER_CC2E;
  4607. tmpccmr1 = TIMx->CCMR1;
  4608. tmpccer = TIMx->CCER;
  4609. /* Set the filter */
  4610. tmpccmr1 &= ~TIM_CCMR1_IC2F;
  4611. tmpccmr1 |= (TIM_ICFilter << 12);
  4612. /* Select the Polarity and set the CC2E Bit */
  4613. tmpccer &= ~(TIM_CCER_CC2P | TIM_CCER_CC2NP);
  4614. tmpccer |= (TIM_ICPolarity << 4);
  4615. /* Write to TIMx CCMR1 and CCER registers */
  4616. TIMx->CCMR1 = tmpccmr1 ;
  4617. TIMx->CCER = tmpccer;
  4618. }
  4619. /**
  4620. * @brief Configure the TI3 as Input.
  4621. * @param TIMx to select the TIM peripheral
  4622. * @param TIM_ICPolarity The Input Polarity.
  4623. * This parameter can be one of the following values:
  4624. * @arg TIM_ICPolarity_Rising
  4625. * @arg TIM_ICPolarity_Falling
  4626. * @arg TIM_ICPolarity_BothEdge
  4627. * @param TIM_ICSelection specifies the input to be used.
  4628. * This parameter can be one of the following values:
  4629. * @arg TIM_ICSelection_DirectTI: TIM Input 3 is selected to be connected to IC3.
  4630. * @arg TIM_ICSelection_IndirectTI: TIM Input 3 is selected to be connected to IC4.
  4631. * @arg TIM_ICSelection_TRC: TIM Input 3 is selected to be connected to TRC.
  4632. * @param TIM_ICFilter Specifies the Input Capture Filter.
  4633. * This parameter must be a value between 0x00 and 0x0F.
  4634. * @retval None
  4635. * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI3FP4
  4636. * (on channel1 path) is used as the input signal. Therefore CCMR2 must be
  4637. * protected against un-initialized filter and polarity values.
  4638. */
  4639. static void TIM_TI3_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
  4640. uint32_t TIM_ICFilter)
  4641. {
  4642. uint32_t tmpccmr2 = 0;
  4643. uint32_t tmpccer = 0;
  4644. /* Disable the Channel 3: Reset the CC3E Bit */
  4645. TIMx->CCER &= ~TIM_CCER_CC3E;
  4646. tmpccmr2 = TIMx->CCMR2;
  4647. tmpccer = TIMx->CCER;
  4648. /* Select the Input */
  4649. tmpccmr2 &= ~TIM_CCMR2_CC3S;
  4650. tmpccmr2 |= TIM_ICSelection;
  4651. /* Set the filter */
  4652. tmpccmr2 &= ~TIM_CCMR2_IC3F;
  4653. tmpccmr2 |= ((TIM_ICFilter << 4) & TIM_CCMR2_IC3F);
  4654. /* Select the Polarity and set the CC3E Bit */
  4655. tmpccer &= ~(TIM_CCER_CC3P | TIM_CCER_CC3NP);
  4656. tmpccer |= ((TIM_ICPolarity << 8) & (TIM_CCER_CC3P | TIM_CCER_CC3NP));
  4657. /* Write to TIMx CCMR2 and CCER registers */
  4658. TIMx->CCMR2 = tmpccmr2;
  4659. TIMx->CCER = tmpccer;
  4660. }
  4661. /**
  4662. * @brief Configure the TI4 as Input.
  4663. * @param TIMx to select the TIM peripheral
  4664. * @param TIM_ICPolarity The Input Polarity.
  4665. * This parameter can be one of the following values:
  4666. * @arg TIM_ICPolarity_Rising
  4667. * @arg TIM_ICPolarity_Falling
  4668. * @arg TIM_ICPolarity_BothEdge
  4669. * @param TIM_ICSelection specifies the input to be used.
  4670. * This parameter can be one of the following values:
  4671. * @arg TIM_ICSelection_DirectTI: TIM Input 4 is selected to be connected to IC4.
  4672. * @arg TIM_ICSelection_IndirectTI: TIM Input 4 is selected to be connected to IC3.
  4673. * @arg TIM_ICSelection_TRC: TIM Input 4 is selected to be connected to TRC.
  4674. * @param TIM_ICFilter Specifies the Input Capture Filter.
  4675. * This parameter must be a value between 0x00 and 0x0F.
  4676. * @note TIM_ICFilter and TIM_ICPolarity are not used in INDIRECT mode as TI4FP3
  4677. * (on channel1 path) is used as the input signal. Therefore CCMR2 must be
  4678. * protected against un-initialized filter and polarity values.
  4679. * @retval None
  4680. */
  4681. static void TIM_TI4_SetConfig(TIM_TypeDef *TIMx, uint32_t TIM_ICPolarity, uint32_t TIM_ICSelection,
  4682. uint32_t TIM_ICFilter)
  4683. {
  4684. uint32_t tmpccmr2 = 0;
  4685. uint32_t tmpccer = 0;
  4686. /* Disable the Channel 4: Reset the CC4E Bit */
  4687. TIMx->CCER &= ~TIM_CCER_CC4E;
  4688. tmpccmr2 = TIMx->CCMR2;
  4689. tmpccer = TIMx->CCER;
  4690. /* Select the Input */
  4691. tmpccmr2 &= ~TIM_CCMR2_CC4S;
  4692. tmpccmr2 |= (TIM_ICSelection << 8);
  4693. /* Set the filter */
  4694. tmpccmr2 &= ~TIM_CCMR2_IC4F;
  4695. tmpccmr2 |= ((TIM_ICFilter << 12) & TIM_CCMR2_IC4F);
  4696. /* Select the Polarity and set the CC4E Bit */
  4697. tmpccer &= ~(TIM_CCER_CC4P | TIM_CCER_CC4NP);
  4698. tmpccer |= ((TIM_ICPolarity << 12) & (TIM_CCER_CC4P | TIM_CCER_CC4NP));
  4699. /* Write to TIMx CCMR2 and CCER registers */
  4700. TIMx->CCMR2 = tmpccmr2;
  4701. TIMx->CCER = tmpccer ;
  4702. }
  4703. /**
  4704. * @brief Selects the Input Trigger source
  4705. * @param TIMx to select the TIM peripheral
  4706. * @param InputTriggerSource The Input Trigger source.
  4707. * This parameter can be one of the following values:
  4708. * @arg TIM_TS_ITR0: Internal Trigger 0
  4709. * @arg TIM_TS_ITR1: Internal Trigger 1
  4710. * @arg TIM_TS_ITR2: Internal Trigger 2
  4711. * @arg TIM_TS_ITR3: Internal Trigger 3
  4712. * @arg TIM_TS_TI1F_ED: TI1 Edge Detector
  4713. * @arg TIM_TS_TI1FP1: Filtered Timer Input 1
  4714. * @arg TIM_TS_TI2FP2: Filtered Timer Input 2
  4715. * @arg TIM_TS_ETRF: External Trigger input
  4716. * @retval None
  4717. */
  4718. static void TIM_ITRx_SetConfig(TIM_TypeDef *TIMx, uint16_t InputTriggerSource)
  4719. {
  4720. uint32_t tmpsmcr = 0;
  4721. /* Get the TIMx SMCR register value */
  4722. tmpsmcr = TIMx->SMCR;
  4723. /* Reset the TS Bits */
  4724. tmpsmcr &= ~TIM_SMCR_TS;
  4725. /* Set the Input Trigger source and the slave mode*/
  4726. tmpsmcr |= InputTriggerSource | TIM_SLAVEMODE_EXTERNAL1;
  4727. /* Write to TIMx SMCR */
  4728. TIMx->SMCR = tmpsmcr;
  4729. }
  4730. /**
  4731. * @brief Configures the TIMx External Trigger (ETR).
  4732. * @param TIMx to select the TIM peripheral
  4733. * @param TIM_ExtTRGPrescaler The external Trigger Prescaler.
  4734. * This parameter can be one of the following values:
  4735. * @arg TIM_ETRPRESCALER_DIV1 : ETRP Prescaler OFF.
  4736. * @arg TIM_ETRPRESCALER_DIV2 : ETRP frequency divided by 2.
  4737. * @arg TIM_ETRPRESCALER_DIV4 : ETRP frequency divided by 4.
  4738. * @arg TIM_ETRPRESCALER_DIV8 : ETRP frequency divided by 8.
  4739. * @param TIM_ExtTRGPolarity The external Trigger Polarity.
  4740. * This parameter can be one of the following values:
  4741. * @arg TIM_ETRPOLARITY_INVERTED : active low or falling edge active.
  4742. * @arg TIM_ETRPOLARITY_NONINVERTED : active high or rising edge active.
  4743. * @param ExtTRGFilter External Trigger Filter.
  4744. * This parameter must be a value between 0x00 and 0x0F
  4745. * @retval None
  4746. */
  4747. void TIM_ETR_SetConfig(TIM_TypeDef* TIMx, uint32_t TIM_ExtTRGPrescaler,
  4748. uint32_t TIM_ExtTRGPolarity, uint32_t ExtTRGFilter)
  4749. {
  4750. uint32_t tmpsmcr = 0;
  4751. tmpsmcr = TIMx->SMCR;
  4752. /* Reset the ETR Bits */
  4753. tmpsmcr &= ~(TIM_SMCR_ETF | TIM_SMCR_ETPS | TIM_SMCR_ECE | TIM_SMCR_ETP);
  4754. /* Set the Prescaler, the Filter value and the Polarity */
  4755. tmpsmcr |= (uint32_t)(TIM_ExtTRGPrescaler | (TIM_ExtTRGPolarity | (ExtTRGFilter << 8)));
  4756. /* Write to TIMx SMCR */
  4757. TIMx->SMCR = tmpsmcr;
  4758. }
  4759. /**
  4760. * @brief Enables or disables the TIM Capture Compare Channel x.
  4761. * @param TIMx to select the TIM peripheral
  4762. * @param Channel specifies the TIM Channel
  4763. * This parameter can be one of the following values:
  4764. * @arg TIM_CHANNEL_1: TIM Channel 1
  4765. * @arg TIM_CHANNEL_2: TIM Channel 2
  4766. * @arg TIM_CHANNEL_3: TIM Channel 3
  4767. * @arg TIM_CHANNEL_4: TIM Channel 4
  4768. * @param ChannelState: specifies the TIM Channel CCxE bit new state.
  4769. * This parameter can be: TIM_CCx_ENABLE or TIM_CCx_Disable.
  4770. * @retval None
  4771. */
  4772. void TIM_CCxChannelCmd(TIM_TypeDef* TIMx, uint32_t Channel, uint32_t ChannelState)
  4773. {
  4774. uint32_t tmp = 0;
  4775. /* Check the parameters */
  4776. assert_param(IS_TIM_CC1_INSTANCE(TIMx));
  4777. assert_param(IS_TIM_CHANNELS(Channel));
  4778. tmp = TIM_CCER_CC1E << Channel;
  4779. /* Reset the CCxE Bit */
  4780. TIMx->CCER &= ~tmp;
  4781. /* Set or reset the CCxE Bit */
  4782. TIMx->CCER |= (uint32_t)(ChannelState << Channel);
  4783. }
  4784. /**
  4785. * @}
  4786. */
  4787. #endif /* HAL_TIM_MODULE_ENABLED */
  4788. /**
  4789. * @}
  4790. */
  4791. /**
  4792. * @}
  4793. */
  4794. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/