|
今天用 uCOS v2.84在Win32的port版本测试了如下:
- #include "includes.h"
- #define TASK_STK_SIZE 1024
- OS_STK _Stk0[TASK_STK_SIZE];
- OS_STK _Stk1[TASK_STK_SIZE];
- OS_STK _Stk2[TASK_STK_SIZE];
- void Initialzer(void *pdata);
- void Task_prio_1(void *pdata);
- void Task_prio_2(void *pdata);
- int main(void)
- {
- printf(" - uCOS-II V%4.2f Port Win,test suite\n", ((FP32)OSVersion())/100);
- OSInit();
- OSTaskCreate(Initialzer,(void *)0,&_Stk0[TASK_STK_SIZE],3);
- OSStart();
- printf(" -- never reach here --\n");
- return 0;
- }
- void Initialzer(void *pdata)
- {
- printf("Initialzer: ***** Now Begin Initialze *****\n");
- printf("Initialzer: ***** Now is before the Creation of Task1 *****\n");
- OSTaskCreate(Task_prio_1, (void *)0, &_Stk1[TASK_STK_SIZE], 1);
-
- printf("Initialzer: ***** Now is before the Creation of Task1 *****\n");
- OSTaskCreate(Task_prio_2, (void *)0, &_Stk2[TASK_STK_SIZE], 2);
-
- printf("Initialzer: ***** Now Initialze Finished *****\n");
- OSTaskSuspend(OS_PRIO_SELF);
-
- printf("Initialzer: -- this mesage never occur ! -- \n");
- }
- void DetectAndQuit(INT8U prio)
- {
- if (_kbhit())
- {
- if(prio!=1)OSTaskDel(1);
- if(prio!=2)OSTaskDel(2);
- if(prio!=3)OSTaskDel(3);
- exit(0);
- }
- }
- void myDelay()
- {
- INT32U i=0;
- while(i<100000000){
- //编译器要禁用优化,或启用调试模式
- i+=1;
- }
- }
- void Task_prio_1(void *pdata)
- {
- INT32U loopCnt=0;
- //测试主要是这里,1:用自己的延时函数避免printf过于频繁
- // 0:用OS的延时函数,这样任务调度正常
- #define TEST_MY_DELAY 1
- while (1)
- {
- printf("Task_prio_1: ***** New loop %d for Working Task 1 *****\n",loopCnt++);
- DetectAndQuit(1);
- #if TEST_MY_DELAY
- myDelay();
- #else
- OSTimeDly(1);
- #endif
- }
- }
- void Task_prio_2(void *pdata)
- {
- INT32U loopCnt=0;
- while (1)
- {
- printf("Task_prio_2: ***** New loop %d for Working Task 2 *****\n",loopCnt++);
- DetectAndQuit(2);
- OSTimeDly(1);
- }
- }
复制代码
配置TEST_MY_DELAY宏为1
使用VC 6.0 编译后,输出只有Task_prio_1,因为OS调度任务总是选择prio最高的,
而最高的任务(Task_prio_1)总是在运行,且丝毫不让OS暂停自己,OS也没办法,就算在
中断ISR中调用OSIntExit()的检测任务调度也没用。
当采用配置TEST_MY_DELAY宏为0,
在每个循环中调用了OSTimeDly(),这样休眠自己,让OS有机会运行其他的任务。
我想这就是"实时"的需要吧,用户必须设置好任务的Prio,
并且最高优先级的任务必须根据逻辑,在相关的地方调用***Pend(),或***Dly(),让
OS有机会运行其他低优先级的任务,否则就成了单任务的DOS了。
|
|