Timing method of software timer, electronic equipment and storage medium

Through software timer queue management and configuration of hardware timers, combined with periodic marking processing, the system efficiency reduction caused by frequent interruptions of hardware timers is solved, and efficient timing management and accurate timing accuracy are achieved.

CN120029737AInactive Publication Date: 2025-05-23沐曦科技(成都)有限公司
View PDF 5 Cites 0 Cited by

Patent Information

Application Number
CN202510496472.9
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-04-21
Publication Date
2025-05-23
Estimated Expiration
Not applicable · inactive patent

AI Technical Summary

Technical Problem

In multitasking systems, frequent interrupts triggered by hardware timers lead to reduced system execution efficiency, especially when tasks are not required.

Method used

All software timers are managed through the software timer queue. Each time the hardware timer is configured, the next expired software timer is selected, and after a hardware interrupt occurs, decide whether to remove the timer from the queue based on the periodic flag of the software timer.

Benefits of technology

The software timer that effectively avoids non-periodic states is configured to the hardware timer next time, reduces unnecessary hardware interrupts, improves the execution efficiency of the system, and maintains the timing accuracy to the hardware timer standard, with an error of less than 0.01.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120029737A_ABST
    Figure CN120029737A_ABST
Patent Text Reader

Abstract

The invention relates to the technical field of chip design, in particular to a timing method of a software timer, electronic equipment and a storage medium, a hardware timer is reconfigured through a software timer queue, and the step of reconfiguring the hardware timer each time comprises the following steps: traversing the software timer queue, searching the next target expiration time, and obtaining a target software timer bound with the target expiration time; modifying the interruption initiating time of the hardware timer into timeout time; when the hardware timer is interrupted, if the periodicity mark bound with the target software timer is in a non-periodicity state, removing the target software timer from the software timer queue; if the periodicity mark bound with the target software timer is in the periodicity state, the target software timer is not removed. Unnecessary hardware interruption caused by the fact that the software timer in the non-periodic state is configured to the hardware timer next time is avoided, and the execution efficiency of the system is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of chip design, and in particular to a timing method of a software timer, an electronic device and a storage medium. Background Art

[0002] Timers are an indispensable key component in modern embedded systems and computer architectures. They are widely used in time management, task scheduling, event triggering, system monitoring, and real-time operating systems (RTOS) to ensure that the system can perform specific tasks or operations at predetermined time intervals. Timers are mainly divided into two categories: hardware timers and software timers, each with unique advantages and applicable scenarios. Hardware timers are usually implemented by dedicated hardware circuits with high accuracy and reliability. They are directly connected to the system clock and can accurately generate time interrupts. The method of configuring hardware timers through software timers is to receive timing requests through the software timer, calculate the corresponding hardware timer configuration according to the type and parameters of the request, and then write these configurations to the hardware timer through system calls or driver interfaces. When the hardware timer reaches the preset time point, it will trigger a hardware interrupt and execute the preset operation to notify the software layer to perform the corresponding task or operation.

[0003] When the system includes multiple tasks that require processors, the system will select the software timer with the shortest interval between task executions to configure the hardware timer, and use the shortest interval as a benchmark to count other timings. Assume that the system needs to execute preset operation A every 1ms, and execute preset operation B every 100us after executing task A. At this time, the system will configure the trigger time of the hardware timer to 100us according to the configuration strategy of the hardware timer. This will cause the hardware timer to trigger an interrupt every 100us, and the interrupt will be triggered at 200us, 300us, and up to 900us after A is executed. However, the triggering of these interrupts does not require the execution of the corresponding tasks. When the system is frequently interrupted without executing tasks, it will directly reduce the execution efficiency of the system. Summary of the invention

[0004] In view of the above technical problems, the technical solution adopted by the present invention is: a timing method of a software timer, the method comprising the following steps: S100, obtaining all software timers in a software timer queue, each software timer being bound with its own expiration time, periodicity mark and preset operation to be executed; wherein the periodicity mark is a periodic state or a non-periodic state.

