8.3 Message Buffers
Multitask processing requires the inter-task communication function (message transfer function) that reports the processing result of a task to another task. The RI600PX therefore provides the message buffers for copying and transferring the arbitrary size of message.

The following shows a processing flow when using a message buffer.

Figure 8-3 Processing Flow (Message buffer)



8.3.1 Create message buffer

Message buffers are created by one of the following methods.

1 ) Creation by the system configuration file
The static API message_buffer[] described in the system configuration file creates a message buffer.
Refer to 20.16 Message Buffer Information (message_buffer[]) for the details of message_buffer[].



2 ) Creation by cre_mbf or acre_mbf
The cre_mbf creates a message buffer with message buffer ID indicated by parameter mbfid according to the content of parameter pk_cmbf.
The acre_mbf creates a message buffer according to the content of parameter pk_cmbf, and returns the created message buffer ID.
The information specified is shown below.



- Message buffer attribute (mbfatr)
Only TA_TFIFO (the order of task wait queue for sending is managed by FIFO order.) can be specified for mbfatr.


- Maximum message size (maxmsz)

- Size of the message buffer area (mbfsz), Start address of the message buffer area (mbf)
The message buffer area should be generated to the area other than memory objects and user stacks.


These service calls can be called from tasks that belong to Trusted Domain.
The following describes an example for coding acre_mbf as a representative.


 #include    "kernel.h"              /*Standard header file definition*/
 #include    "kernel_id.h"           /*Header file generated by cfg600px*/
 
 #define MAX_MSGSZ 64                /*Maximum message size (in bytes)*/
 #define MBFSZ     256               /*Size of the message buffer area (in bytes)*/
 
 #pragma section B BRI_RAM           /*Section for the message buffer area*/
 static UW mbf_area[MBFSZ/sizeof(UW)]; /*Message buffer area*/
 #pragma section
 
 #pragma task Task1                  /*Refer to note*/
 void Task1 (VP_INT exinf);          /*Refer to note*/
 void Task1 (VP_INT exinf)
 {
     ER      mbfid;        /*Declares variable*/
     T_CMBF  pk_cmbf = {   /*Declares and initializes variable*/
         TA_TFIFO,            /*Message buffer attribute (mbfatr)*/
         MAX_MSGSZ,           /*Maximum message size (in bytes)(maxmsz)*/
          MBFSZ,                /*Size of the message buffer area (in bytes) (mbfsz)*/
         (VP)mbf_area         /*Start address of the message buffer area (mbf)*/
     };
 
     /* ......... */
 
     mbfid = acre_mbf ( &pk_cmbf );  /*Create message buffer/
 
     /* ......... */
 }


Note These statements are unnecessary for the task which is created by the system configuration file because the cfg600px generates these statement into the "kernel_id.h".

8.3.2 Delete message buffer

- del_mbf
This service call deletes the message buffer specified by parameter mbfid.
When there are waiting tasks for the target message buffer by using snd_mbf, tsnd_mbf, rcv_mbf or trcv_mbf, this service call cancels the WAITING state of the tasks and returns E_DLT as a return value of the snd_mbf, tsnd_mbf, rcv_mbf or trcv_mbf.
This service call can be called from tasks that belong to Trusted Domain.
The following describes an example for coding this service call.





 #include    "kernel.h"              /*Standard header file definition*/
 #include    "kernel_id.h"           /*Header file generated by cfg600px*/
 #pragma task Task1                  /*Refer to note*/
 void Task1 (VP_INT exinf);          /*Refer to note*/
 void Task1 (VP_INT exinf)
 {
     ID      mbfid = 8;          /*Declares and initializes variable*/
 
     /* ......... */
 
     ercd = del_mbf ( mbfid );   /*Delete data queue*/
 
     /* ......... */
 }


Note These statements are unnecessary for the task which is created by the system configuration file because the cfg600px generates these statement into the "kernel_id.h".

8.3.3 Send to message buffer

A message is transmitted by issuing the following service call from the processing program.

- snd_mbf (Wait)

- psnd_mbf, ipsnd_mbf (Polling)

- tsnd_mbf (Wait with time-out)

- snd_mbf (Wait)
This service call processes as follows according to the situation of the message buffer specified by the parameter mbfid.


