timer overflow

Technical questions regarding the XTC tools and programming with XMOS.
richard
Respected Member
Posts: 318
Joined: Tue Dec 15, 2009 12:46 am

Post by richard »

babazaroni wrote:I'm ok with either taking the time, or capturing it in the case statement, but I'm still wondering about this overflow situation.

Lets assume the case statement fires and 4294E6 is captured in 'time'. Now 1E6 gets added to 'time', but 4295E6 overflows to 32704 in a 32 bit word.

Now, until the actual timer value overflows, the case statement will continue to fire immediately on entry, instead of waiting 1E6 ticks, which is undesired.

I don't want to write a bunch of code which detects the overflow and makes adjustments, if there is a better way to handle this situation.
Consider the conditional timer input

Code: Select all

t when timerafter(time).
If the hardware logic waited for the following condition became true:

Code: Select all

currentTime > time
Then you would be correct - you would run into issues around the point where the time wrapped around as a 32bit integer. However the hardware actually waits for the following comparison to became true:

Code: Select all

(signed int)time - (signed int)currentTime < 0
Because the comparison is relative to the current time there is no need to worry about whether the time is about to wrap around as a 32bit integer. The input will behave correctly so long as at the point when input is executed the time you are waiting for is between (2^31 - 1) timer ticks in the past and 2^31 time ticks in the future. 2^31 timer ticks equates to roughly 21 seconds.

If you wanted to wait for longer than 21 seconds you would need to worry about overflow. For example if you wanted to wait for 30 seconds you would need to first wait for 21 seconds to pass using a conditional timer input and then wait for a further 9 seconds to pass with a second conditional timer input.


User avatar
larry
Respected Member
Posts: 275
Joined: Fri Mar 12, 2010 6:03 pm

Post by larry »

The timer will just a have a simple comparator against the set 32bit time. Normally you implement this by subtracting the two unsigned values and looking at the top bit.

Code: Select all

#define AFTER(condition, current) (((condition - current) & 0x80000000) != 0)