1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
37
38
39
40
41
49
50
55
63
64
69
85
86
91
98
99
104
113
114
119
/* ... */
/* ... */
#include "cpu_utils.h"
Includes
xTaskHandle xIdleHandle = NULL;
__IO uint32_t osCPU_Usage = 0;
uint32_t osCPU_IdleStartTime = 0;
uint32_t osCPU_IdleSpentTime = 0;
uint32_t osCPU_TotalIdleTime = 0;
Private variables
/* ... */
void vApplicationIdleHook(void)
{
if( xIdleHandle == NULL )
{
xIdleHandle = xTaskGetCurrentTaskHandle();
}if (xIdleHandle == NULL) { ... }
}{ ... }
/* ... */
void vApplicationTickHook (void)
{
static int tick = 0;
if(tick ++ > CALCULATION_PERIOD)
{
tick = 0;
if(osCPU_TotalIdleTime > 1000)
{
osCPU_TotalIdleTime = 1000;
}if (osCPU_TotalIdleTime > 1000) { ... }
osCPU_Usage = (100 - (osCPU_TotalIdleTime * 100) / CALCULATION_PERIOD);
osCPU_TotalIdleTime = 0;
}if (tick ++ > CALCULATION_PERIOD) { ... }
}{ ... }
/* ... */
void StartIdleMonitor (void)
{
if( xTaskGetCurrentTaskHandle() == xIdleHandle )
{
osCPU_IdleStartTime = xTaskGetTickCountFromISR();
}if (xTaskGetCurrentTaskHandle() == xIdleHandle) { ... }
}{ ... }
/* ... */
void EndIdleMonitor (void)
{
if( xTaskGetCurrentTaskHandle() == xIdleHandle )
{
osCPU_IdleSpentTime = xTaskGetTickCountFromISR() - osCPU_IdleStartTime;
osCPU_TotalIdleTime += osCPU_IdleSpentTime;
}if (xTaskGetCurrentTaskHandle() == xIdleHandle) { ... }
}{ ... }
/* ... */
uint16_t osGetCPUUsage (void)
{
return (uint16_t)osCPU_Usage;
}{ ... }