#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 troisiéme semaphore
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 troisiéme semaphore
if (xSemaphoreLED1 == NULL || xSemaphoreLED2 == NULL || xSemaphoreLED3 == NULL) {
while (1); // Erreur
}
// Création des tâches
xTaskCreate(BlinkLED1_Task, "BlinkLED1", 128, NULL, 2, 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 (prend initialement le sémaphore)
vTaskStartScheduler();
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 3, permettant à la tâche associée de s'exécuter
xSemaphoreGive(xSemaphoreLED3);
}
}
}
// Tâche pour faire clignoter la LED 3
void BlinkLED3_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 LED3. Si le sémaphore est déjà pris, la tâche attend jusqu'à ce qu'il soit libéré.
if (xSemaphoreTake(xSemaphoreLED3, portMAX_DELAY) == pdTRUE) {
// Allume la LED 3 (connectée sur GPIO_PIN_7)
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_SET);
// La tâche attend pendant 750 ms (0.75 seconde)
vTaskDelay(pdMS_TO_TICKS(750)); // Durée d'allumage
// Éteint la LED 3
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET);
// Libère le sémaphore pour la LED 3, permettant à la tâche associée de s'exécuter
xSemaphoreGive(xSemaphoreLED1); // Retour à la 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);
GPIO_InitStruct.Pin = GPIO_PIN_7;
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)
}