[0005] S200, when the interrupt of the hardware timer is cleared, reconfiguring the hardware timer according to the software timer queue, each step of reconfiguring the hardware timer includes: S210, traverse the software timer queue, find the next target expiration time, and obtain the target software timer bound to the target expiration time.

[0006] S220, obtaining a timeout time according to the expiration time of the target software timer and the current system time, and modifying the time when the hardware timer initiates an interrupt to the timeout time.

[0007] S230: When the timing of the hardware timer reaches the timeout period, the hardware timer generates an interrupt, and the interrupt is used to request the system to process the target preset operation bound to the target software timer.

[0008] S240, if the periodic flag bound to the target software timer is in a non-periodic state, remove the target software timer from the software timer queue; if the periodic flag bound to the target software timer is in a periodic state, do not remove it.

[0009] S250, clearing the interrupt of the hardware timer.

[0010] In addition, the present invention also provides a non-transitory computer-readable storage medium, in which at least one instruction or at least one program is stored, and the at least one instruction or the at least one program is loaded and executed by a processor to implement the above method.

[0011] In addition, the present invention also provides an electronic device, including a processor and the above-mentioned non-transitory computer-readable storage medium.

[0012] The present invention has at least the following beneficial effects: The embodiment of the present invention provides a timing method, electronic device and storage medium of a software timer, which manages all software timers through a software timer queue, and configures the next software timer to expire in the software timer queue to a hardware timer each time. After a hardware interrupt is generated, if the software timer configured to the hardware timer is in a non-periodic state, it needs to be removed from the software queue; otherwise, it does not need to be removed. This avoids the software timer in a non-periodic state from being configured to the hardware timer next time and thus generating unnecessary hardware interrupts, thereby improving the execution efficiency of the system. Its timing accuracy can meet the standard of the hardware timer, with an error of less than 0.01. BRIEF DESCRIPTION OF THE DRAWINGS

[0013] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.

[0014] Figure 1 A flowchart of a timing method of a software timer provided in an embodiment of the present invention; Figure 2 A flow chart of the steps of reconfiguring the hardware timer each time provided by an embodiment of the present invention. DETAILED DESCRIPTION

[0015] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of the present invention.

[0016] Unless otherwise defined, all technical and scientific terms used in the embodiments of the present invention have the same meanings as commonly understood by those skilled in the art.

[0017] See also Figure 1 , which shows a flow chart of a timing method of a software timer, the method comprising the following steps: S100, obtaining all software timers in a software timer queue, each software timer being bound with its own expiration time, periodicity mark and preset operation to be executed; wherein the periodicity mark is a periodic state or a non-periodic state.

[0018] Among them, the software timer is a timing mechanism based on software implementation, which uses the clock or timing function of the operating system to simulate the behavior of the hardware timer. The software timer allows you to specify a timing time, and when the timing time arrives, the system will automatically execute the preset operation.

[0019] The timing time generally refers to the time interval or period at which the software timer is set, and the time interval determines the expiration time of the software timer and triggers the operation.

[0020] The expiration time is the time point when the hardware timer initiates an interrupt to the system. That is, when the system clock is equal to the expiration time, the software timer initiates an interrupt to the system and automatically executes the preset operation. The expiration time is equal to the current system time when the software timer is executed plus the timing time.

[0021] In one embodiment, the preset operation is a predetermined event or the execution of a preset callback function. As an example, the predetermined event is a change in a certain state or the occurrence of a signal.

[0022] The software timer queue is a data structure used to manage and schedule multiple software timers. Each element in the queue is an index of a software timer, pointing to the location of the corresponding software timer in memory. The software timer queue allows the system to add, remove, and query timers, and schedule them according to the expiration time of the software timer to ensure that the preset operation is automatically executed at the appropriate time.

[0023] The periodic flag is used to mark the type of the current software timer. If it is a periodic software timer, the periodic flag is a periodic state; if it is a non-periodic software timer, the flag is a non-periodic state.

