Question
STM32H7 CPU runs too slowly occasionally
test code as below:
#include <rtthread.h>
#include <rthw.h>
#define CPU_USAGE_CALC_TICK 100
#define CPU_USAGE_LOOP 100
static rt_uint8_t cpu_usage_major = 0, cpu_usage_minor= 0;
static rt_uint32_t total_count = 0;
static rt_uint32_t _count = 0;
//static rt_uint32_t loop;
static void cpu_usage_idle_hook()
{
rt_uint32_t count;
rt_tick_t tick;
volatile rt_uint32_t loop;
if (total_count == 0)
{
/* get total count */
rt_enter_critical();
tick = rt_tick_get();
while(rt_tick_get() - tick < CPU_USAGE_CALC_TICK)
{
count++;
loop = 0;
while (loop < CPU_USAGE_LOOP) loop ++;
}
total_count = count;
rt_exit_critical();
}
count = 0;
/* get CPU usage */
rt_enter_critical();
tick = rt_tick_get();
while (rt_tick_get() - tick < CPU_USAGE_CALC_TICK)
{
count ++;
loop = 0;
while (loop < CPU_USAGE_LOOP) loop ++;
}
_count = count;
rt_exit_critical();
/* calculate major and minor */
if (count < total_count)
{
count = total_count - count;
cpu_usage_major = (count * 100) / total_count;
cpu_usage_minor = ((count * 100) % total_count) * 100 / total_count;
}
else
{
//total_count = count;
/* no CPU usage */
cpu_usage_major = 0;
cpu_usage_minor = 0;
}
}The same code runs at different speeds,the total_count = 54580 ,However the is _count = 13633,75% reduction in CPU performance 。
The assembly code is as follows:

