You are on page 1of 10

#include "includes.

h"

#define
WORDs)

TASK_STK_SIZE

512

#define

N_TASKS

OS_STK

TaskStk1[N_TASKS][TASK_STK_SIZE]; // Tasks stacks

OS_STK

TaskStk2[N_TASKS][TASK_STK_SIZE];

OS_STK

TaskStartStk[TASK_STK_SIZE];

OS_STK

TaskConStk[TASK_STK_SIZE];

INT8U TaskData1[N_TASKS];

// Size of each task's stacks (# of


// Number of identical tasks

// Startup task stack

// Parameters to pass to each task

INT8U TaskData2[N_TASKS];
INT8U err;
INT8U i;

OS_EVENT *q1;

// 2 queue

OS_EVENT *q2;

OS_CPU_SR cpu_sr;

void

*Msg1[6];

void

*Msg2[6];

static

void

Task1(void *pdata);

static

void

Task2(void *pdata);

// 2 arry for msg

// Function prototypes of tasks

static
task

void

TaskStart(void *pdata);

// Function prototypes of Startup

static
task

void

TaskCon(void *pdata);

//Function prototypes of Control

static
task

void

TaskStartCreateTasks(void);

// Function prototypes of creat

void main (void)


{
err=err;
BSP_IntDisAll();

// Install uC/OS-II's running hardware environment

BSP_Init();
OSInit();

// Initialize uC/OS-II

q1 = OSQCreate(&Msg1[0],6);
is the capacity

// set a queue , 6

q2 = OSQCreate(&Msg2[0],6);
is the capacity

// set a queue , 6

OSTaskCreate(TaskStart,(void *)0, &TaskStartStk[TASK_STK_SIZE - 1], 0);


OSTaskCreate(TaskCon, (void *)0, &TaskConStk[TASK_STK_SIZE - 1],i+4);
OSStart();
multitasking

// Start

void TaskStart(void *pdata)


{
#if OS_CRITICAL_METHOD == 3
cpu_sr = cpu_sr;

// Allocate storage for CPU status register


// Avoid warnings

#endif

pdata = pdata;
warning

// Prevent compiler

OSTmr_Init();
OSStatInit();
TaskStartCreateTasks();

// Create all the application tasks

OSTaskSuspend(OS_PRIO_SELF);

// Suspend the TaskStart

OSTimeDlyHMSM(0, 0, 10, 0);


}

void TaskStartCreateTasks (void)


{
for (i = 0; i <N_TASKS; i++)

// Create N_TASKS identical tasks

{
TaskData1[i] = i;

// Each task will pass its own id

OSTaskCreate(Task1, (void *)&TaskData1[i], &TaskStk1[i][TASK_STK_SIZE - 1],


i+1);
}

for (i = 0; i <N_TASKS; i++)

// Create N_TASKS identical tasks

{
TaskData2[i] = i;

// Each task will pass its own id

OSTaskCreate(Task2, (void *)&TaskData2[i], &TaskStk2[i][TASK_STK_SIZE - 1],


i+4);
}
}

void Task1 (void *pdata)


{
INT8U err;
INT8U id;
void *mg;
id=*(int *)pdata;

for (;;)
{
OSTimeDlyHMSM(0, 0, 0,500);

/* Wait 2 second

mg=OSQPend(q1,0,&err);
message
*/

*/
/* apply for

switch(err)
{
case OS_NO_ERR:
/* If it is normally, just print the string.*/
printf("\n\r TASK_%d GOT --> %s\n\r",id,(char *)mg);
OSTimeDlyHMSM(0, 0, 0, 400*(4-id));
break;
default :
/* If the queue is empty or has been deleted, print another
string.*/
printf("\n\r QUEUE_ONE %d is EMPTY!\n\r",id);
OSTimeDlyHMSM(0, 0, 0, 400*(4-id));
break;

}
}
}

void Task2 (void *pdata)


