f0d2a275c609719b3d14a888fede337117747e57
[mTask.git] / int / com / lib / STM32F7xx_HAL_Driver / Src / stm32f7xx_hal_lptim.c
1 /**
2 ******************************************************************************
3 * @file stm32f7xx_hal_lptim.c
4 * @author MCD Application Team
5 * @version V1.1.0
6 * @date 22-April-2016
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) 2016 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 */
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 {
235 return HAL_ERROR;
236 }
237
238 /* Check the parameters */
239 assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
240
241 assert_param(IS_LPTIM_CLOCK_SOURCE(hlptim->Init.Clock.Source));
242 assert_param(IS_LPTIM_CLOCK_PRESCALER(hlptim->Init.Clock.Prescaler));
243 if ((hlptim->Init.Clock.Source) == LPTIM_CLOCKSOURCE_ULPTIM)
244 {
245 assert_param(IS_LPTIM_CLOCK_POLARITY(hlptim->Init.UltraLowPowerClock.Polarity));
246 assert_param(IS_LPTIM_CLOCK_SAMPLE_TIME(hlptim->Init.UltraLowPowerClock.SampleTime));
247 }
248 assert_param(IS_LPTIM_TRG_SOURCE(hlptim->Init.Trigger.Source));
249 if ((hlptim->Init.Trigger.Source) != LPTIM_TRIGSOURCE_SOFTWARE)
250 {
251 assert_param(IS_LPTIM_TRIG_SAMPLE_TIME(hlptim->Init.Trigger.SampleTime));
252 assert_param(IS_LPTIM_EXT_TRG_POLARITY(hlptim->Init.Trigger.ActiveEdge));
253 }
254 assert_param(IS_LPTIM_OUTPUT_POLARITY(hlptim->Init.OutputPolarity));
255 assert_param(IS_LPTIM_UPDATE_MODE(hlptim->Init.UpdateMode));
256 assert_param(IS_LPTIM_COUNTER_SOURCE(hlptim->Init.CounterSource));
257
258 if(hlptim->State == HAL_LPTIM_STATE_RESET)
259 {
260 /* Allocate lock resource and initialize it */
261 hlptim->Lock = HAL_UNLOCKED;
262 /* Init the low level hardware */
263 HAL_LPTIM_MspInit(hlptim);
264 }
265
266 /* Change the LPTIM state */
267 hlptim->State = HAL_LPTIM_STATE_BUSY;
268
269 /* Get the LPTIMx CFGR value */
270 tmpcfgr = hlptim->Instance->CFGR;
271
272 if ((hlptim->Init.Clock.Source) == LPTIM_CLOCKSOURCE_ULPTIM)
273 {
274 tmpcfgr &= (uint32_t)(~(LPTIM_CFGR_CKPOL | LPTIM_CFGR_CKFLT));
275 }
276 if ((hlptim->Init.Trigger.Source) != LPTIM_TRIGSOURCE_SOFTWARE)
277 {
278 tmpcfgr &= (uint32_t)(~ (LPTIM_CFGR_TRGFLT | LPTIM_CFGR_TRIGSEL));
279 }
280
281 /* Clear CKSEL, PRESC, TRIGEN, TRGFLT, WAVPOL, PRELOAD & COUNTMODE bits */
282 tmpcfgr &= (uint32_t)(~(LPTIM_CFGR_CKSEL | LPTIM_CFGR_TRIGEN | LPTIM_CFGR_PRELOAD |
283 LPTIM_CFGR_WAVPOL | LPTIM_CFGR_PRESC | LPTIM_CFGR_COUNTMODE ));
284
285 /* Set initialization parameters */
286 tmpcfgr |= (hlptim->Init.Clock.Source |
287 hlptim->Init.Clock.Prescaler |
288 hlptim->Init.OutputPolarity |
289 hlptim->Init.UpdateMode |
290 hlptim->Init.CounterSource);
291
292 if ((hlptim->Init.Clock.Source) == LPTIM_CLOCKSOURCE_ULPTIM)
293 {
294 tmpcfgr |= (hlptim->Init.UltraLowPowerClock.Polarity |
295 hlptim->Init.UltraLowPowerClock.SampleTime);
296 }
297
298 if ((hlptim->Init.Trigger.Source) != LPTIM_TRIGSOURCE_SOFTWARE)
299 {
300 /* Enable External trigger and set the trigger source */
301 tmpcfgr |= (hlptim->Init.Trigger.Source |
302 hlptim->Init.Trigger.ActiveEdge |
303 hlptim->Init.Trigger.SampleTime);
304 }
305
306 /* Write to LPTIMx CFGR */
307 hlptim->Instance->CFGR = tmpcfgr;
308
309 /* Change the LPTIM state */
310 hlptim->State = HAL_LPTIM_STATE_READY;
311
312 /* Return function status */
313 return HAL_OK;
314 }
315
316 /**
317 * @brief DeInitializes the LPTIM peripheral.
318 * @param hlptim: LPTIM handle
319 * @retval HAL status
320 */
321 HAL_StatusTypeDef HAL_LPTIM_DeInit(LPTIM_HandleTypeDef *hlptim)
322 {
323 /* Check the LPTIM handle allocation */
324 if(hlptim == NULL)
325 {
326 return HAL_ERROR;
327 }
328
329 /* Change the LPTIM state */
330 hlptim->State = HAL_LPTIM_STATE_BUSY;
331
332 /* Disable the LPTIM Peripheral Clock */
333 __HAL_LPTIM_DISABLE(hlptim);
334
335 /* DeInit the low level hardware: CLOCK, NVIC.*/
336 HAL_LPTIM_MspDeInit(hlptim);
337
338 /* Change the LPTIM state */
339 hlptim->State = HAL_LPTIM_STATE_RESET;
340
341 /* Release Lock */
342 __HAL_UNLOCK(hlptim);
343
344 /* Return function status */
345 return HAL_OK;
346 }
347
348 /**
349 * @brief Initializes the LPTIM MSP.
350 * @param hlptim: LPTIM handle
351 * @retval None
352 */
353 __weak void HAL_LPTIM_MspInit(LPTIM_HandleTypeDef *hlptim)
354 {
355 /* Prevent unused argument(s) compilation warning */
356 UNUSED(hlptim);
357
358 /* NOTE : This function Should not be modified, when the callback is needed,
359 the HAL_LPTIM_MspInit could be implemented in the user file
360 */
361 }
362
363 /**
364 * @brief DeInitializes LPTIM MSP.
365 * @param hlptim: LPTIM handle
366 * @retval None
367 */
368 __weak void HAL_LPTIM_MspDeInit(LPTIM_HandleTypeDef *hlptim)
369 {
370 /* Prevent unused argument(s) compilation warning */
371 UNUSED(hlptim);
372
373 /* NOTE : This function Should not be modified, when the callback is needed,
374 the HAL_LPTIM_MspDeInit could be implemented in the user file
375 */
376 }
377
378 /**
379 * @}
380 */
381
382 /** @defgroup LPTIM_Group2 LPTIM Start-Stop operation functions
383 * @brief Start-Stop operation functions.
384 *
385 @verbatim
386 ==============================================================================
387 ##### LPTIM Start Stop operation functions #####
388 ==============================================================================
389 [..] This section provides functions allowing to:
390 (+) Start the PWM mode.
391 (+) Stop the PWM mode.
392 (+) Start the One pulse mode.
393 (+) Stop the One pulse mode.
394 (+) Start the Set once mode.
395 (+) Stop the Set once mode.
396 (+) Start the Encoder mode.
397 (+) Stop the Encoder mode.
398 (+) Start the Timeout mode.
399 (+) Stop the Timeout mode.
400 (+) Start the Counter mode.
401 (+) Stop the Counter mode.
402
403
404 @endverbatim
405 * @{
406 */
407
408 /**
409 * @brief Starts the LPTIM PWM generation.
410 * @param hlptim : LPTIM handle
411 * @param Period : Specifies the Autoreload value.
412 * This parameter must be a value between 0x0000 and 0xFFFF.
413 * @param Pulse : Specifies the compare value.
414 * This parameter must be a value between 0x0000 and 0xFFFF.
415 * @retval HAL status
416 */
417 HAL_StatusTypeDef HAL_LPTIM_PWM_Start(LPTIM_HandleTypeDef *hlptim, uint32_t Period, uint32_t Pulse)
418 {
419 /* Check the parameters */
420 assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
421 assert_param(IS_LPTIM_PERIOD(Period));
422 assert_param(IS_LPTIM_PULSE(Pulse));
423
424 /* Set the LPTIM state */
425 hlptim->State= HAL_LPTIM_STATE_BUSY;
426
427 /* Reset WAVE bit to set PWM mode */
428 hlptim->Instance->CFGR &= ~LPTIM_CFGR_WAVE;
429
430 /* Enable the Peripheral */
431 __HAL_LPTIM_ENABLE(hlptim);
432
433 /* Load the period value in the autoreload register */
434 __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
435
436 /* Load the pulse value in the compare register */
437 __HAL_LPTIM_COMPARE_SET(hlptim, Pulse);
438
439 /* Start timer in continuous mode */
440 __HAL_LPTIM_START_CONTINUOUS(hlptim);
441
442 /* Change the TIM state*/
443 hlptim->State= HAL_LPTIM_STATE_READY;
444
445 /* Return function status */
446 return HAL_OK;
447 }
448
449 /**
450 * @brief Stops the LPTIM PWM generation.
451 * @param hlptim : LPTIM handle
452 * @retval HAL status
453 */
454 HAL_StatusTypeDef HAL_LPTIM_PWM_Stop(LPTIM_HandleTypeDef *hlptim)
455 {
456 /* Check the parameters */
457 assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
458
459 /* Set the LPTIM state */
460 hlptim->State= HAL_LPTIM_STATE_BUSY;
461
462 /* Disable the Peripheral */
463 __HAL_LPTIM_DISABLE(hlptim);
464
465 /* Change the TIM state*/
466 hlptim->State= HAL_LPTIM_STATE_READY;
467
468 /* Return function status */
469 return HAL_OK;
470 }
471
472 /**
473 * @brief Starts the LPTIM PWM generation in interrupt mode.
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_PWM_Start_IT(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
488 /* Set the LPTIM state */
489 hlptim->State= HAL_LPTIM_STATE_BUSY;
490
491 /* Reset WAVE bit to set PWM mode */
492 hlptim->Instance->CFGR &= ~LPTIM_CFGR_WAVE;
493
494 /* Enable Autoreload write complete interrupt */
495 __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARROK);
496
497 /* Enable Compare write complete interrupt */
498 __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_CMPOK);
499
500 /* Enable Autoreload match interrupt */
501 __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARRM);
502
503 /* Enable Compare match interrupt */
504 __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_CMPM);
505
506 /* If external trigger source is used, then enable external trigger interrupt */
507 if ((hlptim->Init.Trigger.Source) != LPTIM_TRIGSOURCE_SOFTWARE)
508 {
509 /* Enable external trigger interrupt */
510 __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_EXTTRIG);
511 }
512
513 /* Enable the Peripheral */
514 __HAL_LPTIM_ENABLE(hlptim);
515
516 /* Load the period value in the autoreload register */
517 __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
518
519 /* Load the pulse value in the compare register */
520 __HAL_LPTIM_COMPARE_SET(hlptim, Pulse);
521
522 /* Start timer in continuous mode */
523 __HAL_LPTIM_START_CONTINUOUS(hlptim);
524
525 /* Change the TIM state*/
526 hlptim->State= HAL_LPTIM_STATE_READY;
527
528 /* Return function status */
529 return HAL_OK;
530 }
531
532 /**
533 * @brief Stops the LPTIM PWM generation in interrupt mode.
534 * @param hlptim : LPTIM handle
535 * @retval HAL status
536 */
537 HAL_StatusTypeDef HAL_LPTIM_PWM_Stop_IT(LPTIM_HandleTypeDef *hlptim)
538 {
539 /* Check the parameters */
540 assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
541
542 /* Set the LPTIM state */
543 hlptim->State= HAL_LPTIM_STATE_BUSY;
544
545 /* Disable the Peripheral */
546 __HAL_LPTIM_DISABLE(hlptim);
547
548 /* Disable Autoreload write complete interrupt */
549 __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_ARROK);
550
551 /* Disable Compare write complete interrupt */
552 __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_CMPOK);
553
554 /* Disable Autoreload match interrupt */
555 __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_ARRM);
556
557 /* Disable Compare match interrupt */
558 __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_CMPM);
559
560 /* If external trigger source is used, then disable external trigger interrupt */
561 if ((hlptim->Init.Trigger.Source) != LPTIM_TRIGSOURCE_SOFTWARE)
562 {
563 /* Disable external trigger interrupt */
564 __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_EXTTRIG);
565 }
566
567 /* Change the TIM state*/
568 hlptim->State= HAL_LPTIM_STATE_READY;
569
570 /* Return function status */
571 return HAL_OK;
572 }
573
574 /**
575 * @brief Starts the LPTIM One pulse generation.
576 * @param hlptim : LPTIM handle
577 * @param Period : Specifies the Autoreload value.
578 * This parameter must be a value between 0x0000 and 0xFFFF.
579 * @param Pulse : Specifies the compare value.
580 * This parameter must be a value between 0x0000 and 0xFFFF.
581 * @retval HAL status
582 */
583 HAL_StatusTypeDef HAL_LPTIM_OnePulse_Start(LPTIM_HandleTypeDef *hlptim, uint32_t Period, uint32_t Pulse)
584 {
585 /* Check the parameters */
586 assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
587 assert_param(IS_LPTIM_PERIOD(Period));
588 assert_param(IS_LPTIM_PULSE(Pulse));
589
590 /* Set the LPTIM state */
591 hlptim->State= HAL_LPTIM_STATE_BUSY;
592
593 /* Reset WAVE bit to set one pulse mode */
594 hlptim->Instance->CFGR &= ~LPTIM_CFGR_WAVE;
595
596 /* Enable the Peripheral */
597 __HAL_LPTIM_ENABLE(hlptim);
598
599 /* Load the period value in the autoreload register */
600 __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
601
602 /* Load the pulse value in the compare register */
603 __HAL_LPTIM_COMPARE_SET(hlptim, Pulse);
604
605 /* Start timer in continuous mode */
606 __HAL_LPTIM_START_SINGLE(hlptim);
607
608 /* Change the TIM state*/
609 hlptim->State= HAL_LPTIM_STATE_READY;
610
611 /* Return function status */
612 return HAL_OK;
613 }
614
615 /**
616 * @brief Stops the LPTIM One pulse generation.
617 * @param hlptim : LPTIM handle
618 * @retval HAL status
619 */
620 HAL_StatusTypeDef HAL_LPTIM_OnePulse_Stop(LPTIM_HandleTypeDef *hlptim)
621 {
622 /* Check the parameters */
623 assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
624
625 /* Set the LPTIM state */
626 hlptim->State= HAL_LPTIM_STATE_BUSY;
627
628 /* Disable the Peripheral */
629 __HAL_LPTIM_DISABLE(hlptim);
630
631 /* Change the TIM state*/
632 hlptim->State= HAL_LPTIM_STATE_READY;
633
634 /* Return function status */
635 return HAL_OK;
636 }
637
638 /**
639 * @brief Starts the LPTIM One pulse generation in interrupt mode.
640 * @param hlptim : LPTIM handle
641 * @param Period : Specifies the Autoreload value.
642 * This parameter must be a value between 0x0000 and 0xFFFF.
643 * @param Pulse : Specifies the compare value.
644 * This parameter must be a value between 0x0000 and 0xFFFF.
645 * @retval HAL status
646 */
647 HAL_StatusTypeDef HAL_LPTIM_OnePulse_Start_IT(LPTIM_HandleTypeDef *hlptim, uint32_t Period, uint32_t Pulse)
648 {
649 /* Check the parameters */
650 assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
651 assert_param(IS_LPTIM_PERIOD(Period));
652 assert_param(IS_LPTIM_PULSE(Pulse));
653
654 /* Set the LPTIM state */
655 hlptim->State= HAL_LPTIM_STATE_BUSY;
656
657 /* Reset WAVE bit to set one pulse mode */
658 hlptim->Instance->CFGR &= ~LPTIM_CFGR_WAVE;
659
660 /* Enable Autoreload write complete interrupt */
661 __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARROK);
662
663 /* Enable Compare write complete interrupt */
664 __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_CMPOK);
665
666 /* Enable Autoreload match interrupt */
667 __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARRM);
668
669 /* Enable Compare match interrupt */
670 __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_CMPM);
671
672 /* If external trigger source is used, then enable external trigger interrupt */
673 if ((hlptim->Init.Trigger.Source) != LPTIM_TRIGSOURCE_SOFTWARE)
674 {
675 /* Enable external trigger interrupt */
676 __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_EXTTRIG);
677 }
678
679 /* Enable the Peripheral */
680 __HAL_LPTIM_ENABLE(hlptim);
681
682 /* Load the period value in the autoreload register */
683 __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
684
685 /* Load the pulse value in the compare register */
686 __HAL_LPTIM_COMPARE_SET(hlptim, Pulse);
687
688 /* Start timer in continuous mode */
689 __HAL_LPTIM_START_SINGLE(hlptim);
690
691 /* Change the TIM state*/
692 hlptim->State= HAL_LPTIM_STATE_READY;
693
694 /* Return function status */
695 return HAL_OK;
696 }
697
698 /**
699 * @brief Stops the LPTIM One pulse generation in interrupt mode.
700 * @param hlptim : LPTIM handle
701 * @retval HAL status
702 */
703 HAL_StatusTypeDef HAL_LPTIM_OnePulse_Stop_IT(LPTIM_HandleTypeDef *hlptim)
704 {
705 /* Check the parameters */
706 assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
707
708 /* Set the LPTIM state */
709 hlptim->State= HAL_LPTIM_STATE_BUSY;
710
711 /* Disable the Peripheral */
712 __HAL_LPTIM_DISABLE(hlptim);
713
714 /* Disable Autoreload write complete interrupt */
715 __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_ARROK);
716
717 /* Disable Compare write complete interrupt */
718 __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_CMPOK);
719
720 /* Disable Autoreload match interrupt */
721 __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_ARRM);
722
723 /* Disable Compare match interrupt */
724 __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_CMPM);
725
726 /* If external trigger source is used, then disable external trigger interrupt */
727 if ((hlptim->Init.Trigger.Source) != LPTIM_TRIGSOURCE_SOFTWARE)
728 {
729 /* Disable external trigger interrupt */
730 __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_EXTTRIG);
731 }
732
733 /* Change the TIM state*/
734 hlptim->State= HAL_LPTIM_STATE_READY;
735
736 /* Return function status */
737 return HAL_OK;
738 }
739
740 /**
741 * @brief Starts the LPTIM in Set once mode.
742 * @param hlptim : LPTIM handle
743 * @param Period : Specifies the Autoreload value.
744 * This parameter must be a value between 0x0000 and 0xFFFF.
745 * @param Pulse : Specifies the compare value.
746 * This parameter must be a value between 0x0000 and 0xFFFF.
747 * @retval HAL status
748 */
749 HAL_StatusTypeDef HAL_LPTIM_SetOnce_Start(LPTIM_HandleTypeDef *hlptim, uint32_t Period, uint32_t Pulse)
750 {
751 /* Check the parameters */
752 assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
753 assert_param(IS_LPTIM_PERIOD(Period));
754 assert_param(IS_LPTIM_PULSE(Pulse));
755
756 /* Set the LPTIM state */
757 hlptim->State= HAL_LPTIM_STATE_BUSY;
758
759 /* Set WAVE bit to enable the set once mode */
760 hlptim->Instance->CFGR |= LPTIM_CFGR_WAVE;
761
762 /* Enable the Peripheral */
763 __HAL_LPTIM_ENABLE(hlptim);
764
765 /* Load the period value in the autoreload register */
766 __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
767
768 /* Load the pulse value in the compare register */
769 __HAL_LPTIM_COMPARE_SET(hlptim, Pulse);
770
771 /* Start timer in continuous mode */
772 __HAL_LPTIM_START_SINGLE(hlptim);
773
774 /* Change the TIM state*/
775 hlptim->State= HAL_LPTIM_STATE_READY;
776
777 /* Return function status */
778 return HAL_OK;
779 }
780
781 /**
782 * @brief Stops the LPTIM Set once mode.
783 * @param hlptim : LPTIM handle
784 * @retval HAL status
785 */
786 HAL_StatusTypeDef HAL_LPTIM_SetOnce_Stop(LPTIM_HandleTypeDef *hlptim)
787 {
788 /* Check the parameters */
789 assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
790
791 /* Set the LPTIM state */
792 hlptim->State= HAL_LPTIM_STATE_BUSY;
793
794 /* Disable the Peripheral */
795 __HAL_LPTIM_DISABLE(hlptim);
796
797 /* Change the TIM state*/
798 hlptim->State= HAL_LPTIM_STATE_READY;
799
800 /* Return function status */
801 return HAL_OK;
802 }
803
804 /**
805 * @brief Starts the LPTIM Set once mode in interrupt mode.
806 * @param hlptim : LPTIM handle
807 * @param Period : Specifies the Autoreload value.
808 * This parameter must be a value between 0x0000 and 0xFFFF.
809 * @param Pulse : Specifies the compare value.
810 * This parameter must be a value between 0x0000 and 0xFFFF.
811 * @retval HAL status
812 */
813 HAL_StatusTypeDef HAL_LPTIM_SetOnce_Start_IT(LPTIM_HandleTypeDef *hlptim, uint32_t Period, uint32_t Pulse)
814 {
815 /* Check the parameters */
816 assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
817 assert_param(IS_LPTIM_PERIOD(Period));
818 assert_param(IS_LPTIM_PULSE(Pulse));
819
820 /* Set the LPTIM state */
821 hlptim->State= HAL_LPTIM_STATE_BUSY;
822
823 /* Set WAVE bit to enable the set once mode */
824 hlptim->Instance->CFGR |= LPTIM_CFGR_WAVE;
825
826 /* Enable Autoreload write complete interrupt */
827 __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARROK);
828
829 /* Enable Compare write complete interrupt */
830 __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_CMPOK);
831
832 /* Enable Autoreload match interrupt */
833 __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARRM);
834
835 /* Enable Compare match interrupt */
836 __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_CMPM);
837
838 /* If external trigger source is used, then enable external trigger interrupt */
839 if ((hlptim->Init.Trigger.Source) != LPTIM_TRIGSOURCE_SOFTWARE)
840 {
841 /* Enable external trigger interrupt */
842 __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_EXTTRIG);
843 }
844
845 /* Enable the Peripheral */
846 __HAL_LPTIM_ENABLE(hlptim);
847
848 /* Load the period value in the autoreload register */
849 __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
850
851 /* Load the pulse value in the compare register */
852 __HAL_LPTIM_COMPARE_SET(hlptim, Pulse);
853
854 /* Start timer in continuous mode */
855 __HAL_LPTIM_START_SINGLE(hlptim);
856
857 /* Change the TIM state*/
858 hlptim->State= HAL_LPTIM_STATE_READY;
859
860 /* Return function status */
861 return HAL_OK;
862 }
863
864 /**
865 * @brief Stops the LPTIM Set once mode in interrupt mode.
866 * @param hlptim : LPTIM handle
867 * @retval HAL status
868 */
869 HAL_StatusTypeDef HAL_LPTIM_SetOnce_Stop_IT(LPTIM_HandleTypeDef *hlptim)
870 {
871 /* Check the parameters */
872 assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
873
874 /* Set the LPTIM state */
875 hlptim->State= HAL_LPTIM_STATE_BUSY;
876
877 /* Disable the Peripheral */
878 __HAL_LPTIM_DISABLE(hlptim);
879
880 /* Disable Autoreload write complete interrupt */
881 __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_ARROK);
882
883 /* Disable Compare write complete interrupt */
884 __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_CMPOK);
885
886 /* Disable Autoreload match interrupt */
887 __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_ARRM);
888
889 /* Disable Compare match interrupt */
890 __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_CMPM);
891
892 /* If external trigger source is used, then disable external trigger interrupt */
893 if ((hlptim->Init.Trigger.Source) != LPTIM_TRIGSOURCE_SOFTWARE)
894 {
895 /* Disable external trigger interrupt */
896 __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_EXTTRIG);
897 }
898
899 /* Change the TIM state*/
900 hlptim->State= HAL_LPTIM_STATE_READY;
901
902 /* Return function status */
903 return HAL_OK;
904 }
905
906 /**
907 * @brief Starts the Encoder interface.
908 * @param hlptim : LPTIM handle
909 * @param Period : Specifies the Autoreload value.
910 * This parameter must be a value between 0x0000 and 0xFFFF.
911 * @retval HAL status
912 */
913 HAL_StatusTypeDef HAL_LPTIM_Encoder_Start(LPTIM_HandleTypeDef *hlptim, uint32_t Period)
914 {
915 uint32_t tmpcfgr = 0;
916
917 /* Check the parameters */
918 assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
919 assert_param(IS_LPTIM_PERIOD(Period));
920 assert_param(hlptim->Init.Clock.Source == LPTIM_CLOCKSOURCE_APBCLOCK_LPOSC);
921 assert_param(hlptim->Init.Clock.Prescaler == LPTIM_PRESCALER_DIV1);
922 assert_param(IS_LPTIM_CLOCK_POLARITY(hlptim->Init.UltraLowPowerClock.Polarity));
923
924 /* Set the LPTIM state */
925 hlptim->State= HAL_LPTIM_STATE_BUSY;
926
927 /* Get the LPTIMx CFGR value */
928 tmpcfgr = hlptim->Instance->CFGR;
929
930 /* Clear CKPOL bits */
931 tmpcfgr &= (uint32_t)(~LPTIM_CFGR_CKPOL);
932
933 /* Set Input polarity */
934 tmpcfgr |= hlptim->Init.UltraLowPowerClock.Polarity;
935
936 /* Write to LPTIMx CFGR */
937 hlptim->Instance->CFGR = tmpcfgr;
938
939 /* Set ENC bit to enable the encoder interface */
940 hlptim->Instance->CFGR |= LPTIM_CFGR_ENC;
941
942 /* Enable the Peripheral */
943 __HAL_LPTIM_ENABLE(hlptim);
944
945 /* Load the period value in the autoreload register */
946 __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
947
948 /* Start timer in continuous mode */
949 __HAL_LPTIM_START_CONTINUOUS(hlptim);
950
951 /* Change the TIM state*/
952 hlptim->State= HAL_LPTIM_STATE_READY;
953
954 /* Return function status */
955 return HAL_OK;
956 }
957
958 /**
959 * @brief Stops the Encoder interface.
960 * @param hlptim : LPTIM handle
961 * @retval HAL status
962 */
963 HAL_StatusTypeDef HAL_LPTIM_Encoder_Stop(LPTIM_HandleTypeDef *hlptim)
964 {
965 /* Check the parameters */
966 assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
967
968 /* Set the LPTIM state */
969 hlptim->State= HAL_LPTIM_STATE_BUSY;
970
971 /* Disable the Peripheral */
972 __HAL_LPTIM_DISABLE(hlptim);
973
974 /* Reset ENC bit to disable the encoder interface */
975 hlptim->Instance->CFGR &= ~LPTIM_CFGR_ENC;
976
977 /* Change the TIM state*/
978 hlptim->State= HAL_LPTIM_STATE_READY;
979
980 /* Return function status */
981 return HAL_OK;
982 }
983
984 /**
985 * @brief Starts the Encoder interface in interrupt mode.
986 * @param hlptim : LPTIM handle
987 * @param Period : Specifies the Autoreload value.
988 * This parameter must be a value between 0x0000 and 0xFFFF.
989 * @retval HAL status
990 */
991 HAL_StatusTypeDef HAL_LPTIM_Encoder_Start_IT(LPTIM_HandleTypeDef *hlptim, uint32_t Period)
992 {
993 uint32_t tmpcfgr = 0;
994
995 /* Check the parameters */
996 assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
997 assert_param(IS_LPTIM_PERIOD(Period));
998 assert_param(hlptim->Init.Clock.Source == LPTIM_CLOCKSOURCE_APBCLOCK_LPOSC);
999 assert_param(hlptim->Init.Clock.Prescaler == LPTIM_PRESCALER_DIV1);
1000 assert_param(IS_LPTIM_CLOCK_POLARITY(hlptim->Init.UltraLowPowerClock.Polarity));
1001
1002 /* Set the LPTIM state */
1003 hlptim->State= HAL_LPTIM_STATE_BUSY;
1004
1005 /* Configure edge sensitivity for encoder mode */
1006 /* Get the LPTIMx CFGR value */
1007 tmpcfgr = hlptim->Instance->CFGR;
1008
1009 /* Clear CKPOL bits */
1010 tmpcfgr &= (uint32_t)(~LPTIM_CFGR_CKPOL);
1011
1012 /* Set Input polarity */
1013 tmpcfgr |= hlptim->Init.UltraLowPowerClock.Polarity;
1014
1015 /* Write to LPTIMx CFGR */
1016 hlptim->Instance->CFGR = tmpcfgr;
1017
1018 /* Set ENC bit to enable the encoder interface */
1019 hlptim->Instance->CFGR |= LPTIM_CFGR_ENC;
1020
1021 /* Enable "switch to down direction" interrupt */
1022 __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_DOWN);
1023
1024 /* Enable "switch to up direction" interrupt */
1025 __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_UP);
1026
1027 /* Enable the Peripheral */
1028 __HAL_LPTIM_ENABLE(hlptim);
1029
1030 /* Load the period value in the autoreload register */
1031 __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
1032
1033 /* Start timer in continuous mode */
1034 __HAL_LPTIM_START_CONTINUOUS(hlptim);
1035
1036 /* Change the TIM state*/
1037 hlptim->State= HAL_LPTIM_STATE_READY;
1038
1039 /* Return function status */
1040 return HAL_OK;
1041 }
1042
1043 /**
1044 * @brief Stops the Encoder interface in interrupt mode.
1045 * @param hlptim : LPTIM handle
1046 * @retval HAL status
1047 */
1048 HAL_StatusTypeDef HAL_LPTIM_Encoder_Stop_IT(LPTIM_HandleTypeDef *hlptim)
1049 {
1050 /* Check the parameters */
1051 assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
1052
1053 /* Set the LPTIM state */
1054 hlptim->State= HAL_LPTIM_STATE_BUSY;
1055
1056 /* Disable the Peripheral */
1057 __HAL_LPTIM_DISABLE(hlptim);
1058
1059 /* Reset ENC bit to disable the encoder interface */
1060 hlptim->Instance->CFGR &= ~LPTIM_CFGR_ENC;
1061
1062 /* Disable "switch to down direction" interrupt */
1063 __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_DOWN);
1064
1065 /* Disable "switch to up direction" interrupt */
1066 __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_UP);
1067
1068 /* Change the TIM state*/
1069 hlptim->State= HAL_LPTIM_STATE_READY;
1070
1071 /* Return function status */
1072 return HAL_OK;
1073 }
1074
1075 /**
1076 * @brief Starts the Timeout function. The first trigger event will start the
1077 * timer, any successive trigger event will reset the counter and
1078 * the timer restarts.
1079 * @param hlptim : LPTIM handle
1080 * @param Period : Specifies the Autoreload value.
1081 * This parameter must be a value between 0x0000 and 0xFFFF.
1082 * @param Timeout : Specifies the TimeOut value to rest the counter.
1083 * This parameter must be a value between 0x0000 and 0xFFFF.
1084 * @retval HAL status
1085 */
1086 HAL_StatusTypeDef HAL_LPTIM_TimeOut_Start(LPTIM_HandleTypeDef *hlptim, uint32_t Period, uint32_t Timeout)
1087 {
1088 /* Check the parameters */
1089 assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
1090 assert_param(IS_LPTIM_PERIOD(Period));
1091 assert_param(IS_LPTIM_PULSE(Timeout));
1092
1093 /* Set the LPTIM state */
1094 hlptim->State= HAL_LPTIM_STATE_BUSY;
1095
1096 /* Set TIMOUT bit to enable the timeout function */
1097 hlptim->Instance->CFGR |= LPTIM_CFGR_TIMOUT;
1098
1099 /* Enable the Peripheral */
1100 __HAL_LPTIM_ENABLE(hlptim);
1101
1102 /* Load the period value in the autoreload register */
1103 __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
1104
1105 /* Load the Timeout value in the compare register */
1106 __HAL_LPTIM_COMPARE_SET(hlptim, Timeout);
1107
1108 /* Start timer in continuous mode */
1109 __HAL_LPTIM_START_CONTINUOUS(hlptim);
1110
1111 /* Change the TIM state*/
1112 hlptim->State= HAL_LPTIM_STATE_READY;
1113
1114 /* Return function status */
1115 return HAL_OK;
1116 }
1117
1118 /**
1119 * @brief Stops the Timeout function.
1120 * @param hlptim : LPTIM handle
1121 * @retval HAL status
1122 */
1123 HAL_StatusTypeDef HAL_LPTIM_TimeOut_Stop(LPTIM_HandleTypeDef *hlptim)
1124 {
1125 /* Check the parameters */
1126 assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
1127
1128 /* Set the LPTIM state */
1129 hlptim->State= HAL_LPTIM_STATE_BUSY;
1130
1131 /* Disable the Peripheral */
1132 __HAL_LPTIM_DISABLE(hlptim);
1133
1134 /* Reset TIMOUT bit to enable the timeout function */
1135 hlptim->Instance->CFGR &= ~LPTIM_CFGR_TIMOUT;
1136
1137 /* Change the TIM state*/
1138 hlptim->State= HAL_LPTIM_STATE_READY;
1139
1140 /* Return function status */
1141 return HAL_OK;
1142 }
1143
1144 /**
1145 * @brief Starts the Timeout function in interrupt mode. The first trigger
1146 * event will start the timer, any successive trigger event will reset
1147 * the counter and the timer restarts.
1148 * @param hlptim : LPTIM handle
1149 * @param Period : Specifies the Autoreload value.
1150 * This parameter must be a value between 0x0000 and 0xFFFF.
1151 * @param Timeout : Specifies the TimeOut value to rest the counter.
1152 * This parameter must be a value between 0x0000 and 0xFFFF.
1153 * @retval HAL status
1154 */
1155 HAL_StatusTypeDef HAL_LPTIM_TimeOut_Start_IT(LPTIM_HandleTypeDef *hlptim, uint32_t Period, uint32_t Timeout)
1156 {
1157 /* Check the parameters */
1158 assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
1159 assert_param(IS_LPTIM_PERIOD(Period));
1160 assert_param(IS_LPTIM_PULSE(Timeout));
1161
1162 /* Set the LPTIM state */
1163 hlptim->State= HAL_LPTIM_STATE_BUSY;
1164
1165 /* Enable EXTI Line interrupt on the LPTIM Wake-up Timer */
1166 __HAL_LPTIM_WAKEUPTIMER_EXTI_ENABLE_IT();
1167
1168 /* Enable rising edge trigger on the LPTIM Wake-up Timer Exti line */
1169 __HAL_LPTIM_WAKEUPTIMER_EXTI_ENABLE_RISING_EDGE();
1170
1171 /* Set TIMOUT bit to enable the timeout function */
1172 hlptim->Instance->CFGR |= LPTIM_CFGR_TIMOUT;
1173
1174 /* Enable Compare match interrupt */
1175 __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_CMPM);
1176
1177 /* Enable the Peripheral */
1178 __HAL_LPTIM_ENABLE(hlptim);
1179
1180 /* Load the period value in the autoreload register */
1181 __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
1182
1183 /* Load the Timeout value in the compare register */
1184 __HAL_LPTIM_COMPARE_SET(hlptim, Timeout);
1185
1186 /* Start timer in continuous mode */
1187 __HAL_LPTIM_START_CONTINUOUS(hlptim);
1188
1189 /* Change the TIM state*/
1190 hlptim->State= HAL_LPTIM_STATE_READY;
1191
1192 /* Return function status */
1193 return HAL_OK;
1194 }
1195
1196 /**
1197 * @brief Stops the Timeout function in interrupt mode.
1198 * @param hlptim : LPTIM handle
1199 * @retval HAL status
1200 */
1201 HAL_StatusTypeDef HAL_LPTIM_TimeOut_Stop_IT(LPTIM_HandleTypeDef *hlptim)
1202 {
1203 /* Check the parameters */
1204 assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
1205
1206 /* Set the LPTIM state */
1207 hlptim->State= HAL_LPTIM_STATE_BUSY;
1208
1209 /* Disable rising edge trigger on the LPTIM Wake-up Timer Exti line */
1210 __HAL_LPTIM_WAKEUPTIMER_EXTI_DISABLE_RISING_EDGE();
1211
1212 /* Disable EXTI Line interrupt on the LPTIM Wake-up Timer */
1213 __HAL_LPTIM_WAKEUPTIMER_EXTI_DISABLE_IT();
1214
1215 /* Disable the Peripheral */
1216 __HAL_LPTIM_DISABLE(hlptim);
1217
1218 /* Reset TIMOUT bit to enable the timeout function */
1219 hlptim->Instance->CFGR &= ~LPTIM_CFGR_TIMOUT;
1220
1221 /* Disable Compare match interrupt */
1222 __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_CMPM);
1223
1224 /* Change the TIM state*/
1225 hlptim->State= HAL_LPTIM_STATE_READY;
1226
1227 /* Return function status */
1228 return HAL_OK;
1229 }
1230
1231 /**
1232 * @brief Starts the Counter mode.
1233 * @param hlptim : LPTIM handle
1234 * @param Period : Specifies the Autoreload value.
1235 * This parameter must be a value between 0x0000 and 0xFFFF.
1236 * @retval HAL status
1237 */
1238 HAL_StatusTypeDef HAL_LPTIM_Counter_Start(LPTIM_HandleTypeDef *hlptim, uint32_t Period)
1239 {
1240 /* Check the parameters */
1241 assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
1242 assert_param(IS_LPTIM_PERIOD(Period));
1243
1244 /* Set the LPTIM state */
1245 hlptim->State= HAL_LPTIM_STATE_BUSY;
1246
1247 /* If clock source is not ULPTIM clock and counter source is external, then it must not be prescaled */
1248 if((hlptim->Init.Clock.Source != LPTIM_CLOCKSOURCE_ULPTIM) && (hlptim->Init.CounterSource == LPTIM_COUNTERSOURCE_EXTERNAL))
1249 {
1250 /* Check if clock is prescaled */
1251 assert_param(IS_LPTIM_CLOCK_PRESCALERDIV1(hlptim->Init.Clock.Prescaler));
1252 /* Set clock prescaler to 0 */
1253 hlptim->Instance->CFGR &= ~LPTIM_CFGR_PRESC;
1254 }
1255
1256 /* Enable the Peripheral */
1257 __HAL_LPTIM_ENABLE(hlptim);
1258
1259 /* Load the period value in the autoreload register */
1260 __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
1261
1262 /* Start timer in continuous mode */
1263 __HAL_LPTIM_START_CONTINUOUS(hlptim);
1264
1265 /* Change the TIM state*/
1266 hlptim->State= HAL_LPTIM_STATE_READY;
1267
1268 /* Return function status */
1269 return HAL_OK;
1270 }
1271
1272 /**
1273 * @brief Stops the Counter mode.
1274 * @param hlptim : LPTIM handle
1275 * @retval HAL status
1276 */
1277 HAL_StatusTypeDef HAL_LPTIM_Counter_Stop(LPTIM_HandleTypeDef *hlptim)
1278 {
1279 /* Check the parameters */
1280 assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
1281
1282 /* Set the LPTIM state */
1283 hlptim->State= HAL_LPTIM_STATE_BUSY;
1284
1285 /* Disable the Peripheral */
1286 __HAL_LPTIM_DISABLE(hlptim);
1287
1288 /* Change the TIM state*/
1289 hlptim->State= HAL_LPTIM_STATE_READY;
1290
1291 /* Return function status */
1292 return HAL_OK;
1293 }
1294
1295 /**
1296 * @brief Starts the Counter mode in interrupt mode.
1297 * @param hlptim : LPTIM handle
1298 * @param Period : Specifies the Autoreload value.
1299 * This parameter must be a value between 0x0000 and 0xFFFF.
1300 * @retval HAL status
1301 */
1302 HAL_StatusTypeDef HAL_LPTIM_Counter_Start_IT(LPTIM_HandleTypeDef *hlptim, uint32_t Period)
1303 {
1304 /* Check the parameters */
1305 assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
1306 assert_param(IS_LPTIM_PERIOD(Period));
1307
1308 /* Set the LPTIM state */
1309 hlptim->State= HAL_LPTIM_STATE_BUSY;
1310
1311 /* Enable EXTI Line interrupt on the LPTIM Wake-up Timer */
1312 __HAL_LPTIM_WAKEUPTIMER_EXTI_ENABLE_IT();
1313
1314 /* Enable rising edge trigger on the LPTIM Wake-up Timer Exti line */
1315 __HAL_LPTIM_WAKEUPTIMER_EXTI_ENABLE_RISING_EDGE();
1316
1317 /* If clock source is not ULPTIM clock and counter source is external, then it must not be prescaled */
1318 if((hlptim->Init.Clock.Source != LPTIM_CLOCKSOURCE_ULPTIM) && (hlptim->Init.CounterSource == LPTIM_COUNTERSOURCE_EXTERNAL))
1319 {
1320 /* Check if clock is prescaled */
1321 assert_param(IS_LPTIM_CLOCK_PRESCALERDIV1(hlptim->Init.Clock.Prescaler));
1322 /* Set clock prescaler to 0 */
1323 hlptim->Instance->CFGR &= ~LPTIM_CFGR_PRESC;
1324 }
1325
1326 /* Enable Autoreload write complete interrupt */
1327 __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARROK);
1328
1329 /* Enable Autoreload match interrupt */
1330 __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARRM);
1331
1332 /* Enable the Peripheral */
1333 __HAL_LPTIM_ENABLE(hlptim);
1334
1335 /* Load the period value in the autoreload register */
1336 __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
1337
1338 /* Start timer in continuous mode */
1339 __HAL_LPTIM_START_CONTINUOUS(hlptim);
1340
1341 /* Change the TIM state*/
1342 hlptim->State= HAL_LPTIM_STATE_READY;
1343
1344 /* Return function status */
1345 return HAL_OK;
1346 }
1347
1348 /**
1349 * @brief Stops the Counter mode in interrupt mode.
1350 * @param hlptim : LPTIM handle
1351 * @retval HAL status
1352 */
1353 HAL_StatusTypeDef HAL_LPTIM_Counter_Stop_IT(LPTIM_HandleTypeDef *hlptim)
1354 {
1355 /* Check the parameters */
1356 assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
1357
1358 /* Set the LPTIM state */
1359 hlptim->State= HAL_LPTIM_STATE_BUSY;
1360
1361 /* Disable rising edge trigger on the LPTIM Wake-up Timer Exti line */
1362 __HAL_LPTIM_WAKEUPTIMER_EXTI_DISABLE_RISING_EDGE();
1363
1364 /* Disable EXTI Line interrupt on the LPTIM Wake-up Timer */
1365 __HAL_LPTIM_WAKEUPTIMER_EXTI_DISABLE_IT();
1366
1367 /* Disable the Peripheral */
1368 __HAL_LPTIM_DISABLE(hlptim);
1369
1370 /* Disable Autoreload write complete interrupt */
1371 __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_ARROK);
1372
1373 /* Disable Autoreload match interrupt */
1374 __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_ARRM);
1375
1376 /* Change the TIM state*/
1377 hlptim->State= HAL_LPTIM_STATE_READY;
1378
1379 /* Return function status */
1380 return HAL_OK;
1381 }
1382
1383 /**
1384 * @}
1385 */
1386
1387 /** @defgroup LPTIM_Group3 LPTIM Read operation functions
1388 * @brief Read operation functions.
1389 *
1390 @verbatim
1391 ==============================================================================
1392 ##### LPTIM Read operation functions #####
1393 ==============================================================================
1394 [..] This section provides LPTIM Reading functions.
1395 (+) Read the counter value.
1396 (+) Read the period (Auto-reload) value.
1397 (+) Read the pulse (Compare)value.
1398 @endverbatim
1399 * @{
1400 */
1401
1402 /**
1403 * @brief This function returns the current counter value.
1404 * @param hlptim: LPTIM handle
1405 * @retval Counter value.
1406 */
1407 uint32_t HAL_LPTIM_ReadCounter(LPTIM_HandleTypeDef *hlptim)
1408 {
1409 /* Check the parameters */
1410 assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
1411
1412 return (hlptim->Instance->CNT);
1413 }
1414
1415 /**
1416 * @brief This function return the current Autoreload (Period) value.
1417 * @param hlptim: LPTIM handle
1418 * @retval Autoreload value.
1419 */
1420 uint32_t HAL_LPTIM_ReadAutoReload(LPTIM_HandleTypeDef *hlptim)
1421 {
1422 /* Check the parameters */
1423 assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
1424
1425 return (hlptim->Instance->ARR);
1426 }
1427
1428 /**
1429 * @brief This function return the current Compare (Pulse) value.
1430 * @param hlptim: LPTIM handle
1431 * @retval Compare value.
1432 */
1433 uint32_t HAL_LPTIM_ReadCompare(LPTIM_HandleTypeDef *hlptim)
1434 {
1435 /* Check the parameters */
1436 assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
1437
1438 return (hlptim->Instance->CMP);
1439 }
1440
1441 /**
1442 * @}
1443 */
1444
1445
1446
1447 /** @defgroup LPTIM_Group4 LPTIM IRQ handler
1448 * @brief LPTIM IRQ handler.
1449 *
1450 @verbatim
1451 ==============================================================================
1452 ##### LPTIM IRQ handler #####
1453 ==============================================================================
1454 [..] This section provides LPTIM IRQ handler function.
1455
1456 @endverbatim
1457 * @{
1458 */
1459
1460 /**
1461 * @brief This function handles LPTIM interrupt request.
1462 * @param hlptim: LPTIM handle
1463 * @retval None
1464 */
1465 void HAL_LPTIM_IRQHandler(LPTIM_HandleTypeDef *hlptim)
1466 {
1467 /* Compare match interrupt */
1468 if(__HAL_LPTIM_GET_FLAG(hlptim, LPTIM_FLAG_CMPM) != RESET)
1469 {
1470 if(__HAL_LPTIM_GET_IT_SOURCE(hlptim, LPTIM_IT_CMPM) !=RESET)
1471 {
1472 /* Clear Compare match flag */
1473 __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_CMPM);
1474 /* Compare match Callback */
1475 HAL_LPTIM_CompareMatchCallback(hlptim);
1476 }
1477 }
1478
1479 /* Autoreload match interrupt */
1480 if(__HAL_LPTIM_GET_FLAG(hlptim, LPTIM_FLAG_ARRM) != RESET)
1481 {
1482 if(__HAL_LPTIM_GET_IT_SOURCE(hlptim, LPTIM_IT_ARRM) !=RESET)
1483 {
1484 /* Clear Autoreload match flag */
1485 __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_ARRM);
1486 /* Autoreload match Callback */
1487 HAL_LPTIM_AutoReloadMatchCallback(hlptim);
1488 }
1489 }
1490
1491 /* Trigger detected interrupt */
1492 if(__HAL_LPTIM_GET_FLAG(hlptim, LPTIM_FLAG_EXTTRIG) != RESET)
1493 {
1494 if(__HAL_LPTIM_GET_IT_SOURCE(hlptim, LPTIM_IT_EXTTRIG) !=RESET)
1495 {
1496 /* Clear Trigger detected flag */
1497 __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_EXTTRIG);
1498 /* Trigger detected callback */
1499 HAL_LPTIM_TriggerCallback(hlptim);
1500 }
1501 }
1502
1503 /* Compare write interrupt */
1504 if(__HAL_LPTIM_GET_FLAG(hlptim, LPTIM_FLAG_CMPOK) != RESET)
1505 {
1506 if(__HAL_LPTIM_GET_IT_SOURCE(hlptim, LPTIM_FLAG_CMPM) !=RESET)
1507 {
1508 /* Clear Compare write flag */
1509 __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_CMPOK);
1510 /* Compare write Callback */
1511 HAL_LPTIM_CompareWriteCallback(hlptim);
1512 }
1513 }
1514
1515 /* Autoreload write interrupt */
1516 if(__HAL_LPTIM_GET_FLAG(hlptim, LPTIM_FLAG_ARROK) != RESET)
1517 {
1518 if(__HAL_LPTIM_GET_IT_SOURCE(hlptim, LPTIM_IT_ARROK) !=RESET)
1519 {
1520 /* Clear Autoreload write flag */
1521 __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_ARROK);
1522 /* Autoreload write Callback */
1523 HAL_LPTIM_AutoReloadWriteCallback(hlptim);
1524 }
1525 }
1526
1527 /* Direction counter changed from Down to Up interrupt */
1528 if(__HAL_LPTIM_GET_FLAG(hlptim, LPTIM_FLAG_UP) != RESET)
1529 {
1530 if(__HAL_LPTIM_GET_IT_SOURCE(hlptim, LPTIM_IT_UP) !=RESET)
1531 {
1532 /* Clear Direction counter changed from Down to Up flag */
1533 __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_UP);
1534 /* Direction counter changed from Down to Up Callback */
1535 HAL_LPTIM_DirectionUpCallback(hlptim);
1536 }
1537 }
1538
1539 /* Direction counter changed from Up to Down interrupt */
1540 if(__HAL_LPTIM_GET_FLAG(hlptim, LPTIM_FLAG_DOWN) != RESET)
1541 {
1542 if(__HAL_LPTIM_GET_IT_SOURCE(hlptim, LPTIM_IT_DOWN) !=RESET)
1543 {
1544 /* Clear Direction counter changed from Up to Down flag */
1545 __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_DOWN);
1546 /* Direction counter changed from Up to Down Callback */
1547 HAL_LPTIM_DirectionDownCallback(hlptim);
1548 }
1549 }
1550
1551 __HAL_LPTIM_WAKEUPTIMER_EXTI_CLEAR_FLAG();
1552 }
1553
1554 /**
1555 * @brief Compare match callback in non blocking mode
1556 * @param hlptim : LPTIM handle
1557 * @retval None
1558 */
1559 __weak void HAL_LPTIM_CompareMatchCallback(LPTIM_HandleTypeDef *hlptim)
1560 {
1561 /* Prevent unused argument(s) compilation warning */
1562 UNUSED(hlptim);
1563
1564 /* NOTE : This function Should not be modified, when the callback is needed,
1565 the HAL_LPTIM_CompareMatchCallback could be implemented in the user file
1566 */
1567 }
1568
1569 /**
1570 * @brief Autoreload match callback in non blocking mode
1571 * @param hlptim : LPTIM handle
1572 * @retval None
1573 */
1574 __weak void HAL_LPTIM_AutoReloadMatchCallback(LPTIM_HandleTypeDef *hlptim)
1575 {
1576 /* Prevent unused argument(s) compilation warning */
1577 UNUSED(hlptim);
1578
1579 /* NOTE : This function Should not be modified, when the callback is needed,
1580 the HAL_LPTIM_AutoReloadMatchCallback could be implemented in the user file
1581 */
1582 }
1583
1584 /**
1585 * @brief Trigger detected callback in non blocking mode
1586 * @param hlptim : LPTIM handle
1587 * @retval None
1588 */
1589 __weak void HAL_LPTIM_TriggerCallback(LPTIM_HandleTypeDef *hlptim)
1590 {
1591 /* Prevent unused argument(s) compilation warning */
1592 UNUSED(hlptim);
1593
1594 /* NOTE : This function Should not be modified, when the callback is needed,
1595 the HAL_LPTIM_TriggerCallback could be implemented in the user file
1596 */
1597 }
1598
1599 /**
1600 * @brief Compare write callback in non blocking mode
1601 * @param hlptim : LPTIM handle
1602 * @retval None
1603 */
1604 __weak void HAL_LPTIM_CompareWriteCallback(LPTIM_HandleTypeDef *hlptim)
1605 {
1606 /* Prevent unused argument(s) compilation warning */
1607 UNUSED(hlptim);
1608
1609 /* NOTE : This function Should not be modified, when the callback is needed,
1610 the HAL_LPTIM_CompareWriteCallback could be implemented in the user file
1611 */
1612 }
1613
1614 /**
1615 * @brief Autoreload write callback in non blocking mode
1616 * @param hlptim : LPTIM handle
1617 * @retval None
1618 */
1619 __weak void HAL_LPTIM_AutoReloadWriteCallback(LPTIM_HandleTypeDef *hlptim)
1620 {
1621 /* Prevent unused argument(s) compilation warning */
1622 UNUSED(hlptim);
1623
1624 /* NOTE : This function Should not be modified, when the callback is needed,
1625 the HAL_LPTIM_AutoReloadWriteCallback could be implemented in the user file
1626 */
1627 }
1628
1629 /**
1630 * @brief Direction counter changed from Down to Up callback in non blocking mode
1631 * @param hlptim : LPTIM handle
1632 * @retval None
1633 */
1634 __weak void HAL_LPTIM_DirectionUpCallback(LPTIM_HandleTypeDef *hlptim)
1635 {
1636 /* Prevent unused argument(s) compilation warning */
1637 UNUSED(hlptim);
1638
1639 /* NOTE : This function Should not be modified, when the callback is needed,
1640 the HAL_LPTIM_DirectionUpCallback could be implemented in the user file
1641 */
1642 }
1643
1644 /**
1645 * @brief Direction counter changed from Up to Down callback in non blocking mode
1646 * @param hlptim : LPTIM handle
1647 * @retval None
1648 */
1649 __weak void HAL_LPTIM_DirectionDownCallback(LPTIM_HandleTypeDef *hlptim)
1650 {
1651 /* Prevent unused argument(s) compilation warning */
1652 UNUSED(hlptim);
1653
1654 /* NOTE : This function Should not be modified, when the callback is needed,
1655 the HAL_LPTIM_DirectionDownCallback could be implemented in the user file
1656 */
1657 }
1658
1659 /**
1660 * @}
1661 */
1662
1663 /** @defgroup LPTIM_Group5 Peripheral State functions
1664 * @brief Peripheral State functions.
1665 *
1666 @verbatim
1667 ==============================================================================
1668 ##### Peripheral State functions #####
1669 ==============================================================================
1670 [..]
1671 This subsection permits to get in run-time the status of the peripheral.
1672
1673 @endverbatim
1674 * @{
1675 */
1676
1677 /**
1678 * @brief Returns the LPTIM state.
1679 * @param hlptim: LPTIM handle
1680 * @retval HAL state
1681 */
1682 HAL_LPTIM_StateTypeDef HAL_LPTIM_GetState(LPTIM_HandleTypeDef *hlptim)
1683 {
1684 return hlptim->State;
1685 }
1686
1687 /**
1688 * @}
1689 */
1690
1691
1692 /**
1693 * @}
1694 */
1695
1696 #endif /* HAL_LPTIM_MODULE_ENABLED */
1697 /**
1698 * @}
1699 */
1700
1701 /**
1702 * @}
1703 */
1704
1705 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/