#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
void tledR(void* p);
void tledV(void* p);
TaskHandle_t HLR;
TaskHandle_t HLV;
QueueHandle_t Q;
SemaphoreHandle_t SyncSemaphore; // Déclaration du sémaphore pour synchronisation
GPIO_PinState etat;
void MX_GPIO_Init(void);
void SystemClock_Config(void);
int main(void)
{
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
// Création du sémaphore avec un compteur initial de 0
SyncSemaphore = xSemaphoreCreateBinary();;
xTaskCreate(tledV, "tledV", 128, NULL, 1, &HLV);
xTaskCreate(tledR, "tledR", 128, NULL, 1, &HLR);
Q = xQueueCreate(10, sizeof(GPIO_PinState));
vTaskStartScheduler();
while (1)
{
// Votre code principal
}
}
void tledV(void *pvParameters)
{
while (1)
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_SET);
etat = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0);
xQueueSendToBack(Q, &etat, 200);
// Libération du sémaphore pour signaler que la tâche V a terminé
xSemaphoreGive(SyncSemaphore);
vTaskDelay(5000);
}
}
void tledR(void *pvParameters)
{
GPIO_PinState Vcase;
while (1)
{
// Attendre que la tâche V ait terminé
vTaskDelay(3000);
if (xSemaphoreTake(SyncSemaphore, portMAX_DELAY) == pdTRUE) {
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_RESET);
xQueueReceive(Q, &Vcase, 200);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, Vcase);
vTaskDelay(2000);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
}
}
}
void MX_GPIO_Init(void)
{
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct = {0};
/*Configure GPIO pins : PA0, PA1, PA4 */
GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1;
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;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
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
}
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 ralentissement de FreeRTOS
void vApplicationIdleHook(void)
{
// Fonction de ralentissement de FreeRTOS
}