#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "stm32c0xx_hal.h"
#include <stdio.h>
// Définition des pins pour les LEDs
#define LED1_PIN GPIO_PIN_5
#define LED2_PIN GPIO_PIN_6
#define LED3_PIN GPIO_PIN_7 // LED3
#define LED_PORT GPIOA
// Déclaration du mutex
SemaphoreHandle_t uartMutex;
// Fonction pour simuler l'envoi de messages UART
void UART_Send(const char *message) {
printf("%s", message);
}
// Tâche 1 - Envoie un message et contrôle les LEDs
void UART_Task1(void *pvParameters) {
for (;;) {
if (xSemaphoreTake(uartMutex, portMAX_DELAY)) { // ✔ Mutex pris
UART_Send("Task 1: Hello from UART!\r\n");
HAL_GPIO_WritePin(LED_PORT, LED1_PIN, GPIO_PIN_SET);
vTaskDelay(pdMS_TO_TICKS(500));
HAL_GPIO_WritePin(LED_PORT, LED1_PIN, GPIO_PIN_RESET);
xSemaphoreGive(uartMutex); // ✔ Mutex libéré
}
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
// Tâche 2 - Envoie un message et contrôle les LEDs
void UART_Task2(void *pvParameters) {
for (;;) {
if (xSemaphoreTake(uartMutex, portMAX_DELAY)) { // ✔ Mutex pris
UART_Send("Task 2: Message from UART\r\n");
HAL_GPIO_WritePin(LED_PORT, LED2_PIN, GPIO_PIN_SET);
vTaskDelay(pdMS_TO_TICKS(500));
HAL_GPIO_WritePin(LED_PORT, LED2_PIN, GPIO_PIN_RESET);
xSemaphoreGive(uartMutex); // ✔ Mutex libéré
}
vTaskDelay(pdMS_TO_TICKS(1500));
}
}
// TÂCHE 3
void UART_Task3(void *pvParameters) {
for (;;) {
if (xSemaphoreTake(uartMutex, portMAX_DELAY)) { // Prendre mutex
UART_Send("Task 3: Third UART Task + LED3\r\n");
HAL_GPIO_WritePin(LED_PORT, LED3_PIN, GPIO_PIN_SET);
vTaskDelay(pdMS_TO_TICKS(400));
HAL_GPIO_WritePin(LED_PORT, LED3_PIN, GPIO_PIN_RESET);
xSemaphoreGive(uartMutex); // Libérer mutex
}
vTaskDelay(pdMS_TO_TICKS(2000)); // Pause 2s
}
}
void MX_GPIO_Init(void) {
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct = {0};
// Initialiser LED1 (Pin 5)
GPIO_InitStruct.Pin = LED1_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LED_PORT, &GPIO_InitStruct);
// Initialiser LED2 (Pin 6)
GPIO_InitStruct.Pin = LED2_PIN;
HAL_GPIO_Init(LED_PORT, &GPIO_InitStruct);
// Initialiser LED3 (Pin 7)
GPIO_InitStruct.Pin = LED3_PIN;
HAL_GPIO_Init(LED_PORT, &GPIO_InitStruct);
}
int main(void) {
HAL_Init();
MX_GPIO_Init();
// Créer un mutex pour protéger l'accès à l'UART
uartMutex = xSemaphoreCreateMutex();
if (uartMutex != NULL) {
// Créer les tâches UART_Task1 et UART_Task2 avec des priorités différentes
xTaskCreate(UART_Task1, "UART Task 1", 256, NULL, 1, NULL);
xTaskCreate(UART_Task2, "UART Task 2", 256, NULL, 2, NULL);
// Création de la 3ᵉ tâche
xTaskCreate(UART_Task3, "UART Task 3", 256, NULL, 1, NULL);
vTaskStartScheduler();
}
while (1) {}
}
void loop() {
}