1, Using STM32CubeMX to build a project
1. Open the software
2. Create project based on chip
3. Select the corresponding chip
- Here I use stm32f429igt6
4. Set GPIO
5. Set serial port
6. Set the clock to 180MHz
- Internal clock is selected here. If you need external clock, please customize the configuration
7. Project setting
8. Generate project, compile, run and verify
2, TencentOS tiny file migration
1. Get source code
- Official open source address: https://github.com/Tencent/TencentOS-tiny
2. Download it to the local area and extract it as shown in the figure
3. Copy the source file to the mdk project directory
- Open the project directory generated by STM32CubeMX, add the TencentOS tiny folder, and copy the three folders in step 2 to this folder
4. Add configuration directory and file
- The content of the header file is as follows. Note to modify the name of the imported header file:
#ifndef _TOS_CONFIG_H_ #define _TOS_CONFIG_H_ #include "stm32f4xx_hal.h" // Target chip header file, users need to change according to the situation #define TOS_CFG_TASK_PRIO_MAX 10u // Configure the default maximum number of priorities supported by TencentOS tiny #define TOS_CFG_ROUND_ROBIN_EN 0u // Configure whether the kernel of TencentOS tiny turns on time slice rotation #define TOS_CFG_OBJECT_VERIFY_EN 1u // Configure TencentOS tiny to check whether the pointer is legal #define TOS_CFG_TASK_DYNAMIC_CREATE_EN 1u // TencentOS tiny dynamic task creation function macro #define TOS_CFG_EVENT_EN 1u // TencentOS tiny event module function macro #define TOS_CFG_MMBLK_EN 1u //Configure whether the memory block management module is enabled by TencentOS tiny #define TOS_CFG_MMHEAP_EN 1u //Configure whether TencentOS tiny turns on dynamic memory module #define TOS_CFG_MMHEAP_DEFAULT_POOL_EN 1u // TencentOS tiny default dynamic memory pool function macro #define TOS_CFG_MMHEAP_DEFAULT_POOL_SIZE 0x100 // Configure the default dynamic memory pool size of TencentOS tiny #define TOS_CFG_MUTEX_EN 1u // Configure whether or not TencentOS tiny turns on the mutex module #define TOS_CFG_MESSAGE_QUEUE_EN 1u // Configure whether the message queuing module is enabled by TencentOS tiny #define TOS_CFG_MAIL_QUEUE_EN 1u // Configure whether the message mailbox module is enabled by TencentOS tiny #define TOS_CFG_PRIORITY_MESSAGE_QUEUE_EN 1u // Configure whether the priority message queuing module is enabled by TencentOS tiny #define TOS_CFG_PRIORITY_MAIL_QUEUE_EN 1u // Configure whether the priority message mailbox module is enabled by TencentOS tiny #define TOS_CFG_TIMER_EN 1u // Configure whether the software timer module is enabled by TencentOS tiny #define TOS_CFG_PWR_MGR_EN 0u // Configure whether TencentOS tiny turns on peripheral power management module #define TOS_CFG_TICKLESS_EN 0u // Configure Tickless low power module switch #define TOS_CFG_SEM_EN 1u // Configure whether the semaphore module is enabled by TencentOS tiny #define TOS_CFG_TASK_STACK_DRAUGHT_DEPTH_DETACT_EN 1u // Configure whether task stack depth detection is enabled for TencentOS tiny #define TOS_CFG_FAULT_BACKTRACE_EN 0u // Configure whether TencentOS tiny enables exception stack backtracking #define TOS_CFG_IDLE_TASK_STK_SIZE 128u // Configure the TencentOS tiny idle task stack size #define TOS_CFG_CPU_TICK_PER_SECOND 1000u // Configure the tick frequency of TencentOS tiny #define TOS_CFG_CPU_CLOCK (SystemCoreClock) // Configure TencentOS tiny CPU frequency #define TOS_CFG_TIMER_AS_PROC 1u // Configure whether TIMER is configured as function mode #endif
3, MDK project group directory and header file settings
1. Add group directory
- tos/arch
- tos/kernel
- tos/cmsis_os
-
tos/config
2. Add tos/arch group file
3. Add tos/kernel grouping file
4. Add TOS / CMSIS? OS group file
5. Add tos/config group file
6. Add header file path
- Add the header file path as follows
4, Modify MDK source code and create TencentOS tiny task
1. Modify stm32f4xx? It. C interrupt configuration file
- Add the following code
/* USER CODE BEGIN Includes */ #include "tos_k.h" /* USER CODE END Includes */
- Modify the PendSV_Handler function
__weak void PendSV_Handler(void) { /* USER CODE BEGIN PendSV_IRQn 0 */ /* USER CODE END PendSV_IRQn 0 */ /* USER CODE BEGIN PendSV_IRQn 1 */ /* USER CODE END PendSV_IRQn 1 */ }
- Modify systick'u handler function
void SysTick_Handler(void) { /* USER CODE BEGIN SysTick_IRQn 0 */ /* USER CODE END SysTick_IRQn 0 */ HAL_IncTick(); /* USER CODE BEGIN SysTick_IRQn 1 */ if (tos_knl_is_running()) { tos_knl_irq_enter(); tos_tick_handler(); tos_knl_irq_leave(); } /* USER CODE END SysTick_IRQn 1 */ }
2. Create a new task in the main.c function
- The source code of main.c is as follows
/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** * @attention * * <h2><center>© Copyright (c) 2020 STMicroelectronics. * All rights reserved.</center></h2> * * This software component is licensed by ST under BSD 3-Clause license, * the "License"; You may not use this file except in compliance with the * License. You may obtain a copy of the License at: * opensource.org/licenses/BSD-3-Clause * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" #include "usart.h" #include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "cmsis_os.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ //task1 #define TASK1_STK_SIZE 256 void task1(void *pdata); osThreadDef(task1, osPriorityNormal, 1, TASK1_STK_SIZE); //task2 #define TASK2_STK_SIZE 256 void task2(void *pdata); osThreadDef(task2, osPriorityNormal, 1, TASK2_STK_SIZE); /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ void task1(void *pdata) { int count = 1; while(1) { printf("\r\nHello world!\r\n###This is task1 ,count is %d \r\n", count++); HAL_GPIO_TogglePin(GPIOH,GPIO_PIN_10); osDelay(500); } } void task2(void *pdata) { int count = 1; while(1) { printf("\r\nHello TencentOS !\r\n***This is task2 ,count is %d \r\n", count++); osDelay(1000); } } int fputc(int ch, FILE *f) { if (ch == '\n') { HAL_UART_Transmit(&huart1, (void *)"\r", 1,30000); } HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF); return ch; } /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ 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(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_USART1_UART_Init(); /* USER CODE BEGIN 2 */ osKernelInitialize(); //TOS Tiny kernel initialize osThreadCreate(osThread(task1), NULL);// Create task1 osThreadCreate(osThread(task2), NULL);// Create task2 osKernelStart();//Start TOS Tiny /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Configure the main internal regulator output voltage */ __HAL_RCC_PWR_CLK_ENABLE(); __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); /** Initializes the CPU, AHB and APB busses clocks */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; RCC_OscInitStruct.PLL.PLLM = 8; RCC_OscInitStruct.PLL.PLLN = 180; RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; RCC_OscInitStruct.PLL.PLLQ = 4; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Activate the Over-Drive mode */ if (HAL_PWREx_EnableOverDrive() != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB busses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) { Error_Handler(); } } /* USER CODE BEGIN 4 */ /* USER CODE END 4 */ /** * @brief This function is executed in case of error occurrence. * @retval None */ void Error_Handler(void) { /* USER CODE BEGIN Error_Handler_Debug */ /* User can add his own implementation to report the HAL error return state */ /* USER CODE END Error_Handler_Debug */ } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */ void assert_failed(uint8_t *file, uint32_t line) { /* USER CODE BEGIN 6 */ /* User can add his own implementation to report the file name and line number, tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
3. Observation
- Task 1 and task 2 run alternately