CMSIS-RTOS2  
Real-Time Operating System API
 
Loading...
Searching...
No Matches
Event Flags

Synchronize threads using event flags. More...

Data Structures

struct  osEventFlagsAttr_t
 Attributes structure for event flags. More...
 

Typedefs

typedef void * osEventFlagsId_t
 

Functions

osEventFlagsId_t osEventFlagsNew (const osEventFlagsAttr_t *attr)
 Create and Initialize an Event Flags object.
 
uint32_t osEventFlagsSet (osEventFlagsId_t ef_id, uint32_t flags)
 Set the specified Event Flags.
 
uint32_t osEventFlagsClear (osEventFlagsId_t ef_id, uint32_t flags)
 Clear the specified Event Flags.
 
uint32_t osEventFlagsGet (osEventFlagsId_t ef_id)
 Get the current Event Flags.
 
uint32_t osEventFlagsWait (osEventFlagsId_t ef_id, uint32_t flags, uint32_t options, uint32_t timeout)
 Wait for one or more Event Flags to become signaled.
 
osStatus_t osEventFlagsDelete (osEventFlagsId_t ef_id)
 Delete an Event Flags object.
 
const char * osEventFlagsGetName (osEventFlagsId_t ef_id)
 Get name of an Event Flags object.
 

Description

Synchronize threads using event flags.

The event flags management functions in CMSIS-RTOS allow you to control or wait for event flags. Each signal has up to 31 event flags.

A thread

When a thread wakes up and resumes execution, its signal flags are automatically cleared (unless event flags option osFlagsNoClear is specified).

Note
The functions osEventFlagsSet, osEventFlagsClear, osEventFlagsGet, and osEventFlagsWait can be called from Interrupt Service Routines.

Working with Events

Here is a simple example that shows how two thread can communicate with each others using event flags:

Simple event communication

The following steps are required to use event flags:

  1. In the thread that is supposed to send a event with id sig1_id, call the set function:
    osDelay(1000U); // wait for 1 second
    osEventFlagsSet(sig1_id, 0x0001U); // set the flag 0x0001U for event sig1_id
  2. In another thread (or threads) that are supposed to wait for the event, call the wait function:
    osEventFlagsWait(sig1_id, 0x0001U, NULL, osWaitForever); // wait forever for any flag

Code Example

#include "cmsis_os2.h" // CMSIS RTOS header file
/*----------------------------------------------------------------------------
* Event Flags creation & usage
*---------------------------------------------------------------------------*/
#define FLAGS_MSK1 0x00000001U
osEventFlagsId_t evt_id; // event flags id
osThreadId_t tid_Thread_EventSender; // thread id 1
osThreadId_t tid_Thread_EventReceiver; // thread id 2
void Thread_EventSender (void *argument); // thread function 1
void Thread_EventReceiver (void *argument); // thread function 2
int Init_Events (void) {
evt_id = osEventFlagsNew(NULL);
if (evt_id == NULL) {
; // Event Flags object not created, handle failure
}
tid_Thread_EventSender = osThreadNew(Thread_EventSender, NULL, NULL);
if (tid_Thread_EventSender == NULL) {
return(-1);
}
tid_Thread_EventReceiver = osThreadNew(Thread_EventReceiver, NULL, NULL);
if (tid_Thread_EventReceiver == NULL) {
return(-1);
}
return(0);
}
void Thread_EventSender (void *argument) {
while (1) {
osEventFlagsSet(evt_id, FLAGS_MSK1);
osThreadYield(); // suspend thread
}
}
void Thread_EventReceiver (void *argument) {
uint32_t flags;
while (1) {
flags = osEventFlagsWait(evt_id, FLAGS_MSK1, osFlagsWaitAny, osWaitForever);
//handle event
}
}

Data Structure Documentation

◆ osEventFlagsAttr_t

struct osEventFlagsAttr_t

Attributes structure for event flags.

Attributes to configure an event flag set.

Refer to Memory Management for details about usage of

Data Fields
const char * name name of the event flags

Pointer to a constant string with a human readable name (displayed during debugging) of the event flag object.

Default: NULL no name specified.

uint32_t attr_bits attribute bits

Reserved for future use (must be set to '0' for future compatibility).

void * cb_mem memory for control block

Pointer to a memory for the event flag control block object. Refer to Manual User-defined Allocation for more information.

Default: NULL to use Automatic Dynamic Allocation for the event flag control block.

