📄 mailbox.c
字号:
/*
; ---------------------------------------------------------------------
; File: example1.c
;
; uC/OS Real-time multitasking kernel for the MIPS processor.
;
; Simple example of using multiple tasks and mailboxes.
;
; Created by Marco Graziano (marcog@crl.com).
; ----------------------------------------------------------------------
*/
#include "ucos.h" /* uC/OS interface */
#include "cpu.h"
/* allocate memory for tasks' stacks */
#define STACKSIZE 2048
uint Stack1[STACKSIZE];
uint Stack2[STACKSIZE];
uint Stack3[STACKSIZE];
/* mailbox event control blocks */
OS_EVENT *Mbox1;
OS_EVENT *Mbox2;
OS_EVENT *Mbox3;
char MsgFrom1[] = "From 1";
char MsgFrom2[] = "From 2";
char MsgFrom3[] = "From 3";
/*
; ---------------------------------------------------------------
; Task 1
; Task running at the lowest priority.
; Wait for a message in the Box 1. Post message to Box 2.
; ---------------------------------------------------------------
*/
void
Task1(void *Id)
{
char *Msg;
uint err;
char buf[64];
char *pbuf;
for (;;) {
/* wait for a message from the input mailbox */
Msg = (char *)OSMboxPend(Mbox1, 100, &err);
/* print task's id */
putchar( *(char*)Id );
putchar( ',' );
putstr( Msg );
putchar( ',' );
itoa( OSTimeGet(), buf, 10 );
putstr( buf );
putchar( '\n' );
/* post the input message to the output mailbox */
OSMboxPost(Mbox2, MsgFrom1);
}
}
/*
; ---------------------------------------------------------------
; Task2
; Wait for a message in Box 2. Post message to Box 3.
; ---------------------------------------------------------------
*/
void
Task2(void *Id)
{
char *Msg;
uint err;
for (;;) {
/* wait for a message from the input mailbox */
Msg = (char *)OSMboxPend(Mbox2, 100, &err);
/* print task's id */
putchar( *(char*)Id );
putchar( ',' );
putstr( Msg );
putchar( '\n' );
/* post the input message to the output mailbox */
OSMboxPost(Mbox3, MsgFrom2);
}
}
/*
; ---------------------------------------------------------------
; Task3
; Wait for a message in Box 3. Post message to Box 1.
; ---------------------------------------------------------------
*/
void
Task3(void *Id)
{
char *Msg;
uint err;
for (;;) {
/* wait for a message from the input mailbox */
Msg = (char *)OSMboxPend(Mbox3, 100, &err);
/* print task's id */
putchar( *(char*)Id );
putchar( ',' );
putstr( Msg );
putchar( '\n' );
/* post the input message to the output mailbox */
OSMboxPost(Mbox1, MsgFrom3);
}
}
/*
; ---------------------------------------------------------------
* Main function.
; ---------------------------------------------------------------
*/
int
main(int argc, char **argv)
{
char Id1 = '1';
char Id2 = '2';
char Id3 = '3';
/* needed by uC/OS */
OSInit();
/*
* create the first mailbox in the pipeline with a message
* in it to get the first task started.
*/
Mbox1 = OSMboxCreate( "From 0" );
/* create the remaining mailboxes empty */
Mbox2 = OSMboxCreate((void *)0);
Mbox3 = OSMboxCreate((void *)0);
/*
* create the tasks in uC/OS and assign increasing
* priorities to them so that Task3 at the end of
* the pipeline has the highest priority.
*/
OSTaskCreate(Task1, (void *)&Id1, (void *)&Stack1[STACKSIZE], 1);
OSTaskCreate(Task2, (void *)&Id2, (void *)&Stack2[STACKSIZE], 2);
OSTaskCreate(Task3, (void *)&Id3, (void *)&Stack3[STACKSIZE], 3);
CPUInit();
/* start the os */
OSStart();
/* never reached */
} /* main */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -