Page 1 of 1

Using VT as timeout generator

Posted: Wed Jun 13, 2012 2:43 pm
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!

Re: Using VT as timeout generator

Posted: Wed Jun 13, 2012 3:04 pm
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

Re: Using VT as timeout generator

Posted: Wed Jun 13, 2012 3:07 pm
by king2
OK, thanks for quick help!