图1:rtconfig.h配置
2.2
添加CPU占用率统计的实现代码:
/* Private define -----------------------------------------------------------*/
#define MM32_CPU_USAGE_TICK 10
#define MM32_CPU_USAGE_LOOP 100
/* Private variables --------------------------------------------------------*/
rt_uint8_t mm32_cpu_usage_integer = 0;
rt_uint8_t mm32_cpu_usage_decimal = 0;
rt_uint32_t mm32_cpu_usage_count = 0;
/******************************************************************************
*
@Brief
*
@param
* @retval
*
@attention
******************************************************************************/
void mm32_cpu_usage_hook(void)
{
volatile rt_tick_t tick = 0;
volatile rt_uint32_t count = 0;
volatile rt_uint32_t loop = 0;
if(mm32_cpu_usage_count == 0)
{
rt_enter_critical();
tick = rt_tick_get();
while((rt_tick_get() - tick) < MM32_CPU_USAGE_TICK)
{
mm32_cpu_usage_count++;
loop = 0;
while(loop < MM32_CPU_USAGE_LOOP)
{
loop++;
}
}
rt_exit_critical();
}
count = 0;
tick = rt_tick_get();
while((rt_tick_get() - tick) < MM32_CPU_USAGE_TICK)
{
count++;
loop = 0;
while(loop < MM32_CPU_USAGE_LOOP)
{
loop++;
}
}
/* Calculate MM32 CPU Usage */
if(count < mm32_cpu_usage_count)
{
count = mm32_cpu_usage_count - count;
mm32_cpu_usage_integer = (count * 100) / mm32_cpu_usage_count;
mm32_cpu_usage_decimal = ((count * 100) % mm32_cpu_usage_count) * 100 / mm32_cpu_usage_count;
}
else
{
mm32_cpu_usage_count = count;
mm32_cpu_usage_integer = 0;
mm32_cpu_usage_decimal = 0;
}
}
/******************************************************************************
* @brief
* @param
* @retval
* @attention
******************************************************************************/
void mm32_cpu_usage_get(rt_uint8_t *integer, rt_uint8_t *decimal)
{
RT_ASSERT(integer != RT_NULL);
RT_ASSERT(decimal != RT_NULL);
*integer = mm32_cpu_usage_integer;
*decimal = mm32_cpu_usage_decimal;
}
2.3
添加测试线程代码,并将CPU利用率的钩子函数设置为IDLE钩子函数,在其中一个线程中读取并打印当前的CPU利用率:
/* Private define -----------------------------------------------------------*/
#define MM32_THREAD_STACK_SIZE 512
#define MM32_THREAD_PRIORITY 5
#define MM32_THREAD_TIMESLICE 10
/* Private variables --------------------------------------------------------*/
static rt_thread_t mm32_thread1 = RT_NULL;
static char mm32_thread2_stack[1024];
static struct rt_thread mm32_thread2;
/******************************************************************************
* @brief
* @param
* @retval
* @attention
******************************************************************************/
void mm32_thread1_entry(void *parameter)
{
rt_uint8_t integer = 0;
rt_uint8_t decimal = 0;
/* Init LEDs GPIO */
BSP_LED_Init();
while(1)
{
/* Toggle LED1~LED2 status */
BSP_LED1_TOGGLE();
BSP_LED2_TOGGLE();
mm32_cpu_usage_get(&integer, &decimal);
rt_kprintf("MM32 CPU Usage : %d.%d%rn", integer, decimal);
/* Delay 500ms */
rt_thread_mdelay(1000);
}
}
/******************************************************************************
* @brief
* @param
* @retval
* @attention
******************************************************************************/
void mm32_thread2_entry(void *parameter)
{
while(1)
{
/* Toggle LED3~LED4 status */
BSP_LED3_TOGGLE();
BSP_LED4_TOGGLE();
/* Delay 250ms */
rt_thread_mdelay(250);
}
}
/******************************************************************************
* @brief
* @param
* @retval
* @attention
******************************************************************************/
int main(void)
{
mm32_thread1 = rt_thread_create("thread1",
mm32_thread1_entry, RT_NULL,
MM32_THREAD_STACK_SIZE,
MM32_THREAD_PRIORITY,
MM32_THREAD_TIMESLICE);
if(mm32_thread1 != RT_NULL)
{
rt_thread_startup(mm32_thread1);
}
rt_thread_init(&mm32_thread2, "thread2",
mm32_thread2_entry, RT_NULL,
&mm32_thread2_stack[0],
sizeof(mm32_thread2_stack),
MM32_THREAD_PRIORITY - 1,
MM32_THREAD_TIMESLICE);
rt_thread_startup(&mm32_thread2);
/* Set Idle Thread Hook */
rt_thread_idle_sethook(mm32_cpu_usage_hook);
return 0;
}
3. 结果验证
编译软件工程无误后,下载代码到芯片;在串口终端工具中我们可以看到当前两个LED灯闪烁线程对于CPU的利用率情况:
图2:运行结果
0