Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Real time embedded FreeRTOS mailing list 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT Training


sprintf in FreeRTOS

Posted by pugglewuggle on November 23, 2015

I saw a couple posts asking about thread safety for sprintf online. Is it safe to use sprintf from stdio.h within a FreeRTOS task or is that a bad idea? If it is not a good idea, what is the alternative because I really need this functionality.

sprintf in FreeRTOS

Posted by rtel on November 23, 2015

The short answer is "it depends on the chip, compiler, and library you are using".

The longer answer is that in some situations in which FreeRTOS is used it is ok to call sprintf() from multiple tasks, unlike printf() which will always generate a conflict when accessing whichever console is used. However, if you are using a standard GCC sprintf(), then be aware that it will use a huge amount of stack, and might pull in all the floating point libraries which will have the effect of doubling the binary image size.

Many of the demo applications in FreeRTOS include a file called prtinf-stdarg.c which contains a small and limited but very stack light sprintf() implementation. Be aware that not all the printf-stdarg.c files implement snprintf() as they just ignore the buffer size argument. The FreeRTOS+ download contains some more featured implementations too.


sprintf in FreeRTOS

Posted by heinbali01 on November 23, 2015

Hi Pugglewuggle

The [v]s[n]printf family is very useful but it its definitions and usage are a bit controversial ( see e.g. http://linux.die.net/man/3/snprintf )

Some implementations are stack hungry, up to > 1 KB. Some implementations are not interrupt-proof. You will have to try it out to know.

I attached an implementation that is interrupt-proof and uses a modest amount of stack, about 80 bytes.

snprintf : the official versions may return an amount that it bigger that the size supplied. If that happens, the result may not nul terminated (GCC and MSC that I just tried do write a nul at the end).

The attached version will always write a terminating nul and it will return at most size - 1 bytes.

~~~~~ char buffer[ 6 ]; int rc = snprintf( buffer, sizeof buffer, "%s", "Hello world" ); ~~~~~

official snprintf returns "Hello" with value 11 attached snprintf returns "Hello" with value 5



printf-stdarg.c (14134 bytes)

sprintf in FreeRTOS

Posted by heinbali01 on November 23, 2015

After I pressed "Post" I saw that Richard had responded earlier :-)

The source that I attached indeed contains the full-featured version of [v]s[n]printf() with an extensive printf syntax and that also checks for the buffer size. You find it in the The FreeRTOS+ downloads.

Note that the usage of the user-provided function:

~~~~~ void vOutputChar( const char cChar, const TickType_t xTicksToWait ) ~~~~~

is a bit historical. The idea was to that if the buffer is NULL, the function vOutputChar() will be called for each character:

~~~~~ sprintf( NULL, "This output goes to the user functionn" ); ~~~~~


[ Back to the top ]    [ About FreeRTOS ]    [ Sitemap ]    [ ]

Copyright (C) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd.. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.

Latest News:

FreeRTOS V9.0.0 is now available for download.

Free TCP/IP and file system demos for the RTOS

Sponsored Links

⇓ Now With No Code Size Limit! ⇓
⇑ Free Download Without Registering ⇑

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Renesas Electronics Gold Alliance RTOS Partner.jpg

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Atmel RTOS partner supporting ARM Cortex-M3 and AVR32 microcontrollers

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Xilinx Microblaze and Zynq partner

Silicon Labs low power RTOS partner

Altera RTOS partner for Nios II and Cortex-A9 SoC

Freescale Alliance RTOS Member supporting ARM and ColdFire microcontrollers

Infineon ARM Cortex-M microcontrollers

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

Cypress RTOS partner supporting ARM Cortex-M3

Fujitsu RTOS partner supporting ARM Cortex-M3 and FM3

Microsemi (previously Actel) RTOS partner supporting ARM Cortex-M3

Atollic Partner

IAR Partner

Keil ARM Partner

Embedded Artists