Timing processing method, device, equipment and storage medium for real-time operating system

By setting the hardware timer comparison value according to the actual processing time of the timing event and triggering an interrupt when it is reached, the energy consumption problem caused by meaningless interrupts in the embedded operating system is solved, and the time accuracy and interrupt response speed of the system are improved.

CN112199170BActive Publication Date: 2024-08-30GUANGZHOU ZHIYUAN ELECTRONICS CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202010930325.5
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2020-09-07
Publication Date
2024-08-30
Estimated Expiration
2040-09-07

AI Technical Summary

Technical Problem

Periodic interrupts generated by hardware timers in existing embedded operating systems are still activated when the system is sleeping, resulting in meaningless work that increases system energy consumption, and insufficient clock accuracy and interrupt response speed.

Method used

By determining the actual processing time based on the timing time of the timing event and the current system time, setting the hardware timer comparison value, and triggering an interrupt to process the timing event when the count value is reached, periodic interruptions are avoided and the hardware timer is only activated when needed.

Benefits of technology

It reduces system power consumption, improves time accuracy and interrupt response speed, ensures the continuity of task execution, and reduces clock recording errors.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN112199170B
    Figure CN112199170B
Patent Text Reader

Abstract

The embodiment of the present application discloses a timing processing method, device, equipment and storage medium of a real-time operating system. The technical solution provided by the embodiment of the present application determines the actual processing time of the timing event to be processed according to the timing time from the current system time, so as to determine the hardware timer comparison value of the actual processing time of the event, and when the hardware timer count value reaches the hardware timer comparison value, triggers the hardware timer comparison interrupt to call the function to process the timing event, and no longer generates interrupts periodically, but generates interrupts at the time point when the timing event needs to be processed, thereby saving system power consumption, ensuring the continuity of task execution, and not stopping the hardware timer when recording the system time value, and the clock accuracy is related to the counting frequency of the hardware timer, which effectively improves the system's time accuracy and interrupt response speed.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The embodiments of the present application relate to the field of computer technology, and in particular to a timing processing method, apparatus, device and storage medium for a real-time operating system. Background Art

[0002] In most existing embedded operating systems, a hardware timer is generally used to generate a periodic interrupt with a period of milliseconds to implement the system clock. The interval between two interrupts is the minimum clock unit, called the clock tick, or Tick for short. The hardware timer periodically generates interrupts to drive the scheduler, timing system, and other clock-related modules.

[0003] During most of the time when the operating system is running, except for system time maintenance, clock interrupts have no practical significance for the operation of the system and do not trigger process scheduling and timer events. When the system is in sleep mode to save power, this meaningless periodic clock interrupt will drive meaningless work of the system and increase unnecessary energy consumption. Summary of the invention

[0004] The embodiments of the present application provide a timing processing method, apparatus, device and storage medium for a real-time operating system to reduce system power consumption and improve the system's time accuracy and interrupt response speed.

[0005] In a first aspect, an embodiment of the present application provides a timing processing method of a real-time operating system, comprising:

[0006] Determine the actual processing time of the timed event based on the timed event and the current system time;

[0007] Based on the actual processing time of the timing event to be processed, a hardware timer comparison value is set, wherein the hardware timer count value is calculated by the hardware timer at a set frequency;

[0008] In response to the hardware timer count value reaching the hardware timer comparison value, a hardware timer comparison interrupt is triggered to process the timing event.

[0009] Further, the determining of the actual processing time for processing the timed event according to the timing time of the timed event and the current system time includes:

[0010] Obtain the current system time, add the time required to process the timed event to the current system time, and obtain the actual processing time required to process the timed event;

[0011] The timed events are inserted into the event chain list in the order of actual processing time.

[0012] Further, after setting the hardware timer comparison value based on the actual processing time of the timed event to be processed, the method further includes:

[0013] Determine whether the added timed event is located at the first position of all timed events in the event chain list;

[0014] If yes, update the hardware timer comparison value;

[0015] Otherwise, keep the current hardware timer compare value.

[0016] Further, the setting of the hardware timer comparison value based on the actual processing time of the timed event to be processed includes:

[0017] Determine the current system time according to the set frequency of the hardware timer and the count value of the hardware timer;

[0018] Determine whether the current system time is less than the actual processing time required to process the timed event;

[0019] If yes, then setting the hardware timer comparison value according to the actual processing time, and determining to start detecting the hardware timer comparison interrupt;

[0020] Otherwise, a hardware timer comparison interrupt is triggered to handle the timing event.

[0021] Further, after the hardware timer comparison interrupt is triggered in response to the hardware timer count value reaching the hardware timer comparison value to process the timing event, the method further includes:

[0022] Determine whether it is necessary to set the comparison interrupt of the next timed event;

[0023] If yes, then set the hardware timer comparison value for the next hardware timer comparison interrupt according to the actual processing time of the next timed event;

[0024] Otherwise, it is determined to stop detecting the hardware timer comparison interrupt.

[0025] Further, in response to the hardware timer count value reaching the hardware timer comparison value, triggering a hardware timer comparison interrupt to process the timing event includes:

[0026] In response to the hardware timer count value reaching the hardware timer comparison value, determining whether there is a timed event in the event chain list;

[0027] If there is a timed event in the event list, take out the first timed event in the event list, and determine whether the current system time reaches the actual processing time of the timed event;

[0028] If the current system time reaches the actual processing time of the timed event, the timed event is deleted from the event linked list and the callback function of the timed event is called to process the timed event.

[0029] Furthermore, if the current system time reaches the actual processing time of the timed event, the timed event is deleted from the event linked list and the callback function of the timed event is called to process the timed event, the method further includes:

[0030] Determine whether task switching is required based on the return value of the callback function, and re-determine whether there is a timed event in the event list;

