Method for optimizing performance of high-precision timer of operating system in hard real-time scenario

CN120335992BActive Publication Date: 2026-10-30粤港澳大湾区(广东)国创中心
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202510383152.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-03-28
Publication Date
2026-10-30
Estimated Expiration
2045-03-28

AI Technical Summary

Technical Problem

如果任务超时,可能会导致系统崩溃、设备损坏甚至人员伤亡等严重后果

Benefits of technology

本发明提供的硬实时场景下的操作系统高精度定时器性能优化实现方法,在复杂的系统运行环境中,高精度定时器的稳定性和准确性至关重要。本发明具备多方面突出优势,为系统的高效运行提供了有力保障。在系统运行过程中,长时间关中断操作往往会对高精度定时器产生不良影响。而本发明凭借独特的设计思路,巧妙地规避了这一问题。在不支持中断嵌套的系统里,其他中断也极有可能干扰高精度定时器的正常工作,导致计时偏差。但本发明通过创新的技术手段,成功避免了此类干扰,确保高精度定时器在各种复杂系统条件下都能稳定运行,维持极高的计时精度。无论是单次定时器场景,还是周期定时器场景,本发明都能够适配。在单次定时器应用中,其能够精准地触发一次计时操作,满足对特定时刻精准控制的需求。在周期定时器的应用场景下,又能以稳定的周期频率持续工作,为系统提供持续且精准的时间基准。在硬实时场景中,本发明所提供的高精度定时器展现出了精度优势。在对时间精度要求苛刻的硬实时系统中,如工业自动化控制、航空航天等领域,本发明能够提供更为精准的时间控制,极大地提升了系统的响应速度和运行稳定性,减少因计时误差导致的潜在风险,为各行业的高效、安全运行提供了可靠的技术支撑。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120335992B_ABST
    Figure CN120335992B_ABST
Patent Text Reader

Abstract

The application provides an operating system high-precision timer performance optimization implementation method in a hard real-time scenario, wherein a hardware timer driver is encapsulated and initialized in the operating system, and information such as a timing duration and a callback function is transmitted when the timer driver is called; the timer duration is recalculated according to the initialization information and the timing duration, so that the timer interrupt is slightly earlier than the expected timeout moment, thereby the callback can be shielded from the influence of other interrupts and the CPU is kept busy until the expected timeout moment, and then the real timeout callback function is called. Compared with the prior art, the application greatly improves the timer precision by sacrificing a small amount of CPU computing capacity and real-time performance, and better meets the demand of high timer precision.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of computer application technology, and more specifically, to a method for optimizing the performance of a high-precision timer in an operating system under hard real-time scenarios. Background Technology

[0002] With the rapid development of modern society, many industries have increasingly stringent performance requirements for equipment and systems. In industrial scenarios such as industrial control, autonomous driving, and communication systems, there are many tasks with extremely strict time requirements; these tasks are often referred to as real-time tasks. Real-time tasks require systems to respond within a specific time frame to ensure functional correctness and safety. Based on the severity of the consequences of a real-time task timeout or failure, real-time tasks can be further subdivided into hard real-time tasks and soft real-time tasks. Hard real-time tasks have the most stringent time requirements and must be completed within a specified time. If a task times out, it may lead to serious consequences such as system crashes, equipment damage, or even personal injury. While soft real-time tasks also require completion within a specific time, they do not cause catastrophic consequences and typically only affect system performance or user experience.

