UC/OS II (micro control operation systemtwo) is a ROM-based, customizable, preemptible, and real-time multi-task kernel with high portability, it is particularly suitable for microprocessor and controller, and is a real-time operating system (RTOs) with the same performance as many commercial operating systems ). To provide the best porting performance, uC/oⅱ uses ANSI to the maximum extent.
C language development, and has been transplanted to more than 40 processor systems, covering a variety of CPUs (including DSP) from 8-bit to 64-bit ).
UC/OS II can be considered as a multi-task scheduler.The system services related to multi-task operating systems, such as semaphores and mailboxes, are added to the task scheduler. Its main features are openness.Source code,CodeThe structure is clear, clear, detailed, organized, portable, And customizable.. The kernel is preemptible.You can manage up to 60 tasks.
Strictly speaking, uC/OS-II is just a real-time operating system kernel, which only contains basic functions such as task scheduling, task management, time management, memory management and communication and synchronization between tasks. No additional services are provided, such as input/output management, file system, and network. However, due to the good scalability and open source code of uC/OS-II, these non-essential functions can be fully implemented by the user as needed.
Task Management
UC/OS-IIUp to 64 tasks are supported, corresponding to priority 0 ~ 63, where 0 is the highest priority. 63At the lowest level, the system retains four tasks with the highest priority and four tasks with the lowest priority.
56. UC/OS-II provides various function calls for task management, including task creation, task deletion, task priority change, task suspension, and recovery. Two tasks are automatically generated during system initialization: one is an idle task with the lowest priority, and the other is a system task, it has a low priority and is responsible for calculating the current CPU utilization.
A user task must be created when a task is started after system initialization, that is, an application must be created.Program(User Tasks and applications are easy for Windows users. Otherwise, the system will crash. Of course, there are other requirements. Let's talk about the functions related to task management in the future.:
1: Create the task ostaskcreat ()/ostaskcreatext ()
If you want UCOS to manage your tasks, you must first create a task. You can create a task by passing the task address and other parameters to one of the following two functions. When calling ostaskcreat (), four parameters are required:
Ostaskcreate (void (* task) (void * PD), void * pdata, OS _stk * ptos, intuPrio)
Task: a pointer to the task code,
Pdata: the pointer to the parameter passed to the task,
Ptos: The top pointer of the stack allocated to the task, and PRIO is the priority assigned to the task.
You can also use ostaskcreatext (). However, this function requires nine parameters. The first four parameters are the same as ostaskcreat,
For example:Int8uOstaskcreateext (void (* task) (void * PD), void * pdata, OS _stk * ptos, int8u Prio, int16uID,OS _stk * pbos, OS _stk
* Pbos, OS _stk * pbos, int16u OPT)
The ID parameter creates a special identifier for the task to be created.
Pbos is a pointer to the bottom of the stack of a task and is used for Stack inspection.
STK_ Size is used to specify the size of the number of stack members.
Pext refers to the data domain attached to the user.To expand the OS _tcb of the task.
OPT is used to set the ostaskcreateext () option to specify whether stack check is allowed, whether the stack is cleared, and whether the task needs to perform floating-point operations.
2: Task Stack OS _stk ()
Each task has its own stack. The stack must be declared as an OS _stk type and consists of continuous memory space. Stack space can be statically allocated or dynamically allocated.
3: Stack check ostaskstkchk ()
Sometimes it is necessary to determine the size of the stack space required by the task, because this can avoid assigning too much stack space to the task, thus reducing the ram space required by application code.
4: Delete the task ostaskdel ()
Sometimes you need to delete a task. Deleting a task means that the task returns in sleep state, not that the task code is deleted, but the task code is no longer called by UCOS. Before deleting a task, ensure that the deleted task is not a idle task.
5: request to delete the task ostaskdelreq ()
Sometimes, tasks occupy resources such as memory buffering or semaphores. If another task tries to delete the task, the occupied resources will be lost because they are not released. In this case, you need to find a way to own these resources before you release the resources after using the resources.
6: Changing the task priority ostaskchangeprio ()
When a task is created, a priority is assigned to the task. During the running of the program, you can call this function to change the priority of the task. That is to say, UCOS allows a dynamic change to the task priority.
7: pending task ostasksuspend ()
Task suspension is an additional function, that is, if the task is suspended while waiting for the delay time to arrive, you need to cancel the suspension of the task and wait for the delay time to arrive, the task can be transferred to the ready state. A task can suspend itself or other tasks.
8: Restore task ostaskresume ()
A suspended task can be recovered only through this function.
9: Get task information ostaskquery ()
Call this function to obtain information about itself or other application tasks.
Time management
The time management of uC/OS-II is implemented through scheduled interruption, which is generally10 ms or 100 msOnce,The time frequency depends on the timer programming of the hardware system. The interval between interruptions is fixed, and the interruption also becomes a clock cycle. The implicit meaning here is that if you choose a chip to use UCOS
System, the prerequisite must have a timer.
UC/OS-II requires the user to call the system functions related to the clock cycle provided by the system in the scheduled interrupt service program, such as the interrupt-level task switching function and the system time function.
UCOS time management functions
1: Task latency function ostimedly ()
UCOS provides a function that can be called by a task and delay the task for a specific period of time, that is, ostimedly (). after a task calls ostimedly (), it enters the ready state once the specified time expires or other tasks cancel the delay by calling ostimedlyresume. This task runs immediately only when it has the highest priority among all ready tasks.
2: time, minute, second delay function osrimedlyhmsm ()
Like ostimedly (), calling the osrimedlyhmsm () function will also schedule a task in UCOS and execute the next ready task with the highest priority. When ostimedlyhmsm () expires or ostimedlyresume () expires, it will be ready immediately. Similarly, a task starts to run only when it has the highest priority among all ready tasks.
3: Restore delayed task ostimedlyresume ()
Delayed tasks can not wait for the delay to expire, but cancel the delay of other tasks to make themselves ready. This function can be used to achieve this. In fact, ostimedlyresume () you can also wake up waiting events.
4: system time ostimeget () and ostimeset ()
Memory Management
In ANSIIn C, the malloc and free functions are used to dynamically allocate and release memory. For example, in LinuxYes. However, in an embedded real-time system, multiple such operations may cause memory fragmentation, because the embedded system, especially UCOS, is in the real address mode, in this mode, the entire block of continuous space is required when the task stack is allocated. Otherwise, the task cannot run correctly. Memory ManagementAlgorithmMalloc
And the execution time of free is also unknown. This is the biggest contradiction between the real-time kernel.
Based on the above reasons, uC/OS-II manages consecutive large blocks of memory by partition. Each partition contains an integer with the same size as the memory block, but the memory size between different partitions can be different. When you need to dynamically allocate memory, the system selects an appropriate partition and allocates memory by block. When the memory is released, the block is put back to the partition it previously belongs to. This can effectively solve the fragmentation problem and the execution time is fixed.
At the same time, uCOS-II encapsulates a dynamic memory allocation function suitable for itself based on the above processing.Osmemget () andOsmemput (), but before using these two functions to dynamically allocate memory, you need to create a memory spaceThat isMemory segments.
Data Structure of memory control blocks
Typedef Struct
{
Void* Osmemaddr; pointer to the starting address of the memory partition.
Void* Osmemfreelist; pointer to the next idle memory control block or the next idle memory block,
Int32uOsmemblksize; the size of the memory block in the memory partition, which is defined when the memory partition is created.
Int32uOsmemnblks; the total number of memory blocks in a memory partition is also defined when this memory partition is created.
Int32uOsmemnfree; number of currently available memory partition blocks.
} OS _mem;
1; create a memory partition, osmemcreate ()
2: allocate a memory block, osmemget ()
The application requests a memory block from the created memory partition by calling this function. The unique parameter of this function is the pointer to a specific memory partition.
3: release a memory block, osmemput ()
4: query the status of a memory partition, osqmemquery ().
Inter-task communication and Synchronization
For a multi-task operating system, communication and synchronization between tasks are essential. UC/OS-II providesFour TypesThe synchronization objects are semaphores, mailboxes, message queues, and events. All these synchronization objects are created, waited, sent, and queried interfaces for communication and synchronization between processes.
Task Scheduling
UC/OS-IIThe real-time and multi-task kernel of the detachable type is used. The real-time kernel of the detachable model is ready to run at any time.. The scheduling of uC/OS-II tasks is a fully preemptible Scheduling Based on the task priority, that is, once the task with the highest priority is in the ready state, immediately preemptible the CPU resources of running low-priority tasks. To simplify the system design, uC/OS-II requires that all tasks have different priorities, because the priority of the task also uniquely identifies the task itself.
UCOS task scheduling occurs in the following situations:
1)Because a high-priority task requires some critical resources, the request is suspended and the processor is released.Low-priority tasks in the status are obtained for execution. This scheduling is also called task-level context switching.
2) because of the arrival of the clock cycle, the kernel finds that the high-priority task has the execution condition (such as the time when the sleep clock arrives) in the clock interrupt processing program ), in the interrupted state, the task is directly switched to a high-priority task for execution. This type of scheduling is also called an interrupt-level context switch.
These two scheduling methods are very common in the execution process of uC/OS-II. Generally, the former occurs in the System Service, and the latter occurs in the service program with clock interruption.
Scheduling can be divided into two parts: finding the highest priority task and switching the task. The most important task is to create a ready task table. UEach task in c/o s has an independent stack space andA task control block
TCB (TaskControl Block), where the first member variable isService Stack pointer. The task scheduling module first uses the variable ostcbhighrdyRecord the TCB location of the current most advanced ready taskAnd then call the OS _task_sw () function to switch tasks.