[0031] If there is no timed event in the event list or the current system time has not reached the actual processing time of the timed event, the task is switched according to the return value of the callback function, and it is determined whether there is a timed event in the event list;

[0032] If there is a timed event in the event list, the next hardware timer comparison value is determined according to the actual processing time of the first timed event in the event list, and the task is switched according to the task switching requirements.

[0033] Furthermore, before determining the actual processing time for processing the timed event according to the timing time of the timed event and the current system time, the method further includes:

[0034] Determine whether the hardware timer overflows according to two consecutive hardware timer count values ​​of the hardware timer, so as to update the overflow accumulated value of the hardware timer;

[0035] The current system time is determined based on the overflow accumulated value of the hardware timer and the latest hardware timer count value.

[0036] In a second aspect, an embodiment of the present application provides a timing processing device for a real-time operating system, including a time determination module, an interrupt setting module, and an interrupt response module, wherein:

[0037] A time determination module is used to determine the actual processing time of the timed event according to the timing time of the timed event and the current system time;

[0038] An interrupt setting module, used for setting a hardware timer comparison value based on an actual processing time of a timing event to be processed, wherein the hardware timer count value is calculated by the hardware timer at a set frequency;

[0039] The interrupt response module is used to trigger a hardware timer comparison interrupt in response to the hardware timer count value reaching the hardware timer comparison value to process the timing event.

[0040] Furthermore, the time determination module is specifically used for:

[0041] Obtain the current system time, add the time required to process the timed event to the current system time, and obtain the actual processing time required to process the timed event;

[0042] The timed events are inserted into the event chain list in the order of actual processing time.

[0043] Furthermore, after the time determination module sets the hardware timer comparison value based on the actual processing time of the timed event to be processed, it also includes:

[0044] Determine whether the added timed event is located at the first position of all timed events in the event chain list;

[0045] If yes, update the hardware timer comparison value;

[0046] Otherwise, keep the current hardware timer compare value.

[0047] Furthermore, the interrupt setting module is specifically used for:

[0048] Determine the current system time according to the set frequency of the hardware timer and the count value of the hardware timer;

[0049] Determine whether the current system time is less than the actual processing time required to process the timed event;

[0050] If yes, then setting the hardware timer comparison value according to the actual processing time, and determining to start detecting the hardware timer comparison interrupt;

[0051] Otherwise, a hardware timer comparison interrupt is triggered to handle the timing event.

[0052] Furthermore, the device further comprises an interrupt update module, which is used for, after the interrupt response module triggers a hardware timer comparison interrupt in response to the hardware timer count value reaching the hardware timer comparison value to process the timing event:

[0053] Determine whether it is necessary to set the comparison interrupt of the next timed event;

[0054] If yes, then set the hardware timer comparison value for the next hardware timer comparison interrupt according to the actual processing time of the next timed event;

[0055] Otherwise, it is determined to stop detecting the hardware timer comparison interrupt.

[0056] Furthermore, the interrupt response module is specifically used for:

[0057] In response to the hardware timer count value reaching the hardware timer comparison value, determining whether there is a timed event in the event chain list;

[0058] If there is a timed event in the event list, take out the first timed event in the event list, and determine whether the current system time reaches the actual processing time of the timed event;

[0059] If the current system time reaches the actual processing time of the timed event, the timed event is deleted from the event linked list and the callback function of the timed event is called to process the timed event.

[0060] Furthermore, the interrupt response module is also used for:

[0061] Determine whether task switching is required based on the return value of the callback function, and re-determine whether there is a timed event in the event list;

[0062] If there is no timed event in the event list or the current system time has not reached the actual processing time of the timed event, the task is switched according to the return value of the callback function, and it is determined whether there is a timed event in the event list;

[0063] If there is a timed event in the event list, the next hardware timer comparison value is determined according to the actual processing time of the first timed event in the event list, and the task is switched according to the task switching requirements.

[0064] Furthermore, the device further comprises a time acquisition module, which is used for, before the time determination module determines the actual processing time to be processed of the timed event according to the timing time of the timed event and the current system time:

[0065] Determine whether the hardware timer overflows according to two consecutive hardware timer count values ​​of the hardware timer, so as to update the overflow accumulated value of the hardware timer;

[0066] The current system time is determined based on the overflow accumulated value of the hardware timer and the latest hardware timer count value.

[0067] In a third aspect, an embodiment of the present application provides a computer device, including: a memory and one or more processors;

[0068] The memory is used to store one or more programs;

[0069] When the one or more programs are executed by the one or more processors, the one or more processors implement the timing processing method of the real-time operating system as described in the first aspect.

[0070] In a fourth aspect, an embodiment of the present application provides a storage medium comprising computer executable instructions, which, when executed by a computer processor, are used to execute the timing processing method of the real-time operating system as described in the first aspect.

[0071] The embodiment of the present application determines the actual processing time when the timing event needs to be processed according to the timing time from the current system time to determine the hardware timer comparison value of the actual processing time of the event, and triggers the hardware timer comparison interrupt when the hardware timer count value reaches the hardware timer comparison value to call the function to process the timing event. The interrupt is no longer generated periodically, but is generated at the time point when the timing event needs to be processed, thereby saving system power consumption and ensuring the continuity of task execution. The hardware timer will not be stopped when recording the system time value. The clock accuracy is related to the counting frequency of the hardware timer, which effectively improves the system's time accuracy and interrupt response speed. BRIEF DESCRIPTION OF THE DRAWINGS

[0072] Figure 1 It is a flow chart of a timing processing method of a real-time operating system provided in an embodiment of the present application;

[0073] Figure 2 It is a schematic diagram of the system architecture of a timing processing system of a real-time operating system provided in an embodiment of the present application;

[0074] Figure 3 is a flowchart of another timing processing method of a real-time operating system provided in an embodiment of the present application;

[0075] Figure 4 This is a processing flow chart for adding a timed event provided by an embodiment of the present application;