- There is a task in the reception wait queue.
This service call transfers the message specified by parameter msg to the task in the top of the reception wait queue. As a result, the task is unlinked from the reception wait queue and moves from the WAITING state (message reception wait state) to the READY state, or from the WAITING-SUSPENDED state to the SUSPENDED state.


- There is no task neither in the reception wait queue and transmission wait queue and there is available space in the message buffer.
This service call stores the message specified by parameter msg to the message buffer. As a result, the size of available space in the target message buffer decreases by the amount calculated by the following expression.


The amount of decrease = up4( msgsz ) + VTSZ_MBFTBL

- There is no task neither in the reception wait queue and transmission wait queue and there is no available space in the message buffer, or there is a task in the transmission wait queue.
This service call queues the invoking task to the transmission wait queue of the target message buffer and moves it from the RUNNING state to the WAITING state (message transmission wait state).
The sending WAITING state for a message buffer is cancelled in the following cases.



Sending WAITING State for a Message Buffer Cancel Operation

Return Value

Available space was secured in the message buffer area as a result of issuing rcv_mbf.

E_OK

Available space was secured in the message buffer area as a result of issuing prcv_mbf.

E_OK

Available space was secured in the message buffer area as a result of issuing trcv_mbf.

E_OK

The task at the top of the transmission wait queue was forcedly released from waiting by following either.

- Forced release from waiting (accept rel_wai while waiting).

- Forced release from waiting (accept irel_wai while waiting).

- Forced release from waiting (accept ter_tsk while waiting).

- The time specified by tmout for tsnd_mbf has elapsed.

E_OK

Forced release from waiting (accept rel_wai while waiting).

E_RLWAI

Forced release from waiting (accept irel_wai while waiting).

E_RLWAI

The message buffer is reset as a result of issuing vrst_mbf.

EV_RST

Forced release from waiting (accept del_mbf while waiting).

E_DLT



The following describes an example for coding this service call.

 #include    "kernel.h"              /*Standard header file definition*/
 #include    "kernel_id.h"           /*Header file generated by cfg600px*/
 #pragma task Task1                  /*Refer to note 3*/
 void Task1 (VP_INT exinf);          /*Refer to note 3*/
 void Task1 (VP_INT exinf)
 {
     ER      ercd;                   /*Declares variable*/
     ID      mbfid = 1;              /*Declares and initializes variable*/
     B       msg[] = {1,2,3};        /*Declares and initializes variable*/
     UINT    msgsz = sizeof( msg );  /*Declares and initializes variable*/
 
     /* ......... */
 
     ercd = snd_mbf (mbfid, (VP)msg, msgsz);   /*Send to message buffer*/
 
     if (ercd == E_OK) {
         /* ......... */             /*Normal termination processing*/
     } else if (ercd == E_RLWAI) {
         /* ......... */             /*Forced termination processing*/
     }
 
     /* ......... */
 }


Note 1 Message is written to the message buffer area in the order of the message transmission request.

Note 2 Invoking tasks are queued to the transmission wait queue of the target message buffer in the FIFO order.

Note 3 These statements are unnecessary for the task which is created by the system configuration file because the cfg600px generates these statement into the "kernel_id.h".

- psnd_mbf, ipsnd_mbf (Polling)
This service call processes as follows according to the situation of the message buffer specified by the parameter mbfid.


- There is a task in the reception wait queue.
This service call transfers the message specified by parameter msg to the task in the top of the reception wait queue. As a result, the task is unlinked from the reception wait queue and moves from the WAITING state (message reception wait state) to the READY state, or from the WAITING-SUSPENDED state to the SUSPENDED state.


- There is no task neither in the reception wait queue and transmission wait queue and there is available space in the message buffer.
This service call stores the message specified by parameter msg to the message buffer. As a result, the size of available space in the target message buffer decreases by the amount calculated by the following expression.


The amount of decrease = up4( msgsz ) + VTSZ_MBFTBL

- There is no task neither in the reception wait queue and transmission wait queue and there is no available space in the message buffer, or there is a task in the transmission wait queue.
This service call returns "E_TMOUT".


