In the last one Sending data through USART in STM32F0 programming with STM32Cube+Keil5 Based on that, I want to use the operating system to complete the experiment of USART sending string in the task.
Setting change of STM32CubeMX
- Enable FreeRTOS in Pinout
- Configure FreeRTOS in Configuration
- Add tasks
- Generate code
Edit code in Keil5
- main function
int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration----------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* Hardware abstraction layer */ /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* Clock */ /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); /* STM32CubeMX GPIO initialization function generated */ MX_USART1_UART_Init(); /* STM32CubeMX Generated USART1 initialization function */ MX_USART2_UART_Init(); /* STM32CubeMX Generated USART2 initialization function */ /* USER CODE BEGIN 2 */ printf("\n\r Creating Task1 \n\r"); /* The statement I added. The character output from the serial port after the initialization of the serial port.*/ /* USER CODE END 2 */ /* Call init function for freertos objects (in freertos.c) */ MX_FREERTOS_Init(); /* STM32CubeMX Generated operating system kernel initialization function */ /* Start scheduler */ osKernelStart(); /* Start the scheduler, which controls the task execution. The following while() statement is out of reach*/ /* We should never get here as control is now taken by the scheduler */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ }
- freertos.c
Add include header file
/* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "stm32f0xx_hal.h" / * I added it, otherwise I don't know printf*/ /* USER CODE END Includes */
Task handle defined by STM32CubeMX
osThreadId defaultTaskHandle; /* Default idle task handle */ osThreadId myTask1Handle; /* myTask 1 task handle I need */
Task function prototype declared by STM32CubeMX
void StartDefaultTask(void const * argument); void StartTask1(void const * argument);
Tasks defined and created by STM32CubeMX
/* definition and creation of defaultTask */ osThreadDef(defaultTask, StartDefaultTask, osPriorityIdle, 0, 128); defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL); /* definition and creation of myTask1 */ osThreadDef(myTask1, StartTask1, osPriorityNormal, 0, 128); myTask1Handle = osThreadCreate(osThread(myTask1), NULL);
I didn't do anything in my spare time
void StartDefaultTask(void const * argument) { /* USER CODE BEGIN StartDefaultTask */ /* Infinite loop */ for(;;) { osDelay(1); } /* USER CODE END StartDefaultTask */ }
Task 1 output string
void StartTask1(void const * argument) { /* USER CODE BEGIN StartTask1 */ /* Infinite loop */ for(;;) { printf("Task 1 is runing \r\n"); /* The printf has been redirected to USART2. Please refer to "sending data through USART by programming STM32F0 with STM32Cube+Keil5" (https://blog.csdn.net/aleif2p6/article/details/82918298) */ osDelay(3000); /* Delay 3 s */ } /* USER CODE END StartTask1 */ }
- Compile link Project/Build Target
- Burn program Flash/Download
- Operation effect