[0003] High-precision timers are a key guarantee for meeting high real-time requirements. To ensure timely completion of real-time tasks, systems typically rely on mechanisms such as low-latency interrupt handling, real-time scheduling algorithms, load control, and resource management. Timers, especially high-precision timers, are crucial for ensuring accurate processing of real-time tasks within a specified timeframe. In fields such as industrial control, autonomous driving, and communication systems, many tasks have extremely high time accuracy requirements. These tasks rely on timer interfaces of various precisions provided by the operating system, with high-precision timers being a core component ensuring the timely execution of time-critical tasks. Timers in real-time operating systems can be divided into hardware timers and software timers. Hardware timers are implemented in hardware, with timing functions provided by the chip itself. Generally, an external crystal oscillator provides the chip with an input clock. The chip provides a set of configuration registers to accept control inputs, and the chip's interrupt controller generates a time interrupt upon reaching the set time value. They offer high precision, reaching nanosecond levels. Software timers are a type of system interface provided by the operating system, implemented through software calculations. Built on top of hardware timers, they enable the system to provide timer services that are not limited by hardware timer resources. Currently, software timers are typically implemented based on the system's Systick, achieving millisecond-level precision control. High-precision timers are typically implemented by directly calling hardware timers. Summary of the Invention

[0004] This invention aims to overcome the shortcomings of the prior art and provide a method for optimizing the performance of high-precision timers in operating systems for hard real-time scenarios. This method provides an implementation of a high-precision timer in scenarios with extremely high timing accuracy requirements, achieving timing accuracy at the 1µs level.

[0005] The technical solution adopted in this invention is a method for optimizing the performance of high-precision timers in operating systems under hard real-time scenarios. It is commonly implemented based on real-time operating systems, but is equally applicable to hard real-time scenarios in general-purpose operating systems. The objective of this invention is achieved through at least one of the following technical solutions.

[0006] A method for optimizing the performance of a high-precision timer in a hard real-time operating system includes the following steps: encapsulating this method in the hardware timer driver of the deployment platform; initializing the hardware timer driver of this method; calling the high-precision timer interface of this invention, passing in the timing duration, callback function, and other information; calculating the actual timeout moment of the high-precision timer and setting the hardware timer based on the driver initialization information; after the hardware timer times out, calling the discrimination callback function of this method; in the discrimination callback function, busy waiting until the busy waiting time exceeds the busy waiting protection time; if the busy waiting time exceeds the busy waiting protection time, returning an error code; when the busy waiting time reaches the expected timeout moment, calling the passed-in timeout callback function.

[0007] Furthermore, in step S1, the operating system of the deployment platform is typically a real-time operating system, commonly found in scenarios with hard real-time requirements. The hardware timer receives a stable clock signal source and counts; when it increments to a specified value or decrements to 0, it issues an interrupt to notify the CPU that the timer has timed out and requires an immediate response.

[0008] The hardware timer driver encapsulates the method described in this invention for different types of hardware timers. After encapsulation, users can invoke the high-precision timer implemented by this method using the conventional timer invocation method. The method specifies the expected timeout duration. Break it down into setting compensation duration Actual timeout duration Busy waiting time It consists of three parts: the expected timeout duration equals the sum of the set compensation duration, the actual timeout duration, and the busy waiting duration, as shown in the formula: The hardware timer calls the discrimination callback function earlier than the expected timeout time to disable on-core interrupts in advance, preventing other interrupts from affecting the response of the high-precision timer interrupt. After the expected timeout time is reached, the discrimination callback function calls the passed-in timeout callback function. The set compensation duration is the duration required by the system to set the hardware timer. Due to the need for hardware timer control register writing operations, interrupt disabling and enabling operations, the actual effective time of the hardware timer will be later than the call time, resulting in a setting delay. The theoretical value of the set compensation duration is equal to the setting delay of the hardware timer, used to compensate for the hardware timer setting delay. The actual timeout duration is the timeout duration written into the hardware timer control register. The hardware timer issues a timeout interrupt based on the actual timeout duration, indicating to the CPU that the hardware timer has timed out; the actual timeout duration... The value equals the expected timeout duration. With set compensation duration Busy waiting time The difference is calculated using the formula: The busy-wait duration is the duration for waking up the timer in advance. Many interrupt-disabling operations exist in the system, and when the system is in an interrupt-disabling state, it cannot respond to any interrupts. When a hardware timer times out and issues an interrupt, if the system is in an interrupt-disabling state, it cannot respond to the hardware timer's interrupt request in a timely manner, causing interrupt processing delays and reducing timer accuracy. Similarly, in systems that do not support interrupt nesting, the system cannot respond to the hardware timer's interrupt request in a timely manner when in an interrupt state, leading to delayed timer callback function calls and reduced accuracy. Therefore, the purpose of busy-waiting is to ensure that the hardware timer issues a timer interrupt slightly earlier than the expected timeout time, thereby guaranteeing that when the expected timeout time arrives, the hardware timer interrupt service routine can hold the CPU and process the relevant interrupt in a timely manner.

