stm32f3xx_hal_tim.c 167 KB

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