timerafter

Technical questions regarding the XTC tools and programming with XMOS.
omega7
Active Member
Posts: 32
Joined: Thu Jun 03, 2010 12:16 pm

timerafter

Post by omega7 »

Hi all,

My question concerns the using of the timerafter event. I suspect that the event sometimes is fired unintentionally.

Here is the code:

Code: Select all

case (enableTimer > 0) => detectTimer when timerafter(actualTime + USEC(3650)) :> void:
The code is a kind of watchdog. When a signal isn't toggling on a port for more than 3650 us, the timer should fire the event:

Code: Select all

case detectPort when pinsneq(portLevel) :> void:

detectPort :> portLevel;

detectTimer :> actualTime;
The problem is that the timer seems to fire now and then, while the toggle signal on detectPort is still there.

Can someone explain what happens when the (actualTime + USEC(3650)) overflows the unsigned size ?

Are there other causes than may explain the problem?

Thanks!

Martin


User avatar
bsmithyman
Experienced Member
Posts: 126
Joined: Fri Feb 12, 2010 10:31 pm

Post by bsmithyman »

I'm not totally sure what's going on, but with the way you've written it the compiler is going to have to do some thinking, so it might be possible to make it more explicit. Instead of putting the line "timerafter(actualTime + USEC(3600))" you could try using "timerafter(triggerTime)" as the guard and setting the delay in your pin toggle case, i.e. "triggerTime = actualTime + USEC(3600)". As it stands, I'm not sure what the compiler does when you have an evaluated statement in the guard. It might try to be smart and prevent the overflow (when really you do want the overflow to happen, just before you set the event).

Cheers,
Brendan

Edit: Fixed unresolved parentheses.
User avatar
segher
XCore Expert
Posts: 844
Joined: Sun Jul 11, 2010 1:31 am

Post by segher »

omega7 wrote:

Code: Select all

case (enableTimer > 0) => detectTimer when timerafter(actualTime + USEC(3650)) :> void:
Show that USEC() macro?
omega7
Active Member
Posts: 32
Joined: Thu Jun 03, 2010 12:16 pm

Post by omega7 »

The USEC Macro? Here it is!

Code: Select all

#define USEC(x)		(x*100)
User avatar
segher
XCore Expert
Posts: 844
Joined: Sun Jul 11, 2010 1:31 am

Post by segher »

If your actualTime variable is unsigned, overflow is not a problem (it will wrap);
if it is signed, in C, that is undefined behaviour (but it will still work in XC I think).

It will also work fine on the xcore timer, there is no overflow there.

You'll have to show more of the code, and/or investigate better when exactly
this happens.

Good luck,


Segher