[0009] Furthermore, the hardware timer driver also needs to be initialized. During initialization, the hardware timer's set duration and default busy-wait duration under the current environment need to be determined and set, or default values ​​should be used. One possible method for determination is to use the default timer scheme. The timeout duration is The hardware timer's call delay time for its timeout callback function is statistically analyzed, and the average delay time is used as an estimate of the hardware timer's set duration. The busy-wait duration can be determined during the initialization of the hardware timer driver or separately and then set as a default value. The busy-wait duration should be slightly larger than the maximum delay time of the hardware timer in the system. A rough method for determining the busy-wait duration during driver initialization is to apply the determined compensation duration to the driver and then set it again. The duration of each timeout is The hardware timer is used to count the delay duration of its timeout callback function call, and the maximum value is taken as the busy waiting time. The busy waiting time can also be determined through long-term experiments and the result is set as the default value.

[0010] Furthermore, the high-precision timer interface mentioned in step S3 is a re-encapsulated interface in the hardware timer driver according to the method of the present invention, which adds two processes, compensation setting and busy-wait, to the original hardware timer driver interface. Before calling this method, the hardware timer driver needs to be initialized, and the compensation duration and busy-wait duration are set using measured values ​​or default values. The passed callback function is the function that should be called after the expected timeout time is reached.

[0011] Furthermore, the driver initialization information includes the already set compensation duration and busy-wait duration. The actual timeout duration is obtained by subtracting the set compensation duration from the expected timeout duration, and then subtracting the busy-wait duration. The expected timeout time is calculated in the high-precision timer interface. After entering the interface function, the system counter is first accessed to obtain the current time value, and the expected timeout duration is obtained by adding the current time to the expected timeout duration. The system counter is usually globally unique and shared, and broadcast to all cores. This counter has high time accuracy, reaching the nanosecond level. In this invention, the time obtained by accessing the system counter is used as an estimate of the actual time.

[0012] Furthermore, the determination callback function is the interrupt service routine that responds to the hardware timer interrupt. When the hardware timer reaches its actual timeout duration, it sends an interrupt request to the CPU through the interrupt controller. After receiving the interrupt request, the CPU looks up the interrupt vector table according to the interrupt number and calls the corresponding interrupt service routine, which is the determination callback function registered in the interrupt vector table during the initialization of this method.

[0013] Furthermore, the main process of the callback function is to obtain the system clock and determine whether the expected timeout period has been reached. If not, the system clock is obtained again, and the determination continues. If the busy wait time exceeds the busy wait protection time, an error code is returned; if the expected timeout period is reached, the passed-in timeout callback function is invoked.

[0014] Furthermore, the busy-wait protection duration is a configurable value, which can use a default value or a user-provided setting to prevent indefinite busy-waiting due to unexpected situations. When the busy-wait duration exceeds the busy-wait protection duration, the busy-wait state is exited and a busy-wait timeout error code is returned.

[0015] Furthermore, the timeout callback function is the callback function that needs to be called after the expected timeout period is reached. Using a synchronous call method to invoke the timeout callback function avoids the impact of other interrupt services and reduces the overhead of synchronous operations, task scheduling, and context switching. In multi-core systems, it is bound to the slave core based on the hardware timer interrupt, further reducing the sacrifice in computing power and real-time performance.

