Timer hierarchical calling method and device and single-chip microcomputer

By adopting a hierarchical calling method in the microcontroller, the business layer, abstract layer and hardware layer work together to solve the problems of timer resource waste and poor adaptability, achieve efficient management of timing requirements and resource saving, and improve adaptability. sex.

CN118760520APending Publication Date: 2024-10-11REEMOON TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410849921.9
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-06-27
Publication Date
2024-10-11

AI Technical Summary

Technical Problem

In a single chip microcomputer, when the timing demand is large, the existing technology directly calls the underlying timer resources, resulting in a large demand for timer resources, resource waste and poor adaptability.

Method used

Using a hierarchical calling method, through the hierarchical architecture of the business layer, abstract layer and hardware layer, the business layer sends a timing service creation interface, the abstract layer creates a timing service and drives the hardware layer timer, and the abstract layer detects in real time that the timing task is completed and notifies The business layer realizes efficient management of timer resources.

Benefits of technology

It realizes that the timing requirements of the business do not depend on the number of timer resources, saves the timer resources of the microcontroller, improves the adaptability of the program to different types of microcontrollers, and simplifies the adaptation and transplantation work.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118760520A_ABST
    Figure CN118760520A_ABST
Patent Text Reader

Abstract

The invention provides a hierarchical calling method and device for timers and a single-chip microcomputer, and relates to the field of single-chip microcomputers. The method is applied to the single-chip microcomputer, a software layered architecture of the single-chip microcomputer comprises a service layer, an abstraction layer and a hardware layer, and hardware resources of the single-chip microcomputer comprise a timer. Compared with the prior art, the service layer, the abstraction layer and the hardware layer are used for calling the timer resources through the interface in sequence, so that the timing requirement of the service does not depend on the number of the timer resources, and the timer resources of the single-chip microcomputer are saved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of single-chip microcomputers, and in particular to a hierarchical calling method and device of a timer and a single-chip microcomputer. Background Art

[0002] The timer function is usually required to implement specific business needs in microcontroller applications, and developers usually design it to directly call the underlying timer resources to implement timing. In this way, when the timing demand is large, the demand for microcontroller timer resources is also large. Summary of the invention

[0003] The purpose of the present invention is to provide a hierarchical calling method, device and single chip microcomputer of a timer to improve the problems existing in the prior art.

[0004] The embodiments of the present invention can be implemented as follows:

[0005] In a first aspect, the present invention provides a hierarchical calling method of a timer, which is applied to a single-chip microcomputer, wherein the software hierarchical architecture of the single-chip microcomputer includes a service layer, an abstract layer and a hardware layer, and the hardware resources of the single-chip microcomputer include a timer, and the method includes:

[0006] When there is a timing requirement, the business layer sends a first call instruction for creating an interface for a timing service to the abstract layer;

[0007] The abstract layer creates a timing service based on the first call instruction, and sends a second call instruction for the timer interface to the hardware layer, so that the hardware layer drives the timer to start timing and feeds back a timing notification to the abstract layer every preset period;

[0008] The abstract layer creates a scheduled task based on the third calling instruction each time it receives a third calling instruction from the business layer for a newly added interface for a scheduled task;

[0009] For each target scheduled task created, the abstract layer starts to detect in real time whether the target scheduled task has been executed based on the scheduled notification of the hardware layer when the creation is completed, and when it is detected that the target scheduled task has been executed, it notifies the business layer that the target scheduled task has been executed.

[0010] Optionally, the step of the abstract layer creating a scheduled task based on the third calling instruction includes:

[0011] The abstract layer obtains the timing duration, callback interface and task type defined by the third calling instruction;

[0012] The abstract layer creates the scheduled task based on the task type, and associates the scheduled task with the scheduled time and the callback interface.

[0013] Optionally, for each target scheduled task created, the abstract layer starts to detect in real time whether the target scheduled task has been executed based on the timing notification of the hardware layer when the creation is completed, and when it is detected that the target scheduled task has been executed, notifies the business layer that the target scheduled task has been executed, including:

[0014] For each target scheduled task created, if the task type of the target scheduled task is repetitive, the abstract layer starts to continuously detect in real time whether the scheduled duration of the target scheduled task has arrived based on the scheduled notification of the hardware layer when the creation is completed;

[0015] The abstract layer sends a callback instruction for the callback interface of the target scheduled task to the business layer each time it detects that the scheduled duration of the target scheduled task has arrived;

[0016] When the business layer determines that the target scheduled task belongs to the repetitive scheduled task based on the callback instruction, it determines that the target scheduled task has completed one execution.

