B站Michale_ee——ESP32_IDF SDK——FreeRTOS_6 任务通知同步、任务通知值
一、任务通知同步
1.API简介
(1)释放任务通知
(2)获取任务通知
2.示例代码及运行结果
#include <stdio.h>
#include <inttypes.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_chip_info.h"
#include "esp_flash.h"
#include "esp_system.h"static TaskHandle_t xTask1 = NULL, xTask2 = NULL;void Task1(void *pvparam)
{while(1){printf("Task1 wait notificaiton!\n");ulTaskNotifyTake( pdTRUE, portMAX_DELAY );printf("Task1 got notificaiton!\n");vTaskDelay(pdMS_TO_TICKS(3000));}
}void Task2(void *pvparam)
{while(1){vTaskDelay(pdMS_TO_TICKS(5000));printf("Task2 notify Task1\n");xTaskNotifyGive( xTask1 );}
}void app_main(void)
{vTaskSuspendAll();xTaskCreatePinnedToCore(Task1, "Task1", 1024*5, NULL, 1, &xTask1, 1); //! ESP32-S3为双核,CPU0主要运行WiFi和蓝牙;CPU1用于运行应用程序;xTaskCreatePinnedToCore(Task2, "Task2", 1024*5, NULL, 1, &xTask2, 1);xTaskResumeAll();}
二、任务通知值
1.API简介
(1)等待任务通知
(2)发送任务通知
2.示例代码及运行结果
#include <stdio.h>
#include <inttypes.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_chip_info.h"
#include "esp_flash.h"
#include "esp_system.h"static TaskHandle_t xTask1 = NULL, xTask2 = NULL;void Task1(void *pvparam)
{uint32_t ulNotifiedValue;while(1){printf("Task1 wait notificaiton!\n");xTaskNotifyWait( 0x00, /* Don't clear any notification bits on entry. */ULONG_MAX, /* Reset the notification value to 0 on exit. */&ulNotifiedValue, /* Notified value pass out in ulNotifiedValue. */portMAX_DELAY ); /* Block indefinitely. */if( ( ulNotifiedValue & 0x01 ) != 0 ){/* Bit 0 was set - process whichever event is represented by bit 0. */printf("Task1 process bit0 event!\n");}if( ( ulNotifiedValue & 0x02 ) != 0 ){/* Bit 1 was set - process whichever event is represented by bit 1. */printf("Task1 process bit1 event!\n");}if( ( ulNotifiedValue & 0x04 ) != 0 ){/* Bit 2 was set - process whichever event is represented by bit 2. */printf("Task1 process bit2 event!\n");}}
}void Task2(void *pvparam)
{while(1){printf("Task2 notify Bit0\n");xTaskNotify( xTask1, 0x01, eSetValueWithOverwrite);vTaskDelay(pdMS_TO_TICKS(5000));printf("Task2 notify Bit1\n");xTaskNotify( xTask1, 0x02, eSetValueWithOverwrite);vTaskDelay(pdMS_TO_TICKS(5000));printf("Task2 notify Bit2\n");xTaskNotify( xTask1, 0x04, eSetValueWithOverwrite);vTaskDelay(pdMS_TO_TICKS(5000));}
}void app_main(void)
{vTaskSuspendAll();xTaskCreatePinnedToCore(Task1, "Task1", 1024*5, NULL, 1, &xTask1, 1); //! ESP32-S3为双核,CPU0主要运行WiFi和蓝牙;CPU1用于运行应用程序;xTaskCreatePinnedToCore(Task2, "Task2", 1024*5, NULL, 1, &xTask2, 1);xTaskResumeAll();}