UC/OS-II beginner programmer's Guide

Source: Internet
Author: User

UC/OS-II beginner programmer's Guide

(A) uC/OS-II overview uC/OS-II is a priority-based hard real-time kernel can be preemptive. Since its release in, it has been widely used all over the world. It is a kernel specially designed for embedded devices, it has been transplanted to more than 40 CPUs with different structures and runs on various systems from 8-bit to 64-bit. It is particularly worth mentioning that the system has passed the us faa certification since version 2.51 and can run on systems with extremely demanding security requirements, such as spacecraft. Since uC/OS-II offers free access to code, choosing uC/OS is undoubtedly the most economical option for Embedded RTOS. Basic Structure of uC/OS-II applications

Application of uC/OS-II, naturally to develop applications for it, the following discusses the basic structure of the Application Based on uC/OS-II and precautions.

Each uC/OS-II application requires at least one task. Each task must be written as an infinite loop. The following is the recommended structure: Void task (void * pdata)

{

Int8u err;

Inittimer (); // optional

For (;;)

{

// Your application code

.......

........

Ostimedly (1); // optional

}

} The above is the basic structure. Why should we write an infinite loop? This is because the system reserves a stack space for each task. When the task is switched, the system restores the context and executes A reti command to return it. If you allow the task to run to the last curly braces (which generally means a RET command), it is likely to destroy the system stack space and thus make the application execution uncertain. In other words, it is "running and flying. Therefore, each task must be written as an infinite loop. Programmers must believe that their tasks will give up the CPU right, whether the system forces (through ISR) or voluntarily gives up (by calling the OS API ). Now let's talk about the inittimer () function in the above program. This function should be provided by the system, and the programmer is obligated to call it within the task with the highest priority and cannot be called within the for loop. Note that this function is related to the CPU used, and each system has its own timer initialization program. In the help manual for uC/OS-II, the author specifically stressed that timer initialization programs cannot be called in osinit () or osstart, this will damage the portability of the system and cause performance loss. Therefore, the best way to do this is to call the program with the highest priority. This ensures that when osstart () calls the system's internal function osstarthighrdy () to start multiple tasks, the first step is to execute the timer initialization program. You can also create a task with the highest priority to do only one thing, that is, execute timer initialization, and then call ostasksuspend () to hook yourself up and never execute it again. However, this will waste a TCB space. For those systems with tight Ram, it is better not to use them.

3. Some important uC/OS-II APIs

Any operating system provides a lot of APIS for programmers, and uC/OS-II is no exception. Because uC/OS-II is oriented to embedded development, does not require big and complete, so the kernel provides the API is mostly and multi-task is closely related. There are mainly the following types:

1) Task Type

2) Message class

3) Synchronization class

4) Time

5) critical section and events

I personally think that for junior programmers, task and time are two types of APIs that must be mastered first. Next I will introduce more important:

1) ostaskcreate Function

This function should be called at least once in the main function and after the osinit function is called. The role is to create a task. Currently, there are four parameters: the entry address of the task, the parameters of the task, the first address of the task stack, and the priority of the task. After calling this function, the system will first apply for an empty TCB pointer from the TCB idle list, and then initialize the task stack based on the parameters given by the user, in the internal task readiness table, mark the task as ready. Finally, a task is created successfully.

2) ostasksuspend Function

This function is very simple. You should understand its function at first glance, and it can suspend the specified task. If the current task is suspended, osshed, the system executes the task switchover pilot function, to perform a task switchover. This function has only one parameter, that is, the priority of the specified task. Why is the priority? In fact, in the system, priority not only indicates the execution order of a task, but also plays a role for each task separately. In other words, priority is the task id. Therefore, uC/OS-II does not allow tasks of the same priority.

3) ostaskresume Function

This function is opposite to the above function, which is used to restore the specified suspended function to the ready state. If the priority of a recovery task is higher than that of the current task, a task switchover is triggered. The parameter is similar to the ostasksuspend function, and is the priority of the specified task. Note that this function is not required to be used in pairs with the ostasksuspend function.

4) OS _enter_critical macro

Many people think that it is a function. In fact, if it is not, carefully analyze the OS _cpu.h file. It is a macro with OS _exit_critical that will be discussed below. They all involve the implementation of specific CPUs. Generally, it is replaced by one or more Embedded Assembly codes. Because the system wants to hide the internal implementation by programmers at the upper layer, it is generally declared that the system enters the critical section after executing this command. In fact, it is just an interruption. In this way, as long as the task does not voluntarily give up the CPU usage, other tasks will not have the opportunity to occupy the CPU, It is exclusive to this task. So we entered the critical section. This macro can be used less or less because it will damage some system services, especially time services. And the system's response performance to the outside world is reduced.

