cancel
Showing results for 
Search instead for 
Did you mean: 

Can't update SysTick->VAL

Clonimus74
Senior II

Hi,

In my system I use STM32L476.

In order to save power once every second, when heavy processing is done, I switch to PLL 80MHz, the rest of the time I use MSI clock at 32MHz.

In order to have a stable SysTick I update the reload value of the SysTick according to the desired clock, but I need to change the value of the current SysTick counter value to the relative value after clock change.

For some reason the SysTick counter value (SysTick->VAL) is always loaded with the reload value (minus 1 or 2). I can't seem to set the desired value to the counter.

What am I missing?

Here is my relevant code segment:

  static volatile float new_systick_val;
  enum SystemFaultsEnum status = eSYSTEM_OK;
  uint32_t systick_reload_val;
        
/*
 
Here I change the clock to either 32MHz or 80MHz
if the desired clock is 80MHz
new_systick_val = SysTick->VAL * 319999.0f / (((float)SystemCoreClock/100.0f) - 1.0f);
and if desired clock is 80MHz
new_systick_val = SysTick->VAL * 799999.0f / (((float)SystemCoreClock/100.0f) - 1.0f);
 
*/
 
  SystemCoreClockUpdate();
  systick_reload_val = (uint32_t)((SystemCoreClock/100) - 1UL); /* set systick to 10msec */
  SysTick->LOAD  = systick_reload_val;
  SysTick->VAL = (uint32_t)new_systick_val;

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions

The math looks broken, but any write to SysTick->VAL clears the register.

Do the change at the top of the 10ms interval, in the handler...

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

3 REPLIES 3

The math looks broken, but any write to SysTick->VAL clears the register.

Do the change at the top of the 10ms interval, in the handler...

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
joemccarr
Senior

Maybe have a bit in your code that is set when you go to 80MHz and reset when your running at 32KHz and have the sysTick interrupt routine look at the bit and change the reload value based on that bit.

Thanks Clive,

The math is good, but if you see an error you're welcome to point it out.

I didn't find info regarding the resetting of the register when it is written to, but that seems to be what's happening.

I don't like to have code in the SysTick handler besides the counter and tick variable, but seems there's no other choice, I will use your suggestion.