STM32L4xx_HAL_Driver  1.14.0
stm32l4xx_ll_lptim.c
Go to the documentation of this file.
1 
19 #if defined(USE_FULL_LL_DRIVER)
20 
21 /* Includes ------------------------------------------------------------------*/
22 #include "stm32l4xx_ll_lptim.h"
23 #include "stm32l4xx_ll_bus.h"
24 #include "stm32l4xx_ll_rcc.h"
25 
26 
27 #ifdef USE_FULL_ASSERT
28 #include "stm32_assert.h"
29 #else
30 #define assert_param(expr) ((void)0U)
31 #endif
32 
37 #if defined (LPTIM1) || defined (LPTIM2)
38 
43 /* Private types -------------------------------------------------------------*/
44 /* Private variables ---------------------------------------------------------*/
45 /* Private constants ---------------------------------------------------------*/
46 /* Private macros ------------------------------------------------------------*/
50 #define IS_LL_LPTIM_CLOCK_SOURCE(__VALUE__) (((__VALUE__) == LL_LPTIM_CLK_SOURCE_INTERNAL) \
51  || ((__VALUE__) == LL_LPTIM_CLK_SOURCE_EXTERNAL))
52 
53 #define IS_LL_LPTIM_CLOCK_PRESCALER(__VALUE__) (((__VALUE__) == LL_LPTIM_PRESCALER_DIV1) \
54  || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV2) \
55  || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV4) \
56  || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV8) \
57  || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV16) \
58  || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV32) \
59  || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV64) \
60  || ((__VALUE__) == LL_LPTIM_PRESCALER_DIV128))
61 
62 #define IS_LL_LPTIM_WAVEFORM(__VALUE__) (((__VALUE__) == LL_LPTIM_OUTPUT_WAVEFORM_PWM) \
63  || ((__VALUE__) == LL_LPTIM_OUTPUT_WAVEFORM_SETONCE))
64 
65 #define IS_LL_LPTIM_OUTPUT_POLARITY(__VALUE__) (((__VALUE__) == LL_LPTIM_OUTPUT_POLARITY_REGULAR) \
66  || ((__VALUE__) == LL_LPTIM_OUTPUT_POLARITY_INVERSE))
67 
72 /* Private function prototypes -----------------------------------------------*/
73 /* Private functions ---------------------------------------------------------*/
80 /* Exported functions --------------------------------------------------------*/
96 ErrorStatus LL_LPTIM_DeInit(LPTIM_TypeDef *LPTIMx)
97 {
98  ErrorStatus result = SUCCESS;
99 
100  /* Check the parameters */
101  assert_param(IS_LPTIM_INSTANCE(LPTIMx));
102 
103  if (LPTIMx == LPTIM1)
104  {
105  LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_LPTIM1);
106  LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_LPTIM1);
107  }
108 #if defined(LPTIM2)
109  else if (LPTIMx == LPTIM2)
110  {
111  LL_APB1_GRP2_ForceReset(LL_APB1_GRP2_PERIPH_LPTIM2);
112  LL_APB1_GRP2_ReleaseReset(LL_APB1_GRP2_PERIPH_LPTIM2);
113  }
114 #endif /* LPTIM2 */
115  else
116  {
117  result = ERROR;
118  }
119 
120  return result;
121 }
122 
130 {
131  /* Set the default configuration */
132  LPTIM_InitStruct->ClockSource = LL_LPTIM_CLK_SOURCE_INTERNAL;
133  LPTIM_InitStruct->Prescaler = LL_LPTIM_PRESCALER_DIV1;
134  LPTIM_InitStruct->Waveform = LL_LPTIM_OUTPUT_WAVEFORM_PWM;
135  LPTIM_InitStruct->Polarity = LL_LPTIM_OUTPUT_POLARITY_REGULAR;
136 }
137 
148 ErrorStatus LL_LPTIM_Init(LPTIM_TypeDef *LPTIMx, LL_LPTIM_InitTypeDef *LPTIM_InitStruct)
149 {
150  ErrorStatus result = SUCCESS;
151  /* Check the parameters */
152  assert_param(IS_LPTIM_INSTANCE(LPTIMx));
153  assert_param(IS_LL_LPTIM_CLOCK_SOURCE(LPTIM_InitStruct->ClockSource));
154  assert_param(IS_LL_LPTIM_CLOCK_PRESCALER(LPTIM_InitStruct->Prescaler));
155  assert_param(IS_LL_LPTIM_WAVEFORM(LPTIM_InitStruct->Waveform));
156  assert_param(IS_LL_LPTIM_OUTPUT_POLARITY(LPTIM_InitStruct->Polarity));
157 
158  /* The LPTIMx_CFGR register must only be modified when the LPTIM is disabled
159  (ENABLE bit is reset to 0).
160  */
161  if (LL_LPTIM_IsEnabled(LPTIMx) == 1UL)
162  {
163  result = ERROR;
164  }
165  else
166  {
167  /* Set CKSEL bitfield according to ClockSource value */
168  /* Set PRESC bitfield according to Prescaler value */
169  /* Set WAVE bitfield according to Waveform value */
170  /* Set WAVEPOL bitfield according to Polarity value */
171  MODIFY_REG(LPTIMx->CFGR,
172  (LPTIM_CFGR_CKSEL | LPTIM_CFGR_PRESC | LPTIM_CFGR_WAVE | LPTIM_CFGR_WAVPOL),
173  LPTIM_InitStruct->ClockSource | \
174  LPTIM_InitStruct->Prescaler | \
175  LPTIM_InitStruct->Waveform | \
176  LPTIM_InitStruct->Polarity);
177  }
178 
179  return result;
180 }
181 
199 void LL_LPTIM_Disable(LPTIM_TypeDef *LPTIMx)
200 {
201  LL_RCC_ClocksTypeDef rcc_clock;
202  uint32_t tmpclksource = 0;
203  uint32_t tmpIER;
204  uint32_t tmpCFGR;
205  uint32_t tmpCMP;
206  uint32_t tmpARR;
207  uint32_t tmpOR;
208 #if defined(LPTIM_RCR_REP)
209  uint32_t tmpRCR;
210 #endif
211 
212  /* Check the parameters */
213  assert_param(IS_LPTIM_INSTANCE(LPTIMx));
214 
215  __disable_irq();
216 
217  /********** Save LPTIM Config *********/
218  /* Save LPTIM source clock */
219  switch ((uint32_t)LPTIMx)
220  {
221  case LPTIM1_BASE:
222  tmpclksource = LL_RCC_GetLPTIMClockSource(LL_RCC_LPTIM1_CLKSOURCE);
223  break;
224 #if defined(LPTIM2)
225  case LPTIM2_BASE:
226  tmpclksource = LL_RCC_GetLPTIMClockSource(LL_RCC_LPTIM2_CLKSOURCE);
227  break;
228 #endif /* LPTIM2 */
229  default:
230  break;
231  }
232 
233  /* Save LPTIM configuration registers */
234  tmpIER = LPTIMx->IER;
235  tmpCFGR = LPTIMx->CFGR;
236  tmpCMP = LPTIMx->CMP;
237  tmpARR = LPTIMx->ARR;
238  tmpOR = LPTIMx->OR;
239 #if defined(LPTIM_RCR_REP)
240  tmpRCR = LPTIMx->RCR;
241 #endif
242 
243  /************* Reset LPTIM ************/
244  (void)LL_LPTIM_DeInit(LPTIMx);
245 
246  /********* Restore LPTIM Config *******/
247  LL_RCC_GetSystemClocksFreq(&rcc_clock);
248 
249 #if defined(LPTIM_RCR_REP)
250  if ((tmpCMP != 0UL) || (tmpARR != 0UL) || (tmpRCR != 0UL))
251 #else
252  if ((tmpCMP != 0UL) || (tmpARR != 0UL))
253 #endif
254  {
255  /* Force LPTIM source kernel clock from APB */
256  switch ((uint32_t)LPTIMx)
257  {
258  case LPTIM1_BASE:
259  LL_RCC_SetLPTIMClockSource(LL_RCC_LPTIM1_CLKSOURCE_PCLK1);
260  break;
261 #if defined(LPTIM2)
262  case LPTIM2_BASE:
263  LL_RCC_SetLPTIMClockSource(LL_RCC_LPTIM2_CLKSOURCE_PCLK1);
264  break;
265 #endif /* LPTIM2 */
266  default:
267  break;
268  }
269 
270  if (tmpCMP != 0UL)
271  {
272  /* Restore CMP and ARR registers (LPTIM should be enabled first) */
273  LPTIMx->CR |= LPTIM_CR_ENABLE;
274  LPTIMx->CMP = tmpCMP;
275 
276  /* Polling on CMP write ok status after above restore operation */
277  do
278  {
279  rcc_clock.SYSCLK_Frequency--; /* Used for timeout */
280  } while (((LL_LPTIM_IsActiveFlag_CMPOK(LPTIMx) != 1UL)) && ((rcc_clock.SYSCLK_Frequency) > 0UL));
281 
282  LL_LPTIM_ClearFlag_CMPOK(LPTIMx);
283  }
284 
285  if (tmpARR != 0UL)
286  {
287  LPTIMx->CR |= LPTIM_CR_ENABLE;
288  LPTIMx->ARR = tmpARR;
289 
290  LL_RCC_GetSystemClocksFreq(&rcc_clock);
291  /* Polling on ARR write ok status after above restore operation */
292  do
293  {
294  rcc_clock.SYSCLK_Frequency--; /* Used for timeout */
295  } while (((LL_LPTIM_IsActiveFlag_ARROK(LPTIMx) != 1UL)) && ((rcc_clock.SYSCLK_Frequency) > 0UL));
296 
297  LL_LPTIM_ClearFlag_ARROK(LPTIMx);
298  }
299 
300 #if defined(LPTIM_RCR_REP)
301  if (tmpRCR != 0UL)
302  {
303  LPTIMx->CR |= LPTIM_CR_ENABLE;
304  LPTIMx->RCR = tmpRCR;
305 
306  LL_RCC_GetSystemClocksFreq(&rcc_clock);
307  /* Polling on RCR write ok status after above restore operation */
308  do
309  {
310  rcc_clock.SYSCLK_Frequency--; /* Used for timeout */
311  } while (((LL_LPTIM_IsActiveFlag_REPOK(LPTIMx) != 1UL)) && ((rcc_clock.SYSCLK_Frequency) > 0UL));
312 
313  LL_LPTIM_ClearFlag_REPOK(LPTIMx);
314  }
315 #endif
316 
317  /* Restore LPTIM source kernel clock */
318  LL_RCC_SetLPTIMClockSource(tmpclksource);
319  }
320 
321  /* Restore configuration registers (LPTIM should be disabled first) */
322  LPTIMx->CR &= ~(LPTIM_CR_ENABLE);
323  LPTIMx->IER = tmpIER;
324  LPTIMx->CFGR = tmpCFGR;
325  LPTIMx->OR = tmpOR;
326 
327  __enable_irq();
328 }
329 
334 #endif /* LPTIM1 || LPTIM2 */
335 
340 #endif /* USE_FULL_LL_DRIVER */
341 
342 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
__STATIC_INLINE void LL_LPTIM_ClearFlag_ARROK(LPTIM_TypeDef *LPTIMx)
Clear the autoreload register update interrupt flag (ARROKCF). ICR ARROKCF LL_LPTIM_ClearFlag_ARROK...
LPTIM Init structure definition.
RCC Clocks Frequency Structure.
__STATIC_INLINE void LL_LPTIM_ClearFlag_CMPOK(LPTIM_TypeDef *LPTIMx)
Clear the compare register update interrupt flag (CMPOKCF). ICR CMPOKCF LL_LPTIM_ClearFlag_CMPOK.
Header file of LPTIM LL module.
void LL_LPTIM_Disable(LPTIM_TypeDef *LPTIMx)
Disable the LPTIM instance CR ENABLE LL_LPTIM_Disable.
Header file of RCC LL module.
__STATIC_INLINE void LL_RCC_SetLPTIMClockSource(uint32_t LPTIMxSource)
Configure LPTIMx clock source CCIPR LPTIMxSEL LL_RCC_SetLPTIMClockSource.
__STATIC_INLINE uint32_t LL_LPTIM_IsEnabled(LPTIM_TypeDef *LPTIMx)
Indicates whether the LPTIM instance is enabled. CR ENABLE LL_LPTIM_IsEnabled.
__STATIC_INLINE void LL_APB1_GRP1_ReleaseReset(uint32_t Periphs)
Release APB1 peripherals reset. APB1RSTR1 TIM2RST LL_APB1_GRP1_ReleaseReset APB1RSTR1 TIM3RST LL_AP...
__STATIC_INLINE void LL_APB1_GRP2_ForceReset(uint32_t Periphs)
Force APB1 peripherals reset. APB1RSTR2 LPUART1RST LL_APB1_GRP2_ForceReset APB1RSTR2 I2C4RST LL_APB...
ErrorStatus LL_LPTIM_Init(LPTIM_TypeDef *LPTIMx, LL_LPTIM_InitTypeDef *LPTIM_InitStruct)
Configure the LPTIMx peripheral according to the specified parameters.
void LL_RCC_GetSystemClocksFreq(LL_RCC_ClocksTypeDef *RCC_Clocks)
Return the frequencies of different on chip clocks; System, AHB, APB1 and APB2 buses clocks...
__STATIC_INLINE void LL_APB1_GRP2_ReleaseReset(uint32_t Periphs)
Release APB1 peripherals reset. APB1RSTR2 LPUART1RST LL_APB1_GRP2_ReleaseReset APB1RSTR2 I2C4RST LL...
__STATIC_INLINE uint32_t LL_LPTIM_IsActiveFlag_CMPOK(LPTIM_TypeDef *LPTIMx)
Informs application whether the APB bus write operation to the LPTIMx_CMP register has been successfu...
void LL_LPTIM_StructInit(LL_LPTIM_InitTypeDef *LPTIM_InitStruct)
Set each fields of the LPTIM_InitStruct structure to its default value.
__STATIC_INLINE void LL_LPTIM_ClearFlag_REPOK(LPTIM_TypeDef *LPTIMx)
Clear the repetition register update interrupt flag (REPOKCF). ICR REPOKCF LL_LPTIM_ClearFlag_REPOK...
__STATIC_INLINE uint32_t LL_LPTIM_IsActiveFlag_ARROK(LPTIM_TypeDef *LPTIMx)
Informs application whether the APB bus write operation to the LPTIMx_ARR register has been successfu...
ErrorStatus LL_LPTIM_DeInit(LPTIM_TypeDef *LPTIMx)
Set LPTIMx registers to their reset values.
Header file of BUS LL module.
__STATIC_INLINE void LL_APB1_GRP1_ForceReset(uint32_t Periphs)
Force APB1 peripherals reset. APB1RSTR1 TIM2RST LL_APB1_GRP1_ForceReset APB1RSTR1 TIM3RST LL_APB1_G...
MODIFY_REG(hrtc->Instance->CR, RTC_CR_WUCKSEL,(uint32_t) WakeUpClock)
__STATIC_INLINE uint32_t LL_LPTIM_IsActiveFlag_REPOK(LPTIM_TypeDef *LPTIMx)
Informs application whether the APB bus write operation to the LPTIMx_RCR register has been successfu...
__STATIC_INLINE uint32_t LL_RCC_GetLPTIMClockSource(uint32_t LPTIMx)
Get LPTIMx clock source CCIPR LPTIMxSEL LL_RCC_GetLPTIMClockSource.
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))