简体   繁体   English

M4 皮质中的观察点

[英]Watchpoint in cortex M4

I have a global constant array const uint32_t p[5] = {1, 2, 3, 4, 5};我有一个全局常量数组const uint32_t p[5] = {1, 2, 3, 4, 5}; . . I have made it read-protected by我已将其设置为受读保护

     DWT->COMP1 = (uint32_t)&p;
     DWT->MASK1 = 6;
     DWT->FUNCTION1 = (1 << 11)| (1 << 0) | (1 << 2);
   
   

When I access the array members using a for loop, interrupt is generated 5 times and is exactly what I want.当我使用 for 循环访问数组成员时,会生成 5 次中断,这正是我想要的。

for(int i= 0; i<5; i++){
    printf("p[%d] = %d\t",i,p[i]);
}

However, When I try to access the variables using a simple print statement without any loop但是,当我尝试使用没有任何循环的简单打印语句访问变量时

printf("p[0] = %d\t", p[0]);

, DebugMon_interrupt is not generated. , DebugMon_interrupt 不产生。 This behaviour is quite weird.这种行为非常奇怪。

If I remove the const keyword from the array, then it works fine ie interrupt is generated both with and without loop while accessing the array elements.如果我从数组中删除 const 关键字,那么它可以正常工作,即在访问数组元素时,无论有无循环都会产生中断。

I bet @Colin is right.我打赌@Colin 是对的。 This happens because of code optimization during compilation.这是因为编译期间的代码优化。

Not sure about your particular scenario, but you can try to inform compiler to skip optimizations for a specified code block:不确定您的特定场景,但您可以尝试通知编译器跳过指定代码块的优化:

#pragma GCC push_options
#pragma GCC optimize ("O0")

printf("p[0] = %d\t", p[0]);

#pragma GCC pop_options

Edit:编辑:

I missed the fact the array is const .我错过了数组是const的事实。 Obviously, in this case compiler will use hard-coded values.显然,在这种情况下,编译器将使用硬编码值。 As per comments below, solution is to remove const (and possibly remove compiler optimizations as well).根据下面的评论,解决方案是删除const (也可能删除编译器优化)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM