#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "stm32c0xx_hal.h"
#include <stdio.h>
// Définition des pins pour les LEDs
#define LED
HAL_GPIO_WritePin(LED_PORT, LED1_PIN, GPIO_PIN_SET);
vTaskDelay(pdMS_TO_TICKS(500)); // LED1 allumée pendant 500ms
HAL_GPIO_WritePin(LED_PORT, LED1_PIN, GPIO_PIN_RESET);
xSemaphoreGive(uartMutex); // Libérer le mutex
}
vTaskDelay(pdMS_TO_TICKS(1000)); // Pause de 1 seconde
}
}
// Tâche 2 - Priorité moyenne
void UART_Task2(void *pvParameters) {
for (;;) {
if (xSemaphoreTake(uartMutex, portMAX_DELAY)) {
UART_Send("Task 2: Message from UART Task 2\r\n");
// Contrôle LED2
HAL_GPIO_WritePin(LED_PORT, LED2_PIN, GPIO_PIN_SET);
vTaskDelay(pdMS_TO_TICKS(500)); // LED2 allumée pendant 500ms
HAL_GPIO_WritePin(LED_PORT, LED2_PIN, GPIO_PIN_RESET);
xSemaphoreGive(uartMutex); // Libérer le mutex
}
vTaskDelay(pdMS_TO_TICKS(1500)); // Pause de 1,5 seconde
}
}
// Tâche 3 - Priorité haute
void UART_Task3(void *pvParameters) {
for (;;) {
if (xSemaphoreTake(uartMutex, portMAX_DELAY)) {
UART_Send("Task 3: Message from UART Task 3\r\n");
// Contrôle LED3
HAL_GPIO_WritePin(LED_PORT, LED3_PIN, GPIO_PIN_SET);
vTaskDelay(pdMS_TO_TICKS(500)); // LED3 allumée pendant 500ms
HAL_GPIO_WritePin(LED_PORT, LED3_PIN, GPIO_PIN_RESET);
xSemaphoreGive(uartMutex); // Libérer le mutex
}
vTaskDelay(pdMS_TO_TICKS(2000)); // Pause de 2 secondes
}
}
// Initialisation des GPIOs pour les LEDs
void MX_GPIO_Init(void) {
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct = {0};
// Initialiser LED1, LED2 et LED3
GPIO_InitStruct.Pin = LED1_PIN | LED2_PIN | LED3_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);
}
int main(void) {
HAL_Init();
MX_GPIO_Init(); // Initialisation des GPIOs pour les LEDs
// Créer un mutex pour protéger l'accès à l'UART
uartMutex = xSemaphoreCreateMutex();
if (uartMutex != NULL) {
// Créer les tâches avec des priorités différentes
xTaskCreate(UART_Task1, "UART Task 1", 256, NULL, 1, NULL); // Priorité 1 pour Task 1
xTaskCreate(UART_Task2, "UART Task 2", 256, NULL, 2, NULL); // Priorité 2 pour Task 2
xTaskCreate(UART_Task3, "UART Task 3", 256, NULL, 3, NULL); // Priorité 3 pour Task 3
// Démarrer le scheduler FreeRTOS
vTaskStartScheduler();
}
// Si le système arrive ici, cela signifie que la mémoire est insuffisante
while (1) {
}
}
// Fonction loop obligatoire pour STM32duino
void loop() {
// Vide car FreeRTOS gère les tâches
}
Loading
st-nucleo-c031c6
st-nucleo-c031c6