Flags are similar to binary semaphores. There are two differences:
Flags can be useful when a task requires the results of several tasks. For example: to calculate some physical parameters a task needs information about the temperature, pressure and humidity. Each of these parameters is measured in its own task which sets a flag when measurement is complete.
#include <osa.h> OST_FLAG8 F_Sensors; #define TEMPERATURE_MEASURED 0x01 #define PRESSURE_MEASURED 0x02 #define HUMIDITY_MEASURED 0x04 void Task_Calc (void) { for (;;) { // Wait for all measurements complete OS_Flag_Wait_AllOn(F_Sensors, TEMPERATURE_MEASURED | PRESSURE_MEASURED | HUMIDITY_MEASURED); OS_Flag_Clear(F_Sensors, TEMPERATURE_MEASURED | PRESSURE_MEASURED | HUMIDITY_MEASURED); // Now we can calculate ... } }
There are three types of flags: 8-, 16- and 32-bit.
OST_FLAG MyFlag1; // 8-bit flags OST_FLAG16 MyFlag2; // 16-bit flags OST_FLAG32 MyFlag3; // 32-bit flags
It is possible to use different size of flags at the same time.
Flags can be allocated in any memory bank, e.g.:
bank2 OST_FLAG16 MyFlag;
Service | Arguments | Description | Properties |
---|---|---|---|
Creating | |||
OS_Flag_Create | (flag) | (flag = 0) Create flag and clear all its bits | |
Management | |||
OS_Flag_Init | (flag, value) | (flag = value) Set flag to given value | |
OS_Flag_Set | (flag, mask) | (flag = flag | mask) Set bits in flag according to mask | |
OS_Flag_Clear | (flag, mask) | (flag &= ~mask) Clear bits in flag according to mask | |
Checking | |||
bool OS_Flag_Check_AllOn | (flag, mask) | ( if ((flag & mask)==mask) ) Check if all bits in flag specified by mask are set | |
bool OS_Flag_Check_On | (flag, mask) | ( if ((flag & mask)!=0) ) Check if any bit in flags specified by mask is set | |
bool OS_Flag_Check_AllOff | (flag, mask) | ( if ((flag & mask)==0) ) Check if all bits in flag specified by mask are clear | |
bool OS_Flag_Check_Off | (flag, mask) | ( if ((flag & mask)!=mask) ) Check if any bit in flag specified by mask is clear | |
Waiting | |||
OS_Flag_Wait_AllOn | (flag, mask) | ( if ((flag & mask)==mask) ) Wait until all bits in flag specified by mask are set | |
OS_Flag_Wait_On | (flag, mask) | ( wait ((flag & mask)!=0) ) Wait until any bit in flag specified by mask is set | |
OS_Flag_Wait_AllOff | (flag, mask) | ( wait ((flag & mask)==0) ) Wait until all bits in flag specified by mask are clear | |
OS_Flag_Wait_Off | (flag, mask) | ( wait ((flag & mask)!=mask) ) Wait until any bit in flag specified by mask is clear | |
OS_Flag_Wait_AllOn_TO | (flag, mask, timeout) | ( wait ((flag & mask)==mask) ) Wait until all bits in flags specified by mask are set. Exit if timeout expired | |
OS_Flag_Wait_On_TO | (flag, mask, timeout) | ( wait ((flag & mask)!=0) ) Wait until any bit in flags specified by mask is set. Exit if timeout expired | |
OS_Flag_Wait_AllOff_TO | (flag, mask, timeout) | ( wait ((flag & mask)==0) ) Wait until all bits in flag specified by mask are clear. Exit if timeout expired | |
OS_Flag_Wait_Off_TO | (flag, mask, timeout) | ( wait ((flag & mask)!=mask) ) Wait until any bit in flag specified by mask is clear. Exit if timeout expired |