[0076] Figure 5 It is a flow chart of processing a timing event in response to a comparison interrupt provided by an embodiment of the present application;

[0077] Figure 6 It is a structural diagram of a timing processing device of a real-time operating system provided in an embodiment of the present application;

[0078] Figure 7 It is a structural diagram of a computer device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0079] In order to make the purpose, technical scheme and advantages of the present application clearer, the specific embodiments of the present application are further described in detail below in conjunction with the accompanying drawings. It is understood that the specific embodiments described herein are only used to explain the present application, rather than to limit the present application. It should also be noted that, for the convenience of description, only the part related to the present application but not all the contents are shown in the accompanying drawings. Before discussing the exemplary embodiments in more detail, it should be mentioned that some exemplary embodiments are described as processes or methods depicted as flow charts. Although the flow chart describes each operation (or step) as a sequential process, many of the operations therein can be implemented in parallel, concurrently or simultaneously. In addition, the order of each operation can be rearranged. The process can be terminated when its operation is completed, but it can also have additional steps not included in the accompanying drawings. The process can correspond to a method, a function, a procedure, a subroutine, a subprogram, etc.

[0080] Figure 1 A flowchart of a timing processing method for a real-time operating system provided in an embodiment of the present application is given. The timing processing method for a real-time operating system provided in an embodiment of the present application can be executed by a timing processing device of a real-time operating system, and the timing processing device of the real-time operating system can be implemented by hardware and / or software and integrated in a computer device.

[0081] Figure 2 A schematic diagram of the system architecture of a timing processing system of a real-time operating system provided in an embodiment of the present application is given. Figure 2 As shown, the timing processing system of the embedded real-time operating system defines a system timer layer and a hardware abstraction layer that communicate with each other in software, which is used to implement a timing processing method of a real-time operating system provided in an embodiment of the present application.

[0082] Among them, the hardware abstraction layer provides a hardware timer with a count value of a set number of bits (for example, 32 bits, 64 bits). The hardware timer calculates at a set frequency to obtain the hardware timer count value, and the hardware timer has the characteristic of setting the time when the interrupt occurs without stopping the timer, and it takes effect immediately after setting.

[0083] This embodiment is described by taking a hardware timer with a 64-bit count value as an example. In other embodiments, the number of bits of the count value can be set according to actual needs. The hardware timer count value of the hardware timer starts counting from 0 at a set frequency. In this embodiment, a 64-bit count value is set, that is, the hardware timer count value of the hardware counter can count up to 2 64 -1.

[0084] Furthermore, the hardware counter can be set to generate an interrupt (comparison interrupt) when it reaches any specified count value. The hardware abstraction layer provides three interfaces to the system timer layer, which are used to set the time (or count value) when the comparison interrupt occurs, obtain the hardware timer count value of the current hardware timer, and obtain the set frequency of the hardware timer.

[0085] Furthermore, the system timer layer is mainly used to manage timed events and provide system time to the upper layer (operating system or application). The system timer layer can call the interface of the hardware abstraction layer and provide two functions to the upper layer: obtaining the current system time and adding and deleting timed events, and also managing the rotation between tasks of the same priority.

[0086] The following description is made by taking the timing processing method of the real-time operating system executed by the timing processing device of the real-time operating system as an example. Figure 1 , the timing processing method of the real-time operating system includes:

[0087] S101: Determine the actual processing time for processing the timed event according to the timing time of the timed event and the current system time.

[0088] Among them, a timed event is an event that needs to be processed after a set time has passed from the current moment. The processing method of a timed event is generally to call a callback function for processing. A callback function is a function called through a function pointer. For example, a function pointer (address) is passed as a parameter to another function. This function is used to call the function it points to, which is called the callback function.

[0089] Exemplarily, when a timing event needs to be processed, the system timer layer determines the timing time of the timing event. At the same time, the system timer layer determines the setting frequency of the current hardware timer and the hardware timer count value through the interface of the hardware abstraction layer, and the current system time can be determined according to the setting frequency and the hardware timer count value.

[0090] Furthermore, after determining the timing time of the timing event and the current system time, the system time corresponding to the timing time from the current system time is calculated, and the system time is the actual processing time determined to be required to process the timing event.

[0091] S102: Setting a hardware timer comparison value based on the actual processing time of the timing event to be processed.

[0092] The hardware timer count value is calculated by the hardware timer at a set frequency.

[0093] Exemplarily, the system timer layer determines the count value of the hardware timer when the actual processing time is reached based on the hardware timer count value corresponding to the current system time. The count value is the hardware timer comparison value. When the hardware timer count value reaches the hardware timer comparison value, a comparison interrupt occurs in the hardware timer.

[0094] Furthermore, after determining the hardware timer comparison value, the interface of the hardware abstraction layer is called to set the hardware timer comparison value. When the hardware timer count value of the hardware timer reaches the hardware timer comparison value, a comparison interrupt occurs in the hardware timer in the hardware abstraction layer, and an interrupt is requested to the system timer layer and the system timer layer's processing function (callback function) for the timing node is called.

[0095] S103: In response to the hardware timer count value reaching the hardware timer comparison value, trigger a hardware timer comparison interrupt to process the timing event.

[0096] Exemplarily, when a comparison interrupt occurs in the hardware timer in the hardware abstraction layer and the system timer layer calls the processing function of the timing node, it is confirmed that there is a timing event that needs to be processed, and the callback function of the corresponding timing event is called to respond to the interrupt request to process the timing event that reaches the timing time.

[0097] In the above, the actual processing time when the timing event needs to be processed is determined according to the timing time from the current system time, so as to determine the hardware timer comparison value of the actual processing time of the event, and when the hardware timer count value reaches the hardware timer comparison value, the hardware timer comparison interrupt is triggered to call the function to process the timing event. The interrupt is no longer generated periodically, but is generated at the time point when the timing event needs to be processed, thereby saving system power consumption and ensuring the continuity of task execution.

