Page 1 of 1

Time related questions

Posted: Wed Mar 21, 2018 8:11 pm
by faisal
Time Measurement Module:
I see that it uses the 'real time counter', which is the DWT cycle counter in STM32. Does it handle the wrapping around of the real time counter? If you start a time measurement just before the real time counter wraps around, and stop it after the wrap - would that be problematic? Looking at tm_stop(), I see this:

Code: Select all

  tmp->last = (now - tmp->last) - offset;


Absolute high resolution System Time:
According to this post, viewtopic.php?f=3&t=4166&e=1&view=unread#p30955, it seems that the system timer wrap arounds are handled in tickless mode. If I configure the system tick to be 1Mhz, how can I get absolute system time in a way that isn't limited to the system timer width? My system uptime can be over a month. I looked at the chVTTimeElapsedSince, chTimeDiff, but none of those seem to handle the systick timer wrap around - i just see something like (end - start) .

This post, viewtopic.php?f=24&t=4265&start=10#p30959, mentions introducing a global sysinterval_t variable, which can be updated each time the systick wraps around. Then, the absolute time would be the global sysinterval_t + current system time. Is some functionality similar to that already present, can I use any existing APIs to accomplish this?

Using Core SysTick timer:
Can the Cortex-M 24-bit SysTick timer be used in ChibiOS, or must we use a 16-bit or 32-bit hardware timer ?

Re: Time related questions

Posted: Wed Mar 21, 2018 9:45 pm
by Giovanni
Hi,

Wrapping of counters is handled by arithmetic tricks. Virtual timers would not work without this basic functionality, 16 bits counters wrap quite fast.

See this for example:

Code: Select all

bool chSysIsCounterWithinX(rtcnt_t cnt, rtcnt_t start, rtcnt_t end) {

  return (bool)((cnt - start) < (end - start));
}

void chSysPolledDelayX(rtcnt_t cycles) {
  rtcnt_t start = chSysGetRealtimeCounterX();
  rtcnt_t end  = start + cycles;

  while (chSysIsCounterWithinX(chSysGetRealtimeCounterX(), start, end)) {
  }
}


Note that it does not matter if start<end or end>start.

System time is handled using an HW counter in tickless mode, so you need to make a tradeoff between resolution and system time duration before wrapping.

For tickless more a 16 or 32 bits time is required, 24 is no good because we rely on the size of the counter to match a C type size.

Giovanni

Re: Time related questions

Posted: Wed Mar 21, 2018 11:15 pm
by faisal
Giovanni wrote:System time is handled using an HW counter in tickless mode, so you need to make a tradeoff between resolution and system time duration before wrapping.


So the system interval which is now in RT5 allows for setting timeouts and the such which are longer than a wrap around of the system timer. In other words, it allows for scheduling interrupts in the future based on the system timer. Similarly is there no mechanism to measure time intervals greater than that of the system timer? In the post that I referenced, you mentioned that an interrupt is triggered each time the system timer wraps - is there a hook in which I can put code to update my global sysinterval_t uptime variable?

Giovanni wrote:For tickless more a 16 or 32 bits time is required, 24 is no good because we rely on the size of the counter to match a C type size.
Giovanni


Thanks for letting me know. Any plans to support it in the future?

Re: Time related questions

Posted: Thu Mar 22, 2018 8:34 am
by Giovanni
No plans for systick, it is simply not the kind of timer required for tickless mode.

You can have intervals larger than system time, this is the point of having 2 separate types, interrupts are triggered when required (not necessarily on wrapping) to keep virtual timers updated.

Giovanni