Chapter 8. RTX-51 Real-Time Operating System
Chapter 8. RTX-51 Real-Time Operating System
Chapter 8. RTX-51 Real-Time Operating System
RTX51 Full which performs both round-robin and preemptive task switching
with 4 task priorities and can be operated with interrupt functions in parallel.
RTX51 supports signal passing; message passing with a mailbox system and
semaphores. The os_wait function of RTX51 can wait for the following events:
interrupt; timeout; signal from task or interrupt; message from task or interrupt;
semaphore.
RTX51 Tiny which is a subset of RTX51 Full. RTX51 Tiny easily runs on
single-chip systems without off-chip memory. However, program using RTX51
Tiny can access off-chip memory. RTX51 Tiny allows round-robin task
switching, supports signal passing and can be operated with interrupt functions
in parallel. The os_wait function of RTX51 Tiny can wait for the following
events: timeout; interval; signal from task or interrupt.
The rest of this section uses RTX-51 to refer to RTX-51 Full and RTX-51 Tiny.
Differences between the two are stated where applicable.
Introduction
Many microcontroller applications require simultaneous execution of multiple
jobs or tasks. For such applications, a real-time operating system (RTOS) allows
flexible scheduling of system resources (CPU, memory, etc.) to several tasks.
RTX-51 implements a powerful RTOS that is easy to use. RTX-51 works with
all 8051 derivatives.
You write and compile RTX-51 programs using standard C constructs and
compiling them with C51. Only a few deviations from standard C are required
in order to specify the task ID and priority. RTX-51 programs also require that 8
you include the RTX51.H or RTX51TNY.H header file. When you select in the
µVision2 dialog Options for Target - Target the operating system, the linker adds
the appropriate RTX-51 library file.
170 Chapter 8. RTX-51 Real-Time Operating System
job0 () _task_ 0 {
job1 () _task_ 1 {
while (1) { /* Endless loop */
counter1++; /* Increment counter 1 */
}
}
8 RTX51 starts the program with task 0 (assigned to job0). The function
os_create_task marks task 1 (assigned to job1) as ready for execution. These
two functions are simple count loops. After the timeout period has been
completed, RTX51 interrupts job0 and begins execution of job1. This function
even reaches the timeout and the system continues with job0.
Getting Started and Creating Applications 171
int counter0;
int counter1;
job0 () _task_ 0 {
os_create_task (1);
while (1) {
counter0++; /* Increment counter 0 */
os_wait (K_TMO, 3, 0); /* Wait 3 timer ticks */
}
}
job1 () _task_ 1 {
while (1) {
counter1++; /* Increment counter 1 */
os_wait (K_TMO, 5, 0); /* Wait 5 timer ticks */
}
}
This program is similar to the previous example with the exception that job0 is
interrupted with os_wait after counter0 has been incremented. RTX51 waits
three timer ticks until job0 is ready for execution again. During this time, job1 is
executed. This function also calls os_wait with a timeout of 5 ticks. The result:
counter0 is incremented every three ticks and counter1 is incremented every five
timer ticks. 8
172 Chapter 8. RTX-51 Real-Time Operating System
int counter0;
int counter1;
job0 () _task_ 0 {
os_create_task (1);
while (1) {
if (++counter0 == 0) { /* On counter 0 overflow */
os_send_signal (1); /* Send signal to task 1 */
}
}
}
job1 () _task_ 1 {
while (1) {
os_wait (K_SIG, 0, 0); /* Wait for signal; no timeout */
counter1++; /* Increment counter 1 */
}
}
In this example, task 1 waits for a signal from task 0 and therefore processes the
overflow from counter0.
In the previous example, task 1 is not immediately started after a signal has
arrived, but only after a timeout occurs for task 0. If task 1 is defined with a
8
174 Chapter 8. RTX-51 Real-Time Operating System
Additional debug and support functions in RTX-51 Full include the following:
Function Description
oi_reset_int_mask Disables interrupt sources external to RTX-51.
oi_set_int_mask Enables interrupt sources external to RTX-51.
os_check_mailbox Returns information about the state of a specific mailbox.
8 os_check_mailboxes
os_check_pool
Returns information about the state of all mailboxes in the system.
Returns information about the blocks in a memory pool.
os_check_semaphore Returns information about the state of a specific semaphore.
os_check_semaphores Returns information about the state of all semaphores in the system.
os_check_task Returns information about a specific task.
Getting Started and Creating Applications 175
Function Description
os_check_tasks Returns information about all tasks in the system.
CAN Functions
The CAN functions are available only with RTX-51 Full. CAN controllers
supported include the Philips 82C200 and 80C592 and the Intel 82526. More
CAN controllers are in preparation.
8
176 Chapter 8. RTX-51 Real-Time Operating System
8
Getting Started and Creating Applications 177
Software
The TRAFFIC application is composed of three files that can be found in the
\KEIL\C51\EXAMPLES\TRAFFIC folder.
TRAFFIC.C contains the traffic light controller program that is divided into the
following tasks:
Task 0 init: initializes the serial interface and starts all other tasks. Task 0
deletes itself since initialization is needed only once.
Task 1 command: is the command processor for the traffic light controller.
This task controls and processes serial commands received.
Task 2 clock: controls the time clock.
Task 3 blinking: flashes the yellow light when the clock time is outside the
active time range.
Task 4 lights: controls the traffic light phases while the clock time is in the
active time range (between the start and end times).
Task 5 keyread: reads the pedestrian push button and sends a signal to the
task lights.
Task 6 get_escape: If an ESC character is encountered in the serial stream
the command task gets a signal to terminate the display command.
SERIAL.C implements an interrupt driven serial interface. This file contains the
functions putchar and getkey. The high-level I/O functions printf and getline
call these basic I/O routines. The traffic light application will also operate
without using interrupt driven serial I/O. but will not perform as well.
GETLINE.C is the command line editor for characters received from the serial
port. This source file is also used by the MEASURE application.
8
178 Chapter 8. RTX-51 Real-Time Operating System
TRAFFIC Project
Open the TRAFFIC.UV2 project file that is
located in \KEIL\C51\EXAMPLES\TRAFFIC folder
with µVision2. The source files for the
TRAFFIC project will be shown in the
Project Window – Files page.
Build the TRAFFIC program with Project - Build or the toolbar button.
The push_key
signal function
simulates the
pedestrian
push key that
8 switches the
light system to
walk state.
This function
is called with
the Push for Use Debug – Function Editor to open TRAFFIC.INC. This file is specified
Getting Started and Creating Applications 179
the Push for under Options for Target – Debug – Initialization File and defines the
Walk toolbar signal function push_key, the port initialization and the toolbar button.
button. Note: the VTREG symbol Clock is literalized with a back quote (`),
since there is a C function named clock in the TRAFFIC.C module.
Refer to “Literal Symbols” on page 121 for more information.
The Serial Window #1 displays the printf output and allows you to enter
the traffic light controller commands described in the table above.
8
180 Chapter 8. RTX-51 Real-Time Operating System
The following section exemplifies RTX debugging with the TRAFFIC example.
Stop program execution, reset the CPU and kill all breakpoints.
µVision2 is completely kernel aware. You may display the task status
with the menu command Peripherals – RTX Tiny Tasklist.
8
Getting Started and Creating Applications 181
The dialog RTX51 Tiny Tasklist gives you the following information:
Heading Description
TID task_id used in the definition of the task function.
Task Name name of the task function.
State task state of the function; explained in detail in the next table.
Wait for event the task is waiting for; the following events are possible (also in combination):
Event Timeout: the task Timer is set to the duration is specified with the os_wait function
call. After the Timer decrements to zero, the task goes into Ready state.
Interval: the time interval specified with os_wait is added to the task Timer value.
After the Timer decrements to zero, the task goes into Ready state.
Signal: the os_wait function was called with K_SIG and the task waits for Sig = 1.
Sig status of the Signal bit that is assigned to this task.
Timer value of the Timer that is assigned to this task. The Timer value decrements with
every RTX system timer tick. If the Timer becomes zero and the task is waiting for
Timeout or Interval the task goes into Ready state.
Stack value of the stack pointer (SP) that is used when this task is Running.
RTX-51 Tiny contains an efficient stack management that is explained in the "RTX51 Tiny" User’s
Guide, Chapter 5: RTX51 Tiny, Stack Management.
This manual provides detailed information about the Stack value.
8
182 Chapter 8. RTX-51 Real-Time Operating System
The Debug – Breakpoints… dialog allows you to define breakpoints that stop
the program execution only when the task specified in the _TaskRunning_
debug function argument is Running. Refer to “Predefined Functions” on page
134 for a detailed description of the _TaskRunning_ debug function.