ft32f0xx_tim.c 104 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885
  1. /**
  2. ******************************************************************************
  3. * @file ft32f0xx_tim.c
  4. * @author FMD AE
  5. * @brief This file provides firmware functions to manage the following
  6. * functionalities of the TIM peripheral:
  7. * + TimeBase management
  8. * + Output Compare management
  9. * + Input Capture management
  10. * + Interrupts, DMA and flags management
  11. * + Clocks management
  12. * + Synchronization management
  13. * + Specific interface management
  14. * + Specific remapping management
  15. * @version V1.0.0
  16. * @data 2021-07-01
  17. ******************************************************************************
  18. */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "ft32f0xx_tim.h"
  21. #include "ft32f0xx_rcc.h"
  22. /* ---------------------- TIM registers bit mask ------------------------ */
  23. #define SMCR_ETR_MASK ((uint16_t)0x00FF)
  24. #define CCMR_OFFSET ((uint16_t)0x0018)
  25. #define CCER_CCE_SET ((uint16_t)0x0001)
  26. #define CCER_CCNE_SET ((uint16_t)0x0004)
  27. static void TI1_Config(TIM_TypeDef* TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,
  28. uint16_t TIM_ICFilter);
  29. static void TI2_Config(TIM_TypeDef* TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,
  30. uint16_t TIM_ICFilter);
  31. static void TI3_Config(TIM_TypeDef* TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,
  32. uint16_t TIM_ICFilter);
  33. static void TI4_Config(TIM_TypeDef* TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,
  34. uint16_t TIM_ICFilter);
  35. /**
  36. * @brief Deinitializes the TIMx peripheral registers to their default reset values.
  37. * @param TIMx: where x can be 1, 2, 3, 6, 7, 14, 15, 16 and 17 to select the TIM peripheral.
  38. * @retval None
  39. *
  40. */
  41. void TIM_DeInit(TIM_TypeDef* TIMx)
  42. {
  43. /* Check the parameters */
  44. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  45. if (TIMx == TIM1)
  46. {
  47. RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM1, ENABLE);
  48. RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM1, DISABLE);
  49. }
  50. // else if (TIMx == TIM2)
  51. // {
  52. // RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM2, ENABLE);
  53. // RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM2, DISABLE);
  54. // }
  55. else if (TIMx == TIM3)
  56. {
  57. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM3, ENABLE);
  58. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM3, DISABLE);
  59. }
  60. else if (TIMx == TIM6)
  61. {
  62. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM6, ENABLE);
  63. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM6, DISABLE);
  64. }
  65. // else if (TIMx == TIM7)
  66. // {
  67. // RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM7, ENABLE);
  68. // RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM7, DISABLE);
  69. // }
  70. else if (TIMx == TIM14)
  71. {
  72. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM14, ENABLE);
  73. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM14, DISABLE);
  74. }
  75. else if (TIMx == TIM15)
  76. {
  77. RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM15, ENABLE);
  78. RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM15, DISABLE);
  79. }
  80. else if (TIMx == TIM16)
  81. {
  82. RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM16, ENABLE);
  83. RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM16, DISABLE);
  84. }
  85. else
  86. {
  87. if (TIMx == TIM17)
  88. {
  89. RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM17, ENABLE);
  90. RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM17, DISABLE);
  91. }
  92. }
  93. }
  94. /**
  95. * @brief Initializes the TIMx Time Base Unit peripheral according to
  96. * the specified parameters in the TIM_TimeBaseInitStruct.
  97. * @param TIMx: where x can be 1, 2, 3, 6, 7, 14, 15, 16 and 17 to select the TIM
  98. * peripheral.
  99. * @param TIM_TimeBaseInitStruct: pointer to a TIM_TimeBaseInitTypeDef
  100. * structure that contains the configuration information for
  101. * the specified TIM peripheral.
  102. * @retval None
  103. */
  104. void TIM_TimeBaseInit(TIM_TypeDef* TIMx, TIM_TimeBaseInitTypeDef* TIM_TimeBaseInitStruct)
  105. {
  106. uint16_t tmpcr1 = 0;
  107. /* Check the parameters */
  108. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  109. assert_param(IS_TIM_COUNTER_MODE(TIM_TimeBaseInitStruct->TIM_CounterMode));
  110. assert_param(IS_TIM_CKD_DIV(TIM_TimeBaseInitStruct->TIM_ClockDivision));
  111. tmpcr1 = TIMx->CR1;
  112. if((TIMx == TIM1) || (TIMx == TIM2) || (TIMx == TIM3))
  113. {
  114. /* Select the Counter Mode */
  115. tmpcr1 &= (uint16_t)(~((uint16_t)(TIM_CR1_DIR | TIM_CR1_CMS)));
  116. tmpcr1 |= (uint32_t)TIM_TimeBaseInitStruct->TIM_CounterMode;
  117. }
  118. if(TIMx != TIM6)
  119. {
  120. /* Set the clock division */
  121. tmpcr1 &= (uint16_t)(~((uint16_t)TIM_CR1_CKD));
  122. tmpcr1 |= (uint32_t)TIM_TimeBaseInitStruct->TIM_ClockDivision;
  123. }
  124. TIMx->CR1 = tmpcr1;
  125. /* Set the Autoreload value */
  126. TIMx->ARR = TIM_TimeBaseInitStruct->TIM_Period ;
  127. /* Set the Prescaler value */
  128. TIMx->PSC = TIM_TimeBaseInitStruct->TIM_Prescaler;
  129. if ((TIMx == TIM1) || (TIMx == TIM15)|| (TIMx == TIM16) || (TIMx == TIM17))
  130. {
  131. /* Set the Repetition Counter value */
  132. TIMx->RCR = TIM_TimeBaseInitStruct->TIM_RepetitionCounter;
  133. }
  134. /* Generate an update event to reload the Prescaler and the Repetition counter
  135. values immediately */
  136. TIMx->EGR = TIM_PSCReloadMode_Immediate;
  137. }
  138. /**
  139. * @brief Fills each TIM_TimeBaseInitStruct member with its default value.
  140. * @param TIM_TimeBaseInitStruct: pointer to a TIM_TimeBaseInitTypeDef structure
  141. * which will be initialized.
  142. * @retval None
  143. */
  144. void TIM_TimeBaseStructInit(TIM_TimeBaseInitTypeDef* TIM_TimeBaseInitStruct)
  145. {
  146. /* Set the default configuration */
  147. TIM_TimeBaseInitStruct->TIM_Period = 0xFFFFFFFF;
  148. TIM_TimeBaseInitStruct->TIM_Prescaler = 0x0000;
  149. TIM_TimeBaseInitStruct->TIM_ClockDivision = TIM_CKD_DIV1;
  150. TIM_TimeBaseInitStruct->TIM_CounterMode = TIM_CounterMode_Up;
  151. TIM_TimeBaseInitStruct->TIM_RepetitionCounter = 0x0000;
  152. }
  153. /**
  154. * @brief Configures the TIMx Prescaler.
  155. * @param TIMx: where x can be 1, 2, 3, 6, 7, 14, 15, 16 and 17 to select the TIM peripheral.
  156. * @param Prescaler: specifies the Prescaler Register value
  157. * @param TIM_PSCReloadMode: specifies the TIM Prescaler Reload mode
  158. * This parameter can be one of the following values:
  159. * @arg TIM_PSCReloadMode_Update: The Prescaler is loaded at the update event.
  160. * @arg TIM_PSCReloadMode_Immediate: The Prescaler is loaded immediatly.
  161. * @retval None
  162. */
  163. void TIM_PrescalerConfig(TIM_TypeDef* TIMx, uint16_t Prescaler, uint16_t TIM_PSCReloadMode)
  164. {
  165. /* Check the parameters */
  166. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  167. assert_param(IS_TIM_PRESCALER_RELOAD(TIM_PSCReloadMode));
  168. /* Set the Prescaler value */
  169. TIMx->PSC = Prescaler;
  170. /* Set or reset the UG Bit */
  171. TIMx->EGR = TIM_PSCReloadMode;
  172. }
  173. /**
  174. * @brief Specifies the TIMx Counter Mode to be used.
  175. * @param TIMx: where x can be 1, 2, or 3 to select the TIM peripheral.
  176. * @param TIM_CounterMode: specifies the Counter Mode to be used
  177. * This parameter can be one of the following values:
  178. * @arg TIM_CounterMode_Up: TIM Up Counting Mode
  179. * @arg TIM_CounterMode_Down: TIM Down Counting Mode
  180. * @arg TIM_CounterMode_CenterAligned1: TIM Center Aligned Mode1
  181. * @arg TIM_CounterMode_CenterAligned2: TIM Center Aligned Mode2
  182. * @arg TIM_CounterMode_CenterAligned3: TIM Center Aligned Mode3
  183. * @retval None
  184. */
  185. void TIM_CounterModeConfig(TIM_TypeDef* TIMx, uint16_t TIM_CounterMode)
  186. {
  187. uint16_t tmpcr1 = 0;
  188. /* Check the parameters */
  189. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  190. assert_param(IS_TIM_COUNTER_MODE(TIM_CounterMode));
  191. tmpcr1 = TIMx->CR1;
  192. /* Reset the CMS and DIR Bits */
  193. tmpcr1 &= (uint16_t)(~((uint16_t)(TIM_CR1_DIR | TIM_CR1_CMS)));
  194. /* Set the Counter Mode */
  195. tmpcr1 |= TIM_CounterMode;
  196. /* Write to TIMx CR1 register */
  197. TIMx->CR1 = tmpcr1;
  198. }
  199. /**
  200. * @brief Sets the TIMx Counter Register value
  201. * @param TIMx: where x can be 1, 2, 3, 6, 7, 14, 15, 16 and 17 to select the TIM
  202. * peripheral.
  203. * @param Counter: specifies the Counter register new value.
  204. * @retval None
  205. */
  206. void TIM_SetCounter(TIM_TypeDef* TIMx, uint32_t Counter)
  207. {
  208. /* Check the parameters */
  209. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  210. /* Set the Counter Register value */
  211. TIMx->CNT = Counter;
  212. }
  213. /**
  214. * @brief Sets the TIMx Autoreload Register value
  215. * @param TIMx: where x can be 1, 2, 3, 6, 7, 14, 15, 16 and 17 to select the TIM peripheral.
  216. * @param Autoreload: specifies the Autoreload register new value.
  217. * @retval None
  218. */
  219. void TIM_SetAutoreload(TIM_TypeDef* TIMx, uint32_t Autoreload)
  220. {
  221. /* Check the parameters */
  222. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  223. /* Set the Autoreload Register value */
  224. TIMx->ARR = Autoreload;
  225. }
  226. /**
  227. * @brief Gets the TIMx Counter value.
  228. * @param TIMx: where x can be 1, 2, 3, 6, 7, 14, 15, 16 and 17 to select the TIM
  229. * peripheral.
  230. * @retval Counter Register value.
  231. */
  232. uint32_t TIM_GetCounter(TIM_TypeDef* TIMx)
  233. {
  234. /* Check the parameters */
  235. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  236. /* Get the Counter Register value */
  237. return TIMx->CNT;
  238. }
  239. /**
  240. * @brief Gets the TIMx Prescaler value.
  241. * @param TIMx: where x can be 1, 2, 3, 6, 7, 14, 15, 16 and 17 to select the TIM
  242. * peripheral.
  243. * @retval Prescaler Register value.
  244. */
  245. uint16_t TIM_GetPrescaler(TIM_TypeDef* TIMx)
  246. {
  247. /* Check the parameters */
  248. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  249. /* Get the Prescaler Register value */
  250. return TIMx->PSC;
  251. }
  252. /**
  253. * @brief Enables or Disables the TIMx Update event.
  254. * @param TIMx: where x can be 1, 2, 3, 6, 7, 14, 15, 16 and 17 to select the TIM
  255. * peripheral.
  256. * @param NewState: new state of the TIMx UDIS bit
  257. * This parameter can be: ENABLE or DISABLE.
  258. * @retval None
  259. */
  260. void TIM_UpdateDisableConfig(TIM_TypeDef* TIMx, FunctionalState NewState)
  261. {
  262. /* Check the parameters */
  263. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  264. assert_param(IS_FUNCTIONAL_STATE(NewState));
  265. if (NewState != DISABLE)
  266. {
  267. /* Set the Update Disable Bit */
  268. TIMx->CR1 |= TIM_CR1_UDIS;
  269. }
  270. else
  271. {
  272. /* Reset the Update Disable Bit */
  273. TIMx->CR1 &= (uint16_t)~((uint16_t)TIM_CR1_UDIS);
  274. }
  275. }
  276. /**
  277. * @brief Configures the TIMx Update Request Interrupt source.
  278. * @param TIMx: where x can be 1, 2, 3, 6, 7, 14, 15, 16 and 17 to select the TIM
  279. * peripheral.
  280. * @param TIM_UpdateSource: specifies the Update source.
  281. * This parameter can be one of the following values:
  282. * @arg TIM_UpdateSource_Regular: Source of update is the counter
  283. * overflow/underflow or the setting of UG bit, or an update
  284. * generation through the slave mode controller.
  285. * @arg TIM_UpdateSource_Global: Source of update is counter overflow/underflow.
  286. * @retval None
  287. */
  288. void TIM_UpdateRequestConfig(TIM_TypeDef* TIMx, uint16_t TIM_UpdateSource)
  289. {
  290. /* Check the parameters */
  291. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  292. assert_param(IS_TIM_UPDATE_SOURCE(TIM_UpdateSource));
  293. if (TIM_UpdateSource != TIM_UpdateSource_Global)
  294. {
  295. /* Set the URS Bit */
  296. TIMx->CR1 |= TIM_CR1_URS;
  297. }
  298. else
  299. {
  300. /* Reset the URS Bit */
  301. TIMx->CR1 &= (uint16_t)~((uint16_t)TIM_CR1_URS);
  302. }
  303. }
  304. /**
  305. * @brief Enables or disables TIMx peripheral Preload register on ARR.
  306. * @param TIMx: where x can be 1, 2, 3, 6, 7, 14, 15, 16 and 17 to select the TIM
  307. * peripheral.
  308. * @param NewState: new state of the TIMx peripheral Preload register
  309. * This parameter can be: ENABLE or DISABLE.
  310. * @retval None
  311. */
  312. void TIM_ARRPreloadConfig(TIM_TypeDef* TIMx, FunctionalState NewState)
  313. {
  314. /* Check the parameters */
  315. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  316. assert_param(IS_FUNCTIONAL_STATE(NewState));
  317. if (NewState != DISABLE)
  318. {
  319. /* Set the ARR Preload Bit */
  320. TIMx->CR1 |= TIM_CR1_ARPE;
  321. }
  322. else
  323. {
  324. /* Reset the ARR Preload Bit */
  325. TIMx->CR1 &= (uint16_t)~((uint16_t)TIM_CR1_ARPE);
  326. }
  327. }
  328. /**
  329. * @brief Selects the TIMx's One Pulse Mode.
  330. * @param TIMx: where x can be 1, 2, 3, 6, 7, 14, 15, 16 and 17 to select the TIM
  331. * peripheral.
  332. * @param TIM_OPMode: specifies the OPM Mode to be used.
  333. * This parameter can be one of the following values:
  334. * @arg TIM_OPMode_Single
  335. * @arg TIM_OPMode_Repetitive
  336. * @retval None
  337. */
  338. void TIM_SelectOnePulseMode(TIM_TypeDef* TIMx, uint16_t TIM_OPMode)
  339. {
  340. /* Check the parameters */
  341. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  342. assert_param(IS_TIM_OPM_MODE(TIM_OPMode));
  343. /* Reset the OPM Bit */
  344. TIMx->CR1 &= (uint16_t)~((uint16_t)TIM_CR1_OPM);
  345. /* Configure the OPM Mode */
  346. TIMx->CR1 |= TIM_OPMode;
  347. }
  348. /**
  349. * @brief Sets the TIMx Clock Division value.
  350. * @param TIMx: where x can be 1, 2, 3, 14, 15, 16 and 17 to select the TIM peripheral.
  351. * @param TIM_CKD: specifies the clock division value.
  352. * This parameter can be one of the following value:
  353. * @arg TIM_CKD_DIV1: TDTS = Tck_tim
  354. * @arg TIM_CKD_DIV2: TDTS = 2*Tck_tim
  355. * @arg TIM_CKD_DIV4: TDTS = 4*Tck_tim
  356. * @retval None
  357. */
  358. void TIM_SetClockDivision(TIM_TypeDef* TIMx, uint16_t TIM_CKD)
  359. {
  360. /* Check the parameters */
  361. assert_param(IS_TIM_LIST4_PERIPH(TIMx));
  362. assert_param(IS_TIM_CKD_DIV(TIM_CKD));
  363. /* Reset the CKD Bits */
  364. TIMx->CR1 &= (uint16_t)~((uint16_t)TIM_CR1_CKD);
  365. /* Set the CKD value */
  366. TIMx->CR1 |= TIM_CKD;
  367. }
  368. /**
  369. * @brief Enables or disables the specified TIM peripheral.
  370. * @param TIMx: where x can be 1, 2, 3, 6, 7, 14, 15, 16 and 17to select the TIMx
  371. * peripheral.
  372. * @param NewState: new state of the TIMx peripheral.
  373. * This parameter can be: ENABLE or DISABLE.
  374. * @retval None
  375. */
  376. void TIM_Cmd(TIM_TypeDef* TIMx, FunctionalState NewState)
  377. {
  378. /* Check the parameters */
  379. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  380. assert_param(IS_FUNCTIONAL_STATE(NewState));
  381. if (NewState != DISABLE)
  382. {
  383. /* Enable the TIM Counter */
  384. TIMx->CR1 |= TIM_CR1_CEN;
  385. }
  386. else
  387. {
  388. /* Disable the TIM Counter */
  389. TIMx->CR1 &= (uint16_t)(~((uint16_t)TIM_CR1_CEN));
  390. }
  391. }
  392. /**
  393. * @}
  394. */
  395. /**
  396. * @brief Configures the: Break feature, dead time, Lock level, OSSI/OSSR State
  397. * and the AOE(automatic output enable).
  398. * @param TIMx: where x can be 1, 15, 16 or 17 to select the TIM
  399. * @param TIM_BDTRInitStruct: pointer to a TIM_BDTRInitTypeDef structure that
  400. * contains the BDTR Register configuration information for the TIM peripheral.
  401. * @retval None
  402. */
  403. void TIM_BDTRConfig(TIM_TypeDef* TIMx, TIM_BDTRInitTypeDef *TIM_BDTRInitStruct)
  404. {
  405. /* Check the parameters */
  406. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  407. assert_param(IS_TIM_OSSR_STATE(TIM_BDTRInitStruct->TIM_OSSRState));
  408. assert_param(IS_TIM_OSSI_STATE(TIM_BDTRInitStruct->TIM_OSSIState));
  409. assert_param(IS_TIM_LOCK_LEVEL(TIM_BDTRInitStruct->TIM_LOCKLevel));
  410. assert_param(IS_TIM_BREAK_STATE(TIM_BDTRInitStruct->TIM_Break));
  411. assert_param(IS_TIM_BREAK_POLARITY(TIM_BDTRInitStruct->TIM_BreakPolarity));
  412. assert_param(IS_TIM_AUTOMATIC_OUTPUT_STATE(TIM_BDTRInitStruct->TIM_AutomaticOutput));
  413. /* Set the Lock level, the Break enable Bit and the Ploarity, the OSSR State,
  414. the OSSI State, the dead time value and the Automatic Output Enable Bit */
  415. TIMx->BDTR = (uint32_t)TIM_BDTRInitStruct->TIM_OSSRState | TIM_BDTRInitStruct->TIM_OSSIState |
  416. TIM_BDTRInitStruct->TIM_LOCKLevel | TIM_BDTRInitStruct->TIM_DeadTime |
  417. TIM_BDTRInitStruct->TIM_Break | TIM_BDTRInitStruct->TIM_BreakPolarity |
  418. TIM_BDTRInitStruct->TIM_AutomaticOutput;
  419. }
  420. /**
  421. * @brief Fills each TIM_BDTRInitStruct member with its default value.
  422. * @param TIM_BDTRInitStruct: pointer to a TIM_BDTRInitTypeDef structure which
  423. * will be initialized.
  424. * @retval None
  425. */
  426. void TIM_BDTRStructInit(TIM_BDTRInitTypeDef* TIM_BDTRInitStruct)
  427. {
  428. /* Set the default configuration */
  429. TIM_BDTRInitStruct->TIM_OSSRState = TIM_OSSRState_Disable;
  430. TIM_BDTRInitStruct->TIM_OSSIState = TIM_OSSIState_Disable;
  431. TIM_BDTRInitStruct->TIM_LOCKLevel = TIM_LOCKLevel_OFF;
  432. TIM_BDTRInitStruct->TIM_DeadTime = 0x00;
  433. TIM_BDTRInitStruct->TIM_Break = TIM_Break_Disable;
  434. TIM_BDTRInitStruct->TIM_BreakPolarity = TIM_BreakPolarity_Low;
  435. TIM_BDTRInitStruct->TIM_AutomaticOutput = TIM_AutomaticOutput_Disable;
  436. }
  437. /**
  438. * @brief Enables or disables the TIM peripheral Main Outputs.
  439. * @param TIMx: where x can be 1, 15, 16 or 17 to select the TIMx peripheral.
  440. * @param NewState: new state of the TIM peripheral Main Outputs.
  441. * This parameter can be: ENABLE or DISABLE.
  442. * @retval None
  443. */
  444. void TIM_CtrlPWMOutputs(TIM_TypeDef* TIMx, FunctionalState NewState)
  445. {
  446. /* Check the parameters */
  447. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  448. assert_param(IS_FUNCTIONAL_STATE(NewState));
  449. if (NewState != DISABLE)
  450. {
  451. /* Enable the TIM Main Output */
  452. TIMx->BDTR |= TIM_BDTR_MOE;
  453. }
  454. else
  455. {
  456. /* Disable the TIM Main Output */
  457. TIMx->BDTR &= (uint16_t)(~((uint16_t)TIM_BDTR_MOE));
  458. }
  459. }
  460. /**
  461. * @}
  462. */
  463. /**
  464. * @brief Initializes the TIMx Channel1 according to the specified
  465. * parameters in the TIM_OCInitStruct.
  466. * @param TIMx: where x can be 1, 2, 3, 14, 15, 16 and 17 to select the TIM peripheral.
  467. * @param TIM_OCInitStruct: pointer to a TIM_OCInitTypeDef structure
  468. * that contains the configuration information for the specified TIM
  469. * peripheral.
  470. * @retval None
  471. */
  472. void TIM_OC1Init(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct)
  473. {
  474. uint16_t tmpccmrx = 0, tmpccer = 0, tmpcr2 = 0;
  475. /* Check the parameters */
  476. assert_param(IS_TIM_LIST4_PERIPH(TIMx));
  477. assert_param(IS_TIM_OC_MODE(TIM_OCInitStruct->TIM_OCMode));
  478. assert_param(IS_TIM_OUTPUT_STATE(TIM_OCInitStruct->TIM_OutputState));
  479. assert_param(IS_TIM_OC_POLARITY(TIM_OCInitStruct->TIM_OCPolarity));
  480. /* Disable the Channel 1: Reset the CC1E Bit */
  481. TIMx->CCER &= (uint16_t)(~(uint16_t)TIM_CCER_CC1E);
  482. /* Get the TIMx CCER register value */
  483. tmpccer = TIMx->CCER;
  484. /* Get the TIMx CR2 register value */
  485. tmpcr2 = TIMx->CR2;
  486. /* Get the TIMx CCMR1 register value */
  487. tmpccmrx = TIMx->CCMR1;
  488. /* Reset the Output Compare Mode Bits */
  489. tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CCMR1_OC1M));
  490. tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CCMR1_CC1S));
  491. /* Select the Output Compare Mode */
  492. tmpccmrx |= TIM_OCInitStruct->TIM_OCMode;
  493. /* Reset the Output Polarity level */
  494. tmpccer &= (uint16_t)(~((uint16_t)TIM_CCER_CC1P));
  495. /* Set the Output Compare Polarity */
  496. tmpccer |= TIM_OCInitStruct->TIM_OCPolarity;
  497. /* Set the Output State */
  498. tmpccer |= TIM_OCInitStruct->TIM_OutputState;
  499. if((TIMx == TIM1) || (TIMx == TIM15) || (TIMx == TIM16) || (TIMx == TIM17))
  500. {
  501. assert_param(IS_TIM_OUTPUTN_STATE(TIM_OCInitStruct->TIM_OutputNState));
  502. assert_param(IS_TIM_OCN_POLARITY(TIM_OCInitStruct->TIM_OCNPolarity));
  503. assert_param(IS_TIM_OCNIDLE_STATE(TIM_OCInitStruct->TIM_OCNIdleState));
  504. assert_param(IS_TIM_OCIDLE_STATE(TIM_OCInitStruct->TIM_OCIdleState));
  505. /* Reset the Output N Polarity level */
  506. tmpccer &= (uint16_t)(~((uint16_t)TIM_CCER_CC1NP));
  507. /* Set the Output N Polarity */
  508. tmpccer |= TIM_OCInitStruct->TIM_OCNPolarity;
  509. /* Reset the Output N State */
  510. tmpccer &= (uint16_t)(~((uint16_t)TIM_CCER_CC1NE));
  511. /* Set the Output N State */
  512. tmpccer |= TIM_OCInitStruct->TIM_OutputNState;
  513. /* Reset the Ouput Compare and Output Compare N IDLE State */
  514. tmpcr2 &= (uint16_t)(~((uint16_t)TIM_CR2_OIS1));
  515. tmpcr2 &= (uint16_t)(~((uint16_t)TIM_CR2_OIS1N));
  516. /* Set the Output Idle state */
  517. tmpcr2 |= TIM_OCInitStruct->TIM_OCIdleState;
  518. /* Set the Output N Idle state */
  519. tmpcr2 |= TIM_OCInitStruct->TIM_OCNIdleState;
  520. }
  521. /* Write to TIMx CR2 */
  522. TIMx->CR2 = tmpcr2;
  523. /* Write to TIMx CCMR1 */
  524. TIMx->CCMR1 = tmpccmrx;
  525. /* Set the Capture Compare Register value */
  526. TIMx->CCR1 = TIM_OCInitStruct->TIM_Pulse;
  527. /* Write to TIMx CCER */
  528. TIMx->CCER = tmpccer;
  529. }
  530. /**
  531. * @brief Initializes the TIMx Channel2 according to the specified
  532. * parameters in the TIM_OCInitStruct.
  533. * @param TIMx: where x can be 1, 2, 3 or 15 to select the TIM peripheral.
  534. * @param TIM_OCInitStruct: pointer to a TIM_OCInitTypeDef structure
  535. * that contains the configuration information for the specified TIM
  536. * peripheral.
  537. * @retval None
  538. */
  539. void TIM_OC2Init(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct)
  540. {
  541. uint16_t tmpccmrx = 0, tmpccer = 0, tmpcr2 = 0;
  542. /* Check the parameters */
  543. assert_param(IS_TIM_LIST6_PERIPH(TIMx));
  544. assert_param(IS_TIM_OC_MODE(TIM_OCInitStruct->TIM_OCMode));
  545. assert_param(IS_TIM_OUTPUT_STATE(TIM_OCInitStruct->TIM_OutputState));
  546. assert_param(IS_TIM_OC_POLARITY(TIM_OCInitStruct->TIM_OCPolarity));
  547. /* Disable the Channel 2: Reset the CC2E Bit */
  548. TIMx->CCER &= (uint16_t)(~((uint16_t)TIM_CCER_CC2E));
  549. /* Get the TIMx CCER register value */
  550. tmpccer = TIMx->CCER;
  551. /* Get the TIMx CR2 register value */
  552. tmpcr2 = TIMx->CR2;
  553. /* Get the TIMx CCMR1 register value */
  554. tmpccmrx = TIMx->CCMR1;
  555. /* Reset the Output Compare mode and Capture/Compare selection Bits */
  556. tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CCMR1_OC2M));
  557. tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CCMR1_CC2S));
  558. /* Select the Output Compare Mode */
  559. tmpccmrx |= (uint16_t)(TIM_OCInitStruct->TIM_OCMode << 8);
  560. /* Reset the Output Polarity level */
  561. tmpccer &= (uint16_t)(~((uint16_t)TIM_CCER_CC2P));
  562. /* Set the Output Compare Polarity */
  563. tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OCPolarity << 4);
  564. /* Set the Output State */
  565. tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OutputState << 4);
  566. if((TIMx == TIM1) || (TIMx == TIM15))
  567. {
  568. /* Check the parameters */
  569. assert_param(IS_TIM_OCIDLE_STATE(TIM_OCInitStruct->TIM_OCIdleState));
  570. /* Reset the Ouput Compare State */
  571. tmpcr2 &= (uint16_t)(~((uint16_t)TIM_CR2_OIS2));
  572. /* Set the Output Idle state */
  573. tmpcr2 |= (uint16_t)(TIM_OCInitStruct->TIM_OCIdleState << 2);
  574. if (TIMx == TIM1)
  575. {
  576. /* Check the parameters */
  577. assert_param(IS_TIM_OUTPUTN_STATE(TIM_OCInitStruct->TIM_OutputNState));
  578. assert_param(IS_TIM_OCN_POLARITY(TIM_OCInitStruct->TIM_OCNPolarity));
  579. assert_param(IS_TIM_OCNIDLE_STATE(TIM_OCInitStruct->TIM_OCNIdleState));
  580. /* Reset the Output N Polarity level */
  581. tmpccer &= (uint16_t)(~((uint16_t)TIM_CCER_CC2NP));
  582. /* Set the Output N Polarity */
  583. tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OCNPolarity << 4);
  584. /* Reset the Output N State */
  585. tmpccer &= (uint16_t)(~((uint16_t)TIM_CCER_CC2NE));
  586. /* Set the Output N State */
  587. tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OutputNState << 4);
  588. /* Reset the Output Compare N IDLE State */
  589. tmpcr2 &= (uint16_t)(~((uint16_t)TIM_CR2_OIS2N));
  590. /* Set the Output N Idle state */
  591. tmpcr2 |= (uint16_t)(TIM_OCInitStruct->TIM_OCNIdleState << 2);
  592. }
  593. }
  594. /* Write to TIMx CR2 */
  595. TIMx->CR2 = tmpcr2;
  596. /* Write to TIMx CCMR1 */
  597. TIMx->CCMR1 = tmpccmrx;
  598. /* Set the Capture Compare Register value */
  599. TIMx->CCR2 = TIM_OCInitStruct->TIM_Pulse;
  600. /* Write to TIMx CCER */
  601. TIMx->CCER = tmpccer;
  602. }
  603. /**
  604. * @brief Initializes the TIMx Channel3 according to the specified
  605. * parameters in the TIM_OCInitStruct.
  606. * @param TIMx: where x can be 1, 2 or 3 to select the TIM peripheral.
  607. * @param TIM_OCInitStruct: pointer to a TIM_OCInitTypeDef structure
  608. * that contains the configuration information for the specified TIM
  609. * peripheral.
  610. * @retval None
  611. */
  612. void TIM_OC3Init(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct)
  613. {
  614. uint16_t tmpccmrx = 0, tmpccer = 0, tmpcr2 = 0;
  615. /* Check the parameters */
  616. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  617. assert_param(IS_TIM_OC_MODE(TIM_OCInitStruct->TIM_OCMode));
  618. assert_param(IS_TIM_OUTPUT_STATE(TIM_OCInitStruct->TIM_OutputState));
  619. assert_param(IS_TIM_OC_POLARITY(TIM_OCInitStruct->TIM_OCPolarity));
  620. /* Disable the Channel 2: Reset the CC2E Bit */
  621. TIMx->CCER &= (uint16_t)(~((uint16_t)TIM_CCER_CC3E));
  622. /* Get the TIMx CCER register value */
  623. tmpccer = TIMx->CCER;
  624. /* Get the TIMx CR2 register value */
  625. tmpcr2 = TIMx->CR2;
  626. /* Get the TIMx CCMR2 register value */
  627. tmpccmrx = TIMx->CCMR2;
  628. /* Reset the Output Compare mode and Capture/Compare selection Bits */
  629. tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CCMR2_OC3M));
  630. tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CCMR2_CC3S));
  631. /* Select the Output Compare Mode */
  632. tmpccmrx |= TIM_OCInitStruct->TIM_OCMode;
  633. /* Reset the Output Polarity level */
  634. tmpccer &= (uint16_t)(~((uint16_t)TIM_CCER_CC3P));
  635. /* Set the Output Compare Polarity */
  636. tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OCPolarity << 8);
  637. /* Set the Output State */
  638. tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OutputState << 8);
  639. if(TIMx == TIM1)
  640. {
  641. assert_param(IS_TIM_OUTPUTN_STATE(TIM_OCInitStruct->TIM_OutputNState));
  642. assert_param(IS_TIM_OCN_POLARITY(TIM_OCInitStruct->TIM_OCNPolarity));
  643. assert_param(IS_TIM_OCNIDLE_STATE(TIM_OCInitStruct->TIM_OCNIdleState));
  644. assert_param(IS_TIM_OCIDLE_STATE(TIM_OCInitStruct->TIM_OCIdleState));
  645. /* Reset the Output N Polarity level */
  646. tmpccer &= (uint16_t)(~((uint16_t)TIM_CCER_CC3NP));
  647. /* Set the Output N Polarity */
  648. tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OCNPolarity << 8);
  649. /* Reset the Output N State */
  650. tmpccer &= (uint16_t)(~((uint16_t)TIM_CCER_CC3NE));
  651. /* Set the Output N State */
  652. tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OutputNState << 8);
  653. /* Reset the Ouput Compare and Output Compare N IDLE State */
  654. tmpcr2 &= (uint16_t)(~((uint16_t)TIM_CR2_OIS3));
  655. tmpcr2 &= (uint16_t)(~((uint16_t)TIM_CR2_OIS3N));
  656. /* Set the Output Idle state */
  657. tmpcr2 |= (uint16_t)(TIM_OCInitStruct->TIM_OCIdleState << 4);
  658. /* Set the Output N Idle state */
  659. tmpcr2 |= (uint16_t)(TIM_OCInitStruct->TIM_OCNIdleState << 4);
  660. }
  661. /* Write to TIMx CR2 */
  662. TIMx->CR2 = tmpcr2;
  663. /* Write to TIMx CCMR2 */
  664. TIMx->CCMR2 = tmpccmrx;
  665. /* Set the Capture Compare Register value */
  666. TIMx->CCR3 = TIM_OCInitStruct->TIM_Pulse;
  667. /* Write to TIMx CCER */
  668. TIMx->CCER = tmpccer;
  669. }
  670. /**
  671. * @brief Initializes the TIMx Channel4 according to the specified
  672. * parameters in the TIM_OCInitStruct.
  673. * @param TIMx: where x can be 1, 2 or 3 to select the TIM peripheral.
  674. * @param TIM_OCInitStruct: pointer to a TIM_OCInitTypeDef structure
  675. * that contains the configuration information for the specified TIM
  676. * peripheral.
  677. * @retval None
  678. */
  679. void TIM_OC4Init(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct)
  680. {
  681. uint16_t tmpccmrx = 0, tmpccer = 0, tmpcr2 = 0;
  682. /* Check the parameters */
  683. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  684. assert_param(IS_TIM_OC_MODE(TIM_OCInitStruct->TIM_OCMode));
  685. assert_param(IS_TIM_OUTPUT_STATE(TIM_OCInitStruct->TIM_OutputState));
  686. assert_param(IS_TIM_OC_POLARITY(TIM_OCInitStruct->TIM_OCPolarity));
  687. /* Disable the Channel 2: Reset the CC4E Bit */
  688. TIMx->CCER &= (uint16_t)(~((uint16_t)TIM_CCER_CC4E));
  689. /* Get the TIMx CCER register value */
  690. tmpccer = TIMx->CCER;
  691. /* Get the TIMx CR2 register value */
  692. tmpcr2 = TIMx->CR2;
  693. /* Get the TIMx CCMR2 register value */
  694. tmpccmrx = TIMx->CCMR2;
  695. /* Reset the Output Compare mode and Capture/Compare selection Bits */
  696. tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CCMR2_OC4M));
  697. tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CCMR2_CC4S));
  698. /* Select the Output Compare Mode */
  699. tmpccmrx |= (uint16_t)(TIM_OCInitStruct->TIM_OCMode << 8);
  700. /* Reset the Output Polarity level */
  701. tmpccer &= (uint16_t)(~((uint16_t)TIM_CCER_CC4P));
  702. /* Set the Output Compare Polarity */
  703. tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OCPolarity << 12);
  704. /* Set the Output State */
  705. tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OutputState << 12);
  706. if(TIMx == TIM1)
  707. {
  708. assert_param(IS_TIM_OCIDLE_STATE(TIM_OCInitStruct->TIM_OCIdleState));
  709. /* Reset the Ouput Compare IDLE State */
  710. tmpcr2 &= (uint16_t)(~((uint16_t)TIM_CR2_OIS4));
  711. /* Set the Output Idle state */
  712. tmpcr2 |= (uint16_t)(TIM_OCInitStruct->TIM_OCIdleState << 6);
  713. }
  714. /* Write to TIMx CR2 */
  715. TIMx->CR2 = tmpcr2;
  716. /* Write to TIMx CCMR2 */
  717. TIMx->CCMR2 = tmpccmrx;
  718. /* Set the Capture Compare Register value */
  719. TIMx->CCR4 = TIM_OCInitStruct->TIM_Pulse;
  720. /* Write to TIMx CCER */
  721. TIMx->CCER = tmpccer;
  722. }
  723. /**
  724. * @brief Fills each TIM_OCInitStruct member with its default value.
  725. * @param TIM_OCInitStruct: pointer to a TIM_OCInitTypeDef structure which will
  726. * be initialized.
  727. * @retval None
  728. */
  729. void TIM_OCStructInit(TIM_OCInitTypeDef* TIM_OCInitStruct)
  730. {
  731. /* Set the default configuration */
  732. TIM_OCInitStruct->TIM_OCMode = TIM_OCMode_Timing;
  733. TIM_OCInitStruct->TIM_OutputState = TIM_OutputState_Disable;
  734. TIM_OCInitStruct->TIM_OutputNState = TIM_OutputNState_Disable;
  735. TIM_OCInitStruct->TIM_Pulse = 0x0000000;
  736. TIM_OCInitStruct->TIM_OCPolarity = TIM_OCPolarity_High;
  737. TIM_OCInitStruct->TIM_OCNPolarity = TIM_OCPolarity_High;
  738. TIM_OCInitStruct->TIM_OCIdleState = TIM_OCIdleState_Reset;
  739. TIM_OCInitStruct->TIM_OCNIdleState = TIM_OCNIdleState_Reset;
  740. }
  741. /**
  742. * @brief Selects the TIM Output Compare Mode.
  743. * @note This function disables the selected channel before changing the Output
  744. * Compare Mode.
  745. * User has to enable this channel using TIM_CCxCmd and TIM_CCxNCmd functions.
  746. * @param TIMx: where x can be 1, 2, 3, 14, 15, 16 or 17 to select the TIM peripheral.
  747. * @param TIM_Channel: specifies the TIM Channel
  748. * This parameter can be one of the following values:
  749. * @arg TIM_Channel_1: TIM Channel 1
  750. * @arg TIM_Channel_2: TIM Channel 2
  751. * @arg TIM_Channel_3: TIM Channel 3
  752. * @arg TIM_Channel_4: TIM Channel 4
  753. * @param TIM_OCMode: specifies the TIM Output Compare Mode.
  754. * This parameter can be one of the following values:
  755. * @arg TIM_OCMode_Timing
  756. * @arg TIM_OCMode_Active
  757. * @arg TIM_OCMode_Toggle
  758. * @arg TIM_OCMode_PWM1
  759. * @arg TIM_OCMode_PWM2
  760. * @arg TIM_ForcedAction_Active
  761. * @arg TIM_ForcedAction_InActive
  762. * @retval None
  763. */
  764. void TIM_SelectOCxM(TIM_TypeDef* TIMx, uint16_t TIM_Channel, uint16_t TIM_OCMode)
  765. {
  766. uint32_t tmp = 0;
  767. uint16_t tmp1 = 0;
  768. /* Check the parameters */
  769. assert_param(IS_TIM_LIST4_PERIPH(TIMx));
  770. assert_param(IS_TIM_OCM(TIM_OCMode));
  771. tmp = (uint32_t) TIMx;
  772. tmp += CCMR_OFFSET;
  773. tmp1 = CCER_CCE_SET << (uint16_t)TIM_Channel;
  774. /* Disable the Channel: Reset the CCxE Bit */
  775. TIMx->CCER &= (uint16_t) ~tmp1;
  776. if((TIM_Channel == TIM_Channel_1) ||(TIM_Channel == TIM_Channel_3))
  777. {
  778. tmp += (TIM_Channel>>1);
  779. /* Reset the OCxM bits in the CCMRx register */
  780. *(__IO uint32_t *) tmp &= (uint32_t)~((uint32_t)TIM_CCMR1_OC1M);
  781. /* Configure the OCxM bits in the CCMRx register */
  782. *(__IO uint32_t *) tmp |= TIM_OCMode;
  783. }
  784. else
  785. {
  786. tmp += (uint16_t)(TIM_Channel - (uint16_t)4)>> (uint16_t)1;
  787. /* Reset the OCxM bits in the CCMRx register */
  788. *(__IO uint32_t *) tmp &= (uint32_t)~((uint32_t)TIM_CCMR1_OC2M);
  789. /* Configure the OCxM bits in the CCMRx register */
  790. *(__IO uint32_t *) tmp |= (uint16_t)(TIM_OCMode << 8);
  791. }
  792. }
  793. /**
  794. * @brief Sets the TIMx Capture Compare1 Register value
  795. * @param TIMx: where x can be 1, 2, 3, 14, 15, 16 or 17 to select the TIM peripheral.
  796. * @param Compare1: specifies the Capture Compare1 register new value.
  797. * @retval None
  798. */
  799. void TIM_SetCompare1(TIM_TypeDef* TIMx, uint32_t Compare1)
  800. {
  801. /* Check the parameters */
  802. assert_param(IS_TIM_LIST4_PERIPH(TIMx));
  803. /* Set the Capture Compare1 Register value */
  804. TIMx->CCR1 = Compare1;
  805. }
  806. /**
  807. * @brief Sets the TIMx Capture Compare2 Register value
  808. * @param TIMx: where x can be 1, 2, 3 or 15 to select the TIM peripheral.
  809. * @param Compare2: specifies the Capture Compare2 register new value.
  810. * @retval None
  811. */
  812. void TIM_SetCompare2(TIM_TypeDef* TIMx, uint32_t Compare2)
  813. {
  814. /* Check the parameters */
  815. assert_param(IS_TIM_LIST6_PERIPH(TIMx));
  816. /* Set the Capture Compare2 Register value */
  817. TIMx->CCR2 = Compare2;
  818. }
  819. /**
  820. * @brief Sets the TIMx Capture Compare3 Register value
  821. * @param TIMx: where x can be 1, 2 or 3 to select the TIM peripheral.
  822. * @param Compare3: specifies the Capture Compare3 register new value.
  823. * @retval None
  824. */
  825. void TIM_SetCompare3(TIM_TypeDef* TIMx, uint32_t Compare3)
  826. {
  827. /* Check the parameters */
  828. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  829. /* Set the Capture Compare3 Register value */
  830. TIMx->CCR3 = Compare3;
  831. }
  832. /**
  833. * @brief Sets the TIMx Capture Compare4 Register value
  834. * @param TIMx: where x can be 1, 2 or 3 to select the TIM peripheral.
  835. * @param Compare4: specifies the Capture Compare4 register new value.
  836. * @retval None
  837. */
  838. void TIM_SetCompare4(TIM_TypeDef* TIMx, uint32_t Compare4)
  839. {
  840. /* Check the parameters */
  841. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  842. /* Set the Capture Compare4 Register value */
  843. TIMx->CCR4 = Compare4;
  844. }
  845. /**
  846. * @brief Forces the TIMx output 1 waveform to active or inactive level.
  847. * @param TIMx: where x can be 1, 2, 3, 14, 15, 16 or 17 to select the TIM peripheral.
  848. * @param TIM_ForcedAction: specifies the forced Action to be set to the output waveform.
  849. * This parameter can be one of the following values:
  850. * @arg TIM_ForcedAction_Active: Force active level on OC1REF
  851. * @arg TIM_ForcedAction_InActive: Force inactive level on OC1REF.
  852. * @retval None
  853. */
  854. void TIM_ForcedOC1Config(TIM_TypeDef* TIMx, uint16_t TIM_ForcedAction)
  855. {
  856. uint16_t tmpccmr1 = 0;
  857. /* Check the parameters */
  858. assert_param(IS_TIM_LIST4_PERIPH(TIMx));
  859. assert_param(IS_TIM_FORCED_ACTION(TIM_ForcedAction));
  860. tmpccmr1 = TIMx->CCMR1;
  861. /* Reset the OC1M Bits */
  862. tmpccmr1 &= (uint16_t)~((uint16_t)TIM_CCMR1_OC1M);
  863. /* Configure The Forced output Mode */
  864. tmpccmr1 |= TIM_ForcedAction;
  865. /* Write to TIMx CCMR1 register */
  866. TIMx->CCMR1 = tmpccmr1;
  867. }
  868. /**
  869. * @brief Forces the TIMx output 2 waveform to active or inactive level.
  870. * @param TIMx: where x can be 1, 2, 3, or 15 to select the TIM peripheral.
  871. * @param TIM_ForcedAction: specifies the forced Action to be set to the output waveform.
  872. * This parameter can be one of the following values:
  873. * @arg TIM_ForcedAction_Active: Force active level on OC2REF
  874. * @arg TIM_ForcedAction_InActive: Force inactive level on OC2REF.
  875. * @retval None
  876. */
  877. void TIM_ForcedOC2Config(TIM_TypeDef* TIMx, uint16_t TIM_ForcedAction)
  878. {
  879. uint16_t tmpccmr1 = 0;
  880. /* Check the parameters */
  881. assert_param(IS_TIM_LIST6_PERIPH(TIMx));
  882. assert_param(IS_TIM_FORCED_ACTION(TIM_ForcedAction));
  883. tmpccmr1 = TIMx->CCMR1;
  884. /* Reset the OC2M Bits */
  885. tmpccmr1 &= (uint16_t)~((uint16_t)TIM_CCMR1_OC2M);
  886. /* Configure The Forced output Mode */
  887. tmpccmr1 |= (uint16_t)(TIM_ForcedAction << 8);
  888. /* Write to TIMx CCMR1 register */
  889. TIMx->CCMR1 = tmpccmr1;
  890. }
  891. /**
  892. * @brief Forces the TIMx output 3 waveform to active or inactive level.
  893. * @param TIMx: where x can be 1, 2 or 3 to select the TIM peripheral.
  894. * @param TIM_ForcedAction: specifies the forced Action to be set to the output waveform.
  895. * This parameter can be one of the following values:
  896. * @arg TIM_ForcedAction_Active: Force active level on OC3REF
  897. * @arg TIM_ForcedAction_InActive: Force inactive level on OC3REF.
  898. * @retval None
  899. */
  900. void TIM_ForcedOC3Config(TIM_TypeDef* TIMx, uint16_t TIM_ForcedAction)
  901. {
  902. uint16_t tmpccmr2 = 0;
  903. /* Check the parameters */
  904. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  905. assert_param(IS_TIM_FORCED_ACTION(TIM_ForcedAction));
  906. tmpccmr2 = TIMx->CCMR2;
  907. /* Reset the OC1M Bits */
  908. tmpccmr2 &= (uint16_t)~((uint16_t)TIM_CCMR2_OC3M);
  909. /* Configure The Forced output Mode */
  910. tmpccmr2 |= TIM_ForcedAction;
  911. /* Write to TIMx CCMR2 register */
  912. TIMx->CCMR2 = tmpccmr2;
  913. }
  914. /**
  915. * @brief Forces the TIMx output 4 waveform to active or inactive level.
  916. * @param TIMx: where x can be 1, 2 or 3 to select the TIM peripheral.
  917. * @param TIM_ForcedAction: specifies the forced Action to be set to the output waveform.
  918. * This parameter can be one of the following values:
  919. * @arg TIM_ForcedAction_Active: Force active level on OC4REF
  920. * @arg TIM_ForcedAction_InActive: Force inactive level on OC4REF.
  921. * @retval None
  922. */
  923. void TIM_ForcedOC4Config(TIM_TypeDef* TIMx, uint16_t TIM_ForcedAction)
  924. {
  925. uint16_t tmpccmr2 = 0;
  926. /* Check the parameters */
  927. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  928. assert_param(IS_TIM_FORCED_ACTION(TIM_ForcedAction));
  929. tmpccmr2 = TIMx->CCMR2;
  930. /* Reset the OC2M Bits */
  931. tmpccmr2 &= (uint16_t)~((uint16_t)TIM_CCMR2_OC4M);
  932. /* Configure The Forced output Mode */
  933. tmpccmr2 |= (uint16_t)(TIM_ForcedAction << 8);
  934. /* Write to TIMx CCMR2 register */
  935. TIMx->CCMR2 = tmpccmr2;
  936. }
  937. /**
  938. * @brief Sets or Resets the TIM peripheral Capture Compare Preload Control bit.
  939. * @param TIMx: where x can be 1, 2, 3 or 15 to select the TIMx peripheral
  940. * @param NewState: new state of the Capture Compare Preload Control bit
  941. * This parameter can be: ENABLE or DISABLE.
  942. * @retval None
  943. */
  944. void TIM_CCPreloadControl(TIM_TypeDef* TIMx, FunctionalState NewState)
  945. {
  946. /* Check the parameters */
  947. assert_param(IS_TIM_LIST6_PERIPH(TIMx));
  948. assert_param(IS_FUNCTIONAL_STATE(NewState));
  949. if (NewState != DISABLE)
  950. {
  951. /* Set the CCPC Bit */
  952. TIMx->CR2 |= TIM_CR2_CCPC;
  953. }
  954. else
  955. {
  956. /* Reset the CCPC Bit */
  957. TIMx->CR2 &= (uint16_t)~((uint16_t)TIM_CR2_CCPC);
  958. }
  959. }
  960. /**
  961. * @brief Enables or disables the TIMx peripheral Preload register on CCR1.
  962. * @param TIMx: where x can be 1, 2, 3, 14, 15, 16 and 17 to select the TIM peripheral.
  963. * @param TIM_OCPreload: new state of the TIMx peripheral Preload register
  964. * This parameter can be one of the following values:
  965. * @arg TIM_OCPreload_Enable
  966. * @arg TIM_OCPreload_Disable
  967. * @retval None
  968. */
  969. void TIM_OC1PreloadConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPreload)
  970. {
  971. uint16_t tmpccmr1 = 0;
  972. /* Check the parameters */
  973. assert_param(IS_TIM_LIST4_PERIPH(TIMx));
  974. assert_param(IS_TIM_OCPRELOAD_STATE(TIM_OCPreload));
  975. tmpccmr1 = TIMx->CCMR1;
  976. /* Reset the OC1PE Bit */
  977. tmpccmr1 &= (uint16_t)~((uint16_t)TIM_CCMR1_OC1PE);
  978. /* Enable or Disable the Output Compare Preload feature */
  979. tmpccmr1 |= TIM_OCPreload;
  980. /* Write to TIMx CCMR1 register */
  981. TIMx->CCMR1 = tmpccmr1;
  982. }
  983. /**
  984. * @brief Enables or disables the TIMx peripheral Preload register on CCR2.
  985. * @param TIMx: where x can be 1, 2, 3 and 15 to select the TIM peripheral.
  986. * @param TIM_OCPreload: new state of the TIMx peripheral Preload register
  987. * This parameter can be one of the following values:
  988. * @arg TIM_OCPreload_Enable
  989. * @arg TIM_OCPreload_Disable
  990. * @retval None
  991. */
  992. void TIM_OC2PreloadConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPreload)
  993. {
  994. uint16_t tmpccmr1 = 0;
  995. /* Check the parameters */
  996. assert_param(IS_TIM_LIST6_PERIPH(TIMx));
  997. assert_param(IS_TIM_OCPRELOAD_STATE(TIM_OCPreload));
  998. tmpccmr1 = TIMx->CCMR1;
  999. /* Reset the OC2PE Bit */
  1000. tmpccmr1 &= (uint16_t)~((uint16_t)TIM_CCMR1_OC2PE);
  1001. /* Enable or Disable the Output Compare Preload feature */
  1002. tmpccmr1 |= (uint16_t)(TIM_OCPreload << 8);
  1003. /* Write to TIMx CCMR1 register */
  1004. TIMx->CCMR1 = tmpccmr1;
  1005. }
  1006. /**
  1007. * @brief Enables or disables the TIMx peripheral Preload register on CCR3.
  1008. * @param TIMx: where x can be 1, 2 or 3 to select the TIM peripheral.
  1009. * @param TIM_OCPreload: new state of the TIMx peripheral Preload register
  1010. * This parameter can be one of the following values:
  1011. * @arg TIM_OCPreload_Enable
  1012. * @arg TIM_OCPreload_Disable
  1013. * @retval None
  1014. */
  1015. void TIM_OC3PreloadConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPreload)
  1016. {
  1017. uint16_t tmpccmr2 = 0;
  1018. /* Check the parameters */
  1019. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1020. assert_param(IS_TIM_OCPRELOAD_STATE(TIM_OCPreload));
  1021. tmpccmr2 = TIMx->CCMR2;
  1022. /* Reset the OC3PE Bit */
  1023. tmpccmr2 &= (uint16_t)~((uint16_t)TIM_CCMR2_OC3PE);
  1024. /* Enable or Disable the Output Compare Preload feature */
  1025. tmpccmr2 |= TIM_OCPreload;
  1026. /* Write to TIMx CCMR2 register */
  1027. TIMx->CCMR2 = tmpccmr2;
  1028. }
  1029. /**
  1030. * @brief Enables or disables the TIMx peripheral Preload register on CCR4.
  1031. * @param TIMx: where x can be 1, 2 or 3 to select the TIM peripheral.
  1032. * @param TIM_OCPreload: new state of the TIMx peripheral Preload register
  1033. * This parameter can be one of the following values:
  1034. * @arg TIM_OCPreload_Enable
  1035. * @arg TIM_OCPreload_Disable
  1036. * @retval None
  1037. */
  1038. void TIM_OC4PreloadConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPreload)
  1039. {
  1040. uint16_t tmpccmr2 = 0;
  1041. /* Check the parameters */
  1042. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1043. assert_param(IS_TIM_OCPRELOAD_STATE(TIM_OCPreload));
  1044. tmpccmr2 = TIMx->CCMR2;
  1045. /* Reset the OC4PE Bit */
  1046. tmpccmr2 &= (uint16_t)~((uint16_t)TIM_CCMR2_OC4PE);
  1047. /* Enable or Disable the Output Compare Preload feature */
  1048. tmpccmr2 |= (uint16_t)(TIM_OCPreload << 8);
  1049. /* Write to TIMx CCMR2 register */
  1050. TIMx->CCMR2 = tmpccmr2;
  1051. }
  1052. /**
  1053. * @brief Configures the TIMx Output Compare 1 Fast feature.
  1054. * @param TIMx: where x can be 1, 2, 3, 14, 15, 16 or 17 to select the TIM peripheral.
  1055. * @param TIM_OCFast: new state of the Output Compare Fast Enable Bit.
  1056. * This parameter can be one of the following values:
  1057. * @arg TIM_OCFast_Enable: TIM output compare fast enable
  1058. * @arg TIM_OCFast_Disable: TIM output compare fast disable
  1059. * @retval None
  1060. */
  1061. void TIM_OC1FastConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCFast)
  1062. {
  1063. uint16_t tmpccmr1 = 0;
  1064. /* Check the parameters */
  1065. assert_param(IS_TIM_LIST4_PERIPH(TIMx));
  1066. assert_param(IS_TIM_OCFAST_STATE(TIM_OCFast));
  1067. /* Get the TIMx CCMR1 register value */
  1068. tmpccmr1 = TIMx->CCMR1;
  1069. /* Reset the OC1FE Bit */
  1070. tmpccmr1 &= (uint16_t)~((uint16_t)TIM_CCMR1_OC1FE);
  1071. /* Enable or Disable the Output Compare Fast Bit */
  1072. tmpccmr1 |= TIM_OCFast;
  1073. /* Write to TIMx CCMR1 */
  1074. TIMx->CCMR1 = tmpccmr1;
  1075. }
  1076. /**
  1077. * @brief Configures the TIMx Output Compare 2 Fast feature.
  1078. * @param TIMx: where x can be 1, 2, 3 or 15 to select the TIM peripheral.
  1079. * @param TIM_OCFast: new state of the Output Compare Fast Enable Bit.
  1080. * This parameter can be one of the following values:
  1081. * @arg TIM_OCFast_Enable: TIM output compare fast enable
  1082. * @arg TIM_OCFast_Disable: TIM output compare fast disable
  1083. * @retval None
  1084. */
  1085. void TIM_OC2FastConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCFast)
  1086. {
  1087. uint16_t tmpccmr1 = 0;
  1088. /* Check the parameters */
  1089. assert_param(IS_TIM_LIST6_PERIPH(TIMx));
  1090. assert_param(IS_TIM_OCFAST_STATE(TIM_OCFast));
  1091. /* Get the TIMx CCMR1 register value */
  1092. tmpccmr1 = TIMx->CCMR1;
  1093. /* Reset the OC2FE Bit */
  1094. tmpccmr1 &= (uint16_t)~((uint16_t)TIM_CCMR1_OC2FE);
  1095. /* Enable or Disable the Output Compare Fast Bit */
  1096. tmpccmr1 |= (uint16_t)(TIM_OCFast << 8);
  1097. /* Write to TIMx CCMR1 */
  1098. TIMx->CCMR1 = tmpccmr1;
  1099. }
  1100. /**
  1101. * @brief Configures the TIMx Output Compare 3 Fast feature.
  1102. * @param TIMx: where x can be 1, 2 or 3 to select the TIM peripheral.
  1103. * @param TIM_OCFast: new state of the Output Compare Fast Enable Bit.
  1104. * This parameter can be one of the following values:
  1105. * @arg TIM_OCFast_Enable: TIM output compare fast enable
  1106. * @arg TIM_OCFast_Disable: TIM output compare fast disable
  1107. * @retval None
  1108. */
  1109. void TIM_OC3FastConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCFast)
  1110. {
  1111. uint16_t tmpccmr2 = 0;
  1112. /* Check the parameters */
  1113. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1114. assert_param(IS_TIM_OCFAST_STATE(TIM_OCFast));
  1115. /* Get the TIMx CCMR2 register value */
  1116. tmpccmr2 = TIMx->CCMR2;
  1117. /* Reset the OC3FE Bit */
  1118. tmpccmr2 &= (uint16_t)~((uint16_t)TIM_CCMR2_OC3FE);
  1119. /* Enable or Disable the Output Compare Fast Bit */
  1120. tmpccmr2 |= TIM_OCFast;
  1121. /* Write to TIMx CCMR2 */
  1122. TIMx->CCMR2 = tmpccmr2;
  1123. }
  1124. /**
  1125. * @brief Configures the TIMx Output Compare 4 Fast feature.
  1126. * @param TIMx: where x can be 1, 2 or 3 to select the TIM peripheral.
  1127. * @param TIM_OCFast: new state of the Output Compare Fast Enable Bit.
  1128. * This parameter can be one of the following values:
  1129. * @arg TIM_OCFast_Enable: TIM output compare fast enable
  1130. * @arg TIM_OCFast_Disable: TIM output compare fast disable
  1131. * @retval None
  1132. */
  1133. void TIM_OC4FastConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCFast)
  1134. {
  1135. uint16_t tmpccmr2 = 0;
  1136. /* Check the parameters */
  1137. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1138. assert_param(IS_TIM_OCFAST_STATE(TIM_OCFast));
  1139. /* Get the TIMx CCMR2 register value */
  1140. tmpccmr2 = TIMx->CCMR2;
  1141. /* Reset the OC4FE Bit */
  1142. tmpccmr2 &= (uint16_t)~((uint16_t)TIM_CCMR2_OC4FE);
  1143. /* Enable or Disable the Output Compare Fast Bit */
  1144. tmpccmr2 |= (uint16_t)(TIM_OCFast << 8);
  1145. /* Write to TIMx CCMR2 */
  1146. TIMx->CCMR2 = tmpccmr2;
  1147. }
  1148. /**
  1149. * @brief Clears or safeguards the OCREF1 signal on an external event
  1150. * @param TIMx: where x can be 1, 2, 3, 14, 15, 16 or 17 to select the TIM peripheral.
  1151. * @param TIM_OCClear: new state of the Output Compare Clear Enable Bit.
  1152. * This parameter can be one of the following values:
  1153. * @arg TIM_OCClear_Enable: TIM Output clear enable
  1154. * @arg TIM_OCClear_Disable: TIM Output clear disable
  1155. * @retval None
  1156. */
  1157. void TIM_ClearOC1Ref(TIM_TypeDef* TIMx, uint16_t TIM_OCClear)
  1158. {
  1159. uint16_t tmpccmr1 = 0;
  1160. /* Check the parameters */
  1161. assert_param(IS_TIM_LIST4_PERIPH(TIMx));
  1162. assert_param(IS_TIM_OCCLEAR_STATE(TIM_OCClear));
  1163. tmpccmr1 = TIMx->CCMR1;
  1164. /* Reset the OC1CE Bit */
  1165. tmpccmr1 &= (uint16_t)~((uint16_t)TIM_CCMR1_OC1CE);
  1166. /* Enable or Disable the Output Compare Clear Bit */
  1167. tmpccmr1 |= TIM_OCClear;
  1168. /* Write to TIMx CCMR1 register */
  1169. TIMx->CCMR1 = tmpccmr1;
  1170. }
  1171. /**
  1172. * @brief Clears or safeguards the OCREF2 signal on an external event
  1173. * @param TIMx: where x can be 1, 2, 3 or 15 to select the TIM peripheral.
  1174. * @param TIM_OCClear: new state of the Output Compare Clear Enable Bit.
  1175. * This parameter can be one of the following values:
  1176. * @arg TIM_OCClear_Enable: TIM Output clear enable
  1177. * @arg TIM_OCClear_Disable: TIM Output clear disable
  1178. * @retval None
  1179. */
  1180. void TIM_ClearOC2Ref(TIM_TypeDef* TIMx, uint16_t TIM_OCClear)
  1181. {
  1182. uint16_t tmpccmr1 = 0;
  1183. /* Check the parameters */
  1184. assert_param(IS_TIM_LIST6_PERIPH(TIMx));
  1185. assert_param(IS_TIM_OCCLEAR_STATE(TIM_OCClear));
  1186. tmpccmr1 = TIMx->CCMR1;
  1187. /* Reset the OC2CE Bit */
  1188. tmpccmr1 &= (uint16_t)~((uint16_t)TIM_CCMR1_OC2CE);
  1189. /* Enable or Disable the Output Compare Clear Bit */
  1190. tmpccmr1 |= (uint16_t)(TIM_OCClear << 8);
  1191. /* Write to TIMx CCMR1 register */
  1192. TIMx->CCMR1 = tmpccmr1;
  1193. }
  1194. /**
  1195. * @brief Clears or safeguards the OCREF3 signal on an external event
  1196. * @param TIMx: where x can be 1, 2 or 3 to select the TIM peripheral.
  1197. * @param TIM_OCClear: new state of the Output Compare Clear Enable Bit.
  1198. * This parameter can be one of the following values:
  1199. * @arg TIM_OCClear_Enable: TIM Output clear enable
  1200. * @arg TIM_OCClear_Disable: TIM Output clear disable
  1201. * @retval None
  1202. */
  1203. void TIM_ClearOC3Ref(TIM_TypeDef* TIMx, uint16_t TIM_OCClear)
  1204. {
  1205. uint16_t tmpccmr2 = 0;
  1206. /* Check the parameters */
  1207. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1208. assert_param(IS_TIM_OCCLEAR_STATE(TIM_OCClear));
  1209. tmpccmr2 = TIMx->CCMR2;
  1210. /* Reset the OC3CE Bit */
  1211. tmpccmr2 &= (uint16_t)~((uint16_t)TIM_CCMR2_OC3CE);
  1212. /* Enable or Disable the Output Compare Clear Bit */
  1213. tmpccmr2 |= TIM_OCClear;
  1214. /* Write to TIMx CCMR2 register */
  1215. TIMx->CCMR2 = tmpccmr2;
  1216. }
  1217. /**
  1218. * @brief Clears or safeguards the OCREF4 signal on an external event
  1219. * @param TIMx: where x can be 1, 2 or 3 to select the TIM peripheral.
  1220. * @param TIM_OCClear: new state of the Output Compare Clear Enable Bit.
  1221. * This parameter can be one of the following values:
  1222. * @arg TIM_OCClear_Enable: TIM Output clear enable
  1223. * @arg TIM_OCClear_Disable: TIM Output clear disable
  1224. * @retval None
  1225. */
  1226. void TIM_ClearOC4Ref(TIM_TypeDef* TIMx, uint16_t TIM_OCClear)
  1227. {
  1228. uint16_t tmpccmr2 = 0;
  1229. /* Check the parameters */
  1230. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1231. assert_param(IS_TIM_OCCLEAR_STATE(TIM_OCClear));
  1232. tmpccmr2 = TIMx->CCMR2;
  1233. /* Reset the OC4CE Bit */
  1234. tmpccmr2 &= (uint16_t)~((uint16_t)TIM_CCMR2_OC4CE);
  1235. /* Enable or Disable the Output Compare Clear Bit */
  1236. tmpccmr2 |= (uint16_t)(TIM_OCClear << 8);
  1237. /* Write to TIMx CCMR2 register */
  1238. TIMx->CCMR2 = tmpccmr2;
  1239. }
  1240. /**
  1241. * @brief Configures the TIMx channel 1 polarity.
  1242. * @param TIMx: where x can be 1, 2, 3, 14, 15, 16 or 17 to select the TIM peripheral.
  1243. * @param TIM_OCPolarity: specifies the OC1 Polarity
  1244. * This parmeter can be one of the following values:
  1245. * @arg TIM_OCPolarity_High: Output Compare active high
  1246. * @arg TIM_OCPolarity_Low: Output Compare active low
  1247. * @retval None
  1248. */
  1249. void TIM_OC1PolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPolarity)
  1250. {
  1251. uint16_t tmpccer = 0;
  1252. /* Check the parameters */
  1253. assert_param(IS_TIM_LIST4_PERIPH(TIMx));
  1254. assert_param(IS_TIM_OC_POLARITY(TIM_OCPolarity));
  1255. tmpccer = TIMx->CCER;
  1256. /* Set or Reset the CC1P Bit */
  1257. tmpccer &= (uint16_t)~((uint16_t)TIM_CCER_CC1P);
  1258. tmpccer |= TIM_OCPolarity;
  1259. /* Write to TIMx CCER register */
  1260. TIMx->CCER = tmpccer;
  1261. }
  1262. /**
  1263. * @brief Configures the TIMx Channel 1N polarity.
  1264. * @param TIMx: where x can be 1, 15, 16 or 17 to select the TIM peripheral.
  1265. * @param TIM_OCNPolarity: specifies the OC1N Polarity
  1266. * This parmeter can be one of the following values:
  1267. * @arg TIM_OCNPolarity_High: Output Compare active high
  1268. * @arg TIM_OCNPolarity_Low: Output Compare active low
  1269. * @retval None
  1270. */
  1271. void TIM_OC1NPolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCNPolarity)
  1272. {
  1273. uint16_t tmpccer = 0;
  1274. /* Check the parameters */
  1275. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  1276. assert_param(IS_TIM_OCN_POLARITY(TIM_OCNPolarity));
  1277. tmpccer = TIMx->CCER;
  1278. /* Set or Reset the CC1NP Bit */
  1279. tmpccer &= (uint16_t)~((uint16_t)TIM_CCER_CC1NP);
  1280. tmpccer |= TIM_OCNPolarity;
  1281. /* Write to TIMx CCER register */
  1282. TIMx->CCER = tmpccer;
  1283. }
  1284. /**
  1285. * @brief Configures the TIMx channel 2 polarity.
  1286. * @param TIMx: where x can be 1, 2, 3, or 15 to select the TIM peripheral.
  1287. * @param TIM_OCPolarity: specifies the OC2 Polarity
  1288. * This parmeter can be one of the following values:
  1289. * @arg TIM_OCPolarity_High: Output Compare active high
  1290. * @arg TIM_OCPolarity_Low: Output Compare active low
  1291. * @retval None
  1292. */
  1293. void TIM_OC2PolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPolarity)
  1294. {
  1295. uint16_t tmpccer = 0;
  1296. /* Check the parameters */
  1297. assert_param(IS_TIM_LIST6_PERIPH(TIMx));
  1298. assert_param(IS_TIM_OC_POLARITY(TIM_OCPolarity));
  1299. tmpccer = TIMx->CCER;
  1300. /* Set or Reset the CC2P Bit */
  1301. tmpccer &= (uint16_t)~((uint16_t)TIM_CCER_CC2P);
  1302. tmpccer |= (uint16_t)(TIM_OCPolarity << 4);
  1303. /* Write to TIMx CCER register */
  1304. TIMx->CCER = tmpccer;
  1305. }
  1306. /**
  1307. * @brief Configures the TIMx Channel 2N polarity.
  1308. * @param TIMx: where x can be 1 to select the TIM peripheral.
  1309. * @param TIM_OCNPolarity: specifies the OC2N Polarity
  1310. * This parmeter can be one of the following values:
  1311. * @arg TIM_OCNPolarity_High: Output Compare active high
  1312. * @arg TIM_OCNPolarity_Low: Output Compare active low
  1313. * @retval None
  1314. */
  1315. void TIM_OC2NPolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCNPolarity)
  1316. {
  1317. uint16_t tmpccer = 0;
  1318. /* Check the parameters */
  1319. assert_param(IS_TIM_LIST1_PERIPH(TIMx));
  1320. assert_param(IS_TIM_OCN_POLARITY(TIM_OCNPolarity));
  1321. tmpccer = TIMx->CCER;
  1322. /* Set or Reset the CC2NP Bit */
  1323. tmpccer &= (uint16_t)~((uint16_t)TIM_CCER_CC2NP);
  1324. tmpccer |= (uint16_t)(TIM_OCNPolarity << 4);
  1325. /* Write to TIMx CCER register */
  1326. TIMx->CCER = tmpccer;
  1327. }
  1328. /**
  1329. * @brief Configures the TIMx channel 3 polarity.
  1330. * @param TIMx: where x can be 1, 2 or 3 to select the TIM peripheral.
  1331. * @param TIM_OCPolarity: specifies the OC3 Polarity
  1332. * This parmeter can be one of the following values:
  1333. * @arg TIM_OCPolarity_High: Output Compare active high
  1334. * @arg TIM_OCPolarity_Low: Output Compare active low
  1335. * @retval None
  1336. */
  1337. void TIM_OC3PolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPolarity)
  1338. {
  1339. uint16_t tmpccer = 0;
  1340. /* Check the parameters */
  1341. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1342. assert_param(IS_TIM_OC_POLARITY(TIM_OCPolarity));
  1343. tmpccer = TIMx->CCER;
  1344. /* Set or Reset the CC3P Bit */
  1345. tmpccer &= (uint16_t)~((uint16_t)TIM_CCER_CC3P);
  1346. tmpccer |= (uint16_t)(TIM_OCPolarity << 8);
  1347. /* Write to TIMx CCER register */
  1348. TIMx->CCER = tmpccer;
  1349. }
  1350. /**
  1351. * @brief Configures the TIMx Channel 3N polarity.
  1352. * @param TIMx: where x can be 1 to select the TIM peripheral.
  1353. * @param TIM_OCNPolarity: specifies the OC3N Polarity
  1354. * This parmeter can be one of the following values:
  1355. * @arg TIM_OCNPolarity_High: Output Compare active high
  1356. * @arg TIM_OCNPolarity_Low: Output Compare active low
  1357. * @retval None
  1358. */
  1359. void TIM_OC3NPolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCNPolarity)
  1360. {
  1361. uint16_t tmpccer = 0;
  1362. /* Check the parameters */
  1363. assert_param(IS_TIM_LIST1_PERIPH(TIMx));
  1364. assert_param(IS_TIM_OCN_POLARITY(TIM_OCNPolarity));
  1365. tmpccer = TIMx->CCER;
  1366. /* Set or Reset the CC3NP Bit */
  1367. tmpccer &= (uint16_t)~((uint16_t)TIM_CCER_CC3NP);
  1368. tmpccer |= (uint16_t)(TIM_OCNPolarity << 8);
  1369. /* Write to TIMx CCER register */
  1370. TIMx->CCER = tmpccer;
  1371. }
  1372. /**
  1373. * @brief Configures the TIMx channel 4 polarity.
  1374. * @param TIMx: where x can be 1, 2 or 3 to select the TIM peripheral.
  1375. * @param TIM_OCPolarity: specifies the OC4 Polarity
  1376. * This parmeter can be one of the following values:
  1377. * @arg TIM_OCPolarity_High: Output Compare active high
  1378. * @arg TIM_OCPolarity_Low: Output Compare active low
  1379. * @retval None
  1380. */
  1381. void TIM_OC4PolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPolarity)
  1382. {
  1383. uint16_t tmpccer = 0;
  1384. /* Check the parameters */
  1385. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1386. assert_param(IS_TIM_OC_POLARITY(TIM_OCPolarity));
  1387. tmpccer = TIMx->CCER;
  1388. /* Set or Reset the CC4P Bit */
  1389. tmpccer &= (uint16_t)~((uint16_t)TIM_CCER_CC4P);
  1390. tmpccer |= (uint16_t)(TIM_OCPolarity << 12);
  1391. /* Write to TIMx CCER register */
  1392. TIMx->CCER = tmpccer;
  1393. }
  1394. /**
  1395. * @brief Selects the OCReference Clear source.
  1396. * @param TIMx: where x can be 1, 2 or 3 to select the TIM peripheral.
  1397. * @param TIM_OCReferenceClear: specifies the OCReference Clear source.
  1398. * This parameter can be one of the following values:
  1399. * @arg TIM_OCReferenceClear_ETRF: The internal OCreference clear input is connected to ETRF.
  1400. * @arg TIM_OCReferenceClear_OCREFCLR: The internal OCreference clear input is connected to OCREF_CLR input.
  1401. * @retval None
  1402. */
  1403. void TIM_SelectOCREFClear(TIM_TypeDef* TIMx, uint16_t TIM_OCReferenceClear)
  1404. {
  1405. /* Check the parameters */
  1406. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1407. assert_param(TIM_OCREFERENCECECLEAR_SOURCE(TIM_OCReferenceClear));
  1408. /* Set the TIM_OCReferenceClear source */
  1409. TIMx->SMCR &= (uint16_t)~((uint16_t)TIM_SMCR_OCCS);
  1410. TIMx->SMCR |= TIM_OCReferenceClear;
  1411. }
  1412. /**
  1413. * @brief Enables or disables the TIM Capture Compare Channel x.
  1414. * @param TIMx: where x can be 1, 2, 3, 14, 15, 16 or 17 to select the TIM peripheral.
  1415. * @param TIM_Channel: specifies the TIM Channel
  1416. * This parameter can be one of the following values:
  1417. * @arg TIM_Channel_1: TIM Channel 1
  1418. * @arg TIM_Channel_2: TIM Channel 2
  1419. * @arg TIM_Channel_3: TIM Channel 3
  1420. * @arg TIM_Channel_4: TIM Channel 4
  1421. * @param TIM_CCx: specifies the TIM Channel CCxE bit new state.
  1422. * This parameter can be: TIM_CCx_Enable or TIM_CCx_Disable.
  1423. * @retval None
  1424. */
  1425. void TIM_CCxCmd(TIM_TypeDef* TIMx, uint16_t TIM_Channel, uint16_t TIM_CCx)
  1426. {
  1427. uint16_t tmp = 0;
  1428. /* Check the parameters */
  1429. assert_param(IS_TIM_LIST4_PERIPH(TIMx));
  1430. assert_param(IS_TIM_CCX(TIM_CCx));
  1431. tmp = CCER_CCE_SET << TIM_Channel;
  1432. /* Reset the CCxE Bit */
  1433. TIMx->CCER &= (uint16_t)~ tmp;
  1434. /* Set or reset the CCxE Bit */
  1435. TIMx->CCER |= (uint16_t)(TIM_CCx << TIM_Channel);
  1436. }
  1437. /**
  1438. * @brief Enables or disables the TIM Capture Compare Channel xN.
  1439. * @param TIMx: where x can be 1, 15, 16 or 17 to select the TIM peripheral.
  1440. * @param TIM_Channel: specifies the TIM Channel
  1441. * This parmeter can be one of the following values:
  1442. * @arg TIM_Channel_1: TIM Channel 1
  1443. * @arg TIM_Channel_2: TIM Channel 2
  1444. * @arg TIM_Channel_3: TIM Channel 3
  1445. * @param TIM_CCxN: specifies the TIM Channel CCxNE bit new state.
  1446. * This parameter can be: TIM_CCxN_Enable or TIM_CCxN_Disable.
  1447. * @retval None
  1448. */
  1449. void TIM_CCxNCmd(TIM_TypeDef* TIMx, uint16_t TIM_Channel, uint16_t TIM_CCxN)
  1450. {
  1451. uint16_t tmp = 0;
  1452. /* Check the parameters */
  1453. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  1454. assert_param(IS_TIM_COMPLEMENTARY_CHANNEL(TIM_Channel));
  1455. assert_param(IS_TIM_CCXN(TIM_CCxN));
  1456. tmp = CCER_CCNE_SET << TIM_Channel;
  1457. /* Reset the CCxNE Bit */
  1458. TIMx->CCER &= (uint16_t) ~tmp;
  1459. /* Set or reset the CCxNE Bit */
  1460. TIMx->CCER |= (uint16_t)(TIM_CCxN << TIM_Channel);
  1461. }
  1462. /**
  1463. * @brief Selects the TIM peripheral Commutation event.
  1464. * @param TIMx: where x can be 1, 15, 16 or 17 to select the TIMx peripheral
  1465. * @param NewState: new state of the Commutation event.
  1466. * This parameter can be: ENABLE or DISABLE.
  1467. * @retval None
  1468. */
  1469. void TIM_SelectCOM(TIM_TypeDef* TIMx, FunctionalState NewState)
  1470. {
  1471. /* Check the parameters */
  1472. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  1473. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1474. if (NewState != DISABLE)
  1475. {
  1476. /* Set the COM Bit */
  1477. TIMx->CR2 |= TIM_CR2_CCUS;
  1478. }
  1479. else
  1480. {
  1481. /* Reset the COM Bit */
  1482. TIMx->CR2 &= (uint16_t)~((uint16_t)TIM_CR2_CCUS);
  1483. }
  1484. }
  1485. /**
  1486. * @}
  1487. */
  1488. /**
  1489. * @brief Initializes the TIM peripheral according to the specified
  1490. * parameters in the TIM_ICInitStruct.
  1491. * @param TIMx: where x can be 1, 2, 3, 14, 15, 16 or 17 to select the TIM peripheral.
  1492. * @param TIM_ICInitStruct: pointer to a TIM_ICInitTypeDef structure
  1493. * that contains the configuration information for the specified TIM
  1494. * peripheral.
  1495. * @retval None
  1496. */
  1497. void TIM_ICInit(TIM_TypeDef* TIMx, TIM_ICInitTypeDef* TIM_ICInitStruct)
  1498. {
  1499. /* Check the parameters */
  1500. assert_param(IS_TIM_LIST4_PERIPH(TIMx));
  1501. assert_param(IS_TIM_CHANNEL(TIM_ICInitStruct->TIM_Channel));
  1502. assert_param(IS_TIM_IC_SELECTION(TIM_ICInitStruct->TIM_ICSelection));
  1503. assert_param(IS_TIM_IC_PRESCALER(TIM_ICInitStruct->TIM_ICPrescaler));
  1504. assert_param(IS_TIM_IC_FILTER(TIM_ICInitStruct->TIM_ICFilter));
  1505. assert_param(IS_TIM_IC_POLARITY(TIM_ICInitStruct->TIM_ICPolarity));
  1506. if (TIM_ICInitStruct->TIM_Channel == TIM_Channel_1)
  1507. {
  1508. assert_param(IS_TIM_LIST4_PERIPH(TIMx));
  1509. /* TI1 Configuration */
  1510. TI1_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity,
  1511. TIM_ICInitStruct->TIM_ICSelection,
  1512. TIM_ICInitStruct->TIM_ICFilter);
  1513. /* Set the Input Capture Prescaler value */
  1514. TIM_SetIC1Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
  1515. }
  1516. else if (TIM_ICInitStruct->TIM_Channel == TIM_Channel_2)
  1517. {
  1518. assert_param(IS_TIM_LIST6_PERIPH(TIMx));
  1519. /* TI2 Configuration */
  1520. TI2_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity,
  1521. TIM_ICInitStruct->TIM_ICSelection,
  1522. TIM_ICInitStruct->TIM_ICFilter);
  1523. /* Set the Input Capture Prescaler value */
  1524. TIM_SetIC2Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
  1525. }
  1526. else if (TIM_ICInitStruct->TIM_Channel == TIM_Channel_3)
  1527. {
  1528. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1529. /* TI3 Configuration */
  1530. TI3_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity,
  1531. TIM_ICInitStruct->TIM_ICSelection,
  1532. TIM_ICInitStruct->TIM_ICFilter);
  1533. /* Set the Input Capture Prescaler value */
  1534. TIM_SetIC3Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
  1535. }
  1536. else
  1537. {
  1538. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1539. /* TI4 Configuration */
  1540. TI4_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity,
  1541. TIM_ICInitStruct->TIM_ICSelection,
  1542. TIM_ICInitStruct->TIM_ICFilter);
  1543. /* Set the Input Capture Prescaler value */
  1544. TIM_SetIC4Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
  1545. }
  1546. }
  1547. /**
  1548. * @brief Fills each TIM_ICInitStruct member with its default value.
  1549. * @param TIM_ICInitStruct: pointer to a TIM_ICInitTypeDef structure which will
  1550. * be initialized.
  1551. * @retval None
  1552. */
  1553. void TIM_ICStructInit(TIM_ICInitTypeDef* TIM_ICInitStruct)
  1554. {
  1555. /* Set the default configuration */
  1556. TIM_ICInitStruct->TIM_Channel = TIM_Channel_1;
  1557. TIM_ICInitStruct->TIM_ICPolarity = TIM_ICPolarity_Rising;
  1558. TIM_ICInitStruct->TIM_ICSelection = TIM_ICSelection_DirectTI;
  1559. TIM_ICInitStruct->TIM_ICPrescaler = TIM_ICPSC_DIV1;
  1560. TIM_ICInitStruct->TIM_ICFilter = 0x00;
  1561. }
  1562. /**
  1563. * @brief Configures the TIM peripheral according to the specified
  1564. * parameters in the TIM_ICInitStruct to measure an external PWM signal.
  1565. * @param TIMx: where x can be 1, 2, 3 or 15 to select the TIM peripheral.
  1566. * @param TIM_ICInitStruct: pointer to a TIM_ICInitTypeDef structure
  1567. * that contains the configuration information for the specified TIM
  1568. * peripheral.
  1569. * @retval None
  1570. */
  1571. void TIM_PWMIConfig(TIM_TypeDef* TIMx, TIM_ICInitTypeDef* TIM_ICInitStruct)
  1572. {
  1573. uint16_t icoppositepolarity = TIM_ICPolarity_Rising;
  1574. uint16_t icoppositeselection = TIM_ICSelection_DirectTI;
  1575. /* Check the parameters */
  1576. assert_param(IS_TIM_LIST6_PERIPH(TIMx));
  1577. /* Select the Opposite Input Polarity */
  1578. if (TIM_ICInitStruct->TIM_ICPolarity == TIM_ICPolarity_Rising)
  1579. {
  1580. icoppositepolarity = TIM_ICPolarity_Falling;
  1581. }
  1582. else
  1583. {
  1584. icoppositepolarity = TIM_ICPolarity_Rising;
  1585. }
  1586. /* Select the Opposite Input */
  1587. if (TIM_ICInitStruct->TIM_ICSelection == TIM_ICSelection_DirectTI)
  1588. {
  1589. icoppositeselection = TIM_ICSelection_IndirectTI;
  1590. }
  1591. else
  1592. {
  1593. icoppositeselection = TIM_ICSelection_DirectTI;
  1594. }
  1595. if (TIM_ICInitStruct->TIM_Channel == TIM_Channel_1)
  1596. {
  1597. /* TI1 Configuration */
  1598. TI1_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity, TIM_ICInitStruct->TIM_ICSelection,
  1599. TIM_ICInitStruct->TIM_ICFilter);
  1600. /* Set the Input Capture Prescaler value */
  1601. TIM_SetIC1Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
  1602. /* TI2 Configuration */
  1603. TI2_Config(TIMx, icoppositepolarity, icoppositeselection, TIM_ICInitStruct->TIM_ICFilter);
  1604. /* Set the Input Capture Prescaler value */
  1605. TIM_SetIC2Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
  1606. }
  1607. else
  1608. {
  1609. /* TI2 Configuration */
  1610. TI2_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity, TIM_ICInitStruct->TIM_ICSelection,
  1611. TIM_ICInitStruct->TIM_ICFilter);
  1612. /* Set the Input Capture Prescaler value */
  1613. TIM_SetIC2Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
  1614. /* TI1 Configuration */
  1615. TI1_Config(TIMx, icoppositepolarity, icoppositeselection, TIM_ICInitStruct->TIM_ICFilter);
  1616. /* Set the Input Capture Prescaler value */
  1617. TIM_SetIC1Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
  1618. }
  1619. }
  1620. /**
  1621. * @brief Gets the TIMx Input Capture 1 value.
  1622. * @param TIMx: where x can be 1, 2, 3, 14, 15, 16 or 17 to select the TIM peripheral.
  1623. * @retval Capture Compare 1 Register value.
  1624. */
  1625. uint32_t TIM_GetCapture1(TIM_TypeDef* TIMx)
  1626. {
  1627. /* Check the parameters */
  1628. assert_param(IS_TIM_LIST4_PERIPH(TIMx));
  1629. /* Get the Capture 1 Register value */
  1630. return TIMx->CCR1;
  1631. }
  1632. /**
  1633. * @brief Gets the TIMx Input Capture 2 value.
  1634. * @param TIMx: where x can be 1, 2, 3 or 15 to select the TIM peripheral.
  1635. * @retval Capture Compare 2 Register value.
  1636. */
  1637. uint32_t TIM_GetCapture2(TIM_TypeDef* TIMx)
  1638. {
  1639. /* Check the parameters */
  1640. assert_param(IS_TIM_LIST6_PERIPH(TIMx));
  1641. /* Get the Capture 2 Register value */
  1642. return TIMx->CCR2;
  1643. }
  1644. /**
  1645. * @brief Gets the TIMx Input Capture 3 value.
  1646. * @param TIMx: where x can be 1, 2 or 3 to select the TIM peripheral.
  1647. * @retval Capture Compare 3 Register value.
  1648. */
  1649. uint32_t TIM_GetCapture3(TIM_TypeDef* TIMx)
  1650. {
  1651. /* Check the parameters */
  1652. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1653. /* Get the Capture 3 Register value */
  1654. return TIMx->CCR3;
  1655. }
  1656. /**
  1657. * @brief Gets the TIMx Input Capture 4 value.
  1658. * @param TIMx: where x can be 1, 2 or 3 to select the TIM peripheral.
  1659. * @retval Capture Compare 4 Register value.
  1660. */
  1661. uint32_t TIM_GetCapture4(TIM_TypeDef* TIMx)
  1662. {
  1663. /* Check the parameters */
  1664. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1665. /* Get the Capture 4 Register value */
  1666. return TIMx->CCR4;
  1667. }
  1668. /**
  1669. * @brief Sets the TIMx Input Capture 1 prescaler.
  1670. * @param TIMx: where x can be 1, 2, 3, 14, 15, 16 or 17 to select the TIM peripheral.
  1671. * @param TIM_ICPSC: specifies the Input Capture1 prescaler new value.
  1672. * This parameter can be one of the following values:
  1673. * @arg TIM_ICPSC_DIV1: no prescaler
  1674. * @arg TIM_ICPSC_DIV2: capture is done once every 2 events
  1675. * @arg TIM_ICPSC_DIV4: capture is done once every 4 events
  1676. * @arg TIM_ICPSC_DIV8: capture is done once every 8 events
  1677. * @retval None
  1678. */
  1679. void TIM_SetIC1Prescaler(TIM_TypeDef* TIMx, uint16_t TIM_ICPSC)
  1680. {
  1681. /* Check the parameters */
  1682. assert_param(IS_TIM_LIST4_PERIPH(TIMx));
  1683. assert_param(IS_TIM_IC_PRESCALER(TIM_ICPSC));
  1684. /* Reset the IC1PSC Bits */
  1685. TIMx->CCMR1 &= (uint16_t)~((uint16_t)TIM_CCMR1_IC1PSC);
  1686. /* Set the IC1PSC value */
  1687. TIMx->CCMR1 |= TIM_ICPSC;
  1688. }
  1689. /**
  1690. * @brief Sets the TIMx Input Capture 2 prescaler.
  1691. * @param TIMx: where x can be 1, 2, 3 or 15 to select the TIM peripheral.
  1692. * @param TIM_ICPSC: specifies the Input Capture2 prescaler new value.
  1693. * This parameter can be one of the following values:
  1694. * @arg TIM_ICPSC_DIV1: no prescaler
  1695. * @arg TIM_ICPSC_DIV2: capture is done once every 2 events
  1696. * @arg TIM_ICPSC_DIV4: capture is done once every 4 events
  1697. * @arg TIM_ICPSC_DIV8: capture is done once every 8 events
  1698. * @retval None
  1699. */
  1700. void TIM_SetIC2Prescaler(TIM_TypeDef* TIMx, uint16_t TIM_ICPSC)
  1701. {
  1702. /* Check the parameters */
  1703. assert_param(IS_TIM_LIST6_PERIPH(TIMx));
  1704. assert_param(IS_TIM_IC_PRESCALER(TIM_ICPSC));
  1705. /* Reset the IC2PSC Bits */
  1706. TIMx->CCMR1 &= (uint16_t)~((uint16_t)TIM_CCMR1_IC2PSC);
  1707. /* Set the IC2PSC value */
  1708. TIMx->CCMR1 |= (uint16_t)(TIM_ICPSC << 8);
  1709. }
  1710. /**
  1711. * @brief Sets the TIMx Input Capture 3 prescaler.
  1712. * @param TIMx: where x can be 1, 2 or 3 to select the TIM peripheral.
  1713. * @param TIM_ICPSC: specifies the Input Capture3 prescaler new value.
  1714. * This parameter can be one of the following values:
  1715. * @arg TIM_ICPSC_DIV1: no prescaler
  1716. * @arg TIM_ICPSC_DIV2: capture is done once every 2 events
  1717. * @arg TIM_ICPSC_DIV4: capture is done once every 4 events
  1718. * @arg TIM_ICPSC_DIV8: capture is done once every 8 events
  1719. * @retval None
  1720. */
  1721. void TIM_SetIC3Prescaler(TIM_TypeDef* TIMx, uint16_t TIM_ICPSC)
  1722. {
  1723. /* Check the parameters */
  1724. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1725. assert_param(IS_TIM_IC_PRESCALER(TIM_ICPSC));
  1726. /* Reset the IC3PSC Bits */
  1727. TIMx->CCMR2 &= (uint16_t)~((uint16_t)TIM_CCMR2_IC3PSC);
  1728. /* Set the IC3PSC value */
  1729. TIMx->CCMR2 |= TIM_ICPSC;
  1730. }
  1731. /**
  1732. * @brief Sets the TIMx Input Capture 4 prescaler.
  1733. * @param TIMx: where x can be 1, 2 or 3 to select the TIM peripheral.
  1734. * @param TIM_ICPSC: specifies the Input Capture4 prescaler new value.
  1735. * This parameter can be one of the following values:
  1736. * @arg TIM_ICPSC_DIV1: no prescaler
  1737. * @arg TIM_ICPSC_DIV2: capture is done once every 2 events
  1738. * @arg TIM_ICPSC_DIV4: capture is done once every 4 events
  1739. * @arg TIM_ICPSC_DIV8: capture is done once every 8 events
  1740. * @retval None
  1741. */
  1742. void TIM_SetIC4Prescaler(TIM_TypeDef* TIMx, uint16_t TIM_ICPSC)
  1743. {
  1744. /* Check the parameters */
  1745. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1746. assert_param(IS_TIM_IC_PRESCALER(TIM_ICPSC));
  1747. /* Reset the IC4PSC Bits */
  1748. TIMx->CCMR2 &= (uint16_t)~((uint16_t)TIM_CCMR2_IC4PSC);
  1749. /* Set the IC4PSC value */
  1750. TIMx->CCMR2 |= (uint16_t)(TIM_ICPSC << 8);
  1751. }
  1752. /**
  1753. * @}
  1754. */
  1755. /**
  1756. * @brief Enables or disables the specified TIM interrupts.
  1757. * @param TIMx: where x can be 1, 2, 3, 6, 7, 14, 15, 16 or 17 to select the TIMx peripheral.
  1758. * @param TIM_IT: specifies the TIM interrupts sources to be enabled or disabled.
  1759. * This parameter can be any combination of the following values:
  1760. * @arg TIM_IT_Update: TIM update Interrupt source
  1761. * @arg TIM_IT_CC1: TIM Capture Compare 1 Interrupt source
  1762. * @arg TIM_IT_CC2: TIM Capture Compare 2 Interrupt source
  1763. * @arg TIM_IT_CC3: TIM Capture Compare 3 Interrupt source
  1764. * @arg TIM_IT_CC4: TIM Capture Compare 4 Interrupt source
  1765. * @arg TIM_IT_COM: TIM Commutation Interrupt source
  1766. * @arg TIM_IT_Trigger: TIM Trigger Interrupt source
  1767. * @arg TIM_IT_Break: TIM Break Interrupt source
  1768. *
  1769. * @note TIM6 and TIM7 can only generate an update interrupt.
  1770. * @note TIM15 can have only TIM_IT_Update, TIM_IT_CC1,TIM_IT_CC2 or TIM_IT_Trigger.
  1771. * @note TIM14, TIM16 and TIM17 can have TIM_IT_Update or TIM_IT_CC1.
  1772. * @note TIM_IT_Break is used only with TIM1 and TIM15.
  1773. * @note TIM_IT_COM is used only with TIM1, TIM15, TIM16 and TIM17.
  1774. *
  1775. * @param NewState: new state of the TIM interrupts.
  1776. * This parameter can be: ENABLE or DISABLE.
  1777. * @retval None
  1778. */
  1779. void TIM_ITConfig(TIM_TypeDef* TIMx, uint16_t TIM_IT, FunctionalState NewState)
  1780. {
  1781. /* Check the parameters */
  1782. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  1783. assert_param(IS_TIM_IT(TIM_IT));
  1784. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1785. if (NewState != DISABLE)
  1786. {
  1787. /* Enable the Interrupt sources */
  1788. TIMx->DIER |= TIM_IT;
  1789. }
  1790. else
  1791. {
  1792. /* Disable the Interrupt sources */
  1793. TIMx->DIER &= (uint16_t)~TIM_IT;
  1794. }
  1795. }
  1796. /**
  1797. * @brief Configures the TIMx event to be generate by software.
  1798. * @param TIMx: where x can be 1, 2, 3, 6, 7, 14, 15, 16 or 17 to select the
  1799. * TIM peripheral.
  1800. * @param TIM_EventSource: specifies the event source.
  1801. * This parameter can be one or more of the following values:
  1802. * @arg TIM_EventSource_Update: Timer update Event source
  1803. * @arg TIM_EventSource_CC1: Timer Capture Compare 1 Event source
  1804. * @arg TIM_EventSource_CC2: Timer Capture Compare 2 Event source
  1805. * @arg TIM_EventSource_CC3: Timer Capture Compare 3 Event source
  1806. * @arg TIM_EventSource_CC4: Timer Capture Compare 4 Event source
  1807. * @arg TIM_EventSource_COM: Timer COM event source
  1808. * @arg TIM_EventSource_Trigger: Timer Trigger Event source
  1809. * @arg TIM_EventSource_Break: Timer Break event source
  1810. *
  1811. * @note TIM6 and TIM7 can only generate an update event.
  1812. * @note TIM_EventSource_COM and TIM_EventSource_Break are used only with TIM1.
  1813. *
  1814. * @retval None
  1815. */
  1816. void TIM_GenerateEvent(TIM_TypeDef* TIMx, uint16_t TIM_EventSource)
  1817. {
  1818. /* Check the parameters */
  1819. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  1820. assert_param(IS_TIM_EVENT_SOURCE(TIM_EventSource));
  1821. /* Set the event sources */
  1822. TIMx->EGR = TIM_EventSource;
  1823. }
  1824. /**
  1825. * @brief Checks whether the specified TIM flag is set or not.
  1826. * @param TIMx: where x can be 1, 2, 3, 6, 7, 14, 15, 16 or 17 to select the TIM peripheral.
  1827. * @param TIM_FLAG: specifies the flag to check.
  1828. * This parameter can be one of the following values:
  1829. * @arg TIM_FLAG_Update: TIM update Flag
  1830. * @arg TIM_FLAG_CC1: TIM Capture Compare 1 Flag
  1831. * @arg TIM_FLAG_CC2: TIM Capture Compare 2 Flag
  1832. * @arg TIM_FLAG_CC3: TIM Capture Compare 3 Flag
  1833. * @arg TIM_FLAG_CC4: TIM Capture Compare 4 Flag
  1834. * @arg TIM_FLAG_COM: TIM Commutation Flag
  1835. * @arg TIM_FLAG_Trigger: TIM Trigger Flag
  1836. * @arg TIM_FLAG_Break: TIM Break Flag
  1837. * @arg TIM_FLAG_CC1OF: TIM Capture Compare 1 overcapture Flag
  1838. * @arg TIM_FLAG_CC2OF: TIM Capture Compare 2 overcapture Flag
  1839. * @arg TIM_FLAG_CC3OF: TIM Capture Compare 3 overcapture Flag
  1840. * @arg TIM_FLAG_CC4OF: TIM Capture Compare 4 overcapture Flag
  1841. *
  1842. * @note TIM6 and TIM7 can have only one update flag.
  1843. * @note TIM15 can have only TIM_FLAG_Update, TIM_FLAG_CC1, TIM_FLAG_CC2 or TIM_FLAG_Trigger.
  1844. * @note TIM14, TIM16 and TIM17 can have TIM_FLAG_Update or TIM_FLAG_CC1.
  1845. * @note TIM_FLAG_Break is used only with TIM1 and TIM15.
  1846. * @note TIM_FLAG_COM is used only with TIM1 TIM15, TIM16 and TIM17.
  1847. *
  1848. * @retval The new state of TIM_FLAG (SET or RESET).
  1849. */
  1850. FlagStatus TIM_GetFlagStatus(TIM_TypeDef* TIMx, uint16_t TIM_FLAG)
  1851. {
  1852. ITStatus bitstatus = RESET;
  1853. /* Check the parameters */
  1854. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  1855. assert_param(IS_TIM_GET_FLAG(TIM_FLAG));
  1856. if ((TIMx->SR & TIM_FLAG) != (uint16_t)RESET)
  1857. {
  1858. bitstatus = SET;
  1859. }
  1860. else
  1861. {
  1862. bitstatus = RESET;
  1863. }
  1864. return bitstatus;
  1865. }
  1866. /**
  1867. * @brief Clears the TIMx's pending flags.
  1868. * @param TIMx: where x can be 1, 2, 3, 6, 7, 14, 15, 16 or 17 to select the TIM peripheral.
  1869. * @param TIM_FLAG: specifies the flag bit to clear.
  1870. * This parameter can be any combination of the following values:
  1871. * @arg TIM_FLAG_Update: TIM update Flag
  1872. * @arg TIM_FLAG_CC1: TIM Capture Compare 1 Flag
  1873. * @arg TIM_FLAG_CC2: TIM Capture Compare 2 Flag
  1874. * @arg TIM_FLAG_CC3: TIM Capture Compare 3 Flag
  1875. * @arg TIM_FLAG_CC4: TIM Capture Compare 4 Flag
  1876. * @arg TIM_FLAG_COM: TIM Commutation Flag
  1877. * @arg TIM_FLAG_Trigger: TIM Trigger Flag
  1878. * @arg TIM_FLAG_Break: TIM Break Flag
  1879. * @arg TIM_FLAG_CC1OF: TIM Capture Compare 1 overcapture Flag
  1880. * @arg TIM_FLAG_CC2OF: TIM Capture Compare 2 overcapture Flag
  1881. * @arg TIM_FLAG_CC3OF: TIM Capture Compare 3 overcapture Flag
  1882. * @arg TIM_FLAG_CC4OF: TIM Capture Compare 4 overcapture Flag
  1883. *
  1884. * @note TIM6 and TIM7 can have only one update flag.
  1885. * @note TIM15 can have only TIM_FLAG_Update, TIM_FLAG_CC1,TIM_FLAG_CC2 or
  1886. * TIM_FLAG_Trigger.
  1887. * @note TIM14, TIM16 and TIM17 can have TIM_FLAG_Update or TIM_FLAG_CC1.
  1888. * @note TIM_FLAG_Break is used only with TIM1 and TIM15.
  1889. * @note TIM_FLAG_COM is used only with TIM1, TIM15, TIM16 and TIM17.
  1890. *
  1891. * @retval None
  1892. */
  1893. void TIM_ClearFlag(TIM_TypeDef* TIMx, uint16_t TIM_FLAG)
  1894. {
  1895. /* Check the parameters */
  1896. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  1897. assert_param(IS_TIM_CLEAR_FLAG(TIM_FLAG));
  1898. /* Clear the flags */
  1899. TIMx->SR = (uint16_t)~TIM_FLAG;
  1900. }
  1901. /**
  1902. * @brief Checks whether the TIM interrupt has occurred or not.
  1903. * @param TIMx: where x can be 1, 2, 3, 6, 7, 14, 15, 16 or 17 to select the TIM peripheral.
  1904. * @param TIM_IT: specifies the TIM interrupt source to check.
  1905. * This parameter can be one of the following values:
  1906. * @arg TIM_IT_Update: TIM update Interrupt source
  1907. * @arg TIM_IT_CC1: TIM Capture Compare 1 Interrupt source
  1908. * @arg TIM_IT_CC2: TIM Capture Compare 2 Interrupt source
  1909. * @arg TIM_IT_CC3: TIM Capture Compare 3 Interrupt source
  1910. * @arg TIM_IT_CC4: TIM Capture Compare 4 Interrupt source
  1911. * @arg TIM_IT_COM: TIM Commutation Interrupt source
  1912. * @arg TIM_IT_Trigger: TIM Trigger Interrupt source
  1913. * @arg TIM_IT_Break: TIM Break Interrupt source
  1914. *
  1915. * @note TIM6 and TIM7 can generate only an update interrupt.
  1916. * @note TIM15 can have only TIM_IT_Update, TIM_IT_CC1, TIM_IT_CC2 or TIM_IT_Trigger.
  1917. * @note TIM14, TIM16 and TIM17 can have TIM_IT_Update or TIM_IT_CC1.
  1918. * @note TIM_IT_Break is used only with TIM1 and TIM15.
  1919. * @note TIM_IT_COM is used only with TIM1, TIM15, TIM16 and TIM17.
  1920. *
  1921. * @retval The new state of the TIM_IT(SET or RESET).
  1922. */
  1923. ITStatus TIM_GetITStatus(TIM_TypeDef* TIMx, uint16_t TIM_IT)
  1924. {
  1925. ITStatus bitstatus = RESET;
  1926. uint16_t itstatus = 0x0, itenable = 0x0;
  1927. /* Check the parameters */
  1928. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  1929. assert_param(IS_TIM_GET_IT(TIM_IT));
  1930. itstatus = TIMx->SR & TIM_IT;
  1931. itenable = TIMx->DIER & TIM_IT;
  1932. if ((itstatus != (uint16_t)RESET) && (itenable != (uint16_t)RESET))
  1933. {
  1934. bitstatus = SET;
  1935. }
  1936. else
  1937. {
  1938. bitstatus = RESET;
  1939. }
  1940. return bitstatus;
  1941. }
  1942. /**
  1943. * @brief Clears the TIMx's interrupt pending bits.
  1944. * @param TIMx: where x can be 1, 2, 3, 6, 7, 14, 15, 16 or 17 to select the TIM peripheral.
  1945. * @param TIM_IT: specifies the pending bit to clear.
  1946. * This parameter can be any combination of the following values:
  1947. * @arg TIM_IT_Update: TIM1 update Interrupt source
  1948. * @arg TIM_IT_CC1: TIM Capture Compare 1 Interrupt source
  1949. * @arg TIM_IT_CC2: TIM Capture Compare 2 Interrupt source
  1950. * @arg TIM_IT_CC3: TIM Capture Compare 3 Interrupt source
  1951. * @arg TIM_IT_CC4: TIM Capture Compare 4 Interrupt source
  1952. * @arg TIM_IT_COM: TIM Commutation Interrupt source
  1953. * @arg TIM_IT_Trigger: TIM Trigger Interrupt source
  1954. * @arg TIM_IT_Break: TIM Break Interrupt source
  1955. *
  1956. * @note TIM6 and TIM7 can generate only an update interrupt.
  1957. * @note TIM15 can have only TIM_IT_Update, TIM_IT_CC1, TIM_IT_CC2 or TIM_IT_Trigger.
  1958. * @note TIM14, TIM16 and TIM17 can have TIM_IT_Update or TIM_IT_CC1.
  1959. * @note TIM_IT_Break is used only with TIM1 and TIM15.
  1960. * @note TIM_IT_COM is used only with TIM1, TIM15, TIM16 and TIM17.
  1961. *
  1962. * @retval None
  1963. */
  1964. void TIM_ClearITPendingBit(TIM_TypeDef* TIMx, uint16_t TIM_IT)
  1965. {
  1966. /* Check the parameters */
  1967. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  1968. assert_param(IS_TIM_IT(TIM_IT));
  1969. /* Clear the IT pending Bit */
  1970. TIMx->SR = (uint16_t)~TIM_IT;
  1971. }
  1972. /**
  1973. * @brief Configures the TIMx's DMA interface.
  1974. * @param TIMx: where x can be 1, 2, 3, 15, 16 or 17 to select the TIM peripheral.
  1975. * @param TIM_DMABase: DMA Base address.
  1976. * This parameter can be one of the following values:
  1977. * @arg TIM_DMABase_CR1
  1978. * @arg TIM_DMABase_CR2
  1979. * @arg TIM_DMABase_SMCR
  1980. * @arg TIM_DMABase_DIER
  1981. * @arg TIM_DMABase_SR
  1982. * @arg TIM_DMABase_EGR
  1983. * @arg TIM_DMABase_CCMR1
  1984. * @arg TIM_DMABase_CCMR2
  1985. * @arg TIM_DMABase_CCER
  1986. * @arg TIM_DMABase_CNT
  1987. * @arg TIM_DMABase_PSC
  1988. * @arg TIM_DMABase_ARR
  1989. * @arg TIM_DMABase_CCR1
  1990. * @arg TIM_DMABase_CCR2
  1991. * @arg TIM_DMABase_CCR3
  1992. * @arg TIM_DMABase_CCR4
  1993. * @arg TIM_DMABase_DCR
  1994. * @arg TIM_DMABase_OR
  1995. * @param TIM_DMABurstLength: DMA Burst length. This parameter can be one value
  1996. * between: TIM_DMABurstLength_1Transfer and TIM_DMABurstLength_18Transfers.
  1997. * @retval None
  1998. */
  1999. void TIM_DMAConfig(TIM_TypeDef* TIMx, uint16_t TIM_DMABase, uint16_t TIM_DMABurstLength)
  2000. {
  2001. /* Check the parameters */
  2002. assert_param(IS_TIM_LIST4_PERIPH(TIMx));
  2003. assert_param(IS_TIM_DMA_BASE(TIM_DMABase));
  2004. assert_param(IS_TIM_DMA_LENGTH(TIM_DMABurstLength));
  2005. /* Set the DMA Base and the DMA Burst Length */
  2006. TIMx->DCR = TIM_DMABase | TIM_DMABurstLength;
  2007. }
  2008. /**
  2009. * @brief Enables or disables the TIMx's DMA Requests.
  2010. * @param TIMx: where x can be 1, 2, 3, 6, 7, 15, 16 or 17 to select the TIM peripheral.
  2011. * @param TIM_DMASource: specifies the DMA Request sources.
  2012. * This parameter can be any combination of the following values:
  2013. * @arg TIM_DMA_Update: TIM update Interrupt source
  2014. * @arg TIM_DMA_CC1: TIM Capture Compare 1 DMA source
  2015. * @arg TIM_DMA_CC2: TIM Capture Compare 2 DMA source
  2016. * @arg TIM_DMA_CC3: TIM Capture Compare 3 DMA source
  2017. * @arg TIM_DMA_CC4: TIM Capture Compare 4 DMA source
  2018. * @arg TIM_DMA_COM: TIM Commutation DMA source
  2019. * @arg TIM_DMA_Trigger: TIM Trigger DMA source
  2020. * @param NewState: new state of the DMA Request sources.
  2021. * This parameter can be: ENABLE or DISABLE.
  2022. * @retval None
  2023. */
  2024. void TIM_DMACmd(TIM_TypeDef* TIMx, uint16_t TIM_DMASource, FunctionalState NewState)
  2025. {
  2026. /* Check the parameters */
  2027. assert_param(IS_TIM_LIST10_PERIPH(TIMx));
  2028. assert_param(IS_TIM_DMA_SOURCE(TIM_DMASource));
  2029. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2030. if (NewState != DISABLE)
  2031. {
  2032. /* Enable the DMA sources */
  2033. TIMx->DIER |= TIM_DMASource;
  2034. }
  2035. else
  2036. {
  2037. /* Disable the DMA sources */
  2038. TIMx->DIER &= (uint16_t)~TIM_DMASource;
  2039. }
  2040. }
  2041. /**
  2042. * @brief Selects the TIMx peripheral Capture Compare DMA source.
  2043. * @param TIMx: where x can be 1, 2, 3, 15, 16 or 17 to select the TIM peripheral.
  2044. * @param NewState: new state of the Capture Compare DMA source
  2045. * This parameter can be: ENABLE or DISABLE.
  2046. * @retval None
  2047. */
  2048. void TIM_SelectCCDMA(TIM_TypeDef* TIMx, FunctionalState NewState)
  2049. {
  2050. /* Check the parameters */
  2051. assert_param(IS_TIM_LIST5_PERIPH(TIMx));
  2052. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2053. if (NewState != DISABLE)
  2054. {
  2055. /* Set the CCDS Bit */
  2056. TIMx->CR2 |= TIM_CR2_CCDS;
  2057. }
  2058. else
  2059. {
  2060. /* Reset the CCDS Bit */
  2061. TIMx->CR2 &= (uint16_t)~((uint16_t)TIM_CR2_CCDS);
  2062. }
  2063. }
  2064. /**
  2065. * @}
  2066. */
  2067. /**
  2068. * @brief Configures the TIMx internal Clock
  2069. * @param TIMx: where x can be 1, 2, 3, or 15 to select the TIM peripheral.
  2070. * @retval None
  2071. */
  2072. void TIM_InternalClockConfig(TIM_TypeDef* TIMx)
  2073. {
  2074. /* Check the parameters */
  2075. assert_param(IS_TIM_LIST6_PERIPH(TIMx));
  2076. /* Disable slave mode to clock the prescaler directly with the internal clock */
  2077. TIMx->SMCR &= (uint16_t)(~((uint16_t)TIM_SMCR_SMS));
  2078. }
  2079. /**
  2080. * @brief Configures the TIMx Internal Trigger as External Clock
  2081. * @param TIMx: where x can be 1, 2, 3, or 15 to select the TIM peripheral.
  2082. * @param TIM_ITRSource: Trigger source.
  2083. * This parameter can be one of the following values:
  2084. * @arg TIM_TS_ITR0: Internal Trigger 0
  2085. * @arg TIM_TS_ITR1: Internal Trigger 1
  2086. * @arg TIM_TS_ITR2: Internal Trigger 2
  2087. * @arg TIM_TS_ITR3: Internal Trigger 3
  2088. * @retval None
  2089. */
  2090. void TIM_ITRxExternalClockConfig(TIM_TypeDef* TIMx, uint16_t TIM_InputTriggerSource)
  2091. {
  2092. /* Check the parameters */
  2093. assert_param(IS_TIM_LIST6_PERIPH(TIMx));
  2094. assert_param(IS_TIM_INTERNAL_TRIGGER_SELECTION(TIM_InputTriggerSource));
  2095. /* Select the Internal Trigger */
  2096. TIM_SelectInputTrigger(TIMx, TIM_InputTriggerSource);
  2097. /* Select the External clock mode1 */
  2098. TIMx->SMCR |= TIM_SlaveMode_External1;
  2099. }
  2100. /**
  2101. * @brief Configures the TIMx Trigger as External Clock
  2102. * @param TIMx: where x can be 1, 2, 3, or 15 to select the TIM peripheral.
  2103. * @param TIM_TIxExternalCLKSource: Trigger source.
  2104. * This parameter can be one of the following values:
  2105. * @arg TIM_TIxExternalCLK1Source_TI1ED: TI1 Edge Detector
  2106. * @arg TIM_TIxExternalCLK1Source_TI1: Filtered Timer Input 1
  2107. * @arg TIM_TIxExternalCLK1Source_TI2: Filtered Timer Input 2
  2108. * @param TIM_ICPolarity: specifies the TIx Polarity.
  2109. * This parameter can be one of the following values:
  2110. * @arg TIM_ICPolarity_Rising
  2111. * @arg TIM_ICPolarity_Falling
  2112. * @param ICFilter: specifies the filter value.
  2113. * This parameter must be a value between 0x0 and 0xF.
  2114. * @retval None
  2115. */
  2116. void TIM_TIxExternalClockConfig(TIM_TypeDef* TIMx, uint16_t TIM_TIxExternalCLKSource,
  2117. uint16_t TIM_ICPolarity, uint16_t ICFilter)
  2118. {
  2119. /* Check the parameters */
  2120. assert_param(IS_TIM_LIST6_PERIPH(TIMx));
  2121. assert_param(IS_TIM_IC_POLARITY(TIM_ICPolarity));
  2122. assert_param(IS_TIM_IC_FILTER(ICFilter));
  2123. /* Configure the Timer Input Clock Source */
  2124. if (TIM_TIxExternalCLKSource == TIM_TIxExternalCLK1Source_TI2)
  2125. {
  2126. TI2_Config(TIMx, TIM_ICPolarity, TIM_ICSelection_DirectTI, ICFilter);
  2127. }
  2128. else
  2129. {
  2130. TI1_Config(TIMx, TIM_ICPolarity, TIM_ICSelection_DirectTI, ICFilter);
  2131. }
  2132. /* Select the Trigger source */
  2133. TIM_SelectInputTrigger(TIMx, TIM_TIxExternalCLKSource);
  2134. /* Select the External clock mode1 */
  2135. TIMx->SMCR |= TIM_SlaveMode_External1;
  2136. }
  2137. /**
  2138. * @brief Configures the External clock Mode1
  2139. * @param TIMx: where x can be 1, 2 or 3 to select the TIM peripheral.
  2140. * @param TIM_ExtTRGPrescaler: The external Trigger Prescaler.
  2141. * This parameter can be one of the following values:
  2142. * @arg TIM_ExtTRGPSC_OFF: ETRP Prescaler OFF.
  2143. * @arg TIM_ExtTRGPSC_DIV2: ETRP frequency divided by 2.
  2144. * @arg TIM_ExtTRGPSC_DIV4: ETRP frequency divided by 4.
  2145. * @arg TIM_ExtTRGPSC_DIV8: ETRP frequency divided by 8.
  2146. * @param TIM_ExtTRGPolarity: The external Trigger Polarity.
  2147. * This parameter can be one of the following values:
  2148. * @arg TIM_ExtTRGPolarity_Inverted: active low or falling edge active.
  2149. * @arg TIM_ExtTRGPolarity_NonInverted: active high or rising edge active.
  2150. * @param ExtTRGFilter: External Trigger Filter.
  2151. * This parameter must be a value between 0x00 and 0x0F
  2152. * @retval None
  2153. */
  2154. void TIM_ETRClockMode1Config(TIM_TypeDef* TIMx, uint16_t TIM_ExtTRGPrescaler, uint16_t TIM_ExtTRGPolarity,
  2155. uint16_t ExtTRGFilter)
  2156. {
  2157. uint16_t tmpsmcr = 0;
  2158. /* Check the parameters */
  2159. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  2160. assert_param(IS_TIM_EXT_PRESCALER(TIM_ExtTRGPrescaler));
  2161. assert_param(IS_TIM_EXT_POLARITY(TIM_ExtTRGPolarity));
  2162. assert_param(IS_TIM_EXT_FILTER(ExtTRGFilter));
  2163. /* Configure the ETR Clock source */
  2164. TIM_ETRConfig(TIMx, TIM_ExtTRGPrescaler, TIM_ExtTRGPolarity, ExtTRGFilter);
  2165. /* Get the TIMx SMCR register value */
  2166. tmpsmcr = TIMx->SMCR;
  2167. /* Reset the SMS Bits */
  2168. tmpsmcr &= (uint16_t)(~((uint16_t)TIM_SMCR_SMS));
  2169. /* Select the External clock mode1 */
  2170. tmpsmcr |= TIM_SlaveMode_External1;
  2171. /* Select the Trigger selection : ETRF */
  2172. tmpsmcr &= (uint16_t)(~((uint16_t)TIM_SMCR_TS));
  2173. tmpsmcr |= TIM_TS_ETRF;
  2174. /* Write to TIMx SMCR */
  2175. TIMx->SMCR = tmpsmcr;
  2176. }
  2177. /**
  2178. * @brief Configures the External clock Mode2
  2179. * @param TIMx: where x can be 1, 2 or 3 to select the TIM peripheral.
  2180. * @param TIM_ExtTRGPrescaler: The external Trigger Prescaler.
  2181. * This parameter can be one of the following values:
  2182. * @arg TIM_ExtTRGPSC_OFF: ETRP Prescaler OFF.
  2183. * @arg TIM_ExtTRGPSC_DIV2: ETRP frequency divided by 2.
  2184. * @arg TIM_ExtTRGPSC_DIV4: ETRP frequency divided by 4.
  2185. * @arg TIM_ExtTRGPSC_DIV8: ETRP frequency divided by 8.
  2186. * @param TIM_ExtTRGPolarity: The external Trigger Polarity.
  2187. * This parameter can be one of the following values:
  2188. * @arg TIM_ExtTRGPolarity_Inverted: active low or falling edge active.
  2189. * @arg TIM_ExtTRGPolarity_NonInverted: active high or rising edge active.
  2190. * @param ExtTRGFilter: External Trigger Filter.
  2191. * This parameter must be a value between 0x00 and 0x0F
  2192. * @retval None
  2193. */
  2194. void TIM_ETRClockMode2Config(TIM_TypeDef* TIMx, uint16_t TIM_ExtTRGPrescaler,
  2195. uint16_t TIM_ExtTRGPolarity, uint16_t ExtTRGFilter)
  2196. {
  2197. /* Check the parameters */
  2198. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  2199. assert_param(IS_TIM_EXT_PRESCALER(TIM_ExtTRGPrescaler));
  2200. assert_param(IS_TIM_EXT_POLARITY(TIM_ExtTRGPolarity));
  2201. assert_param(IS_TIM_EXT_FILTER(ExtTRGFilter));
  2202. /* Configure the ETR Clock source */
  2203. TIM_ETRConfig(TIMx, TIM_ExtTRGPrescaler, TIM_ExtTRGPolarity, ExtTRGFilter);
  2204. /* Enable the External clock mode2 */
  2205. TIMx->SMCR |= TIM_SMCR_ECE;
  2206. }
  2207. /**
  2208. * @}
  2209. */
  2210. /**
  2211. * @brief Selects the Input Trigger source
  2212. * @param TIMx: where x can be 1, 2, 3 or 15 to select the TIM peripheral.
  2213. * @param TIM_InputTriggerSource: The Input Trigger source.
  2214. * This parameter can be one of the following values:
  2215. * @arg TIM_TS_ITR0: Internal Trigger 0
  2216. * @arg TIM_TS_ITR1: Internal Trigger 1
  2217. * @arg TIM_TS_ITR2: Internal Trigger 2
  2218. * @arg TIM_TS_ITR3: Internal Trigger 3
  2219. * @arg TIM_TS_TI1F_ED: TI1 Edge Detector
  2220. * @arg TIM_TS_TI1FP1: Filtered Timer Input 1
  2221. * @arg TIM_TS_TI2FP2: Filtered Timer Input 2
  2222. * @arg TIM_TS_ETRF: External Trigger input
  2223. * @retval None
  2224. */
  2225. void TIM_SelectInputTrigger(TIM_TypeDef* TIMx, uint16_t TIM_InputTriggerSource)
  2226. {
  2227. uint16_t tmpsmcr = 0;
  2228. /* Check the parameters */
  2229. assert_param(IS_TIM_LIST6_PERIPH(TIMx));
  2230. assert_param(IS_TIM_TRIGGER_SELECTION(TIM_InputTriggerSource));
  2231. /* Get the TIMx SMCR register value */
  2232. tmpsmcr = TIMx->SMCR;
  2233. /* Reset the TS Bits */
  2234. tmpsmcr &= (uint16_t)(~((uint16_t)TIM_SMCR_TS));
  2235. /* Set the Input Trigger source */
  2236. tmpsmcr |= TIM_InputTriggerSource;
  2237. /* Write to TIMx SMCR */
  2238. TIMx->SMCR = tmpsmcr;
  2239. }
  2240. /**
  2241. * @brief Selects the TIMx Trigger Output Mode.
  2242. * @param TIMx: where x can be 1, 2, 3, 6, 7, or 15 to select the TIM peripheral.
  2243. * @param TIM_TRGOSource: specifies the Trigger Output source.
  2244. * This parameter can be one of the following values:
  2245. *
  2246. * - For all TIMx
  2247. * @arg TIM_TRGOSource_Reset: The UG bit in the TIM_EGR register is used as the trigger output (TRGO).
  2248. * @arg TIM_TRGOSource_Enable: The Counter Enable CEN is used as the trigger output (TRGO).
  2249. * @arg TIM_TRGOSource_Update: The update event is selected as the trigger output (TRGO).
  2250. *
  2251. * - For all TIMx except TIM6 and TIM7
  2252. * @arg TIM_TRGOSource_OC1: The trigger output sends a positive pulse when the CC1IF flag
  2253. * is to be set, as soon as a capture or compare match occurs (TRGO).
  2254. * @arg TIM_TRGOSource_OC1Ref: OC1REF signal is used as the trigger output (TRGO).
  2255. * @arg TIM_TRGOSource_OC2Ref: OC2REF signal is used as the trigger output (TRGO).
  2256. * @arg TIM_TRGOSource_OC3Ref: OC3REF signal is used as the trigger output (TRGO).
  2257. * @arg TIM_TRGOSource_OC4Ref: OC4REF signal is used as the trigger output (TRGO).
  2258. *
  2259. * @retval None
  2260. */
  2261. void TIM_SelectOutputTrigger(TIM_TypeDef* TIMx, uint16_t TIM_TRGOSource)
  2262. {
  2263. /* Check the parameters */
  2264. assert_param(IS_TIM_LIST9_PERIPH(TIMx));
  2265. assert_param(IS_TIM_TRGO_SOURCE(TIM_TRGOSource));
  2266. /* Reset the MMS Bits */
  2267. TIMx->CR2 &= (uint16_t)~((uint16_t)TIM_CR2_MMS);
  2268. /* Select the TRGO source */
  2269. TIMx->CR2 |= TIM_TRGOSource;
  2270. }
  2271. /**
  2272. * @brief Selects the TIMx Slave Mode.
  2273. * @param TIMx: where x can be 1, 2, 3 or 15 to select the TIM peripheral.
  2274. * @param TIM_SlaveMode: specifies the Timer Slave Mode.
  2275. * This parameter can be one of the following values:
  2276. * @arg TIM_SlaveMode_Reset: Rising edge of the selected trigger signal (TRGI) re-initializes
  2277. * the counter and triggers an update of the registers.
  2278. * @arg TIM_SlaveMode_Gated: The counter clock is enabled when the trigger signal (TRGI) is high.
  2279. * @arg TIM_SlaveMode_Trigger: The counter starts at a rising edge of the trigger TRGI.
  2280. * @arg TIM_SlaveMode_External1: Rising edges of the selected trigger (TRGI) clock the counter.
  2281. * @retval None
  2282. */
  2283. void TIM_SelectSlaveMode(TIM_TypeDef* TIMx, uint16_t TIM_SlaveMode)
  2284. {
  2285. /* Check the parameters */
  2286. assert_param(IS_TIM_LIST6_PERIPH(TIMx));
  2287. assert_param(IS_TIM_SLAVE_MODE(TIM_SlaveMode));
  2288. /* Reset the SMS Bits */
  2289. TIMx->SMCR &= (uint16_t)~((uint16_t)TIM_SMCR_SMS);
  2290. /* Select the Slave Mode */
  2291. TIMx->SMCR |= TIM_SlaveMode;
  2292. }
  2293. /**
  2294. * @brief Sets or Resets the TIMx Master/Slave Mode.
  2295. * @param TIMx: where x can be 1, 2, 3, or 15 to select the TIM peripheral.
  2296. * @param TIM_MasterSlaveMode: specifies the Timer Master Slave Mode.
  2297. * This parameter can be one of the following values:
  2298. * @arg TIM_MasterSlaveMode_Enable: synchronization between the current timer
  2299. * and its slaves (through TRGO).
  2300. * @arg TIM_MasterSlaveMode_Disable: No action
  2301. * @retval None
  2302. */
  2303. void TIM_SelectMasterSlaveMode(TIM_TypeDef* TIMx, uint16_t TIM_MasterSlaveMode)
  2304. {
  2305. /* Check the parameters */
  2306. assert_param(IS_TIM_LIST6_PERIPH(TIMx));
  2307. assert_param(IS_TIM_MSM_STATE(TIM_MasterSlaveMode));
  2308. /* Reset the MSM Bit */
  2309. TIMx->SMCR &= (uint16_t)~((uint16_t)TIM_SMCR_MSM);
  2310. /* Set or Reset the MSM Bit */
  2311. TIMx->SMCR |= TIM_MasterSlaveMode;
  2312. }
  2313. /**
  2314. * @brief Configures the TIMx External Trigger (ETR).
  2315. * @param TIMx: where x can be 1, 2 or 3 to select the TIM peripheral.
  2316. * @param TIM_ExtTRGPrescaler: The external Trigger Prescaler.
  2317. * This parameter can be one of the following values:
  2318. * @arg TIM_ExtTRGPSC_OFF: ETRP Prescaler OFF.
  2319. * @arg TIM_ExtTRGPSC_DIV2: ETRP frequency divided by 2.
  2320. * @arg TIM_ExtTRGPSC_DIV4: ETRP frequency divided by 4.
  2321. * @arg TIM_ExtTRGPSC_DIV8: ETRP frequency divided by 8.
  2322. * @param TIM_ExtTRGPolarity: The external Trigger Polarity.
  2323. * This parameter can be one of the following values:
  2324. * @arg TIM_ExtTRGPolarity_Inverted: active low or falling edge active.
  2325. * @arg TIM_ExtTRGPolarity_NonInverted: active high or rising edge active.
  2326. * @param ExtTRGFilter: External Trigger Filter.
  2327. * This parameter must be a value between 0x00 and 0x0F
  2328. * @retval None
  2329. */
  2330. void TIM_ETRConfig(TIM_TypeDef* TIMx, uint16_t TIM_ExtTRGPrescaler, uint16_t TIM_ExtTRGPolarity,
  2331. uint16_t ExtTRGFilter)
  2332. {
  2333. uint16_t tmpsmcr = 0;
  2334. /* Check the parameters */
  2335. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  2336. assert_param(IS_TIM_EXT_PRESCALER(TIM_ExtTRGPrescaler));
  2337. assert_param(IS_TIM_EXT_POLARITY(TIM_ExtTRGPolarity));
  2338. assert_param(IS_TIM_EXT_FILTER(ExtTRGFilter));
  2339. tmpsmcr = TIMx->SMCR;
  2340. /* Reset the ETR Bits */
  2341. tmpsmcr &= SMCR_ETR_MASK;
  2342. /* Set the Prescaler, the Filter value and the Polarity */
  2343. tmpsmcr |= (uint16_t)(TIM_ExtTRGPrescaler | (uint16_t)(TIM_ExtTRGPolarity | (uint16_t)(ExtTRGFilter << (uint16_t)8)));
  2344. /* Write to TIMx SMCR */
  2345. TIMx->SMCR = tmpsmcr;
  2346. }
  2347. /**
  2348. * @}
  2349. */
  2350. /**
  2351. * @brief Configures the TIMx Encoder Interface.
  2352. * @param TIMx: where x can be 1, 2 or 3 to select the TIM peripheral.
  2353. * @param TIM_EncoderMode: specifies the TIMx Encoder Mode.
  2354. * This parameter can be one of the following values:
  2355. * @arg TIM_EncoderMode_TI1: Counter counts on TI1FP1 edge depending on TI2FP2 level.
  2356. * @arg TIM_EncoderMode_TI2: Counter counts on TI2FP2 edge depending on TI1FP1 level.
  2357. * @arg TIM_EncoderMode_TI12: Counter counts on both TI1FP1 and TI2FP2 edges depending
  2358. * on the level of the other input.
  2359. * @param TIM_IC1Polarity: specifies the IC1 Polarity
  2360. * This parmeter can be one of the following values:
  2361. * @arg TIM_ICPolarity_Falling: IC Falling edge.
  2362. * @arg TIM_ICPolarity_Rising: IC Rising edge.
  2363. * @param TIM_IC2Polarity: specifies the IC2 Polarity
  2364. * This parmeter can be one of the following values:
  2365. * @arg TIM_ICPolarity_Falling: IC Falling edge.
  2366. * @arg TIM_ICPolarity_Rising: IC Rising edge.
  2367. * @retval None
  2368. */
  2369. void TIM_EncoderInterfaceConfig(TIM_TypeDef* TIMx, uint16_t TIM_EncoderMode,
  2370. uint16_t TIM_IC1Polarity, uint16_t TIM_IC2Polarity)
  2371. {
  2372. uint16_t tmpsmcr = 0;
  2373. uint16_t tmpccmr1 = 0;
  2374. uint16_t tmpccer = 0;
  2375. /* Check the parameters */
  2376. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  2377. assert_param(IS_TIM_ENCODER_MODE(TIM_EncoderMode));
  2378. assert_param(IS_TIM_IC_POLARITY(TIM_IC1Polarity));
  2379. assert_param(IS_TIM_IC_POLARITY(TIM_IC2Polarity));
  2380. /* Get the TIMx SMCR register value */
  2381. tmpsmcr = TIMx->SMCR;
  2382. /* Get the TIMx CCMR1 register value */
  2383. tmpccmr1 = TIMx->CCMR1;
  2384. /* Get the TIMx CCER register value */
  2385. tmpccer = TIMx->CCER;
  2386. /* Set the encoder Mode */
  2387. tmpsmcr &= (uint16_t)(~((uint16_t)TIM_SMCR_SMS));
  2388. tmpsmcr |= TIM_EncoderMode;
  2389. /* Select the Capture Compare 1 and the Capture Compare 2 as input */
  2390. tmpccmr1 &= (uint16_t)(((uint16_t)~((uint16_t)TIM_CCMR1_CC1S)) & (uint16_t)(~((uint16_t)TIM_CCMR1_CC2S)));
  2391. tmpccmr1 |= TIM_CCMR1_CC1S_0 | TIM_CCMR1_CC2S_0;
  2392. /* Set the TI1 and the TI2 Polarities */
  2393. tmpccer &= (uint16_t)~((uint16_t)(TIM_CCER_CC1P | TIM_CCER_CC1NP)) & (uint16_t)~((uint16_t)(TIM_CCER_CC2P | TIM_CCER_CC2NP));
  2394. tmpccer |= (uint16_t)(TIM_IC1Polarity | (uint16_t)(TIM_IC2Polarity << (uint16_t)4));
  2395. /* Write to TIMx SMCR */
  2396. TIMx->SMCR = tmpsmcr;
  2397. /* Write to TIMx CCMR1 */
  2398. TIMx->CCMR1 = tmpccmr1;
  2399. /* Write to TIMx CCER */
  2400. TIMx->CCER = tmpccer;
  2401. }
  2402. /**
  2403. * @brief Enables or disables the TIMx's Hall sensor interface.
  2404. * @param TIMx: where x can be 1, 2 or 3 to select the TIM peripheral.
  2405. * @param NewState: new state of the TIMx Hall sensor interface.
  2406. * This parameter can be: ENABLE or DISABLE.
  2407. * @retval None
  2408. */
  2409. void TIM_SelectHallSensor(TIM_TypeDef* TIMx, FunctionalState NewState)
  2410. {
  2411. /* Check the parameters */
  2412. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  2413. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2414. if (NewState != DISABLE)
  2415. {
  2416. /* Set the TI1S Bit */
  2417. TIMx->CR2 |= TIM_CR2_TI1S;
  2418. }
  2419. else
  2420. {
  2421. /* Reset the TI1S Bit */
  2422. TIMx->CR2 &= (uint16_t)~((uint16_t)TIM_CR2_TI1S);
  2423. }
  2424. }
  2425. /**
  2426. * @}
  2427. */
  2428. /**
  2429. * @brief Configures the TIM14 Remapping input Capabilities.
  2430. * @param TIMx: where x can be 14 to select the TIM peripheral.
  2431. * @param TIM_Remap: specifies the TIM input reampping source.
  2432. * This parameter can be one of the following values:
  2433. * @arg TIM14_GPIO: TIM14 Channel 1 is connected to GPIO.
  2434. * @arg TIM14_RTC_CLK: TIM14 Channel 1 is connected to RTC input clock.
  2435. * RTC input clock can be LSE, LSI or HSE/div128.
  2436. * @arg TIM14_HSE_DIV32: TIM14 Channel 1 is connected to HSE/32 clock.
  2437. * @arg TIM14_MCO: TIM14 Channel 1 is connected to MCO clock.
  2438. * MCO clock can be HSI14, SYSCLK, HSI, HSE or PLL/2.
  2439. * @retval None
  2440. */
  2441. void TIM_RemapConfig(TIM_TypeDef* TIMx, uint16_t TIM_Remap)
  2442. {
  2443. /* Check the parameters */
  2444. assert_param(IS_TIM_LIST11_PERIPH(TIMx));
  2445. assert_param(IS_TIM_REMAP(TIM_Remap));
  2446. /* Set the Timer remapping configuration */
  2447. TIMx->OR = TIM_Remap;
  2448. }
  2449. /**
  2450. * @}
  2451. */
  2452. /**
  2453. * @brief Configure the TI1 as Input.
  2454. * @param TIMx: where x can be 1, 2, 3, 14, 15, 16 or 17 to select the TIM peripheral.
  2455. * @param TIM_ICPolarity: The Input Polarity.
  2456. * This parameter can be one of the following values:
  2457. * @arg TIM_ICPolarity_Rising
  2458. * @arg TIM_ICPolarity_Falling
  2459. * @param TIM_ICSelection: specifies the input to be used.
  2460. * This parameter can be one of the following values:
  2461. * @arg TIM_ICSelection_DirectTI: TIM Input 1 is selected to be connected to IC1.
  2462. * @arg TIM_ICSelection_IndirectTI: TIM Input 1 is selected to be connected to IC2.
  2463. * @arg TIM_ICSelection_TRC: TIM Input 1 is selected to be connected to TRC.
  2464. * @param TIM_ICFilter: Specifies the Input Capture Filter.
  2465. * This parameter must be a value between 0x00 and 0x0F.
  2466. * @retval None
  2467. */
  2468. static void TI1_Config(TIM_TypeDef* TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,
  2469. uint16_t TIM_ICFilter)
  2470. {
  2471. uint16_t tmpccmr1 = 0, tmpccer = 0;
  2472. /* Disable the Channel 1: Reset the CC1E Bit */
  2473. TIMx->CCER &= (uint16_t)~((uint16_t)TIM_CCER_CC1E);
  2474. tmpccmr1 = TIMx->CCMR1;
  2475. tmpccer = TIMx->CCER;
  2476. /* Select the Input and set the filter */
  2477. tmpccmr1 &= (uint16_t)(((uint16_t)~((uint16_t)TIM_CCMR1_CC1S)) & ((uint16_t)~((uint16_t)TIM_CCMR1_IC1F)));
  2478. tmpccmr1 |= (uint16_t)(TIM_ICSelection | (uint16_t)(TIM_ICFilter << (uint16_t)4));
  2479. /* Select the Polarity and set the CC1E Bit */
  2480. tmpccer &= (uint16_t)~((uint16_t)(TIM_CCER_CC1P | TIM_CCER_CC1NP));
  2481. tmpccer |= (uint16_t)(TIM_ICPolarity | (uint16_t)TIM_CCER_CC1E);
  2482. /* Write to TIMx CCMR1 and CCER registers */
  2483. TIMx->CCMR1 = tmpccmr1;
  2484. TIMx->CCER = tmpccer;
  2485. }
  2486. /**
  2487. * @brief Configure the TI2 as Input.
  2488. * @param TIMx: where x can be 1, 2, 3, or 15 to select the TIM peripheral.
  2489. * @param TIM_ICPolarity: The Input Polarity.
  2490. * This parameter can be one of the following values:
  2491. * @arg TIM_ICPolarity_Rising
  2492. * @arg TIM_ICPolarity_Falling
  2493. * @param TIM_ICSelection: specifies the input to be used.
  2494. * This parameter can be one of the following values:
  2495. * @arg TIM_ICSelection_DirectTI: TIM Input 2 is selected to be connected to IC2.
  2496. * @arg TIM_ICSelection_IndirectTI: TIM Input 2 is selected to be connected to IC1.
  2497. * @arg TIM_ICSelection_TRC: TIM Input 2 is selected to be connected to TRC.
  2498. * @param TIM_ICFilter: Specifies the Input Capture Filter.
  2499. * This parameter must be a value between 0x00 and 0x0F.
  2500. * @retval None
  2501. */
  2502. static void TI2_Config(TIM_TypeDef* TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,
  2503. uint16_t TIM_ICFilter)
  2504. {
  2505. uint16_t tmpccmr1 = 0, tmpccer = 0, tmp = 0;
  2506. /* Disable the Channel 2: Reset the CC2E Bit */
  2507. TIMx->CCER &= (uint16_t)~((uint16_t)TIM_CCER_CC2E);
  2508. tmpccmr1 = TIMx->CCMR1;
  2509. tmpccer = TIMx->CCER;
  2510. tmp = (uint16_t)(TIM_ICPolarity << 4);
  2511. /* Select the Input and set the filter */
  2512. tmpccmr1 &= (uint16_t)(((uint16_t)~((uint16_t)TIM_CCMR1_CC2S)) & ((uint16_t)~((uint16_t)TIM_CCMR1_IC2F)));
  2513. tmpccmr1 |= (uint16_t)(TIM_ICFilter << 12);
  2514. tmpccmr1 |= (uint16_t)(TIM_ICSelection << 8);
  2515. /* Select the Polarity and set the CC2E Bit */
  2516. tmpccer &= (uint16_t)~((uint16_t)(TIM_CCER_CC2P | TIM_CCER_CC2NP));
  2517. tmpccer |= (uint16_t)(tmp | (uint16_t)TIM_CCER_CC2E);
  2518. /* Write to TIMx CCMR1 and CCER registers */
  2519. TIMx->CCMR1 = tmpccmr1 ;
  2520. TIMx->CCER = tmpccer;
  2521. }
  2522. /**
  2523. * @brief Configure the TI3 as Input.
  2524. * @param TIMx: where x can be 1, 2 or 3 to select the TIM peripheral.
  2525. * @param TIM_ICPolarity: The Input Polarity.
  2526. * This parameter can be one of the following values:
  2527. * @arg TIM_ICPolarity_Rising
  2528. * @arg TIM_ICPolarity_Falling
  2529. * @param TIM_ICSelection: specifies the input to be used.
  2530. * This parameter can be one of the following values:
  2531. * @arg TIM_ICSelection_DirectTI: TIM Input 3 is selected to be connected to IC3.
  2532. * @arg TIM_ICSelection_IndirectTI: TIM Input 3 is selected to be connected to IC4.
  2533. * @arg TIM_ICSelection_TRC: TIM Input 3 is selected to be connected to TRC.
  2534. * @param TIM_ICFilter: Specifies the Input Capture Filter.
  2535. * This parameter must be a value between 0x00 and 0x0F.
  2536. * @retval None
  2537. */
  2538. static void TI3_Config(TIM_TypeDef* TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,
  2539. uint16_t TIM_ICFilter)
  2540. {
  2541. uint16_t tmpccmr2 = 0, tmpccer = 0, tmp = 0;
  2542. /* Disable the Channel 3: Reset the CC3E Bit */
  2543. TIMx->CCER &= (uint16_t)~((uint16_t)TIM_CCER_CC3E);
  2544. tmpccmr2 = TIMx->CCMR2;
  2545. tmpccer = TIMx->CCER;
  2546. tmp = (uint16_t)(TIM_ICPolarity << 8);
  2547. /* Select the Input and set the filter */
  2548. tmpccmr2 &= (uint16_t)(((uint16_t)~((uint16_t)TIM_CCMR2_CC3S)) & ((uint16_t)~((uint16_t)TIM_CCMR2_IC3F)));
  2549. tmpccmr2 |= (uint16_t)(TIM_ICSelection | (uint16_t)(TIM_ICFilter << (uint16_t)4));
  2550. /* Select the Polarity and set the CC3E Bit */
  2551. tmpccer &= (uint16_t)~((uint16_t)(TIM_CCER_CC3P | TIM_CCER_CC3NP));
  2552. tmpccer |= (uint16_t)(tmp | (uint16_t)TIM_CCER_CC3E);
  2553. /* Write to TIMx CCMR2 and CCER registers */
  2554. TIMx->CCMR2 = tmpccmr2;
  2555. TIMx->CCER = tmpccer;
  2556. }
  2557. /**
  2558. * @brief Configure the TI4 as Input.
  2559. * @param TIMx: where x can be 1, 2 or 3 to select the TIM peripheral.
  2560. * @param TIM_ICPolarity: The Input Polarity.
  2561. * This parameter can be one of the following values:
  2562. * @arg TIM_ICPolarity_Rising
  2563. * @arg TIM_ICPolarity_Falling
  2564. * @param TIM_ICSelection: specifies the input to be used.
  2565. * This parameter can be one of the following values:
  2566. * @arg TIM_ICSelection_DirectTI: TIM Input 4 is selected to be connected to IC4.
  2567. * @arg TIM_ICSelection_IndirectTI: TIM Input 4 is selected to be connected to IC3.
  2568. * @arg TIM_ICSelection_TRC: TIM Input 4 is selected to be connected to TRC.
  2569. * @param TIM_ICFilter: Specifies the Input Capture Filter.
  2570. * This parameter must be a value between 0x00 and 0x0F.
  2571. * @retval None
  2572. */
  2573. static void TI4_Config(TIM_TypeDef* TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,
  2574. uint16_t TIM_ICFilter)
  2575. {
  2576. uint16_t tmpccmr2 = 0, tmpccer = 0, tmp = 0;
  2577. /* Disable the Channel 4: Reset the CC4E Bit */
  2578. TIMx->CCER &= (uint16_t)~((uint16_t)TIM_CCER_CC4E);
  2579. tmpccmr2 = TIMx->CCMR2;
  2580. tmpccer = TIMx->CCER;
  2581. tmp = (uint16_t)(TIM_ICPolarity << 12);
  2582. /* Select the Input and set the filter */
  2583. tmpccmr2 &= (uint16_t)((uint16_t)(~(uint16_t)TIM_CCMR2_CC4S) & ((uint16_t)~((uint16_t)TIM_CCMR2_IC4F)));
  2584. tmpccmr2 |= (uint16_t)(TIM_ICSelection << 8);
  2585. tmpccmr2 |= (uint16_t)(TIM_ICFilter << 12);
  2586. /* Select the Polarity and set the CC4E Bit */
  2587. tmpccer &= (uint16_t)~((uint16_t)(TIM_CCER_CC4P));
  2588. tmpccer |= (uint16_t)(tmp | (uint16_t)TIM_CCER_CC4E);
  2589. /* Write to TIMx CCMR2 and CCER registers */
  2590. TIMx->CCMR2 = tmpccmr2;
  2591. TIMx->CCER = tmpccer;
  2592. }
  2593. /**
  2594. * @}
  2595. */
  2596. /**
  2597. * @}
  2598. */
  2599. /**
  2600. * @}
  2601. */
  2602. /************************ (C) COPYRIGHT FMD *****END OF FILE****/