12 Replies Latest reply on Jun 3, 2013 11:05 PM by user_1386626

    sprintf  PSoC4 GCC Compiler

    user_1386626

      Compiling  programs on the PIONEER KIT using sprintf  almost uses all the flash memory even for a simple program as shown below:

         

      #include <device.h>
      #include <stdio.h>
      void main()
      {
          char tmpStr[10];
          int n =5;
          Tx_Start();
          sprintf(tmpStr,"%d", n);     /* Create a formatted string */
          Tx_UartPutString(tmpStr);  
      }

         

      Flash used: 30496 of 32768 bytes (93.1 %).
      SRAM used: 2412 of 4096 bytes (58.9 %).

      --------------- Build Succeeded: 05/21/2013 17:03:04 ---------------
      Programming started for device: 'PSoC 4 CY8C4245AXI-483'.

         

      Using sprintf with floats completely over flows the flash.

         

      ERROR: .\ARM_GCC_441\Debug\HCSR04.elf section `.data' will not fit in region `rom'
      The command 'arm-none-eabi-gcc.exe' failed with exit code '1'.
      ERROR: region `rom' overflowed by 1032 bytes

         

      Is this a bug in the GCC Compiler or  am I missing something?

         

      James

        • 1. Re: sprintf  PSoC4 GCC Compiler
          ki.leung

          it should have 32k of flash. Strange.

          • 2. Re: sprintf  PSoC4 GCC Compiler
            ki.leung

            Just tried that, looks like it use upto around 30k of flash. I guess you should raise a case for Cypress support.

            • 3. Re: sprintf  PSoC4 GCC Compiler
              ki.leung

              Tried something simliar in PSoC3, it is around 1k. for PSoC5 is is around 20k. May be there is something to do with the GCC compiler as both PSoC4 and 5 should be using the GCC.

              • 4. Re: sprintf  PSoC4 GCC Compiler
                user_1377889

                I had the very same isuue yesterday, sprintf() blew up a PSoC4 project requiring 16k flash more than the chip has got.

                   

                Afaik for PSoC1 Cypress has built 3 different selectable <stdio.h> libraries supporting different complexity for the printf() function requiring different amount of flash. I cannot tell if the GCC  will allow for this.

                   

                 

                   

                Bob

                • 5. Re: sprintf  PSoC4 GCC Compiler
                  ki.leung

                  I did some search on the net and it seems that this is an common complain with the GCC compiler, Some wrote there own routine to handle this function

                  • 6. Re: sprintf  PSoC4 GCC Compiler
                    ki.leung

                    Just went thru the creator KP&S, on page 2 it mentioned the sprintf() with float arguement problem (ID 148013) and mentioned that is a detect on the M0 library. The problem may be related to your's. Cypress mentioned a new tool-chainwould be used the next major released.

                    • 7. Re: sprintf  PSoC4 GCC Compiler
                      anush.shet

                       Hey guys have you checked the tiny printf for embedded applications?

                         

                      http://www.sparetimelabs.com/tinyprintf/tinyprintf.php

                         

                      Try this guy. He will take very small amount of flash.

                      • 8. Re: sprintf  PSoC4 GCC Compiler
                        user_14586677

                        He seems to state if printing longs even his printf()/sprintf() footprint

                           

                        gets large. I have an application that can be used as  a test bed, will

                           

                        try it out when I get a chance.

                           

                         

                           

                        Regards, Dana.

                        • 9. Re: sprintf  PSoC4 GCC Compiler
                          ki.leung

                          good stuff. tks :-)

                          • 10. Re: sprintf  PSoC4 GCC Compiler
                            user_1386626

                            Thanks for all your comments! The sprintf in the GCC compiler gets worse with floats --- no output!. Ran identical code with PSoC5 using Keil compiler and PSoC4 with GCC.

                               

                            #include <device.h>
                            #include <stdio.h>
                            void main()
                            {
                                char tmpStr[10];
                                float n =-665.67;
                                Tx_Start();
                                sprintf(tmpStr,"%.3f", n);     /* Create a formatted string */
                                while(1)
                                {
                                Tx_PutChar(10);
                                Tx_PutChar(13);
                                Tx_PutString(tmpStr);  
                                }
                            }

                            PoC5:
                            Flash used: 25628 of 262144 bytes (9.8 %).
                            SRAM used: 1620 of 65536 bytes (2.5 %).

                            Tx Output:
                            -665.670                                                                       
                            -665.670                                                                       
                            -665.670                                                                       
                            -665.670                                                                       
                            -665.670                                                                       
                            -665.670
                            .
                            .

                            PSoC4:
                            Identical code

                            Flash used: 31180 of 32768 bytes (95.2 %).
                            SRAM used: 2404 of 4096 bytes (58.7 %).

                            Tx Output: None
                            CPU in Limbo

                               

                            Please POST if you have been able to get output from sprintf with floats. Require validation before I create a case.

                               

                            James

                            • 11. Re: sprintf  PSoC4 GCC Compiler
                              ki.leung

                               As in my previous post, they already say that spritf with float would cause problem. please check my the creator KP&S.

                              • 12. Re: sprintf  PSoC4 GCC Compiler
                                user_1386626

                                The sprintf is not useful for PSOC4 using the present stdio.h implementation. For integer formatting it takes to much flash and for floats it doesn't work at all.  The itoa is not defined in the GCC compiler because it not a standard ANSI function. Here is one implementation of itoa that you may find useful in a future project.

                                   

                                /**
                                     * C++ version 0.4 char* style "itoa":
                                     * Written by Lukás Chmela
                                     * Released under GPLv3.
                                     * http://www.jb.man.ac.uk/~slowe/cpp/itoa.html
                                     */
                                    char* itoa(int value, char* result, int base) {
                                        // check that the base if valid
                                        if (base < 2 || base > 36) { *result = '\0'; return result; }
                                   
                                        char* ptr = result, *ptr1 = result, tmp_char;
                                        int tmp_value;
                                   
                                        do {
                                            tmp_value = value;
                                            value /= base;
                                            *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
                                        } while ( value );
                                   
                                        // Apply negative sign
                                        if (tmp_value < 0) *ptr++ = '-';
                                        *ptr-- = '\0';
                                        while(ptr1 < ptr) {
                                            tmp_char = *ptr;
                                            *ptr--= *ptr1;
                                            *ptr1++ = tmp_char;
                                        }
                                        return result;
                                    }

                                   

                                 

                                   

                                Tested the above function  on the rangefinder (HCSR401bundle01)project posted on this this forum. Didn't want to wire the 602 LCD Module to the pioneer board so I just  added the Serial Communication Block  to the project  configured as Tx  and outputted  the distance to a virtual PC port.