NetBurner 3.5.6
PDF Version
OSFifo - FIFO

RTOS FIFO Example Application

Overview

This application demonstrates the use of a First-In-First-Out (FIFO) queue in a Real-Time Operating System (RTOS) environment. The program creates two tasks that communicate with each other through a FIFO buffer, showcasing inter-task communication and timeout handling.

Application Description

The OSFifo example creates a producer-consumer pattern where:

  • One task (FifoPostTask) posts messages to a FIFO queue
  • Another task (UserMain) reads messages from the FIFO queue
  • A timeout mechanism is implemented to demonstrate FIFO timeout features

Key Components

Data Structure

typedef struct {
void *pUsedByFifo; // Internal FIFO linked list pointer (do not modify)
int x; // Data payload
BOOL free; // Availability flag
} MyStructure;

FIFO Management

  • FIFO Object: OS_FIFO MyFIFO - The main FIFO queue
  • Structure Array: MyStructure StructArray[NUM_POSTS] - Pre-allocated message pool
  • Memory Management: Uses static allocation instead of dynamic memory

Program Flow

Initialization Phase

  1. Initialize the FIFO object
  2. Initialize all structures in the array as "free"
  3. Create the FIFO posting task
  4. Enable system diagnostics

Runtime Operation

Producer Task (FifoPostTask)

  • Runs continuously in a loop
  • Posts NUM_POSTS (5) messages per cycle
  • For each message:
    • Finds a free structure in the array
    • Sets the data value (x) to the current count
    • Marks the structure as "in use"
    • Posts the structure pointer to the FIFO
  • Waits 9 seconds between posting cycles

Consumer Task (UserMain)

  • Continuously pends on the FIFO with a 5-second timeout
  • When a message is received:
    • Prints the received data value
    • Marks the structure as "free" for reuse
  • When timeout occurs:
    • Prints a timeout message
    • Continues waiting for the next message

Timeout Demonstration

The application is designed to demonstrate FIFO timeout behavior:

  • The producer posts 5 messages quickly, then waits 9 seconds
  • The consumer has a 5-second timeout on FIFO pend operations
  • After consuming all 5 messages, the consumer will timeout once before the next batch arrives

Key Features

Memory Management

  • Uses static memory allocation instead of dynamic allocation
  • Implements a simple free-list mechanism for structure reuse
  • Prevents memory leaks through proper structure lifecycle management

Error Handling

  • Checks for task creation errors
  • Handles FIFO timeout conditions gracefully
  • Validates structure availability before posting

Synchronization

  • Demonstrates safe inter-task communication using FIFO
  • Shows proper handling of resource contention
  • Illustrates timeout-based resource management

Configuration

  • NUM_POSTS: Number of structures to post per cycle (currently 5)
  • USER_TASK_STK_SIZE: Stack size for the posting task
  • TICKS_PER_SECOND: System tick rate for timing calculations
  • MAIN_PRIO - 1: Priority level for the posting task

Expected Output

The application will display:

  1. FIFO posting messages showing structure index and data value
  2. FIFO reading messages showing read count and data value
  3. Periodic timeout messages during the 9-second delay periods
  4. Delay notifications before each new posting cycle

Usage Notes

  • This example is designed for educational purposes to demonstrate FIFO usage
  • The timeout feature illustrates how to handle blocking operations with time limits
  • The static memory management approach is suitable for resource-constrained embedded systems
  • System diagnostics are enabled for debugging (should be disabled in production)

Technical Requirements

  • NetBurner RTOS environment
  • Standard C++ compiler support
  • RTOS task creation and timing services
  • FIFO queue implementation (OS_FIFO class)