#include "FreeRTOS.h"
#include "task.h"
#include "stm32c0xx_hal.h"
#include "semphr.h"
SemaphoreHandle_t xSemaphoreLED1;//declarer le premier semaphore
SemaphoreHandle_t xSemaphoreLED2;//declarer le deuxième semaphore
SemaphoreHandle_t xSemaphoreLED3;//declarer le 3éme semaphore ajouter
void BlinkLED1_Task(void *pvParameters);
void BlinkLED2_Task(void *pvParameters);
void BlinkLED3_Task(void *pvParameters);
int main(void) {
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
xSemaphoreLED1 = xSemaphoreCreateBinary();//créer le premier semaphore
xSemaphoreLED2 = xSemaphoreCreateBinary();//créer le deuxième semaphore
xSemaphoreLED3 = xSemaphoreCreateBinary();//créer le 3eme semaphore ajouter
if (xSemaphoreLED1 == NULL || xSemaphoreLED2 == NULL) {
while (1); // Erreur
}
// Créer les tâches
xTaskCreate(BlinkLED1_Task, "BlinkLED1", 128, NULL, 1, NULL);
xTaskCreate(BlinkLED2_Task, "BlinkLED2", 128, NULL, 1, NULL);
xTaskCreate(BlinkLED3_Task, "BlinkLED3", 128, NULL, 1, NULL);
xSemaphoreGive(xSemaphoreLED1); // Initialisation du sémaphore de LED1
vTaskStartScheduler();// Démarrer le scheduler FreeRTOS
while (1);
}
// Tâche pour faire clignoter la LED 1
void BlinkLED1_Task(void *pvParameters) {
// Boucle infinie pour maintenir la tâche en cours d'exécution
for (;;) {
// Tente de prendre le sémaphore pour accéder à la LED1. Si le sémaphore est déjà pris, la tâche attend jusqu'à ce qu'il soit libéré.
if (xSemaphoreTake(xSemaphoreLED1, portMAX_DELAY) == pdTRUE) {
// Allume la LED 1 (connectée sur GPIO_PIN_5)
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
// La tâche attend pendant 1000 ms (1 seconde)
vTaskDelay(pdMS_TO_TICKS(1000)); // Convertit les millisecondes en ticks de l'OS FreeRTOS
// Éteint la LED 1
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET) ;
// Libère le sémaphore pour la LED 2, permettant à la tâche associée de s'exécuter
xSemaphoreGive(xSemaphoreLED2);
}
}
}
// Tâche pour faire clignoter la LED 2
void BlinkLED2_Task(void *pvParameters) {
// Boucle infinie pour maintenir la tâche en cours d'exécution
for (;;) {
// Tente de prendre le sémaphore pour accéder à la LED2. Si le sémaphore est déjà pris, la tâche attend jusqu'à ce qu'il soit libéré.
if (xSemaphoreTake(xSemaphoreLED2, portMAX_DELAY ) == pdTRUE) {
// Allume la LED 2 (connectée sur GPIO_PIN_6)
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_SET);
// La tâche attend pendant 500 ms (0.5 seconde)
vTaskDelay(pdMS_TO_TICKS(500)); // Convertit les millisecondes en ticks de l'OS FreeRTOS
// Éteint la LED 2
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_RESET);
// Libère le sémaphore pour la LED 1, permettant à la tâche associée de s'exécuter
xSemaphoreGive(xSemaphoreLED1);
}
}
}
// Ajoutez une troisième tâche pour une LED3 supplémentaire.
void BlinkLED3_Task(void *pvParameters) {
for (;;) {
if (xSemaphoreTake(xSemaphoreLED3, portMAX_DELAY) == pdTRUE) {
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_SET);
vTaskDelay(pdMS_TO_TICKS(700));
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET);
xSemaphoreGive(xSemaphoreLED1); // boucle vers LED1
}
}
}
void MX_GPIO_Init(void) {
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_6;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
void loop(void) {
// Rien ici, car FreeRTOS gère les tâches
}
void SystemClock_Config(void) {
// Configuration d'horloge (ajuster selon votre MCU)
}