15-FreeRTOS任务应用函数(1)

简介: 15-FreeRTOS任务应用函数(1)

1-获取系统的任务状态(uxTaskGetSystemState())


1.1 函数描述

1UBaseType_t uxTaskGetSystemState(
2TaskStatus_t * const pxTaskStatusArray,
3const UBaseType_t uxArraySize,
4unsigned long * const pulTotalRunTime );

configUSE_TRACE_FACILITY 必须设置为1

该函数向TaskStatus_t结构体填充相关信息,系统中每一个任务的信息都可以填充到TaskStatus_t结构体数组中,数组大小由uxArraySize指定。结构体TaskStatus_t定义如下:

1typedef struct xTASK_STATUS
 2{
 3/* 任务句柄 */
 4TaskHandle_t xHandle;
 5
 6/* A pointer to the task's name. This value will be invalid if the task was
 7deleted since the structure was populated! */
 8const signed char *pcTaskName;
 9
10/* A number unique to the task. */
11UBaseType_t xTaskNumber;
12
13/* The state in which the task existed when the structure was populated. */
14eTaskState eCurrentState;
15
16/* The priority at which the task was running (may be inherited) when the
17structure was populated. */
18UBaseType_t uxCurrentPriority;
19
20/* The priority to which the task will return if the task's current priority
21has been inherited to avoid unbounded priority inversion when obtaining a
22mutex. Only valid if configUSE_MUTEXES is defined as 1 in
23FreeRTOSConfig.h. */
24UBaseType_t uxBasePriority;
25
26/* The total run time allocated to the task so far, as defined by the run
27time stats clock. Only valid when configGENERATE_RUN_TIME_STATS is
28defined as 1 in FreeRTOSConfig.h. */
29unsigned long ulRunTimeCounter;
30
31/* Points to the lowest address of the task's stack area. */
32StackType_t *pxStackBase;
33
34/* The minimum amount of stack space that has remained for the task since
35the task was created. The closer this value is to zero the closer the task
36has come to overflowing its stack. */
37configSTACK_DEPTH_TYPE usStackHighWaterMark;
38} TaskStatus_t;


注意,这个函数仅用来调试用,调用此函数会挂起所有任务,直到函数最后才恢复挂起的任务,因此任务可能被挂起很长时间。在文件FreeRTOSConfig.h中,宏configUSE_TRACE_FACILITY必须设置为1,此函数才有效。


1.2 参数描述

pxTaskStatusArray:指向TaskStatus_t类型的结构体数组。这个数组至少要包含1个元素。RTOS控制的任务数量可以使用API函数uxTaskGetNumberOfTasks()获取。
uxArraySize:参数pxTaskStatusArray指向的数组大小,也就是该数组的索引数目。
pulTotalRunTime:如果在文件FreeRTOSConfig.h中设置宏configGENERATE_RUN_TIME_STATS为1,则该函数将总运行时间写入*pulTotalRunTime中。pulTotalRunTime可以设置为NULL,表示忽略总运行时间。
返回值:由uxTaskGetSystemState()填充的TaskStatus_t结构的数量。这个值应该等于uxTaskGetNumberOfTasks() API函数返回的值,但是如果uxArraySize参数传递的值太小,它将为零。


1.3 用例

