stm8l15x_tim4.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. /**
  2. ******************************************************************************
  3. * @file stm8l15x_tim4.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 TIM4 peripheral:
  9. * - TimeBase management
  10. * - Interrupts, DMA and flags management
  11. * - Clocks management
  12. * - Synchronization management
  13. *
  14. * @verbatim
  15. *
  16. * ===================================================================
  17. * How to use this driver
  18. * ===================================================================
  19. * This driver provides functions to configure and initialize the TIM4
  20. * peripheral
  21. * These functions are split in 4 groups:
  22. *
  23. * 1. TIM4 TimeBase management: this group includes all needed functions
  24. * to configure the TIM Timebase unit:
  25. * - Set/Get Prescaler
  26. * - Set/Get Autoreload
  27. * - Select the One Pulse mode
  28. * - Update Request Configuration
  29. * - Update Disable Configuration
  30. * - Auto-Preload Configuration
  31. * - Enable/Disable the counter
  32. *
  33. * 2. TIM4 interrupts, DMA and flags management
  34. * - Enable/Disable interrupt sources
  35. * - Get flags status
  36. * - Clear flags/ Pending bits
  37. * - Enable/Disable DMA requests
  38. *
  39. * 3. TIM4 clocks management: this group includes all needed functions
  40. * to configure the clock controller unit:
  41. * - Select internal clock
  42. *
  43. * 4. TIM4 synchronization management: this group includes all needed
  44. * functions to configure the Synchronization unit:
  45. * - Select Input Trigger
  46. * - Select Output Trigger
  47. * - Select Master Slave Mode
  48. *
  49. *
  50. * @endverbatim
  51. *
  52. ******************************************************************************
  53. * @attention
  54. *
  55. * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  56. *
  57. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  58. * You may not use this file except in compliance with the License.
  59. * You may obtain a copy of the License at:
  60. *
  61. * http://www.st.com/software_license_agreement_liberty_v2
  62. *
  63. * Unless required by applicable law or agreed to in writing, software
  64. * distributed under the License is distributed on an "AS IS" BASIS,
  65. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  66. * See the License for the specific language governing permissions and
  67. * limitations under the License.
  68. *
  69. ******************************************************************************
  70. */
  71. /* Includes ------------------------------------------------------------------*/
  72. #include "stm8l15x_tim4.h"
  73. /** @addtogroup STM8L15x_StdPeriph_Driver
  74. * @{
  75. */
  76. /** @defgroup TIM4
  77. * @brief TIM4 driver modules
  78. * @{
  79. */
  80. /* Private typedef -----------------------------------------------------------*/
  81. /* Private define ------------------------------------------------------------*/
  82. /* Private macro -------------------------------------------------------------*/
  83. /* Private variables ---------------------------------------------------------*/
  84. /* Private function prototypes -----------------------------------------------*/
  85. /** @defgroup TIM4_Private_Functions
  86. * @{
  87. */
  88. /** @defgroup TIM4_Group1 TimeBase management functions
  89. * @brief TimeBase management functions
  90. *
  91. @verbatim
  92. ===============================================================================
  93. TimeBase management functions
  94. ===============================================================================
  95. ===================================================================
  96. TIM4 Driver: how to use it in Timing(Time base) Mode
  97. ===================================================================
  98. To use the Timer in Timing(Time base) mode, the following steps are mandatory:
  99. 1. Enable TIM4 clock using CLK_PeripheralClockConfig(CLK_Peripheral_TIM4, ENABLE) function.
  100. 2. Call TIM4_TimeBaseInit() to configure the Time Base unit with the
  101. corresponding configuration.
  102. 3. Enable global interrupts if you need to generate the update interrupt.
  103. 4. Enable the corresponding interrupt using the function TIM4_ITConfig(TIM4_IT_Update)
  104. 5. Call the TIM4_Cmd(ENABLE) function to enable the TIM4 counter.
  105. Note1: All other functions can be used separately to modify, if needed,
  106. a specific feature of the Timer.
  107. @endverbatim
  108. * @{
  109. */
  110. /**
  111. * @brief Deinitializes the TIM4 peripheral registers to their default reset values.
  112. * @param None
  113. * @retval None
  114. */
  115. void TIM4_DeInit(void)
  116. {
  117. TIM4->CR1 = TIM4_CR1_RESET_VALUE;
  118. TIM4->CR2 = TIM4_CR2_RESET_VALUE;
  119. TIM4->SMCR = TIM4_SMCR_RESET_VALUE;
  120. TIM4->IER = TIM4_IER_RESET_VALUE;
  121. TIM4->CNTR = TIM4_CNTR_RESET_VALUE;
  122. TIM4->PSCR = TIM4_PSCR_RESET_VALUE;
  123. TIM4->ARR = TIM4_ARR_RESET_VALUE;
  124. TIM4->SR1 = TIM4_SR1_RESET_VALUE;
  125. }
  126. /**
  127. * @brief Initializes the TIM4 Time Base Unit according to the specified parameters.
  128. * @param TIM4_Prescaler: Prescaler
  129. * This parameter can be one of the following values:
  130. * @arg TIM4_Prescaler_1: Time base Prescaler = 1 (No effect)
  131. * @arg TIM4_Prescaler_2: Time base Prescaler = 2
  132. * @arg TIM4_Prescaler_4: Time base Prescaler = 4
  133. * @arg TIM4_Prescaler_8: Time base Prescaler = 8
  134. * @arg TIM4_Prescaler_16: Time base Prescaler = 16
  135. * @arg TIM4_Prescaler_32: Time base Prescaler = 32
  136. * @arg TIM4_Prescaler_64: Time base Prescaler = 64
  137. * @arg TIM4_Prescaler_128: Time base Prescaler = 128
  138. * @arg TIM4_Prescaler_256: Time base Prescaler = 256
  139. * @arg TIM4_Prescaler_512: Time base Prescaler = 512
  140. * @arg TIM4_Prescaler_1024: Time base Prescaler = 1024
  141. * @arg TIM4_Prescaler_2048: Time base Prescaler = 2048
  142. * @arg TIM4_Prescaler_4096: Time base Prescaler = 4096
  143. * @arg TIM4_Prescaler_8192: Time base Prescaler = 8192
  144. * @arg TIM4_Prescaler_16384: Time base Prescaler = 16384
  145. * @arg TIM4_Prescaler_32768: Time base Prescaler = 32768
  146. * @param TIM4_Period: This parameter must be a value between 0x00 and 0xFF.
  147. * @retval None
  148. */
  149. void TIM4_TimeBaseInit(TIM4_Prescaler_TypeDef TIM4_Prescaler,
  150. uint8_t TIM4_Period)
  151. {
  152. /* Check TIM4 prescaler value */
  153. assert_param(IS_TIM4_Prescaler(TIM4_Prescaler));
  154. /* Set the Autoreload value */
  155. TIM4->ARR = (uint8_t)(TIM4_Period);
  156. /* Set the Prescaler value */
  157. TIM4->PSCR = (uint8_t)(TIM4_Prescaler);
  158. /* Generate an update event to reload the Prescaler value immediately */
  159. TIM4->EGR = TIM4_EventSource_Update;
  160. }
  161. /**
  162. * @brief Configures the TIM4 Prescaler.
  163. * @param Prescaler: Specifies the Prescaler Register value
  164. * This parameter can be one of the following values:
  165. * @arg TIM4_Prescaler_1: Time base Prescaler = 1 (No effect)
  166. * @arg TIM4_Prescaler_2: Time base Prescaler = 2
  167. * @arg TIM4_Prescaler_4: Time base Prescaler = 4
  168. * @arg TIM4_Prescaler_8: Time base Prescaler = 8
  169. * @arg TIM4_Prescaler_16: Time base Prescaler = 16
  170. * @arg TIM4_Prescaler_32: Time base Prescaler = 32
  171. * @arg TIM4_Prescaler_64: Time base Prescaler = 64
  172. * @arg TIM4_Prescaler_128: Time base Prescaler = 128
  173. * @arg TIM4_Prescaler_256: Time base Prescaler = 256
  174. * @arg TIM4_Prescaler_512: Time base Prescaler = 512
  175. * @arg TIM4_Prescaler_1024: Time base Prescaler = 1024
  176. * @arg TIM4_Prescaler_2048: Time base Prescaler = 2048
  177. * @arg TIM4_Prescaler_4096: Time base Prescaler = 4096
  178. * @arg TIM4_Prescaler_8192: Time base Prescaler = 8192
  179. * @arg TIM4_Prescaler_16384: Time base Prescaler = 16384
  180. * @arg TIM4_Prescaler_32768: Time base Prescaler = 32768
  181. * @param TIM4_PSCReloadMode: Specifies the TIM4 Prescaler Reload mode.
  182. * This parameter can be one of the following values:
  183. * @arg TIM4_PSCReloadMode_Update: Prescaler value is reloaded at every update
  184. * @arg TIM4_PSCReloadMode_Immediate: Prescaler value is reloaded immediately
  185. * @retval None
  186. */
  187. void TIM4_PrescalerConfig(TIM4_Prescaler_TypeDef Prescaler,
  188. TIM4_PSCReloadMode_TypeDef TIM4_PSCReloadMode)
  189. {
  190. /* Check the parameters */
  191. assert_param(IS_TIM4_Prescaler_RELOAD(TIM4_PSCReloadMode));
  192. assert_param(IS_TIM4_Prescaler(Prescaler));
  193. /* Set the Prescaler value */
  194. TIM4->PSCR = (uint8_t) Prescaler;
  195. /* Set or reset the UG Bit */
  196. if (TIM4_PSCReloadMode == TIM4_PSCReloadMode_Immediate)
  197. {
  198. TIM4->EGR |= TIM4_EGR_UG ;
  199. }
  200. else
  201. {
  202. TIM4->EGR &= (uint8_t)(~TIM4_EGR_UG) ;
  203. }
  204. }
  205. /**
  206. * @brief Sets the TIM4 Counter Register value.
  207. * @param Counter: Specifies the Counter register new value.
  208. * This parameter is between 0x00 and 0xFF.
  209. * @retval None
  210. */
  211. void TIM4_SetCounter(uint8_t Counter)
  212. {
  213. /* Set the Counter Register value */
  214. TIM4->CNTR = (uint8_t)(Counter);
  215. }
  216. /**
  217. * @brief Sets the TIM4 Autoreload Register value.
  218. * @param Autoreload: Specifies the Autoreload register new value.
  219. * This parameter is between 0x00 and 0xFF.
  220. * @retval None
  221. */
  222. void TIM4_SetAutoreload(uint8_t Autoreload)
  223. {
  224. /* Set the Autoreload Register value */
  225. TIM4->ARR = (uint8_t)(Autoreload);
  226. }
  227. /**
  228. * @brief Gets the TIM4 Counter value.
  229. * @param None
  230. * @retval Counter Register value.
  231. */
  232. uint8_t TIM4_GetCounter(void)
  233. {
  234. uint8_t tmpcntr = 0;
  235. tmpcntr = TIM4->CNTR;
  236. /* Get the Counter Register value */
  237. return ((uint8_t)tmpcntr);
  238. }
  239. /**
  240. * @brief Gets the TIM4 Prescaler value.
  241. * @param None
  242. * @retval TIM4 Prescaler can be one of the following values:
  243. * - TIM4_Prescaler_1: Time base Prescaler = 1 (No effect)
  244. * - TIM4_Prescaler_2: Time base Prescaler = 2
  245. * - TIM4_Prescaler_4: Time base Prescaler = 4
  246. * - TIM4_Prescaler_8: Time base Prescaler = 8
  247. * - TIM4_Prescaler_16: Time base Prescaler = 16
  248. * - TIM4_Prescaler_32: Time base Prescaler = 32
  249. * - TIM4_Prescaler_64: Time base Prescaler = 64
  250. * - TIM4_Prescaler_128: Time base Prescaler = 128
  251. * - TIM4_Prescaler_256: Time base Prescaler = 256
  252. * - TIM4_Prescaler_512: Time base Prescaler = 512
  253. * - TIM4_Prescaler_1024: Time base Prescaler = 1024
  254. * - TIM4_Prescaler_2048: Time base Prescaler = 2048
  255. * - TIM4_Prescaler_4096: Time base Prescaler = 4096
  256. * - TIM4_Prescaler_8192: Time base Prescaler = 8192
  257. * - TIM4_Prescaler_16384: Time base Prescaler = 16384
  258. * - TIM4_Prescaler_32768: Time base Prescaler = 32768
  259. */
  260. TIM4_Prescaler_TypeDef TIM4_GetPrescaler(void)
  261. {
  262. /* Get the Prescaler Register value */
  263. return ((TIM4_Prescaler_TypeDef)TIM4->PSCR);
  264. }
  265. /**
  266. * @brief Enables or Disables the TIM4 Update event.
  267. * @param NewState: The new state of the TIM4 peripheral Preload register.
  268. * This parameter can be ENABLE or DISABLE
  269. * @retval None
  270. */
  271. void TIM4_UpdateDisableConfig(FunctionalState NewState)
  272. {
  273. /* Check the parameters */
  274. assert_param(IS_FUNCTIONAL_STATE(NewState));
  275. /* Set or Reset the UDIS Bit */
  276. if (NewState != DISABLE)
  277. {
  278. TIM4->CR1 |= TIM4_CR1_UDIS ;
  279. }
  280. else
  281. {
  282. TIM4->CR1 &= (uint8_t)(~TIM4_CR1_UDIS) ;
  283. }
  284. }
  285. /**
  286. * @brief Selects the TIM4 Update Request Interrupt source.
  287. * @param TIM4_UpdateSource: Specifies the Update source.
  288. * This parameter can be one of the following values:
  289. * @arg TIM4_UpdateSource_Global: Global Update request source
  290. * @arg TIM4_UpdateSource_Regular: Regular Update request source
  291. * @retval None
  292. */
  293. void TIM4_UpdateRequestConfig(TIM4_UpdateSource_TypeDef TIM4_UpdateSource)
  294. {
  295. /* Check the parameters */
  296. assert_param(IS_TIM4_UPDATE_SOURCE(TIM4_UpdateSource));
  297. /* Set or Reset the URS Bit */
  298. if (TIM4_UpdateSource == TIM4_UpdateSource_Regular)
  299. {
  300. TIM4->CR1 |= TIM4_CR1_URS ;
  301. }
  302. else
  303. {
  304. TIM4->CR1 &= (uint8_t)(~TIM4_CR1_URS) ;
  305. }
  306. }
  307. /**
  308. * @brief Enables or disables TIM4 peripheral Preload register on ARR.
  309. * @param NewState: The new state of the TIM4 peripheral Preload register.
  310. * This parameter can be ENABLE or DISABLE
  311. * @retval None
  312. */
  313. void TIM4_ARRPreloadConfig(FunctionalState NewState)
  314. {
  315. /* Check the parameters */
  316. assert_param(IS_FUNCTIONAL_STATE(NewState));
  317. /* Set or Reset the ARPE Bit */
  318. if (NewState != DISABLE)
  319. {
  320. TIM4->CR1 |= TIM4_CR1_ARPE ;
  321. }
  322. else
  323. {
  324. TIM4->CR1 &= (uint8_t)(~TIM4_CR1_ARPE) ;
  325. }
  326. }
  327. /**
  328. * @brief Selects the TIM4’s One Pulse Mode.
  329. * @param TIM4_OPMode: Specifies the OPM Mode to be used.
  330. * This parameter can be one of the following values:
  331. * @arg TIM4_OPMode_Single: Single one Pulse mode (OPM Active)
  332. * @arg TIM4_OPMode_Repetitive: Repetitive Pulse mode (OPM inactive)
  333. * @retval None
  334. */
  335. void TIM4_SelectOnePulseMode(TIM4_OPMode_TypeDef TIM4_OPMode)
  336. {
  337. /* Check the parameters */
  338. assert_param(IS_TIM4_OPM_MODE(TIM4_OPMode));
  339. /* Set or Reset the OPM Bit */
  340. if (TIM4_OPMode == TIM4_OPMode_Single)
  341. {
  342. TIM4->CR1 |= TIM4_CR1_OPM ;
  343. }
  344. else
  345. {
  346. TIM4->CR1 &= (uint8_t)(~TIM4_CR1_OPM) ;
  347. }
  348. }
  349. /**
  350. * @brief Enables or disables the TIM4 peripheral.
  351. * @param NewState: The new state of the TIM4 peripheral.
  352. * This parameter can be ENABLE or DISABLE
  353. * @retval None
  354. */
  355. void TIM4_Cmd(FunctionalState NewState)
  356. {
  357. /* Check the parameters */
  358. assert_param(IS_FUNCTIONAL_STATE(NewState));
  359. /* set or Reset the CEN Bit */
  360. if (NewState != DISABLE)
  361. {
  362. TIM4->CR1 |= TIM4_CR1_CEN ;
  363. }
  364. else
  365. {
  366. TIM4->CR1 &= (uint8_t)(~TIM4_CR1_CEN) ;
  367. }
  368. }
  369. /**
  370. * @}
  371. */
  372. /** @defgroup TIM4_Group2 Interrupts DMA and flags management functions
  373. * @brief Interrupts, DMA and flags management functions
  374. *
  375. @verbatim
  376. ===============================================================================
  377. Interrupts, DMA and flags management functions
  378. ===============================================================================
  379. @endverbatim
  380. * @{
  381. */
  382. /**
  383. * @brief Enables or disables the specified TIM4 interrupts.
  384. * @param TIM4_IT: Specifies the TIM4 interrupts sources to be enabled or disabled.
  385. * This parameter can be one of the following values:
  386. * @arg TIM4_IT_Update: Update
  387. * @arg TIM4_IT_Trigger: Trigger
  388. * @param NewState: The new state of the TIM4 peripheral.
  389. * This parameter can be ENABLE or DISABLE
  390. * @retval None
  391. */
  392. void TIM4_ITConfig(TIM4_IT_TypeDef TIM4_IT, FunctionalState NewState)
  393. {
  394. /* Check the parameters */
  395. assert_param(IS_TIM4_IT(TIM4_IT));
  396. assert_param(IS_FUNCTIONAL_STATE(NewState));
  397. if (NewState != DISABLE)
  398. {
  399. /* Enable the Interrupt sources */
  400. TIM4->IER |= (uint8_t)TIM4_IT;
  401. }
  402. else
  403. {
  404. /* Disable the Interrupt sources */
  405. TIM4->IER &= (uint8_t)(~(uint8_t)TIM4_IT);
  406. }
  407. }
  408. /**
  409. * @brief Configures the TIM4 event to be generated by software.
  410. * @param TIM4_EventSource: Specifies the event source.
  411. * This parameter can be one of the following values:
  412. * @arg TIM4_EventSource_Update: Update
  413. * @arg TIM4_EventSource_Trigger: Trigger
  414. * @retval None
  415. */
  416. void TIM4_GenerateEvent(TIM4_EventSource_TypeDef TIM4_EventSource)
  417. {
  418. /* Check the parameters */
  419. assert_param(IS_TIM4_EVENT_SOURCE((uint8_t)TIM4_EventSource));
  420. /* Set the event sources */
  421. TIM4->EGR |= (uint8_t)TIM4_EventSource;
  422. }
  423. /**
  424. * @brief Checks whether the specified TIM4 flag is set or not.
  425. * @param TIM4_FLAG: Specifies the flag to check.
  426. * This parameter can be one of the following values:
  427. * @arg TIM4_FLAG_Update: Update
  428. * @arg TIM4_FLAG_Trigger: Trigger
  429. * @retval FlagStatus: The new state of TIM4_FLAG.
  430. * This parameter can be SET or RESET.
  431. */
  432. FlagStatus TIM4_GetFlagStatus(TIM4_FLAG_TypeDef TIM4_FLAG)
  433. {
  434. FlagStatus bitstatus = RESET;
  435. /* Check the parameters */
  436. assert_param(IS_TIM4_GET_FLAG(TIM4_FLAG));
  437. if ((TIM4->SR1 & (uint8_t)TIM4_FLAG) != 0)
  438. {
  439. bitstatus = SET;
  440. }
  441. else
  442. {
  443. bitstatus = RESET;
  444. }
  445. return ((FlagStatus)bitstatus);
  446. }
  447. /**
  448. * @brief Clears the TIM’s pending flags.
  449. * @param TIM4_FLAG: Specifies the flag to clear.
  450. * This parameter can be one of the following values:
  451. * @arg TIM4_FLAG_Update: Update
  452. * @arg TIM4_FLAG_Trigger: Trigger
  453. * @retval None
  454. */
  455. void TIM4_ClearFlag(TIM4_FLAG_TypeDef TIM4_FLAG)
  456. {
  457. /* Check the parameters */
  458. assert_param(IS_TIM4_CLEAR_FLAG((uint8_t)TIM4_FLAG));
  459. /* Clear the flags (rc_w0) clear this bit by writing 0. Writing ‘1’ has no effect*/
  460. TIM4->SR1 = (uint8_t)(~((uint8_t)TIM4_FLAG));
  461. }
  462. /**
  463. * @brief Checks whether the TIM4 interrupt has occurred or not.
  464. * @param TIM4_IT: Specifies the TIM4 interrupt source to check.
  465. * This parameter can be one of the following values:
  466. * @arg TIM4_IT_Update: Update
  467. * @arg TIM4_IT_Trigger: Trigger
  468. * @retval ITStatus: The new state of the TIM4_IT.
  469. * This parameter can be SET or RESET
  470. */
  471. ITStatus TIM4_GetITStatus(TIM4_IT_TypeDef TIM4_IT)
  472. {
  473. ITStatus bitstatus = RESET;
  474. uint8_t itStatus = 0x0, itEnable = 0x0;
  475. /* Check the parameters */
  476. assert_param(IS_TIM4_GET_IT(TIM4_IT));
  477. itStatus = (uint8_t)(TIM4->SR1 & (uint8_t)TIM4_IT);
  478. itEnable = (uint8_t)(TIM4->IER & (uint8_t)TIM4_IT);
  479. if ((itStatus != (uint8_t)RESET ) && (itEnable != (uint8_t)RESET ))
  480. {
  481. bitstatus = (ITStatus)SET;
  482. }
  483. else
  484. {
  485. bitstatus = (ITStatus)RESET;
  486. }
  487. return ((ITStatus)bitstatus);
  488. }
  489. /**
  490. * @brief Clears the TIM4's interrupt pending bits.
  491. * @param TIM4_IT: Specifies the pending bit to clear.
  492. * This parameter can be one of the following values:
  493. * @arg TIM4_IT_Update: Update
  494. * @arg TIM4_IT_Trigger: Trigger
  495. * @retval None
  496. */
  497. void TIM4_ClearITPendingBit(TIM4_IT_TypeDef TIM4_IT)
  498. {
  499. /* Check the parameters */
  500. assert_param(IS_TIM4_IT(TIM4_IT));
  501. /* Clear the IT pending Bit */
  502. TIM4->SR1 = (uint8_t)(~(uint8_t)TIM4_IT);
  503. }
  504. /**
  505. * @brief Enables or disables the TIMx’s DMA Requests.
  506. * @param TIM4_DMASources: specifies the DMA Request sources.
  507. * This parameter can be one of the following values:
  508. * @arg TIM4_DMASource_Update: Update
  509. * @param NewState: new state of the DMA Request sources.
  510. * This parameter can be: ENABLE or DISABLE.
  511. * @retval None
  512. */
  513. void TIM4_DMACmd( TIM4_DMASource_TypeDef TIM4_DMASource, FunctionalState NewState)
  514. {
  515. /* Check the parameters */
  516. assert_param(IS_FUNCTIONAL_STATE(NewState));
  517. assert_param(IS_TIM4_DMA_SOURCE(TIM4_DMASource));
  518. if (NewState != DISABLE)
  519. {
  520. /* Enable the DMA sources */
  521. TIM4->DER |= (uint8_t)TIM4_DMASource;
  522. }
  523. else
  524. {
  525. /* Disable the DMA sources */
  526. TIM4->DER &= (uint8_t)~TIM4_DMASource;
  527. }
  528. }
  529. /**
  530. * @}
  531. */
  532. /** @defgroup TIM4_Group3 Clocks management functions
  533. * @brief Clocks management functions
  534. *
  535. @verbatim
  536. ===============================================================================
  537. Clocks management functions
  538. ===============================================================================
  539. @endverbatim
  540. * @{
  541. */
  542. /**
  543. * @brief Enables the TIM4 internal Clock.
  544. * @param None
  545. * @retval None
  546. */
  547. void TIM4_InternalClockConfig(void)
  548. {
  549. /* Disable slave mode to clock the prescaler directly with the internal clock */
  550. TIM4->SMCR &= (uint8_t)(~TIM4_SMCR_SMS);
  551. }
  552. /**
  553. * @}
  554. */
  555. /** @defgroup TIM4_Group4 Synchronization management functions
  556. * @brief Synchronization management functions
  557. *
  558. @verbatim
  559. ===============================================================================
  560. Synchronization management functions
  561. ===============================================================================
  562. ===================================================================
  563. TIM4 Driver: how to use it in synchronization Mode
  564. ===================================================================
  565. Case of two/several Timers
  566. **************************
  567. 1. If TIM4 is used as master to other timers use the following functions:
  568. - TIM4_SelectOutputTrigger()
  569. - TIM4_SelectMasterSlaveMode()
  570. 2. If TIM4 is used as slave to other timers use the following functions:
  571. - TIM4_SelectInputTrigger()
  572. - TIM4_SelectSlaveMode()
  573. @endverbatim
  574. * @{
  575. */
  576. /**
  577. * @brief Selects the TIM4 Input Trigger source.
  578. * @param TIM4_InputTriggerSource: Specifies Input Trigger source.
  579. * This parameter can be one of the following values:
  580. * @arg TIM4_TRGSelection_TIM5: TRIG Input source = TIM5 TRIG Output
  581. * @arg TIM4_TRGSelection_TIM1: TRIG Input source = TIM1 TRIG Output
  582. * @arg TIM4_TRGSelection_TIM3: TRIG Input source = TIM3 TRIG Output
  583. * @arg TIM4_TRGSelection_TIM2: TRIG Input source = TIM2 TRIG Output
  584. * @retval None
  585. */
  586. void TIM4_SelectInputTrigger(TIM4_TRGSelection_TypeDef TIM4_InputTriggerSource)
  587. {
  588. uint8_t tmpsmcr = 0;
  589. /* Check the parameters */
  590. assert_param(IS_TIM4_TRIGGER_SELECTION(TIM4_InputTriggerSource));
  591. tmpsmcr = TIM4->SMCR;
  592. /* Select the Trigger Source */
  593. tmpsmcr &= (uint8_t)(~TIM4_SMCR_TS);
  594. tmpsmcr |= (uint8_t)TIM4_InputTriggerSource;
  595. TIM4->SMCR = (uint8_t)tmpsmcr;
  596. }
  597. /**
  598. * @brief Selects the TIM4 Trigger Output Mode.
  599. * @param TIM4_TRGOSource: Specifies the Trigger Output source.
  600. * This parameter can be one of the following values:
  601. * @arg TIM4_TRGOSource_Reset: Trigger Output source = Reset
  602. * @arg TIM4_TRGOSource_Enable: Trigger Output source = TIM4 is enabled
  603. * @arg TIM4_TRGOSource_Update: Trigger Output source = Update event
  604. * @retval None
  605. */
  606. void TIM4_SelectOutputTrigger(TIM4_TRGOSource_TypeDef TIM4_TRGOSource)
  607. {
  608. uint8_t tmpcr2 = 0;
  609. /* Check the parameters */
  610. assert_param(IS_TIM4_TRGO_SOURCE(TIM4_TRGOSource));
  611. tmpcr2 = TIM4->CR2;
  612. /* Reset the MMS Bits */
  613. tmpcr2 &= (uint8_t)(~TIM4_CR2_MMS);
  614. /* Select the TRGO source */
  615. tmpcr2 |= (uint8_t)TIM4_TRGOSource;
  616. TIM4->CR2 = tmpcr2;
  617. }
  618. /**
  619. * @brief Selects the TIM4 Slave Mode.
  620. * @param TIM4_SlaveMode: Specifies the TIM4 Slave Mode.
  621. * This parameter can be one of the following values:
  622. * @arg TIM4_SlaveMode_Disable: Disable slave mode to clock the prescaler
  623. directly with the internal clock
  624. * @arg TIM4_SlaveMode_Reset: Slave Mode Selection = Reset
  625. * @arg TIM4_SlaveMode_Gated: Slave Mode Selection = Gated
  626. * @arg TIM4_SlaveMode_Trigger: Slave Mode Selection = Trigger
  627. * @arg TIM4_SlaveMode_External1: Slave Mode Selection = External 1
  628. * @retval None
  629. */
  630. void TIM4_SelectSlaveMode(TIM4_SlaveMode_TypeDef TIM4_SlaveMode)
  631. {
  632. uint8_t tmpsmcr = 0;
  633. /* Check the parameters */
  634. assert_param(IS_TIM4_SLAVE_MODE(TIM4_SlaveMode));
  635. tmpsmcr = TIM4->SMCR;
  636. /* Reset the SMS Bits */
  637. tmpsmcr &= (uint8_t)(~TIM4_SMCR_SMS);
  638. /* Select the Slave Mode */
  639. tmpsmcr |= (uint8_t)TIM4_SlaveMode;
  640. TIM4->SMCR = tmpsmcr;
  641. }
  642. /**
  643. * @brief Sets or Resets the TIM4 Master/Slave Mode.
  644. * @param NewState: The new state of the synchronization between TIM4 and its slaves (through TRGO).
  645. * This parameter can be ENABLE or DISABLE
  646. * @retval None
  647. */
  648. void TIM4_SelectMasterSlaveMode(FunctionalState NewState)
  649. {
  650. /* Check the parameters */
  651. assert_param(IS_FUNCTIONAL_STATE(NewState));
  652. /* Set or Reset the MSM Bit */
  653. if (NewState != DISABLE)
  654. {
  655. TIM4->SMCR |= TIM4_SMCR_MSM;
  656. }
  657. else
  658. {
  659. TIM4->SMCR &= (uint8_t)(~TIM4_SMCR_MSM);
  660. }
  661. }
  662. /**
  663. * @}
  664. */
  665. /**
  666. * @}
  667. */
  668. /**
  669. * @}
  670. */
  671. /**
  672. * @}
  673. */
  674. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/