[0016] The present invention also provides an embodiment of a computer storage medium storing a computer program that, when executed by a processor, implements the operating system high-precision timer performance optimization method in any of the hard real-time scenarios described above.

[0017] Beneficial effects This invention provides a method for optimizing the performance of high-precision timers in operating systems for hard real-time scenarios. In complex system operating environments, the stability and accuracy of high-precision timers are crucial. This invention offers several significant advantages, providing strong support for efficient system operation. During system operation, prolonged interrupt-disabling operations often negatively impact high-precision timers. However, this invention cleverly avoids this problem through its unique design. In systems that do not support interrupt nesting, other interrupts are also highly likely to interfere with the normal operation of high-precision timers, leading to timing deviations. However, this invention successfully avoids such interference through innovative technical means, ensuring stable operation of the high-precision timer under various complex system conditions and maintaining extremely high timing accuracy. This invention is adaptable to both single-timer and periodic timer scenarios. In single-timer applications, it can accurately trigger a single timing operation, meeting the need for precise control at a specific moment. In periodic timer applications, it can continuously operate at a stable periodic frequency, providing a continuous and accurate time reference for the system. In hard real-time scenarios, the high-precision timer provided by this invention demonstrates its accuracy advantage. In hard real-time systems with stringent time accuracy requirements, such as industrial automation control and aerospace, this invention can provide more precise time control, greatly improving the system's response speed and operational stability, reducing potential risks caused by timing errors, and providing reliable technical support for the efficient and safe operation of various industries. Attached Figure Description

[0018] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on the structures shown in these drawings without creative effort.

[0019] Figure 1This is a diagram illustrating the working steps of an embodiment of the present invention; Figure 2 This is an execution flowchart of an embodiment of the present invention; Figure 3 This is a schematic diagram illustrating the steps of an embodiment of the present invention; Figure 4 This is a schematic diagram of time division for a periodic mode timer according to an embodiment of the present invention. Detailed Implementation

[0020] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative effort are within the scope of protection of the present invention.

[0021] It should be noted that if the embodiments of the present invention involve directional indicators (such as up, down, left, right, front, back, etc.), the directional indicators are only used to explain the relative positional relationship and movement of the components in a specific posture. If the specific posture changes, the directional indicators will also change accordingly.

[0022] Furthermore, if the embodiments of this invention involve descriptions such as "first" or "second," these descriptions are for descriptive purposes only and should not be construed as indicating or implying their relative importance or implicitly specifying the number of technical features indicated. Thus, a feature defined with "first" or "second" may explicitly or implicitly include at least one of those features. Additionally, the use of "and / or" or "and / or" throughout the text includes three parallel solutions. For example, "A and / or B" includes solution A, solution B, or a solution where both A and B are satisfied simultaneously. Furthermore, the technical solutions of the various embodiments can be combined with each other, but this must be based on the ability of those skilled in the art to implement them. When the combination of technical solutions is contradictory or impossible to implement, it should be considered that such a combination of technical solutions does not exist and is not within the scope of protection claimed by this invention.

[0023] Example 1 refer to Figure 1 and Figure 3Specifically, this embodiment provides a method for optimizing the performance of a high-precision timer in a hard real-time operating system, including the following steps: encapsulating this method in the hardware timer driver of the deployment platform; initializing the hardware timer driver of this method; calling the high-precision timer interface of this invention, passing in information such as timing duration and callback function; calculating the actual timeout moment of the high-precision timer and setting the hardware timer according to the driver initialization information; after the hardware timer times out, calling the discrimination callback function of this method; in the discrimination callback function, busy waiting until the busy waiting time exceeds the busy waiting protection time; if the busy waiting time exceeds the busy waiting protection time, returning an error code; when the busy waiting time reaches the expected timeout moment, calling the passed-in timeout callback function.