[0017] Optionally, when the business layer determines that the target scheduled task belongs to the repetitive scheduled task based on the callback instruction, after determining that the target scheduled task has completed one execution, the method further includes:

[0018] When the business layer confirms that the timing requirement of the timing duration corresponding to the target scheduled task has been met, the business layer sends a fourth call instruction for the scheduled task deletion interface to the abstract layer;

[0019] When receiving the fourth calling instruction, the abstract layer deletes the target scheduled task specified by the fourth calling instruction and releases the memory resources occupied by the target scheduled task.

[0020] Optionally, when the business layer determines that the target scheduled task belongs to the repetitive scheduled task based on the callback instruction, after determining that the target scheduled task has completed one execution, the method further includes:

[0021] When the business layer confirms that the duration requirement of the target scheduled task has changed, the business layer sends a fifth call instruction for the scheduled task update interface to the abstract layer;

[0022] The abstract layer obtains a new timing duration based on the fifth calling instruction and determines that the target scheduled task is a scheduled task to be updated;

[0023] The abstract layer sets the timing duration of the to-be-updated timing task to the new timing duration and then continues to execute.

[0024] Optionally, for each target scheduled task created, the abstract layer starts to detect in real time whether the target scheduled task has been executed based on the timing notification of the hardware layer when the creation is completed, and when it is detected that the target scheduled task has been executed, notifies the business layer that the target scheduled task has been executed, including:

[0025] For each target scheduled task created, if the task type of the target scheduled task is a one-time type, the abstract layer starts to detect in real time whether the scheduled duration of the target scheduled task has been reached based on the scheduled notification of the hardware layer when the creation is completed;

[0026] When the abstract layer detects that the timing duration of the target scheduled task has arrived, the abstract layer sends a callback instruction for the callback interface of the target scheduled task to the business layer;

[0027] When the business layer determines that the target scheduled task belongs to the one-time scheduled task based on the callback instruction, it determines that the target scheduled task has ended execution.

[0028] Optionally, when the business layer determines that the target scheduled task belongs to the one-time scheduled task based on the callback instruction, after determining that the target scheduled task has ended execution, the method further includes:

[0029] When the business layer confirms that the target scheduled task has finished execution, the business layer directly sends a fourth call instruction for the scheduled task deletion interface to the abstract layer;

[0030] When the abstract layer receives the fourth calling instruction, it deletes the target scheduled task specified by the fourth calling instruction and releases the memory resources occupied by the target scheduled task.

[0031] Optionally, the method further includes:

[0032] When the timing requirement is met, the business layer sends a sixth call instruction for a timing service destruction interface to the abstract layer;

[0033] The abstract layer destroys the timing service and releases the memory resources occupied by the timing service based on the sixth calling instruction, and sends a stop calling instruction for the timer interface to the hardware layer, so that the hardware layer controls the timer to stop timing.

[0034] In a second aspect, the present invention provides a hierarchical calling device of a timer, which is applied to a single-chip microcomputer, wherein the hardware resources of the single-chip microcomputer include a timer, and the device includes a service layer, an abstract layer and a hardware layer;

[0035] The business layer is used to send a first call instruction for creating an interface for a timing service to the abstract layer when there is a timing requirement;

[0036] The abstract layer is used to create a timing service based on the first call instruction, and send a second call instruction for the timer interface to the hardware layer, so that the hardware layer drives the timer to start timing and feeds back a timing notification to the abstract layer every preset period;

[0037] The abstract layer is also used to create a scheduled task based on the third calling instruction each time the business layer receives a third calling instruction for a newly added interface for a scheduled task;

[0038] For each target scheduled task created, the abstract layer is also used to start real-time detection of whether the target scheduled task has been executed based on the scheduled notification of the hardware layer when the creation is completed, and when it is detected that the target scheduled task has been executed, notify the business layer that the target scheduled task has been executed.

[0039] In a third aspect, the present invention provides a single chip microcomputer, which is used to implement the hierarchical calling method of the timer as described in the first aspect.

