CubeMX配置
开启红框中3个选项,都设置为Enable.
设置一个定时器,定时时间10us~50us,freertos一个时间片为1ms, 定时中断计数用于统计当前运行哪个任务。
代码
/* USER CODE BEGIN 1 */
/* Functions needed when configGENERATE_RUN_TIME_STATS is on */
__weak void configureTimerForRunTimeStats(void)
{
ulHighFrequencyTimerTicks = 0ul;
}
__weak unsigned long getRunTimeCounterValue(void)
{
return ulHighFrequencyTimerTicks;
}
/* USER CODE END 1 */
在FreeRTOSConfig.h声明变量
#if defined(__ICCARM__) || defined(__CC_ARM) || defined(__GNUC__)
#include <stdint.h>
extern uint32_t SystemCoreClock;
/* USER CODE BEGIN 0 */
extern volatile uint32_t ulHighFrequencyTimerTicks;
extern void configureTimerForRunTimeStats(void);
extern unsigned long getRunTimeCounterValue(void);
/* USER CODE END 0 */
#endif
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
/* USER CODE BEGIN Callback 0 */
/* USER CODE END Callback 0 */
if (htim->Instance == TIM7) {
HAL_IncTick();
}
/* USER CODE BEGIN Callback 1 */
#ifdef REPORT_MSG //报告系统负载信息
else if(htim->Instance == TIM6)
ulHighFrequencyTimerTicks++;
#endif
/* USER CODE END Callback 1 */
}
打印任务:
#ifdef REPORT_MSG //报告系统负载信息
uint8_t pcWriteBuffer[500];
const uint8_t TaskShow1[34]="\r\n任务\t\t状态\t优先级\t剩余栈\t序号\r\n",TaskShow2[30]="\r\n任务\t\t运行计数\tCPU 使用率\r\n";
#endif
/* USER CODE END Header_StartTaskMSG */
void StartTaskMSG(void const * argument)
{
/* USER CODE BEGIN StartTaskMSG */
#ifdef REPORT_MSG //报告系统负载信息
HAL_TIM_Base_Start_IT(&htim6);
/* Infinite loop */
for(;;)
{
vTaskList((char *)&pcWriteBuffer);
osMutexWait(myMutexPrintMsgHandle, portMAX_DELAY);
printf("%s\r\n",TaskShow1);
printf("%s\r\n",pcWriteBuffer);
osMutexRelease(myMutexPrintMsgHandle);
memset(pcWriteBuffer,' ',sizeof(pcWriteBuffer));
vTaskGetRunTimeStats((char *)&pcWriteBuffer);
osMutexWait(myMutexPrintMsgHandle, portMAX_DELAY);
printf("%s\r\n",TaskShow2);
printf("%s\r\n",pcWriteBuffer);
osMutexRelease(myMutexPrintMsgHandle);
memset(pcWriteBuffer,' ',sizeof(pcWriteBuffer));
osDelay(1000);
}
#endif
/* USER CODE END StartTaskMSG */
}