3. 3.2 Sémaphores binaires et counting
Sémaphore binaire : signal d'événement entre tâches (0 ou 1). Typiquement : une ISR signale une tâche de traitement.
#include "freertos/semphr.h"
SemaphoreHandle_t xSemButton = xSemaphoreCreateBinary();
// Dans l'ISR (interruption bouton)
void IRAM_ATTR button_isr_handler(void *arg) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
xSemaphoreGiveFromISR(xSemButton, &xHigherPriorityTaskWoken);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
// Dans la tâche qui attend le bouton
void task_button_handler(void *pv) {
while (1) {
if (xSemaphoreTake(xSemButton, portMAX_DELAY) == pdTRUE) {
ESP_LOGI("BUTTON", "Button pressed!");
}
}
}
Sémaphore counting : compte des ressources disponibles (ex : 3 slots DMA libres, 4 connexions HTTP simultanées).
SemaphoreHandle_t xSemSlots = xSemaphoreCreateCounting(3, 3); // max=3, initial=3
xSemaphoreTake(xSemSlots, portMAX_DELAY); // prend un slot (décrémente)
// ... utilise la ressource
xSemaphoreGive(xSemSlots); // libère le slot (incrémente)