[0040] Compared with the prior art, the embodiment of the present invention provides a hierarchical calling method, device and single-chip microcomputer of a timer, the method is applied to a single-chip microcomputer, the software hierarchical architecture of the single-chip microcomputer includes a business layer, an abstract layer and a hardware layer, and the hardware resources of the single-chip microcomputer include a timer. The method is: when there is a timing demand, the business layer sends a first calling instruction for creating an interface for a timing service to the abstract layer; the abstract layer creates a timing service based on the first calling instruction, and sends a second calling instruction for the timer interface to the hardware layer, so that the hardware layer drives the timer to start timing and feeds back a timing notification to the abstract layer every preset period; the abstract layer creates a timing task based on the third calling instruction each time it receives a third calling instruction for a new interface of a timing task from the business layer; for each created target timing task, the abstract layer starts to detect in real time whether the target timing task has been executed based on the timing notification of the hardware layer when the creation is completed, and when it is detected that the target timing task has been executed, it notifies the business layer that the target timing task has been executed. Compared with the prior art, the present invention realizes the calling of timer resources through interfaces in a hierarchical manner, so that the timing demand of the business is not dependent on the number of timer resources, which helps to save the timer resources of the single-chip microcomputer. BRIEF DESCRIPTION OF THE DRAWINGS

[0041] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings required for use in the embodiments are briefly introduced below. It should be understood that the following drawings only show certain embodiments of the present invention and therefore should not be regarded as limiting the scope. For ordinary technicians in this field, other related drawings can be obtained based on these drawings without creative work.

[0042] Figure 1 A schematic diagram of a software layered architecture of a single-chip microcomputer provided in an embodiment of the present invention.

[0043] Figure 2 One of the flowcharts of a hierarchical calling method of a timer provided in an embodiment of the present invention.

[0044] Figure 3 The second flowchart of a hierarchical calling method of a timer provided in an embodiment of the present invention.

[0045] Figure 4 A third flowchart of a hierarchical calling method of a timer provided in an embodiment of the present invention.

[0046] Figure 5 A schematic diagram of the interaction between a business layer, an abstract layer and a hardware layer provided in an embodiment of the present invention. DETAILED DESCRIPTION

[0047] In order to make the purpose, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Generally, the components of the embodiments of the present invention described and shown in the drawings here can be arranged and designed in various different configurations.

[0048] Therefore, the following detailed description of the embodiments of the present invention provided in the accompanying drawings is not intended to limit the scope of the invention claimed for protection, but merely represents selected embodiments of the present invention. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0049] It should be noted that similar reference numerals and letters denote similar items in the following drawings, and therefore, once an item is defined in one drawing, further definition and explanation thereof is not required in subsequent drawings.

[0050] In addition, the terms “first”, “second”, etc., if used, are merely used to distinguish between the descriptions and should not be understood as indicating or implying relative importance.

[0051] It should be noted that, in the absence of conflict, the features in the embodiments of the present invention may be combined with each other.

[0052] See also Figure 1 , Figure 1 A schematic diagram of a software layered architecture of a single-chip microcomputer provided in an embodiment of the present invention. The software layered architecture of the single-chip microcomputer may include a business layer, an abstract layer, and a hardware layer. The hardware resources of the single-chip microcomputer involved in the present invention include a timer. Each layer is responsible for different functions. This software architecture facilitates program management, so that different parts of the system can be independently developed and tested.

[0053] The hardware layer provides a timer interface to the abstract layer, and the abstract layer provides the following five unified interfaces to the business layer:

[0054] 1. Timing service creation interface:

[0055] struct Timer_s*TimerCreate(struct TimerSave_s*timer_c)

[0056] 2. Scheduled service destruction interface:

[0057] void TimerDestroy(struct Timer_s*handle)

[0058] 3. New interface for scheduled tasks:

[0059] struct TimerId_s*TimerAdd(struct Timer_s*handle,void(*timer_cb)(void*args),void*args)

[0060] Note: timer_cb is a callback interface;

[0061] 4. Scheduled task deletion interface:

[0062] int32_t TimerDel(struct Timer_s*handle,struct TimerId_s*timer_id)

[0063] 5. Scheduled task update interface:

[0064] int32_t TimerUpdate(struct Timer_s*handle,struct TimerId_s*timer_id)

[0065] Please refer to Figure 2 , Figure 2A schematic flow chart of a hierarchical calling method of a timer provided in an embodiment of the present invention, the execution subject of the method is the above-mentioned single chip microcomputer, and the method includes the following steps S101 to S104:

[0066] S101. When there is a timing requirement, the business layer sends a first call instruction for creating an interface for a timing service to the abstract layer.

[0067] In this embodiment, when the business layer has a timing requirement and needs to start a timer, the business layer will first send a first call instruction for creating an interface for the timing service it provides to the abstract layer.

[0068] It can be understood that the timing requirement is based on the actual associated business requirements. For example, the business requirement is to control the LED light to flash once every 5 seconds and continue flashing for 10 minutes. This example is only an example and is not limited here.

[0069] S102: The abstract layer creates a timing service based on the first calling instruction, and sends a second calling instruction for the timer interface to the hardware layer, so that the hardware layer drives the timer to start timing and feeds back a timing notification to the abstract layer every preset period.