[0024] Furthermore, the operating system of the deployment platform is the UniProton and the Chuanglong RK3588 development board. The hardware timer receives a stable clock signal and counts. When it increments to a specified value or decrements to 0, it alerts the CPU that the hardware timer has timed out and requires an immediate response. The hardware timer driver encapsulates the method described in this invention for the hardware timer on the development board. After encapsulation, users can invoke the high-precision timer implemented by this method using the conventional timer calling method.

[0025] The method will predict the timeout duration. Break it down into setting compensation duration Actual timeout duration Waiting time It consists of three parts: the expected timeout duration equals the sum of the set compensation duration, the actual timeout duration, and the busy waiting duration, as shown in the formula: The set compensation duration This sets the required duration for the hardware timer in the system. Because operations such as writing to the hardware timer control register and disabling / enabling interrupts are required, the actual effective time of the hardware timer will be later than the call time, resulting in a setting delay. The theoretical value of the compensation duration is equal to the hardware timer's setting delay and is used to compensate for this delay.

[0026] The actual timeout duration is the timeout duration written into the hardware timer control register. The hardware timer issues a timeout interrupt based on the actual timeout duration, indicating to the CPU that the hardware timer has timed out. The actual timeout duration is... The value equals the expected timeout duration. With set compensation duration Waiting time The difference is calculated using the formula: The busy-wait duration is the duration for waking up the timer in advance. Many interrupt-disabling operations exist in the system, and when the system is in an interrupt-disabling state, it cannot respond to any interrupts. When a hardware timer times out and issues an interrupt, if the system is in an interrupt-disabling state, it cannot respond to the hardware timer's interrupt request in a timely manner, causing interrupt processing delays and reducing timer accuracy. Similarly, in systems that do not support interrupt nesting, the system cannot respond to the hardware timer's interrupt request in a timely manner when in an interrupt state, leading to delayed timer callback function calls and reduced accuracy. Therefore, the purpose of busy-waiting is to ensure that the hardware timer issues a timer interrupt slightly earlier than the expected timeout time, thereby guaranteeing that when the expected timeout time arrives, the hardware timer interrupt service routine can hold the CPU and process the relevant interrupt in a timely manner.

[0027] Further, step S2 requires initializing the hardware timer driver. During initialization, it is necessary to determine or set the hardware timer's set duration and default busy-wait duration under the current environment. In this embodiment, the hardware timer's set duration is determined during initialization. A hardware timer with a timeout duration of 1ms for 1000 times is set using the default timer scheme. The call delay of its timeout callback function is statistically analyzed, and the average delay is taken as an estimate of the hardware timer's set duration. Using the above method, the set compensation duration of the hardware timer in this embodiment is measured to be 6µs.

[0028] The busy-wait duration can be determined during the initialization of the hardware timer driver, or it can be determined separately and set as a default value. The busy-wait duration should be slightly larger than the maximum delay of the hardware timer in the system. In this embodiment, the results of an additional long-term test are used as the busy-wait time setting. After 7,200,000 single-timer experiments with a duration of 1ms, the maximum call delay was measured to be 5µs. Therefore, the busy-wait time is set to 5.1µs.

[0029] Furthermore, the high-precision timer interface is a re-encapsulated interface in the hardware timer driver according to the method of the present invention, adding two processes—compensation setting and busy-wait—to the original hardware timer driver interface. Before calling this method, the hardware timer driver needs to be initialized, setting the compensation duration and busy-wait duration using measured values ​​or default values. The passed callback function is the function that should be called after the expected timeout time is reached.

[0030] Furthermore, the step-driven initialization information consists of the already set compensation duration and busy-wait duration. The actual timeout duration is obtained by subtracting the set compensation duration from the expected timeout duration, and then subtracting the busy-wait duration. In this embodiment, when the expected timing duration is 1000us, the actual timing duration is 1000us - 6us - 5.1us = 988.9us.

