/* TP Systeme Temps Reel - RM / EDF sous FreeRTOS (NUCLEO-C031C6, Wokwi)
* MODE_EDF = 0 : priorites fixes Rate Monotonic (3, 2, 1)
* MODE_EDF = 1 : EDF simule par priorites dynamiques */
#include <STM32FreeRTOS.h>
#include <queue.h>
#define MODE_EDF 0
typedef struct {
const char *nom;
uint8_t led;
TickType_t periode; // echeance relative = periode
TickType_t charge; // temps CPU simule (hypothese)
UBaseType_t priorite; // priorite initiale (RM)
volatile TickType_t echeance; // echeance absolue du travail courant
TaskHandle_t handle;
} Tache;
static Tache taches[3] = {
{ "T1", PA5, pdMS_TO_TICKS(100), pdMS_TO_TICKS(20), 3, 0, NULL },
{ "T2", PA6, pdMS_TO_TICKS(200), pdMS_TO_TICKS(40), 2, 0, NULL },
{ "T3", PA7, pdMS_TO_TICKS(400), pdMS_TO_TICKS(180), 1, 0, NULL },
};
typedef struct { const char *nom; TickType_t activation, fin, echeance; } Trace;
static QueueHandle_t fileTraces;
// Consomme du temps CPU : le temps pendant lequel la tache est preemptee ne compte pas.
static void travailler(TickType_t duree) {
TickType_t dernier = xTaskGetTickCount(), fait = 0;
while (fait < duree) {
TickType_t t = xTaskGetTickCount();
if (t != dernier) { fait++; dernier = t; }
}
}
static void tachePeriodique(void *param) {
Tache *tk = (Tache *)param;
TickType_t activation = 0; // toutes les taches demarrent a t = 0
for (;;) {
digitalWrite(tk->led, !digitalRead(tk->led));
travailler(tk->charge);
Trace tr = { tk->nom, activation, xTaskGetTickCount(), activation + tk->periode };
xQueueSend(fileTraces, &tr, 0);
tk->echeance = activation + 2 * tk->periode; // echeance du travail suivant
vTaskDelayUntil(&activation, tk->periode);
}
}
#if MODE_EDF
// Vrai si a est plus urgente que b ; a echeance egale, le travail le plus ancien gagne.
static bool plusUrgente(const Tache *a, const Tache *b) {
if (a->echeance != b->echeance) return a->echeance < b->echeance;
return (a->echeance - a->periode) < (b->echeance - b->periode);
}
// Priorite 4 : s'execute avant les 3 taches a chaque instant d'activation (multiple de 100 ms).
static void ordonnanceurEDF(void *param) {
(void)param;
TickType_t reveil = 0;
for (;;) {
for (int i = 0; i < 3; i++) {
UBaseType_t prio = 3;
for (int j = 0; j < 3; j++)
if (j != i && plusUrgente(&taches[j], &taches[i])) prio--;
vTaskPrioritySet(taches[i].handle, prio);
}
vTaskDelayUntil(&reveil, pdMS_TO_TICKS(100));
}
}
#endif
// Priorite 0 : affiche les traces pendant le temps libre, sans perturber les taches.
static void tacheAffichage(void *param) {
(void)param;
Trace tr;
for (;;) {
if (xQueueReceive(fileTraces, &tr, portMAX_DELAY) == pdTRUE) {
Serial.print(tr.nom);
Serial.print(" activation="); Serial.print((unsigned long)tr.activation);
Serial.print(" fin="); Serial.print((unsigned long)tr.fin);
Serial.print(" echeance="); Serial.print((unsigned long)tr.echeance);
Serial.println(tr.fin <= tr.echeance ? " OK" : " RATEE");
}
}
}
void setup() {
Serial.begin(115200);
Serial.println(MODE_EDF ? "Mode : EDF" : "Mode : RM");
fileTraces = xQueueCreate(16, sizeof(Trace));
for (int i = 0; i < 3; i++) {
pinMode(taches[i].led, OUTPUT);
taches[i].echeance = taches[i].periode;
xTaskCreate(tachePeriodique, taches[i].nom, 128, &taches[i],
taches[i].priorite, &taches[i].handle);
}
xTaskCreate(tacheAffichage, "Log", 192, NULL, tskIDLE_PRIORITY, NULL);
#if MODE_EDF
xTaskCreate(ordonnanceurEDF, "EDF", 128, NULL, 4, NULL);
#endif
vTaskStartScheduler();
Serial.println("Erreur : memoire insuffisante");
}
void loop() {}
Loading
st-nucleo-c031c6
st-nucleo-c031c6