[0070] In this embodiment, the abstract layer creates a timing service in response to the first call instruction sent from the business layer, and sends a second call instruction for the timer interface to the hardware layer, so that the hardware layer drives the timer to start timing in response to the second call instruction and feeds back a timing notification to the abstract layer every preset period.

[0071] The size of the preset period may depend on the clock frequency of the single-chip microcomputer. For example, the preset period may be 10ms, 5ms, or 1ms, etc. This example is only an example and is not limited here.

[0072] S103. Each time the abstract layer receives a third call instruction from the business layer for a new interface for a scheduled task, the abstract layer creates a scheduled task based on the third call instruction.

[0073] In this embodiment, there may be multiple timing requirements in one business requirement. For each timing requirement, the business layer needs to send a third call instruction for a new interface for the scheduled task to the abstract layer, so that the abstract layer can create a scheduled task that matches the timing requirement based on the third call instruction.

[0074] S104. For each target scheduled task created, the abstract layer starts to detect in real time whether the target scheduled task has been executed based on the timing notification of the hardware layer when the creation is completed, and when it is detected that the target scheduled task has been executed, it notifies the business layer that the target scheduled task has been executed.

[0075] In this embodiment, when each scheduled task is created, the abstract layer can start to detect in real time whether the scheduled task has been executed based on the timing channel of the hardware layer, and the business layer needs to be notified when the execution is completed.

[0076] The hierarchical calling method of the timer provided by the embodiment of the present invention is that when there is a timing demand, the business layer sends a first calling instruction for creating an interface for the timing service to the abstract layer; the abstract layer creates the timing service based on the first calling instruction, and sends a second calling instruction for the timer interface to the hardware layer, so that the hardware layer drives the timer to start timing and feeds back a timing notification to the abstract layer every preset period; the abstract layer creates a timing task based on the third calling instruction each time it receives a third calling instruction for the newly added interface of the timing task from the business layer; for each created target timing task, the abstract layer starts to detect in real time whether the target timing task has been executed based on the timing notification of the hardware layer when the creation is completed, and notifies the business layer that the target timing task has been executed when it is detected that the target timing task has been executed. Compared with the prior art, the present invention realizes the calling of timer resources through interfaces in a hierarchical manner, so that the timing demand of the business is not dependent on the number of timer resources, which helps to save the timer resources of the single-chip microcomputer, and the hierarchical calling improves the adaptability of the program to different types of single-chip microcomputers.

[0077] In an optional implementation, for different durations and types of timing requirements, the timing duration and task type of the timing task that the abstract layer needs to create are also different. Correspondingly, the process of "the abstract layer creates a timing task based on the third call instruction" in the above step S103 may include the following sub-steps S1031 to S1032:

[0078] S1031, the abstract layer obtains the timing duration, callback interface and task type defined by the third calling instruction;

[0079] S1032: The abstract layer creates a scheduled task based on the task type, and associates the scheduled task with the scheduled time and the callback interface.

[0080] In this embodiment, the third call instruction sent by the business layer defines the timing duration, callback interface and task type, and the task type can be divided into two types: single type and repeated type. The abstract layer creates a scheduled task based on the acquired task type (uniquely identified by the task ID), and associates the task ID with the acquired task type, timing time and callback interface as its task attribute information.

[0081] If the task type in the third calling instruction is single type, then the scheduled task created by the abstraction layer is a single type scheduled task; if the task type in the third calling instruction is repetitive type, then the scheduled task created by the abstraction layer is a repetitive type scheduled task.

[0082] In an optional implementation, for target scheduled tasks that are repetitive scheduled tasks, the abstraction layer will continue to execute. Figure 3 , the sub-steps of the above step S104 may include S1041 to S1043:

[0083] S1041. For each target scheduled task created, if the task type of the target scheduled task is repetitive, the abstract layer starts to detect in real time and continuously whether the scheduled duration of the target scheduled task has been reached based on the scheduled notification of the hardware layer when the creation is completed;

[0084] S1042. The abstract layer sends a callback instruction for the callback interface of the target scheduled task to the business layer each time it detects that the scheduled duration of the target scheduled task has arrived.

[0085] S1043: When the business layer determines that the target scheduled task is a repetitive scheduled task based on the callback instruction, it determines that the target scheduled task has completed one execution.

[0086] In this embodiment, for repetitive timed tasks, the abstract layer will execute repeatedly, and each time the execution is completed (i.e., the timed duration is reached), a callback is made to the callback interface of the business layer. The callback instruction can carry the task ID, task type (repetitive) and timed duration of the target timed task, so that the business layer can determine which repetitive timed requirement with which timed duration has completed a timing based on the callback instruction, so as to realize the corresponding business requirements later.

