NetBurner 3.5.6
PDF Version
OSMailbox

RTOS Mailbox Example

Overview

This application demonstrates inter-task communication using mailboxes in a real-time operating system (RTOS). The program creates two tasks that communicate through a shared mailbox mechanism, showcasing how tasks can send and receive messages in a multi-threaded environment.

Application Description

The Mailbox Example application consists of two main tasks:

  1. UserMain Task - The primary task that waits for messages from other tasks
  2. MyTask - A secondary task that periodically sends messages to the mailbox

Program Flow

  1. The application initializes the system and enables network connectivity
  2. A mailbox object (MyMailbox) is created and initialized
  3. Two tasks are created with different priorities:
    • UserMain runs at main priority level
    • MyTask runs at main priority + 1 (higher priority)
  4. The tasks communicate through the mailbox using a producer-consumer pattern

Task Behavior

UserMain Task

  • Continuously waits (pends) on the mailbox for incoming messages
  • When a message is received, it prints the message value
  • Increments the received message value before the next iteration
  • Handles error conditions when message reception fails

MyTask

  • Runs in an infinite loop with a 3-second delay between iterations
  • Posts a message containing a uint8_t value to the mailbox
  • Prints status information when posting messages

Key Features

  • Inter-task Communication: Demonstrates safe message passing between tasks
  • Blocking Operations: Shows how tasks can block waiting for messages
  • Error Handling: Includes proper error checking for mailbox operations
  • Debug Output: Provides serial port output for monitoring program execution
  • Network Integration: Waits for active network before starting main operations

Technical Details

Data Structures

  • OS_MBOX MyMailbox - The mailbox object for inter-task communication
  • uint32_t MyTaskStack[USER_TASK_STK_SIZE] - Stack allocation for MyTask
  • uint8_t myMsg - The message data passed between tasks

RTOS Functions Used

  • OSTimeDly() - Task delay function
  • MyMailbox.Init() - Mailbox initialization
  • MyMailbox.Post() - Send message to mailbox
  • MyMailbox.Pend() - Wait for message from mailbox
  • OSSimpleTaskCreatewName() - Task creation with naming

System Functions

Output

The program outputs status messages through the serial debug port, which can be monitored using terminal programs like MTTTY. The output shows:

  • When UserMain is waiting for messages
  • When MyTask posts messages to the mailbox
  • The received message values
  • Any error conditions that occur

Build Requirements

  • RTOS system with mailbox support
  • Serial debug port capability
  • Network stack (optional, used for initialization timing)

Usage

  1. Compile and flash the program to your target device
  2. Connect a serial terminal to the debug port
  3. Monitor the output to observe the mailbox communication pattern
  4. The program runs indefinitely, demonstrating continuous inter-task messaging