ESP32-S开发 之 FreeRTOS Notification

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

本文的主线 信号 => 简介 => 通知 => 问题

信号

1
2
3
4
python
>>> import os
>>> print(os.getpid())
81062
1
kill -3 81062
1
2
man kill
kill - send a signal to a process

简介

A direct to task notification is an event sent directly to a task, rather than indirectly to a task via an intermediary object such as a queue, event group or semaphore. Sending a direct to task notification to a task sets the state of the target task notification to ‘pending’. Just as a task can block on an intermediary object such as a semaphore to wait for that semaphore to be available, a task can block on a task notification to wait for that notification’s state to become pending

  • Performance Benefits
1
Unblocking an RTOS task with a direct notification is 45% faster * and uses less RAM than unblocking a task using an intermediary object such as a binary semaphore
  • Usage Restrictions
1
RTOS task notifications can only be used when there is only one task that can be the recipient of the event

通知

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
#include <Arduino.h>

void sender(void *pvParameters);
void receiver(void *pvParameters);

TaskHandle_t receiverHandler = NULL;

void setup() {
xTaskCreate(&sender, "sender", 2048, NULL, 1, NULL);
xTaskCreate(&receiver, "receiver", 2048, NULL, 1, &receiverHandler);
}

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

void sender(void *pvParameters) {
for( ;; ) {
if (receiverHandler == NULL) {
vTaskDelay(1000 / portTICK_PERIOD_MS);
continue;
}

xTaskNotify(receiverHandler, (1 << 0), eSetBits);
xTaskNotify(receiverHandler, (1 << 1), eSetBits);
vTaskDelay(1000 / portTICK_PERIOD_MS);
xTaskNotify(receiverHandler, (1 << 2), eSetBits);
xTaskNotify(receiverHandler, (1 << 3), eSetBits);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}

void receiver(void *pvParameters) {
uint32_t state;

for( ;; ) {
xTaskNotifyWait(0xffffffff, 0, &state, portMAX_DELAY);
printf("received state %d times\n", state);
}
}

esp32-s-freertos-notification-01.png

问题

when can we not use task notificatin as binary semaphore

1
One key difference is that any task can wait for an take a binary semaphore, but only the specific task can wait for a specific notification, so a direct to task notification can’t replace a semaphore if you don’t know what task is going to want to wait for it, or perhaps you need to publish somewhere what task is waiting for the event.

参考