[0098] Based on the above embodiments, Figure 3 A flowchart of another timing processing method of a real-time operating system provided in an embodiment of the present application, wherein the timing processing method of the real-time operating system is a specific embodiment of the timing processing method of the real-time operating system. Figure 3 , the timing processing method of the real-time operating system includes:

[0099] S201: Determine the current system time according to the hardware timer count value of the hardware timer.

[0100] This embodiment is described by taking a hardware timer that provides a 64-bit count value as an example. For a hardware abstraction layer with a 64-bit hardware timer, the hardware timer can be used directly, and the current system time can be confirmed based on the set frequency of the hardware counter and the hardware timer count value (for example, the quotient of the hardware timer count value and the set frequency is used as the current system time).

[0101] However, there is usually no hardware timer with a 64-bit count value on a 32-bit MCU. In this case, a hardware counter with other bit numbers (such as 32 bits) can be expanded to a 64-bit hardware counter.

[0102] Specifically, for a 64-bit hardware counter obtained by extending a 32-bit hardware counter, determining the current system time according to a hardware timer count value of the hardware timer specifically includes:

[0103] S2011: Determine whether the hardware timer overflows according to two consecutive hardware timer count values ​​of the hardware timer, so as to update the overflow accumulated value of the hardware timer.

[0104] Exemplarily, during initialization, a variable rollover_cnt is defined as the high 32 bits of the 64-bit system time, and the variable is determined as the overflow cumulative value. Whenever the 32-bit hardware timer overflows, the value of rollover_cnt is increased by 1 to complete the update of the overflow cumulative value of the hardware timer.

[0105] Specifically, obtain the hardware timer count value read twice continuously by the current hardware timer, and record them as t1 and t2 respectively, and determine whether t1 is greater than t2. If so, re-obtain the hardware timer count value read twice continuously by the current hardware timer, otherwise, confirm that a hardware timer overflow event occurs and increase the value of rollover_cnt by 1.

[0106] The reason for judging whether t1 is greater than t2 in this embodiment is that when the count value is read twice in succession, a counter overflow event occurs, for example, t1=2 32 -1, t2=1, when using t2 time, the value of rollover_cnt needs to be increased by 1 and then calculated. By reading twice in succession and determining that t1<=t2, the count value of t2 is guaranteed to be available.

[0107] S2012: Determine the current system time according to the overflow accumulated value of the hardware timer and the latest count value of the hardware timer.

[0108] Specifically, the overflow accumulated value (rollover_cnt) is used as the upper 32 bits of the 64-bit count value and is combined with the count value of t2 to form the 64-bit count value of the hardware timer (ie, the hardware timer count value).

[0109] Furthermore, the current system time can be confirmed according to the set frequency of the hardware counter and the count value of the hardware timer. For example, the quotient of the count value of the hardware timer and the set frequency is used as the current system time.

[0110] S202: Determine the actual processing time for processing the timed event according to the timing time of the timed event and the current system time.

[0111] Figure 4 A processing flow chart for adding a timed event is provided in an embodiment of the present application. As shown in step 4, when a newly added timed event is detected, determining the actual processing time specifically includes steps S2021-S2022:

[0112] S2021: Acquire the current system time, add the timing time that the timed event needs to be processed to the current system time, and obtain the actual processing time that the timed event needs to be processed.

[0113] Specifically, when a timed event needs to be added, the current system time is obtained and the timed time to be scheduled is added, and the actual processing time that needs to be processed for this timed event is calculated.

[0114] S2022: Insert the timed event into the event linked list in the order of actual processing time.

[0115] Specifically, after determining the actual processing time of the timed event, the timed event is inserted into the event linked list in the order of the actual processing time (that is, the order of the size of the actual processing time). In this event linked list, the smaller the value of the actual processing time that the timed event needs to be processed, the higher the ranking, and the more it needs to be processed first.

[0116] In a possible embodiment, the event linked list provided in this embodiment sorts the timed events that need to be processed in the system in the order in which they need to be processed. The timed events that are arranged at the front of the linked list need to be processed first. When adding timed events, this event linked list is sorted. The sorting method can be to compare the actual processing time of two timed events. If the first actual processing time minus the second actual processing time is less than 0x80000000000000000 (this value is based on the hardware timer is a 64-bit count value), then the second actual processing time is sorted before the first actual processing time.

[0117] S2023: Determine whether the added timed event is located at the first position of all timed events in the event list. If yes, jump to step S2024, otherwise jump to step S2025.

[0118] Specifically, after inserting the timed event into the event linked list, it is determined whether the insertion position of the timed event is the first of all nodes in the event linked list, and then jumps to step S2024, otherwise jumps to step S2025.

[0119] S2024: Update the hardware timer comparison value.

[0120] If the insertion position of the timed event is the first of all nodes in the event linked list, the hardware timer comparison value set in the hardware abstraction layer needs to be updated to the hardware timer comparison value corresponding to the actual processing time in order to correctly process the first timed event.

[0121] Specifically, when it is determined that the insertion position of the timed event is the first of all nodes in the event linked list, jump to step S203 to determine the hardware timer comparison value and call the interface provided by the hardware abstraction layer (setting the comparison interrupt occurrence time) to set the hardware timer comparison value.

[0122] S2025: Maintain the current hardware timer comparison value.

[0123] If the insertion position of the timed event is not the first of all nodes in the event linked list, the hardware timer comparison value of the timed event that needs to be processed first has been set previously. The current hardware timer comparison value can be maintained to correctly process the timed event that ranks first.

[0124] Optionally, when the insertion position of the timed event is not the first of all nodes in the event linked list, the process may jump to step S204 to wait for the triggering of the hardware timer comparison interrupt to process the first timed event in the event linked list.

