目录
修改FreeRTOS代码
main.c中添加
uint8_t CPU_RunInfo[400];
volatile uint32_t CPU_RunTime;
FreeRTOSconfig.h中修改
/* Run time and task stats gathering related definitions. */
#define configGENERATE_RUN_TIME_STATS 0
#define configUSE_TRACE_FACILITY 0
#define configUSE_STATS_FORMATTING_FUNCTIONS 0
///修改为
/* Run time and task stats gathering related definitions. */
#define configGENERATE_RUN_TIME_STATS 1
#define configUSE_TRACE_FACILITY 1
#define configUSE_STATS_FORMATTING_FUNCTIONS 1
extern volatile uint32_t CPU_RunTime;
#define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() (CPU_RunTime = 0ul)
#define portGET_RUN_TIME_COUNTER_VALUE() CPU_RunTime
创建任务
/*创建任务*/
void rtos_start(void)
{
xTaskCreate( (TaskFunction_t ) LED1_Task,
( char * ) "LED1_Task",
(const configSTACK_DEPTH_TYPE) configMINIMAL_STACK_SIZE ,
(void * ) NULL,
(UBaseType_t ) 14,
(TaskHandle_t *) NULL ) ;
xTaskCreate( (TaskFunction_t ) LED2_Task,
( char * ) "LED2_Task",
(const configSTACK_DEPTH_TYPE) configMINIMAL_STACK_SIZE ,
(void * ) NULL,
(UBaseType_t ) 14,
(TaskHandle_t *) NULL ) ;
xTaskCreate( (TaskFunction_t ) LED3_Task,
( char * ) "LED3_Task",
(const configSTACK_DEPTH_TYPE) configMINIMAL_STACK_SIZE ,
(void * ) NULL,
(UBaseType_t ) 14,
(TaskHandle_t *) NULL ) ;
xTaskCreate( (TaskFunction_t ) LED4_Task,
( char * ) "LED4_Task",
(const configSTACK_DEPTH_TYPE) configMINIMAL_STACK_SIZE ,
(void * ) NULL,
(UBaseType_t ) 14,
(TaskHandle_t *) NULL ) ;
xTaskCreate( (TaskFunction_t ) main_Task,
( char * ) "main_Task",
(const configSTACK_DEPTH_TYPE) configMINIMAL_STACK_SIZE ,
(void * ) NULL,
(UBaseType_t ) 14,
(TaskHandle_t *) NULL ) ;
vTaskStartScheduler();
}
编写任务函数
void LED1_Task( void *pvParameters )
{
(void) pvParameters;
uint8_t count = 0;
while(1)
{
count++;
if(20 == count)
{
count = 0;
u1_printf("LED1_Task running & !\r\n");
}
PINS_DRV_TogglePins(PTD, 1 << 0);
vTaskDelay(800);
}
}
void LED2_Task( void *pvParameters )
{
(void) pvParameters;
uint8_t count = 0;
while(1)
{
count++;
if(20 == count)
{
count = 0;
u1_printf("LED2_Task running & !\r\n");
}
PINS_DRV_TogglePins(PTD, 1 << 1);
vTaskDelay(400);
}
}
void LED3_Task( void *pvParameters )
{
(void) pvParameters;
uint8_t count = 0;
while(1)
{
count++;
if(20 == count)
{
count = 0;
u1_printf("LED3_Task running & !\r\n");
}
PINS_DRV_TogglePins(PTD, 1 << 15);
vTaskDelay(200);
}
}
void LED4_Task( void *pvParameters )
{
(void) pvParameters;
uint8_t count = 0;
while(1)
{
count++;
if(20 == count)
{
count = 0;
u1_printf("LED4_Task running & !\r\n");
}
PINS_DRV_TogglePins(PTD, 1 << 16);
vTaskDelay(100);
}
}
TaskHandle_t TEST_task_handler;
/*创建一个任务并删除自身*/
void TEST_Task( void *pvParameters )
{
(void) pvParameters;
taskENTER_CRITICAL();
u1_printf("test Task running & delete!\r\n");
vTaskDelete(TEST_task_handler);
taskEXIT_CRITICAL();
}
/*主循环*/
void main_Task( void *pvParameters )
{
(void) pvParameters;
uint32_t pinstate;
ftm_state_t ftmStateStruct;
/* 初始化FTM模块 */
FTM_DRV_Init(INST_FLEXTIMER_MC1, &flexTimer_mc1_InitConfig,&ftmStateStruct);
/* 配置并使定时器能溢出中断 */
INT_SYS_InstallHandler(FTM0_Ovf_Reload_IRQn,&ftmTimerISR,(isr_t*) 0U);
INT_SYS_EnableIRQ(FTM0_Ovf_Reload_IRQn);
/* 初始化计数器 */
FTM_DRV_InitCounter(INST_FLEXTIMER_MC1, &flexTimer_mc1_TimerConfig);
FTM_DRV_CounterStart(INST_FLEXTIMER_MC1);
while(1)
{
pinstate = KEY_Proc (0);
if(pinstate ==BTN1_PRES )
{
xTaskCreate( (TaskFunction_t ) TEST_Task,
( char * ) "TEST_Task",
(const configSTACK_DEPTH_TYPE) configMINIMAL_STACK_SIZE ,
(void * ) NULL,
(UBaseType_t ) 1,
(TaskHandle_t *) TEST_task_handler ) ;
taskENTER_CRITICAL();
Printf_CPU_RunInfo();
taskEXIT_CRITICAL();
}
vTaskDelay(20);
}
}
添加定时器模块
配置定时器模块初始化,并注册中断函数
ftm_state_t ftmStateStruct;
/* 初始化FTM模块 */
FTM_DRV_Init(INST_FLEXTIMER_MC1, &flexTimer_mc1_InitConfig,&ftmStateStruct);
/* 配置并使定时器能溢出中断 */
INT_SYS_InstallHandler(FTM0_Ovf_Reload_IRQn,&ftmTimerISR,(isr_t*) 0U);
INT_SYS_EnableIRQ(FTM0_Ovf_Reload_IRQn);
/* 初始化计数器 */
FTM_DRV_InitCounter(INST_FLEXTIMER_MC1, &flexTimer_mc1_TimerConfig);
FTM_DRV_CounterStart(INST_FLEXTIMER_MC1);
编写中断函数
void ftmTimerISR(void)
{
//ftm周期中断 50us进来一此
CPU_RunTime++;
FTM_DRV_ClearStatusFlags(INST_FLEXTIMER_MC1, (uint32_t)FTM_TIME_OVER_FLOW_FLAG); //清除FTM状态标志位
}
打印下系统信息
//打印CPU信息
void Printf_CPU_RunInfo()
{
uint32_t GetFrequency ;
uint32_t ConvertFreqToPeriodTicks ;
memset(CPU_RunInfo,0,400); //信息缓冲区清零
vTaskList((char *)&CPU_RunInfo); //获取任务运行时间信息
u1_printf("------------------------------------------------\r\n");
u1_printf("Name Status Priority Residue Number\r\n");
u1_printf("%s", CPU_RunInfo);
u1_printf("------------------------------------------------\r\n\n");
memset(CPU_RunInfo,0,400); //信息缓冲区清零
vTaskGetRunTimeStats((char *)&CPU_RunInfo);
u1_printf("Task name Run count usage\r\n");
u1_printf("%s", CPU_RunInfo);
u1_printf("------------------------------------------------\r\n\n");
GetFrequency = FTM_DRV_GetFrequency(INST_FLEXTIMER_MC1);
ConvertFreqToPeriodTicks = FTM_DRV_ConvertFreqToPeriodTicks(INST_FLEXTIMER_MC1,20000);
u1_printf("GetFrequency = %d\r\n\n",GetFrequency);
u1_printf("ConvertFreqToPeriodTicks = %d\r\n\n",ConvertFreqToPeriodTicks);
}
打印FreeRTOS系统信息现象
参考资料14. CPU使用率统计 — FreeRTOS内核实现与应用开发实战指南—基于STM32 文档 (embedfire.com)