ESP32 Learn

FreeRTOS Basics: Task & Queue

2025-09-18 路 ~1 min read
Table of contents
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <Arduino.h>

QueueHandle_t q;
void producer(void * ) {
  for (;;) {
    int v = millis();
    xQueueSend(q, & v, portMAX_DELAY);
    vTaskDelay(1000 / portTICK_PERIOD_MS);
  }
}
void consumer(void * ) {
  for (;;) {
    int v;
    if (xQueueReceive(q, & v, portMAX_DELAY)) {
      Serial.println(v);
    }
  }
}
void setup() {
  Serial.begin(115200);
  q = xQueueCreate(5, sizeof(int));
  xTaskCreatePinnedToCore(producer, "prod", 4096, NULL, 1, NULL, 0);
  xTaskCreatePinnedToCore(consumer, "cons", 4096, NULL, 1, NULL, 1);
}
void loop() {}

馃敄 #freertos #task #queue