Keil RTX51 Manual page 10

Table of Contents

Advertisement

10
#include <rtx51tny.h>
1
int counter0;
int counter1;
void job0 (void) _task_ 0 {
os_create (1);
while (1) {
}
}
void job1 (void) _task_ 1 {
while (1) {
}
}
In the above example, job0 enables job1 as before. But now, after incrementing
counter0, job0 calls the os_wait function to pause for 3 clock ticks. At this time,
RTX51 switches to the next task, which is job1. After job1 increments counter1,
it too calls os_wait to pause for 5 clock ticks. Now, RTX51 has no other tasks to exe-
cute, so it enters an idle loop waiting for 3 clock ticks to elapse before it can continue
executing job0.
The result of this example is that counter0 gets incremented every 3 timer ticks and
counter1 gets incremented every 5 timer ticks.
Using Signals with RTX51
You can use the os_wait function to pause a task while waiting for a signal (or binary
semaphore) from another task. This can be used for coordinating two or more tasks.
Waiting for a signal works as follows: If a task goes to wait for a signal, and the signal
flag is 0, the task is suspended until the signal is sent. If the signal flag is already 1 when
the task queries the signal, the flag is cleared, and execution of the task continues. The
following example illustrates this:
#include <rtx51tny.h>
int counter0;
int counter1;
void job0 (void) _task_ 0 {
os_create (1);
while (1) {
}
}
counter0++;
os_wait (K_TMO, 3);
counter1++;
os_wait (K_TMO, 5);
if (++counter0 == 0)
os_send_signal (1);
RTX51 Real-Time Operating System
/* mark task 1 as ready */
/* loop forever */
/* update the counter */
/* pause for 3 clock ticks */
/* loop forever */
/* update the counter */
/* pause for 5 clock ticks */
/* mark task 1 as ready */
/* loop forever */
/* update the counter */
/* signal task 1 */

Advertisement

Table of Contents
loading

Table of Contents