[0024] S200, when the interrupt of the hardware timer is cleared, reconfigure the hardware timer according to the software timer queue.

[0025] The hardware timer is used to time and initiate a hardware interrupt to the system at the scheduled time. The accuracy of the hardware timer determines the level of accuracy that the software timer can achieve.

[0026] In one implementation, all software timers in the software timer queue share one hardware timer.

[0027] It should be noted that each time the hardware timer interrupt is cleared, the hardware timer will be reconfigured according to the software timer queue. When the software timer queue is empty, the hardware timer will not be reconfigured. When there is a software timer in the software timer queue, the hardware timer will be reconfigured. This is an automatic cycle.

[0028] For further information, see Figure 2 , each step of reconfiguring the hardware timer includes: S210, traverse the software timer queue, find the next target expiration time, and obtain the target software timer bound to the target expiration time.

[0029] As an example, three types of scheduled tasks need to be executed in the system, so the execution time is configured through three software timers. The first software timer timer1 is a periodic timer that triggers the preset operation A every 1ms; the second software timer timer2 is a periodic timer that triggers the preset operation B every 30ms; the third software timer timer3 is a non-periodic timer that executes the preset operation C 500us after the preset operation A is executed. Therefore, first add timer1 and timer2 to the software timer queue. If the preset operation B bound to timer2 has just been executed, when traversing the software timer queue at this time, the expiration time bound to timer1 is the target expiration time of the next expiration. If the preset operation A bound to timer1 has just been executed, since the software timer of the preset operation C is added to the timer queue, the software timer queue includes three software timers at this time. When traversing the software timer queue, the expiration time bound to time3 is the target expiration time of the next expiration.

[0030] S220, obtaining a timeout time according to the expiration time of the target software timer and the current system time, and modifying the time when the hardware timer initiates an interrupt to the timeout time.

[0031] The timeout period is equal to the difference between the expiration time of the target software timer and the current system time, and is the time required from the start of the hardware timer to the hardware timer triggering an interrupt or reaching a preset condition.

[0032] S230: When the timing of the hardware timer reaches the timeout period, the hardware timer generates an interrupt, and the interrupt is used to request the system to process the target preset operation bound to the target software timer.

[0033] S240, if the periodic flag bound to the target software timer is in a non-periodic state, remove the target software timer from the software timer queue; if the periodic flag bound to the target software timer is in a periodic state, do not remove it.

[0034] It should be noted that, through the periodic marking of all software timers in the software timer queue, if the target software timer is periodic, it does not need to be removed from the software timer queue after a hardware interrupt is generated, and only needs to continue waiting for the next cycle. If the target software timer is non-periodic, the target software timer needs to be removed from the software timer queue to avoid it being configured to the hardware timer next time and thus generating unnecessary hardware interrupts, thereby improving the execution efficiency of the system.

[0035] S250, clear the interrupt of the hardware timer. S210-S250 are steps for completing a configuration of the hardware timer.

[0036] In one implementation manner, before S100, the method further includes: S10, adding a new software timer to the software timer queue, including: S11, obtaining a placeholder mark for each bit in the software timer queue, wherein the placeholder mark includes a placeholder state and a non-placeholder state. The placeholder state indicates that the bit has been occupied by other software timers, and the non-placeholder state indicates that the bit is not occupied by other software timers. The bit in the non-placeholder state can be used to store the index of the newly added software timer.

[0037] S12, obtaining a target position with a placeholder mark as a non-placeholder state.

[0038] S13, obtaining the expiration time of the newly added software timer, and binding the expiration time of the software timer, the periodic flag, and the preset operation to be executed.

[0039] S14, inserting the newly added software timer into the position of the target bit, and modifying the placeholder mark of the target bit to a placeholder state. By timely updating the placeholder mark of each bit in the software timer queue, conflicts in the insertion positions of other newly added software timers can be prevented.