[0125] It is understandable that the actual processing time of the timed event node is in the count value of the hardware timer, rather than in the tick unit, which can improve the timing accuracy of the timed event. For example, the current system time is 2.3ms (the tick recorded in the system that records the system time in ticks is 2, with an error of 1 tick, while the hardware timer count value recorded in the system that records the system time in the hardware timer count value is 2.3ms*set frequency, and the error is the hardware timer count period).

[0126] Assuming that a timed event needs to be processed after 10ms, the Tick system will process the timed event when the absolute time value is 12ms (the Tick recorded in the system time is 12 at this time), and the timed event is processed 0.3ms in advance. However, in this embodiment, when the hardware timer count value of the hardware timer is (2.3ms+10ms)*the hardware timer setting frequency, a comparison interrupt occurs to process the timed event. At this time, the error of processing the timed event is at most 1 hardware timer count cycle in advance. At this time, timing and timekeeping are separated from Tick and are associated with the hardware timer count frequency, which improves the accuracy of system timing and system time, and this accuracy is related to the hardware timer count frequency.

[0127] S203: Setting a hardware timer comparison value based on the actual processing time of the timed event to be processed.

[0128] In this embodiment, setting the hardware timer comparison value specifically includes steps S2031-S2034:

[0129] S2031: Determine the current system time according to the set frequency of the hardware timer and the count value of the hardware timer.

[0130] Specifically, the quotient of the hardware timer count value and the set frequency is used as the current system time.

[0131] S2032: Determine whether the current system time is less than the actual processing time required to process the timed event. If so, jump to step S2033; otherwise, jump to step S2034.

[0132] Specifically, the current system time is obtained, and it is determined whether the current system time is less than the actual processing time of the newly added timed event. If the current system time is less than the actual processing time, jump to step S2033; otherwise, jump to step S2034.

[0133] S2033: Setting a hardware timer comparison value according to the actual processing time, and determining to start detecting a hardware timer comparison interrupt.

[0134] Specifically, when the current system time is less than the actual processing time of the newly added timed event, the hardware timer comparison value is set in the hardware timer of the hardware abstraction layer according to the actual processing time, and the detection of the hardware timer comparison interrupt is enabled to start the detection of the hardware timer comparison interrupt. After the detection of the comparison terminal is enabled, the hardware abstraction layer will compare the hardware timer count value with the hardware timer comparison value, and trigger the hardware timer comparison interrupt when the hardware timer count value is greater than or equal to the hardware timer comparison value.

[0135] S2034: Trigger a hardware timer comparison interrupt to process the timing event.

[0136] Specifically, when the current system time is greater than or equal to the actual processing time of the newly added timed event, the current system time has exceeded the actual processing time corresponding to the timed event, and an interrupt needs to be triggered immediately to process the timed event, and the callback function is called to process the timed event.

[0137] S204: In response to the hardware timer count value reaching the hardware timer comparison value, trigger a hardware timer comparison interrupt to process the timing event.

[0138] Figure 5 A flowchart of a method for processing a timing event in response to a comparison interrupt provided in an embodiment of the present application is shown in FIG. Figure 5 As shown, in this embodiment, triggering a hardware timer comparison interrupt to process the timing event specifically includes steps S2041-S2048:

[0139] S2041: In response to the hardware timer count value reaching the hardware timer comparison value, determine whether there is a timed event in the event chain table. If yes, jump to step S2042, otherwise jump to step S2046.

[0140] Specifically, when the hardware timer count value of the hardware timer in the hardware abstraction layer is greater than or equal to the set hardware timer comparison value, the hardware abstraction layer triggers the hardware timer comparison interrupt event and sends an interrupt request to the system timer layer to notify the system timer layer to call the callback function to process the timing event. After receiving the interrupt request, the system timer layer determines whether there is a timing event in the event chain list. If so, jump to step S2042, otherwise jump to step S2046.

[0141] S2042: Take out the first timed event in the event list.

[0142] Specifically, if there is a timed event in the event list, the first timed event in the event list is taken out, and the process jumps to step S2043.

[0143] S2043: Determine whether the current system time reaches the actual processing time of the scheduled event.

[0144] Specifically, the current system time is obtained, and it is determined whether the current system time is greater than or equal to the actual processing time of the first timed event retrieved. If so, the process jumps to step S2044; otherwise, the process jumps to step S2046.

[0145] S2044: Delete the timed event from the event linked list and call the callback function of the timed event to process the timed event.

[0146] Specifically, if the current system time reaches the actual processing time of the timed event, the timed event is deleted from the event linked list, and the callback function of the timed event is called to process the timed event.

[0147] In this embodiment, the deletion of a timed event only requires deleting the timed event to be deleted from the event chain list. The deletion of a timed event is divided into two cases: calling the deletion timed event interface when the timed event has not been processed and calling the deletion timed event interface after the timed event has been processed. In both cases, there is a need to access the system timed event chain list, and the deletion of the timed event needs to enter the critical area to ensure that the system timed event chain list is not destroyed.

[0148] S2045: Determine whether task switching is required based on the return value of the callback function.

[0149] Specifically, after calling the callback function to process the timed event, it is determined whether task switching is required based on the return value of the callback function. If task switching is not required, the process directly returns to step S2041.

[0150] If task switching is required, the callback_ret ​​variable is recorded as the need for task switching, and the system timer layer can determine whether task switching is required based on the callback_ret ​​variable.

[0151] Further, after determining whether task switching is required based on the return value of the callback function, re-determine whether there is a timed event in the event list, jump to step S2041, and loop again to take out the timed event of the first node from the event list and determine whether this node needs to be processed. The reason for the cyclic judgment is that there may be multiple timed event nodes with the same or similar count values, and multiple timed event nodes need to be processed in one processing.

[0152] A task is also called a thread. Each task is given a certain priority and has its own set of CPU registers and its own stack space.

[0153] S2046: Switch tasks according to the return value of the callback function.

