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

共16条 2/2 1 2 跳转至
菜鸟
2013-07-14 01:17:02     打赏
11楼
                                                         07.rendport程序的分析
void PorSampleTaskA(W stacd,VP exinf);
void PorSampleTaskB(W stacd,VP exinf);
void PorSamplePutErcd(ER ercd);
void PorSamplePutPor(void);
UW ltostr(UW Val,B *pString,UB Base,UW Length);
void Delay(void);


static ID TaskID_A;
static ID TaskID_B;
static ID PorID;
 
 
EXPORT ER PorSample( void )
{
    T_CPOR cpor;
    T_CTSK ctskA;
    T_CTSK ctskB;

    tm_putstring((UB*)"Rendevons port sample create step 1-Create Rendevons port, ");  //输出:Rendevons port sample create step 1-Create Rendevons port,
    cpor.poratr  = TA_TFIFO;  //任务按照FIFO进行排队;
    cpor.maxcmsz = 80;   //最大调用消息的大小;
    cpor.maxrmsz = 80;    //最大回应消息的大小;
    PorID        = tk_cre_por(&cpor);  //创建集合点端口;
    if(PorID > E_OK) {
        tm_putstring((UB*)"ID is ");  //输出:ID is 1!
        tm_putchar(PorID+48);
        tm_putstring((UB*)" !\n");
    } else {
        tm_putstring((UB*)"Error code is ");
        tm_putchar(PorID+48);
        tm_putstring((UB*)" !\n");
    }


    tm_putstring((UB*)"Rendevons port sample create step 2-Create Task A, ");  //输出:Rendevons port sample create step 2-Create Task A,
    ctskA.exinf   = (VP)NULL;                       /*创建任务A*/
    ctskA.tskatr  = TA_HLNG | TA_RNG0;
    ctskA.task    = PorSampleTaskA;
    ctskA.itskpri = 10;
    ctskA.stksz   = 512*4;
    TaskID_A      = tk_cre_tsk(&ctskA);   
    if(TaskID_A > E_OK) {
        tm_putstring((UB*)"ID is ");   //输出: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");
    }

    tm_putstring((UB*)"Rendevons port sample create step 3-Create Task B, ");  //输出:Rendevons port sample create step 3-Create Task B,
    ctskB.exinf   = (VP)NULL;
    ctskB.tskatr  = TA_HLNG | TA_RNG0;       /*创建任务B*/
    ctskB.task    = PorSampleTaskB;
    ctskB.itskpri = 10;
    ctskB.stksz   = 512*4;
    TaskID_B      = tk_cre_tsk(&ctskB);
    if(TaskID_B>0) {
        tm_putstring((UB*)"ID is ");    //输出: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*)"Rendevons port sample create step 4-Start Task B;\n\n");   //输出:Rendevons port sample create step 4-Start Task B;
    tk_sta_tsk(TaskID_B,0);

    return E_OK;
}

 
void PorSampleTaskA(W stacd,VP exinf)  //任务A的服务函数;
{
    ER ercd;
    UINT i;
    RNO rdvno,cmsgsz;
    B calmsg[80],rplmsg[80];
 
    rplmsg[0] = '\0';
    rplmsg[1] = '\0';
    calmsg[1] = '\0';
    for(i=0;;i++) {
        cmsgsz = tk_acp_por(PorID,0x1,&rdvno ,(VP)calmsg,-1);  //接受集合点端口;接受模式为0x1, 返回集合点编号, 返回调用消息的大小;
        tm_putstring((UB*)"Task A is now accept and reply rendevons port ");   //输出:Task A is now accept and reply rendevons port 
        tm_putchar(i+48+1);  
        tm_putstring((UB*)" times\n");
        if(cmsgsz > E_OK){
            tm_putstring((UB*)"Task A accept rendevons port message:");
            tm_putstring((UB*)calmsg);
        } else {
            tm_putstring((UB*)"Task A cannot accept rendevons port: ");
            PorSamplePutErcd(cmsgsz);
        }
        tm_putstring((UB*)"\n");


        switch(calmsg[0]){
            case '0':
                rplmsg[0] = 'a';
                break;
            case '1':
                rplmsg[0] = 'b';
                break;
            case '2':
                rplmsg[0] = 'c';
                break;
            case '3':
                rplmsg[0] = 'd';
                break;
            case '4':
                rplmsg[0] = 'e';
                break;
            case '5':
                rplmsg[0] = 'f';
                break;
            case '6':
                rplmsg[0] = 'g';
                break;
            case '7':
                rplmsg[0] = 'h';
                break;
            case '8':
                rplmsg[0] = 'i';
                break;
            case '9':
                rplmsg[0] = 'j';
                break;
            default:
                rplmsg[0] = 'k';
                break;
        }
        ercd = tk_rpl_rdv(rdvno ,(VP)rplmsg,strlen(rplmsg));  // 回应集合点,集合点编号,回应消息的数据包的地址, 回应消息的大小;
        if(ercd == E_OK){
            tm_putstring((UB*)"Task A reply rendevons port message:");   
            tm_putstring((UB*)rplmsg);
        } else {
            tm_putstring((UB*)"Task A cannot reply rendevons port: ");
            PorSamplePutErcd(ercd);
        }
        tm_putstring((UB*)"\n");
        PorSamplePutPor();
    }
}
 
 

void PorSampleTaskB(W stacd,VP exinf) //任务B的服务函数 

{
    B c;
    ER ercd;
    UINT i,rmsgsz;
    B calmsg[80];


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


    calmsg[1]='\0';
    for(i=0;i<=9;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;
        }
        tm_putstring((UB*)"Task B is now calling rendevons port ");   //输出:Task B is now calling rendevons port 
        tm_putchar(i+48+1);
        tm_putstring((UB*)" times\n");
        calmsg[0]=(B)(i+48);
        tm_putstring((UB*)"Task B send rendevons port message:"); //输出:Task B send rendevons port message:
        tm_putstring((UB*)calmsg);  
        tm_putstring((UB*)"\n");
        rmsgsz = tk_cal_por(PorID,0x1,(VP)calmsg,strlen(calmsg),-1);  // 调用集合点端口,调用位模式,消息数据包地址,返回调用消息的大小;
        if(rmsgsz > E_OK){
            tm_putstring((UB*)"Task B can receive rendevons port message:");  //输出:Task B can receive rendevons port message:
            tm_putstring((UB*)calmsg);
         } else {
            tm_putstring((UB*)"Task B cannot receive rendevons port message:");
            PorSamplePutErcd(rmsgsz);
        }
        tm_putstring((UB*)"\n");
        PorSamplePutPor();
    }


    tm_putstring((UB*)"Task B will delete rendevons port;\n");  //输出:Task B will delete rendevons port;
    tk_del_por(PorID);
    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");  //输出:Task B will terminate&delete self;
    tk_exd_tsk();
}

 
void PorSamplePutErcd(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 PorSamplePutPor(void)
{
    B info[10];
    T_RPOR rpor;


    tm_putstring((UB*)"Now rendevons port infor is wtsk atsk maxcmsz maxrmsz:");  //输出:Now rendevons port infor is wtsk atsk maxcmsz maxrmsz:
    tk_ref_por(PorID, &rpor);   //查询集合点的各种状态;
    ltostr(rpor.wtsk,info,10,10);
    tm_putstring((UB*)info);   
    tm_putstring((UB*)" ");
    ltostr(rpor.atsk,info,10,10);
    tm_putstring((UB*)info);
    tm_putstring((UB*)" ");
    ltostr(rpor.maxcmsz,info,10,10);
    tm_putstring((UB*)info);
    tm_putstring((UB*)" ");
    ltostr(rpor.maxrmsz,info,10,10);
    tm_putstring((UB*)info);
    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){
        /* 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';
    }


    /* 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--);
}

运行结果:

Rendevons port sample create step 1-Create Rendevons port, ID is 1!

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

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

Rendevons port sample create step 4-Start Task B;

Task B will start task A;

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

若输入不是e:

Task B is now calling rendevons port  1times

Task B send rendevons port message: 0

Task A is now accept and reply rendevons port 1times

Task A reply rendevons port message: a

Now rendevons port infor is wtsk atsk maxcmsz maxrmsz: 0 0 80 80

Task B can receive rendevons port message: a

Now rendevons port infor is wtsk atsk maxcmsz maxrmsz: 0 2 80 80

若输入e:

Task B will delete rendevons port;

Task B will terminate&delete task A;

Task B will terminate&delete self;


创建任务A,B, 创建一个集合点,先启动任务B, 在任务B里面启动任务A, 

任务B向消息数据包里写入0,然后任务调用集合端口,读取到0, 然后通过

回应集合点端口, 让任务B接收到字符a,这样一直循环执行下去;





菜鸟
2013-07-14 18:18:38     打赏
12楼
 
#include "MpfSample.h"
 
void MpfSampleTaskA(W stacd,VP exinf);
void MpfSampleTaskB(W stacd,VP exinf);
void MpfSamplePutCnt(void);
void MpfSamplePutErcd(ER ercd);
UW ltostr(UW Val,B *pString,UB Base,UW Length);


static ID TaskID_A;
static ID TaskID_B;
static ID mpfid;
static VP blf=NULL;

 
ER MpfSample( void)
{
    ER ercd = E_OK;
    T_CTSK ctsk;
    T_CMPF cmpf;


    tm_putstring((UB*)"Mempool fix sample create Task A;\n");   //输出:Mempool fix sample create Task A;
    ctsk.exinf = (VP)NULL;
    ctsk.tskatr = TA_HLNG | TA_RNG0;
    ctsk.task = MpfSampleTaskA;
    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*)"Mempool fix sample create Task B;\n");   //输出:Mempool fix sample create Task B;
    ctsk.exinf = (VP)NULL;
    ctsk.tskatr = TA_HLNG | TA_RNG0;
    ctsk.task = MpfSampleTaskB;
    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*)"Mempool fix sample create a mempoolf;\n");  //输出:Mempool fix sample create a mempoolf;
    cmpf.exinf = (VP)NULL;
    cmpf.mpfatr = TA_TFIFO | TA_RNG0;
    cmpf.mpfcnt = 20;   //内存池块计数;
    cmpf.blfsz = 100;  //内存块大小;
    mpfid = tk_cre_mpf(&cmpf);    //创建固定尺寸的内存池;
    if (mpfid < E_OK){
        ercd = mpfid;
        return ercd;
    }
    tm_putstring((UB*)"Mempool fix sample create a mempoolf successfully;\n");   //输出:Mempool fix sample create a mempoolf successfully;
    MpfSamplePutCnt(); //输出:Now Free memory block number is 5


    tm_putstring((UB*)"Mempool fix sample start Task B;\n");  //输出:Mempool fix sample start Task B;
    tk_sta_tsk(TaskID_B,0); 


    return TRUE;

 
void MpfSampleTaskA(W stacd,VP exinf)
{
    ER ercd;
    B c;


    tm_putstring((UB*)"Task A enter sleep status;\n");   //输出:Task A enter sleep status;
    tk_slp_tsk(-1);


    for(;;){
        tm_putstring((UB*)"Task A run;\n");
        tm_putstring((UB*)"Task A print the contents of the memory block;\n");
        tm_putstring((UB*)blf);
        tm_putstring((UB*)"\n");


        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 A release the memory block;\n");
            ercd = tk_rel_mpf(mpfid, blf);
            blf=NULL;
            if(E_OK == ercd){
                tm_putstring((UB*)"Task A release the memory block successfully;\n");
            } else {
                tm_putstring((UB*)"Task A can't release the memory block;\n");
                MpfSamplePutErcd(ercd);
            }
            MpfSamplePutCnt();


            tm_putstring((UB*)"Task A enter sleep status;\n");
            tk_slp_tsk(-1);
        }
    }


    tm_putstring((UB*)"Task A delete the memory block;\n");   //输出:Task A delete the memory block;
    ercd = tk_del_mpf(mpfid);   //删除固定尺寸内存池;
    if(E_OK == ercd){
        tm_putstring((UB*)"Task A delete the memory block successfully;\n");  //输出:Task A delete the memory block successfully;
    } else {
        tm_putstring((UB*)"Task A can't delete the memory block;\n");    
    }


    tm_putstring((UB*)"Task A will terminate&delete task B;\n");  //输出: Task A will terminate&delete task B;
    tk_ter_tsk(TaskID_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();
}

 
void MpfSampleTaskB(W stacd,VP exinf)
{
    ER ercd;
    size_t len;
    B *buf = "uTenux OS is perfect;";
    B blfadd[10];


    tm_putstring((UB*)"Task B start Task A;\n");   //输出:Task B start Task A;
    ercd = tk_sta_tsk(TaskID_A,0);
    if (E_OK != ercd){
        tm_putstring((UB*)"Task B start Task A failed;\n");
    }


    for(;;){
        tm_putstring((UB*)"Task B get a memory block;\n");  //输出:Task B get a memory block;
        ercd = tk_get_mpf(mpfid, &blf, TMO_FEVR);  
        if(E_OK == ercd){
            tm_putstring((UB*)"Task B get a memory block successfully:");  //输出:Task B get a memory block successfully: 2000117C
            ltostr((UW)blf,blfadd,16,10);  
            tm_putstring((UB*)blfadd);
            tm_putstring((UB*)"\n");
            MpfSamplePutCnt();   //输出:Now Free memory block number is 4
            len = strlen(buf)+1;
            memcpy(blf, buf, len);  //把buf地址给blf;
        } else {
            tm_putstring((UB*)"Task B can't get a memory block;\n");
        }


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

 
void MpfSamplePutErcd(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 MpfSamplePutCnt(void)
{
    B frbcnt[10];
    T_RMPF rmpf;


    tm_putstring((UB*)"Now Free memory block number is ");
    tk_ref_mpf(mpfid, &rmpf);
    ltostr(rmpf.frbcnt,frbcnt,10,10);
    tm_putstring((UB*)frbcnt);
    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){
        /* 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';
    }


    /* 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;
}



运行结果:

Mempool fix sample create Task A;

Mempool fix sample create Task B;

Mempool fix sample create a mempoolf;

Mempool fix sample create a mempoolf successfully;

Now Free memory block number is 5

Mempool fix sample start Task B;

Task B start Task A;

Task A enter sleep status;

Task B get a memory block;

Task B get a memory block successfully: 2000117C

Now Free memory block number is 4

Task A run;

Task A print the contents of the memory block;

uTenux OS is perfect;

Input Command(e=exit):

若输入不是e:

Task A release the memory block;

Task A delete the memory block successfully;

Now Free memory block number is 5

Task A enter sleep status;


若输入的是e:

Exit from all task;

Task A release the memory block;

Task A delete the memory block successfully;

Task A will terminate&delete task B;

Task A will terminate&delete self;


 


高工
2013-07-17 10:08:47     打赏
13楼
我表示来看进程了,但是一堆代码,暂时搞不清状况,不知道LZ 从哪开始的

菜鸟
2013-07-22 21:30:15     打赏
14楼

                                               点亮第一个LED灯

#define PIN_PA21        {PIO_PA21, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT}    //管脚的结构体;有mask, id, pio *pio, type, attribuate; 

extern int main( void )
{
const Pin led_pin1 = PIN_PA21;     //定义一个用户管脚;
PIO_Configure(&led_pin1, 1);   //配置管脚;
PIO_Set(&led_pin1);     //管脚置1;
//PIO_Clear(&led_pin1);
}


高工
2013-07-22 21:37:04     打赏
15楼
介个贴源代码没有意思啊

高工
2013-07-23 09:23:46     打赏
16楼
我想说头帖做的真心不错,后续LZ 也是按照之前的计划开发准备的吗?

共16条 2/2 1 2 跳转至

回复

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