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;
相关文章
|
消息中间件 弹性计算 物联网
MQTT常见问题之发布MQTT主题消息失败如何解决
MQTT(Message Queuing Telemetry Transport)是一个轻量级的、基于发布/订阅模式的消息协议,广泛用于物联网(IoT)中设备间的通信。以下是MQTT使用过程中可能遇到的一些常见问题及其答案的汇总:
|
API
FreeRTOS学习笔记—FreeRTOS移植
本文学习了如何移植FreeRTOS到自己的STM32工程。最后,根据正点原子提供的测试main函数,测试了移植效果。
1026 0
FreeRTOS学习笔记—FreeRTOS移植
|
消息中间件 存储 API
|
12月前
|
存储 安全 API
基于FreeRTOS中的串口不定长接收(使用队列进行数据传输)
基于FreeRTOS中的串口不定长接收(使用队列进行数据传输)
1152 0
|
7月前
|
人工智能 安全 测试技术
本周 AI Benchmark 方向论文推荐
由北京大学和微软亚洲研究院的魏李等人提出的 FEA-Bench,是一个专为评估大型语言模型(LLMs)在代码库级别进行增量开发能力的基准测试。它从 83 个 GitHub 仓库中收集了 1,401 个任务实例,专注于新功能的实现。研究表明,即使是先进的 LLMs 在此任务中的表现仍远低于预期,揭示了仓库级代码开发的重大挑战。
276 0
|
10月前
|
机器学习/深度学习 监控 算法
机器学习在图像识别中的应用:解锁视觉世界的钥匙
机器学习在图像识别中的应用:解锁视觉世界的钥匙
1073 95
|
11月前
|
搜索推荐 数据挖掘 API
API接口在电商的应用及收益
本文探讨了API接口技术在电商领域的应用及其带来的收益。API接口作为连接电商平台与外部系统的桥梁,实现了高效、实时的数据交换和集成,提升了用户体验、运营效率和市场竞争力。具体应用包括库存管理、支付网关、物流跟踪、自动化业务流程、个性化推荐和精准营销等方面。通过实战案例分析,展示了亚马逊和小型电商公司如何利用API接口实现自动化管理,提高了工作效率和客户满意度。未来,API接口技术将更加注重智能化、标准化、安全性和跨界合作。
420 3
|
11月前
|
机器学习/深度学习 数据挖掘
R中单细胞RNA-seq数据分析教程 (2)
R中单细胞RNA-seq数据分析教程 (2)
R中单细胞RNA-seq数据分析教程 (2)
|
存储 NoSQL 数据处理
探索MongoDB:灵活、高性能的NoSQL数据库解决方案与应用实践
探索MongoDB:灵活、高性能的NoSQL数据库解决方案与应用实践
523 1
|
数据采集 前端开发 JavaScript
Python爬虫技术:动态JavaScript加载音频的解析
Python爬虫技术:动态JavaScript加载音频的解析