2 ******************************************************************************
3 * @file stm32f4xx_lptim.c
4 * @author MCD Application Team
7 * @brief This file provides firmware functions to manage the following
8 * functionalities of the Low Power Timer (LPT) peripheral:
9 * + Initialization functions.
10 * + Configuration functions.
11 * + Interrupts and flags management functions.
15 ================================================================================
16 ##### How to use this driver #####
17 ================================================================================
21 - Configure the clock source, the prescaler, the waveform shape and
22 the output polarity by filling the "LPTIM_InitTypeDef" structure and
24 - If the ULPTIM source is selected as clock source, configure the digital
25 Glitch filter by setting the number of consecutive samples
26 to be detected by using LPTIM_ConfigClockGlitchFilter.
27 - To select a software start use LPTIM_SelectSoftwareStart.
28 - To select an external trigger for the start of the counter, configure
29 the source and its active edge polarity by calling
30 LPTIM_ConfigExternalTrigger. Configure the Digital Glitch filter for
31 the external triggers by setting the number of consecutive samples
32 to be detected by using LPTIM_ConfigTriggerGlitchFilter.
33 - Select the operating mode of the peripheral by using
34 LPTIM_SelectOperatingMode, 2 modes can be selected:
35 + Continuous mode: the timer is free running, the timer is started
36 from a trigger event and never stops until the timer is disabled
37 + One shot mode: the timer is started from a trigger event and
38 stops when reaching the auto-reload value.
39 - Use LPTIM_SetAutoreloadValue to set the auto-reload value and
40 LPTIM_SetCompareValue to set the compare value.
41 - Configure the preload mode by using LPTIM_ConfigUpdate function. 2 modes
43 + The Autoreload and compare registers are updated immediately after
45 + The Autoreload and compare registers are updated at the end of
47 - Enable the peripheral by calling LPTIM_Cmd.
51 - To select the encoder feature, use the function: LPTIM_SelectEncoderMode.
52 - To select on which edge (Rising edge, falling edge or both edges)
53 the counter is incremented, use LPTIM_SelectClockPolarity.
57 - Use LPTIM_SelectCounterMode to select the counting mode. In this mode
58 the counter is incremented on each valid event on ULPTIM.
62 In this case, the trigger will reset the timer. The first trigger event
63 will start the timer, any successive trigger event will reset the counter
64 and the timer restarts.
65 - To active this feature use LPTIM_TimoutCmd.
67 Interrupt configuration:
68 ------------------------
69 - Use LPTIM_ITConfig to configure an interruption.
70 - Call LPTIM_GetFlagStatus to get a flag status.
71 - Call LPTIM_GetITStatus to get an interrupt status.
72 - Use LPTIM_ClearFlag to clear a flag.
75 ******************************************************************************
78 * <h2><center>© COPYRIGHT 2016 STMicroelectronics</center></h2>
80 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
81 * You may not use this file except in compliance with the License.
82 * You may obtain a copy of the License at:
84 * http://www.st.com/software_license_agreement_liberty_v2
86 * Unless required by applicable law or agreed to in writing, software
87 * distributed under the License is distributed on an "AS IS" BASIS,
88 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
89 * See the License for the specific language governing permissions and
90 * limitations under the License.
92 ******************************************************************************
95 /* Includes ------------------------------------------------------------------*/
96 #include "stm32f4xx_lptim.h"
99 /** @addtogroup STM32F4xx_StdPeriph_Driver
104 * @brief LPTIM driver modules
107 #if defined(STM32F410xx)
108 /* External variables --------------------------------------------------------*/
109 /* Private typedef -----------------------------------------------------------*/
110 /* Private defines -----------------------------------------------------------*/
112 #define CFGR_INIT_CLEAR_MASK ((uint32_t) 0xFFCFF1FE)
113 #define CFGR_TRIG_AND_POL_CLEAR_MASK ((uint32_t) 0xFFF91FFF)
114 /* Private macros ------------------------------------------------------------*/
115 /* Private variables ---------------------------------------------------------*/
116 /* Private function prototypes -----------------------------------------------*/
117 /* Private functions ---------------------------------------------------------*/
119 /** @defgroup LPTIM_Private_Functions
123 /** @defgroup LPTIM_Group1 Initialization functions
124 * @brief Initialization functions
127 ===============================================================================
128 Initialization functions
129 ===============================================================================
130 This section provides functions allowing to:
131 - Deinitialize the LPTimer
132 - Initialize the Clock source, the Prescaler, the Ouput Waveform shape and Polarity
133 - Initialize the member of LPTIM_InitStruct structer with default value
140 * @brief Deinitializes the LPTIMx peripheral registers to their default reset values.
141 * @param LPTIMx: where x can be 1.
145 void LPTIM_DeInit(LPTIM_TypeDef
* LPTIMx
)
147 /* Check the parameters */
148 assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx
));
150 /* Deinitializes the LPTIM1 peripheral */
153 RCC_APB1PeriphResetCmd(RCC_APB1Periph_LPTIM1
, ENABLE
);
154 RCC_APB1PeriphResetCmd(RCC_APB1Periph_LPTIM1
, DISABLE
);
159 * @brief Initializes the LPTIMx peripheral according to the specified parameters
160 * in the LPTIM_InitStruct.
161 * @param LPTIMx: where x can be 1.
162 * @param LPTIM_InitStruct: pointer to an LPTIM_InitTypeDef structure that contains
163 * the configuration information for the specified LPTIM peripheral.
166 * @note It is mandatory to disable the peripheral to use this function.
168 void LPTIM_Init(LPTIM_TypeDef
* LPTIMx
, LPTIM_InitTypeDef
* LPTIM_InitStruct
)
170 uint32_t tmpreg1
= 0;
172 /* Check the parameters */
173 assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx
));
174 assert_param(IS_LPTIM_CLOCK_SOURCE(LPTIM_InitStruct
->LPTIM_ClockSource
));
175 assert_param(IS_LPTIM_CLOCK_PRESCALER(LPTIM_InitStruct
->LPTIM_Prescaler
));
176 assert_param(IS_LPTIM_WAVEFORM(LPTIM_InitStruct
->LPTIM_Waveform
));
177 assert_param(IS_LPTIM_OUTPUT_POLARITY(LPTIM_InitStruct
->LPTIM_OutputPolarity
));
179 /* Get the LPTIMx CFGR value */
180 tmpreg1
= LPTIMx
->CFGR
;
182 /* Clear CKSEL, PRESC, WAVE and WAVEPOL bits */
183 tmpreg1
&= CFGR_INIT_CLEAR_MASK
;
185 /* Set or Reset CKSEL bit according to LPTIM_ClockSource value */
186 /* Set or Reset PRESC bits according to LPTIM_Prescaler value */
187 /* Set or Reset WAVE bit according to LPTIM_Waveform value */
188 /* Set or Reset WAVEPOL bit according to LPTIM_OutputPolarity value */
189 tmpreg1
|= (LPTIM_InitStruct
->LPTIM_ClockSource
| LPTIM_InitStruct
->LPTIM_Prescaler
190 |LPTIM_InitStruct
->LPTIM_Waveform
| LPTIM_InitStruct
->LPTIM_OutputPolarity
);
192 /* Write to LPTIMx CFGR */
193 LPTIMx
->CFGR
= tmpreg1
;
197 * @brief Fills each LPTIM_InitStruct member with its default value.
198 * @param LPTIM_InitStruct : pointer to a LPTIM_InitTypeDef structure which will be initialized.
201 void LPTIM_StructInit(LPTIM_InitTypeDef
* LPTIM_InitStruct
)
203 /* APB Clock/Low Power oscillators is selected as default Clock source*/
204 LPTIM_InitStruct
->LPTIM_ClockSource
= LPTIM_ClockSource_APBClock_LPosc
;
206 /* High Polarity is selected as default polarity */
207 LPTIM_InitStruct
->LPTIM_OutputPolarity
= LPTIM_OutputPolarity_High
;
209 /* DIV=1 is selected as default prescaler */
210 LPTIM_InitStruct
->LPTIM_Prescaler
= LPTIM_Prescaler_DIV1
;
212 /* PWM/One pulse mode is selected as default Waveform shape */
213 LPTIM_InitStruct
->LPTIM_Waveform
= LPTIM_Waveform_PWM_OnePulse
;
220 /** @defgroup LPTIM_Group2 Configuration functions
221 * @brief Configuration functions
224 ===============================================================================
225 Configuration functions
226 ===============================================================================
227 This section provides functions allowing to configure the Low Power Timer:
228 - Select the Clock source.
229 - Configure the Glitch filter for the external clock and the external clock.
230 - Configure the prescaler of the counter.
231 - Select the Trigger source of the counter.
232 - Configure the operating mode (Single or Continuous mode).
233 - Select the Waveform shape (PWM/One Pulse or Set once) and polarity.
234 - Enable or disable the Encoder mode and the Timeout function.
235 - Write on the Autoreload and the Compare registers and configure the
237 - Get the Counter value.
238 - Enable or disable the peripheral.
245 * @brief Enables or disables the specified LPTIM peripheral.
246 * @param LPTIMx: where x can be 1.
247 * @param NewState: new state of the LPTIMx peripheral.
248 * This parameter can be: ENABLE or DISABLE.
251 void LPTIM_Cmd(LPTIM_TypeDef
* LPTIMx
, FunctionalState NewState
)
253 /* Check the parameters */
254 assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx
));
255 assert_param(IS_FUNCTIONAL_STATE(NewState
));
257 if(NewState
!= DISABLE
)
259 /* Set the ENABLE bit */
260 LPTIMx
->CR
|= LPTIM_CR_ENABLE
;
264 /* Reset the ENABLE bit */
265 LPTIMx
->CR
&= ~(LPTIM_CR_ENABLE
);
270 * @brief Selects the Clock source of the LPTIM counter.
271 * @param LPTIMx: where x can be 1.
272 * @param LPTIM_ClockSource: the selected clock source.
273 * This parameter can be:
274 * @arg LPTIM_ClockSource_APBClock_LPosc : APB clock/LP oscillators selected
275 * @arg LPTIM_ClockSource_ULPTIM: ULPTIM (external input) selected
278 * @note It is mandatory to disable the peripheral to use this function.
280 void LPTIM_SelectClockSource(LPTIM_TypeDef
* LPTIMx
, uint32_t LPTIM_ClockSource
)
282 /* Check the parameters */
283 assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx
));
284 assert_param(IS_LPTIM_CLOCK_SOURCE(LPTIM_ClockSource
));
286 /* Clear the CKSEL bit */
287 LPTIMx
->CFGR
&= ~(LPTIM_CFGR_CKSEL
);
289 /* Set or Reset the CKSEL bit */
290 LPTIMx
->CFGR
|= LPTIM_ClockSource
;
294 * @brief Configures the polarity of the edge to be used to count
295 * if the ULPTIM input is selected.
296 * @param LPTIMx: where x can be 1.
297 * @param LPTIM_ClockPolarity: the selected clock polarity.
298 * This parameter can be:
299 * @arg LPTIM_ClockPolarity_RisingEdge : Counter Clock = LPTIM Clock / 1
300 * @arg LPTIM_ClockPolarity_FallingEdge : Counter Clock = LPTIM Clock / 2
301 * @arg LPTIM_ClockPolarity_BothEdges : Counter Clock = LPTIM Clock / 4
304 * @note It is mandatory to disable the peripheral to use this function.
306 void LPTIM_SelectULPTIMClockPolarity(LPTIM_TypeDef
* LPTIMx
, uint32_t LPTIM_ClockPolarity
)
308 uint32_t tmpreg1
= 0;
310 /* Check the parameters */
311 assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx
));
312 assert_param(IS_LPTIM_CLOCK_POLARITY(LPTIM_ClockPolarity
));
314 /* Get the LPTIMx CFGR value */
315 tmpreg1
= LPTIMx
->CFGR
;
317 /* Clear the CKPOL bits */
318 tmpreg1
&= ~(LPTIM_CFGR_CKPOL
);
320 /* Set or Reset the PRESC bits */
321 tmpreg1
|= LPTIM_ClockPolarity
;
323 /* Write to LPTIMx CFGR */
324 LPTIMx
->CFGR
= tmpreg1
;
328 * @brief Configures the Clock Prescaler.
329 * @param LPTIMx: where x can be 1.
330 * @param LPTIM_Prescaler: the selected clock prescaler.
331 * This parameter can be:
332 * @arg LPTIM_Prescaler_DIV1 : Counter Clock = LPTIM Clock / 1
333 * @arg LPTIM_Prescaler_DIV2 : Counter Clock = LPTIM Clock / 2
334 * @arg LPTIM_Prescaler_DIV4 : Counter Clock = LPTIM Clock / 4
335 * @arg LPTIM_Prescaler_DIV8 : Counter Clock = LPTIM Clock / 8
336 * @arg LPTIM_Prescaler_DIV16 : Counter Clock = LPTIM Clock / 16
337 * @arg LPTIM_Prescaler_DIV32 : Counter Clock = LPTIM Clock / 32
338 * @arg LPTIM_Prescaler_DIV64 : Counter Clock = LPTIM Clock / 64
339 * @arg LPTIM_Prescaler_DIV128 : Counter Clock = LPTIM Clock / 128
342 * @note It is mandatory to disable the peripheral to use this function.
344 void LPTIM_ConfigPrescaler(LPTIM_TypeDef
* LPTIMx
, uint32_t LPTIM_Prescaler
)
346 uint32_t tmpreg1
= 0;
348 /* Check the parameters */
349 assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx
));
350 assert_param(IS_LPTIM_CLOCK_PRESCALER(LPTIM_Prescaler
));
352 /* Get the LPTIMx CFGR value */
353 tmpreg1
= LPTIMx
->CFGR
;
355 /* Clear the PRESC bits */
356 tmpreg1
&= ~(LPTIM_CFGR_PRESC
);
358 /* Set or Reset the PRESC bits */
359 tmpreg1
|= LPTIM_Prescaler
;
361 /* Write to LPTIMx CFGR */
362 LPTIMx
->CFGR
= tmpreg1
;
366 * @brief Selects the trigger source for the counter and its polarity.
367 * @param LPTIMx: where x can be 1.
368 * @param LPTIM_ExtTRGSource: the selected external trigger.
369 * This parameter can be:
370 * @arg LPTIM_ExtTRGSource_Trig0 : ext_trig0
371 * @arg LPTIM_ExtTRGSource_Trig1 : ext_trig1
372 * @arg LPTIM_ExtTRGSource_Trig2 : ext_trig2
373 * @arg LPTIM_ExtTRGSource_Trig3 : ext_trig3
374 * @arg LPTIM_ExtTRGSource_Trig4 : ext_trig4
375 * @arg LPTIM_ExtTRGSource_Trig5 : ext_trig5
376 * @arg LPTIM_ExtTRGSource_Trig6 : ext_trig6
377 * @arg LPTIM_ExtTRGSource_Trig7 : ext_trig7
378 * @param LPTIM_ExtTRGPolarity: the selected external trigger.
379 * This parameter can be:
380 * @arg LPTIM_ExtTRGPolarity_RisingEdge : Rising edge polarity selected
381 * @arg LPTIM_ExtTRGPolarity_FallingEdge : Falling edge polarity selected
382 * @arg LPTIM_ExtTRGPolarity_BothEdges : Both edges polarity selected
385 * @note It is mandatory to disable the peripheral to use this function.
387 void LPTIM_ConfigExternalTrigger(LPTIM_TypeDef
* LPTIMx
, uint32_t LPTIM_ExtTRGSource
, uint32_t LPTIM_ExtTRGPolarity
)
389 uint32_t tmpreg1
= 0;
391 /* Check the parameters */
392 assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx
));
393 assert_param(IS_LPTIM_EXT_TRG_SOURCE(LPTIM_ExtTRGSource
));
394 assert_param(IS_LPTIM_EXT_TRG_POLARITY(LPTIM_ExtTRGPolarity
));
396 /* Get the LPTIMx CFGR value */
397 tmpreg1
= LPTIMx
->CFGR
;
399 /* Clear the TRIGEN and TRIGSEL bits */
400 tmpreg1
&= CFGR_TRIG_AND_POL_CLEAR_MASK
;
402 /* Set or Reset the TRIGEN and TRIGSEL bits */
403 tmpreg1
|= (LPTIM_ExtTRGSource
| LPTIM_ExtTRGPolarity
);
405 /* Write to LPTIMx CFGR */
406 LPTIMx
->CFGR
= tmpreg1
;
410 * @brief Selects a software start of the counter.
411 * @param LPTIMx: where x can be 1.
414 * @note It is mandatory to disable the peripheral to use this function.
416 void LPTIM_SelectSoftwareStart(LPTIM_TypeDef
* LPTIMx
)
418 /* Check the parameters */
419 assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx
));
421 /* Reset the TRIGEN bits to allow a software start */
422 LPTIMx
->CFGR
&= ~(LPTIM_CFGR_TRIGEN
);
426 * @brief Configures the digital filter for trigger by determining the number of consecutive
427 * samples at the specified level to detect a correct transition.
428 * @param LPTIMx: where x can be 1.
429 * @param LPTIM_TrigSampleTime: the number of samples to detect a valid transition.
430 * This parameter can be:
431 * @arg LPTIM_TrigSampleTime_DirectTransistion : Event is detected on input transitions
432 * @arg LPTIM_TrigSampleTime_2Transistions : Event is detected after 2 consecutive samples at the active level
433 * @arg LPTIM_TrigSampleTime_4Transistions : Event is detected after 4 consecutive samples at the active level
434 * @arg LPTIM_TrigSampleTime_8Transistions : Event is detected after 8 consecutive samples at the active level
437 * @note It is mandatory to disable the peripheral to use this function.
438 * @note An auxiliary clock must be present to use this feature.
440 void LPTIM_ConfigTriggerGlitchFilter(LPTIM_TypeDef
* LPTIMx
, uint32_t LPTIM_TrigSampleTime
)
442 uint32_t tmpreg1
= 0;
444 /* Check the parameters */
445 assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx
));
446 assert_param(IS_LPTIM_TRIG_SAMPLE_TIME(LPTIM_TrigSampleTime
));
448 /* Get the LPTIMx CFGR value */
449 tmpreg1
= LPTIMx
->CFGR
;
451 /* Clear the TRGFLT bits */
452 tmpreg1
&= ~(LPTIM_CFGR_TRGFLT
);
454 /* Set or Reset the TRGFLT bits according to LPTIM_TrigSampleTime */
455 tmpreg1
|= (LPTIM_TrigSampleTime
);
457 /* Write to LPTIMx CFGR */
458 LPTIMx
->CFGR
= tmpreg1
;
462 * @brief Configures the digital filter for the external clock by determining the number
463 of consecutive samples at the specified level to detect a correct transition.
464 * @param LPTIMx: where x can be 1.
465 * @param LPTIM_ClockSampleTime: the number of samples to detect a valid transition.
466 * This parameter can be:
467 * @arg LPTIM_ClockSampleTime_DirectTransistion : Event is detected on input transitions
468 * @arg LPTIM_ClockSampleTime_2Transistions : Event is detected after 2 consecutive samples at the active level
469 * @arg LPTIM_ClockSampleTime_4Transistions : Event is detected after 4 consecutive samples at the active level
470 * @arg LPTIM_ClockSampleTime_8Transistions : Event is detected after 8 consecutive samples at the active level
473 * @note It is mandatory to disable the peripheral to use this function.
474 * @note An auxiliary clock must be present to use this feature.
476 void LPTIM_ConfigClockGlitchFilter(LPTIM_TypeDef
* LPTIMx
, uint32_t LPTIM_ClockSampleTime
)
478 uint32_t tmpreg1
= 0;
480 /* Check the parameters */
481 assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx
));
482 assert_param(IS_LPTIM_CLOCK_SAMPLE_TIME(LPTIM_ClockSampleTime
));
484 /* Get the LPTIMx CFGR value */
485 tmpreg1
= LPTIMx
->CFGR
;
487 /* Clear the CKFLT bits */
488 tmpreg1
&= ~(LPTIM_CFGR_CKFLT
);
490 /* Set or Reset the CKFLT bits according to LPTIM_ClockSampleTime */
491 tmpreg1
|= LPTIM_ClockSampleTime
;
493 /* Write to LPTIMx CFGR */
494 LPTIMx
->CFGR
= tmpreg1
;
498 * @brief Selects an operating mode.
499 * @param LPTIMx: where x can be 1.
500 * @param LPTIM_Mode: the selected mode.
501 * This parameter can be:
502 * @arg LPTIM_Mode_Continuous : Timer starts in Continuous mode
503 * @arg LPTIM_Mode_Single : Timer will starts in Single mode
506 void LPTIM_SelectOperatingMode(LPTIM_TypeDef
* LPTIMx
, uint32_t LPTIM_Mode
)
508 /* Check the parameters */
509 assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx
));
510 assert_param(IS_LPTIM_MODE(LPTIM_Mode
));
513 if(LPTIM_Mode
== LPTIM_Mode_Continuous
)
515 /* Set the CNTSTRT to select the continuous start*/
516 LPTIMx
->CR
|= LPTIM_Mode_Continuous
;
518 else if(LPTIM_Mode
== LPTIM_Mode_Single
)
520 /* Set the SNGSTRT to select the continuous start*/
521 LPTIMx
->CR
|= LPTIM_Mode_Single
;
526 * @brief Enables or disables the Timeout function.
527 * @param LPTIMx: where x can be 1.
528 * @param NewState: new state of the Timeout function.
529 * This parameter can be: ENABLE or DISABLE.
532 * @note It is mandatory to disable the peripheral to use this function.
534 void LPTIM_TimoutCmd(LPTIM_TypeDef
* LPTIMx
, FunctionalState NewState
)
536 /* Check the parameters */
537 assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx
));
538 assert_param(IS_FUNCTIONAL_STATE(NewState
));
540 if(NewState
!= DISABLE
)
542 /* Set the TIMOUT bit */
543 LPTIMx
->CFGR
|= LPTIM_CFGR_TIMOUT
;
547 /* Reset the TIMOUT bit */
548 LPTIMx
->CFGR
&= ~(LPTIM_CFGR_TIMOUT
);
553 * @brief Configures the Waveform shape.
554 * @param LPTIMx: where x can be 1.
555 * @param LPTIM_Waveform: the selected waveform shape.
556 * This parameter can be:
557 * @arg LPTIM_Waveform_PWM_OnePulse : PWM/One Pulse is selected
558 * @arg LPTIM_Waveform_SetOnce : Set once is selected
561 * @note It is mandatory to disable the peripheral to use this function.
563 void LPTIM_ConfigWaveform(LPTIM_TypeDef
* LPTIMx
, uint32_t LPTIM_Waveform
)
565 /* Check the parameters */
566 assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx
));
567 assert_param(IS_LPTIM_WAVEFORM(LPTIM_Waveform
));
569 /* Clear the WAVE bit */
570 LPTIMx
->CFGR
&= ~(LPTIM_CFGR_CKFLT
);
572 /* Set or Reset the WAVE bit according to LPTIM_Waveform */
573 LPTIMx
->CFGR
|= (LPTIM_Waveform
);
577 * @brief Configures the Autoreload and Compare registers update mode.
578 * @param LPTIMx: where x can be 1.
579 * @param LPTIM_Update: The selected update mode.
580 * This parameter can be:
581 * @arg LPTIM_Update_Immediate : Registers updated after APB write
582 * @arg LPTIM_Update_EndOfPeriod : Registers updated at the end of current timer preload
585 * @note It is mandatory to disable the peripheral to use this function.
587 void LPTIM_ConfigUpdate(LPTIM_TypeDef
* LPTIMx
, uint32_t LPTIM_Update
)
589 /* Check the parameters */
590 assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx
));
591 assert_param(IS_LPTIM_UPDATE(LPTIM_Update
));
593 /* Clear the PRELOAD bit */
594 LPTIMx
->CFGR
&= ~(LPTIM_CFGR_PRELOAD
);
596 /* Set or Reset the PRELOAD bit according to LPTIM_Update */
597 LPTIMx
->CFGR
|= (LPTIM_Update
);
601 * @brief Writes the passed parameter in the Autoreload register.
602 * @param LPTIMx: where x can be 1.
603 * @param LPTIM_Autoreload: The Autoreload value.
604 * This parameter must be a value between 0x0000 and 0xFFFF
607 void LPTIM_SetAutoreloadValue(LPTIM_TypeDef
* LPTIMx
, uint32_t LPTIM_Autoreload
)
609 /* Check the parameters */
610 assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx
));
611 assert_param(IS_LPTIM_AUTORELOAD(LPTIM_Autoreload
));
613 /* Write LPTIM_Autoreload in Autoreload register */
614 LPTIMx
->ARR
= LPTIM_Autoreload
;
618 * @brief Writes the passed parameter in the Compare register.
619 * @param LPTIMx: where x can be 1.
620 * @param LPTIM_Compare: The Compare value.
621 * This parameter must be a value between 0x0000 and 0xFFFF
624 void LPTIM_SetCompareValue(LPTIM_TypeDef
* LPTIMx
, uint32_t LPTIM_Compare
)
626 /* Check the parameters */
627 assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx
));
628 assert_param(IS_LPTIM_COMPARE(LPTIM_Compare
));
630 /* Write LPTIM_Compare in Compare register */
631 LPTIMx
->CMP
= LPTIM_Compare
;
635 * @brief Enables or disables the Counter mode. When the Counter mode is enabled,
636 * the counter is incremented each valid event on ULPTIM
637 * @param LPTIMx: where x can be 1.
638 * @param NewState: new state of the Counter mode.
639 * This parameter can be: ENABLE or DISABLE.
642 * @note It is mandatory to disable the peripheral to use this function.
644 void LPTIM_SelectCounterMode(LPTIM_TypeDef
* LPTIMx
, FunctionalState NewState
)
646 /* Check the parameters */
647 assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx
));
648 assert_param(IS_FUNCTIONAL_STATE(NewState
));
650 if(NewState
!= DISABLE
)
652 /* Set the COUNTMODE bit */
653 LPTIMx
->CFGR
|= LPTIM_CFGR_COUNTMODE
;
657 /* Reset the COUNTMODE bit */
658 LPTIMx
->CFGR
&= ~(LPTIM_CFGR_COUNTMODE
);
663 * @brief Enables or disables the Encoder mode.
664 * @param LPTIMx: where x can be 1.
665 * @param NewState: New state of the encoder mode.
666 * This parameter can be: ENABLE or DISABLE.
669 * @note It is mandatory to disable the peripheral to use this function.
671 void LPTIM_SelectEncoderMode(LPTIM_TypeDef
* LPTIMx
, FunctionalState NewState
)
673 /* Check the parameters */
674 assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx
));
675 assert_param(IS_FUNCTIONAL_STATE(NewState
));
677 if(NewState
!= DISABLE
)
679 /* Set the ENC bit */
680 LPTIMx
->CFGR
|= LPTIM_CFGR_ENC
;
684 /* Reset the ENC bit */
685 LPTIMx
->CFGR
&= ~(LPTIM_CFGR_ENC
);
690 * @brief Gets the LPTIMx counter value.
691 * @param LPTIMx: where x can be 1.
692 * @retval Counter Register value
694 uint32_t LPTIM_GetCounterValue(LPTIM_TypeDef
* LPTIMx
)
696 /* Check the parameters */
697 assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx
));
699 /* Get the Counter Register value */
704 * @brief Gets the LPTIMx Autoreload value.
705 * @param LPTIMx: where x can be 1.
706 * @retval Counter Register value
708 uint32_t LPTIM_GetAutoreloadValue(LPTIM_TypeDef
* LPTIMx
)
710 /* Check the parameters */
711 assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx
));
713 /* Get the Counter Register value */
718 * @brief Gets the LPTIMx Compare value.
719 * @param LPTIMx: where x can be 1.
720 * @retval Counter Register value
722 uint32_t LPTIM_GetCompareValue(LPTIM_TypeDef
* LPTIMx
)
724 /* Check the parameters */
725 assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx
));
727 /* Get the Counter Register value */
732 * @brief LPTIM Input 1 Remap.
733 * @param LPTIMx: where x can be 1.
734 * @param LPTIM_OPTR :
735 * This Parameter can be :
736 * @arg LPTIM_OP_PAD_AF : Port B5 on AF1 or Port C0 on AF1 for input timer
737 * @arg LPTIM_OP_PAD_PA4 : Input remapped to Port A4
738 * @arg RCC_LPTIM1CLKSOURCE_LSI : Input remapped to Port B9
739 * @arg LPTIM_OP_TIM_DAC : Input coming from timer 6 output (for encoder mode)
740 * @retval Counter Register value
742 void LPTIM_RemapConfig(LPTIM_TypeDef
* LPTIMx
, uint32_t LPTIM_OPTR
)
744 /* Check the parameters */
745 assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx
));
747 /* Get the Counter Register value */
748 LPTIMx
->OR
= LPTIM_OPTR
;
755 /** @defgroup LPTIM_Group3 Interrupts and flags management functions
756 * @brief Interrupts and flags management functions
759 ===============================================================================
760 Interrupts and flags management functions
761 ===============================================================================
762 This section provides functions allowing to configure the LPTIM Interrupts, get
763 the status and clear flags bits.
765 The LPTIM provides 7 Flags and Interrupts sources (2 flags and Interrupt sources
766 are available only on LPTIM peripherals equipped with encoder mode interface)
768 Flags and Interrupts sources:
769 =============================
771 2. Auto-reload match.
772 3. External trigger event.
773 4. Autoreloaded register write completed.
774 5. Compare register write completed.
775 6. Direction change: from up to down [Available only for LPTIM peripheral with
777 7. Direction change: from down to up [Available only for LPTIM peripheral with
780 - To enable a specific interrupt source, use "LPTIM_ITConfig" function.
781 - To check if an interrupt was occurred, call "LPTIM_GetITStatus" function and read
783 - To get a flag status, call the "LPTIM_GetFlagStatus" function and read the returned
785 - To clear a flag or an interrupt, use LPTIM_ClearFlag function with the
786 corresponding flag (interrupt).
793 * @brief Enables or disables the specified LPTIM interrupts.
794 * @param LPTIMx: where x can be 1.
795 * @param LPTIM_IT: specifies the TIM interrupts sources to be enabled or disabled.
796 * This parameter can be any combination of the following values:
797 * @arg LPTIM_IT_DOWN: Counter direction change up to down Interrupt source
798 * @arg LPTIM_IT_UP: Counter direction change down to up Interrupt source
799 * @arg LPTIM_IT_ARROK: Autoreload register update OK Interrupt source
800 * @arg LPTIM_IT_CMPOK: Compare register update OK Interrupt source
801 * @arg LPTIM_IT_EXTTRIG: External trigger edge event Interrupt source
802 * @arg LPTIM_IT_ARRM: Autoreload match Interrupt source
803 * @arg LPTIM_IT_CMPM: Compare match Interrupt source
804 * @note LPTIM_IT_DOWN is available only for LPTIM1.
805 * @note LPTIM_IT_UP is available only for LPTIM1.
806 * @param NewState: new state of the TIM interrupts.
807 * This parameter can be: ENABLE or DISABLE.
810 * @note It is mandatory to disable the peripheral to use this function.
812 void LPTIM_ITConfig(LPTIM_TypeDef
* LPTIMx
, uint32_t LPTIM_IT
, FunctionalState NewState
)
814 /* Check the parameters */
815 assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx
));
816 assert_param(IS_LPTIM_IT(LPTIM_IT
));
817 assert_param(IS_FUNCTIONAL_STATE(NewState
));
819 if(NewState
!= DISABLE
)
821 /* Enable the Interrupt sources */
822 LPTIMx
->IER
|= LPTIM_IT
;
826 /* Disable the Interrupt sources */
827 LPTIMx
->IER
&= ~(LPTIM_IT
);
832 * @brief Checks whether the specified LPTIM flag is set or not.
833 * @param LPTIMx: where x can be 1.
834 * @param LPTIM_FLAG: specifies the flag to check.
835 * This parameter can be any combination of the following values:
836 * @arg LPTIM_FLAG_DOWN: Counter direction change up Flag
837 * @arg LPTIM_FLAG_UP: Counter direction change down to up Flag
838 * @arg LPTIM_FLAG_ARROK: Autoreload register update OK Flag
839 * @arg LPTIM_FLAG_CMPOK: Compare register update OK Flag
840 * @arg LPTIM_FLAG_EXTTRIG: External trigger edge event Flag
841 * @arg LPTIM_FLAG_ARRM: Autoreload match Flag
842 * @arg LPTIM_FLAG_CMPM: Compare match Flag
843 * @note LPTIM_Flag_DOWN is generated only for LPTIM1.
844 * @note LPTIM_Flag_UP is generated only for LPTIM1.
845 * @param NewState: new state of the TIM interrupts.
846 * This parameter can be: ENABLE or DISABLE.
849 FlagStatus
LPTIM_GetFlagStatus(LPTIM_TypeDef
* LPTIMx
, uint32_t LPTIM_FLAG
)
851 ITStatus bitstatus
= RESET
;
853 /* Check the parameters */
854 assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx
));
855 assert_param(IS_LPTIM_GET_FLAG(LPTIM_FLAG
));
857 if((LPTIMx
->ISR
& LPTIM_FLAG
) != (RESET
))
869 * @brief Clears the LPTIMx's pending flag.
870 * @param LPTIMx: where x can be 1.
871 * @param LPTIM_CLEARF: specifies the pending bit to clear.
872 * This parameter can be any combination of the following values:
873 * @arg LPTIM_CLEARF_DOWN: Counter direction change up Clear Flag
874 * @arg LPTIM_CLEARF_UP: Counter direction change down to up Clear Flag
875 * @arg LPTIM_CLEARF_ARROK: Autoreload register update OK Clear Flag
876 * @arg LPTIM_CLEARF_CMPOK: Compare register update OK Clear Flag
877 * @arg LPTIM_CLEARF_EXTTRIG: External trigger edge event Clear Flag
878 * @arg LPTIM_CLEARF_ARRM: Autoreload match Clear Flag
879 * @arg LPTIM_CLEARF_CMPM: Compare match Clear Flag
880 * @note LPTIM_Flag_DOWN is generated only for LPTIM1.
881 * @note LPTIM_Flag_UP is generated only for LPTIM1.
884 void LPTIM_ClearFlag(LPTIM_TypeDef
* LPTIMx
, uint32_t LPTIM_CLEARF
)
886 /* Check the parameters */
887 assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx
));
888 assert_param(IS_LPTIM_CLEAR_FLAG(LPTIM_CLEARF
));
890 /* Clear the IT pending Bit */
891 LPTIMx
->ICR
|= LPTIM_CLEARF
;
895 * @brief Check whether the specified LPTIM interrupt has occurred or not.
896 * @param LPTIMx: where x can be 1.
897 * @param LPTIM_IT: specifies the LPTIM interrupt source to check.
898 * @arg LPTIM_IT_DOWN: Counter direction change up to down Interrupt source
899 * @arg LPTIM_IT_UP: Counter direction change down to up Interrupt source
900 * @arg LPTIM_IT_ARROK: Autoreload register update OK Interrupt source
901 * @arg LPTIM_IT_CMPOK: Compare register update OK Interrupt source
902 * @arg LPTIM_IT_EXTTRIG: External trigger edge event Interrupt source
903 * @arg LPTIM_IT_ARRM: Autoreload match Interrupt source
904 * @arg LPTIM_IT_CMPM: Compare match Interrupt source
905 * @retval The new state of LPTIM_IT (SET or RESET).
907 ITStatus
LPTIM_GetITStatus(LPTIM_TypeDef
* LPTIMx
, uint32_t LPTIM_IT
)
909 ITStatus bitstatus
= RESET
;
910 uint32_t itstatus
= 0x0, itenable
= 0x0;
912 /* Check the parameters */
913 assert_param(IS_LPTIM_ALL_PERIPH(LPTIMx
));
914 assert_param(IS_LPTIM_IT(LPTIM_IT
));
916 /* Get the Interrupt Status bit value */
917 itstatus
= LPTIMx
->ISR
& LPTIM_IT
;
919 /* Check if the Interrupt is enabled */
920 itenable
= LPTIMx
->IER
& LPTIM_IT
;
922 if((itstatus
!= RESET
) && (itenable
!= RESET
))
940 #endif /* STM32F410xx */
950 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/