2016-03-24 08:03 AM
Hi,
I'm using the STM32L4 with the low power timer (LPTIM1).I'm using it with the LSE clock in counter mode with IT (the counter count until reaching AutoReload value, then it goes into interruption ...).I'd like to know if the AutoReload register can be modified on the fly ?I tried to modify it inside the IRQ_handle and it seems to work (I can generate a PWM signal this way).But when I modify it outside of the IRQ_handle (in my while(1) for example) it doesn't work.When I stop my program in debug mode, I see AutoReload set to the good value and the counter exceeding AutoReload (instead of going into interruption).Does anybody know if modifying the register on the fly is doable ?Thanks2016-03-24 08:57 AM
> When I stop my program in debug mode, I see AutoReload set to the good value and the counter exceeding AutoReload (instead of going into interruption).
This is what happens if you set the autoreload value below the current counter value. JW2016-03-25 02:50 AM
My bad, I forgot to freeze the timer in debug mode and I was watching wrong values.
But no, I don't set autoreload with a value below counter. Here is a simple program I did to test it :int
main (
void
){
/* Initialisation and all ... */
/* My test program */
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_10, SET);
timer_start(500);
// Start the timer in continuous mode with AutoReload set to 500ms
if
(HAL_LPTIM_ReadCounter(&lptim1_handle) <= 0x1333)
// Check that counter is not below the future AutoReload value
{
timer_change(150);
//Modify AutoReload to 150ms
}
while
(1);
}
void
LPTIM1_IRQHandler(
void
)
{
__HAL_LPTIM_CLEAR_FLAG(&lptim1_handle, LPTIM_FLAG_ARRM);
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_10, RESET);
timer_stop();
}
As you can see the program is simple :
- I SET PIN10
- Start the timer and modify AutoReload value
- I RESET the PIN10 in the irq_handle
When I watch PIN10 with an oscilloscope it's being reseted after 200us (instead of 150ms). And of course, it's modifying the AutoReload that causes the problem. If I don't modify it, everything works fine (the PIN is reseted after 500ms).
Here is a capture of my timer's registers at 2 differents breakpoint (to me everyting seems fine):
- one after the start of the timer
- one after modifying AutoReload
2016-03-25 03:48 AM
2016-03-25 04:39 AM
Hi Klem,
AS mentionned in the STM32L4x6 reference Manual : in page 1024: chapter LPTIM/ section register update“
The LPTIMx_ARR register and LPTIMx_CMP register are updated immediately after the
APB bus write operation, or at the end of the current period if the timer is already started.�
-
So it is allowed to update immediately the ARR value.
“
When the PRELOAD bit is reset to ‘0’, the LPTIMx_ARR and the LPTIMx_CMP
registers are immediately updated after any write access.�
-
You should ensure that your preload bit is reset to 0
“
After a write to the LPTIMx_ARR register or the LPTIMx_CMP register, a new write
operation to the same register can only be performed when the previous write operation is
completed. Any successive write before respectively the ARROK flag or the CMPOK flag be
set, will lead to unpredictable results.�
-
You should check the ARROk flad is set before any new update
-Hannibal -
2016-03-25 10:01 AM
Hey thanks a lot, it's working.
Now I check the ARROK flag and reset it after the start of the timer.My new AutoReload value is set.2016-03-28 04:23 AM
Hi Klem,
It's nice to hear that. Keep up the good work. -Hannibal-