第四章 基于RTX51实时操作系统程序设计基础

Download Report

Transcript 第四章 基于RTX51实时操作系统程序设计基础

实时操作系统概述
电子系统设计——基于RTX51实时操作系统程序
设计基础
DACCON.7
DACCON.6
DACCO
N.5
DACCO
N.4
DACCO
N.3
DACCO
N.2
DACCO
N.1
DACC
ON.0
MODE
RNG1
RNG0
CLR1
CLR0
SYNC
*PD1
*PD0
一、实时操作系统概述
操作系统是一种管理计算机硬件的程序,为应用程序提供了基本的运行条件,在计
算机用户和计算机硬件之间扮演着中介的角色。
The operating system (OS) is the main program that controls how your computer
system functions. The OS manages the computer’s hardware, including the processor,
memory, and storage devices, as well as peripheral devices.
It is responsible for the management, scheduling, and interaction of tasks. Your first
interaction with the OS is the user interface.
Four categories:
Real-Time (RTOS)
Single-User, Single-Task
Single-User, Multitask
Multiuser
WHAT THE OS DOES





Provides user interface
Manages the CPU
Manages memory and storage
Manages hardware and peripheral devices
Coordinates application software with the CPU
The operating systems provides a way for the user to interact with the
computer, manages the processor (CPU), manages the memory and storage,
manages the computer system’s hardware and peripheral devices, and
provides a consistent means for software applications to work with the CPU.
THE USER INTERFACE


Enables you to
interact with the
computer
Types of interfaces:



Command-driven
interface
Menu-driven
interface
Graphical user
interface (GUI)
PROCESSOR MANAGEMENT

Controls the timing of events the processor
works on
 Interrupts
 Interrupt
handler
 Interrupt table
 Stack
MEMORY AND STORAGE MANAGEMENT

The operating system allocates space in
RAM for instructions and data
HARDWARE AND PERIPHERAL DEVICE MANAGEMENT

Device drivers



Programs that enable the operating system to
communicate with peripheral devices
Provided by the manufacturer of the device
Plug and Play


Hardware and software standard
Facilitates the installation of new hardware
FILE MANAGEMENT
The operating system provides an
organizational structure for the computer’s
contents
 Hierarchical structure of directories

 Drives
 Folders

Subfolders

Files
REAL-TIME OPERATING SYSTEMS
REAL-TIME OPERATING SYSTEMS
实时系统指系统的计算正确性不
仅取决于计算的逻辑正确性,还
取决于产生结果的时间。
一个实时操作系统面对外界环境
发生的变化(包括时间的消逝)
时,必须确定性地保证在满足时
间要求的情况下予以正确的反应
处理。
REAL-TIME OPERATING SYSTEMS
– Multiple events handled by a single processor
– Events may occur simultaneously
– Processor must handle multiple, often competing events
– Wide range of RTOS systems


Systems with a specific purpose
and a certain result
Uses include:
 Industrial machines
 Robotic equipment
 Automobiles
 Video game consoles
 Home appliances
