2 Replies Latest reply on Nov 6, 2020 8:40 AM by LePo_1062026

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

    Kova_3742411

      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

        • 1. Re: PSoC5LP, Creator 4.4: sprintf float not working (tried other solutions)
          MoTa_728816

          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.

          • 2. Re: PSoC5LP, Creator 4.4: sprintf float not working (tried other solutions)
            LePo_1062026

            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