课程所需,看了看ucosii代码,写了个ppt如下:
http://download.csdn.net/detail/clarkchenhot/9540875
例子介绍:
下面贴个读写者的例子代码:
#include "..\ucos\includes.h"
#define TASK_STK_SIZE 512 // Stack size, in bytes
//#define SUSPEND_RESUME // Task 1 and 2 use suspend and resume for scheduling
// (if not defined: use time delays)
OS_STK TaskStk1[TASK_STK_SIZE]; // Task stacks
OS_STK TaskStk2[TASK_STK_SIZE];
OS_STK TaskStk3[TASK_STK_SIZE];
OS_STK TaskStk4[TASK_STK_SIZE];
OS_STK TaskStk5[TASK_STK_SIZE];
void Task_one_create(void *pdata);
void Task_two_read(void *pdata);
void Task_three_read(void *pdata);
void Task_four_write(void *pdata);
void Task_five_write(void *pdata);
void output(char *p,OS_FLAG_GRP *ptr);
#define COUNT 3//the count tag of read or write
OS_FLAG_GRP *share;
OS_FLAG_GRP *ret;
INT8U flags = 0;
OS_CPU_SR cpu_sr = 0;
INT8U read_count =0;
INT8U write_count = 0;
/*
*********************************************************************************************************
* MAIN
*********************************************************************************************************
*/
int main(void)
{
OSInit(); //Calling sequence -->OSInitHookBegin-->OSTaskStkInit-->OSTCBInitHook-->OSTaskCreateHook-->OSInitHookEnd
OSTaskCreate(Task_one_create, NULL, &TaskStk1[TASK_STK_SIZE-1], 5); //Calling sequence -->OSTaskStkInit-->OSTCBInitHook-->OSTaskCreateHook
OSTaskCreate(Task_two_read, NULL, &TaskStk2[TASK_STK_SIZE-1], 30);
OSTaskCreate(Task_three_read, NULL, &TaskStk3[TASK_STK_SIZE-1], 40);
OSTaskCreate(Task_four_write, NULL, &TaskStk4[TASK_STK_SIZE-1], 10);
OSTaskCreate(Task_five_write, NULL, &TaskStk5[TASK_STK_SIZE-1], 20);
OSStart(); //Calling sequence -->OSTaskSwHook-->OSStartHighRdy
//OSTimeDlyHMSM(0,0,30,0);
//OSTaskDel(10);
//OSTaskDel(20);
//OSTaskDel(30);
//OSTaskDel(40);
return 0;
}
void Task_one_create(void *pdata)
{
INT8U err;
OS_FLAGS init = 0x0000;
INT8U *pname = "test flags";
pdata = pdata;
share = OSFlagCreate(init,&err);
OSFlagNameSet(share,pname,&err);
output("init:",share);
OSTimeDlyHMSM(1,1,30,0);
//ret = OSFlagDel(share,OS_DEL_NO_PEND,&err);
}
void Task_two_read(void *pdata)
{
OS_FLAGS ret;
INT8U err;
pdata = pdata;
while(1){
output("before task_two_read pend write bit:",share);
ret = OSFlagPend(share,0x2,OS_FLAG_WAIT_CLR_ALL,0,&err);//pend on bit 1(write bit)
ret = OSFlagPost(share,0x1,OS_FLAG_SET,&err);//set bit 0 = 1(read bit)
flags++;//the number of readers
output("task_two read from buff",share);
read_count++;
OSTimeDlyHMSM(0,0,2,0);//emulate the process of read
if(flags==1){
//output(NULL,share);
ret = OSFlagPost(share,0x1,OS_FLAG_CLR,&err);
output("task_two_read release bit0",share);//end of read , release bit0(read bit)
flags--;
}
else if(flags>1)
flags--;
if(read_count >=COUNT)
break;
OSTimeDlyHMSM(0,0,7,0);
}
printf("Del task_read_two prio is:30\n");
OSTaskDel(30);
}
void Task_three_read(void *pdata)
{
OS_FLAGS ret;
INT8U err;
pdata = pdata;
while(1){
output("before task_three_read pend write bit:",share);
ret = OSFlagPend(share,0x2,OS_FLAG_WAIT_CLR_ALL,0,&err);//pend bit 2(write bit)
ret = OSFlagPost(share,0x1,OS_FLAG_SET,&err);//set bit 1 = 1(read bit)
flags++;//the number of readers
output("task_three read from buff",share);
read_count++;
OSTimeDlyHMSM(0,0,2,0);//emulate the process of read
if(flags==1){
//output(NULL,share);
ret = OSFlagPost(share,0x1,OS_FLAG_CLR,&err);
output("task_three_read release bit0",share);//end of read , release bit1(read bit)
flags--;
}
else if(flags>1)
flags--;
if(read_count >=COUNT)
break;
OSTimeDlyHMSM(0,0,7,0);
}
printf("Del task_read_three prio is:40\n");
OSTaskDel(40);
}
void Task_four_write(void *pdata)
{
OS_FLAGS ret;
INT16U timeout;
INT8U err;
//char *input="task_four_write";
pdata = pdata;
timeout = 1;
while(1){
output("before task_four_write pend write/read pend:",share);
ret = OSFlagPend(share,0x3,OS_FLAG_WAIT_CLR_ALL,timeout,&err);
if(err!=0){//if apply failed ,try again!
err = 0;
OSTimeDlyHMSM(0,0,5,0);
continue;
}
ret = OSFlagPost(share,0x2,OS_FLAG_SET,&err);
output("task_four write buff\n",share);
write_count++;
OSTimeDlyHMSM(0,0,2,0);//emulat the process of write
ret = OSFlagPost(share,0x2,OS_FLAG_CLR,&err);//release the bit 1(write bit)
output("task_four_write release bit1",share);
if(write_count>=COUNT)
break;
OSTimeDlyHMSM(0,0,4,0);
}
printf("Del task_write_four prio is:10\n");
OSTaskDel(10);
}
void Task_five_write(void *pdata)
{
OS_FLAGS ret;
INT16U timeout;
INT8U err;
//char *input="task_four_write";
pdata = pdata;
timeout = 1;
while(1){
output("before task_five_write pend write/read pend:",share);
ret = OSFlagPend(share,0x3,OS_FLAG_WAIT_CLR_ALL,timeout,&err);
if(err!=0){//if apply failed ,try again!
err = 0;
OSTimeDlyHMSM(0,0,5,0);
continue;
}
ret = OSFlagPost(share,0x2,OS_FLAG_SET,&err);
output("task_five write buff\n",share);
write_count++;
OSTimeDlyHMSM(0,0,5,0);//emulat the process of write
ret = OSFlagPost(share,0x2,OS_FLAG_CLR,&err);//release the bit 1(write bit)
output("task_five_write release bit1",share);
if(write_count>=COUNT)
break;
OSTimeDlyHMSM(0,0,4,0);
}
printf("Del task_write_five prio is:20\n");
OSTaskDel(20);
}
void output(char *p,OS_FLAG_GRP *ptr){
OS_FLAG_NODE *temp;
OS_TCB *tcp_temp;
temp = ptr->OSFlagWaitList;
printf("---------------------begin--------------------\n");
if(p!=NULL)
printf("%s\n",p);
printf("OSFlagFlags=0x%x\n",ptr->OSFlagFlags);
OS_ENTER_CRITICAL();
while(temp!=NULL){
tcp_temp = temp->OSFlagNodeTCB;
printf("wait task prio = %u\n",tcp_temp->OSTCBPrio);
temp = temp->OSFlagNodeNext;
}
OS_EXIT_CRITICAL();
printf("----------------------end---------------------\n\n");
}