HAL LPTIM How to Use ¶
- group LPTIM_How_To_Use
-
Use the LPTIM HAL driver as follows: ¶
(Non-optional call) Call HAL_LPTIM_Init() to initialize the LPTIM driver by establishing a link with ¶
the LPTIM physical instance.
(Non-optional call) Call HAL_LPTIM_SetConfig() to configure the timebase unit: ¶
Select the clock source hal_lptim_clk_src_t:
:
LPTIM is clocked by an internal clock source. The counter is incremented following each internal clock pulse.
HAL_LPTIM_CLK_EXTERNAL_SYNCHRONOUS
:
The LPTIM counter clock signal is generated from the external Input1 signal.
The LPTIM external Input1 is sampled with the internal clock provided to the LPTIM.
Configure LPTIM Input1 with
HAL_LPTIM_SetConfigInput1().HAL_LPTIM_CLK_EXTERNAL_ASYNCHRONOUS
:
The LPTIM counter clock signal is generated from the external Input1 signal.
Configure the LPTIM external Input1 by calling HAL_LPTIM_SetConfigInput1().HAL_LPTIM_CLK_ENCODER_SUBMODE_1 to HAL_LPTIM_CLK_ENCODER_SUBMODE_3
:
The LPTIM counter clock signal is generated from the two external input signals, Input1 and Input2.
Select the prescaler division factor hal_lptim_clk_src_presc_t
Set period value: number from 0 to 65535 (0x0000 to 0xFFFF)
Set repetition counter value: number from 0 to 255 (0x00 to 0xFF)
Select the functioning mode with hal_lptim_mode_t:
HAL_LPTIM_ONE_SHOT : When the LPTIM counter is stopped, a trigger event starts it. The counter is stopped on an update event.
HAL_LPTIM_SET_ONCE : A first trigger event starts the LPTIM counter for a single one-shot cycle.
HAL_LPTIM_CONTINUOUS : The LPTIM counter starts from a trigger event and never stops until the timer is disabled.
HAL_LPTIM_TIMEOUT : The detection of an active edge on one selected trigger input can be used to reset the LPTIM counter.
If needed, call HAL_LPTIM_DeInit() to reset the driver. ¶
When an external clock is used, configure LPTIM Input1 by calling HAL_LPTIM_SetConfigInput1(). ¶
Select the Input1 source with hal_lptim_input1_src_t
Select the Input1 polarity with hal_lptim_input1_polarity_t
Select the Input1 filter with hal_lptim_filter_t
Select the usage: ¶
- To use an LPTIM instance as a simple counter: ¶
Configure the LPTIM time base unit. The functioning mode must be set to HAL_LPTIM_CONTINUOUS when calling HAL_LPTIM_SetConfig().
Start the LPTIM counter. Two execution modes are available:
Polling: HAL_LPTIM_Start()
Interrupt: HAL_LPTIM_Start_IT(), in this case, the update interrupt is enabled
Stop the LPTIM counter. Call HAL_LPTIM_Stop() and HAL_LPTIM_Stop_IT(), as per the selected execution mode.
- To generate a PWM signal: ¶
Configure the LPTIM time base unit. The functioning mode must be set to HAL_LPTIM_CONTINUOUS when calling HAL_LPTIM_SetConfig().
Configure the output channel(s) by calling HAL_LPTIM_OC_SetConfigChannel().
To start PWM signal generation, first start the output channel, then the LPTIM time base unit by calling HAL_LPTIM_Start(). Three execution modes are available:
Polling: HAL_LPTIM_OC_StartChannel()
Interrupt: HAL_LPTIM_OC_StartChannel_IT(), in this case, the compare match interrupt is enabled
Stop PWM signal generation. Call HAL_LPTIM_OC_StopChannel() or HAL_LPTIM_OC_StopChannel_IT(), as per the selected execution mode.
- To generate a one-pulse signal: ¶
Configure the LPTIM time base unit. The functioning mode must be set to HAL_LPTIM_ONE_SHOT when calling HAL_LPTIM_SetConfig().
Configure the output channel(s) by calling HAL_LPTIM_OC_SetConfigChannel().
If the pulse generation is triggered when an active edge is detected on the external trigger input, configure the external trigger input by calling HAL_LPTIM_SetConfigExtTrigInput().
To start pulse generation, first start the output channel(s), then the LPTIM time base unit by calling HAL_LPTIM_Start(). Three execution modes are available:
Polling: HAL_LPTIM_OC_StartChannel()
Interrupt: HAL_LPTIM_OC_StartChannel_IT(), in this case, the compare match interrupt is enabled
Stop signal generation. Call HAL_LPTIM_OC_StopChannel() or HAL_LPTIM_OC_StopChannel_IT(), as per the selected execution mode.
- To generate a set once signal: ¶
Configure the LPTIM time base unit. The functioning mode must be set to HAL_LPTIM_SET_ONCE when calling HAL_LPTIM_SetConfig().
If the signal generation is triggered when an active edge is detected on the external trigger input, configure the external trigger input by calling HAL_LPTIM_SetConfigExtTrigInput().
Configure the output channel(s) by calling HAL_LPTIM_OC_SetConfigChannel().
To start signal generation, first start the channel(s), then the LPTIM time base unit by calling HAL_LPTIM_Start(). Two execution modes are available:
Polling: HAL_LPTIM_OC_StartChannel()
Interrupt: HAL_LPTIM_OC_StartChannel_IT(), in this case, the compare match interrupt is enabled
Stop signal generation. Call HAL_LPTIM_OC_StopChannel() or HAL_LPTIM_OC_StopChannel_IT(), as per the selected execution mode.
- To capture an input signal: ¶
Configure the LPTIM time base unit. The functioning mode must be set to HAL_LPTIM_CONTINUOUS when calling HAL_LPTIM_SetConfig().
Configure the input channel(s) by calling HAL_LPTIM_IC_SetConfigChannel().
To start a capture, first start the input channel, then the LPTIM time base unit (HAL_LPTIM_Start()). Three execution modes are available:
Polling: HAL_LPTIM_IC_StartChannel()
Interrupt: HAL_LPTIM_IC_StartChannel_IT(), in this case, the capture interrupt is enabled
DMA: HAL_LPTIM_IC_StartChannel_DMA(), in this case, the capture DMA request is enabled
Stop input signal capture. Call HAL_LPTIM_IC_StopChannel() or HAL_LPTIM_IC_StopChannel_IT() or HAL_LPTIM_IC_StopChannel_DMA(), as per the selected execution mode.
- To use the LPTIM timeout feature: ¶
Configure the LPTIM time base unit. The functioning mode must be set to HAL_LPTIM_TIMEOUT when calling HAL_LPTIM_SetConfig().
Configure the external trigger input by calling HAL_LPTIM_SetConfigExtTrigInput().
Configure the timeout value by calling HAL_LPTIM_OC_SetPulse(). Two execution modes are available:
Polling: HAL_LPTIM_Start()
Interrupt: HAL_LPTIM_Start_IT(), in this case, the update event is enabled
To stop pulse generation, call HAL_LPTIM_Stop() and HAL_LPTIM_Stop_IT(), as per the selected execution mode.
- To use the LPTIM encoder interface: ¶
Configure the LPTIM time base unit. The functioning mode must be set to HAL_LPTIM_CONTINUOUS and encoder mode must be selected as the LPTIM clock source when calling HAL_LPTIM_SetConfig().
Configure the encoder interface (LPTIM Input1 and Input2) by calling HAL_LPTIM_SetConfigEncoder(). Two execution modes are available:
Polling: HAL_LPTIM_Start()
Interrupt: HAL_LPTIM_Start_IT(), in this case, the update event is enabled
To stop the encoder interface, call HAL_LPTIM_Stop() and HAL_LPTIM_Stop_IT(), as per the selected execution mode.
Callbacks definition in interrupt or DMA mode ¶
When the compilation define USE_HAL_LPTIM_REGISTER_CALLBACKS is set to 1, the user can configure the driver callbacks dynamically using their own method:
Callback name
Default callback
Register callback
ErrorCallback
HAL_LPTIM_ErrorCallback
HAL_LPTIM_RegisterErrorCallback
StopCallback
HAL_LPTIM_StopCallback
HAL_LPTIM_RegisterStopCallback
InputCaptureStopCallback
HAL_LPTIM_InputCaptureStopCallback
HAL_LPTIM_RegisterChannelStopCallback
UpdateCallback
HAL_LPTIM_UpdateCallback
HAL_LPTIM_RegisterUpdateCallback
UpdateHalfCpltCallback
HAL_LPTIM_UpdateHalfCpltCallback
HAL_LPTIM_RegisterUpdateHalfCpltCallback
RepUpdateCallback
HAL_LPTIM_RepUpdateCallback
HAL_LPTIM_RegisterRepUpdateCallback
TriggerCallback
HAL_LPTIM_TriggerCallback
HAL_LPTIM_RegisterTriggerCallback
InputCaptureCallback
HAL_LPTIM_InputCaptureCallback
HAL_LPTIM_RegisterInputCaptureCallback
InputCaptureHalfCpltCallback
HAL_LPTIM_InputCaptureHalfCpltCallback
HAL_LPTIM_RegisterInputCaptureHalfCpltCallback
InputOverCaptureCallback
HAL_LPTIM_InputOverCaptureCallback
HAL_LPTIM_RegisterOverCaptureCallback
CompareMatchCallback
HAL_LPTIM_CompareMatchCallback
HAL_LPTIM_RegisterCompareMatchCallback
CompareUpdateCallback
HAL_LPTIM_CompareUpdateCallback
HAL_LPTIM_RegisterCompareUpdateCallback
AutoReloadMatchCallback
HAL_LPTIM_AutoReloadMatchCallback
HAL_LPTIM_RegisterAutoReloadMatchCallback
AutoReloadUpdateCallback
HAL_LPTIM_AutoReloadUpdateCallback
HAL_LPTIM_RegisterAutoReloadUpdateCallback
DirectionDownCallback
HAL_LPTIM_DirectionDownCallback
HAL_LPTIM_RegisterDirectionDownCallback
DirectionUpCallback
HAL_LPTIM_DirectionUpCallback
HAL_LPTIM_RegisterDirectionUpCallback
To unregister a callback, register the default callback via the registration function.
By default, after HAL_LPTIM_Init() and when the state is HAL_LPTIM_STATE_INIT, all callbacks are set to the corresponding default weak functions.
Callbacks can be registered in the handle global state HAL_LPTIM_STATE_INIT and HAL_LPTIM_STATE_IDLE.
When the compilation define USE_HAL_LPTIM_REGISTER_CALLBACKS is set to 0 or is not defined, the callback registration feature is not available and weak callbacks are used, represented by the default value in the table above.
Note
If needed, some configuration parameters can be changed on the fly (e.g., period HAL_LPTIM_SetPeriod()).
Note
If needed, some configuration parameters can be changed on the fly (e.g., PWM duty cycle HAL_LPTIM_OC_SetChannelPulse()).
Note
In case of software start (external trigger not configured), call HAL_LPTIM_Start()
to start the LPTIM counter for one-shot counting.
In one-pulse mode, the output waveform is similar to the PWM mode for the first pulse. Then the output is permanently reset.
Note
If needed, some configuration parameters can be changed on the fly (e.g., PWM duty cycle HAL_LPTIM_OC_SetChannelPulse()).
Note
If needed, some configuration parameters can be changed on the fly (e.g., PWM duty cycle HAL_LPTIM_OC_SetChannelPulse()).
Note
If needed, some configuration parameters can be changed on the fly (e.g., period HAL_LPTIM_SetPeriod()).