stm32l1xx_hal_tim.c 148 KB

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