Reputation: 19
The C compiler seems to be optimizing out a variable that I don't expect. The code in question is below:
uint32_t GetSysTick(void);
uint32_t timeout = GetSysTick() + 9000; //9sec
while(len && (GetSysTick() < timeout))
{
... some code that will decrement len
}
The compiler will optimize out the "timeout" variable. Typically the while loop will exit once len reaches zero but if the process is taking longer than expected once systick exceeds the timeout then it should also exit, of course none of this happens if timeout is optimized out. I'm sure if I define timeout as volatile that should keep it from being optimized out but technically its not a volatile. What am I missing here? Should I use volatile on the return value from GetSysTick()? (not sure if that is even legal)
For completeness here is GetSysTick and declaration of systick. Both are in another C file. systick gets incremented every millisecond in an interrupt.
static volatile uint32_t systick=0;
uint32_t GetSysTick(void)
{
return systick;
}
Upvotes: 0
Views: 159
Reputation: 19
So after further investigating my compiler I found out that I was not using the latest version. Apparently I was on the gnu arm C compiler version 5.2 once I updated to the latest version 5.4 my timeout variable is not being optimized out. Lesson here, is always check for updates on your tools. Thanks everyone for your input and suggestions. Thanks @JohnBollinger to taking the time to test it with his setup.
Upvotes: 1