450 likes | 1.04k Views
RTOS Programming Tools: MicroC/OS-II and VxWorks Lesson-4: MicroC/OS-II (MUCOS) Mailbox and Queue Functions. 1. Mailbox Function.
E N D
RTOS Programming Tools: MicroC/OS-II and VxWorks Lesson-4: MicroC/OS-II (MUCOS) Mailbox and Queue Functions
1. Mailbox Function • Used to communicate a pointer for information. MUCOS permits one message-pointer per mailbox. At the pointer, there can be a string or data structure of no size limit.
Assume a pointer to the mailbox = *mboxMsg, • Pointer to the message event, *MsgPointer (for retrieving the message itself).
OSMboxCreate (*mboxMsg) OS_Event *OSMboxCreate (void *mboxMsg) To create and initialise a mailbox message pointer for the ECB of a mailbox message. (Examples 10.19 Steps 6 and 8)
void *OSMboxPend • (OS_Event *mboxMsg, unsigned short timeOut, unsigned byte *MboxErr)- To check if mailbox message pending (available) then message pointer is read and mailbox emptied * mboxMsg again points to NULL). If message is not available (*mboxMsg points to NULL) it waits, suspends the task, and blocks further running (Example 10.19 Step 46)
void *OSMboxAccept (OS_EVENT * mboxMsg) - To check if mailbox message at the *MsgPointer, is available at *mboxMsg. Unlike OSMboxPend function, it does not block (suspend) the task if message is not available. If available, it returns the pointer (Example 10.19 Step 52)
unsigned byte OSMboxPost (OS_EVENT *mboxMsg, void *MsgPointer) - Sends a message of task at address MsgPointer by posting the address pointer to the mboxMsg. Context switch to that task or any another task if of higher priority will also occur. If box is full, then the message is not placed and error sent. (Example 10.19 steps 24 and 37)
unsigned byte OSMboxQuery (OS_EVENT *mboxMsg, OS_MBOX_DATA *mboxData) - To get mailbox error information, OS_NO_ERR and OS_ERR_EVENT_TYPE at a data structure To get mailbox error information, OS_NO_ERR and OS_ERR_EVENT_TYPE at a data structure
unsigned byte OSMboxQuery (OS_EVENT *mboxMsg, OS_MBOX_DATA *mboxData) - To get mailbox error information, OS_NO_ERR and OS_ERR_EVENT_TYPE at a data structure To get mailbox error information, OS_NO_ERR and OS_ERR_EVENT_TYPE at a data structure
Programming Example for communication a message to display task after delivering the chocolate in chocolate vending machine Chapter-10 L4: "Embedded Systems - Architecture, Programming and Design", Raj Kamal, Publs.: McGraw-Hill, Inc.
Step A: Initiating the Mailbox Chapter-10 L4: "Embedded Systems - Architecture, Programming and Design", Raj Kamal, Publs.: McGraw-Hill, Inc.
#define OS_MAX_EVENTS 8 /*When total number of IPCs needed in an application = 8*/ #define OS_MboX_EN 1 /*When the use of semaphores is contemplated */ Chapter-10 L4: "Embedded Systems - Architecture, Programming and Design", Raj Kamal, Publs.: McGraw-Hill, Inc.
Step B: Global IPC functions and their parameters declarations Chapter-10 L4: "Embedded Systems - Architecture, Programming and Design", Raj Kamal, Publs.: McGraw-Hill, Inc.
OS_EVENT *mboxStrMsgthanks; /* When mail box is to be used to send an amount message */ int *i = 0; char [ ] mboxStrMsgthanks; mboxStrMsgthanks = OSMboxCreate (*i); /* When mail box is to be used the initially it is to point to null */ Chapter-10 L4: "Embedded Systems - Architecture, Programming and Design", Raj Kamal, Publs.: McGraw-Hill, Inc.
Step C: Main function Chapter-10 L4: "Embedded Systems - Architecture, Programming and Design", Raj Kamal, Publs.: McGraw-Hill, Inc.
void main (void) { OSInit (); /* Create First task */ OSTaskCreate (FirstTask, void (*) 0,(void *)&FirstTaskStack[ FirstTaskStackSize], FirstTaskPriority); OSStart ( ); } Chapter-10 L4: "Embedded Systems - Architecture, Programming and Design", Raj Kamal, Publs.: McGraw-Hill, Inc.
Step D: First task Chapter-10 L4: "Embedded Systems - Architecture, Programming and Design", Raj Kamal, Publs.: McGraw-Hill, Inc.
static void FirstTask (void *taskPointer) { /*Create Application Tasks*/ OSTaskCreate (ReadTask, void (*) 0,(void *)&ReadTaskStack [ReadTaskStackSize], ReadTaskPriority); OSTaskCreate (DeliveryTask, void (*) 0,(void *)&DeliveryTaskStack [DeliveryTaskStackSize], DeliveryTaskPriority); OSTaskCreate (DisplayTask, void (*) 0,(void *)&DisplayTaskStack [DisplayTaskStackSize], DisplayTaskPriority); Chapter-10 L4: "Embedded Systems - Architecture, Programming and Design", Raj Kamal, Publs.: McGraw-Hill, Inc.
/*System clock time set */ OSTimeSet (presetTime); OSTickInit (); /* Initiate system timer ticking*/ /* Create apllication related highest prio task */. . while (1) { OSTaskSuspend (FirstTaskPriority); } Chapter-10 L4: "Embedded Systems - Architecture, Programming and Design", Raj Kamal, Publs.: McGraw-Hill, Inc.
(v) Read task sending the amount value after collecting the coins for permitting the reset the amount Chapter-10 L4: "Embedded Systems - Architecture, Programming and Design", Raj Kamal, Publs.: McGraw-Hill, Inc.
static void ReadTask (void *taskPointer) {... int *amount; while (1) {...; /* Code similar to one given in earlier lessons*/ ...; ...; OSTimeDelay (3000); }; } Chapter-10 L4: "Embedded Systems - Architecture, Programming and Design", Raj Kamal, Publs.: McGraw-Hill, Inc.
(vi) Delivery task taking the requires semaphores Chapter-10 L4: "Embedded Systems - Architecture, Programming and Design", Raj Kamal, Publs.: McGraw-Hill, Inc.
static void DeliveryTask (void *taskPointer) {.. while (1) { ...; /* Code similar to one given in earlier lessons*/ ...; ...; mboxStrMsgthanks = “Collect Nice Chocolate Thansk for your visit, Visit Again”; OSMboxPost (mboxStrMsgthanks);/* Post the message into the mailbox*/ OSTimeDelay (3000); OSTimeDlyResume (ReadTaskPriority); };} Chapter-10 L4: "Embedded Systems - Architecture, Programming and Design", Raj Kamal, Publs.: McGraw-Hill, Inc.
(vi) Display task pending for taking the mailbox thanks message Chapter-10 L4: "Embedded Systems - Architecture, Programming and Design", Raj Kamal, Publs.: McGraw-Hill, Inc.
static void DisplayTask (void *taskPointer) {.. String displayThanks;; while (1) { displayThanks = OSMboxPend (mboxStrMsgthanks, 0, *MboxErr); /* Wait for mailbox message, read mboxStrMsgthanks in displayThanks and then reset the mboxStrMsgthanks to null*/ ...; ...; ...; OSTimeDlyResume (DeliveryTaskPriority); }} Chapter-10 L4: "Embedded Systems - Architecture, Programming and Design", Raj Kamal, Publs.: McGraw-Hill, Inc.
2. Queue Functions • The message pointers post into a queue by the tasks either at the back as in a queue or at the front as in a stack. A task can thus insert a given message for deleting either in the first in first out (FIFO) mode or in priority mode for priority message.
Assume pointer, **Qtop, to a queue of message pointers and • Assume two pointers, *QfrontPointer and *QbackPointer, which send and retrieve, respectively, the message pointer for the message.
OSQCreate (**QTop, qSize) • OS_Event OSQCreate (void **QTop, unsigned byte qSize) OS creates a Queue ECB. This creates and initialises an array of pointers for the queue at QTop .
Queue can be of maximum size = qSize. QTop should point to top (0th element of an array). ECB points at the QMsgPointer. (Example 20 Step 8)
unsigned byte OSQPost (OS_EVENT*QMsgPointer, void *QMsg) -Sends a pointer of the message QMsg to the QMsgPointer at the queue back. The message is at a queue back pointer in the ECB (Example 10.20 step 21)
unsigned byte OSQPostFront (OS_EVENT *QMsgPointer, void * QMsg) - Sends QMsg pointer to the QMsgPointer at the queue. It points to the queue front pointer in the ECB where pointer for QMsg now stores pushing other message-pointers back. (Example 10.20 step 30)
void *OSQPend (OS_Event *QMsgPointer, unsigned short timeOut, unsigned byte *Qerr) - The message pointer points to the queue front (head) at the ECB for the queue defined by QMsgPointer. It suspends the task if no message is pending (until either message received or wait period, passed by argument timeOut finishes after timeOut ticks of RTOS timer). (Example 10.20 Step 39)
unsigned byte *OSQFlush (OS_EVENT *QMsgPointer) - To eliminate all the messages in the queue that have been sent. This function checks if a queue has a message pending at QMsgPointer.
unsigned byte OSQQuery (OS_EVENT *QMsgPointer, OS_Q_DATA *QData) -To get queue message information and error information. To get mailbox error information, OS_NO_ERR and OS_ERR_EVENT_TYPE at a data structure
(i) OS_NO_ERR, when querying succeeds; or (ii) OS_ERR_EVENT_TYPE, if *QMsgPointer is not pointing to queue message. To get mailbox error information, OS_NO_ERR and OS_ERR_EVENT_TYPE at a data structure
(vi) MUCOS has mailbox functions and a simple feature of mailbox has one message pointer per mailbox. There can be any number of messages or bytes, provided the same pointer accesses them.
We learnt (vii) MUCOS has queue functions. A queue receives from a sender task an array of message pointers. Message pointers insertion can be such that later on it can retrieve in FIFO (first-in first-out) mode as well as priority mode from a queue
End of Lesson-4 on MicroC/OS-II (MUCOS) Mailbox and Queue Functions