ch32v20x_tim5.c 70 KB

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