sprintf bug in PSoC Creator 3 with 5LP and format %f

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable
        Very simple program to test sprintf. This work wiith Creator 2.2 not in Creator 3 with %f as format. Put a breahpoint after the line sprintf and look the string strMsg1. You must see Testing: 12.35 and I see : Testing : then \0. Where is the bug ??? #include        #include          void testSprintf(void) { char strMsg1[21]; sprintf(strMsg1,"Testing:%f",(float)12.35); } int main() { testSprintf(); for(;;) { } }        
0 Likes
1 Solution
lock attach
Attachments are accessible only for community members.
nicholausf_91
Employee
Employee

We default to newlib-nano in PSoC Creator 3.0 because it is leaner, which leaves more flash and memory) for your design. As hli observed, newlib-nano weak references the code that handles floating point as a part of stdio. 

   

 

   

You can enable floating point support by going to: Build Settings -> ARM GCC 4.7.3 -> Linker -> Command Line -> Custom Flags. Set its value to "-u _printf_float" (without the quotes).

   

 

   

We already have a bug on file to make this the default behavior. Sorry this tripped you up.

   

Thanks,

   

Nick

View solution in original post

0 Likes
35 Replies
Anonymous
Not applicable

 Very simple program to test sprintf with 5LP

   

This work wiith Creator 2.2 not in Creator 3 with %f as format.

   

Put a breakpoint after the line sprintf and look the string strMsg1.

   

You must see Testing: 12.35 and I see : Testing : then \0.

   

Where is the bug ?

   

#include <device.h>

   

#include <stdio.h>

   

void testSprintf(void)

   

{   

   

char strMsg1[21];

   

sprintf(strMsg1,"Testing:%f",(float)12.35);

   

}

   

int main()

   

{

   

    testSprintf();

   

    for(;;)

   

    {    }

   

}

   
        
   
    Sorry for the presentation of the first message.   
0 Likes
Anonymous
Not applicable

 For info with Creator 2.2 I use ARM_GCC_441 and

   

with Creator 3 I use ARM_GCC_473.

   

Thank for you help

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Should there be whitespace after the ":"

   

 

   

Should

   

 

   

sprintf(strMsg1,"Testing:%f",(float)12.35);

   

 

   

be changed to

   

 

   

sprintf(strMsg1,"Testing: %f",(float)12.35);

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

 Sorry but obviusly that it's not the problem.

   

Try the program with the breakpoint on the line after  sprintf(strMsg1,"Testing: %f",(float)12.35)  and look strMsg1

   

sprintf(strMsg1,"Testing: %d",12); //is working 

   

sprintf(strMsg1,"Testing: %f",12.35); // is not working with Creator 3.0 but ok in Creator 2.2

   

Best regards

0 Likes
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

I think this is due to the usage of newlib-nano. According to blogs.arm.com/embedded/879-shrink-your-mcu-code-size-with-gcc-arm-embedded-47/ using floating point IO is now "weak" referenced (to reduce the library size for printf). This implies that one needs to manually add libary code for having FP IO. It might be sufficient to add the math libaries ( see www.cypress.com/ ), but maybe not. I see nothing mentioned in the Creator 3.0 release notes.

   

Maybe this helps: github.com/32bitmicro/newlib-nano-1.0/blob/master/newlib/README.nano

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

 Thank for your help.

   

Finally your have reason for the newlib-nano.  It 'is not Additional Libraries 'm'

   

For correct working you must go to the menu Build Stteing/Linker/General and : Use newlib-nano = FALSE

   

By defaut it is True ( bad idea ).

   

Now sprintf( %f,....) is working.

   

 Thank for your help.

0 Likes
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

Did you try the suggestion in the second link (adding the linker parameter to link in the FP stuff for printf)?

0 Likes
lock attach
Attachments are accessible only for community members.
nicholausf_91
Employee
Employee

We default to newlib-nano in PSoC Creator 3.0 because it is leaner, which leaves more flash and memory) for your design. As hli observed, newlib-nano weak references the code that handles floating point as a part of stdio. 

   

 

   

You can enable floating point support by going to: Build Settings -> ARM GCC 4.7.3 -> Linker -> Command Line -> Custom Flags. Set its value to "-u _printf_float" (without the quotes).

   

 

   

We already have a bug on file to make this the default behavior. Sorry this tripped you up.

   

Thanks,

   

Nick

