更换语言:
   Contact us
  • Follow CooCox
  •         
  • CooCox Market
  •         
  • Tech Support
  •     
Home  ›  CoOS › Intertask Communication

Intertask Communication

     Information transfer is sometimes needed among tasks or between the task and the ISR. Information transfer can be also called intertask communication.

     There are two ways to implement it: through the global variable or by sending messages.

When using the global variable, it is important to ensure that each task or ISR possesses the variable alone. The only way to ensure it is enabling the interrupt. When two tasks share one variable, each task possesses the variable alone through firstly enabling then disabling the interrupt or by the semaphore (see chapter 5.1). Please note that a task can communicate with the ISR only through the global variable and the task won’t know when the global variable has been modified by the ISR (unless the ISR sends signals to the task in manner of semaphore or the task keeps searching the variable’s value). In this case, CooCox CoOS supplies the mailboxes and the message queues to avoid the problems above.

  •  Mailboxes

     System or the user code can send a message by the core services. A typical mail message, also known as the exchange of information, refers to a task or an ISR using a pointer variable, through the core services to put a message (that is, a pointer) into the mailbox. Similarly, one or more tasks can receive this message by the core services. The tasks sending and receiving the message promise that the content that the pointer points to is just that piece of message.

     The mailbox of CooCox CoOS is a typical message mailbox which is composed of two parts: one is the information which expressed by a pointer of void; the other is the waiting list which composed of the tasks waiting for this mailbox. The waiting list supports two kinds of sorting: FIFO and preemptive priority. The sorting mode is determined by the user when creating the mailbox.

     You can create a mailbox by calling CoCreateMbox ( ) in CooCox CoOS. After being created successfully, there won’t be any message inside. You can send a message to the mailbox by calling CoPostMail ( ) or isr_PostMail ( ) respectively in a task or the ISR. You can also get a message from the mailbox by calling CoPendMail ( ) or CoAcceptMail ( ).

Code 6 The use of the mailbox
  • void myTaskA(void* pdata)
    {
           void* pmail;
           StatusType err;
           ..........
           mboxID = CoCreateMbox(EVENT_SORT_TYPE_PRIO); //Sort by preemptive         
           priority pmail = CoPendMail(mboxID,0,&err);   
           ..........
    }
    void myTaskB(void* pdata)
    {
           ......
           CoPostMail(mboxID,"hello,world");
           ......
    }
    void myISR(void)
    {
            CoEnterISR ( );
            ......
           isr_PostMail(mboxID,"hello,CooCox");
           CoExitISR ( );
    }

  •  Message Queues

     Message queue is just an array of mailboxes used to send messages to the task in fact. The task or the ISR can put multiple messages (that is, the pointers of the message) to the message queue through the core services. Similarly, one or more tasks can receive this message by the core services. The tasks sending and receiving the message promise that the content that the pointer points to is just that piece of message.

     The difference between the mailbox and the message queue is that the former can store only one piece of message while the latter can store multiple of it. The maximum pieces of message stored in a queue are determined by the user when creating the queue in CooCox CoOS.

    In CooCox CoOS, message queue is composed of two parts: one is the struct which pointed to the message queue; the other is the waiting list which composed of the tasks waiting for this message queue. The waiting list supports two kinds of sorting: FIFO and preemptive priority. The sorting mode is determined by the user when creating the message queue.

     You can create a message queue by calling CoCreateQueue ( ) in CooCox CoOS. After being created successfully, there won’t be any message inside. You can send a message to the message queue by calling CoPostQueueMail ( ) or isr_PostQueueMaill ( ) respectively in a task or the ISR. Similarly, you can also obtain a message from the message queue by calling CoPendQueueMail ( ) or CoAcceptQueueMail ( ).

Code 7 The use of the message queue
  • void myTaskA(void* pdata)
    {
           void* pmail;
           Void* queue[5];
           StatusType err;
           ..........
           queueID = CoCreateQueue(queue,5,EVENT_SORT_TYPE_PRIO);
           //5 grade, sorting by preemptive priority
           pmail = CoPendQueueMail(queueID ,0,&err);   
           ..........
    }
    void myTaskB(void* pdata)
    {
           ......
           CoPostQueueMail(queueID ,"hello,world");
           ......
    }
    void myISR(void)
    {
           CoEnterISR ( );
           ......
           isr_PostQueueMail(queueID ,"hello,CooCox");
           CoExitISR ( );
    }


CooCox CoOS

© 2009 -2011 CooCox - Terms of Use         Business Model         Market             About us