/* USER CODE BEGIN Header */ /** ****************************************************************************** * File Name : freertos.c * Description : Code for freertos applications ****************************************************************************** * @attention * * Copyright (c) 2024 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "FreeRTOS.h" #include "task.h" #include "main.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include #include "ensi_uart.h" #include "queue.h" #include "semphr.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ typedef enum{ COMMAND_NOP, COMMAND_ON, COMMAND_OFF, COMMAND_TOGGLE, COMMAND_BLINK, COMMAND_HELP, COMMAND_UNKOWN } command_t; /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ #define COMMAND_QUEUE_DEPTH 10 #define MAX_COMMAND_LENGTH 10 #define BLINK_PERIOD_ms 50 #define TEXT_ON_COMMAND "on" #define TEXT_OFF_COMMAND "off" #define TEXT_TOGGLE_COMMAND "toggle" #define TEXT_BLINK_COMMAND "blink" #define TEXT_HELP_COMMAND "help" /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN Variables */ QueueHandle_t command_queue; SemaphoreHandle_t help_semaph; /* USER CODE END Variables */ /* Private function prototypes -----------------------------------------------*/ /* USER CODE BEGIN FunctionPrototypes */ void app_init(void); static void console_task(void* pvParameters); static void led_control_task(void* pvParameters); static void help_menu_task(void* pvParameters); /* USER CODE END FunctionPrototypes */ /* Private application code --------------------------------------------------*/ /* USER CODE BEGIN Application */ void app_init(void) { xTaskCreate( console_task, "Tache console",configMINIMAL_STACK_SIZE, NULL, 2, NULL); xTaskCreate( help_menu_task, "Tache help", configMINIMAL_STACK_SIZE, NULL, 3, NULL); xTaskCreate( led_control_task, "Tache LED",configMINIMAL_STACK_SIZE, NULL, 4, NULL); command_queue = xQueueCreate( COMMAND_QUEUE_DEPTH, sizeof(uint8_t) ); help_semaph = xSemaphoreCreateBinary(); xSemaphoreGive( help_semaph ); // À décommenter pour afficher le menu d'aide au démarrage } void console_task( void* pvParameters ) { char user_input[MAX_COMMAND_LENGTH]; command_t binary_command; while(1) { ENSI_UART_PutString((uint8_t*)"LED control# "); ENSI_UART_GetString((uint8_t*)user_input); ENSI_UART_PutString((uint8_t*)"\r\n"); if( !strcmp(user_input, TEXT_ON_COMMAND) ) { binary_command = COMMAND_ON; } else if( !strcmp(user_input, TEXT_OFF_COMMAND) ) { binary_command = COMMAND_OFF; } else if( !strcmp(user_input, TEXT_TOGGLE_COMMAND) ) { binary_command = COMMAND_TOGGLE; } else if( !strcmp(user_input, TEXT_BLINK_COMMAND) ) { binary_command = COMMAND_BLINK; } else if( !strcmp(user_input, TEXT_HELP_COMMAND) ) { binary_command = COMMAND_HELP; xSemaphoreGive( help_semaph ); } else { binary_command = COMMAND_UNKOWN; ENSI_UART_PutString((uint8_t*)"> Command not implemented\r\n"); } xQueueSend( command_queue, &binary_command, portMAX_DELAY ); } } void led_control_task( void* pvParameters ) { command_t command; uint8_t must_blink = pdFALSE; while(1) { if( xQueueReceive( command_queue, &command, BLINK_PERIOD_ms ) == pdTRUE ) { switch( command ) { case COMMAND_ON: HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, SET); must_blink = pdFALSE; break; case COMMAND_OFF: HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, RESET); must_blink = pdFALSE; break; case COMMAND_TOGGLE: HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin); must_blink = pdFALSE; break; case COMMAND_BLINK: HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin); must_blink = pdTRUE; break; default: break; } } else { if( must_blink ) { HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin); } } } } void help_menu_task( void* pvParameters ) { while(1) { xSemaphoreTake( help_semaph, portMAX_DELAY ); ENSI_UART_PutString((uint8_t*)"*** HELP MENU ***\r\n"); ENSI_UART_PutString((uint8_t*)"\t on - turn on the LED\r\n"); ENSI_UART_PutString((uint8_t*)"\t off - turn off the LED\r\n"); ENSI_UART_PutString((uint8_t*)"\t toggle - change the LED state\r\n"); ENSI_UART_PutString((uint8_t*)"\t blink - make the LED blink at 10 Hz\r\n"); ENSI_UART_PutString((uint8_t*)"\t help - open this menu\r\n"); ENSI_UART_PutString((uint8_t*)"\t note: other commands can be implemented!\r\n"); } } /* USER CODE END Application */