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:
- UserMain Task - The primary task that waits for messages from other tasks
- MyTask - A secondary task that periodically sends messages to the mailbox
Program Flow
- The application initializes the system and enables network connectivity
- A mailbox object (
MyMailbox
) is created and initialized
- Two tasks are created with different priorities:
UserMain
runs at main priority level
MyTask
runs at main priority + 1 (higher priority)
- 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
- Compile and flash the program to your target device
- Connect a serial terminal to the debug port
- Monitor the output to observe the mailbox communication pattern
- The program runs indefinitely, demonstrating continuous inter-task messaging