[0031] The expected timeout is calculated in the high-precision timer interface. After entering the interface function, the system counter is first accessed to obtain the current time value, and the expected timeout duration is obtained by adding the current time to the expected timeout duration.

[0032] The system counter, typically globally unique and shared, is broadcast to all cores. This counter has high time accuracy, down to the nanosecond level. In this invention, the time obtained by accessing the system counter is used as an estimate of the actual time.

[0033] Furthermore, the determination callback function is the interrupt service routine that responds to the hardware timer interrupt. When the hardware timer reaches its actual timeout duration, it sends an interrupt request to the CPU through the interrupt controller. After receiving the interrupt request, the CPU looks up the interrupt vector table according to the interrupt number and calls the corresponding interrupt service routine, which is the determination callback function registered in the interrupt vector table during the initialization of this method. In this embodiment, the interrupt number is 322.

[0034] Furthermore, the main process of the callback function is to obtain the system clock and determine whether the expected timeout period has been reached. If not, the system clock is obtained again, and the determination continues. If the busy wait time exceeds the busy wait protection time, an error code is returned; if the expected timeout period is reached, the passed-in timeout callback function is invoked.

[0035] Furthermore, the busy-wait protection duration is a configurable value, which can be a default value or a user-defined value, used to prevent indefinite busy-waiting caused by unexpected situations. When the busy-wait duration exceeds the busy-wait protection duration, the busy-wait state is exited and a busy-wait timeout error code is returned. In this embodiment, the busy-wait protection duration is set to 20µs. When the busy-wait duration exceeds 20µs, the busy-wait state is exited and a busy-wait timeout error code -1 is returned.

[0036] Furthermore, the timeout callback function is the callback function that needs to be invoked after the expected timeout period is reached. Using a synchronous invocation method to call the timeout callback function avoids the impact of other interrupt services and reduces the overhead of synchronous operations, task scheduling, and context switching.

[0037] Example 2 refer to Figure 2 and Figure 4 This embodiment provides a method for optimizing the performance of high-precision timers in a hard real-time scenario using a periodic mode in the UniProton real-time operating system. It runs on the Chuanglong RK3588 development board and can be used for periodic timing tasks in hard real-time scenarios.

[0038] The periodic mode timer differs significantly from the single-shot mode timer. The main difference is that the periodic mode timer only needs to be set once to trigger periodically, thus requiring only initial compensation settings. In this embodiment, when the hardware timer is set to periodic mode, upon reaching its timeout, it not only issues an interrupt request but also automatically reloads the hardware timer, eliminating the need for repeated settings.

[0039] Since the periodic mode timer automatically loads the timing period, if using Figure 1 The early wake-up mode sets a hardware timer. Since there is only a hardware setting delay in the first cycle, it will cause each cycle to be set ahead by a hardware setting compensation time, and the error will continue to accumulate, leading to timing errors.

[0040] In this embodiment, the general implementation scheme is similar to that of Embodiment 1, the main difference being that this embodiment will... Figure 1 The early wake-up setting scheme was changed to Figure 2 The solution involves delaying the target timeout. Essentially, it still involves setting the hardware timer's timeout earlier than the expected timeout, then keeping the CPU busy-waiting. The difference lies in... Figure 1 The solution achieves its goal by shortening the timer wake-up time. This embodiment... Figure 2 The solution achieves its goal by delaying the target's wake-up time.

[0041] In this embodiment, the busy-wait judgment in the timer interrupt service routine is similar to that in embodiment 1, until the busy-wait duration exceeds the busy-wait protection duration or the current time reaches the target timeout time.

[0042] like Figure 4 As shown, the timer setting time is denoted as . The timer takes effect at the following time: Timer number The timeout time is recorded as follows: The timeout of the target for the tth time is denoted as . .

