cancel
Showing results for 
Search instead for 
Did you mean: 

SYSTICK is really funky - I think

carl2399
Associate II
Posted on September 10, 2012 at 09:25

In my application I set up SysTick with:

SysTick_Config(SystemCoreClock / 1000);

to generate a 1ms interrupt.

I got some weird timing anomalies and eventually used a timer interrupt to generate 1ms interrupts using the PSC (prescale) and ARR (auto-reload) registers.

When I compare the two I get about 1012 to 1032 SysTick interrupts for every 1000 timer interrupts - yup that's right folks, it varies every cycle. When I compare timer interrupts to other timer interrupts, they all report as being perfectly in sync with each other.

To me, it seems that SysTick counts ''ticks'' and expecting anything more than that might not be correct. 

The difference probably wouldn't matter if I wasn't using a 3ppm oscillator to clock the system. Although to be honest I have no way of telling which time reference is correct.

Any ideas as to what's going on?
1 REPLY 1
carl2399
Associate II
Posted on September 11, 2012 at 03:46

Okay, more details:

void
TestLoop
(
void
)
{
RTC_TimeTypeDef 
time
;
u16 sec = 0;
u16 tick;
u32 lastms, ms;
for
(;;)
{
RTC_GetTime(RTC_Format_BIN, &
time
);
if
(sec != 
time
.RTC_Seconds)
{
sec = 
time
.RTC_Seconds;
tick = IRQ3;
IRQ3 = 0;
ms = MilliSeconds;
printf
(
''Test: %d %d %d

''
, Ticks, tick, ms - lastms);
lastms = ms;
}
}
}

Ticks is updated by a 1000Hz timer interrupt routine and counts from 0 to 9 IRQ3 is updated by a 6000Hz timer. Milliseconds is updated by SysTick interrupt which is set as per the previous message. The RTC clock is clocked using a 32768Hz crystal on the LSE processor pins. The output of this loop is:

Test: 345 6000 1014 
Test: 345 6000 1024 
Test: 345 6000 1017 
Test: 345 6000 1015 
Test: 345 6000 1023 
Test: 345 6000 1019 
Test: 345 6000 1016 
Test: 345 6000 1019 
Test: 345 6000 1025 
Test: 345 6000 1018 
Test: 345 6000 1015 
Test: 345 6000 1020

after 45 minutes:

Test: 336 6000 1020 
Test: 336 6000 1015 
Test: 336 6000 1025 
Test: 336 6000 1015 
Test: 336 6000 1014

So there's 9ms of drift in 45 minutes which is 3.3ppm -or- about 0.3s of relative drift per day between the LSE and HSE clock sources. The one that really sticks out is the variation in the SysTick number which seems to be wrong by about 20000ppm!!