Recently, a reader asked the following question: Why is the default configuration for the RTOS system tick (Tick) set to 1000? Can I configure it to 100, 10000, or 2000?
Many beginners have this question, including myself when I first learned about RTOS. I was confused about what the different tick configurations meant and their impacts.Today, I will briefly discuss the RTOS system tick!
What is the System Tick?
The system tick (SysTick) is also referred to as clock pulse or system heartbeat in some contexts.
The operating system can switch between multiple tasks by relying on a system timer that generates interrupts at a **frequency to provide scheduling (context switching) to enable task switching.
This timer is what we refer to as the system tick in this article.
In earlier years, 51 and 430 microcontrollers running RTOS used a separate timer to provide the system tick.
To address the RTOS issue, the Cortex-M core comes with a built-in system tick timer.
You will find that many microcontrollers on the market come with the SysTick timer, such as those with Cortex-M0, M3, and M4 cores, and they can be easily used by calling the official API functions.
System Configuration File
Typically, the system tick (OS_TICKS) is located in the system configuration file, and configuring this file is an important step. (Some systems allow configuration through a graphical interface, which essentially configures the system configuration file)
For example, FreeRTOSConfig.h
Another example is the os_cfg.h file for ucos systems
OS_TICKS is generally configured to 1000, which is easily understood from the macro definition and comments, indicating the number of system ticks per second.
There are many configuration options here, and beginners may not understand some of the options’ meanings. It is advisable to use the official default configuration, and as you become more familiar, you will naturally understand these options.
What is the Appropriate Configuration for the System Tick?
Configuring the system tick to 1000 means that the system performs a round-robin scheduling every 1ms to check if there are higher-priority tasks to execute (and switch tasks).
What does 1ms mean?
You might think that 1ms is a very short time, but for an operating system, 1ms is quite long.
For a microcontroller with a 100M clock frequency, executing a scheduling (dozens of instructions) takes place in the microsecond range. So, how long is 1ms for the system?
Why is it configured to 1000?
Many people may wonder why it is set to 1000 instead of 100, 10000, or 2000?
1000 is a relatively suitable middle value; other values like 100, 10000, or 2000 are also possible, but they are not conducive to system performance and programming.
a. If the tick is too high, such as 10K or even 100K, it places a significant burden on the system because the scheduling itself will consume CPU time.
b. A 1ms tick is convenient for programming system delays. Values like 2k or 10k make it inconvenient to calculate delays.
vTaskDelay(1000);
If the tick value is 1000, it represents a delay of 1 second;
If the tick value is 2000, it represents a delay of 0.5 seconds, which is clearly not conducive to programming;
What are the impacts of configuring to other values?
Besides 1000, the only other values that make delay calculations easy are 1 and 1M. (Clearly1 or1M are not realistic)
Configuring to 1 means the system only responds once per second; is that still a real-time operating system?
Configuring to 1M means scheduling occurs every 1us, and the CPU is basically occupied with scheduling tasks, leaving no time for other operations.
Moreover, 100, 10000, or 2000 are also possible values, but they make delay calculations inconvenient.
Summary
1. For real-time operating systems, the SysTick should ideally be configured to 1000 unless there are special circumstances;
2. In systems where it is permissible, a higher SysTick value results in better real-time performance; conversely, lower values degrade real-time performance;
3. For processors with relatively low clock frequencies (e.g., below 10M), the SysTick value can be adjusted according to the first point;
—— The End ——Recommended Reading Click the blue text to jump☞ Do you need to understand the task state machine well when using FreeRTOS?☞ Step-by-step guide to running FreeRTOS on STM32F4
☞ Illustrated series on FreeRTOS principles: Basic framework of the task manager☞ What is the relationship between Fourier Transform, Laplace Transform, and Z Transform? Why do we need to transform?
Feel free to forward, comment, like, and share with your friends. Thank you for your support!