A multi-task scheduling method for embedded systems based on timer clock control
The timer clock control method in embedded systems ensures efficient task execution despite CPU blocking, optimizing resource use and lowering costs without high-power chips or complex operating systems.
Patent Information
- Application Number
- CN202011159559.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-10-27
- Publication Date
- 2024-12-17
- Estimated Expiration
- 2040-10-27
AI Technical Summary
Traditional embedded software systems face inefficiencies in multi-task scheduling due to CPU blocking, leading to resource and time waste, exacerbated by the need for high-power chips and complex operating systems, increasing development costs and difficulty.
A method utilizing a timer clock control to sequence tasks with defined delay times, enabling smooth execution of tasks even when one is blocked, without requiring high-power chips or parallel processing systems.
Optimizes task scheduling by ensuring continuous execution of tasks, saving CPU resources and time, while reducing system costs by avoiding the need for advanced hardware.
Smart Images

Figure CN112256419B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of computer software, and in particular to an embedded system multi-task scheduling method based on timer clock control. Background Art
[0002] With the continuous development and deepening of Internet of Things technology, various embedded software systems are widely used in different industries. Among them, most Internet of Things devices need to collect data from various types of sensor devices, and process, transmit and respond to the data. Traditional embedded software development mainly adopts the front-end and back-end software design mode, and its multi-task program operation process adopts sequential execution and delayed waiting strategies. This causes that if the CPU resources are blocked or occupied by a task during the multi-task scheduling process, other tasks will not be processed in a timely and effective manner, resulting in serious waste of time and resources. To solve this problem, chips with higher computing power or embedded operating systems with multi-task parallel processing capabilities are usually used to speed up the operation of the system, but this solution increases the R&D and procurement costs of the system, and the development difficulty of the entire system is also increased accordingly, which is not easy for embedded software developers to master. Therefore, there is an urgent need for a better multi-task scheduling method that can deal with the problem of time and resource waste caused by a task blocking or occupying the CPU, and can reduce system overhead. Summary of the invention
[0003] 1. Technical issues to be resolved
[0004] Based on the above problems, the present invention provides an embedded system multi-task scheduling method based on timer clock control, which reduces the waste of time and resources caused by a task blocking or occupying the CPU without using a chip with higher computing power and an embedded operating system with multi-task parallel processing capabilities.
[0005] (II) Technical solution
[0006] Based on the above technical problems, the present invention provides an embedded system multi-task scheduling method based on timer clock control, comprising the following steps:
[0007] S1. Divide the entire program into multiple task function modules with a single specific function according to the functions implemented by the embedded software;
[0008] S2, using the universal timer of the main control chip as the delay clock, and setting the minimum reference time t0 of the delay clock;
[0009] S3, marking the clock sequence corresponding to each task in the delay clock list with an enumeration type, and defining the predetermined delay time t corresponding to the task execution according to the actual execution status of the task;
[0010] S4, defining two pointers in the timer interrupt service subroutine, the first pointer points to an array storing a flag indicating whether each task enables a delay clock, and the second pointer points to an array storing the predetermined delay time t;
[0011] S5. Poll each task in turn according to the clock sequence. When each task starts the delay clock and reaches the predetermined delay time t, it is executed, and then polls the next task after execution.
[0012] Furthermore, the step S3 includes starting the delay clock of the first task in the clock sequence, and setting the predetermined delay time t of the first task to zero in the initialization setting subroutine.
[0013] Furthermore, the step S5 includes the following steps:
[0014] S5.1, entering the first task according to the clock sequence;
[0015] S5.2, determine whether the delay clock is turned on for the current task, if not, skip the task, proceed to the next task, and re-enter step S5.2; if yes, proceed to step S5.3;
[0016] S5.3, determine whether the delay time corresponding to the current task execution has arrived, if so, execute the corresponding task in the main program, if not, skip the task, enter the next task, and return to step S5.2;
[0017] S5.4. After executing the current task, if the current task is the first task, reset the scheduled delay time t of the first task; otherwise, clear the delay clock corresponding to the task and start the clock of the next task;
[0018] S5.5. Determine whether all tasks have been polled. If not, proceed to the next task and return to step S5.2. If yes, the main program enters the next loop, restarts a new round of polling according to the clock sequence, and returns to step S5.1.
[0019] Furthermore, the method for determining whether the predetermined delay time corresponding to the current task execution has been reached in step S5.3 includes the following steps:
[0020] S5.3.1. Determine whether the predetermined delay time t corresponding to the task execution is zero. If it is zero, the task delay time t′ is the predetermined delay time t, that is, t′=t; if it is not zero, the task delay time t′ is obtained by subtracting the minimum reference time t0 from the predetermined delay time t, that is, t′=t-t0;
[0021] S5.3.2. Determine whether the task delay time t′ is equal to zero. If so, the scheduled delay time t corresponding to the task execution has been reached. If not, the scheduled delay time corresponding to the task execution has not been reached.
[0022] Furthermore, the predetermined delay time t and the minimum reference time t0 are in a multiple relationship.
[0023] Furthermore, the minimum reference time t0 of the delay clock is the overflow time of the timer, and the overflow time is determined according to the automatic reload value and the pre-scaling system.
[0024] The present invention also discloses an embedded system, comprising:
[0025] at least one processor; and at least one memory in communication with the processor, wherein:
[0026] The memory stores program instructions that can be executed by the processor, and the processor calls the program instructions to execute the embedded system multi-task scheduling method based on timer clock control.
[0027] The present invention also discloses a non-transitory computer-readable storage medium, wherein the non-transitory computer-readable storage medium stores computer instructions, and the computer instructions enable the computer to execute the embedded system multi-task scheduling method based on timer clock control.
[0028] (III) Beneficial effects
[0029] The above technical solution of the present invention has the following advantages:
[0030] The present invention enables the system to execute each task in sequence according to the clock sequence and the corresponding delay time set for each task execution. Even if a task is blocked or occupies the CPU, it will not stop in a task processing program waiting for its execution to be completed, but continue with the next task, ensuring that other tasks can also be executed normally and smoothly, optimizing the multi-task scheduling process of the embedded system, the main control chip is always in a working state, avoiding the waste of CPU processing resources and time; and the above effect is achieved without using a chip with higher computing power and an embedded operating system with multi-task parallel processing capability, thereby relatively reducing the system cost. BRIEF DESCRIPTION OF THE DRAWINGS
[0031] The features and advantages of the present invention will be more clearly understood by referring to the accompanying drawings, which are schematic and should not be construed as limiting the present invention in any way. In the accompanying drawings:
[0032] Figure 1 The present invention is a flowchart of a method for multi-task scheduling in an embedded system based on timer clock control according to an embodiment of the present invention. DETAILED DESCRIPTION
[0033] The specific implementation of the present invention is further described in detail below in conjunction with the accompanying drawings and examples. The following examples are used to illustrate the present invention, but are not intended to limit the scope of the present invention.
[0034] The embodiment of the present invention discloses a multi-task scheduling method for an embedded system based on timer clock control, such as Figure 1 As shown, the following steps are included:
[0035] S1. Divide the entire program into multiple task function modules with single specific functions according to the functions implemented by the embedded software, complete the program design and functional testing of each task, and implement the mutual calling logic of each task in the main program;
[0036] Taking the embedded software implementation of a data acquisition device connected to five different types of sensors as an example, the acquisition programs of the five sensors can be set as five task modules respectively.
[0037] S2, using a general timer of the main control chip as a delay clock, and setting the overflow time of the timer as the minimum reference time t0 of the delay clock;
[0038] Taking the main control chip of the STM32F4 series as an example, its general timer TIM3 can be set as a delay clock, and the overflow time 10ms can be set as the minimum reference time by configuring the automatic reload value and the pre-division coefficient.
[0039] S3, in the delay clock list, enumerate the clock sequence corresponding to each task mark in the clock list according to the execution order of the tasks, and define the predetermined delay time t corresponding to the task execution according to the actual execution of the task, the predetermined delay time t is a multiple relationship with the minimum reference time t0, start the delay clock of the first task in the clock sequence, and set the predetermined delay time t of the first task to zero in the initialization setting subroutine;
[0040] The setting of this predetermined delay time t is usually based on the data preparation time of other devices that communicate with the entity implemented by the embedded software function. Taking the embedded software implementation of the data acquisition device communicating with the sensor as an example, the predetermined delay time t is the power-on startup time of the sensor.
[0041] S4. Two pointers are defined in the timer interrupt service subroutine, the first pointer points to an array storing a flag indicating whether each task enables a delay clock, and the second pointer points to an array storing a defined predetermined delay time t;
[0042] S5. Poll each task in turn according to the clock sequence. When a task starts the delay clock and reaches the predetermined delay time t, it is executed. After execution, poll the next task:
[0043] S5.1, entering the first task according to the clock sequence;
[0044] S5.2, determine whether the delay clock is turned on for the current task, if not, skip the task, proceed to the next task, and re-enter step S5.2; if yes, proceed to step S5.3;
[0045] S5.3, determine whether the delay time corresponding to the current task execution has arrived, if so, execute the corresponding task in the main program, if not, skip the task, enter the next task, and return to step S5.2;
[0046] The method for determining whether the delay time has been reached is:
[0047] S5.3.1. Determine whether the predetermined delay time t corresponding to the task execution is zero. If it is zero, the task delay time t′ is the defined task delay time t, that is, t′=t; if it is not zero, the predetermined delay time t is subtracted from the minimum reference time t0 to obtain the task delay time t′, that is, t′=t-t0;
[0048] If the predetermined delay time t is zero, the task starts the delay clock immediately; if t is not zero, the task needs an overflow time, i.e., the minimum reference time, before starting the delay clock;
[0049] S5.3.2, determine whether the task delay time t' is equal to zero. If so, the predetermined delay time t corresponding to the task execution has been reached, and the corresponding task in the main program is executed. If not, the delay time corresponding to the task execution has not been reached, skip the task, enter the next task, and return to step S5.2;
[0050] By scheduling tasks in this way, when a task is blocked, it will jump to the next task after reaching the delay time requirement, so that other tasks can be executed normally and smoothly, instead of stopping in a task handler waiting for its execution to be completed. In this step, the main control chip in the system is always in working state, which greatly saves time.
[0051] S5.4. After executing the current task, if the current task is the first task, reset the scheduled delay time t of the first task; otherwise, clear the delay clock corresponding to the task and start the clock of the next task;
[0052] Note that the delay clock start flag of the first task will never be cleared, and its scheduled delay time t will only be reset after the first task is executed so that the program can start executing again when it enters the next loop.
[0053] S5.5. Determine whether all tasks have been polled. If not, proceed to the next task and return to step S5.2. If yes, the main program enters the next loop, restarts a new round of polling according to the clock sequence, and returns to step S5.1.
[0054] For a task that is blocked, when the loop program performs a new round of polling, it will be polled again according to the clock sequence.
[0055] Finally, it should be noted that the above method can be converted into software program instructions, which can be implemented by using a control system including a processor and a memory, or by computer instructions stored in a non-transitory computer-readable storage medium. The above integrated unit implemented in the form of a software functional unit can be stored in a computer-readable storage medium. The above software functional unit is stored in a storage medium, including a number of instructions to enable a computer device (which can be a personal computer, a server, or a network device, etc.) or a processor to perform some steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (Read-Only Memory, ROM), random access memory (Random Access Memory, RAM), disk or optical disk and other media that can store program codes.
[0056] In summary, through the above-mentioned embedded system multi-task scheduling method based on timer clock control, the system can execute each task in sequence according to the clock sequence and the corresponding delay time set for each task. Even if a task is blocked or occupies the CPU, it will not stop in a task processing program waiting for its execution to be completed, but continue with the next task to ensure that other tasks can also be executed normally and smoothly, thereby optimizing the multi-task scheduling process of the embedded system, and the main control chip is always in a working state, avoiding the waste of CPU processing resources and time; and the above effect is achieved without using chips with higher computing power and embedded operating systems with multi-task parallel processing capabilities, thereby relatively reducing system costs.
[0057] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the embodiments of the present invention are described in conjunction with the accompanying drawings, those skilled in the art may make various modifications and variations without departing from the spirit and scope of the present invention, and such modifications and variations shall fall within the scope defined by the appended claims.
Claims
1. A multi-task scheduling method for embedded systems based on timer clock control, It is characterized in that The following steps are involved: S1. Divide the entire program into multiple task function modules with a single specific function according to the functions implemented by the embedded software; S2, using the universal timer of the main control chip as the delay clock, setting the minimum reference time t of the delay clock 0 ; S3, marking the clock sequence corresponding to each task in the delay clock list with an enumeration type, and defining the predetermined delay time t corresponding to the task execution according to the actual execution status of the task; S4, defining two pointers in the timer interrupt service subroutine, the first pointer points to an array storing whether each task enables a delay clock flag, and the second pointer points to an array storing the predetermined delay time t; S5, polling each task in turn according to the clock sequence, when each task starts the delay clock and reaches the predetermined delay time t, polling the next task after execution; S5.1, entering the first task according to the clock sequence; S5.2, determine whether the delay clock is turned on for the current task, if not, skip the task, proceed to the next task, and re-enter step S5.2; if yes, proceed to step S5.3; S5.3, determine whether the delay time corresponding to the current task execution has arrived, if so, execute the corresponding task in the main program, if not, skip the task, enter the next task, and return to step S5.2; S5.3.
1. Determine whether the predetermined delay time t corresponding to the task execution is zero. If it is zero, the task delay time t′ is the predetermined delay time t, that is, t′=t; if it is not zero, the predetermined delay time t is subtracted from the minimum reference time t 0 Get the task delay time t′, that is, t′=tt 0 ; S5.3.2, determine whether the task delay time t' is equal to zero, if so, the scheduled delay time t corresponding to the task execution has been reached, if not, the scheduled delay time corresponding to the task execution has not been reached; S5.
4. After executing the current task, if the current task is the first task, reset the scheduled delay time t of the first task; otherwise, clear the delay clock corresponding to the task and start the clock of the next task; S5.
5. Determine whether all tasks have been polled. If not, proceed to the next task and return to step S5.
2. If yes, the main program enters the next loop, restarts a new round of polling according to the clock sequence, and returns to step S5.
1.
2. The embedded system multi-task scheduling method based on timer clock control according to claim 1, It is characterized in that The step S3 includes starting the delay clock of the first task in the clock sequence, and setting the predetermined delay time t of the first task to zero in the initialization setting subroutine.
3. The embedded system multi-task scheduling method based on timer clock control according to claim 1, It is characterized in that The predetermined delay time t and the minimum reference time t 0 It is a multiplication relationship.
4. The embedded system multi-task scheduling method based on timer clock control according to claim 1, It is characterized in that The minimum reference time t of the delay clock 0 is the overflow time of the timer, and the overflow time is determined according to the automatic reload value and the pre-scaling system.
5. An embedded system, It is characterized in that include: at least one processor; and at least one memory in communication with the processor, wherein: The memory stores program instructions that can be executed by the processor, and the processor calls the program instructions to execute the embedded system multi-task scheduling method based on timer clock control as described in any one of claims 1 to 4.
6. A non-transitory computer-readable storage medium, It is characterized in that The non-transitory computer-readable storage medium stores computer instructions, and the computer instructions enable the computer to execute the embedded system multi-task scheduling method based on timer clock control as described in any one of claims 1 to 4.
Citation Information
Cited By
Dynamic load balancing method and system based on session granularity and bit mask mapping
CN121433862A
