Thank you for the quick response.
In the following example Task_1 will occasional run before its scheduled time.
#pragma CLOCK_FREQ 20000000
#define HTASK0 0
#define HTASK1 1
intcon.GIE = 1; // Global Inerrupt enable.
intcon.PEIE = 1; //Peripheral Interrupt Enable.
//Set interrupt Prioritys.
rcon.IPEN = 1;// Enable low Priority.
intcon2.TMR0IP = 0; // Timer0 set to low.
intcon.TMR0IE = 1; //enable TMR0 overflow bit.
Sys_Sleep( 2000 );
Sys_Sleep( 12000 );
t0con = 128;
SysCreateTask( HTASK0 , 5 , Task_0 );
SysCreateTask( HTASK1 , 5 , Task_1 );
SysStartTask( HTASK0 );
SysStartTask( HTASK1 );
while( 1 )
tmr0h = 0xF6;
tmr0l = 0x3B;
intcon.TMR0IF = 0;
What seems to be happening is while task_1 is running task_0 will reach its scheduled wake up time.
When Sys_Sleep is called from task_1 it will will erroneously be put into the sleep queue before task_0
if( i != SLEEP_QUEUE_HEAD ) // start reached
remainingSleep = GetTaskWakeUpTime( i ) - scheduler.os_tickCnt;
remainingSleep = 0;
// When the time diff in less than 0, we know that the task to insert times out before this task,
// so we insert before it in the queue
if( sleepTime >= remainingSleep )
The calculated value for the remaning sleep time of next task in the queue will be wrong because it has already passed its scheduled wake up time.
because the os_sleepUpdateHead has already been moved
the next time SysTimerIdleProcess() is called it will move both task_0 and task_1 into the run queue.
I would appreciate any feedback you could give me on this.