STM32H735xx HAL User Manual
stm32h7xx_ll_lptim.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32h7xx_ll_lptim.c
00004   * @author  MCD Application Team
00005   * @brief   LPTIM LL module driver.
00006   ******************************************************************************
00007   * @attention
00008   *
00009   * Copyright (c) 2017 STMicroelectronics.
00010   * All rights reserved.
00011   *
00012   * This software is licensed under terms that can be found in the LICENSE file
00013   * in the root directory of this software component.
00014   * If no LICENSE file comes with this software, it is provided AS-IS.
00015   *
00016   ******************************************************************************
00017   */
00018 #if defined(USE_FULL_LL_DRIVER)
00019 
00020 /* Includes ------------------------------------------------------------------*/
00021 #include "stm32h7xx_ll_lptim.h"
00022 #include "stm32h7xx_ll_bus.h"
00023 #include "stm32h7xx_ll_rcc.h"
00024 
00025 
00026 #ifdef  USE_FULL_ASSERT
00027 #include "stm32_assert.h"
00028 #else
00029 #define assert_param(expr) ((void)0U)
00030 #endif /* USE_FULL_ASSERT */
00031 
00032 /** @addtogroup STM32H7xx_LL_Driver
00033   * @{
00034   */
00035 
00036 #if defined (LPTIM1) || defined (LPTIM2) || defined (LPTIM3) || defined (LPTIM4) || defined (LPTIM5)
00037 
00038 /** @addtogroup LPTIM_LL
00039   * @{
00040   */
00041 
00042 /* Private types -------------------------------------------------------------*/
00043 /* Private variables ---------------------------------------------------------*/
00044 /* Private constants ---------------------------------------------------------*/
00045 /* Private macros ------------------------------------------------------------*/
00046 /** @addtogroup LPTIM_LL_Private_Macros
00047   * @{
00048   */
00049 #define IS_LL_LPTIM_CLOCK_SOURCE(__VALUE__) (((__VALUE__) == LL_LPTIM_CLK_SOURCE_INTERNAL) \
00050                                              || ((__VALUE__) == LL_LPTIM_CLK_SOURCE_EXTERNAL))
00051 
00052 #define IS_LL_LPTIM_CLOCK_PRESCALER(__VALUE__) (((__VALUE__) == LL_LPTIM_PRESCALER_DIV1)   \
00053                                                 || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV2)   \
00054                                                 || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV4)   \
00055                                                 || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV8)   \
00056                                                 || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV16)  \
00057                                                 || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV32)  \
00058                                                 || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV64)  \
00059                                                 || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV128))
00060 
00061 #define IS_LL_LPTIM_WAVEFORM(__VALUE__) (((__VALUE__) == LL_LPTIM_OUTPUT_WAVEFORM_PWM) \
00062                                          || ((__VALUE__) == LL_LPTIM_OUTPUT_WAVEFORM_SETONCE))
00063 
00064 #define IS_LL_LPTIM_OUTPUT_POLARITY(__VALUE__) (((__VALUE__) == LL_LPTIM_OUTPUT_POLARITY_REGULAR) \
00065                                                 || ((__VALUE__) == LL_LPTIM_OUTPUT_POLARITY_INVERSE))
00066 /**
00067   * @}
00068   */
00069 
00070 
00071 /* Private function prototypes -----------------------------------------------*/
00072 /* Private functions ---------------------------------------------------------*/
00073 /** @defgroup LPTIM_Private_Functions LPTIM Private Functions
00074   * @{
00075   */
00076 /**
00077   * @}
00078   */
00079 /* Exported functions --------------------------------------------------------*/
00080 /** @addtogroup LPTIM_LL_Exported_Functions
00081   * @{
00082   */
00083 
00084 /** @addtogroup LPTIM_LL_EF_Init
00085   * @{
00086   */
00087 
00088 /**
00089   * @brief  Set LPTIMx registers to their reset values.
00090   * @param  LPTIMx LP Timer instance
00091   * @retval An ErrorStatus enumeration value:
00092   *          - SUCCESS: LPTIMx registers are de-initialized
00093   *          - ERROR: invalid LPTIMx instance
00094   */
00095 ErrorStatus LL_LPTIM_DeInit(LPTIM_TypeDef *LPTIMx)
00096 {
00097   ErrorStatus result = SUCCESS;
00098 
00099   /* Check the parameters */
00100   assert_param(IS_LPTIM_INSTANCE(LPTIMx));
00101 
00102   if (LPTIMx == LPTIM1)
00103   {
00104     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_LPTIM1);
00105     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_LPTIM1);
00106   }
00107   else if (LPTIMx == LPTIM2)
00108   {
00109     LL_APB4_GRP1_ForceReset(LL_APB4_GRP1_PERIPH_LPTIM2);
00110     LL_APB4_GRP1_ReleaseReset(LL_APB4_GRP1_PERIPH_LPTIM2);
00111   }
00112 #if defined(LPTIM3)
00113   else if (LPTIMx == LPTIM3)
00114   {
00115     LL_APB4_GRP1_ForceReset(LL_APB4_GRP1_PERIPH_LPTIM3);
00116     LL_APB4_GRP1_ReleaseReset(LL_APB4_GRP1_PERIPH_LPTIM3);
00117   }
00118 #endif /* LPTIM3 */
00119 #if defined(LPTIM4)
00120   else if (LPTIMx == LPTIM4)
00121   {
00122     LL_APB4_GRP1_ForceReset(LL_APB4_GRP1_PERIPH_LPTIM4);
00123     LL_APB4_GRP1_ReleaseReset(LL_APB4_GRP1_PERIPH_LPTIM4);
00124   }
00125 #endif /* LPTIM4 */
00126 #if defined(LPTIM5)
00127   else if (LPTIMx == LPTIM5)
00128   {
00129     LL_APB4_GRP1_ForceReset(LL_APB4_GRP1_PERIPH_LPTIM5);
00130     LL_APB4_GRP1_ReleaseReset(LL_APB4_GRP1_PERIPH_LPTIM5);
00131   }
00132 #endif /* LPTIM5 */
00133   else
00134   {
00135     result = ERROR;
00136   }
00137 
00138   return result;
00139 }
00140 
00141 /**
00142   * @brief  Set each fields of the LPTIM_InitStruct structure to its default
00143   *         value.
00144   * @param  LPTIM_InitStruct pointer to a @ref LL_LPTIM_InitTypeDef structure
00145   * @retval None
00146   */
00147 void LL_LPTIM_StructInit(LL_LPTIM_InitTypeDef *LPTIM_InitStruct)
00148 {
00149   /* Set the default configuration */
00150   LPTIM_InitStruct->ClockSource = LL_LPTIM_CLK_SOURCE_INTERNAL;
00151   LPTIM_InitStruct->Prescaler   = LL_LPTIM_PRESCALER_DIV1;
00152   LPTIM_InitStruct->Waveform    = LL_LPTIM_OUTPUT_WAVEFORM_PWM;
00153   LPTIM_InitStruct->Polarity    = LL_LPTIM_OUTPUT_POLARITY_REGULAR;
00154 }
00155 
00156 /**
00157   * @brief  Configure the LPTIMx peripheral according to the specified parameters.
00158   * @note LL_LPTIM_Init can only be called when the LPTIM instance is disabled.
00159   * @note LPTIMx can be disabled using unitary function @ref LL_LPTIM_Disable().
00160   * @param  LPTIMx LP Timer Instance
00161   * @param  LPTIM_InitStruct pointer to a @ref LL_LPTIM_InitTypeDef structure
00162   * @retval An ErrorStatus enumeration value:
00163   *          - SUCCESS: LPTIMx instance has been initialized
00164   *          - ERROR: LPTIMx instance hasn't been initialized
00165   */
00166 ErrorStatus LL_LPTIM_Init(LPTIM_TypeDef *LPTIMx, LL_LPTIM_InitTypeDef *LPTIM_InitStruct)
00167 {
00168   ErrorStatus result = SUCCESS;
00169   /* Check the parameters */
00170   assert_param(IS_LPTIM_INSTANCE(LPTIMx));
00171   assert_param(IS_LL_LPTIM_CLOCK_SOURCE(LPTIM_InitStruct->ClockSource));
00172   assert_param(IS_LL_LPTIM_CLOCK_PRESCALER(LPTIM_InitStruct->Prescaler));
00173   assert_param(IS_LL_LPTIM_WAVEFORM(LPTIM_InitStruct->Waveform));
00174   assert_param(IS_LL_LPTIM_OUTPUT_POLARITY(LPTIM_InitStruct->Polarity));
00175 
00176   /* The LPTIMx_CFGR register must only be modified when the LPTIM is disabled
00177      (ENABLE bit is reset to 0).
00178   */
00179   if (LL_LPTIM_IsEnabled(LPTIMx) == 1UL)
00180   {
00181     result = ERROR;
00182   }
00183   else
00184   {
00185     /* Set CKSEL bitfield according to ClockSource value */
00186     /* Set PRESC bitfield according to Prescaler value */
00187     /* Set WAVE bitfield according to Waveform value */
00188     /* Set WAVEPOL bitfield according to Polarity value */
00189     MODIFY_REG(LPTIMx->CFGR,
00190                (LPTIM_CFGR_CKSEL | LPTIM_CFGR_PRESC | LPTIM_CFGR_WAVE | LPTIM_CFGR_WAVPOL),
00191                LPTIM_InitStruct->ClockSource | \
00192                LPTIM_InitStruct->Prescaler | \
00193                LPTIM_InitStruct->Waveform | \
00194                LPTIM_InitStruct->Polarity);
00195   }
00196 
00197   return result;
00198 }
00199 
00200 /**
00201   * @brief  Disable the LPTIM instance
00202   * @rmtoll CR           ENABLE        LL_LPTIM_Disable
00203   * @param  LPTIMx Low-Power Timer instance
00204   * @note   The following sequence is required to solve LPTIM disable HW limitation.
00205   *         Please check Errata Sheet ES0335 for more details under "MCU may remain
00206   *         stuck in LPTIM interrupt when entering Stop mode" section.
00207   * @retval None
00208   */
00209 void LL_LPTIM_Disable(LPTIM_TypeDef *LPTIMx)
00210 {
00211   LL_RCC_ClocksTypeDef rcc_clock;
00212   uint32_t tmpclksource = 0;
00213   uint32_t tmpIER;
00214   uint32_t tmpCFGR;
00215   uint32_t tmpCMP;
00216   uint32_t tmpARR;
00217   uint32_t primask_bit;
00218   uint32_t tmpCFGR2;
00219 
00220   /* Check the parameters */
00221   assert_param(IS_LPTIM_INSTANCE(LPTIMx));
00222 
00223   /* Enter critical section */
00224   primask_bit = __get_PRIMASK();
00225   __set_PRIMASK(1) ;
00226 
00227   /********** Save LPTIM Config *********/
00228   /* Save LPTIM source clock */
00229   switch ((uint32_t)LPTIMx)
00230   {
00231     case LPTIM1_BASE:
00232       tmpclksource = LL_RCC_GetLPTIMClockSource(LL_RCC_LPTIM1_CLKSOURCE);
00233       break;
00234     case LPTIM2_BASE:
00235       tmpclksource = LL_RCC_GetLPTIMClockSource(LL_RCC_LPTIM2_CLKSOURCE);
00236       break;
00237 #if defined(LPTIM3)&&defined(LPTIM4)&&defined(LPTIM5)
00238     case LPTIM3_BASE:
00239     case LPTIM4_BASE:
00240     case LPTIM5_BASE:
00241       tmpclksource = LL_RCC_GetLPTIMClockSource(LL_RCC_LPTIM345_CLKSOURCE);
00242       break;
00243 #elif defined(LPTIM3)
00244     case LPTIM3_BASE:
00245       tmpclksource = LL_RCC_GetLPTIMClockSource(LL_RCC_LPTIM3_CLKSOURCE);
00246       break;
00247 #endif /* LPTIM3 && LPTIM4 && LPTIM5 */
00248     default:
00249       break;
00250   }
00251 
00252   /* Save LPTIM configuration registers */
00253   tmpIER = LPTIMx->IER;
00254   tmpCFGR = LPTIMx->CFGR;
00255   tmpCMP = LPTIMx->CMP;
00256   tmpARR = LPTIMx->ARR;
00257   tmpCFGR2 = LPTIMx->CFGR2;
00258 
00259   /************* Reset LPTIM ************/
00260   (void)LL_LPTIM_DeInit(LPTIMx);
00261 
00262   /********* Restore LPTIM Config *******/
00263   LL_RCC_GetSystemClocksFreq(&rcc_clock);
00264 
00265   if ((tmpCMP != 0UL) || (tmpARR != 0UL))
00266   {
00267     /* Force LPTIM source kernel clock from APB */
00268     switch ((uint32_t)LPTIMx)
00269     {
00270       case LPTIM1_BASE:
00271         LL_RCC_SetLPTIMClockSource(LL_RCC_LPTIM1_CLKSOURCE_PCLK1);
00272         break;
00273       case LPTIM2_BASE:
00274         LL_RCC_SetLPTIMClockSource(LL_RCC_LPTIM2_CLKSOURCE_PCLK4);
00275         break;
00276 #if defined(LPTIM3)&&defined(LPTIM4)&&defined(LPTIM5)
00277       case LPTIM3_BASE:
00278       case LPTIM4_BASE:
00279       case LPTIM5_BASE:
00280         LL_RCC_SetLPTIMClockSource(LL_RCC_LPTIM345_CLKSOURCE_PCLK4);
00281         break;
00282 #elif defined(LPTIM3)
00283       case LPTIM3_BASE:
00284         LL_RCC_SetLPTIMClockSource(LL_RCC_LPTIM3_CLKSOURCE_PCLK4);
00285         break;
00286 #endif /* LPTIM3 && LPTIM4 && LPTIM5*/
00287       default:
00288         break;
00289     }
00290 
00291     if (tmpCMP != 0UL)
00292     {
00293       /* Restore CMP and ARR registers (LPTIM should be enabled first) */
00294       LPTIMx->CR |= LPTIM_CR_ENABLE;
00295       LPTIMx->CMP = tmpCMP;
00296 
00297       /* Polling on CMP write ok status after above restore operation */
00298       do
00299       {
00300         rcc_clock.SYSCLK_Frequency--; /* Used for timeout */
00301       } while (((LL_LPTIM_IsActiveFlag_CMPOK(LPTIMx) != 1UL)) && ((rcc_clock.SYSCLK_Frequency) > 0UL));
00302 
00303       LL_LPTIM_ClearFlag_CMPOK(LPTIMx);
00304     }
00305 
00306     if (tmpARR != 0UL)
00307     {
00308       LPTIMx->CR |= LPTIM_CR_ENABLE;
00309       LPTIMx->ARR = tmpARR;
00310 
00311       LL_RCC_GetSystemClocksFreq(&rcc_clock);
00312       /* Polling on ARR write ok status after above restore operation */
00313       do
00314       {
00315         rcc_clock.SYSCLK_Frequency--; /* Used for timeout */
00316       }
00317       while (((LL_LPTIM_IsActiveFlag_ARROK(LPTIMx) != 1UL)) && ((rcc_clock.SYSCLK_Frequency) > 0UL));
00318 
00319       LL_LPTIM_ClearFlag_ARROK(LPTIMx);
00320     }
00321 
00322 
00323     /* Restore LPTIM source kernel clock */
00324     LL_RCC_SetLPTIMClockSource(tmpclksource);
00325   }
00326 
00327   /* Restore configuration registers (LPTIM should be disabled first) */
00328   LPTIMx->CR &= ~(LPTIM_CR_ENABLE);
00329   LPTIMx->IER = tmpIER;
00330   LPTIMx->CFGR = tmpCFGR;
00331   LPTIMx->CFGR2 = tmpCFGR2;
00332 
00333   /* Exit critical section: restore previous priority mask */
00334   __set_PRIMASK(primask_bit);
00335 }
00336 
00337 /**
00338   * @}
00339   */
00340 
00341 /**
00342   * @}
00343   */
00344 
00345 /**
00346   * @}
00347   */
00348 
00349 #endif /* LPTIM1 || LPTIM2 ||  LPTIM3 || LPTIM4 || LPTIM5 */
00350 
00351 /**
00352   * @}
00353   */
00354 
00355 #endif /* USE_FULL_LL_DRIVER */