[0043] In this embodiment, the first target timeout time The determination method is the time when the timer takes effect. Add a timer to set the delay duration Increase waiting time The formula is: In this embodiment, the actual timer delay duration is equal to the set compensation duration, which is 6µs. The busy wait duration is the same as in Embodiment 1, which is 5.1µs. The value of each subsequent target timeout is the sum of the previous timeout and the expected timeout duration; the formula is: After implementation according to the above scheme, the hardware timer will be woken up about one busy wait time before the target timeout time each time, thereby achieving the effect of disabling interrupts and holding the CPU for busy waiting. This scheme can significantly improve the timer accuracy by sacrificing a small amount of performance and the real-time performance of other interrupts.

[0044] Example 3 The present invention also provides an embodiment of a computer storage medium storing a computer program that, when executed by a processor, implements the operating system high-precision timer performance optimization method in any of the hard real-time scenarios described above.

[0045] Those skilled in the art will recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.

[0046] Those skilled in the art will readily understand that, for the sake of convenience and brevity, the specific working processes of the systems, devices, and units described above can be referred to the corresponding processes in the foregoing method embodiments, and will not be repeated here. Those skilled in the art will also readily understand that the various embodiments of this application have different focuses, and for the sake of convenience and brevity, the same or similar parts may not be repeated in different embodiments. Therefore, parts not described or not described in detail in one embodiment can be referred to the descriptions in other embodiments.

[0047] In the above embodiments, implementation can be achieved, in whole or in part, through software, hardware, firmware, or any combination thereof. When implemented using software, it can be implemented, in whole or in part, as a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, all or part of the processes or functions described in the embodiments of this application are generated. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium or transmitted through the computer-readable storage medium. The computer instructions can be transmitted from one website, computer, server, or data center to another website, computer, server, or data center via wired (e.g., coaxial cable, fiber optic, digital subscriber line (DSL) or wireless (e.g., infrared, wireless, microwave, etc.)) means. The computer-readable storage medium can be any available medium accessible to a computer or a data storage device such as a server or data center that integrates one or more available media. The available media may be magnetic media (e.g., floppy disks, hard disks, magnetic tapes), optical media (e.g., digital versatile discs (DVDs)), or semiconductor media (e.g., solid-state disks (SSDs)).

[0048] In complex system operating environments, the stability and accuracy of high-precision timers play a decisive role in the reliable operation of the system. The performance optimization method for high-precision timers in operating systems under hard real-time scenarios proposed in this invention, with its numerous significant advantages, becomes a powerful force in promoting efficient system operation. During system operation, prolonged interrupt-disabling operations often have a negative impact on high-precision timers. However, this invention cleverly circumvents this problem with its unique design concept. In systems that do not support interrupt nesting, other interrupts are highly likely to interfere with the normal operation of high-precision timers, leading to timing deviations. However, this invention, through innovative technical measures, successfully resists such interference, ensuring that the high-precision timer can operate smoothly under various complex system conditions, always maintaining extremely high timing accuracy. This invention is perfectly suited for both single-shot timer and periodic timer applications. In practical applications of single-shot timers, it can accurately trigger single-shot timing operations, fully meeting the need for precise control of specific moments. In applications of periodic timers, it can operate continuously at a stable periodic frequency, providing the system with a persistent and accurate time reference. In hard real-time scenarios where time accuracy requirements are extremely stringent, the high-precision timer performance optimization method for operating systems in hard real-time scenarios provided by this invention can achieve more precise time control in hard real-time systems with extremely high time accuracy requirements, such as industrial automation control and aerospace. This significantly improves the system's response speed, enhances operational stability, and effectively reduces potential risks caused by timing errors, providing a solid and reliable technical guarantee for the efficient and safe operation of various industries.

