在大多数Cortex-M的处理器中调试端口包含一个32位的自由运行计数器,它可以计算 CPU 的时钟周期。计数器是 Debug 观察和跟踪(DWT)模块的一部分,可以很容易地用于测量代码的执行时间。下面的代码是启用和初始化这个特性非常有用。
注意测试需要重新断电上电
#define ARM_CM_DEMCR (*(uint32_t *)0xE000EDFC)
#define ARM_CM_DWT_CTRL (*(uint32_t *)0xE0001000)
#define ARM_CM_DWT_CYCCNT (*(uint32_t *)0xE0001004)
void start_Math(void)
{
if (ARM_CM_DWT_CTRL != 0) { // See if DWT is available
ARM_CM_DEMCR |= 1 << 24; // Set bit 24
ARM_CM_DWT_CYCCNT = 0;
ARM_CM_DWT_CTRL |= 1 << 0; // Set bit 0
}
}
void test_mearure()
{
start_Math();
// ARM_CM_DWT_CYCCNT = 0;
start = ARM_CM_DWT_CYCCNT;
// Code to measure
arm_sin_f32(32768/4);//对应2pi/4=1/2 *PI 90度
stop = ARM_CM_DWT_CYCCNT;
delta = stop-start;
printf("arm_sin_f32 %d\r\n",delta);
start_Math();
// Code to measure
arm_cos_f32(32768/4);//对应2pi/4=1/2 *PI 90度
stop = ARM_CM_DWT_CYCCNT;
delta = stop-start;
printf("arm_cos_f32 %d\r\n",delta);
start_Math();
// Code to measure
arm_cos_q31(32768/4);//对应2pi/4=1/2 *PI 90度
stop = ARM_CM_DWT_CYCCNT;
delta = stop-start;
printf("arm_cos_q31 %d\r\n",delta);
start_Math();
// Code to measure
arm_sin_q31(32768/4);//对应2pi/4=1/2 *PI 90度
stop = ARM_CM_DWT_CYCCNT;
delta = stop-start;
printf("arm_sin_q31 %d\r\n",delta);
start_Math();
// Code to measure
arm_sin_q15(32768/4);//对应2pi/4=1/2 *PI 90度
stop = ARM_CM_DWT_CYCCNT;
delta = stop-start;
printf("arm_sin_q15 %d\r\n",delta);
start_Math();
// Code to measure
arm_cos_q15(32768/4);//对应2pi/4=1/2 *PI 90度
stop = ARM_CM_DWT_CYCCNT;
delta = stop-start;
printf("arm_cos_q15 %d\r\n",delta);
}
int main(void)
{
system_clock_config();
usart_configuration();
printf("这个是三角函数测试开启浮点运算FPU AT32F423 主频 144M 指令周期数 \r\n");
printf("这个是三角函数测试不开启浮点运算FPU AT32F423 主频 144M 指令周期数 \r\n");
test_mearure();
}
MCU上的代码执行时间-腾讯云开发者社区-腾讯云