Last active 1776394742

获取任务运行时间、运行计数、栈占用

g_runtime.c Raw
1/**
2 ******************************************************************************
3 * @file user\global\g_runtime.c
4 * @author luhuaishuai
5 * @version v0.1
6 * @date 2026-1-12
7 * @brief Briefly describe the function of your function
8 ******************************************************************************
9 */
10
11/* Includes -------------------------------------------------------------------*/
12#include "g_runtime.h"
13
14
15
16
17/* variables ------------------------------------------------------------------*/
18TaskRunTimeTypeDef TaskRunTimeStat;
19
20
21
22/* code -----------------------------------------------------------------------*/
23
24/**
25 * @brief GetRunTime:计算线程运行间隔时间
26 *
27 * @note none
28 *
29 * @param taskID : 任务ID
30 *
31 * @retval runtime : 任务周期
32 */
33
34uint32_t GetTask_RunTime(uint8_t taskID)
35{
36 static uint32_t lasttime[Task_combined] = {0};
37
38 uint32_t runtime = 0;
39 uint32_t curtime = HAL_GetTick();
40
41 runtime = curtime - lasttime[taskID]; //计算线程运行间隔时间 runtime运行一次
42
43 lasttime[taskID] = curtime;
44
45 return runtime;
46}
47
48/**
49 * @brief GetTask_Beatcnt:线程运行计数,用于判断线程是否在运行
50 *
51 * @note none
52 *
53 * @param taskID : 任务ID
54 *
55 * @retval beatcnt[taskID] : 线程运行计数
56 */
57
58uint32_t GetTask_Beatcnt(uint8_t taskID)
59{
60 static uint8_t beatcnt[Task_combined] = {0};
61
62 beatcnt[taskID]++;
63
64 if(beatcnt[taskID] >= 10)
65 {
66 beatcnt[taskID] = 0;
67 }
68
69 return beatcnt[taskID];
70}
71
72
73
74/**
75 * @brief Get_Free_Stack:获取剩余任务栈大小
76 *
77 * @note none
78 *
79 * @param taskID : 任务ID
80 *
81 * @retval free_stack[taskID] : 剩余任务栈大小
82 */
83
84uint32_t Get_Free_Stack(uint8_t taskID)
85{
86 static uint32_t free_stack[Task_combined] = {0};
87
88 free_stack[taskID] = uxTaskGetStackHighWaterMark(NULL);
89
90 return free_stack[taskID];
91}
92
g_runtime.h Raw
1
2#ifndef __GRUNTIME_H
3#define __GRUNTIME_H
4
5
6/* includes ----------------------------------------------------------------------------------------------*/
7#include "global.h"
8
9
10
11/* macro ------------------------------------------------------------------------------------------------*/
12#define Task_combined 8
13
14
15
16
17/* struct ------------------------------------------------------------------------------------------------*/
18typedef struct TaskRunTime
19{
20 uint32_t threads_runtime; // 运行时间
21 uint32_t threads_counter; // 任务计数
22 uint32_t threads_freestack; // 剩余栈空间
23
24} TaskRunTime;
25
26
27
28enum Task_ID
29{
30 HeartBeatTaskID = 0,
31 DownLinkTaskID,
32 UPLinkTaskID,
33 YkcTaskID,
34
35};
36
37
38
39typedef struct TaskRunTimeTypeDef
40{
41 TaskRunTime HeartBeatTask;
42 TaskRunTime DownLinkTask;
43 TaskRunTime UPLinkTask;
44 TaskRunTime YkcTask;
45
46} TaskRunTimeTypeDef;
47
48
49
50/* Exported functions prototypes ------------------------------------------------------------------------*/
51uint32_t GetTask_RunTime(uint8_t taskID);
52uint32_t GetTask_Beatcnt(uint8_t taskID);
53uint32_t Get_Free_Stack(uint8_t taskID);
54
55/* Exported constants -----------------------------------------------------------------------------------*/
56extern TaskRunTimeTypeDef TaskRunTimeStat;
57
58
59#endif /* __GRUNTIME_H */
60