I attempting to create a good way to timeout during a cofunction. While doing so, I ran across some strange behavior of DelayMs (And also DelaySec). My question is at the end of this posting.
First, here’s my code:
// File: timeoutTest.c
// Description: test Rabbit cofunctions and timeouts
//
// Original Author: Eric McRae
#use “BLxS2xx.lib”
#use “stddefs.h”
// Function: tooLongTaskV
// Description: runs too long and aborts
// Arguments: none
// Returns: void
// Notes: cofunction
cofunc void
tooLongTaskV( void )
{
u32 startTimeL; // time this function was started
u8 bailB; // flag to signal timeout
int rtn; // return value from DelayMs
everytime // run these statement on every continuation pass
{
printf("tooLong continued at %d
", MS_TIMER);
if( (MS_TIMER - startTimeL) > 1000 )
{ // If we’ve been continuing for more than a second
bailB = 1; // set the bail flag
printf("tooLong is bailing at %d
", MS_TIMER);
}
}
startTimeL = MS_TIMER; // capture the starting time of the function
bailB = 0; // clear the bail flag
printf("TooLong starting at %d
", MS_TIMER);
while( (rtn = DelayMs(3000)) == 0 )
{ // spin waiting for Delay to eat up 3 seconds
if( bailB ) // but if we see the bail flag
{
printf("TooLong aborting at %d
", MS_TIMER);
abort; // bail out
}
}
// only get here if DelayMs expired
printf("tooLong finishing because DelayMs returned %d at %d
", rtn, MS_TIMER);
}
// Mainline starts here
void main()
{
int i, rtn;
// Initialize the controller
brdInit();
printf ("Starting timeout Test at %d
", MS_TIMER);
while( 1 )
{
costate // just one costate - loop on long task.
{
rtn = waitfordone tooLongTaskV();
printf("waitfordone returned %d
", rtn );
}
}
}
Here’s what gets printed:
Starting timeout Test at 50
TooLong starting at 77
tooLong finishing because DelayMs returned 1 at 88
waitfordone returned 1
TooLong starting at 129
tooLong finishing because DelayMs returned 1 at 145
waitfordone returned 1
TooLong starting at 186
tooLong finishing because DelayMs returned 1 at 202
waitfordone returned 1
TooLong starting at 242
tooLong finishing because DelayMs returned 1 at 258
waitfordone returned 1
TooLong starting at 300
tooLong finishing because DelayMs returned 1 at 316
waitfordone returned 1
Note that DelayMs(3000) seems to be returning true after just 10 - 15 mSec.
I tried it with DelaySec(3) and got the same behavior.
What I expected was to get lots of output from the everytime block until 1 second expired and then the function returns. If change the while … DelayMS to while(1) and add a yield if bailB is not true, that’s in fact what happens.
Why is DelayMs() returning non-zero immediately?