Theory of Operation
RTX51 Tiny uses the standard 8051 Timer 0 (mode 1) to generate a periodic interrupt. This
interrupt is the RTX51 Tiny Timer Tick. Timeout and interval values specified for the RTX51 Tiny library routines are measured using the RTX51 Tiny Timer Tick.
By default？缺省；默认？, the RTX51 Timer Tick interrupt occurs every 10,000 machine cycles. So, for a standard 8051 running at 12MHz, the period of the timer tick is 0.01 seconds or a frequency of 100Hz. The value may be changed in the CONF_TNY.A51 configuration file.
RTX51 Tiny is basically a task switcher. To create an RTX51 Tiny program, you must create an application with one or more task functions. The following details will help you to quickly gain an understanding of RTX51.
！ Tasks are defined in the C Programming Language using new keywords supported by the
Keil C51 Compiler.
！ RTX51 Tiny maintains each task in exactly one state (Running, Ready, Waiting, Deleted,
！ Only one task at a time may be in the Running State.
！ Many tasks may be in the Ready, Waiting, Deleted, or Time-Out States.
！ An Idle Task is always ready to run in the event that all of your defined tasks are blocked.
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 RUNNING
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 READY 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 WAITING
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 DELETED
(with os_create_task) into the DELETED State.
Tasks which were interrupted by a Round-Robin Time-Out are in the TIME-OUT TIME-OUT State. This state is equivalent to the READY State for Round-Robin programs.
Events in a real-time operating system may be used to control the execution of tasks in the program. A task may wait for an event(Event TIMEOUT、INTERVAL) or may set event flags for
other tasks(Event SIGNAL).
The os_wait function allows a task to wait for one or more events.
！ The Timeout is a common event for which a task can wait. A timeout is simply a number
of clock ticks. While a task waits for a timeout, other tasks may execute. Once the
specified number of timer ticks elapses, the task may continue execution.在等待事件发
！ The Interval is a variation of the Timeout. An interval is like a timeout except that the
specified number of clock ticks is relative to the last time the os_wait function was invoked
by the task. The Interval may be used to generate a task which is run on a regular,
synchronous schedule (like once every second) regardless of how long the task takes between
calls to the os_wait function. If the specified number of clock ticks have already elapsed
(since the os_wait function was last invoked) the task is re-started immediately - no other
tasks execute.与Timeout事件不同的是？RTX51 TINY的定时器不复位？也就是说Interval
！ A Signal is a simple form of inter-task communication. A task can wait for another task to
send it a signal (with the os_send_signal and isr_send_signal functions).如果一个任务要等
！ Each task has a Ready flag which may be set by other tasks (with the os_set_ready and
isr_set_ready functions). A task that is waiting for a timeout, interval, or signal may be
started by setting its ready flag.这里的start指的是任务进入Ready状态。
The Task Scheduler assigns the processor to a task. The RTX51 Tiny scheduler determines which task to run using to the following rules:
The current task is interrupted if:
; The task calls the os_switch_task and another task is ready to run.
； The task calls the os_wait function and the specified event has not occurred.
？ The task has executed for longer than the defined round-robin time slice. Another task is started if:
？ No other task is running.
， The task to be started is in the READY State or TIME-OUT State.
Round-Robin Task Switching
RTX51 Tiny may be configured to use Round-Robin Multitasking (or task switching). Round-Robin allows quasi-parallel execution of several tasks. Tasks are not really executed
concurrently but are time-sliced (the available CPU time is divided into time slices and RTX51 Tiny assigns a time slice to each task). Since the time slice is short (only a few milliseconds) it appears as though tasks execute simultaneously.
Tasks execute for the duration of their time-slice (unless the task's time slice is given up). Then, RTX51 Tiny switches to the next task that is ready to run. The duration of a time slice may be defined by the RTX51 Tiny Configuration.
！ Rather than wait for a task's time slice to expire, you may use the os_wait function or the
os_switch_task function to allow RTX51 Tiny to switch to another task. The os_wait
function suspends the current task (changes it to the WAITING State) until the specified
event occurs (and the task is changed to the READY State). During this time, any number of
other tasks may run.
Configuration RTX51 Tiny
A default configuration of CONF_TNY.A51 is included in the RTX51 Tiny Library. However, to
guarantee the configuration used for your application, you must copy the CONF_TNY.A51
file to your project folder and add it to your project.
！ If you do not include a configuration file (CONF_TNY.A51) in your project, the default
configuration file from the library is included automatically. Subsequent changes to the
configuration file stored in the library may have adverse effects on the operation of your
Hardware Timer Configuration
！ INT_CLOCK specifies the number of cycles before the timer generates an interrupt. This
number may be a value from 1000-65535. Small numbers generate faster interrupts. This
number is used to calculate the reload value for the timer (65536-INT_CLOCK). The
default setting is 10000.溢出时间就是Timer Tick。
Round-Robin Task Switching is enabled by default.
！ TIMESHARING specifies the number of RTX51 Tiny timer ticks each task runs before a
Round-Robin task switch. A value of 0 disables Round-Robin task switching. The default
setting is 5 timer ticks.
RTX51 Tiny includes two library files:
！ RTX51TNY.LIB is used for non-banking RTX51 Tiny programs.
！ RTX51BT.LIB is used for code-banking RTX51 Tiny programs.
！ You should not explicitly include either of the RTX51 Tiny libraries in your application.
That is done automatically when you use the µVision IDE or when you use the
command-line linker. If you do not include a configuration file (CONF_TNY.A51) in
your project, the default configuration file from the library is included automatically.所
Using RTX51 Tiny
When you write programs for RTX51 Tiny, you must define your RTX51 Tiny Tasks using the _task_ keyword and use the RTX51 Tiny kernel routines, which are prototyped in RTX51TNY.H.
RTX51 Tiny requires the use of only one include file: RTX51TNY.H. All library routines and
constants are defined in this header file. You may include it in your RTX51 Tiny source files as
follows: #include <rtx51tny.h>
There are a few rules you must follow when creating RTX51 Tiny programs.
！ Be sure to include the RTX51TNY.H header file.
！ Do not create a main C function. RTX51 Tiny has its own main function.
！ Your program must contain at least one task function.
！ RTX51 Tiny programs must have interrupts enabled (EA=1). Be careful if you disable
interrupts for critical sections. Refer to Interrupts in the Overview for details on how the
interrupt system is used by RTX51 Tiny.
！ Your program must call at least one RTX51 Tiny library routine (like os_wait).
Otherwise, the linker will not include the RTX51 Tiny Library.
！ Task 0 is the first function in your program that executes. You must call the
os_create_task function from task 0 to run other tasks.
！ Task functions must never exit or return. The task must repeat using a while(1) or similar
construct. Use the os_delete_task function to halt a running task.
！ You must specify RTX51 Tiny in µVision or on the linker command line.
Real-Time or multitasking applications are composed of one or more tasks that perform specific operations. RTX51 Tiny supports a maximum of 16 tasks.
Tasks are simply C functions that have a void return type and a void argument list and are
declared using the _task_ function attribute. For example: void func (void) _task_ task_id func is the name of the task function
task_id is a task ID number from 0 to 15
！ All tasks should be implemented as endless loops. A task should never return.
！ Tasks cannot return a function value. They must have a void return type.
！ Parameters may not be passed to a task. Tasks must have a void argument list.
！ Each task must be assigned a unique, non-recurring task ID.
！ To minimize the memory requirements of RTX51 Tiny, number your tasks sequentially
beginning with 0.
Compiling and Linking