1,使用FreeRTOS
2,最大任务32个,可在FreeRTOSConfig中修改
3,默认创建两个任务
4,串口FIFO,任务1默认功能为打印串口1接收到的数据
5,频率180M,频率不同可能造成无法下载的问题
由于文件大小限制,通用库函数需要自行添加
单片机源程序如下:
- #include "includes.h"
- #include "bsp.h"
- void SystemClock_Config(void);
- //任务函数
- void start_task(void *pvParameters);
- void task1_task(void *pvParameters);
- void task2_task(void *pvParameters);
- void task3_task(void *pvParameters);
- void task4_task(void *pvParameters);
- void task5_task(void *pvParameters);
- void task6_task(void *pvParameters);
- void task7_task(void *pvParameters);
- void task8_task(void *pvParameters);
- //任务句柄
- TaskHandle_t StartTask_Handler = NULL;
- TaskHandle_t Task1Task_Handler = NULL;
- TaskHandle_t Task2Task_Handler = NULL;
- TaskHandle_t Task3Task_Handler = NULL;
- TaskHandle_t Task4Task_Handler = NULL;
- TaskHandle_t Task5Task_Handler = NULL;
- TaskHandle_t Task6Task_Handler = NULL;
- TaskHandle_t Task7Task_Handler = NULL;
- TaskHandle_t Task8Task_Handler = NULL;
- //任务优先级,最大32个优先级,最多32个认为
- #define StartTask_PRO 1
- #define Task1_PRO 2
- #define Task2_PRO 3
- #define Task3_PRO 4
- #define Task4_PRO 5
- #define Task5_PRO 6
- #define Task6_PRO 7
- #define Task7_PRO 8
- #define Task8_PRO 9
- //任务延迟
- #define TASK1_DELAY 50
- #define TASK2_DELAY 100
- #define TASK3_DELAY 50
- #define TASK4_DELAY 50
- #define TASK5_DELAY 50
- #define TASK6_DELAY 50
- #define TASK7_DELAY 50
- #define TASK8_DELAY 5000
- //任务堆栈大小,默认为512
- #define TASK_STACK_SIZE 64*8
- int main(void)
- {
- HAL_Init();
- SystemClock_Config();
- delay_init(SystemCoreClock/100000);
- BSP_Init();
- //创建开始任务
- xTaskCreate((TaskFunction_t )start_task, //任务函数
- (const char* )"start_task", //任务名称
- (uint16_t )TASK_STACK_SIZE, //任务堆栈大小
- (void* )NULL, //传递给任务函数的参数
- (UBaseType_t )StartTask_PRO, //任务优先级
- (TaskHandle_t* )&StartTask_Handler); //任务句柄
-
- vTaskStartScheduler(); //开启任务调度
- }
- //task1任务函数
- void task1_task(void *pvParameters)
- {
- uint8_t buflen;
- uint8_t buf[64];
- while(1)
- {
- HAL_GPIO_WritePin(GPIOB,GPIO_PIN_7,GPIO_PIN_SET);
-
- #if SYS_DEBUG
- printf("F429-1\r\n");
- printf(" \r\n");
- #endif
-
-
- buflen=comReadBuf(COM1,buf);
- if(buflen>0)
- {
- printf("BUFLEN:%d\r\n",buflen);
- comSendBuf(COM1,buf,buflen);
- printf("\r\n");
- buflen=0;
- comClearRxFifo(COM1);
- }
- vTaskDelay(TASK1_DELAY);
- }
-
- }
- void start_task(void *pvParameters)
- {
- while(1)
- {
- //进入临界区
- taskENTER_CRITICAL();
-
- //创建TASK1任务
- xTaskCreate((TaskFunction_t ) task1_task,
- (const char* ) "task1_task",
- (uint16_t ) TASK_STACK_SIZE,
- (void* ) NULL,
- (UBaseType_t ) Task1_PRO,
- (TaskHandle_t* ) &Task1Task_Handler);
-
-
-
-
-
-
-
-
-
-
- vTaskDelete(StartTask_Handler); //删除开始任务
- taskEXIT_CRITICAL(); //退出临界区
-
- }
- }
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
- __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_HSE;
- RCC_OscInitStruct.HSEState = RCC_HSE_ON;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- RCC_OscInitStruct.PLL.PLLM = 25;
- RCC_OscInitStruct.PLL.PLLN = 360;
- RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
- RCC_OscInitStruct.PLL.PLLQ = 4;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- }
- /** Activate the Over-Drive mode
- */
- if (HAL_PWREx_EnableOverDrive() != HAL_OK)
- {
- }
- /** 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)
- {
- }
- }
- #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 */
复制代码
所有资料51hei提供下载:
F429_RTOS.zip
(43.62 KB, 下载次数: 39)
|