TaskHandle_t receiverTaskHandle = NULL;
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
const int buttonPin = 4;
void IRAM_ATTR button_isr() {
xHigherPriorityTaskWoken = pdFALSE;
vTaskNotifyGiveFromISR(receiverTaskHandle, &xHigherPriorityTaskWoken);
if (xHigherPriorityTaskWoken) {
portYIELD_FROM_ISR();
}
}
void senderTask(void *parameter) {
while (1) {
if (receiverTaskHandle != NULL) {
Serial.println("Sender Task: Enviando notificação periódica...");
xTaskNotifyGive(receiverTaskHandle);
}
vTaskDelay(pdMS_TO_TICKS(3000));
}
}
void receiverTask(void *parameter) {
while (1) {
Serial.println("Receiver Task: Aguardando notificação...");
uint32_t notificationCount = ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
if (notificationCount > 0) {
Serial.print("Receiver Task: Notificação recebida! Contador: ");
Serial.println(notificationCount);
Serial.println("------------------------------------");
}
}
}
void setup() {
Serial.begin(115200);
vTaskDelay(pdMS_TO_TICKS(1000));
Serial.println("--- Exemplo de Notificação de Tarefa FreeRTOS ---");
pinMode(buttonPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(buttonPin), button_isr, FALLING);
xTaskCreatePinnedToCore(
receiverTask,
"Receiver Task",
2048,
NULL,
2,
&receiverTaskHandle,
1);
xTaskCreatePinnedToCore(
senderTask,
"Sender Task",
2048,
NULL,
1,
NULL,
0);
Serial.println("Setup concluído. Tarefas e interrupção configuradas.");
Serial.println("Pressione o botão do GPIO 4 para enviar uma notificação via ISR.");
}
void loop() {
vTaskDelay(portMAX_DELAY);
}