Message Mail is an important tool in the process communication. In this example, use a POST task to send messages (one character, from A-Z), receive messages with a pend. Note that the send task in this example is sent each clock cycle, and the receiving task receives once every 3 clock cycles. So, there must be some characters that cannot be received.
Since the message mailbox (the message queue of length 1) is a special case of the message queue, this will also be experimentally experiment in the subsequent message queue program, see if there is a character loss.
/ ************************************************** *********************************************************** ******** UC / OS-II * The real-time kernel ** (c) Copyright 2004, Yuan Fei, Hefei University of Technology * All Rights reserved * *** filename: Test.c * Programmer ( Siya fei * description: this file illustrate the usage of message mailbox. ****************************************************** *********************************************************** *********************** /
#include "incrudes.h" / *************************************************** *********************************************************** ************** TYPE ****************************************************** *********************************************************** ********************* /
Typedef char message; / * message is a single character * /
/ ************************************************** *********************************************************** *******% constants ************************************************** *********************************************************** *************** /
# s z _ t e e _ _ e i i i i i i i i i i i i i i i i i *********************************************************** *********************************** VARIABLES ************ *********************************************************** *************************************************** /
Os_stk taskstk [n_tasks] [task_stk_size]; / * tasks stacks * / os_stk taskstartstk [task_stk_size];
OS_EVENT * MAILBOX;
/ ************************************************** *********************************************************** ******** Function protoypes ***************************************************** *********************************************************** **************** /
Void TaskStart; / * function prototypes of startup task * / static void mboxpend (void * data); / * Wait for characters * / static void mboxpost (void * data); / * Send characters Processes * /
/ ************************************************** *********************************************************** ************************************************** *********************************************************** *************** /
Void main (void) {pc_dispclrscr (DISP_FGND_WHITE DISP_BGND_BLACK); / * Clear the screen * / osinit (); / * Initialize UC / OS-II * /
PC_DOSSaveReturn (); / * Save environment to return to DOS * / PC_VectSet (uCOS, OSCtxSw); / * Install uC / OS-II's context switch vector * / OSTaskCreate (TaskStart, (void *) 0, & TaskStartStk [TASK_STK_SIZE - 1] , 0); OsStart (); / * start multitasking * /}
/ ************************************************** *********************************************************** ******** Startup Task ********************************************************** *********************************************************** **************** / VOID TASKSTART (VOID * PDATA) {# if os_critical_method == 3 / * Allocate Storage for CPU Status Register * / OS_CPU_SR CPU_SR; #ENDIF INT16S Key;
PDATA = PDATA; / * Prevent Compiler Warning * /
OS_ENTER_CRITICAL (); PC_VectSet (0x08, OSTickISR); / * Install uC / OS-II's clock tick ISR * / PC_SetTickRate (OS_TICKS_PER_SEC); / * Reprogram tick rate * / OS_EXIT_CRITICAL (); Mailbox = OSMboxCreate ((void *) 0) ;
Orstaskcreate (MboxPost, 0, & Taskstk [0] [Task_stk_size - 1], 2); OSTASKCREATE (Mboxpend, 0, & Taskstk [1] [Task_Stk_size - 1], 3);
For (;;) {if (Pc_GetKey (& Key) == true) {/ * see ife (key == 0x1b) {/ * yes, see t it's escape key * / pc_DOSRETURN () ; / * Return to dos * /}}}}} }timedlyhmsm (0, 0, 1, 0); / * Wait One Second * /}}
Static void mboxpost (void * pdata) {message msg; for (msg = 'a'; msg <= 'z'; msg ) {OSMBoxPost (Mailbox, & MSG); ostimedly (1); if (MSG == 'Z' ) {Msg = 'a';}}}
Static void mboxpend (void * pdata) {Message Msg; INT16U i; int8u err; for (i = 0; i) {msg = * (message *) Osmboxpend (Mailbox, 0, & Err);
PC_Dispchar (I% 80, I / 80, MSG, DISP_FGND_WHITE); ostimedly (3); if (i> 2000) {i = 0;}}}