REAL-TIME OPERATING SYSTEMS
1、Task
A task is a program running on the CPU core
of a microcontroller.
A real-time operating system allows the
execution of multiple tasks on a single CPU.
All tasks execute as if they completely
"owned" the entire CPU.
Without a multitasking kernel (an RTOS), only
one task can be executed by the CPU at a time.
This is called a single-task system.
REAL-TIME OPERATING SYSTEMS
a、Single-task systems (superloop)
A superloop application is basically a program that runs in
an endless loop, calling OS functions to execute the
appropriate operations (task level). No real-time kernel is
used, so interrupt service routines (ISRs) must be used
for real-time parts of the software or critical operations
(interrupt level). This type of system is typically used in
small, uncomplex systems or if real-time behavior is not
critical.
REAL-TIME OPERATING SYSTEMS
void main (void)
{ int counter = 0;
while (1) /* repeat forever */
{check_serial_io (); /*
check for serial input */
process_serial_cmds (); /*
process serial input */
check_kbd_io (); /* check
for keyboard input */
process_kbd_cmds (); /*
process keyboard input */
adjust_ctrlr_parms (); /*
adjust the controller */
counter++; /* increment
counter */ }
}
REAL-TIME OPERATING SYSTEMS
b、COOPERATIVE MULTITASKING
Cooperative multitasking expects cooperation of
all tasks. Tasks can only be suspended by calling
a function of the operating system. If they do not,
the system “hangs”, which means that other tasks
have no chance of being executed by the CPU
while the first task is being carried out.
REAL-TIME OPERATING SYSTEMS
REAL-TIME OPERATING SYSTEMS
c、PREEMPTIVES MULTITASKING
A real-time operating system needs a regular timerinterrupt to interrupt tasks at defined times and to
perform task-switches if necessary. The highestpriority
task in the READY state is therefore always executed,
whether it is an interrupted task or not. If an ISR makes
a higher priority task ready, a task switch will occur and
the task will be executed before the interrupted task is
returned to.
REAL-TIME OPERATING SYSTEMS
REAL-TIME OPERATING SYSTEMS
1、Scheduling
There are different algorithms that determine which task
to execute, called schedulers.
All schedulers have one thing in common:
they distinguish between tasks that are ready to be
executed (in the READY state) and the other tasks that
are suspended for any reason (delay, waiting for mailbox,
waiting for semaphore, waiting for event, and so on). The
scheduler selects one of the tasks in the READY state
and activates it (executes the program of this task). The
task which is currently executing is referred to as the
active task.
REAL-TIME OPERATING SYSTEMS
a、ROUND-ROBIN SCHEDULING ALGORITHM
With round-robin scheduling, the scheduler has a list of
tasks and, when deactivating the active task, activates
the next task that is in the READY state. Round-robin
can be used with either preemptive or cooperative
multitasking. It works well if you do not need to
guarantee response time, if the response time is not an
issue, or if all tasks have the same priority.
All tasks are on the same level; the possession of the
CPU changes periodically after a predefined execution
time. This time is called timeslice, and may be defined
individually for every task.
REAL-TIME OPERATING SYSTEMS
b、PRIORITY-CONTROLLED SCHEDULING
ALGORITHM
In real-world applications, different tasks require different
response times. For example, in an application that controls
a motor, a keyboard, and a display, the motor usually
requires faster reaction time than the keyboard and display.
While the display is being updated, the motor needs to be
controlled. This makes preemptive multitasking a must.
Round-robin might work, but because it cannot guarantee a
specific reaction time.
REAL-TIME OPERATING SYSTEMS
In priority-controlled scheduling, every task is assigned a
priority. The order of execution depends on this priority.
The scheduler activates the task that has the highest
priority of all tasks in the READY state.
This means that every time a task with higher priority than
the active task gets ready, it immediately becomes the
active task.
REAL-TIME OPERATING SYSTEMS
2、Communication between tasks
In a multitasking (multithreaded) program, multiple tasks work
completely separately. Because they all work in the same application,
it will sometimes be necessary for them to exchange information with
each other.
a、GLOBAL VARIABLES
The easiest way to do this is by using global variables. In certain situations,
it can make sense for tasks to communicate via global variables, but most
of the time this method has various disadvantages.
For example, if you want to synchronize a task to start when the value of a
global variable changes, you have to poll this variable, wasting precious
calculation time and power, and the reaction time depends on how often
you poll.
REAL-TIME OPERATING SYSTEMS
b、COMMUNICATION MECHANISMS
When multiple tasks work with one another, they often have to:
● exchange data,
● synchronize with another task, or
● make sure that a resource is used by no more than one task at a time.
a) Signals
Signals represent the simplest and fastest form of task communication. No
actual information is exchanged - only a stimulus is activated for a task.
These can always be used when a pure task synchronisation is required
without data exchange.
b) MAILBOXES AND QUEUES
A mailbox is basically a data buffer managed by the RTOS and is used for
sending a message to a task.
A queue works in a similar manner, but handle larger messages than mailboxes,
and every message may have a individual size.
REAL-TIME OPERATING SYSTEMS
c) SEMAPHORES
In a multi-tasking system there is often competition for resources. When
several tasks can use the same portion of memory, the same serial I/O
channel or another system resource, you have to find a way to keep the tasks
out of each other‘s way. The semaphore is a protocol mechanism, which is
used primarily to control access to shared resources (mutual exclusion).
By means of the semaphore concept, resources can be shared free of
conflicts between the individual tasks.
RTX51 多任务实时操作系统
RTX51 是美国Keil 公司开发的一种小型的应用于MCS51 系列
单片机的实时多任务操作系统,它的使用可以简化比较复杂、
有严格时间限制的软件的设计过程。RTX51主要有两个不同的
可用版本:
①RTX5lFull版。这里称之为RTX51的标准版,既可以以循环
(Round 一Robin )方式执行任务,也可以按4 级任务优先级
的方式切换不同优先级的任务。标准版以并行方式工作,支
持中断管理,信号和消息可以通过邮箱系统在不同任务之间
传递。
②RTX5lTiny版。这里称之为RTX5l的精简版,是其标准版的
一个子集。它可以很容易地运行在8051 的单芯片系统而不需
要任何外部数据存储器。通用性强,系统需求低,但功能上
受到限制。它只支持循环方式和信号方式的任务切换,而不
支持优先级方式的任务切换。
RTX51 多任务实时操作系统
Advantages in using a Real-Time Multitasking Executive:
● A program can be more easily implemented, tested and maintained by
breaking down the problem to be solved into individual, easily comprehensible
tasks.
● The modular approach allows individual tasks to be used in other projects.
● Since the real-time and multitasking problems which occur are already
solved the time required for creating programs and testing is considerably
reduced.
Advantages of RTX-51:
● Simple use of RTX-51 by integration in the Keil C51 development system.
● Complete support of all C51 features such as floating-point operations,
reentrant functions and interrupt functions.
● User-friendly configuration of RTX-51 for all members of the 8051 family.
● Flexibility - only requires a few system resources and can also be applied for
time-critical applications
RTX51 多任务实时操作系统
RTX51Tiny系统资源需求:
Parameter
Maximum Number of Defined Tasks
Maximum Number of Active Tasks
Required CODE Space
Required DATA Space
Required STACK Space
Required XDATA Space
Timer
System Clock Divisor
Interrupt Latency
Context Switch Time
Limits
16
16
900 Bytes Max
7 Bytes
3 Bytes/Task
0 Bytes
0
1,000-65,535
20 Cycles or Less
100-700 Cycles
RTX51 多任务实时操作系统
RTX-51 requires the following 8051 system resources:
CODE Memory:
Approx. 6 to 8 Kbytes, depending on the function scope used.
Internal (DATA and IDATA) RAM:
40 to 46 bytes for system data (depending on the selected processor type).
20 to 200 bytes for the stack (can be configured by the user).
Register bank 0 for standard tasks; register banks 1, 2 and 3 for fast tasks or
C51 interrupt functions.
External (XDATA) RAM:
Minimal 450 bytes.
Timer 0, 1 or 2 for the system clock (can be configured by the user).
RTX51 多任务实时操作系统
RTX-51 recognizes two classes of tasks:
● Fast tasks with especially short responses and interrupt times. Each
fast task uses an individual register bank of the 8051 and contains its
own stack area. RTX-51 supports a maximum of three fast tasks active
at a time.
● Standard tasks that require somewhat more time for the task
switching,therefore less internal memory than the fast tasks. All
standard tasks share a register bank and a stack area; during a task
change the current contents of registers and the stack are stored in the
external RAM. RTX-51 supports a maximum of 16 standard tasks
active at a time.
Task States
Each RTX51 Tiny task is always in exactly one state which tells the disposition of
the task.
State
RUNNING
READY
WAITING
DELETED
TIME-OUT
Description
The task that is currently running is in the RUNNING State. Only one task at
a time may be in this state. The os_running_task_id returns the task
number of the currently executing task.
Tasks which are ready to run are in the READY State. Once the Running
task has completed processing, RTX51 Tiny selects and starts the next
Ready task. A task may be made ready immediately (even if the task is
waiting for a timeout or signal) by setting its ready flag using the
os_set_ready or isr_set_ready functions.
Tasks which are waiting for an event are in the WAITING State. Once the
event occurs, the task is switched to the READY State. The os_wait function
is used to place a task in the WAITING State.
Tasks which have not been started or tasks which have been deleted are in
the DELETED State. The os_delete_task routine places a task that has
been started (with os_create_task) into the DELETED State.
Tasks which were interrupted by a Round-Robin Time-Out are in the TIMEOUT State. This state is equivalent to the READY State for Round-Robin
RTX51 多任务实时操作系统
Multi-Tasking Programs
More sophisticated C programs may implement a pseudo-multitasking
scheme where several functions (or tasks) are called in a loop. For example:
void main (void)
{ int counter = 0;
while (1) /* repeat forever */
{check_serial_io (); /*
check for serial input */
process_serial_cmds (); /*
process serial input */
check_kbd_io (); /* check
for keyboard input */
process_kbd_cmds (); /*
process keyboard input */
adjust_ctrlr_parms (); /*
adjust the controller */
counter++; /* increment
counter */ }
}
RTX51 Tiny Programs
void check_serial_io_task (void) _task_ 1 {
/* This task checks for serial I/O */
}
void process_serial_cmds_task (void) _task_ 2 {
/* This task processes serial commands */
}
void check_kbd_io_task (void) _task_ 3 {
/* This task checks for keyboard I/O */
}
void process_kbd_cmds_task (void) _task_ 4 {
/* This task processes keyboard commands */
}
void startup_task (void) _task_ 0 {
os_create_task (1); /* Create serial_io Task */
os_create_task (2); /* Create serial_cmds Task */
os_create_task (3); /* Create kbd_io Task */
os_create_task (4); /* Create kbd_cmds Task */
os_delete_task (0); /* Delete the Startup Task */
}