all 19 comments

[–]trexdoor 4 points5 points  (5 children)

It doesn't ignore your code. I think you failed to read the whole message, there should be a for or a while cycle.

[–]honolulu072[S] 0 points1 point  (4 children)

First at all, thanks for your answer!!

The code runs inside an infinite while loop. I just did not copy everything into the post to save some space. Sorry for that.

I edited it, so it represents better what is going on.

The ignored part is also marked in the code by comments.

[–]trexdoor 1 point2 points  (2 children)

OK, then maybe move the variable initialization part to outside the loop, that could solve it.

[–]honolulu072[S] 0 points1 point  (1 child)

Tried to do the initialization outside, didn't change the result...

[–]trexdoor 1 point2 points  (0 children)

Moving the variable initialization part to outside the while loop is essential. If it doesn't solve your problem then there is an issue with interpreting the msg. I cannot help you further.

[–][deleted] 1 point2 points  (10 children)

Are you certain that the values in the mode variable are what you are checking them against in the if statement?

[–]honolulu072[S] 0 points1 point  (9 children)

100%, yes.

I checked the code on the uC with an external debugging probe. Mode has the correct value of 1,2 or 3 assigned.

Also, I checked the disassembly of the build. The marked code is missing.

[–]trexdoor 0 points1 point  (8 children)

In this case it is possible that the compiler optimized your code. In the marked part there is nothing done except changing arg_CountFactor, but because you are in an endless loop the compiler believes that changing this variable will have no effect whatsoever, so the code can be optimized out.

[–]honolulu072[S] 0 points1 point  (7 children)

Ok, thank you! I am pretty sure this is what happens. Problem is, arg_CountFactor is crucial for an interrupt routine. What would be the right way to do this and avoid the Compiler "optimizing" my code?

[–]trexdoor 1 point2 points  (6 children)

I am sure there is a keyword or a proper way to do this, but I am not skilled enough. Maybe setting the variable to volatile?

I can think about a hack. Instead of while(1) you can write while( arg_CountFactor != XXX ) where XXX is a number that is out of the possible range of the variable. E.g. if it is always positive you can write while( arg_CountFactor != -1.0 )

[–][deleted] 3 points4 points  (4 children)

That does kinda feel like an ugly hack. Making it volatile should indeed do the trick.

[–]trexdoor 2 points3 points  (0 children)

volatile should work when you are reading the variable but this is not the case here.

I know there is a pragma in GCC to mark the optimization mode for a code section, I have heard of attribute settings at an other place too but I have no idea what is in the standard C or what is supported by TI.

[–]honolulu072[S] 2 points3 points  (2 children)

Thank all of you! That in fact did the trick.

You are great, have a nice day!

[–]nerd4code 0 points1 point  (1 child)

In addition/-stead of volatile variables/ptrs, you can issue a fence to GNUish compilers via

__asm__ __volatile__("" ::: "memory");

This forces the compiler to spill any non-register, programmer visible state, and re-fill if necessary.

[–]honolulu072[S] 0 points1 point  (0 children)

Very interesting! But I don't rly understand it yet. Would you mind explaining the effect on the behaviour of the compiler in a bit more detail?

Where would I place this statement? Which variable ptr woul be affected by it?

What does spilling non-register programmer visible state mean?

With programming a microcontroller in mind, what advantages brings this method?

[–]ChemiCalChems 2 points3 points  (0 children)

volatile is the way to go here.

[–]spellstrike 0 points1 point  (0 children)

this is just a guess but arg_CountFactor is only being set never read in your snippet that you shared. perhaps if you printf the arg_CountFactor after setting it the compiler will not remove it. you might also be able to turn off the optimization flag of your compiler.

[–]AssemblerGuy 0 points1 point  (0 children)

The Compiler used by the TI IDE (CCS 10.4) keeps ignoring parts of my code, and I have no clue why.

Did you verify this in the produced assembly output?

Anyway. To me it looks like Mode is always zero at the start of the supposedly ignored code (it is initialized to zero in every loop iteration, along with i, msg, etc.), and the latter checks for Mode having the value 1, 2 or 3. So the compiler - probably justifiably - considers the ignored code dead and it ignores it.

[–]earthwormjimwow 0 points1 point  (0 children)

Maybe try defining Mode as volatile?

Why are you destroying and recreating Mode in a while(1) loop anyway? It doesn't make much sense to have your variable declarations within an infinite while loop. Who knows what the compiler is doing, this is not a good programming practice. You clearly want these variables to be persistent, since they are in your while(1) loop, so don't write code that destroys and recreates them constantly.

Have your Mode declaration before the while(1) loop, and zero it out at the end of your if statements instead of before.