这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 嵌入式开发 » 软件与操作系统 » 极速梦想的学习日志

共16条 1/2 1 2 跳转至

极速梦想的学习日志

菜鸟
2013-07-10 23:01:50     打赏

                                        学习进程贴

        组名:极速梦想


AT91SAM3S板学习进度表

日            期                标                                          题                         链接               备注
2013/5/27               申请AT91SAM3S开发板
                    申请贴

2013/5/31                 什么是uTenux
                       跳转
        准备考试
2013/6/24                   AT91SAM3S电路和初步认识
                        跳转

2013/6/26                   LED和LCD例子的运行
                        跳转

2013/7/10                     01.task程序的分析
                        跳转

2013/7/11                         02.semaphore程序的分析
                        2楼

2013/7/11                     03.eventflag程序的分析                         5楼
2013/7/12                     04.mailbox程序分析
                        7楼

2013/7/12                     05.mutex程序的分析
                        8楼

2013/7/13                     06.messbuf程序的分析
                        9楼

2013/7/14                     07.rendport程序的分析
                        11楼

2013/7/14                     08.mempool程序的分析
                        12楼

2013/7/22                     点亮第一个LED灯
                        14楼






























关键词: 极速     梦想     学习     日志    

菜鸟
2013-07-11 12:48:49     打赏
2楼

                                                                            02.semaphore

 
#include "SemSample.h"


void SemSampleTaskA(W stacd,VP exinf);
void SemSampleTaskB(W stacd,VP exinf);
void SemSamplePutCnt(void);
void SemSamplePutErcd(ER ercd);
UW ltostr(UW Val,B *pString,UB Base,UW Length);


static ID TaskID_A;
static ID TaskID_B;
static ID semid;


