Reliable timings using Threads

Discussions and support about ChibiOS/RT, the free embedded RTOS.
Koen
Posts: 35
Joined: Mon Dec 21, 2015 12:15 am
Been thanked: 5 times

Reliable timings using Threads

Postby Koen » Fri Jan 29, 2016 3:50 am

Hello,

in Reliable timings using Threads, this example is given :

Code: Select all

msg_t my_thread(void *param) {
 
  systime_t time = chTimeNow();     // T0
  while (true) {
    time += MS2ST(1000);            // Next deadline
    do_something();
    chThdSleepUntil(time);
  }
}


And includes a warning about do_something() execution time having to be shorter than the deadline which is great.

But, unless I'm mistaken, a warning about chTimeNow() restarting back from 0 would help too.

If the loop runs long enough for the current time to restart from 0, the delay time will be : (time - now) == (12345678 - 0) == 12345678 == very long.
Last edited by Koen on Fri Jan 29, 2016 3:57 am, edited 2 times in total.

Koen
Posts: 35
Joined: Mon Dec 21, 2015 12:15 am
Been thanked: 5 times

Re: Reliable timings using Threads

Postby Koen » Fri Jan 29, 2016 3:54 am

Which brings me to this question, is this a deadline-safe and rollback-safe way to achieve the same goal ?

Code: Select all

msg_t my_thread(void *param) {

  while (true) {
    const systime_t start = chVTGetSystemTimeX();
    do_something();
    const systime_t elapsed = chVTGetSystemTimeX() - start;
    if(elapsed < 1000) {
      chThdSleepMilliseconds(1000 - elapsed);
    }
  }
}

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: Reliable timings using Threads

Postby Giovanni » Fri Jan 29, 2016 9:08 am

Hi,

There is no rollback issue. The system time is meant to roll back and that arithmetic is not affected by that.

Assume N be the number of bits of system time.

If the current time is 2^N - 1 (highest number with N bits) then the next deadline would be (2^N - 1 + 1000) % (2^N) -> 999.

999 is exactly 1000 ticks from "now".

Giovanni


Return to “ChibiOS/RT”

Who is online

Users browsing this forum: No registered users and 3 guests