Content originally posted in LPCWare by ToBeFrank on Wed Aug 22 19:04:32 MST 2012
Quote: celephicus
Greetings,
I am using the systick timer to generate the usual 10ms interrupts, and incrementing a counter on every interrupt. What I now need is timing resolution to the microseconds to catch a nasty little realtime bug.
I think that the following pseudocode will work:
disable_interrupts()
timer = systick_counter
ticks = my_tick_counter
if isSystickInterrupt Pending():
ticks += 1
enable_interrupts()
The check for a pending interrupt catches the case where I read the systick counter just after is has hit zero and been reloaded, but before the systick ISR has incremented my tick counter.
Will this work?
I'm confused what you're asking. I'm assuming the code above is for getting the tick counter from your 10ms systick, not the microsecond timing you're asking about? If so, the systick at 10ms resolution won't roll over for 497 days. Will your device really be on continuously for that long? If the answer is yes, a simpler solution is to use a uint64_t for the counter variable. It will add minimal extra time to your systick interrupt and you won't have to worry about rollover.
For the microsecond resolution, use one of the hardware timers and configure the prescale to give you 1us counts.