[0087] Optionally, when the business layer determines that the target scheduled task corresponds to the scheduled duration, the abstract layer can delete the corresponding repetitive scheduled task. Figure 3 That is, after the above step S1043, the method may further include S105-S106:

[0088] S105. When the business layer confirms that the timing requirement of the timing duration corresponding to the target scheduled task has been met, the business layer sends a fourth call instruction for the scheduled task deletion interface to the abstract layer;

[0089] S106. When receiving the fourth calling instruction, the abstract layer deletes the target scheduled task specified by the fourth calling instruction and releases the memory resources occupied by the target scheduled task.

[0090] In this embodiment, based on the callback instruction in S1043, the business layer can obtain the task ID of the target scheduled task, and then generate a fourth call instruction for the scheduled task deletion interface based on the task ID of the target scheduled task, so that the abstract layer can delete the target scheduled task specified by the fourth call instruction based on the fourth call instruction and release the memory resources occupied by the target scheduled task.

[0091] In this way, when the business layer confirms that the timing requirement of a repetitive scheduled task has been met, it can promptly let the abstract layer delete the repetitive scheduled task and release the memory resources it occupies, avoiding redundant heaps of useless scheduled tasks that occupy memory. Optionally, when the business layer confirms that the duration of the repetitive timing requirement has changed, it can let the abstract layer modify the timing duration of the corresponding repetitive scheduled task. Please refer to Figure 3 That is, after the above step S1043, the method may further include S107 to S109:

[0092] S107, when the business layer confirms that the duration requirement of the target scheduled task has changed, the business layer sends a fifth call instruction for the scheduled task update interface to the abstract layer;

[0093] S108, the abstraction layer obtains a new timing duration based on the fifth call instruction and determines that the target scheduled task is a scheduled task to be updated;

[0094] S109: The abstract layer sets the timing duration of the scheduled task to be updated to the new timing duration and then continues to execute.

[0095] In this embodiment, based on the callback instruction in S1043, the business layer can obtain the task ID of the target scheduled task, and then generate a fifth call instruction for the scheduled task update interface based on the task ID of the target scheduled task and the new scheduled duration, so that the abstract layer can update the scheduled duration of the specified target scheduled task to the new scheduled duration based on the fifth call instruction, so as to continue executing the target scheduled task based on the new scheduled duration.

[0096] In this way, when the business layer confirms that the timing requirement corresponding to a repetitive scheduled task has changed, there is no need for the abstract layer to delete and recreate the scheduled task, but the abstract layer can flexibly modify the timing duration of the repetitive scheduled task.

[0097] In another optional implementation, for a target scheduled task that is a one-time scheduled task, the abstraction layer will only execute once. Figure 4 , the sub-steps of the above step S104 may include S104a to S104c:

[0098] S104a, for each target scheduled task created, if the task type of the target scheduled task is a one-time type, the abstract layer starts to detect in real time whether the scheduled duration of the target scheduled task has arrived based on the timing notification of the hardware layer when the creation is completed;

[0099] S104b, when the abstract layer detects that the target scheduled task has reached its scheduled duration, the abstract layer sends a callback instruction for the callback interface of the target scheduled task to the business layer;

[0100] S104c: When the business layer determines that the target scheduled task is a one-time scheduled task based on the callback instruction, it determines that the target scheduled task has ended execution.

[0101] In this embodiment, for a one-time scheduled task, the abstract layer will only execute it once, and call back the callback interface of the business layer once when the execution is completed (i.e., the scheduled duration is reached). The callback instruction can carry the task ID, task type (one-time type) and scheduled duration of the target scheduled task, so that the business layer can determine which one-time scheduled requirement with which scheduled duration has been completed based on the callback instruction, so as to realize the corresponding business requirements later.

[0102] Optionally, when the business layer receives the callback instruction of a one-time scheduled task sent by the abstract layer, it considers that the abstract layer has finished executing the scheduled task, that is, the timing requirement corresponding to the one-time scheduled task has been met, and the abstract layer can delete the scheduled task in time. Figure 4 After the above step S104c, the method may further include S111 to S112:

[0103] S111, when the business layer confirms that the target scheduled task has finished executing, the business layer directly sends a fourth call instruction for the scheduled task deletion interface to the abstract layer;

[0104] S112. When the abstract layer receives the fourth calling instruction, it deletes the target scheduled task specified by the fourth calling instruction and releases the memory resources occupied by the target scheduled task.

