8 Replies Latest reply on Jul 23, 2013 6:34 PM by user_1386626

    Alternate for sprintf in PSoC 4

    asha.ganesan

      Hi All,

         

      As you may know, PSoC 4 has limited flash when compared with PSoC 3 and PSoC 5LP. Using the GCC print and sprintf functions takes up ~30k of flash, which easily overflows PSoC 4’s flash space.To avoid this, we have developed the tinyprintf component and an example project showing its use. 

         


      The component supports d, u, c, x, X, and s formats.  This component uses an open source printf solution created by Spare Time Labs 2.0. DRSW found this implementation on github. You can find the source at https://github.com/cjlano/tinyprintf. The solution is distributed under the BSD license. This basically means that the solution is to be taken as is and the author is not responsible under any circumstances for its use.

         


      The project builds with 1656 bytes flash used with the UART and tinyprintf components initialized. With all the examples the project takes 3912 bytes of flash.

         

       

         

      Regards,

         

      Asha

        • 1. Re: Alternate for sprintf in PSoC 4
          user_78878863

          Do you know whether this also works on the PSoC3 and 5? Would be useful there too...

          • 2. Re: Alternate for sprintf in PSoC 4
            ki.leung
                    Very nice, shall be able to use it for the next project.   
            • 3. Re: Alternate for sprintf in PSoC 4
              user_1377889

              I already adapted and used it for a PSoC4 and it will work for any other PSoC as well.

                 

               

                 

              Bob

              • 4. Re: Alternate for sprintf in PSoC 4
                user_1377889

                Yes, I just checked: Avagadro posted May, 22nd a link to the tinyprintf library and since that time I use it for PSoC4.

                   

                Since I am quite content with the implementation of printf() in PSoC3 and 5 I didn't change something there.

                   

                 

                   

                Bob

                • 5. Re: Alternate for sprintf in PSoC 4
                  user_78878863
                          I was just asking because now and then the question comes up how to save flash memory on PSoC3 or 5. So now there is one more answer in our toolbox :)   
                  • 6. Re: Alternate for sprintf in PSoC 4
                    user_38832401

                     I am really glad someone has done something about this as I really need to be able to send data to a terminal in the PSoC4.  But I have loaded the project as is in the zip and I get compile errors.  I am using a PSoC4 kit and it has been working otherwise.  Here are the errors:

                       

                    Error1:

                       

                    An error occurred when binding instance "PSoC_4_Pioneer_Kit_1" of symbol "PSoC_4_Pioneer_Kit": (Unable to locate and customize component 'PSoC_4_Pioneer_Kit' used in schematic 'D:\Users\Sean\Documents\Cypress Projects - Examples\Psoc4 - tiny printf\PSoC4_printf.cydsn\TopDesign\TopDesign.cysch'.).

                       

                    Error 2:

                       

                    Every signal bit must have exactly one driver (e.g., connected to one schematic input terminal or one instance output terminal). The given bits do not have any drivers. Add drivers for the indicated signal bits.

                       

                    Error3:

                       

                    An error occurred trying to retrieve the DRCs for an instance, 'PSoC_4_Pioneer_Kit_1'. Unable to locate and customize component 'PSoC_4_Pioneer_Kit' used in schematic 'D:\Users\Sean\Documents\Cypress Projects - Examples\Psoc4 - tiny printf\PSoC4_printf.cydsn\TopDesign\TopDesign.cysch'.

                       

                     

                       

                    Any help would be greatly appreciated especially if it is quick! :)

                       

                    Sean

                    • 7. Re: Alternate for sprintf in PSoC 4
                      user_38832401

                       Never mind.  I used an updated project here: http://www.element14.com/community/message/78621#78621/l/psoc-4-pioneer-kit-community-project023-tinyprintf-example

                         

                      It worked just fine.  Now if only there were clear instructions on what needs to be copied over to my existing project! :)  Sorry but some of this stuff is new to me.

                      • 8. Re: Alternate for sprintf in PSoC 4
                        user_1386626

                        Very nice thanks !  Before your posting I used the following functions (an impementation of  itoa) to my programs to get arount one of the sprintf issues. Lookinf forward to using tinyprintf.

                           

                        /**
                             * 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;
                            }