1 / 39

1. Mailbox Function

RTOS Programming Tools: MicroC/OS-II and VxWorks Lesson-4: MicroC/OS-II (MUCOS) Mailbox and Queue Functions. 1. Mailbox Function.

Download Presentation

1. Mailbox Function

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. RTOS Programming Tools: MicroC/OS-II and VxWorks Lesson-4: MicroC/OS-II (MUCOS) Mailbox and Queue Functions

  2. 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.

  3. Assume a pointer to the mailbox = *mboxMsg, • Pointer to the message event, *MsgPointer (for retrieving the message itself).

  4. 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)

  5. 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)

  6. 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)

  7. 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)

  8. 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

  9. 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

  10. 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.

  11. Step A: Initiating the Mailbox Chapter-10 L4: "Embedded Systems - Architecture, Programming and Design", Raj Kamal, Publs.: McGraw-Hill, Inc.

  12. #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.

  13. Step B: Global IPC functions and their parameters declarations Chapter-10 L4: "Embedded Systems - Architecture, Programming and Design", Raj Kamal, Publs.: McGraw-Hill, Inc.

  14. 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.

  15. Step C: Main function Chapter-10 L4: "Embedded Systems - Architecture, Programming and Design", Raj Kamal, Publs.: McGraw-Hill, Inc.

  16. 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.

  17. Step D: First task Chapter-10 L4: "Embedded Systems - Architecture, Programming and Design", Raj Kamal, Publs.: McGraw-Hill, Inc.

  18. 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.

  19. /*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.

  20. (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.

  21. 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.

  22. (vi) Delivery task taking the requires semaphores Chapter-10 L4: "Embedded Systems - Architecture, Programming and Design", Raj Kamal, Publs.: McGraw-Hill, Inc.

  23. 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.

  24. (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.

  25. 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.

  26. 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.

  27. 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.

  28. 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 .

  29. 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)

  30. 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)

  31. 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)

  32. 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)

  33. 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.

  34. 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

  35. (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

  36. Summary

  37. (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.

  38. 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

  39. End of Lesson-4 on MicroC/OS-II (MUCOS) Mailbox and Queue Functions

More Related