0 Likes
lock attach
Attachments are accessible only for community members.
RobynW
Employee
Employee
5 likes given First comment on blog 10 likes received

 Hello,

   

The Newlib-nano libraries do not include floating point support by default.  You can either use the full Newlib libraries (which are bigger).  You can add floating point to the Newlib-nano libraries which is a bit bigger than not using the libraries but stillsmaller than Newlib.

   

You have two choices for using floating point with Newlib-nano....

   

1) You can add "-u _printf_float" to the linker command line.

   

2) You can also put a reference to the support function into one of the object files with a statment like....

   

asm (".global _scanf_float");

   

 

   

Attaching the newlib-nano readme.

   

Hope that helps,

   

-Robyn

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked
        @Robyn   
Checked, tested and confirmed. Works like a charm.   
Are there any more secret documents we ought to read for the new release?   
   
Bob   
0 Likes
RobynW
Employee
Employee
5 likes given First comment on blog 10 likes received

 @Bob

   

I'd recommend reading the blog hli points to earlier in this post.  I think it's a great high level description by Joey Ye on ARM's embedded open source toolchain team.  ARM put a lot of thought into the new 4.7 compiler and the new Newlib-nano libraries.  They are a lot more targeted for small memory devices and we wanted to let the PSoC Community take the best advantage of that.

   

We try to make sure all the docuemntation needed is readily available from within Creator.  We do have some challenges when it's not our documentation though!

   

-Robyn 

0 Likes
Anonymous
Not applicable
0 Likes
Anonymous
Not applicable

Hi everybody,

   

I have the same problem with float values and found this topic here during my search. I am using Creator 3.2 and ARM GCC 4.8.4

   

In the Linker options I changed "Use newlib-nano" to FALSE as recommended here. But the problem still exist in my case.

   

        binary_combf=binary_comb/32;
        
        sprintf(TransmitBuffer,"temp_result_float: %f", binary_combf);
        UART_1_PutString(TransmitBuffer);

   

"binary_comb" is just in integer value, by dividing it with 32 there should be an float value (float binary_combf). But by showing

   

the result via UART there is just 0.

   

Am I doing anything wrong or is it some kind of unsolved bug?

   

Thank you in advance for your help

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

We are now at Creator version 3.3. I would suggest you to upgrade because that issue is cured.

   

Use newlib nano and newlib nano float formatting in project build settings -> Linker

   

Increase heap to 200 bytes.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

It is me again,

   

sorry, I found a related topic called "Defect report: sprintf() with floats just prints 0x00 with no error." And there i found the solution.

   

My problem was solved by adjusting the heap size to 0x0200!

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Fine that it works. But I still suggest you to upgrade to 3.3.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

I have already used creator version 3.3, increased the heap size to 0x0200,  Used newlib nano and newlib nano float formatting in project build settings -> Linker, but the float number still doesn't appear on my LCD. this is my example code:

   

#include <project.h>
#include <stdio.h>

   

int main()
{
    
    
    char teststring[16];
    float test = -1.2345;
    LCD_Start();
    LCD_Position(0,0);
    sprintf(teststring, "%.2f", test ); 
    LCD_PrintString(teststring);
    for(;;)
    {
    
    }
   
}

   

sorry i am real beginner at this.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Welcome in the forum!

   

The code looks ok at first sight. Can you post your complete project, so that we all can have a look at all of your settings? To do so, use
Creator->File->Create Workspace Bundle (minimal)
and attach the resulting file. Tell us please, which board you use, CY8C-042, -044, -049, -030, -050 etc.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

Hello Bob.

   

Can you help me solve the problem with float values. I use creator 3.3 version, I've enabled newlib-nano, newlib-nano Floating formatting, but my values still doesn't appear, my code is correct, I've checked it in Debug state.

   

Maybe increasing of heap will help me, but I have no idea how to do that. Help me pls...

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

You do need to increase the heap or the program might crash.

   

Open the .cydwr-file (where you assign the pins), select the "System" tab at the bottom and in the list you find the settings for the heap which you should increase to 0x200

   

 

   

Bob

0 Likes
Anonymous
Not applicable

thanks a lot, that helped me.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

You are always welcome!

   

 

   

Bob

   

What's your dog's size? We have got a Ratonera   and a Maremmano mix

0 Likes
Anonymous
Not applicable

I have German Shepherd of common size, but with broken ears, so they are not straight-up