1/*本例演示如是使用uxTaskGetSystemState()函数来获取运行时间信息,并将其转化为程序员更易识别的字符格式,这些转化后的字符保存到pcWriteBuffer中。*/
 2void vTaskGetRunTimeStats(signed char *pcWriteBuffer )
 3{
 4TaskStatus_t*pxTaskStatusArray;
 5volatileUBaseType_t uxArraySize, x;
 6unsignedlong ulTotalRunTime, ulStatsAsPercentage;
 7
 8/* 防御性代码,确保字符串有合理的结束*/
 9*pcWriteBuffer = 0x00;
10
11/* 获取任务总数目*/
12uxArraySize = uxTaskGetNumberOfTasks ();
13
14/*为每个任务的TaskStatus_t结构体分配内存,也可以静态的分配一个足够大的数组 */
15pxTaskStatusArray = pvPortMalloc( uxArraySize * sizeof( TaskStatus_t ));
16
17if(pxTaskStatusArray != NULL )
18{
19/*获取每个任务的状态信息 */
20uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize,&ulTotalRunTime );
21
22/* 百分比计算 */
23ulTotalRunTime /= 100UL;
24
25/* 避免除零错误 */
26if(ulTotalRunTime > 0 )
27{
28/* 将获得的每一个任务状态信息部分的转化为程序员容易识别的字符串格式*/
29for( x = 0; x < uxArraySize; x++ )
30{
31/* 计算任务运行时间与总运行时间的百分比。*/
32ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter /ulTotalRunTime;
33
34if( ulStatsAsPercentage > 0UL )
35{
36sprintf( pcWriteBuffer, "%s\t\t%lu\t\t%lu%%\r\n",
37pxTaskStatusArray[ x ].pcTaskName,
38pxTaskStatusArray[ x ].ulRunTimeCounter,
39ulStatsAsPercentage );
40}
41else
42{
43/* 任务运行时间不足总运行时间的1%*/
44sprintf( pcWriteBuffer, "%s\t\t%lu\t\t<1%%\r\n",
45pxTaskStatusArray[ x ].pcTaskName,
46pxTaskStatusArray[x ].ulRunTimeCounter );
47}
48
49pcWriteBuffer += strlen( ( char * ) pcWriteBuffer );
50}
51}
52
53/* 释放之前申请的内存*/
54vPortFree( pxTaskStatusArray );
55}
56}



2-获取任务消息(vTaskGetInfo())


2.1 函数描述

1void vTaskGetInfo( TaskHandle_t xTask,
2TaskStatus_t *pxTaskStatus,
3BaseType_t xGetFreeStackSpace,
4eTaskState eState );

configUSE_TRACE_FACILITY必须设置1

uxTaskGetSystemState()为系统中的每个任务填充TaskStatus_t结构,而vTaskGetInfo()仅为单个任务填充TaskStatus_t结构。TaskStatus_t结构包含任务句柄的成员、任务名称、任务优先级、任务状态和任务消耗的运行时间总量。

注意:此函数仅用于调试,因为使用它会导致调度器在较长时间内保持挂起状态。


2.2 参数描述

xTask : 正在查询任务的句柄。将xTask设置为NULL将返回调用任务的信息。
pxTaskStatus:由pxTaskStatus指向的TaskStatus_t结构体将被xTask参数中传递的句柄所引用的任务的信息填充。
xGetFreeStackSpace: TaskStatus_t结构包含一个成员,用于报告正在查询的任务的堆栈高水位标记。堆栈高水位标志是曾经存在的堆栈空间的最小值,因此该数字越接近零,任务就越接近溢出堆栈。计算堆栈高水位标志需要相对较长的时间,并且可能使系统暂时无响应——因此提供xGetFreeStackSpace参数以允许跳过高水位标志检查。如果xGetFreeStackSpace没有设置为pdFALSE,高水位值只会被写入TaskStatus_t结构。
eState: TaskStatus_t结构体包含一个成员,用于报告正在查询的任务的状态。获取任务状态没有简单的赋值那么快——因此提供eState参数以允许从TaskStatus_t结构中省略状态信息。要获得状态信息,请将eState设置为eInvalid—否则在eState中传递的值将作为TaskStatus_t结构中的任务状态报告。

2.3 用例