[0040] In one embodiment, the configuration step of the periodic mark includes: when there is no dependency between the newly added software timer and other software timers, and the newly added software timer is a periodic software timer, configuring its periodic mark to a periodic state; when there is no dependency between the newly added software timer and other software timers, and the newly added software timer is a non-periodic software timer, configuring its periodic mark to a non-periodic state; when the newly added software timer depends on other software timers, and the timing time of the newly added software timer is less than the dependent software timer, configuring the periodic mark of the newly added software timer to a non-periodic state. When the newly added software timer depends on other software timers, and the timing time of the newly added software timer is greater than or equal to the dependent software timer, configuring the periodic mark of the newly added software timer to a periodic state.

[0041] Among them, the software timer in the periodic state does not need to be removed from the software timer queue after generating a hardware interrupt. The software timer in the non-periodic state needs to be removed from the software timer queue after generating a hardware interrupt, so as to avoid the software timer in the non-periodic state from frequently generating unnecessary hardware interrupts and improve the execution efficiency of the system.

[0042] In one implementation manner, before S100, the method further includes: S20, extending the maximum number of allocated software timers in the timer queue, including: S21, obtaining the maximum number of bits currently allocated to the timer queue.

[0043] S22, obtaining a preset number of expansions of the timer queue each time.

[0044] S23, obtaining the maximum number of bits of the timer queue after expansion according to the maximum number of bits currently allocated to the timer queue and the preset number, wherein the maximum number of bits after expansion is the maximum allocated number of the software timer.

[0045] The maximum allocation quantity of the software timer after the extension is equal to the sum of the current maximum allocation quantity and the preset quantity.

[0046] In one implementation, before S100, the method further includes: S30, modifying the timing time of the software timer, including: S31, searching the software timer queue to obtain the software timer to be modified.

[0047] S32: Calculate a new expiration time according to the new timing time of the timer to be modified and the current system time.

[0048] S33, modifying the expiration time of the software timer to be modified to a new expiration time.

[0049] It should be noted that, through S20, each time the software timer queue is expanded, a preset number of software timer allocation positions can be added.

[0050] In summary, the embodiment of the present invention provides a timing method for a software timer, which manages all software timers through a software timer queue, and each time the next software timer to expire in the software timer queue is configured to the hardware timer. After a hardware interrupt is generated, if the software timer configured to the hardware timer is in a non-periodic state, it needs to be removed from the software queue; otherwise, it does not need to be removed. This avoids the software timer in a non-periodic state from being configured to the hardware timer next time and thus generating unnecessary hardware interrupts, thereby improving the execution efficiency of the system. Its timing accuracy can meet the standard of the hardware timer, with an error of less than 0.01.

[0051] An embodiment of the present invention also provides a non-transitory computer-readable storage medium, which can be set in an electronic device to store at least one instruction or at least one program related to implementing a method in a method embodiment. The at least one instruction or the at least one program is loaded and executed by the processor to implement the method provided in the above embodiment.

[0052] An embodiment of the present invention further provides an electronic device, comprising a processor and the aforementioned non-transitory computer-readable storage medium.

[0053] An embodiment of the present invention further provides a computer program product, which includes program code. When the program product is run on an electronic device, the program code is used to enable the electronic device to execute the steps of the method according to various exemplary embodiments of the present invention described above in this specification.

[0054] Those skilled in the art can clearly understand that for the convenience and simplicity of description, only the division of the above-mentioned functional units and modules is used as an example. In actual applications, the above-mentioned functions can be assigned to different functional units and modules as needed, that is, the internal structure of the device can be divided into different functional units or modules to complete all or part of the functions described above.

[0055] Although some specific embodiments of the present invention have been described in detail by way of example, it should be understood by those skilled in the art that the above examples are for illustration only and are not intended to limit the scope of the present invention. It should also be understood by those skilled in the art that various modifications may be made to the embodiments without departing from the scope and spirit of the present invention. The scope of the present invention is defined by the appended claims.

Claims

