PSoC5LP, Creator 4.4: sprintf float not working (tried other solutions)

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

cross mob
Kova_3742411
Level 1
Level 1

I'm working on a PsoC5lp board with PsoC creator 4.4 . My project is fairly due to the driver for my I2C display that my school is making me use. I set the heap size to 200 bytes. I set "use newlib-nano" to true. I set "newlib-nano float formatting" to true ( -u _printf_float). I also set link time optimization to true and and optimization level to size because of the aforementioned driver.

When I use sprintf on any non-zero float, the stringbuffer contains the following three values in place of the float:

0x00 '\000'
0x2E '.'
0x80 '\200'

The code I use is fairly simple and equivalent to:

char stringbuffer[20];
sprintf(stringbuffer, "(%.1f, %.1f)", 1.1, 1.1);

What is going wrong? I saw on stackoverflow that there might be a bug in the linker script... but that goes way above my head.

https://stackoverflow.com/questions/28746062/snprintf-prints-garbage-floats-with-newlib-nano

0 Likes
1 Solution
lock attach
Attachments are accessible only for community members.
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

I think that 200 bytes for heap is not enough.

I tested with heap = 200 bytes and got

001-heap_200.JPG

Then I changed the heap = 0x200 and got

002-heap_0x200.JPG

If your program is bigger then you may need more.

I used CY8CKIT-059

schematic

003-schematic.JPG

pins

004-pins.JPG

main.c

====================

#include "project.h"

#include "string.h"

#define STR_BUF_LEN 32

char str[STR_BUF_LEN+1] ;

int main(void)

{

    char stringbuffer[20];

    float f = 0.0 ;

  

    CyGlobalIntEnable; /* Enable global interrupts. */

    UART_Start() ;

  

    UART_PutString("\x1b[2J\x1b[;H") ;

    UART_PutString("UART Float Test\n\r") ;

  

    sprintf(stringbuffer, "(%.1f, %.1f)", 1.1, 1.1);

    UART_PutString(stringbuffer) ;

    UART_PutString("\n\r") ;

    for(;;)

    {

        snprintf(str, STR_BUF_LEN, "(%.1f, %.1f)\n\r", f, 1.1) ;

        f += 0.1 ;

        UART_PutString(str) ;

        CyDelay(1000) ;

    }

}

====================

moto

(Edited) Some typos fixed.

View solution in original post

0 Likes
2 Replies
lock attach
Attachments are accessible only for community members.
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

I think that 200 bytes for heap is not enough.

I tested with heap = 200 bytes and got

001-heap_200.JPG

Then I changed the heap = 0x200 and got

002-heap_0x200.JPG

If your program is bigger then you may need more.

I used CY8CKIT-059

schematic

003-schematic.JPG

pins

004-pins.JPG

main.c

====================

#include "project.h"

#include "string.h"

#define STR_BUF_LEN 32

char str[STR_BUF_LEN+1] ;

int main(void)

{

    char stringbuffer[20];

    float f = 0.0 ;

  

    CyGlobalIntEnable; /* Enable global interrupts. */

    UART_Start() ;

  

    UART_PutString("\x1b[2J\x1b[;H") ;

    UART_PutString("UART Float Test\n\r") ;

  

    sprintf(stringbuffer, "(%.1f, %.1f)", 1.1, 1.1);

    UART_PutString(stringbuffer) ;

    UART_PutString("\n\r") ;

    for(;;)

    {

        snprintf(str, STR_BUF_LEN, "(%.1f, %.1f)\n\r", f, 1.1) ;

        f += 0.1 ;

        UART_PutString(str) ;

        CyDelay(1000) ;

    }

}

====================

moto

(Edited) Some typos fixed.

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

kova,

moto-san is correct about increasing the heap.   You may also need to make sure the stack many need to be increased.

It appears you got the floating point library setting correct.

Len

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