Implementation method of preemptive multi-tasking real-time operating system for micro single-chip microcomputer
By designing a preemptive multi-task real-time operating system on a micro-controller, and using task parameter tables, ready tables and delay tables to optimize resource allocation and scheduling, the problems of large space occupation and poor real-time performance of existing operating systems are solved, and efficient task scheduling is achieved and real-time control.
Patent Information
- Application Number
- CN202111195599.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-10-14
- Publication Date
- 2024-10-29
- Estimated Expiration
- 2041-10-14
AI Technical Summary
The existing embedded operating system occupies a large space on the micro-controller and has poor real-time performance, making it difficult to meet the needs of multi-task scheduling and real-time control.
A preemptive multi-task real-time operating system is designed. Through the use of task parameter table, task ready table and task delay table, storage resource allocation and task scheduling are optimized to achieve an operating system with small code volume and good real-time performance.
It realizes efficient task scheduling and real-time control on the micro-controller, reduces the occupation of hardware resources, and is suitable for the micro-controller environment with limited resources.
Smart Images

Figure CN113900787B_ABST
Abstract
Description
Technical Field
[0001] The invention relates to the technical field of single-chip computer programming, and in particular to a method for realizing a preemptive multi-task real-time operating system for a micro single-chip computer. Background Art
[0002] With the development of single-chip microcomputer technology, single-chip microcomputers have been widely used in various fields of daily production. Single-chip microcomputers adopt a miniaturized design, integrating CPU, memory, general I / O and interrupt system, and are usually used as the control core of the system. In order to effectively realize control, software development for single-chip microcomputers is usually multi-tasking and has high requirements for real-time performance. The operating system can provide services such as multi-tasking scheduling and interrupt management. Therefore, implanting an operating system in a single-chip microcomputer can meet the control requirements of the system.
[0003] However, currently, single-chip microcomputers usually adopt miniaturized design and have less hardware resources. Existing embedded operating systems such as RTLinux, uc / OS, Vxworks, etc. have more code. Direct transplantation will occupy more hardware resources of single-chip microcomputers, which will easily lead to insufficient resources for task program development and difficulty in meeting real-time requirements. Therefore, it is of great significance to develop a method for implementing a preemptive multi-tasking real-time operating system suitable for micro single-chip microcomputers. Summary of the invention
[0004] The purpose of the present invention is to solve the problems of large space occupation and poor real-time performance of existing embedded operating systems when implanted into micro single-chip microcomputers, and to propose a preemptive multi-tasking real-time operating system implementation method for micro single-chip microcomputers. The method has smaller code size and better real-time performance, and can provide functions such as task scheduling, semaphore management, and interrupt service.
[0005] In order to achieve the above object, the technical solution adopted by the present invention is:
[0006] A method for realizing a preemptive multi-task real-time operating system for a micro single-chip microcomputer includes an operating system establishment process and an operating system scheduling process.
[0007] Furthermore, the operating system establishment process includes the following steps:
[0008] (1.1) Determine the priority of each task based on the system usage requirements;
[0009] (1.2) Allocate storage space for each task and establish an entry function;
[0010] (1.3) Establish task parameter table, task ready table and task delay table;
[0011] (1.4) Set up and start interrupts;
[0012] (1.5) Start the highest priority task from the task ready list.
[0013] Furthermore, the specific method of step (1.2) is:
[0014] (1.201) Apply for a storage space in the system dedicated to task operation;
[0015] (1.202) Determine the running space required by the task, allocate private storage space for the task in the applied storage space, and establish memory usage detection flags at 1 / 3, 1 / 2, 2 / 3, and 7 / 8 of the allocated space;
[0016] (1.203) Write the entry function according to the function to be implemented by the task. The parameters of the entry function need to be converted to void* type and filled into the parameter table. When the function is actually used, it is converted to the required type.
[0017] The specific method of step (1.3) is:
[0018] (1.301) Allocate a storage space of 8 storage units long as the storage address of the task parameter table;
[0019] (1.302) Allocate a storage unit as the task ready list. Each bit in the storage unit represents a priority. The highest bit represents the highest priority, and the priorities represented by other bits decrease in sequence.
[0020] (1.303) Allocate a storage unit as the task delay table. Each bit of the storage unit represents a priority. The highest bit represents the highest priority, and the priorities represented by other bits decrease in sequence.
[0021] (1.304) Fill the task name, initial state, and priority into the task parameter table; obtain the first address of the task entry function, the parameters of the entry function, the first address of the task private storage space, and the size of the storage space and fill them into the task parameter table; initialize the task error in the task parameter table to 0;
[0022] (1.305) Get the initial state and priority of the task from the task parameter table. If the initial state is ready, fill in the task ready table according to the priority; if the initial state is delayed, fill in the task delay table according to the priority;
[0023] The specific method of step (1.4) is:
[0024] (1.401) Start the timer and set the timing time to a time slice. Each time the timing ends, an interrupt will be generated, and each interrupt will perform an operating system scheduling;
[0025] (1.402) Get the interrupt mapping table and register the interrupt service function in the interrupt mapping table;
[0026] (1.403) Enable interrupts.
[0027] Furthermore, the scheduling process of the operating system includes the following steps:
[0028] (2.1) When the operating system timer generates an interrupt, the task ready table is retrieved. If there is a task with a higher priority, the task with a higher priority is executed. If the current task is the highest priority task, the current task is continued to be executed.
[0029] (2.2) When the operating system timer generates an interrupt, the task delay table is retrieved. If there is a delayed task, the task is restored from its private space and placed in the task ready table;
[0030] (2.3) When the current task needs to be delayed, the task is stored in its private storage space, and the task is placed in the task delay table, and a scheduling is performed immediately.
[0031] The present invention has the following advantages:
[0032] 1. The operating system of the present invention uses a task parameter table to store task parameters, a task ready table to store ready tasks, and a task delay table to store delayed tasks, wherein the task parameter table only occupies 8 storage units, and the task ready table and the task delay table each occupy only 1 storage unit. The entire operating system requires less storage resources and is more suitable for micro single-chip microcomputers;
[0033] 2. The operating system adopts a preemptive design and has good real-time performance. BRIEF DESCRIPTION OF THE DRAWINGS
[0034] Figure 1 It is the operating system creation flow chart;
[0035] Figure 2 It is a task priority diagram;
[0036] Figure 3 It is a schematic diagram of the operating system. DETAILED DESCRIPTION
[0037] The present invention is further described below in conjunction with the accompanying drawings and specific embodiments.
[0038] like Figure 1-3 As shown, a method for implementing a preemptive multi-tasking real-time operating system for a micro single-chip microcomputer includes an operating system establishment process and an operating system scheduling process. The establishment of the operating system includes the following steps:
[0039] (1) Determine the priority of each task based on the system's usage requirements, mainly based on the real-time performance required by the task. Tasks with high real-time requirements have higher priority;
[0040] (2) Allocate storage space for each task and establish an entry function. The storage space is mainly used to store task running parameters. When a task is delayed, the running parameters are stored in the storage space, and can be directly restored when the subsequent task is running. The method of establishing a task entry function is as follows
[0041] int TaskName(void *arg){
[0042] Function realization;
[0043] }
[0044] TaskName indicates the task name, and arg indicates the parameters required for the task to run;
[0045] (3) Create a task parameter table, a task ready table, and a task delay table. The task parameter table is the following structure:
[0046] {TASK_ENTRY entry;
[0047] void *stk_base;
[0048] void *arg;
[0049] char *name;
[0050] uint16 stk_size;
[0051] int stat;
[0052] int err;
[0053] uint16 pri;
[0054] }
[0055] Each item occupies a storage unit, entry is the task entry function pointer, *stk_base is the first address of the task storage space, name is the task name, stk_size is the task storage space size, stat is the task status, err is the task error status, and pri is the task priority. The task ready table and the task delay table are both a storage unit, and each bit of the storage unit represents a task priority;
[0056] (4) Setting and starting interrupts mainly includes the steps of obtaining the interrupt mapping table, registering the interrupt service function to the interrupt mapping table, and activating the interrupt mapping table. The interrupt mapping table is an inherent attribute of various microcontrollers. It mainly establishes a mapping relationship between the interrupt number and the interrupt service function. Obtaining and activating the interrupt mapping table calls a fixed interface function. To register an interrupt service function, you need to establish the following function pointer
[0057] typedef int (*irq_handler)(int irq);
[0058] Assign the interrupt service function to the function pointer, and put the assigned function pointer into the position corresponding to the interrupt number in the interrupt mapping table;
[0059] (5) Start the highest priority task from the task ready table, mainly to retrieve the task priority in the task ready table, select the highest priority task, and call its entry function.
[0060] The scheduling process of the operating system includes the following steps:
[0061] (1) When the operating system timer generates an interrupt, the task ready table is retrieved. If there is a task with a higher priority, the task with a higher priority is executed. The specific implementation code is as follows:
[0062] i = get_hipri(rdy_list);
[0063] if (i != current_pri)
[0064] current_pri = i;
[0065] Among them, get_hipri means getting the highest priority function, rdy_list is the task ready list, if the current task is the highest priority task, then continue to execute the current task;
[0066] (2) When the operating system timer generates an interrupt, the task delay table is retrieved. If there is a delayed task, the task is restored from its private space and placed in the task ready table. The specific implementation code is as follows:
[0067] for (i = 0; i < TASK_MAX_NUM; i++) {
[0068] m = 1 << i;
[0069] if (delay_list & m) {
[0070] if (0 == --tick_time) {
[0071] delay_list &= ~m;
[0072] rdy_list |= m;}}}
[0073] Among them, TASK_MAX_NUM is the maximum number of tasks, delay_list is the task delay list, tick_time is the task delay time, and rdy_list is the task ready list;
[0074] (3) When the current task needs to be delayed, the task is stored in its private storage space, and the task is placed in the task delay table, and a scheduling is performed immediately;
[0075] delay_list |= msk;
[0076] tick_time = delay_time;
[0077] rdy_list &= ~msk;
[0078] Among them, delay_list is the task delay list, tick_time is the task delay time, delay_time is the set task delay time, and rdy_list is the task ready list.
[0079] In summary, the method includes the establishment of an operating system and the scheduling process of the operating system. The operating system is mainly composed of tasks. The establishment of the operating system and the scheduling of the operating system are in a sequential relationship. The establishment of the operating system requires the steps of determining task priorities, creating task entry functions, configuring interrupts, etc. The preemptive mode is mainly realized through the scheduling of the operating system. The scheduling of the operating system is mainly realized by generating interrupts through the system timer, and coordinating the delay table and the ready table of the task. The micro-chip microcomputer operating system implemented by the present invention has the advantages of small space occupation and good portability.
[0080] The above description of the disclosed embodiments enables one skilled in the art to implement or use the present invention. Various modifications to these embodiments will be apparent to one skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the present invention. Therefore, the present invention will not be limited to the embodiments shown herein, but rather to the widest scope consistent with the principles and novel features disclosed herein.
[0081] The present invention uses specific embodiments to illustrate the principles and implementation methods of the present invention. The description of the above embodiments is only used to help understand the method of the present invention and its core idea. At the same time, for those skilled in the art, according to the idea of the present invention, there will be changes in the specific implementation methods and application scope. In summary, the content of this specification should not be understood as a limitation on the present invention.
Claims
1. A method for implementing a preemptive multitasking real-time operating system for a micro single-chip microcomputer, It is characterized in that Including the operating system establishment process and the operating system scheduling process; The operating system establishment process includes the following steps: (1.1) Determine the priority of each task based on the system usage requirements; (1.2) Allocate storage space for each task and establish an entry function; the specific method is as follows: (1.201) Apply for a storage space in the system specifically for task operation; (1.202) Determine the running space required by the task, allocate private storage space for the task in the applied storage space, and establish memory usage detection flags at 1 / 3, 1 / 2, 2 / 3, and 7 / 8 of the allocated space; (1.203) Write the entry function according to the function to be implemented by the task. The parameters of the entry function need to be converted to void* type and filled into the parameter table. When the function is actually used, it is converted to the required type. (1.3) Establish task parameter table, task ready table and task delay table; the specific method is: (1.301) Allocate a storage space of 8 storage units long as the storage address of the task parameter table; (1.302) Allocate a storage unit as the task ready list. Each bit of the storage unit represents a priority. The highest bit represents the highest priority, and the priorities represented by other bits decrease in sequence. (1.303) Allocate a storage unit as the task delay table. Each bit of the storage unit represents a priority. The highest bit represents the highest priority, and the priorities represented by other bits decrease in sequence. (1.304) Fill the task name, initial state, and priority into the task parameter table; obtain the first address of the task entry function, the parameters of the entry function, the first address of the task private storage space, and the size of the storage space into the task parameter table; initialize the task error in the task parameter table to 0; (1.305) Get the initial state and priority of the task from the task parameter table. If the initial state is ready, fill in the task ready table according to the priority; if the initial state is delayed, fill in the task delay table according to the priority; (1.4) Set and start the interrupt; the specific method is: (1.401) Start the timer and set the timing time to a time slice. Each time the timing ends, an interrupt will be generated, and each interrupt will perform an operating system scheduling; (1.402) Obtain the interrupt mapping table and register the interrupt service function in the interrupt mapping table; (1.403) Enable interrupts; (1.5) Start the highest priority task from the task ready list; The scheduling process of the operating system includes the following steps: (2.1) When the operating system timer generates an interrupt, the task ready table is retrieved. If there is a task with a higher priority, the task with a higher priority is executed. If the current task is the highest priority task, the current task continues to be executed. (2.2) When the operating system timer generates an interrupt, the task delay table is retrieved. If there is a delayed task, the task is restored from its private space and placed in the task ready table; (2.3) When the current task needs to be delayed, the task is stored in its private storage space, and the task is placed in the task delay table, and a scheduling is performed immediately.
Citation Information
Patent Citations
Mixed scheduling method of embedded real-time operating system
CN101751289A
Method for implementing multi-task real-time operating system embedded into 51 series singlechip
CN102063325A
A minimal operating system applied to the control of a UPS single-chip microcomputer and a multitask management method based on the minimal operating system
CN109324885A
Task scheduling processing system and method of embedded real-time operating system
CN111367652A
Realizing method for multiple task real-time operation system
CN1409209A