[0105] In this embodiment, in this embodiment, based on the callback instruction in S104c, the business layer can obtain the task ID of the target scheduled task, and then generate a fourth call instruction for the scheduled task deletion interface based on the task ID of the target scheduled task, so that the abstract layer can delete the target scheduled task specified by the fourth call instruction based on the fourth call instruction, and release the memory resources occupied by the target scheduled task.

[0106] In this way, when the business layer confirms that the timing requirement of a one-time scheduled task corresponding to the scheduled duration has been met, it will promptly let the abstract layer delete the one-time scheduled task and release the memory resources it occupies, avoiding the redundant heap of useless scheduled tasks that occupy memory.

[0107] In an optional implementation, when all timing requirements are met, the business layer can stop calling the timing resources to save timer resources. Correspondingly, the method can also include S201~S202:

[0108] S201, when the timing requirement is met, the business layer sends a sixth call instruction for the timing service destruction interface to the abstract layer;

[0109] S202: The abstract layer destroys the timing service based on the sixth calling instruction and releases the memory resources occupied by the timing service, and sends a stop calling instruction for the timer interface to the hardware layer, so that the hardware layer controls the timer to stop timing.

[0110] In this way, when the business layer confirms that all timing requirements have been met, it promptly instructs the abstract layer to destroy the timing service and release the memory resources it occupies. At the same time, the abstract layer promptly instructs the hardware layer to control the timer to stop timing, avoiding the timer from performing meaningless timing when there is no timing requirement at the business layer, thereby avoiding the waste of timer resources.

[0111] For ease of understanding, the following describes the hierarchical calling process by taking a specific timing requirement as an example.

[0112] In the optional example, assuming that the preset period is 10ms, taking the business requirement of controlling the LED light to flash once every 5 seconds and continuously flash for 1 minute as an example, there are two timing requirements: 1. Repeating timing with a timing duration of 5 seconds; 2. Single timing with a timing duration of 10 minutes.

[0113] In this case, please refer to Figure 5 , the interaction between the business layer, abstract layer and hardware layer is as follows:

[0114] 1. The business layer calls the timing service creation interface of the abstract layer (i.e., sends the first call instruction), so that the abstract layer creates a timing service and calls the timer interface of the hardware layer (sends the second call instruction), so that the hardware layer drives the timer to start counting and sends a timing notification to the abstract layer every 10ms;

[0115] 2. The business layer needs to send two third call instructions to the abstract layer. One is to let the abstract layer create a repetitive scheduled task (called scheduled task A) with a scheduled duration of 5 seconds, and the other is to let the abstract layer create a one-time scheduled task (called scheduled task A) with a scheduled duration of 10 minutes.

[0116] 3. For scheduled task A, from the moment of creation, the abstract layer starts to execute the scheduled task A: the number of times the scheduled notification is received is recorded in real time until the number of times is equal to 500, and it is determined that the scheduled task A has been executed once. At this time, the business layer is informed through the callback interface (the callback instruction for scheduled task A is sent every 5 seconds), and the number of times is cleared and recorded again to start the next execution of scheduled task A, and the cycle continues;

[0117] 4. For scheduled task B, starting from the creation completion, the abstract layer records the number of received scheduled notifications in real time until the number of received notifications is equal to 6000. It determines that the scheduled task B is completed and stops executing. At this time, the business layer is notified through the callback interface (after 1 minute, the callback instruction for scheduled task A is sent);

[0118] 5. When the business layer receives a callback instruction about scheduled task B (at this time, it can also receive a callback instruction about scheduled task A), it can confirm that both timing requirements have been met, that is, send two fourth call instructions to the abstract layer, so that the abstract layer deletes scheduled tasks A and B and releases the memory resources occupied by the two.

[0119] 6. Then the business layer sends a sixth call instruction to the abstract layer, so that the abstract layer destroys the timing service and releases the memory resources occupied by the timing service. At the same time, it sends a stop call instruction for the timer interface to the hardware layer, so that the hardware layer controls the timer to stop timing.

[0120] When explanation is needed, Figure 5 The content shown is only a brief description, and the above examples are only examples and are not limiting here.

[0121] It should be noted that the execution order of each step in the above method embodiment is not limited to that shown in the drawings, and the execution order of each step shall be based on the actual application situation.

[0122] Compared with the prior art, the embodiments of the present invention have the following beneficial effects:

