Mailbox using Cortex M4

This example describes how to use mailbox in CORTEX-M4.We have used Cortex m4 development board

Procedure for mailbox using Cortex m4

  1. Initialize UART0 @9600 baud rate
  2. Create a Send Task and a receive Task
  3. Compose and send the mailbox data using send task
  4. Discard send task and free-up memories
  5. Retrieve the messages using receive task

Mailbox using Cortex M4-C Source code


#include <RTL.h>                      /* RTX kernel functions & defines      */#include <LPC407x_8x_177x_8x.h>
#include "uart0.h"

OS_TID tsk1;                          /* assigned identification for task 1  */OS_TID tsk2;                          /* assigned identification for task 2  */
typedef struct {                      /* Message object structure            */  int voltage;                      /* AD result of measured voltage       */  int current;                      /* AD result of measured current       */  U32   counter;                      /* A counter value                     */} T_MEAS;

os_mbx_declare (MsgBox,16);             /* Declare an RTX mailbox              */_declare_box (mpool,sizeof(T_MEAS),16);  /* Dynamic memory pool                */
__task void send_task (void);
__task void rec_task (void);

unsigned char str[6];


/*----------------------------------------------------------------------------
 *  Task 1:  RTX Kernel starts this task with os_sys_init (send_task)
 *---------------------------------------------------------------------------*/__task void send_task (void) {
   T_MEAS *mptr;

   tsk1 = os_tsk_self ();                    /* get own task identification number  */  //2. Create receive Task
   tsk2 = os_tsk_create (rec_task, 0);       /* start task 2                        */   os_mbx_init (MsgBox, sizeof(MsgBox));      /* initialize the mailbox             */   os_dly_wait (5);                          /* Startup delay for MCB21xx           */
  //3.Compose a mail of data
   mptr = _alloc_box (mpool);                /* Allocate a memory for the message   */   mptr->voltage = 223;                     /* Set the message content             */   mptr->current = 17;
   mptr->counter = 1207;
   os_mbx_send (MsgBox, mptr, 0xffff);       /* Send the message to the mailbox     */   os_dly_wait (100);

   mptr = _alloc_box (mpool);
   mptr->voltage = 227;                     /* Prepare a 2nd message               */   mptr->current = 12;
   mptr->counter = 1708;
   os_mbx_send (MsgBox, mptr, 0xffff);       /* And send it.                        */   os_tsk_pass ();                           /* Cooperative multitasking            */   os_dly_wait (100);

   mptr = _alloc_box (mpool);
   mptr->voltage = 229;                     /* Prepare a 3rd message               */   mptr->current = 11;
   mptr->counter = 2371;
  //3. Send the Mail of data
   os_mbx_send (MsgBox, mptr, 0xffff);       /* And send it.                        */   os_dly_wait (100);
  
  //4. Discard the send task
   os_tsk_delete_self ();                    /* We are done here, delete this task  */}

/*----------------------------------------------------------------------------
 *  Task 2: RTX Kernel starts this task with os_tsk_create (rec_task, 0)
 *---------------------------------------------------------------------------*/__task void rec_task (void) {
  T_MEAS *rptr;
 //5. Retrieve the mail of data and display it in UART
  for (;;) 
 {
  os_mbx_wait (MsgBox, (void **)&rptr, 0xffff);     /* wait for the message    */  UART0_puts((unsigned char *)"\r\nVoltage:");
  itoa(rptr->voltage,str);
  UART0_puts(str);
  
  UART0_puts((unsigned char *)"\r\nCurrent:");
  itoa(rptr->current,str);
  UART0_puts(str);
  
  UART0_puts((unsigned char *)"\r\nNumber of cycles:");
  itoa(rptr->counter,str);
  UART0_puts(str);
  
  _free_box (mpool, rptr);                     /* free memory allocated for message  */  }
}

/*----------------------------------------------------------------------------
 *        Main: Initialize and start RTX Kernel
 *---------------------------------------------------------------------------*/int main (void) 
{ 
  //1. Initialize Serial Port
  init_serial();                       /* initialize the serial interface     */  _init_box (mpool, sizeof(mpool),     /* initialize the 'mpool' memory for   */        sizeof(T_MEAS));         /* the membox dynamic allocation       */  //2. Create and Initialize the Send Task
  os_sys_init (send_task);             /* initialize and start task 1         */}


/*----------------------------------------------------------------------------
 * end of file
 *---------------------------------------------------------------------------*/
Pantech:
Leave a Comment

This website uses cookies.