[0154] If there is no timed event in the event list or the current system time has not reached the actual processing time of the timed event, the task is switched according to the return value of the callback function.

[0155] Specifically, when the callback_ret ​​variable indicates that task switching is required, find the highest priority task (the real-time operating system provided in this embodiment is scheduled based on priority. If there are tasks with the same priority as the highest priority task, the tasks with the same priority will run in turns using time slices, where the time slice refers to the time allocated by the operating system to task execution).

[0156] After finding the highest priority task or when task switching is not required, determine whether there is a task of the same priority as the highest priority task. If so, set the task switching requirement for rotating tasks of the same priority. After completing the setting of rotating tasks of the same priority or when there is no task of the same priority, jump to step S2047.

[0157] It is understandable that in this embodiment, there is no Tick concept, and it will not be determined whether to switch tasks every time a Tick interrupt arrives. In order to realize the function of rotating tasks with the same priority, a timed event node is set to complete the task switching function, and the timing value of this timed node is set to the time slice of the task rotation, and the callback function of the timed event node is set to the need for task switching. The timed node is added to the system event list, and the task switching will be performed after processing this timed event, so as to achieve the function of rotating tasks with the same priority in time slices.

[0158] S2047: Determine whether there is a timed event in the event list.

[0159] Specifically, determine whether there is a timed event in the event list. If so, jump to step S2048. Otherwise, end the current timed event processing flow and wait for the next hardware timer comparison interrupt to be triggered.

[0160] S2048: Determine the next hardware timer comparison value according to the actual processing time of the first timed event in the event chain list, and perform task switching according to the task switching requirement.

[0161] If there is a timed event in the event list, the next hardware timer comparison value is determined according to the actual processing time of the first timed event in the event list (the determination and setting of the hardware timer comparison value can refer to step S203), and whether task switching is required is determined according to the task switching requirement, and the switching task is executed in response to the task switching requirement that requires task switching.

[0162] S205: Setting the comparison interrupt of the next timed event.

[0163] After the processing of the timed event is completed, it is necessary to set the comparison interrupt of the next timed event in the event chain list. Specifically, the setting of the comparison interrupt of the next timed event includes steps S2051-S2053:

[0164] S2051: Determine whether it is necessary to set a comparison interrupt for the next timed event.

[0165] Specifically, it is determined whether there is any timed event in the event list. If there is any timed event, the process jumps to step S2052; if there is no timed event, the process jumps to step S2053.

[0166] S2052: Setting a hardware timer comparison value for the next hardware timer comparison interrupt according to the actual processing time of the next timing event.

[0167] If there are still timed events in the event list, obtain the timed time of the first timed event in the time list, and determine the hardware timer comparison value of the next hardware timer comparison interrupt based on the current system time and the timed time, notify the hardware abstraction layer to configure the hardware timer according to the hardware timer comparison value, and return to step S203 to determine whether the hardware timer count value reaches the hardware timer comparison value.

[0168] S2053: Stop detecting the hardware timer comparison interrupt.

[0169] If there is no timing event in the event chain list, the detection of the hardware timer comparison interrupt is disabled to stop the detection of the hardware timer comparison interrupt, and return to step S203 to wait for the addition of a new timing event to enable the detection of the hardware timer comparison interrupt.

[0170] In the above, the actual processing time of the timed event to be processed is determined according to the timing time from the current system time, so as to determine the hardware timer comparison value of the actual processing time of the event, and when the hardware timer count value reaches the hardware timer comparison value, the hardware timer comparison interrupt is triggered to call the function to process the timed event, and the interrupt is no longer generated periodically, but the interrupt is generated at the time point when the timed event needs to be processed, thereby saving system power consumption and ensuring the continuity of task execution. The hardware timer is used to complete the processing of the timed event and the system time record in the Tick system, and the hardware timer will not be stopped when recording the system time value, effectively reducing the cumulative error of the system time record, and effectively performing the rotation of tasks with the same priority, and the hardware timer will not be stopped when recording the system time value. The clock accuracy is related to the counting frequency of the hardware timer, which effectively improves the system's time accuracy and interrupt response speed.

[0171] Figure 6 A schematic diagram of the structure of a timing processing device of a real-time operating system provided in an embodiment of the present application is given. Figure 6 The timing processing device of the real-time operating system includes a time determination module 61, an interrupt setting module 62 and an interrupt response module 63.

[0172] Among them, the time determination module 61 is used to determine the actual processing time of the timing event that needs to be processed according to the timing time of the timing event and the current system time; the interrupt setting module 62 is used to set the hardware timer comparison value based on the actual processing time of the timing event that needs to be processed, and the hardware timer count value is calculated by the hardware timer at a set frequency; the interrupt response module 63 is used to trigger the hardware timer comparison interrupt in response to the hardware timer count value reaching the hardware timer comparison value to process the timing event.

[0173] In the above, the actual processing time when the timing event needs to be processed is determined according to the timing time from the current system time, so as to determine the hardware timer comparison value of the actual processing time of the event, and when the hardware timer count value reaches the hardware timer comparison value, the hardware timer comparison interrupt is triggered to call the function to process the timing event. The interrupt is no longer generated periodically, but is generated at the time point when the timing event needs to be processed, thereby saving system power consumption and ensuring the continuity of task execution.

[0174] In a possible embodiment, the time determination module 61 is specifically used for:

[0175] Obtain the current system time, add the time required to process the timed event to the current system time, and obtain the actual processing time required to process the timed event;

[0176] The timed events are inserted into the event chain list in the order of actual processing time.

[0177] In a possible embodiment, after the time determination module 61 sets the hardware timer comparison value based on the actual processing time of the timed event to be processed, it further includes:

[0178] Determine whether the added timed event is located at the first position of all timed events in the event chain list;

[0179] If yes, update the hardware timer comparison value;

