ch32v20x_tim.c 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353
  1. /********************************** (C) COPYRIGHT *******************************
  2. * File Name : ch32v20x_tim.c
  3. * Author : WCH
  4. * Version : V1.0.0
  5. * Date : 2021/06/06
  6. * Description : This file provides all the TIM firmware functions.
  7. *********************************************************************************
  8. * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
  9. * Attention: This software (modified or not) and binary are used for
  10. * microcontroller manufactured by Nanjing Qinheng Microelectronics.
  11. *******************************************************************************/
  12. #include "ch32v20x_tim.h"
  13. #include "ch32v20x_rcc.h"
  14. /* TIM registers bit mask */
  15. #define SMCFGR_ETR_Mask ((uint16_t)0x00FF)
  16. #define CHCTLR_Offset ((uint16_t)0x0018)
  17. #define CCER_CCE_Set ((uint16_t)0x0001)
  18. #define CCER_CCNE_Set ((uint16_t)0x0004)
  19. static void TI1_Config(TIM_TypeDef *TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,
  20. uint16_t TIM_ICFilter);
  21. static void TI2_Config(TIM_TypeDef *TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,
  22. uint16_t TIM_ICFilter);
  23. static void TI3_Config(TIM_TypeDef *TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,
  24. uint16_t TIM_ICFilter);
  25. static void TI4_Config(TIM_TypeDef *TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,
  26. uint16_t TIM_ICFilter);
  27. /*********************************************************************
  28. * @fn TIM_DeInit
  29. *
  30. * @brief Deinitializes the TIMx peripheral registers to their default
  31. * reset values.
  32. *
  33. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  34. *
  35. * @return none
  36. */
  37. void TIM_DeInit(TIM_TypeDef *TIMx)
  38. {
  39. if(TIMx == TIM1)
  40. {
  41. RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM1, ENABLE);
  42. RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM1, DISABLE);
  43. }
  44. else if(TIMx == TIM2)
  45. {
  46. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM2, ENABLE);
  47. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM2, DISABLE);
  48. }
  49. else if(TIMx == TIM3)
  50. {
  51. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM3, ENABLE);
  52. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM3, DISABLE);
  53. }
  54. else if(TIMx == TIM4)
  55. {
  56. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM4, ENABLE);
  57. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM4, DISABLE);
  58. }
  59. else if(TIMx == TIM5)
  60. {
  61. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM5, ENABLE);
  62. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM5, DISABLE);
  63. }
  64. }
  65. /*********************************************************************
  66. * @fn TIM_TimeBaseInit
  67. *
  68. * @brief Initializes the TIMx Time Base Unit peripheral according to
  69. * the specified parameters in the TIM_TimeBaseInitStruct.
  70. *
  71. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  72. * TIM_TimeBaseInitStruct - pointer to a TIM_TimeBaseInitTypeDef
  73. * structure.
  74. *
  75. * @return none
  76. */
  77. void TIM_TimeBaseInit(TIM_TypeDef *TIMx, TIM_TimeBaseInitTypeDef *TIM_TimeBaseInitStruct)
  78. {
  79. uint16_t tmpcr1 = 0;
  80. tmpcr1 = TIMx->CTLR1;
  81. if((TIMx == TIM1) || (TIMx == TIM2) || (TIMx == TIM3) || (TIMx == TIM4) || (TIMx == TIM5))
  82. {
  83. tmpcr1 &= (uint16_t)(~((uint16_t)(TIM_DIR | TIM_CMS)));
  84. tmpcr1 |= (uint32_t)TIM_TimeBaseInitStruct->TIM_CounterMode;
  85. }
  86. tmpcr1 &= (uint16_t)(~((uint16_t)TIM_CTLR1_CKD));
  87. tmpcr1 |= (uint32_t)TIM_TimeBaseInitStruct->TIM_ClockDivision;
  88. TIMx->CTLR1 = tmpcr1;
  89. TIMx->ATRLR = TIM_TimeBaseInitStruct->TIM_Period;
  90. TIMx->PSC = TIM_TimeBaseInitStruct->TIM_Prescaler;
  91. if((TIMx == TIM1))
  92. {
  93. TIMx->RPTCR = TIM_TimeBaseInitStruct->TIM_RepetitionCounter;
  94. }
  95. TIMx->SWEVGR = TIM_PSCReloadMode_Immediate;
  96. }
  97. /*********************************************************************
  98. * @fn TIM_OC1Init
  99. *
  100. * @brief Initializes the TIMx Channel1 according to the specified
  101. * parameters in the TIM_OCInitStruct.
  102. *
  103. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  104. * TIM_OCInitStruct - pointer to a TIM_OCInitTypeDef structure.
  105. *
  106. * @return none
  107. */
  108. void TIM_OC1Init(TIM_TypeDef *TIMx, TIM_OCInitTypeDef *TIM_OCInitStruct)
  109. {
  110. uint16_t tmpccmrx = 0, tmpccer = 0, tmpcr2 = 0;
  111. TIMx->CCER &= (uint16_t)(~(uint16_t)TIM_CC1E);
  112. tmpccer = TIMx->CCER;
  113. tmpcr2 = TIMx->CTLR2;
  114. tmpccmrx = TIMx->CHCTLR1;
  115. tmpccmrx &= (uint16_t)(~((uint16_t)TIM_OC1M));
  116. tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CC1S));
  117. tmpccmrx |= TIM_OCInitStruct->TIM_OCMode;
  118. tmpccer &= (uint16_t)(~((uint16_t)TIM_CC1P));
  119. tmpccer |= TIM_OCInitStruct->TIM_OCPolarity;
  120. tmpccer |= TIM_OCInitStruct->TIM_OutputState;
  121. if((TIMx == TIM1))
  122. {
  123. tmpccer &= (uint16_t)(~((uint16_t)TIM_CC1NP));
  124. tmpccer |= TIM_OCInitStruct->TIM_OCNPolarity;
  125. tmpccer &= (uint16_t)(~((uint16_t)TIM_CC1NE));
  126. tmpccer |= TIM_OCInitStruct->TIM_OutputNState;
  127. tmpcr2 &= (uint16_t)(~((uint16_t)TIM_OIS1));
  128. tmpcr2 &= (uint16_t)(~((uint16_t)TIM_OIS1N));
  129. tmpcr2 |= TIM_OCInitStruct->TIM_OCIdleState;
  130. tmpcr2 |= TIM_OCInitStruct->TIM_OCNIdleState;
  131. }
  132. TIMx->CTLR2 = tmpcr2;
  133. TIMx->CHCTLR1 = tmpccmrx;
  134. TIMx->CH1CVR = TIM_OCInitStruct->TIM_Pulse;
  135. TIMx->CCER = tmpccer;
  136. }
  137. /*********************************************************************
  138. * @fn TIM_OC2Init
  139. *
  140. * @brief Initializes the TIMx Channel2 according to the specified
  141. * parameters in the TIM_OCInitStruct.
  142. *
  143. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  144. * TIM_OCInitStruct - pointer to a TIM_OCInitTypeDef structure.
  145. *
  146. * @return none
  147. */
  148. void TIM_OC2Init(TIM_TypeDef *TIMx, TIM_OCInitTypeDef *TIM_OCInitStruct)
  149. {
  150. uint16_t tmpccmrx = 0, tmpccer = 0, tmpcr2 = 0;
  151. TIMx->CCER &= (uint16_t)(~((uint16_t)TIM_CC2E));
  152. tmpccer = TIMx->CCER;
  153. tmpcr2 = TIMx->CTLR2;
  154. tmpccmrx = TIMx->CHCTLR1;
  155. tmpccmrx &= (uint16_t)(~((uint16_t)TIM_OC2M));
  156. tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CC2S));
  157. tmpccmrx |= (uint16_t)(TIM_OCInitStruct->TIM_OCMode << 8);
  158. tmpccer &= (uint16_t)(~((uint16_t)TIM_CC2P));
  159. tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OCPolarity << 4);
  160. tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OutputState << 4);
  161. if((TIMx == TIM1))
  162. {
  163. tmpccer &= (uint16_t)(~((uint16_t)TIM_CC2NP));
  164. tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OCNPolarity << 4);
  165. tmpccer &= (uint16_t)(~((uint16_t)TIM_CC2NE));
  166. tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OutputNState << 4);
  167. tmpcr2 &= (uint16_t)(~((uint16_t)TIM_OIS2));
  168. tmpcr2 &= (uint16_t)(~((uint16_t)TIM_OIS2N));
  169. tmpcr2 |= (uint16_t)(TIM_OCInitStruct->TIM_OCIdleState << 2);
  170. tmpcr2 |= (uint16_t)(TIM_OCInitStruct->TIM_OCNIdleState << 2);
  171. }
  172. TIMx->CTLR2 = tmpcr2;
  173. TIMx->CHCTLR1 = tmpccmrx;
  174. TIMx->CH2CVR = TIM_OCInitStruct->TIM_Pulse;
  175. TIMx->CCER = tmpccer;
  176. }
  177. /*********************************************************************
  178. * @fn TIM_OC3Init
  179. *
  180. * @brief Initializes the TIMx Channel3 according to the specified
  181. * parameters in the TIM_OCInitStruct.
  182. *
  183. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  184. * TIM_OCInitStruct - pointer to a TIM_OCInitTypeDef structure.
  185. *
  186. * @return none
  187. */
  188. void TIM_OC3Init(TIM_TypeDef *TIMx, TIM_OCInitTypeDef *TIM_OCInitStruct)
  189. {
  190. uint16_t tmpccmrx = 0, tmpccer = 0, tmpcr2 = 0;
  191. TIMx->CCER &= (uint16_t)(~((uint16_t)TIM_CC3E));
  192. tmpccer = TIMx->CCER;
  193. tmpcr2 = TIMx->CTLR2;
  194. tmpccmrx = TIMx->CHCTLR2;
  195. tmpccmrx &= (uint16_t)(~((uint16_t)TIM_OC3M));
  196. tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CC3S));
  197. tmpccmrx |= TIM_OCInitStruct->TIM_OCMode;
  198. tmpccer &= (uint16_t)(~((uint16_t)TIM_CC3P));
  199. tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OCPolarity << 8);
  200. tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OutputState << 8);
  201. if((TIMx == TIM1))
  202. {
  203. tmpccer &= (uint16_t)(~((uint16_t)TIM_CC3NP));
  204. tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OCNPolarity << 8);
  205. tmpccer &= (uint16_t)(~((uint16_t)TIM_CC3NE));
  206. tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OutputNState << 8);
  207. tmpcr2 &= (uint16_t)(~((uint16_t)TIM_OIS3));
  208. tmpcr2 &= (uint16_t)(~((uint16_t)TIM_OIS3N));
  209. tmpcr2 |= (uint16_t)(TIM_OCInitStruct->TIM_OCIdleState << 4);
  210. tmpcr2 |= (uint16_t)(TIM_OCInitStruct->TIM_OCNIdleState << 4);
  211. }
  212. TIMx->CTLR2 = tmpcr2;
  213. TIMx->CHCTLR2 = tmpccmrx;
  214. TIMx->CH3CVR = TIM_OCInitStruct->TIM_Pulse;
  215. TIMx->CCER = tmpccer;
  216. }
  217. /*********************************************************************
  218. * @fn TIM_OC4Init
  219. *
  220. * @brief Initializes the TIMx Channel4 according to the specified
  221. * parameters in the TIM_OCInitStruct.
  222. *
  223. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  224. * TIM_OCInitStruct - pointer to a TIM_OCInitTypeDef structure.
  225. *
  226. * @return none
  227. */
  228. void TIM_OC4Init(TIM_TypeDef *TIMx, TIM_OCInitTypeDef *TIM_OCInitStruct)
  229. {
  230. uint16_t tmpccmrx = 0, tmpccer = 0, tmpcr2 = 0;
  231. TIMx->CCER &= (uint16_t)(~((uint16_t)TIM_CC4E));
  232. tmpccer = TIMx->CCER;
  233. tmpcr2 = TIMx->CTLR2;
  234. tmpccmrx = TIMx->CHCTLR2;
  235. tmpccmrx &= (uint16_t)(~((uint16_t)TIM_OC4M));
  236. tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CC4S));
  237. tmpccmrx |= (uint16_t)(TIM_OCInitStruct->TIM_OCMode << 8);
  238. tmpccer &= (uint16_t)(~((uint16_t)TIM_CC4P));
  239. tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OCPolarity << 12);
  240. tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OutputState << 12);
  241. if((TIMx == TIM1))
  242. {
  243. tmpcr2 &= (uint16_t)(~((uint16_t)TIM_OIS4));
  244. tmpcr2 |= (uint16_t)(TIM_OCInitStruct->TIM_OCIdleState << 6);
  245. }
  246. TIMx->CTLR2 = tmpcr2;
  247. TIMx->CHCTLR2 = tmpccmrx;
  248. TIMx->CH4CVR = TIM_OCInitStruct->TIM_Pulse;
  249. TIMx->CCER = tmpccer;
  250. }
  251. /*********************************************************************
  252. * @fn TIM_ICInit
  253. *
  254. * @brief IInitializes the TIM peripheral according to the specified
  255. * parameters in the TIM_ICInitStruct.
  256. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  257. * TIM_ICInitStruct - pointer to a TIM_ICInitTypeDef structure.
  258. *
  259. * @return none
  260. */
  261. void TIM_ICInit(TIM_TypeDef *TIMx, TIM_ICInitTypeDef *TIM_ICInitStruct)
  262. {
  263. if(TIM_ICInitStruct->TIM_Channel == TIM_Channel_1)
  264. {
  265. TI1_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity,
  266. TIM_ICInitStruct->TIM_ICSelection,
  267. TIM_ICInitStruct->TIM_ICFilter);
  268. TIM_SetIC1Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
  269. }
  270. else if(TIM_ICInitStruct->TIM_Channel == TIM_Channel_2)
  271. {
  272. TI2_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity,
  273. TIM_ICInitStruct->TIM_ICSelection,
  274. TIM_ICInitStruct->TIM_ICFilter);
  275. TIM_SetIC2Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
  276. }
  277. else if(TIM_ICInitStruct->TIM_Channel == TIM_Channel_3)
  278. {
  279. TI3_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity,
  280. TIM_ICInitStruct->TIM_ICSelection,
  281. TIM_ICInitStruct->TIM_ICFilter);
  282. TIM_SetIC3Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
  283. }
  284. else
  285. {
  286. TI4_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity,
  287. TIM_ICInitStruct->TIM_ICSelection,
  288. TIM_ICInitStruct->TIM_ICFilter);
  289. TIM_SetIC4Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
  290. }
  291. }
  292. /*********************************************************************
  293. * @fn TIM_PWMIConfig
  294. *
  295. * @brief Configures the TIM peripheral according to the specified
  296. * parameters in the TIM_ICInitStruct to measure an external
  297. * PWM signal.
  298. *
  299. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  300. * TIM_ICInitStruct - pointer to a TIM_ICInitTypeDef structure.
  301. *
  302. * @return none
  303. */
  304. void TIM_PWMIConfig(TIM_TypeDef *TIMx, TIM_ICInitTypeDef *TIM_ICInitStruct)
  305. {
  306. uint16_t icoppositepolarity = TIM_ICPolarity_Rising;
  307. uint16_t icoppositeselection = TIM_ICSelection_DirectTI;
  308. if(TIM_ICInitStruct->TIM_ICPolarity == TIM_ICPolarity_Rising)
  309. {
  310. icoppositepolarity = TIM_ICPolarity_Falling;
  311. }
  312. else
  313. {
  314. icoppositepolarity = TIM_ICPolarity_Rising;
  315. }
  316. if(TIM_ICInitStruct->TIM_ICSelection == TIM_ICSelection_DirectTI)
  317. {
  318. icoppositeselection = TIM_ICSelection_IndirectTI;
  319. }
  320. else
  321. {
  322. icoppositeselection = TIM_ICSelection_DirectTI;
  323. }
  324. if(TIM_ICInitStruct->TIM_Channel == TIM_Channel_1)
  325. {
  326. TI1_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity, TIM_ICInitStruct->TIM_ICSelection,
  327. TIM_ICInitStruct->TIM_ICFilter);
  328. TIM_SetIC1Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
  329. TI2_Config(TIMx, icoppositepolarity, icoppositeselection, TIM_ICInitStruct->TIM_ICFilter);
  330. TIM_SetIC2Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
  331. }
  332. else
  333. {
  334. TI2_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity, TIM_ICInitStruct->TIM_ICSelection,
  335. TIM_ICInitStruct->TIM_ICFilter);
  336. TIM_SetIC2Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
  337. TI1_Config(TIMx, icoppositepolarity, icoppositeselection, TIM_ICInitStruct->TIM_ICFilter);
  338. TIM_SetIC1Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
  339. }
  340. }
  341. /*********************************************************************
  342. * @fn TIM_BDTRConfig
  343. *
  344. * @brief Configures the: Break feature, dead time, Lock level, the OSSI,
  345. * the OSSR State and the AOE(automatic output enable).
  346. *
  347. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  348. * TIM_BDTRInitStruct - pointer to a TIM_BDTRInitTypeDef structure.
  349. *
  350. * @return none
  351. */
  352. void TIM_BDTRConfig(TIM_TypeDef *TIMx, TIM_BDTRInitTypeDef *TIM_BDTRInitStruct)
  353. {
  354. TIMx->BDTR = (uint32_t)TIM_BDTRInitStruct->TIM_OSSRState | TIM_BDTRInitStruct->TIM_OSSIState |
  355. TIM_BDTRInitStruct->TIM_LOCKLevel | TIM_BDTRInitStruct->TIM_DeadTime |
  356. TIM_BDTRInitStruct->TIM_Break | TIM_BDTRInitStruct->TIM_BreakPolarity |
  357. TIM_BDTRInitStruct->TIM_AutomaticOutput;
  358. }
  359. /*********************************************************************
  360. * @fn TIM_TimeBaseStructInit
  361. *
  362. * @brief Fills each TIM_TimeBaseInitStruct member with its default value.
  363. *
  364. * @param TIM_TimeBaseInitStruct - pointer to a TIM_TimeBaseInitTypeDef structure.
  365. *
  366. * @return none
  367. */
  368. void TIM_TimeBaseStructInit(TIM_TimeBaseInitTypeDef *TIM_TimeBaseInitStruct)
  369. {
  370. TIM_TimeBaseInitStruct->TIM_Period = 0xFFFF;
  371. TIM_TimeBaseInitStruct->TIM_Prescaler = 0x0000;
  372. TIM_TimeBaseInitStruct->TIM_ClockDivision = TIM_CKD_DIV1;
  373. TIM_TimeBaseInitStruct->TIM_CounterMode = TIM_CounterMode_Up;
  374. TIM_TimeBaseInitStruct->TIM_RepetitionCounter = 0x0000;
  375. }
  376. /*********************************************************************
  377. * @fn TIM_OCStructInit
  378. *
  379. * @brief Fills each TIM_OCInitStruct member with its default value.
  380. *
  381. * @param TIM_OCInitStruct - pointer to a TIM_OCInitTypeDef structure.
  382. *
  383. * @return none
  384. */
  385. void TIM_OCStructInit(TIM_OCInitTypeDef *TIM_OCInitStruct)
  386. {
  387. TIM_OCInitStruct->TIM_OCMode = TIM_OCMode_Timing;
  388. TIM_OCInitStruct->TIM_OutputState = TIM_OutputState_Disable;
  389. TIM_OCInitStruct->TIM_OutputNState = TIM_OutputNState_Disable;
  390. TIM_OCInitStruct->TIM_Pulse = 0x0000;
  391. TIM_OCInitStruct->TIM_OCPolarity = TIM_OCPolarity_High;
  392. TIM_OCInitStruct->TIM_OCNPolarity = TIM_OCPolarity_High;
  393. TIM_OCInitStruct->TIM_OCIdleState = TIM_OCIdleState_Reset;
  394. TIM_OCInitStruct->TIM_OCNIdleState = TIM_OCNIdleState_Reset;
  395. }
  396. /*********************************************************************
  397. * @fn TIM_ICStructInit
  398. *
  399. * @brief Fills each TIM_ICInitStruct member with its default value.
  400. *
  401. * @param TIM_ICInitStruct - pointer to a TIM_ICInitTypeDef structure.
  402. *
  403. * @return none
  404. */
  405. void TIM_ICStructInit(TIM_ICInitTypeDef *TIM_ICInitStruct)
  406. {
  407. TIM_ICInitStruct->TIM_Channel = TIM_Channel_1;
  408. TIM_ICInitStruct->TIM_ICPolarity = TIM_ICPolarity_Rising;
  409. TIM_ICInitStruct->TIM_ICSelection = TIM_ICSelection_DirectTI;
  410. TIM_ICInitStruct->TIM_ICPrescaler = TIM_ICPSC_DIV1;
  411. TIM_ICInitStruct->TIM_ICFilter = 0x00;
  412. }
  413. /*********************************************************************
  414. * @fn TIM_BDTRStructInit
  415. *
  416. * @brief Fills each TIM_BDTRInitStruct member with its default value.
  417. *
  418. * @param TIM_BDTRInitStruct - pointer to a TIM_BDTRInitTypeDef structure.
  419. *
  420. * @return none
  421. */
  422. void TIM_BDTRStructInit(TIM_BDTRInitTypeDef *TIM_BDTRInitStruct)
  423. {
  424. TIM_BDTRInitStruct->TIM_OSSRState = TIM_OSSRState_Disable;
  425. TIM_BDTRInitStruct->TIM_OSSIState = TIM_OSSIState_Disable;
  426. TIM_BDTRInitStruct->TIM_LOCKLevel = TIM_LOCKLevel_OFF;
  427. TIM_BDTRInitStruct->TIM_DeadTime = 0x00;
  428. TIM_BDTRInitStruct->TIM_Break = TIM_Break_Disable;
  429. TIM_BDTRInitStruct->TIM_BreakPolarity = TIM_BreakPolarity_Low;
  430. TIM_BDTRInitStruct->TIM_AutomaticOutput = TIM_AutomaticOutput_Disable;
  431. }
  432. /*********************************************************************
  433. * @fn TIM_Cmd
  434. *
  435. * @brief Enables or disables the specified TIM peripheral.
  436. *
  437. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  438. * NewState - ENABLE or DISABLE.
  439. *
  440. * @return none
  441. */
  442. void TIM_Cmd(TIM_TypeDef *TIMx, FunctionalState NewState)
  443. {
  444. if(NewState != DISABLE)
  445. {
  446. TIMx->CTLR1 |= TIM_CEN;
  447. }
  448. else
  449. {
  450. TIMx->CTLR1 &= (uint16_t)(~((uint16_t)TIM_CEN));
  451. }
  452. }
  453. /*********************************************************************
  454. * @fn TIM_CtrlPWMOutputs
  455. *
  456. * @brief Enables or disables the TIM peripheral Main Outputs.
  457. *
  458. * @param TIMx - where x can be 1/8/9/10 to select the TIM peripheral.
  459. * NewState - ENABLE or DISABLE.
  460. *
  461. * @return none
  462. */
  463. void TIM_CtrlPWMOutputs(TIM_TypeDef *TIMx, FunctionalState NewState)
  464. {
  465. if(NewState != DISABLE)
  466. {
  467. TIMx->BDTR |= TIM_MOE;
  468. }
  469. else
  470. {
  471. TIMx->BDTR &= (uint16_t)(~((uint16_t)TIM_MOE));
  472. }
  473. }
  474. /*********************************************************************
  475. * @fn TIM_ITConfig
  476. *
  477. * @brief Enables or disables the specified TIM interrupts.
  478. *
  479. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  480. * TIM_IT - specifies the TIM interrupts sources to be enabled or disabled.
  481. * TIM_IT_Update - TIM update Interrupt source.
  482. * TIM_IT_CC1 - TIM Capture Compare 1 Interrupt source.
  483. * TIM_IT_CC2 - TIM Capture Compare 2 Interrupt source
  484. * TIM_IT_CC3 - TIM Capture Compare 3 Interrupt source.
  485. * TIM_IT_CC4 - TIM Capture Compare 4 Interrupt source.
  486. * TIM_IT_COM - TIM Commutation Interrupt source.
  487. * TIM_IT_Trigger - TIM Trigger Interrupt source.
  488. * TIM_IT_Break - TIM Break Interrupt source.
  489. * NewState - ENABLE or DISABLE.
  490. *
  491. * @return none
  492. */
  493. void TIM_ITConfig(TIM_TypeDef *TIMx, uint16_t TIM_IT, FunctionalState NewState)
  494. {
  495. if(NewState != DISABLE)
  496. {
  497. TIMx->DMAINTENR |= TIM_IT;
  498. }
  499. else
  500. {
  501. TIMx->DMAINTENR &= (uint16_t)~TIM_IT;
  502. }
  503. }
  504. /*******************************************************************************
  505. * @fn TIM_GenerateEvent
  506. *
  507. * @brief Configures the TIMx event to be generate by software.
  508. *
  509. * @param TIMx: where x can be 1 to 4 to select the TIM peripheral.
  510. * TIM_EventSource: specifies the event source.
  511. * TIM_EventSource_Update: Timer update Event source.
  512. * TIM_EventSource_CC1: Timer Capture Compare 1 Event source.
  513. * TIM_EventSource_CC2: Timer Capture Compare 2 Event source.
  514. * TIM_EventSource_CC3: Timer Capture Compare 3 Event source.
  515. * TIM_EventSource_CC4: Timer Capture Compare 4 Event source.
  516. * TIM_EventSource_COM: Timer COM event source.
  517. * TIM_EventSource_Trigger: Timer Trigger Event source.
  518. * TIM_EventSource_Break: Timer Break event source.
  519. *
  520. * @return None
  521. */
  522. void TIM_GenerateEvent(TIM_TypeDef *TIMx, uint16_t TIM_EventSource)
  523. {
  524. TIMx->SWEVGR = TIM_EventSource;
  525. }
  526. /*********************************************************************
  527. * @fn TIM_DMAConfig
  528. *
  529. * @brief Configures the TIMx's DMA interface.
  530. *
  531. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  532. * TIM_DMABase: DMA Base address.
  533. * TIM_DMABase_CR.
  534. * TIM_DMABase_CR2.
  535. * TIM_DMABase_SMCR.
  536. * TIM_DMABase_DIER.
  537. * TIM1_DMABase_SR.
  538. * TIM_DMABase_EGR.
  539. * TIM_DMABase_CCMR1.
  540. * TIM_DMABase_CCMR2.
  541. * TIM_DMABase_CCER.
  542. * TIM_DMABase_CNT.
  543. * TIM_DMABase_PSC.
  544. * TIM_DMABase_CCR1.
  545. * TIM_DMABase_CCR2.
  546. * TIM_DMABase_CCR3.
  547. * TIM_DMABase_CCR4.
  548. * TIM_DMABase_BDTR.
  549. * TIM_DMABase_DCR.
  550. * TIM_DMABurstLength - DMA Burst length.
  551. * TIM_DMABurstLength_1Transfer.
  552. * TIM_DMABurstLength_18Transfers.
  553. *
  554. * @return none
  555. */
  556. void TIM_DMAConfig(TIM_TypeDef *TIMx, uint16_t TIM_DMABase, uint16_t TIM_DMABurstLength)
  557. {
  558. TIMx->DMACFGR = TIM_DMABase | TIM_DMABurstLength;
  559. }
  560. /*********************************************************************
  561. * @fn TIM_DMACmd
  562. *
  563. * @brief Enables or disables the TIMx's DMA Requests.
  564. *
  565. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  566. * TIM_DMASource - specifies the DMA Request sources.
  567. * TIM_DMA_Update - TIM update Interrupt source.
  568. * TIM_DMA_CC1 - TIM Capture Compare 1 DMA source.
  569. * TIM_DMA_CC2 - TIM Capture Compare 2 DMA source.
  570. * TIM_DMA_CC3 - TIM Capture Compare 3 DMA source.
  571. * TIM_DMA_CC4 - TIM Capture Compare 4 DMA source.
  572. * TIM_DMA_COM - TIM Commutation DMA source.
  573. * TIM_DMA_Trigger - TIM Trigger DMA source.
  574. * NewState - ENABLE or DISABLE.
  575. *
  576. * @return none
  577. */
  578. void TIM_DMACmd(TIM_TypeDef *TIMx, uint16_t TIM_DMASource, FunctionalState NewState)
  579. {
  580. if(NewState != DISABLE)
  581. {
  582. TIMx->DMAINTENR |= TIM_DMASource;
  583. }
  584. else
  585. {
  586. TIMx->DMAINTENR &= (uint16_t)~TIM_DMASource;
  587. }
  588. }
  589. /*********************************************************************
  590. * @fn TIM_InternalClockConfig
  591. *
  592. * @brief Configures the TIMx internal Clock.
  593. *
  594. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  595. *
  596. * @return none
  597. */
  598. void TIM_InternalClockConfig(TIM_TypeDef *TIMx)
  599. {
  600. TIMx->SMCFGR &= (uint16_t)(~((uint16_t)TIM_SMS));
  601. }
  602. /*********************************************************************
  603. * @fn TIM_ITRxExternalClockConfig
  604. *
  605. * @brief Configures the TIMx Internal Trigger as External Clock.
  606. *
  607. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  608. * TIM_InputTriggerSource: Trigger source.
  609. * TIM_TS_ITR0 - Internal Trigger 0.
  610. * TIM_TS_ITR1 - Internal Trigger 1.
  611. * TIM_TS_ITR2 - Internal Trigger 2.
  612. * TIM_TS_ITR3 - Internal Trigger 3.
  613. *
  614. * @return none
  615. */
  616. void TIM_ITRxExternalClockConfig(TIM_TypeDef *TIMx, uint16_t TIM_InputTriggerSource)
  617. {
  618. TIM_SelectInputTrigger(TIMx, TIM_InputTriggerSource);
  619. TIMx->SMCFGR |= TIM_SlaveMode_External1;
  620. }
  621. /*********************************************************************
  622. * @fn TIM_TIxExternalClockConfig
  623. *
  624. * @brief Configures the TIMx Trigger as External Clock.
  625. *
  626. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  627. * TIM_TIxExternalCLKSource - Trigger source.
  628. * TIM_TIxExternalCLK1Source_TI1ED - TI1 Edge Detector.
  629. * TIM_TIxExternalCLK1Source_TI1 - Filtered Timer Input 1.
  630. * TIM_TIxExternalCLK1Source_TI2 - Filtered Timer Input 2.
  631. * TIM_ICPolarity - specifies the TIx Polarity.
  632. * TIM_ICPolarity_Rising.
  633. * TIM_ICPolarity_Falling.
  634. * TIM_DMA_COM - TIM Commutation DMA source.
  635. * TIM_DMA_Trigger - TIM Trigger DMA source.
  636. * ICFilter - specifies the filter value.
  637. * This parameter must be a value between 0x0 and 0xF.
  638. *
  639. * @return none
  640. */
  641. void TIM_TIxExternalClockConfig(TIM_TypeDef *TIMx, uint16_t TIM_TIxExternalCLKSource,
  642. uint16_t TIM_ICPolarity, uint16_t ICFilter)
  643. {
  644. if(TIM_TIxExternalCLKSource == TIM_TIxExternalCLK1Source_TI2)
  645. {
  646. TI2_Config(TIMx, TIM_ICPolarity, TIM_ICSelection_DirectTI, ICFilter);
  647. }
  648. else
  649. {
  650. TI1_Config(TIMx, TIM_ICPolarity, TIM_ICSelection_DirectTI, ICFilter);
  651. }
  652. TIM_SelectInputTrigger(TIMx, TIM_TIxExternalCLKSource);
  653. TIMx->SMCFGR |= TIM_SlaveMode_External1;
  654. }
  655. /*********************************************************************
  656. * @fn TIM_ETRClockMode1Config
  657. *
  658. * @brief Configures the External clock Mode1.
  659. *
  660. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  661. * TIM_ExtTRGPrescaler - The external Trigger Prescaler.
  662. * TIM_ExtTRGPSC_OFF - ETRP Prescaler OFF.
  663. * TIM_ExtTRGPSC_DIV2 - ETRP frequency divided by 2.
  664. * TIM_ExtTRGPSC_DIV4 - ETRP frequency divided by 4.
  665. * TIM_ExtTRGPSC_DIV8 - ETRP frequency divided by 8.
  666. * TIM_ExtTRGPolarity - The external Trigger Polarity.
  667. * TIM_ExtTRGPolarity_Inverted - active low or falling edge active.
  668. * TIM_ExtTRGPolarity_NonInverted - active high or rising edge active.
  669. * ExtTRGFilter - External Trigger Filter.
  670. * This parameter must be a value between 0x0 and 0xF.
  671. *
  672. * @return none
  673. */
  674. void TIM_ETRClockMode1Config(TIM_TypeDef *TIMx, uint16_t TIM_ExtTRGPrescaler, uint16_t TIM_ExtTRGPolarity,
  675. uint16_t ExtTRGFilter)
  676. {
  677. uint16_t tmpsmcr = 0;
  678. TIM_ETRConfig(TIMx, TIM_ExtTRGPrescaler, TIM_ExtTRGPolarity, ExtTRGFilter);
  679. tmpsmcr = TIMx->SMCFGR;
  680. tmpsmcr &= (uint16_t)(~((uint16_t)TIM_SMS));
  681. tmpsmcr |= TIM_SlaveMode_External1;
  682. tmpsmcr &= (uint16_t)(~((uint16_t)TIM_TS));
  683. tmpsmcr |= TIM_TS_ETRF;
  684. TIMx->SMCFGR = tmpsmcr;
  685. }
  686. /*********************************************************************
  687. * @fn TIM_ETRClockMode2Config
  688. *
  689. * @brief Configures the External clock Mode2.
  690. *
  691. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  692. * TIM_ExtTRGPrescaler - The external Trigger Prescaler.
  693. * TIM_ExtTRGPSC_OFF - ETRP Prescaler OFF.
  694. * TIM_ExtTRGPSC_DIV2 - ETRP frequency divided by 2.
  695. * TIM_ExtTRGPSC_DIV4 - ETRP frequency divided by 4.
  696. * TIM_ExtTRGPSC_DIV8 - ETRP frequency divided by 8.
  697. * TIM_ExtTRGPolarity - The external Trigger Polarity.
  698. * TIM_ExtTRGPolarity_Inverted - active low or falling edge active.
  699. * TIM_ExtTRGPolarity_NonInverted - active high or rising edge active.
  700. * ExtTRGFilter - External Trigger Filter.
  701. * This parameter must be a value between 0x0 and 0xF.
  702. *
  703. * @return none
  704. */
  705. void TIM_ETRClockMode2Config(TIM_TypeDef *TIMx, uint16_t TIM_ExtTRGPrescaler,
  706. uint16_t TIM_ExtTRGPolarity, uint16_t ExtTRGFilter)
  707. {
  708. TIM_ETRConfig(TIMx, TIM_ExtTRGPrescaler, TIM_ExtTRGPolarity, ExtTRGFilter);
  709. TIMx->SMCFGR |= TIM_ECE;
  710. }
  711. /*********************************************************************
  712. * @fn TIM_ETRConfig
  713. *
  714. * @brief Configures the TIMx External Trigger (ETR).
  715. *
  716. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  717. * TIM_ExtTRGPrescaler - The external Trigger Prescaler.
  718. * TIM_ExtTRGPSC_OFF - ETRP Prescaler OFF.
  719. * TIM_ExtTRGPSC_DIV2 - ETRP frequency divided by 2.
  720. * TIM_ExtTRGPSC_DIV4 - ETRP frequency divided by 4.
  721. * TIM_ExtTRGPSC_DIV8 - ETRP frequency divided by 8.
  722. * TIM_ExtTRGPolarity - The external Trigger Polarity.
  723. * TIM_ExtTRGPolarity_Inverted - active low or falling edge active.
  724. * TIM_ExtTRGPolarity_NonInverted - active high or rising edge active.
  725. * ExtTRGFilter - External Trigger Filter.
  726. * This parameter must be a value between 0x0 and 0xF.
  727. *
  728. * @return none
  729. */
  730. void TIM_ETRConfig(TIM_TypeDef *TIMx, uint16_t TIM_ExtTRGPrescaler, uint16_t TIM_ExtTRGPolarity,
  731. uint16_t ExtTRGFilter)
  732. {
  733. uint16_t tmpsmcr = 0;
  734. tmpsmcr = TIMx->SMCFGR;
  735. tmpsmcr &= SMCFGR_ETR_Mask;
  736. tmpsmcr |= (uint16_t)(TIM_ExtTRGPrescaler | (uint16_t)(TIM_ExtTRGPolarity | (uint16_t)(ExtTRGFilter << (uint16_t)8)));
  737. TIMx->SMCFGR = tmpsmcr;
  738. }
  739. /*********************************************************************
  740. * @fn TIM_PrescalerConfig
  741. *
  742. * @brief Configures the TIMx Prescaler.
  743. *
  744. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  745. * Prescaler - specifies the Prescaler Register value.
  746. * TIM_PSCReloadMode - specifies the TIM Prescaler Reload mode.
  747. * TIM_PSCReloadMode - specifies the TIM Prescaler Reload mode.
  748. * TIM_PSCReloadMode_Update - The Prescaler is loaded at the update event.
  749. * TIM_PSCReloadMode_Immediate - The Prescaler is loaded immediately.
  750. *
  751. * @return none
  752. */
  753. void TIM_PrescalerConfig(TIM_TypeDef *TIMx, uint16_t Prescaler, uint16_t TIM_PSCReloadMode)
  754. {
  755. TIMx->PSC = Prescaler;
  756. TIMx->SWEVGR = TIM_PSCReloadMode;
  757. }
  758. /*********************************************************************
  759. * @fn TIM_CounterModeConfig
  760. *
  761. * @brief Specifies the TIMx Counter Mode to be used.
  762. *
  763. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  764. * TIM_CounterMode - specifies the Counter Mode to be used.
  765. * TIM_CounterMode_Up - TIM Up Counting Mode.
  766. * TIM_CounterMode_Down - TIM Down Counting Mode.
  767. * TIM_CounterMode_CenterAligned1 - TIM Center Aligned Mode1.
  768. * TIM_CounterMode_CenterAligned2 - TIM Center Aligned Mode2.
  769. * TIM_CounterMode_CenterAligned3 - TIM Center Aligned Mode3.
  770. *
  771. * @return none
  772. */
  773. void TIM_CounterModeConfig(TIM_TypeDef *TIMx, uint16_t TIM_CounterMode)
  774. {
  775. uint16_t tmpcr1 = 0;
  776. tmpcr1 = TIMx->CTLR1;
  777. tmpcr1 &= (uint16_t)(~((uint16_t)(TIM_DIR | TIM_CMS)));
  778. tmpcr1 |= TIM_CounterMode;
  779. TIMx->CTLR1 = tmpcr1;
  780. }
  781. /*********************************************************************
  782. * @fn TIM_SelectInputTrigger
  783. *
  784. * @brief Selects the Input Trigger source.
  785. *
  786. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  787. * TIM_InputTriggerSource - The Input Trigger source.
  788. * TIM_TS_ITR0 - Internal Trigger 0.
  789. * TIM_TS_ITR1 - Internal Trigger 1.
  790. * TIM_TS_ITR2 - Internal Trigger 2.
  791. * TIM_TS_ITR3 - Internal Trigger 3.
  792. * TIM_TS_TI1F_ED - TI1 Edge Detector.
  793. * TIM_TS_TI1FP1 - Filtered Timer Input 1.
  794. * TIM_TS_TI2FP2 - Filtered Timer Input 2.
  795. * TIM_TS_ETRF - External Trigger input.
  796. *
  797. * @return none
  798. */
  799. void TIM_SelectInputTrigger(TIM_TypeDef *TIMx, uint16_t TIM_InputTriggerSource)
  800. {
  801. uint16_t tmpsmcr = 0;
  802. tmpsmcr = TIMx->SMCFGR;
  803. tmpsmcr &= (uint16_t)(~((uint16_t)TIM_TS));
  804. tmpsmcr |= TIM_InputTriggerSource;
  805. TIMx->SMCFGR = tmpsmcr;
  806. }
  807. /*********************************************************************
  808. * @fn TIM_EncoderInterfaceConfig
  809. *
  810. * @brief Configures the TIMx Encoder Interface.
  811. *
  812. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  813. * TIM_EncoderMode - specifies the TIMx Encoder Mode.
  814. * TIM_EncoderMode_TI1 - Counter counts on TI1FP1 edge depending
  815. * on TI2FP2 level.
  816. * TIM_EncoderMode_TI2 - Counter counts on TI2FP2 edge depending
  817. * on TI1FP1 level.
  818. * TIM_EncoderMode_TI12 - Counter counts on both TI1FP1 and
  819. * TI2FP2 edges depending.
  820. * TIM_IC1Polarity - specifies the IC1 Polarity.
  821. * TIM_ICPolarity_Falling - IC Falling edge.
  822. * TTIM_ICPolarity_Rising - IC Rising edge.
  823. * TIM_IC2Polarity - specifies the IC2 Polarity.
  824. * TIM_ICPolarity_Falling - IC Falling edge.
  825. * TIM_ICPolarity_Rising - IC Rising edge.
  826. *
  827. * @return none
  828. */
  829. void TIM_EncoderInterfaceConfig(TIM_TypeDef *TIMx, uint16_t TIM_EncoderMode,
  830. uint16_t TIM_IC1Polarity, uint16_t TIM_IC2Polarity)
  831. {
  832. uint16_t tmpsmcr = 0;
  833. uint16_t tmpccmr1 = 0;
  834. uint16_t tmpccer = 0;
  835. tmpsmcr = TIMx->SMCFGR;
  836. tmpccmr1 = TIMx->CHCTLR1;
  837. tmpccer = TIMx->CCER;
  838. tmpsmcr &= (uint16_t)(~((uint16_t)TIM_SMS));
  839. tmpsmcr |= TIM_EncoderMode;
  840. tmpccmr1 &= (uint16_t)(((uint16_t) ~((uint16_t)TIM_CC1S)) & (uint16_t)(~((uint16_t)TIM_CC2S)));
  841. tmpccmr1 |= TIM_CC1S_0 | TIM_CC2S_0;
  842. tmpccer &= (uint16_t)(((uint16_t) ~((uint16_t)TIM_CC1P)) & ((uint16_t) ~((uint16_t)TIM_CC2P)));
  843. tmpccer |= (uint16_t)(TIM_IC1Polarity | (uint16_t)(TIM_IC2Polarity << (uint16_t)4));
  844. TIMx->SMCFGR = tmpsmcr;
  845. TIMx->CHCTLR1 = tmpccmr1;
  846. TIMx->CCER = tmpccer;
  847. }
  848. /*********************************************************************
  849. * @fn TIM_ForcedOC1Config
  850. *
  851. * @brief Forces the TIMx output 1 waveform to active or inactive level.
  852. *
  853. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  854. * TIM_ForcedAction - specifies the forced Action to be set to the
  855. * output waveform.
  856. * TIM_ForcedAction_Active - Force active level on OC1REF.
  857. * TIM_ForcedAction_InActive - Force inactive level on OC1REF.
  858. *
  859. * @return none
  860. */
  861. void TIM_ForcedOC1Config(TIM_TypeDef *TIMx, uint16_t TIM_ForcedAction)
  862. {
  863. uint16_t tmpccmr1 = 0;
  864. tmpccmr1 = TIMx->CHCTLR1;
  865. tmpccmr1 &= (uint16_t) ~((uint16_t)TIM_OC1M);
  866. tmpccmr1 |= TIM_ForcedAction;
  867. TIMx->CHCTLR1 = tmpccmr1;
  868. }
  869. /*********************************************************************
  870. * @fn TIM_ForcedOC2Config
  871. *
  872. * @brief Forces the TIMx output 2 waveform to active or inactive level.
  873. *
  874. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  875. * TIM_ForcedAction - specifies the forced Action to be set to the
  876. * output waveform.
  877. * TIM_ForcedAction_Active - Force active level on OC2REF.
  878. * TIM_ForcedAction_InActive - Force inactive level on OC2REF.
  879. *
  880. * @return none
  881. */
  882. void TIM_ForcedOC2Config(TIM_TypeDef *TIMx, uint16_t TIM_ForcedAction)
  883. {
  884. uint16_t tmpccmr1 = 0;
  885. tmpccmr1 = TIMx->CHCTLR1;
  886. tmpccmr1 &= (uint16_t) ~((uint16_t)TIM_OC2M);
  887. tmpccmr1 |= (uint16_t)(TIM_ForcedAction << 8);
  888. TIMx->CHCTLR1 = tmpccmr1;
  889. }
  890. /*********************************************************************
  891. * @fn TIM_ForcedOC3Config
  892. *
  893. * @brief Forces the TIMx output 3 waveform to active or inactive level.
  894. *
  895. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  896. * TIM_ForcedAction - specifies the forced Action to be set to the
  897. * output waveform.
  898. * TIM_ForcedAction_Active - Force active level on OC3REF.
  899. * TIM_ForcedAction_InActive - Force inactive level on OC3REF.
  900. *
  901. * @return none
  902. */
  903. void TIM_ForcedOC3Config(TIM_TypeDef *TIMx, uint16_t TIM_ForcedAction)
  904. {
  905. uint16_t tmpccmr2 = 0;
  906. tmpccmr2 = TIMx->CHCTLR2;
  907. tmpccmr2 &= (uint16_t) ~((uint16_t)TIM_OC3M);
  908. tmpccmr2 |= TIM_ForcedAction;
  909. TIMx->CHCTLR2 = tmpccmr2;
  910. }
  911. /*********************************************************************
  912. * @fn TIM_ForcedOC4Config
  913. *
  914. * @brief Forces the TIMx output 4 waveform to active or inactive level.
  915. *
  916. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  917. * TIM_ForcedAction - specifies the forced Action to be set to the
  918. * output waveform.
  919. * TIM_ForcedAction_Active - Force active level on OC4REF.
  920. * TIM_ForcedAction_InActive - Force inactive level on OC4REF.
  921. *
  922. * @return none
  923. */
  924. void TIM_ForcedOC4Config(TIM_TypeDef *TIMx, uint16_t TIM_ForcedAction)
  925. {
  926. uint16_t tmpccmr2 = 0;
  927. tmpccmr2 = TIMx->CHCTLR2;
  928. tmpccmr2 &= (uint16_t) ~((uint16_t)TIM_OC4M);
  929. tmpccmr2 |= (uint16_t)(TIM_ForcedAction << 8);
  930. TIMx->CHCTLR2 = tmpccmr2;
  931. }
  932. /*********************************************************************
  933. * @fn TIM_ARRPreloadConfig
  934. *
  935. * @brief Enables or disables TIMx peripheral Preload register on ARR.
  936. *
  937. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  938. * NewState - ENABLE or DISABLE.
  939. *
  940. * @return none
  941. */
  942. void TIM_ARRPreloadConfig(TIM_TypeDef *TIMx, FunctionalState NewState)
  943. {
  944. if(NewState != DISABLE)
  945. {
  946. TIMx->CTLR1 |= TIM_ARPE;
  947. }
  948. else
  949. {
  950. TIMx->CTLR1 &= (uint16_t) ~((uint16_t)TIM_ARPE);
  951. }
  952. }
  953. /*********************************************************************
  954. * @fn TIM_SelectCOM
  955. *
  956. * @brief Selects the TIM peripheral Commutation event.
  957. *
  958. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  959. * NewState - ENABLE or DISABLE.
  960. *
  961. * @return none
  962. */
  963. void TIM_SelectCOM(TIM_TypeDef *TIMx, FunctionalState NewState)
  964. {
  965. if(NewState != DISABLE)
  966. {
  967. TIMx->CTLR2 |= TIM_CCUS;
  968. }
  969. else
  970. {
  971. TIMx->CTLR2 &= (uint16_t) ~((uint16_t)TIM_CCUS);
  972. }
  973. }
  974. /*********************************************************************
  975. * @fn TIM_SelectCCDMA
  976. *
  977. * @brief Selects the TIMx peripheral Capture Compare DMA source.
  978. *
  979. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  980. * NewState - ENABLE or DISABLE.
  981. *
  982. * @return none
  983. */
  984. void TIM_SelectCCDMA(TIM_TypeDef *TIMx, FunctionalState NewState)
  985. {
  986. if(NewState != DISABLE)
  987. {
  988. TIMx->CTLR2 |= TIM_CCDS;
  989. }
  990. else
  991. {
  992. TIMx->CTLR2 &= (uint16_t) ~((uint16_t)TIM_CCDS);
  993. }
  994. }
  995. /*********************************************************************
  996. * @fn TIM_CCPreloadControl
  997. *
  998. * @brief DSets or Resets the TIM peripheral Capture Compare Preload Control bit.
  999. * reset values (Affects also the I2Ss).
  1000. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  1001. * NewState - ENABLE or DISABLE.
  1002. *
  1003. * @return none
  1004. */
  1005. void TIM_CCPreloadControl(TIM_TypeDef *TIMx, FunctionalState NewState)
  1006. {
  1007. if(NewState != DISABLE)
  1008. {
  1009. TIMx->CTLR2 |= TIM_CCPC;
  1010. }
  1011. else
  1012. {
  1013. TIMx->CTLR2 &= (uint16_t) ~((uint16_t)TIM_CCPC);
  1014. }
  1015. }
  1016. /*********************************************************************
  1017. * @fn TIM_OC1PreloadConfig
  1018. *
  1019. * @brief Enables or disables the TIMx peripheral Preload register on CCR1.
  1020. *
  1021. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  1022. * TIM_OCPreload - new state of the TIMx peripheral Preload register.
  1023. * TIM_OCPreload_Enable.
  1024. * TIM_OCPreload_Disable.
  1025. *
  1026. * @return none
  1027. */
  1028. void TIM_OC1PreloadConfig(TIM_TypeDef *TIMx, uint16_t TIM_OCPreload)
  1029. {
  1030. uint16_t tmpccmr1 = 0;
  1031. tmpccmr1 = TIMx->CHCTLR1;
  1032. tmpccmr1 &= (uint16_t) ~((uint16_t)TIM_OC1PE);
  1033. tmpccmr1 |= TIM_OCPreload;
  1034. TIMx->CHCTLR1 = tmpccmr1;
  1035. }
  1036. /*********************************************************************
  1037. * @fn TIM_OC2PreloadConfig
  1038. *
  1039. * @brief Enables or disables the TIMx peripheral Preload register on CCR2.
  1040. *
  1041. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  1042. * TIM_OCPreload - new state of the TIMx peripheral Preload register.
  1043. * TIM_OCPreload_Enable.
  1044. * TIM_OCPreload_Disable.
  1045. *
  1046. * @return none
  1047. */
  1048. void TIM_OC2PreloadConfig(TIM_TypeDef *TIMx, uint16_t TIM_OCPreload)
  1049. {
  1050. uint16_t tmpccmr1 = 0;
  1051. tmpccmr1 = TIMx->CHCTLR1;
  1052. tmpccmr1 &= (uint16_t) ~((uint16_t)TIM_OC2PE);
  1053. tmpccmr1 |= (uint16_t)(TIM_OCPreload << 8);
  1054. TIMx->CHCTLR1 = tmpccmr1;
  1055. }
  1056. /*********************************************************************
  1057. * @fn TIM_OC3PreloadConfig
  1058. *
  1059. * @brief Enables or disables the TIMx peripheral Preload register on CCR3.
  1060. *
  1061. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  1062. * TIM_OCPreload - new state of the TIMx peripheral Preload register.
  1063. * TIM_OCPreload_Enable.
  1064. * TIM_OCPreload_Disable.
  1065. *
  1066. * @return none
  1067. */
  1068. void TIM_OC3PreloadConfig(TIM_TypeDef *TIMx, uint16_t TIM_OCPreload)
  1069. {
  1070. uint16_t tmpccmr2 = 0;
  1071. tmpccmr2 = TIMx->CHCTLR2;
  1072. tmpccmr2 &= (uint16_t) ~((uint16_t)TIM_OC3PE);
  1073. tmpccmr2 |= TIM_OCPreload;
  1074. TIMx->CHCTLR2 = tmpccmr2;
  1075. }
  1076. /*********************************************************************
  1077. * @fn TIM_OC4PreloadConfig
  1078. *
  1079. * @brief Enables or disables the TIMx peripheral Preload register on CCR4.
  1080. *
  1081. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  1082. * TIM_OCPreload - new state of the TIMx peripheral Preload register.
  1083. * TIM_OCPreload_Enable.
  1084. * TIM_OCPreload_Disable.
  1085. *
  1086. * @return none
  1087. */
  1088. void TIM_OC4PreloadConfig(TIM_TypeDef *TIMx, uint16_t TIM_OCPreload)
  1089. {
  1090. uint16_t tmpccmr2 = 0;
  1091. tmpccmr2 = TIMx->CHCTLR2;
  1092. tmpccmr2 &= (uint16_t) ~((uint16_t)TIM_OC4PE);
  1093. tmpccmr2 |= (uint16_t)(TIM_OCPreload << 8);
  1094. TIMx->CHCTLR2 = tmpccmr2;
  1095. }
  1096. /*********************************************************************
  1097. * @fn TIM_OC1FastConfig
  1098. *
  1099. * @brief Configures the TIMx Output Compare 1 Fast feature.
  1100. *
  1101. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  1102. * TIM_OCFast - new state of the Output Compare Fast Enable Bit.
  1103. * TIM_OCFast_Enable - TIM output compare fast enable.
  1104. * TIM_OCFast_Disable - TIM output compare fast disable.
  1105. *
  1106. * @return none
  1107. */
  1108. void TIM_OC1FastConfig(TIM_TypeDef *TIMx, uint16_t TIM_OCFast)
  1109. {
  1110. uint16_t tmpccmr1 = 0;
  1111. tmpccmr1 = TIMx->CHCTLR1;
  1112. tmpccmr1 &= (uint16_t) ~((uint16_t)TIM_OC1FE);
  1113. tmpccmr1 |= TIM_OCFast;
  1114. TIMx->CHCTLR1 = tmpccmr1;
  1115. }
  1116. /*********************************************************************
  1117. * @fn TIM_OC2FastConfig
  1118. *
  1119. * @brief Configures the TIMx Output Compare 2 Fast feature.
  1120. *
  1121. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  1122. * TIM_OCFast - new state of the Output Compare Fast Enable Bit.
  1123. * TIM_OCFast_Enable - TIM output compare fast enable.
  1124. * TIM_OCFast_Disable - TIM output compare fast disable.
  1125. *
  1126. * @return none
  1127. */
  1128. void TIM_OC2FastConfig(TIM_TypeDef *TIMx, uint16_t TIM_OCFast)
  1129. {
  1130. uint16_t tmpccmr1 = 0;
  1131. tmpccmr1 = TIMx->CHCTLR1;
  1132. tmpccmr1 &= (uint16_t) ~((uint16_t)TIM_OC2FE);
  1133. tmpccmr1 |= (uint16_t)(TIM_OCFast << 8);
  1134. TIMx->CHCTLR1 = tmpccmr1;
  1135. }
  1136. /*********************************************************************
  1137. * @fn TIM_OC3FastConfig
  1138. *
  1139. * @brief Configures the TIMx Output Compare 3 Fast feature.
  1140. *
  1141. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  1142. * TIM_OCFast - new state of the Output Compare Fast Enable Bit.
  1143. * TIM_OCFast_Enable - TIM output compare fast enable.
  1144. * TIM_OCFast_Disable - TIM output compare fast disable.
  1145. *
  1146. * @return none
  1147. */
  1148. void TIM_OC3FastConfig(TIM_TypeDef *TIMx, uint16_t TIM_OCFast)
  1149. {
  1150. uint16_t tmpccmr2 = 0;
  1151. tmpccmr2 = TIMx->CHCTLR2;
  1152. tmpccmr2 &= (uint16_t) ~((uint16_t)TIM_OC3FE);
  1153. tmpccmr2 |= TIM_OCFast;
  1154. TIMx->CHCTLR2 = tmpccmr2;
  1155. }
  1156. /*********************************************************************
  1157. * @fn TIM_OC4FastConfig
  1158. *
  1159. * @brief Configures the TIMx Output Compare 4 Fast feature.
  1160. *
  1161. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  1162. * TIM_OCFast - new state of the Output Compare Fast Enable Bit.
  1163. * TIM_OCFast_Enable - TIM output compare fast enable.
  1164. * TIM_OCFast_Disable - TIM output compare fast disable.
  1165. *
  1166. * @return none
  1167. */
  1168. void TIM_OC4FastConfig(TIM_TypeDef *TIMx, uint16_t TIM_OCFast)
  1169. {
  1170. uint16_t tmpccmr2 = 0;
  1171. tmpccmr2 = TIMx->CHCTLR2;
  1172. tmpccmr2 &= (uint16_t) ~((uint16_t)TIM_OC4FE);
  1173. tmpccmr2 |= (uint16_t)(TIM_OCFast << 8);
  1174. TIMx->CHCTLR2 = tmpccmr2;
  1175. }
  1176. /*********************************************************************
  1177. * @fn TIM_ClearOC1Ref
  1178. *
  1179. * @brief Clears or safeguards the OCREF1 signal on an external event.
  1180. *
  1181. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  1182. * TIM_OCClear - new state of the Output Compare Clear Enable Bit.
  1183. * TIM_OCClear_Enable - TIM Output clear enable.
  1184. * TIM_OCClear_Disable - TIM Output clear disable.
  1185. *
  1186. * @return none
  1187. */
  1188. void TIM_ClearOC1Ref(TIM_TypeDef *TIMx, uint16_t TIM_OCClear)
  1189. {
  1190. uint16_t tmpccmr1 = 0;
  1191. tmpccmr1 = TIMx->CHCTLR1;
  1192. tmpccmr1 &= (uint16_t) ~((uint16_t)TIM_OC1CE);
  1193. tmpccmr1 |= TIM_OCClear;
  1194. TIMx->CHCTLR1 = tmpccmr1;
  1195. }
  1196. /*********************************************************************
  1197. * @fn TIM_ClearOC2Ref
  1198. *
  1199. * @brief Clears or safeguards the OCREF2 signal on an external event.
  1200. *
  1201. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  1202. * TIM_OCClear - new state of the Output Compare Clear Enable Bit.
  1203. * TIM_OCClear_Enable - TIM Output clear enable.
  1204. * TIM_OCClear_Disable - TIM Output clear disable.
  1205. *
  1206. * @return none
  1207. */
  1208. void TIM_ClearOC2Ref(TIM_TypeDef *TIMx, uint16_t TIM_OCClear)
  1209. {
  1210. uint16_t tmpccmr1 = 0;
  1211. tmpccmr1 = TIMx->CHCTLR1;
  1212. tmpccmr1 &= (uint16_t) ~((uint16_t)TIM_OC2CE);
  1213. tmpccmr1 |= (uint16_t)(TIM_OCClear << 8);
  1214. TIMx->CHCTLR1 = tmpccmr1;
  1215. }
  1216. /*********************************************************************
  1217. * @fn TIM_ClearOC3Ref
  1218. *
  1219. * @brief Clears or safeguards the OCREF3 signal on an external event.
  1220. *
  1221. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  1222. * TIM_OCClear - new state of the Output Compare Clear Enable Bit.
  1223. * TIM_OCClear_Enable - TIM Output clear enable.
  1224. * TIM_OCClear_Disable - TIM Output clear disable.
  1225. *
  1226. * @return none
  1227. */
  1228. void TIM_ClearOC3Ref(TIM_TypeDef *TIMx, uint16_t TIM_OCClear)
  1229. {
  1230. uint16_t tmpccmr2 = 0;
  1231. tmpccmr2 = TIMx->CHCTLR2;
  1232. tmpccmr2 &= (uint16_t) ~((uint16_t)TIM_OC3CE);
  1233. tmpccmr2 |= TIM_OCClear;
  1234. TIMx->CHCTLR2 = tmpccmr2;
  1235. }
  1236. /*********************************************************************
  1237. * @fn TIM_ClearOC4Ref
  1238. *
  1239. * @brief Clears or safeguards the OCREF4 signal on an external event.
  1240. *
  1241. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  1242. * TIM_OCClear - new state of the Output Compare Clear Enable Bit.
  1243. * TIM_OCClear_Enable - TIM Output clear enable.
  1244. * TIM_OCClear_Disable - TIM Output clear disable.
  1245. *
  1246. * @return none
  1247. */
  1248. void TIM_ClearOC4Ref(TIM_TypeDef *TIMx, uint16_t TIM_OCClear)
  1249. {
  1250. uint16_t tmpccmr2 = 0;
  1251. tmpccmr2 = TIMx->CHCTLR2;
  1252. tmpccmr2 &= (uint16_t) ~((uint16_t)TIM_OC4CE);
  1253. tmpccmr2 |= (uint16_t)(TIM_OCClear << 8);
  1254. TIMx->CHCTLR2 = tmpccmr2;
  1255. }
  1256. /*********************************************************************
  1257. * @fn TIM_OC1PolarityConfig
  1258. *
  1259. * @brief Configures the TIMx channel 1 polarity.
  1260. *
  1261. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  1262. * TIM_OCPolarity - specifies the OC1 Polarity.
  1263. * TIM_OCPolarity_High - Output Compare active high.
  1264. * TIM_OCPolarity_Low - Output Compare active low.
  1265. *
  1266. * @return none
  1267. */
  1268. void TIM_OC1PolarityConfig(TIM_TypeDef *TIMx, uint16_t TIM_OCPolarity)
  1269. {
  1270. uint16_t tmpccer = 0;
  1271. tmpccer = TIMx->CCER;
  1272. tmpccer &= (uint16_t) ~((uint16_t)TIM_CC1P);
  1273. tmpccer |= TIM_OCPolarity;
  1274. TIMx->CCER = tmpccer;
  1275. }
  1276. /*********************************************************************
  1277. * @fn TIM_OC1NPolarityConfig
  1278. *
  1279. * @brief Configures the TIMx channel 1 polarity.
  1280. *
  1281. * @param TIMx - where x can be 1 to select the TIM peripheral.
  1282. * TIM_OCNPolarity - specifies the OC1N Polarity.
  1283. * TIM_OCNPolarity_High - Output Compare active high.
  1284. * TIM_OCNPolarity_Low - Output Compare active low.
  1285. *
  1286. * @return none
  1287. */
  1288. void TIM_OC1NPolarityConfig(TIM_TypeDef *TIMx, uint16_t TIM_OCNPolarity)
  1289. {
  1290. uint16_t tmpccer = 0;
  1291. tmpccer = TIMx->CCER;
  1292. tmpccer &= (uint16_t) ~((uint16_t)TIM_CC1NP);
  1293. tmpccer |= TIM_OCNPolarity;
  1294. TIMx->CCER = tmpccer;
  1295. }
  1296. /*********************************************************************
  1297. * @fn TIM_OC2PolarityConfig
  1298. *
  1299. * @brief Configures the TIMx channel 2 polarity.
  1300. *
  1301. * @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
  1302. * TIM_OCPolarity - specifies the OC2 Polarity.
  1303. * TIM_OCPolarity_High - Output Compare active high.
  1304. * TIM_OCPolarity_Low - Output Compare active low.
  1305. *
  1306. * @return none
  1307. */
  1308. void TIM_OC2PolarityConfig(TIM_TypeDef *TIMx, uint16_t TIM_OCPolarity)
  1309. {
  1310. uint16_t tmpccer = 0;
  1311. tmpccer = TIMx->CCER;
  1312. tmpccer &= (uint16_t) ~((uint16_t)TIM_CC2P);
  1313. tmpccer |= (uint16_t)(TIM_OCPolarity << 4);
  1314. TIMx->CCER = tmpccer;
  1315. }
  1316. /*********************************************************************
  1317. * @fn TIM_OC2NPolarityConfig
  1318. *
  1319. * @brief Configures the TIMx channel 2 polarity.
  1320. *
  1321. * @param TIMx - where x can be 1 to select the TIM peripheral.
  1322. * TIM_OCNPolarity - specifies the OC1N Polarity.
  1323. * TIM_OCNPolarity_High - Output Compare active high.
  1324. * TIM_OCNPolarity_Low - Output Compare active low.
  1325. *
  1326. * @return none
  1327. */
  1328. void TIM_OC2NPolarityConfig(TIM_TypeDef *TIMx, uint16_t TIM_OCNPolarity)
  1329. {
  1330. uint16_t tmpccer = 0;
  1331. tmpccer = TIMx->CCER;
  1332. tmpccer &= (uint16_t) ~((uint16_t)TIM_CC2NP);
  1333. tmpccer |= (uint16_t)(TIM_OCNPolarity << 4);
  1334. TIMx->CCER = tmpccer;
  1335. }
  1336. /*********************************************************************
  1337. * @fn TIM_OC3PolarityConfig
  1338. *
  1339. * @brief Configures the TIMx Channel 3 polarity.
  1340. *
  1341. * @param TIMx - where x can be 1 to 4 select the TIM peripheral.
  1342. * TIM_OCPolarit - specifies the OC3 Polarity.
  1343. * TIM_OCPolarity_High - Output Compare active high.
  1344. * TIM_OCPolarity_Low - Output Compare active low.
  1345. *
  1346. * @return none
  1347. */
  1348. void TIM_OC3PolarityConfig(TIM_TypeDef *TIMx, uint16_t TIM_OCPolarity)
  1349. {
  1350. uint16_t tmpccer = 0;
  1351. tmpccer = TIMx->CCER;
  1352. tmpccer &= (uint16_t) ~((uint16_t)TIM_CC3P);
  1353. tmpccer |= (uint16_t)(TIM_OCPolarity << 8);
  1354. TIMx->CCER = tmpccer;
  1355. }
  1356. /*********************************************************************
  1357. * @fn TIM_OC3NPolarityConfig
  1358. *
  1359. * @brief Configures the TIMx Channel 3N polarity.
  1360. *
  1361. * @param TIMx - where x can be 1 to select the TIM peripheral.
  1362. * TIM_OCNPolarity - specifies the OC2N Polarity.
  1363. * TIM_OCNPolarity_High - Output Compare active high.
  1364. * TIM_OCNPolarity_Low - Output Compare active low.
  1365. *
  1366. * @return none
  1367. */
  1368. void TIM_OC3NPolarityConfig(TIM_TypeDef *TIMx, uint16_t TIM_OCNPolarity)
  1369. {
  1370. uint16_t tmpccer = 0;
  1371. tmpccer = TIMx->CCER;
  1372. tmpccer &= (uint16_t) ~((uint16_t)TIM_CC3NP);
  1373. tmpccer |= (uint16_t)(TIM_OCNPolarity << 8);
  1374. TIMx->CCER = tmpccer;
  1375. }
  1376. /*********************************************************************
  1377. * @fn TIM_OC4PolarityConfig
  1378. *
  1379. * @brief Configures the TIMx Channel 4 polarity.
  1380. *
  1381. * @param TIMx - where x can be 1 to 4 select the TIM peripheral.
  1382. * TIM_OCPolarit - specifies the OC3 Polarity.
  1383. * TIM_OCPolarity_High - Output Compare active high.
  1384. * TIM_OCPolarity_Low - Output Compare active low.
  1385. *
  1386. * @return none
  1387. */
  1388. void TIM_OC4PolarityConfig(TIM_TypeDef *TIMx, uint16_t TIM_OCPolarity)
  1389. {
  1390. uint16_t tmpccer = 0;
  1391. tmpccer = TIMx->CCER;
  1392. tmpccer &= (uint16_t) ~((uint16_t)TIM_CC4P);
  1393. tmpccer |= (uint16_t)(TIM_OCPolarity << 12);
  1394. TIMx->CCER = tmpccer;
  1395. }
  1396. /*********************************************************************
  1397. * @fn TIM_CCxCmd
  1398. *
  1399. * @brief Enables or disables the TIM Capture Compare Channel x.
  1400. *
  1401. * @param TIMx - where x can be 1 to 4 select the TIM peripheral.
  1402. * TIM_Channel - specifies the TIM Channel.
  1403. * TIM_Channel_1 - TIM Channel 1.
  1404. * TIM_Channel_2 - TIM Channel 2.
  1405. * TIM_Channel_3 - TIM Channel 3.
  1406. * TIM_Channel_4 - TIM Channel 4.
  1407. * TIM_CCx - specifies the TIM Channel CCxE bit new state.
  1408. * TIM_CCx_Enable.
  1409. * TIM_CCx_Disable.
  1410. *
  1411. * @return none
  1412. */
  1413. void TIM_CCxCmd(TIM_TypeDef *TIMx, uint16_t TIM_Channel, uint16_t TIM_CCx)
  1414. {
  1415. uint16_t tmp = 0;
  1416. tmp = CCER_CCE_Set << TIM_Channel;
  1417. TIMx->CCER &= (uint16_t)~tmp;
  1418. TIMx->CCER |= (uint16_t)(TIM_CCx << TIM_Channel);
  1419. }
  1420. /*********************************************************************
  1421. * @fn TIM_CCxNCmd
  1422. *
  1423. * @brief Enables or disables the TIM Capture Compare Channel xN.
  1424. *
  1425. * @param TIMx - where x can be 1 select the TIM peripheral.
  1426. * TIM_Channel - specifies the TIM Channel.
  1427. * TIM_Channel_1 - TIM Channel 1.
  1428. * TIM_Channel_2 - TIM Channel 2.
  1429. * TIM_Channel_3 - TIM Channel 3.
  1430. * TIM_CCxN - specifies the TIM Channel CCxNE bit new state.
  1431. * TIM_CCxN_Enable.
  1432. * TIM_CCxN_Disable.
  1433. *
  1434. * @return none
  1435. */
  1436. void TIM_CCxNCmd(TIM_TypeDef *TIMx, uint16_t TIM_Channel, uint16_t TIM_CCxN)
  1437. {
  1438. uint16_t tmp = 0;
  1439. tmp = CCER_CCNE_Set << TIM_Channel;
  1440. TIMx->CCER &= (uint16_t)~tmp;
  1441. TIMx->CCER |= (uint16_t)(TIM_CCxN << TIM_Channel);
  1442. }
  1443. /*********************************************************************
  1444. * @fn TIM_SelectOCxM
  1445. *
  1446. * @brief Selects the TIM Output Compare Mode.
  1447. *
  1448. * @param TIMx - where x can be 1 to 4 select the TIM peripheral.
  1449. * TIM_Channel - specifies the TIM Channel.
  1450. * TIM_Channel_1 - TIM Channel 1.
  1451. * TIM_Channel_2 - TIM Channel 2.
  1452. * TIM_Channel_3 - TIM Channel 3.
  1453. * TIM_Channel_4 - TIM Channel 4.
  1454. * TIM_OCMode - specifies the TIM Output Compare Mode.
  1455. * TIM_OCMode_Timing.
  1456. * TIM_OCMode_Active.
  1457. * TIM_OCMode_Toggle.
  1458. * TIM_OCMode_PWM1.
  1459. * TIM_OCMode_PWM2.
  1460. * TIM_ForcedAction_Active.
  1461. * TIM_ForcedAction_InActive.
  1462. *
  1463. * @return none
  1464. */
  1465. void TIM_SelectOCxM(TIM_TypeDef *TIMx, uint16_t TIM_Channel, uint16_t TIM_OCMode)
  1466. {
  1467. uint32_t tmp = 0;
  1468. uint16_t tmp1 = 0;
  1469. tmp = (uint32_t)TIMx;
  1470. tmp += CHCTLR_Offset;
  1471. tmp1 = CCER_CCE_Set << (uint16_t)TIM_Channel;
  1472. TIMx->CCER &= (uint16_t)~tmp1;
  1473. if((TIM_Channel == TIM_Channel_1) || (TIM_Channel == TIM_Channel_3))
  1474. {
  1475. tmp += (TIM_Channel >> 1);
  1476. *(__IO uint32_t *)tmp &= (uint32_t) ~((uint32_t)TIM_OC1M);
  1477. *(__IO uint32_t *)tmp |= TIM_OCMode;
  1478. }
  1479. else
  1480. {
  1481. tmp += (uint16_t)(TIM_Channel - (uint16_t)4) >> (uint16_t)1;
  1482. *(__IO uint32_t *)tmp &= (uint32_t) ~((uint32_t)TIM_OC2M);
  1483. *(__IO uint32_t *)tmp |= (uint16_t)(TIM_OCMode << 8);
  1484. }
  1485. }
  1486. /*********************************************************************
  1487. * @fn TIM_UpdateDisableConfig
  1488. *
  1489. * @brief Enables or Disables the TIMx Update event.
  1490. *
  1491. * @param TIMx - where x can be 1 to 4 select the TIM peripheral.
  1492. * NewState - ENABLE or DISABLE.
  1493. *
  1494. * @return none
  1495. */
  1496. void TIM_UpdateDisableConfig(TIM_TypeDef *TIMx, FunctionalState NewState)
  1497. {
  1498. if(NewState != DISABLE)
  1499. {
  1500. TIMx->CTLR1 |= TIM_UDIS;
  1501. }
  1502. else
  1503. {
  1504. TIMx->CTLR1 &= (uint16_t) ~((uint16_t)TIM_UDIS);
  1505. }
  1506. }
  1507. /*********************************************************************
  1508. * @fn TIM_UpdateRequestConfig
  1509. *
  1510. * @brief Configures the TIMx Update Request Interrupt source.
  1511. *
  1512. * @param TIMx - where x can be 1 to 4 select the TIM peripheral.
  1513. * TIM_UpdateSource - specifies the Update source.
  1514. * TIM_UpdateSource_Regular.
  1515. * TIM_UpdateSource_Global.
  1516. *
  1517. * @return none
  1518. */
  1519. void TIM_UpdateRequestConfig(TIM_TypeDef *TIMx, uint16_t TIM_UpdateSource)
  1520. {
  1521. if(TIM_UpdateSource != TIM_UpdateSource_Global)
  1522. {
  1523. TIMx->CTLR1 |= TIM_URS;
  1524. }
  1525. else
  1526. {
  1527. TIMx->CTLR1 &= (uint16_t) ~((uint16_t)TIM_URS);
  1528. }
  1529. }
  1530. /*********************************************************************
  1531. * @fn TIM_SelectHallSensor
  1532. *
  1533. * @brief Enables or disables the TIMx's Hall sensor interface.
  1534. *
  1535. * @param TIMx - where x can be 1 to 4 select the TIM peripheral.
  1536. * NewState - ENABLE or DISABLE.
  1537. *
  1538. * @return none
  1539. */
  1540. void TIM_SelectHallSensor(TIM_TypeDef *TIMx, FunctionalState NewState)
  1541. {
  1542. if(NewState != DISABLE)
  1543. {
  1544. TIMx->CTLR2 |= TIM_TI1S;
  1545. }
  1546. else
  1547. {
  1548. TIMx->CTLR2 &= (uint16_t) ~((uint16_t)TIM_TI1S);
  1549. }
  1550. }
  1551. /*********************************************************************
  1552. * @fn TIM_SelectOnePulseMode
  1553. *
  1554. * @brief Selects the TIMx's One Pulse Mode.
  1555. *
  1556. * @param TIMx - where x can be 1 to 4 select the TIM peripheral.
  1557. * TIM_OPMode - specifies the OPM Mode to be used.
  1558. * TIM_OPMode_Single.
  1559. * TIM_OPMode_Repetitive.
  1560. *
  1561. * @return none
  1562. */
  1563. void TIM_SelectOnePulseMode(TIM_TypeDef *TIMx, uint16_t TIM_OPMode)
  1564. {
  1565. TIMx->CTLR1 &= (uint16_t) ~((uint16_t)TIM_OPM);
  1566. TIMx->CTLR1 |= TIM_OPMode;
  1567. }
  1568. /*********************************************************************
  1569. * @fn TIM_SelectOutputTrigger
  1570. *
  1571. * @brief Selects the TIMx Trigger Output Mode.
  1572. *
  1573. * @param TIMx - where x can be 1 to 4 select the TIM peripheral.
  1574. * TIM_TRGOSource - specifies the Trigger Output source.
  1575. * TIM_TRGOSource_Reset - The UG bit in the TIM_EGR register is
  1576. * used as the trigger output (TRGO).
  1577. * TIM_TRGOSource_Enable - The Counter Enable CEN is used as the
  1578. * trigger output (TRGO).
  1579. * TIM_TRGOSource_Update - The update event is selected as the
  1580. * trigger output (TRGO).
  1581. * TIM_TRGOSource_OC1 - The trigger output sends a positive pulse
  1582. * when the CC1IF flag is to be set, as soon as a capture or compare match occurs (TRGO).
  1583. * TIM_TRGOSource_OC1Ref - OC1REF signal is used as the trigger output (TRGO).
  1584. * TIM_TRGOSource_OC2Ref - OC2REF signal is used as the trigger output (TRGO).
  1585. * TIM_TRGOSource_OC3Ref - OC3REF signal is used as the trigger output (TRGO).
  1586. * TIM_TRGOSource_OC4Ref - OC4REF signal is used as the trigger output (TRGO).
  1587. *
  1588. * @return none
  1589. */
  1590. void TIM_SelectOutputTrigger(TIM_TypeDef *TIMx, uint16_t TIM_TRGOSource)
  1591. {
  1592. TIMx->CTLR2 &= (uint16_t) ~((uint16_t)TIM_MMS);
  1593. TIMx->CTLR2 |= TIM_TRGOSource;
  1594. }
  1595. /*********************************************************************
  1596. * @fn TIM_SelectSlaveMode
  1597. *
  1598. * @brief Selects the TIMx Slave Mode.
  1599. *
  1600. * @param TIMx - where x can be 1 to 4 select the TIM peripheral.
  1601. * TIM_SlaveMode - specifies the Timer Slave Mode.
  1602. * TIM_SlaveMode_Reset - Rising edge of the selected trigger
  1603. * signal (TRGI) re-initializes.
  1604. * TIM_SlaveMode_Gated - The counter clock is enabled when the
  1605. * trigger signal (TRGI) is high.
  1606. * TIM_SlaveMode_Trigger - The counter starts at a rising edge
  1607. * of the trigger TRGI.
  1608. * TIM_SlaveMode_External1 - Rising edges of the selected trigger
  1609. * (TRGI) clock the counter.
  1610. *
  1611. * @return none
  1612. */
  1613. void TIM_SelectSlaveMode(TIM_TypeDef *TIMx, uint16_t TIM_SlaveMode)
  1614. {
  1615. TIMx->SMCFGR &= (uint16_t) ~((uint16_t)TIM_SMS);
  1616. TIMx->SMCFGR |= TIM_SlaveMode;
  1617. }
  1618. /*********************************************************************
  1619. * @fn TIM_SelectMasterSlaveMode
  1620. *
  1621. * @brief Sets or Resets the TIMx Master/Slave Mode.
  1622. *
  1623. * @param TIMx - where x can be 1 to 4 select the TIM peripheral.
  1624. * TIM_MasterSlaveMode - specifies the Timer Master Slave Mode.
  1625. * TIM_MasterSlaveMode_Enable - synchronization between the current
  1626. * timer and its slaves (through TRGO).
  1627. * TIM_MasterSlaveMode_Disable - No action.
  1628. *
  1629. * @return none
  1630. */
  1631. void TIM_SelectMasterSlaveMode(TIM_TypeDef *TIMx, uint16_t TIM_MasterSlaveMode)
  1632. {
  1633. TIMx->SMCFGR &= (uint16_t) ~((uint16_t)TIM_MSM);
  1634. TIMx->SMCFGR |= TIM_MasterSlaveMode;
  1635. }
  1636. /*********************************************************************
  1637. * @fn TIM_SetCounter
  1638. *
  1639. * @brief Sets the TIMx Counter Register value.
  1640. *
  1641. * @param TIMx - where x can be 1 to 4 select the TIM peripheral.
  1642. * Counter - specifies the Counter register new value.
  1643. *
  1644. * @return none
  1645. */
  1646. void TIM_SetCounter(TIM_TypeDef *TIMx, uint16_t Counter)
  1647. {
  1648. TIMx->CNT = Counter;
  1649. }
  1650. /*********************************************************************
  1651. * @fn TIM_SetAutoreload
  1652. *
  1653. * @brief Sets the TIMx Autoreload Register value.
  1654. *
  1655. * @param TIMx - where x can be 1 to 4 select the TIM peripheral.
  1656. * Autoreload - specifies the Autoreload register new value.
  1657. *
  1658. * @return none
  1659. */
  1660. void TIM_SetAutoreload(TIM_TypeDef *TIMx, uint16_t Autoreload)
  1661. {
  1662. TIMx->ATRLR = Autoreload;
  1663. }
  1664. /*********************************************************************
  1665. * @fn TIM_SetCompare1
  1666. *
  1667. * @brief Sets the TIMx Capture Compare1 Register value.
  1668. *
  1669. * @param TIMx - where x can be 1 to 4 select the TIM peripheral.
  1670. * Compare1 - specifies the Capture Compare1 register new value.
  1671. *
  1672. * @return none
  1673. */
  1674. void TIM_SetCompare1(TIM_TypeDef *TIMx, uint16_t Compare1)
  1675. {
  1676. TIMx->CH1CVR = Compare1;
  1677. }
  1678. /*********************************************************************
  1679. * @fn TIM_SetCompare2
  1680. *
  1681. * @brief Sets the TIMx Capture Compare2 Register value.
  1682. *
  1683. * @param TIMx - where x can be 1 to 4 select the TIM peripheral.
  1684. * Compare1 - specifies the Capture Compare1 register new value.
  1685. *
  1686. * @return none
  1687. */
  1688. void TIM_SetCompare2(TIM_TypeDef *TIMx, uint16_t Compare2)
  1689. {
  1690. TIMx->CH2CVR = Compare2;
  1691. }
  1692. /*********************************************************************
  1693. * @fn TIM_SetCompare3
  1694. *
  1695. * @brief Sets the TIMx Capture Compare3 Register value.
  1696. *
  1697. * @param TIMx - where x can be 1 to 4 select the TIM peripheral.
  1698. * Compare1 - specifies the Capture Compare1 register new value.
  1699. *
  1700. * @return none
  1701. */
  1702. void TIM_SetCompare3(TIM_TypeDef *TIMx, uint16_t Compare3)
  1703. {
  1704. TIMx->CH3CVR = Compare3;
  1705. }
  1706. /*********************************************************************
  1707. * @fn TIM_SetCompare4
  1708. *
  1709. * @brief Sets the TIMx Capture Compare4 Register value.
  1710. *
  1711. * @param TIMx - where x can be 1 to 4 select the TIM peripheral.
  1712. * Compare1 - specifies the Capture Compare1 register new value.
  1713. *
  1714. * @return none
  1715. */
  1716. void TIM_SetCompare4(TIM_TypeDef *TIMx, uint16_t Compare4)
  1717. {
  1718. TIMx->CH4CVR = Compare4;
  1719. }
  1720. /*********************************************************************
  1721. * @fn TIM_SetIC1Prescaler
  1722. *
  1723. * @brief Sets the TIMx Input Capture 1 prescaler.
  1724. *
  1725. * @param TIMx - where x can be 1 to 4 select the TIM peripheral.
  1726. * TIM_ICPSC - specifies the Input Capture1 prescaler new value.
  1727. * TIM_ICPSC_DIV1 - no prescaler.
  1728. * TIM_ICPSC_DIV2 - capture is done once every 2 events.
  1729. * TIM_ICPSC_DIV4 - capture is done once every 4 events.
  1730. * TIM_ICPSC_DIV8 - capture is done once every 8 events.
  1731. *
  1732. * @return none
  1733. */
  1734. void TIM_SetIC1Prescaler(TIM_TypeDef *TIMx, uint16_t TIM_ICPSC)
  1735. {
  1736. TIMx->CHCTLR1 &= (uint16_t) ~((uint16_t)TIM_IC1PSC);
  1737. TIMx->CHCTLR1 |= TIM_ICPSC;
  1738. }
  1739. /*********************************************************************
  1740. * @fn TIM_SetIC2Prescaler
  1741. *
  1742. * @brief Sets the TIMx Input Capture 2 prescaler.
  1743. *
  1744. * @param TIMx - where x can be 1 to 4 select the TIM peripheral.
  1745. * TIM_ICPSC - specifies the Input Capture1 prescaler new value.
  1746. * TIM_ICPSC_DIV1 - no prescaler.
  1747. * TIM_ICPSC_DIV2 - capture is done once every 2 events.
  1748. * TIM_ICPSC_DIV4 - capture is done once every 4 events.
  1749. * TIM_ICPSC_DIV8 - capture is done once every 8 events.
  1750. *
  1751. * @return none
  1752. */
  1753. void TIM_SetIC2Prescaler(TIM_TypeDef *TIMx, uint16_t TIM_ICPSC)
  1754. {
  1755. TIMx->CHCTLR1 &= (uint16_t) ~((uint16_t)TIM_IC2PSC);
  1756. TIMx->CHCTLR1 |= (uint16_t)(TIM_ICPSC << 8);
  1757. }
  1758. /*********************************************************************
  1759. * @fn TIM_SetIC3Prescaler
  1760. *
  1761. * @brief Sets the TIMx Input Capture 3 prescaler.
  1762. *
  1763. * @param TIMx - where x can be 1 to 4 select the TIM peripheral.
  1764. * TIM_ICPSC - specifies the Input Capture1 prescaler new value.
  1765. * TIM_ICPSC_DIV1 - no prescaler.
  1766. * TIM_ICPSC_DIV2 - capture is done once every 2 events.
  1767. * TIM_ICPSC_DIV4 - capture is done once every 4 events.
  1768. * TIM_ICPSC_DIV8 - capture is done once every 8 events.
  1769. *
  1770. * @return none
  1771. */
  1772. void TIM_SetIC3Prescaler(TIM_TypeDef *TIMx, uint16_t TIM_ICPSC)
  1773. {
  1774. TIMx->CHCTLR2 &= (uint16_t) ~((uint16_t)TIM_IC3PSC);
  1775. TIMx->CHCTLR2 |= TIM_ICPSC;
  1776. }
  1777. /*********************************************************************
  1778. * @fn TIM_SetIC4Prescaler
  1779. *
  1780. * @brief Sets the TIMx Input Capture 4 prescaler.
  1781. *
  1782. * @param TIMx - where x can be 1 to 4 select the TIM peripheral.
  1783. * TIM_ICPSC - specifies the Input Capture1 prescaler new value.
  1784. * TIM_ICPSC_DIV1 - no prescaler.
  1785. * TIM_ICPSC_DIV2 - capture is done once every 2 events.
  1786. * TIM_ICPSC_DIV4 - capture is done once every 4 events.
  1787. * TIM_ICPSC_DIV8 - capture is done once every 8 events.
  1788. *
  1789. * @return none
  1790. */
  1791. void TIM_SetIC4Prescaler(TIM_TypeDef *TIMx, uint16_t TIM_ICPSC)
  1792. {
  1793. TIMx->CHCTLR2 &= (uint16_t) ~((uint16_t)TIM_IC4PSC);
  1794. TIMx->CHCTLR2 |= (uint16_t)(TIM_ICPSC << 8);
  1795. }
  1796. /*********************************************************************
  1797. * @fn TIM_SetClockDivision
  1798. *
  1799. * @brief Sets the TIMx Clock Division value.
  1800. *
  1801. * @param TIMx - where x can be 1 to 4 select the TIM peripheral.
  1802. * TIM_CKD - specifies the clock division value.
  1803. * TIM_CKD_DIV1 - TDTS = Tck_tim.
  1804. * TIM_CKD_DIV2 - TDTS = 2*Tck_tim.
  1805. * TIM_CKD_DIV4 - TDTS = 4*Tck_tim.
  1806. *
  1807. * @return none
  1808. */
  1809. void TIM_SetClockDivision(TIM_TypeDef *TIMx, uint16_t TIM_CKD)
  1810. {
  1811. TIMx->CTLR1 &= (uint16_t) ~((uint16_t)TIM_CTLR1_CKD);
  1812. TIMx->CTLR1 |= TIM_CKD;
  1813. }
  1814. /*********************************************************************
  1815. * @fn TIM_GetCapture1
  1816. *
  1817. * @brief Gets the TIMx Input Capture 1 value.
  1818. *
  1819. * @param TIMx - where x can be 1 to 4 select the TIM peripheral.
  1820. *
  1821. * @return TIMx->CH1CVR - Capture Compare 1 Register value.
  1822. */
  1823. uint16_t TIM_GetCapture1(TIM_TypeDef *TIMx)
  1824. {
  1825. return TIMx->CH1CVR;
  1826. }
  1827. /*********************************************************************
  1828. * @fn TIM_GetCapture2
  1829. *
  1830. * @brief Gets the TIMx Input Capture 2 value.
  1831. *
  1832. * @param TIMx - where x can be 1 to 4 select the TIM peripheral.
  1833. *
  1834. * @return TIMx->CH2CVR - Capture Compare 2 Register value.
  1835. */
  1836. uint16_t TIM_GetCapture2(TIM_TypeDef *TIMx)
  1837. {
  1838. return TIMx->CH2CVR;
  1839. }
  1840. /*********************************************************************
  1841. * @fn TIM_GetCapture3
  1842. *
  1843. * @brief Gets the TIMx Input Capture 3 value.
  1844. *
  1845. * @param TIMx - where x can be 1 to 4 select the TIM peripheral.
  1846. *
  1847. * @return TIMx->CH3CVR - Capture Compare 3 Register value.
  1848. */
  1849. uint16_t TIM_GetCapture3(TIM_TypeDef *TIMx)
  1850. {
  1851. return TIMx->CH3CVR;
  1852. }
  1853. /*********************************************************************
  1854. * @fn TIM_GetCapture4
  1855. *
  1856. * @brief Gets the TIMx Input Capture 4 value.
  1857. *
  1858. * @param TIMx - where x can be 1 to 4 select the TIM peripheral.
  1859. *
  1860. * @return TIMx->CH4CVR - Capture Compare 4 Register value.
  1861. */
  1862. uint16_t TIM_GetCapture4(TIM_TypeDef *TIMx)
  1863. {
  1864. return TIMx->CH4CVR;
  1865. }
  1866. /*********************************************************************
  1867. * @fn TIM_GetCounter
  1868. *
  1869. * @brief Gets the TIMx Counter value.
  1870. *
  1871. * @param TIMx - where x can be 1 to 4 select the TIM peripheral.
  1872. *
  1873. * @return TIMx->CNT - Counter Register value.
  1874. */
  1875. uint16_t TIM_GetCounter(TIM_TypeDef *TIMx)
  1876. {
  1877. return TIMx->CNT;
  1878. }
  1879. /*********************************************************************
  1880. * @fn TIM_GetPrescaler
  1881. *
  1882. * @brief Gets the TIMx Prescaler value.
  1883. *
  1884. * @param TIMx - where x can be 1 to 4 select the TIM peripheral.
  1885. *
  1886. * @return TIMx->PSC - Prescaler Register value.
  1887. */
  1888. uint16_t TIM_GetPrescaler(TIM_TypeDef *TIMx)
  1889. {
  1890. return TIMx->PSC;
  1891. }
  1892. /*********************************************************************
  1893. * @fn TIM_GetFlagStatus
  1894. *
  1895. * @brief Checks whether the specified TIM flag is set or not.
  1896. *
  1897. * @param TIMx - where x can be 1 to 4 select the TIM peripheral.
  1898. * TIM_FLAG - specifies the flag to check.
  1899. * TIM_FLAG_Update - TIM update Flag.
  1900. * TIM_FLAG_CC1 - TIM Capture Compare 1 Flag.
  1901. * TIM_FLAG_CC2 - TIM Capture Compare 2 Flag.
  1902. * TIM_FLAG_CC3 - TIM Capture Compare 3 Flag.
  1903. * TIM_FLAG_CC4 - TIM Capture Compare 4 Flag.
  1904. * TIM_FLAG_COM - TIM Commutation Flag.
  1905. * TIM_FLAG_Trigger - TIM Trigger Flag.
  1906. * TIM_FLAG_Break - TIM Break Flag.
  1907. * TIM_FLAG_CC1OF - TIM Capture Compare 1 overcapture Flag.
  1908. * TIM_FLAG_CC2OF - TIM Capture Compare 2 overcapture Flag.
  1909. * TIM_FLAG_CC3OF - TIM Capture Compare 3 overcapture Flag.
  1910. * TIM_FLAG_CC4OF - TIM Capture Compare 4 overcapture Flag.
  1911. *
  1912. * @return none
  1913. */
  1914. FlagStatus TIM_GetFlagStatus(TIM_TypeDef *TIMx, uint16_t TIM_FLAG)
  1915. {
  1916. ITStatus bitstatus = RESET;
  1917. if((TIMx->INTFR & TIM_FLAG) != (uint16_t)RESET)
  1918. {
  1919. bitstatus = SET;
  1920. }
  1921. else
  1922. {
  1923. bitstatus = RESET;
  1924. }
  1925. return bitstatus;
  1926. }
  1927. /*********************************************************************
  1928. * @fn TIM_ClearFlag
  1929. *
  1930. * @brief Clears the TIMx's pending flags.
  1931. *
  1932. * @param TIMx - where x can be 1 to 4 select the TIM peripheral.
  1933. * TIM_FLAG - specifies the flag to check.
  1934. * TIM_FLAG_Update - TIM update Flag.
  1935. * TIM_FLAG_CC1 - TIM Capture Compare 1 Flag.
  1936. * TIM_FLAG_CC2 - TIM Capture Compare 2 Flag.
  1937. * TIM_FLAG_CC3 - TIM Capture Compare 3 Flag.
  1938. * TIM_FLAG_CC4 - TIM Capture Compare 4 Flag.
  1939. * TIM_FLAG_COM - TIM Commutation Flag.
  1940. * TIM_FLAG_Trigger - TIM Trigger Flag.
  1941. * TIM_FLAG_Break - TIM Break Flag.
  1942. * TIM_FLAG_CC1OF - TIM Capture Compare 1 overcapture Flag.
  1943. * TIM_FLAG_CC2OF - TIM Capture Compare 2 overcapture Flag.
  1944. * TIM_FLAG_CC3OF - TIM Capture Compare 3 overcapture Flag.
  1945. * TIM_FLAG_CC4OF - TIM Capture Compare 4 overcapture Flag.
  1946. *
  1947. * @return none
  1948. */
  1949. void TIM_ClearFlag(TIM_TypeDef *TIMx, uint16_t TIM_FLAG)
  1950. {
  1951. TIMx->INTFR = (uint16_t)~TIM_FLAG;
  1952. }
  1953. /*********************************************************************
  1954. * @fn TIM_GetITStatus
  1955. *
  1956. * @brief Checks whether the TIM interrupt has occurred or not.
  1957. *
  1958. * @param TIMx - where x can be 1 to 4 select the TIM peripheral.
  1959. * TIM_IT - specifies the TIM interrupt source to check.
  1960. * TIM_IT_Update - TIM update Interrupt source.
  1961. * TIM_IT_CC1 - TIM Capture Compare 1 Interrupt source.
  1962. * TIM_IT_CC2 - TIM Capture Compare 2 Interrupt source.
  1963. * TIM_IT_CC3 - TIM Capture Compare 3 Interrupt source.
  1964. * TIM_IT_CC4 - TIM Capture Compare 4 Interrupt source.
  1965. * TIM_IT_COM - TIM Commutation Interrupt source.
  1966. * TIM_IT_Trigger - TIM Trigger Interrupt source.
  1967. * TIM_IT_Break - TIM Break Interrupt source.
  1968. *
  1969. * @return none
  1970. */
  1971. ITStatus TIM_GetITStatus(TIM_TypeDef *TIMx, uint16_t TIM_IT)
  1972. {
  1973. ITStatus bitstatus = RESET;
  1974. uint16_t itstatus = 0x0, itenable = 0x0;
  1975. itstatus = TIMx->INTFR & TIM_IT;
  1976. itenable = TIMx->DMAINTENR & TIM_IT;
  1977. if((itstatus != (uint16_t)RESET) && (itenable != (uint16_t)RESET))
  1978. {
  1979. bitstatus = SET;
  1980. }
  1981. else
  1982. {
  1983. bitstatus = RESET;
  1984. }
  1985. return bitstatus;
  1986. }
  1987. /*********************************************************************
  1988. * @fn TIM_ClearITPendingBit
  1989. *
  1990. * @brief Clears the TIMx's interrupt pending bits.
  1991. *
  1992. * @param TIMx - where x can be 1 to 4 select the TIM peripheral.
  1993. * TIM_IT - specifies the TIM interrupt source to check.
  1994. * TIM_IT_Update - TIM update Interrupt source.
  1995. * TIM_IT_CC1 - TIM Capture Compare 1 Interrupt source.
  1996. * TIM_IT_CC2 - TIM Capture Compare 2 Interrupt source.
  1997. * TIM_IT_CC3 - TIM Capture Compare 3 Interrupt source.
  1998. * TIM_IT_CC4 - TIM Capture Compare 4 Interrupt source.
  1999. * TIM_IT_COM - TIM Commutation Interrupt source.
  2000. * TIM_IT_Trigger - TIM Trigger Interrupt source.
  2001. * TIM_IT_Break - TIM Break Interrupt source.
  2002. *
  2003. * @return none
  2004. */
  2005. void TIM_ClearITPendingBit(TIM_TypeDef *TIMx, uint16_t TIM_IT)
  2006. {
  2007. TIMx->INTFR = (uint16_t)~TIM_IT;
  2008. }
  2009. /*********************************************************************
  2010. * @fn TI1_Config
  2011. *
  2012. * @brief Configure the TI1 as Input.
  2013. *
  2014. * @param TIMx - where x can be 1 to 4 select the TIM peripheral.
  2015. * IM_ICPolarity - The Input Polarity.
  2016. * TIM_ICPolarity_Rising.
  2017. * TIM_ICPolarity_Falling.
  2018. * TIM_ICSelection - specifies the input to be used.
  2019. * TIM_ICSelection_DirectTI - TIM Input 1 is selected to be
  2020. * connected to IC1.
  2021. * TIM_ICSelection_IndirectTI - TIM Input 1 is selected to be
  2022. * connected to IC2.
  2023. * TIM_ICSelection_TRC - TIM Input 1 is selected to be connected
  2024. * to TRC.
  2025. * TIM_ICFilter - Specifies the Input Capture Filter.
  2026. * This parameter must be a value between 0x00 and 0x0F.
  2027. *
  2028. * @return none
  2029. */
  2030. static void TI1_Config(TIM_TypeDef *TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,
  2031. uint16_t TIM_ICFilter)
  2032. {
  2033. uint16_t tmpccmr1 = 0, tmpccer = 0;
  2034. TIMx->CCER &= (uint16_t) ~((uint16_t)TIM_CC1E);
  2035. tmpccmr1 = TIMx->CHCTLR1;
  2036. tmpccer = TIMx->CCER;
  2037. tmpccmr1 &= (uint16_t)(((uint16_t) ~((uint16_t)TIM_CC1S)) & ((uint16_t) ~((uint16_t)TIM_IC1F)));
  2038. tmpccmr1 |= (uint16_t)(TIM_ICSelection | (uint16_t)(TIM_ICFilter << (uint16_t)4));
  2039. if((TIMx == TIM1) || (TIMx == TIM2) || (TIMx == TIM3) || (TIMx == TIM4) || (TIMx == TIM5))
  2040. {
  2041. tmpccer &= (uint16_t) ~((uint16_t)(TIM_CC1P));
  2042. tmpccer |= (uint16_t)(TIM_ICPolarity | (uint16_t)TIM_CC1E);
  2043. }
  2044. else
  2045. {
  2046. tmpccer &= (uint16_t) ~((uint16_t)(TIM_CC1P | TIM_CC1NP));
  2047. tmpccer |= (uint16_t)(TIM_ICPolarity | (uint16_t)TIM_CC1E);
  2048. }
  2049. TIMx->CHCTLR1 = tmpccmr1;
  2050. TIMx->CCER = tmpccer;
  2051. }
  2052. /*********************************************************************
  2053. * @fn TI2_Config
  2054. *
  2055. * @brief Configure the TI2 as Input.
  2056. *
  2057. * @param TIMx - where x can be 1 to 4 select the TIM peripheral.
  2058. * IM_ICPolarity - The Input Polarity.
  2059. * TIM_ICPolarity_Rising.
  2060. * TIM_ICPolarity_Falling.
  2061. * TIM_ICSelection - specifies the input to be used.
  2062. * TIM_ICSelection_DirectTI - TIM Input 1 is selected to be
  2063. * connected to IC1.
  2064. * TIM_ICSelection_IndirectTI - TIM Input 1 is selected to be
  2065. * connected to IC2.
  2066. * TIM_ICSelection_TRC - TIM Input 1 is selected to be connected
  2067. * to TRC.
  2068. * TIM_ICFilter - Specifies the Input Capture Filter.
  2069. * This parameter must be a value between 0x00 and 0x0F.
  2070. *
  2071. * @return none
  2072. */
  2073. static void TI2_Config(TIM_TypeDef *TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,
  2074. uint16_t TIM_ICFilter)
  2075. {
  2076. uint16_t tmpccmr1 = 0, tmpccer = 0, tmp = 0;
  2077. TIMx->CCER &= (uint16_t) ~((uint16_t)TIM_CC2E);
  2078. tmpccmr1 = TIMx->CHCTLR1;
  2079. tmpccer = TIMx->CCER;
  2080. tmp = (uint16_t)(TIM_ICPolarity << 4);
  2081. tmpccmr1 &= (uint16_t)(((uint16_t) ~((uint16_t)TIM_CC2S)) & ((uint16_t) ~((uint16_t)TIM_IC2F)));
  2082. tmpccmr1 |= (uint16_t)(TIM_ICFilter << 12);
  2083. tmpccmr1 |= (uint16_t)(TIM_ICSelection << 8);
  2084. if((TIMx == TIM1) || (TIMx == TIM2) || (TIMx == TIM3) || (TIMx == TIM4) || (TIMx == TIM5))
  2085. {
  2086. tmpccer &= (uint16_t) ~((uint16_t)(TIM_CC2P));
  2087. tmpccer |= (uint16_t)(tmp | (uint16_t)TIM_CC2E);
  2088. }
  2089. else
  2090. {
  2091. tmpccer &= (uint16_t) ~((uint16_t)(TIM_CC2P | TIM_CC2NP));
  2092. tmpccer |= (uint16_t)(TIM_ICPolarity | (uint16_t)TIM_CC2E);
  2093. }
  2094. TIMx->CHCTLR1 = tmpccmr1;
  2095. TIMx->CCER = tmpccer;
  2096. }
  2097. /*********************************************************************
  2098. * @fn TI3_Config
  2099. *
  2100. * @brief Configure the TI3 as Input.
  2101. *
  2102. * @param TIMx - where x can be 1 to 4 select the TIM peripheral.
  2103. * IM_ICPolarity - The Input Polarity.
  2104. * TIM_ICPolarity_Rising.
  2105. * TIM_ICPolarity_Falling.
  2106. * TIM_ICSelection - specifies the input to be used.
  2107. * TIM_ICSelection_DirectTI - TIM Input 1 is selected to be
  2108. * connected to IC1.
  2109. * TIM_ICSelection_IndirectTI - TIM Input 1 is selected to be
  2110. * connected to IC2.
  2111. * TIM_ICSelection_TRC - TIM Input 1 is selected to be connected
  2112. * to TRC.
  2113. * TIM_ICFilter - Specifies the Input Capture Filter.
  2114. * This parameter must be a value between 0x00 and 0x0F.
  2115. *
  2116. * @return none
  2117. */
  2118. static void TI3_Config(TIM_TypeDef *TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,
  2119. uint16_t TIM_ICFilter)
  2120. {
  2121. uint16_t tmpccmr2 = 0, tmpccer = 0, tmp = 0;
  2122. TIMx->CCER &= (uint16_t) ~((uint16_t)TIM_CC3E);
  2123. tmpccmr2 = TIMx->CHCTLR2;
  2124. tmpccer = TIMx->CCER;
  2125. tmp = (uint16_t)(TIM_ICPolarity << 8);
  2126. tmpccmr2 &= (uint16_t)(((uint16_t) ~((uint16_t)TIM_CC3S)) & ((uint16_t) ~((uint16_t)TIM_IC3F)));
  2127. tmpccmr2 |= (uint16_t)(TIM_ICSelection | (uint16_t)(TIM_ICFilter << (uint16_t)4));
  2128. if((TIMx == TIM1) || (TIMx == TIM2) || (TIMx == TIM3) || (TIMx == TIM4) || (TIMx == TIM5))
  2129. {
  2130. tmpccer &= (uint16_t) ~((uint16_t)(TIM_CC3P));
  2131. tmpccer |= (uint16_t)(tmp | (uint16_t)TIM_CC3E);
  2132. }
  2133. else
  2134. {
  2135. tmpccer &= (uint16_t) ~((uint16_t)(TIM_CC3P | TIM_CC3NP));
  2136. tmpccer |= (uint16_t)(TIM_ICPolarity | (uint16_t)TIM_CC3E);
  2137. }
  2138. TIMx->CHCTLR2 = tmpccmr2;
  2139. TIMx->CCER = tmpccer;
  2140. }
  2141. /*********************************************************************
  2142. * @fn TI4_Config
  2143. *
  2144. * @brief Configure the TI4 as Input.
  2145. *
  2146. * @param TIMx - where x can be 1 to 4 select the TIM peripheral.
  2147. * IM_ICPolarity - The Input Polarity.
  2148. * TIM_ICPolarity_Rising.
  2149. * TIM_ICPolarity_Falling.
  2150. * TIM_ICSelection - specifies the input to be used.
  2151. * TIM_ICSelection_DirectTI - TIM Input 1 is selected to be
  2152. * connected to IC1.
  2153. * TIM_ICSelection_IndirectTI - TIM Input 1 is selected to be
  2154. * connected to IC2.
  2155. * TIM_ICSelection_TRC - TIM Input 1 is selected to be connected
  2156. * to TRC.
  2157. * TIM_ICFilter - Specifies the Input Capture Filter.
  2158. * This parameter must be a value between 0x00 and 0x0F.
  2159. *
  2160. * @return none
  2161. */
  2162. static void TI4_Config(TIM_TypeDef *TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,
  2163. uint16_t TIM_ICFilter)
  2164. {
  2165. uint16_t tmpccmr2 = 0, tmpccer = 0, tmp = 0;
  2166. TIMx->CCER &= (uint16_t) ~((uint16_t)TIM_CC4E);
  2167. tmpccmr2 = TIMx->CHCTLR2;
  2168. tmpccer = TIMx->CCER;
  2169. tmp = (uint16_t)(TIM_ICPolarity << 12);
  2170. tmpccmr2 &= (uint16_t)((uint16_t)(~(uint16_t)TIM_CC4S) & ((uint16_t) ~((uint16_t)TIM_IC4F)));
  2171. tmpccmr2 |= (uint16_t)(TIM_ICSelection << 8);
  2172. tmpccmr2 |= (uint16_t)(TIM_ICFilter << 12);
  2173. if((TIMx == TIM1) || (TIMx == TIM2) || (TIMx == TIM3) || (TIMx == TIM4) || (TIMx == TIM5))
  2174. {
  2175. tmpccer &= (uint16_t) ~((uint16_t)(TIM_CC4P));
  2176. tmpccer |= (uint16_t)(tmp | (uint16_t)TIM_CC4E);
  2177. }
  2178. else
  2179. {
  2180. tmpccer &= (uint16_t) ~((uint16_t)(TIM_CC3P ));
  2181. tmpccer |= (uint16_t)(TIM_ICPolarity | (uint16_t)TIM_CC4E);
  2182. }
  2183. TIMx->CHCTLR2 = tmpccmr2;
  2184. TIMx->CCER = tmpccer;
  2185. }