62 January & February 2021 http://www.elektormagazine.com
Let’s start by examining a potential use case. A MIDI drum machine
may be required to make four simultaneous sounds based upon an
input command received by its serial channel. Ideally, all sound-gen-
erating tasks should start at the exact same time to avoid sounding
‘off ’ to the human ear. Coordination could be achieved by the use of
four separate semaphores and relying upon the speed of the CPU for
timely delivery. But that approach is clumsy and doesn’t scale well as
the number of receiving tasks increase. Event Groups are the preferred
implementation method in FreeRTOS for coordinating multiple tasks
from a single event source.
Event flags
FreeRTOS defines 24 event flags for each Event Group object created
(Figure 1). These flags are represented in the C data type EventBits_t,
a 32-bit-wide data type of which 24 bits are available. Bit 0 is the least
significant bit (LSB) of the available flags. The most significant 8 bits
are reserved for internal use by FreeRTOS.
How these 24 bits are assigned and used by the application is left up
to the programmer. In this demo, the loop() task generates an event
every second that results in two tasks synchronously starting differ-
ing blink patterns via their associated LEDs. Bit 0 of the Event Group
(value 0b0001/decimal value 1) notifies a task named blink2() that
blinks LED1 twice. Bit 1 of the Event Group (value 0b0010/decimal
value 2) is assigned to notify a task named blink3() that blinks
LED2 three times.
Demo program
Before we dive into the code, let’s review what the demo program
hopes to accomplish. Two LEDs are driven by GPIO pins 25 and 26
(lines 5 and 6) in the active high configuration (see the schematic in
Figure 2). These GPIOs are configured in the setup() function as
outputs (lines 62 to 65). Task blink2() controls LED1 by blinking it
twice before waiting for the next event (lines 19 to 25). Task blink3()
Practical
ESP32 Multitasking (6)
Event Groups
By Warren Gay (Canada)
Synchronisation between multiple tasks often arises as a requirement when developing
applications using FreeRTOS. In the previous instalments of this series, we examined
semaphore and task notifications. However, these are only capable of sending an event
from a task or interrupt service routine (ISR) to a single receiving task. When you need to
broadcast an event to several tasks, the Event Group capability is the solution you will be
looking for. In this final article of the series, we examine the capabilities this feature offers
embedded developers.
software
MOD1ESP32FSVN
IO25
IO26
IO32
IO33
IO27
IO14
IO12
IO13
IO35TXD0IO10IO18IO23IO19IO22IO21FSD2
FSVPIO15IO37IO38IO34RXD0FCLKFSD0 FSD3FSD1BOOTIO9IO2FCSIO4
IO0
3V3
3V3GNDIO5GND
USBEN
5VR1 EN
220R2
220
LED1 LED2200274-001-9431 ... 24 23 ... 0Reserve d Event Flag BitsFigure 2: The schematic for the demo program evtgrp.ino.Figure 1: Event flags within the C data type EventBits_t.