4 Replies Latest reply on Sep 26, 2019 9:41 PM by MaMi_1205306

    PSoC Creator sprintf function with the GCC compiler

    MaMi_1205306

      Hi,

       

      We are using PSoC 5LP and also using the SPRINTF function in the firmware.

      This product uses μItron compliant RTOS.

       

      (1)

      Does the sprintf function in GCC support multitasking?

       

       

      The current development environment is as follows.
      -- PSoc Creator 4.2
      -- ARM GCC 5.4-2016-q2-update
      -- Linker settings
      -- Use Newlib-nano: True
      -- Use Newlib-nano Float Formatting: True

       

      We think that sprintf may be causing problems due to simultaneous access from multiple tasks.

      So I'm trying to use re-entrant "_sprintf_r".
      However, the _reent structure must be defined, and the size of this structure is large and memory overflows.

      Looking at the GCC document, it is written that if you define "_REENT_SMALL", the size will be reduced.
      However, We don't know how to specify it.

       

      (2)

      Do you know how to define "_REENT_SMALL" ?

       

      Regards,

        • 1. Re: PSoC Creator sprintf function with the GCC compiler
          DheerajK_81

          Does the sprintf function in GCC support multitasking?

          Typically they are thread safe. However, if you change the locale mid session, you must make sure that these functions aren't affected. Making them reentrant is a good idea.

           

           

          Do you know how to define "_REENT_SMALL" ?

          Go to Build Settings > ARM GCC > Compiler > Preprocessor Definitions and add "_REENT_SMALL" as shown:

           

          Regards,

          Dheeraj

          • 2. Re: PSoC Creator sprintf function with the GCC compiler
            MaMi_1205306

            Dheeraj-san,

             

            Thank you for useful infomation.

            We will try it.

             

            Regards,

            • 3. Re: PSoC Creator sprintf function with the GCC compiler
              LePo_1062026

              MaMi,

               

              After implementing Dheeraj suggestion I'd like to make another recommendation:

              sprintf(char *str ...) functions are not "safe" functions to use for embedded systems string processing.

               

              Let me illustrate:

              char tstr[10]; // This string allocation has only 10 bytes

               

              sprintf(tstr, "This string has 48 characters and needs 49 bytes");    // Remember the '\0' character terminates a string

               

              With the code above, you just overwrote past the tstr array by 39 bytes.  Who knows what havoc occurs next.  Many times you get a stack violation or a watchdog failure (if the WDT is running).

               

              use snprintf(char *str , int str_sz...) instead.  Since you declare the maximum size to be written as the second argument it has checking for the array for str built in.  It prevents array overruns and is considered "safe".

               

              Here what the previous snippet of code would look like with snprintf():

              char tstr[10];  // This string allocation has only 10 bytes

               

              snprintf(tstr, sizeof(tstr), "This string has 48 characters and needs 49 bytes");   // Remember the '\0' character terminates a string

               

              Trust me. I learned the hard way about sprintf().  Many crashes and bizarre anomalies later.  I routinely use snprintf()  instead.  I make only FEW exceptions.

               

              Len

              1 of 1 people found this helpful
              • 4. Re: PSoC Creator sprintf function with the GCC compiler
                MaMi_1205306

                Len-san,

                 

                It was very helpful!

                Tyank you.

                Regards,