Topic : RIDE7 GCC STM32: Code optimization vs local vars, different results

Forum : ARM

Original Post
Post Information Post
June 7, 2011 - 9:20am
Guest

Hi all,

We have an issue with local vars uses, and code optimization.

In a short: we have to convert a integer to string. It works fine with 'no optimization' option, or using 'static variables' for all of the incriminated function.

I know that the following example is a little "hair-pulled", but basically it works.
What is worrying us, is that we have a 40000 line long software ! ! ! If the pb occurs here, it could happen anywhere !

Here is the main file used to test this issue (use template dir of STM32F10x_StdPeriph_Lib_V3.1.2), anything else left as template was.

----------------------------------------------------------------------------------------------------------------------
Ride7 7.30.10.0159
RKit-ARM 1.26.10.0130
Ride7 Patch 7.30.10.0169
----------------------------------------------------------------------------------------------------------------------

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "stm32_eval.h"
#include

#include "stm3210c_eval_lcd.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
USART_InitTypeDef USART_InitStructure;

u8 u8t_Ligne2_LCD[3];

/* Private functions ---------------------------------------------------------*/

//########################################################################################################
void conv_digit(u16 u16_DataConv)
{
/*static*/ u8 c_dix_mille;
/*static*/ u8 c_mille;
/*static*/ u8 c_cent;
/*static*/ u8 c_dix;
/*static*/ u8 c_un;
/*static*/ u8 c_dixieme;
/*static*/ u16 i_trav;

i_trav=u16_DataConv;
c_dix_mille = 0x30;
c_mille = 0x30;
c_cent = 0x30;
c_dix = 0x30;
c_un = 0x30;
c_dixieme = 0x30;

i_trav /=60;
if (u16_DataConv%60)
c_un++;

while ( i_trav>9999 )
{
i_trav -=10000;
c_dix_mille++;
}
while ( i_trav>999 )
{
i_trav -=1000;
c_mille++;
}
while ( i_trav>99 )
{
i_trav -=100;
c_cent++;
}
while ( i_trav>9 )
{
i_trav -=10;
c_dix++;
}
c_un +=(u8)i_trav;

u8t_Ligne2_LCD[0]=(c_cent == '0')? ' ':c_cent;
u8t_Ligne2_LCD[1]=c_dix;
u8t_Ligne2_LCD[2]=c_un;
}
//########################################################################################################
int main(void)
{
// Correct result Opti max & locals Opti max & statics Opti min & locals
conv_digit(3600); // " 60" " 61" NO " 60" " 60"
conv_digit(60); // " 01" " 02" NO " 01" " 01"
conv_digit(70); // " 02" " 02" ok! " 02" " 02"
conv_digit(120); // " 02" " 03" NO " 02" " 02"
conv_digit(150); // " 03" " 03" NO " 03" " 03"
conv_digit(13560); // "226" "227" NO "226" "226"
conv_digit(13600); // "227" "227" ok! "227" "227"

while(1); //infinite loop to allow 'simulation mode' with ride7.

/* Setup STM32 system (clock, PLL and Flash configuration) */
SystemInit();

while(1);
}