GPIO 4ESP32ISR onPress()01pressSerialserialLockTask Apriority 1Task Bpriority 2temp=21pressed1
Back: the ESP32 board (button, chip running the ISR, semaphore lamp, Serial port)Front: the mutex stand and its key, between task A and task BTokens: printed pieces, the key, the button's edge and the semaphore's 1Two FreeRTOS tasks on an ESP32 share one Serial port. Without a rule the port shows temp=pressed then 21; with the mutex serialLock, a task prints only while it holds the one key, and the port shows temp=21 then pressed. A button's interrupt service routine cannot wait, so it never touches the mutex: it gives the binary semaphore press with xSemaphoreGiveFromISR(), and portYIELD_FROM_ISR() switches straight to task B, which does the work.

Sharing with a mutex

  1. Task A prints temp= and then 21, in two calls. Task B prints pressed. If the scheduler runs B between A's two calls, you read temp=pressed, then 21. The port needs one user at a time.

Waking a task from an ISR

What happens

;; step 1/10 · shared
shared resource  one Serial port, two tasks
task A           Serial.print("temp="); Serial.println(21);
task B           Serial.println("pressed");
B runs between A's two calls:
  > temp=pressed
  > 21

Target: an ESP32 DevKit (Arduino-ESP32 core). The ESP32 has two cores; both tasks are pinned to core 1 with xTaskCreatePinnedToCore() so only one runs at a time, which is the story told here. The mutex works the same across cores. Inside an ISR use only the API calls whose names end in FromISR. Mutexes must not be used from ISRs at all, and xSemaphoreGive() must not be called from one.