The following describes an example for coding these service calls.

 #include    "kernel.h"              /*Standard header file definition*/
 #include    "kernel_id.h"           /*Header file generated by cfg600px*/
 #pragma task Task1                  /*Refer to note 2*/
 void Task1 (VP_INT exinf);          /*Refer to note 2*/
 void Task1 (VP_INT exinf)
 {
     ER      ercd;                   /*Declares variable*/
     ID      mbfid = 1;              /*Declares and initializes variable*/
     B       msg[] = {1,2,3};        /*Declares and initializes variable*/
     UINT    msgsz = sizeof( msg );  /*Declares and initializes variable*/
 
     /* ......... */
 
     ercd = psnd_mbf (mbfid, (VP)msg, msgsz); /*Send to message buffer*/
 
     if (ercd == E_OK) {
         /* ......... */             /*Polling success processing*/
     } else if (ercd == E_TMOUT) {
         /* ......... */             /*Polling failure processing*/
     }
 
     /* ......... */
 }


Note 1 Message is written to the message buffer area in the order of the message transmission request.

Note 2 These statements are unnecessary for the task which is created by the system configuration file because the cfg600px generates these statement into the "kernel_id.h".

- tsnd_mbf (Wait with time-out)
This service call processes as follows according to the situation of the message buffer specified by the parameter mbfid.


- There is a task in the reception wait queue.
This service call transfers the message specified by parameter msg to the task in the top of the reception wait queue. As a result, the task is unlinked from the reception wait queue and moves from the WAITING state (message reception wait state) to the READY state, or from the WAITING-SUSPENDED state to the SUSPENDED state.


- There is no task neither in the reception wait queue and transmission wait queue and there is available space in the message buffer.
This service call stores the message specified by parameter msg to the message buffer. As a result, the size of available space in the target message buffer decreases by the amount calculated by the following expression.


The amount of decrease = up4( msgsz ) + VTSZ_MBFTBL

- There is no task neither in the reception wait queue and transmission wait queue and there is no available space in the message buffer, or there is a task in the transmission wait queue.
This service call queues the invoking task to the transmission wait queue of the target message buffer and moves it from the RUNNING state to the WAITING state with time (message transmission wait state).
The sending WAITING state for a message buffer is cancelled in the following cases.



Sending WAITING State for a Message Buffer Cancel Operation

Return Value

Available space was secured in the message buffer area as a result of issuing rcv_mbf.

E_OK

Available space was secured in the message buffer area as a result of issuing prcv_mbf.

E_OK

Available space was secured in the message buffer area as a result of issuing trcv_mbf.

E_OK

The task at the top of the transmission wait queue was forcedly released from waiting by following either.

- Forced release from waiting (accept rel_wai while waiting).

- Forced release from waiting (accept irel_wai while waiting).

- Forced release from waiting (accept ter_tsk while waiting).

- The time specified by tmout for tsnd_mbf has elapsed.

E_OK

Forced release from waiting (accept rel_wai while waiting).

E_RLWAI

Forced release from waiting (accept irel_wai while waiting).

E_RLWAI

The message buffer is reset as a result of issuing vrst_mbf.

EV_RST

The time specified by tmout has elapsed.

E_TMOUT

Forced release from waiting (accept del_mbf while waiting).

E_DLT



The following describes an example for coding this service call.

 #include    "kernel.h"              /*Standard header file definition*/
 #include    "kernel_id.h"           /*Header file generated by cfg600px*/
 #pragma task Task1                  /*Refer to note 4*/
 void Task1 (VP_INT exinf);          /*Refer to note 4*/
 void Task1 (VP_INT exinf)
 {
     ER      ercd;                   /*Declares variable*/
     ID      mbfid = 1;              /*Declares and initializes variable*/
     B       msg[] = {1,2,3};        /*Declares and initializes variable*/
     TMO     tmout = 3600;           /*Declares and initializes variable*/
 
     /* ......... */
 
     ercd = tsnd_mbf (mbfid, (VP)msg, msgsz, tmout); /*Send to message buffer*/
 
     if (ercd == E_OK) {
         /* ......... */             /*Normal termination processing*/
     } else if (ercd == E_RLWAI) {
         /* ......... */             /*Forced termination processing*/
     } else if (ercd == E_TMOUT) {
         /* ......... */             /*Time-out processing*/
     }
 
     /* ......... */
 }


