Time, no matter in any life situation, is a very important concept. Just imagine, if there were no time, almost everything in the world would go wrong. But at the same time, many scientists have also questioned whether time is real in the objective world. (It’s a little too far ~ haha)

To get back to the point, any operating system can’t run without time. Because the operating system needs to have a defining standard to plan the execution of various processes or threads, time is the unified standard. Through the passage of time, the operating system regularly checks whether the thread has reached the scheduling standard, regularly checks whether there are some scheduled tasks to be performed, and so on.

RT-Thread clock management has an extensive documentation for reference. Check out the following link: https://www.rt-thread.org/doc…

This article attempts to summarize the learning process of RT-thread clock management from the following aspects



Description of clock-related concepts

What is a clock beat? Any operating system needs to provide an artificial clock beat, usually called the system heartbeat, and the system heartbeat is periodically provided by a hardware timer. The beat of the clock is like the second hand of the clock in our life. Every second, the second hand moves one grid.

In the operating system, when a hardware timer is interrupted once, the global variable (RT_TICK), which records the clock’s ticks, accumulates. This variable increases, not decreases, because time is always moving forward. For example, if we initialize the hardware timer to interrupt every 1 millisecond, the RT_TICK will increase by 1 every 1 millisecond.

As shown in the figure above, the hardware timer breaks every 1 millisecond to produce a beat. If the system detects that a thread’s time slice runs out at the eighth beat, a thread scheduling is performed. If a timer is detected to be out of time at the n+1 tick, the timer task is started.

How does RT-thread implement clock beats? It is believed that many engineers know that there is a tick clock SysStick hardware timer inside the Cortex-M series microcontroller, and RT-thread uses this SysStick clock to trigger the timer interrupt, and then realize the global variable of the clock beat continuously self-increasing.

Timer management mechanism

In the single chip naked computer programming, usually use the hardware timer for counting, when the hardware timer count value meets the overflow condition, it will trigger the timer interrupt, and then we can handle the task in the timer interrupt.

In RT-Thread real-time operating system, a software timer mechanism is provided. The timing length of the software timer is in the unit of clock beat, and the timing length must be an integer multiple of the clock beat. The software timer can be set as a single or periodic trigger, as well as in HARD_TIMER mode or SOFT_TIMER mode.

Timeout timer HARD_TIMER mode, this mode of timer function, need under the context of interrupt execution, and to the requirement of timeout function and interrupt service function is consistent, that is to say, a timeout function execution time short enough, can’t hung thread executes, can’t go to apply for or release dynamic memory.

SOFT_TIMER mode. This mode starts a timer thread, and the timer timeout function is executed in the context of the timer thread. This mode is not as complicated as HARD_TIMER mode, because it is a timer thread doing the work and scheduling.

The timer module of RT-thread maintains an ordered timer chain list, which is used to manage the current active timer. Each time the clock beat is interrupted, the timer chain list will be detected to see if there is a timeout. RT-thread has made a detailed description of the working mechanism of the timer chain list, as shown in the figure below.

Search for orderly linked list, it is time consuming, so in order to speed up the list of search, RT – Thread on the basis of the original order list, joined the jump table algorithm, using this algorithm can speed up the search element on the list, and improve the efficiency of search, but the jump table algorithm is a kind of algorithm with “space” in time, will have certain memory consumption.



Timer related API functions

RT-Thread provides a series of API interface functions to facilitate developers to timer a series of operations, including: Create/initialize timers, start timers, run timers, and delete/deactivate timers. All timers are removed from the timer list after the timer timeout, and a periodic timer is added to the timer list when it starts again, depending on the timer parameter Settings. The timer state parameter that has timeout is changed each time the operating system clock interrupt occurs.



Timer application example

Timer related application examples, mainly in order to verify the above timer-related API function interface, there are two timer examples, respectively is the dynamic timer example and static timer example.

Example source code download links: https://github.com/embediot/r… https://gitee.com/embediot/rt…

Both the dynamic timer example and the static timer example create two timers, one for a single firing mode and one for a periodic firing mode.

In the timer_test.h header file, recompile the project source code by opening the corresponding macro definition switch and download it to the development board to verify the experimental phenomenon, as shown in the figure below.



Precautions for Timer Use

When using RT-thread timer, in order to ensure the normal operation of the timer, there should be the following precautions:

1. The clock beat of the system should be set according to different applications. The clock beat is generally 1-100ms. The smaller the value of the clock beat, the faster the frequency, and the greater the extra overhead of the system will be.

2. In the interrupt function of the system beat, it will constantly check the hardware timer chain list. If the timer timeout time arrives, it will deal with the corresponding timeout task.

3. Timer chain list hop algorithm, is to use space to exchange for time, so according to the actual hardware resources to set the number of hop table level, the number of jump surface layer macro definition RT_TIMER_SKIP_LIST_LEVEL default is 1.

4. The timer precision of RT-thread is determined by the clock beat, and the timer timeout must be an integer multiple of the clock beat. In the microcontroller of Cortex-M series, the working mechanism of SysStick can be used to obtain a delay lower than the clock beat.

5. An example function of a high precision delay below the clock beat is shown in the figure below.

Thanks for reading!