lpcware

Systick Reset or Recalibration

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by kwinchell on Wed Aug 28 15:42:52 MST 2013
I have an application that uses systick.

Here is my code:

void Scheduler_Start(void)
{
    NVIC_SetPriority(PendSV_IRQn, 3);

    __enable_irq();

    SysTick->CTRL = (1<<SYSTICK_CLKSOURCE) | (1<<SYSTICK_ENABLE) | (1<<SYSTICK_TICKINT); // enable the timer

}


void Scheduler_Init(void)
{
    // Disable the scheduler
    SysTick->STCTRL = (uint8_t) 0;       
    SetSchedulerPeriod();   // set period to correct value for PCLK

    NVIC_SetPriority(SysTick_IRQn, 3);

    SchedulerTicks = 0;
    // the Scheduler Clock (SysTick) is ready to run, just needs to be enabled
}   // end of function Scheduler_Init()

/***********************************************************************
** SetSchedulerPeriod
*
*  DESCRIPTION: Set the systick timer reload value to the correct value for the clock
*
***********************************************************************/
void SetSchedulerPeriod(void)
{
    SysTick->STRELOAD = GetClocksPerMicrosecond() * SCHEDULER_TICK_TIME_US;
    SysTick->STCURR = 0;
}


It all works fine, normally.

My application requires an interrupt event to resynchronize the period to the current event.
According to the UM, this can be done by writing to System Timer Current value register (STCURR - 0xE000 E018)

The note is:
23:0 CURRENT Reading this register returns the current value of the System Tick
counter. Writing any value clears the System Tick counter and the
COUNTFLAG bit in STCTRL.

Therefore I call SetSchedulerPeriod() to write to STCUR.
Now, my Systick time is set to 100uSec. 
Therefore, I expect the next interrupt to occur at 100 uSec.
Instead, I get the interrupt at 125 uSec.  This is not a problem for me.
The problem is that every once in a while, it happens in 25 uSec.
This is a MAJOR problem for me.

1. Why is this happening?
2. How can I get a consistant reset time?

Outcomes