5) OS _exit_critical macro

This is another macro used together with the macro described above. It is described in the system manual to exit the critical section. In fact, it is re-opening and interrupted. It must be noted that it must be aligned with the above macro, otherwise it will have unexpected consequences. In the worst case, the system crashes. We recommend that programmers use these two macros as few as possible, because they do damage the system's multi-task performance.

6) ostimedly Function

This should be the one with the most calls by programmers. This function completes the function very easily, that is, it suspends the current task and then switches the task. After the specified time arrives, restore the current task to the ready state, but not necessarily run. If the task is the highest priority after recovery, run it. Simply put, a task can be executed again after a certain delay, or the CPU usage is temporarily abandoned. A task can call the APIs that can discard CPU usage without explicit calling, but the multi-task performance will be greatly reduced because task switching is only performed by the clock mechanism. A good task should take the initiative to give up the right to use it after completing some operations. Do you want to share some good things!

(4) uC/OS-II multi-task implementation mechanism analysis previously said that uC/OS-II is a priority-based multi-task kernel can be preemptive. How is its multitasking mechanism implemented? Understanding these principles can help us write more robust code. Because of our elementary programmers, this article does not intend to write another uC/OS-II source code analysis, that too many articles, This article intends to discuss this problem from the perspective of the implementation principle.

First, let's see why the multi-task mechanism can be implemented? In fact, in the case of a single CPU, there is no real multi-task mechanism, there are only different tasks to use the CPU in turn, so in essence it is still a single task. However, because the CPU execution speed is very fast, and the task switching frequency is very frequent and the switching speed is very fast, we feel that many tasks are running at the same time. This is the so-called multitasking mechanism.

As described above, it is not difficult to find that to implement a multi-task mechanism, the target CPU must have a way to change the PC at runtime; otherwise, the switch cannot be implemented. Unfortunately, no CPU currently supports this command to directly set the PC pointer. However, generally, CPUs allow indirect modification of PCs through commands such as JMP and call. The implementation of our multitasking mechanism is also based on this starting point. In fact, we use the call command or Soft Interrupt command to modify the PC, mainly Soft Interrupt. But in some CPUs, there is no such concept as Soft Interrupt. Therefore, we use several push commands on those CPUs and add a call command to simulate a Soft Interrupt.

Think back to your knowledge in the microcomputer principles course. When an interruption occurs, the CPU saves the values of the current PC and Status Register to the stack, then, set the PC as the endpoint address of the interrupted service program, and then execute the interrupted service program in a machine cycle. After execution, A reti command is generally executed, which restores the values in the current stack to the Status Register and PC. In this way, the system will go back to the point before the interruption and continue the execution. So imagine? What will happen if the value in the stack is manually changed during the interruption? Or by changing the value of the current stack pointer, what will happen? If the changes are random, the results are unpredictable errors. Because we cannot determine what commands will be executed next to the machine, but if the changes are planned and follow certain rules, we can implement a multi-task mechanism. In fact, this is the core of almost all operating systems. However, their implementation is not as simple as this.

Next, let's take a look at the uC/OS-II and then how to deal with this. In uC/OS-II, each task has a task control block, which is a complex data structure. A pointer is stored where the fast offset of a task is 0, which records the dedicated stack address of the task. In fact, in UC/OS-II, each task has its own dedicated stack and cannot infringe upon each other. This requires programmers to make sure they are in their programs. The general practice is to declare them as static arrays. It must be declared as the OS _stk type. When a task has its own stack, you can record each task stack there where the fast offset of the previously mentioned task control is 0. In the future, whenever a task is switched, the system will inevitably enter an interrupt, which is generally achieved through soft interruption or clock interruption. Then, the system saves the stack address of the current task and restores the stack address of the task to be switched. Because the stack of the task must also store the address (remember what we mentioned earlier, the system will first interrupt when a task is switched, once the CPU is interrupted, the address will be pushed into the stack). In this way, the PC address can be changed to the address of the next task.

The above is the uC/OS-II multi-task implementation mechanism, we are here to talk about this problem, is to hope that our programmers can use this mechanism, write more robust, more efficient code.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.