#include "stm32c0xx_hal.h"
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h" // Ajout de l'en-tête pour la file d'attente
#include "semphr.h" // Ajout de l'en-tête pour le sémaphore
// Déclarations des fonctions pour les tâches LED
void tledR(void* p);
void tledV(void* p);
void tledB(void* p);
void tledO(void* p); // Déclaration de la tâche LED orange
void tledY(void* p); // Déclaration de la tâche LED jaune
// Déclarations des handles de tâche
TaskHandle_t HLR;
TaskHandle_t HLV;
TaskHandle_t HLB;
TaskHandle_t HLO; // Handle de la tâche LED orange
TaskHandle_t HLY; // Handle de la tâche LED jaune
// Déclaration du handle de sémaphore
SemaphoreHandle_t S = NULL;
void MX_GPIO_Init(void);
void SystemClock_Config(void);
int main(void)
{
HAL_Init(); // Initialisation de la bibliothèque HAL
/* Configuration de l'horloge du système */
SystemClock_Config();
/* Initialisation de tous les périphériques configurés */
MX_GPIO_Init();
/* Création du sémaphore avant les tâches pour éviter les conditions de concurrence */
S = xSemaphoreCreateBinary();
if (S != NULL) {
/* Donne initialement le sémaphore pour qu'une des tâches puisse le prendre */
xSemaphoreGive(S);
/* Création des tâches */
xTaskCreate(tledV, "tledV", 128, NULL, 1, &HLV);
xTaskCreate(tledR, "tledR", 128, NULL, 1, &HLR);
xTaskCreate(tledB, "tledB", 128, NULL, 1, &HLB);
xTaskCreate(tledO, "tledO", 128, NULL, 1, &HLO); // Création de la tâche LED orange
xTaskCreate(tledY, "tledY", 128, NULL, 1, &HLY); // Création de la tâche LED jaune
/* Démarrage du planificateur */
vTaskStartScheduler();
}
while (1)
{
// Votre code principal (ne devrait jamais être atteint car le planificateur prend le relais)
}
}
// Fonction pour la tâche LED verte
void tledV(void* p) {
while (1) {
if (xSemaphoreTake(S, portMAX_DELAY) == pdTRUE) { // Prendre le sémaphore
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_SET); // Allumer la LED verte
HAL_Delay(1000); // Attendre 1 seconde
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_RESET); // Éteindre la LED verte
xSemaphoreGive(S); // Relâcher le sémaphore
vTaskDelay(100); // Attendre un peu avant de reprendre pour permettre aux autres tâches de s'exécuter
}
}
}
// Fonction pour la tâche LED rouge
void tledR(void* p) {
while (1) {
if (xSemaphoreTake(S, portMAX_DELAY) == pdTRUE) { // Prendre le sémaphore
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET); // Allumer la LED rouge
HAL_Delay(1000); // Attendre 1 seconde
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET); // Éteindre la LED rouge
xSemaphoreGive(S); // Relâcher le sémaphore
vTaskDelay(100); // Attendre un peu avant de reprendre pour permettre aux autres tâches de s'exécuter
}
}
}
// Fonction pour la tâche LED bleue
void tledB(void* p) {
while (1) {
if (xSemaphoreTake(S, portMAX_DELAY) == pdTRUE) { // Prendre le sémaphore
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET); // Allumer la LED bleue
HAL_Delay(2000); // Attendre 2 secondes
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET); // Éteindre la LED bleue
xSemaphoreGive(S); // Relâcher le sémaphore
vTaskDelay(100); // Attendre un peu avant de reprendre pour permettre aux autres tâches de s'exécuter
}
}
}
// Fonction pour la tâche LED orange
void tledO(void* p) {
while (1) {
if (xSemaphoreTake(S, portMAX_DELAY) == pdTRUE) { // Prendre le sémaphore
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, GPIO_PIN_SET); // Allumer la LED orange
HAL_Delay(1500); // Attendre 1.5 seconde
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, GPIO_PIN_RESET); // Éteindre la LED orange
xSemaphoreGive(S); // Relâcher le sémaphore
vTaskDelay(100); // Attendre un peu avant de reprendre pour permettre aux autres tâches de s'exécuter
}
}
}
// Fonction pour la tâche LED jaune
void tledY(void* p) {
while (1) {
if (xSemaphoreTake(S, portMAX_DELAY) == pdTRUE) { // Prendre le sémaphore
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11, GPIO_PIN_SET); // Allumer la LED jaune
HAL_Delay(500); // Attendre 0.5 seconde
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11, GPIO_PIN_RESET); // Éteindre la LED jaune
xSemaphoreGive(S); // Relâcher le sémaphore
vTaskDelay(100); // Attendre un peu avant de reprendre pour permettre aux autres tâches de s'exécuter
}
}
}
void MX_GPIO_Init(void)
{
/* Activation des horloges des ports GPIO */
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* Configuration des broches GPIO : PA0, PA1, PA4, PA11, PA12 */
GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_4 | GPIO_PIN_11 | GPIO_PIN_12;
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);
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/* Configuration de l'oscillateur HSI */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
// Gérer l'erreur de manière appropriée
}
/* Configuration des horloges */
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
// Gérer l'erreur de manière appropriée
}
}
// Fonction de hook d'inactivité de FreeRTOS
void vApplicationIdleHook(void)
{
// Fonction de hook d'inactivité de FreeRTOS (peut être utilisée pour économiser de l'énergie)
}