Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - What is the UCOS operating system?
What is the UCOS operating system?
U C/O S is a free and open source real-time operating system with compact structure and detachable real-time kernel.

The predecessor of μC/OS-II is μC/OS, which was first published in the May and June issue of Embedded System Programming by American embedded system expert Jean J.Labrosse in 1992. The source code of μC/OS was published in B B S of this magazine.

μC/OS and μC/OS-II are specially designed for embedded computer applications, and most of the codes are written in C language. The hardware-related parts of CPU are written in assembly language, and the assembly language part with a total of about 200 lines is compressed to a minimum to facilitate transplantation to any other CPU. Users can embed μC/OS-II into the developed products as long as they have standard ANSI C cross compiler, assembler, connector and other software tools. μC/OS-II has the characteristics of high execution efficiency, small occupied space, excellent real-time performance and strong expansibility, and the smallest kernel can be compiled to 2KB. μC/OS-II has been transplanted to almost all well-known CPUs.

Strictly speaking, uC/OS-II is just a real-time operating system kernel, which only includes basic functions such as task scheduling, task management, time management, memory management and communication and synchronization between tasks. No additional services such as input and output management, file system and network are provided. However, due to the good expansibility and open source code of uC/OS-II, these unnecessary functions can be realized by users according to their own needs.

The goal of uC/OS-II is to realize a preemptive real-time kernel based on priority scheduling, which provides the most basic system services, such as semaphore, mailbox, message queue, memory management, interrupt management and so on.

task management

UC/OS-II can support up to 64 tasks, corresponding to priorities 0 ~ 63 respectively, where 0 is the highest priority. 63 is the lowest level. The system reserves four tasks with the highest priority and four tasks with the lowest priority, and the number of tasks available to all users is 56.

UC/OS-II provides various function calls for task management, including creating tasks, deleting tasks, changing task priorities, suspending and resuming tasks, etc.

When the system is initialized, two tasks will be automatically generated: one is an idle task with the lowest priority, and the task is changed to only do the accumulation operation of a plastic variable; The other is a system task with the second lowest priority, which is responsible for counting the current cpu utilization.

time management

The time management of uC/OS-II is realized by timing interrupt, which generally occurs once every10ms or100ms. Time frequency depends on the timer programming of the hardware system by the user. The interval of interruption is fixed, and the interruption becomes a clock beat.

UC/OS-II requires users to call the system functions related to the clock beat provided by the system in the scheduled interrupt service program, such as task switching function of interrupt level, system time function, etc.

memory management

In ANSI C, malloc and free functions are used to dynamically allocate and release memory. However, in embedded real-time systems, many such errors will lead to memory fragmentation, and the execution time of malloc and free is uncertain due to memory management algorithms.

In uC/OS-II, continuous mass cache is managed by partitions. Each partition contains an integer number of memory blocks with the same size, but the memory size can be different between different partitions. When users need to dynamically allocate memory, the system selects the appropriate partition and allocates memory by block. When releasing memory, put the block back to its previous partition, which can effectively solve the fragmentation problem and the execution time is fixed.

Inter-task communication and synchronization

For multitask operating system, communication and synchronization between tasks are essential. UC/OS-II provides four synchronization objects, namely semaphore, mailbox, message queue and event. All these synchronization objects have interfaces for creating, waiting, sending and querying to realize communication and synchronization between processes.

task scheduling

UC/OS-II uses a removable real-time multitasking kernel. The removable real-time kernel is the highest priority task that is ready to run at any time.

The task scheduling of uC/os-II is a preemptive scheduling based entirely on task priority, that is, once the task with the highest priority is in a ready state, it will immediately seize the processor resources of the running low-priority task. In order to simplify the system design, uC/OS-II stipulates that all tasks have different priorities, because the priority of a task also uniquely marks the task itself.

Task scheduling will occur in the following situations:

1) The high-priority task actively requests to suspend and abandon the processor because it needs some key resources. At this point, the low-priority tasks in the ready state will be executed. This scheduling is also called task-level context switching.

2) Due to the arrival of the clock beat, when the kernel finds that the high-priority task has obtained the execution conditions (such as the arrival time of the sleep clock), it directly switches to the execution of the high-priority task in the interrupt state. This scheduling is also called context switching at interrupt level.

These two scheduling methods are very common in the execution of uC/OS-II. Generally speaking, the former occurs in the system service, and the latter occurs in the service program with clock interruption.

The content of scheduling work can be divided into two parts: finding the highest priority task and task switching. By establishing a ready task list, the search for its highest priority task is realized. Each task in u C/O S has an independent stack space and a data structure called task control block (TCB), in which the first member variable is the saved task stack pointer. The task scheduling module first records the TCB address of the current top-level ready task with the variable OSTCBHighRdy, and then calls the OS_TASK_SW () function to switch tasks.

Components of μC/OS-II

μC/OS-II can be roughly divided into five parts: kernel, task processing, time processing, task synchronization and communication, and CPU transplantation.

1) core part (OSCore.c)

It is the processing core of the operating system, including the initialization of the operating system, the operation of the operating system, the preamble of interrupt entry and exit, clock beat, task scheduling, event handling and so on. All the parts that can maintain the basic work of the system are here.

2) task processing part (OSTask.c)

The content of the task processing part is closely related to the operation of the task. Include task creation, deletion, pause, resumption, etc. Because μC/OS-II is scheduled by task, this part is also very important.

3) clock part (OSTime.c)

The minimum clock unit in μC/OS-II is timetick. Operations such as task delay are completed here.

4) Task synchronization and communication part

It is an event processing part, including semaphore, mailbox, mailbox queue, event flag and so on. It is mainly used for interconnection between tasks and access to key resources.

5) Interface with CPU

Refers to the transplant part of μC/OS-II to the CPU used. Because μC/OS-II is a universal operating system, the realization of key problems still needs to be transplanted according to the specific content and requirements of specific CPU. This part is usually written in assembly language, because it involves system pointers such as SP. It mainly includes the bottom realization of interrupt-level task switching, the bottom realization of task-level task switching, the generation and processing of clock beat, and the related processing part of interrupt.