ESP32-S开发 之 FreeRTOS EventGroup

本文基于ESP32-S开发 之 FreeRTOS Semaphore

本文的主线 简介 => 事件组

简介

Event groups can be used to synchronise tasks, creating what is often referred to as a task ‘rendezvous’. A task synchronisation point is a place in application code at which a task will wait in the Blocked state (not consuming any CPU time) until all the other tasks taking part in the synchronisation also reached their synchronisation point.

  • Usage Challenges
1
2
3
Avoiding the creation of race conditions in the user's application

Avoiding non-determinism

事件组

1
vim main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <Arduino.h>

#define EV_1 (1<<0) // 1
#define EV_2 (1<<1) // 10

void TaskMain(void *pvParameters);
void Task1(void *pvParameters);
void Task2(void *pvParameters);

EventGroupHandle_t eventGroupHandle;
uint32_t events = 0;

void setup() {
eventGroupHandle = xEventGroupCreate();

xTaskCreate(&TaskMain, "Task Main", 1024, NULL, 1, NULL);
xTaskCreate(&Task1, "Task 1", 1024, NULL, 1, NULL);
xTaskCreate(&Task2, "Task 2", 1024, NULL, 1, NULL);
}

void loop() {
// implicit invocation of vTaskStartScheduler
}

void TaskMain(void *pvParameters) {
for( ;; ) {
uint32_t events = 0;
events |= EV_1;
events |= EV_2;

xEventGroupWaitBits(eventGroupHandle, events, pdTRUE, pdTRUE, portMAX_DELAY);
printf("Task Main\r\n");
xEventGroupClearBits(eventGroupHandle, events);
}
}

void Task1(void *pvParameters) {
for( ;; ) {
vTaskDelay(1000 / portTICK_PERIOD_MS);
printf("Task 1\r\n");
xEventGroupSetBits(eventGroupHandle, EV_1);
}
}

void Task2(void *pvParameters) {
for( ;; ) {
vTaskDelay(2000 / portTICK_PERIOD_MS);
printf("Task 2\r\n");
xEventGroupSetBits(eventGroupHandle, EV_2);
}
}

esp32-s-freertos-eventgroup-01.png

参考