写数据端wirteTask()写入一个数据到邮箱队列中,readTask()从邮箱队列中读取数据,可以通过写数据的方式来控制readTask()读数据端的执行方向
#include <stdio.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_spi_flash.h"
#include "freertos/queue.h"
void wirteTask(void *pvParam)
{
QueueHandle_t Mailbox;
Mailbox = (QueueHandle_t)pvParam;
BaseType_t xStatus;
int i = 0;
while (1)
{
xStatus = xQueueOverwrite(Mailbox, &i);
if (xStatus != pdPASS)
{
printf("send fail\n");
}
else
{
printf("send done\n");
}
i++;
vTaskDelay(6000 / portTICK_PERIOD_MS);
}
}
void readTask(void *pvParam)
{
QueueHandle_t Mailbox;
Mailbox = (QueueHandle_t)pvParam;
BaseType_t xStatus;
int i;
while (1)
{
xStatus = xQueuePeek(Mailbox, &i, portMAX_DELAY);
if (xStatus != pdPASS)
{
printf("read fail\n");
}
else
{
printf("read i=%d\n", i);
}
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
void app_main(void)
{
QueueHandle_t Mailbox;
Mailbox = xQueueCreate(1, sizeof(int));
if (Mailbox != NULL)
{
printf("create queue successfully\n");
xTaskCreate(wirteTask, "wirteTask", 1024 * 5, (void *)Mailbox, 1, NULL);
xTaskCreate(readTask, "readTask1", 1024 * 5, (void *)Mailbox, 2, NULL);
xTaskCreate(readTask, "readTask2", 1024 * 5, (void *)Mailbox, 2, NULL);
xTaskCreate(readTask, "readTask3", 1024 * 5, (void *)Mailbox, 2, NULL);
}
else
{
printf("can not create queue\n");
}
}