我创建了4个任务:优先级依次递减 任务1输出4个任务所用堆栈大小,只能被任务4唤起; 任务2用一个信号量控制任务3使之延时1秒; 任务3利用邮箱给任务4传送消息并等待任务4回信; 任务4收到任务3的消息输出到串口并给任务3回信,唤起任务1。
不加任务2和信号量时,程序是好用的,加上之后串口没有输出了。 请问操作系统是怎么调度的?非常感谢!!! 程序如下:
#define TaskStkLengh 500 OS_STK Task1Stk [TaskStkLengh]; OS_STK Task2Stk [TaskStkLengh]; OS_STK Task3Stk [TaskStkLengh]; OS_STK Task4Stk [TaskStkLengh]; void Task1(void *pdata); void Task2(void *pdata); void Task3(void *pdata); void Task4(void *pdata); OS_EVENT *ackmbox; OS_EVENT *txmbox; OS_EVENT *key;
void UART0_Init(void) { uint16 Fdiv; U0LCR = 0x83; Fdiv = (Fpclk / 16) /115200; U0DLM = Fdiv / 256; U0DLL = Fdiv % 256; U0LCR = 0x03; } void UART0_SendByte(uint8 data) { U0THR = data; while( (U0LSR&0x40)==0 ); } void UART0_SendStr(uint8 *str) { while(1) { if( *str == '\0' ) break; UART0_SendByte(*str++); } }
int main (void) { OSInit (); OSTaskCreateExt (Task1,(void *)0,&Task1Stk[TaskStkLengh-1],3,1,&Task1Stk[0],TaskStkLengh,(void *)0,OS_TASK_OPT_STK_CHK); OSStart (); return 0; } void Task1(void *pdata) // 优先级3 { OS_STK_DATA data; INT8U i; char s[10]; pdata = pdata; TargetInit (); PINSEL0 = 0x00000005; UART0_Init (); OSTaskCreateExt (Task2,(void *)0,&Task2Stk[TaskStkLengh-1],4,2,&Task2Stk[0],TaskStkLengh,(void *)0,OS_TASK_OPT_STK_CHK); OSTaskCreateExt (Task3,(void *)0,&Task3Stk[TaskStkLengh-1],5,3,&Task3Stk[0],TaskStkLengh,(void *)0,OS_TASK_OPT_STK_CHK); OSTaskCreateExt (Task4,(void *)0,&Task4Stk[TaskStkLengh-1],6,4,&Task4Stk[0],TaskStkLengh,(void *)0,OS_TASK_OPT_STK_CHK); while(1) { OSTaskSuspend(OS_PRIO_SELF); // 无条件挂起任务本身 for(i=3;i<7;i++) { OSTaskStkChk(i,&data); sprintf(s,"%6d,",data.OSUsed); UART0_SendStr((uint8 *)s); } } } void Task2 (void *pdata) // 优先级4 { key = OSSemCreate(1); //创建一个信号量 while(1) { OSTimeDlyHMSM(0,0,1,0); //延时1秒 OSSemPost(key); //释放信号量 } } void Task3 (void *pdata) // 优先级5 { uint8 txmsg = 'A'; INT8U err; pdata = pdata; ackmbox = OSMboxCreate((void*)0); txmbox = OSMboxCreate((void*)0); while (1) { OSSemPend(key,0,&err); //等待信号量 OSMboxPost(txmbox,(void *)&txmsg); OSMboxPend(ackmbox,0,&err); if(err==OS_NO_ERR) { txmsg++; if(txmsg == 'Z') txmsg = 'A'; } } } void Task4(void *pdata) // 优先级6 { uint8 *rcmsg; INT8U err; pdata = pdata; while (1) { rcmsg = (uint8*)OSMboxPend(txmbox,0,&err); if(err==OS_NO_ERR) { UART0_SendStr(rcmsg); OSTaskResume(3); // 唤起任务Task1() OSMboxPost(ackmbox,(void*)1); } } }
|