I think ucosii

Source: Internet
Author: User

Some time ago, I looked at Ren Zhe's ucosii book and thought that I 'd forget to write something in just two days.

There are a lot of documents and books on the Internet for detailed instructions on ucoⅱ code.

(For example, ucoⅱ source code details,

Code Chinese comments

Usage of various functions

Ucosii reference manual,

Books that describe ucocios include PDF files or other electronic files.

(Original, Ren Zhe, Shao Beibei's http://wenku.baidu.com/view/1949346f011ca300a6c39087.html)

Baidu Library and search engine can find various transplantation blog posts, courseware and source code on single chip microcomputer and arm.

In addition, both chip providers and Development Board providers provide transplanted demos. Many people also write blogs Based on others' demos.

Official http://micrium.com/page/products

)

Here, we mainly use our simplest words to understand the common features of ucoⅱ. We will not discuss kernel implementation.

Mainly divided into the following parts

1. ucosii Overview

2. ucosii porting and configuration (Overview)

3. Use ucosii

4. ucosi tailoring and Optimization

I. ucoⅱ Overview

Main functions:

1. Multi-task management

2. Inter-Task Communication

3. Interrupt and time management

4. Memory Management

Features:

A maximum of 56 tasks are supported.

It is a class of the detachable kernel and always executes the task with the highest priority.

Must use idle tasks

Version: 2.52 2.80 2.86

Additional features

Micro C/OS-II (kernel) Micro C/OS-III (kernel)

Μc/timespaceos RTOS add-ons

Micro C/TCP-IP micro C/GUI

Μc/File System µc/USB

Μc/USB Host µc/USB device

Μc/can µc/Modbus

Μc/FL µc/building blocks

2. ucosii porting and configuration (simple description)

Concept of operating system migration
The porting of an operating system allows a real-time operating system to run on a specific microprocessor platform.
The main code of MCOs-II is written in standard C language, which is easy to transplant. However, some processor-related code still needs to be written in the assembly language, because the microc/OS-ⅱ can only be implemented in the assembly language when reading and writing the Processor register.
The main task of porting is to modify some Code related to the processor hardware.
Conditions to be met by the processor
The C compiler of the processor can generate reusable code.
You can enable or disable the interrupt in the program.
The processor supports interruption and can generate scheduled interruption (usually between 10-Hz ).
The processor supports hardware stacks that can hold a certain amount of data (up to several KB)
The processor has commands to store and read stack pointers and other CPU registers to the stack (or memory ).
Understand the characteristics of the processor
Size end
Growth direction
Timer usage and interrupt management

Porting involves modifying three CPU-related files.

The total amount of source code for uC/OS-II is about 6000-7000 lines, a total of 15 files. Porting a UC/OS-II to an ARM processor requires three files related to the ARM architecture, with a code volume of about 500 lines.

OS _cpu.h OS _cpu_a.asm OS _cpu_c.c
Configuration

The OS _cfg.h file is an application-related configuration file, mainly used to set the operating system. Including:
Set the maximum number of system tasks OS _max_tasks;
You can set OS _max_events for up to event control blocks;
Set OS _stk_growth in the stack direction (1 to decrease and 0 to increase );
Whether the stack test OS _task_create_ext is supported;
Support for task statistics OS _ask_stat_en;
Whether the event flag group OS _flg_en… is supported...

Iii. Use ucosii (for specific functions, refer

Ucosii common function query http://wenku.baidu.com/view/02f11f6c011ca300a6c390c2.html

Function reference manual http://www.doc88.com/p-361144437527.html

Ucosii API function http://blog.chinaunix.net/uid-20766895-id-219986.html

Ucoⅱ function Daquan http://wenku.baidu.com/view/1b56d219fad6195f312ba6a3.html)

Get version:

The application calls osversion () to obtain the current version of μC/OS-ⅱ. Osversion ()
Function returns the version number multiplied by 100. In other words, 200 indicates version 2.00.

Task model

Void yourtask (void * pdata)
{
For (;;){
/* User code */
One of the service routines that call μC/OS-ⅱ:
Osmboxpend ();
Osqpend ();
Ossempend ();
Ostaskdel (OS _prio_self );
Ostasksuspend (OS _prio_self );
Ostimedly ();
Ostimedlyhmsm ();
/* User code */
}
}

First, a demo program tested on PC

/***************************** Test ******** * **********************/# include "includes. H "# define task_stk_size 512 // Task Stack length OS _stk mytaskstk [task_stk_size]; // defines the task stack area OS _stk youtaskstk [task_stk_size]; // defines the int16s key of the task stack area; // key int8u x = 0, y = 0, time = 0 used to exit ucos_ii; // character display position void mytask (void * data ); // declare the task void youtask (void * data ); // declare the task ********* * ***********************/void main (void) {char * S_m = "M"; // define the osinit () character to be displayed; // initialize ucos_ii pc_dossavereturn (); // Save the DOS environment pc_vectset (UCOS, osctxsw ); // install ucos_ii to interrupt ostaskcreate (mytask, // create mytasks_m, // pass parameters to the task & mytaskstk [task_stk_size-1], // set task stack top pointer 0 // task priority to 0); osstart (); // start multi-task management ************ * ********************/void mytask (void * pdata) {char * s_y = "Y"; // define the character to be displayed # If OS _critical_method = 3 OS _cpu_sr cpu_sr; # endif pdata = pdata; OS _enter_critical (); pc_vectset (0x08, ostickisr); // install the clock interrupt vector pc_settickrate (OS _ticks_per_sec); // set the clock frequency OS _exit_critical (); osstatinit (); // initialize the statistics task ostaskcreate (youtask, // create the task mytasks_y, // pass the parameter to the task & youtaskstk [task_stk_size-1], // set the top pointer of the task stack 2 // the priority of mytask is 2); For (;) {If (x> 50) {x = 0; Y + = 2;} pc_dispchar (X, Y, // the position where the characters are displayed * (char *) pdata, disp_bgnd_black + disp_fgnd_white); x + = 1; // If you press ESC, exit ucos_iiif (pc_getkey (& Key) = true) {If (Key = 0x1b) {pc_dosreturn (); // restore the DOS environment} ostimedlyhmsm (0, 0, 3, 0 ); // wait 3 seconds}/************************* task youtask ****** * **********************/void youtask (void * pdata) {# If OS _critical_method = 3 OS _cpu_sr cpu_sr; # endif pdata = pdata; For (;) {If (time = 20) ostasksuspend (0 ); if (time = 40) ostaskresume (0); time ++; If (x> 50) {x = 0; y + = 2;} pc_dispchar (x, y, // character display position * (char *) pdata, disp_bgnd_black + disp_fgnd_white); x + = 1; ostimedlyhmsm (0, 0, 1, 0 ); // wait 1 second}/************************ end ******* *****************************/

1. Multi-task management

First, each task needs to create a Task Stack to save the data during task switching.

# Define task_stk_size 512 // Task Stack Length
OS _stk mytaskstk [task_stk_size]; // defines the task stack area.

Task functions include:

The latter of the two functions of the task is more flexible but will increase the overhead.

Int8u ostaskcreate (void (* task) (void * PD), void * pdata, OS _stk * ptos, int8u PRIO) // task function pointer, parameter, task stack top pointer, priority

Int8u ostaskcreateext (void (* task) (void * PD ),
Void * pdata,
OS _stk * ptos,
Int8u Prio,
Int16u ID,
OS _stk * pbos,
Int32u stk_size,
Void * pext,
Int16u OPT)
{

Delete a task

Int8u ostaskdel (int8u PRIO)
Void requestortask (void * pdata) // Delete another task
Void tasktobedeleted (void * pdata) // delete yourself

Change Priority int8u ostaskchangeprio (int8u oldprio, int8u newprio)

Pending task int8u ostasksuspend (int8u PRIO)

Restore task int8u ostaskresume (int8u PRIO)

Query a task

OS _tcb mytaskdata;
 
Void mytask (void * pdata)
{
Pdata = pdata;
For (;;){
/* User code */
Err = ostaskquery (10, & mytaskdata );
/* Examine error code ..*/
/* User code */
}
}

Procedure

Create a Task Stack

Osinit ()

Create a task

Osstart ()

2. Inter-Task Communication

OS _eventwaitlistinit: initialization of the Event Control Block

Make a task readyOS _eventtaskrdy:

Make a waiting task readyOS _eventtaskwait:

Enables a task waiting for timeout to enter the ready state.OS _eventto:


Semaphores(It is actually a global variable and a management mechanism is added)

Is a sign to indicate whether public resources are available now. If the resource is unavailable, the queue waits for a certain time limit or returns directly.

Application Scenario: multiple tasks need to access the same resource. To avoid confusion in data resources caused by competing resources of each task, semaphores are required for management.

Access to public resources by tasks

Related functions: Create, request (waiting and not waiting), release, delete, query

Procedure:

Declare a semaphore OS _event * fun_semp;

Create a semaphore (register the semaphore just declared)

Request/release semaphores

Mutex semaphores (Semaphores with priority added)

It has more functions than semaphores to avoid turning over the priority.

Application Scenario: multiple tasks need to access public resources and have strict task priority requirements. Priority inversion is not allowed.

Related functions: Create, request, release, delete, query

Procedure:

Same semaphore operation process

Semaphores(Semaphores containing status groups)

Application Scenario: IF Task 4 requires tasks 1, 2, and 3 to be executed at the same time, the semaphore set is used to complete this function.

After Task 1 is completed, change the first position of the semaphore set.

After Task 2 is completed, change the second digit of the semaphore set

After Task 3 is completed, change the third digit of the semaphore set.

Task 4 depends on whether the 1, 2, and 3 bits of the semaphore set have changed.

Related functions: Create a semaphore set, request/query a semaphore set, send/modify a semaphore set, delete a semaphore set, and query a semaphore set

Procedure:

Declare a semaphore set OS _flag_grp * sem_f;

Create a semaphore set (register the semaphore set just declared)

Send/modify semaphores

Request/query semaphores

Message mailbox(Actually, it is a global pointer. Different tasks use this pointer to transmit data)

The function is to provide data transmission between tasks, but one-way transmission of data between two tasks requires a mailbox.

Communication between different tasks requires defining multiple mailboxes for mutual interference.

Application Scenario: When two or more tasks need to transmit one piece of data.

Related functions: Create, receive, send, query, and delete

Procedure:

Define a message mailbox OS _event * str_box;

Create

Receiving/sending

(Delete)

Message Queue(The mechanism for transmitting multiple messages at a time)

Similar to the mailbox, but there is a lot of data transferred each time. Communication between different tasks requires multiple queues to be defined for mutual interference.

Application Scenario: When two or more tasks need to transmit multiple data records.

Related functions: Create, receive, send, query, and delete

Procedure:

Define the message pointer array void * msggrp [Message Queue Length];

Define the Event Control Block OS _event * str_q;

Create

Receiving/sending

(Delete)

3. Interrupt and time management

Ostimedly () latency is calculated by the clock cycle.
Ostimedlyhmsm () latency is calculated by hour, minute, and second in milliseconds.
Ostimedlyresu () Cancel Delay
Ostimeget () System Time Acquisition
Ostimeset () system time settings

4. Memory Management

Related functions

Register the memory Zone

Apply for memory blocks

Release memory blocks

Procedure:

The memory opening area defines the int8u partition [100] [32];

Register the memory Zone

Apply for memory blocks

Memory block usage

Release memory blocks

Iv. ucosi tailoring and Optimization

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.