[0180] Otherwise, keep the current hardware timer compare value.

[0181] In a possible embodiment, the interruption setting module 62 is specifically used for:

[0182] Determine the current system time according to the set frequency of the hardware timer and the count value of the hardware timer;

[0183] Determine whether the current system time is less than the actual processing time required to process the timed event;

[0184] If yes, then setting the hardware timer comparison value according to the actual processing time, and determining to start detecting the hardware timer comparison interrupt;

[0185] Otherwise, a hardware timer comparison interrupt is triggered to handle the timing event.

[0186] In a possible embodiment, the device further includes an interrupt update module, which is used for, after the interrupt response module 63 triggers a hardware timer comparison interrupt in response to the hardware timer count value reaching the hardware timer comparison value to process the timing event:

[0187] Determine whether it is necessary to set the comparison interrupt of the next timed event;

[0188] If yes, then set the hardware timer comparison value for the next hardware timer comparison interrupt according to the actual processing time of the next timed event;

[0189] Otherwise, it is determined to stop detecting the hardware timer comparison interrupt.

[0190] In a possible embodiment, the interrupt response module 63 is specifically used to:

[0191] In response to the hardware timer count value reaching the hardware timer comparison value, determining whether there is a timed event in the event chain list;

[0192] If there is a timed event in the event list, take out the first timed event in the event list, and determine whether the current system time reaches the actual processing time of the timed event;

[0193] If the current system time reaches the actual processing time of the timed event, the timed event is deleted from the event linked list and the callback function of the timed event is called to process the timed event.

[0194] In a possible embodiment, the interrupt response module 63 is further used for:

[0195] Determine whether task switching is required based on the return value of the callback function, and re-determine whether there is a timed event in the event list;

[0196] If there is no timed event in the event list or the current system time has not reached the actual processing time of the timed event, the task is switched according to the return value of the callback function, and it is determined whether there is a timed event in the event list;

[0197] If there is a timed event in the event list, the next hardware timer comparison value is determined according to the actual processing time of the first timed event in the event list, and the task is switched according to the task switching requirements.

[0198] In a possible embodiment, the device further includes a time acquisition module, which is used for: before the time determination module 61 determines the actual processing time to process the timed event according to the timing time of the timed event and the current system time:

[0199] Determine whether the hardware timer overflows according to two consecutive hardware timer count values ​​of the hardware timer, so as to update the overflow accumulated value of the hardware timer;

[0200] The current system time is determined based on the overflow accumulated value of the hardware timer and the latest hardware timer count value.

[0201] The embodiment of the present application also provides a computer device, which can integrate the timing processing device of the real-time operating system provided by the embodiment of the present application. Figure 7 Schematic diagram of a computer device provided in an embodiment of the present application. Figure 7 The computer device includes: an input device 73, an output device 74, a memory 72 and one or more processors 71; the memory 72 is used to store one or more programs; when the one or more programs are executed by the one or more processors 71, the one or more processors 71 implement the timing processing method of the real-time operating system provided in the above embodiment. The input device 73, the output device 74, the memory 72 and the processor 71 can be connected by a bus or other means. Figure 7 The example of connecting through bus is taken in the following.

[0202] The memory 72, as a computing device readable storage medium, can be used to store software programs, computer executable programs and modules, such as the program instructions / modules corresponding to the timing processing method of the real-time operating system described in any embodiment of the present application (for example, the moment determination module 61, the interrupt setting module 62 and the interrupt response module 63 in the timing processing device of the real-time operating system). The memory 72 may mainly include a storage program area and a storage data area, wherein the storage program area can store the operating system, at least one application required for the function; the storage data area can store data created according to the use of the device, etc. In addition, the memory 72 may include a high-speed random access memory, and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, or other non-volatile solid-state storage device. In some instances, the memory 72 may further include a memory remotely arranged relative to the processor 71, and these remote memories may be connected to the device via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network and a combination thereof.

[0203] The input device 73 may be used to receive input digital or character information and generate key signal input related to user settings and function control of the device. The output device 74 may include a display device such as a display screen.

[0204] The processor 71 executes various functional applications and data processing of the device by running the software programs, instructions and modules stored in the memory 72, that is, implements the timing processing method of the real-time operating system mentioned above.

[0205] The timing processing apparatus, device and computer of the real-time operating system provided above can be used to execute the timing processing method of the real-time operating system provided by any of the above embodiments, and have corresponding functions and beneficial effects.

[0206] An embodiment of the present application also provides a storage medium containing computer executable instructions, which, when executed by a computer processor, are used to execute a timing processing method for a real-time operating system as provided in the above embodiment, and the timing processing method for the real-time operating system includes: determining an actual processing time of a timing event that needs to be processed based on the timing time of the timing event and the current system time; setting a hardware timer comparison value based on the actual processing time of the timing event that needs to be processed, the hardware timer count value being calculated by the hardware timer at a set frequency; in response to the hardware timer count value reaching the hardware timer comparison value, triggering a hardware timer comparison interrupt to process the timing event.

[0207] Storage medium - any of various types of memory devices or storage devices. The term "storage medium" is intended to include: installation media, such as CD-ROM, floppy disk or tape device; computer system memory or random access memory, such as DRAM, DDR RAM, SRAM, EDO RAM, Rambus RAM, etc.; non-volatile memory, such as flash memory, magnetic media (such as hard disk or optical storage); registers or other similar types of memory elements, etc. Storage media may also include other types of memory or combinations thereof. In addition, the storage medium may be located in the first computer system in which the program is executed, or may be located in a different second computer system, which is connected to the first computer system via a network (such as the Internet). The second computer system can provide program instructions to the first computer for execution. The term "storage medium" may include two or more storage media that may reside in different locations (for example, in different computer systems connected by a network). The storage medium may store program instructions (for example, embodied as a computer program) that can be executed by one or more processors.