0 Likes
tiphc_1376351
Level 3
Level 3
First like received

I am using PSoC Creator 3.3.  When I step through the following program using the debugger

   

<code>

   

int main()
{
    int j;
    int n;
    double x;
    char float_value[16];
    
    x = 0.0;
    j = 0;
    n = 0;
    j = sprintf(float_value,"%5.2lf",45.);
    n = sscanf(float_value,"%lf",&x);
    
    CyGlobalIntEnable; /* Enable global interrupts. */

   

    for(;;)
    {
        asm("NOP");
    }
}

   

</code>

   

with "use newlib-nano" set to FALSE, and a heap size of 0x0800 bytes, both the sprintf() and the sscanf() work properly.

   

With "use newlib-nano" set to FALSE, and a heap size of 0x0200 bytes, the program gets trapped in IntDefaultHandler() when it tries to execute the sprintf().

   

With "use newlib-nano" and "use newlib-nano Float Formatting" set to TRUE and a heap size of 0x0800, the sprintf() works properly but the sscanf() does not.

   

With "use newlib-nano" and "use newlib-nano Float Formatting" set to TRUE and a heap size of 0x0200, the sprintf() works properly but the sscanf() does not.

   

I conclude that the support for floating point formats in newlib-nano is still dodgy.

0 Likes

Timothy, thank you so much for this post!

   

I was struggling all day trying to find what's wrong with my code because I couldn't print floats to my LCD. I increased the heap size and set newlib-nano to false as you said and then I was able to see float values to my LCD!

   

 

   

I am using a CY8CKIT - 042- BLE by the way.

   

 

   

Kind regards,

   

Nikos

0 Likes
DaHu_285096
Level 5
Level 5
10 likes received 250 replies posted 100 replies posted

Be great if there was a pull-down menu option in IDE to use the FloatF library. Maybe a button hooked to a script that make the appropriate nano library setting and adjusts the memory to suit.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

I do not have had any problems printing floats, although I do not use an LCD but using the UART-USB bridge and PuTTY.

   

My settings are:

   

Heap 0x200

   

Use newlib-nano

   

use newlib-nano float formatting

   

Only restriction I found so far is that the float formatting is not reentrant

   

Advantage is that the nano library is considerably smaller!

   

 

   

Bob

0 Likes
tiphc_1376351
Level 3
Level 3
First like received

As noted above, the sprintf() function seems to work in the newlib-nano while sscanf() does not.

0 Likes
Anonymous
Not applicable

I make all the step on Psoc Creator 3.3 project setting, change link value and thats not work, any can helpme resolve that?

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

@Fernandov

   

Can you please post your complete project, so that we all can have a look at all of your settings. To do so, use
Creator->File->Create Workspace Bundle (minimal)
and attach the resulting file.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

Thanks for the helpful responses. I set the newlib-nano float formatting = true and heap size 0x200 and sprintf with %f worked!

   

As a beginner I've found this forum (and Cypress' documentation and videos in general) very helpful compared to some other manufacturers.

   

Now if I would stop trying to use ports with bypass caps, or ports allocated to kitprog on my CYC8KIT-059...

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Welcome in the forum, david.

   

The schematic for your -059 kit is already installed on your PC. Look at ..\Program Files (x86)\Cypress\CY8CKIT-059\1.0\Hardware\

   

You will see in the .pdf which pins are already in use.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

The information in this thread do not seem to work with PsOC Creator 4. I've gone round and round and cannot get it to display. I am using the uart and sprintf on my pioneer BLE board.

   

This causes a hang:

   

            char str[10];
            float tempVal = 175.72;

   

sprintf(str, "New Temp = %+5.1f", tempVal);
            UART_UartPutString(str);
            UART_UartPutCRLF(0);

   

 

   

Banging my head against the wall, here.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Stop banging!!! I couldn't sleep a wink!!! Set the heap space (in System view) to 0x0200 and in project build settings -> linker set "use newlib nano float formatting" to true.

   

 

   

Bob

0 Likes
Len_CONSULTRON
Level 9
Level 9
Beta tester 500 solutions authored 1000 replies posted

The %f descriptor in sscanf is not working in the newlib-nano version of lib.c.  Turn off newlib-nano and use the standard lib.c.  It works.  Sad part is the the standard lib.c is much larger than the nano version.

Len
"Engineering is an Art. The Art of Compromise."
0 Likes