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呢?