[0049] The above description is merely a preferred embodiment of the present invention and does not limit the patent scope of the present invention. Any equivalent structural transformations made using the contents of the present invention's specification and drawings under the inventive concept of the present invention, or direct / indirect applications in other related technical fields, are included within the patent protection scope of the present invention.

Claims

1. A method for optimizing the performance of a high-precision timer in an operating system under hard real-time scenarios, characterized in that, include: Encapsulate a high-precision timer performance optimization program in the hardware timer driver of the deployment platform; The initialization encapsulation includes a hardware timer driver with high-precision timer performance optimization programs; Call the high-precision timer interface of the deployment platform, and pass in the expected timeout duration and callback function data information; Based on the hardware timer driver initialization information, calculate the actual timeout moment of the high-precision timer and set the hardware timer; After the hardware timer times out, the discrimination callback function of the hardware timer driver, which encapsulates a high-precision timer performance optimization program, is called. In the judgment callback function, the system waits until the busy waiting time exceeds the busy waiting protection time. If the busy waiting time exceeds the busy waiting protection time, an error code is returned. If the busy waiting reaches the expected timeout, the passed-in timeout callback function is called. Expected timeout duration Break it down into setting compensation duration Actual timeout duration Waiting time It consists of three parts: the expected timeout duration equals the sum of the set compensation duration, the actual timeout duration, and the busy waiting duration, as shown in the formula: ; During initialization, it is necessary to determine and then set the hardware timer compensation duration and busy wait duration for the current environment, or use the default values. The default timer scheme should be used for setting. The timeout duration is For the hardware timer, the call delay of its timeout callback function is counted, and the average delay is taken as the estimated value of the hardware timer's compensation duration. During driver initialization, after the compensation duration measurement is completed, it is applied to the driver, and then set again. The timeout duration is The hardware timer is used to count the delay duration of its timeout callback function, and the maximum value is taken as the busy waiting time.

2. The method for optimizing the performance of high-precision timers in operating systems under hard real-time scenarios according to claim 1, characterized in that, The aforementioned driver initialization information refers to the already set compensation duration and busy waiting duration; the actual timeout duration is obtained by subtracting the set compensation duration from the expected timeout duration and then subtracting the busy waiting duration. The expected timeout time is calculated in the high-precision timer interface; after entering the interface function, the system counter is first accessed to obtain the current time value, and the expected timeout time is obtained by adding the expected timeout duration to the current time.

3. The method for optimizing the performance of high-precision timers in operating systems under hard real-time scenarios according to claim 2, characterized in that, The discrimination callback function is an interrupt service routine that responds to the hardware timer interrupt; when the hardware timer reaches its actual timeout duration, an interrupt request is sent to the CPU through the interrupt controller; after receiving the interrupt request, the CPU looks up the interrupt vector table according to the interrupt number and calls the corresponding interrupt service routine.

4. The method for optimizing the performance of high-precision timers in operating systems under hard real-time scenarios according to claim 3, characterized in that, The main process of the judgment callback function is to obtain the system clock and determine whether the expected timeout time has been reached; if not, obtain the system clock again and continue to make the judgment; when the busy wait time exceeds the busy wait protection time, return an error code; when the expected timeout time is reached, call the passed timeout callback function.

5. The method for optimizing the performance of high-precision timers in operating systems under hard real-time scenarios according to claim 4, characterized in that, The busy-wait protection duration is a configurable value, which can be a default value or a user-provided setting value. It is used to prevent indefinite busy-wait due to unexpected situations. When the busy-wait duration exceeds the busy-wait protection duration, the busy-wait state is exited and a busy-wait timeout error code is returned.

6. The method for optimizing the performance of high-precision timers in operating systems under hard real-time scenarios according to claim 5, characterized in that, The timeout callback function is the callback function that needs to be called after the expected timeout time is reached; the timeout callback function is called synchronously.

Citation Information

Patent Citations

  • Method for reducing jittering in POWERLINK SoC frame synchronization based on MCU

    CN103812635A