Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1024487
  • 博文数量: 238
  • 博客积分: 2842
  • 博客等级: 少校
  • 技术积分: 2765
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-16 00:20
个人简介

stdlf

文章分类

全部博文(238)

文章存档

2013年(6)

2012年(13)

2011年(82)

2010年(89)

2009年(48)

我的朋友

分类:

2009-09-03 22:34:18

uC/OS-II源码分析(二)
2008年08月03日 星期日 09:18 P.M.

2)使用消息邮箱


#define           TASK_STK_SIZE      512               
#define           TASK_START_ID        0                 /* 任务ID*/
#define           TASK_CLK_ID          1
#define           TASK_1_ID            2
#define           TASK_2_ID            3
#define           TASK_3_ID            4
#define           TASK_4_ID            5
#define           TASK_5_ID            6

#define           TASK_START_PRIO     10                 /* 任务优先级*/
#define           TASK_CLK_PRIO       11
#define           TASK_1_PRIO         12
#define           TASK_2_PRIO         13
#define           TASK_3_PRIO         14
#define           TASK_4_PRIO         15
#define           TASK_5_PRIO         16
OS_STK         TaskStartStk[TASK_STK_SIZE];            
OS_STK         TaskClkStk[TASK_STK_SIZE];             
OS_STK         Task1Stk[TASK_STK_SIZE];                
OS_STK         Task2Stk[TASK_STK_SIZE];                
OS_STK         Task3Stk[TASK_STK_SIZE];               
OS_STK         Task4Stk[TASK_STK_SIZE];              
OS_STK         Task5Stk[TASK_STK_SIZE];               
OS_EVENT     
*AckMbox;                                /* 任务和使用的消息邮箱*/
OS_EVENT     
*TxMbox;

void main (void)
{
     OS_STK
*ptos;
     OS_STK
*pbos;
     INT32U   size;
     PC_DispClrScr(DISP_FGND_WHITE);                       
     OSInit();                                            
     PC_DOSSaveReturn();                                  
     PC_VectSet(uCOS, OSCtxSw);                            
     PC_ElapsedInit();                                     
     ptos        
= &TaskStartStk[TASK_STK_SIZE - 1];        
     pbos        
= &TaskStartStk[0];
     size        
= TASK_STK_SIZE;
     OSTaskStkInit_FPE_x86(
&ptos, &pbos, &size);            
     OSTaskCreateExt(TaskStart,
         (
void *)0,
         ptos,
         TASK_START_PRIO,
         TASK_START_ID,
         pbos,
         size,
         (
void *)0,
         OS_TASK_OPT_STK_CHK
| OS_TASK_OPT_STK_CLR);
     OSStart();                                             
}
void   TaskStart (void *pdata)
{
#if OS_CRITICAL_METHOD == 3                               
     OS_CPU_SR   cpu_sr;
#endif
     INT16S      key;
     pdata
= pdata;                                       
     TaskStartDispInit();                                
     OS_ENTER_CRITICAL();                                
     PC_VectSet(
0x08, OSTickISR);
     PC_SetTickRate(OS_TICKS_PER_SEC);                    
     OS_EXIT_CRITICAL();
     OSStatInit();                                          
     AckMbox
= OSMboxCreate((void *)0);                     /* 创建两个消息邮箱*/
     TxMbox  
= OSMboxCreate((void *)0);
     TaskStartCreateTasks();                               
    
for (;;) {
         TaskStartDisp();                                 

        
if (PC_GetKey(&key)) {                           
            
if (key == 0x1B) {                            
                 PC_DOSReturn();                           
             }
         }
         OSCtxSwCtr
= 0;                                    
         OSTimeDly(OS_TICKS_PER_SEC);                      
     }
}
void   Task1 (void *pdata)
{
     INT8U        err;
     OS_STK_DATA data;                       
/* 任务堆栈数据*/
     INT16U       time;                      
     INT8U        i;
    
char         s[80];
     pdata
= pdata;
    
for (;;) {
        
for (i = 0; i < 7; i++) {
             PC_ElapsedStart();
             err  
= OSTaskStkChk(TASK_START_PRIO + i, &data);//执行堆栈检查
             time = PC_ElapsedStop();
            
if (err == OS_NO_ERR) {
                 sprintf(s,
"%4ld         %4ld         %4ld         %6d",
                     data.OSFree
+ data.OSUsed,
                     data.OSFree,
                     data.OSUsed,
                     time);
                 PC_DispStr(
19, 12 + i, s, DISP_FGND_BLACK + DISP_BGND_LIGHT_GRAY);
             }
         }
         OSTimeDlyHMSM(
0, 0, 0, 100);                       /* 挂起mS*/
     }
}
void   Task4 (void *data)
{
    
char    txmsg;
     INT8U   err;
     data  
= data;
     txmsg
= 'A';
    
for (;;) {
         OSMboxPost(TxMbox, (
void *)&txmsg);      /* 发消息给Task #5*/
         OSMboxPend(AckMbox,
0, &err);            /* 等待Task #5的应答消息*/
         txmsg
++;                                 /*下一个要发的消息数据*/
        
if (txmsg == 'Z') {
             txmsg
= 'A';                         /* 循环发送A-Z*/
         }
     }
}
void   Task5 (void *data)
{
    
char  *rxmsg;
     INT8U   err;
     data
= data;
    
for (;;) {
         rxmsg
= (char *)OSMboxPend(TxMbox, 0, &err); /* 等待来自Task #4的数据*/
         PC_DispChar(
70, 18, *rxmsg, DISP_FGND_YELLOW + DISP_BGND_BLUE);
         OSTimeDlyHMSM(
0, 0, 1, 0); /* 挂起秒,让给其他任务运行*/
         OSMboxPost(AckMbox, (
void *)1); /*发送接收到数据的应答消息*/
     }
}
阅读(1173) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~