cancel
Showing results for 
Search instead for 
Did you mean: 

Strang debugging observation

Amir1
Associate II

Hello All

I am new to this community and, for that matter, to the whole area of embedding programming. So please forgive if my question is trivial.

I have created a simple project for F303RE using CubeMx, turning the LED on/off using the blue button, and am working on it in TrueStudio. The project works as expected. Then, just to explore, I have defined a few int variables in the main function and have assigned them in the while(1) loop, These are int a, b, c; (in the main) and a = GPIOA; b = GPIOB, and c=2*a; (in the while(1) loop). But, when I step over in the debugging mode, the debugger ignores them all and jumps over to the next instruction (which is a pin read function for the button). I even have set breakpoints on these assignments, but the result is the same; the debugger arrow simply does not stop at any of these assignments! What is interesting however is that the assignments are done correctly since, when I hover the mouse over any of these variables, I see that the variables have assumed the expected values.

I wonder if anyone could help me understand what is wrong.

Thanks in advance

Amir

9 REPLIES 9
S.Ma
Principal

To optimize memory "value", all code that looks doing nothing will be removed by compiler. Unused functions, calculations doing nothing, delay SW loops, etc...

Change the compiler options to NO OPTIMISATIONS if you'd like to start easy.

Later you'll start to use "volatile" on variables to survive the compiler chop chop, or add asm("nop;"); markers for breakpoints.

Also note, that GPIOA and GPIOB are constants, the addresses of the respective GPIO registers blocks.

What you probably wanted to write is a = GPIOA->IDR; b = GPIOB->IDR. In this case, the compiler ought to generate code to read the input registers themselves, as they are qualified as volatile, but it still may simply throw away the result and even not allocate space for the a, b, c variables, as they are not volatile and they are not used in any library function.

JW

Amir1
Associate II

Thanks. I do not know how to set the optimization level in TrueStudio. However, I inserted volatile before the declaration statements, and the result is the same; the debugger nonetheless jumps over the assignments. I should also mention that by adding the assignment c = 2*a, I intended to fool the compiler into thinking that these variables, or at least variable a, are used somewhere.

Thanks. Actually, I did only want to check the "variables" view of the debugging perspective and see how general purpose registers are used for exchanges. So I thought that I would assign something to my variables whose values are known, for example GPIOA, GPIOB, and GPIOC. That is why I wrote a=GPIOA, etc., and I expected to see 0x48000000 for a, 0x48000400 for b, and so on. As I said, when I hover my mouse over a, b, and the other variables, I do indeed see the expected values as those mentioned above, that is the addresses of the ports. But the variables do not show in the varables views or general purpose registers. Then the debugging mode shows that the assignments are completely ignored. I later added int i = 0; and i = i+1;. These are also ignored as the rest.

S.Ma
Principal

You should try to make a more practical code. GPIOA,B,C are addresses, hence pointers. There should not be math operations in such HW peripheral pointers. What do you really want to do or learn?

Amir1
Associate II

Yes, this specific part of the code is not practical; the practical part of the code reads the switch and commands the LED, and it works fine. The addition is just a curious exercise for me to gain a better understanding of the IDE and its views, etc, as I explained in my reply to Jan. Nonetheless, I cannot make sense of the behaviour of the debugging mode. After all, I am trying to store some values (be it addresses, but values nonetheless) in some volatile variables. Why is the code ignored by the debugger? Even a simple assignment not having to do with any GPIO (int i=0;) and increment (I++) is ignored. Can you tell me how to turn off the optimization? This compiler is way too smart presumably.

Done! Thanks. Set it to low, and now I get all the variables and no step-over.

Amir1
Associate II

Please disregard my very last question. I have now turned the optimization done, and now all variables are showing up with no code step-over. Thanks all for the insight.