Best Synchronization Techniques for Task Communication in FreeRTOS on ESP32
Hi guys i'm developing a real-time sensor data display system on an ESP32 microcontroller using FreeRTOS. The project involves two main tasks:
TaskA handles sensor data collection and processing while TaskB performs calculations and displays the results on an LCD.
To ensure smooth operation, I need to synchronize their execution so that:
TaskA doesn’t send data to TaskB until the data is fully ready and TaskB doesn’t process or display any data until it has been correctly received from TaskA.
Code :
QueueHandle_t xQueue;
void TaskA(void *pvParameters) {
int sensorData = 0;
while (1) {
sensorData = collectSensorData();
xQueueSend(xQueue, &sensorData, portMAX_DELAY);
vTaskDelay(pdMS_TO_TICKS(100));
}
}
void TaskB(void *pvParameters) {
int receivedData = 0;
while (1) {
if (xQueueReceive(xQueue, &receivedData, portMAX_DELAY)) {
displayDataOnLCD(receivedData);
}
}
}
void setup() {
xQueue = xQueueCreate(10, sizeof(int));
xTaskCreate(TaskA, "Task A", 2048, NULL, 2, NULL);
xTaskCreate(TaskB, "Task B", 2048, NULL, 1, NULL);
vTaskStartScheduler();
}
TaskA is assigned a higher priority to ensure timely data collection. My primary goal is to minimize latency and guarantee reliable data transfer between these two tasks. What is the best synchronization technique in FreeRTOS to achieve this, ensuring that data is transferred efficiently and processed only when it's ready?
1 Reply
look into mutexes