#include "stm32c0xx.h"
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#define LED_PIN GPIO_PIN_13
#define BUTTON_PIN GPIO_PIN_0
QueueHandle_t myQueueHandle;
// Initialisation des GPIOs
void GPIO_Init(void) {
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct = {0};
// Configuration du bouton poussoir
GPIO_InitStruct.Pin = BUTTON_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
// Configuration de la LED
GPIO_InitStruct.Pin = LED_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
// Assurez-vous que la LED est éteinte au démarrage
HAL_GPIO_WritePin(GPIOC, LED_PIN, GPIO_PIN_RESET);
}
// Tâche pour lire le bouton et envoyer un message dans la queue
void TaskButton(void *argument) {
uint8_t message = 1;
uint8_t lastState = GPIO_PIN_SET;
for (;;) {
uint8_t currentState = HAL_GPIO_ReadPin(GPIOA, BUTTON_PIN);
if (currentState == GPIO_PIN_RESET && lastState == GPIO_PIN_SET) {
vTaskDelay(pdMS_TO_TICKS(50));
if (HAL_GPIO_ReadPin(GPIOA, BUTTON_PIN) == GPIO_PIN_RESET) {
xQueueSendToBack(myQueueHandle, &message, 0);
message = !message;
}
}
lastState = currentState;
vTaskDelay(pdMS_TO_TICKS(10));
}
}
// Tâche pour contrôler la LED
void TaskLED(void *argument) {
uint8_t receivedMessage;
for (;;) {
if (xQueueReceive(myQueueHandle, &receivedMessage, portMAX_DELAY) == pdPASS) {
HAL_GPIO_WritePin(GPIOC, LED_PIN, receivedMessage ? GPIO_PIN_SET : GPIO_PIN_RESET);
}
}
}
int main(void) {
HAL_Init();
GPIO_Init();
myQueueHandle = xQueueCreate(5, sizeof(uint8_t));
if (myQueueHandle == NULL) {
while (1);
}
xTaskCreate(TaskButton, "ButtonTask", 128, NULL, 1, NULL);
xTaskCreate(TaskLED, "LEDTask", 128, NULL, 1, NULL);
vTaskStartScheduler();
while (1);
}
void loop() {
// Fonction vide pour satisfaire le compilateur
}