Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:08

0001 /**
0002   ******************************************************************************
0003   * @file    stm32h7xx_hal_lptim.c
0004   * @author  MCD Application Team
0005   * @brief   LPTIM HAL module driver.
0006   *          This file provides firmware functions to manage the following
0007   *          functionalities of the Low Power Timer (LPTIM) peripheral:
0008   *           + Initialization and de-initialization functions.
0009   *           + Start/Stop operation functions in polling mode.
0010   *           + Start/Stop operation functions in interrupt mode.
0011   *           + Reading operation functions.
0012   *           + Peripheral State functions.
0013   *
0014   ******************************************************************************
0015   * @attention
0016   *
0017   * Copyright (c) 2017 STMicroelectronics.
0018   * All rights reserved.
0019   *
0020   * This software is licensed under terms that can be found in the LICENSE file
0021   * in the root directory of this software component.
0022   * If no LICENSE file comes with this software, it is provided AS-IS.
0023   *
0024   ******************************************************************************
0025   @verbatim
0026   ==============================================================================
0027                      ##### How to use this driver #####
0028   ==============================================================================
0029     [..]
0030       The LPTIM HAL driver can be used as follows:
0031 
0032       (#)Initialize the LPTIM low level resources by implementing the
0033         HAL_LPTIM_MspInit():
0034          (++) Enable the LPTIM interface clock using __HAL_RCC_LPTIMx_CLK_ENABLE().
0035          (++) In case of using interrupts (e.g. HAL_LPTIM_PWM_Start_IT()):
0036              (+++) Configure the LPTIM interrupt priority using HAL_NVIC_SetPriority().
0037              (+++) Enable the LPTIM IRQ handler using HAL_NVIC_EnableIRQ().
0038              (+++) In LPTIM IRQ handler, call HAL_LPTIM_IRQHandler().
0039 
0040       (#)Initialize the LPTIM HAL using HAL_LPTIM_Init(). This function
0041          configures mainly:
0042          (++) The instance: LPTIM1 or LPTIM2.
0043          (++) Clock: the counter clock.
0044              (+++) Source   : it can be either the ULPTIM input (IN1) or one of
0045                               the internal clock; (APB, LSE, LSI or MSI).
0046              (+++) Prescaler: select the clock divider.
0047          (++)  UltraLowPowerClock : To be used only if the ULPTIM is selected
0048                as counter clock source.
0049              (+++) Polarity:   polarity of the active edge for the counter unit
0050                                if the ULPTIM input is selected.
0051              (+++) SampleTime: clock sampling time to configure the clock glitch
0052                                filter.
0053          (++) Trigger: How the counter start.
0054              (+++) Source: trigger can be software or one of the hardware triggers.
0055              (+++) ActiveEdge : only for hardware trigger.
0056              (+++) SampleTime : trigger sampling time to configure the trigger
0057                                 glitch filter.
0058          (++) OutputPolarity : 2 opposite polarities are possible.
0059          (++) UpdateMode: specifies whether the update of the autoreload and
0060               the compare values is done immediately or after the end of current
0061               period.
0062          (++) Input1Source: Source selected for input1 (GPIO or comparator output).
0063          (++) Input2Source: Source selected for input2 (GPIO or comparator output).
0064               Input2 is used only for encoder feature so is used only for LPTIM1 instance.
0065 
0066       (#)Six modes are available:
0067 
0068          (++) PWM Mode: To generate a PWM signal with specified period and pulse,
0069          call HAL_LPTIM_PWM_Start() or HAL_LPTIM_PWM_Start_IT() for interruption
0070          mode.
0071 
0072          (++) One Pulse Mode: To generate pulse with specified width in response
0073          to a stimulus, call HAL_LPTIM_OnePulse_Start() or
0074          HAL_LPTIM_OnePulse_Start_IT() for interruption mode.
0075 
0076          (++) Set once Mode: In this mode, the output changes the level (from
0077          low level to high level if the output polarity is configured high, else
0078          the opposite) when a compare match occurs. To start this mode, call
0079          HAL_LPTIM_SetOnce_Start() or HAL_LPTIM_SetOnce_Start_IT() for
0080          interruption mode.
0081 
0082          (++) Encoder Mode: To use the encoder interface call
0083          HAL_LPTIM_Encoder_Start() or HAL_LPTIM_Encoder_Start_IT() for
0084          interruption mode. Only available for LPTIM1 instance.
0085 
0086          (++) Time out Mode: an active edge on one selected trigger input rests
0087          the counter. The first trigger event will start the timer, any
0088          successive trigger event will reset the counter and the timer will
0089          restart. To start this mode call HAL_LPTIM_TimeOut_Start_IT() or
0090          HAL_LPTIM_TimeOut_Start_IT() for interruption mode.
0091 
0092          (++) Counter Mode: counter can be used to count external events on
0093          the LPTIM Input1 or it can be used to count internal clock cycles.
0094          To start this mode, call HAL_LPTIM_Counter_Start() or
0095          HAL_LPTIM_Counter_Start_IT() for interruption mode.
0096 
0097 
0098       (#) User can stop any process by calling the corresponding API:
0099           HAL_LPTIM_Xxx_Stop() or HAL_LPTIM_Xxx_Stop_IT() if the process is
0100           already started in interruption mode.
0101 
0102       (#) De-initialize the LPTIM peripheral using HAL_LPTIM_DeInit().
0103 
0104     *** Callback registration ***
0105   =============================================
0106   [..]
0107   The compilation define  USE_HAL_LPTIM_REGISTER_CALLBACKS when set to 1
0108   allows the user to configure dynamically the driver callbacks.
0109   [..]
0110   Use Function HAL_LPTIM_RegisterCallback() to register a callback.
0111   HAL_LPTIM_RegisterCallback() takes as parameters the HAL peripheral handle,
0112   the Callback ID and a pointer to the user callback function.
0113   [..]
0114   Use function HAL_LPTIM_UnRegisterCallback() to reset a callback to the
0115   default weak function.
0116   HAL_LPTIM_UnRegisterCallback takes as parameters the HAL peripheral handle,
0117   and the Callback ID.
0118   [..]
0119   These functions allow to register/unregister following callbacks:
0120 
0121     (+) MspInitCallback         : LPTIM Base Msp Init Callback.
0122     (+) MspDeInitCallback       : LPTIM Base Msp DeInit Callback.
0123     (+) CompareMatchCallback    : Compare match Callback.
0124     (+) AutoReloadMatchCallback : Auto-reload match Callback.
0125     (+) TriggerCallback         : External trigger event detection Callback.
0126     (+) CompareWriteCallback    : Compare register write complete Callback.
0127     (+) AutoReloadWriteCallback : Auto-reload register write complete Callback.
0128     (+) DirectionUpCallback     : Up-counting direction change Callback.
0129     (+) DirectionDownCallback   : Down-counting direction change Callback.
0130 
0131   [..]
0132   By default, after the Init and when the state is HAL_LPTIM_STATE_RESET
0133   all interrupt callbacks are set to the corresponding weak functions:
0134   examples HAL_LPTIM_TriggerCallback(), HAL_LPTIM_CompareMatchCallback().
0135 
0136   [..]
0137   Exception done for MspInit and MspDeInit functions that are reset to the legacy weak
0138   functionalities in the Init/DeInit only when these callbacks are null
0139   (not registered beforehand). If not, MspInit or MspDeInit are not null, the Init/DeInit
0140   keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
0141 
0142   [..]
0143   Callbacks can be registered/unregistered in HAL_LPTIM_STATE_READY state only.
0144   Exception done MspInit/MspDeInit that can be registered/unregistered
0145   in HAL_LPTIM_STATE_READY or HAL_LPTIM_STATE_RESET state,
0146   thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
0147   In that case first register the MspInit/MspDeInit user callbacks
0148   using HAL_LPTIM_RegisterCallback() before calling DeInit or Init function.
0149 
0150   [..]
0151   When The compilation define USE_HAL_LPTIM_REGISTER_CALLBACKS is set to 0 or
0152   not defined, the callback registration feature is not available and all callbacks
0153   are set to the corresponding weak functions.
0154 
0155   @endverbatim
0156   ******************************************************************************
0157   */
0158 
0159 /* Includes ------------------------------------------------------------------*/
0160 #include "stm32h7xx_hal.h"
0161 
0162 /** @addtogroup STM32H7xx_HAL_Driver
0163   * @{
0164   */
0165 
0166 /** @defgroup LPTIM LPTIM
0167   * @ingroup RTEMSBSPsARMSTM32H7
0168   * @brief LPTIM HAL module driver.
0169   * @{
0170   */
0171 
0172 #ifdef HAL_LPTIM_MODULE_ENABLED
0173 
0174 #if defined (LPTIM1) || defined (LPTIM2) || defined (LPTIM3) || defined (LPTIM4) || defined (LPTIM5)
0175 
0176 /* Private typedef -----------------------------------------------------------*/
0177 /* Private define ------------------------------------------------------------*/
0178 /** @addtogroup LPTIM_Private_Constants
0179   * @{
0180   */
0181 #define TIMEOUT                                     1000UL /* Timeout is 1s */
0182 /**
0183   * @}
0184   */
0185 
0186 /* Private macro -------------------------------------------------------------*/
0187 /* Private variables ---------------------------------------------------------*/
0188 /* Private function prototypes -----------------------------------------------*/
0189 #if (USE_HAL_LPTIM_REGISTER_CALLBACKS == 1)
0190 static void LPTIM_ResetCallback(LPTIM_HandleTypeDef *lptim);
0191 #endif /* USE_HAL_LPTIM_REGISTER_CALLBACKS */
0192 static HAL_StatusTypeDef LPTIM_WaitForFlag(const LPTIM_HandleTypeDef *hlptim, uint32_t flag);
0193 
0194 /* Exported functions --------------------------------------------------------*/
0195 
0196 /** @defgroup LPTIM_Exported_Functions LPTIM Exported Functions
0197   * @ingroup RTEMSBSPsARMSTM32H7
0198   * @{
0199   */
0200 
0201 /** @defgroup LPTIM_Exported_Functions_Group1 Initialization/de-initialization functions
0202   * @ingroup RTEMSBSPsARMSTM32H7
0203   *  @brief    Initialization and Configuration functions.
0204   *
0205 @verbatim
0206   ==============================================================================
0207               ##### Initialization and de-initialization functions #####
0208   ==============================================================================
0209     [..]  This section provides functions allowing to:
0210       (+) Initialize the LPTIM according to the specified parameters in the
0211           LPTIM_InitTypeDef and initialize the associated handle.
0212       (+) DeInitialize the LPTIM peripheral.
0213       (+) Initialize the LPTIM MSP.
0214       (+) DeInitialize the LPTIM MSP.
0215 
0216 @endverbatim
0217   * @{
0218   */
0219 
0220 /**
0221   * @brief  Initialize the LPTIM according to the specified parameters in the
0222   *         LPTIM_InitTypeDef and initialize the associated handle.
0223   * @param  hlptim LPTIM handle
0224   * @retval HAL status
0225   */
0226 HAL_StatusTypeDef HAL_LPTIM_Init(LPTIM_HandleTypeDef *hlptim)
0227 {
0228   uint32_t tmpcfgr;
0229 
0230   /* Check the LPTIM handle allocation */
0231   if (hlptim == NULL)
0232   {
0233     return HAL_ERROR;
0234   }
0235 
0236   /* Check the parameters */
0237   assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
0238 
0239   assert_param(IS_LPTIM_CLOCK_SOURCE(hlptim->Init.Clock.Source));
0240   assert_param(IS_LPTIM_CLOCK_PRESCALER(hlptim->Init.Clock.Prescaler));
0241   if ((hlptim->Init.Clock.Source == LPTIM_CLOCKSOURCE_ULPTIM)
0242       || (hlptim->Init.CounterSource == LPTIM_COUNTERSOURCE_EXTERNAL))
0243   {
0244     assert_param(IS_LPTIM_CLOCK_POLARITY(hlptim->Init.UltraLowPowerClock.Polarity));
0245     assert_param(IS_LPTIM_CLOCK_SAMPLE_TIME(hlptim->Init.UltraLowPowerClock.SampleTime));
0246   }
0247   assert_param(IS_LPTIM_TRG_SOURCE(hlptim->Init.Trigger.Source));
0248   if (hlptim->Init.Trigger.Source != LPTIM_TRIGSOURCE_SOFTWARE)
0249   {
0250     assert_param(IS_LPTIM_EXT_TRG_POLARITY(hlptim->Init.Trigger.ActiveEdge));
0251     assert_param(IS_LPTIM_TRIG_SAMPLE_TIME(hlptim->Init.Trigger.SampleTime));
0252   }
0253   assert_param(IS_LPTIM_OUTPUT_POLARITY(hlptim->Init.OutputPolarity));
0254   assert_param(IS_LPTIM_UPDATE_MODE(hlptim->Init.UpdateMode));
0255   assert_param(IS_LPTIM_COUNTER_SOURCE(hlptim->Init.CounterSource));
0256 
0257   if (hlptim->State == HAL_LPTIM_STATE_RESET)
0258   {
0259     /* Allocate lock resource and initialize it */
0260     hlptim->Lock = HAL_UNLOCKED;
0261 
0262 #if (USE_HAL_LPTIM_REGISTER_CALLBACKS == 1)
0263     /* Reset interrupt callbacks to legacy weak callbacks */
0264     LPTIM_ResetCallback(hlptim);
0265 
0266     if (hlptim->MspInitCallback == NULL)
0267     {
0268       hlptim->MspInitCallback = HAL_LPTIM_MspInit;
0269     }
0270 
0271     /* Init the low level hardware : GPIO, CLOCK, NVIC */
0272     hlptim->MspInitCallback(hlptim);
0273 #else
0274     /* Init the low level hardware : GPIO, CLOCK, NVIC */
0275     HAL_LPTIM_MspInit(hlptim);
0276 #endif /* USE_HAL_LPTIM_REGISTER_CALLBACKS */
0277   }
0278 
0279   /* Change the LPTIM state */
0280   hlptim->State = HAL_LPTIM_STATE_BUSY;
0281 
0282   /* Get the LPTIMx CFGR value */
0283   tmpcfgr = hlptim->Instance->CFGR;
0284 
0285   if ((hlptim->Init.Clock.Source == LPTIM_CLOCKSOURCE_ULPTIM)
0286       || (hlptim->Init.CounterSource == LPTIM_COUNTERSOURCE_EXTERNAL))
0287   {
0288     tmpcfgr &= (uint32_t)(~(LPTIM_CFGR_CKPOL | LPTIM_CFGR_CKFLT));
0289   }
0290   if (hlptim->Init.Trigger.Source != LPTIM_TRIGSOURCE_SOFTWARE)
0291   {
0292     tmpcfgr &= (uint32_t)(~(LPTIM_CFGR_TRGFLT | LPTIM_CFGR_TRIGSEL));
0293   }
0294 
0295   /* Clear CKSEL, PRESC, TRIGEN, TRGFLT, WAVPOL, PRELOAD & COUNTMODE bits */
0296   tmpcfgr &= (uint32_t)(~(LPTIM_CFGR_CKSEL | LPTIM_CFGR_TRIGEN | LPTIM_CFGR_PRELOAD |
0297                           LPTIM_CFGR_WAVPOL | LPTIM_CFGR_PRESC | LPTIM_CFGR_COUNTMODE));
0298 
0299   /* Set initialization parameters */
0300   tmpcfgr |= (hlptim->Init.Clock.Source    |
0301               hlptim->Init.Clock.Prescaler |
0302               hlptim->Init.OutputPolarity  |
0303               hlptim->Init.UpdateMode      |
0304               hlptim->Init.CounterSource);
0305 
0306   /* Glitch filters for internal triggers and  external inputs are configured
0307    * only if an internal clock source is provided to the LPTIM
0308    */
0309   if (hlptim->Init.Clock.Source == LPTIM_CLOCKSOURCE_APBCLOCK_LPOSC)
0310   {
0311     tmpcfgr |= (hlptim->Init.Trigger.SampleTime |
0312                 hlptim->Init.UltraLowPowerClock.SampleTime);
0313   }
0314 
0315   /* Configure LPTIM external clock polarity and digital filter */
0316   if ((hlptim->Init.Clock.Source == LPTIM_CLOCKSOURCE_ULPTIM)
0317       || (hlptim->Init.CounterSource == LPTIM_COUNTERSOURCE_EXTERNAL))
0318   {
0319     tmpcfgr |= (hlptim->Init.UltraLowPowerClock.Polarity |
0320                 hlptim->Init.UltraLowPowerClock.SampleTime);
0321   }
0322 
0323   /* Configure LPTIM external trigger */
0324   if (hlptim->Init.Trigger.Source != LPTIM_TRIGSOURCE_SOFTWARE)
0325   {
0326     /* Enable External trigger and set the trigger source */
0327     tmpcfgr |= (hlptim->Init.Trigger.Source     |
0328                 hlptim->Init.Trigger.ActiveEdge |
0329                 hlptim->Init.Trigger.SampleTime);
0330   }
0331 
0332   /* Write to LPTIMx CFGR */
0333   hlptim->Instance->CFGR = tmpcfgr;
0334 
0335   /* Configure LPTIM input sources */
0336   if ((hlptim->Instance == LPTIM1) || (hlptim->Instance == LPTIM2))
0337   {
0338     /* Check LPTIM Input1 and Input2 sources */
0339     assert_param(IS_LPTIM_INPUT1_SOURCE(hlptim->Instance, hlptim->Init.Input1Source));
0340     assert_param(IS_LPTIM_INPUT2_SOURCE(hlptim->Instance, hlptim->Init.Input2Source));
0341 
0342     /* Configure LPTIM Input1 and Input2 sources */
0343     hlptim->Instance->CFGR2 = (hlptim->Init.Input1Source | hlptim->Init.Input2Source);
0344   }
0345   else
0346   {
0347     if (hlptim->Instance == LPTIM3)
0348     {
0349       /* Check LPTIM Input1 source */
0350       assert_param(IS_LPTIM_INPUT1_SOURCE(hlptim->Instance, hlptim->Init.Input1Source));
0351 
0352       /* Configure LPTIM Input1 source */
0353       hlptim->Instance->CFGR2 = hlptim->Init.Input1Source;
0354     }
0355   }
0356 
0357   /* Change the LPTIM state */
0358   hlptim->State = HAL_LPTIM_STATE_READY;
0359 
0360   /* Return function status */
0361   return HAL_OK;
0362 }
0363 
0364 /**
0365   * @brief  DeInitialize the LPTIM peripheral.
0366   * @param  hlptim LPTIM handle
0367   * @retval HAL status
0368   */
0369 HAL_StatusTypeDef HAL_LPTIM_DeInit(LPTIM_HandleTypeDef *hlptim)
0370 {
0371   /* Check the LPTIM handle allocation */
0372   if (hlptim == NULL)
0373   {
0374     return HAL_ERROR;
0375   }
0376 
0377   /* Change the LPTIM state */
0378   hlptim->State = HAL_LPTIM_STATE_BUSY;
0379 
0380   /* Disable the LPTIM Peripheral Clock */
0381   __HAL_LPTIM_DISABLE(hlptim);
0382 
0383   if (HAL_LPTIM_GetState(hlptim) == HAL_LPTIM_STATE_TIMEOUT)
0384   {
0385     return HAL_TIMEOUT;
0386   }
0387 
0388 #if (USE_HAL_LPTIM_REGISTER_CALLBACKS == 1)
0389   if (hlptim->MspDeInitCallback == NULL)
0390   {
0391     hlptim->MspDeInitCallback = HAL_LPTIM_MspDeInit;
0392   }
0393 
0394   /* DeInit the low level hardware: CLOCK, NVIC.*/
0395   hlptim->MspDeInitCallback(hlptim);
0396 #else
0397   /* DeInit the low level hardware: CLOCK, NVIC.*/
0398   HAL_LPTIM_MspDeInit(hlptim);
0399 #endif /* USE_HAL_LPTIM_REGISTER_CALLBACKS */
0400 
0401   /* Change the LPTIM state */
0402   hlptim->State = HAL_LPTIM_STATE_RESET;
0403 
0404   /* Release Lock */
0405   __HAL_UNLOCK(hlptim);
0406 
0407   /* Return function status */
0408   return HAL_OK;
0409 }
0410 
0411 /**
0412   * @brief  Initialize the LPTIM MSP.
0413   * @param  hlptim LPTIM handle
0414   * @retval None
0415   */
0416 __weak void HAL_LPTIM_MspInit(LPTIM_HandleTypeDef *hlptim)
0417 {
0418   /* Prevent unused argument(s) compilation warning */
0419   UNUSED(hlptim);
0420 
0421   /* NOTE : This function should not be modified, when the callback is needed,
0422             the HAL_LPTIM_MspInit could be implemented in the user file
0423    */
0424 }
0425 
0426 /**
0427   * @brief  DeInitialize LPTIM MSP.
0428   * @param  hlptim LPTIM handle
0429   * @retval None
0430   */
0431 __weak void HAL_LPTIM_MspDeInit(LPTIM_HandleTypeDef *hlptim)
0432 {
0433   /* Prevent unused argument(s) compilation warning */
0434   UNUSED(hlptim);
0435 
0436   /* NOTE : This function should not be modified, when the callback is needed,
0437             the HAL_LPTIM_MspDeInit could be implemented in the user file
0438    */
0439 }
0440 
0441 /**
0442   * @}
0443   */
0444 
0445 /** @defgroup LPTIM_Exported_Functions_Group2 LPTIM Start-Stop operation functions
0446   * @ingroup RTEMSBSPsARMSTM32H7
0447   *  @brief   Start-Stop operation functions.
0448   *
0449 @verbatim
0450   ==============================================================================
0451                 ##### LPTIM Start Stop operation functions #####
0452   ==============================================================================
0453     [..]  This section provides functions allowing to:
0454       (+) Start the PWM mode.
0455       (+) Stop the PWM mode.
0456       (+) Start the One pulse mode.
0457       (+) Stop the One pulse mode.
0458       (+) Start the Set once mode.
0459       (+) Stop the Set once mode.
0460       (+) Start the Encoder mode.
0461       (+) Stop the Encoder mode.
0462       (+) Start the Timeout mode.
0463       (+) Stop the Timeout mode.
0464       (+) Start the Counter mode.
0465       (+) Stop the Counter mode.
0466 
0467 
0468 @endverbatim
0469   * @{
0470   */
0471 
0472 /**
0473   * @brief  Start the LPTIM PWM generation.
0474   * @param  hlptim LPTIM handle
0475   * @param  Period Specifies the Autoreload value.
0476   *         This parameter must be a value between 0x0001 and 0xFFFF.
0477   * @param  Pulse Specifies the compare value.
0478   *         This parameter must be a value between 0x0000 and 0xFFFF.
0479   * @retval HAL status
0480   */
0481 HAL_StatusTypeDef HAL_LPTIM_PWM_Start(LPTIM_HandleTypeDef *hlptim, uint32_t Period, uint32_t Pulse)
0482 {
0483   /* Check the parameters */
0484   assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
0485   assert_param(IS_LPTIM_PERIOD(Period));
0486   assert_param(IS_LPTIM_PULSE(Pulse));
0487 
0488   /* Set the LPTIM state */
0489   hlptim->State = HAL_LPTIM_STATE_BUSY;
0490 
0491   /* Reset WAVE bit to set PWM mode */
0492   hlptim->Instance->CFGR &= ~LPTIM_CFGR_WAVE;
0493 
0494   /* Enable the Peripheral */
0495   __HAL_LPTIM_ENABLE(hlptim);
0496 
0497   /* Clear flag */
0498   __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_ARROK);
0499 
0500   /* Load the period value in the autoreload register */
0501   __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
0502 
0503   /* Wait for the completion of the write operation to the LPTIM_ARR register */
0504   if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_ARROK) == HAL_TIMEOUT)
0505   {
0506     return HAL_TIMEOUT;
0507   }
0508 
0509   /* Clear flag */
0510   __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_CMPOK);
0511 
0512   /* Load the pulse value in the compare register */
0513   __HAL_LPTIM_COMPARE_SET(hlptim, Pulse);
0514 
0515   /* Wait for the completion of the write operation to the LPTIM_CMP register */
0516   if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_CMPOK) == HAL_TIMEOUT)
0517   {
0518     return HAL_TIMEOUT;
0519   }
0520 
0521   /* Start timer in continuous mode */
0522   __HAL_LPTIM_START_CONTINUOUS(hlptim);
0523 
0524   /* Change the LPTIM state */
0525   hlptim->State = HAL_LPTIM_STATE_READY;
0526 
0527   /* Return function status */
0528   return HAL_OK;
0529 }
0530 
0531 /**
0532   * @brief  Stop the LPTIM PWM generation.
0533   * @param  hlptim LPTIM handle
0534   * @retval HAL status
0535   */
0536 HAL_StatusTypeDef HAL_LPTIM_PWM_Stop(LPTIM_HandleTypeDef *hlptim)
0537 {
0538   /* Check the parameters */
0539   assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
0540 
0541   /* Change the LPTIM state */
0542   hlptim->State = HAL_LPTIM_STATE_BUSY;
0543 
0544   /* Disable the Peripheral */
0545   __HAL_LPTIM_DISABLE(hlptim);
0546 
0547   if (HAL_LPTIM_GetState(hlptim) == HAL_LPTIM_STATE_TIMEOUT)
0548   {
0549     return HAL_TIMEOUT;
0550   }
0551 
0552   /* Change the LPTIM state */
0553   hlptim->State = HAL_LPTIM_STATE_READY;
0554 
0555   /* Return function status */
0556   return HAL_OK;
0557 }
0558 
0559 /**
0560   * @brief  Start the LPTIM PWM generation in interrupt mode.
0561   * @param  hlptim LPTIM handle
0562   * @param  Period Specifies the Autoreload value.
0563   *         This parameter must be a value between 0x0001 and 0xFFFF
0564   * @param  Pulse Specifies the compare value.
0565   *         This parameter must be a value between 0x0000 and 0xFFFF
0566   * @retval HAL status
0567   */
0568 HAL_StatusTypeDef HAL_LPTIM_PWM_Start_IT(LPTIM_HandleTypeDef *hlptim, uint32_t Period, uint32_t Pulse)
0569 {
0570   /* Check the parameters */
0571   assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
0572   assert_param(IS_LPTIM_PERIOD(Period));
0573   assert_param(IS_LPTIM_PULSE(Pulse));
0574 
0575   /* Set the LPTIM state */
0576   hlptim->State = HAL_LPTIM_STATE_BUSY;
0577 
0578   /* Reset WAVE bit to set PWM mode */
0579   hlptim->Instance->CFGR &= ~LPTIM_CFGR_WAVE;
0580 
0581   /* Enable the Peripheral */
0582   __HAL_LPTIM_ENABLE(hlptim);
0583 
0584   /* Clear flag */
0585   __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_ARROK);
0586 
0587   /* Load the period value in the autoreload register */
0588   __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
0589 
0590   /* Wait for the completion of the write operation to the LPTIM_ARR register */
0591   if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_ARROK) == HAL_TIMEOUT)
0592   {
0593     return HAL_TIMEOUT;
0594   }
0595 
0596   /* Clear flag */
0597   __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_CMPOK);
0598 
0599   /* Load the pulse value in the compare register */
0600   __HAL_LPTIM_COMPARE_SET(hlptim, Pulse);
0601 
0602   /* Wait for the completion of the write operation to the LPTIM_CMP register */
0603   if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_CMPOK) == HAL_TIMEOUT)
0604   {
0605     return HAL_TIMEOUT;
0606   }
0607 
0608   /* Disable the Peripheral */
0609   __HAL_LPTIM_DISABLE(hlptim);
0610 
0611   if (HAL_LPTIM_GetState(hlptim) == HAL_LPTIM_STATE_TIMEOUT)
0612   {
0613     return HAL_TIMEOUT;
0614   }
0615 
0616   /* Enable Autoreload write complete interrupt */
0617   __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARROK);
0618 
0619   /* Enable Compare write complete interrupt */
0620   __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_CMPOK);
0621 
0622   /* Enable Autoreload match interrupt */
0623   __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARRM);
0624 
0625   /* Enable Compare match interrupt */
0626   __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_CMPM);
0627 
0628   /* If external trigger source is used, then enable external trigger interrupt */
0629   if ((hlptim->Init.Trigger.Source) != LPTIM_TRIGSOURCE_SOFTWARE)
0630   {
0631     /* Enable external trigger interrupt */
0632     __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_EXTTRIG);
0633   }
0634 
0635   /* Enable the Peripheral */
0636   __HAL_LPTIM_ENABLE(hlptim);
0637 
0638   /* Start timer in continuous mode */
0639   __HAL_LPTIM_START_CONTINUOUS(hlptim);
0640 
0641   /* Change the LPTIM state */
0642   hlptim->State = HAL_LPTIM_STATE_READY;
0643 
0644   /* Return function status */
0645   return HAL_OK;
0646 }
0647 
0648 /**
0649   * @brief  Stop the LPTIM PWM generation in interrupt mode.
0650   * @param  hlptim LPTIM handle
0651   * @retval HAL status
0652   */
0653 HAL_StatusTypeDef HAL_LPTIM_PWM_Stop_IT(LPTIM_HandleTypeDef *hlptim)
0654 {
0655   /* Check the parameters */
0656   assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
0657 
0658   /* Change the LPTIM state */
0659   hlptim->State = HAL_LPTIM_STATE_BUSY;
0660 
0661   /* Disable the Peripheral */
0662   __HAL_LPTIM_DISABLE(hlptim);
0663 
0664   if (HAL_LPTIM_GetState(hlptim) == HAL_LPTIM_STATE_TIMEOUT)
0665   {
0666     return HAL_TIMEOUT;
0667   }
0668 
0669   /* Disable Autoreload write complete interrupt */
0670   __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_ARROK);
0671 
0672   /* Disable Compare write complete interrupt */
0673   __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_CMPOK);
0674 
0675   /* Disable Autoreload match interrupt */
0676   __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_ARRM);
0677 
0678   /* Disable Compare match interrupt */
0679   __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_CMPM);
0680 
0681   /* If external trigger source is used, then disable external trigger interrupt */
0682   if ((hlptim->Init.Trigger.Source) != LPTIM_TRIGSOURCE_SOFTWARE)
0683   {
0684     /* Disable external trigger interrupt */
0685     __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_EXTTRIG);
0686   }
0687 
0688   /* Change the LPTIM state */
0689   hlptim->State = HAL_LPTIM_STATE_READY;
0690 
0691   /* Return function status */
0692   return HAL_OK;
0693 }
0694 
0695 /**
0696   * @brief  Start the LPTIM One pulse generation.
0697   * @param  hlptim LPTIM handle
0698   * @param  Period Specifies the Autoreload value.
0699   *         This parameter must be a value between 0x0001 and 0xFFFF.
0700   * @param  Pulse Specifies the compare value.
0701   *         This parameter must be a value between 0x0000 and 0xFFFF.
0702   * @retval HAL status
0703   */
0704 HAL_StatusTypeDef HAL_LPTIM_OnePulse_Start(LPTIM_HandleTypeDef *hlptim, uint32_t Period, uint32_t Pulse)
0705 {
0706   /* Check the parameters */
0707   assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
0708   assert_param(IS_LPTIM_PERIOD(Period));
0709   assert_param(IS_LPTIM_PULSE(Pulse));
0710 
0711   /* Set the LPTIM state */
0712   hlptim->State = HAL_LPTIM_STATE_BUSY;
0713 
0714   /* Reset WAVE bit to set one pulse mode */
0715   hlptim->Instance->CFGR &= ~LPTIM_CFGR_WAVE;
0716 
0717   /* Enable the Peripheral */
0718   __HAL_LPTIM_ENABLE(hlptim);
0719 
0720   /* Clear flag */
0721   __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_ARROK);
0722 
0723   /* Load the period value in the autoreload register */
0724   __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
0725 
0726   /* Wait for the completion of the write operation to the LPTIM_ARR register */
0727   if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_ARROK) == HAL_TIMEOUT)
0728   {
0729     return HAL_TIMEOUT;
0730   }
0731 
0732   /* Clear flag */
0733   __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_CMPOK);
0734 
0735   /* Load the pulse value in the compare register */
0736   __HAL_LPTIM_COMPARE_SET(hlptim, Pulse);
0737 
0738   /* Wait for the completion of the write operation to the LPTIM_CMP register */
0739   if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_CMPOK) == HAL_TIMEOUT)
0740   {
0741     return HAL_TIMEOUT;
0742   }
0743 
0744   /* Start timer in single (one shot) mode */
0745   __HAL_LPTIM_START_SINGLE(hlptim);
0746 
0747   /* Change the LPTIM state */
0748   hlptim->State = HAL_LPTIM_STATE_READY;
0749 
0750   /* Return function status */
0751   return HAL_OK;
0752 }
0753 
0754 /**
0755   * @brief  Stop the LPTIM One pulse generation.
0756   * @param  hlptim LPTIM handle
0757   * @retval HAL status
0758   */
0759 HAL_StatusTypeDef HAL_LPTIM_OnePulse_Stop(LPTIM_HandleTypeDef *hlptim)
0760 {
0761   /* Check the parameters */
0762   assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
0763 
0764   /* Set the LPTIM state */
0765   hlptim->State = HAL_LPTIM_STATE_BUSY;
0766 
0767   /* Disable the Peripheral */
0768   __HAL_LPTIM_DISABLE(hlptim);
0769 
0770   if (HAL_LPTIM_GetState(hlptim) == HAL_LPTIM_STATE_TIMEOUT)
0771   {
0772     return HAL_TIMEOUT;
0773   }
0774 
0775   /* Change the LPTIM state */
0776   hlptim->State = HAL_LPTIM_STATE_READY;
0777 
0778   /* Return function status */
0779   return HAL_OK;
0780 }
0781 
0782 /**
0783   * @brief  Start the LPTIM One pulse generation in interrupt mode.
0784   * @param  hlptim LPTIM handle
0785   * @param  Period Specifies the Autoreload value.
0786   *         This parameter must be a value between 0x0001 and 0xFFFF.
0787   * @param  Pulse Specifies the compare value.
0788   *         This parameter must be a value between 0x0000 and 0xFFFF.
0789   * @retval HAL status
0790   */
0791 HAL_StatusTypeDef HAL_LPTIM_OnePulse_Start_IT(LPTIM_HandleTypeDef *hlptim, uint32_t Period, uint32_t Pulse)
0792 {
0793   /* Check the parameters */
0794   assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
0795   assert_param(IS_LPTIM_PERIOD(Period));
0796   assert_param(IS_LPTIM_PULSE(Pulse));
0797 
0798   /* Set the LPTIM state */
0799   hlptim->State = HAL_LPTIM_STATE_BUSY;
0800 
0801   /* Reset WAVE bit to set one pulse mode */
0802   hlptim->Instance->CFGR &= ~LPTIM_CFGR_WAVE;
0803 
0804   /* Enable the Peripheral */
0805   __HAL_LPTIM_ENABLE(hlptim);
0806 
0807   /* Clear flag */
0808   __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_ARROK);
0809 
0810   /* Load the period value in the autoreload register */
0811   __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
0812 
0813   /* Wait for the completion of the write operation to the LPTIM_ARR register */
0814   if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_ARROK) == HAL_TIMEOUT)
0815   {
0816     return HAL_TIMEOUT;
0817   }
0818 
0819   /* Clear flag */
0820   __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_CMPOK);
0821 
0822   /* Load the pulse value in the compare register */
0823   __HAL_LPTIM_COMPARE_SET(hlptim, Pulse);
0824 
0825   /* Wait for the completion of the write operation to the LPTIM_CMP register */
0826   if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_CMPOK) == HAL_TIMEOUT)
0827   {
0828     return HAL_TIMEOUT;
0829   }
0830 
0831   /* Disable the Peripheral */
0832   __HAL_LPTIM_DISABLE(hlptim);
0833 
0834   if (HAL_LPTIM_GetState(hlptim) == HAL_LPTIM_STATE_TIMEOUT)
0835   {
0836     return HAL_TIMEOUT;
0837   }
0838 
0839   /* Enable Autoreload write complete interrupt */
0840   __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARROK);
0841 
0842   /* Enable Compare write complete interrupt */
0843   __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_CMPOK);
0844 
0845   /* Enable Autoreload match interrupt */
0846   __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARRM);
0847 
0848   /* Enable Compare match interrupt */
0849   __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_CMPM);
0850 
0851   /* If external trigger source is used, then enable external trigger interrupt */
0852   if ((hlptim->Init.Trigger.Source) != LPTIM_TRIGSOURCE_SOFTWARE)
0853   {
0854     /* Enable external trigger interrupt */
0855     __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_EXTTRIG);
0856   }
0857 
0858   /* Enable the Peripheral */
0859   __HAL_LPTIM_ENABLE(hlptim);
0860 
0861   /* Start timer in single (one shot) mode */
0862   __HAL_LPTIM_START_SINGLE(hlptim);
0863 
0864   /* Change the LPTIM state */
0865   hlptim->State = HAL_LPTIM_STATE_READY;
0866 
0867   /* Return function status */
0868   return HAL_OK;
0869 }
0870 
0871 /**
0872   * @brief  Stop the LPTIM One pulse generation in interrupt mode.
0873   * @param  hlptim LPTIM handle
0874   * @retval HAL status
0875   */
0876 HAL_StatusTypeDef HAL_LPTIM_OnePulse_Stop_IT(LPTIM_HandleTypeDef *hlptim)
0877 {
0878   /* Check the parameters */
0879   assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
0880 
0881   /* Set the LPTIM state */
0882   hlptim->State = HAL_LPTIM_STATE_BUSY;
0883 
0884 
0885   /* Disable the Peripheral */
0886   __HAL_LPTIM_DISABLE(hlptim);
0887 
0888   if (HAL_LPTIM_GetState(hlptim) == HAL_LPTIM_STATE_TIMEOUT)
0889   {
0890     return HAL_TIMEOUT;
0891   }
0892 
0893   /* Disable Autoreload write complete interrupt */
0894   __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_ARROK);
0895 
0896   /* Disable Compare write complete interrupt */
0897   __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_CMPOK);
0898 
0899   /* Disable Autoreload match interrupt */
0900   __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_ARRM);
0901 
0902   /* Disable Compare match interrupt */
0903   __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_CMPM);
0904 
0905   /* If external trigger source is used, then disable external trigger interrupt */
0906   if ((hlptim->Init.Trigger.Source) != LPTIM_TRIGSOURCE_SOFTWARE)
0907   {
0908     /* Disable external trigger interrupt */
0909     __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_EXTTRIG);
0910   }
0911 
0912   /* Change the LPTIM state */
0913   hlptim->State = HAL_LPTIM_STATE_READY;
0914 
0915   /* Return function status */
0916   return HAL_OK;
0917 }
0918 
0919 /**
0920   * @brief  Start the LPTIM in Set once mode.
0921   * @param  hlptim LPTIM handle
0922   * @param  Period Specifies the Autoreload value.
0923   *         This parameter must be a value between 0x0001 and 0xFFFF.
0924   * @param  Pulse Specifies the compare value.
0925   *         This parameter must be a value between 0x0000 and 0xFFFF.
0926   * @retval HAL status
0927   */
0928 HAL_StatusTypeDef HAL_LPTIM_SetOnce_Start(LPTIM_HandleTypeDef *hlptim, uint32_t Period, uint32_t Pulse)
0929 {
0930   /* Check the parameters */
0931   assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
0932   assert_param(IS_LPTIM_PERIOD(Period));
0933   assert_param(IS_LPTIM_PULSE(Pulse));
0934 
0935   /* Set the LPTIM state */
0936   hlptim->State = HAL_LPTIM_STATE_BUSY;
0937 
0938   /* Set WAVE bit to enable the set once mode */
0939   hlptim->Instance->CFGR |= LPTIM_CFGR_WAVE;
0940 
0941   /* Enable the Peripheral */
0942   __HAL_LPTIM_ENABLE(hlptim);
0943 
0944   /* Clear flag */
0945   __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_ARROK);
0946 
0947   /* Load the period value in the autoreload register */
0948   __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
0949 
0950   /* Wait for the completion of the write operation to the LPTIM_ARR register */
0951   if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_ARROK) == HAL_TIMEOUT)
0952   {
0953     return HAL_TIMEOUT;
0954   }
0955 
0956   /* Clear flag */
0957   __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_CMPOK);
0958 
0959   /* Load the pulse value in the compare register */
0960   __HAL_LPTIM_COMPARE_SET(hlptim, Pulse);
0961 
0962   /* Wait for the completion of the write operation to the LPTIM_CMP register */
0963   if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_CMPOK) == HAL_TIMEOUT)
0964   {
0965     return HAL_TIMEOUT;
0966   }
0967 
0968   /* Start timer in single (one shot) mode */
0969   __HAL_LPTIM_START_SINGLE(hlptim);
0970 
0971   /* Change the LPTIM state */
0972   hlptim->State = HAL_LPTIM_STATE_READY;
0973 
0974   /* Return function status */
0975   return HAL_OK;
0976 }
0977 
0978 /**
0979   * @brief  Stop the LPTIM Set once mode.
0980   * @param  hlptim LPTIM handle
0981   * @retval HAL status
0982   */
0983 HAL_StatusTypeDef HAL_LPTIM_SetOnce_Stop(LPTIM_HandleTypeDef *hlptim)
0984 {
0985   /* Check the parameters */
0986   assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
0987 
0988   /* Set the LPTIM state */
0989   hlptim->State = HAL_LPTIM_STATE_BUSY;
0990 
0991   /* Disable the Peripheral */
0992   __HAL_LPTIM_DISABLE(hlptim);
0993 
0994   if (HAL_LPTIM_GetState(hlptim) == HAL_LPTIM_STATE_TIMEOUT)
0995   {
0996     return HAL_TIMEOUT;
0997   }
0998 
0999   /* Change the LPTIM state */
1000   hlptim->State = HAL_LPTIM_STATE_READY;
1001 
1002   /* Return function status */
1003   return HAL_OK;
1004 }
1005 
1006 /**
1007   * @brief  Start the LPTIM Set once mode in interrupt mode.
1008   * @param  hlptim LPTIM handle
1009   * @param  Period Specifies the Autoreload value.
1010   *         This parameter must be a value between 0x0000 and 0xFFFF.
1011   * @param  Pulse Specifies the compare value.
1012   *         This parameter must be a value between 0x0000 and 0xFFFF.
1013   * @retval HAL status
1014   */
1015 HAL_StatusTypeDef HAL_LPTIM_SetOnce_Start_IT(LPTIM_HandleTypeDef *hlptim, uint32_t Period, uint32_t Pulse)
1016 {
1017   /* Check the parameters */
1018   assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
1019   assert_param(IS_LPTIM_PERIOD(Period));
1020   assert_param(IS_LPTIM_PULSE(Pulse));
1021 
1022   /* Set the LPTIM state */
1023   hlptim->State = HAL_LPTIM_STATE_BUSY;
1024 
1025   /* Set WAVE bit to enable the set once mode */
1026   hlptim->Instance->CFGR |= LPTIM_CFGR_WAVE;
1027 
1028   /* Enable the Peripheral */
1029   __HAL_LPTIM_ENABLE(hlptim);
1030 
1031   /* Clear flag */
1032   __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_ARROK);
1033 
1034   /* Load the period value in the autoreload register */
1035   __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
1036 
1037   /* Wait for the completion of the write operation to the LPTIM_ARR register */
1038   if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_ARROK) == HAL_TIMEOUT)
1039   {
1040     return HAL_TIMEOUT;
1041   }
1042 
1043   /* Clear flag */
1044   __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_CMPOK);
1045 
1046   /* Load the pulse value in the compare register */
1047   __HAL_LPTIM_COMPARE_SET(hlptim, Pulse);
1048 
1049   /* Wait for the completion of the write operation to the LPTIM_CMP register */
1050   if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_CMPOK) == HAL_TIMEOUT)
1051   {
1052     return HAL_TIMEOUT;
1053   }
1054 
1055   /* Disable the Peripheral */
1056   __HAL_LPTIM_DISABLE(hlptim);
1057 
1058   if (HAL_LPTIM_GetState(hlptim) == HAL_LPTIM_STATE_TIMEOUT)
1059   {
1060     return HAL_TIMEOUT;
1061   }
1062 
1063   /* Enable Autoreload write complete interrupt */
1064   __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARROK);
1065 
1066   /* Enable Compare write complete interrupt */
1067   __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_CMPOK);
1068 
1069   /* Enable Autoreload match interrupt */
1070   __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARRM);
1071 
1072   /* Enable Compare match interrupt */
1073   __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_CMPM);
1074 
1075   /* If external trigger source is used, then enable external trigger interrupt */
1076   if ((hlptim->Init.Trigger.Source) != LPTIM_TRIGSOURCE_SOFTWARE)
1077   {
1078     /* Enable external trigger interrupt */
1079     __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_EXTTRIG);
1080   }
1081 
1082   /* Enable the Peripheral */
1083   __HAL_LPTIM_ENABLE(hlptim);
1084 
1085   /* Start timer in single (one shot) mode */
1086   __HAL_LPTIM_START_SINGLE(hlptim);
1087 
1088   /* Change the LPTIM state */
1089   hlptim->State = HAL_LPTIM_STATE_READY;
1090 
1091   /* Return function status */
1092   return HAL_OK;
1093 }
1094 
1095 /**
1096   * @brief  Stop the LPTIM Set once mode in interrupt mode.
1097   * @param  hlptim LPTIM handle
1098   * @retval HAL status
1099   */
1100 HAL_StatusTypeDef HAL_LPTIM_SetOnce_Stop_IT(LPTIM_HandleTypeDef *hlptim)
1101 {
1102   /* Check the parameters */
1103   assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
1104 
1105   /* Set the LPTIM state */
1106   hlptim->State = HAL_LPTIM_STATE_BUSY;
1107 
1108   /* Disable the Peripheral */
1109   __HAL_LPTIM_DISABLE(hlptim);
1110 
1111   if (HAL_LPTIM_GetState(hlptim) == HAL_LPTIM_STATE_TIMEOUT)
1112   {
1113     return HAL_TIMEOUT;
1114   }
1115 
1116   /* Disable Autoreload write complete interrupt */
1117   __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_ARROK);
1118 
1119   /* Disable Compare write complete interrupt */
1120   __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_CMPOK);
1121 
1122   /* Disable Autoreload match interrupt */
1123   __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_ARRM);
1124 
1125   /* Disable Compare match interrupt */
1126   __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_CMPM);
1127 
1128   /* If external trigger source is used, then disable external trigger interrupt */
1129   if ((hlptim->Init.Trigger.Source) != LPTIM_TRIGSOURCE_SOFTWARE)
1130   {
1131     /* Disable external trigger interrupt */
1132     __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_EXTTRIG);
1133   }
1134 
1135   /* Change the LPTIM state */
1136   hlptim->State = HAL_LPTIM_STATE_READY;
1137 
1138   /* Return function status */
1139   return HAL_OK;
1140 }
1141 
1142 /**
1143   * @brief  Start the Encoder interface.
1144   * @param  hlptim LPTIM handle
1145   * @param  Period Specifies the Autoreload value.
1146   *         This parameter must be a value between 0x0001 and 0xFFFF.
1147   * @retval HAL status
1148   */
1149 HAL_StatusTypeDef HAL_LPTIM_Encoder_Start(LPTIM_HandleTypeDef *hlptim, uint32_t Period)
1150 {
1151   uint32_t          tmpcfgr;
1152 
1153   /* Check the parameters */
1154   assert_param(IS_LPTIM_ENCODER_INTERFACE_INSTANCE(hlptim->Instance));
1155   assert_param(IS_LPTIM_PERIOD(Period));
1156   assert_param(hlptim->Init.Clock.Source == LPTIM_CLOCKSOURCE_APBCLOCK_LPOSC);
1157   assert_param(hlptim->Init.Clock.Prescaler == LPTIM_PRESCALER_DIV1);
1158   assert_param(IS_LPTIM_CLOCK_POLARITY(hlptim->Init.UltraLowPowerClock.Polarity));
1159 
1160   /* Set the LPTIM state */
1161   hlptim->State = HAL_LPTIM_STATE_BUSY;
1162 
1163   /* Get the LPTIMx CFGR value */
1164   tmpcfgr = hlptim->Instance->CFGR;
1165 
1166   /* Clear CKPOL bits */
1167   tmpcfgr &= (uint32_t)(~LPTIM_CFGR_CKPOL);
1168 
1169   /* Set Input polarity */
1170   tmpcfgr |=  hlptim->Init.UltraLowPowerClock.Polarity;
1171 
1172   /* Write to LPTIMx CFGR */
1173   hlptim->Instance->CFGR = tmpcfgr;
1174 
1175   /* Set ENC bit to enable the encoder interface */
1176   hlptim->Instance->CFGR |= LPTIM_CFGR_ENC;
1177 
1178   /* Enable the Peripheral */
1179   __HAL_LPTIM_ENABLE(hlptim);
1180 
1181   /* Clear flag */
1182   __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_ARROK);
1183 
1184   /* Load the period value in the autoreload register */
1185   __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
1186 
1187   /* Wait for the completion of the write operation to the LPTIM_ARR register */
1188   if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_ARROK) == HAL_TIMEOUT)
1189   {
1190     return HAL_TIMEOUT;
1191   }
1192 
1193   /* Start timer in continuous mode */
1194   __HAL_LPTIM_START_CONTINUOUS(hlptim);
1195 
1196   /* Change the LPTIM state */
1197   hlptim->State = HAL_LPTIM_STATE_READY;
1198 
1199   /* Return function status */
1200   return HAL_OK;
1201 }
1202 
1203 /**
1204   * @brief  Stop the Encoder interface.
1205   * @param  hlptim LPTIM handle
1206   * @retval HAL status
1207   */
1208 HAL_StatusTypeDef HAL_LPTIM_Encoder_Stop(LPTIM_HandleTypeDef *hlptim)
1209 {
1210   /* Check the parameters */
1211   assert_param(IS_LPTIM_ENCODER_INTERFACE_INSTANCE(hlptim->Instance));
1212 
1213   /* Set the LPTIM state */
1214   hlptim->State = HAL_LPTIM_STATE_BUSY;
1215 
1216   /* Disable the Peripheral */
1217   __HAL_LPTIM_DISABLE(hlptim);
1218 
1219   if (HAL_LPTIM_GetState(hlptim) == HAL_LPTIM_STATE_TIMEOUT)
1220   {
1221     return HAL_TIMEOUT;
1222   }
1223 
1224   /* Reset ENC bit to disable the encoder interface */
1225   hlptim->Instance->CFGR &= ~LPTIM_CFGR_ENC;
1226 
1227   /* Change the LPTIM state */
1228   hlptim->State = HAL_LPTIM_STATE_READY;
1229 
1230   /* Return function status */
1231   return HAL_OK;
1232 }
1233 
1234 /**
1235   * @brief  Start the Encoder interface in interrupt mode.
1236   * @param  hlptim LPTIM handle
1237   * @param  Period Specifies the Autoreload value.
1238   *         This parameter must be a value between 0x0000 and 0xFFFF.
1239   * @retval HAL status
1240   */
1241 HAL_StatusTypeDef HAL_LPTIM_Encoder_Start_IT(LPTIM_HandleTypeDef *hlptim, uint32_t Period)
1242 {
1243   uint32_t          tmpcfgr;
1244 
1245   /* Check the parameters */
1246   assert_param(IS_LPTIM_ENCODER_INTERFACE_INSTANCE(hlptim->Instance));
1247   assert_param(IS_LPTIM_PERIOD(Period));
1248   assert_param(hlptim->Init.Clock.Source == LPTIM_CLOCKSOURCE_APBCLOCK_LPOSC);
1249   assert_param(hlptim->Init.Clock.Prescaler == LPTIM_PRESCALER_DIV1);
1250   assert_param(IS_LPTIM_CLOCK_POLARITY(hlptim->Init.UltraLowPowerClock.Polarity));
1251 
1252   /* Set the LPTIM state */
1253   hlptim->State = HAL_LPTIM_STATE_BUSY;
1254 
1255   /* Configure edge sensitivity for encoder mode */
1256   /* Get the LPTIMx CFGR value */
1257   tmpcfgr = hlptim->Instance->CFGR;
1258 
1259   /* Clear CKPOL bits */
1260   tmpcfgr &= (uint32_t)(~LPTIM_CFGR_CKPOL);
1261 
1262   /* Set Input polarity */
1263   tmpcfgr |=  hlptim->Init.UltraLowPowerClock.Polarity;
1264 
1265   /* Write to LPTIMx CFGR */
1266   hlptim->Instance->CFGR = tmpcfgr;
1267 
1268   /* Set ENC bit to enable the encoder interface */
1269   hlptim->Instance->CFGR |= LPTIM_CFGR_ENC;
1270 
1271   /* Enable the Peripheral */
1272   __HAL_LPTIM_ENABLE(hlptim);
1273 
1274   /* Clear flag */
1275   __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_ARROK);
1276 
1277   /* Load the period value in the autoreload register */
1278   __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
1279 
1280   /* Wait for the completion of the write operation to the LPTIM_ARR register */
1281   if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_ARROK) == HAL_TIMEOUT)
1282   {
1283     return HAL_TIMEOUT;
1284   }
1285 
1286   /* Disable the Peripheral */
1287   __HAL_LPTIM_DISABLE(hlptim);
1288 
1289   if (HAL_LPTIM_GetState(hlptim) == HAL_LPTIM_STATE_TIMEOUT)
1290   {
1291     return HAL_TIMEOUT;
1292   }
1293 
1294   /* Enable "switch to down direction" interrupt */
1295   __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_DOWN);
1296 
1297   /* Enable "switch to up direction" interrupt */
1298   __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_UP);
1299 
1300   /* Enable the Peripheral */
1301   __HAL_LPTIM_ENABLE(hlptim);
1302 
1303   /* Start timer in continuous mode */
1304   __HAL_LPTIM_START_CONTINUOUS(hlptim);
1305 
1306   /* Change the LPTIM state */
1307   hlptim->State = HAL_LPTIM_STATE_READY;
1308 
1309   /* Return function status */
1310   return HAL_OK;
1311 }
1312 
1313 /**
1314   * @brief  Stop the Encoder interface in interrupt mode.
1315   * @param  hlptim LPTIM handle
1316   * @retval HAL status
1317   */
1318 HAL_StatusTypeDef HAL_LPTIM_Encoder_Stop_IT(LPTIM_HandleTypeDef *hlptim)
1319 {
1320   /* Check the parameters */
1321   assert_param(IS_LPTIM_ENCODER_INTERFACE_INSTANCE(hlptim->Instance));
1322 
1323   /* Set the LPTIM state */
1324   hlptim->State = HAL_LPTIM_STATE_BUSY;
1325 
1326   /* Disable the Peripheral */
1327   __HAL_LPTIM_DISABLE(hlptim);
1328 
1329   if (HAL_LPTIM_GetState(hlptim) == HAL_LPTIM_STATE_TIMEOUT)
1330   {
1331     return HAL_TIMEOUT;
1332   }
1333 
1334   /* Reset ENC bit to disable the encoder interface */
1335   hlptim->Instance->CFGR &= ~LPTIM_CFGR_ENC;
1336 
1337   /* Disable "switch to down direction" interrupt */
1338   __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_DOWN);
1339 
1340   /* Disable "switch to up direction" interrupt */
1341   __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_UP);
1342 
1343   /* Change the LPTIM state */
1344   hlptim->State = HAL_LPTIM_STATE_READY;
1345 
1346   /* Return function status */
1347   return HAL_OK;
1348 }
1349 
1350 /**
1351   * @brief  Start the Timeout function.
1352   * @note   The first trigger event will start the timer, any successive
1353   *         trigger event will reset the counter and the timer restarts.
1354   * @param  hlptim LPTIM handle
1355   * @param  Period Specifies the Autoreload value.
1356   *         This parameter must be a value between 0x0001 and 0xFFFF.
1357   * @param  Timeout Specifies the TimeOut value to reset the counter.
1358   *         This parameter must be a value between 0x0000 and 0xFFFF.
1359   * @retval HAL status
1360   */
1361 HAL_StatusTypeDef HAL_LPTIM_TimeOut_Start(LPTIM_HandleTypeDef *hlptim, uint32_t Period, uint32_t Timeout)
1362 {
1363   /* Check the parameters */
1364   assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
1365   assert_param(IS_LPTIM_PERIOD(Period));
1366   assert_param(IS_LPTIM_PULSE(Timeout));
1367 
1368   /* Set the LPTIM state */
1369   hlptim->State = HAL_LPTIM_STATE_BUSY;
1370 
1371   /* Set TIMOUT bit to enable the timeout function */
1372   hlptim->Instance->CFGR |= LPTIM_CFGR_TIMOUT;
1373 
1374   /* Enable the Peripheral */
1375   __HAL_LPTIM_ENABLE(hlptim);
1376 
1377   /* Clear flag */
1378   __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_ARROK);
1379 
1380   /* Load the period value in the autoreload register */
1381   __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
1382 
1383   /* Wait for the completion of the write operation to the LPTIM_ARR register */
1384   if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_ARROK) == HAL_TIMEOUT)
1385   {
1386     return HAL_TIMEOUT;
1387   }
1388 
1389   /* Clear flag */
1390   __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_CMPOK);
1391 
1392   /* Load the Timeout value in the compare register */
1393   __HAL_LPTIM_COMPARE_SET(hlptim, Timeout);
1394 
1395   /* Wait for the completion of the write operation to the LPTIM_CMP register */
1396   if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_CMPOK) == HAL_TIMEOUT)
1397   {
1398     return HAL_TIMEOUT;
1399   }
1400 
1401   /* Start timer in continuous mode */
1402   __HAL_LPTIM_START_CONTINUOUS(hlptim);
1403 
1404   /* Change the LPTIM state */
1405   hlptim->State = HAL_LPTIM_STATE_READY;
1406 
1407   /* Return function status */
1408   return HAL_OK;
1409 }
1410 
1411 /**
1412   * @brief  Stop the Timeout function.
1413   * @param  hlptim LPTIM handle
1414   * @retval HAL status
1415   */
1416 HAL_StatusTypeDef HAL_LPTIM_TimeOut_Stop(LPTIM_HandleTypeDef *hlptim)
1417 {
1418   /* Check the parameters */
1419   assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
1420 
1421   /* Set the LPTIM state */
1422   hlptim->State = HAL_LPTIM_STATE_BUSY;
1423 
1424   /* Disable the Peripheral */
1425   __HAL_LPTIM_DISABLE(hlptim);
1426 
1427   if (HAL_LPTIM_GetState(hlptim) == HAL_LPTIM_STATE_TIMEOUT)
1428   {
1429     return HAL_TIMEOUT;
1430   }
1431 
1432   /* Reset TIMOUT bit to enable the timeout function */
1433   hlptim->Instance->CFGR &= ~LPTIM_CFGR_TIMOUT;
1434 
1435   /* Change the LPTIM state */
1436   hlptim->State = HAL_LPTIM_STATE_READY;
1437 
1438   /* Return function status */
1439   return HAL_OK;
1440 }
1441 
1442 /**
1443   * @brief  Start the Timeout function in interrupt mode.
1444   * @note   The first trigger event will start the timer, any successive
1445   *         trigger event will reset the counter and the timer restarts.
1446   * @param  hlptim LPTIM handle
1447   * @param  Period Specifies the Autoreload value.
1448   *         This parameter must be a value between 0x0001 and 0xFFFF.
1449   * @param  Timeout Specifies the TimeOut value to reset the counter.
1450   *         This parameter must be a value between 0x0000 and 0xFFFF.
1451   * @retval HAL status
1452   */
1453 HAL_StatusTypeDef HAL_LPTIM_TimeOut_Start_IT(LPTIM_HandleTypeDef *hlptim, uint32_t Period, uint32_t Timeout)
1454 {
1455   /* Check the parameters */
1456   assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
1457   assert_param(IS_LPTIM_PERIOD(Period));
1458   assert_param(IS_LPTIM_PULSE(Timeout));
1459 
1460   /* Set the LPTIM state */
1461   hlptim->State = HAL_LPTIM_STATE_BUSY;
1462 
1463   /* Set TIMOUT bit to enable the timeout function */
1464   hlptim->Instance->CFGR |= LPTIM_CFGR_TIMOUT;
1465 
1466   /* Enable the Peripheral */
1467   __HAL_LPTIM_ENABLE(hlptim);
1468 
1469   /* Clear flag */
1470   __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_ARROK);
1471 
1472   /* Load the period value in the autoreload register */
1473   __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
1474 
1475   /* Wait for the completion of the write operation to the LPTIM_ARR register */
1476   if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_ARROK) == HAL_TIMEOUT)
1477   {
1478     return HAL_TIMEOUT;
1479   }
1480 
1481   /* Clear flag */
1482   __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_CMPOK);
1483 
1484   /* Load the Timeout value in the compare register */
1485   __HAL_LPTIM_COMPARE_SET(hlptim, Timeout);
1486 
1487   /* Wait for the completion of the write operation to the LPTIM_CMP register */
1488   if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_CMPOK) == HAL_TIMEOUT)
1489   {
1490     return HAL_TIMEOUT;
1491   }
1492 
1493   /* Disable the Peripheral */
1494   __HAL_LPTIM_DISABLE(hlptim);
1495 
1496   if (HAL_LPTIM_GetState(hlptim) == HAL_LPTIM_STATE_TIMEOUT)
1497   {
1498     return HAL_TIMEOUT;
1499   }
1500 
1501   /* Enable Compare match interrupt */
1502   __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_CMPM);
1503 
1504   /* Enable the Peripheral */
1505   __HAL_LPTIM_ENABLE(hlptim);
1506 
1507   /* Start timer in continuous mode */
1508   __HAL_LPTIM_START_CONTINUOUS(hlptim);
1509 
1510   /* Change the LPTIM state */
1511   hlptim->State = HAL_LPTIM_STATE_READY;
1512 
1513   /* Return function status */
1514   return HAL_OK;
1515 }
1516 
1517 /**
1518   * @brief  Stop the Timeout function in interrupt mode.
1519   * @param  hlptim LPTIM handle
1520   * @retval HAL status
1521   */
1522 HAL_StatusTypeDef HAL_LPTIM_TimeOut_Stop_IT(LPTIM_HandleTypeDef *hlptim)
1523 {
1524   /* Check the parameters */
1525   assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
1526 
1527   /* Set the LPTIM state */
1528   hlptim->State = HAL_LPTIM_STATE_BUSY;
1529 
1530   /* Disable the Peripheral */
1531   __HAL_LPTIM_DISABLE(hlptim);
1532 
1533   if (HAL_LPTIM_GetState(hlptim) == HAL_LPTIM_STATE_TIMEOUT)
1534   {
1535     return HAL_TIMEOUT;
1536   }
1537 
1538   /* Reset TIMOUT bit to enable the timeout function */
1539   hlptim->Instance->CFGR &= ~LPTIM_CFGR_TIMOUT;
1540 
1541   /* Disable Compare match interrupt */
1542   __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_CMPM);
1543 
1544   /* Change the LPTIM state */
1545   hlptim->State = HAL_LPTIM_STATE_READY;
1546 
1547   /* Return function status */
1548   return HAL_OK;
1549 }
1550 
1551 /**
1552   * @brief  Start the Counter mode.
1553   * @param  hlptim LPTIM handle
1554   * @param  Period Specifies the Autoreload value.
1555   *         This parameter must be a value between 0x0001 and 0xFFFF.
1556   * @retval HAL status
1557   */
1558 HAL_StatusTypeDef HAL_LPTIM_Counter_Start(LPTIM_HandleTypeDef *hlptim, uint32_t Period)
1559 {
1560   /* Check the parameters */
1561   assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
1562   assert_param(IS_LPTIM_PERIOD(Period));
1563 
1564   /* Set the LPTIM state */
1565   hlptim->State = HAL_LPTIM_STATE_BUSY;
1566 
1567   /* If clock source is not ULPTIM clock and counter source is external, then it must not be prescaled */
1568   if ((hlptim->Init.Clock.Source != LPTIM_CLOCKSOURCE_ULPTIM)
1569       && (hlptim->Init.CounterSource == LPTIM_COUNTERSOURCE_EXTERNAL))
1570   {
1571     /* Check if clock is prescaled */
1572     assert_param(IS_LPTIM_CLOCK_PRESCALERDIV1(hlptim->Init.Clock.Prescaler));
1573     /* Set clock prescaler to 0 */
1574     hlptim->Instance->CFGR &= ~LPTIM_CFGR_PRESC;
1575   }
1576 
1577   /* Enable the Peripheral */
1578   __HAL_LPTIM_ENABLE(hlptim);
1579 
1580   /* Clear flag */
1581   __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_ARROK);
1582 
1583   /* Load the period value in the autoreload register */
1584   __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
1585 
1586   /* Wait for the completion of the write operation to the LPTIM_ARR register */
1587   if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_ARROK) == HAL_TIMEOUT)
1588   {
1589     return HAL_TIMEOUT;
1590   }
1591 
1592   /* Start timer in continuous mode */
1593   __HAL_LPTIM_START_CONTINUOUS(hlptim);
1594 
1595   /* Change the LPTIM state */
1596   hlptim->State = HAL_LPTIM_STATE_READY;
1597 
1598   /* Return function status */
1599   return HAL_OK;
1600 }
1601 
1602 /**
1603   * @brief  Stop the Counter mode.
1604   * @param  hlptim LPTIM handle
1605   * @retval HAL status
1606   */
1607 HAL_StatusTypeDef HAL_LPTIM_Counter_Stop(LPTIM_HandleTypeDef *hlptim)
1608 {
1609   /* Check the parameters */
1610   assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
1611 
1612   /* Set the LPTIM state */
1613   hlptim->State = HAL_LPTIM_STATE_BUSY;
1614 
1615   /* Disable the Peripheral */
1616   __HAL_LPTIM_DISABLE(hlptim);
1617 
1618   if (HAL_LPTIM_GetState(hlptim) == HAL_LPTIM_STATE_TIMEOUT)
1619   {
1620     return HAL_TIMEOUT;
1621   }
1622 
1623   /* Change the LPTIM state */
1624   hlptim->State = HAL_LPTIM_STATE_READY;
1625 
1626   /* Return function status */
1627   return HAL_OK;
1628 }
1629 
1630 /**
1631   * @brief  Start the Counter mode in interrupt mode.
1632   * @param  hlptim LPTIM handle
1633   * @param  Period Specifies the Autoreload value.
1634   *         This parameter must be a value between 0x0001 and 0xFFFF.
1635   * @retval HAL status
1636   */
1637 HAL_StatusTypeDef HAL_LPTIM_Counter_Start_IT(LPTIM_HandleTypeDef *hlptim, uint32_t Period)
1638 {
1639   /* Check the parameters */
1640   assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
1641   assert_param(IS_LPTIM_PERIOD(Period));
1642 
1643   /* Set the LPTIM state */
1644   hlptim->State = HAL_LPTIM_STATE_BUSY;
1645 
1646   /* If clock source is not ULPTIM clock and counter source is external, then it must not be prescaled */
1647   if ((hlptim->Init.Clock.Source != LPTIM_CLOCKSOURCE_ULPTIM)
1648       && (hlptim->Init.CounterSource == LPTIM_COUNTERSOURCE_EXTERNAL))
1649   {
1650     /* Check if clock is prescaled */
1651     assert_param(IS_LPTIM_CLOCK_PRESCALERDIV1(hlptim->Init.Clock.Prescaler));
1652     /* Set clock prescaler to 0 */
1653     hlptim->Instance->CFGR &= ~LPTIM_CFGR_PRESC;
1654   }
1655 
1656   /* Enable the Peripheral */
1657   __HAL_LPTIM_ENABLE(hlptim);
1658 
1659   /* Clear flag */
1660   __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_ARROK);
1661 
1662   /* Load the period value in the autoreload register */
1663   __HAL_LPTIM_AUTORELOAD_SET(hlptim, Period);
1664 
1665   /* Wait for the completion of the write operation to the LPTIM_ARR register */
1666   if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_ARROK) == HAL_TIMEOUT)
1667   {
1668     return HAL_TIMEOUT;
1669   }
1670 
1671   /* Disable the Peripheral */
1672   __HAL_LPTIM_DISABLE(hlptim);
1673 
1674   if (HAL_LPTIM_GetState(hlptim) == HAL_LPTIM_STATE_TIMEOUT)
1675   {
1676     return HAL_TIMEOUT;
1677   }
1678 
1679   /* Enable Autoreload write complete interrupt */
1680   __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARROK);
1681 
1682   /* Enable Autoreload match interrupt */
1683   __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARRM);
1684 
1685   /* Enable the Peripheral */
1686   __HAL_LPTIM_ENABLE(hlptim);
1687 
1688   /* Start timer in continuous mode */
1689   __HAL_LPTIM_START_CONTINUOUS(hlptim);
1690 
1691   /* Change the LPTIM state */
1692   hlptim->State = HAL_LPTIM_STATE_READY;
1693 
1694   /* Return function status */
1695   return HAL_OK;
1696 }
1697 
1698 /**
1699   * @brief  Stop the Counter mode in interrupt mode.
1700   * @param  hlptim LPTIM handle
1701   * @retval HAL status
1702   */
1703 HAL_StatusTypeDef HAL_LPTIM_Counter_Stop_IT(LPTIM_HandleTypeDef *hlptim)
1704 {
1705   /* Check the parameters */
1706   assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
1707 
1708   /* Set the LPTIM state */
1709   hlptim->State = HAL_LPTIM_STATE_BUSY;
1710 
1711   /* Disable the Peripheral */
1712   __HAL_LPTIM_DISABLE(hlptim);
1713 
1714   if (HAL_LPTIM_GetState(hlptim) == HAL_LPTIM_STATE_TIMEOUT)
1715   {
1716     return HAL_TIMEOUT;
1717   }
1718 
1719   /* Disable Autoreload write complete interrupt */
1720   __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_ARROK);
1721 
1722   /* Disable Autoreload match interrupt */
1723   __HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_ARRM);
1724   /* Change the LPTIM state */
1725   hlptim->State = HAL_LPTIM_STATE_READY;
1726 
1727   /* Return function status */
1728   return HAL_OK;
1729 }
1730 
1731 /**
1732   * @}
1733   */
1734 
1735 /** @defgroup LPTIM_Exported_Functions_Group3 LPTIM Read operation functions
1736   * @ingroup RTEMSBSPsARMSTM32H7
1737   *  @brief  Read operation functions.
1738   *
1739 @verbatim
1740   ==============================================================================
1741                   ##### LPTIM Read operation functions #####
1742   ==============================================================================
1743 [..]  This section provides LPTIM Reading functions.
1744       (+) Read the counter value.
1745       (+) Read the period (Auto-reload) value.
1746       (+) Read the pulse (Compare)value.
1747 @endverbatim
1748   * @{
1749   */
1750 
1751 /**
1752   * @brief  Return the current counter value.
1753   * @param  hlptim LPTIM handle
1754   * @retval Counter value.
1755   */
1756 uint32_t HAL_LPTIM_ReadCounter(const LPTIM_HandleTypeDef *hlptim)
1757 {
1758   /* Check the parameters */
1759   assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
1760 
1761   return (hlptim->Instance->CNT);
1762 }
1763 
1764 /**
1765   * @brief  Return the current Autoreload (Period) value.
1766   * @param  hlptim LPTIM handle
1767   * @retval Autoreload value.
1768   */
1769 uint32_t HAL_LPTIM_ReadAutoReload(const LPTIM_HandleTypeDef *hlptim)
1770 {
1771   /* Check the parameters */
1772   assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
1773 
1774   return (hlptim->Instance->ARR);
1775 }
1776 
1777 /**
1778   * @brief  Return the current Compare (Pulse) value.
1779   * @param  hlptim LPTIM handle
1780   * @retval Compare value.
1781   */
1782 uint32_t HAL_LPTIM_ReadCompare(const LPTIM_HandleTypeDef *hlptim)
1783 {
1784   /* Check the parameters */
1785   assert_param(IS_LPTIM_INSTANCE(hlptim->Instance));
1786 
1787   return (hlptim->Instance->CMP);
1788 }
1789 
1790 /**
1791   * @}
1792   */
1793 
1794 /** @defgroup LPTIM_Exported_Functions_Group4 LPTIM IRQ handler and callbacks
1795   * @ingroup RTEMSBSPsARMSTM32H7
1796   *  @brief  LPTIM  IRQ handler.
1797   *
1798 @verbatim
1799   ==============================================================================
1800                       ##### LPTIM IRQ handler and callbacks  #####
1801   ==============================================================================
1802 [..]  This section provides LPTIM IRQ handler and callback functions called within
1803       the IRQ handler:
1804    (+) LPTIM interrupt request handler
1805    (+) Compare match Callback
1806    (+) Auto-reload match Callback
1807    (+) External trigger event detection Callback
1808    (+) Compare register write complete Callback
1809    (+) Auto-reload register write complete Callback
1810    (+) Up-counting direction change Callback
1811    (+) Down-counting direction change Callback
1812 
1813 @endverbatim
1814   * @{
1815   */
1816 
1817 /**
1818   * @brief  Handle LPTIM interrupt request.
1819   * @param  hlptim LPTIM handle
1820   * @retval None
1821   */
1822 void HAL_LPTIM_IRQHandler(LPTIM_HandleTypeDef *hlptim)
1823 {
1824   /* Compare match interrupt */
1825   if (__HAL_LPTIM_GET_FLAG(hlptim, LPTIM_FLAG_CMPM) != RESET)
1826   {
1827     if (__HAL_LPTIM_GET_IT_SOURCE(hlptim, LPTIM_IT_CMPM) != RESET)
1828     {
1829       /* Clear Compare match flag */
1830       __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_CMPM);
1831 
1832       /* Compare match Callback */
1833 #if (USE_HAL_LPTIM_REGISTER_CALLBACKS == 1)
1834       hlptim->CompareMatchCallback(hlptim);
1835 #else
1836       HAL_LPTIM_CompareMatchCallback(hlptim);
1837 #endif /* USE_HAL_LPTIM_REGISTER_CALLBACKS */
1838     }
1839   }
1840 
1841   /* Autoreload match interrupt */
1842   if (__HAL_LPTIM_GET_FLAG(hlptim, LPTIM_FLAG_ARRM) != RESET)
1843   {
1844     if (__HAL_LPTIM_GET_IT_SOURCE(hlptim, LPTIM_IT_ARRM) != RESET)
1845     {
1846       /* Clear Autoreload match flag */
1847       __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_ARRM);
1848 
1849       /* Autoreload match Callback */
1850 #if (USE_HAL_LPTIM_REGISTER_CALLBACKS == 1)
1851       hlptim->AutoReloadMatchCallback(hlptim);
1852 #else
1853       HAL_LPTIM_AutoReloadMatchCallback(hlptim);
1854 #endif /* USE_HAL_LPTIM_REGISTER_CALLBACKS */
1855     }
1856   }
1857 
1858   /* Trigger detected interrupt */
1859   if (__HAL_LPTIM_GET_FLAG(hlptim, LPTIM_FLAG_EXTTRIG) != RESET)
1860   {
1861     if (__HAL_LPTIM_GET_IT_SOURCE(hlptim, LPTIM_IT_EXTTRIG) != RESET)
1862     {
1863       /* Clear Trigger detected flag */
1864       __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_EXTTRIG);
1865 
1866       /* Trigger detected callback */
1867 #if (USE_HAL_LPTIM_REGISTER_CALLBACKS == 1)
1868       hlptim->TriggerCallback(hlptim);
1869 #else
1870       HAL_LPTIM_TriggerCallback(hlptim);
1871 #endif /* USE_HAL_LPTIM_REGISTER_CALLBACKS */
1872     }
1873   }
1874 
1875   /* Compare write interrupt */
1876   if (__HAL_LPTIM_GET_FLAG(hlptim, LPTIM_FLAG_CMPOK) != RESET)
1877   {
1878     if (__HAL_LPTIM_GET_IT_SOURCE(hlptim, LPTIM_IT_CMPOK) != RESET)
1879     {
1880       /* Clear Compare write flag */
1881       __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_CMPOK);
1882 
1883       /* Compare write Callback */
1884 #if (USE_HAL_LPTIM_REGISTER_CALLBACKS == 1)
1885       hlptim->CompareWriteCallback(hlptim);
1886 #else
1887       HAL_LPTIM_CompareWriteCallback(hlptim);
1888 #endif /* USE_HAL_LPTIM_REGISTER_CALLBACKS */
1889     }
1890   }
1891 
1892   /* Autoreload write interrupt */
1893   if (__HAL_LPTIM_GET_FLAG(hlptim, LPTIM_FLAG_ARROK) != RESET)
1894   {
1895     if (__HAL_LPTIM_GET_IT_SOURCE(hlptim, LPTIM_IT_ARROK) != RESET)
1896     {
1897       /* Clear Autoreload write flag */
1898       __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_ARROK);
1899 
1900       /* Autoreload write Callback */
1901 #if (USE_HAL_LPTIM_REGISTER_CALLBACKS == 1)
1902       hlptim->AutoReloadWriteCallback(hlptim);
1903 #else
1904       HAL_LPTIM_AutoReloadWriteCallback(hlptim);
1905 #endif /* USE_HAL_LPTIM_REGISTER_CALLBACKS */
1906     }
1907   }
1908 
1909   /* Direction counter changed from Down to Up interrupt */
1910   if (__HAL_LPTIM_GET_FLAG(hlptim, LPTIM_FLAG_UP) != RESET)
1911   {
1912     if (__HAL_LPTIM_GET_IT_SOURCE(hlptim, LPTIM_IT_UP) != RESET)
1913     {
1914       /* Clear Direction counter changed from Down to Up flag */
1915       __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_UP);
1916 
1917       /* Direction counter changed from Down to Up Callback */
1918 #if (USE_HAL_LPTIM_REGISTER_CALLBACKS == 1)
1919       hlptim->DirectionUpCallback(hlptim);
1920 #else
1921       HAL_LPTIM_DirectionUpCallback(hlptim);
1922 #endif /* USE_HAL_LPTIM_REGISTER_CALLBACKS */
1923     }
1924   }
1925 
1926   /* Direction counter changed from Up to Down interrupt */
1927   if (__HAL_LPTIM_GET_FLAG(hlptim, LPTIM_FLAG_DOWN) != RESET)
1928   {
1929     if (__HAL_LPTIM_GET_IT_SOURCE(hlptim, LPTIM_IT_DOWN) != RESET)
1930     {
1931       /* Clear Direction counter changed from Up to Down flag */
1932       __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_DOWN);
1933 
1934       /* Direction counter changed from Up to Down Callback */
1935 #if (USE_HAL_LPTIM_REGISTER_CALLBACKS == 1)
1936       hlptim->DirectionDownCallback(hlptim);
1937 #else
1938       HAL_LPTIM_DirectionDownCallback(hlptim);
1939 #endif /* USE_HAL_LPTIM_REGISTER_CALLBACKS */
1940     }
1941   }
1942 }
1943 
1944 /**
1945   * @brief  Compare match callback in non-blocking mode.
1946   * @param  hlptim LPTIM handle
1947   * @retval None
1948   */
1949 __weak void HAL_LPTIM_CompareMatchCallback(LPTIM_HandleTypeDef *hlptim)
1950 {
1951   /* Prevent unused argument(s) compilation warning */
1952   UNUSED(hlptim);
1953 
1954   /* NOTE : This function should not be modified, when the callback is needed,
1955             the HAL_LPTIM_CompareMatchCallback could be implemented in the user file
1956    */
1957 }
1958 
1959 /**
1960   * @brief  Autoreload match callback in non-blocking mode.
1961   * @param  hlptim LPTIM handle
1962   * @retval None
1963   */
1964 __weak void HAL_LPTIM_AutoReloadMatchCallback(LPTIM_HandleTypeDef *hlptim)
1965 {
1966   /* Prevent unused argument(s) compilation warning */
1967   UNUSED(hlptim);
1968 
1969   /* NOTE : This function should not be modified, when the callback is needed,
1970             the HAL_LPTIM_AutoReloadMatchCallback could be implemented in the user file
1971    */
1972 }
1973 
1974 /**
1975   * @brief  Trigger detected callback in non-blocking mode.
1976   * @param  hlptim LPTIM handle
1977   * @retval None
1978   */
1979 __weak void HAL_LPTIM_TriggerCallback(LPTIM_HandleTypeDef *hlptim)
1980 {
1981   /* Prevent unused argument(s) compilation warning */
1982   UNUSED(hlptim);
1983 
1984   /* NOTE : This function should not be modified, when the callback is needed,
1985             the HAL_LPTIM_TriggerCallback could be implemented in the user file
1986    */
1987 }
1988 
1989 /**
1990   * @brief  Compare write callback in non-blocking mode.
1991   * @param  hlptim LPTIM handle
1992   * @retval None
1993   */
1994 __weak void HAL_LPTIM_CompareWriteCallback(LPTIM_HandleTypeDef *hlptim)
1995 {
1996   /* Prevent unused argument(s) compilation warning */
1997   UNUSED(hlptim);
1998 
1999   /* NOTE : This function should not be modified, when the callback is needed,
2000             the HAL_LPTIM_CompareWriteCallback could be implemented in the user file
2001    */
2002 }
2003 
2004 /**
2005   * @brief  Autoreload write callback in non-blocking mode.
2006   * @param  hlptim LPTIM handle
2007   * @retval None
2008   */
2009 __weak void HAL_LPTIM_AutoReloadWriteCallback(LPTIM_HandleTypeDef *hlptim)
2010 {
2011   /* Prevent unused argument(s) compilation warning */
2012   UNUSED(hlptim);
2013 
2014   /* NOTE : This function should not be modified, when the callback is needed,
2015             the HAL_LPTIM_AutoReloadWriteCallback could be implemented in the user file
2016    */
2017 }
2018 
2019 /**
2020   * @brief  Direction counter changed from Down to Up callback in non-blocking mode.
2021   * @param  hlptim LPTIM handle
2022   * @retval None
2023   */
2024 __weak void HAL_LPTIM_DirectionUpCallback(LPTIM_HandleTypeDef *hlptim)
2025 {
2026   /* Prevent unused argument(s) compilation warning */
2027   UNUSED(hlptim);
2028 
2029   /* NOTE : This function should not be modified, when the callback is needed,
2030             the HAL_LPTIM_DirectionUpCallback could be implemented in the user file
2031    */
2032 }
2033 
2034 /**
2035   * @brief  Direction counter changed from Up to Down callback in non-blocking mode.
2036   * @param  hlptim LPTIM handle
2037   * @retval None
2038   */
2039 __weak void HAL_LPTIM_DirectionDownCallback(LPTIM_HandleTypeDef *hlptim)
2040 {
2041   /* Prevent unused argument(s) compilation warning */
2042   UNUSED(hlptim);
2043 
2044   /* NOTE : This function should not be modified, when the callback is needed,
2045             the HAL_LPTIM_DirectionDownCallback could be implemented in the user file
2046    */
2047 }
2048 
2049 #if (USE_HAL_LPTIM_REGISTER_CALLBACKS == 1)
2050 /**
2051   * @brief  Register a User LPTIM callback to be used instead of the weak predefined callback
2052   * @param hlptim LPTIM handle
2053   * @param CallbackID ID of the callback to be registered
2054   *        This parameter can be one of the following values:
2055   *          @arg @ref HAL_LPTIM_MSPINIT_CB_ID          LPTIM Base Msp Init Callback ID
2056   *          @arg @ref HAL_LPTIM_MSPDEINIT_CB_ID        LPTIM Base Msp DeInit Callback ID
2057   *          @arg @ref HAL_LPTIM_COMPARE_MATCH_CB_ID    Compare match Callback ID
2058   *          @arg @ref HAL_LPTIM_AUTORELOAD_MATCH_CB_ID Auto-reload match Callback ID
2059   *          @arg @ref HAL_LPTIM_TRIGGER_CB_ID          External trigger event detection Callback ID
2060   *          @arg @ref HAL_LPTIM_COMPARE_WRITE_CB_ID    Compare register write complete Callback ID
2061   *          @arg @ref HAL_LPTIM_AUTORELOAD_WRITE_CB_ID Auto-reload register write complete Callback ID
2062   *          @arg @ref HAL_LPTIM_DIRECTION_UP_CB_ID     Up-counting direction change Callback ID
2063   *          @arg @ref HAL_LPTIM_DIRECTION_DOWN_CB_ID   Down-counting direction change Callback ID
2064   * @param pCallback pointer to the callback function
2065   * @retval status
2066   */
2067 HAL_StatusTypeDef HAL_LPTIM_RegisterCallback(LPTIM_HandleTypeDef        *hlptim,
2068                                              HAL_LPTIM_CallbackIDTypeDef CallbackID,
2069                                              pLPTIM_CallbackTypeDef      pCallback)
2070 {
2071   HAL_StatusTypeDef status = HAL_OK;
2072 
2073   if (pCallback == NULL)
2074   {
2075     return HAL_ERROR;
2076   }
2077 
2078   if (hlptim->State == HAL_LPTIM_STATE_READY)
2079   {
2080     switch (CallbackID)
2081     {
2082       case HAL_LPTIM_MSPINIT_CB_ID :
2083         hlptim->MspInitCallback = pCallback;
2084         break;
2085 
2086       case HAL_LPTIM_MSPDEINIT_CB_ID :
2087         hlptim->MspDeInitCallback = pCallback;
2088         break;
2089 
2090       case HAL_LPTIM_COMPARE_MATCH_CB_ID :
2091         hlptim->CompareMatchCallback = pCallback;
2092         break;
2093 
2094       case HAL_LPTIM_AUTORELOAD_MATCH_CB_ID :
2095         hlptim->AutoReloadMatchCallback = pCallback;
2096         break;
2097 
2098       case HAL_LPTIM_TRIGGER_CB_ID :
2099         hlptim->TriggerCallback = pCallback;
2100         break;
2101 
2102       case HAL_LPTIM_COMPARE_WRITE_CB_ID :
2103         hlptim->CompareWriteCallback = pCallback;
2104         break;
2105 
2106       case HAL_LPTIM_AUTORELOAD_WRITE_CB_ID :
2107         hlptim->AutoReloadWriteCallback = pCallback;
2108         break;
2109 
2110       case HAL_LPTIM_DIRECTION_UP_CB_ID :
2111         hlptim->DirectionUpCallback = pCallback;
2112         break;
2113 
2114       case HAL_LPTIM_DIRECTION_DOWN_CB_ID :
2115         hlptim->DirectionDownCallback = pCallback;
2116         break;
2117 
2118       default :
2119         /* Return error status */
2120         status =  HAL_ERROR;
2121         break;
2122     }
2123   }
2124   else if (hlptim->State == HAL_LPTIM_STATE_RESET)
2125   {
2126     switch (CallbackID)
2127     {
2128       case HAL_LPTIM_MSPINIT_CB_ID :
2129         hlptim->MspInitCallback = pCallback;
2130         break;
2131 
2132       case HAL_LPTIM_MSPDEINIT_CB_ID :
2133         hlptim->MspDeInitCallback = pCallback;
2134         break;
2135 
2136       default :
2137         /* Return error status */
2138         status =  HAL_ERROR;
2139         break;
2140     }
2141   }
2142   else
2143   {
2144     /* Return error status */
2145     status =  HAL_ERROR;
2146   }
2147 
2148   return status;
2149 }
2150 
2151 /**
2152   * @brief  Unregister a LPTIM callback
2153   *         LLPTIM callback is redirected to the weak predefined callback
2154   * @param hlptim LPTIM handle
2155   * @param CallbackID ID of the callback to be unregistered
2156   *        This parameter can be one of the following values:
2157   *          @arg @ref HAL_LPTIM_MSPINIT_CB_ID          LPTIM Base Msp Init Callback ID
2158   *          @arg @ref HAL_LPTIM_MSPDEINIT_CB_ID        LPTIM Base Msp DeInit Callback ID
2159   *          @arg @ref HAL_LPTIM_COMPARE_MATCH_CB_ID    Compare match Callback ID
2160   *          @arg @ref HAL_LPTIM_AUTORELOAD_MATCH_CB_ID Auto-reload match Callback ID
2161   *          @arg @ref HAL_LPTIM_TRIGGER_CB_ID          External trigger event detection Callback ID
2162   *          @arg @ref HAL_LPTIM_COMPARE_WRITE_CB_ID    Compare register write complete Callback ID
2163   *          @arg @ref HAL_LPTIM_AUTORELOAD_WRITE_CB_ID Auto-reload register write complete Callback ID
2164   *          @arg @ref HAL_LPTIM_DIRECTION_UP_CB_ID     Up-counting direction change Callback ID
2165   *          @arg @ref HAL_LPTIM_DIRECTION_DOWN_CB_ID   Down-counting direction change Callback ID
2166   * @retval status
2167   */
2168 HAL_StatusTypeDef HAL_LPTIM_UnRegisterCallback(LPTIM_HandleTypeDef        *hlptim,
2169                                                HAL_LPTIM_CallbackIDTypeDef CallbackID)
2170 {
2171   HAL_StatusTypeDef status = HAL_OK;
2172 
2173   if (hlptim->State == HAL_LPTIM_STATE_READY)
2174   {
2175     switch (CallbackID)
2176     {
2177       case HAL_LPTIM_MSPINIT_CB_ID :
2178         /* Legacy weak MspInit Callback */
2179         hlptim->MspInitCallback = HAL_LPTIM_MspInit;
2180         break;
2181 
2182       case HAL_LPTIM_MSPDEINIT_CB_ID :
2183         /* Legacy weak Msp DeInit Callback */
2184         hlptim->MspDeInitCallback = HAL_LPTIM_MspDeInit;
2185         break;
2186 
2187       case HAL_LPTIM_COMPARE_MATCH_CB_ID :
2188         /* Legacy weak Compare match Callback */
2189         hlptim->CompareMatchCallback = HAL_LPTIM_CompareMatchCallback;
2190         break;
2191 
2192       case HAL_LPTIM_AUTORELOAD_MATCH_CB_ID :
2193         /* Legacy weak Auto-reload match Callback */
2194         hlptim->AutoReloadMatchCallback = HAL_LPTIM_AutoReloadMatchCallback;
2195         break;
2196 
2197       case HAL_LPTIM_TRIGGER_CB_ID :
2198         /* Legacy weak External trigger event detection Callback */
2199         hlptim->TriggerCallback = HAL_LPTIM_TriggerCallback;
2200         break;
2201 
2202       case HAL_LPTIM_COMPARE_WRITE_CB_ID :
2203         /* Legacy weak Compare register write complete Callback */
2204         hlptim->CompareWriteCallback = HAL_LPTIM_CompareWriteCallback;
2205         break;
2206 
2207       case HAL_LPTIM_AUTORELOAD_WRITE_CB_ID :
2208         /* Legacy weak Auto-reload register write complete Callback */
2209         hlptim->AutoReloadWriteCallback = HAL_LPTIM_AutoReloadWriteCallback;
2210         break;
2211 
2212       case HAL_LPTIM_DIRECTION_UP_CB_ID :
2213         /* Legacy weak Up-counting direction change Callback */
2214         hlptim->DirectionUpCallback = HAL_LPTIM_DirectionUpCallback;
2215         break;
2216 
2217       case HAL_LPTIM_DIRECTION_DOWN_CB_ID :
2218         /* Legacy weak Down-counting direction change Callback */
2219         hlptim->DirectionDownCallback = HAL_LPTIM_DirectionDownCallback;
2220         break;
2221 
2222       default :
2223         /* Return error status */
2224         status =  HAL_ERROR;
2225         break;
2226     }
2227   }
2228   else if (hlptim->State == HAL_LPTIM_STATE_RESET)
2229   {
2230     switch (CallbackID)
2231     {
2232       case HAL_LPTIM_MSPINIT_CB_ID :
2233         /* Legacy weak MspInit Callback */
2234         hlptim->MspInitCallback = HAL_LPTIM_MspInit;
2235         break;
2236 
2237       case HAL_LPTIM_MSPDEINIT_CB_ID :
2238         /* Legacy weak Msp DeInit Callback */
2239         hlptim->MspDeInitCallback = HAL_LPTIM_MspDeInit;
2240         break;
2241 
2242       default :
2243         /* Return error status */
2244         status =  HAL_ERROR;
2245         break;
2246     }
2247   }
2248   else
2249   {
2250     /* Return error status */
2251     status =  HAL_ERROR;
2252   }
2253 
2254   return status;
2255 }
2256 #endif /* USE_HAL_LPTIM_REGISTER_CALLBACKS */
2257 
2258 /**
2259   * @}
2260   */
2261 
2262 /** @defgroup LPTIM_Group5 Peripheral State functions
2263   * @ingroup RTEMSBSPsARMSTM32H7
2264   *  @brief   Peripheral State functions.
2265   *
2266 @verbatim
2267   ==============================================================================
2268                       ##### Peripheral State functions #####
2269   ==============================================================================
2270     [..]
2271     This subsection permits to get in run-time the status of the peripheral.
2272 
2273 @endverbatim
2274   * @{
2275   */
2276 
2277 /**
2278   * @brief  Return the LPTIM handle state.
2279   * @param  hlptim LPTIM handle
2280   * @retval HAL state
2281   */
2282 HAL_LPTIM_StateTypeDef HAL_LPTIM_GetState(const LPTIM_HandleTypeDef *hlptim)
2283 {
2284   /* Return LPTIM handle state */
2285   return hlptim->State;
2286 }
2287 
2288 /**
2289   * @}
2290   */
2291 
2292 
2293 /**
2294   * @}
2295   */
2296 
2297 /* Private functions ---------------------------------------------------------*/
2298 
2299 /** @defgroup LPTIM_Private_Functions LPTIM Private Functions
2300   * @ingroup RTEMSBSPsARMSTM32H7
2301   * @{
2302   */
2303 #if (USE_HAL_LPTIM_REGISTER_CALLBACKS == 1)
2304 /**
2305   * @brief  Reset interrupt callbacks to the legacy weak callbacks.
2306   * @param  lptim pointer to a LPTIM_HandleTypeDef structure that contains
2307   *                the configuration information for LPTIM module.
2308   * @retval None
2309   */
2310 static void LPTIM_ResetCallback(LPTIM_HandleTypeDef *lptim)
2311 {
2312   /* Reset the LPTIM callback to the legacy weak callbacks */
2313   lptim->CompareMatchCallback    = HAL_LPTIM_CompareMatchCallback;
2314   lptim->AutoReloadMatchCallback = HAL_LPTIM_AutoReloadMatchCallback;
2315   lptim->TriggerCallback         = HAL_LPTIM_TriggerCallback;
2316   lptim->CompareWriteCallback    = HAL_LPTIM_CompareWriteCallback;
2317   lptim->AutoReloadWriteCallback = HAL_LPTIM_AutoReloadWriteCallback;
2318   lptim->DirectionUpCallback     = HAL_LPTIM_DirectionUpCallback;
2319   lptim->DirectionDownCallback   = HAL_LPTIM_DirectionDownCallback;
2320 }
2321 #endif /* USE_HAL_LPTIM_REGISTER_CALLBACKS */
2322 
2323 /**
2324   * @brief  LPTimer Wait for flag set
2325   * @param  hlptim pointer to a LPTIM_HandleTypeDef structure that contains
2326   *                the configuration information for LPTIM module.
2327   * @param  flag   The lptim flag
2328   * @retval HAL status
2329   */
2330 static HAL_StatusTypeDef LPTIM_WaitForFlag(const LPTIM_HandleTypeDef *hlptim, uint32_t flag)
2331 {
2332   HAL_StatusTypeDef result = HAL_OK;
2333   uint32_t count = TIMEOUT * (SystemCoreClock / 20UL / 1000UL);
2334   do
2335   {
2336     count--;
2337     if (count == 0UL)
2338     {
2339       result = HAL_TIMEOUT;
2340     }
2341   } while ((!(__HAL_LPTIM_GET_FLAG((hlptim), (flag)))) && (count != 0UL));
2342 
2343   return result;
2344 }
2345 
2346 /**
2347   * @brief  Disable LPTIM HW instance.
2348   * @param  hlptim pointer to a LPTIM_HandleTypeDef structure that contains
2349   *                the configuration information for LPTIM module.
2350   * @note   The following sequence is required to solve LPTIM disable HW limitation.
2351   *         Please check Errata Sheet ES0335 for more details under "MCU may remain
2352   *         stuck in LPTIM interrupt when entering Stop mode" section.
2353   * @retval None
2354   */
2355 void LPTIM_Disable(LPTIM_HandleTypeDef *hlptim)
2356 {
2357   uint32_t tmpclksource = 0;
2358   uint32_t tmpIER;
2359   uint32_t tmpCFGR;
2360   uint32_t tmpCMP;
2361   uint32_t tmpARR;
2362   uint32_t primask_bit;
2363   uint32_t tmpCFGR2;
2364 
2365   /* Enter critical section */
2366   primask_bit = __get_PRIMASK();
2367   __set_PRIMASK(1) ;
2368 
2369   /*********** Save LPTIM Config ***********/
2370   /* Save LPTIM source clock */
2371   switch ((uint32_t)hlptim->Instance)
2372   {
2373     case LPTIM1_BASE:
2374       tmpclksource = __HAL_RCC_GET_LPTIM1_SOURCE();
2375       break;
2376     case LPTIM2_BASE:
2377       tmpclksource = __HAL_RCC_GET_LPTIM2_SOURCE();
2378       break;
2379 #if defined(LPTIM3)
2380     case LPTIM3_BASE:
2381       tmpclksource = __HAL_RCC_GET_LPTIM3_SOURCE();
2382       break;
2383 #endif /* LPTIM3 */
2384 #if defined(LPTIM4)
2385     case LPTIM4_BASE:
2386       tmpclksource = __HAL_RCC_GET_LPTIM4_SOURCE();
2387       break;
2388 #endif /* LPTIM4 */
2389 #if defined(LPTIM5)
2390     case LPTIM5_BASE:
2391       tmpclksource = __HAL_RCC_GET_LPTIM5_SOURCE();
2392       break;
2393 #endif /* LPTIM5 */
2394     default:
2395       break;
2396   }
2397 
2398   /* Save LPTIM configuration registers */
2399   tmpIER = hlptim->Instance->IER;
2400   tmpCFGR = hlptim->Instance->CFGR;
2401   tmpCMP = hlptim->Instance->CMP;
2402   tmpARR = hlptim->Instance->ARR;
2403   tmpCFGR2 = hlptim->Instance->CFGR2;
2404 
2405   /*********** Reset LPTIM ***********/
2406   switch ((uint32_t)hlptim->Instance)
2407   {
2408     case LPTIM1_BASE:
2409       __HAL_RCC_LPTIM1_FORCE_RESET();
2410       __HAL_RCC_LPTIM1_RELEASE_RESET();
2411       break;
2412     case LPTIM2_BASE:
2413       __HAL_RCC_LPTIM2_FORCE_RESET();
2414       __HAL_RCC_LPTIM2_RELEASE_RESET();
2415       break;
2416 #if defined(LPTIM3)
2417     case LPTIM3_BASE:
2418       __HAL_RCC_LPTIM3_FORCE_RESET();
2419       __HAL_RCC_LPTIM3_RELEASE_RESET();
2420       break;
2421 #endif /* LPTIM3 */
2422 #if defined(LPTIM4)
2423     case LPTIM4_BASE:
2424       __HAL_RCC_LPTIM4_FORCE_RESET();
2425       __HAL_RCC_LPTIM4_RELEASE_RESET();
2426       break;
2427 #endif /* LPTIM4 */
2428 #if defined(LPTIM5)
2429     case LPTIM5_BASE:
2430       __HAL_RCC_LPTIM5_FORCE_RESET();
2431       __HAL_RCC_LPTIM5_RELEASE_RESET();
2432       break;
2433 #endif /* LPTIM5 */
2434     default:
2435       break;
2436   }
2437 
2438   /*********** Restore LPTIM Config ***********/
2439   if ((tmpCMP != 0UL) || (tmpARR != 0UL))
2440   {
2441     /* Force LPTIM source kernel clock from APB */
2442     switch ((uint32_t)hlptim->Instance)
2443     {
2444       case LPTIM1_BASE:
2445         __HAL_RCC_LPTIM1_CONFIG(RCC_LPTIM1CLKSOURCE_D2PCLK1);
2446         break;
2447       case LPTIM2_BASE:
2448         __HAL_RCC_LPTIM2_CONFIG(RCC_LPTIM2CLKSOURCE_D3PCLK1);
2449         break;
2450 #if defined(LPTIM3)
2451       case LPTIM3_BASE:
2452         __HAL_RCC_LPTIM3_CONFIG(RCC_LPTIM3CLKSOURCE_D3PCLK1);
2453         break;
2454 #endif /* LPTIM3 */
2455 #if defined(LPTIM4)
2456       case LPTIM4_BASE:
2457         __HAL_RCC_LPTIM4_CONFIG(RCC_LPTIM4CLKSOURCE_D3PCLK1);
2458         break;
2459 #endif /* LPTIM4 */
2460 #if defined(LPTIM5)
2461       case LPTIM5_BASE:
2462         __HAL_RCC_LPTIM5_CONFIG(RCC_LPTIM5CLKSOURCE_D3PCLK1);
2463         break;
2464 #endif /* LPTIM5 */
2465       default:
2466         break;
2467     }
2468 
2469     if (tmpCMP != 0UL)
2470     {
2471       /* Restore CMP register (LPTIM should be enabled first) */
2472       hlptim->Instance->CR |= LPTIM_CR_ENABLE;
2473       hlptim->Instance->CMP = tmpCMP;
2474 
2475       /* Wait for the completion of the write operation to the LPTIM_CMP register */
2476       if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_CMPOK) == HAL_TIMEOUT)
2477       {
2478         hlptim->State = HAL_LPTIM_STATE_TIMEOUT;
2479       }
2480       __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_CMPOK);
2481     }
2482 
2483     if (tmpARR != 0UL)
2484     {
2485       /* Restore ARR register (LPTIM should be enabled first) */
2486       hlptim->Instance->CR |= LPTIM_CR_ENABLE;
2487       hlptim->Instance->ARR = tmpARR;
2488 
2489       /* Wait for the completion of the write operation to the LPTIM_ARR register */
2490       if (LPTIM_WaitForFlag(hlptim, LPTIM_FLAG_ARROK) == HAL_TIMEOUT)
2491       {
2492         hlptim->State = HAL_LPTIM_STATE_TIMEOUT;
2493       }
2494 
2495       __HAL_LPTIM_CLEAR_FLAG(hlptim, LPTIM_FLAG_ARROK);
2496     }
2497 
2498     /* Restore LPTIM source kernel clock */
2499     switch ((uint32_t)hlptim->Instance)
2500     {
2501       case LPTIM1_BASE:
2502         __HAL_RCC_LPTIM1_CONFIG(tmpclksource);
2503         break;
2504       case LPTIM2_BASE:
2505         __HAL_RCC_LPTIM2_CONFIG(tmpclksource);
2506         break;
2507 #if defined(LPTIM3)
2508       case LPTIM3_BASE:
2509         __HAL_RCC_LPTIM3_CONFIG(tmpclksource);
2510         break;
2511 #endif /* LPTIM3 */
2512 #if defined(LPTIM4)
2513       case LPTIM4_BASE:
2514         __HAL_RCC_LPTIM4_CONFIG(tmpclksource);
2515         break;
2516 #endif /* LPTIM4 */
2517 #if defined(LPTIM5)
2518       case LPTIM5_BASE:
2519         __HAL_RCC_LPTIM5_CONFIG(tmpclksource);
2520         break;
2521 #endif /* LPTIM5 */
2522       default:
2523         break;
2524     }
2525   }
2526 
2527   /* Restore configuration registers (LPTIM should be disabled first) */
2528   hlptim->Instance->CR &= ~(LPTIM_CR_ENABLE);
2529   hlptim->Instance->IER = tmpIER;
2530   hlptim->Instance->CFGR = tmpCFGR;
2531   hlptim->Instance->CFGR2 = tmpCFGR2;
2532 
2533   /* Exit critical section: restore previous priority mask */
2534   __set_PRIMASK(primask_bit);
2535 }
2536 /**
2537   * @}
2538   */
2539 #endif /* LPTIM1 || LPTIM2 ||  LPTIM3 || LPTIM4 || LPTIM5 */
2540 
2541 #endif /* HAL_LPTIM_MODULE_ENABLED */
2542 /**
2543   * @}
2544   */
2545 
2546 /**
2547   * @}
2548   */