You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1706 lines
50 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f7xx_hal_lptim.c
  4. * @author MCD Application Team
  5. * @version V1.2.2
  6. * @date 14-April-2017
  7. * @brief LPTIM HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the Low Power Timer (LPTIM) peripheral:
  10. * + Initialization and de-initialization functions.
  11. * + Start/Stop operation functions in polling mode.
  12. * + Start/Stop operation functions in interrupt mode.
  13. * + Reading operation functions.
  14. * + Peripheral State functions.
  15. *
  16. @verbatim
  17. ==============================================================================
  18. ##### How to use this driver #####
  19. ==============================================================================
  20. [..]
  21. The LPTIM HAL driver can be used as follows:
  22. (#)Initialize the LPTIM low level resources by implementing the
  23. HAL_LPTIM_MspInit():
  24. (##) Enable the LPTIM interface clock using __LPTIMx_CLK_ENABLE().
  25. (##) In case of using interrupts (e.g. HAL_LPTIM_PWM_Start_IT()):
  26. (+++) Configure the LPTIM interrupt priority using HAL_NVIC_SetPriority().
  27. (+++) Enable the LPTIM IRQ handler using HAL_NVIC_EnableIRQ().
  28. (+++) In LPTIM IRQ handler, call HAL_LPTIM_IRQHandler().
  29. (#)Initialize the LPTIM HAL using HAL_LPTIM_Init(). This function
  30. configures mainly:
  31. (##) The instance: LPTIM1.
  32. (##) Clock: the counter clock.
  33. (+++) Source: it can be either the ULPTIM input (IN1) or one of
  34. the internal clock; (APB, LSE, LSI or MSI).
  35. (+++) Prescaler: select the clock divider.
  36. (##) UltraLowPowerClock : To be used only if the ULPTIM is selected
  37. as counter clock source.
  38. (+++) Polarity: polarity of the active edge for the counter unit
  39. if the ULPTIM input is selected.
  40. (+++) SampleTime: clock sampling time to configure the clock glitch
  41. filter.
  42. (##) Trigger: How the counter start.
  43. (+++) Source: trigger can be software or one of the hardware triggers.
  44. (+++) ActiveEdge: only for hardware trigger.
  45. (+++) SampleTime: trigger sampling time to configure the trigger
  46. glitch filter.
  47. (##) OutputPolarity: 2 opposite polarities are possibles.
  48. (##) UpdateMode: specifies whether the update of the autoreload and
  49. the compare values is done immediately or after the end of current
  50. period.
  51. (#)Six modes are available:
  52. (##) PWM Mode: To generate a PWM signal with specified period and pulse,
  53. call HAL_LPTIM_PWM_Start() or HAL_LPTIM_PWM_Start_IT() for interruption
  54. mode.
  55. (##) One Pulse Mode: To generate pulse with specified width in response
  56. to a stimulus, call HAL_LPTIM_OnePulse_Start() or
  57. HAL_LPTIM_OnePulse_Start_IT() for interruption mode.
  58. (##) Set once Mode: In this mode, the output changes the level (from
  59. low level to high level if the output polarity is configured high, else
  60. the opposite) when a compare match occurs. To start this mode, call
  61. HAL_LPTIM_SetOnce_Start() or HAL_LPTIM_SetOnce_Start_IT() for
  62. interruption mode.
  63. (##) Encoder Mode: To use the encoder interface call
  64. HAL_LPTIM_Encoder_Start() or HAL_LPTIM_Encoder_Start_IT() for
  65. interruption mode.
  66. (##) Time out Mode: an active edge on one selected trigger input rests
  67. the counter. The first trigger event will start the timer, any
  68. successive trigger event will reset the counter and the timer will
  69. restart. To start this mode call HAL_LPTIM_TimeOut_Start_IT() or
  70. HAL_LPTIM_TimeOut_Start_IT() for interruption mode.
  71. (##) Counter Mode: counter can be used to count external events on
  72. the LPTIM Input1 or it can be used to count internal clock cycles.
  73. To start this mode, call HAL_LPTIM_Counter_Start() or
  74. HAL_LPTIM_Counter_Start_IT() for interruption mode.
  75. (#) User can stop any process by calling the corresponding API:
  76. HAL_LPTIM_Xxx_Stop() or HAL_LPTIM_Xxx_Stop_IT() if the process is
  77. already started in interruption mode.
  78. (#) Call HAL_LPTIM_DeInit() to deinitialize the LPTIM peripheral.
  79. @endverbatim
  80. ******************************************************************************
  81. * @attention
  82. *
  83. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  84. *
  85. * Redistribution and use in source and binary forms, with or without modification,
  86. * are permitted provided that the following conditions are met:
  87. * 1. Redistributions of source code must retain the above copyright notice,
  88. * this list of conditions and the following disclaimer.
  89. * 2. Redistributions in binary form must reproduce the above copyright notice,
  90. * this list of conditions and the following disclaimer in the documentation
  91. * and/or other materials provided with the distribution.
  92. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  93. * may be used to endorse or promote products derived from this software
  94. * without specific prior written permission.
  95. *
  96. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  97. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  98. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  99. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  100. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  101. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  102. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  103. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  104. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  105. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  106. *
  107. ******************************************************************************
  108. */
  109. /* Includes ------------------------------------------------------------------*/
  110. #include "stm32f7xx_hal.h"
  111. /** @addtogroup STM32F7xx_HAL_Driver
  112. * @{
  113. */
  114. /** @defgroup LPTIM LPTIM
  115. * @brief LPTIM HAL module driver.
  116. * @{
  117. */
  118. #ifdef HAL_LPTIM_MODULE_ENABLED
  119. /* Private types -------------------------------------------------------------*/
  120. /** @defgroup LPTIM_Private_Types LPTIM Private Types
  121. * @{
  122. */
  123. /**
  124. * @}
  125. */
  126. /* Private defines -----------------------------------------------------------*/
  127. /** @defgroup LPTIM_Private_Defines LPTIM Private Defines
  128. * @{
  129. */
  130. /**
  131. * @}
  132. */
  133. /* Private variables ---------------------------------------------------------*/
  134. /** @addtogroup LPTIM_Private_Variables LPTIM Private Variables
  135. * @{
  136. */
  137. /**
  138. * @}
  139. */
  140. /* Private constants ---------------------------------------------------------*/
  141. /** @addtogroup LPTIM_Private_Constants LPTIM Private Constants
  142. * @{
  143. */
  144. /**
  145. * @}
  146. */
  147. /* Private macros ------------------------------------------------------------*/
  148. /** @addtogroup LPTIM_Private_Macros LPTIM Private Macros
  149. * @{
  150. */
  151. /**
  152. * @}
  153. */
  154. /* Private function prototypes -----------------------------------------------*/
  155. /** @addtogroup LPTIM_Private_Functions_Prototypes LPTIM Private Functions Prototypes
  156. * @{
  157. */
  158. /**
  159. * @}
  160. */
  161. /* Private functions ---------------------------------------------------------*/
  162. /** @addtogroup LPTIM_Private_Functions LPTIM Private Functions
  163. * @{
  164. */
  165. /**
  166. * @}
  167. */
  168. /* Exported functions ---------------------------------------------------------*/
  169. /** @defgroup LPTIM_Exported_Functions LPTIM Exported Functions
  170. * @{
  171. */
  172. /** @defgroup LPTIM_Group1 Initialization/de-initialization functions
  173. * @brief Initialization and Configuration functions.
  174. *
  175. @verbatim
  176. ==============================================================================
  177. ##### Initialization and de-initialization functions #####
  178. ==============================================================================
  179. [..] This section provides functions allowing to:
  180. (+) Initialize the LPTIM according to the specified parameters in the
  181. LPTIM_InitTypeDef and creates the associated handle.
  182. (+) DeInitialize the LPTIM peripheral.
  183. (+) Initialize the LPTIM MSP.
  184. (+) DeInitialize LPTIM MSP.
  185. @endverbatim
  186. * @{
  187. */
  188. /**
  189. * @brief Initializes the LPTIM according to the specified parameters in the
  190. * LPTIM_InitTypeDef and creates the associated handle.
  191. * @param hlptim: LPTIM handle
  192. * @retval HAL status
  193. */
  194. HAL_StatusTypeDef HAL_LPTIM_Init(LPTIM_HandleTypeDef *hlptim)
  195. {
  196. uint32_t tmpcfgr = 0;
  197. /* Check the LPTIM handle allocation */
  198. if(hlptim == NULL)
  199. {
  200. return HAL_ERROR;
  201. }
  202. /* Check the parameters */
  203. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  204. assert_param(IS_LPTIM_CLOCK_SOURCE(hlptim->Init.Clock.Source));
  205. assert_param(IS_LPTIM_CLOCK_PRESCALER(hlptim->Init.Clock.Prescaler));
  206. if ((hlptim->Init.Clock.Source) == LPTIM_CLOCKSOURCE_ULPTIM)
  207. {
  208. assert_param(IS_LPTIM_CLOCK_POLARITY(hlptim->Init.UltraLowPowerClock.Polarity));
  209. assert_param(IS_LPTIM_CLOCK_SAMPLE_TIME(hlptim->Init.UltraLowPowerClock.SampleTime));
  210. }
  211. assert_param(IS_LPTIM_TRG_SOURCE(hlptim->Init.Trigger.Source));
  212. if ((hlptim->Init.Trigger.Source) != LPTIM_TRIGSOURCE_SOFTWARE)
  213. {
  214. assert_param(IS_LPTIM_TRIG_SAMPLE_TIME(hlptim->Init.Trigger.SampleTime));
  215. assert_param(IS_LPTIM_EXT_TRG_POLARITY(hlptim->Init.Trigger.ActiveEdge));
  216. }
  217. assert_param(IS_LPTIM_OUTPUT_POLARITY(hlptim->Init.OutputPolarity));
  218. assert_param(IS_LPTIM_UPDATE_MODE(hlptim->Init.UpdateMode));
  219. assert_param(IS_LPTIM_COUNTER_SOURCE(hlptim->Init.CounterSource));
  220. if(hlptim->State == HAL_LPTIM_STATE_RESET)
  221. {
  222. /* Allocate lock resource and initialize it */
  223. hlptim->Lock = HAL_UNLOCKED;
  224. /* Init the low level hardware */
  225. HAL_LPTIM_MspInit(hlptim);
  226. }
  227. /* Change the LPTIM state */
  228. hlptim->State = HAL_LPTIM_STATE_BUSY;
  229. /* Get the LPTIMx CFGR value */
  230. tmpcfgr = hlptim->Instance->CFGR;
  231. if ((hlptim->Init.Clock.Source) == LPTIM_CLOCKSOURCE_ULPTIM)
  232. {
  233. tmpcfgr &= (uint32_t)(~(LPTIM_CFGR_CKPOL | LPTIM_CFGR_CKFLT));
  234. }
  235. if ((hlptim->Init.Trigger.Source) != LPTIM_TRIGSOURCE_SOFTWARE)
  236. {
  237. tmpcfgr &= (uint32_t)(~ (LPTIM_CFGR_TRGFLT | LPTIM_CFGR_TRIGSEL));
  238. }
  239. /* Clear CKSEL, PRESC, TRIGEN, TRGFLT, WAVPOL, PRELOAD & COUNTMODE bits */
  240. tmpcfgr &= (uint32_t)(~(LPTIM_CFGR_CKSEL | LPTIM_CFGR_TRIGEN | LPTIM_CFGR_PRELOAD |
  241. LPTIM_CFGR_WAVPOL | LPTIM_CFGR_PRESC | LPTIM_CFGR_COUNTMODE ));
  242. /* Set initialization parameters */
  243. tmpcfgr |= (hlptim->Init.Clock.Source |
  244. hlptim->Init.Clock.Prescaler |
  245. hlptim->Init.OutputPolarity |
  246. hlptim->Init.UpdateMode |
  247. hlptim->Init.CounterSource);
  248. if ((hlptim->Init.Clock.Source) == LPTIM_CLOCKSOURCE_ULPTIM)
  249. {
  250. tmpcfgr |= (hlptim->Init.UltraLowPowerClock.Polarity |
  251. hlptim->Init.UltraLowPowerClock.SampleTime);
  252. }
  253. if ((hlptim->Init.Trigger.Source) != LPTIM_TRIGSOURCE_SOFTWARE)
  254. {
  255. /* Enable External trigger and set the trigger source */
  256. tmpcfgr |= (hlptim->Init.Trigger.Source |
  257. hlptim->Init.Trigger.ActiveEdge |
  258. hlptim->Init.Trigger.SampleTime);
  259. }
  260. /* Write to LPTIMx CFGR */
  261. hlptim->Instance->CFGR = tmpcfgr;
  262. /* Change the LPTIM state */
  263. hlptim->State = HAL_LPTIM_STATE_READY;
  264. /* Return function status */
  265. return HAL_OK;
  266. }
  267. /**
  268. * @brief DeInitializes the LPTIM peripheral.
  269. * @param hlptim: LPTIM handle
  270. * @retval HAL status
  271. */
  272. HAL_StatusTypeDef HAL_LPTIM_DeInit(LPTIM_HandleTypeDef *hlptim)
  273. {
  274. /* Check the LPTIM handle allocation */
  275. if(hlptim == NULL)
  276. {
  277. return HAL_ERROR;
  278. }
  279. /* Change the LPTIM state */
  280. hlptim->State = HAL_LPTIM_STATE_BUSY;
  281. /* Disable the LPTIM Peripheral Clock */
  282. __HAL_LPTIM_DISABLE(hlptim);
  283. /* DeInit the low level hardware: CLOCK, NVIC.*/
  284. HAL_LPTIM_MspDeInit(hlptim);
  285. /* Change the LPTIM state */
  286. hlptim->State = HAL_LPTIM_STATE_RESET;
  287. /* Release Lock */
  288. __HAL_UNLOCK(hlptim);
  289. /* Return function status */
  290. return HAL_OK;
  291. }
  292. /**
  293. * @brief Initializes the LPTIM MSP.
  294. * @param hlptim: LPTIM handle
  295. * @retval None
  296. */
  297. __weak void HAL_LPTIM_MspInit(LPTIM_HandleTypeDef *hlptim)
  298. {
  299. /* Prevent unused argument(s) compilation warning */
  300. UNUSED(hlptim);
  301. /* NOTE : This function Should not be modified, when the callback is needed,
  302. the HAL_LPTIM_MspInit could be implemented in the user file
  303. */
  304. }
  305. /**
  306. * @brief DeInitializes LPTIM MSP.
  307. * @param hlptim: LPTIM handle
  308. * @retval None
  309. */
  310. __weak void HAL_LPTIM_MspDeInit(LPTIM_HandleTypeDef *hlptim)
  311. {
  312. /* Prevent unused argument(s) compilation warning */
  313. UNUSED(hlptim);
  314. /* NOTE : This function Should not be modified, when the callback is needed,
  315. the HAL_LPTIM_MspDeInit could be implemented in the user file
  316. */
  317. }
  318. /**
  319. * @}
  320. */
  321. /** @defgroup LPTIM_Group2 LPTIM Start-Stop operation functions
  322. * @brief Start-Stop operation functions.
  323. *
  324. @verbatim
  325. ==============================================================================
  326. ##### LPTIM Start Stop operation functions #####
  327. ==============================================================================
  328. [..] This section provides functions allowing to:
  329. (+) Start the PWM mode.
  330. (+) Stop the PWM mode.
  331. (+) Start the One pulse mode.
  332. (+) Stop the One pulse mode.
  333. (+) Start the Set once mode.
  334. (+) Stop the Set once mode.
  335. (+) Start the Encoder mode.
  336. (+) Stop the Encoder mode.
  337. (+) Start the Timeout mode.
  338. (+) Stop the Timeout mode.
  339. (+) Start the Counter mode.
  340. (+) Stop the Counter mode.
  341. @endverbatim
  342. * @{
  343. */
  344. /**
  345. * @brief Starts the LPTIM PWM generation.
  346. * @param hlptim : LPTIM handle
  347. * @param Period : Specifies the Autoreload value.
  348. * This parameter must be a value between 0x0000 and 0xFFFF.
  349. * @param Pulse : Specifies the compare value.
  350. * This parameter must be a value between 0x0000 and 0xFFFF.
  351. * @retval HAL status
  352. */
  353. HAL_StatusTypeDef HAL_LPTIM_PWM_Start(LPTIM_HandleTypeDef *hlptim, uint32_t Period, uint32_t Pulse)
  354. {
  355. /* Check the parameters */
  356. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  357. assert_param(IS_LPTIM_PERIOD(Period));
  358. assert_param(IS_LPTIM_PULSE(Pulse));
  359. /* Set the LPTIM state */
  360. hlptim->State= HAL_LPTIM_STATE_BUSY;
  361. /* Reset WAVE bit to set PWM mode */
  362. hlptim->Instance->CFGR &= ~LPTIM_CFGR_WAVE;
  363. /* Enable the Peripheral */
  364. __HAL_LPTIM_ENABLE(hlptim);
  365. /* Load the period value in the autoreload register */
  366. __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
  367. /* Load the pulse value in the compare register */
  368. __HAL_LPTIM_COMPARE_SET(hlptim, Pulse);
  369. /* Start timer in continuous mode */
  370. __HAL_LPTIM_START_CONTINUOUS(hlptim);
  371. /* Change the TIM state*/
  372. hlptim->State= HAL_LPTIM_STATE_READY;
  373. /* Return function status */
  374. return HAL_OK;
  375. }
  376. /**
  377. * @brief Stops the LPTIM PWM generation.
  378. * @param hlptim : LPTIM handle
  379. * @retval HAL status
  380. */
  381. HAL_StatusTypeDef HAL_LPTIM_PWM_Stop(LPTIM_HandleTypeDef *hlptim)
  382. {
  383. /* Check the parameters */
  384. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  385. /* Set the LPTIM state */
  386. hlptim->State= HAL_LPTIM_STATE_BUSY;
  387. /* Disable the Peripheral */
  388. __HAL_LPTIM_DISABLE(hlptim);
  389. /* Change the TIM state*/
  390. hlptim->State= HAL_LPTIM_STATE_READY;
  391. /* Return function status */
  392. return HAL_OK;
  393. }
  394. /**
  395. * @brief Starts the LPTIM PWM generation in interrupt mode.
  396. * @param hlptim : LPTIM handle
  397. * @param Period : Specifies the Autoreload value.
  398. * This parameter must be a value between 0x0000 and 0xFFFF
  399. * @param Pulse : Specifies the compare value.
  400. * This parameter must be a value between 0x0000 and 0xFFFF
  401. * @retval HAL status
  402. */
  403. HAL_StatusTypeDef HAL_LPTIM_PWM_Start_IT(LPTIM_HandleTypeDef *hlptim, uint32_t Period, uint32_t Pulse)
  404. {
  405. /* Check the parameters */
  406. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  407. assert_param(IS_LPTIM_PERIOD(Period));
  408. assert_param(IS_LPTIM_PULSE(Pulse));
  409. /* Set the LPTIM state */
  410. hlptim->State= HAL_LPTIM_STATE_BUSY;
  411. /* Reset WAVE bit to set PWM mode */
  412. hlptim->Instance->CFGR &= ~LPTIM_CFGR_WAVE;
  413. /* Enable Autoreload write complete interrupt */
  414. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARROK);
  415. /* Enable Compare write complete interrupt */
  416. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_CMPOK);
  417. /* Enable Autoreload match interrupt */
  418. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARRM);
  419. /* Enable Compare match interrupt */
  420. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_CMPM);
  421. /* If external trigger source is used, then enable external trigger interrupt */
  422. if ((hlptim->Init.Trigger.Source) != LPTIM_TRIGSOURCE_SOFTWARE)
  423. {
  424. /* Enable external trigger interrupt */
  425. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_EXTTRIG);
  426. }
  427. /* Enable the Peripheral */
  428. __HAL_LPTIM_ENABLE(hlptim);
  429. /* Load the period value in the autoreload register */
  430. __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
  431. /* Load the pulse value in the compare register */
  432. __HAL_LPTIM_COMPARE_SET(hlptim, Pulse);
  433. /* Start timer in continuous mode */
  434. __HAL_LPTIM_START_CONTINUOUS(hlptim);
  435. /* Change the TIM state*/
  436. hlptim->State= HAL_LPTIM_STATE_READY;
  437. /* Return function status */
  438. return HAL_OK;
  439. }
  440. /**
  441. * @brief Stops the LPTIM PWM generation in interrupt mode.
  442. * @param hlptim : LPTIM handle
  443. * @retval HAL status
  444. */
  445. HAL_StatusTypeDef HAL_LPTIM_PWM_Stop_IT(LPTIM_HandleTypeDef *hlptim)
  446. {
  447. /* Check the parameters */
  448. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  449. /* Set the LPTIM state */
  450. hlptim->State= HAL_LPTIM_STATE_BUSY;
  451. /* Disable the Peripheral */
  452. __HAL_LPTIM_DISABLE(hlptim);
  453. /* Disable Autoreload write complete interrupt */
  454. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_ARROK);
  455. /* Disable Compare write complete interrupt */
  456. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_CMPOK);
  457. /* Disable Autoreload match interrupt */
  458. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_ARRM);
  459. /* Disable Compare match interrupt */
  460. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_CMPM);
  461. /* If external trigger source is used, then disable external trigger interrupt */
  462. if ((hlptim->Init.Trigger.Source) != LPTIM_TRIGSOURCE_SOFTWARE)
  463. {
  464. /* Disable external trigger interrupt */
  465. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_EXTTRIG);
  466. }
  467. /* Change the TIM state*/
  468. hlptim->State= HAL_LPTIM_STATE_READY;
  469. /* Return function status */
  470. return HAL_OK;
  471. }
  472. /**
  473. * @brief Starts the LPTIM One pulse generation.
  474. * @param hlptim : LPTIM handle
  475. * @param Period : Specifies the Autoreload value.
  476. * This parameter must be a value between 0x0000 and 0xFFFF.
  477. * @param Pulse : Specifies the compare value.
  478. * This parameter must be a value between 0x0000 and 0xFFFF.
  479. * @retval HAL status
  480. */
  481. HAL_StatusTypeDef HAL_LPTIM_OnePulse_Start(LPTIM_HandleTypeDef *hlptim, uint32_t Period, uint32_t Pulse)
  482. {
  483. /* Check the parameters */
  484. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  485. assert_param(IS_LPTIM_PERIOD(Period));
  486. assert_param(IS_LPTIM_PULSE(Pulse));
  487. /* Set the LPTIM state */
  488. hlptim->State= HAL_LPTIM_STATE_BUSY;
  489. /* Reset WAVE bit to set one pulse mode */
  490. hlptim->Instance->CFGR &= ~LPTIM_CFGR_WAVE;
  491. /* Enable the Peripheral */
  492. __HAL_LPTIM_ENABLE(hlptim);
  493. /* Load the period value in the autoreload register */
  494. __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
  495. /* Load the pulse value in the compare register */
  496. __HAL_LPTIM_COMPARE_SET(hlptim, Pulse);
  497. /* Start timer in continuous mode */
  498. __HAL_LPTIM_START_SINGLE(hlptim);
  499. /* Change the TIM state*/
  500. hlptim->State= HAL_LPTIM_STATE_READY;
  501. /* Return function status */
  502. return HAL_OK;
  503. }
  504. /**
  505. * @brief Stops the LPTIM One pulse generation.
  506. * @param hlptim : LPTIM handle
  507. * @retval HAL status
  508. */
  509. HAL_StatusTypeDef HAL_LPTIM_OnePulse_Stop(LPTIM_HandleTypeDef *hlptim)
  510. {
  511. /* Check the parameters */
  512. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  513. /* Set the LPTIM state */
  514. hlptim->State= HAL_LPTIM_STATE_BUSY;
  515. /* Disable the Peripheral */
  516. __HAL_LPTIM_DISABLE(hlptim);
  517. /* Change the TIM state*/
  518. hlptim->State= HAL_LPTIM_STATE_READY;
  519. /* Return function status */
  520. return HAL_OK;
  521. }
  522. /**
  523. * @brief Starts the LPTIM One pulse generation in interrupt mode.
  524. * @param hlptim : LPTIM handle
  525. * @param Period : Specifies the Autoreload value.
  526. * This parameter must be a value between 0x0000 and 0xFFFF.
  527. * @param Pulse : Specifies the compare value.
  528. * This parameter must be a value between 0x0000 and 0xFFFF.
  529. * @retval HAL status
  530. */
  531. HAL_StatusTypeDef HAL_LPTIM_OnePulse_Start_IT(LPTIM_HandleTypeDef *hlptim, uint32_t Period, uint32_t Pulse)
  532. {
  533. /* Check the parameters */
  534. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  535. assert_param(IS_LPTIM_PERIOD(Period));
  536. assert_param(IS_LPTIM_PULSE(Pulse));
  537. /* Set the LPTIM state */
  538. hlptim->State= HAL_LPTIM_STATE_BUSY;
  539. /* Reset WAVE bit to set one pulse mode */
  540. hlptim->Instance->CFGR &= ~LPTIM_CFGR_WAVE;
  541. /* Enable Autoreload write complete interrupt */
  542. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARROK);
  543. /* Enable Compare write complete interrupt */
  544. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_CMPOK);
  545. /* Enable Autoreload match interrupt */
  546. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARRM);
  547. /* Enable Compare match interrupt */
  548. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_CMPM);
  549. /* If external trigger source is used, then enable external trigger interrupt */
  550. if ((hlptim->Init.Trigger.Source) != LPTIM_TRIGSOURCE_SOFTWARE)
  551. {
  552. /* Enable external trigger interrupt */
  553. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_EXTTRIG);
  554. }
  555. /* Enable the Peripheral */
  556. __HAL_LPTIM_ENABLE(hlptim);
  557. /* Load the period value in the autoreload register */
  558. __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
  559. /* Load the pulse value in the compare register */
  560. __HAL_LPTIM_COMPARE_SET(hlptim, Pulse);
  561. /* Start timer in continuous mode */
  562. __HAL_LPTIM_START_SINGLE(hlptim);
  563. /* Change the TIM state*/
  564. hlptim->State= HAL_LPTIM_STATE_READY;
  565. /* Return function status */
  566. return HAL_OK;
  567. }
  568. /**
  569. * @brief Stops the LPTIM One pulse generation in interrupt mode.
  570. * @param hlptim : LPTIM handle
  571. * @retval HAL status
  572. */
  573. HAL_StatusTypeDef HAL_LPTIM_OnePulse_Stop_IT(LPTIM_HandleTypeDef *hlptim)
  574. {
  575. /* Check the parameters */
  576. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  577. /* Set the LPTIM state */
  578. hlptim->State= HAL_LPTIM_STATE_BUSY;
  579. /* Disable the Peripheral */
  580. __HAL_LPTIM_DISABLE(hlptim);
  581. /* Disable Autoreload write complete interrupt */
  582. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_ARROK);
  583. /* Disable Compare write complete interrupt */
  584. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_CMPOK);
  585. /* Disable Autoreload match interrupt */
  586. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_ARRM);
  587. /* Disable Compare match interrupt */
  588. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_CMPM);
  589. /* If external trigger source is used, then disable external trigger interrupt */
  590. if ((hlptim->Init.Trigger.Source) != LPTIM_TRIGSOURCE_SOFTWARE)
  591. {
  592. /* Disable external trigger interrupt */
  593. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_EXTTRIG);
  594. }
  595. /* Change the TIM state*/
  596. hlptim->State= HAL_LPTIM_STATE_READY;
  597. /* Return function status */
  598. return HAL_OK;
  599. }
  600. /**
  601. * @brief Starts the LPTIM in Set once mode.
  602. * @param hlptim : LPTIM handle
  603. * @param Period : Specifies the Autoreload value.
  604. * This parameter must be a value between 0x0000 and 0xFFFF.
  605. * @param Pulse : Specifies the compare value.
  606. * This parameter must be a value between 0x0000 and 0xFFFF.
  607. * @retval HAL status
  608. */
  609. HAL_StatusTypeDef HAL_LPTIM_SetOnce_Start(LPTIM_HandleTypeDef *hlptim, uint32_t Period, uint32_t Pulse)
  610. {
  611. /* Check the parameters */
  612. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  613. assert_param(IS_LPTIM_PERIOD(Period));
  614. assert_param(IS_LPTIM_PULSE(Pulse));
  615. /* Set the LPTIM state */
  616. hlptim->State= HAL_LPTIM_STATE_BUSY;
  617. /* Set WAVE bit to enable the set once mode */
  618. hlptim->Instance->CFGR |= LPTIM_CFGR_WAVE;
  619. /* Enable the Peripheral */
  620. __HAL_LPTIM_ENABLE(hlptim);
  621. /* Load the period value in the autoreload register */
  622. __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
  623. /* Load the pulse value in the compare register */
  624. __HAL_LPTIM_COMPARE_SET(hlptim, Pulse);
  625. /* Start timer in continuous mode */
  626. __HAL_LPTIM_START_SINGLE(hlptim);
  627. /* Change the TIM state*/
  628. hlptim->State= HAL_LPTIM_STATE_READY;
  629. /* Return function status */
  630. return HAL_OK;
  631. }
  632. /**
  633. * @brief Stops the LPTIM Set once mode.
  634. * @param hlptim : LPTIM handle
  635. * @retval HAL status
  636. */
  637. HAL_StatusTypeDef HAL_LPTIM_SetOnce_Stop(LPTIM_HandleTypeDef *hlptim)
  638. {
  639. /* Check the parameters */
  640. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  641. /* Set the LPTIM state */
  642. hlptim->State= HAL_LPTIM_STATE_BUSY;
  643. /* Disable the Peripheral */
  644. __HAL_LPTIM_DISABLE(hlptim);
  645. /* Change the TIM state*/
  646. hlptim->State= HAL_LPTIM_STATE_READY;
  647. /* Return function status */
  648. return HAL_OK;
  649. }
  650. /**
  651. * @brief Starts the LPTIM Set once mode in interrupt mode.
  652. * @param hlptim : LPTIM handle
  653. * @param Period : Specifies the Autoreload value.
  654. * This parameter must be a value between 0x0000 and 0xFFFF.
  655. * @param Pulse : Specifies the compare value.
  656. * This parameter must be a value between 0x0000 and 0xFFFF.
  657. * @retval HAL status
  658. */
  659. HAL_StatusTypeDef HAL_LPTIM_SetOnce_Start_IT(LPTIM_HandleTypeDef *hlptim, uint32_t Period, uint32_t Pulse)
  660. {
  661. /* Check the parameters */
  662. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  663. assert_param(IS_LPTIM_PERIOD(Period));
  664. assert_param(IS_LPTIM_PULSE(Pulse));
  665. /* Set the LPTIM state */
  666. hlptim->State= HAL_LPTIM_STATE_BUSY;
  667. /* Set WAVE bit to enable the set once mode */
  668. hlptim->Instance->CFGR |= LPTIM_CFGR_WAVE;
  669. /* Enable Autoreload write complete interrupt */
  670. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARROK);
  671. /* Enable Compare write complete interrupt */
  672. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_CMPOK);
  673. /* Enable Autoreload match interrupt */
  674. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARRM);
  675. /* Enable Compare match interrupt */
  676. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_CMPM);
  677. /* If external trigger source is used, then enable external trigger interrupt */
  678. if ((hlptim->Init.Trigger.Source) != LPTIM_TRIGSOURCE_SOFTWARE)
  679. {
  680. /* Enable external trigger interrupt */
  681. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_EXTTRIG);
  682. }
  683. /* Enable the Peripheral */
  684. __HAL_LPTIM_ENABLE(hlptim);
  685. /* Load the period value in the autoreload register */
  686. __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
  687. /* Load the pulse value in the compare register */
  688. __HAL_LPTIM_COMPARE_SET(hlptim, Pulse);
  689. /* Start timer in continuous mode */
  690. __HAL_LPTIM_START_SINGLE(hlptim);
  691. /* Change the TIM state*/
  692. hlptim->State= HAL_LPTIM_STATE_READY;
  693. /* Return function status */
  694. return HAL_OK;
  695. }
  696. /**
  697. * @brief Stops the LPTIM Set once mode in interrupt mode.
  698. * @param hlptim : LPTIM handle
  699. * @retval HAL status
  700. */
  701. HAL_StatusTypeDef HAL_LPTIM_SetOnce_Stop_IT(LPTIM_HandleTypeDef *hlptim)
  702. {
  703. /* Check the parameters */
  704. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  705. /* Set the LPTIM state */
  706. hlptim->State= HAL_LPTIM_STATE_BUSY;
  707. /* Disable the Peripheral */
  708. __HAL_LPTIM_DISABLE(hlptim);
  709. /* Disable Autoreload write complete interrupt */
  710. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_ARROK);
  711. /* Disable Compare write complete interrupt */
  712. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_CMPOK);
  713. /* Disable Autoreload match interrupt */
  714. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_ARRM);
  715. /* Disable Compare match interrupt */
  716. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_CMPM);
  717. /* If external trigger source is used, then disable external trigger interrupt */
  718. if ((hlptim->Init.Trigger.Source) != LPTIM_TRIGSOURCE_SOFTWARE)
  719. {
  720. /* Disable external trigger interrupt */
  721. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_EXTTRIG);
  722. }
  723. /* Change the TIM state*/
  724. hlptim->State= HAL_LPTIM_STATE_READY;
  725. /* Return function status */
  726. return HAL_OK;
  727. }
  728. /**
  729. * @brief Starts the Encoder interface.
  730. * @param hlptim : LPTIM handle
  731. * @param Period : Specifies the Autoreload value.
  732. * This parameter must be a value between 0x0000 and 0xFFFF.
  733. * @retval HAL status
  734. */
  735. HAL_StatusTypeDef HAL_LPTIM_Encoder_Start(LPTIM_HandleTypeDef *hlptim, uint32_t Period)
  736. {
  737. uint32_t tmpcfgr = 0;
  738. /* Check the parameters */
  739. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  740. assert_param(IS_LPTIM_PERIOD(Period));
  741. assert_param(hlptim->Init.Clock.Source == LPTIM_CLOCKSOURCE_APBCLOCK_LPOSC);
  742. assert_param(hlptim->Init.Clock.Prescaler == LPTIM_PRESCALER_DIV1);
  743. assert_param(IS_LPTIM_CLOCK_POLARITY(hlptim->Init.UltraLowPowerClock.Polarity));
  744. /* Set the LPTIM state */
  745. hlptim->State= HAL_LPTIM_STATE_BUSY;
  746. /* Get the LPTIMx CFGR value */
  747. tmpcfgr = hlptim->Instance->CFGR;
  748. /* Clear CKPOL bits */
  749. tmpcfgr &= (uint32_t)(~LPTIM_CFGR_CKPOL);
  750. /* Set Input polarity */
  751. tmpcfgr |= hlptim->Init.UltraLowPowerClock.Polarity;
  752. /* Write to LPTIMx CFGR */
  753. hlptim->Instance->CFGR = tmpcfgr;
  754. /* Set ENC bit to enable the encoder interface */
  755. hlptim->Instance->CFGR |= LPTIM_CFGR_ENC;
  756. /* Enable the Peripheral */
  757. __HAL_LPTIM_ENABLE(hlptim);
  758. /* Load the period value in the autoreload register */
  759. __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
  760. /* Start timer in continuous mode */
  761. __HAL_LPTIM_START_CONTINUOUS(hlptim);
  762. /* Change the TIM state*/
  763. hlptim->State= HAL_LPTIM_STATE_READY;
  764. /* Return function status */
  765. return HAL_OK;
  766. }
  767. /**
  768. * @brief Stops the Encoder interface.
  769. * @param hlptim : LPTIM handle
  770. * @retval HAL status
  771. */
  772. HAL_StatusTypeDef HAL_LPTIM_Encoder_Stop(LPTIM_HandleTypeDef *hlptim)
  773. {
  774. /* Check the parameters */
  775. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  776. /* Set the LPTIM state */
  777. hlptim->State= HAL_LPTIM_STATE_BUSY;
  778. /* Disable the Peripheral */
  779. __HAL_LPTIM_DISABLE(hlptim);
  780. /* Reset ENC bit to disable the encoder interface */
  781. hlptim->Instance->CFGR &= ~LPTIM_CFGR_ENC;
  782. /* Change the TIM state*/
  783. hlptim->State= HAL_LPTIM_STATE_READY;
  784. /* Return function status */
  785. return HAL_OK;
  786. }
  787. /**
  788. * @brief Starts the Encoder interface in interrupt mode.
  789. * @param hlptim : LPTIM handle
  790. * @param Period : Specifies the Autoreload value.
  791. * This parameter must be a value between 0x0000 and 0xFFFF.
  792. * @retval HAL status
  793. */
  794. HAL_StatusTypeDef HAL_LPTIM_Encoder_Start_IT(LPTIM_HandleTypeDef *hlptim, uint32_t Period)
  795. {
  796. uint32_t tmpcfgr = 0;
  797. /* Check the parameters */
  798. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  799. assert_param(IS_LPTIM_PERIOD(Period));
  800. assert_param(hlptim->Init.Clock.Source == LPTIM_CLOCKSOURCE_APBCLOCK_LPOSC);
  801. assert_param(hlptim->Init.Clock.Prescaler == LPTIM_PRESCALER_DIV1);
  802. assert_param(IS_LPTIM_CLOCK_POLARITY(hlptim->Init.UltraLowPowerClock.Polarity));
  803. /* Set the LPTIM state */
  804. hlptim->State= HAL_LPTIM_STATE_BUSY;
  805. /* Configure edge sensitivity for encoder mode */
  806. /* Get the LPTIMx CFGR value */
  807. tmpcfgr = hlptim->Instance->CFGR;
  808. /* Clear CKPOL bits */
  809. tmpcfgr &= (uint32_t)(~LPTIM_CFGR_CKPOL);
  810. /* Set Input polarity */
  811. tmpcfgr |= hlptim->Init.UltraLowPowerClock.Polarity;
  812. /* Write to LPTIMx CFGR */
  813. hlptim->Instance->CFGR = tmpcfgr;
  814. /* Set ENC bit to enable the encoder interface */
  815. hlptim->Instance->CFGR |= LPTIM_CFGR_ENC;
  816. /* Enable "switch to down direction" interrupt */
  817. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_DOWN);
  818. /* Enable "switch to up direction" interrupt */
  819. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_UP);
  820. /* Enable the Peripheral */
  821. __HAL_LPTIM_ENABLE(hlptim);
  822. /* Load the period value in the autoreload register */
  823. __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
  824. /* Start timer in continuous mode */
  825. __HAL_LPTIM_START_CONTINUOUS(hlptim);
  826. /* Change the TIM state*/
  827. hlptim->State= HAL_LPTIM_STATE_READY;
  828. /* Return function status */
  829. return HAL_OK;
  830. }
  831. /**
  832. * @brief Stops the Encoder interface in interrupt mode.
  833. * @param hlptim : LPTIM handle
  834. * @retval HAL status
  835. */
  836. HAL_StatusTypeDef HAL_LPTIM_Encoder_Stop_IT(LPTIM_HandleTypeDef *hlptim)
  837. {
  838. /* Check the parameters */
  839. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  840. /* Set the LPTIM state */
  841. hlptim->State= HAL_LPTIM_STATE_BUSY;
  842. /* Disable the Peripheral */
  843. __HAL_LPTIM_DISABLE(hlptim);
  844. /* Reset ENC bit to disable the encoder interface */
  845. hlptim->Instance->CFGR &= ~LPTIM_CFGR_ENC;
  846. /* Disable "switch to down direction" interrupt */
  847. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_DOWN);
  848. /* Disable "switch to up direction" interrupt */
  849. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_UP);
  850. /* Change the TIM state*/
  851. hlptim->State= HAL_LPTIM_STATE_READY;
  852. /* Return function status */
  853. return HAL_OK;
  854. }
  855. /**
  856. * @brief Starts the Timeout function. The first trigger event will start the
  857. * timer, any successive trigger event will reset the counter and
  858. * the timer restarts.
  859. * @param hlptim : LPTIM handle
  860. * @param Period : Specifies the Autoreload value.
  861. * This parameter must be a value between 0x0000 and 0xFFFF.
  862. * @param Timeout : Specifies the TimeOut value to rest the counter.
  863. * This parameter must be a value between 0x0000 and 0xFFFF.
  864. * @retval HAL status
  865. */
  866. HAL_StatusTypeDef HAL_LPTIM_TimeOut_Start(LPTIM_HandleTypeDef *hlptim, uint32_t Period, uint32_t Timeout)
  867. {
  868. /* Check the parameters */
  869. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  870. assert_param(IS_LPTIM_PERIOD(Period));
  871. assert_param(IS_LPTIM_PULSE(Timeout));
  872. /* Set the LPTIM state */
  873. hlptim->State= HAL_LPTIM_STATE_BUSY;
  874. /* Set TIMOUT bit to enable the timeout function */
  875. hlptim->Instance->CFGR |= LPTIM_CFGR_TIMOUT;
  876. /* Enable the Peripheral */
  877. __HAL_LPTIM_ENABLE(hlptim);
  878. /* Load the period value in the autoreload register */
  879. __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
  880. /* Load the Timeout value in the compare register */
  881. __HAL_LPTIM_COMPARE_SET(hlptim, Timeout);
  882. /* Start timer in continuous mode */
  883. __HAL_LPTIM_START_CONTINUOUS(hlptim);
  884. /* Change the TIM state*/
  885. hlptim->State= HAL_LPTIM_STATE_READY;
  886. /* Return function status */
  887. return HAL_OK;
  888. }
  889. /**
  890. * @brief Stops the Timeout function.
  891. * @param hlptim : LPTIM handle
  892. * @retval HAL status
  893. */
  894. HAL_StatusTypeDef HAL_LPTIM_TimeOut_Stop(LPTIM_HandleTypeDef *hlptim)
  895. {
  896. /* Check the parameters */
  897. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  898. /* Set the LPTIM state */
  899. hlptim->State= HAL_LPTIM_STATE_BUSY;
  900. /* Disable the Peripheral */
  901. __HAL_LPTIM_DISABLE(hlptim);
  902. /* Reset TIMOUT bit to enable the timeout function */
  903. hlptim->Instance->CFGR &= ~LPTIM_CFGR_TIMOUT;
  904. /* Change the TIM state*/
  905. hlptim->State= HAL_LPTIM_STATE_READY;
  906. /* Return function status */
  907. return HAL_OK;
  908. }
  909. /**
  910. * @brief Starts the Timeout function in interrupt mode. The first trigger
  911. * event will start the timer, any successive trigger event will reset
  912. * the counter and the timer restarts.
  913. * @param hlptim : LPTIM handle
  914. * @param Period : Specifies the Autoreload value.
  915. * This parameter must be a value between 0x0000 and 0xFFFF.
  916. * @param Timeout : Specifies the TimeOut value to rest the counter.
  917. * This parameter must be a value between 0x0000 and 0xFFFF.
  918. * @retval HAL status
  919. */
  920. HAL_StatusTypeDef HAL_LPTIM_TimeOut_Start_IT(LPTIM_HandleTypeDef *hlptim, uint32_t Period, uint32_t Timeout)
  921. {
  922. /* Check the parameters */
  923. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  924. assert_param(IS_LPTIM_PERIOD(Period));
  925. assert_param(IS_LPTIM_PULSE(Timeout));
  926. /* Set the LPTIM state */
  927. hlptim->State= HAL_LPTIM_STATE_BUSY;
  928. /* Enable EXTI Line interrupt on the LPTIM Wake-up Timer */
  929. __HAL_LPTIM_WAKEUPTIMER_EXTI_ENABLE_IT();
  930. /* Enable rising edge trigger on the LPTIM Wake-up Timer Exti line */
  931. __HAL_LPTIM_WAKEUPTIMER_EXTI_ENABLE_RISING_EDGE();
  932. /* Set TIMOUT bit to enable the timeout function */
  933. hlptim->Instance->CFGR |= LPTIM_CFGR_TIMOUT;
  934. /* Enable Compare match interrupt */
  935. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_CMPM);
  936. /* Enable the Peripheral */
  937. __HAL_LPTIM_ENABLE(hlptim);
  938. /* Load the period value in the autoreload register */
  939. __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
  940. /* Load the Timeout value in the compare register */
  941. __HAL_LPTIM_COMPARE_SET(hlptim, Timeout);
  942. /* Start timer in continuous mode */
  943. __HAL_LPTIM_START_CONTINUOUS(hlptim);
  944. /* Change the TIM state*/
  945. hlptim->State= HAL_LPTIM_STATE_READY;
  946. /* Return function status */
  947. return HAL_OK;
  948. }
  949. /**
  950. * @brief Stops the Timeout function in interrupt mode.
  951. * @param hlptim : LPTIM handle
  952. * @retval HAL status
  953. */
  954. HAL_StatusTypeDef HAL_LPTIM_TimeOut_Stop_IT(LPTIM_HandleTypeDef *hlptim)
  955. {
  956. /* Check the parameters */
  957. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  958. /* Set the LPTIM state */
  959. hlptim->State= HAL_LPTIM_STATE_BUSY;
  960. /* Disable rising edge trigger on the LPTIM Wake-up Timer Exti line */
  961. __HAL_LPTIM_WAKEUPTIMER_EXTI_DISABLE_RISING_EDGE();
  962. /* Disable EXTI Line interrupt on the LPTIM Wake-up Timer */
  963. __HAL_LPTIM_WAKEUPTIMER_EXTI_DISABLE_IT();
  964. /* Disable the Peripheral */
  965. __HAL_LPTIM_DISABLE(hlptim);
  966. /* Reset TIMOUT bit to enable the timeout function */
  967. hlptim->Instance->CFGR &= ~LPTIM_CFGR_TIMOUT;
  968. /* Disable Compare match interrupt */
  969. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_CMPM);
  970. /* Change the TIM state*/
  971. hlptim->State= HAL_LPTIM_STATE_READY;
  972. /* Return function status */
  973. return HAL_OK;
  974. }
  975. /**
  976. * @brief Starts the Counter mode.
  977. * @param hlptim : LPTIM handle
  978. * @param Period : Specifies the Autoreload value.
  979. * This parameter must be a value between 0x0000 and 0xFFFF.
  980. * @retval HAL status
  981. */
  982. HAL_StatusTypeDef HAL_LPTIM_Counter_Start(LPTIM_HandleTypeDef *hlptim, uint32_t Period)
  983. {
  984. /* Check the parameters */
  985. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  986. assert_param(IS_LPTIM_PERIOD(Period));
  987. /* Set the LPTIM state */
  988. hlptim->State= HAL_LPTIM_STATE_BUSY;
  989. /* If clock source is not ULPTIM clock and counter source is external, then it must not be prescaled */
  990. if((hlptim->Init.Clock.Source != LPTIM_CLOCKSOURCE_ULPTIM) && (hlptim->Init.CounterSource == LPTIM_COUNTERSOURCE_EXTERNAL))
  991. {
  992. /* Check if clock is prescaled */
  993. assert_param(IS_LPTIM_CLOCK_PRESCALERDIV1(hlptim->Init.Clock.Prescaler));
  994. /* Set clock prescaler to 0 */
  995. hlptim->Instance->CFGR &= ~LPTIM_CFGR_PRESC;
  996. }
  997. /* Enable the Peripheral */
  998. __HAL_LPTIM_ENABLE(hlptim);
  999. /* Load the period value in the autoreload register */
  1000. __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
  1001. /* Start timer in continuous mode */
  1002. __HAL_LPTIM_START_CONTINUOUS(hlptim);
  1003. /* Change the TIM state*/
  1004. hlptim->State= HAL_LPTIM_STATE_READY;
  1005. /* Return function status */
  1006. return HAL_OK;
  1007. }
  1008. /**
  1009. * @brief Stops the Counter mode.
  1010. * @param hlptim : LPTIM handle
  1011. * @retval HAL status
  1012. */
  1013. HAL_StatusTypeDef HAL_LPTIM_Counter_Stop(LPTIM_HandleTypeDef *hlptim)
  1014. {
  1015. /* Check the parameters */
  1016. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  1017. /* Set the LPTIM state */
  1018. hlptim->State= HAL_LPTIM_STATE_BUSY;
  1019. /* Disable the Peripheral */
  1020. __HAL_LPTIM_DISABLE(hlptim);
  1021. /* Change the TIM state*/
  1022. hlptim->State= HAL_LPTIM_STATE_READY;
  1023. /* Return function status */
  1024. return HAL_OK;
  1025. }
  1026. /**
  1027. * @brief Starts the Counter mode in interrupt mode.
  1028. * @param hlptim : LPTIM handle
  1029. * @param Period : Specifies the Autoreload value.
  1030. * This parameter must be a value between 0x0000 and 0xFFFF.
  1031. * @retval HAL status
  1032. */
  1033. HAL_StatusTypeDef HAL_LPTIM_Counter_Start_IT(LPTIM_HandleTypeDef *hlptim, uint32_t Period)
  1034. {
  1035. /* Check the parameters */
  1036. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  1037. assert_param(IS_LPTIM_PERIOD(Period));
  1038. /* Set the LPTIM state */
  1039. hlptim->State= HAL_LPTIM_STATE_BUSY;
  1040. /* Enable EXTI Line interrupt on the LPTIM Wake-up Timer */
  1041. __HAL_LPTIM_WAKEUPTIMER_EXTI_ENABLE_IT();
  1042. /* Enable rising edge trigger on the LPTIM Wake-up Timer Exti line */
  1043. __HAL_LPTIM_WAKEUPTIMER_EXTI_ENABLE_RISING_EDGE();
  1044. /* If clock source is not ULPTIM clock and counter source is external, then it must not be prescaled */
  1045. if((hlptim->Init.Clock.Source != LPTIM_CLOCKSOURCE_ULPTIM) && (hlptim->Init.CounterSource == LPTIM_COUNTERSOURCE_EXTERNAL))
  1046. {
  1047. /* Check if clock is prescaled */
  1048. assert_param(IS_LPTIM_CLOCK_PRESCALERDIV1(hlptim->Init.Clock.Prescaler));
  1049. /* Set clock prescaler to 0 */
  1050. hlptim->Instance->CFGR &= ~LPTIM_CFGR_PRESC;
  1051. }
  1052. /* Enable Autoreload write complete interrupt */
  1053. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARROK);
  1054. /* Enable Autoreload match interrupt */
  1055. __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARRM);
  1056. /* Enable the Peripheral */
  1057. __HAL_LPTIM_ENABLE(hlptim);
  1058. /* Load the period value in the autoreload register */
  1059. __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
  1060. /* Start timer in continuous mode */
  1061. __HAL_LPTIM_START_CONTINUOUS(hlptim);
  1062. /* Change the TIM state*/
  1063. hlptim->State= HAL_LPTIM_STATE_READY;
  1064. /* Return function status */
  1065. return HAL_OK;
  1066. }
  1067. /**
  1068. * @brief Stops the Counter mode in interrupt mode.
  1069. * @param hlptim : LPTIM handle
  1070. * @retval HAL status
  1071. */
  1072. HAL_StatusTypeDef HAL_LPTIM_Counter_Stop_IT(LPTIM_HandleTypeDef *hlptim)
  1073. {
  1074. /* Check the parameters */
  1075. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  1076. /* Set the LPTIM state */
  1077. hlptim->State= HAL_LPTIM_STATE_BUSY;
  1078. /* Disable rising edge trigger on the LPTIM Wake-up Timer Exti line */
  1079. __HAL_LPTIM_WAKEUPTIMER_EXTI_DISABLE_RISING_EDGE();
  1080. /* Disable EXTI Line interrupt on the LPTIM Wake-up Timer */
  1081. __HAL_LPTIM_WAKEUPTIMER_EXTI_DISABLE_IT();
  1082. /* Disable the Peripheral */
  1083. __HAL_LPTIM_DISABLE(hlptim);
  1084. /* Disable Autoreload write complete interrupt */
  1085. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_ARROK);
  1086. /* Disable Autoreload match interrupt */
  1087. __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_ARRM);
  1088. /* Change the TIM state*/
  1089. hlptim->State= HAL_LPTIM_STATE_READY;
  1090. /* Return function status */
  1091. return HAL_OK;
  1092. }
  1093. /**
  1094. * @}
  1095. */
  1096. /** @defgroup LPTIM_Group3 LPTIM Read operation functions
  1097. * @brief Read operation functions.
  1098. *
  1099. @verbatim
  1100. ==============================================================================
  1101. ##### LPTIM Read operation functions #####
  1102. ==============================================================================
  1103. [..] This section provides LPTIM Reading functions.
  1104. (+) Read the counter value.
  1105. (+) Read the period (Auto-reload) value.
  1106. (+) Read the pulse (Compare)value.
  1107. @endverbatim
  1108. * @{
  1109. */
  1110. /**
  1111. * @brief This function returns the current counter value.
  1112. * @param hlptim: LPTIM handle
  1113. * @retval Counter value.
  1114. */
  1115. uint32_t HAL_LPTIM_ReadCounter(LPTIM_HandleTypeDef *hlptim)
  1116. {
  1117. /* Check the parameters */
  1118. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  1119. return (hlptim->Instance->CNT);
  1120. }
  1121. /**
  1122. * @brief This function return the current Autoreload (Period) value.
  1123. * @param hlptim: LPTIM handle
  1124. * @retval Autoreload value.
  1125. */
  1126. uint32_t HAL_LPTIM_ReadAutoReload(LPTIM_HandleTypeDef *hlptim)
  1127. {
  1128. /* Check the parameters */
  1129. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  1130. return (hlptim->Instance->ARR);
  1131. }
  1132. /**
  1133. * @brief This function return the current Compare (Pulse) value.
  1134. * @param hlptim: LPTIM handle
  1135. * @retval Compare value.
  1136. */
  1137. uint32_t HAL_LPTIM_ReadCompare(LPTIM_HandleTypeDef *hlptim)
  1138. {
  1139. /* Check the parameters */
  1140. assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
  1141. return (hlptim->Instance->CMP);
  1142. }
  1143. /**
  1144. * @}
  1145. */
  1146. /** @defgroup LPTIM_Group4 LPTIM IRQ handler
  1147. * @brief LPTIM IRQ handler.
  1148. *
  1149. @verbatim
  1150. ==============================================================================
  1151. ##### LPTIM IRQ handler #####
  1152. ==============================================================================
  1153. [..] This section provides LPTIM IRQ handler function.
  1154. @endverbatim
  1155. * @{
  1156. */
  1157. /**
  1158. * @brief This function handles LPTIM interrupt request.
  1159. * @param hlptim: LPTIM handle
  1160. * @retval None
  1161. */
  1162. void HAL_LPTIM_IRQHandler(LPTIM_HandleTypeDef *hlptim)
  1163. {
  1164. /* Compare match interrupt */
  1165. if(__HAL_LPTIM_GET_FLAG(hlptim, LPTIM_FLAG_CMPM) != RESET)
  1166. {
  1167. if(__HAL_LPTIM_GET_IT_SOURCE(hlptim, LPTIM_IT_CMPM) !=RESET)
  1168. {
  1169. /* Clear Compare match flag */
  1170. __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_CMPM);
  1171. /* Compare match Callback */
  1172. HAL_LPTIM_CompareMatchCallback(hlptim);
  1173. }
  1174. }
  1175. /* Autoreload match interrupt */
  1176. if(__HAL_LPTIM_GET_FLAG(hlptim, LPTIM_FLAG_ARRM) != RESET)
  1177. {
  1178. if(__HAL_LPTIM_GET_IT_SOURCE(hlptim, LPTIM_IT_ARRM) !=RESET)
  1179. {
  1180. /* Clear Autoreload match flag */
  1181. __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_ARRM);
  1182. /* Autoreload match Callback */
  1183. HAL_LPTIM_AutoReloadMatchCallback(hlptim);
  1184. }
  1185. }
  1186. /* Trigger detected interrupt */
  1187. if(__HAL_LPTIM_GET_FLAG(hlptim, LPTIM_FLAG_EXTTRIG) != RESET)
  1188. {
  1189. if(__HAL_LPTIM_GET_IT_SOURCE(hlptim, LPTIM_IT_EXTTRIG) !=RESET)
  1190. {
  1191. /* Clear Trigger detected flag */
  1192. __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_EXTTRIG);
  1193. /* Trigger detected callback */
  1194. HAL_LPTIM_TriggerCallback(hlptim);
  1195. }
  1196. }
  1197. /* Compare write interrupt */
  1198. if(__HAL_LPTIM_GET_FLAG(hlptim, LPTIM_FLAG_CMPOK) != RESET)
  1199. {
  1200. if(__HAL_LPTIM_GET_IT_SOURCE(hlptim, LPTIM_FLAG_CMPM) !=RESET)
  1201. {
  1202. /* Clear Compare write flag */
  1203. __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_CMPOK);
  1204. /* Compare write Callback */
  1205. HAL_LPTIM_CompareWriteCallback(hlptim);
  1206. }
  1207. }
  1208. /* Autoreload write interrupt */
  1209. if(__HAL_LPTIM_GET_FLAG(hlptim, LPTIM_FLAG_ARROK) != RESET)
  1210. {
  1211. if(__HAL_LPTIM_GET_IT_SOURCE(hlptim, LPTIM_IT_ARROK) !=RESET)
  1212. {
  1213. /* Clear Autoreload write flag */
  1214. __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_ARROK);
  1215. /* Autoreload write Callback */
  1216. HAL_LPTIM_AutoReloadWriteCallback(hlptim);
  1217. }
  1218. }
  1219. /* Direction counter changed from Down to Up interrupt */
  1220. if(__HAL_LPTIM_GET_FLAG(hlptim, LPTIM_FLAG_UP) != RESET)
  1221. {
  1222. if(__HAL_LPTIM_GET_IT_SOURCE(hlptim, LPTIM_IT_UP) !=RESET)
  1223. {
  1224. /* Clear Direction counter changed from Down to Up flag */
  1225. __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_UP);
  1226. /* Direction counter changed from Down to Up Callback */
  1227. HAL_LPTIM_DirectionUpCallback(hlptim);
  1228. }
  1229. }
  1230. /* Direction counter changed from Up to Down interrupt */
  1231. if(__HAL_LPTIM_GET_FLAG(hlptim, LPTIM_FLAG_DOWN) != RESET)
  1232. {
  1233. if(__HAL_LPTIM_GET_IT_SOURCE(hlptim, LPTIM_IT_DOWN) !=RESET)
  1234. {
  1235. /* Clear Direction counter changed from Up to Down flag */
  1236. __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_DOWN);
  1237. /* Direction counter changed from Up to Down Callback */
  1238. HAL_LPTIM_DirectionDownCallback(hlptim);
  1239. }
  1240. }
  1241. __HAL_LPTIM_WAKEUPTIMER_EXTI_CLEAR_FLAG();
  1242. }
  1243. /**
  1244. * @brief Compare match callback in non blocking mode
  1245. * @param hlptim : LPTIM handle
  1246. * @retval None
  1247. */
  1248. __weak void HAL_LPTIM_CompareMatchCallback(LPTIM_HandleTypeDef *hlptim)
  1249. {
  1250. /* Prevent unused argument(s) compilation warning */
  1251. UNUSED(hlptim);
  1252. /* NOTE : This function Should not be modified, when the callback is needed,
  1253. the HAL_LPTIM_CompareMatchCallback could be implemented in the user file
  1254. */
  1255. }
  1256. /**
  1257. * @brief Autoreload match callback in non blocking mode
  1258. * @param hlptim : LPTIM handle
  1259. * @retval None
  1260. */
  1261. __weak void HAL_LPTIM_AutoReloadMatchCallback(LPTIM_HandleTypeDef *hlptim)
  1262. {
  1263. /* Prevent unused argument(s) compilation warning */
  1264. UNUSED(hlptim);
  1265. /* NOTE : This function Should not be modified, when the callback is needed,
  1266. the HAL_LPTIM_AutoReloadMatchCallback could be implemented in the user file
  1267. */
  1268. }
  1269. /**
  1270. * @brief Trigger detected callback in non blocking mode
  1271. * @param hlptim : LPTIM handle
  1272. * @retval None
  1273. */
  1274. __weak void HAL_LPTIM_TriggerCallback(LPTIM_HandleTypeDef *hlptim)
  1275. {
  1276. /* Prevent unused argument(s) compilation warning */
  1277. UNUSED(hlptim);
  1278. /* NOTE : This function Should not be modified, when the callback is needed,
  1279. the HAL_LPTIM_TriggerCallback could be implemented in the user file
  1280. */
  1281. }
  1282. /**
  1283. * @brief Compare write callback in non blocking mode
  1284. * @param hlptim : LPTIM handle
  1285. * @retval None
  1286. */
  1287. __weak void HAL_LPTIM_CompareWriteCallback(LPTIM_HandleTypeDef *hlptim)
  1288. {
  1289. /* Prevent unused argument(s) compilation warning */
  1290. UNUSED(hlptim);
  1291. /* NOTE : This function Should not be modified, when the callback is needed,
  1292. the HAL_LPTIM_CompareWriteCallback could be implemented in the user file
  1293. */
  1294. }
  1295. /**
  1296. * @brief Autoreload write callback in non blocking mode
  1297. * @param hlptim : LPTIM handle
  1298. * @retval None
  1299. */
  1300. __weak void HAL_LPTIM_AutoReloadWriteCallback(LPTIM_HandleTypeDef *hlptim)
  1301. {
  1302. /* Prevent unused argument(s) compilation warning */
  1303. UNUSED(hlptim);
  1304. /* NOTE : This function Should not be modified, when the callback is needed,
  1305. the HAL_LPTIM_AutoReloadWriteCallback could be implemented in the user file
  1306. */
  1307. }
  1308. /**
  1309. * @brief Direction counter changed from Down to Up callback in non blocking mode
  1310. * @param hlptim : LPTIM handle
  1311. * @retval None
  1312. */
  1313. __weak void HAL_LPTIM_DirectionUpCallback(LPTIM_HandleTypeDef *hlptim)
  1314. {
  1315. /* Prevent unused argument(s) compilation warning */
  1316. UNUSED(hlptim);
  1317. /* NOTE : This function Should not be modified, when the callback is needed,
  1318. the HAL_LPTIM_DirectionUpCallback could be implemented in the user file
  1319. */
  1320. }
  1321. /**
  1322. * @brief Direction counter changed from Up to Down callback in non blocking mode
  1323. * @param hlptim : LPTIM handle
  1324. * @retval None
  1325. */
  1326. __weak void HAL_LPTIM_DirectionDownCallback(LPTIM_HandleTypeDef *hlptim)
  1327. {
  1328. /* Prevent unused argument(s) compilation warning */
  1329. UNUSED(hlptim);
  1330. /* NOTE : This function Should not be modified, when the callback is needed,
  1331. the HAL_LPTIM_DirectionDownCallback could be implemented in the user file
  1332. */
  1333. }
  1334. /**
  1335. * @}
  1336. */
  1337. /** @defgroup LPTIM_Group5 Peripheral State functions
  1338. * @brief Peripheral State functions.
  1339. *
  1340. @verbatim
  1341. ==============================================================================
  1342. ##### Peripheral State functions #####
  1343. ==============================================================================
  1344. [..]
  1345. This subsection permits to get in run-time the status of the peripheral.
  1346. @endverbatim
  1347. * @{
  1348. */
  1349. /**
  1350. * @brief Returns the LPTIM state.
  1351. * @param hlptim: LPTIM handle
  1352. * @retval HAL state
  1353. */
  1354. HAL_LPTIM_StateTypeDef HAL_LPTIM_GetState(LPTIM_HandleTypeDef *hlptim)
  1355. {
  1356. return hlptim->State;
  1357. }
  1358. /**
  1359. * @}
  1360. */
  1361. /**
  1362. * @}
  1363. */
  1364. #endif /* HAL_LPTIM_MODULE_ENABLED */
  1365. /**
  1366. * @}
  1367. */
  1368. /**
  1369. * @}
  1370. */
  1371. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/