{
INT8U err;
INT8U id;
void *mg;

id=*(int *)pdata;

for (;;)
{
OSTimeDlyHMSM(0, 0, 2, 0);

/* Wait 2 second

mg=OSQPend(q2,0,&err);

/* apply for message

*/
*/
switch(err)
{
case OS_NO_ERR:
/* If it is normally, just print the string.*/
printf("\n\r TASK_%d GOT --> %s\n\r",id+3,(char *)mg);
OSTimeDlyHMSM(0, 0, 0, 400*(4-id));
break;

default :

/* If the queue is empty or has been deleted, print another


string.*/
printf("\n\r QUEUE2 is EMPTY, %d CANNOT get message!\n\r",id+3);
OSTimeDlyHMSM(0, 0, 0, 400*(4-id));
break;
}
}
}

void TaskCon (void *pdata)


{
INT8U i,j;
INT8U err;
INT8U note=1;
/* for flush the queue

*/

INT16U del=3;
/* for delete the queue

*/

OS_EVENT *q;
OS_Q_DATA *data = 0;

static char
*s[]={"message0","message1","message2","message3","message4","message5"};
/* queue1's message
*/

static char
*t[]={"messageA","messageB","messageC","messageD","messageE","messageF"};
/* queue2's message
*/

pdata=pdata;

for (;;)
{
printf("\n.................ADD MESSAGE TO QUEUE_1....................\n\r");

for( i = 0 ; i< 6; i++ )


{
err = OSQPostFront(q1,(void*)s[i]);

/* post message to q1 LIFO*/

switch(err)
{
case OS_NO_ERR:
printf("\n\r QUEUE1 %d add--> %s\n\r",i,s[i]);
OSTimeDlyHMSM(0, 0, 0, 500);
break;
case OS_Q_FULL:
printf("\n\r QUEUE1 is FULL, CANNOT add.\n\r");
OSTimeDlyHMSM(0, 0, 0, 500);
break;
default: break;
}
}

if(del>= 0)
{
printf("\n.................ADD MESSAGE TO QUEUE_2...................\n\r");

for( j = 0;j<6; j++ )


{
q2 FIFO

err = OSQPost(q2,(void*)t[j]);
*/

/* post message to

switch(err)
{
case OS_NO_ERR:
printf("\n\r QUEUE2 %d add--> %s\n\r",j,t[j]);
OSTimeDlyHMSM(0, 0, 0, 500);
break;
case OS_Q_FULL:
printf("\n\r QUEUE2 is FULL, CANNOT add. \n\r");
OSTimeDlyHMSM(0, 0, 0, 500);
break;
default: break;
}
}

if(del>=0)
{
if(note==1)
{
OSQFlush(q2);
printf("\n..................ADD MESSAGE TO QUEUE_2....................\n\r");
printf("\n.....................CLEAR UP QUEUE_2.......................\n\r");

OSTimeDlyHMSM(0,0,0,500);
note=0;
}

else
{
note=1;
}
}

about q2

err=OSQQuery(q2,data);
*/

/* get the information

if(err==OS_NO_ERR)
{
printf("\n\r");
printf("\n\r QUEUE2'information:\n\r");
OSTimeDly(100);
printf("NextMsg:\t%s\n\rNumMsg:\t%d\n\rQSize:\t%d\n\r",(char *)data>OSMsg,data->OSNMsgs,data->OSQSize);
OSTimeDly(100);
printf("\n\r");
}

/* print the information about q2 */

OSTimeDlyHMSM(0, 0, 0,500);

if(del==0)

/* Wait 500 minisecond

*/

{
q=OSQDel(q2,OS_DEL_ALWAYS,&err);

/* delete the q2 */

OSTimeDlyHMSM(0, 0, 0,500);

if(q==(OS_EVENT *)0)
{
printf("\n\r DELETE QUEUE2 OK!\n\r");
}
}

else
{
del--;
printf("\n\r DELETE QUEUE2 FAILED!\n\r");
}
}
}