Using VT as timeout generator

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
king2
Posts: 98
Joined: Mon May 28, 2012 1:57 pm
Been thanked: 2 times

Using VT as timeout generator

Post by king2 »

I want to implement timeout system in my project. This means that I want to set timer and continue endless loop. If in this loop I will see that timer was expired, I want to do something (save timeout error, for example).

Pseudo-code:

Code: Select all

while (1) {
  if (!waiting_answer && timerExpired(tm_msg)) {
      send_msg();
      setTimer(tm_msg, 100);
      setTimer(tm_answer, 100);
      waiting_answer = 1;
  }

  if (cmd_received()) {
      setTimer(tm_msg, 100);
      waiting_answer = 0;
  }

  if (waiting_answer && timerExpired(tm_anwser)) {
      // something happens, ERROR!
      loudScreamAndCry();
  }
  doSomethingWithDataReceived();
}


So, I want to:
1. Timer can be set in any place. If timer was set already, just reset ot and restart count from zero.
2. Do not wait for timer to expire, but continue working
3. Do not call any callbacks while timer was expired. Just return another value when I need this.

Can I do this and how?

Thanks!
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Using VT as timeout generator

Post by Giovanni »

You don't even need a timer for that, just the system time. You can use chTimeNow() and chTimeIsWithin().

For example:

Code: Select all

  systime_t start = chTimeNow();
  systime_t end = start + MS2ST(500); /* Milliseconds.*/
  while (chTimeIsWithin(start, end)) {
    /* Do things.*/
  }
/* Timeout.*/


There are variations on the above, you may check for timeout within the loop etc.

Giovanni
king2
Posts: 98
Joined: Mon May 28, 2012 1:57 pm
Been thanked: 2 times

Re: Using VT as timeout generator

Post by king2 »

OK, thanks for quick help!
Post Reply