1void vAFunction( void )
 2{
 3TaskHandle_t xHandle;
 4TaskStatus_t xTaskDetails;
 5
 6/* 从任务的名称中获取任务句柄。*/
 7xHandle = xTaskGetHandle( "Task_Name" );
 8
 9/* 检查句柄是不是任务本身. */
10configASSERT( xHandle );
11
12/* 使用句柄可以获得关于任务的进一步信息。 */
13vTaskGetInfo( /* 正在查询任务的句柄。 */
14xHandle,
15/* 要用信息完成的TaskStatus_t结构xTask。 */
16&xTaskDetails,
17/* Include the stack high water mark value in the
18TaskStatus_t structure. */
19pdTRUE,
20/* 在TaskStatus_t结构中包含任务状态*/
21eInvalid );
22}


2.4 The TaskStatus_t definition

1typedef struct xTASK_STATUS
 2{
 3/* 对象中的其余信息所归属的任务句柄结构有关。*/
 4TaskHandle_t xHandle;
 5
 6/*指向任务名称的指针。这个值将无效,如果任务被删除,因为结构已填充! */
 7const signed char *pcTaskName;
 8
 9/* A number unique to the task. */
10UBaseType_t xTaskNumber;
11
12/* The state in which the task existed when the structure was populated. */
13eTaskState eCurrentState;
14
15/* The priority at which the task was running (may be inherited) when the
16structure was populated. */
17UBaseType_t uxCurrentPriority;
18
19/* The priority to which the task will return if the task's current priority
20has been inherited to avoid unbounded priority inversion when obtaining a
21mutex. Only valid if configUSE_MUTEXES is defined as 1 in
22FreeRTOSConfig.h. */
23UBaseType_t uxBasePriority;
24
25/* The total run time allocated to the task so far, as defined by the run
26time stats clock. Only valid when configGENERATE_RUN_TIME_STATS is
27defined as 1 in FreeRTOSConfig.h. */
28unsigned long ulRunTimeCounter;
29
30/* Points to the lowest address of the task's stack area. */
31StackType_t *pxStackBase;
32
33/* The minimum amount of stack space that has remained for the task since
34the task was created. The closer this value is to zero the closer the task
35has come to overflowing its stack. */
36configSTACK_DEPTH_TYPE usStackHighWaterMark;
37} TaskStatus_t;
相关文章
|
1月前
|
API 调度
【FreeRTOS】软件定时器的使用
【FreeRTOS】软件定时器的使用
|
8月前
|
API
FreeRTOS学习笔记—FreeRTOS移植
本文学习了如何移植FreeRTOS到自己的STM32工程。最后,根据正点原子提供的测试main函数,测试了移植效果。
189 0
FreeRTOS学习笔记—FreeRTOS移植
|
IDE 调度 开发工具
如何在S32DS中使用SystemView分析FreeRTOS
如何在S32DS中使用SystemView分析FreeRTOS
如何在S32DS中使用SystemView分析FreeRTOS
|
1月前
|
消息中间件 Web App开发 API
FreeRTOS介绍 和 将FreeRTOS移植到STM32F103C8T6
FreeRTOS介绍 和 将FreeRTOS移植到STM32F103C8T6
FreeRTOS介绍 和 将FreeRTOS移植到STM32F103C8T6
|
11天前
|
消息中间件 算法 编译器
【FreeRTOS(一)】FreeRTOS新手入门——初识FreeRTOS
【FreeRTOS(一)】FreeRTOS新手入门——初识FreeRTOS
|
1月前
|
API 调度 芯片
FreeRTOS 延时函数和软件定时器 详解
FreeRTOS 延时函数和软件定时器 详解
FreeRTOS 延时函数和软件定时器 详解
|
8月前
|
调度 开发者
【Freertos基础入门】2个Freertos的Delay函数
【Freertos基础入门】2个Freertos的Delay函数
269 1
|
测试技术
16-FreeRTOS任务应用函数(2)
16-FreeRTOS任务应用函数(2)
|
存储 编解码 调度
17-FreeRTOS任务应用函数(3)
17-FreeRTOS任务应用函数(3)