【FreeRTOS】小白进阶之如何使用FreeRTOS IDLE空闲任务

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liwei16611/article/details/82531477

主要介绍空闲钩子函数的基本使用。

1、头文件定义及启动任务

#include "FreeRTOS.h"
#include "task.h"
#include "supporting_functions.h"


void vTaskFunction( void *pvParameters );

static uint32_t ulIdleCycleCount = 0UL;

const char *pcTextForTask1 = "Task 1 is running\r\n";
const char *pcTextForTask2 = "Task 2 is running\r\n";

int main( void )
{
	// 创建任务
	xTaskCreate( vTaskFunction, "Task 1", 1000, (void*)pcTextForTask1, 1, NULL );
	xTaskCreate( vTaskFunction, "Task 2", 1000, (void*)pcTextForTask2, 2, NULL );

	// 开始调度任务
	vTaskStartScheduler();

	for( ;; );
	return 0;
}

2、任务函数
 

void vTaskFunction( void *pvParameters )
{
    char *pcTaskName;
    const TickType_t xDelay250ms = pdMS_TO_TICKS( 250UL );

	pcTaskName = ( char * ) pvParameters;

	for( ;; )
	{
		// 打印空闲任务累加的计数值
		vPrintStringAndNumber( pcTaskName, ulIdleCycleCount );

		vTaskDelay( xDelay250ms );
	}
}

3、空闲钩子回调函数

// 空闲钩子函数
void vApplicationIdleHook( void )
{
	// 累加变量计数
	ulIdleCycleCount++;
}

猜你喜欢

转载自blog.csdn.net/liwei16611/article/details/82531477
今日推荐