This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

sprintf for floats. SES "PrintF Floating Point Supported" does not work

I am trying to use sprintf to format a float type variable, and I am not having any luck. The values printed are either blank or wrong. Sometimes I even Hardfault, which I believe is a buffer overflow from sprintf trying to print too many characters.

Before you ask, yes I changed "PrintF Floating Point Supported" as shown in the following answer:

https://devzone.nordicsemi.com/f/nordic-q-a/30535/nrf52-printf-float-in-ses

I have tried both "Float" and "Double", neither seems to do anything. 

I am using SEGGER Embedded Studio 5.42.

Code: (I had to use snprintf to avoid a buffer overflow)

char buf[20];

float f = 12.34;

printf("printf: %f\n", f);

snprintf(buf, 20, "%f", f);

printf("sprintf: %s\n", buf);

result:

printf: 80298907385281280000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000
sprintf: 8029892515591747200

Previously it would print "0.000000" but it changes based on the value I try to print. 

I wrote my own version of ftoa to get around this problem for now but it is not an ideal workaround. 

float f = 12.34;
ftoa(f, buf, 10);
printf("ftoa: %s\n", buf);

result:

ftoa: 12.3400000000

Any advice would be great, Thank you. 

Parents
  • csulz said:
    The screenshot shows it. luos_s.elf and json.o. Does that help solve the problem with sprintf?

     It doesn't really tel l me much.

    It has been a while, but I tested the TWI scanner example from SDK17.0.2 with floats a while back, and got it working with the settings from my screenshot. Can you try another example than the one you are using, such as the twi_scanner? Just to see if there may be any other settings that are not working?

    Is it possible for me to reproduce the issue you are seeing on a DK? Can you zip your project folder so that I can unzip it in an unmodified SDK and test your application? (please test that it works in an unmodified SDK before uploading it here).

    Best regards,

    Edvin

  •  It doesn't really tel l me much.

    I didn't think so. I thought it was strange that you needed that information. 

    Is printf/sprintf part of the SDK? Or is it part of the SEGGER implementation of the stdio? Does changing the Nordic SDK change the stdlib implementation that SEGGER tries to link in? The stdio.h file is under the SEGGER file system folders and is written by Segger. 

    We are using SDK 15.3, we haven't yet had a reason to switch as it has been working so far. Is there an example in that version that you would recommend?

    I really don't think I can zip my entire application to send to you. I can try to make a minimal version to see if that works. But I am not optimistic, I am going to link in the exact same SEGGER stdio implementation that I have problems with now. I will try it anyway. 

    What version of SES did you use in your project you tested a while back?

Reply
  •  It doesn't really tel l me much.

    I didn't think so. I thought it was strange that you needed that information. 

    Is printf/sprintf part of the SDK? Or is it part of the SEGGER implementation of the stdio? Does changing the Nordic SDK change the stdlib implementation that SEGGER tries to link in? The stdio.h file is under the SEGGER file system folders and is written by Segger. 

    We are using SDK 15.3, we haven't yet had a reason to switch as it has been working so far. Is there an example in that version that you would recommend?

    I really don't think I can zip my entire application to send to you. I can try to make a minimal version to see if that works. But I am not optimistic, I am going to link in the exact same SEGGER stdio implementation that I have problems with now. I will try it anyway. 

    What version of SES did you use in your project you tested a while back?

Children
  • csulz said:
    Is printf/sprintf part of the SDK? Or is it part of the SEGGER implementation of the stdio?

     It is not part of the SDK, no. It is part of the standard libraries, and this is implemented by the IDE providers (Segger in this case). 

     

    csulz said:
    We are using SDK 15.3, we haven't yet had a reason to switch as it has been working so far. Is there an example in that version that you would recommend?

     I would recommend the latest one (17.0.2), because it has the most bugfixes. Also, there aren't much API changes between SDK15 and SDK17. 

    Perhaps you can check this example, then:

    ble_app_uart_float.zip

    At least I get the floats in the log:

    Do you see the same if you test this project? NB: You need to unzip it in SDK17.0.2\examples\ble_peripheral.

    BR,

    Edvin

Related