compiler variable optimization

Hi All !
I'm updating a variable (time_out_serial) decreasing it by 1 at each 1 milisecond on a ISR. However when the variable time_out_serial is 0 it does not leave the while loop.


I realized that because of the compiler optimization , apparently the compiler does not "know" that eventually the variable time_out_serial will be 0.

One solution would be to declare the variable as a volatile, but my question is if it's possible to configure the E2 studio compiler optimization settings, in order to, prevent from declaring the variable as a volatile.


 I have the part of the code that i'm refering to , right below :


send ()
{
    while (time_out_serial);
}


Callback_ISR (timer_callback_args_t *p_args)
{
    time_out_serial--;
}

 

 

Kind regards,

 

Phillipe

  • I'd like to rewrite the ISR to avoid timer-wrap-around situations:


    Callback_ISR(timer_callback_args_t *p_args)
    {
       if (time_out_serial) time_out_serial--;
    }
  • Hi Phillipe,

    You may change optimization level in Project > Properties > C/C++ Build > Settings > Optimization > Optimization Level to -Og or -O0.

    Regards,
    adboc
  • In reply to adboc:

    Thank you for your reply Adboc. It worked perfectly.

    I have just one more question about data structure padding.

    I ckecked that the sizeof the structures are not necessarily equal to the sum of the members of the structure.

    For example the sum of the sizeof each member of this structure is 15 bytes, however if I ckeck the
    sizeof the structure it give me 16 bytes.

    typedef struct
    {
    unsigned char Data1[10];
    unsigned char Data2;
    unsigned char Data3;
    unsigned char Data3
    int16_t Data4;


    }sSampleStruct;

    One way to solve that is using __attribute__((packed, aligned(1))), but i would like to know if there's a configuration that I can set on the compiler to avoid using __attribute__((packed, aligned(1))) on each structure and prevent that padding.

    Regards,

    Phillipe
  • In reply to architect:

    Thank you Architect !
  • In reply to Phillipe:

    gcc.gnu.org/.../Structure-Packing-Pragmas.html

    gcc.gnu.org/.../Code-Gen-Options.html

    -fpack-struct[=n]
    Without a value specified, pack all structure members together without holes. When a value is specified (which must be a small power of two), pack structure members according to this value, representing the maximum alignment (that is, objects with default alignment requirements larger than this are output potentially unaligned at the next fitting location.

    Warning: the -fpack-struct switch causes GCC to generate code that is not binary compatible with code generated without that switch. Additionally, it makes the code suboptimal. Use it to conform to a non-default application binary interface.