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