stm32f10x_tim.c 107 KB

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