📄 消息队列.c
字号:
/***************************************************************
* 声明:
* 本程序只具备演示功能,不能保证适用于您的真实应用。如需使用,请根据
* 您的实际需要修改本程序。
*******************************************************************
* 电子科技大学嵌入式软件工程中心 版权所有
*
* Copyright (C) 2006 UESTC ESEC
**************************************************************/
/**************************************************************
* 模块: init.c
*
* 目的:
* 这个程序演示基本的消息队列。程序启动设计了6个普通应用任务以及
一个控制任务TAC,等待消息的任务总是按照优先级的高低来决定获得消息的顺
序,实现消息队列的使用。
**************************************************************/
/***********************************************************************************************************
* uC/OS-II
* The Real-Time Kernel
*
*
************************************************************************************************************
*/
#include <stdio.h>
#include "/host/ide/workspace/ucos/src/includes.h"
/*
*********************************************************************************************************
* CONSTANTS
*********************************************************************************************************
*/
#define TASK_STK_SIZE 512 /* Size of each task's stacks (# of WORDs) */
#define N_TASKS 3 /* Number of tasks which wait for queue1 */
/*
*********************************************************************************************************
* VARIABLES
*********************************************************************************************************
*/
OS_STK TaskStk1[N_TASKS][TASK_STK_SIZE]; /* Tasks stacks */
OS_STK TaskStk2[N_TASKS][TASK_STK_SIZE];
OS_STK TaskStartStk[TASK_STK_SIZE]; /* Startup task stack */
OS_STK TaskConStk[TASK_STK_SIZE];
INT8U TaskData1[N_TASKS]; /* Parameters to pass to each task */
INT8U TaskData2[N_TASKS];
OS_EVENT *q1; /* 2 queue */
OS_EVENT *q2;
void *Msg1[6]; /* 2 arry for msg */
void *Msg2[6];
/*
*********************************************************************************************************
* FUNCTION PROTOTYPES
*********************************************************************************************************
*/
void Taskq1(void *pdata); /* Function prototypes of taskq1s */
void Taskq2(void *pdata); /* Function prototypes of taskq2s */
void TaskStart(void *pdata); /* Function prototypes of Startup task */
void TaskCon(void *pdata); /* Function prototypes of Control task */
static void TaskStartCreateTasks(void);
/*
*********************************************************************************************************
* MAIN
*********************************************************************************************************
*/
extern void main(void);
/*
*
* *********************************************************************************************************
*
*/
void boot_card()
{
ucBsp_init(); /* Initializa ucBsp */
main();
}
extern void OSCpuInit();
extern void OS_Sched(void);
void main (void)
{
INT8U err; /* for setting the mutex */
OSCpuInit(); /* Install uC/OS-II's context switch vector */
OSInit(); /* Initialize uC/OS-II */
printk("Welcome to ucos-II\n");
q1 = OSQCreate(&Msg1[0],6); /* set a queue , 6 is the capacity */
q2 = OSQCreate(&Msg2[0],6); /* set a queue , 6 is the capacity */
OSTaskCreate(TaskStart, (void *)0, &TaskStartStk[TASK_STK_SIZE - 1], 0);
OSStart(); /* Start multitasking */
}
/*
*********************************************************************************************************
* STARTUP TASK
*********************************************************************************************************
*/
void TaskStart (void *pdata)
{
int i;
pdata = pdata; /* Prevent compiler warning */
ucos_x86_idt_set_handler(0x20,(void *)OSTickISR,0x8e00); /* Install uC/OS-II's clock tick ISR */
ucos_timer_init(); /* Reprogram tick rate */
TaskStartCreateTasks(); /* Create all the application tasks */
OSTaskSuspend(OS_PRIO_SELF); /* Suspend the TaskStart */
}
/*
***************************************************************************************************************
* CREATE TASKS
***************************************************************************************************************
*/
void TaskStartCreateTasks (void)
{
INT8U i;
for (i = 0; i <N_TASKS; i++) /* Create N_TASKS identical tasks */
{
TaskData1[i] = i; /* Each task will pass its own id */
OSTaskCreate(Taskq1, (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(Taskq2, (void *)&TaskData2[i], &TaskStk2[i][TASK_STK_SIZE - 1], i+4);
}
OSTaskCreate(TaskCon, (void *)0, &TaskConStk[TASK_STK_SIZE - 1],i+4);
/* Create control tasks */
}
/*
*********************************************************************************************************
* TASKq1
*********************************************************************************************************
*/
void Taskq1 (void *pdata)
{
INT8U err;
INT8U id;
char s[30];
void *mg;
id=*(int *)pdata;
for (;;)
{
OSTimeDlyHMSM(0, 0, 2, 0); /* Wait 2 second */
mg=OSQPend(q1,0,&err); /* apply for message */
switch(err)
{
case OS_NO_ERR:
{
printk(" task %d has got the %s \n",id,(char *)mg);
OSTimeDlyHMSM(0, 0, 0, 200*(4-id));
break;
} /* If it is normally, just print the string.*/
default :
{
printk( "queue1 %d is empty. \n",id);
OSTimeDlyHMSM(0, 0, 0, 200*(4-id));
break;
} /* If the queue is empty or has been deleted, print another string.*/
}
}
}
/*
*********************************************************************************************************
* TASKq2
*********************************************************************************************************
*/
void Taskq2 (void *pdata)
{
INT8U err;
INT8U id;
char s[30];
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:
{
printk(" task %d has got the %s. \n", id+3, (char *)mg);
OSTimeDlyHMSM(0, 0, 0, 200*(4-id));
break;
} /* If it is normally, just print the string.*/
default :
{
printk( "queue2 is empty,%d can't got the message. \n",id+3);
OSTimeDlyHMSM(0, 0, 0, 200*(4-id));
break;
} /* If the queue is empty or has been deleted, print another string.*/
}
}
}
/*
*********************************************************************************************************
* TASKcon
*********************************************************************************************************
*/
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;
char ch[50];
OS_Q_DATA *data;
static char *s[]={ /* queue1's message */
"message0","message1","message2","message3","message4","message5"
};
static char *t[]={ /* queue2's message */
"messageA","messageB","messageC","messageD","messageE","messageF"
};
pdata=pdata;
for (;;)
{
printk("\n-------------Add message to queue1-------------\n");
for( i = 0 ; i < 6 ; i++ )
{
err = OSQPostFront(q1,(void*)s[i]); /* post message to q1 LIFO */
switch(err)
{
case OS_NO_ERR:
{
printk("the queue1 %d add %s\n",i,s[i]);
OSTimeDlyHMSM(0, 0, 0, 150);
break;
}
case OS_Q_FULL:
{
printk("the queue1 is full, don't add.\n");
OSTimeDlyHMSM(0, 0, 0, 150);
break;
}
default :break;
}
}
if(del>=0)
{
printk("\n-------------Add message to queue2-------------\n");
}
for( j = 0 ; j < 6 ; j++ )
{
err = OSQPost(q2,(void*)t[j]); /* post message to q2 FIFO */
switch(err)
{
case OS_NO_ERR:
{
printk("the queue2 %d add %s\n",j,t[j]);
OSTimeDlyHMSM(0, 0, 0, 150);
break;
}
case OS_Q_FULL:
{
printk("the queue2 is full, don't add. \n");
OSTimeDlyHMSM(0, 0, 0, 150);
break;
}
default :break;
}
}
if(del>=0)
{
if(note==1)
{
OSQFlush(q2);
printk("\n--------------clear up the queue2--------------\n"); /* clear up the queue 2. */
note=0;
}
else
note=1;
}
err=OSQQuery(q2,data); /* get the information about q2 */
if(err==OS_NO_ERR)
{
printk("\n------------the queue2'information------------\n");
printk(" NextMsg:%s, NumMsg:%d, QSize:%d.\n",(char *)data->OSMsg,data->OSNMsgs,data->OSQSize);
printk("----------------------------------------------\n");
} /* print the information about q2 */
OSTimeDlyHMSM(0, 0, 0, 500); /* Wait 500 minisecond */
if(del==0)
{
q=OSQDel(q2,OS_DEL_ALWAYS,&err); /* delete the q2 */
if(q==(OS_EVENT *)0)
{
printk(" already successful delete queue2 \n");
}
}
else
{
del--;
printk( " not successful delete queue2 \n");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -