Select Page

Threads Assignment 4

Write an atomTimerDelayUntil() API function

  • atomTimerDelay() function results in the calling task entering into the Blocked state, and then remaining in the Blocked state, for the specified number of ticks from the time atomTimerDelay() was called. The time at which the task that called atomTimerDelay() exits the Blocked state is relative to when atomTimerDelay() was called.
  • Write an API function called atomTimerDelayUntil() which should result in the calling task entering into the Blocked state, and then remaining in the Blocked state, until an absolute time has been reached.
  • The task that calls atomTimerDelayUntil() exits the Blocked state exactly at the specified time, not at a time that is relative to when atomTimerDelayUntil() was called.

atomTimerDelayUntil() function prtotype:

void atomTimerDelayUntil(TickType_t * previousWakeTime,  TickType_t timeIncrement);

previousWakeTime: This parameter should be used on the assumption that atomTimerDelayUntil() is being used to implement a task that executes periodically and with a fixed frequency. In this case previousWakeTime holds the time at which the task last left the Blocked state (was ‘woken’ up). This time is used as a reference point to calculate the time at which the task should next leave the Blocked state. The variable pointed to by previousWakeTime should be updated automatically within the atomTimerDelayUntil() function; it would not normally be modified by the application code, other than when the variable is first initialized.

timeIncrement: This parameter is also named on the assumption that atomTimerDelayUntil() is being used to implement a task that executes periodically and with a fixed frequency – the frequency being set by the timeIncrement value. timeIncrement is specified in ‘ticks’. The MS_TO_TICKS() macro can be used to convert milliseconds to ticks.

How to Use:

/* Define a task that performs an action every 50 milliseconds. */
void periodic_thread(uint32_t entry_param)
{
    TickType_t lastWakeTime;
    const TickType_t period = MS_TO_TICKS( 50 );
 
    /* The lastWakeTime variable needs to be initialized with the current tick
        count. Note that this is the only time the variable is written to explicitly.
        After this assignment, lastWakeTime should be updated  within the
        atomTimerDelayUntil() function. 
   */
 
    lastWakeTime = atomTimeGet();
 
    /* Enter the loop that defines the task behavior. */
    while( 1 ) {  
        /* This task should execute every 50 milliseconds. Time is measured
            in ticks. lastWakeTime should be updated within the atomTimerDelayUntil()
            function so is not explicitly updated by the task.
        */
 
        /* Perform the periodic actions here. */     
 
        atomTimerDelayUntil(&lastWakeTime, period);	
    }
}

References

  1. Introduction to atomthreads
  2. atomthreads kernel Reference
  3. atomthreads Documentation