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
DClin.1
Associate

The event flag feature on STM32 microcontrollers allows for efficient and low-power communication between different parts of a system. When a specific event occurs, such as the completion of a data transfer or the detection of a signal on a GPIO pin, an event flag associated with that event is set. Other parts of the system can then monitor the event flag and take action accordingly, such as waking up from a low-power sleep mode or triggering a software interrupt.

In order to use the event flag feature on an STM32 microcontroller, there are a few key steps that need to be taken:

  1. Enable the Event Manager (EV) peripheral: The Event Manager peripheral needs to be enabled in order to use the event flag feature. This can typically be done by setting a bit in a control register, such as the RCC_APB4ENR register for STM32H7 microcontrollers.
  2. Configure the event sources: Each event source that you want to monitor needs to be configured with the appropriate settings. This typically involves configuring the interrupt or event trigger conditions for the peripheral that generates the event, such as a timer or a GPIO pin.
  3. Configure the event flags: Each event flag that you want to use needs to be configured with the appropriate settings. This typically involves specifying which event source is associated with the flag, as well as any optional filtering or masking settings.
  4. Wait for events: Once the event flags are configured, your application can wait for specific events to occur by monitoring the corresponding event flags. This can typically be done using a polling loop or by using an interrupt to wake up the processor when the event occurs.
  5. Clear the event flags: Once the event has been processed, the associated event flag needs to be cleared in order to allow the system to detect the next occurrence of the event.

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