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