[0123] In the present invention, the business layer, abstract layer, and hardware layer implement the calling of timer resources through interfaces in sequence, so that the timing requirements of the business are not dependent on the number of timer resources, reducing the number of timer resources used by the single-chip microcomputer. At the same time, the layered calling improves the adaptability of the program to different types of single-chip microcomputers. While ensuring that the business logic remains unchanged, it can be conveniently and quickly transplanted to different single-chip microcomputers, reducing the workload of adapting different single-chip microcomputers, or the same single-chip microcomputer only needs to be transplanted once.

[0124] In order to execute the corresponding steps in the above method embodiment and various possible implementation modes, an implementation method of a hierarchical calling device of a timer is provided below.

[0125] The embodiment of the present invention further provides a hierarchical calling device of a timer, which is applied to a single-chip microcomputer, the hardware resources of which include a timer, and the device includes a service layer, an abstract layer and a hardware layer, wherein:

[0126] The business layer is used to send a first call instruction for creating an interface for a timing service to the abstract layer when there is a timing requirement;

[0127] The abstract layer is used to create a timing service based on the first call instruction, and send a second call instruction for the timer interface to the hardware layer, so that the hardware layer drives the timer to start timing and feeds back a timing notification to the abstract layer every preset period;

[0128] The abstract layer is also used to create a scheduled task based on the third calling instruction each time the business layer receives a third calling instruction for a new interface for a scheduled task;

[0129] For each target scheduled task created, the abstract layer is also used to start real-time detection of whether the target scheduled task has been executed based on the timing notification of the hardware layer when the creation is completed, and when it is detected that the target scheduled task has been executed, notify the business layer that the target scheduled task has been executed.

[0130] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working process of the hierarchical calling device of the timer described above can refer to the corresponding process in the aforementioned method embodiment, and will not be repeated here.

[0131] The embodiment of the present invention further provides a single chip microcomputer, which is used to implement the hierarchical calling method of the timer introduced above.

[0132] In summary, the embodiment of the present invention provides a hierarchical calling method, device and single-chip microcomputer of a timer, which is applied to a single-chip microcomputer, and the software hierarchical architecture of the single-chip microcomputer includes a business layer, an abstract layer and a hardware layer, and the hardware resources of the single-chip microcomputer include a timer. The method is: when there is a timing demand, the business layer sends a first call instruction for creating an interface for a timing service to the abstract layer; the abstract layer creates a timing service based on the first call instruction, and sends a second call instruction for the timer interface to the hardware layer, so that the hardware layer drives the timer to start timing and feeds back a timing notification to the abstract layer every preset period; the abstract layer creates a timing task based on the third call instruction each time it receives a third call instruction from the business layer for a new interface for a timing task; for each target timing task created, the abstract layer starts to detect in real time whether the target timing task has been executed based on the timing notification of the hardware layer when the creation is completed, and when it is detected that the target timing task has been executed, it notifies the business layer that the target timing task has been executed. Compared with the prior art, the present invention realizes the calling of timer resources through interfaces in a hierarchical manner, so that the timing demand of the business is not dependent on the number of timer resources, which helps to save the timer resources of the single-chip microcomputer.

[0133] The above are only specific embodiments of the present invention, but the protection scope of the present invention is not limited thereto. Any changes or substitutions that can be easily thought of by a person skilled in the art within the technical scope disclosed by the present invention should be included in the protection scope of the present invention. Therefore, the protection scope of the present invention should be based on the protection scope of the claims.

Claims

1. A hierarchical calling method for timers, It is characterized in that Applied to a single-chip microcomputer, the software layered architecture of the single-chip microcomputer includes a business layer, an abstraction layer and a hardware layer, the hardware resources of the single-chip microcomputer include a timer, and the method includes: When there is a timing requirement, the business layer sends a first call instruction for creating an interface for a timing service to the abstract layer; The abstract layer creates a timing service based on the first call instruction, and sends a second call instruction for the timer interface to the hardware layer, so that the hardware layer drives the timer to start timing and feeds back a timing notification to the abstract layer every preset period; The abstract layer creates a scheduled task based on the third calling instruction each time it receives a third calling instruction from the business layer for a newly added interface for a scheduled task; For each target scheduled task created, the abstract layer starts to detect in real time whether the target scheduled task has been executed based on the scheduled notification of the hardware layer when the creation is completed, and when it is detected that the target scheduled task has been executed, it notifies the business layer that the target scheduled task has been executed.

2. The method according to claim 1, It is characterized in that The step of the abstract layer creating a scheduled task based on the third call instruction includes: The abstract layer obtains the timing duration, callback interface and task type defined by the third calling instruction; The abstract layer creates the scheduled task based on the task type, and associates the scheduled task with the scheduled time and the callback interface.

