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