[0208] Of course, the storage medium containing computer executable instructions provided in the embodiment of the present application is not limited to the timing processing method of the real-time operating system as described above, and can also execute related operations in the timing processing method of the real-time operating system provided in any embodiment of the present application.

[0209] The timing processing apparatus, device and storage medium of the real-time operating system provided in the above embodiments can execute the timing processing method of the real-time operating system provided in any embodiment of the present application. For technical details not described in detail in the above embodiments, please refer to the timing processing method of the real-time operating system provided in any embodiment of the present application.

[0210] The above are only preferred embodiments of the present application and the technical principles used. The present application is not limited to the specific embodiments described herein, and various obvious changes, readjustments and substitutions that can be made by those skilled in the art will not deviate from the scope of protection of the present application. Therefore, although the present application is described in more detail through the above embodiments, the present application is not limited to the above embodiments, and may include more other equivalent embodiments without departing from the concept of the present application, and the scope of the present application is determined by the scope of the claims.

Claims

1. A timing processing method for a real-time operating system, characterized in that: include: Determine the actual processing time of the timed event based on the timed event and the current system time; Based on the actual processing time of the timed event to be processed, a hardware timer comparison value is set, which includes: determining the current system time according to the set frequency of the hardware timer and the hardware timer count value, judging whether the current system time is less than the actual processing time of the timed event to be processed, if so, setting the hardware timer comparison value according to the actual processing time, and determining to start detecting the hardware timer comparison interrupt, otherwise, triggering the hardware timer comparison interrupt to process the timed event, the hardware timer count value is calculated by the hardware timer at the set frequency; In response to the hardware timer count value reaching the hardware timer comparison value, a hardware timer comparison interrupt is triggered to process the timing event.

2. The timing processing method of a real-time operating system according to claim 1, characterized in that: Determining the actual processing time of the timed event according to the timing time of the timed event and the current system time includes: Obtain the current system time, add the time required to process the timed event to the current system time, and obtain the actual processing time required to process the timed event; The timed events are inserted into the event chain list in the order of actual processing time.

3. The timing processing method of a real-time operating system according to claim 2, characterized in that: After the hardware timer comparison value is set based on the actual processing time of the timed event to be processed, the method further includes: Determine whether the added timed event is located at the first position of all timed events in the event chain list; If yes, update the hardware timer comparison value; Otherwise, keep the current hardware timer compare value.

4. The timing processing method of a real-time operating system according to claim 1, characterized in that: After the hardware timer comparison interrupt is triggered in response to the hardware timer count value reaching the hardware timer comparison value to process the timing event, the method further includes: Determine whether it is necessary to set the comparison interrupt of the next timed event; If yes, then set the hardware timer comparison value for the next hardware timer comparison interrupt according to the actual processing time of the next timed event; Otherwise, it is determined to stop detecting the hardware timer comparison interrupt.

5. The timing processing method of a real-time operating system according to claim 1, characterized in that: In response to the hardware timer count value reaching the hardware timer comparison value, triggering a hardware timer comparison interrupt to process the timing event, comprising: In response to the hardware timer count value reaching the hardware timer comparison value, determining whether there is a timed event in the event chain list; If there is a timed event in the event list, take out the first timed event in the event list, and determine whether the current system time reaches the actual processing time of the timed event; If the current system time reaches the actual processing time of the timed event, the timed event is deleted from the event linked list and the callback function of the timed event is called to process the timed event.

6. The timing processing method of a real-time operating system according to claim 5, characterized in that: If the current system time reaches the actual processing time of the timed event, the timed event is deleted from the event linked list and the callback function of the timed event is called to process the timed event, the method further includes: Determine whether task switching is required based on the return value of the callback function, and re-determine whether there is a timed event in the event list; If there is no timed event in the event list or the current system time has not reached the actual processing time of the timed event, the task is switched according to the return value of the callback function, and it is determined whether there is a timed event in the event list; If there is a timed event in the event list, the next hardware timer comparison value is determined according to the actual processing time of the first timed event in the event list, and the task is switched according to the task switching requirements.

7. The timing processing method of a real-time operating system according to any one of claims 1 to 6, characterized in that: Before determining the actual processing time of the timed event according to the timing time of the timed event and the current system time, the method further includes: Determine whether the hardware timer overflows according to two consecutive hardware timer count values ​​of the hardware timer, so as to update the overflow accumulated value of the hardware timer; The current system time is determined based on the overflow accumulated value of the hardware timer and the latest hardware timer count value.

8. A timing processing device for a real-time operating system, characterized in that: It includes a time determination module, an interrupt setting module and an interrupt response module, wherein: A time determination module is used to determine the actual processing time of the timed event according to the timing time of the timed event and the current system time; An interrupt setting module, used for setting a hardware timer comparison value based on an actual processing time of a timing event to be processed, the interrupt setting module is specifically used for determining a current system time according to a set frequency of the hardware timer and a count value of the hardware timer, judging whether the current system time is less than an actual processing time of the timing event to be processed, if so, setting a hardware timer comparison value according to the actual processing time, and determining to start detecting a hardware timer comparison interrupt, otherwise, triggering a hardware timer comparison interrupt to process the timing event, the count value of the hardware timer being calculated by the hardware timer at a set frequency; The interrupt response module is used to trigger a hardware timer comparison interrupt in response to the hardware timer count value reaching the hardware timer comparison value to process the timing event.

9. A computer device, characterized in that: include: memory and one or more processors; The memory is used to store one or more programs; When the one or more programs are executed by the one or more processors, the one or more processors implement the timing processing method of the real-time operating system as described in any one of claims 1-7.

10. A storage medium containing computer executable instructions, characterized in that: The computer executable instructions are used to execute the timing processing method of the real-time operating system as described in any one of claims 1-7 when executed by a computer processor.

Citation Information

Patent Citations

  • Method and device for realizing timers

    CN102103520A