I have a code snippet as given below
static unsigned int ticks; void IRQ_SomeTimer(void) { ticks ++; } int main() { int sav_ticks; sav_ticks = ticks; while (sav_ticks + TIME_TO_WAIT > *(volatile unsigned int *)&ticks); /* Do somethine here */ return 0; }
This compiles and runs on ARM-Mx targets up to optimization level O2, at optimization level O3 the code generated for
while (sav_ticks + TIME_TO_WAIT > *(volatile unsigned int *)&ticks);
is the same as the code generated for
while (sav_ticks + TIME_TO_WAIT > ticks);
Hence the code generated by O3 never comes out of the while loop!! Is this a BUG in Keil armcc(?) I am using Keil U-Vision (MDK ARM 4.73).
By cast I could selectively make the identifier to behave as volatile and hence obtaining more optimized code than the one where it is just declared volatile
Who the heck cast things to volatile pointers, and back-and-forth, project's got to be a nightmare to manage, for nominal to non-existent benefit.
Code it as ((volatile uint32_t)ticks)
Does the example I presented compile cleanly on all the compilers, and optimization settings? It's simpler, cleaner, and doesn't break when the count wraps.
Submit your bug report to your support contact at Keil