#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
/* Bits used by the three tasks. */
#define TASK_0_BIT ( 1 << 0 )
#define TASK_1_BIT ( 1 << 1 )
#define TASK_2_BIT ( 1 << 2 )
#define ALL_SYNC_BITS ( TASK_0_BIT | TASK_1_BIT | TASK_2_BIT )
/* Use an event group to synchronise three tasks. It is assumed this event
group has already been created elsewhere. */
EventGroupHandle_t xEventBits;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
/* Attempt to create the event group. */
xEventBits = xEventGroupCreate();
/* Was the event group created successfully? */
if ( xEventBits == NULL )
{
/* The event group was not created because there was insufficient
FreeRTOS heap available. */
Serial.println("failed to create event group!!");
}
else
{
/* The event group was created. */
xTaskCreate(vTask0, "Task0", 1000, NULL, 0, NULL);
xTaskCreate(vTask1, "Task1", 1000, NULL, 0, NULL);
xTaskCreate(vTask2, "Task2", 1000, NULL, 0, NULL);
}
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
}
void vTask0( void *pvParameters )
{
EventBits_t uxReturn;
TickType_t xTicksToWait = 100 / portTICK_PERIOD_MS;
for ( ;; )
{
/* Perform task functionality here. */
vTaskDelay(1);
/* Set bit 0 in the event group to note this task has reached the
sync point. The other two tasks will set the other two bits defined
by ALL_SYNC_BITS. All three tasks have reached the synchronisation
point when all the ALL_SYNC_BITS are set. Wait a maximum of 100ms
for this to happen. */
uxReturn = xEventGroupSync( xEventBits,
TASK_0_BIT,
ALL_SYNC_BITS,
xTicksToWait );
if ( ( uxReturn & ALL_SYNC_BITS ) == ALL_SYNC_BITS )
{
/* All three tasks reached the synchronisation point before the call
to xEventGroupSync() timed out. */
Serial.println("Success");
}
}
}
void vTask1( void *pvParameters )
{
for ( ;; )
{
/* Perform task functionality here. */
vTaskDelay(100);
/* Set bit 1 in the event group to note this task has reached the
synchronisation point. The other two tasks will set the other two
bits defined by ALL_SYNC_BITS. All three tasks have reached the
synchronisation point when all the ALL_SYNC_BITS are set. Wait
indefinitely for this to happen. */
xEventGroupSync( xEventBits, TASK_1_BIT, ALL_SYNC_BITS, portMAX_DELAY );
/* xEventGroupSync() was called with an indefinite block time, so
this task will only reach here if the syncrhonisation was made by all
three tasks, so there is no need to test the return value. */
}
}
void vTask2( void *pvParameters )
{
for ( ;; )
{
/* Perform task functionality here. */
vTaskDelay(1000);
/* Set bit 2 in the event group to note this task has reached the
synchronisation point. The other two tasks will set the other two
bits defined by ALL_SYNC_BITS. All three tasks have reached the
synchronisation point when all the ALL_SYNC_BITS are set. Wait
indefinitely for this to happen. */
xEventGroupSync( xEventBits, TASK_2_BIT, ALL_SYNC_BITS, portMAX_DELAY );
/* xEventGroupSync() was called with an indefinite block time, so
this task will only reach here if the syncrhonisation was made by all
three tasks, so there is no need to test the return value. */
}
}