uint32_t cb_size size of provided memory for control block

The size (in bytes) of memory block passed with cb_mem. Required value depends on the underlying kernel implementation.

Default: 0 as the default is no memory provided with cb_mem.

Typedef Documentation

◆ osEventFlagsId_t

Event Flags ID identifies the event flags.

Returned by:

Function Documentation

◆ osEventFlagsNew()

osEventFlagsId_t osEventFlagsNew ( const osEventFlagsAttr_t attr)

Create and Initialize an Event Flags object.

Parameters
[in]attrevent flags attributes; NULL: default values.
Returns
event flags ID for reference by other functions or NULL in case of error.

The function osEventFlagsNew creates a new event flags object that is used to send events across threads and returns the pointer to the event flags object identifier or NULL in case of an error. It can be safely called before the RTOS is started (call to osKernelStart), but not before it is initialized (call to osKernelInitialize).

The parameter attr sets the event flags attributes (refer to osEventFlagsAttr_t). Default attributes will be used if set to NULL, i.e. kernel memory allocation is used for the event control block.

Note
Cannot be called from Interrupt Service Routines.

Code Example

#include "cmsis_os2.h" // CMSIS RTOS header file
osEventFlagsId_t evt_id; // message queue id
int Init_Events (void) {
evt_id = osEventFlagsNew(NULL);
if (evt_id == NULL) {
; // Event Flags object not created, handle failure
return(-1);
}
return(0);
}

◆ osEventFlagsSet()

uint32_t osEventFlagsSet ( osEventFlagsId_t  ef_id,
uint32_t  flags 
)

Set the specified Event Flags.

Parameters
[in]ef_idevent flags ID obtained by osEventFlagsNew.
[in]flagsspecifies the flags that shall be set.
Returns
event flags after setting or error code if highest bit set.

The function osEventFlagsSet sets the event flags specified by the parameter flags in an event flags object specified by parameter ef_id.

The threads with highest priority waiting for the flag(s) set will be notified to resume from BLOCKED state. The function returns the event flags stored in the event control block or an error code (highest bit is set, refer to Flags Functions Error Codes). Further threads may be wakened in priority order when the option osFlagsNoClear is given to the osEventFlagsWait call.

Possible Flags Functions Error Codes return values:

  • osFlagsErrorUnknown: unspecified error.
  • osFlagsErrorParameter: parameter ef_id does not identify a valid event flags object or flags has highest bit set.
  • osFlagsErrorResource: the event flags object is in an invalid state.
  • osFlagsErrorSafetyClass: the calling thread safety class is lower than the safety class of the specified event flags object.
Note
This function may be called from Interrupt Service Routines.

Code Example

#include "cmsis_os2.h" // CMSIS RTOS header file
osEventFlagsId_t evt_id; // event flags id
void Thread_EventSender (void *argument) {
while (1) {
osEventFlagsSet(evt_id, 0x00000001U);
osThreadYield(); // suspend thread
}
}

◆ osEventFlagsClear()

uint32_t osEventFlagsClear ( osEventFlagsId_t  ef_id,
uint32_t  flags 
)

Clear the specified Event Flags.

Parameters
[in]ef_idevent flags ID obtained by osEventFlagsNew.
[in]flagsspecifies the flags that shall be cleared.
Returns
event flags before clearing or error code if highest bit set.

The function osEventFlagsClear clears the event flags specified by the parameter flags in an event flags object specified by parameter ef_id. The function returns the event flags before clearing or an error code (highest bit is set, refer to Flags Functions Error Codes).

Possible Flags Functions Error Codes return values:

  • osFlagsErrorUnknown: unspecified error.
  • osFlagsErrorParameter: parameter ef_id does not identify a valid event flags object or flags has highest bit set.
  • osFlagsErrorResource: the event flags object is in an invalid state.
  • osFlagsErrorSafetyClass: the calling thread safety class is lower than the safety class of the specified event flags object.
Note
This function may be called from Interrupt Service Routines.

◆ osEventFlagsGet()

uint32_t osEventFlagsGet ( osEventFlagsId_t  ef_id)

Get the current Event Flags.

Parameters
[in]ef_idevent flags ID obtained by osEventFlagsNew.
Returns
current event flags.

The function osEventFlagsGet returns the event flags currently set in an event flags object specified by parameter ef_id or 0 in case of an error.