1. A timing method of a software timer, characterized in that: The method comprises the following steps: S100, acquiring all software timers in a software timer queue, each software timer being bound with its own expiration time, periodicity mark, and preset operation to be executed; wherein the periodicity mark is a periodic state or a non-periodic state; S200, when the interrupt of the hardware timer is cleared, reconfiguring the hardware timer according to the software timer queue, each step of reconfiguring the hardware timer includes: S210, traversing the software timer queue, searching for the next target expiration time, and obtaining the target software timer bound to the target expiration time; S220, obtaining a timeout time according to the expiration time of the target software timer and the current system time, and modifying the time when the hardware timer initiates an interrupt to the timeout time; S230, when the timing of the hardware timer reaches the timeout time, the hardware timer generates an interrupt, and the interrupt is used to request the system to process the target preset operation bound to the target software timer; S240, if the periodic flag bound to the target software timer is in a non-periodic state, remove the target software timer from the software timer queue; if the periodic flag bound to the target software timer is in a periodic state, do not remove it; S250, clearing the interrupt of the hardware timer.

2. The method according to claim 1, characterized in that: Prior to S100, it also included: S10, adding a new software timer to the software timer queue, including: S11, obtaining a placeholder mark bound to each bit in the software timer queue, wherein the placeholder mark includes a placeholder state and a non-placeholder state; S12, obtaining a target position with a placeholder mark set to a non-placeholder state; S13, obtaining the expiration time of the newly added software timer, and binding the expiration time, the periodicity mark and the preset operation to be executed of the software timer; S14, inserting the newly added software timer into the position of the target bit, and modifying the placeholder mark of the target bit to a placeholder state.

3. The method according to claim 1, characterized in that The steps to configure periodic marking include: When there is no dependency relationship between the newly added software timer and other software timers, and the newly added software timer is a periodic software timer, configuring its periodic flag to a periodic state; When there is no dependency relationship between the newly added software timer and other software timers, and the newly added software timer is a non-periodic software timer, configuring its periodic flag to a non-periodic state; When a newly added software timer depends on other software timers, and the timing time of the newly added software timer is less than the dependent software timer, the periodic flag of the newly added software timer is configured to a non-periodic state; When a newly added software timer depends on other software timers, and the timing time of the newly added software timer is greater than or equal to the dependent software timer, the periodic flag of the newly added software timer is configured as a periodic state.

4. The method according to claim 1, characterized in that: Before the S100, the method further includes: S20, expanding the maximum number of allocated software timers in the timer queue, including: S21, obtaining the maximum number of bits currently allocated to the timer queue; S22, obtaining a preset number of each extension of the timer queue; S23, obtaining the maximum number of bits of the timer queue after expansion according to the maximum number of bits currently allocated to the timer queue and the preset number, wherein the maximum number of bits after expansion is the maximum allocated number of the software timer.

5. The method according to claim 1, characterized in that All software timers in the software timer queue share one hardware timer.

6. The method according to claim 1, characterized in that The preset operation is a predetermined event or the execution of a preset callback function.

7. The method according to claim 1, characterized in that Before S100, the method further includes: S30, modifying the timing time of the software timer, including: S31, searching the software timer queue to obtain the software timer to be modified; S32, calculating a new expiration time according to the new timing time of the software timer to be modified and the current system time; S33, modifying the expiration time of the software timer to be modified to a new expiration time.

8. A non-transitory computer-readable storage medium, wherein at least one instruction or at least one program is stored in the storage medium, characterized in that: The at least one instruction or the at least one program is loaded and executed by the processor to implement the method according to any one of claims 1 to 7.

9. An electronic device, characterized in that: The invention comprises a processor and the non-transitory computer-readable storage medium as claimed in claim 8.

Citation Information

Patent Citations

  • Software timer implementation method for embedded system

    CN108845872A

  • Timing method of virtual timer, apparatus thereof, and electronic apparatus

    CN109952560A

  • Timer implementation method and device

    CN110447013A

  • Flexible scheduling method and device for embedded RTOS and storage medium

    CN117667342A

  • Real-time operating system timer implementation method and device and electronic equipment

    CN119576491A