FreeRTOS interrupt to task with a semaphore
A button interrupt gives a semaphore from the ISR; a task wakes, debounces the press and toggles an LED.
RTOS · Advanced
In a real product: Industrial controller (Industrial): An interrupt hands work to a task, so the ISR stays short.
An interrupt service routine should be short: no printing, no waiting, no debouncing. The standard RTOS pattern is to do the bare minimum in the ISR, signal a task, and do the real work in the task. Here a button interrupt gives a binary semaphore, and a task that was blocked on it wakes up to handle the press.
What you'll learn
- Attaching an interrupt to a pin with
attachInterrupt - Giving a semaphore from an ISR with
xSemaphoreGiveFromISR, the only kind of RTOS call allowed there - Deferring work from an ISR to a task
- Debouncing in a task, where waiting is allowed
How it works
The button on pin 2 (INT0) pulls the pin LOW when pressed. On each falling edge, onPress runs and gives pressed, a binary semaphore. A binary semaphore holds at most one "token", so the many edges a bouncing contact makes collapse into one.
buttonTask waits in xSemaphoreTake, using no CPU. When the ISR gives the semaphore the task wakes up, waits 30 ms for the contacts to settle, and checks that the button is still down. If it is, the press is real: it toggles the LED and prints the count. Finally it takes any token left over from the bounce, without waiting, so the bounce doesn't count as a second press.
xSemaphoreGiveFromISR reports in woken whether a task of higher priority than the interrupted one is now ready. If so, portYIELD_FROM_ISR() switches to it as the ISR returns, instead of at the next tick. (On ARM ports the macro takes woken as its argument; the AVR port's takes none.)
A heartbeat task blinks the on-board LED to show that the rest of the system keeps running while presses are handled.
Try this
- Watch the Tasks panel as you press the button:
buttonwakes the moment the ISR gives the semaphore, between two of the heartbeat's runs. - Remove the debounce delay and the final
xSemaphoreTake(pressed, 0)and count the presses one click makes. - Turn the button's bounce off in its properties and see whether the debounce is still needed.
- Count presses in the ISR with a
volatilevariable instead, and print it from the heartbeat. What can go wrong when printing a multi-byte counter that an ISR changes? - Replace the semaphore with a task notification (
vTaskNotifyGiveFromISRandulTaskNotifyTake).
sketch.ino
// A button interrupt gives a semaphore; a task wakes up, debounces the press and toggles the LED.
#include <Arduino_FreeRTOS.h>
#include <semphr.h>
const int BUTTON = 2; // INT0
const int LED = 12;
const int HEARTBEAT = 13; // the on-board LED
SemaphoreHandle_t pressed;
// The ISR does as little as possible: it signals the task and returns.
void onPress() {
BaseType_t woken = pdFALSE;
xSemaphoreGiveFromISR(pressed, &woken);
if (woken) portYIELD_FROM_ISR();
}
void buttonTask(void *) {
unsigned presses = 0;
pinMode(LED, OUTPUT);
for (;;) {
xSemaphoreTake(pressed, portMAX_DELAY); // sleep until the ISR signals
vTaskDelay(pdMS_TO_TICKS(30)); // let the contacts settle
if (digitalRead(BUTTON) == LOW) { // still pressed: a real press
presses++;
digitalWrite(LED, !digitalRead(LED));
Serial.print(F("press "));
Serial.println(presses);
}
xSemaphoreTake(pressed, 0); // drop a token the bounce left behind
}
}
void heartbeat(void *) {
pinMode(HEARTBEAT, OUTPUT);
for (;;) {
digitalWrite(HEARTBEAT, !digitalRead(HEARTBEAT));
vTaskDelay(pdMS_TO_TICKS(500));
}
}
void setup() {
Serial.begin(9600);
pinMode(BUTTON, INPUT_PULLUP);
pressed = xSemaphoreCreateBinary();
xTaskCreate(buttonTask, "button", 192, NULL, 2, NULL);
xTaskCreate(heartbeat, "beat", 96, NULL, 1, NULL);
attachInterrupt(digitalPinToInterrupt(BUTTON), onPress, FALLING);
Serial.println(F("Press the button"));
}
void loop() {}