📄 mailbox.c
字号:
#include "includes.h"
#define TASK_STK_SIZE 256 /* 任务栈的大小 */
typedef char MESSAGE; /* 邮箱消息类型为单个字符 */
OS_STK TaskStk[TASK_STK_SIZE]; /* 初始任务栈 */
OS_STK SendStk[TASK_STK_SIZE]; /* 发消息任务 */
OS_STK PendStk[TASK_STK_SIZE]; /* 收消息任务 */
/* 邮箱 */
OS_EVENT *Mailbox;
static void TaskStart(void *data);
static void MboxSend(void *data);
static void MboxPend(void *data);
static void MboxQuery(); /* 邮箱内容查询 */
void main (void)
{
PC_DispClrScr(DISP_FGND_WHITE + DISP_BGND_BLACK); /* 清除屏幕 */
/********************** 显示相关信息 ***********************************/
PC_DispStr(30, 0, "uC/OS-II, The Real-Time Kernel", DISP_FGND_WHITE + DISP_BGND_RED + DISP_BLINK);
PC_DispStr(38, 2, "MailBox Example", DISP_FGND_WHITE);
PC_DispStr(1, 6, "<- Press S to Send A Mail Message ->", DISP_FGND_WHITE);
PC_DispStr(1, 7, "<- Press P to Pend A Mail Message ->", DISP_FGND_WHITE);
PC_DispStr(26, 26, "<-PRESS 'ESC' TO QUIT->", DISP_FGND_WHITE);
/***********************************************************************/
OSInit(); /* 初始化 uC/OS-II */
PC_DOSSaveReturn(); /* 保存返回Dos的环境 */
PC_VectSet(uCOS, OSCtxSw); /* 安装上下文切换向量 */
OSTaskCreate(TaskStart, (void *)0, &TaskStk[TASK_STK_SIZE - 1], 10); /* 开始初始化任务 */
OSStart();
}
void TaskStart (void *pdata)
{
#if OS_CRITICAL_METHOD == 3 /* 分配CPU状态寄存器空间 */
OS_CPU_SR cpu_sr;
#endif
INT16S key;
OS_ENTER_CRITICAL();
PC_VectSet(0x08, OSTickISR); /* 设置时钟 */
PC_SetTickRate(OS_TICKS_PER_SEC);
OS_EXIT_CRITICAL();
/* 创建邮箱 */
Mailbox = OSMboxCreate((void *) 0);
OSTaskCreate(MboxSend, 0, &SendStk[TASK_STK_SIZE - 1], 0); /* 创建发送邮箱消息任务 */
OSTaskCreate(MboxPend, 0, &PendStk[TASK_STK_SIZE - 1], 1); /* 创建收取邮箱消息任务 */
OSTimeDlyHMSM(0, 10, 0, 0); /* 挂起任务 */
}
static void MboxSend (void *data)
{
MESSAGE msg; /* 消息 */
INT16S key; /* 按键 */
for (;;) {
if (PC_GetKey(&key) == TRUE) {
if (key == 's'||key == 'S'){ /* 按下s或者S键 */
msg='Z'-random(25); /* 生成随机消息字符 */
OSMboxPost(Mailbox, &msg); /* 向邮箱发送一个消息 */
PC_DispStr(10, 14, "Send Mail Message:", DISP_FGND_WHITE);
PC_DispChar(30, 14,msg, DISP_FGND_YELLOW);
MboxQuery (); /* 查询邮箱的内容 */
}
if (key == 0x1B) { /* ESC键退出 */
PC_DOSReturn();
}
}
OSTimeDlyHMSM(0, 0, 0, 100); /* 挂起100毫秒,让出处理机给收消息任务 */
}
}
static void MboxPend (void *data)
{
MESSAGE msg;
INT8U err; /* 收取消息返回状态 */
INT16S key;
for (;;) {
if (PC_GetKey(&key) == TRUE) {
if (key == 'p'||key == 'P'){ /* 按 p 或者 P 从邮箱中收取消息 */
msg = *(MESSAGE*)OSMboxPend(Mailbox, 0, &err);
PC_DispStr(10,15, "Pend Mail Message:", DISP_FGND_WHITE);
PC_DispChar(30,15, msg, DISP_FGND_YELLOW);
MboxQuery (); /*查询邮箱内容 */
}
if (key == 0x1B) { /* ESC退出 */
PC_DOSReturn();
}
}
OSTimeDlyHMSM(0, 0, 0, 100); /* 挂起100毫秒 */
}
}
static void MboxQuery ()
{
OS_MBOX_DATA *pdata; /* 邮箱内容数据结构 */
MESSAGE *pmsg; /* 邮箱消息 */
INT8U qr=OSMboxQuery(Mailbox,pdata); /* 查询邮箱 */
PC_DispClrRow(10,DISP_BGND_BLACK); /* 删除邮箱内消息显示行 */
if(qr==OS_NO_ERR){ /* 查询成功返回 */
pmsg=pdata->OSMsg; /* 获得邮箱中的消息 */
if(pmsg!=NULL){ /* 显示查询结果 */
PC_DispStr(20, 10, "MAILBOX CONTENT:", DISP_FGND_WHITE);
PC_DispChar(40, 10,*pmsg, DISP_FGND_YELLOW);
}else{
PC_DispStr(20, 10, "MAILBOX IS EMPTY.", DISP_FGND_YELLOW);
}
}else{
PC_DispStr(20, 10, "MAILBOX Query Error!", DISP_FGND_RED);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -