|
|
Home / RL-ARM User's Guide (MDK v4)
Your First RTX Application
This section demonstrates an example of using the RTX kernel for a
simple application. The example is located in the folder
\Keil\ARM\RL\RTX\Examples\RTX_ex1. The application must
perform two activities. The first activity must continuously repeat
50 ms after the second activity completes. The second activity must
repeat 20 ms after the first activity completes.
Hence, you can implement these activities as two separate tasks,
called task1 and task2:
-
Place the code for the two activities into two separate
functions (task1 and task2). Declare the two functions as tasks
using the keyword __task (defined in RTL.H) which
indicates a RTX task.
__task void task1 (void) {
.... place code of task 1 here ....
}
__task void task2 (void) {
.... place code of task 2 here ....
}
-
When the system starts up, the RTK kernel must start before
running any task. To do this, call the os_sys_init function in the C
main function. Pass the function name of the first task as
the parameter to the os_sys_init function. This ensures that
after the RTX kernel initializes, the task starts executing rather
than continuing program execution in the main function.
In this example, task1 starts first. Hence, task1 must create
task2. You can do this using the os_tsk_create function.
__task void task1 (void) {
os_tsk_create (task2, 0);
.... place code of task 1 here ....
}
__task void task2 (void) {
.... place code of task 2 here ....
}
void main (void) {
os_sys_init (task1);
}
-
Now implement the timing requirements. Since both activities
must repeat indefinitely, place the code in an endless loop in each
task. After the task1 activity finishes, it must send a signal to
task2, and it must wait for task2 to complete. Then it must wait
for 50 ms before it can perform the activity again. You can use the
os_dly_wait function to
wait for a number of system intervals. The RTX kernel starts a
system timer by programming one of the on-chip hardware timers of
the ARM processors. By default, the system interval is 10 ms and
timer 0 is used (this is configurable).
You can use the os_evt_wait_or function to
make task1 wait for completion of task2, and you can use the
os_evt_set function to
send the signal to task2. This examples uses bit 2 (position 3)
of the event flags to inform the other task when it
completes.
task2 must start 20 ms after task1 completes. You can use the
same functions in task2 to wait and send signals to task1. The
listing below shows all the statements required to run the
example:
/* Include type and function declarations for RTX. */
#include <rtl.h>
/* id1, id2 will contain task identifications at run-time. */
OS_TID id1, id2;
/* Forward declaration of tasks. */
__task void task1 (void);
__task void task2 (void);
__task void task1 (void){
/* Obtain own system task identification number. */
id1 = os_tsk_self();
/* Create task2 and obtain its task identification number. */
id2 = os_tsk_create (task2, 0);
for (;;) {
/* ... place code for task1 activity here ... */
/* Signal to task2 that task1 has completed. */
os_evt_set(0x0004, id2);
/* Wait for completion of task2 activity. */
/* 0xFFFF makes it wait without timeout. */
/* 0x0004 represents bit 2. */
os_evt_wait_or(0x0004, 0xFFFF);
/* Wait for 50 ms before restarting task1 activity. */
os_dly_wait(5);
}
}
__task void task2 (void) {
for (;;) {
/* Wait for completion of task1 activity. */
/* 0xFFFF makes it wait without timeout. */
/* 0x0004 represents bit 2. */
os_evt_wait_or(0x0004, 0xFFFF);
/* Wait for 20 ms before starting task2 activity. */
os_dly_wait(2);
/* ... place code for task2 activity here ... */
/* Signal to task1 that task2 has completed. */
os_evt_set(0x0004, id1);
}
}
void main (void) {
/* Start the RTX kernel, and then create and execute task1. */
os_sys_init(task1);
}
-
Finally, to compile the code and link it with the RTX library,
you must select the RTX operating system for the project. From the
main menu, select Project —> Options for Target. Select
the Target tab. Select RTX Kernel for the Operating
system. Build the project to generate the absolute file. You
can run the object file output from the linker either on your
target or on the µVision Simulator.
|
|