stm8s_tim5.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417
  1. /**
  2. ******************************************************************************
  3. * @file stm8s_tim5.c
  4. * @author MCD Application Team
  5. * @version V2.3.0
  6. * @date 16-June-2017
  7. * @brief This file contains all the functions for the TIM5 peripheral.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  12. *
  13. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  14. * You may not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at:
  16. *
  17. * http://www.st.com/software_license_agreement_liberty_v2
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS,
  21. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. ******************************************************************************
  26. */
  27. /* Includes ------------------------------------------------------------------*/
  28. #include "stm8s_tim5.h"
  29. /** @addtogroup STM8S_StdPeriph_Driver
  30. * @{
  31. */
  32. /* Private typedef -----------------------------------------------------------*/
  33. /* Private define ------------------------------------------------------------*/
  34. /* Private macro -------------------------------------------------------------*/
  35. /* Private variables ---------------------------------------------------------*/
  36. /* Private function prototypes -----------------------------------------------*/
  37. static void TI1_Config(uint8_t TIM5_ICPolarity, uint8_t TIM5_ICSelection, uint8_t TIM5_ICFilter);
  38. static void TI2_Config(uint8_t TIM5_ICPolarity, uint8_t TIM5_ICSelection, uint8_t TIM5_ICFilter);
  39. static void TI3_Config(uint8_t TIM5_ICPolarity, uint8_t TIM5_ICSelection, uint8_t TIM5_ICFilter);
  40. /**
  41. * @addtogroup TIM5_Public_Functions
  42. * @{
  43. */
  44. /**
  45. * @brief Deinitializes the TIM5 peripheral registers to their default reset values.
  46. * @param None
  47. * @retval None
  48. */
  49. void TIM5_DeInit(void)
  50. {
  51. TIM5->CR1 = (uint8_t)TIM5_CR1_RESET_VALUE;
  52. TIM5->CR2 = TIM5_CR2_RESET_VALUE;
  53. TIM5->SMCR = TIM5_SMCR_RESET_VALUE;
  54. TIM5->IER = (uint8_t)TIM5_IER_RESET_VALUE;
  55. TIM5->SR2 = (uint8_t)TIM5_SR2_RESET_VALUE;
  56. /* Disable channels */
  57. TIM5->CCER1 = (uint8_t)TIM5_CCER1_RESET_VALUE;
  58. TIM5->CCER2 = (uint8_t)TIM5_CCER2_RESET_VALUE;
  59. /* Then reset channel registers: it also works if lock level is equal to 2 or 3 */
  60. TIM5->CCER1 = (uint8_t)TIM5_CCER1_RESET_VALUE;
  61. TIM5->CCER2 = (uint8_t)TIM5_CCER2_RESET_VALUE;
  62. TIM5->CCMR1 = (uint8_t)TIM5_CCMR1_RESET_VALUE;
  63. TIM5->CCMR2 = (uint8_t)TIM5_CCMR2_RESET_VALUE;
  64. TIM5->CCMR3 = (uint8_t)TIM5_CCMR3_RESET_VALUE;
  65. TIM5->CNTRH = (uint8_t)TIM5_CNTRH_RESET_VALUE;
  66. TIM5->CNTRL = (uint8_t)TIM5_CNTRL_RESET_VALUE;
  67. TIM5->PSCR = (uint8_t)TIM5_PSCR_RESET_VALUE;
  68. TIM5->ARRH = (uint8_t)TIM5_ARRH_RESET_VALUE;
  69. TIM5->ARRL = (uint8_t)TIM5_ARRL_RESET_VALUE;
  70. TIM5->CCR1H = (uint8_t)TIM5_CCR1H_RESET_VALUE;
  71. TIM5->CCR1L = (uint8_t)TIM5_CCR1L_RESET_VALUE;
  72. TIM5->CCR2H = (uint8_t)TIM5_CCR2H_RESET_VALUE;
  73. TIM5->CCR2L = (uint8_t)TIM5_CCR2L_RESET_VALUE;
  74. TIM5->CCR3H = (uint8_t)TIM5_CCR3H_RESET_VALUE;
  75. TIM5->CCR3L = (uint8_t)TIM5_CCR3L_RESET_VALUE;
  76. TIM5->SR1 = (uint8_t)TIM5_SR1_RESET_VALUE;
  77. }
  78. /**
  79. * @brief Initializes the TIM5 Time Base Unit according to the specified parameters.
  80. * @param TIM5_Prescaler specifies the Prescaler from TIM5_Prescaler_TypeDef.
  81. * @param TIM5_Period specifies the Period value.
  82. * @retval None
  83. */
  84. void TIM5_TimeBaseInit( TIM5_Prescaler_TypeDef TIM5_Prescaler,
  85. uint16_t TIM5_Period)
  86. {
  87. /* Set the Prescaler value */
  88. TIM5->PSCR = (uint8_t)(TIM5_Prescaler);
  89. /* Set the Autoreload value */
  90. TIM5->ARRH = (uint8_t)(TIM5_Period >> 8) ;
  91. TIM5->ARRL = (uint8_t)(TIM5_Period);
  92. }
  93. /**
  94. * @brief Initializes the TIM5 Channel1 according to the specified parameters.
  95. * @param TIM5_OCMode specifies the Output Compare mode from @ref TIM5_OCMode_TypeDef.
  96. * @param TIM5_OutputState specifies the Output State from @ref TIM5_OutputState_TypeDef.
  97. * @param TIM5_Pulse specifies the Pulse width value.
  98. * @param TIM5_OCPolarity specifies the Output Compare Polarity from @ref TIM5_OCPolarity_TypeDef.
  99. * @retval None
  100. */
  101. void TIM5_OC1Init(TIM5_OCMode_TypeDef TIM5_OCMode,
  102. TIM5_OutputState_TypeDef TIM5_OutputState,
  103. uint16_t TIM5_Pulse,
  104. TIM5_OCPolarity_TypeDef TIM5_OCPolarity)
  105. {
  106. /* Check the parameters */
  107. assert_param(IS_TIM5_OC_MODE_OK(TIM5_OCMode));
  108. assert_param(IS_TIM5_OUTPUT_STATE_OK(TIM5_OutputState));
  109. assert_param(IS_TIM5_OC_POLARITY_OK(TIM5_OCPolarity));
  110. /* Disable the Channel 1: Reset the CCE Bit, Set the Output State , the Output Polarity */
  111. TIM5->CCER1 &= (uint8_t)(~( TIM5_CCER1_CC1E | TIM5_CCER1_CC1P));
  112. /* Set the Output State & Set the Output Polarity */
  113. TIM5->CCER1 |= (uint8_t)((uint8_t)(TIM5_OutputState & TIM5_CCER1_CC1E )|
  114. (uint8_t)(TIM5_OCPolarity & TIM5_CCER1_CC1P));
  115. /* Reset the Output Compare Bits & Set the Output Compare Mode */
  116. TIM5->CCMR1 = (uint8_t)((uint8_t)(TIM5->CCMR1 & (uint8_t)(~TIM5_CCMR_OCM)) |
  117. (uint8_t)TIM5_OCMode);
  118. /* Set the Pulse value */
  119. TIM5->CCR1H = (uint8_t)(TIM5_Pulse >> 8);
  120. TIM5->CCR1L = (uint8_t)(TIM5_Pulse);
  121. }
  122. /**
  123. * @brief Initializes the TIM5 Channel2 according to the specified parameters.
  124. * @param TIM5_OCMode specifies the Output Compare mode from @ref TIM5_OCMode_TypeDef.
  125. * @param TIM5_OutputState specifies the Output State from @ref TIM5_OutputState_TypeDef.
  126. * @param TIM5_Pulse specifies the Pulse width value.
  127. * @param TIM5_OCPolarity specifies the Output Compare Polarity from @ref TIM5_OCPolarity_TypeDef.
  128. * @retval None
  129. */
  130. void TIM5_OC2Init(TIM5_OCMode_TypeDef TIM5_OCMode,
  131. TIM5_OutputState_TypeDef TIM5_OutputState,
  132. uint16_t TIM5_Pulse,
  133. TIM5_OCPolarity_TypeDef TIM5_OCPolarity)
  134. {
  135. /* Check the parameters */
  136. assert_param(IS_TIM5_OC_MODE_OK(TIM5_OCMode));
  137. assert_param(IS_TIM5_OUTPUT_STATE_OK(TIM5_OutputState));
  138. assert_param(IS_TIM5_OC_POLARITY_OK(TIM5_OCPolarity));
  139. /* Disable the Channel 1: Reset the CCE Bit, Set the Output State , the Output Polarity */
  140. TIM5->CCER1 &= (uint8_t)(~( TIM5_CCER1_CC2E | TIM5_CCER1_CC2P ));
  141. /* Set the Output State & Set the Output Polarity */
  142. TIM5->CCER1 |= (uint8_t)((uint8_t)(TIM5_OutputState & TIM5_CCER1_CC2E )| \
  143. (uint8_t)(TIM5_OCPolarity & TIM5_CCER1_CC2P));
  144. /* Reset the Output Compare Bits & Set the Output Compare Mode */
  145. TIM5->CCMR2 = (uint8_t)((uint8_t)(TIM5->CCMR2 & (uint8_t)(~TIM5_CCMR_OCM)) |
  146. (uint8_t)TIM5_OCMode);
  147. /* Set the Pulse value */
  148. TIM5->CCR2H = (uint8_t)(TIM5_Pulse >> 8);
  149. TIM5->CCR2L = (uint8_t)(TIM5_Pulse);
  150. }
  151. /**
  152. * @brief Initializes the TIM5 Channel3 according to the specified parameters.
  153. * @param TIM5_OCMode specifies the Output Compare mode from @ref TIM5_OCMode_TypeDef.
  154. * @param TIM5_OutputState specifies the Output State from @ref TIM5_OutputState_TypeDef.
  155. * @param TIM5_Pulse specifies the Pulse width value.
  156. * @param TIM5_OCPolarity specifies the Output Compare Polarity from @ref TIM5_OCPolarity_TypeDef.
  157. * @retval None
  158. */
  159. void TIM5_OC3Init(TIM5_OCMode_TypeDef TIM5_OCMode,
  160. TIM5_OutputState_TypeDef TIM5_OutputState,
  161. uint16_t TIM5_Pulse,
  162. TIM5_OCPolarity_TypeDef TIM5_OCPolarity)
  163. {
  164. /* Check the parameters */
  165. assert_param(IS_TIM5_OC_MODE_OK(TIM5_OCMode));
  166. assert_param(IS_TIM5_OUTPUT_STATE_OK(TIM5_OutputState));
  167. assert_param(IS_TIM5_OC_POLARITY_OK(TIM5_OCPolarity));
  168. /* Disable the Channel 1: Reset the CCE Bit, Set the Output State, the Output Polarity */
  169. TIM5->CCER2 &= (uint8_t)(~( TIM5_CCER2_CC3E | TIM5_CCER2_CC3P));
  170. /* Set the Output State & Set the Output Polarity */
  171. TIM5->CCER2 |= (uint8_t)((uint8_t)(TIM5_OutputState & TIM5_CCER2_CC3E )|
  172. (uint8_t)(TIM5_OCPolarity & TIM5_CCER2_CC3P ));
  173. /* Reset the Output Compare Bits & Set the Output Compare Mode */
  174. TIM5->CCMR3 = (uint8_t)((uint8_t)(TIM5->CCMR3 & (uint8_t)(~TIM5_CCMR_OCM)) | (uint8_t)TIM5_OCMode);
  175. /* Set the Pulse value */
  176. TIM5->CCR3H = (uint8_t)(TIM5_Pulse >> 8);
  177. TIM5->CCR3L = (uint8_t)(TIM5_Pulse);
  178. }
  179. /**
  180. * @brief Initializes the TIM5 peripheral according to the specified parameters.
  181. * @param TIM5_Channel specifies the Input Capture Channel from @ref TIM5_Channel_TypeDef.
  182. * @param TIM5_ICPolarity specifies the Input Capture Polarity from @ref TIM5_ICPolarity_TypeDef.
  183. * @param TIM5_ICSelection specifies theInput Capture Selection from @ref TIM5_ICSelection_TypeDef.
  184. * @param TIM5_ICPrescaler specifies the Input Capture Prescaler from @ref TIM5_ICPSC_TypeDef.
  185. * @param TIM5_ICFilter specifies the Input Capture Filter value (value can be an integer from 0x00 to 0x0F).
  186. * @retval None
  187. */
  188. void TIM5_ICInit(TIM5_Channel_TypeDef TIM5_Channel,
  189. TIM5_ICPolarity_TypeDef TIM5_ICPolarity,
  190. TIM5_ICSelection_TypeDef TIM5_ICSelection,
  191. TIM5_ICPSC_TypeDef TIM5_ICPrescaler,
  192. uint8_t TIM5_ICFilter)
  193. {
  194. /* Check the parameters */
  195. assert_param(IS_TIM5_CHANNEL_OK(TIM5_Channel));
  196. assert_param(IS_TIM5_IC_POLARITY_OK(TIM5_ICPolarity));
  197. assert_param(IS_TIM5_IC_SELECTION_OK(TIM5_ICSelection));
  198. assert_param(IS_TIM5_IC_PRESCALER_OK(TIM5_ICPrescaler));
  199. assert_param(IS_TIM5_IC_FILTER_OK(TIM5_ICFilter));
  200. if (TIM5_Channel == TIM5_CHANNEL_1)
  201. {
  202. /* TI1 Configuration */
  203. TI1_Config((uint8_t)TIM5_ICPolarity,
  204. (uint8_t)TIM5_ICSelection,
  205. (uint8_t)TIM5_ICFilter);
  206. /* Set the Input Capture Prescaler value */
  207. TIM5_SetIC1Prescaler(TIM5_ICPrescaler);
  208. }
  209. else if (TIM5_Channel == TIM5_CHANNEL_2)
  210. {
  211. /* TI2 Configuration */
  212. TI2_Config((uint8_t)TIM5_ICPolarity,
  213. (uint8_t)TIM5_ICSelection,
  214. (uint8_t)TIM5_ICFilter);
  215. /* Set the Input Capture Prescaler value */
  216. TIM5_SetIC2Prescaler(TIM5_ICPrescaler);
  217. }
  218. else
  219. {
  220. /* TI3 Configuration */
  221. TI3_Config((uint8_t)TIM5_ICPolarity,
  222. (uint8_t)TIM5_ICSelection,
  223. (uint8_t)TIM5_ICFilter);
  224. /* Set the Input Capture Prescaler value */
  225. TIM5_SetIC3Prescaler(TIM5_ICPrescaler);
  226. }
  227. }
  228. /**
  229. * @brief Configures the TIM5 peripheral in PWM Input Mode according to the specified parameters.
  230. * @param TIM5_Channel specifies the Input Capture Channel from @ref TIM5_Channel_TypeDef.
  231. * @param TIM5_ICPolarity specifies the Input Capture Polarity from @ref TIM5_ICPolarity_TypeDef.
  232. * @param TIM5_ICSelection specifies theInput Capture Selection from @ref TIM5_ICSelection_TypeDef.
  233. * @param TIM5_ICPrescaler specifies the Input Capture Prescaler from @ref TIM5_ICPSC_TypeDef.
  234. * @param TIM5_ICFilter specifies the Input Capture Filter value (value can be an integer from 0x00 to 0x0F).
  235. * @retval None
  236. */
  237. void TIM5_PWMIConfig(TIM5_Channel_TypeDef TIM5_Channel,
  238. TIM5_ICPolarity_TypeDef TIM5_ICPolarity,
  239. TIM5_ICSelection_TypeDef TIM5_ICSelection,
  240. TIM5_ICPSC_TypeDef TIM5_ICPrescaler,
  241. uint8_t TIM5_ICFilter)
  242. {
  243. uint8_t icpolarity = (uint8_t)TIM5_ICPOLARITY_RISING;
  244. uint8_t icselection = (uint8_t)TIM5_ICSELECTION_DIRECTTI;
  245. /* Check the parameters */
  246. assert_param(IS_TIM5_PWMI_CHANNEL_OK(TIM5_Channel));
  247. assert_param(IS_TIM5_IC_POLARITY_OK(TIM5_ICPolarity));
  248. assert_param(IS_TIM5_IC_SELECTION_OK(TIM5_ICSelection));
  249. assert_param(IS_TIM5_IC_PRESCALER_OK(TIM5_ICPrescaler));
  250. /* Select the Opposite Input Polarity */
  251. if (TIM5_ICPolarity != TIM5_ICPOLARITY_FALLING)
  252. {
  253. icpolarity = (uint8_t)TIM5_ICPOLARITY_FALLING;
  254. }
  255. else
  256. {
  257. icpolarity = (uint8_t)TIM5_ICPOLARITY_RISING;
  258. }
  259. /* Select the Opposite Input */
  260. if (TIM5_ICSelection == TIM5_ICSELECTION_DIRECTTI)
  261. {
  262. icselection = (uint8_t)TIM5_ICSELECTION_INDIRECTTI;
  263. }
  264. else
  265. {
  266. icselection = (uint8_t)TIM5_ICSELECTION_DIRECTTI;
  267. }
  268. if (TIM5_Channel == TIM5_CHANNEL_1)
  269. {
  270. /* TI1 Configuration */
  271. TI1_Config((uint8_t)TIM5_ICPolarity, (uint8_t)TIM5_ICSelection,
  272. (uint8_t)TIM5_ICFilter);
  273. /* Set the Input Capture Prescaler value */
  274. TIM5_SetIC1Prescaler(TIM5_ICPrescaler);
  275. /* TI2 Configuration */
  276. TI2_Config((uint8_t)icpolarity, (uint8_t)icselection, (uint8_t)TIM5_ICFilter);
  277. /* Set the Input Capture Prescaler value */
  278. TIM5_SetIC2Prescaler(TIM5_ICPrescaler);
  279. }
  280. else
  281. {
  282. /* TI2 Configuration */
  283. TI2_Config((uint8_t)TIM5_ICPolarity, (uint8_t)TIM5_ICSelection,
  284. (uint8_t)TIM5_ICFilter);
  285. /* Set the Input Capture Prescaler value */
  286. TIM5_SetIC2Prescaler(TIM5_ICPrescaler);
  287. /* TI1 Configuration */
  288. TI1_Config((uint8_t)icpolarity, (uint8_t)icselection, (uint8_t)TIM5_ICFilter);
  289. /* Set the Input Capture Prescaler value */
  290. TIM5_SetIC1Prescaler(TIM5_ICPrescaler);
  291. }
  292. }
  293. /**
  294. * @brief Enables or disables the TIM5 peripheral.
  295. * @param NewState new state of the TIM5 peripheral.This parameter can
  296. * be ENABLE or DISABLE.
  297. * @retval None
  298. */
  299. void TIM5_Cmd(FunctionalState NewState)
  300. {
  301. /* Check the parameters */
  302. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  303. /* set or Reset the CEN Bit */
  304. if (NewState != DISABLE)
  305. {
  306. TIM5->CR1 |= TIM5_CR1_CEN ;
  307. }
  308. else
  309. {
  310. TIM5->CR1 &= (uint8_t)(~TIM5_CR1_CEN) ;
  311. }
  312. }
  313. /**
  314. * @brief Enables or disables the specified TIM5 interrupts.
  315. * @param NewState new state of the TIM5 peripheral.
  316. * This parameter can be: ENABLE or DISABLE.
  317. * @param TIM5_IT specifies the TIM5 interrupts sources to be enabled or disabled.
  318. * This parameter can be any combination of the following values:
  319. * - TIM5_IT_UPDATE: TIM5 update Interrupt source
  320. * - TIM5_IT_CC1: TIM5 Capture Compare 1 Interrupt source
  321. * - TIM5_IT_CC2: TIM5 Capture Compare 2 Interrupt source
  322. * - TIM5_IT_CC3: TIM5 Capture Compare 3 Interrupt source
  323. * @param NewState new state of the TIM5 peripheral.
  324. * @retval None
  325. */
  326. void TIM5_ITConfig(TIM5_IT_TypeDef TIM5_IT, FunctionalState NewState)
  327. {
  328. /* Check the parameters */
  329. assert_param(IS_TIM5_IT_OK(TIM5_IT));
  330. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  331. if (NewState != DISABLE)
  332. {
  333. /* Enable the Interrupt sources */
  334. TIM5->IER |= (uint8_t)TIM5_IT;
  335. }
  336. else
  337. {
  338. /* Disable the Interrupt sources */
  339. TIM5->IER &= (uint8_t)(~TIM5_IT);
  340. }
  341. }
  342. /**
  343. * @brief Enables or Disables the TIM5 Update event.
  344. * @param NewState new state of the TIM5 peripheral Preload register.This parameter can
  345. * be ENABLE or DISABLE.
  346. * @retval None
  347. */
  348. void TIM5_UpdateDisableConfig(FunctionalState NewState)
  349. {
  350. /* Check the parameters */
  351. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  352. /* Set or Reset the UDIS Bit */
  353. if (NewState != DISABLE)
  354. {
  355. TIM5->CR1 |= TIM5_CR1_UDIS ;
  356. }
  357. else
  358. {
  359. TIM5->CR1 &= (uint8_t)(~TIM5_CR1_UDIS) ;
  360. }
  361. }
  362. /**
  363. * @brief Selects the TIM5 Update Request Interrupt source.
  364. * @param TIM5_UpdateSource specifies the Update source.
  365. * This parameter can be one of the following values
  366. * - TIM5_UPDATESOURCE_REGULAR
  367. * - TIM5_UPDATESOURCE_GLOBAL
  368. * @retval None
  369. */
  370. void TIM5_UpdateRequestConfig(TIM5_UpdateSource_TypeDef TIM5_UpdateSource)
  371. {
  372. /* Check the parameters */
  373. assert_param(IS_TIM5_UPDATE_SOURCE_OK(TIM5_UpdateSource));
  374. /* Set or Reset the URS Bit */
  375. if (TIM5_UpdateSource != TIM5_UPDATESOURCE_GLOBAL)
  376. {
  377. TIM5->CR1 |= TIM5_CR1_URS ;
  378. }
  379. else
  380. {
  381. TIM5->CR1 &= (uint8_t)(~TIM5_CR1_URS) ;
  382. }
  383. }
  384. /**
  385. * @brief Selects the TIM5’s One Pulse Mode.
  386. * @param TIM5_OPMode specifies the OPM Mode to be used.
  387. * This parameter can be one of the following values
  388. * - TIM5_OPMODE_SINGLE
  389. * - TIM5_OPMODE_REPETITIVE
  390. * @retval None
  391. */
  392. void TIM5_SelectOnePulseMode(TIM5_OPMode_TypeDef TIM5_OPMode)
  393. {
  394. /* Check the parameters */
  395. assert_param(IS_TIM5_OPM_MODE_OK(TIM5_OPMode));
  396. /* Set or Reset the OPM Bit */
  397. if (TIM5_OPMode != TIM5_OPMODE_REPETITIVE)
  398. {
  399. TIM5->CR1 |= TIM5_CR1_OPM ;
  400. }
  401. else
  402. {
  403. TIM5->CR1 &= (uint8_t)(~TIM5_CR1_OPM) ;
  404. }
  405. }
  406. /**
  407. * @brief Configures the TIM5 Prescaler.
  408. * @param Prescaler specifies the Prescaler Register value
  409. * This parameter can be one of the following values
  410. * - TIM5_PRESCALER_1
  411. * - TIM5_PRESCALER_2
  412. * - TIM5_PRESCALER_4
  413. * - TIM5_PRESCALER_8
  414. * - TIM5_PRESCALER_16
  415. * - TIM5_PRESCALER_32
  416. * - TIM5_PRESCALER_64
  417. * - TIM5_PRESCALER_128
  418. * - TIM5_PRESCALER_256
  419. * - TIM5_PRESCALER_512
  420. * - TIM5_PRESCALER_1024
  421. * - TIM5_PRESCALER_2048
  422. * - TIM5_PRESCALER_4096
  423. * - TIM5_PRESCALER_8192
  424. * - TIM5_PRESCALER_16384
  425. * - TIM5_PRESCALER_32768
  426. * @param TIM5_PSCReloadMode specifies the TIM5 Prescaler Reload mode.
  427. * This parameter can be one of the following values
  428. * - TIM5_PSCRELOADMODE_IMMEDIATE: The Prescaler is loaded
  429. * immediately.
  430. * - TIM5_PSCRELOADMODE_UPDATE: The Prescaler is loaded at
  431. * the update event.
  432. * @retval None
  433. */
  434. void TIM5_PrescalerConfig(TIM5_Prescaler_TypeDef Prescaler,
  435. TIM5_PSCReloadMode_TypeDef TIM5_PSCReloadMode)
  436. {
  437. /* Check the parameters */
  438. assert_param(IS_TIM5_PRESCALER_RELOAD_OK(TIM5_PSCReloadMode));
  439. assert_param(IS_TIM5_PRESCALER_OK(Prescaler));
  440. /* Set the Prescaler value */
  441. TIM5->PSCR = (uint8_t)Prescaler;
  442. /* Set or reset the UG Bit */
  443. TIM5->EGR = (uint8_t)TIM5_PSCReloadMode ;
  444. }
  445. /**
  446. * @brief Forces the TIM5 Channel1 output waveform to active or inactive level.
  447. * @param TIM5_ForcedAction specifies the forced Action to be set to the output waveform.
  448. * This parameter can be one of the following values:
  449. * - TIM5_FORCEDACTION_ACTIVE: Force active level on OC1REF
  450. * - TIM5_FORCEDACTION_INACTIVE: Force inactive level on
  451. * OC1REF.
  452. * @retval None
  453. */
  454. void TIM5_ForcedOC1Config(TIM5_ForcedAction_TypeDef TIM5_ForcedAction)
  455. {
  456. /* Check the parameters */
  457. assert_param(IS_TIM5_FORCED_ACTION_OK(TIM5_ForcedAction));
  458. /* Reset the OCM Bits */ /* Configure The Forced output Mode */
  459. TIM5->CCMR1 = (uint8_t)((uint8_t)(TIM5->CCMR1 & (uint8_t)(~TIM5_CCMR_OCM))
  460. | (uint8_t)TIM5_ForcedAction);
  461. }
  462. /**
  463. * @brief Forces the TIM5 Channel2 output waveform to active or inactive level.
  464. * @param TIM5_ForcedAction specifies the forced Action to be set to the output waveform.
  465. * This parameter can be one of the following values:
  466. * - TIM5_FORCEDACTION_ACTIVE: Force active level on OC2REF
  467. * - TIM5_FORCEDACTION_INACTIVE: Force inactive level on
  468. * OC2REF.
  469. * @retval None
  470. */
  471. void TIM5_ForcedOC2Config(TIM5_ForcedAction_TypeDef TIM5_ForcedAction)
  472. {
  473. /* Check the parameters */
  474. assert_param(IS_TIM5_FORCED_ACTION_OK(TIM5_ForcedAction));
  475. /* Reset the OCM Bits */ /* Configure The Forced output Mode */
  476. TIM5->CCMR2 = (uint8_t)((uint8_t)(TIM5->CCMR2 & (uint8_t)(~TIM5_CCMR_OCM))
  477. | (uint8_t)TIM5_ForcedAction);
  478. }
  479. /**
  480. * @brief Forces the TIM5 Channel3 output waveform to active or inactive level.
  481. * @param TIM5_ForcedAction specifies the forced Action to be set to the output waveform.
  482. * This parameter can be one of the following values:
  483. * - TIM5_FORCEDACTION_ACTIVE: Force active level on OC3REF
  484. * - TIM5_FORCEDACTION_INACTIVE: Force inactive level on
  485. * OC3REF.
  486. * @retval None
  487. */
  488. void TIM5_ForcedOC3Config(TIM5_ForcedAction_TypeDef TIM5_ForcedAction)
  489. {
  490. /* Check the parameters */
  491. assert_param(IS_TIM5_FORCED_ACTION_OK(TIM5_ForcedAction));
  492. /* Reset the OCM Bits */ /* Configure The Forced output Mode */
  493. TIM5->CCMR3 = (uint8_t)((uint8_t)(TIM5->CCMR3 & (uint8_t)(~TIM5_CCMR_OCM))
  494. | (uint8_t)TIM5_ForcedAction);
  495. }
  496. /**
  497. * @brief Enables or disables TIM5 peripheral Preload register on ARR.
  498. * @param NewState new state of the TIM5 peripheral Preload register.
  499. * This parameter can be ENABLE or DISABLE.
  500. * @retval None
  501. */
  502. void TIM5_ARRPreloadConfig(FunctionalState NewState)
  503. {
  504. /* Check the parameters */
  505. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  506. /* Set or Reset the ARPE Bit */
  507. if (NewState != DISABLE)
  508. {
  509. TIM5->CR1 |= TIM5_CR1_ARPE ;
  510. }
  511. else
  512. {
  513. TIM5->CR1 &= (uint8_t)(~TIM5_CR1_ARPE) ;
  514. }
  515. }
  516. /**
  517. * @brief Enables or disables the TIM5 peripheral Preload Register on CCR1.
  518. * @param NewState new state of the Capture Compare Preload register.
  519. * This parameter can be ENABLE or DISABLE.
  520. * @retval None
  521. */
  522. void TIM5_OC1PreloadConfig(FunctionalState NewState)
  523. {
  524. /* Check the parameters */
  525. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  526. /* Set or Reset the OC1PE Bit */
  527. if (NewState != DISABLE)
  528. {
  529. TIM5->CCMR1 |= TIM5_CCMR_OCxPE ;
  530. }
  531. else
  532. {
  533. TIM5->CCMR1 &= (uint8_t)(~TIM5_CCMR_OCxPE) ;
  534. }
  535. }
  536. /**
  537. * @brief Enables or disables the TIM5 peripheral Preload Register on CCR2.
  538. * @param NewState new state of the Capture Compare Preload register.
  539. * This parameter can be ENABLE or DISABLE.
  540. * @retval None
  541. */
  542. void TIM5_OC2PreloadConfig(FunctionalState NewState)
  543. {
  544. /* Check the parameters */
  545. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  546. /* Set or Reset the OC2PE Bit */
  547. if (NewState != DISABLE)
  548. {
  549. TIM5->CCMR2 |= TIM5_CCMR_OCxPE ;
  550. }
  551. else
  552. {
  553. TIM5->CCMR2 &= (uint8_t)(~TIM5_CCMR_OCxPE) ;
  554. }
  555. }
  556. /**
  557. * @brief Enables or disables the TIM5 peripheral Preload Register on CCR3.
  558. * @param NewState new state of the Capture Compare Preload register.
  559. * This parameter can be ENABLE or DISABLE.
  560. * @retval None
  561. */
  562. void TIM5_OC3PreloadConfig(FunctionalState NewState)
  563. {
  564. /* Check the parameters */
  565. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  566. /* Set or Reset the OC3PE Bit */
  567. if (NewState != DISABLE)
  568. {
  569. TIM5->CCMR3 |= TIM5_CCMR_OCxPE ;
  570. }
  571. else
  572. {
  573. TIM5->CCMR3 &= (uint8_t)(~TIM5_CCMR_OCxPE) ;
  574. }
  575. }
  576. /**
  577. * @brief Configures the TIM5 event to be generated by software.
  578. * @param TIM5_EventSource specifies the event source.
  579. * This parameter can be one of the following values:
  580. * - TIM5_EVENTSOURCE_UPDATE: TIM5 update Event source
  581. * - TIM5_EVENTSOURCE_CC1: TIM5 Capture Compare 1 Event source
  582. * - TIM5_EVENTSOURCE_CC2: TIM5 Capture Compare 2 Event source
  583. * - TIM5_EVENTSOURCE_CC3: TIM5 Capture Compare 3 Event source
  584. * @retval None
  585. */
  586. void TIM5_GenerateEvent(TIM5_EventSource_TypeDef TIM5_EventSource)
  587. {
  588. /* Check the parameters */
  589. assert_param(IS_TIM5_EVENT_SOURCE_OK(TIM5_EventSource));
  590. /* Set the event sources */
  591. TIM5->EGR = (uint8_t)TIM5_EventSource;
  592. }
  593. /**
  594. * @brief Configures the TIM5 Channel 1 polarity.
  595. * @param TIM5_OCPolarity specifies the OC1 Polarity.
  596. * This parameter can be one of the following values:
  597. * - TIM5_OCPOLARITY_LOW: Output Compare active low
  598. * - TIM5_OCPOLARITY_HIGH: Output Compare active high
  599. * @retval None
  600. */
  601. void TIM5_OC1PolarityConfig(TIM5_OCPolarity_TypeDef TIM5_OCPolarity)
  602. {
  603. /* Check the parameters */
  604. assert_param(IS_TIM5_OC_POLARITY_OK(TIM5_OCPolarity));
  605. /* Set or Reset the CC1P Bit */
  606. if (TIM5_OCPolarity != TIM5_OCPOLARITY_HIGH)
  607. {
  608. TIM5->CCER1 |= TIM5_CCER1_CC1P ;
  609. }
  610. else
  611. {
  612. TIM5->CCER1 &= (uint8_t)(~TIM5_CCER1_CC1P) ;
  613. }
  614. }
  615. /**
  616. * @brief Configures the TIM5 Channel 2 polarity.
  617. * @param TIM5_OCPolarity specifies the OC2 Polarity.
  618. * This parameter can be one of the following values:
  619. * - TIM5_OCPOLARITY_LOW: Output Compare active low
  620. * - TIM5_OCPOLARITY_HIGH: Output Compare active high
  621. * @retval None
  622. */
  623. void TIM5_OC2PolarityConfig(TIM5_OCPolarity_TypeDef TIM5_OCPolarity)
  624. {
  625. /* Check the parameters */
  626. assert_param(IS_TIM5_OC_POLARITY_OK(TIM5_OCPolarity));
  627. /* Set or Reset the CC2P Bit */
  628. if (TIM5_OCPolarity != TIM5_OCPOLARITY_HIGH)
  629. {
  630. TIM5->CCER1 |= TIM5_CCER1_CC2P ;
  631. }
  632. else
  633. {
  634. TIM5->CCER1 &= (uint8_t)(~TIM5_CCER1_CC2P) ;
  635. }
  636. }
  637. /**
  638. * @brief Configures the TIM5 Channel 3 polarity.
  639. * @param TIM5_OCPolarity specifies the OC3 Polarity.
  640. * This parameter can be one of the following values:
  641. * - TIM5_OCPOLARITY_LOW: Output Compare active low
  642. * - TIM5_OCPOLARITY_HIGH: Output Compare active high
  643. * @retval None
  644. */
  645. void TIM5_OC3PolarityConfig(TIM5_OCPolarity_TypeDef TIM5_OCPolarity)
  646. {
  647. /* Check the parameters */
  648. assert_param(IS_TIM5_OC_POLARITY_OK(TIM5_OCPolarity));
  649. /* Set or Reset the CC3P Bit */
  650. if (TIM5_OCPolarity != TIM5_OCPOLARITY_HIGH)
  651. {
  652. TIM5->CCER2 |= TIM5_CCER2_CC3P ;
  653. }
  654. else
  655. {
  656. TIM5->CCER2 &= (uint8_t)(~TIM5_CCER2_CC3P) ;
  657. }
  658. }
  659. /**
  660. * @brief Enables or disables the TIM5 Capture Compare Channel x.
  661. * @param TIM5_Channel specifies the TIM5 Channel.
  662. * This parameter can be one of the following values:
  663. * - TIM5_Channel1: TIM5 Channel1
  664. * - TIM5_Channel2: TIM5 Channel2
  665. * - TIM5_Channel3: TIM5 Channel3
  666. * @param NewState specifies the TIM5 Channel CCxE bit new state.
  667. * This parameter can be: ENABLE or DISABLE.
  668. * @retval None
  669. */
  670. void TIM5_CCxCmd(TIM5_Channel_TypeDef TIM5_Channel, FunctionalState NewState)
  671. {
  672. /* Check the parameters */
  673. assert_param(IS_TIM5_CHANNEL_OK(TIM5_Channel));
  674. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  675. if (TIM5_Channel == TIM5_CHANNEL_1)
  676. {
  677. /* Set or Reset the CC1E Bit */
  678. if (NewState != DISABLE)
  679. {
  680. TIM5->CCER1 |= TIM5_CCER1_CC1E ;
  681. }
  682. else
  683. {
  684. TIM5->CCER1 &= (uint8_t)(~TIM5_CCER1_CC1E) ;
  685. }
  686. }
  687. else if (TIM5_Channel == TIM5_CHANNEL_2)
  688. {
  689. /* Set or Reset the CC2E Bit */
  690. if (NewState != DISABLE)
  691. {
  692. TIM5->CCER1 |= TIM5_CCER1_CC2E;
  693. }
  694. else
  695. {
  696. TIM5->CCER1 &= (uint8_t)(~TIM5_CCER1_CC2E) ;
  697. }
  698. }
  699. else
  700. {
  701. /* Set or Reset the CC3E Bit */
  702. if (NewState != DISABLE)
  703. {
  704. TIM5->CCER2 |= TIM5_CCER2_CC3E;
  705. }
  706. else
  707. {
  708. TIM5->CCER2 &= (uint8_t)(~TIM5_CCER2_CC3E) ;
  709. }
  710. }
  711. }
  712. /**
  713. * @brief Selects the TIM5 Output Compare Mode. This function disables the
  714. * selected channel before changing the Output Compare Mode. User has to
  715. * enable this channel using TIM5_CCxCmd and TIM5_CCxNCmd functions.
  716. * @param TIM5_Channel specifies the TIM5 Channel.
  717. * This parameter can be one of the following values:
  718. * - TIM5_Channel1: TIM5 Channel1
  719. * - TIM5_Channel2: TIM5 Channel2
  720. * - TIM5_Channel3: TIM5 Channel3
  721. * @param TIM5_OCMode specifies the TIM5 Output Compare Mode.
  722. * This parameter can be one of the following values:
  723. * - TIM5_OCMODE_TIMING
  724. * - TIM5_OCMODE_ACTIVE
  725. * - TIM5_OCMODE_TOGGLE
  726. * - TIM5_OCMODE_PWM1
  727. * - TIM5_OCMODE_PWM2
  728. * - TIM5_FORCEDACTION_ACTIVE
  729. * - TIM5_FORCEDACTION_INACTIVE
  730. * @retval None
  731. */
  732. void TIM5_SelectOCxM(TIM5_Channel_TypeDef TIM5_Channel, TIM5_OCMode_TypeDef TIM5_OCMode)
  733. {
  734. /* Check the parameters */
  735. assert_param(IS_TIM5_CHANNEL_OK(TIM5_Channel));
  736. assert_param(IS_TIM5_OCM_OK(TIM5_OCMode));
  737. if (TIM5_Channel == TIM5_CHANNEL_1)
  738. {
  739. /* Disable the Channel 1: Reset the CCE Bit */
  740. TIM5->CCER1 &= (uint8_t)(~TIM5_CCER1_CC1E);
  741. /* Reset the Output Compare Bits Set the Output Compare Mode */
  742. TIM5->CCMR1 = (uint8_t)((uint8_t)(TIM5->CCMR1 & (uint8_t)(~TIM5_CCMR_OCM))
  743. | (uint8_t)TIM5_OCMode);
  744. }
  745. else if (TIM5_Channel == TIM5_CHANNEL_2)
  746. {
  747. /* Disable the Channel 2: Reset the CCE Bit */
  748. TIM5->CCER1 &= (uint8_t)(~TIM5_CCER1_CC2E);
  749. /* Reset the Output Compare Bits ** Set the Output Compare Mode */
  750. TIM5->CCMR2 = (uint8_t)((uint8_t)(TIM5->CCMR2 & (uint8_t)(~TIM5_CCMR_OCM))
  751. | (uint8_t)TIM5_OCMode);
  752. }
  753. else
  754. {
  755. /* Disable the Channel 3: Reset the CCE Bit */
  756. TIM5->CCER2 &= (uint8_t)(~TIM5_CCER2_CC3E);
  757. /* Reset the Output Compare Bits ** Set the Output Compare Mode */
  758. TIM5->CCMR3 = (uint8_t)((uint8_t)(TIM5->CCMR3 & (uint8_t)(~TIM5_CCMR_OCM))
  759. | (uint8_t)TIM5_OCMode);
  760. }
  761. }
  762. /**
  763. * @brief Sets the TIM5 Counter Register value.
  764. * @param Counter specifies the Counter register new value.
  765. * This parameter is between 0x0000 and 0xFFFF.
  766. * @retval None
  767. */
  768. void TIM5_SetCounter(uint16_t Counter)
  769. {
  770. /* Set the Counter Register value */
  771. TIM5->CNTRH = (uint8_t)(Counter >> 8);
  772. TIM5->CNTRL = (uint8_t)(Counter);
  773. }
  774. /**
  775. * @brief Sets the TIM5 Autoreload Register value.
  776. * @param Autoreload specifies the Autoreload register new value.
  777. * This parameter is between 0x0000 and 0xFFFF.
  778. * @retval None
  779. */
  780. void TIM5_SetAutoreload(uint16_t Autoreload)
  781. {
  782. /* Set the Autoreload Register value */
  783. TIM5->ARRH = (uint8_t)(Autoreload >> 8);
  784. TIM5->ARRL = (uint8_t)(Autoreload);
  785. }
  786. /**
  787. * @brief Sets the TIM5 Capture Compare1 Register value.
  788. * @param Compare1 specifies the Capture Compare1 register new value.
  789. * This parameter is between 0x0000 and 0xFFFF.
  790. * @retval None
  791. */
  792. void TIM5_SetCompare1(uint16_t Compare1)
  793. {
  794. /* Set the Capture Compare1 Register value */
  795. TIM5->CCR1H = (uint8_t)(Compare1 >> 8);
  796. TIM5->CCR1L = (uint8_t)(Compare1);
  797. }
  798. /**
  799. * @brief Sets the TIM5 Capture Compare2 Register value.
  800. * @param Compare2 specifies the Capture Compare2 register new value.
  801. * This parameter is between 0x0000 and 0xFFFF.
  802. * @retval None
  803. */
  804. void TIM5_SetCompare2(uint16_t Compare2)
  805. {
  806. /* Set the Capture Compare2 Register value */
  807. TIM5->CCR2H = (uint8_t)(Compare2 >> 8);
  808. TIM5->CCR2L = (uint8_t)(Compare2);
  809. }
  810. /**
  811. * @brief Sets the TIM5 Capture Compare3 Register value.
  812. * @param Compare3 specifies the Capture Compare3 register new value.
  813. * This parameter is between 0x0000 and 0xFFFF.
  814. * @retval None
  815. */
  816. void TIM5_SetCompare3(uint16_t Compare3)
  817. {
  818. /* Set the Capture Compare3 Register value */
  819. TIM5->CCR3H = (uint8_t)(Compare3 >> 8);
  820. TIM5->CCR3L = (uint8_t)(Compare3);
  821. }
  822. /**
  823. * @brief Sets the TIM5 Input Capture 1 prescaler.
  824. * @param TIM5_IC1Prescaler specifies the Input Capture prescaler new value
  825. * This parameter can be one of the following values:
  826. * - TIM5_ICPSC_DIV1: no prescaler
  827. * - TIM5_ICPSC_DIV2: capture is done once every 2 events
  828. * - TIM5_ICPSC_DIV4: capture is done once every 4 events
  829. * - TIM5_ICPSC_DIV8: capture is done once every 8 events
  830. * @retval None
  831. */
  832. void TIM5_SetIC1Prescaler(TIM5_ICPSC_TypeDef TIM5_IC1Prescaler)
  833. {
  834. /* Check the parameters */
  835. assert_param(IS_TIM5_IC_PRESCALER_OK(TIM5_IC1Prescaler));
  836. /* Reset the IC1PSC Bits */ /* Set the IC1PSC value */
  837. TIM5->CCMR1 = (uint8_t)((uint8_t)(TIM5->CCMR1 & (uint8_t)(~TIM5_CCMR_ICxPSC))|
  838. (uint8_t)TIM5_IC1Prescaler);
  839. }
  840. /**
  841. * @brief Sets the TIM5 Input Capture 2 prescaler.
  842. * @param TIM5_IC2Prescaler specifies the Input Capture prescaler new value
  843. * This parameter can be one of the following values:
  844. * - TIM5_ICPSC_DIV1: no prescaler
  845. * - TIM5_ICPSC_DIV2: capture is done once every 2 events
  846. * - TIM5_ICPSC_DIV4: capture is done once every 4 events
  847. * - TIM5_ICPSC_DIV8: capture is done once every 8 events
  848. * @retval None
  849. */
  850. void TIM5_SetIC2Prescaler(TIM5_ICPSC_TypeDef TIM5_IC2Prescaler)
  851. {
  852. /* Check the parameters */
  853. assert_param(IS_TIM5_IC_PRESCALER_OK(TIM5_IC2Prescaler));
  854. /* Reset the IC1PSC Bits */ /* Set the IC1PSC value */
  855. TIM5->CCMR2 = (uint8_t)((uint8_t)(TIM5->CCMR2 & (uint8_t)(~TIM5_CCMR_ICxPSC))
  856. | (uint8_t)TIM5_IC2Prescaler);
  857. }
  858. /**
  859. * @brief Sets the TIM5 Input Capture 3 prescaler.
  860. * @param TIM5_IC3Prescaler specifies the Input Capture prescaler new value
  861. * This parameter can be one of the following values:
  862. * - TIM5_ICPSC_DIV1: no prescaler
  863. * - TIM5_ICPSC_DIV2: capture is done once every 2 events
  864. * - TIM5_ICPSC_DIV4: capture is done once every 4 events
  865. * - TIM5_ICPSC_DIV8: capture is done once every 8 events
  866. * @retval None
  867. */
  868. void TIM5_SetIC3Prescaler(TIM5_ICPSC_TypeDef TIM5_IC3Prescaler)
  869. {
  870. /* Check the parameters */
  871. assert_param(IS_TIM5_IC_PRESCALER_OK(TIM5_IC3Prescaler));
  872. /* Reset the IC1PSC Bits */ /* Set the IC1PSC value */
  873. TIM5->CCMR3 = (uint8_t)((uint8_t)(TIM5->CCMR3 & (uint8_t)(~TIM5_CCMR_ICxPSC)) |
  874. (uint8_t)TIM5_IC3Prescaler);
  875. }
  876. /**
  877. * @brief Gets the TIM5 Input Capture 1 value.
  878. * @param None
  879. * @retval Capture Compare 1 Register value.
  880. */
  881. uint16_t TIM5_GetCapture1(void)
  882. {
  883. uint16_t temp = 0;
  884. temp = ((uint16_t)TIM5->CCR1H << 8);
  885. /* Get the Capture 1 Register value */
  886. return (uint16_t)(temp | (uint16_t)(TIM5->CCR1L));
  887. }
  888. /**
  889. * @brief Gets the TIM5 Input Capture 2 value.
  890. * @param None
  891. * @retval Capture Compare 2 Register value.
  892. */
  893. uint16_t TIM5_GetCapture2(void)
  894. {
  895. uint16_t temp = 0;
  896. temp = ((uint16_t)TIM5->CCR2H << 8);
  897. /* Get the Capture 2 Register value */
  898. return (uint16_t)(temp | (uint16_t)(TIM5->CCR2L));
  899. }
  900. /**
  901. * @brief Gets the TIM5 Input Capture 3 value.
  902. * @param None
  903. * @retval Capture Compare 3 Register value.
  904. */
  905. uint16_t TIM5_GetCapture3(void)
  906. {
  907. uint16_t temp = 0;
  908. temp = ((uint16_t)TIM5->CCR3H << 8);
  909. /* Get the Capture 1 Register value */
  910. return (uint16_t)(temp | (uint16_t)(TIM5->CCR3L));
  911. }
  912. /**
  913. * @brief Gets the TIM5 Counter value.
  914. * @param None
  915. * @retval Counter Register value.
  916. */
  917. uint16_t TIM5_GetCounter(void)
  918. {
  919. uint16_t tmpcntr = 0;
  920. tmpcntr = ((uint16_t)TIM5->CNTRH << 8);
  921. /* Get the Counter Register value */
  922. return (uint16_t)(tmpcntr | (uint16_t)(TIM5->CNTRL));
  923. }
  924. /**
  925. * @brief Gets the TIM5 Prescaler value.
  926. * @param None
  927. * @retval Prescaler Register configuration value @ref TIM5_Prescaler_TypeDef .
  928. */
  929. TIM5_Prescaler_TypeDef TIM5_GetPrescaler(void)
  930. {
  931. /* Get the Prescaler Register value */
  932. return (TIM5_Prescaler_TypeDef)(TIM5->PSCR);
  933. }
  934. /**
  935. * @brief Checks whether the specified TIM5 flag is set or not.
  936. * @param TIM5_FLAG specifies the flag to check.
  937. * This parameter can be one of the following values:
  938. * - TIM5_FLAG_UPDATE: TIM5 update Flag
  939. * - TIM5_FLAG_CC1: TIM5 Capture Compare 1 Flag
  940. * - TIM5_FLAG_CC2: TIM5 Capture Compare 2 Flag
  941. * - TIM5_FLAG_CC3: TIM5 Capture Compare 3 Flag
  942. * - TIM5_FLAG_CC1OF: TIM5 Capture Compare 1 overcapture Flag
  943. * - TIM5_FLAG_CC2OF: TIM5 Capture Compare 2 overcapture Flag
  944. * - TIM5_FLAG_CC3OF: TIM5 Capture Compare 3 overcapture Flag
  945. * @retval FlagStatus The new state of TIM5_FLAG (SET or RESET).
  946. */
  947. FlagStatus TIM5_GetFlagStatus(TIM5_FLAG_TypeDef TIM5_FLAG)
  948. {
  949. FlagStatus bitstatus = RESET;
  950. uint8_t tim5_flag_l, tim5_flag_h;
  951. /* Check the parameters */
  952. assert_param(IS_TIM5_GET_FLAG_OK(TIM5_FLAG));
  953. tim5_flag_l= (uint8_t)(TIM5->SR1 & (uint8_t)TIM5_FLAG);
  954. tim5_flag_h= (uint8_t)((uint16_t)TIM5_FLAG >> 8);
  955. if (((tim5_flag_l)|(uint8_t)(TIM5->SR2 & tim5_flag_h)) != RESET )
  956. {
  957. bitstatus = SET;
  958. }
  959. else
  960. {
  961. bitstatus = RESET;
  962. }
  963. return (FlagStatus)bitstatus;
  964. }
  965. /**
  966. * @brief Clears the TIM5’s pending flags.
  967. * @param TIM5_FLAG specifies the flag to clear.
  968. * This parameter can be one of the following values:
  969. * - TIM5_FLAG_UPDATE: TIM5 update Flag
  970. * - TIM5_FLAG_CC1: TIM5 Capture Compare 1 Flag
  971. * - TIM5_FLAG_CC2: TIM5 Capture Compare 2 Flag
  972. * - TIM5_FLAG_CC3: TIM5 Capture Compare 3 Flag
  973. * - TIM5_FLAG_CC1OF: TIM5 Capture Compare 1 overcapture Flag
  974. * - TIM5_FLAG_CC2OF: TIM5 Capture Compare 2 overcapture Flag
  975. * - TIM5_FLAG_CC3OF: TIM5 Capture Compare 3 overcapture Flag
  976. * @retval None.
  977. */
  978. void TIM5_ClearFlag(TIM5_FLAG_TypeDef TIM5_FLAG)
  979. {
  980. /* Check the parameters */
  981. assert_param(IS_TIM5_CLEAR_FLAG_OK(TIM5_FLAG));
  982. /* Clear the flags (rc_w0) clear this bit by writing 0. Writing ‘1’ has no effect*/
  983. TIM5->SR1 = (uint8_t)(~((uint8_t)(TIM5_FLAG)));
  984. TIM5->SR2 &= (uint8_t)(~((uint8_t)((uint16_t)TIM5_FLAG >> 8)));
  985. }
  986. /**
  987. * @brief Checks whether the TIM5 interrupt has occurred or not.
  988. * @param TIM5_IT specifies the TIM5 interrupt source to check.
  989. * This parameter can be one of the following values:
  990. * - TIM5_IT_UPDATE: TIM5 update Interrupt source
  991. * - TIM5_IT_CC1: TIM5 Capture Compare 1 Interrupt source
  992. * - TIM5_IT_CC2: TIM5 Capture Compare 2 Interrupt source
  993. * - TIM5_IT_CC3: TIM5 Capture Compare 3 Interrupt source
  994. * @retval ITStatus The new state of the TIM5_IT(SET or RESET).
  995. */
  996. ITStatus TIM5_GetITStatus(TIM5_IT_TypeDef TIM5_IT)
  997. {
  998. ITStatus bitstatus = RESET;
  999. uint8_t TIM5_itStatus = 0, TIM5_itEnable = 0;
  1000. /* Check the parameters */
  1001. assert_param(IS_TIM5_GET_IT_OK(TIM5_IT));
  1002. TIM5_itStatus = (uint8_t)(TIM5->SR1 & TIM5_IT);
  1003. TIM5_itEnable = (uint8_t)(TIM5->IER & TIM5_IT);
  1004. if ((TIM5_itStatus != (uint8_t)RESET ) && (TIM5_itEnable != (uint8_t)RESET ))
  1005. {
  1006. bitstatus = SET;
  1007. }
  1008. else
  1009. {
  1010. bitstatus = RESET;
  1011. }
  1012. return (ITStatus)(bitstatus);
  1013. }
  1014. /**
  1015. * @brief Clears the TIM5's interrupt pending bits.
  1016. * @param TIM5_IT specifies the pending bit to clear.
  1017. * This parameter can be one of the following values:
  1018. * - TIM5_IT_UPDATE: TIM5 update Interrupt source
  1019. * - TIM5_IT_CC1: TIM5 Capture Compare 1 Interrupt source
  1020. * - TIM5_IT_CC2: TIM5 Capture Compare 2 Interrupt source
  1021. * - TIM5_IT_CC3: TIM5 Capture Compare 3 Interrupt source
  1022. * @retval None.
  1023. */
  1024. void TIM5_ClearITPendingBit(TIM5_IT_TypeDef TIM5_IT)
  1025. {
  1026. /* Check the parameters */
  1027. assert_param(IS_TIM5_IT_OK(TIM5_IT));
  1028. /* Clear the IT pending Bit */
  1029. TIM5->SR1 = (uint8_t)(~TIM5_IT);
  1030. }
  1031. /**
  1032. * @brief Configure the TI1 as Input.
  1033. * @param TIM5_ICPolarity The Input Polarity.
  1034. * This parameter can be one of the following values:
  1035. * - TIM5_ICPOLARITY_FALLING
  1036. * - TIM5_ICPOLARITY_RISING
  1037. * @param TIM5_ICSelection specifies the input to be used.
  1038. * This parameter can be one of the following values:
  1039. * - TIM5_ICSELECTION_DIRECTTI: TIM5 Input 1 is selected to
  1040. * be connected to IC1.
  1041. * - TIM5_ICSELECTION_INDIRECTTI: TIM5 Input 1 is selected to
  1042. * be connected to IC2.
  1043. * @param TIM5_ICFilter Specifies the Input Capture Filter.
  1044. * This parameter must be a value between 0x00 and 0x0F.
  1045. * @retval None
  1046. */
  1047. static void TI1_Config(uint8_t TIM5_ICPolarity,
  1048. uint8_t TIM5_ICSelection,
  1049. uint8_t TIM5_ICFilter)
  1050. {
  1051. /* Disable the Channel 1: Reset the CCE Bit */
  1052. TIM5->CCER1 &= (uint8_t)(~TIM5_CCER1_CC1E);
  1053. /* Select the Input and set the filter */
  1054. TIM5->CCMR1 = (uint8_t)((uint8_t)(TIM5->CCMR1 & (uint8_t)(~( TIM5_CCMR_CCxS | TIM5_CCMR_ICxF )))
  1055. | (uint8_t)(( (TIM5_ICSelection)) | ((uint8_t)( TIM5_ICFilter << 4))));
  1056. /* Select the Polarity */
  1057. if (TIM5_ICPolarity != TIM5_ICPOLARITY_RISING)
  1058. {
  1059. TIM5->CCER1 |= TIM5_CCER1_CC1P ;
  1060. }
  1061. else
  1062. {
  1063. TIM5->CCER1 &= (uint8_t)(~TIM5_CCER1_CC1P) ;
  1064. }
  1065. /* Set the CCE Bit */
  1066. TIM5->CCER1 |= TIM5_CCER1_CC1E;
  1067. }
  1068. /**
  1069. * @brief Configure the TI2 as Input.
  1070. * @param TIM5_ICPolarity The Input Polarity.
  1071. * This parameter can be one of the following values:
  1072. * - TIM5_ICPOLARITY_FALLING
  1073. * - TIM5_ICPOLARITY_RISING
  1074. * @param TIM5_ICSelection specifies the input to be used.
  1075. * This parameter can be one of the following values:
  1076. * - TIM5_ICSELECTION_DIRECTTI: TIM5 Input 2 is selected to
  1077. * be connected to IC2.
  1078. * - TIM5_ICSELECTION_INDIRECTTI: TIM5 Input 2 is selected to
  1079. * be connected to IC1.
  1080. * @param TIM5_ICFilter Specifies the Input Capture Filter.
  1081. * This parameter must be a value between 0x00 and 0x0F.
  1082. * @retval None
  1083. */
  1084. static void TI2_Config(uint8_t TIM5_ICPolarity,
  1085. uint8_t TIM5_ICSelection,
  1086. uint8_t TIM5_ICFilter)
  1087. {
  1088. /* Disable the Channel 2: Reset the CCE Bit */
  1089. TIM5->CCER1 &= (uint8_t)(~TIM5_CCER1_CC2E);
  1090. /* Select the Input and set the filter */
  1091. TIM5->CCMR2 = (uint8_t)((uint8_t)(TIM5->CCMR2 & (uint8_t)(~( TIM5_CCMR_CCxS | TIM5_CCMR_ICxF)))
  1092. | (uint8_t)(( (TIM5_ICSelection)) | ((uint8_t)( TIM5_ICFilter << 4))));
  1093. /* Select the Polarity */
  1094. if (TIM5_ICPolarity != TIM5_ICPOLARITY_RISING)
  1095. {
  1096. TIM5->CCER1 |= TIM5_CCER1_CC2P ;
  1097. }
  1098. else
  1099. {
  1100. TIM5->CCER1 &= (uint8_t)(~TIM5_CCER1_CC2P) ;
  1101. }
  1102. /* Set the CCE Bit */
  1103. TIM5->CCER1 |= TIM5_CCER1_CC2E;
  1104. }
  1105. /**
  1106. * @brief Configure the TI3 as Input.
  1107. * @param TIM5_ICPolarity The Input Polarity.
  1108. * This parameter can be one of the following values:
  1109. * - TIM5_ICPOLARITY_FALLING
  1110. * - TIM5_ICPOLARITY_RISING
  1111. * @param TIM5_ICSelection specifies the input to be used.
  1112. * This parameter can be one of the following values:
  1113. * - TIM5_ICSELECTION_DIRECTTI: TIM5 Input 3 is selected to
  1114. * be connected to IC3.
  1115. * @param TIM5_ICFilter Specifies the Input Capture Filter.
  1116. * This parameter must be a value between 0x00 and 0x0F.
  1117. * @retval None
  1118. */
  1119. static void TI3_Config(uint8_t TIM5_ICPolarity, uint8_t TIM5_ICSelection,
  1120. uint8_t TIM5_ICFilter)
  1121. {
  1122. /* Disable the Channel 3: Reset the CCE Bit */
  1123. TIM5->CCER2 &= (uint8_t)(~TIM5_CCER2_CC3E);
  1124. /* Select the Input and set the filter */
  1125. TIM5->CCMR3 = (uint8_t)((uint8_t)(TIM5->CCMR3 & (uint8_t)(~( TIM5_CCMR_CCxS | TIM5_CCMR_ICxF)))
  1126. | (uint8_t)(( (TIM5_ICSelection)) | ((uint8_t)( TIM5_ICFilter << 4))));
  1127. /* Select the Polarity */
  1128. if (TIM5_ICPolarity != TIM5_ICPOLARITY_RISING)
  1129. {
  1130. TIM5->CCER2 |= TIM5_CCER2_CC3P ;
  1131. }
  1132. else
  1133. {
  1134. TIM5->CCER2 &= (uint8_t)(~TIM5_CCER2_CC3P) ;
  1135. }
  1136. /* Set the CCE Bit */
  1137. TIM5->CCER2 |= TIM5_CCER2_CC3E;
  1138. }
  1139. /**
  1140. * @brief Enables the TIM5 internal Clock.
  1141. * @par Parameters:
  1142. * None
  1143. * @retval None
  1144. */
  1145. void TIM5_InternalClockConfig(void)
  1146. {
  1147. /* Disable slave mode to clock the prescaler directly with the internal clock */
  1148. TIM5->SMCR &= (uint8_t)(~TIM5_SMCR_SMS);
  1149. }
  1150. /**
  1151. * @brief Selects the TIM5 Trigger Output Mode.
  1152. * @param TIM5_TRGOSource : Specifies the Trigger Output source.
  1153. * This parameter can be one of the @ref TIM5_TRGOSource_TypeDef enumeration.
  1154. * @retval None
  1155. */
  1156. void TIM5_SelectOutputTrigger(TIM5_TRGOSource_TypeDef TIM5_TRGOSource)
  1157. {
  1158. uint8_t tmpcr2 = 0;
  1159. /* Check the parameters */
  1160. assert_param(IS_TIM5_TRGO_SOURCE_OK(TIM5_TRGOSource));
  1161. tmpcr2 = TIM5->CR2;
  1162. /* Reset the MMS Bits */
  1163. tmpcr2 &= (uint8_t)(~TIM5_CR2_MMS);
  1164. /* Select the TRGO source */
  1165. tmpcr2 |= (uint8_t)TIM5_TRGOSource;
  1166. TIM5->CR2 = tmpcr2;
  1167. }
  1168. /**
  1169. * @brief Selects the TIM5 Slave Mode.
  1170. * @param TIM5_SlaveMode : Specifies the TIM5 Slave Mode.
  1171. * This parameter can be one of the @ref TIM5_SlaveMode_TypeDef enumeration.
  1172. * @retval None
  1173. */
  1174. void TIM5_SelectSlaveMode(TIM5_SlaveMode_TypeDef TIM5_SlaveMode)
  1175. {
  1176. uint8_t tmpsmcr = 0;
  1177. /* Check the parameters */
  1178. assert_param(IS_TIM5_SLAVE_MODE_OK(TIM5_SlaveMode));
  1179. tmpsmcr = TIM5->SMCR;
  1180. /* Reset the SMS Bits */
  1181. tmpsmcr &= (uint8_t)(~TIM5_SMCR_SMS);
  1182. /* Select the Slave Mode */
  1183. tmpsmcr |= (uint8_t)TIM5_SlaveMode;
  1184. TIM5->SMCR = tmpsmcr;
  1185. }
  1186. /**
  1187. * @brief Selects the TIM5 Input Trigger source.
  1188. * @param TIM5_InputTriggerSource : Specifies Input Trigger source.
  1189. * This parameter can be one of the @ref TIM5_TS_TypeDef enumeration.
  1190. * @retval None
  1191. */
  1192. void TIM5_SelectInputTrigger(TIM5_TS_TypeDef TIM5_InputTriggerSource)
  1193. {
  1194. uint8_t tmpsmcr = 0;
  1195. /* Check the parameters */
  1196. assert_param(IS_TIM5_TRIGGER_SELECTION_OK(TIM5_InputTriggerSource));
  1197. tmpsmcr = TIM5->SMCR;
  1198. /* Select the Trigger Source */
  1199. tmpsmcr &= (uint8_t)(~TIM5_SMCR_TS);
  1200. tmpsmcr |= (uint8_t)TIM5_InputTriggerSource;
  1201. TIM5->SMCR = (uint8_t)tmpsmcr;
  1202. }
  1203. /**
  1204. * @brief Configures the TIM5 Encoder Interface.
  1205. * @param TIM5_EncoderMode : Specifies the TIM5 Encoder Mode.
  1206. * This parameter can be one of the @ref TIM5_EncoderMode_TypeDef enumeration.
  1207. * @param TIM5_IC1Polarity : Specifies the IC1 Polarity.
  1208. * This parameter can be one of the @ref TIM5_ICPolarity_TypeDef enumeration.
  1209. * @param TIM5_IC2Polarity : Specifies the IC2 Polarity.
  1210. * This parameter can be one of the @ref TIM5_ICPolarity_TypeDef enumeration.
  1211. * @retval None
  1212. */
  1213. void TIM5_EncoderInterfaceConfig(TIM5_EncoderMode_TypeDef TIM5_EncoderMode,
  1214. TIM5_ICPolarity_TypeDef TIM5_IC1Polarity,
  1215. TIM5_ICPolarity_TypeDef TIM5_IC2Polarity)
  1216. {
  1217. uint8_t tmpsmcr = 0;
  1218. uint8_t tmpccmr1 = 0;
  1219. uint8_t tmpccmr2 = 0;
  1220. /* Check the parameters */
  1221. assert_param(IS_TIM5_ENCODER_MODE_OK(TIM5_EncoderMode));
  1222. assert_param(IS_TIM5_IC_POLARITY_OK(TIM5_IC1Polarity));
  1223. assert_param(IS_TIM5_IC_POLARITY_OK(TIM5_IC2Polarity));
  1224. tmpsmcr = TIM5->SMCR;
  1225. tmpccmr1 = TIM5->CCMR1;
  1226. tmpccmr2 = TIM5->CCMR2;
  1227. /* Set the encoder Mode */
  1228. tmpsmcr &= (uint8_t)(TIM5_SMCR_MSM | TIM5_SMCR_TS) ;
  1229. tmpsmcr |= (uint8_t)TIM5_EncoderMode;
  1230. /* Select the Capture Compare 1 and the Capture Compare 2 as input */
  1231. tmpccmr1 &= (uint8_t)(~TIM5_CCMR_CCxS);
  1232. tmpccmr2 &= (uint8_t)(~TIM5_CCMR_CCxS);
  1233. tmpccmr1 |= TIM5_CCMR_TIxDirect_Set;
  1234. tmpccmr2 |= TIM5_CCMR_TIxDirect_Set;
  1235. /* Set the TI1 and the TI2 Polarities */
  1236. if (TIM5_IC1Polarity == TIM5_ICPOLARITY_FALLING)
  1237. {
  1238. TIM5->CCER1 |= TIM5_CCER1_CC1P ;
  1239. }
  1240. else
  1241. {
  1242. TIM5->CCER1 &= (uint8_t)(~TIM5_CCER1_CC1P) ;
  1243. }
  1244. if (TIM5_IC2Polarity == TIM5_ICPOLARITY_FALLING)
  1245. {
  1246. TIM5->CCER1 |= TIM5_CCER1_CC2P ;
  1247. }
  1248. else
  1249. {
  1250. TIM5->CCER1 &= (uint8_t)(~TIM5_CCER1_CC2P) ;
  1251. }
  1252. TIM5->SMCR = tmpsmcr;
  1253. TIM5->CCMR1 = tmpccmr1;
  1254. TIM5->CCMR2 = tmpccmr2;
  1255. }
  1256. /**
  1257. * @}
  1258. */
  1259. /**
  1260. * @}
  1261. */
  1262. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/