#include <Arduino_FreeRTOS.h>
// Déclaration des identifiants de tâches et du mutex
TaskHandle_t taskHighPriority;
TaskHandle_t taskLowPriority;
SemaphoreHandle_t xMutex;
// Fonctions des tâches
void taskHigh(void *pvParameters);
void taskLow(void *pvParameters);
void setup() {
Serial.begin(115200);
// Création du mutex
xMutex = xSemaphoreCreateMutex();
if (xMutex != NULL) {
Serial.println("Mutex created");
}
// Création des tâches
xTaskCreate(taskHigh, "HighTask", 128, NULL, 2, &taskHighPriority); // Priorité haute (2)
xTaskCreate(taskLow, "LowTask", 128, NULL, 1, &taskLowPriority); // Priorité basse (1)
}
void loop() {
// La boucle loop() d'Arduino est vide car les tâches sont gérées par FreeRTOS
}
void taskHigh(void *pvParameters) {
while (1) {
if (xSemaphoreTake(xMutex, 1000 / portTICK_PERIOD_MS) == pdTRUE) { // Attente du mutex pendant 1 seconde
Serial.println("High priority task got the mutex");
vTaskDelay(1000 / portTICK_PERIOD_MS); // Traitement de la ressource pendant 1 seconde
xSemaphoreGive(xMutex); // Rendre le mutex
} else {
Serial.println("High priority task couldn't get the mutex");
}
}
}
void taskLow(void *pvParameters) {
while (1) {
if (xSemaphoreTake(xMutex, 2000 / portTICK_PERIOD_MS) == pdTRUE) { // Attente du mutex pendant 2 secondes
Serial.println("Low priority task got the mutex");
vTaskDelay(2000 / portTICK_PERIOD_MS); // Traitement de la ressource pendant 2 secondes
xSemaphoreGive(xMutex); // Rendre le mutex
} else {
Serial.println("Low priority task couldn't get the mutex");
}
}
}