Design and Implementation of Timer/Timeout Mechanisms in Embedded Software

Follow and star “Embedded Development Notes”to not miss exciting content!

1. Background

In the process of embedded software program design, timeout (or timer) handling situations are frequently encountered. The basic handling idea is to perform related program processing when the time is up. Below are two program design solutions for timeout (or timer).

2. Solution One

The basic idea: A timer interrupt uses a variable TICK, with an interrupt interval time t. When preparing to start the timer, read the current TICK. During program execution, continuously read the current TICK information and calculate accordingly. Therefore, for time calculation, it is sufficient to compute the difference between the start STARTTICK and end ENDTICK. The time calculation is T=(ENDTICK-STARTTICK)*t;

Use a timer interrupt to handle an interrupt every t time, incrementing the time count value s_u32TCNT as shown in the figure below.

Design and Implementation of Timer/Timeout Mechanisms in Embedded Software

Define a structure in the program to save the timeout start and end times, as defined in the figure below.

Design and Implementation of Timer/Timeout Mechanisms in Embedded Software

In places where timeout handling is needed, obtain the current s_u32TCNT in real-time and assign it to u32EndTimeTick. Calculate the time difference between u32StartTimeTick and u32EndTimeTick to determine if the time has arrived. The program design example code is shown in the figure below;

Design and Implementation of Timer/Timeout Mechanisms in Embedded Software

3. Solution Two

The basic idea: Define a callback function and a callback registration function, registering the timer/timeout service function as a callback. Each time the timer interrupt executes, the callback function only needs to decrement the timing variable TCNT by 1. When TCNT reaches 0, the timer/timeout period has arrived, and the timeout flag is set. The application program only needs to check the flag to determine if the timer/timeout period has arrived;

The definitions of the callback function and callback registration function are shown in the figure below, where multiple timeout/timer callback functions can be registered in the callback function array.

Design and Implementation of Timer/Timeout Mechanisms in Embedded SoftwareDesign and Implementation of Timer/Timeout Mechanisms in Embedded Software

In the timer interrupt function, iterate through the processing. The example code for the timer interrupt function is shown in the figure below.

Design and Implementation of Timer/Timeout Mechanisms in Embedded Software

4. Comparison Summary

Solution One has the advantage of having less execution content in the interrupt execution unit, making the code operations easy to understand. However, the disadvantage is that real-time calculations of the start and end TICK differences in applications lead to lower code execution efficiency.

Solution Two has the advantage of registering timeout functions in callbacks, providing better program extensibility without excessive numerical calculations, resulting in relatively higher code execution efficiency. However, the disadvantage is that the timer interrupt needs to iterate through all registered callbacks, leading to relatively more execution content in the interrupt.

Original link: https://blog.csdn.net/qq_38767222/article/details/90374420

Design and Implementation of Timer/Timeout Mechanisms in Embedded Software If you find this article helpful, please give it a follow! Design and Implementation of Timer/Timeout Mechanisms in Embedded Software

Design and Implementation of Timer/Timeout Mechanisms in Embedded Software Please give a recommendation! Design and Implementation of Timer/Timeout Mechanisms in Embedded Software

Leave a Comment