cancel
Showing results for 
Search instead for 
Did you mean: 

How does the event flag work on STM32? Part 1

B.Montanari
ST Employee

How does the event flag work on STM32?

GOAL:
The purpose of this article is to provide a brief explanation with a working example of how to implement and use the Event Flag resources. Although the example is using the STM32G474 Discovery kit, you can apply this knowledge base in any other ST board or microcontroller.

INTRODUCTION:
Event flags are very useful extension to semaphores and mutexes and provide a powerful tool for thread synchronization. Upon creation using the tx_event_flags_create() call, 32 event flags in the group are initialized with zero, where each event flag is represented by a single bit, thus the event has 32 bits. Threads can ask to watch specific bits by OR or AND and they can operate on all 32 event flags in a group at the same time. If these bits are set, the thread is informed and this can be used to synchronize multiples threads.
The image below shows a quick flowchart of the possible and most typical combinations of OR and AND.
209.png
EVENT FLAGS CREATE
Event flags are created by function tx_eventflags_create.
 
/* GLOBAL VARIABLES */
TX_EVENT_FLAGS_GROUP event_ptr;

/* In main */
tx_event_flags_create(&event_ptr, "External Interrupt"); 
First argument is event flag handle event_ptr. Second argument is event name, in this case: External Interrupt.
EVENT FLAG SET
The event flags are set by function tx_event_flags_set. First argument is event flags handle. Second argument is the flags to be set. Third argument is the way how the flags is set, either ORed or AND. In the following example, the flag bit 1 (10b) will be ORed with event flag content (the AND mode is used mainly to clear event flags already set).
VOID my_thread_entry2(ULONG initial_input)
{   
       while(1)
       {
             tx_thread_sleep(50);
             tx_event_flags_set(&event_ptr, 0x2, TX_OR);
       }
}
211.png
EVENT FLAG GET
For a thread to check if a flag is set or not, we can use a function called tx_event_flags_get.
tx_event_flags_get(&event_ptr, 0x3, TX_AND_CLEAR ,&events, TX_WAIT_FOREVER);

The first argument is the event flag handle event_ptr. The second argument is flags positions desired 0x3 (11b). The third argument is how to get the flags: TX_AND, TX_OR, TX_OR_CLEAR, TX_AND_CLEAR. The forth argument is pointer to grab the flag result. The fifty and last argument is the waiting time, ranging from: TX_NO_WAIT (0x00) to TX_WAIT_FOREVER (0xFFFFFFFF). Event flags get with OR:
214.png
Event flags get with AND:
216.png
Event flags get with OR CLEAR:
218.png
Event flags get with AND CLEAR:
220.png
EXAMPLE CODE:
To show an implementation of the EF (Event Flag) resource, this article uses the RGB LED and Joystick buttons available on STM32G474 Discovery kit. Hence, an Event Flag Group is created, where each bit is associated with a joystick button. The flags are set by the EXTI (External Interrupt) Callback function, which checks which button has been pressed. To manage the Color Led, three timer channels in PWM mode is associated with each signal Led pin.
According to the flag set, two threads manage the PWM timers changing the Timer Capture Compare Registers, and the following functions are implemented:
  • SELECT Button: set the Event Flag 0 (1b) to change LED to rainbow mode.
  • UP Button: set the Event Flag 1 (10b) to change LED color to red.
  • RIGHT Button: set Event Flag 2 (100b) to change LED color to green.
  • DOWN Button: set Event Flag 3 (1000b) to change LED color to blue. 
  • LEFT Button: set Event Flag 4 (10000b) to turn LED off.
The first thread, called by OneColorThread is waiting for an event flag from Up, Right, Down or Left button. When a flag is set, the thread will run cleaning the flag and updating the timer capture compare values, that affects the PWM duty cycles changing the Led color.
The second thread, called by RainbowThread will run while the SELECT button flag is set, different from the other thread, this will not clear the flag when run. The flag that corresponds to the Select button is never cleared by the EXTI Callback function to update to the next stage.
The second thread should be running continuously, because the PWM duty cycles need to be updated continuously to change the LED color, this is also useful to demonstrate an alternative way of management using the Event Flag resources. To simplify the code, a lookup table is created to generate a triangle waveform with 30% of time at 0%. So, a 120° phase is applied in each channel according to generate the following waves and colors.
222.png
In part 2, we will cover the step by step of the introduced concepts implementation on an STM32 project, using the STM32CubeIDE.
See you soon!

 
Comments
BarryWhit
Senior III

B.Montanari's KB articles are normally excellent and I've learned a lot from reading many of of them. This one, however, fails to provide basic and crucial context so it's not entirely clear what the article is even about (unless you already knew what event flags are to begin with).

 

Issue 1: "Event Flags" are a software abstraction provided by the threadx RTOS which ST has been increasingly transitioning it's offering towards. Note that (currently) the words "threadx" or "RTOS" don't actually appear anywhere in this article. Nonetheless, one hint is that all the API functions mentioned are named with a "tx_" prefix, since that is part of the code convention for threadx APIs.

Issue 2: "Event flags" are NOT a hardware feature. You will find no mention of them in the RM for the G4 family, which is the MCU used in the article. Various STM32 register do include "Events" which serve as a way of communicating, but they are not not what this article is about.

Version history
Last update:
‎2022-09-06 12:52 AM
Updated by: