NetBurner 3.5.6
PDF Version
OSFlags

OSFlags RTOS Example

Overview

This example demonstrates how to use OSFlags in a Real-Time Operating System (RTOS) environment to coordinate multiple tasks through event signaling. The application creates multiple tasks that set flags at different intervals, while a main task waits for these events to occur.

Application Description

The OSFlags example illustrates inter-task communication using event flags. It creates three worker tasks that operate on different time schedules and set corresponding flags. The main task monitors these flags and responds when any (or optionally all) of the flags are set.

Key Components

  • Global OS Flag Object: gOsFlag - A shared flag object used for inter-task communication
  • Three Worker Tasks: Each task sets a specific flag after a time delay
  • Main Monitoring Task: Waits for flag events and processes them

Task Structure

Worker Tasks

  1. OsFlagTask1
    • Priority: MAIN_PRIO + 1
    • Delay: 2 seconds
    • Sets: TASK_FLAG_1 (bit 0x00000001)
  2. OsFlagTask2
    • Priority: MAIN_PRIO + 2
    • Delay: 4 seconds
    • Sets: TASK_FLAG_2 (bit 0x00000002)
  3. OsFlagTask3
    • Priority: MAIN_PRIO + 3
    • Delay: 6 seconds
    • Sets: TASK_FLAG_3 (bit 0x00000004)

Main Task (UserMain)

The main task performs the following operations:

  1. Initializes the system and network
  2. Creates the three worker tasks
  3. Enters an infinite loop that:
    • Waits for any flag to be set using PendAny()
    • Identifies which flags are currently set
    • Prints the status of active flags
    • Clears the processed flags

Flag Operations

Pending Modes

The application supports two different pending modes:

  • PendAny(): Returns when one or more flags are set (default behavior)
  • PendAll(): Returns only when all specified flags are set (commented out)

Flag Management

  • Setting Flags: Each worker task sets its assigned flag using gOsFlag.Set()
  • Reading Flags: The main task reads flag state using gOsFlag.State()
  • Clearing Flags: Processed flags are cleared using gOsFlag.Clear()

Expected Behavior

The application will produce output showing which tasks have set their flags:

OSFlagPendAny() detected the following flag(s) are set: TASK1
OSFlagPendAny() detected the following flag(s) are set: TASK2
OSFlagPendAny() detected the following flag(s) are set: TASK1 TASK3
uint8_t OSFlagPendAny(OS_FLAGS *flags, uint32_t bit_mask, uint16_t timeout)
This function waits a number of time ticks specified by timeout until any of the flags indicated by b...
Definition nbrtos.h:1518

The timing pattern will be:

  • Task1 flag every 2 seconds
  • Task2 flag every 4 seconds
  • Task3 flag every 6 seconds

Configuration Options

Modifying Pending Behavior

To change from "any flag" to "all flags" behavior:

  1. Comment out the PendAny() call
  2. Uncomment the PendAll() call

This will cause the main task to wait until all three flags are set simultaneously before proceeding.

Timing Adjustments

Task delays can be modified by changing the multiplier values:

  • OSTimeDly((2 * TICKS_PER_SECOND)) - 2 second delay
  • OSTimeDly((4 * TICKS_PER_SECOND)) - 4 second delay
  • OSTimeDly((6 * TICKS_PER_SECOND)) - 6 second delay

System Requirements

  • RTOS environment with OSFlags support
  • Network capability (DHCP)
  • System diagnostics support
  • Multi-tasking support

Key Learning Points

This example demonstrates:

  1. Inter-task Communication: Using flags to coordinate between tasks
  2. Event-Driven Programming: Tasks respond to events rather than polling
  3. Bitwise Operations: Flag manipulation using bitmasking
  4. Task Priorities: Different priority levels for worker tasks
  5. Synchronization: Coordinating multiple asynchronous tasks

Usage Notes

  • The application waits for network initialization (DHCP) before starting
  • System diagnostics are enabled (should be disabled for production)
  • Flag clearing is important to prevent false triggers
  • Only one pending function (PendAny or PendAll) can be active at a time