A queue between tasks
Two jobs need to pass data safely. setup() calls xQueueCreate(3, sizeof(char)): the kernel sets aside a queue of 3 slots, each 1 byte. A queue is first in, first out (FIFO): items leave in the order they came.
The logger calls xQueueReceive with portMAX_DELAY: wait for as long as it takes. The queue is empty, so the logger becomes Blocked and uses no CPU time. The keys task is also Blocked, in its 20 ms delay. Core 1 runs the idle task.
Sending from a task
You type 7890. When its 20 ms delay ends, the keys task wakes and calls xQueueSend. The kernel copies '7' into slot 0: a copy, not a pointer. The logger turns Ready, but keys has priority 2, higher than 1, so keys keeps the CPU.
The loop sends '8' into slot 1 and '9' into slot 2. Each new item joins at the back. Now all 3 slots are full, and the logger is still waiting its turn for the CPU.
xQueueSend for '0' finds no free slot. Its last argument is how long to wait, in ticks; we passed 0, so it returns errQUEUE_FULL at once and the key is dropped. A longer wait, like pdMS_TO_TICKS(10), would block keys until a slot frees.
Keys goes back to its 20 ms delay and blocks. Now the logger is the highest-priority task that can run. Each xQueueReceive copies out the oldest item and frees its slot: '7', then '8', then '9'. Empty again, the logger blocks.
Sending from an ISR
You press the button. Its ISR interrupts the idle task. An ISR is not a task, so it can never block: it must use xQueueSendFromISR, which never waits. 'B' lands in slot 0: the queue is a ring, so writing wraps around.
The send woke the logger, priority 1, higher than the interrupted idle task, so woken became pdTRUE. portYIELD_FROM_ISR(woken) makes the interrupt return into the logger, which copies 'B' out of slot 0 and prints it. Without it, the logger could wait until the next tick.
What happens
;; step 1/8 · create
queue a FIFO box of fixed slots, owned by the kernel
line 1 q = xQueueCreate(3, sizeof(char));
3 slots x 1 byte
sender copies an item in at the back
reader copies the oldest item out at the front
// ESP32 DevKitC, Arduino core. Serial Monitor at 115200, "No line ending".
// Type keys to send them; press the button on GPIO 4 (to GND) to send B.
QueueHandle_t q;
const int BUTTON = 4;
void keysTask(void *) { // priority 2
for (;;) {
while (Serial.available()) {
char c = Serial.read();
if (xQueueSend(q, &c, 0) != pdPASS) // wait 0 ticks for space
Serial.println("queue full, key lost");
}
vTaskDelay(pdMS_TO_TICKS(20)); // Blocked for 20 ms
}
}
void loggerTask(void *) { // priority 1
char c;
for (;;) {
xQueueReceive(q, &c, portMAX_DELAY); // Blocked until an item comes
Serial.printf("got %c\n", c);
}
}
void ARDUINO_ISR_ATTR onButton() {
char c = 'B';
BaseType_t woken = pdFALSE;
xQueueSendFromISR(q, &c, &woken); // never waits
portYIELD_FROM_ISR(woken); // switch now if a higher task woke
}
void setup() {
Serial.begin(115200);
q = xQueueCreate(3, sizeof(char));
pinMode(BUTTON, INPUT_PULLUP);
attachInterrupt(BUTTON, onButton, FALLING);
xTaskCreatePinnedToCore(keysTask, "keys", 4096, NULL, 2, NULL, 1);
xTaskCreatePinnedToCore(loggerTask, "logger", 4096, NULL, 1, NULL, 1);
}
void loop() { vTaskDelay(portMAX_DELAY); } // loop() has nothing to do
The card shows the five queue calls only. The whole sketch, under the steps, wraps them in the keys task, the logger task, the button ISR and setup(). xQueueSend returns pdPASS when the item went in. The ESP32 has two cores and FreeRTOS runs an idle task on each. The sketch pins both tasks to core 1 so one core tells the story; the button interrupt and Wi-Fi details of the second core are left out.