-
为了更好的跟踪查看FreeRTOS系统的运行情况方便我们对系统运行的进一步了解,FreeRTOS系统自带了系统实时状态查询的函数vTaskList 和 vTaskGetRunTimeStats,分别用于查看系统任务的运行状态,任务栈的使用情况和CPU利用率等消息。
-
如何实现
-
打开相关的宏定义
FreeRTOSConfig.hFreeRTOSConfig.h #define configUSE_TRACE_FACILITY 1 //default 0 #define configGENERATE_RUN_TIME_STATS 1 //default 0 #define configUSE_STATS_FORMATTING_FUNCTIONS 1 //default 0 #define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() (Debug_DelayTicks = 0UL) #define portGET_RUN_TIME_COUNTER_VALUE() Debug_DelayTicks
-
定义一个高于系统节拍时钟的变量和初始化定时器
FreeRTOSConfig.hextern volatile unsigned long Debug_DelayTicks;
-
定义一个高于系统节拍时钟的变量和初始化定时器
#include "bsp_TiMbase.h" volatile unsigned long Debug_DelayTicks = 0UL;//定义计数变量 //定时器中断初始化 void TIMx_NVIC_Init(void) { NVIC_InitTypeDef NVIC_InitStructure; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); NVIC_InitStructure.NVIC_IRQChannel = TIM_IRQ; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 4; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } //定时器初始化 void TIMx_Init(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_APBxClock_FUN (TIM_CLK, ENABLE); TIM_TimeBaseStructure.TIM_Period=20000;//设置50us中断一次 TIM_TimeBaseStructure.TIM_Prescaler= 71; TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; TIM_TimeBaseInit(TIM, &TIM_TimeBaseStructure); TIM_ClearFlag(TIM, TIM_FLAG_Update); TIM_ITConfig(TIM,TIM_IT_Update,ENABLE); TIM_Cmd(TIM, ENABLE); TIM_APBxClock_FUN (TIM_CLK, ENABLE); ; }
-
串口初始化和重定义
bsp_usart.c#include <stdio.h> void USART3_NVIC_Init(void) { NVIC_InitTypeDef NVIC_InitStructure; /* Configure the NVIC Preemption Priority Bits */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); /* Enable the USARTy Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =3; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } void USART3_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; /* config USART3 clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); RCC_APB2PeriphClockCmd((RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO), ENABLE); /* USART3 GPIO config */ /* Configure USART3 Tx (PB.10) as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_Init(GPIOB, &GPIO_InitStructure); /* Configure USART3 Rx (PB.11) as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; // GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_Init(GPIOB, &GPIO_InitStructure); /* USART3 mode config */ USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No ; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART3, &USART_InitStructure); USART3_NVIC_Init(); USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); USART_Cmd(USART3, ENABLE); USART_ClearFlag(USART3, USART_FLAG_TC); } int fputc(int ch, FILE *f) { USART_SendData(USART3, (uint8_t) ch); while (USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET); return (ch); } int fgetc(FILE *f) { while (USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == RESET); return (int)USART_ReceiveData(USART3); }
-
-
如何实现-创建获取FreeRTOS状态的任务
main.cvoid vRTOS_RunTime_State(void *pvParameters) { unsigned char pcWriteBuffer[500]; unsigned long BaseSticks = 100UL; while(1) { if(Debug_DelayTicks % BaseSticks == 0) { printf("=================================================\r\n"); printf("任务名 任务状态 优先级 剩余栈 任务号 \r\n"); vTaskList((char *)&pcWriteBuffer); printf("%s\r\n", pcWriteBuffer); printf("\r\n任务名 运行计数 使用率\r\n"); vTaskGetRunTimeStats((char *)&pcWriteBuffer); printf("%s\r\n", pcWriteBuffer); printf("=================================================\r\n"); } vTaskDelay(30); } }
-
运行结果
任务名 任务状态 优先级 剩余栈 任务号 vRTOS_RunTime_S R 2 335 3 IDLE R 0 114 4 vTaskLED1 B 3 278 2 vTaskLED0 B 1 278 1 B-阻塞, R-就绪, D-删除, S-挂起 任务名 运行计数 使用率 vRTOS_RunTime_S 84 1% IDLE 8016 98% vTaskLED0 0 <1% vTaskLED1 0 <1%
> 参考 > 安富莱STM32 -V4 开发板 FreeRTOS教程 >FreeRTOS 内核实现与应用开发实战 > https://www.cnblogs.com/seifguo/p/9480935.html > https://blog.csdn.net/zhzht19861011/article/details/50717549
FreeRTOS基础教程:FreeRTOS系统实时状态获取
最新推荐文章于 2025-01-26 23:48:03 发布