We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
I'm debugging LPC1114x301(NXP Cortex-M0) with Keil 4.11 Evaluation and ULink2, and no error, no warning after builing.
There's a line of C code in my project, "while (GstpKm.ucState != 0x01) {}", where GstpKm is a global structure in RAM, ucState is it's first member(unsigned char).
If Optimization is Level 0, the compiler will give the following assembly:
0x000001C0 LDR R0,[pc, #28] 0x000001C2 LDRB R0,[R0, #0x00] 0x000001C4 CMP R0, #0x01 0x000001C6 BNZ 0x000001C0
You can see the codes above, in each cycle, CPU reads "GstpKm.ucState" and then compare it with 0x01. These codes are correct, when the timer interrupt changes "GstpKm.ucState" to "0x01", the while loop will finish.
However, if Optimization is Level1, Level2 or Level3(anyone of them), the compiler will give wrong assembly.The fllowing is in Level 1.
0x0000018E LDRB R0,[R6, #0x00] ;get GstpKm.ucState 0x00000190 CMP R0, #0x01 ;compare 0x01 0x00000192 BNZ 0x00000190 ;go back
CPU reads "GstpKm.ucState" before the comparing loop, while inside the loop, "GstpKm.ucState" is kept in R0 and not updated, so CPU will be blocked inside While() forever.
Do you have any idea of it?
make that variable volatile, I guess...?