Note
This function may be called from Interrupt Service Routines.

◆ osEventFlagsWait()

uint32_t osEventFlagsWait ( osEventFlagsId_t  ef_id,
uint32_t  flags,
uint32_t  options,
uint32_t  timeout 
)

Wait for one or more Event Flags to become signaled.

Parameters
[in]ef_idevent flags ID obtained by osEventFlagsNew.
[in]flagsspecifies the flags to wait for.
[in]optionsspecifies flags options (osFlagsXxxx).
[in]timeoutTimeout Values or 0 in case of no time-out.
Returns
event flags before clearing or error code if highest bit set.

The function osEventFlagsWait suspends the execution of the currently RUNNING thread until any or all event flags specified by the parameter flags in the event object specified by parameter ef_id are set. When these event flags are already set, the function returns instantly. Otherwise, the thread is put into the state BLOCKED.

The options parameter specifies the wait condition:

Option
osFlagsWaitAny Wait for any flag (default).
osFlagsWaitAll Wait for all flags.
osFlagsNoClear Do not clear flags which have been specified to wait for.

If osFlagsNoClear is set in the options osEventFlagsClear can be used to clear flags manually.

The parameter timeout specifies how long the system waits for event flags. While the system waits, the thread that is calling this function is put into the BLOCKED state. The parameter timeout can have the following values:

  • when timeout is 0, the function returns instantly (i.e. try semantics).
  • when timeout is set to osWaitForever the function will wait for an infinite time until the event flags become available (i.e. wait semantics).
  • all other values specify a time in kernel ticks for a timeout (i.e. timed-wait semantics).

The function returns the event flags before clearing or an error code (highest bit is set, refer to Flags Functions Error Codes).

Possible Flags Functions Error Codes return values:

  • osFlagsErrorUnknown: unspecified error.
  • osFlagsErrorTimeout: awaited flags have not been set in the given time.
  • osFlagsErrorResource: awaited flags have not been set when no timeout was specified.
  • osFlagsErrorParameter: parameter ef_id does not identify a valid event flags object or flags has highest bit set.
  • osFlagsErrorSafetyClass: the calling thread safety class is lower than the safety class of the specified event flags object.
Note
May be called from Interrupt Service Routines if the parameter timeout is set to 0.

Code Example

#include "cmsis_os2.h" // CMSIS RTOS header file
osEventFlagsId_t evt_id; // event flags id
void Thread_EventReceiver (void *argument) {
uint32_t flags;
while (1) {
flags = osEventFlagsWait(evt_id, 0x00000001U, osFlagsWaitAny, osWaitForever);
//handle event
}
}

◆ osEventFlagsDelete()

osStatus_t osEventFlagsDelete ( osEventFlagsId_t  ef_id)

Delete an Event Flags object.

Parameters
[in]ef_idevent flags ID obtained by osEventFlagsNew.
Returns
status code that indicates the execution status of the function.

The function osEventFlagsDelete deletes the event flags object specified by parameter ef_id and releases the internal memory obtained for the event flags handling. After this call, the ef_id is no longer valid and cannot be used. This can cause starvation of threads that are waiting for flags of this event object. The ef_id may be created again using the function osEventFlagsNew.

Possible osStatus_t return values:

  • osOK: the specified event flags object has been deleted.
  • osErrorISR: osEventFlagsDelete cannot be called from interrupt service routines.
  • osErrorParameter: parameter ef_id is NULL or invalid.
  • osErrorResource: the event flags object is in an invalid state.
  • osFlagsErrorSafetyClass: the calling thread safety class is lower than the safety class of the specified event flags object.
Note
This function cannot be called from Interrupt Service Routines.

◆ osEventFlagsGetName()

const char * osEventFlagsGetName ( osEventFlagsId_t  ef_id)

Get name of an Event Flags object.

Parameters
[in]ef_idevent flags ID obtained by osEventFlagsNew.
Returns
name as null-terminated string.

The function osEventFlagsGetName returns the pointer to the name string of the event flags object identified by parameter ef_id or NULL in case of an error.

Note
This function may be called from Interrupt Service Routines.

Code Example

#include "cmsis_os2.h" // CMSIS RTOS header file
osEventFlagsId_t evt_id; // event flasg id
void EvtFlagsGetName_example (void) {
char *name;
name = osEventFlagsGetName(evt_id);
if (name == NULL) {
// Failed to get the event flags object name
}
}