ESP32 - data transmission method between tasks

1. The transmission data is a small number of bytes

1. Signal amount

The semaphore itself is implemented based on a queue, so it takes up more memory, and it can only be sent and received once, and cleared after reading.

Binary semaphore: It is mainly used for task synchronization, and can also be used to pass a change flag between tasks.

Counting semaphore: It is mainly used for task synchronization, and can also be used to pass a counting event between tasks.

2. Event bits and event groups

It is mainly used for synchronization between tasks and interrupts. It works in broadcast mode (one send and multiple receive), and you can choose to clear or not clear after reading.

Can also be used to pass status signals (8 or 24 bits) between tasks.

3. Task notification

It can be used not only for inter-task synchronization, but also for transferring 32bit data.

It can be used to replace binary semaphores, counting semaphores, and event groups, and it occupies less memory.

3.1 Application example 1 of notification (lightweight notification, suitable for binary or counting semaphore)

//定义通知句柄
static TaskHandle_t Rx_NotifyHandle;
//Websocket事件中发送通知
xTaskNotifyGive(Rx_NotifyHandle);
//websocket_rx_task任务中接收通知
ulTaskNotifyTake(pdTRUE,portMAX_DELAY);
//创建带有通知句柄参数的接收任务websocket_rx_task
xTaskCreate(websocket_rx_task, "websocket_rx_task", 1500*2, NULL, configMAX_PRIORITIES-3, &Rx_NotifyHandle);

3.1 Application example 2 of notification (complete notification function)

//定义通知句柄
TaskHandle_t UartTxTask_NotifyHandle;
//在另一个任务中发送通知
xTaskNotify(UartTxTask_NotifyHandle,0x0100,eSetBits);
//下面是接收通知任务的相关内容
uint32_t ulNotifiedValue = 0;
//等待通知,这里是不等待
xTaskNotifyWait(0, ULONG_MAX,&ulNotifiedValue, pdMS_TO_TICKS(0)); 
//是要等待的通知吗
if((ulNotifiedValue & 0x0100) == 0x0100){
    //添加自己的代码
}
//创建含有通知句柄参数的任务uart2_tx_task
xTaskCreate(uart2_tx_task, "uart2_tx_task", bufSize*2, NULL, configMAX_PRIORITIES-5, &UartTxTask_NotifyHandle); 
The following two functions are used to send from the ISR interrupt service routine
void vTaskNotifyGiveFromISR( TaskHandle_t xTaskToNotify,
                              BaseType_t *pxHigherPriorityTaskWoken );

BaseType_t xTaskNotifyFromISR( TaskHandle_t xTaskToNotify,
                                uint32_t ulValue,
                                eNotifyAction eAction,
                                BaseType_t *pxHigherPriorityTaskWoken );

Second, the transmission data is a large number of bytes

1. Queue

Queues are the most basic and flexible way to communicate between tasks. The disadvantage is that the length is fixed.

2. StreamBuffer

Stream buffers are good for high-volume data interactions, but only for one-to-one applications.

3. MessageBuffer

The message buffer is implemented based on the stream buffer, with a length tag, and reads one message at a time.

I personally think that it is more suitable for transmitting data through serial port, network port, WIFI, etc.

Guess you like

Origin blog.csdn.net/tsliuch/article/details/125311007