This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Trouble with hardware floating point LM4F120H5QR

I wrote a simple program:

#include <stdio.h>

char result_str[16];

int main()
{
float y = 3.5, x = 1.2, z;
z = x + y;
sprintf(result_str, "%f", z);
 //z = sqrtf(x);
return 0;
}

At the end of the program I have a result like "4.700000" as a result_str.

But if I uncomment the string  z = sqrtf(x); I have something strange: the result of sprintf operation is "0.000000" (even if processor do not execute sqrtf statement). What the trouble??? 

I enable the floating point module at reset ISR:

HWREG(NVIC_CPAC) = ((HWREG(NVIC_CPAC) & ~(NVIC_CPAC_CP10_M | NVIC_CPAC_CP11_M)) |
NVIC_CPAC_CP10_FULL | NVIC_CPAC_CP11_FULL);

The compiler is IAR EWARM, the platform is Stellaris EK-LM4F120XL LaunchPad.

  • Tankist,

    This appears to be a compiler problem.  Are you sure you are using the latest version of the tool chain?  It also could be over optimizing your code.  Can you try putting "volatile" before the float in the variable declarations?  Can you try changing the level of optimization in the compiler options?

    Thanks,

    Sean

  • Oh, I broke my head for this problem. Optimization is off (I try some levels of optimization). "Volatile" do not provide the expected effect. Compiler version does not too fresh, but not too old.

    I tried to check this in CCS, but I have no problem there. Just one warning: disassembler instructions in the CCS does not match TI datasheet spmu159a, but in IAR it is completely match.

     

  • Hi,

    Not working with IAR, but one mention: some compilers are more pedantic than others - and usually tend to be smarter than necessary - try to declare floats as volatile and then initialize them as x=3.5f; and try also this code below just to see if your code is optimized at compile stage (use a function outside the main and call it):

    float sum(float a, float b)

    {
            return (a+b)/2.0;
    }
     
    an then, to use it in main:
    z = sqrtf(sum(x, y));
    Petrei

     

  • Thanks for advice, but it didn't help. I achieved success only when I made the float point variables global.
    As a result, my final test code look like this:

    #include <stdio.h>
    #include <math.h>

    float y = 3.5f, x = 1.2f, z;

    int main()
    {
    char result_str[16];

    z = x + y;
    sprintf(result_str, "%f", z);
    z = sqrtf(x);

    return 0;
    }
    It is not a good result, I wanted to use the local variables, but it is work.