Note 1 Message is written to the message buffer area in the order of the message transmission request.

Note 2 Invoking tasks are queued to the transmission wait queue of the target message buffer in the FIFO order.

Note 3 TMO_FEVR is specified for wait time tmout, processing equivalent to snd_mbf will be executed. When TMO_POL is specified, processing equivalent to psnd_mbf will be executed.

Note 4 These statements are unnecessary for the task which is created by the system configuration file because the cfg600px generates these statement into the "kernel_id.h".

8.3.4 Receive from message buffer

A message is received (waiting forever, polling, or with time-out) by issuing the following service call from the processing program.

- rcv_mbf (Wait)

- prcv_mbf (Polling)

- trcv_mbf (Wait with time-out)

- rcv_mbf (Wait)
This service call processes as follows according to the situation of the message buffer specified by the parameter mbfid.


- There is a message in the message buffer.
This service call takes out the oldest message from the message buffer and stores the message to the area specified by msg and return the size of the message. As a result, the size of available space in the target message buffer increases by the amount calculated by the following expression.


The amount of increase = up4( Return value ) + VTSZ_MBFTBL

In addition, this service call repeats the following processing until task in the transmission wait queue is lost or it becomes impossible to store the message in the message buffer.

- When there is a task in the transmission wait queue and there is available space in the message buffer for the message specified by the task in the top of the transmission wait queue, the task is unlinked from the transmission wait queue and moves from the WAITING state (message transmission wait state) to the READY state, or from the WAITING-SUSPENDED state to the SUSPENDED state. As a result, the size of available space in the target message buffer decreases by the amount calculated by the following expression.

The amount of decrease =up4( The message size sent by the task ) + VTSZ_MBFTBL

- There is no message in the message buffer and there is a task in the transmission wait queue.
This service call stores the message specified by the task in the top of the transmission wait queue to the area pointed by the parameter msg. As a result, the task is unlinked from the transmission wait queue and moves from the WAITING state (message transmission wait state) to the READY state, or from the WAITING-SUSPENDED state to the SUSPENDED state.
Note, this situation is caused only when the size of the message buffer is 0.



- There is no message in the message buffer and there is no task in the transmission wait queue.
This service call queues the invoking task to the reception wait queue of the target message buffer and moves it from the RUNNING state to the WAITING state (message reception wait state).
The receiving WAITING state for a message buffer is cancelled in the following cases.



Receiving WAITING State for a Message Buffer Cancel Operation

Return Value

Message was sent to the message buffer area as a result of issuing snd_mbf.

E_OK

Message was sent to the message buffer area as a result of issuing psnd_mbf.

E_OK

Message was sent to the message buffer area as a result of issuing ipsnd_mbf.

E_OK

Message was sent to the message buffer area as a result of issuing tsnd_mbf.

E_OK

Forced release from waiting (accept rel_wai while waiting).

E_RLWAI

Forced release from waiting (accept irel_wai while waiting).

E_RLWAI

Forced release from waiting (accept del_mbf while waiting).

E_DLT



The following describes an example for coding this service call.

 #include    "kernel.h"              /*Standard header file definition*/
 #include    "kernel_id.h"           /*Header file generated by cfg600px*/
 #pragma task Task1                  /*Refer to note 3*/
 void Task1 (VP_INT exinf);          /*Refer to note 3*/
 void Task1 (VP_INT exinf)
 {
     ER      ercd;                   /*Declares variable*/
     ID      mbfid = 1;              /*Declares and initializes variable*/
     B       msg[16];                /*Declares variable (maximum message size)*/
 
     /* ......... */
 
     ercd = rcv_mbf (mbfid, (VP)msg); /*Receive from message buffer */
 
     if (ercd == E_OK) {
         /* ......... */             /*Normal termination processing*/
     } else if (ercd == E_RLWAI) {
         /* ......... */             /*Forced termination processing*/
     }
 
     /* ......... */
 }


Note 1 The maximum message size is defined at creating the message buffer. The size of the area pointed by msg requires at least the maximum message size.

Note 2 Invoking tasks are queued to the reception wait queue of the target message buffer in the order of the message reception request.

Note 3 These statements are unnecessary for the task which is created by the system configuration file because the cfg600px generates these statement into the "kernel_id.h".

