I have a global constant array 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(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. This behaviour is quite weird.
If I remove the const keyword from the array, then it works fine i.e interrupt is generated both with and without loop while accessing the array elements.
I bet @Colin is right. 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
. 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).