Understanding the Differences Between Relative and Absolute Delays in FreeRTOS

Follow+Star PublicNumber, don’t miss wonderful content

Understanding the Differences Between Relative and Absolute Delays in FreeRTOS

Author | strongerHuang

WeChat Public Account | Embedded Column

Delays in embedded software code are quite common, but there are many types of delays depending on what you use.

Embedded Column

1

A Delay Problem
Problem:Periodically (fixed time) to handle a certain task. How would you implement it?
For example: collect sensor data every 10ms, then calculate a result using an algorithm, and finally send it out via serial port.

For many readers who are used to bare-metal programming, the first thought might be: using a timer to trigger an interrupt every 10ms and handle it in the interrupt.

Interrupt functions are suitable for handling simple data but are not suitable for algorithms, communication, and other processes that require long CPU occupancy.

For places with high timing precision requirements, timers are suitable. However, as mentioned in this chapter, for periodic sensor data collection, the requirements are not very high, so we introduce the absolute delay discussed in this article.

In the real-time operating system FreeRTOS tasks, using vTaskDelayUntil for absolute delay can perfectly solve this problem.

Embedded Column

2

The Meaning of Relative and Absolute Delays

This article uses the relative delay function vTaskDelay and the absolute delay function vTaskDelayUntil in FreeRTOS to illustrate.

Relative Delay: Each delay starts from the execution of the function vTaskDelay() until the specified delay time (parameter: tick value) ends.
Absolute Delay: Refers to executing the task that calls the vTaskDelayUntil() function every specified time (parameter: tick value).
Text descriptions may not be intuitive enough, so the following sections will detail their differences using code examples, delay values (IO high and low change waveforms), and task execution diagrams.

Embedded Column

3

Differences Between Relative and Absolute Delays

Using actual code as an example: in a task, adding a 10ms system delay, then executing the task (about 1ms duration, example replaced with delay).

Relative Delay Code:
Understanding the Differences Between Relative and Absolute Delays in FreeRTOS
Absolute Delay Code:
Understanding the Differences Between Relative and Absolute Delays in FreeRTOS
Note:
1.The TestDelay function is used only for testing (1ms delay), replacing the time-consuming processes like data collection, algorithms, and sending.
2.The only difference between the two codes is the system delay: one is vTaskDelay(10); the other is vTaskDelayUntil(&xLastWakeTime, 10);.
3.The system clock frequency is 1000, meaning the above system delay is 10 ticks, which equals 10ms.
Upon seeing the code, did you think of the differences in their output results?
Let’s look at the differences in results: using pin PA0 to output high and low levels, we derive the delay time.
Relative Delay Result:
Understanding the Differences Between Relative and Absolute Delays in FreeRTOS
Absolute Delay Result:
Understanding the Differences Between Relative and Absolute Delays in FreeRTOS
The result is: The cycle of relative delay is the system delay of 10ms + the task execution time of about 1ms, totaling 11ms. The cycle of absolute delay is simply 10ms.

Embedded Column

4

Viewing the Differences in Another Way
If the previous differences are still unclear, let’s explain a more easily understandable distinction using text + task execution diagrams.
1. Relative Delay
First, look at the task execution diagram presented according to the above code:
Understanding the Differences Between Relative and Absolute Delays in FreeRTOS
This involves principles of task switching in operating systems and high-priority task preemption. If you are not familiar, please review until you understand before returning.
Upon powering on, the TEST task enters a delay (blocked) state, and the system executes other ready tasks. The FreeRTOS kernel periodically checks whether the block time of the TEST task has been reached; if it has, it sets the TEST task to a ready state. If the TEST task has the highest priority among ready tasks, it will preempt the CPU and execute the task body code again, looping continuously.
Each time the TEST task has a system delay, it starts counting from the call to the delay function vTaskDelay(), hence the name relative delay.
From the above diagram, we can see:
If an interrupt occurs during the execution of the TEST task, or if a higher-priority task preempts it, the execution cycle of the TEST task will be extended. Therefore, using the relative delay function vTaskDelay(), the TEST task cannot execute periodically.
2. Absolute Delay
Understanding the Differences Between Relative and Absolute Delays in FreeRTOS
The variable xLastWakeTime defined in the code is actually used to save the last system counter value (to facilitate checking whether the next delay time has arrived).
Comparing with the task execution diagram of the relative delay program above, we can see that the system delay time includes the program execution time. Even if an interrupt occurs midway or a higher-priority task interrupts, it will not affect the next execution time (i.e., this cycle will not change; of course, the interrupt time cannot exceed the system delay value).
Note: The image includes a note: Generally, the program execution time should be less than the total interval time (10ms).
If the interrupt time is too long and the delay exceeds the system delay, the program will execute immediately without further delay (the task will not block for delay).

———— END ————

Reply in the background with 『RTOS』『FreeRTOS』 to read more related articles.
FollowWeChat Public Account 『Embedded Column』, check the bottom menu for more content, reply “Join Group” to join the technical exchange group according to the rules.

Understanding the Differences Between Relative and Absolute Delays in FreeRTOS

Click “Read Original” to see more shares, welcome to share, collect, like, and view.

Leave a Comment

×