stm8l15x_tim5.c 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154
  1. /**
  2. ******************************************************************************
  3. * @file stm8l15x_tim5.c
  4. * @author MCD Application Team
  5. * @version V1.6.1
  6. * @date 30-September-2014
  7. * @brief This file provides firmware functions to manage the following
  8. * functionalities of the TIM5 peripheral:
  9. * - TimeBase management
  10. * - Output Compare management
  11. * - Input Capture management
  12. * - Interrupts, DMA and flags management
  13. * - Clocks management
  14. * - Synchronization management
  15. * - Specific interface management
  16. *
  17. * @verbatim
  18. *
  19. * ===================================================================
  20. * How to use this driver
  21. * ===================================================================
  22. * This driver provides functions to configure and initialize the TIM5
  23. * peripheral
  24. * These functions are split in 7 groups:
  25. *
  26. * 1. TIM5 TimeBase management: this group includes all needed functions
  27. * to configure the TIM Timebase unit:
  28. * - Set/Get Prescaler
  29. * - Set/Get Autoreload
  30. * - Counter modes configuration
  31. * - Select the One Pulse mode
  32. * - Update Request Configuration
  33. * - Update Disable Configuration
  34. * - Auto-Preload Configuration
  35. * - Enable/Disable the counter
  36. *
  37. * 2. TIM5 Output Compare management: this group includes all needed
  38. * functions to configure the Capture/Compare unit used in Output
  39. * compare mode:
  40. * - Configure each channel, independently, in Output Compare mode
  41. * - Select the output compare modes
  42. * - Select the Polarities of each channel
  43. * - Set/Get the Capture/Compare register values
  44. * - Select the Output Compare Fast mode
  45. * - Select the Output Compare Forced mode
  46. * - Output Compare-Preload Configuration
  47. * - Enable/Disable the Capture/Compare Channels
  48. *
  49. * 3. TIM5 Input Capture management: this group includes all needed
  50. * functions to configure the Capture/Compare unit used in
  51. * Input Capture mode:
  52. * - Configure each channel in input capture mode
  53. * - Configure Channel1/2 in PWM Input mode
  54. * - Set the Input Capture Prescaler
  55. * - Get the Capture/Compare values
  56. *
  57. * 4. TIM5 interrupts, DMA and flags management
  58. * - Enable/Disable interrupt sources
  59. * - Get flags status
  60. * - Clear flags/ Pending bits
  61. * - Enable/Disable DMA requests
  62. * - Select CaptureCompare DMA request
  63. *
  64. * 5. TIM5 clocks management: this group includes all needed functions
  65. * to configure the clock controller unit:
  66. * - Select internal/External clock
  67. * - Select the external clock mode: ETR(Mode1/Mode2) or TIx
  68. *
  69. * 6. TIM5 synchronization management: this group includes all needed
  70. * functions to configure the Synchronization unit:
  71. * - Select Input Trigger
  72. * - Select Output Trigger
  73. * - Select Master Slave Mode
  74. * - ETR Configuration when used as external trigger
  75. *
  76. * 7. TIM5 specific interface management, this group includes all
  77. * needed functions to use the specific TIM5 interface:
  78. * - Encoder Interface Configuration
  79. * - Select Hall Sensor
  80. *
  81. * @endverbatim
  82. *
  83. ******************************************************************************
  84. * @attention
  85. *
  86. * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  87. *
  88. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  89. * You may not use this file except in compliance with the License.
  90. * You may obtain a copy of the License at:
  91. *
  92. * http://www.st.com/software_license_agreement_liberty_v2
  93. *
  94. * Unless required by applicable law or agreed to in writing, software
  95. * distributed under the License is distributed on an "AS IS" BASIS,
  96. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  97. * See the License for the specific language governing permissions and
  98. * limitations under the License.
  99. *
  100. ******************************************************************************
  101. */
  102. /* Includes ------------------------------------------------------------------*/
  103. #include "stm8l15x_TIM5.h"
  104. /** @addtogroup STM8L15x_StdPeriph_Driver
  105. * @{
  106. */
  107. /** @defgroup TIM5
  108. * @brief TIM5 driver modules
  109. * @{
  110. */
  111. /* Private typedef -----------------------------------------------------------*/
  112. /* Private define ------------------------------------------------------------*/
  113. /* Private macro -------------------------------------------------------------*/
  114. /* Private variables ---------------------------------------------------------*/
  115. /* Private function prototypes -----------------------------------------------*/
  116. static void TI1_Config(TIM5_ICPolarity_TypeDef TIM5_ICPolarity,
  117. TIM5_ICSelection_TypeDef TIM5_ICSelection,
  118. uint8_t TIM5_ICFilter);
  119. static void TI2_Config(TIM5_ICPolarity_TypeDef TIM5_ICPolarity,
  120. TIM5_ICSelection_TypeDef TIM5_ICSelection,
  121. uint8_t TIM5_ICFilter);
  122. /** @defgroup TIM5_Private_Functions
  123. * @{
  124. */
  125. /** @defgroup TIM5_Group1 TimeBase management functions
  126. * @brief TimeBase management functions
  127. *
  128. @verbatim
  129. ===============================================================================
  130. TimeBase management functions
  131. ===============================================================================
  132. ===================================================================
  133. TIM5 Driver: how to use it in Timing(Time base) Mode
  134. ===================================================================
  135. To use the Timer in Timing(Time base) mode, the following steps are mandatory:
  136. 1. Enable TIM5 clock using CLK_PeripheralClockConfig(CLK_Peripheral_TIM5, ENABLE) function.
  137. 2. Call TIM5_TimeBaseInit() to configure the Time Base unit with the
  138. corresponding configuration.
  139. 3. Enable global interrupts if you need to generate the update interrupt.
  140. 4. Enable the corresponding interrupt using the function TIM5_ITConfig(TIM5_IT_Update)
  141. 5. Call the TIM5_Cmd(ENABLE) function to enable the TIM5 counter.
  142. Note1: All other functions can be used separately to modify, if needed,
  143. a specific feature of the Timer.
  144. @endverbatim
  145. * @{
  146. */
  147. /**
  148. * @brief Deinitialize the TIM5 peripheral registers to their default reset values.
  149. * @param None
  150. * @retval None
  151. */
  152. void TIM5_DeInit(void)
  153. {
  154. TIM5->CR1 = TIM_CR1_RESET_VALUE;
  155. TIM5->CR2 = TIM_CR2_RESET_VALUE;
  156. TIM5->SMCR = TIM_SMCR_RESET_VALUE;
  157. TIM5->ETR = TIM_ETR_RESET_VALUE;
  158. TIM5->IER = TIM_IER_RESET_VALUE;
  159. TIM5->SR2 = TIM_SR2_RESET_VALUE;
  160. /* Disable channels */
  161. TIM5->CCER1 = TIM_CCER1_RESET_VALUE;
  162. /* Configure channels as inputs: it is necessary if lock level is equal to 2 or 3 */
  163. TIM5->CCMR1 = 0x01;/*TIM5_ICxSource_TIxFPx */
  164. TIM5->CCMR2 = 0x01;/*TIM5_ICxSource_TIxFPx */
  165. /* Then reset channel registers: it also works if lock level is equal to 2 or 3 */
  166. TIM5->CCER1 = TIM_CCER1_RESET_VALUE;
  167. TIM5->CCMR1 = TIM_CCMR1_RESET_VALUE;
  168. TIM5->CCMR2 = TIM_CCMR2_RESET_VALUE;
  169. TIM5->CNTRH = TIM_CNTRH_RESET_VALUE;
  170. TIM5->CNTRL = TIM_CNTRL_RESET_VALUE;
  171. TIM5->PSCR = TIM_PSCR_RESET_VALUE;
  172. TIM5->ARRH = TIM_ARRH_RESET_VALUE;
  173. TIM5->ARRL = TIM_ARRL_RESET_VALUE;
  174. TIM5->CCR1H = TIM_CCR1H_RESET_VALUE;
  175. TIM5->CCR1L = TIM_CCR1L_RESET_VALUE;
  176. TIM5->CCR2H = TIM_CCR2H_RESET_VALUE;
  177. TIM5->CCR2L = TIM_CCR2L_RESET_VALUE;
  178. TIM5->OISR = TIM_OISR_RESET_VALUE;
  179. TIM5->EGR = 0x01;/* TIM_EGR_UG */
  180. TIM5->BKR = TIM_BKR_RESET_VALUE;
  181. TIM5->SR1 = TIM_SR1_RESET_VALUE;
  182. }
  183. /**
  184. * @brief Initializes the TIM5 Time Base Unit according to the specified parameters.
  185. * @param TIM5_Prescaler: Prescaler
  186. * This parameter can be one of the following values:
  187. * @arg TIM5_Prescaler_1: Time base Prescaler = 1 (No effect)
  188. * @arg TIM5_Prescaler_2: Time base Prescaler = 2
  189. * @arg TIM5_Prescaler_4: Time base Prescaler = 4
  190. * @arg TIM5_Prescaler_8: Time base Prescaler = 8
  191. * @arg TIM5_Prescaler_16: Time base Prescaler = 16
  192. * @arg TIM5_Prescaler_32: Time base Prescaler = 32
  193. * @arg TIM5_Prescaler_64: Time base Prescaler = 64
  194. * @arg TIM5_Prescaler_128: Time base Prescaler = 128
  195. * @param TIM5_CounterMode: Counter mode
  196. * This parameter can be one of the following values:
  197. * @arg TIM5_CounterMode_Up: Counter Up Mode
  198. * @arg TIM5_CounterMode_Down: Counter Down Mode
  199. * @arg TIM5_CounterMode_CenterAligned1: Counter Central aligned Mode 1
  200. * @arg TIM5_CounterMode_CenterAligned2: Counter Central aligned Mode 2
  201. * @arg TIM5_CounterMode_CenterAligned3: Counter Central aligned Mode 3
  202. * @param TIM5_Period: This parameter must be a value between 0x0000 and 0xFFFF.
  203. * @retval None
  204. */
  205. void TIM5_TimeBaseInit(TIM5_Prescaler_TypeDef TIM5_Prescaler,
  206. TIM5_CounterMode_TypeDef TIM5_CounterMode,
  207. uint16_t TIM5_Period)
  208. {
  209. assert_param(IS_TIM5_PRESCALER(TIM5_Prescaler));
  210. assert_param(IS_TIM5_COUNTER_MODE(TIM5_CounterMode));
  211. /* Set the Autoreload value */
  212. TIM5->ARRH = (uint8_t)(TIM5_Period >> 8) ;
  213. TIM5->ARRL = (uint8_t)(TIM5_Period);
  214. /* Set the Prescaler value */
  215. TIM5->PSCR = (uint8_t)(TIM5_Prescaler);
  216. /* Select the Counter Mode */
  217. TIM5->CR1 &= (uint8_t)((uint8_t)(~TIM_CR1_CMS)) & ((uint8_t)(~TIM_CR1_DIR));
  218. TIM5->CR1 |= (uint8_t)(TIM5_CounterMode);
  219. /* Generate an update event to reload the Prescaler value immediately */
  220. TIM5->EGR = TIM5_EventSource_Update;
  221. }
  222. /**
  223. * @brief Configures the TIM5 Prescaler.
  224. * @param Prescaler: Specifies the Prescaler Register value
  225. * This parameter can be one of the following values:
  226. * @arg TIM5_Prescaler_1: Time base Prescaler = 1 (No effect)
  227. * @arg TIM5_Prescaler_2: Time base Prescaler = 2
  228. * @arg TIM5_Prescaler_4: Time base Prescaler = 4
  229. * @arg TIM5_Prescaler_8: Time base Prescaler = 8
  230. * @arg TIM5_Prescaler_16: Time base Prescaler = 16
  231. * @arg TIM5_Prescaler_32: Time base Prescaler = 32
  232. * @arg TIM5_Prescaler_64: Time base Prescaler = 64
  233. * @arg TIM5_Prescaler_128: Time base Prescaler = 128
  234. * @param TIM5_PSCReloadMode: Specifies the TIM5 Prescaler Reload mode.
  235. * This parameter can be one of the following values:
  236. * @arg TIM5_PSCReloadMode_Update: Prescaler value is reloaded at every update
  237. * @arg TIM5_PSCReloadMode_Immediate: Prescaler value is reloaded at every update
  238. * @retval None
  239. */
  240. void TIM5_PrescalerConfig(TIM5_Prescaler_TypeDef Prescaler,
  241. TIM5_PSCReloadMode_TypeDef TIM5_PSCReloadMode)
  242. {
  243. /* Check the parameters */
  244. assert_param(IS_TIM5_PRESCALER(Prescaler));
  245. assert_param(IS_TIM5_PRESCALER_RELOAD(TIM5_PSCReloadMode));
  246. /* Set the Prescaler value */
  247. TIM5->PSCR = (uint8_t)(Prescaler);
  248. /* Set or reset the UG Bit */
  249. if (TIM5_PSCReloadMode == TIM5_PSCReloadMode_Immediate)
  250. {
  251. TIM5->EGR |= TIM_EGR_UG ;
  252. }
  253. else
  254. {
  255. TIM5->EGR &= (uint8_t)(~TIM_EGR_UG) ;
  256. }
  257. }
  258. /**
  259. * @brief Specifies the TIM5 Counter Mode to be used.
  260. * @param TIM5_CounterMode: Specifies the Counter Mode to be used
  261. * This parameter can be one of the following values:
  262. * @arg TIM5_CounterMode_Up: Counter Up Mode
  263. * @arg TIM5_CounterMode_Down: Counter Down Mode
  264. * @arg TIM5_CounterMode_CenterAligned1: Counter Central aligned Mode 1
  265. * @arg TIM5_CounterMode_CenterAligned2: Counter Central aligned Mode 2
  266. * @arg TIM5_CounterMode_CenterAligned3: Counter Central aligned Mode 3
  267. * @retval None
  268. */
  269. void TIM5_CounterModeConfig(TIM5_CounterMode_TypeDef TIM5_CounterMode)
  270. {
  271. uint8_t tmpcr1 = 0;
  272. /* Check the parameters */
  273. assert_param(IS_TIM5_COUNTER_MODE(TIM5_CounterMode));
  274. tmpcr1 = TIM5->CR1;
  275. /* Reset the CMS and DIR Bits */
  276. tmpcr1 &= (uint8_t)((uint8_t)(~TIM_CR1_CMS) & (uint8_t)(~TIM_CR1_DIR));
  277. /* Set the Counter Mode */
  278. tmpcr1 |= (uint8_t)TIM5_CounterMode;
  279. TIM5->CR1 = tmpcr1;
  280. }
  281. /**
  282. * @brief Sets the TIM5 Counter Register value.
  283. * @param Counter: Specifies the Counter register new value.
  284. * This parameter is between 0x0000 and 0xFFFF.
  285. * @retval None
  286. */
  287. void TIM5_SetCounter(uint16_t Counter)
  288. {
  289. /* Set the Counter Register value */
  290. TIM5->CNTRH = (uint8_t)(Counter >> 8);
  291. TIM5->CNTRL = (uint8_t)(Counter);
  292. }
  293. /**
  294. * @brief Sets the TIM5 Autoreload Register value.
  295. * @param Autoreload: Specifies the Autoreload register new value.
  296. * This parameter is between 0x0000 and 0xFFFF.
  297. * @retval None
  298. */
  299. void TIM5_SetAutoreload(uint16_t Autoreload)
  300. {
  301. /* Set the Autoreload Register value */
  302. TIM5->ARRH = (uint8_t)(Autoreload >> 8);
  303. TIM5->ARRL = (uint8_t)(Autoreload);
  304. }
  305. /**
  306. * @brief Gets the TIM5 Counter value.
  307. * @param None
  308. * @retval Counter Register value.
  309. */
  310. uint16_t TIM5_GetCounter(void)
  311. {
  312. uint16_t tmpcnt = 0;
  313. uint8_t tmpcntrl, tmpcntrh;
  314. tmpcntrh = TIM5->CNTRH;
  315. tmpcntrl = TIM5->CNTRL;
  316. tmpcnt = (uint16_t)(tmpcntrl);
  317. tmpcnt |= (uint16_t)((uint16_t)tmpcntrh << 8);
  318. /* Get the Counter Register value */
  319. return ((uint16_t)tmpcnt);
  320. }
  321. /**
  322. * @brief Gets the TIM5 Prescaler value.
  323. * @param None
  324. * @retval TIM5 Prescaler, it can be one of the following values:
  325. * - TIM5_Prescaler_1: Time base Prescaler = 1 (No effect)
  326. * - TIM5_Prescaler_2: Time base Prescaler = 2
  327. * - TIM5_Prescaler_4: Time base Prescaler = 4
  328. * - TIM5_Prescaler_8: Time base Prescaler = 8
  329. * - TIM5_Prescaler_16: Time base Prescaler = 16
  330. * - TIM5_Prescaler_32: Time base Prescaler = 32
  331. * - TIM5_Prescaler_64: Time base Prescaler = 64
  332. * - TIM5_Prescaler_128: Time base Prescaler = 128
  333. */
  334. TIM5_Prescaler_TypeDef TIM5_GetPrescaler(void)
  335. {
  336. /* Get the Prescaler Register value */
  337. return ((TIM5_Prescaler_TypeDef)TIM5->PSCR);
  338. }
  339. /**
  340. * @brief Enables or Disables the TIM5 Update event.
  341. * @param NewState: The new state of the TIM5 peripheral Preload register.
  342. * This parameter can be ENABLE or DISABLE
  343. * @retval None
  344. */
  345. void TIM5_UpdateDisableConfig(FunctionalState NewState)
  346. {
  347. /* Check the parameters */
  348. assert_param(IS_FUNCTIONAL_STATE(NewState));
  349. /* Set or Reset the UDIS Bit */
  350. if (NewState != DISABLE)
  351. {
  352. TIM5->CR1 |= TIM_CR1_UDIS;
  353. }
  354. else
  355. {
  356. TIM5->CR1 &= (uint8_t)(~TIM_CR1_UDIS);
  357. }
  358. }
  359. /**
  360. * @brief Selects the TIM5 Update Request Interrupt source.
  361. * @param TIM5_UpdateSource: Specifies the Update source.
  362. * This parameter can be one of the following values:
  363. * @arg TIM5_UpdateSource_Global: Global Update request source
  364. * @arg TIM5_UpdateSource_Regular: Regular Update request source
  365. * @retval None
  366. */
  367. void TIM5_UpdateRequestConfig(TIM5_UpdateSource_TypeDef TIM5_UpdateSource)
  368. {
  369. /* Check the parameters */
  370. assert_param(IS_TIM5_UPDATE_SOURCE(TIM5_UpdateSource));
  371. /* Set or Reset the URS Bit */
  372. if (TIM5_UpdateSource == TIM5_UpdateSource_Regular)
  373. {
  374. TIM5->CR1 |= TIM_CR1_URS ;
  375. }
  376. else
  377. {
  378. TIM5->CR1 &= (uint8_t)(~TIM_CR1_URS);
  379. }
  380. }
  381. /**
  382. * @brief Enables or disables TIM5 peripheral Preload register on ARR.
  383. * @param NewState: The new state of the TIM5 peripheral Preload register.
  384. * This parameter can be ENABLE or DISABLE
  385. * @retval None
  386. */
  387. void TIM5_ARRPreloadConfig(FunctionalState NewState)
  388. {
  389. /* Check the parameters */
  390. assert_param(IS_FUNCTIONAL_STATE(NewState));
  391. /* Set or Reset the ARPE Bit */
  392. if (NewState != DISABLE)
  393. {
  394. TIM5->CR1 |= TIM_CR1_ARPE;
  395. }
  396. else
  397. {
  398. TIM5->CR1 &= (uint8_t)(~TIM_CR1_ARPE);
  399. }
  400. }
  401. /**
  402. * @brief Selects the TIM’s One Pulse Mode.
  403. * @param TIM5_OPMode: Specifies the OPM Mode to be used.
  404. * This parameter can be one of the following values:
  405. * @arg TIM5_OPMode_Single: Single one Pulse mode (OPM Active)
  406. * @arg TIM5_OPMode_Repetitive: Single one Pulse mode (OPM Active)
  407. * @retval None
  408. */
  409. void TIM5_SelectOnePulseMode(TIM5_OPMode_TypeDef TIM5_OPMode)
  410. {
  411. /* Check the parameters */
  412. assert_param(IS_TIM5_OPM_MODE(TIM5_OPMode));
  413. /* Set or Reset the OPM Bit */
  414. if (TIM5_OPMode == TIM5_OPMode_Single)
  415. {
  416. TIM5->CR1 |= TIM_CR1_OPM ;
  417. }
  418. else
  419. {
  420. TIM5->CR1 &= (uint8_t)(~TIM_CR1_OPM);
  421. }
  422. }
  423. /**
  424. * @brief Enables or disables the TIM5 peripheral.
  425. * @param NewState: The new state of the TIM5 peripheral.
  426. * This parameter can be ENABLE or DISABLE
  427. * @retval None
  428. */
  429. void TIM5_Cmd(FunctionalState NewState)
  430. {
  431. /* Check the parameters */
  432. assert_param(IS_FUNCTIONAL_STATE(NewState));
  433. /* set or Reset the CEN Bit */
  434. if (NewState != DISABLE)
  435. {
  436. TIM5->CR1 |= TIM_CR1_CEN;
  437. }
  438. else
  439. {
  440. TIM5->CR1 &= (uint8_t)(~TIM_CR1_CEN);
  441. }
  442. }
  443. /**
  444. * @}
  445. */
  446. /** @defgroup TIM5_Group2 Output Compare management functions
  447. * @brief Output Compare management functions
  448. *
  449. @verbatim
  450. ===============================================================================
  451. Output Compare management functions
  452. ===============================================================================
  453. ===================================================================
  454. TIM5 Driver: how to use it in Output Compare Mode
  455. ===================================================================
  456. To use the Timer in Output Compare mode, the following steps are mandatory:
  457. 1. Enable TIM5 clock using CLK_PeripheralClockConfig(CLK_Peripheral_TIM5, ENABLE) function.
  458. 2. Configure the TIM5 pins in output mode by configuring the corresponding GPIO pins
  459. 3. Configure the Time base unit as described in the first part of this driver, if needed,
  460. otherwise the Timer will run with the default configuration:
  461. - Autoreload value = 0xFFFF
  462. - Prescaler value = 0x0000
  463. - Counter mode = Up counting
  464. 4. Call TIM5_OCxInit() to configure the channel x with the desired parameters
  465. including:
  466. - TIM5 Output Compare mode: TIM5_OCMode
  467. - TIM5 Output State: TIM5_OutputState
  468. - TIM5 Pulse value: TIM5_Pulse
  469. - TIM5 Output Compare Polarity: TIM5_OCPolarity
  470. - TIM5 Output Idle State: TIM5_OCIdleState
  471. 5. Call the TIM5_Cmd(ENABLE) function to enable the TIM5 counter.
  472. Note1: All other functions can be used separately to modify, if needed,
  473. a specific feature of the Timer.
  474. Note2: If the corresponding interrupt or DMA request are needed, the user should:
  475. 1. Enable global interrupts (or the DMA) to use the TIM5 interrupts (or DMA requests).
  476. 2. Enable the corresponding interrupt (or DMA request) using the function
  477. TIM5_ITConfig(TIM5_IT_CCx) (or TIM5_DMACmd(TIM5_DMASource_CCx))
  478. @endverbatim
  479. * @{
  480. */
  481. /**
  482. * @brief Initializes the TIM5 Channel1 according to the specified parameters.
  483. * @param TIM5_OCMode: Output Compare Mode
  484. * This parameter can be one of the following values:
  485. * @arg TIM5_OCMode_Timing: Timing (Frozen) Mode
  486. * @arg TIM5_OCMode_Active: Active Mode
  487. * @arg TIM5_OCMode_Inactive: Inactive Mode
  488. * @arg TIM5_OCMode_Toggle: Toggle Mode
  489. * @arg TIM5_OCMode_PWM1: PWM Mode 1
  490. * @arg TIM5_OCMode_PWM2: PWM Mode 2
  491. * @param TIM5_OutputState: Output state
  492. * This parameter can be one of the following values:
  493. * @arg TIM5_OutputState_Disable: Output compare State disabled (channel output disabled)
  494. * @arg TIM5_OutputState_Enable: Output compare State enabled (channel output enabled)
  495. * @param TIM5_Pulse: This parameter must be a value between 0x0000 and 0xFFFF.
  496. * @param TIM5_OCPolarity: Polarity
  497. * This parameter can be one of the following values:
  498. * @arg TIM5_OCPolarity_High: Output compare polarity = High
  499. * @arg TIM5_OCPolarity_Low: Output compare polarity = Low
  500. * @param TIM5_OCIdleState: Output Compare Idle State
  501. * This parameter can be one of the following values:
  502. * @arg TIM5_OCIdleState_Reset: Output Compare Idle state = Reset
  503. * @arg TIM5_OCIdleState_Set: Output Compare Idle state = Set
  504. * @retval None
  505. */
  506. void TIM5_OC1Init(TIM5_OCMode_TypeDef TIM5_OCMode,
  507. TIM5_OutputState_TypeDef TIM5_OutputState,
  508. uint16_t TIM5_Pulse,
  509. TIM5_OCPolarity_TypeDef TIM5_OCPolarity,
  510. TIM5_OCIdleState_TypeDef TIM5_OCIdleState)
  511. {
  512. uint8_t tmpccmr1 = 0;
  513. /* Check the parameters */
  514. assert_param(IS_TIM5_OC_MODE(TIM5_OCMode));
  515. assert_param(IS_TIM5_OUTPUT_STATE(TIM5_OutputState));
  516. assert_param(IS_TIM5_OC_POLARITY(TIM5_OCPolarity));
  517. assert_param(IS_TIM5_OCIDLE_STATE(TIM5_OCIdleState));
  518. tmpccmr1 = TIM5->CCMR1;
  519. /* Disable the Channel 1: Reset the CCE Bit */
  520. TIM5->CCER1 &= (uint8_t)(~TIM_CCER1_CC1E);
  521. /* Reset the Output Compare Bits */
  522. tmpccmr1 &= (uint8_t)(~TIM_CCMR_OCM);
  523. /* Set the Output Compare Mode */
  524. tmpccmr1 |= (uint8_t)TIM5_OCMode;
  525. TIM5->CCMR1 = tmpccmr1;
  526. /* Set the Output State */
  527. if (TIM5_OutputState == TIM5_OutputState_Enable)
  528. {
  529. TIM5->CCER1 |= TIM_CCER1_CC1E;
  530. }
  531. else
  532. {
  533. TIM5->CCER1 &= (uint8_t)(~TIM_CCER1_CC1E);
  534. }
  535. /* Set the Output Polarity */
  536. if (TIM5_OCPolarity == TIM5_OCPolarity_Low)
  537. {
  538. TIM5->CCER1 |= TIM_CCER1_CC1P;
  539. }
  540. else
  541. {
  542. TIM5->CCER1 &= (uint8_t)(~TIM_CCER1_CC1P);
  543. }
  544. /* Set the Output Idle state */
  545. if (TIM5_OCIdleState == TIM5_OCIdleState_Set)
  546. {
  547. TIM5->OISR |= TIM_OISR_OIS1;
  548. }
  549. else
  550. {
  551. TIM5->OISR &= (uint8_t)(~TIM_OISR_OIS1);
  552. }
  553. /* Set the Pulse value */
  554. TIM5->CCR1H = (uint8_t)(TIM5_Pulse >> 8);
  555. TIM5->CCR1L = (uint8_t)(TIM5_Pulse);
  556. }
  557. /**
  558. * @brief Initializes the TIM5 Channel2 according to the specified parameters.
  559. * @param TIM5_OCMode: Output Compare Mode
  560. * This parameter can be one of the following values:
  561. * @arg TIM5_OCMode_Timing: Timing (Frozen) Mode
  562. * @arg TIM5_OCMode_Active: Active Mode
  563. * @arg TIM5_OCMode_Inactive: Inactive Mode
  564. * @arg TIM5_OCMode_Toggle: Toggle Mode
  565. * @arg TIM5_OCMode_PWM1: PWM Mode 1
  566. * @arg TIM5_OCMode_PWM2: PWM Mode 2
  567. * @param TIM5_OutputState: Output state
  568. * This parameter can be one of the following values:
  569. * @arg TIM5_OutputState_Disable: Output compare State disabled (channel output disabled)
  570. * @arg TIM5_OutputState_Enable: Output compare State enabled (channel output enabled)
  571. * @param TIM5_Pulse: This parameter must be a value between 0x0000 and 0xFFFF.
  572. * @param TIM5_OCPolarity: Polarity
  573. * This parameter can be one of the following values:
  574. * @arg TIM5_OCPolarity_High: Output compare polarity = High
  575. * @arg TIM5_OCPolarity_Low: Output compare polarity = Low
  576. * @param TIM5_OCIdleState: Output Compare Idle State
  577. * This parameter can be one of the following values:
  578. * @arg TIM5_OCIdleState_Reset: Output Compare Idle state = Reset
  579. * @arg TIM5_OCIdleState_Set: Output Compare Idle state = Set
  580. * @retval None
  581. */
  582. void TIM5_OC2Init(TIM5_OCMode_TypeDef TIM5_OCMode,
  583. TIM5_OutputState_TypeDef TIM5_OutputState,
  584. uint16_t TIM5_Pulse,
  585. TIM5_OCPolarity_TypeDef TIM5_OCPolarity,
  586. TIM5_OCIdleState_TypeDef TIM5_OCIdleState)
  587. {
  588. uint8_t tmpccmr2 = 0;
  589. /* Check the parameters */
  590. assert_param(IS_TIM5_OC_MODE(TIM5_OCMode));
  591. assert_param(IS_TIM5_OUTPUT_STATE(TIM5_OutputState));
  592. assert_param(IS_TIM5_OC_POLARITY(TIM5_OCPolarity));
  593. assert_param(IS_TIM5_OCIDLE_STATE(TIM5_OCIdleState));
  594. tmpccmr2 = TIM5->CCMR2;
  595. /* Disable the Channel 2: Reset the CCE Bit */
  596. TIM5->CCER1 &= (uint8_t)(~TIM_CCER1_CC2E);
  597. /* Reset the Output Compare Bits */
  598. tmpccmr2 &= (uint8_t)(~TIM_CCMR_OCM);
  599. /* Set the Output Compare Mode */
  600. tmpccmr2 |= (uint8_t)TIM5_OCMode;
  601. TIM5->CCMR2 = tmpccmr2;
  602. /* Set the Output State */
  603. if (TIM5_OutputState == TIM5_OutputState_Enable)
  604. {
  605. TIM5->CCER1 |= TIM_CCER1_CC2E;
  606. }
  607. else
  608. {
  609. TIM5->CCER1 &= (uint8_t)(~TIM_CCER1_CC2E);
  610. }
  611. /* Set the Output Polarity */
  612. if (TIM5_OCPolarity == TIM5_OCPolarity_Low)
  613. {
  614. TIM5->CCER1 |= TIM_CCER1_CC2P;
  615. }
  616. else
  617. {
  618. TIM5->CCER1 &= (uint8_t)(~TIM_CCER1_CC2P);
  619. }
  620. /* Set the Output Idle state */
  621. if (TIM5_OCIdleState == TIM5_OCIdleState_Set)
  622. {
  623. TIM5->OISR |= TIM_OISR_OIS2;
  624. }
  625. else
  626. {
  627. TIM5->OISR &= (uint8_t)(~TIM_OISR_OIS2);
  628. }
  629. /* Set the Pulse value */
  630. TIM5->CCR2H = (uint8_t)(TIM5_Pulse >> 8);
  631. TIM5->CCR2L = (uint8_t)(TIM5_Pulse);
  632. }
  633. /**
  634. * @brief Configures the Break feature, dead time, Lock level, the OSSI,
  635. * and the AOE(automatic output enable).
  636. * @param TIM5_OSSIState: Off-State Selection for Idle mode states.
  637. * This parameter can be one of the following values:
  638. * @arg TIM5_OSSIState_Enable: Off-State Selection for Idle mode enabled
  639. * @arg TIM5_OSSIState_Disable: Off-State Selection for Idle mode disabled
  640. * @param TIM5_LockLevel: Lock level.
  641. * This parameter can be one of the following values:
  642. * @arg TIM5_LockLevel_Off: Lock option disabled
  643. * @arg TIM5_LockLevel_1: Select Lock Level 1
  644. * @arg TIM5_LockLevel_2: Select Lock Level 2
  645. * @arg TIM5_LockLevel_3: Select Lock Level 3
  646. * @param TIM5_BreakState: Break Input enable/disable .
  647. * This parameter can be one of the following values:
  648. * @arg TIM5_BreakState_Disable: Break State disabled (break option disabled)
  649. * @arg TIM5_BreakState_Enable: Break State enabled (break option enabled)
  650. * @param TIM5_BreakPolarity: Break Polarity.
  651. * This parameter can be one of the following values:
  652. * @arg TIM5_BreakPolarity_High: if Break, channel polarity = High
  653. * @arg TIM5_BreakPolarity_Low: if Break, channel polarity = Low
  654. * @param TIM5_AutomaticOutput: TIM5 AOE Bit Set/Reset .
  655. * This parameter can be one of the following values:
  656. * @arg TIM5_AutomaticOutput_Enable: Automatic Output option enabled
  657. * @arg TIM5_AutomaticOutput_Disable: Automatic Output option disabled
  658. * @retval None
  659. */
  660. void TIM5_BKRConfig(TIM5_OSSIState_TypeDef TIM5_OSSIState,
  661. TIM5_LockLevel_TypeDef TIM5_LockLevel,
  662. TIM5_BreakState_TypeDef TIM5_BreakState,
  663. TIM5_BreakPolarity_TypeDef TIM5_BreakPolarity,
  664. TIM5_AutomaticOutput_TypeDef TIM5_AutomaticOutput)
  665. {
  666. /* Check the parameters */
  667. assert_param(IS_TIM5_OSSI_STATE(TIM5_OSSIState));
  668. assert_param(IS_TIM5_LOCK_LEVEL(TIM5_LockLevel));
  669. assert_param(IS_TIM5_BREAK_STATE(TIM5_BreakState));
  670. assert_param(IS_TIM5_BREAK_POLARITY(TIM5_BreakPolarity));
  671. assert_param(IS_TIM5_AUTOMATIC_OUTPUT_STATE(TIM5_AutomaticOutput));
  672. /* Set the Lock level, the Break enable Bit and the Polarity, the OSSI State,
  673. the dead time value and the Automatic Output Enable Bit */
  674. TIM5->BKR = (uint8_t)((uint8_t)((uint8_t)((uint8_t)((uint8_t)TIM5_OSSIState | (uint8_t)TIM5_LockLevel) | \
  675. (uint8_t)((uint8_t)TIM5_BreakState | (uint8_t)TIM5_BreakPolarity)) | \
  676. TIM5_AutomaticOutput));
  677. }
  678. /**
  679. * @brief Enables or disables the TIM5 peripheral Main Outputs.
  680. * @param NewState: The new state of the TIM5 peripheral.
  681. * This parameter can be ENABLE or DISABLE
  682. * @retval None
  683. */
  684. void TIM5_CtrlPWMOutputs(FunctionalState NewState)
  685. {
  686. /* Check the parameters */
  687. assert_param(IS_FUNCTIONAL_STATE(NewState));
  688. /* Set or Reset the MOE Bit */
  689. if (NewState != DISABLE)
  690. {
  691. TIM5->BKR |= TIM_BKR_MOE ;
  692. }
  693. else
  694. {
  695. TIM5->BKR &= (uint8_t)(~TIM_BKR_MOE) ;
  696. }
  697. }
  698. /**
  699. * @brief Selects the TIM5 Output Compare Mode. This function disables the
  700. * selected channel before changing the Output Compare Mode. User has to
  701. * enable this channel using TIM5_CCxCmd and TIM5_CCxNCmd functions.
  702. * @param TIM5_Channel: Specifies the TIM5 Channel.
  703. * This parameter can be one of the following values:
  704. * @arg TIM5_Channel_1: Channel 1
  705. * @arg TIM5_Channel_2: Channel 2
  706. * @param TIM5_OCMode: Specifies the TIM5 Output Compare Mode.
  707. * This parameter can be one of the following values:
  708. * @arg TIM5_OCMode_Timing: Timing (Frozen) Mode
  709. * @arg TIM5_OCMode_Active: Active Mode
  710. * @arg TIM5_OCMode_Inactive: Inactive Mode
  711. * @arg TIM5_OCMode_Toggle: Toggle Mode
  712. * @arg TIM5_OCMode_PWM1: PWM Mode 1
  713. * @arg TIM5_OCMode_PWM2: PWM Mode 2
  714. * @retval None
  715. */
  716. void TIM5_SelectOCxM(TIM5_Channel_TypeDef TIM5_Channel,
  717. TIM5_OCMode_TypeDef TIM5_OCMode)
  718. {
  719. /* Check the parameters */
  720. assert_param(IS_TIM5_CHANNEL(TIM5_Channel));
  721. assert_param(IS_TIM5_OCM(TIM5_OCMode));
  722. if (TIM5_Channel == TIM5_Channel_1)
  723. {
  724. /* Disable the Channel 1: Reset the CCE Bit */
  725. TIM5->CCER1 &= (uint8_t)(~TIM_CCER1_CC1E);
  726. /* Reset the Output Compare Bits */
  727. TIM5->CCMR1 &= (uint8_t)(~TIM_CCMR_OCM);
  728. /* Set the Output Compare Mode */
  729. TIM5->CCMR1 |= (uint8_t)TIM5_OCMode;
  730. }
  731. else /* if (TIM5_Channel == TIM5_Channel_2) */
  732. {
  733. /* Disable the Channel 2: Reset the CCE Bit */
  734. TIM5->CCER1 &= (uint8_t)(~TIM_CCER1_CC2E);
  735. /* Reset the Output Compare Bits */
  736. TIM5->CCMR2 &= (uint8_t)(~TIM_CCMR_OCM);
  737. /* Set the Output Compare Mode */
  738. TIM5->CCMR2 |= (uint8_t)TIM5_OCMode;
  739. }
  740. }
  741. /**
  742. * @brief Sets the TIM5 Capture Compare1 Register value.
  743. * @param Compare: Specifies the Capture Compare1 register new value.
  744. * This parameter is between 0x0000 and 0xFFFF.
  745. * @retval None
  746. */
  747. void TIM5_SetCompare1(uint16_t Compare)
  748. {
  749. /* Set the Capture Compare1 Register value */
  750. TIM5->CCR1H = (uint8_t)(Compare >> 8);
  751. TIM5->CCR1L = (uint8_t)(Compare);
  752. }
  753. /**
  754. * @brief Sets the TIM5 Capture Compare2 Register value.
  755. * @param Compare: Specifies the Capture Compare2 register new value.
  756. * This parameter is between 0x0000 and 0xFFFF.
  757. * @retval None
  758. */
  759. void TIM5_SetCompare2(uint16_t Compare)
  760. {
  761. /* Set the Capture Compare2 Register value */
  762. TIM5->CCR2H = (uint8_t)(Compare >> 8);
  763. TIM5->CCR2L = (uint8_t)(Compare);
  764. }
  765. /**
  766. * @brief Forces the TIM5 Channel1 output waveform to active or inactive level.
  767. * @param TIM5_ForcedAction: Specifies the forced Action to be set to the output waveform.
  768. * This parameter can be one of the following values:
  769. * @arg TIM5_ForcedAction_Active: Output Reference is forced low
  770. * @arg TIM5_ForcedAction_Inactive: Output Reference is forced high
  771. * @retval None
  772. */
  773. void TIM5_ForcedOC1Config(TIM5_ForcedAction_TypeDef TIM5_ForcedAction)
  774. {
  775. uint8_t tmpccmr1 = 0;
  776. /* Check the parameters */
  777. assert_param(IS_TIM5_FORCED_ACTION(TIM5_ForcedAction));
  778. tmpccmr1 = TIM5->CCMR1;
  779. /* Reset the OCM Bits */
  780. tmpccmr1 &= (uint8_t)(~TIM_CCMR_OCM);
  781. /* Configure The Forced output Mode */
  782. tmpccmr1 |= (uint8_t)TIM5_ForcedAction;
  783. TIM5->CCMR1 = tmpccmr1;
  784. }
  785. /**
  786. * @brief Forces the TIM5 Channel2 output waveform to active or inactive level.
  787. * @param TIM5_ForcedAction: Specifies the forced Action to be set to the output waveform.
  788. * This parameter can be one of the following values:
  789. * @arg TIM5_ForcedAction_Active: Output Reference is forced low
  790. * @arg TIM5_ForcedAction_Inactive: Output Reference is forced high
  791. * @retval None
  792. */
  793. void TIM5_ForcedOC2Config(TIM5_ForcedAction_TypeDef TIM5_ForcedAction)
  794. {
  795. uint8_t tmpccmr2 = 0;
  796. /* Check the parameters */
  797. assert_param(IS_TIM5_FORCED_ACTION(TIM5_ForcedAction));
  798. tmpccmr2 = TIM5->CCMR2;
  799. /* Reset the OCM Bits */
  800. tmpccmr2 &= (uint8_t)(~TIM_CCMR_OCM);
  801. /* Configure The Forced output Mode */
  802. tmpccmr2 |= (uint8_t)TIM5_ForcedAction;
  803. TIM5->CCMR2 = tmpccmr2;
  804. }
  805. /**
  806. * @brief Enables or disables the TIM5 peripheral Preload Register on CCR1.
  807. * @param NewState: The new state of the Capture Compare Preload register.
  808. * This parameter can be ENABLE or DISABLE
  809. * @retval None
  810. */
  811. void TIM5_OC1PreloadConfig(FunctionalState NewState)
  812. {
  813. /* Check the parameters */
  814. assert_param(IS_FUNCTIONAL_STATE(NewState));
  815. /* Set or Reset the OC1PE Bit */
  816. if (NewState != DISABLE)
  817. {
  818. TIM5->CCMR1 |= TIM_CCMR_OCxPE ;
  819. }
  820. else
  821. {
  822. TIM5->CCMR1 &= (uint8_t)(~TIM_CCMR_OCxPE) ;
  823. }
  824. }
  825. /**
  826. * @brief Enables or disables the TIM5 peripheral Preload Register on CCR2.
  827. * @param NewState: The new state of the Capture Compare Preload register.
  828. * This parameter can be ENABLE or DISABLE
  829. * @retval None
  830. */
  831. void TIM5_OC2PreloadConfig(FunctionalState NewState)
  832. {
  833. /* Check the parameters */
  834. assert_param(IS_FUNCTIONAL_STATE(NewState));
  835. /* Set or Reset the OC2PE Bit */
  836. if (NewState != DISABLE)
  837. {
  838. TIM5->CCMR2 |= TIM_CCMR_OCxPE ;
  839. }
  840. else
  841. {
  842. TIM5->CCMR2 &= (uint8_t)(~TIM_CCMR_OCxPE) ;
  843. }
  844. }
  845. /**
  846. * @brief Configures the TIM5 Capture Compare 1 Fast feature.
  847. * @param NewState: The new state of the Output Compare Fast Enable bit.
  848. * This parameter can be ENABLE or DISABLE
  849. * @retval None
  850. */
  851. void TIM5_OC1FastConfig(FunctionalState NewState)
  852. {
  853. /* Check the parameters */
  854. assert_param(IS_FUNCTIONAL_STATE(NewState));
  855. /* Set or Reset the OC1FE Bit */
  856. if (NewState != DISABLE)
  857. {
  858. TIM5->CCMR1 |= TIM_CCMR_OCxFE ;
  859. }
  860. else
  861. {
  862. TIM5->CCMR1 &= (uint8_t)(~TIM_CCMR_OCxFE) ;
  863. }
  864. }
  865. /**
  866. * @brief Configures the TIM5 Capture Compare 2 Fast feature.
  867. * @param NewState: The new state of the Output Compare Fast Enable bit.
  868. * This parameter can be ENABLE or DISABLE
  869. * @retval None
  870. */
  871. void TIM5_OC2FastConfig(FunctionalState NewState)
  872. {
  873. /* Check the parameters */
  874. assert_param(IS_FUNCTIONAL_STATE(NewState));
  875. /* Set or Reset the OC2FE Bit */
  876. if (NewState != DISABLE)
  877. {
  878. TIM5->CCMR2 |= TIM_CCMR_OCxFE ;
  879. }
  880. else
  881. {
  882. TIM5->CCMR2 &= (uint8_t)(~TIM_CCMR_OCxFE) ;
  883. }
  884. }
  885. /**
  886. * @brief Configures the TIM5 Channel 1 polarity.
  887. * @param TIM5_OCPolarity: Specifies the OC1 Polarity.
  888. * This parameter can be one of the following values:
  889. * @arg TIM5_OCPolarity_High: Output compare polarity = High
  890. * @arg TIM5_OCPolarity_Low: Output compare polarity = Low
  891. * @retval None
  892. */
  893. void TIM5_OC1PolarityConfig(TIM5_OCPolarity_TypeDef TIM5_OCPolarity)
  894. {
  895. /* Check the parameters */
  896. assert_param(IS_TIM5_OC_POLARITY(TIM5_OCPolarity));
  897. /* Set or Reset the CC1P Bit */
  898. if (TIM5_OCPolarity == TIM5_OCPolarity_Low)
  899. {
  900. TIM5->CCER1 |= TIM_CCER1_CC1P ;
  901. }
  902. else
  903. {
  904. TIM5->CCER1 &= (uint8_t)(~TIM_CCER1_CC1P) ;
  905. }
  906. }
  907. /**
  908. * @brief Configures the TIM5 Channel 2 polarity.
  909. * @param TIM5_OCPolarity: Specifies the OC2 Polarity.
  910. * This parameter can be one of the following values:
  911. * @arg TIM5_OCPolarity_High: Output compare polarity = High
  912. * @arg TIM5_OCPolarity_Low: Output compare polarity = Low
  913. * @retval None
  914. */
  915. void TIM5_OC2PolarityConfig(TIM5_OCPolarity_TypeDef TIM5_OCPolarity)
  916. {
  917. /* Check the parameters */
  918. assert_param(IS_TIM5_OC_POLARITY(TIM5_OCPolarity));
  919. /* Set or Reset the CC2P Bit */
  920. if (TIM5_OCPolarity == TIM5_OCPolarity_Low)
  921. {
  922. TIM5->CCER1 |= TIM_CCER1_CC2P ;
  923. }
  924. else
  925. {
  926. TIM5->CCER1 &= (uint8_t)(~TIM_CCER1_CC2P) ;
  927. }
  928. }
  929. /**
  930. * @brief Enables or disables the TIM5 Capture Compare Channel x.
  931. * @param TIM5_Channel: Specifies the TIM5 Channel.
  932. * This parameter can be one of the following values:
  933. * @arg TIM5_Channel_1: Channel 1
  934. * @arg TIM5_Channel_2: Channel 2
  935. * @param NewState: Specifies the TIM5 Channel CCxE bit new state.
  936. * This parameter can be ENABLE or DISABLE
  937. * @retval None
  938. */
  939. void TIM5_CCxCmd(TIM5_Channel_TypeDef TIM5_Channel,
  940. FunctionalState NewState)
  941. {
  942. /* Check the parameters */
  943. assert_param(IS_TIM5_CHANNEL(TIM5_Channel));
  944. assert_param(IS_FUNCTIONAL_STATE(NewState));
  945. if (TIM5_Channel == TIM5_Channel_1)
  946. {
  947. /* Set or Reset the CC1E Bit */
  948. if (NewState != DISABLE)
  949. {
  950. TIM5->CCER1 |= TIM_CCER1_CC1E ;
  951. }
  952. else
  953. {
  954. TIM5->CCER1 &= (uint8_t)(~TIM_CCER1_CC1E) ;
  955. }
  956. }
  957. else /* if (TIM5_Channel == TIM5_Channel_2) */
  958. {
  959. /* Set or Reset the CC2E Bit */
  960. if (NewState != DISABLE)
  961. {
  962. TIM5->CCER1 |= TIM_CCER1_CC2E;
  963. }
  964. else
  965. {
  966. TIM5->CCER1 &= (uint8_t)(~TIM_CCER1_CC2E) ;
  967. }
  968. }
  969. }
  970. /** @defgroup TIM5_Group3 Input Capture management functions
  971. * @brief Input Capture management functions
  972. *
  973. @verbatim
  974. ===============================================================================
  975. Input Capture management functions
  976. ===============================================================================
  977. ===================================================================
  978. TIM5 Driver: how to use it in Input Capture Mode
  979. ===================================================================
  980. To use the Timer in Input Capture mode, the following steps are mandatory:
  981. 1. Enable TIM5 clock using CLK_PeripheralClockConfig(CLK_Peripheral_TIM5, ENABLE) function.
  982. 2. Configure the TIM5 pins in input mode by configuring the corresponding GPIO pins
  983. 3. Configure the Time base unit as described in the first part of this driver, if needed,
  984. otherwise the Timer will run with the default configuration:
  985. - Autoreload value = 0xFFFF
  986. - Prescaler value = 0x0
  987. - Counter mode = Up counting
  988. 4. Call TIM5_ICInit() to configure the desired channel to measure only
  989. frequency or duty cycle of the input signal using the corresponding configuration:
  990. - TIM5 Channel: TIM5_Channel
  991. - TIM5 Input Capture polarity: TIM5_ICPolarity
  992. - TIM5 Input Capture selection: TIM5_ICSelection
  993. - TIM5 Input Capture Prescaler: TIM5_ICPSC
  994. - TIM5 Input Capture filter value
  995. or,
  996. Call TIM5_PWMIConfig() to configure the desired channels with the
  997. corresponding configuration and to measure the frequency and the duty
  998. cycle of the input signal.
  999. 5. Enable global interrupts or the DMA to read the measured frequency.
  1000. 6. Enable the corresponding interrupt (or DMA request) to read the captured value,
  1001. using the function TIM5_ITConfig(TIM5_IT_CCx) (or TIM5_DMACmd(TIM5_DMASource_CCx))
  1002. 7. Call the TIM5_Cmd(ENABLE) function to enable the TIM5 counter.
  1003. 8. Use TIM5_GetCapturex() to read the captured value corresponding to
  1004. channel x.
  1005. Note1: All other functions can be used separately to modify, if needed,
  1006. a specific feature of the Timer.
  1007. @endverbatim
  1008. * @{
  1009. */
  1010. /**
  1011. * @brief Initializes the TIM5 peripheral according to the specified parameters.
  1012. * @param TIM5_Channel: TIM5 Channel
  1013. * This parameter can be one of the following values:
  1014. * @arg TIM5_Channel_1: Channel 1
  1015. * @arg TIM5_Channel_2: Channel 2
  1016. * @param TIM5_ICPolarity: Input Capture Polarity
  1017. * This parameter can be one of the following values:
  1018. * @arg TIM5_ICPolarity_Rising: Input Capture on Rising Edge
  1019. * @arg TIM5_ICPolarity_Falling: Input Capture on Falling Edge
  1020. * @param TIM5_ICSelection: Input Capture Selection
  1021. * This parameter can be one of the following values:
  1022. * @arg TIM5_ICSelection_DirectTI: Input Capture mapped on the direct input
  1023. * @arg TIM5_ICSelection_IndirectTI: Input Capture mapped on the indirect input
  1024. * @arg TIM5_ICSelection_TRGI: Input Capture mapped on the Trigger Input
  1025. * @param TIM5_ICPrescaler: Input Capture Prescaler
  1026. * This parameter can be one of the following values:
  1027. * @arg TIM5_ICPSC_DIV1: Input Capture Prescaler = 1 (one capture every 1 event)
  1028. * @arg TIM5_ICPSC_DIV2: Input Capture Prescaler = 2 (one capture every 2 events)
  1029. * @arg TIM5_ICPSC_DIV4: Input Capture Prescaler = 4 (one capture every 4 events)
  1030. * @arg TIM5_ICPSC_DIV8: Input Capture Prescaler = 8 (one capture every 8 events)
  1031. * @param TIM5_ICFilter: This parameter must be a value between 0x00 and 0x0F.
  1032. * @retval None
  1033. */
  1034. void TIM5_ICInit(TIM5_Channel_TypeDef TIM5_Channel,
  1035. TIM5_ICPolarity_TypeDef TIM5_ICPolarity,
  1036. TIM5_ICSelection_TypeDef TIM5_ICSelection,
  1037. TIM5_ICPSC_TypeDef TIM5_ICPrescaler,
  1038. uint8_t TIM5_ICFilter)
  1039. {
  1040. /* Check the parameters */
  1041. assert_param(IS_TIM5_CHANNEL(TIM5_Channel));
  1042. if (TIM5_Channel == TIM5_Channel_1)
  1043. {
  1044. /* TI1 Configuration */
  1045. TI1_Config(TIM5_ICPolarity, TIM5_ICSelection, TIM5_ICFilter);
  1046. /* Set the Input Capture Prescaler value */
  1047. TIM5_SetIC1Prescaler(TIM5_ICPrescaler);
  1048. }
  1049. else /* if (TIM5_Channel == TIM5_Channel_2) */
  1050. {
  1051. /* TI2 Configuration */
  1052. TI2_Config(TIM5_ICPolarity, TIM5_ICSelection, TIM5_ICFilter);
  1053. /* Set the Input Capture Prescaler value */
  1054. TIM5_SetIC2Prescaler(TIM5_ICPrescaler);
  1055. }
  1056. }
  1057. /**
  1058. * @brief Configures the TIM5 peripheral in PWM Input Mode according to the
  1059. * specified parameters.
  1060. * @param TIM5_Channel: TIM5 Channel
  1061. * This parameter can be one of the following values:
  1062. * @arg TIM5_Channel_1: Channel 1
  1063. * @arg TIM5_Channel_2: Channel 2
  1064. * @param TIM5_ICPolarity: Input Capture Polarity
  1065. * This parameter can be one of the following values:
  1066. * @arg TIM5_ICPolarity_Rising: Input Capture on Rising Edge
  1067. * @arg TIM5_ICPolarity_Falling: Input Capture on Falling Edge
  1068. * @param TIM5_ICSelection: Input Capture Selection
  1069. * This parameter can be one of the following values:
  1070. * @arg TIM5_ICSelection_DirectTI: Input Capture mapped on the direct input
  1071. * @arg TIM5_ICSelection_IndirectTI: Input Capture mapped on the indirect input
  1072. * @arg TIM5_ICSelection_TRGI: Input Capture mapped on the Trigger Input
  1073. * @param TIM5_ICPrescaler: Input Capture Prescaler
  1074. * This parameter can be one of the following values:
  1075. * @arg TIM5_ICPSC_DIV1: Input Capture Prescaler = 1 (one capture every 1 event)
  1076. * @arg TIM5_ICPSC_DIV2: Input Capture Prescaler = 2 (one capture every 2 events)
  1077. * @arg TIM5_ICPSC_DIV4: Input Capture Prescaler = 4 (one capture every 4 events)
  1078. * @arg TIM5_ICPSC_DIV8: Input Capture Prescaler = 8 (one capture every 8 events)
  1079. * @retval None
  1080. */
  1081. void TIM5_PWMIConfig(TIM5_Channel_TypeDef TIM5_Channel,
  1082. TIM5_ICPolarity_TypeDef TIM5_ICPolarity,
  1083. TIM5_ICSelection_TypeDef TIM5_ICSelection,
  1084. TIM5_ICPSC_TypeDef TIM5_ICPrescaler,
  1085. uint8_t TIM5_ICFilter)
  1086. {
  1087. uint8_t icpolarity = TIM5_ICPolarity_Rising;
  1088. uint8_t icselection = TIM5_ICSelection_DirectTI;
  1089. /* Check the parameters */
  1090. assert_param(IS_TIM5_CHANNEL(TIM5_Channel));
  1091. /* Select the Opposite Input Polarity */
  1092. if (TIM5_ICPolarity == TIM5_ICPolarity_Rising)
  1093. {
  1094. icpolarity = TIM5_ICPolarity_Falling;
  1095. }
  1096. else
  1097. {
  1098. icpolarity = TIM5_ICPolarity_Rising;
  1099. }
  1100. /* Select the Opposite Input */
  1101. if (TIM5_ICSelection == TIM5_ICSelection_DirectTI)
  1102. {
  1103. icselection = TIM5_ICSelection_IndirectTI;
  1104. }
  1105. else
  1106. {
  1107. icselection = TIM5_ICSelection_DirectTI;
  1108. }
  1109. if (TIM5_Channel == TIM5_Channel_1)
  1110. {
  1111. /* TI1 Configuration */
  1112. TI1_Config(TIM5_ICPolarity, TIM5_ICSelection,
  1113. TIM5_ICFilter);
  1114. /* Set the Input Capture Prescaler value */
  1115. TIM5_SetIC1Prescaler(TIM5_ICPrescaler);
  1116. /* TI2 Configuration */
  1117. TI2_Config((TIM5_ICPolarity_TypeDef)icpolarity, (TIM5_ICSelection_TypeDef)icselection, TIM5_ICFilter);
  1118. /* Set the Input Capture Prescaler value */
  1119. TIM5_SetIC2Prescaler(TIM5_ICPrescaler);
  1120. }
  1121. else
  1122. {
  1123. /* TI2 Configuration */
  1124. TI2_Config(TIM5_ICPolarity, TIM5_ICSelection,
  1125. TIM5_ICFilter);
  1126. /* Set the Input Capture Prescaler value */
  1127. TIM5_SetIC2Prescaler(TIM5_ICPrescaler);
  1128. /* TI1 Configuration */
  1129. TI1_Config((TIM5_ICPolarity_TypeDef)icpolarity, (TIM5_ICSelection_TypeDef)icselection, TIM5_ICFilter);
  1130. /* Set the Input Capture Prescaler value */
  1131. TIM5_SetIC1Prescaler(TIM5_ICPrescaler);
  1132. }
  1133. }
  1134. /**
  1135. * @brief Gets the TIM5 Input Capture 1 value.
  1136. * @param None
  1137. * @retval Capture Compare 1 Register value.
  1138. */
  1139. uint16_t TIM5_GetCapture1(void)
  1140. {
  1141. uint16_t tmpccr1 = 0;
  1142. uint8_t tmpccr1l, tmpccr1h;
  1143. tmpccr1h = TIM5->CCR1H;
  1144. tmpccr1l = TIM5->CCR1L;
  1145. tmpccr1 = (uint16_t)(tmpccr1l);
  1146. tmpccr1 |= (uint16_t)((uint16_t)tmpccr1h << 8);
  1147. /* Get the Capture 1 Register value */
  1148. return ((uint16_t)tmpccr1);
  1149. }
  1150. /**
  1151. * @brief Gets the TIM5 Input Capture 2 value.
  1152. * @param None
  1153. * @retval Capture Compare 2 Register value.
  1154. */
  1155. uint16_t TIM5_GetCapture2(void)
  1156. {
  1157. uint16_t tmpccr2 = 0;
  1158. uint8_t tmpccr2l, tmpccr2h;
  1159. tmpccr2h = TIM5->CCR2H;
  1160. tmpccr2l = TIM5->CCR2L;
  1161. tmpccr2 = (uint16_t)(tmpccr2l);
  1162. tmpccr2 |= (uint16_t)((uint16_t)tmpccr2h << 8);
  1163. /* Get the Capture 2 Register value */
  1164. return ((uint16_t)tmpccr2);
  1165. }
  1166. /**
  1167. * @brief Sets the TIM5 Input Capture 1 prescaler.
  1168. * @param TIM5_IC1Prescaler: Specifies the Input Capture prescaler new value
  1169. * This parameter can be one of the following values:
  1170. * @arg TIM5_ICPSC_DIV1: Input Capture Prescaler = 1 (one capture every 1 event)
  1171. * @arg TIM5_ICPSC_DIV2: Input Capture Prescaler = 2 (one capture every 2 events)
  1172. * @arg TIM5_ICPSC_DIV4: Input Capture Prescaler = 4 (one capture every 4 events)
  1173. * @arg TIM5_ICPSC_DIV8: Input Capture Prescaler = 8 (one capture every 8 events)
  1174. * @retval None
  1175. */
  1176. void TIM5_SetIC1Prescaler(TIM5_ICPSC_TypeDef TIM5_IC1Prescaler)
  1177. {
  1178. uint8_t tmpccmr1 = 0;
  1179. /* Check the parameters */
  1180. assert_param(IS_TIM5_IC_PRESCALER(TIM5_IC1Prescaler));
  1181. tmpccmr1 = TIM5->CCMR1;
  1182. /* Reset the IC1PSC Bits */
  1183. tmpccmr1 &= (uint8_t)(~TIM_CCMR_ICxPSC);
  1184. /* Set the IC1PSC value */
  1185. tmpccmr1 |= (uint8_t)TIM5_IC1Prescaler;
  1186. TIM5->CCMR1 = tmpccmr1;
  1187. }
  1188. /**
  1189. * @brief Sets the TIM5 Input Capture 2 prescaler.
  1190. * @param TIM5_IC2Prescaler: Specifies the Input Capture prescaler new value
  1191. * This parameter can be one of the following values:
  1192. * @arg TIM5_ICPSC_DIV1: Input Capture Prescaler = 1 (one capture every 1 event)
  1193. * @arg TIM5_ICPSC_DIV2: Input Capture Prescaler = 2 (one capture every 2 events)
  1194. * @arg TIM5_ICPSC_DIV4: Input Capture Prescaler = 4 (one capture every 4 events)
  1195. * @arg TIM5_ICPSC_DIV8: Input Capture Prescaler = 8 (one capture every 8 events)
  1196. * @retval None
  1197. */
  1198. void TIM5_SetIC2Prescaler(TIM5_ICPSC_TypeDef TIM5_IC2Prescaler)
  1199. {
  1200. uint8_t tmpccmr2 = 0;
  1201. /* Check the parameters */
  1202. assert_param(IS_TIM5_IC_PRESCALER(TIM5_IC2Prescaler));
  1203. tmpccmr2 = TIM5->CCMR2;
  1204. /* Reset the IC2PSC Bits */
  1205. tmpccmr2 &= (uint8_t)(~TIM_CCMR_ICxPSC);
  1206. /* Set the IC2PSC value */
  1207. tmpccmr2 |= (uint8_t)TIM5_IC2Prescaler;
  1208. TIM5->CCMR2 = tmpccmr2;
  1209. }
  1210. /**
  1211. * @}
  1212. */
  1213. /** @defgroup TIM5_Group4 Interrupts DMA and flags management functions
  1214. * @brief Interrupts, DMA and flags management functions
  1215. *
  1216. @verbatim
  1217. ===============================================================================
  1218. Interrupts, DMA and flags management functions
  1219. ===============================================================================
  1220. @endverbatim
  1221. * @{
  1222. */
  1223. /**
  1224. * @brief Enables or disables the specified TIM5 interrupts.
  1225. * @param TIM5_IT: Specifies the TIM5 interrupts sources to be enabled or disabled.
  1226. * This parameter can be any combination of the following values:
  1227. * @arg TIM5_IT_Update: Update
  1228. * @arg TIM5_IT_CC1: Capture Compare Channel1
  1229. * @arg TIM5_IT_CC2: Capture Compare Channel2
  1230. * @arg TIM5_IT_Trigger: Trigger
  1231. * @arg TIM5_IT_Break: Break
  1232. * @param NewState: The new state of the TIM5 peripheral.
  1233. * This parameter can be ENABLE or DISABLE
  1234. * @retval None
  1235. */
  1236. void TIM5_ITConfig(TIM5_IT_TypeDef TIM5_IT, FunctionalState NewState)
  1237. {
  1238. /* Check the parameters */
  1239. assert_param(IS_TIM5_IT(TIM5_IT));
  1240. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1241. if (NewState != DISABLE)
  1242. {
  1243. /* Enable the Interrupt sources */
  1244. TIM5->IER |= (uint8_t)TIM5_IT;
  1245. }
  1246. else
  1247. {
  1248. /* Disable the Interrupt sources */
  1249. TIM5->IER &= (uint8_t)(~(uint8_t)TIM5_IT);
  1250. }
  1251. }
  1252. /**
  1253. * @brief Configures the TIM5 event to be generated by software.
  1254. * @param TIM5_EventSource: Specifies the event source.
  1255. * This parameter can be any combination of the following values:
  1256. * @arg TIM5_EventSource_Update: Update
  1257. * @arg TIM5_EventSource_CC1: Capture Compare Channel1
  1258. * @arg TIM5_EventSource_CC2: Capture Compare Channel2
  1259. * @arg TIM5_EventSource_Trigger: Trigger
  1260. * @arg TIM5_EventSource_Break: Break
  1261. * @retval None
  1262. */
  1263. void TIM5_GenerateEvent(TIM5_EventSource_TypeDef TIM5_EventSource)
  1264. {
  1265. /* Check the parameters */
  1266. assert_param(IS_TIM5_EVENT_SOURCE((uint8_t)TIM5_EventSource));
  1267. /* Set the event sources */
  1268. TIM5->EGR |= (uint8_t)TIM5_EventSource;
  1269. }
  1270. /**
  1271. * @brief Checks whether the specified TIM5 flag is set or not.
  1272. * @param TIM5_FLAG: Specifies the flag to check.
  1273. * This parameter can be any combination of the following values:
  1274. * @arg TIM5_FLAG_Update: Update
  1275. * @arg TIM5_FLAG_CC1: Capture Compare Channel1
  1276. * @arg TIM5_FLAG_CC2: Capture Compare Channel2
  1277. * @arg TIM5_FLAG_Trigger: Trigger
  1278. * @arg TIM5_FLAG_Break: Break
  1279. * @arg TIM5_FLAG_CC1OF: Capture compare 1 over capture
  1280. * @arg TIM5_FLAG_CC2OF: Capture compare 2 over capture
  1281. * @retval FlagStatus: The new state of TIM5_FLAG (SET or RESET)
  1282. */
  1283. FlagStatus TIM5_GetFlagStatus(TIM5_FLAG_TypeDef TIM5_FLAG)
  1284. {
  1285. FlagStatus bitstatus = RESET;
  1286. uint8_t tim5_flag_l = 0, tim5_flag_h = 0;
  1287. /* Check the parameters */
  1288. assert_param(IS_TIM5_GET_FLAG(TIM5_FLAG));
  1289. tim5_flag_l = (uint8_t)(TIM5->SR1 & (uint8_t)(TIM5_FLAG));
  1290. tim5_flag_h = (uint8_t)(TIM5->SR2 & (uint8_t)((uint16_t)TIM5_FLAG >> 8));
  1291. if ((uint8_t)(tim5_flag_l | tim5_flag_h) != 0)
  1292. {
  1293. bitstatus = SET;
  1294. }
  1295. else
  1296. {
  1297. bitstatus = RESET;
  1298. }
  1299. return ((FlagStatus)bitstatus);
  1300. }
  1301. /**
  1302. * @brief Clears the TIM’s pending flags.
  1303. * @param TIM5_FLAG: Specifies the flag to clear.
  1304. * This parameter can be any combination of the following values:
  1305. * @arg TIM5_FLAG_Update: Update
  1306. * @arg TIM5_FLAG_CC1: Capture Compare Channel1
  1307. * @arg TIM5_FLAG_CC2: Capture Compare Channel2
  1308. * @arg TIM5_FLAG_Trigger: Trigger
  1309. * @arg TIM5_FLAG_Break: Break
  1310. * @retval None
  1311. */
  1312. void TIM5_ClearFlag(TIM5_FLAG_TypeDef TIM5_FLAG)
  1313. {
  1314. /* Check the parameters */
  1315. assert_param(IS_TIM5_CLEAR_FLAG((uint16_t)TIM5_FLAG));
  1316. /* Clear the flags (rc_w0) clear this bit by writing 0. Writing ‘1’ has no effect*/
  1317. TIM5->SR1 = (uint8_t)(~(uint8_t)(TIM5_FLAG));
  1318. TIM5->SR2 = (uint8_t)(~(uint8_t)((uint16_t)TIM5_FLAG >> 8));
  1319. }
  1320. /**
  1321. * @brief Checks whether the TIM5 interrupt has occurred or not.
  1322. * @param TIM5_IT: Specifies the TIM5 interrupt source to check.
  1323. * This parameter can be any combination of the following values:
  1324. * @arg TIM5_IT_Update: Update
  1325. * @arg TIM5_IT_CC1: Capture Compare Channel1
  1326. * @arg TIM5_IT_CC2: Capture Compare Channel2
  1327. * @arg TIM5_IT_Trigger: Trigger
  1328. * @arg TIM5_IT_Break: Break
  1329. * @retval ITStatus: The new state of the TIM5_IT (SET or RESET)
  1330. */
  1331. ITStatus TIM5_GetITStatus(TIM5_IT_TypeDef TIM5_IT)
  1332. {
  1333. ITStatus bitstatus = RESET;
  1334. uint8_t TIM5_itStatus = 0x0, TIM5_itEnable = 0x0;
  1335. /* Check the parameters */
  1336. assert_param(IS_TIM5_GET_IT(TIM5_IT));
  1337. TIM5_itStatus = (uint8_t)(TIM5->SR1 & (uint8_t)TIM5_IT);
  1338. TIM5_itEnable = (uint8_t)(TIM5->IER & (uint8_t)TIM5_IT);
  1339. if ((TIM5_itStatus != (uint8_t)RESET ) && (TIM5_itEnable != (uint8_t)RESET))
  1340. {
  1341. bitstatus = (ITStatus)SET;
  1342. }
  1343. else
  1344. {
  1345. bitstatus = (ITStatus)RESET;
  1346. }
  1347. return ((ITStatus)bitstatus);
  1348. }
  1349. /**
  1350. * @brief Clears the TIM's interrupt pending bits.
  1351. * @param TIM5_IT: Specifies the pending bit to clear.
  1352. * This parameter can be any combination of the following values:
  1353. * @arg TIM5_IT_Update: Update
  1354. * @arg TIM5_IT_CC1: Capture Compare Channel1
  1355. * @arg TIM5_IT_CC2: Capture Compare Channel2
  1356. * @arg TIM5_IT_Trigger: Trigger
  1357. * @arg TIM5_IT_Break: Break
  1358. * @retval None
  1359. */
  1360. void TIM5_ClearITPendingBit(TIM5_IT_TypeDef TIM5_IT)
  1361. {
  1362. /* Check the parameters */
  1363. assert_param(IS_TIM5_IT(TIM5_IT));
  1364. /* Clear the IT pending Bit */
  1365. TIM5->SR1 = (uint8_t)(~(uint8_t)TIM5_IT);
  1366. }
  1367. /**
  1368. * @brief Enables or disables the TIM5 DMA Requests.
  1369. * @param TIM5_DMASource: specifies the DMA Request sources.
  1370. * This parameter can be any combination of the following values:
  1371. * @arg TIM5_DMASource_Update: TIM5 DMA Update Request
  1372. * @arg TIM5_DMASource_CC1: TIM5 DMA CC1 Request
  1373. * @arg TIM5_DMASource_CC2: TIM5 DMA CC2 Request
  1374. * @param NewState: new state of the DMA Request sources.
  1375. * This parameter can be: ENABLE or DISABLE.
  1376. * @retval None
  1377. */
  1378. void TIM5_DMACmd( TIM5_DMASource_TypeDef TIM5_DMASource, FunctionalState NewState)
  1379. {
  1380. /* Check the parameters */
  1381. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1382. assert_param(IS_TIM5_DMA_SOURCE(TIM5_DMASource));
  1383. if (NewState != DISABLE)
  1384. {
  1385. /* Enable the DMA sources */
  1386. TIM5->DER |= TIM5_DMASource;
  1387. }
  1388. else
  1389. {
  1390. /* Disable the DMA sources */
  1391. TIM5->DER &= (uint8_t)(~TIM5_DMASource);
  1392. }
  1393. }
  1394. /**
  1395. * @brief Selects the TIM5 peripheral Capture Compare DMA source.
  1396. * @param NewState: new state of the Capture Compare DMA source.
  1397. * This parameter can be: ENABLE or DISABLE.
  1398. * @retval None
  1399. */
  1400. void TIM5_SelectCCDMA(FunctionalState NewState)
  1401. {
  1402. /* Check the parameters */
  1403. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1404. if (NewState != DISABLE)
  1405. {
  1406. /* Set the CCDS Bit */
  1407. TIM5->CR2 |= TIM_CR2_CCDS;
  1408. }
  1409. else
  1410. {
  1411. /* Reset the CCDS Bit */
  1412. TIM5->CR2 &= (uint8_t)(~TIM_CR2_CCDS);
  1413. }
  1414. }
  1415. /**
  1416. * @}
  1417. */
  1418. /** @defgroup TIM5_Group5 Clocks management functions
  1419. * @brief Clocks management functions
  1420. *
  1421. @verbatim
  1422. ===============================================================================
  1423. Clocks management functions
  1424. ===============================================================================
  1425. @endverbatim
  1426. * @{
  1427. */
  1428. /**
  1429. * @brief Enables the TIM5 internal Clock.
  1430. * @par Parameters:
  1431. * None
  1432. * @retval None
  1433. */
  1434. void TIM5_InternalClockConfig(void)
  1435. {
  1436. /* Disable slave mode to clock the prescaler directly with the internal clock */
  1437. TIM5->SMCR &= (uint8_t)(~TIM_SMCR_SMS);
  1438. }
  1439. /**
  1440. * @brief Configures the TIM5 Trigger as External Clock.
  1441. * @param TIM5_TIxExternalCLKSource: Specifies Trigger source.
  1442. * This parameter can be one of the following values:
  1443. * @arg TIM5_TIxExternalCLK1Source_TI1ED: External Clock mode 1 source = TI1ED
  1444. * @arg TIM5_TIxExternalCLK1Source_TI1: External Clock mode 1 source = TI1
  1445. * @arg TIM5_TIxExternalCLK1Source_TI2: External Clock mode 1 source = TI2
  1446. * @param TIM5_ICPolarity: Specifies the TIx Polarity.
  1447. * This parameter can be one of the following values:
  1448. * @arg TIM5_ICPolarity_Rising: Input Capture on Rising Edge
  1449. * @arg TIM5_ICPolarity_Falling: Input Capture on Falling Edge
  1450. * @param ICFilter: Specifies the filter value.
  1451. * This parameter must be a value between 0x00 and 0x0F
  1452. * @retval None
  1453. */
  1454. void TIM5_TIxExternalClockConfig(TIM5_TIxExternalCLK1Source_TypeDef TIM5_TIxExternalCLKSource,
  1455. TIM5_ICPolarity_TypeDef TIM5_ICPolarity,
  1456. uint8_t ICFilter)
  1457. {
  1458. /* Check the parameters */
  1459. assert_param(IS_TIM5_TIXCLK_SOURCE(TIM5_TIxExternalCLKSource));
  1460. assert_param(IS_TIM5_IC_POLARITY(TIM5_ICPolarity));
  1461. assert_param(IS_TIM5_IC_FILTER(ICFilter));
  1462. /* Configure the TIM5 Input Clock Source */
  1463. if (TIM5_TIxExternalCLKSource == TIM5_TIxExternalCLK1Source_TI2)
  1464. {
  1465. TI2_Config(TIM5_ICPolarity, TIM5_ICSelection_DirectTI, ICFilter);
  1466. }
  1467. else
  1468. {
  1469. TI1_Config(TIM5_ICPolarity, TIM5_ICSelection_DirectTI, ICFilter);
  1470. }
  1471. /* Select the Trigger source */
  1472. TIM5_SelectInputTrigger((TIM5_TRGSelection_TypeDef)TIM5_TIxExternalCLKSource);
  1473. /* Select the External clock mode1 */
  1474. TIM5->SMCR |= (uint8_t)(TIM5_SlaveMode_External1);
  1475. }
  1476. /**
  1477. * @brief Configures the TIM5 External clock Mode1.
  1478. * @param TIM5_ExtTRGPrescaler: Specifies the external Trigger Prescaler.
  1479. * This parameter can be one of the following values:
  1480. * @arg TIM5_ExtTRGPSC_OFF: No External Trigger prescaler
  1481. * @arg TIM5_ExtTRGPSC_DIV2: External Trigger prescaler = 2 (ETRP frequency divided by 2)
  1482. * @arg TIM5_ExtTRGPSC_DIV4: External Trigger prescaler = 4 (ETRP frequency divided by 4)
  1483. * @arg TIM5_ExtTRGPSC_DIV8: External Trigger prescaler = 8 (ETRP frequency divided by 8)
  1484. * @param TIM5_ExtTRGPolarity: Specifies the external Trigger Polarity.
  1485. * This parameter can be one of the following values:
  1486. * @arg TIM5_ExtTRGPolarity_Inverted: External Trigger Polarity = inverted
  1487. * @arg TIM5_ExtTRGPolarity_NonInverted: External Trigger Polarity = non inverted
  1488. * @param ExtTRGFilter: Specifies the External Trigger Filter.
  1489. * This parameter must be a value between 0x00 and 0x0F
  1490. * @retval None
  1491. */
  1492. void TIM5_ETRClockMode1Config(TIM5_ExtTRGPSC_TypeDef TIM5_ExtTRGPrescaler,
  1493. TIM5_ExtTRGPolarity_TypeDef TIM5_ExtTRGPolarity,
  1494. uint8_t ExtTRGFilter)
  1495. {
  1496. /* Configure the ETR Clock source */
  1497. TIM5_ETRConfig(TIM5_ExtTRGPrescaler, TIM5_ExtTRGPolarity, ExtTRGFilter);
  1498. /* Select the External clock mode1 */
  1499. TIM5->SMCR &= (uint8_t)(~TIM_SMCR_SMS);
  1500. TIM5->SMCR |= (uint8_t)(TIM5_SlaveMode_External1);
  1501. /* Select the Trigger selection: ETRF */
  1502. TIM5->SMCR &= (uint8_t)(~TIM_SMCR_TS);
  1503. TIM5->SMCR |= (uint8_t)((TIM5_TRGSelection_TypeDef)TIM5_TRGSelection_ETRF);
  1504. }
  1505. /**
  1506. * @brief Configures the TIM5 External clock Mode2.
  1507. * @param TIM5_ExtTRGPrescaler: Specifies the external Trigger Prescaler.
  1508. * This parameter can be one of the following values:
  1509. * @arg TIM5_ExtTRGPSC_OFF: No External Trigger prescaler
  1510. * @arg TIM5_ExtTRGPSC_DIV2: External Trigger prescaler = 2 (ETRP frequency divided by 2)
  1511. * @arg TIM5_ExtTRGPSC_DIV4: External Trigger prescaler = 4 (ETRP frequency divided by 4)
  1512. * @arg TIM5_ExtTRGPSC_DIV8: External Trigger prescaler = 8 (ETRP frequency divided by 8)
  1513. * @param TIM5_ExtTRGPolarity: Specifies the external Trigger Polarity.
  1514. * This parameter can be one of the following values:
  1515. * @arg TIM5_ExtTRGPolarity_Inverted: External Trigger Polarity = inverted
  1516. * @arg TIM5_ExtTRGPolarity_NonInverted: External Trigger Polarity = non inverted
  1517. * @param ExtTRGFilter: Specifies the External Trigger Filter.
  1518. * This parameter must be a value between 0x00 and 0x0F
  1519. * @retval None
  1520. */
  1521. void TIM5_ETRClockMode2Config(TIM5_ExtTRGPSC_TypeDef TIM5_ExtTRGPrescaler,
  1522. TIM5_ExtTRGPolarity_TypeDef TIM5_ExtTRGPolarity,
  1523. uint8_t ExtTRGFilter)
  1524. {
  1525. /* Configure the ETR Clock source */
  1526. TIM5_ETRConfig(TIM5_ExtTRGPrescaler, TIM5_ExtTRGPolarity, ExtTRGFilter);
  1527. /* Enable the External clock mode2 */
  1528. TIM5->ETR |= TIM_ETR_ECE ;
  1529. }
  1530. /**
  1531. * @}
  1532. */
  1533. /** @defgroup TIM5_Group6 Synchronization management functions
  1534. * @brief Synchronization management functions
  1535. *
  1536. @verbatim
  1537. ===============================================================================
  1538. Synchronization management functions
  1539. ===============================================================================
  1540. ===================================================================
  1541. TIM5 Driver: how to use it in synchronization Mode
  1542. ===================================================================
  1543. Case of two/several Timers
  1544. **************************
  1545. 1. If TIM5 is used as master to other timers use the following functions:
  1546. - TIM5_SelectOutputTrigger()
  1547. - TIM5_SelectMasterSlaveMode()
  1548. 2. If TIM5 is used as slave to other timers use the following functions:
  1549. - TIM5_SelectInputTrigger()
  1550. - TIM5_SelectSlaveMode()
  1551. Case of Timers and external trigger (TRIG pin)
  1552. ********************************************
  1553. 1. Configure the External trigger using TIM5_ETRConfig()
  1554. 2. Configure the Slave Timer using the following functions:
  1555. - TIM5_SelectInputTrigger()
  1556. - TIM5_SelectSlaveMode()
  1557. @endverbatim
  1558. * @{
  1559. */
  1560. /**
  1561. * @brief Selects the TIM5 Input Trigger source.
  1562. * @param TIM5_InputTriggerSource: Specifies Input Trigger source.
  1563. * This parameter can be one of the following values:
  1564. * @arg TIM5_TRGSelection_TIM4: TRIG Input source = TIM TRIG Output
  1565. * @arg TIM5_TRGSelection_TIM1: TRIG Input source = TIM TRIG Output
  1566. * @arg TIM5_TRGSelection_TIM3: TRIG Input source = TIM TRIG Output
  1567. * @arg TIM5_TRGSelection_TIM2: TRIG Input source = TIM TRIG Output
  1568. * @arg TIM5_TRGSelection_TI1F_ED: TRIG Input source = TI1F_ED (TI1 Edge Detector)
  1569. * @arg TIM5_TRGSelection_TI1FP1: TRIG Input source = TI1FP1 (Filtered Timer Input 1)
  1570. * @arg TIM5_TRGSelection_TI2FP2: TRIG Input source = TI2FP2 (Filtered Timer Input 2)
  1571. * @arg TIM5_TRGSelection_ETRF: TRIG Input source = ETRF (External Trigger Input )
  1572. * @retval None
  1573. */
  1574. void TIM5_SelectInputTrigger(TIM5_TRGSelection_TypeDef TIM5_InputTriggerSource)
  1575. {
  1576. uint8_t tmpsmcr = 0;
  1577. /* Check the parameters */
  1578. assert_param(IS_TIM5_TRIGGER_SELECTION(TIM5_InputTriggerSource));
  1579. tmpsmcr = TIM5->SMCR;
  1580. /* Select the Trigger Source */
  1581. tmpsmcr &= (uint8_t)(~TIM_SMCR_TS);
  1582. tmpsmcr |= (uint8_t)TIM5_InputTriggerSource;
  1583. TIM5->SMCR = (uint8_t)tmpsmcr;
  1584. }
  1585. /**
  1586. * @brief Selects the TIM5 Trigger Output Mode.
  1587. * @param TIM5_TRGOSource: Specifies the Trigger Output source.
  1588. * This parameter can be one of the following values:
  1589. * @arg TIM5_TRGOSource_Reset: Trigger Output source = Reset
  1590. * @arg TIM5_TRGOSource_Enable: Trigger Output source = TIM5 is enabled
  1591. * @arg TIM5_TRGOSource_Update: Trigger Output source = Update event
  1592. * @arg TIM5_TRGOSource_OC1: Trigger Output source = output compare channel1
  1593. * @arg TIM5_TRGOSource_OC1REF: Trigger Output source = output compare channel 1 reference
  1594. * @arg TIM5_TRGOSource_OC2REF: Trigger Output source = output compare channel 2 reference
  1595. * @retval None
  1596. */
  1597. void TIM5_SelectOutputTrigger(TIM5_TRGOSource_TypeDef TIM5_TRGOSource)
  1598. {
  1599. uint8_t tmpcr2 = 0;
  1600. /* Check the parameters */
  1601. assert_param(IS_TIM5_TRGO_SOURCE(TIM5_TRGOSource));
  1602. tmpcr2 = TIM5->CR2;
  1603. /* Reset the MMS Bits */
  1604. tmpcr2 &= (uint8_t)(~TIM_CR2_MMS);
  1605. /* Select the TRGO source */
  1606. tmpcr2 |= (uint8_t)TIM5_TRGOSource;
  1607. TIM5->CR2 = tmpcr2;
  1608. }
  1609. /**
  1610. * @brief Selects the TIM5 Slave Mode.
  1611. * @param TIM5_SlaveMode: Specifies the TIM5 Slave Mode.
  1612. * This parameter can be one of the following values:
  1613. * @arg TIM5_SlaveMode_Reset: Slave Mode Selection = Reset
  1614. * @arg TIM5_SlaveMode_Gated: Slave Mode Selection = Gated
  1615. * @arg TIM5_SlaveMode_Trigger: Slave Mode Selection = Trigger
  1616. * @arg TIM5_SlaveMode_External1: Slave Mode Selection = External 1
  1617. * @retval None
  1618. */
  1619. void TIM5_SelectSlaveMode(TIM5_SlaveMode_TypeDef TIM5_SlaveMode)
  1620. {
  1621. uint8_t tmpsmcr = 0;
  1622. /* Check the parameters */
  1623. assert_param(IS_TIM5_SLAVE_MODE(TIM5_SlaveMode));
  1624. tmpsmcr = TIM5->SMCR;
  1625. /* Reset the SMS Bits */
  1626. tmpsmcr &= (uint8_t)(~TIM_SMCR_SMS);
  1627. /* Select the Slave Mode */
  1628. tmpsmcr |= (uint8_t)TIM5_SlaveMode;
  1629. TIM5->SMCR = tmpsmcr;
  1630. }
  1631. /**
  1632. * @brief Sets or Resets the TIM5 Master/Slave Mode.
  1633. * @param NewState: The new state of the synchronization between TIM5 and its slaves (through TRGO).
  1634. * This parameter can be ENABLE or DISABLE
  1635. * @retval None
  1636. */
  1637. void TIM5_SelectMasterSlaveMode(FunctionalState NewState)
  1638. {
  1639. /* Check the parameters */
  1640. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1641. /* Set or Reset the MSM Bit */
  1642. if (NewState != DISABLE)
  1643. {
  1644. TIM5->SMCR |= TIM_SMCR_MSM;
  1645. }
  1646. else
  1647. {
  1648. TIM5->SMCR &= (uint8_t)(~TIM_SMCR_MSM);
  1649. }
  1650. }
  1651. /**
  1652. * @brief Configures the TIM5 External Trigger.
  1653. * @param TIM5_ExtTRGPrescaler: Specifies the external Trigger Prescaler.
  1654. * This parameter can be one of the following values:
  1655. * @arg TIM5_ExtTRGPSC_OFF: No External Trigger prescaler
  1656. * @arg TIM5_ExtTRGPSC_DIV2: External Trigger prescaler = 2 (ETRP frequency divided by 2)
  1657. * @arg TIM5_ExtTRGPSC_DIV4: External Trigger prescaler = 4 (ETRP frequency divided by 4)
  1658. * @arg TIM5_ExtTRGPSC_DIV8: External Trigger prescaler = 8 (ETRP frequency divided by 8)
  1659. * @param TIM5_ExtTRGPolarity: Specifies the external Trigger Polarity.
  1660. * This parameter can be one of the following values:
  1661. * @arg TIM5_ExtTRGPolarity_Inverted: External Trigger Polarity = inverted
  1662. * @arg TIM5_ExtTRGPolarity_NonInverted: External Trigger Polarity = non inverted
  1663. * @param ExtTRGFilter: Specifies the External Trigger Filter.
  1664. * This parameter must be a value between 0x00 and 0x0F
  1665. * @retval None
  1666. */
  1667. void TIM5_ETRConfig(TIM5_ExtTRGPSC_TypeDef TIM5_ExtTRGPrescaler,
  1668. TIM5_ExtTRGPolarity_TypeDef TIM5_ExtTRGPolarity,
  1669. uint8_t ExtTRGFilter)
  1670. {
  1671. /* Check the parameters */
  1672. assert_param(IS_TIM5_EXT_PRESCALER(TIM5_ExtTRGPrescaler));
  1673. assert_param(IS_TIM5_EXT_POLARITY(TIM5_ExtTRGPolarity));
  1674. assert_param(IS_TIM5_EXT_FILTER(ExtTRGFilter));
  1675. /* Set the Prescaler, the Filter value and the Polarity */
  1676. TIM5->ETR |= (uint8_t)((uint8_t)((uint8_t)TIM5_ExtTRGPrescaler | (uint8_t)TIM5_ExtTRGPolarity)
  1677. | (uint8_t)ExtTRGFilter);
  1678. }
  1679. /**
  1680. * @}
  1681. */
  1682. /** @defgroup TIM5_Group7 Specific interface management functions
  1683. * @brief Specific interface management functions
  1684. *
  1685. @verbatim
  1686. ===============================================================================
  1687. Specific interface management functions
  1688. ===============================================================================
  1689. @endverbatim
  1690. * @{
  1691. */
  1692. /**
  1693. * @brief Configures the TIM5 Encoder Interface.
  1694. * @param TIM5_EncoderMode: Specifies the TIM5 Encoder Mode.
  1695. * This parameter can be one of the following values:
  1696. * @arg TIM5_EncoderMode_TI1: Encoder mode 1
  1697. * @arg TIM5_EncoderMode_TI2: Encoder mode 2
  1698. * @arg TIM5_EncoderMode_TI12: Encoder mode 3
  1699. * @param TIM5_IC1Polarity: Specifies the IC1 Polarity.
  1700. * This parameter can be one of the following values:
  1701. * @arg TIM5_ICPolarity_Rising: Input Capture on Rising Edge
  1702. * @arg TIM5_ICPolarity_Falling: Input Capture on Falling Edge
  1703. * @param TIM5_IC2Polarity: Specifies the IC2 Polarity.
  1704. * This parameter can be one of the following values:
  1705. * @arg TIM5_ICPolarity_Rising: Input Capture on Rising Edge
  1706. * @arg TIM5_ICPolarity_Falling: Input Capture on Falling Edge
  1707. * @retval None
  1708. */
  1709. void TIM5_EncoderInterfaceConfig(TIM5_EncoderMode_TypeDef TIM5_EncoderMode,
  1710. TIM5_ICPolarity_TypeDef TIM5_IC1Polarity,
  1711. TIM5_ICPolarity_TypeDef TIM5_IC2Polarity)
  1712. {
  1713. uint8_t tmpsmcr = 0;
  1714. uint8_t tmpccmr1 = 0;
  1715. uint8_t tmpccmr2 = 0;
  1716. /* Check the parameters */
  1717. assert_param(IS_TIM5_ENCODER_MODE(TIM5_EncoderMode));
  1718. assert_param(IS_TIM5_IC_POLARITY(TIM5_IC1Polarity));
  1719. assert_param(IS_TIM5_IC_POLARITY(TIM5_IC2Polarity));
  1720. tmpsmcr = TIM5->SMCR;
  1721. tmpccmr1 = TIM5->CCMR1;
  1722. tmpccmr2 = TIM5->CCMR2;
  1723. /* Set the encoder Mode */
  1724. tmpsmcr &= (uint8_t)(TIM_SMCR_MSM | TIM_SMCR_TS) ;
  1725. tmpsmcr |= (uint8_t)TIM5_EncoderMode;
  1726. /* Select the Capture Compare 1 and the Capture Compare 2 as input */
  1727. tmpccmr1 &= (uint8_t)(~TIM_CCMR_CCxS);
  1728. tmpccmr2 &= (uint8_t)(~TIM_CCMR_CCxS);
  1729. tmpccmr1 |= TIM_CCMR_TIxDirect_Set;
  1730. tmpccmr2 |= TIM_CCMR_TIxDirect_Set;
  1731. /* Set the TI1 and the TI2 Polarities */
  1732. if (TIM5_IC1Polarity == TIM5_ICPolarity_Falling)
  1733. {
  1734. TIM5->CCER1 |= TIM_CCER1_CC1P ;
  1735. }
  1736. else
  1737. {
  1738. TIM5->CCER1 &= (uint8_t)(~TIM_CCER1_CC1P) ;
  1739. }
  1740. if (TIM5_IC2Polarity == TIM5_ICPolarity_Falling)
  1741. {
  1742. TIM5->CCER1 |= TIM_CCER1_CC2P ;
  1743. }
  1744. else
  1745. {
  1746. TIM5->CCER1 &= (uint8_t)(~TIM_CCER1_CC2P) ;
  1747. }
  1748. TIM5->SMCR = tmpsmcr;
  1749. TIM5->CCMR1 = tmpccmr1;
  1750. TIM5->CCMR2 = tmpccmr2;
  1751. }
  1752. /**
  1753. * @brief Enables or Disables the TIM’s Hall sensor interface.
  1754. * @param NewState: The new state of the TIM5 Hall sensor interface.
  1755. * This parameter can be ENABLE or DISABLE
  1756. * @retval None
  1757. */
  1758. void TIM5_SelectHallSensor(FunctionalState NewState)
  1759. {
  1760. /* Check the parameters */
  1761. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1762. /* Set or Reset the TI1S Bit */
  1763. if (NewState != DISABLE)
  1764. {
  1765. TIM5->CR2 |= TIM_CR2_TI1S;
  1766. }
  1767. else
  1768. {
  1769. TIM5->CR2 &= (uint8_t)(~TIM_CR2_TI1S);
  1770. }
  1771. }
  1772. /**
  1773. * @}
  1774. */
  1775. /**
  1776. * @brief Configure the TI1 as Input.
  1777. * @param TIM5_ICPolarity: Input Capture Polarity
  1778. * This parameter can be one of the following values:
  1779. * @arg TIM5_ICPolarity_Rising: Input Capture on Rising Edge
  1780. * @arg TIM5_ICPolarity_Falling: Input Capture on Falling Edge
  1781. * @param TIM5_ICSelection: Specifies the input to be used.
  1782. * This parameter can be one of the following values:
  1783. * @arg TIM5_ICSelection_DirectTI: Input Capture mapped on the direct input
  1784. * @arg TIM5_ICSelection_IndirectTI: Input Capture mapped on the indirect input
  1785. * @arg TIM5_ICSelection_TRGI: Input Capture mapped on the Trigger Input
  1786. * @param TIM5_ICFilter: Specifies the Input Capture Filter.
  1787. * This parameter must be a value between 0x00 and 0x0F.
  1788. * @retval None
  1789. */
  1790. static void TI1_Config(TIM5_ICPolarity_TypeDef TIM5_ICPolarity, \
  1791. TIM5_ICSelection_TypeDef TIM5_ICSelection, \
  1792. uint8_t TIM5_ICFilter)
  1793. {
  1794. uint8_t tmpccmr1 = 0;
  1795. uint8_t tmpicpolarity = TIM5_ICPolarity;
  1796. tmpccmr1 = TIM5->CCMR1;
  1797. /* Check the parameters */
  1798. assert_param(IS_TIM5_IC_POLARITY(TIM5_ICPolarity));
  1799. assert_param(IS_TIM5_IC_SELECTION(TIM5_ICSelection));
  1800. assert_param(IS_TIM5_IC_FILTER(TIM5_ICFilter));
  1801. /* Disable the Channel 1: Reset the CCE Bit */
  1802. TIM5->CCER1 &= (uint8_t)(~TIM_CCER1_CC1E);
  1803. /* Select the Input and set the filter */
  1804. tmpccmr1 &= (uint8_t)(~TIM_CCMR_CCxS) & (uint8_t)(~TIM_CCMR_ICxF);
  1805. tmpccmr1 |= (uint8_t)(((uint8_t)(TIM5_ICSelection)) | ((uint8_t)(TIM5_ICFilter << 4)));
  1806. TIM5->CCMR1 = tmpccmr1;
  1807. /* Select the Polarity */
  1808. if (tmpicpolarity == (uint8_t)(TIM5_ICPolarity_Falling))
  1809. {
  1810. TIM5->CCER1 |= TIM_CCER1_CC1P;
  1811. }
  1812. else
  1813. {
  1814. TIM5->CCER1 &= (uint8_t)(~TIM_CCER1_CC1P);
  1815. }
  1816. /* Set the CCE Bit */
  1817. TIM5->CCER1 |= TIM_CCER1_CC1E;
  1818. }
  1819. /**
  1820. * @brief Configure the TI2 as Input.
  1821. * @param TIM5_ICPolarity: Input Capture Polarity
  1822. * This parameter can be one of the following values:
  1823. * @arg TIM5_ICPolarity_Rising: Input Capture on Rising Edge
  1824. * @arg TIM5_ICPolarity_Falling: Input Capture on Falling Edge
  1825. * @param TIM5_ICSelection: Specifies the input to be used.
  1826. * This parameter can be one of the following values:
  1827. * @arg TIM5_ICSelection_DirectTI: Input Capture mapped on the direct input
  1828. * @arg TIM5_ICSelection_IndirectTI: Input Capture mapped on the indirect input
  1829. * @arg TIM5_ICSelection_TRGI: Input Capture mapped on the Trigger Input
  1830. * @param TIM5_ICFilter: Specifies the Input Capture Filter.
  1831. * This parameter must be a value between 0x00 and 0x0F.
  1832. * @retval None
  1833. */
  1834. static void TI2_Config(TIM5_ICPolarity_TypeDef TIM5_ICPolarity,
  1835. TIM5_ICSelection_TypeDef TIM5_ICSelection,
  1836. uint8_t TIM5_ICFilter)
  1837. {
  1838. uint8_t tmpccmr2 = 0;
  1839. uint8_t tmpicpolarity = TIM5_ICPolarity;
  1840. /* Check the parameters */
  1841. assert_param(IS_TIM5_IC_POLARITY(TIM5_ICPolarity));
  1842. assert_param(IS_TIM5_IC_SELECTION(TIM5_ICSelection));
  1843. assert_param(IS_TIM5_IC_FILTER(TIM5_ICFilter));
  1844. tmpccmr2 = TIM5->CCMR2;
  1845. /* Disable the Channel 2: Reset the CCE Bit */
  1846. TIM5->CCER1 &= (uint8_t)(~TIM_CCER1_CC2E);
  1847. /* Select the Input and set the filter */
  1848. tmpccmr2 &= (uint8_t)(~TIM_CCMR_CCxS) & (uint8_t)(~TIM_CCMR_ICxF);
  1849. tmpccmr2 |= (uint8_t)(((uint8_t)(TIM5_ICSelection)) | ((uint8_t)(TIM5_ICFilter << 4)));
  1850. TIM5->CCMR2 = tmpccmr2;
  1851. /* Select the Polarity */
  1852. if (tmpicpolarity == TIM5_ICPolarity_Falling)
  1853. {
  1854. TIM5->CCER1 |= TIM_CCER1_CC2P ;
  1855. }
  1856. else
  1857. {
  1858. TIM5->CCER1 &= (uint8_t)(~TIM_CCER1_CC2P) ;
  1859. }
  1860. /* Set the CCE Bit */
  1861. TIM5->CCER1 |= TIM_CCER1_CC2E;
  1862. }
  1863. /**
  1864. * @}
  1865. */
  1866. /**
  1867. * @}
  1868. */
  1869. /**
  1870. * @}
  1871. */
  1872. /**
  1873. * @}
  1874. */
  1875. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/