stm32f4xx_lptim.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_lptim.c
  4. * @author MCD Application Team
  5. * @version V1.8.1
  6. * @date 27-January-2022
  7. * @brief This file provides firmware functions to manage the following
  8. * functionalities of the Low Power Timer (LPT) peripheral:
  9. * + Initialization functions.
  10. * + Configuration functions.
  11. * + Interrupts and flags management functions.
  12. *
  13. * @verbatim
  14. *
  15. ================================================================================
  16. ##### How to use this driver #####
  17. ================================================================================
  18. Basic configuration:
  19. --------------------
  20. - Configure the clock source, the prescaler, the waveform shape and
  21. the output polarity by filling the "LPTIM_InitTypeDef" structure and
  22. calling LPTIM_Init.
  23. - If the ULPTIM source is selected as clock source, configure the digital
  24. Glitch filter by setting the number of consecutive samples
  25. to be detected by using LPTIM_ConfigClockGlitchFilter.
  26. - To select a software start use LPTIM_SelectSoftwareStart.
  27. - To select an external trigger for the start of the counter, configure
  28. the source and its active edge polarity by calling
  29. LPTIM_ConfigExternalTrigger. Configure the Digital Glitch filter for
  30. the external triggers by setting the number of consecutive samples
  31. to be detected by using LPTIM_ConfigTriggerGlitchFilter.
  32. - Select the operating mode of the peripheral by using
  33. LPTIM_SelectOperatingMode, 2 modes can be selected:
  34. + Continuous mode: the timer is free running, the timer is started
  35. from a trigger event and never stops until the timer is disabled
  36. + One shot mode: the timer is started from a trigger event and
  37. stops when reaching the auto-reload value.
  38. - Use LPTIM_SetAutoreloadValue to set the auto-reload value and
  39. LPTIM_SetCompareValue to set the compare value.
  40. - Configure the preload mode by using LPTIM_ConfigUpdate function. 2 modes
  41. are available:
  42. + The Autoreload and compare registers are updated immediately after
  43. APB write.
  44. + The Autoreload and compare registers are updated at the end of
  45. counter period.
  46. - Enable the peripheral by calling LPTIM_Cmd.
  47. Encoder mode:
  48. -------------
  49. - To select the encoder feature, use the function: LPTIM_SelectEncoderMode.
  50. - To select on which edge (Rising edge, falling edge or both edges)
  51. the counter is incremented, use LPTIM_SelectClockPolarity.
  52. Counter mode:
  53. -------------
  54. - Use LPTIM_SelectCounterMode to select the counting mode. In this mode
  55. the counter is incremented on each valid event on ULPTIM.
  56. Timeout function:
  57. -----------------
  58. In this case, the trigger will reset the timer. The first trigger event
  59. will start the timer, any successive trigger event will reset the counter
  60. and the timer restarts.
  61. - To active this feature use LPTIM_TimoutCmd.
  62. Interrupt configuration:
  63. ------------------------
  64. - Use LPTIM_ITConfig to configure an interruption.
  65. - Call LPTIM_GetFlagStatus to get a flag status.
  66. - Call LPTIM_GetITStatus to get an interrupt status.
  67. - Use LPTIM_ClearFlag to clear a flag.
  68. @endverbatim
  69. *
  70. ******************************************************************************
  71. * @attention
  72. *
  73. * Copyright (c) 2016 STMicroelectronics.
  74. * All rights reserved.
  75. *
  76. * This software is licensed under terms that can be found in the LICENSE file
  77. * in the root directory of this software component.
  78. * If no LICENSE file comes with this software, it is provided AS-IS.
  79. *
  80. ******************************************************************************
  81. */
  82. /* Includes ------------------------------------------------------------------*/
  83. #include "stm32f4xx_lptim.h"
  84. /** @addtogroup STM32F4xx_StdPeriph_Driver
  85. * @{
  86. */
  87. /** @defgroup LPTIM
  88. * @brief LPTIM driver modules
  89. * @{
  90. */
  91. #if defined(STM32F410xx) || defined(STM32F413_423xx)
  92. /* External variables --------------------------------------------------------*/
  93. /* Private typedef -----------------------------------------------------------*/
  94. /* Private defines -----------------------------------------------------------*/
  95. #define CFGR_INIT_CLEAR_MASK ((uint32_t) 0xFFCFF1FE)
  96. #define CFGR_TRIG_AND_POL_CLEAR_MASK ((uint32_t) 0xFFF91FFF)
  97. /* Private macros ------------------------------------------------------------*/
  98. /* Private variables ---------------------------------------------------------*/
  99. /* Private function prototypes -----------------------------------------------*/
  100. /* Private functions ---------------------------------------------------------*/
  101. /** @defgroup LPTIM_Private_Functions
  102. * @{
  103. */
  104. /** @defgroup LPTIM_Group1 Initialization functions
  105. * @brief Initialization functions
  106. *
  107. @verbatim
  108. ===============================================================================
  109. Initialization functions
  110. ===============================================================================
  111. This section provides functions allowing to:
  112. - Deinitialize the LPTimer
  113. - Initialize the Clock source, the Prescaler, the Ouput Waveform shape and Polarity
  114. - Initialize the member of LPTIM_InitStruct structer with default value
  115. @endverbatim
  116. * @{
  117. */
  118. /**
  119. * @brief Deinitializes the LPTIMx peripheral registers to their default reset values.
  120. * @param LPTIMx: where x can be 1.
  121. * @retval None
  122. *
  123. */
  124. void LPTIM_DeInit(LPTIM_TypeDef* LPTIMx)
  125. {
  126. /* Check the parameters */
  127. assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx));
  128. /* Deinitializes the LPTIM1 peripheral */
  129. if(LPTIMx == LPTIM1)
  130. {
  131. RCC_APB1PeriphResetCmd(RCC_APB1Periph_LPTIM1, ENABLE);
  132. RCC_APB1PeriphResetCmd(RCC_APB1Periph_LPTIM1, DISABLE);
  133. }
  134. }
  135. /**
  136. * @brief Initializes the LPTIMx peripheral according to the specified parameters
  137. * in the LPTIM_InitStruct.
  138. * @param LPTIMx: where x can be 1.
  139. * @param LPTIM_InitStruct: pointer to an LPTIM_InitTypeDef structure that contains
  140. * the configuration information for the specified LPTIM peripheral.
  141. * @retval None
  142. *
  143. * @note It is mandatory to disable the peripheral to use this function.
  144. */
  145. void LPTIM_Init(LPTIM_TypeDef* LPTIMx, LPTIM_InitTypeDef* LPTIM_InitStruct)
  146. {
  147. uint32_t tmpreg1 = 0;
  148. /* Check the parameters */
  149. assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx));
  150. assert_param(IS_LPTIM_CLOCK_SOURCE(LPTIM_InitStruct->LPTIM_ClockSource));
  151. assert_param(IS_LPTIM_CLOCK_PRESCALER(LPTIM_InitStruct->LPTIM_Prescaler));
  152. assert_param(IS_LPTIM_WAVEFORM(LPTIM_InitStruct->LPTIM_Waveform));
  153. assert_param(IS_LPTIM_OUTPUT_POLARITY(LPTIM_InitStruct->LPTIM_OutputPolarity));
  154. /* Get the LPTIMx CFGR value */
  155. tmpreg1 = LPTIMx->CFGR;
  156. /* Clear CKSEL, PRESC, WAVE and WAVEPOL bits */
  157. tmpreg1 &= CFGR_INIT_CLEAR_MASK;
  158. /* Set or Reset CKSEL bit according to LPTIM_ClockSource value */
  159. /* Set or Reset PRESC bits according to LPTIM_Prescaler value */
  160. /* Set or Reset WAVE bit according to LPTIM_Waveform value */
  161. /* Set or Reset WAVEPOL bit according to LPTIM_OutputPolarity value */
  162. tmpreg1 |= (LPTIM_InitStruct->LPTIM_ClockSource | LPTIM_InitStruct->LPTIM_Prescaler
  163. |LPTIM_InitStruct->LPTIM_Waveform | LPTIM_InitStruct->LPTIM_OutputPolarity);
  164. /* Write to LPTIMx CFGR */
  165. LPTIMx->CFGR = tmpreg1;
  166. }
  167. /**
  168. * @brief Fills each LPTIM_InitStruct member with its default value.
  169. * @param LPTIM_InitStruct : pointer to a LPTIM_InitTypeDef structure which will be initialized.
  170. * @retval None
  171. */
  172. void LPTIM_StructInit(LPTIM_InitTypeDef* LPTIM_InitStruct)
  173. {
  174. /* APB Clock/Low Power oscillators is selected as default Clock source*/
  175. LPTIM_InitStruct->LPTIM_ClockSource = LPTIM_ClockSource_APBClock_LPosc;
  176. /* High Polarity is selected as default polarity */
  177. LPTIM_InitStruct->LPTIM_OutputPolarity = LPTIM_OutputPolarity_High;
  178. /* DIV=1 is selected as default prescaler */
  179. LPTIM_InitStruct->LPTIM_Prescaler = LPTIM_Prescaler_DIV1;
  180. /* PWM/One pulse mode is selected as default Waveform shape */
  181. LPTIM_InitStruct->LPTIM_Waveform = LPTIM_Waveform_PWM_OnePulse;
  182. }
  183. /**
  184. * @}
  185. */
  186. /** @defgroup LPTIM_Group2 Configuration functions
  187. * @brief Configuration functions
  188. *
  189. @verbatim
  190. ===============================================================================
  191. Configuration functions
  192. ===============================================================================
  193. This section provides functions allowing to configure the Low Power Timer:
  194. - Select the Clock source.
  195. - Configure the Glitch filter for the external clock and the external clock.
  196. - Configure the prescaler of the counter.
  197. - Select the Trigger source of the counter.
  198. - Configure the operating mode (Single or Continuous mode).
  199. - Select the Waveform shape (PWM/One Pulse or Set once) and polarity.
  200. - Enable or disable the Encoder mode and the Timeout function.
  201. - Write on the Autoreload and the Compare registers and configure the
  202. preload mode.
  203. - Get the Counter value.
  204. - Enable or disable the peripheral.
  205. @endverbatim
  206. * @{
  207. */
  208. /**
  209. * @brief Enables or disables the specified LPTIM peripheral.
  210. * @param LPTIMx: where x can be 1.
  211. * @param NewState: new state of the LPTIMx peripheral.
  212. * This parameter can be: ENABLE or DISABLE.
  213. * @retval None
  214. */
  215. void LPTIM_Cmd(LPTIM_TypeDef* LPTIMx, FunctionalState NewState)
  216. {
  217. /* Check the parameters */
  218. assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx));
  219. assert_param(IS_FUNCTIONAL_STATE(NewState));
  220. if(NewState != DISABLE)
  221. {
  222. /* Set the ENABLE bit */
  223. LPTIMx->CR |= LPTIM_CR_ENABLE;
  224. }
  225. else
  226. {
  227. /* Reset the ENABLE bit */
  228. LPTIMx->CR &= ~(LPTIM_CR_ENABLE);
  229. }
  230. }
  231. /**
  232. * @brief Selects the Clock source of the LPTIM counter.
  233. * @param LPTIMx: where x can be 1.
  234. * @param LPTIM_ClockSource: the selected clock source.
  235. * This parameter can be:
  236. * @arg LPTIM_ClockSource_APBClock_LPosc : APB clock/LP oscillators selected
  237. * @arg LPTIM_ClockSource_ULPTIM: ULPTIM (external input) selected
  238. * @retval None
  239. *
  240. * @note It is mandatory to disable the peripheral to use this function.
  241. */
  242. void LPTIM_SelectClockSource(LPTIM_TypeDef* LPTIMx, uint32_t LPTIM_ClockSource)
  243. {
  244. /* Check the parameters */
  245. assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx));
  246. assert_param(IS_LPTIM_CLOCK_SOURCE(LPTIM_ClockSource));
  247. /* Clear the CKSEL bit */
  248. LPTIMx->CFGR &= ~(LPTIM_CFGR_CKSEL);
  249. /* Set or Reset the CKSEL bit */
  250. LPTIMx->CFGR |= LPTIM_ClockSource;
  251. }
  252. /**
  253. * @brief Configures the polarity of the edge to be used to count
  254. * if the ULPTIM input is selected.
  255. * @param LPTIMx: where x can be 1.
  256. * @param LPTIM_ClockPolarity: the selected clock polarity.
  257. * This parameter can be:
  258. * @arg LPTIM_ClockPolarity_RisingEdge : Counter Clock = LPTIM Clock / 1
  259. * @arg LPTIM_ClockPolarity_FallingEdge : Counter Clock = LPTIM Clock / 2
  260. * @arg LPTIM_ClockPolarity_BothEdges : Counter Clock = LPTIM Clock / 4
  261. * @retval None
  262. *
  263. * @note It is mandatory to disable the peripheral to use this function.
  264. */
  265. void LPTIM_SelectULPTIMClockPolarity(LPTIM_TypeDef* LPTIMx, uint32_t LPTIM_ClockPolarity)
  266. {
  267. uint32_t tmpreg1 = 0;
  268. /* Check the parameters */
  269. assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx));
  270. assert_param(IS_LPTIM_CLOCK_POLARITY(LPTIM_ClockPolarity));
  271. /* Get the LPTIMx CFGR value */
  272. tmpreg1 = LPTIMx->CFGR;
  273. /* Clear the CKPOL bits */
  274. tmpreg1 &= ~(LPTIM_CFGR_CKPOL);
  275. /* Set or Reset the PRESC bits */
  276. tmpreg1 |= LPTIM_ClockPolarity;
  277. /* Write to LPTIMx CFGR */
  278. LPTIMx->CFGR = tmpreg1;
  279. }
  280. /**
  281. * @brief Configures the Clock Prescaler.
  282. * @param LPTIMx: where x can be 1.
  283. * @param LPTIM_Prescaler: the selected clock prescaler.
  284. * This parameter can be:
  285. * @arg LPTIM_Prescaler_DIV1 : Counter Clock = LPTIM Clock / 1
  286. * @arg LPTIM_Prescaler_DIV2 : Counter Clock = LPTIM Clock / 2
  287. * @arg LPTIM_Prescaler_DIV4 : Counter Clock = LPTIM Clock / 4
  288. * @arg LPTIM_Prescaler_DIV8 : Counter Clock = LPTIM Clock / 8
  289. * @arg LPTIM_Prescaler_DIV16 : Counter Clock = LPTIM Clock / 16
  290. * @arg LPTIM_Prescaler_DIV32 : Counter Clock = LPTIM Clock / 32
  291. * @arg LPTIM_Prescaler_DIV64 : Counter Clock = LPTIM Clock / 64
  292. * @arg LPTIM_Prescaler_DIV128 : Counter Clock = LPTIM Clock / 128
  293. * @retval None
  294. *
  295. * @note It is mandatory to disable the peripheral to use this function.
  296. */
  297. void LPTIM_ConfigPrescaler(LPTIM_TypeDef* LPTIMx, uint32_t LPTIM_Prescaler)
  298. {
  299. uint32_t tmpreg1 = 0;
  300. /* Check the parameters */
  301. assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx));
  302. assert_param(IS_LPTIM_CLOCK_PRESCALER(LPTIM_Prescaler));
  303. /* Get the LPTIMx CFGR value */
  304. tmpreg1 = LPTIMx->CFGR;
  305. /* Clear the PRESC bits */
  306. tmpreg1 &= ~(LPTIM_CFGR_PRESC);
  307. /* Set or Reset the PRESC bits */
  308. tmpreg1 |= LPTIM_Prescaler;
  309. /* Write to LPTIMx CFGR */
  310. LPTIMx->CFGR = tmpreg1;
  311. }
  312. /**
  313. * @brief Selects the trigger source for the counter and its polarity.
  314. * @param LPTIMx: where x can be 1.
  315. * @param LPTIM_ExtTRGSource: the selected external trigger.
  316. * This parameter can be:
  317. * @arg LPTIM_ExtTRGSource_Trig0 : ext_trig0
  318. * @arg LPTIM_ExtTRGSource_Trig1 : ext_trig1
  319. * @arg LPTIM_ExtTRGSource_Trig2 : ext_trig2
  320. * @arg LPTIM_ExtTRGSource_Trig3 : ext_trig3
  321. * @arg LPTIM_ExtTRGSource_Trig4 : ext_trig4
  322. * @arg LPTIM_ExtTRGSource_Trig5 : ext_trig5
  323. * @arg LPTIM_ExtTRGSource_Trig6 : ext_trig6
  324. * @arg LPTIM_ExtTRGSource_Trig7 : ext_trig7
  325. * @param LPTIM_ExtTRGPolarity: the selected external trigger.
  326. * This parameter can be:
  327. * @arg LPTIM_ExtTRGPolarity_RisingEdge : Rising edge polarity selected
  328. * @arg LPTIM_ExtTRGPolarity_FallingEdge : Falling edge polarity selected
  329. * @arg LPTIM_ExtTRGPolarity_BothEdges : Both edges polarity selected
  330. * @retval None
  331. *
  332. * @note It is mandatory to disable the peripheral to use this function.
  333. */
  334. void LPTIM_ConfigExternalTrigger(LPTIM_TypeDef* LPTIMx, uint32_t LPTIM_ExtTRGSource, uint32_t LPTIM_ExtTRGPolarity)
  335. {
  336. uint32_t tmpreg1 = 0;
  337. /* Check the parameters */
  338. assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx));
  339. assert_param(IS_LPTIM_EXT_TRG_SOURCE(LPTIM_ExtTRGSource));
  340. assert_param(IS_LPTIM_EXT_TRG_POLARITY(LPTIM_ExtTRGPolarity));
  341. /* Get the LPTIMx CFGR value */
  342. tmpreg1 = LPTIMx->CFGR;
  343. /* Clear the TRIGEN and TRIGSEL bits */
  344. tmpreg1 &= CFGR_TRIG_AND_POL_CLEAR_MASK;
  345. /* Set or Reset the TRIGEN and TRIGSEL bits */
  346. tmpreg1 |= (LPTIM_ExtTRGSource | LPTIM_ExtTRGPolarity);
  347. /* Write to LPTIMx CFGR */
  348. LPTIMx->CFGR = tmpreg1;
  349. }
  350. /**
  351. * @brief Selects a software start of the counter.
  352. * @param LPTIMx: where x can be 1.
  353. * @retval None
  354. *
  355. * @note It is mandatory to disable the peripheral to use this function.
  356. */
  357. void LPTIM_SelectSoftwareStart(LPTIM_TypeDef* LPTIMx)
  358. {
  359. /* Check the parameters */
  360. assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx));
  361. /* Reset the TRIGEN bits to allow a software start */
  362. LPTIMx->CFGR &= ~(LPTIM_CFGR_TRIGEN);
  363. }
  364. /**
  365. * @brief Configures the digital filter for trigger by determining the number of consecutive
  366. * samples at the specified level to detect a correct transition.
  367. * @param LPTIMx: where x can be 1.
  368. * @param LPTIM_TrigSampleTime: the number of samples to detect a valid transition.
  369. * This parameter can be:
  370. * @arg LPTIM_TrigSampleTime_DirectTransistion : Event is detected on input transitions
  371. * @arg LPTIM_TrigSampleTime_2Transistions : Event is detected after 2 consecutive samples at the active level
  372. * @arg LPTIM_TrigSampleTime_4Transistions : Event is detected after 4 consecutive samples at the active level
  373. * @arg LPTIM_TrigSampleTime_8Transistions : Event is detected after 8 consecutive samples at the active level
  374. * @retval None
  375. *
  376. * @note It is mandatory to disable the peripheral to use this function.
  377. * @note An auxiliary clock must be present to use this feature.
  378. */
  379. void LPTIM_ConfigTriggerGlitchFilter(LPTIM_TypeDef* LPTIMx, uint32_t LPTIM_TrigSampleTime)
  380. {
  381. uint32_t tmpreg1 = 0;
  382. /* Check the parameters */
  383. assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx));
  384. assert_param(IS_LPTIM_TRIG_SAMPLE_TIME(LPTIM_TrigSampleTime));
  385. /* Get the LPTIMx CFGR value */
  386. tmpreg1 = LPTIMx->CFGR;
  387. /* Clear the TRGFLT bits */
  388. tmpreg1 &= ~(LPTIM_CFGR_TRGFLT);
  389. /* Set or Reset the TRGFLT bits according to LPTIM_TrigSampleTime */
  390. tmpreg1 |= (LPTIM_TrigSampleTime);
  391. /* Write to LPTIMx CFGR */
  392. LPTIMx->CFGR = tmpreg1;
  393. }
  394. /**
  395. * @brief Configures the digital filter for the external clock by determining the number
  396. of consecutive samples at the specified level to detect a correct transition.
  397. * @param LPTIMx: where x can be 1.
  398. * @param LPTIM_ClockSampleTime: the number of samples to detect a valid transition.
  399. * This parameter can be:
  400. * @arg LPTIM_ClockSampleTime_DirectTransistion : Event is detected on input transitions
  401. * @arg LPTIM_ClockSampleTime_2Transistions : Event is detected after 2 consecutive samples at the active level
  402. * @arg LPTIM_ClockSampleTime_4Transistions : Event is detected after 4 consecutive samples at the active level
  403. * @arg LPTIM_ClockSampleTime_8Transistions : Event is detected after 8 consecutive samples at the active level
  404. * @retval None
  405. *
  406. * @note It is mandatory to disable the peripheral to use this function.
  407. * @note An auxiliary clock must be present to use this feature.
  408. */
  409. void LPTIM_ConfigClockGlitchFilter(LPTIM_TypeDef* LPTIMx, uint32_t LPTIM_ClockSampleTime)
  410. {
  411. uint32_t tmpreg1 = 0;
  412. /* Check the parameters */
  413. assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx));
  414. assert_param(IS_LPTIM_CLOCK_SAMPLE_TIME(LPTIM_ClockSampleTime));
  415. /* Get the LPTIMx CFGR value */
  416. tmpreg1 = LPTIMx->CFGR;
  417. /* Clear the CKFLT bits */
  418. tmpreg1 &= ~(LPTIM_CFGR_CKFLT);
  419. /* Set or Reset the CKFLT bits according to LPTIM_ClockSampleTime */
  420. tmpreg1 |= LPTIM_ClockSampleTime;
  421. /* Write to LPTIMx CFGR */
  422. LPTIMx->CFGR = tmpreg1;
  423. }
  424. /**
  425. * @brief Selects an operating mode.
  426. * @param LPTIMx: where x can be 1.
  427. * @param LPTIM_Mode: the selected mode.
  428. * This parameter can be:
  429. * @arg LPTIM_Mode_Continuous : Timer starts in Continuous mode
  430. * @arg LPTIM_Mode_Single : Timer will starts in Single mode
  431. * @retval None
  432. */
  433. void LPTIM_SelectOperatingMode(LPTIM_TypeDef* LPTIMx, uint32_t LPTIM_Mode)
  434. {
  435. /* Check the parameters */
  436. assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx));
  437. assert_param(IS_LPTIM_MODE(LPTIM_Mode));
  438. if(LPTIM_Mode == LPTIM_Mode_Continuous)
  439. {
  440. /* Set the CNTSTRT to select the continuous start*/
  441. LPTIMx->CR |= LPTIM_Mode_Continuous;
  442. }
  443. else /*LPTIM_Mode_Single */
  444. {
  445. /* Set the SNGSTRT to select the continuous start*/
  446. LPTIMx->CR |= LPTIM_Mode_Single;
  447. }
  448. }
  449. /**
  450. * @brief Enables or disables the Timeout function.
  451. * @param LPTIMx: where x can be 1.
  452. * @param NewState: new state of the Timeout function.
  453. * This parameter can be: ENABLE or DISABLE.
  454. * @retval None
  455. *
  456. * @note It is mandatory to disable the peripheral to use this function.
  457. */
  458. void LPTIM_TimoutCmd(LPTIM_TypeDef* LPTIMx, FunctionalState NewState)
  459. {
  460. /* Check the parameters */
  461. assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx));
  462. assert_param(IS_FUNCTIONAL_STATE(NewState));
  463. if(NewState != DISABLE)
  464. {
  465. /* Set the TIMOUT bit */
  466. LPTIMx->CFGR |= LPTIM_CFGR_TIMOUT;
  467. }
  468. else
  469. {
  470. /* Reset the TIMOUT bit */
  471. LPTIMx->CFGR &= ~(LPTIM_CFGR_TIMOUT);
  472. }
  473. }
  474. /**
  475. * @brief Configures the Waveform shape.
  476. * @param LPTIMx: where x can be 1.
  477. * @param LPTIM_Waveform: the selected waveform shape.
  478. * This parameter can be:
  479. * @arg LPTIM_Waveform_PWM_OnePulse : PWM/One Pulse is selected
  480. * @arg LPTIM_Waveform_SetOnce : Set once is selected
  481. * @retval None
  482. *
  483. * @note It is mandatory to disable the peripheral to use this function.
  484. */
  485. void LPTIM_ConfigWaveform(LPTIM_TypeDef* LPTIMx, uint32_t LPTIM_Waveform)
  486. {
  487. /* Check the parameters */
  488. assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx));
  489. assert_param(IS_LPTIM_WAVEFORM(LPTIM_Waveform));
  490. /* Clear the WAVE bit */
  491. LPTIMx->CFGR &= ~(LPTIM_CFGR_CKFLT);
  492. /* Set or Reset the WAVE bit according to LPTIM_Waveform */
  493. LPTIMx->CFGR |= (LPTIM_Waveform);
  494. }
  495. /**
  496. * @brief Configures the Autoreload and Compare registers update mode.
  497. * @param LPTIMx: where x can be 1.
  498. * @param LPTIM_Update: The selected update mode.
  499. * This parameter can be:
  500. * @arg LPTIM_Update_Immediate : Registers updated after APB write
  501. * @arg LPTIM_Update_EndOfPeriod : Registers updated at the end of current timer preload
  502. * @retval None
  503. *
  504. * @note It is mandatory to disable the peripheral to use this function.
  505. */
  506. void LPTIM_ConfigUpdate(LPTIM_TypeDef* LPTIMx, uint32_t LPTIM_Update)
  507. {
  508. /* Check the parameters */
  509. assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx));
  510. assert_param(IS_LPTIM_UPDATE(LPTIM_Update));
  511. /* Clear the PRELOAD bit */
  512. LPTIMx->CFGR &= ~(LPTIM_CFGR_PRELOAD);
  513. /* Set or Reset the PRELOAD bit according to LPTIM_Update */
  514. LPTIMx->CFGR |= (LPTIM_Update);
  515. }
  516. /**
  517. * @brief Writes the passed parameter in the Autoreload register.
  518. * @param LPTIMx: where x can be 1.
  519. * @param LPTIM_Autoreload: The Autoreload value.
  520. * This parameter must be a value between 0x0000 and 0xFFFF
  521. * @retval None
  522. */
  523. void LPTIM_SetAutoreloadValue(LPTIM_TypeDef* LPTIMx, uint32_t LPTIM_Autoreload)
  524. {
  525. /* Check the parameters */
  526. assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx));
  527. assert_param(IS_LPTIM_AUTORELOAD(LPTIM_Autoreload));
  528. /* Write LPTIM_Autoreload in Autoreload register */
  529. LPTIMx->ARR = LPTIM_Autoreload;
  530. }
  531. /**
  532. * @brief Writes the passed parameter in the Compare register.
  533. * @param LPTIMx: where x can be 1.
  534. * @param LPTIM_Compare: The Compare value.
  535. * This parameter must be a value between 0x0000 and 0xFFFF
  536. * @retval None
  537. */
  538. void LPTIM_SetCompareValue(LPTIM_TypeDef* LPTIMx, uint32_t LPTIM_Compare)
  539. {
  540. /* Check the parameters */
  541. assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx));
  542. assert_param(IS_LPTIM_COMPARE(LPTIM_Compare));
  543. /* Write LPTIM_Compare in Compare register */
  544. LPTIMx->CMP = LPTIM_Compare;
  545. }
  546. /**
  547. * @brief Enables or disables the Counter mode. When the Counter mode is enabled,
  548. * the counter is incremented each valid event on ULPTIM
  549. * @param LPTIMx: where x can be 1.
  550. * @param NewState: new state of the Counter mode.
  551. * This parameter can be: ENABLE or DISABLE.
  552. * @retval None
  553. *
  554. * @note It is mandatory to disable the peripheral to use this function.
  555. */
  556. void LPTIM_SelectCounterMode(LPTIM_TypeDef* LPTIMx, FunctionalState NewState)
  557. {
  558. /* Check the parameters */
  559. assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx));
  560. assert_param(IS_FUNCTIONAL_STATE(NewState));
  561. if(NewState != DISABLE)
  562. {
  563. /* Set the COUNTMODE bit */
  564. LPTIMx->CFGR |= LPTIM_CFGR_COUNTMODE;
  565. }
  566. else
  567. {
  568. /* Reset the COUNTMODE bit */
  569. LPTIMx->CFGR &= ~(LPTIM_CFGR_COUNTMODE);
  570. }
  571. }
  572. /**
  573. * @brief Enables or disables the Encoder mode.
  574. * @param LPTIMx: where x can be 1.
  575. * @param NewState: New state of the encoder mode.
  576. * This parameter can be: ENABLE or DISABLE.
  577. * @retval None
  578. *
  579. * @note It is mandatory to disable the peripheral to use this function.
  580. */
  581. void LPTIM_SelectEncoderMode(LPTIM_TypeDef* LPTIMx, FunctionalState NewState)
  582. {
  583. /* Check the parameters */
  584. assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx));
  585. assert_param(IS_FUNCTIONAL_STATE(NewState));
  586. if(NewState != DISABLE)
  587. {
  588. /* Set the ENC bit */
  589. LPTIMx->CFGR |= LPTIM_CFGR_ENC;
  590. }
  591. else
  592. {
  593. /* Reset the ENC bit */
  594. LPTIMx->CFGR &= ~(LPTIM_CFGR_ENC);
  595. }
  596. }
  597. /**
  598. * @brief Gets the LPTIMx counter value.
  599. * @param LPTIMx: where x can be 1.
  600. * @retval Counter Register value
  601. */
  602. uint32_t LPTIM_GetCounterValue(LPTIM_TypeDef* LPTIMx)
  603. {
  604. /* Check the parameters */
  605. assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx));
  606. /* Get the Counter Register value */
  607. return LPTIMx->CNT;
  608. }
  609. /**
  610. * @brief Gets the LPTIMx Autoreload value.
  611. * @param LPTIMx: where x can be 1.
  612. * @retval Counter Register value
  613. */
  614. uint32_t LPTIM_GetAutoreloadValue(LPTIM_TypeDef* LPTIMx)
  615. {
  616. /* Check the parameters */
  617. assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx));
  618. /* Get the Counter Register value */
  619. return LPTIMx->ARR;
  620. }
  621. /**
  622. * @brief Gets the LPTIMx Compare value.
  623. * @param LPTIMx: where x can be 1.
  624. * @retval Counter Register value
  625. */
  626. uint32_t LPTIM_GetCompareValue(LPTIM_TypeDef* LPTIMx)
  627. {
  628. /* Check the parameters */
  629. assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx));
  630. /* Get the Counter Register value */
  631. return LPTIMx->CMP;
  632. }
  633. /**
  634. * @brief LPTIM Input 1 Remap.
  635. * @param LPTIMx: where x can be 1.
  636. * @param LPTIM_OPTR :
  637. * This Parameter can be :
  638. * @arg LPTIM_OP_PAD_AF : Port B5 on AF1 or Port C0 on AF1 for input timer
  639. * @arg LPTIM_OP_PAD_PA4 : Input remapped to Port A4
  640. * @arg RCC_LPTIM1CLKSOURCE_LSI : Input remapped to Port B9
  641. * @arg LPTIM_OP_TIM_DAC : Input coming from timer 6 output (for encoder mode)
  642. * @retval Counter Register value
  643. */
  644. void LPTIM_RemapConfig(LPTIM_TypeDef* LPTIMx, uint32_t LPTIM_OPTR)
  645. {
  646. /* Check the parameters */
  647. assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx));
  648. /* Get the Counter Register value */
  649. LPTIMx->OR = LPTIM_OPTR;
  650. }
  651. /**
  652. * @}
  653. */
  654. /** @defgroup LPTIM_Group3 Interrupts and flags management functions
  655. * @brief Interrupts and flags management functions
  656. *
  657. @verbatim
  658. ===============================================================================
  659. Interrupts and flags management functions
  660. ===============================================================================
  661. This section provides functions allowing to configure the LPTIM Interrupts, get
  662. the status and clear flags bits.
  663. The LPTIM provides 7 Flags and Interrupts sources (2 flags and Interrupt sources
  664. are available only on LPTIM peripherals equipped with encoder mode interface)
  665. Flags and Interrupts sources:
  666. =============================
  667. 1. Compare match.
  668. 2. Auto-reload match.
  669. 3. External trigger event.
  670. 4. Autoreloaded register write completed.
  671. 5. Compare register write completed.
  672. 6. Direction change: from up to down [Available only for LPTIM peripheral with
  673. encoder mode module]
  674. 7. Direction change: from down to up [Available only for LPTIM peripheral with
  675. encoder mode module]
  676. - To enable a specific interrupt source, use "LPTIM_ITConfig" function.
  677. - To check if an interrupt was occurred, call "LPTIM_GetITStatus" function and read
  678. the returned value.
  679. - To get a flag status, call the "LPTIM_GetFlagStatus" function and read the returned
  680. value.
  681. - To clear a flag or an interrupt, use LPTIM_ClearFlag function with the
  682. corresponding flag (interrupt).
  683. @endverbatim
  684. * @{
  685. */
  686. /**
  687. * @brief Enables or disables the specified LPTIM interrupts.
  688. * @param LPTIMx: where x can be 1.
  689. * @param LPTIM_IT: specifies the TIM interrupts sources to be enabled or disabled.
  690. * This parameter can be any combination of the following values:
  691. * @arg LPTIM_IT_DOWN: Counter direction change up to down Interrupt source
  692. * @arg LPTIM_IT_UP: Counter direction change down to up Interrupt source
  693. * @arg LPTIM_IT_ARROK: Autoreload register update OK Interrupt source
  694. * @arg LPTIM_IT_CMPOK: Compare register update OK Interrupt source
  695. * @arg LPTIM_IT_EXTTRIG: External trigger edge event Interrupt source
  696. * @arg LPTIM_IT_ARRM: Autoreload match Interrupt source
  697. * @arg LPTIM_IT_CMPM: Compare match Interrupt source
  698. * @note LPTIM_IT_DOWN is available only for LPTIM1.
  699. * @note LPTIM_IT_UP is available only for LPTIM1.
  700. * @param NewState: new state of the TIM interrupts.
  701. * This parameter can be: ENABLE or DISABLE.
  702. * @retval None
  703. *
  704. * @note It is mandatory to disable the peripheral to use this function.
  705. */
  706. void LPTIM_ITConfig(LPTIM_TypeDef* LPTIMx, uint32_t LPTIM_IT, FunctionalState NewState)
  707. {
  708. /* Check the parameters */
  709. assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx));
  710. assert_param(IS_LPTIM_IT(LPTIM_IT));
  711. assert_param(IS_FUNCTIONAL_STATE(NewState));
  712. if(NewState != DISABLE)
  713. {
  714. /* Enable the Interrupt sources */
  715. LPTIMx->IER |= LPTIM_IT;
  716. }
  717. else
  718. {
  719. /* Disable the Interrupt sources */
  720. LPTIMx->IER &= ~(LPTIM_IT);
  721. }
  722. }
  723. /**
  724. * @brief Checks whether the specified LPTIM flag is set or not.
  725. * @param LPTIMx: where x can be 1.
  726. * @param LPTIM_FLAG: specifies the flag to check.
  727. * This parameter can be any combination of the following values:
  728. * @arg LPTIM_FLAG_DOWN: Counter direction change up Flag
  729. * @arg LPTIM_FLAG_UP: Counter direction change down to up Flag
  730. * @arg LPTIM_FLAG_ARROK: Autoreload register update OK Flag
  731. * @arg LPTIM_FLAG_CMPOK: Compare register update OK Flag
  732. * @arg LPTIM_FLAG_EXTTRIG: External trigger edge event Flag
  733. * @arg LPTIM_FLAG_ARRM: Autoreload match Flag
  734. * @arg LPTIM_FLAG_CMPM: Compare match Flag
  735. * @note LPTIM_Flag_DOWN is generated only for LPTIM1.
  736. * @note LPTIM_Flag_UP is generated only for LPTIM1.
  737. * @param NewState: new state of the TIM interrupts.
  738. * This parameter can be: ENABLE or DISABLE.
  739. * @retval None
  740. */
  741. FlagStatus LPTIM_GetFlagStatus(LPTIM_TypeDef* LPTIMx, uint32_t LPTIM_FLAG)
  742. {
  743. ITStatus bitstatus = RESET;
  744. /* Check the parameters */
  745. assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx));
  746. assert_param(IS_LPTIM_GET_FLAG(LPTIM_FLAG));
  747. if((LPTIMx->ISR & LPTIM_FLAG) != (RESET))
  748. {
  749. bitstatus = SET;
  750. }
  751. else
  752. {
  753. bitstatus = RESET;
  754. }
  755. return bitstatus;
  756. }
  757. /**
  758. * @brief Clears the LPTIMx's pending flag.
  759. * @param LPTIMx: where x can be 1.
  760. * @param LPTIM_CLEARF: specifies the pending bit to clear.
  761. * This parameter can be any combination of the following values:
  762. * @arg LPTIM_CLEARF_DOWN: Counter direction change up Clear Flag
  763. * @arg LPTIM_CLEARF_UP: Counter direction change down to up Clear Flag
  764. * @arg LPTIM_CLEARF_ARROK: Autoreload register update OK Clear Flag
  765. * @arg LPTIM_CLEARF_CMPOK: Compare register update OK Clear Flag
  766. * @arg LPTIM_CLEARF_EXTTRIG: External trigger edge event Clear Flag
  767. * @arg LPTIM_CLEARF_ARRM: Autoreload match Clear Flag
  768. * @arg LPTIM_CLEARF_CMPM: Compare match Clear Flag
  769. * @note LPTIM_Flag_DOWN is generated only for LPTIM1.
  770. * @note LPTIM_Flag_UP is generated only for LPTIM1.
  771. * @retval None
  772. */
  773. void LPTIM_ClearFlag(LPTIM_TypeDef* LPTIMx, uint32_t LPTIM_CLEARF)
  774. {
  775. /* Check the parameters */
  776. assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx));
  777. assert_param(IS_LPTIM_CLEAR_FLAG(LPTIM_CLEARF));
  778. /* Clear the IT pending Bit */
  779. LPTIMx->ICR |= LPTIM_CLEARF;
  780. }
  781. /**
  782. * @brief Check whether the specified LPTIM interrupt has occurred or not.
  783. * @param LPTIMx: where x can be 1.
  784. * @param LPTIM_IT: specifies the LPTIM interrupt source to check.
  785. * @arg LPTIM_IT_DOWN: Counter direction change up to down Interrupt source
  786. * @arg LPTIM_IT_UP: Counter direction change down to up Interrupt source
  787. * @arg LPTIM_IT_ARROK: Autoreload register update OK Interrupt source
  788. * @arg LPTIM_IT_CMPOK: Compare register update OK Interrupt source
  789. * @arg LPTIM_IT_EXTTRIG: External trigger edge event Interrupt source
  790. * @arg LPTIM_IT_ARRM: Autoreload match Interrupt source
  791. * @arg LPTIM_IT_CMPM: Compare match Interrupt source
  792. * @retval The new state of LPTIM_IT (SET or RESET).
  793. */
  794. ITStatus LPTIM_GetITStatus(LPTIM_TypeDef* LPTIMx, uint32_t LPTIM_IT)
  795. {
  796. ITStatus bitstatus = RESET;
  797. uint32_t itstatus = 0x0, itenable = 0x0;
  798. /* Check the parameters */
  799. assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx));
  800. assert_param(IS_LPTIM_IT(LPTIM_IT));
  801. /* Get the Interrupt Status bit value */
  802. itstatus = LPTIMx->ISR & LPTIM_IT;
  803. /* Check if the Interrupt is enabled */
  804. itenable = LPTIMx->IER & LPTIM_IT;
  805. if((itstatus != RESET) && (itenable != RESET))
  806. {
  807. bitstatus = SET;
  808. }
  809. else
  810. {
  811. bitstatus = RESET;
  812. }
  813. return bitstatus;
  814. }
  815. /**
  816. * @}
  817. */
  818. /**
  819. * @}
  820. */
  821. #endif /* STM32F410xx || STM32F413_423xx */
  822. /**
  823. * @}
  824. */
  825. /**
  826. * @}
  827. */