- prcv_mbf (Polling)
This service call processes as follows according to the situation of the message buffer specified by the parameter mbfid.


- There is a message in the message buffer.
This service call takes out the oldest message from the message buffer and stores the message to the area specified by msg and return the size of the message. As a result, the size of available space in the target message buffer increases by the amount calculated by the following expression.


The amount of increase = up4( Return value ) + VTSZ_MBFTBL

In addition, this service call repeats the following processing until task in the transmission wait queue is lost or it becomes impossible to store the message in the message buffer.

- When there is a task in the transmission wait queue and there is available space in the message buffer for the message specified by the task in the top of the transmission wait queue, the task is unlinked from the transmission wait queue and moves from the WAITING state (message transmission wait state) to the READY state, or from the WAITING-SUSPENDED state to the SUSPENDED state. As a result, the size of available space in the target message buffer decreases by the amount calculated by the following expression.

The amount of decrease =up4( The message size sent by the task ) + VTSZ_MBFTBL

- There is no message in the message buffer and there is a task in the transmission wait queue.
This service call stores the message specified by the task in the top of the transmission wait queue to the area pointed by the parameter msg. As a result, the task is unlinked from the transmission wait queue and moves from the WAITING state (message transmission wait state) to the READY state, or from the WAITING-SUSPENDED state to the SUSPENDED state.
Note, this situation is caused only when the size of the message buffer is 0.



- There is no message in the message buffer and there is no task in the transmission wait queue.
This service call returns "E_TMOUT".


The following describes an example for coding these service calls.

 #include    "kernel.h"              /*Standard header file definition*/
 #include    "kernel_id.h"           /*Header file generated by cfg600px*/
 #pragma task Task1                  /*Refer to note 2*/
 void Task1 (VP_INT exinf);          /*Refer to note 2*/
 void Task1 (VP_INT exinf)
 {
     ER      ercd;                   /*Declares variable*/
     ID      mbfid = 1;              /*Declares and initializes variable*/
     B       msg[16];                /*Declares variable (maximum message size)*/
 
     /* ......... */
 
     ercd = prcv_mbf (mbfid, (VP)msg); /*Receive from message buffer */
 
     if (ercd == E_OK) {
         /* ......... */             /*Polling success processing*/
     } else if (ercd == E_TMOUT) {
         /* ......... */             /*Polling failure processing*/
     }
 
     /* ......... */
 }


Note 1 The maximum message size is defined at creating the message buffer. The size of the area pointed by msg requires at least the maximum message size.

Note 2 These statements are unnecessary for the task which is created by the system configuration file because the cfg600px generates these statement into the "kernel_id.h".

- trcv_mbf (Wait with time-out)
This service call processes as follows according to the situation of the message buffer specified by the parameter mbfid.


- There is a message in the message buffer.
This service call takes out the oldest message from the message buffer and stores the message to the area specified by msg and return the size of the message. As a result, the size of available space in the target message buffer increases by the amount calculated by the following expression.


The amount of increase = up4( Return value ) + VTSZ_MBFTBL

In addition, this service call repeats the following processing until task in the transmission wait queue is lost or it becomes impossible to store the message in the message buffer.

- When there is a task in the transmission wait queue and there is available space in the message buffer for the message specified by the task in the top of the transmission wait queue, the task is unlinked from the transmission wait queue and moves from the WAITING state (message transmission wait state) to the READY state, or from the WAITING-SUSPENDED state to the SUSPENDED state. As a result, the size of available space in the target message buffer decreases by the amount calculated by the following expression.

The amount of decrease =up4( The message size sent by the task ) + VTSZ_MBFTBL

- There is no message in the message buffer and there is a task in the transmission wait queue.
This service call stores the message specified by the task in the top of the transmission wait queue to the area pointed by the parameter msg. As a result, the task is unlinked from the transmission wait queue and moves from the WAITING state (message transmission wait state) to the READY state, or from the WAITING-SUSPENDED state to the SUSPENDED state.
Note, this situation is caused only when the size of the message buffer is 0.



- There is no message in the message buffer and there is no task in the transmission wait queue.
This service call queues the invoking task to the reception wait queue of the target message buffer and moves it from the RUNNING state to the WAITING state with time (message reception wait state).
The receiving WAITING state for a message buffer is cancelled in the following cases.



