http://www.realview.com.cn/down-list.asp?id=326
第一个RTX运用程序
这部分给出了一个非常简单的使用RTX核的应用程序例子。这个应用程序有两个任务, task1和task2,这两个任务必须在一定的时间(如50ms)后重复执行,当task1执行完后暂停20ms,然后继续执行task2。
现在,一步一步构建这个应用程序。
- 将操作代码写入到task1和task2中。 在RVCT中使用C语言的扩展关键字_task声明这两个任务。
void task1 (void) __task { .... place code of task 1 here .... }
void task2 (void) __task { .... place code of task 2 here .... }
- 运行任务之前启动实时运行器,然后,在C程序的main()函数中调用os_sys_init() 函数。
将任务ID作为一个参数传递到os_sys_init函数中,当程序运行时就直接执行task1。
首先执行rask1。在task1执行时调用 os_tsk_create 创建task2。
void task1 (void) __task { os_tsk_create (task2, 0); .... place code of task 1 here .... }
void task2 (void) __task { .... place code of task 2 here .... }
void main (void) { os_sys_init (task1); }
- 将操作写入到死循环中,不可预知地重复执行两个程序。
首先,系统函数 os_dly_wait() 任务暂停几个系统时间间隔。RTX核通过对ARM处理器片上定时器的编程来实现系统定时器,默认情况下,系统间隔10ms,使用0号定时器,不过这可以调整。
系统函数 os_evt_wait_or 的作用是用于让task1等待task2完成,而 os_evt_set作用是传送信号给task2。
这个例子中使用的是3号事件标志。task2必须在task1完成20ms之后才能启动。同样的函数可以在task2等待task1以及传送信号给task1时使用。以下列举了运行RTX例程所有的要求:
/* 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. */ void task1 (void) __task; void task2 (void) __task; void task1 (void) __task { /* 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 compelted */ 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); } } void task2 (void) __task { 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 task1 has compelted */ os_evt_set(0x0004, id1); } } void main (void) { /* Start the RTX kernel, and then create and execute task1. */ os_sys_init(task1); } - 在工程中,选择Options for Target —> Target —> Operating System —> RTX Kernel,编译这个应用程序,链接RTX函数库,编译得到文件可以在目标板或µVision 3的软件仿真器中运行。