【Home treasure】 esp32s2 freertos communication between tasks xQueue
[Copy link]
This post was last edited by damiaa on 2022-9-14 11:29
【Home treasure】 esp32s2 freertos communication between tasks xQueue
If we want to send a set of data or information to another task, we can use a queue. The advantage of using a queue is that the queue can transmit more data. But the queues must be in the same data format!
We can specify the length of the queue so that the party reading the queue has enough time to read. The queue must have a party to join the queue and a party to take out, otherwise it will be full soon.
For more information, see:
https://docs.espressif.com/projects/esp-idf/zh_CN/latest/esp32/search.html?q=+xQueue&check_keywords=yes&area=default
The following is the specific experimental code of xQueue:
#include <stdio.h>
#include <string.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"//如果使用 xqueue 这个头文件必须
#include "esp_system.h"
#include "esp_spi_flash.h"
//创建队列消息的结构
struct AMessage
{
char ucMessageID;
char ucData[ 20 ];
} xMessage;
//创建队列的句柄
QueueHandle_t xQueue;
// Task to create a queue and post a value.
void vATask( void *pvParameters )
{
static uint8_t id=0;
struct AMessage *pxMessage;
// Create a queue capable of containing 10 pointers to AMessage structures.
// These should be passed by pointer as they contain a lot of data.
//创建队列
xQueue = xQueueCreate( 10, sizeof( struct AMessage * ) );
if( xQueue == 0 ){
// Failed to create the queue.
printf("xQueue Create fail!\n");
}
else
printf("xQueue Create success\n");
// ...
size_t xBytesSent;
uint8_t ucArrayToSend[] = { '0', '1', '2', '3' ,0};
char *pcStringToSend = "String to send";
const TickType_t x100ms = pdMS_TO_TICKS( 100 );
while(1)
{
xMessage.ucMessageID =id++;
//for(i=0;i<9;i++)
// xMessage.ucData =i+0x41;
char str[20]={'M','E','S','S','A','G','E',':',',','0','1','2','3','4','5','6','7','8','9',0};
strcpy(xMessage.ucData,str);
//发送队列消息
pxMessage = & xMessage;
xQueueSend( xQueue, ( void * ) &pxMessage, ( TickType_t ) 0 );
vTaskDelay(3000 / portTICK_PERIOD_MS);
}
}
// Task to receive from the queue.
void vADifferentTask( void *pvParameters )
{
struct AMessage *pxRxedMessage;
while(1)
{
if( xQueue != 0 )
{
//接收队列消息
if( xQueueReceive( xQueue, &( pxRxedMessage ), ( TickType_t ) 10 ) )
{
printf("RxQueueMessage: id is %d,Data is %s\n",pxRxedMessage->ucMessageID,&pxRxedMessage->ucData);
}
}
}
}
void app_main(void)
{
printf("Hello world!\n");
/* Print chip information */
esp_chip_info_t chip_info;
esp_chip_info(&chip_info);
xTaskCreate(vATask, "vATask", 1024*2, NULL, configMAX_PRIORITIES-3, NULL);
xTaskCreate(vADifferentTask, "vADifferentTask", 1024*2, NULL, configMAX_PRIORITIES-4, NULL);
while(1){
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
|