Receiving WAITING State for a Message Buffer Cancel Operation

Return Value

Message was sent to the message buffer area as a result of issuing snd_mbf.

E_OK

Message was sent to the message buffer area as a result of issuing psnd_mbf.

E_OK

Message was sent to the message buffer area as a result of issuing ipsnd_mbf.

E_OK

Message was sent to the message buffer area as a result of issuing tsnd_mbf.

E_OK

Forced release from waiting (accept rel_wai while waiting).

E_RLWAI

Forced release from waiting (accept irel_wai while waiting).

E_RLWAI

The time specified by tmout has elapsed.

E_TMOUT

Forced release from waiting (accept del_mbf while waiting).

E_DLT



The following describes an example for coding this service call.

 #include    "kernel.h"              /*Standard header file definition*/
 #include    "kernel_id.h"           /*Header file generated by cfg600px*/
 #pragma task Task1                  /*Refer to note 4*/
 void Task1 (VP_INT exinf);          /*Refer to note 4*/
 void Task1 (VP_INT exinf)
 {
     ER      ercd;                   /*Declares variable*/
     ID      mbfid = 1;              /*Declares and initializes variable*/
     B       msg[16];                /*Declares variable (maximum message size)*/
     TMO     tmout = 3600;           /*Declares and initializes variable*/
 
     /* ......... */
 
     ercd = trcv_mbf (mbfid, (VP)msg, tmout ); /*Receive from message buffer */
 
     if (ercd == E_OK) {
             /* ......... */         /*Normal termination processing*/
     } else if (ercd == E_RLWAI) {
             /* ......... */         /*Forced termination processing*/
     } else if (ercd == E_TMOUT) {
             /* ......... */         /*Time-out processing*/
     }
 
     /* ......... */
 }


Note 1 The maximum message size is defined at creating the message buffer. The size of the area pointed by msg requires at least the maximum message size.

Note 2 Invoking tasks are queued to the reception wait queue of the target message buffer in the order of the message reception request.

Note 3 TMO_FEVR is specified for wait time tmout, processing equivalent to rcv_mbf will be executed. When TMO_POL is specified, processing equivalent to prcv_mbf will be executed.

Note 4 These statements are unnecessary for the task which is created by the system configuration file because the cfg600px generates these statement into the "kernel_id.h".

8.3.5 Reference message buffer state

A message buffer status is referenced by issuing the following service call from the processing program.

- ref_mbf, iref_mbf
These service calls store the detailed information of the message buffer (existence of waiting tasks, available buffer size, etc.) specified by parameter mbfid into the area specified by parameter pk_rmbf.
The following describes an example for coding this service call.


 #include    "kernel.h"              /*Standard header file definition*/
 #include    "kernel_id.h"           /*Header file generated by cfg600px*/
 #pragma task Task1                  /*Refer to note 2*/
 void Task1 (VP_INT exinf);          /*Refer to note 2*/
 void Task1 (VP_INT exinf)
 {
     ID      mbfid = 1;              /*Declares and initializes variable*/
     T_RMBF  pk_rmbf;                /*Declares message structure*/
     ID      stskid;                 /*Declares variable*/
     ID      rtskid;                 /*Declares variable*/
     UINT    smsgcnt;                /*Declares variable*/
     SIZE    fmbfsz;                 /*Declares variable*/
 
     /* ......... */
 
     ref_mbf (mbfid, &pk_rmbf);       /*Reference message buffer state*/
 
     stskid = pk_rmbf.stskid;        /*Acquires existence of tasks waiting for */
                                     /*message transmission*/
     rtskid = pk_rmbf.rtskid;        /*Acquires existence of tasks waiting for */
                                     /*message reception*/
     smsgcnt = pk_rmbf.smsgcnt;      /*Acquires the number of message in */
                                     /*message buffer*/
     fmbfsz = pk_rmbf.fmbfsz;        /*Acquires the available buffer size */
 
     /* ......... */
 }


Note 1 For details about the message buffer state packet, refer to "[Message buffer state packet: T_RMBF]".

Note 2 These statements are unnecessary for the task which is created by the system configuration file because the cfg600px generates these statement into the "kernel_id.h".