stm32f1xx_hal_tim.c 163 KB

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