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
- OsFlagTask1
- Priority: MAIN_PRIO + 1
- Delay: 2 seconds
- Sets: TASK_FLAG_1 (bit 0x00000001)
- OsFlagTask2
- Priority: MAIN_PRIO + 2
- Delay: 4 seconds
- Sets: TASK_FLAG_2 (bit 0x00000002)
- OsFlagTask3
- Priority: MAIN_PRIO + 3
- Delay: 6 seconds
- Sets: TASK_FLAG_3 (bit 0x00000004)
Main Task (UserMain)
The main task performs the following operations:
- Initializes the system and network
- Creates the three worker tasks
- 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 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:
- Comment out the
PendAny()
call
- 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:
- Inter-task Communication: Using flags to coordinate between tasks
- Event-Driven Programming: Tasks respond to events rather than polling
- Bitwise Operations: Flag manipulation using bitmasking
- Task Priorities: Different priority levels for worker tasks
- 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