ER SemSample(void)
{
    ER ercd = E_OK;
    T_CTSK ctsk;
    T_CSEM csem;     //创建一个T_CSEM型结构体;


    tm_putstring((UB*)"Semaphore sample create Task A;\n");   /*创建任务A*/
    ctsk.exinf = (VP)NULL;
    ctsk.tskatr = TA_HLNG|TA_RNG0;
    ctsk.task = SemSampleTaskA;
    ctsk.itskpri = 24;
    ctsk.stksz = 512*4;
    TaskID_A = tk_cre_tsk(&ctsk);
    if(TaskID_A < E_OK) {
        ercd=TaskID_A;
        return ercd;
    }
    
    tm_putstring((UB*)"Semaphore sample create  Task B;\n");   /*创建任务B*/
    ctsk.exinf = (VP)NULL;
    ctsk.tskatr = TA_HLNG|TA_RNG0;
    ctsk.task = SemSampleTaskB;
    ctsk.itskpri = 26;
    ctsk.stksz = 512*4;
    TaskID_B = tk_cre_tsk(&ctsk);
    if(TaskID_B < E_OK) {
        ercd=TaskID_B;
        return ercd;
    }
    
    tm_putstring((UB*)"Semaphore sample create a semaphore;\n");          /*创建一个信号量*/

    csem.exinf = (VP)NULL;     //扩展信息为空;
    csem.sematr = TA_TFIFO | TA_FIRST;     //属性为先进先出,先来先服务;
    csem.isemcnt = 10;       //信号量初值为10;
    csem.maxsem = 100;   //信号量最大值为100;
    semid = tk_cre_sem(&csem);  //创建一个信号量,ID返回给semid
    if (semid < E_OK){
        ercd = semid;
        return ercd;
    }
    tm_putstring((UB*)"Semaphore sample create a semaphore successfully;\n");
    SemSamplePutCnt();    //以十进制显示当前信号量数量;


    tm_putstring((UB*)"Semaphore sample start Task B;\n");
    tk_sta_tsk(TaskID_B,0);       //启动任务B;


    return TRUE;

 
 
void SemSampleTaskA(W stacd,VP exinf)   //任务A的服务函数;
{
    ER ercd;


    tm_putstring((UB*)"Task A is starting;\n");


    for(;;){
        tm_putstring((UB*)"Task A is waiting semaphore;\n");


        ercd = tk_wai_sem(semid, 1, -1);       //等待信号量, 等待资源量为1, 始终处于等待状态,直到等到资源为止,
        SemSamplePutCnt();      //以十进制显示当前信号量数量;  
        if (E_OK == ercd){
            tm_putstring((UB*)"Task A get semaphore successfully;\n");
        }else{
            tm_putstring((UB*)"Task A can't get semaphore;\n");
            SemSamplePutErcd(ercd);
        }


        tm_putstring((UB*)"Task A release semaphore;\n");
        ercd = tk_sig_sem(semid, 1);     //释放资源, 释放数量为1 ;
        SemSamplePutCnt();    //以十进制显示当前信号量数量;
        if (E_OK == ercd){
            tm_putstring((UB*)"Task A release semaphore successfully;\n");
        } else {
            tm_putstring((UB*)"Task A can't release semaphore;\n");
        }


        tm_putstring((UB*)"Task A enter sleep status;\n");
        tk_slp_tsk(-1);     //进入睡眠状态;
    }
}

 
void SemSampleTaskB(W stacd,VP exinf)     //任务B的服务函数;
{
    ER ercd;
    B c;


    ercd = tk_sta_tsk(TaskID_A,0);       //启动任务A;
    if (E_OK == ercd){
        tm_putstring((UB*)"Task B start Task A successfully;\n");
    } else {
        tm_putstring((UB*)"Task B can't start Task A;\n");
    }


    for(;;){
        tm_putstring((UB*)"Task B is waiting semaphore;\n");


        ercd = tk_wai_sem(semid, 1, -1);             //等待信号量, 等待资源量为1, 始终处于等待状态,直到等到资源为止,
        SemSamplePutCnt();    //以十进制显示当前信号量数量;
        if (E_OK == ercd){
            tm_putstring((UB*)"Task B get semaphore successfully;\n");
        } else {
            tm_putstring((UB*)"Task B can't get semaphore;\n");
        SemSamplePutErcd(ercd);
        }


        tm_putstring((UB*)"Input Command(e=exit):\n");
        c = tm_getchar(0);
        if('e' == c) {
            tm_putstring((UB*)"Exit from all task;\n");
            break;
        } else {
            tm_putstring((UB*)"Task B release semaphore;\n");
            ercd = tk_sig_sem(semid, 1);        //释放资源, 释放数量为1;
            SemSamplePutCnt();      //以十进制显示当前信号量数量;
            if (E_OK == ercd){
        tm_putstring((UB*)"Task B release semaphore successfully;\n");
        } else {
            tm_putstring((UB*)"Task B can't release semaphore;\n");
        }


            tm_putstring((UB*)"Task B wake up Task A;\n");
            tk_wup_tsk(TaskID_A);    //唤醒任务A
        }
    }


    tm_putstring((UB*)"Task B delete semaphore;\n");
    ercd = tk_del_sem(semid);   //删除信号量;
    if (ercd == E_OK){
        tm_putstring((UB*)"Task B delete semaphore successfully;\n");
    } else {
        tm_putstring((UB*)"Task B can't delete semaphore;\n");
    }


    tm_putstring((UB*)"Task B will terminate&delete task A;\n");
    tk_ter_tsk(TaskID_A);            /*暂停并且删除任务A*/
    tk_del_tsk(TaskID_A);
    tm_putstring((UB*)"Task B will terminate&delete self;\n");
    tk_exd_tsk();      //任务B退出; 
}
 
void SemSamplePutErcd(ER ercd)   //返回错误类型
{
    B ercds[10];


    tm_putstring((UB*)"Error code is ");
    switch(ercd){
        case E_OK:
            tm_putstring((UB*)"0:E_OK");
            break;
        case E_SYS:
            tm_putstring((UB*)"-5:E_SYS");
            break;
        case E_NOSPT:
            tm_putstring((UB*)"-9:E_NOSPT");
            break;
        case E_RSATR:
            tm_putstring((UB*)"-11:E_LIMIT");
            break;
        case E_PAR:
            tm_putstring((UB*)"-17:E_PAR");
            break;
        case E_ID:
            tm_putstring((UB*)"-18:E_ID");
            break;
        case E_NOMEM:
            tm_putstring((UB*)"-33:E_NOMEM");
            break;
        case E_LIMIT:
            tm_putstring((UB*)"-34:E_LIMIT");
            break;
        case E_NOEXS:
            tm_putstring((UB*)"-42:E_NOEXS");
            break;
        case E_TMOUT:
            tm_putstring((UB*)"-50:E_TMOUT");
            break;
        default:
            if(ercd>0){
                tm_putstring((UB*)">0,UNKNOWN:");
                ltostr((UW)ercd,ercds,16,10);
                tm_putstring((UB*)ercds);
            } else{
            if(ercd<-67){
                    tm_putstring((UB*)"<-67,UNKNOWN");
                } else {
                    tm_putstring((UB*)"0,-67,UNKNOWN");
                }
            }
            break;
    }
  tm_putstring((UB*)"\n");
}

 
void SemSamplePutCnt(void)        //显示当前信号量;
{
    B semcnt[10];
    T_RSEM rsem;


    tm_putstring((UB*)"Now semaphore count number is ");
    tk_ref_sem(semid, &rsem);   //查询信号量semid的各种状态;
    ltostr(rsem.semcnt,semcnt,10,10);
    tm_putstring((UB*)semcnt);
    tm_putstring((UB*)"\n");
}


 
UW ltostr(UW Val,B *pString,UB Base,UW Length)   //转换为Base进制,然后用指针显示;
{
    B  c, *pInit;
    UB  i,j;
    UW Len,SwapLen;


    *pString='\0';
    pInit=pString;
    if(Val == 0){
        *pString++='0';
        *pString='\0';
    }
    switch(Base){
        /* Base is 10,convert to 10 base string */
        case 10:
            for(;Val>0;){                    /*取十进制的个位,十位,百位,。。。*/
                i=Val%Base;
                Val=Val/Base;
                *pString++=(B)((UB)'0'+i); 
            }
            *pString='\0';
            break;
        /* Base is 16,convert to 16 base string */
        case 16:
            for(;Val>0;){                 /*取十六进制的低位到高位*/
                i=Val%Base;
                Val=Val/Base;
                if(i<=9){
                    *pString++=(B)((UB)'0'+i);
                } else {
                    *pString++=(B)((UB)'A'+i-10);
                }
            }
            *pString='\0';   //最后位置空;
            break;
        default:
            break;
    }


    Len=strlen(pInit);
    SwapLen=Len;
    /* fill '\0' from string end to right */
    if(Length>Len) {
        for(i=Len;i<Length;i++){
            *(pInit+i)='\0';      //没有置数的位,置空;
        }
        *(pInit+Length)='\0';      //最后位置空;
    }


 
    if(SwapLen>0){                                    /*高位和低位互换*/
        for(i=0,j=SwapLen - 1;i<j;i++,j--){
            c=*(pInit+i);
            *(pInit+i)=*(pInit+j);
            *(pInit+j)=c;
        }
    }
    return SwapLen;
}

输出结果:

Semaphore sample create Task A;
Semaphore sample create  Task B;
Semaphore sample create a semaphore;
Semaphore sample create a semaphore successfully;

Now semaphore count number is 10

Semaphore sample start Task B;

Task A is starting;

Task A is waiting semaphore;

Now semaphore count number is 9

Task A get semaphore successfully;

Task A release semaphore;

Now semaphore count number is 10

Task A release semaphore successfully;

Task A enter sleep status;

Task B start Task A successfully;

Task B is waiting semaphore;

Now semaphore count number is 9

Task B get semaphore successfully;

Input Command(e=exit):

若输入e:

Exit from all task;

Task B delete semaphore;

Task B delete semaphore successfully;  

Task B will terminate&delete task A;

Task B will terminate&delete self;


若输入任意其他:

Task B release semaphore;

Now semaphore count number is 10

Task B release semaphore successfully;

Task B wake up Task A;

Task A is waiting semaphore;

Now semaphore count number is 9


Task A get semaphore successfully;

Task A release semaphore;

Now semaphore count number is 10

Task A release semaphore successfully;

Task A enter sleep status;

Task B is waiting semaphore;

Now semaphore count number is 9

Task B get semaphore successfully;

Input Command(e=exit):


如此这样循环下去;

先创建任务A,B, 以及信号量, 先启动任务B,然后在任务B中启动任务A, 由于任务A的


优先级比B高,则转进任务A的服务函数,A进入睡眠状态后,返回执行任务B;





高工
2013-07-11 13:54:50     打赏
3楼
继续加油!

院士
2013-07-11 14:28:27     打赏
4楼

楼主 好多的源代码啊~~

画个流程图呗 清晰 明了,嘿嘿


菜鸟
2013-07-11 21:47:30     打赏
5楼
                                                                03.eventflag 程序分析
                                                               

#include "EventflagSample.h"


void EventflagSampleTaskA(W stacd,VP exinf);
void EventflagSampleTaskB(W stacd,VP exinf);
void EventflagSamplePutErcd(ER ercd);
void EventflagSamplePutFlg(void);
UW ltostr(UW Val,B *pString,UB Base,UW Length);
void Delay(void);


static ID TaskID_A;
static ID TaskID_B;
static ID FlgID;
 
 
EXPORT ER EventflagSample( void )
{
    T_CFLG cflg;     //创建一个T_CFLG型的结构体cflg;
    T_CTSK ctskA;
    T_CTSK ctskB;

    cflg.iflgptn = 1;    //初始事件标志为1;
    cflg.flgatr  = TA_TPRI|TA_WMUL;      //标志属性,按优先级顺序排队,允许等待多个任务;
    FlgID        = tk_cre_flg(&cflg);   //创建事件标志,返回ID给 FlgID 
    if(FlgID>0) {
        tm_putstring((UB*)"Eventflag sample create step 1-Create T eventflag, ID is ");   //输出:Eventflag sample create step 1-Create T eventflag, ID is
        tm_putchar(FlgID+48);     //初始事件标志为1, 在ASCII中,49为1;
        tm_putstring((UB*)" !\n");
    } else {
        tm_putstring((UB*)"Error code is ");
        tm_putchar(FlgID+48);
        tm_putstring((UB*)" !\n");
    }


    ctskA.exinf   = (VP)NULL;                       /*创建任务A*/
    ctskA.tskatr  = TA_HLNG | TA_RNG0;
    ctskA.task    = EventflagSampleTaskA;
    ctskA.itskpri = 10;
    ctskA.stksz   = 512*4;
    TaskID_A      = tk_cre_tsk(&ctskA);
    if(TaskID_A>0) {
        tm_putstring((UB*)"Eventflag sample create step 2-Create Task A, ID is ");      /*l输出:Eventflag sample create step 2-Create Task A, ID is 2!*/
        tm_putchar(TaskID_A+48);
        tm_putstring((UB*)" !\n");
    } else {
        tm_putstring((UB*)"Error code is ");
        tm_putchar(TaskID_A+48);
        tm_putstring((UB*)" !\n");
    }

    ctskB.exinf   = (VP)NULL;               /*创建任务B*/
    ctskB.tskatr  = TA_HLNG | TA_RNG0;
    ctskB.task    = EventflagSampleTaskB;
    ctskB.itskpri = 10;
    ctskB.stksz   = 512*4;
    TaskID_B      = tk_cre_tsk(&ctskB);
    if(TaskID_B>0) {
        tm_putstring((UB*)"Eventflag sample create step 3-Create Task B, ID is ");   /*输出:Eventflag sample create step 3-Create Task B, ID is 3!*/
        tm_putchar(TaskID_B+48);
        tm_putstring((UB*)" !\n");
    } else {
        tm_putstring((UB*)"Error code is ");
        tm_putchar(TaskID_B+48);
        tm_putstring((UB*)" !\n");
    }


    tm_putstring((UB*)"Eventflag sample create step 4-Start Task B;\n\n");    //输出:Eventflag sample create step 4-Start Task B;
    tk_sta_tsk(TaskID_B,0);

    return E_OK;
}
 
void EventflagSampleTaskA(W stacd,VP exinf)     //任务A服务函数;
{
ER ercd;
    UINT i,flgptn;
 
    for(i=0;;i++) {
        tk_wai_flg(FlgID,1,TWF_ANDW ,&flgptn,-1);     //等待事件标志  等待事件标志模式为1, 与等待条件,返回当前事件标志的模式; 
        tm_putstring((UB*)"Task A is now get eventflag 0x00000001 ");   //输出: Task A is now get eventflag 0x00000001
        tm_putchar(i+48+1);                            //输出:1 times
        tm_putstring((UB*)" times\n");
        tk_clr_flg(FlgID,0xFFFE);           //清除事件标志
        tm_putstring((UB*)"Task A is now clr eventflag 0x00000001\n");   //输出:Task A is now clr eventflag 0x00000001
        Delay();
        tm_putstring((UB*)"======================================\n");   //输出:======================================
        Delay();
        tm_putstring((UB*)"Task A  will  set eventflag 0x00000002\n");   //输出:Task A  will  set eventflag 0x00000002
        ercd=tk_set_flg(FlgID,2);     //设置事件标志;
if(E_OK == ercd){
tm_putstring((UB*)"Task A  now  set  eventflag 0x00000002\n");
} else {
tm_putstring((UB*)"Task A cannot set eventflag 0x00000002\n");
EventflagSamplePutErcd(ercd);
}
EventflagSamplePutFlg();
        tm_putstring((UB*)"\n");
    }
}
 
 
void EventflagSampleTaskB(W stacd,VP exinf)   //任务B服务函数
{
B c;
ER ercd;
    UINT i,flgptn;


tm_putstring((UB*)"Task B will start task A;\n");   //Task B will start task A;
tk_sta_tsk(TaskID_A,0);      //启动任务A


    for(i=0;;i++) {
    tm_putstring((UB*)"Task B is running,input Command(e=exit):\n");   //输出:Task B is running,input Command(e=exit):
    c = tm_getchar(0);
    if(c=='e') {
    break;
    }
        tk_wai_flg(FlgID,2,TWF_ANDW ,&flgptn,-1);        //等待事件标志  等待事件标志模式为2, 与等待条件,返回当前事件标志的模式
        tm_putstring((UB*)"Task B is now get eventflag 0x00000002 ");   //输出:Task B is now get eventflag 0x00000002
        tm_putchar(i+48+1);
        tm_putstring((UB*)" times\n");
        tk_clr_flg(FlgID,0xFFFD);
        tm_putstring((UB*)"Task B is now clr eventflag 0x00000002\n");   //输出:Task B is now clr eventflag 0x00000002
        Delay();
        tm_putstring((UB*)"======================================\n");   //输出:======================================
        Delay();
        tm_putstring((UB*)"Task B  will  set eventflag 0x00000001\n");   //输出:Task B  will  set eventflag 0x00000001
        ercd=tk_set_flg(FlgID,1);        //设置事件标志;
if(E_OK == ercd){
tm_putstring((UB*)"Task B   now  set eventflag 0x00000001\n");    //输出:Task B   now  set eventflag 0x00000001
} else {
tm_putstring((UB*)"Task B cannot set eventflag 0x00000001\n");
EventflagSamplePutErcd(ercd);     //返回错误类型 ;
}
EventflagSamplePutFlg();        //返回当前事件标志  
        tm_putstring((UB*)"\n");
    }


tm_putstring((UB*)"Task B will delete flag;\n");        //输出:Task B will delete flag;

    tk_del_flg(FlgID);         //删除事件标志;

tm_putstring((UB*)"Task B will terminate&delete task A;\n");   //输出:Task B will terminate&delete task A;

    tk_ter_tsk(TaskID_A);          /*暂停并删除任务A*/
    tk_del_tsk(TaskID_A);
tm_putstring((UB*)"Task B will terminate&delete self;\n");  //输出:Task B will terminate&delete self;
    tk_exd_tsk();         //任务B退出; 
}

 
void EventflagSamplePutErcd(ER ercd)        //输出返回值类型
{
    B ercds[10];


  tm_putstring((UB*)"Error code is ");
    switch(ercd){
        case E_OK:
        tm_putstring((UB*)"0:E_OK");
        break;
        case E_SYS:
        tm_putstring((UB*)"-5:E_SYS");
        break;
        case E_NOSPT:
        tm_putstring((UB*)"-9:E_NOSPT");
        break;
        case E_RSATR:
        tm_putstring((UB*)"-11:E_LIMIT");
        break;
        case E_PAR:
        tm_putstring((UB*)"-17:E_PAR");
        break;
        case E_ID:
        tm_putstring((UB*)"-18:E_ID");
        break;
        case E_NOMEM:
        tm_putstring((UB*)"-33:E_NOMEM");
        break;
        case E_LIMIT:
        tm_putstring((UB*)"-34:E_LIMIT");
        break;
        case E_NOEXS:
        tm_putstring((UB*)"-42:E_NOEXS");
        break;
        case E_TMOUT:
        tm_putstring((UB*)"-50:E_TMOUT");
        break;
        default:
        if(ercd>0){
           tm_putstring((UB*)">0,UNKNOWN:");
           ltostr((UW)ercd,ercds,16,10);
           tm_putstring((UB*)ercds);
        } else{
        if(ercd<-67){
        tm_putstring((UB*)"<-67,UNKNOWN");
        } else {
        tm_putstring((UB*)"0,-67,UNKNOWN");
        }
        }
        break;
    }
  tm_putstring((UB*)"\n");
}

 
void EventflagSamplePutFlg(void)   //输出事件标志模式; 
{
    B flgptn[10];
    T_RFLG rflg;


tm_putstring((UB*)"Now Flag pattern is 0x0000000");
    tk_ref_flg(FlgID, &rflg);         //查询事件标志;
  ltostr(rflg.flgptn,flgptn,16,10);     //以十六进制输出
  tm_putstring((UB*)flgptn);
  tm_putstring((UB*)"\n");
}
 
UW ltostr(UW Val,B *pString,UB Base,UW Length)
{
B  c, *pInit;
UB  i,j;
UW Len,SwapLen;


*pString='\0';
pInit=pString;
if(Val==0) {
   *pString++='0';
   *pString='\0';                        /*added by cheny to deal with the case that input number is zero 2009-04-15*/
}
switch(Base) {
case 10:                    /* Base is 10,convert to 10 base string */
   for(;Val>0;) {
       i=Val%Base;
       Val=Val/Base;
       *pString++=(B)((UB)'0'+i);
   }
   *pString='\0';
   break;
case 16:                    /* Base is 16,convert to 16 base string */
   for(;Val>0;) {
       i=Val%Base;
       Val=Val/Base;
       if(i<=9) {
           *pString++=(B)((UB)'0'+i);
       } else {
           *pString++=(B)((UB)'A'+i-10);
       }
   }
   *pString='\0';
   break;
default:
   break;
}


Len=strlen(pInit);
SwapLen=Len;
/*fill '\0' from string end to right */
if(Length>Len) {
for(i=Len;i<Length;i++) {
*(pInit+i)='\0';
}
*(pInit+Length)='\0';
}


/* swap high and low order */
if(SwapLen>0) {
for(i=0,j=SwapLen - 1;i<j;i++,j--) {
   c=*(pInit+i);
   *(pInit+i)=*(pInit+j);
   *(pInit+j)=c;
}
}
    return SwapLen;
}

 
void Delay(void)
{
    UINT i=0x1000000;


    for(;i>0;i--);
}

输出结果:

Eventflag sample create step 1-Create T eventflag, ID is 1!

Eventflag sample create step 2-Create Task A, ID is 2!

Eventflag sample create step 3-Create Task B, ID is 3!

Eventflag sample create step 4-Start Task B

Task B will start task A

Task B is running,input Command(e=exit):


若输入的是e:

Task B will delete flag 

Task B will terminate&delete task A

Task B will terminate&delete self


若输入的不是e:

Task A is now get eventflag 0x00000001 1times

"Task A is now clr eventflag 0x00000001

======================================

Task A  will  set eventflag 0x00000002

Task A  now  set  eventflag 0x00000002

Now Flag pattern is 0x00000002


Task B is now get eventflag 0x00000002  1times

Task B is now clr eventflag 0x00000002

======================================

Task B  will  set eventflag 0x00000001

Task B   now  set eventflag 0x00000001

Now Flag pattern is 0x00000002


如果仍然输入不是e:


Task A is now get eventflag 0x00000001 2times

"Task A is now clr eventflag 0x00000001

======================================

Task A  will  set eventflag 0x00000002

Task A  now  set  eventflag 0x00000002

Now Flag pattern is 0x00000002


Task B is now get eventflag 0x00000002  2times

Task B is now clr eventflag 0x00000002

======================================

Task B  will  set eventflag 0x00000001

Task B   now  set eventflag 0x00000001

Now Flag pattern is 0x00000002

 


先启动任务B,启动任务A,然后等待事件标志;执行任务A,然后等待事件标志,返回执行B,然后等待事件标志;依次执行下去;


问题:

    1:任务B启动时,也启动了任务A,两者都需要等待事件标志,优先级也相同,怎么先执行任务A, 后执行任务B呢?

    2:创建任务A,B时,返回的ID为什么分别是2,3呢?


















院士
2013-07-12 11:24:10     打赏
6楼

好多源代码啊~~

偶表示偶没有看完就回帖了


菜鸟
2013-07-12 16:50:18     打赏
7楼
                                             04.mailbox程序分析
#include "MailboxSample.h"


typedef struct u_msg {
    VP  msgque[1];  /* Area for message queue,as same as T_MSG */
    UB  *usrmsg;    /* Area for message pointer */
} U_MSG;


void MbxSampleTaskA(W stacd,VP exinf);
void MbxSampleTaskB(W stacd,VP exinf);
void MbxSamplePutErcd(ER ercd);
UW ltostr(UW Val,B *pString,UB Base,UW Length);


static ID TaskID_A;
static ID TaskID_B;
static ID MbxID_S;
static ID MbxID_R;

 
ER MbxSample( void)
{
    ER ercd=E_OK;
    T_CTSK ctsk;
    T_CMBX cmbx;        //创建一个 T_CMBX型的cmbx;


    tm_putstring((UB*)"Mailbox sample create step 1-Create Task A;\n");   /**创建任务A**/
    ctsk.exinf = (VP)NULL;
    ctsk.tskatr = TA_HLNG|TA_RNG0;
    ctsk.task = MbxSampleTaskA;
    ctsk.itskpri = 20;
    ctsk.stksz = 512*4;
    TaskID_A = tk_cre_tsk(&ctsk);
    if(TaskID_A < E_OK) {
        ercd=TaskID_A;
        return ercd;
    }
    
    tm_putstring((UB*)"Mailbox sample create step 2-Create Task B;\n");   /*创建任务B*/
    ctsk.exinf = (VP)NULL;
    ctsk.tskatr = TA_HLNG|TA_RNG0;
    ctsk.task = MbxSampleTaskB;
    ctsk.itskpri = 24;
    ctsk.stksz = 512*4;
    TaskID_B = tk_cre_tsk(&ctsk);
    if(TaskID_B < E_OK) {
        ercd=TaskID_B;
        return ercd;
    }
    
    tm_putstring((UB*)"Mailbox sample create step 3-Create a send mailbox;\n");    /*创建邮箱*/
    cmbx.exinf = (VP)NULL;          //扩展作息为空;
    cmbx.mbxatr = TA_TFIFO|TA_MFIFO;        //任务按照FIFO顺序排队, 消息也按照FIFO顺序排队;
    MbxID_S = tk_cre_mbx(&cmbx);        //创建一个邮箱,返回值给 MbxID_S,该邮箱定义为发送邮箱;
    if(MbxID_S < E_OK){
        ercd=MbxID_S;
        return ercd;
    }


    tm_putstring((UB*)"Mailbox sample create step 4-Create a return mailbox;\n");     /*创建邮箱*/
    cmbx.exinf = (VP)NULL;
    cmbx.mbxatr = TA_TFIFO|TA_MFIFO;       //任务按照FIFO顺序排队, 消息也按照FIFO顺序排队;
    MbxID_R = tk_cre_mbx(&cmbx);           //创建一个邮箱,返回值给 MbxID_R,该邮箱定义为接收邮箱;
    if(MbxID_R < E_OK){
      ercd=MbxID_R;
      return ercd;
    }


    tm_putstring((UB*)"Mailbox sample create step 5-Start Task A;\n");  //输出:Mailbox sample create step 5-Start Task A;
    ercd = tk_sta_tsk(TaskID_A,0);     //启动任务A
    if (E_OK != ercd){
        tm_putstring((UB*)"start Task A failed;\n");
    }
    return TRUE;


 
void MbxSampleTaskA(W stacd,VP exinf)    //任务A服务函数;
{
    B c;
    ER ercd;
    U_MSG *pk_rcvmsg;
    U_MSG sndmsg;


    tm_putstring((UB*)"Mailbox sample create step 6-Start Task B;\n\n");   //输出:Mailbox sample create step 6-Start Task B;
    ercd = tk_sta_tsk(TaskID_B,0);        //启动任务B;
    if (E_OK != ercd){
        tm_putstring((UB*)"Task B is starting failed;\n");
    }


    for(;;){
        tm_putstring((UB*)"Task A is running,Input Command(e=exit):\n");  //输出:Task A is running,Input Command(e=exit):
        c = tm_getchar(0);
        if('e' == c) {
            break;
        }


        sndmsg.usrmsg = "1-2-3,Please Starting up....\n";
        tm_putstring((UB*)"Task A send a message to Task B:");    //输出:Task A send a message to Task B:
        tm_putstring((UB*)sndmsg.usrmsg);          //输出:1-2-3,Please Starting up....
        ercd = tk_snd_mbx(MbxID_S,(T_MSG*)&sndmsg);   //向发送邮箱发送消息;
        if(E_OK == ercd){
            tm_putstring((UB*)"Task A sent a message successfully;\n");    
        }else{
            tm_putstring((UB*)"Task A can't send a message;\n");
        }


        tm_putstring((UB*)"Task A is waiting Task B return message;\n\n");   //输出:Task A is waiting Task B return message;
        ercd = tk_rcv_mbx(MbxID_R,(T_MSG**)&pk_rcvmsg,-1);   //从接收邮箱中接收消息;
        if(E_OK == ercd){
            tm_putstring((UB*)"Task A have received return message: ");  //输出:Task A have received return message:
            tm_putstring((UB*)pk_rcvmsg->usrmsg);
        }else{
            tm_putstring((UB*)"Task A can't receive return message: \n");
            MbxSamplePutErcd(ercd);
        }
    }


    tm_putstring((UB*)"Task A will delete mailbox;\n");    //输出:Task A will delete mailbox;
    tk_del_mbx(MbxID_S);   /* 删除发送邮箱和接收邮箱
    tk_del_mbx(MbxID_R);               */
    tm_putstring((UB*)"Task A will terminate&delete task B;\n");//输出:Task A will terminate&delete task B;
    tk_ter_tsk(TaskID_B);       /*暂停和删除任务B*/
    tk_del_tsk(TaskID_B);
    tm_putstring((UB*)"Task A will terminate&delete self;\n");  //输出:Task A will terminate&delete self;
    tk_exd_tsk();       //退出任务A
}
 
void MbxSampleTaskB(W stacd,VP exinf)
{
    ER ercd;
    U_MSG *pk_rcvmsg;
    U_MSG sndmsg;


    for(;;){
        tm_putstring((UB*)"Task B is waiting a message;\n");      //输出:Task B is waiting a message;
        ercd = tk_rcv_mbx(MbxID_S,(T_MSG**)&pk_rcvmsg,-1);   //从另一个任务的发送邮箱中接收消息;
        if(E_OK == ercd){
            tm_putstring((UB*)"Task B have received a message: ");  //输出:Task B have received a message:
            tm_putstring((UB*)pk_rcvmsg->usrmsg);  //输出:   1-2-3,Please Starting up....
        }else{
            tm_putstring((UB*)"Task B can't receive a message: ");
            MbxSamplePutErcd(ercd);
        }


        sndmsg.usrmsg = "I have got up...\n";
        tm_putstring((UB*)"Task B return a message to Task A:");  //输出:Task B return a message to Task A:
        tm_putstring((UB*)sndmsg.usrmsg);   //输出:I have got up...
        tm_putstring((UB*)"\n");
        ercd = tk_snd_mbx(MbxID_R,(T_MSG*)&sndmsg);  //向另一个任务的接收邮箱发送消息;
        if(E_OK != ercd){
            tm_putstring((UB*)"Task B can't return a message;\n");
        }
    }
}


 
void MbxSamplePutErcd(ER ercd)
{
    B ercds[10];


    tm_putstring((UB*)"Error code is ");
    switch(ercd){
        case E_OK:
            tm_putstring((UB*)"0:E_OK");
            break;
        case E_SYS:
            tm_putstring((UB*)"-5:E_SYS");
            break;
        case E_NOSPT:
            tm_putstring((UB*)"-9:E_NOSPT");
            break;
        case E_RSATR:
            tm_putstring((UB*)"-11:E_LIMIT");
            break;
        case E_PAR:
            tm_putstring((UB*)"-17:E_PAR");
            break;
        case E_ID:
            tm_putstring((UB*)"-18:E_ID");
            break;
        case E_NOMEM:
            tm_putstring((UB*)"-33:E_NOMEM");
            break;
        case E_LIMIT:
            tm_putstring((UB*)"-34:E_LIMIT");
            break;
        case E_NOEXS:
            tm_putstring((UB*)"-42:E_NOEXS");
            break;
        case E_TMOUT:
            tm_putstring((UB*)"-50:E_TMOUT");
            break;
        default:
            if(ercd>0){
                tm_putstring((UB*)">0,UNKNOWN:");
                ltostr((UW)ercd,ercds,16,10);
                tm_putstring((UB*)ercds);
            } else{
                if(ercd<-67){
                    tm_putstring((UB*)"<-67,UNKNOWN");
                } else {
                    tm_putstring((UB*)"0,-67,UNKNOWN");
                }
            }
            break;
    }
    tm_putstring((UB*)"\n");
}

 
UW ltostr(UW Val,B *pString,UB Base,UW Length)
{
    B  c, *pInit;
    UB  i,j;
    UW Len,SwapLen;


    *pString='\0';
    pInit=pString;
    if(Val==0) {
        *pString++='0';
        *pString='\0';
    }
    switch(Base) {
        case 10:                    /* Base is 10,convert to 10 base string */
            for(;Val>0;) {
                i=Val%Base;
                Val=Val/Base;
                *pString++=(B)((UB)'0'+i);
            }
            *pString='\0';
            break;
        case 16:                    /* Base is 16,convert to 16 base string */
            for(;Val>0;) {
                i=Val%Base;
                Val=Val/Base;
                if(i<=9) {
                    *pString++=(B)((UB)'0'+i);
                } else {
                    *pString++=(B)((UB)'A'+i-10);
                }
            }
            *pString='\0';
            break;
        default:
            break;
    }


    Len=strlen(pInit);
    SwapLen=Len;
    /*fill '\0' from string end to right */
    if(Length>Len) {
        for(i=Len;i<Length;i++) {
            *(pInit+i)='\0';
        }
        *(pInit+Length)='\0';
    }


    /* swap high and low order */
    if(SwapLen>0) {
        for(i=0,j=SwapLen - 1;i<j;i++,j--) {
            c=*(pInit+i);
            *(pInit+i)=*(pInit+j);
            *(pInit+j)=c;
        }
    }
    return SwapLen;

}



运行结果:

Mailbox sample create step 1-Create Task A;

Mailbox sample create step 2-Create Task B;

Mailbox sample create step 3-Create a send mailbox;

Mailbox sample create step 4-Create a return mailbox;

Mailbox sample create step 5-Start Task A;

Mailbox sample create step 6-Start Task B;


Task A is running,Input Command(e=exit):

若输入不是e:

Task A send a message to Task B:  1-2-3,Please Starting up....

Task A sent a message successfully;

Task A is waiting Task B return message;


Task B is waiting a message;

Task B have received a message:   1-2-3,Please Starting up....

Task B return a message to Task A: I have got up..

Task A have received return message: I have got up..


若输入e:

Task A will delete mailbox;

Task A will terminate&delete task B;

Task A will terminate&delete self;


创建任务A, B,发送邮箱和接收邮箱; 先启动任务A, 在任务A里面启动任务B, 由于任务A的优先级高于

任务B, 则先执行任务A。在任务A的服务函数里面, 任务A先向发送邮箱里发送消息,则任务B就从这个

发送邮箱接收消息, 相应的, 任务B向接收邮箱里发送消息, 任务A就从接收邮箱里接收任务B发送的消息,

从而实现了两个任务间的通信;


菜鸟
2013-07-12 22:16:22     打赏
8楼
                                                     05.mutex程序分析       
 
#include "MutexSample.h"
 
void MtxSampleTaskA(W stacd,VP exinf);
void MtxSampleTaskB(W stacd,VP exinf);
UW ltostr(UW Val,B *pString,UB Base,UW Length);
void PutTaskPriority(ID taskid);
void Delay(void);
 
static ID TaskID_A;
static ID TaskID_B;
static ID MtxID;
static UB *GlobalData;
 
ER MtxSample( void)
{
    T_CMTX cmtx;     //创建一个T_CMTX型的结构体;
    T_CTSK ctskA;
    T_CTSK ctskB;


    tm_putstring((UB*)"Mutex sample step 1-Create Mutex;\n");  //输出:Mutex sample step 1-Create Mutex;
    cmtx.exinf  = (VP)NULL;      //扩展信息为空;
    cmtx.mtxatr = TA_TPRI|TA_CEILING;     //属性为任务按照优先级来排队,优先级顶置协议;
    cmtx.ceilpri = 15;    /* priority ceiling for mutex */     互斥体顶置优先级为15;
    MtxID = tk_cre_mtx(&cmtx);   //创建互斥体,返回ID给MtxID;
    if(MtxID>0) {

        tm_putstring((UB*)"Mutex sample create step 1-Create Mutex, ID is ");       //输出:Mutex sample create step 1-Create Mutex, ID is

        tm_putchar(MtxID+48);
        tm_putstring((UB*)" !\n");
    } else {
        tm_putstring((UB*)"Error code is ");
        tm_putchar(MtxID+48);
        tm_putstring((UB*)" !\n");
    }


    ctskA.exinf   = (VP)NULL;                 //创建任务A; 
    ctskA.tskatr  = TA_HLNG | TA_RNG0;
    ctskA.task    = MtxSampleTaskA;
    ctskA.itskpri = 20;
    ctskA.stksz   = 512*4;
    TaskID_A      = tk_cre_tsk(&ctskA);
    if(TaskID_A>0) {
        tm_putstring((UB*)"Mutex sample create step 2-Create Task A, ID is "); //输出:Mutex sample create step 2-Create Task A, ID is
        tm_putchar(TaskID_A+48);
        tm_putstring((UB*)" !\n");
    } else {
        tm_putstring((UB*)"Error code is ");
        tm_putchar(TaskID_A+48);
        tm_putstring((UB*)" !\n");
    }


    ctskB.exinf   = (VP)NULL;                  //创建任务B;
    ctskB.tskatr  = TA_HLNG | TA_RNG0;
    ctskB.task    = MtxSampleTaskB;
    ctskB.itskpri = 20;
    ctskB.stksz   = 512*4;
    TaskID_B      = tk_cre_tsk(&ctskB);
    if(TaskID_B>0) {
        tm_putstring((UB*)"Mutex sample create step 3-Create Task B, ID is ");  //输出:Mutex sample create step 3-Create Task B, ID is
        tm_putchar(TaskID_B+48);
        tm_putstring((UB*)" !\n");
    } else {
        tm_putstring((UB*)"Error code is ");
        tm_putchar(TaskID_B+48);
        tm_putstring((UB*)" !\n");
    }


    tm_putstring((UB*)"Mutex sample create step 4-Start Task B;\n\n");  //输出:Mutex sample create step 4-Start Task B;
    tk_sta_tsk(TaskID_B,0);


    return E_OK;

 

 
void MtxSampleTaskA(W stacd,VP exinf)   //任务A服务函数;
{
tm_putstring((UB*)"Task A start successfully!\n");   //输出:Task A start successfully!
while(1){
        PutTaskPriority(TaskID_A);
tk_loc_mtx(MtxID, TMO_FEVR);
tm_putstring((UB*)"Task A locked the mutex successfully!\n");   //输出:Task A locked the mutex successfully!
        PutTaskPriority(TaskID_A);
GlobalData = "Task A is visiting GlobalData...\n";
tm_putstring((UB*)GlobalData);  //输出: Task A is visiting GlobalData...
        Delay();
        tm_putstring((UB*)"======================================\n");   //输出:======================================
        Delay();
tm_putstring((UB*)"Task A will unlock the mutex!\n\n");   //输出:Task A will unlock the mutex!\n
tk_unl_mtx(MtxID);     //解锁指定的互斥体;
}
}

 
void MtxSampleTaskB(W stacd,VP exinf)  //任务B的服务函数;
{
B c;


    tm_putstring((UB*)"Task B will start task A;\n\n");  //输出:Task B will start task A;
    tk_sta_tsk(TaskID_A,0);  //启动任务A;


while(1){
        tm_putstring((UB*)"Task B is running,input Command(e=exit):\n");  //输出:Task B is running,input Command(e=exit):
        c = tm_getchar(0);
        if(c=='e') {
       break;
        }


        PutTaskPriority(TaskID_B);
tk_loc_mtx(MtxID, TMO_FEVR);  //锁定互斥体;
    tm_putstring((UB*)"Task B locked the mutex successfully!\n");  //输出:Task B locked the mutex successfully!
        PutTaskPriority(TaskID_B);
GlobalData = "Task B is visiting GlobalData...\n";
tm_putstring((UB*)GlobalData);  //输出:Task B is visiting GlobalData...
        Delay();
        tm_putstring((UB*)"======================================\n");   //输出:======================================
        Delay();
tm_putstring((UB*)"Task B will unlock the mutex!\n\n");  //输出:Task B will unlock the mutex!
tk_unl_mtx(MtxID);  //解锁互斥体;
}
tm_putstring((UB*)"Task B will delete mutex;\n");   输出:Task B will delete mutex;
    tk_del_mbx(MtxID);     //删除互斥体;
tm_putstring((UB*)"Task B will terminate&delete task A;\n");   //输出:Task B will terminate&delete task A;
    tk_ter_tsk(TaskID_A);
    tk_del_tsk(TaskID_A);
tm_putstring((UB*)"Task B will terminate&delete self;\n");
    tk_exd_tsk();
}


 
UW ltostr(UW Val,B *pString,UB Base,UW Length)
{
B  c, *pInit;
UB  i,j;
UW Len,SwapLen;


*pString='\0';
pInit=pString;
if(Val==0) {
   *pString++='0';
   *pString='\0';              /*added by cheny to deal with the case that input number is zero 2009-04-15*/
}
switch(Base) {
case 10:                    /* Base is 10,convert to 10 base string */
   for(;Val>0;) {
       i=Val%Base;
       Val=Val/Base;
       *pString++=(B)((UB)'0'+i);
   }
   *pString='\0';
   break;
case 16:                    /* Base is 16,convert to 16 base string */
   for(;Val>0;) {
       i=Val%Base;
       Val=Val/Base;
       if(i<=9) {
           *pString++=(B)((UB)'0'+i);
       } else {
           *pString++=(B)((UB)'A'+i-10);
       }
   }
   *pString='\0';
   break;
default:
   break;
}


Len=strlen(pInit);
SwapLen=Len;
/*fill '\0' from string end to right */
if(Length>Len) {
for(i=Len;i<Length;i++) {
*(pInit+i)='\0';
}
*(pInit+Length)='\0';
}


/* swap high and low order */
if(SwapLen>0) {
for(i=0,j=SwapLen - 1;i<j;i++,j--) {
   c=*(pInit+i);
   *(pInit+i)=*(pInit+j);
   *(pInit+j)=c;
}
}
    return SwapLen;
}

 
void PutTaskPriority(ID taskid)   //查询并打印出任务的优先级;
{
    T_RTSK rtsk;
    ER ercd;
    B tskpri[10];


    ercd = tk_ref_tsk(taskid, &rtsk);  //查询任务的各种状态;
    if(E_OK == ercd){
    if(taskid == TaskID_A){
       tm_putstring((UB*)"Task A");
    }else{
       tm_putstring((UB*)"Task B");
    }
      tm_putstring((UB*)" current priority is ");  
    ltostr(rtsk.tskpri,tskpri,10,10);
      tm_putstring((UB*)tskpri);
      tm_putstring((UB*)"\n");
    }



 
void Delay(void)
{
    UINT i=0x1000000;
    for(;i>0;i--);
}


输出结果:

Mutex sample step 1-Create Mutex;

Mutex sample create step 1-Create Mutex, ID is 1!

Mutex sample create step 2-Create Task A, ID is 2!

Mutex sample create step 3-Create Task B, ID is 3!

Mutex sample create step 4-Start Task B;

Task B will start task A;


Task B is running,input Command(e=exit):

若办公输入的不是e:

Task B current priority is 20

Task B locked the mutex successfully!

Task B current priority is 15

Task B is visiting GlobalData...

======================================

Task B will unlock the mutex!


Task A start successfully!

Task A current priority is 20

Task A locked the mutex successfully!

Task A current priority is 15

Task A is visiting GlobalData...

======================================

Task A will unlock the mutex!


若输入的是e:

Task B will delete mutex;

Task B will terminate&delete task A;

Task B will terminate&delete self;


创建任务A, B,和互斥体MtxID, 任务A, B的优先级相同,先启动任务B, 在任务B里面启动任务A, 

任务B先锁定互斥体,那么任务就不能获得资源, 根据优先级顶置协议,任务B的优先级提高到15,

高于任务A, 当任务B解锁互斥体后, 任务B的优先级回来原来,然后去执行任务A, 执行任务A和执行

任务B过程是一样的;



 


菜鸟
2013-07-13 13:10:54     打赏
9楼
                                                  06.messbuf程序分析
#include "MessbufSample.h"


#define  MBF1SIZE      200
#define  MBF2SIZE      200
#define  MBF1MAXMSIZE  80
#define  MBF2MAXMSIZE  80


void MbfSampleTaskA(W stacd,VP exinf);
void MbfSampleTaskB(W stacd,VP exinf);


void MbfSamplePutErcd(ER ercd);
UW ltostr(UW Val,B *pString,UB Base,UW Length);


static ID TaskID_A;
static ID TaskID_B;
static ID MbfID_1;
static ID MbfID_2;

 
ER MbfSample( void)
{
    ER ercd=E_OK;
    T_CTSK ctsk;
T_CMBF cmbf;      //定义一个T_CMBF型的结构体cmbf


    tm_putstring((UB*)"Message buffer sample create step 1-Create Task A.\n");   //输出:Message buffer sample create step 1-Create Task A.
    ctsk.exinf = (VP)NULL;
    ctsk.tskatr = TA_HLNG|TA_RNG0;           /*创建任务A*/
    ctsk.task = MbfSampleTaskA;
    ctsk.itskpri = 18;
    ctsk.stksz = 512*4;
    TaskID_A = tk_cre_tsk(&ctsk);
    if(TaskID_A < E_OK) {
    ercd=TaskID_A;
        return ercd;
    }
    
    tm_putstring((UB*)"Message buffer sample create step 2-Create Task B.\n");   //输出:Message buffer sample create step 2-Create Task B.
    ctsk.exinf = (VP)NULL;
    ctsk.tskatr = TA_HLNG|TA_RNG0;    /*创建任务B*/
    ctsk.task = MbfSampleTaskB;
    ctsk.itskpri = 20;
    ctsk.stksz = 512*4;
    TaskID_B = tk_cre_tsk(&ctsk);
    if(TaskID_B < E_OK) {
    ercd=TaskID_B;
        return ercd;
    }
    
    tm_putstring((UB*)"Message buffer sample create step 3-Create the first message buffer.\n");   //输出:Message buffer sample create step 3-Create the first message buffer.
    cmbf.exinf = (VP)NULL;
    cmbf.mbfatr = TA_TFIFO;  //任务按照先进先出进行排队;
    cmbf.bufsz = MBF1SIZE;      //消息缓冲大小为200  

    cmbf.maxmsz = MBF1MAXMSIZE;   //消息最大值为80

    cmbf.bufptr = (VP)NULL;  //用户缓冲区地址;



    MbfID_1 = tk_cre_mbf(&cmbf);      //创建消息缓冲区,返回ID给 MbfID_1
    if(MbfID_1 < E_OK){
    ercd=MbfID_1;
        return ercd;
    }


    tm_putstring((UB*)"Message buffer sample create step 4-Create the second message buffer.\n"); //输出:Message buffer sample create step 4-Create the second message buffer.
    cmbf.exinf = (VP)NULL;
    cmbf.mbfatr = TA_TFIFO;     //任务按照先进先出进行排队;
    cmbf.bufsz = MBF2SIZE;     //消息缓冲大小为200  
    cmbf.maxmsz = MBF2MAXMSIZE;    //消息最大值为80
    cmbf.bufptr = (VP)NULL; /*It is valid when  member bufptr(cmbf.bufptr) is evaluated with TA_USERBUF */


    MbfID_2 = tk_cre_mbf(&cmbf);   /创建消息缓冲区,返回ID给 MbfID_2
    if(MbfID_2 < E_OK){
    ercd=MbfID_2;
        return ercd;
    }


    tm_putstring((UB*)"Message buffer sample create step 5-Start Task A !\n");  //输出:Message buffer sample create step 5-Start Task A !
    ercd = tk_sta_tsk(TaskID_A,0);  //启动任务A
if (E_OK != ercd){
tm_putstring((UB*)"start Task A failed!!!\n");
}
    return TRUE;

 
void MbfSampleTaskA(W stacd,VP exinf)  //任务A启动函数;
{
ER ercd;
UW len;
B  c;
T_RMBF pk_rmbf;
W  msgsz;
B msg2mbf1[] = "Do you think that uTenux is the best RTOS ?\n";
B msgofmbf2[100]="\0";


tm_putstring((UB*)"Message buffer sample create step 6- Task A start Task B.\n");  //输出:Message buffer sample create step 6- Task A start Task B.
ercd = tk_sta_tsk(TaskID_B,0);   //启动任务B; 
if (E_OK != ercd){
   tm_putstring((UB*)"Task B start failed!\n");
}


for(;;){
tm_putstring((UB*)"\nTask A is running,Input Command(e=exit):\n");  //输出:Task A is running,Input Command(e=exit):
c = tm_getchar(0);
if('e' == c) {
   break;
}


        len = strlen(msg2mbf1);
tm_putstring((UB*)"Task A will send a message to the first message buffer......\n");  //输出:Task A will send a message to the first message buffer......
tm_putstring((UB*)"The message is : ");  //输出:The message is : Do you think that uTenux is the best RTOS ?
tm_putstring((UB*)msg2mbf1);
ercd = tk_snd_mbf(MbfID_1, (VP)msg2mbf1, len, -1); //向消息缓冲区发送消息;
if(E_OK == ercd){
tm_putstring((UB*)"Task A have successfully sent a message to the first message buffer! \n");  //输出:Task A have successfully sent a message to the first message buffer! 
} else{
tm_putstring((UB*)"Task A can't send a message to the first message buffer! \n");
MbfSamplePutErcd(ercd);
}


tm_putstring((UB*)"Task A will receive a message from the second message buffer......\n");   //输出:Task A will receive a message from the second message buffer......
ercd = tk_ref_mbf(MbfID_2, &pk_rmbf);   //查询消息缓冲区各种状态;
if(E_OK == ercd){
   /*The second message buffer haven't any message */
if(pk_rmbf.frbufsz == MBF2SIZE){
  tm_putstring((UB*)"There isn't a message in the second message buffer ! \n");
  tm_putstring((UB*)"Task A is waiting......\n");
  }
}
tk_rcv_mbf(MbfID_2, (VP)msgofmbf2, -1);  //从消息缓冲区MbfID_2中接收消息;
    tm_putstring((UB*)"Task A is rerunning.\n");  //输出:Task A is rerunning.
tm_putstring((UB*)"Task A have successfully received a message from the second message buffer! \n");  //输出:Task A have successfully received a message from the second message buffer!
tm_putstring((UB*)"The message is : ");  //输出:The message is : 
tm_putstring((UB*)(msgofmbf2));
tm_putstring((UB*)"\n");
}


tm_putstring((UB*)"Task A will delete message buffers.\n");  //输出:Task A will delete message buffers.
tk_del_mbf(MbfID_1);   //删除消息缓冲区1,和2;
tk_del_mbf(MbfID_2);
tm_putstring((UB*)"Task A will terminate and delete task B and task C.\n");  //输出:Task A will terminate and delete task B and task C.\n"
tk_ter_tsk(TaskID_B);   //暂停并删除任务B;
tk_del_tsk(TaskID_B);
tm_putstring((UB*)"Task A will terminate and delete self.\n");  //输出: Task A will terminate and delete self.
tk_exd_tsk();
}
 
void MbfSampleTaskB(W stacd,VP exinf)   //任务B的服务函数
{
ER ercd;
B msgofmbf1[100]="\0";
B msg2mbf2[]= "Yes,it is the best !\n";
UW len;
T_RMBF pk_rmbf;
W  msgsz;


for(;;){
tm_putstring((UB*)"\nTask B is running.\n");   //输出:Task B is running.
tm_putstring((UB*)"Task B will receive a message from the first message buffer....... \n");   //输出:Task B will receive a message from the first message buffer.......
ercd = tk_ref_mbf(MbfID_1, &pk_rmbf);   //查询消息缓冲区的状态
if(E_OK == ercd){
/*The first message buffer haven't any message */
  if(pk_rmbf.frbufsz == MBF1SIZE){
  tm_putstring((UB*)"There isn't a message in the first message buffer ! \n");
  tm_putstring((UB*)"Task B will enter the receive waiting queue of the first message buffer ! \n");
  }
}
msgsz = tk_rcv_mbf(MbfID_1, (VP)msgofmbf1, -1);  //从消息缓冲区里接收信息;
  if(msgsz > 0){
  tm_putstring((UB*)"Task B have successfully received a message from the first message buffer! \n");   //输出:Task B have successfully received a message from the first message buffer!
    tm_putstring((UB*)"The message is : ");  //输出:The message is :  Do you think that uTenux is the best RTOS ?
    tm_putstring((UB*)(msgofmbf1));
  }else{
    tm_putstring((UB*)"Task B can't receive a message from the first message buffer! \n");
  }


len = strlen(msg2mbf2);
tm_putstring((UB*)"Task B will send a message to the second message buffer......\n");   //输出:Task B will send a message to the second message buffer......
tm_putstring((UB*)"The message is : "); //l输出:The message is :
tm_putstring((UB*)msg2mbf2);   //输出: Yes,it is the best !
tm_putstring((UB*)"\n");
ercd = tk_snd_mbf(MbfID_2, (VP)msg2mbf2, len, -1);   //向消息缓冲区里发送消息;
if(E_OK != ercd){
tm_putstring((UB*)"Task B can't send a message to the second message buffer! \n");
MbfSamplePutErcd(ercd);
}
    }
}


 
void MbfSamplePutErcd(ER ercd)
{
    B ercds[10];


  tm_putstring((UB*)"Error code is ");
    switch(ercd){
        case E_OK:
        tm_putstring((UB*)"0:E_OK");
        break;
        case E_SYS:
        tm_putstring((UB*)"-5:E_SYS");
        break;
        case E_NOSPT:
        tm_putstring((UB*)"-9:E_NOSPT");
        break;
        case E_RSATR:
        tm_putstring((UB*)"-11:E_LIMIT");
        break;
        case E_PAR:
        tm_putstring((UB*)"-17:E_PAR");
        break;
        case E_ID:
        tm_putstring((UB*)"-18:E_ID");
        break;
        case E_NOMEM:
        tm_putstring((UB*)"-33:E_NOMEM");
        break;
        case E_LIMIT:
        tm_putstring((UB*)"-34:E_LIMIT");
        break;
        case E_NOEXS:
        tm_putstring((UB*)"-42:E_NOEXS");
        break;
        case E_TMOUT:
        tm_putstring((UB*)"-50:E_TMOUT");
        break;
        default:
        if(ercd>0){
           tm_putstring((UB*)">0,UNKNOWN:");
           ltostr((UW)ercd,ercds,16,10);
           tm_putstring((UB*)ercds);
        } else{
        if(ercd<-67){
        tm_putstring((UB*)"<-67,UNKNOWN");
        } else {
        tm_putstring((UB*)"0,-67,UNKNOWN");
        }
        }
        break;
    }
  tm_putstring((UB*)"\n");
}


 
UW ltostr(UW Val,B *pString,UB Base,UW Length)
{
B  c, *pInit;
UB  i,j;
UW Len,SwapLen;


*pString='\0';
pInit=pString;
if(Val==0) {
   *pString++='0';
   *pString='\0';
}
switch(Base) {
case 10:                    /* Base is 10,convert to 10 base string */
   for(;Val>0;) {
       i=Val%Base;
       Val=Val/Base;
       *pString++=(B)((UB)'0'+i);
   }
   *pString='\0';
   break;
case 16:                    /* Base is 16,convert to 16 base string */
   for(;Val>0;) {
       i=Val%Base;
       Val=Val/Base;
       if(i<=9) {
           *pString++=(B)((UB)'0'+i);
       } else {
           *pString++=(B)((UB)'A'+i-10);
       }
   }
   *pString='\0';
   break;
default:
   break;
}


Len=strlen(pInit);
SwapLen=Len;
/*fill '\0' from string end to right */
if(Length>Len) {
for(i=Len;i<Length;i++) {
*(pInit+i)='\0';
}
*(pInit+Length)='\0';
}


/* swap high and low order */
if(SwapLen>0) {
for(i=0,j=SwapLen - 1;i<j;i++,j--) {
   c=*(pInit+i);
   *(pInit+i)=*(pInit+j);
   *(pInit+j)=c;
}
}
    return SwapLen;
}



运行输出:

Message buffer sample create step 1-Create Task A.

Message buffer sample create step 2-Create Task B.

Message buffer sample create step 3-Create the first message buffer.

Message buffer sample create step 4-Create the second message buffer.

Message buffer sample create step 5-Start Task A !

Message buffer sample create step 6- Task A start Task B.


Task A is running,Input Command(e=exit):

若输入的不是e:

Task A will send a message to the first message buffer......

The message is : Do you think that uTenux is the best RTOS ?

Task A have successfully sent a message to the first message buffer! 

Task A will receive a message from the second message buffer......

There isn't a message in the second message buffer !

Task A is waiting......

Task B is running.

Task B will receive a message from the first message buffer.......

Task B have successfully received a message from the first message buffer!

The message is :  Do you think that uTenux is the best RTOS ?

Task B will send a message to the second message buffer......

The message is : Yes,it is the best !

Task A is running.

Task A have successfully received a message from the second message buffer!

The message is : Yes,it is the best !


若输入e:

Task A will delete message buffers.

Task A will terminate and delete task B and task C.

Task A will terminate and delete self.


创建了两个任务A, B, 和两个消息缓冲区,the first , the second; 先启动任务A, 在任务A里启动任务B, 

任务A向第一个消息缓冲区里写信息,然后等待从第二个消息缓冲区里接收信息, 此时,任务A处于等待状态, 

运行任务B, 任务B成功从第一个消息缓冲区里接收信息, 然后向第二个消息缓冲区里写信息,接着任务A

就能从第一个消息缓冲区里接收到消息, 从而实现了任务间的双向通信;


高工
2013-07-13 20:50:21     打赏
10楼
这是被老外批评后的凑数???

共16条 1/2 1 2 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]