I have an optimisation issue with gcc running on Eclipse. I have a program which runs well with -O0 optimization level, but I need to turn ON other levels of optimization because my software uses too much flash memory space. But when I use -O1 optimization level (for instance) my code doesn't work anymore and I need to do strange modifications as described below with the UART peripheral managing functions.
Code running well with -O0
void function(...)
{
// Send char one by one on UART peripheral
UART_put_char(...)
// Wait end of transmission on UART
while(end_of_transmission_flag == flase);
}
void UART_TxCompleteCallback()
{
// Change flag state
end_of_transmission_flag = true;
}
Code with modification for -O1
void function(...)
{
// Send char one by one on UART peripheral
UART_put_char(...)
// Wait end of transmission on UART
while(end_of_transmission_flag == flase)
{
Dummy = Dummy == 0 ? 1 : 0;
}
}
void UART_TxCompleteCallback()
{
// Change flag state
end_of_transmission_flag = true;
}
I would like to know why gcc is forcing me to do this kind of modifications, do I need to change my "way of think" about the design of my code. Or is there an explanation with this behavior ?
Thanks for your advices.