3. The method according to claim 2, It is characterized in that For each target scheduled task created, the abstract layer starts to detect in real time whether the target scheduled task has been executed based on the timing notification of the hardware layer when the creation is completed, and when the target scheduled task is detected to be executed, notifies the business layer that the target scheduled task has been executed, including: For each target scheduled task created, if the task type of the target scheduled task is repetitive, the abstract layer starts to continuously detect in real time whether the scheduled duration of the target scheduled task has arrived based on the scheduled notification of the hardware layer when the creation is completed; The abstract layer sends a callback instruction for the callback interface of the target scheduled task to the business layer each time it detects that the scheduled duration of the target scheduled task has arrived; When the business layer determines that the target scheduled task belongs to the repetitive scheduled task based on the callback instruction, it determines that the target scheduled task has completed one execution.

4. The method according to claim 3, It is characterized in that When the business layer determines that the target scheduled task belongs to the repetitive scheduled task based on the callback instruction, after determining that the target scheduled task has completed one execution, the method further includes: When the business layer confirms that the timing requirement of the timing duration corresponding to the target scheduled task has been met, the business layer sends a fourth call instruction for the scheduled task deletion interface to the abstract layer; When receiving the fourth calling instruction, the abstract layer deletes the target scheduled task specified by the fourth calling instruction and releases the memory resources occupied by the target scheduled task.

5. The method according to claim 3, It is characterized in that When the business layer determines that the target scheduled task belongs to the repetitive scheduled task based on the callback instruction, after determining that the target scheduled task has completed one execution, the method further includes: When the business layer confirms that the duration requirement of the target scheduled task has changed, the business layer sends a fifth call instruction for the scheduled task update interface to the abstract layer; The abstract layer obtains a new timing duration based on the fifth calling instruction and determines that the target scheduled task is a scheduled task to be updated; The abstract layer sets the timing duration of the to-be-updated timing task to the new timing duration and then continues to execute.

6. The method according to claim 2, It is characterized in that For each target scheduled task created, the abstract layer starts to detect in real time whether the target scheduled task has been executed based on the timing notification of the hardware layer when the creation is completed, and when the target scheduled task is detected to be executed, notifies the business layer that the target scheduled task has been executed, including: For each target scheduled task created, if the task type of the target scheduled task is a one-time type, the abstract layer starts to detect in real time whether the scheduled duration of the target scheduled task has been reached based on the scheduled notification of the hardware layer when the creation is completed; When the abstract layer detects that the timing duration of the target scheduled task has arrived, the abstract layer sends a callback instruction for the callback interface of the target scheduled task to the business layer; When the business layer determines that the target scheduled task belongs to the one-time scheduled task based on the callback instruction, it determines that the target scheduled task has ended execution.

7. The method according to claim 6, It is characterized in that After the step of determining that the target scheduled task has ended execution when the business layer determines that the target scheduled task belongs to the one-time scheduled task based on the callback instruction, the method further includes: When the business layer confirms that the target scheduled task has finished execution, the business layer directly sends a fourth call instruction for the scheduled task deletion interface to the abstract layer; When the abstract layer receives the fourth calling instruction, it deletes the target scheduled task specified by the fourth calling instruction and releases the memory resources occupied by the target scheduled task.

8. The method according to any one of claims 1 to 7, It is characterized in that The method further comprises: When the timing requirement is met, the business layer sends a sixth call instruction for a timing service destruction interface to the abstract layer; The abstract layer destroys the timing service and releases the memory resources occupied by the timing service based on the sixth calling instruction, and sends a stop calling instruction for the timer interface to the hardware layer, so that the hardware layer controls the timer to stop timing.

9. A hierarchical calling device for a timer, It is characterized in that Applied to a single-chip microcomputer, the hardware resources of the single-chip microcomputer include a timer, and the device includes a service layer, an abstraction layer and a hardware layer; The business layer is used to send a first call instruction for creating an interface for a timing service to the abstract layer when there is a timing requirement; The abstract layer is used to create a timing service based on the first call instruction, and send a second call instruction for the timer interface to the hardware layer, so that the hardware layer drives the timer to start timing and feeds back a timing notification to the abstract layer every preset period; The abstract layer is also used to create a scheduled task based on the third calling instruction each time the business layer receives a third calling instruction for a newly added interface for a scheduled task; For each target scheduled task created, the abstract layer is also used to start real-time detection of whether the target scheduled task has been executed based on the scheduled notification of the hardware layer when the creation is completed, and when it is detected that the target scheduled task has been executed, notify the business layer that the target scheduled task has been executed.

10. A single chip microcomputer, It is characterized in that The single chip microcomputer is used to implement the hierarchical calling method of the timer as described in any one of claims 1-8.