This source replaces the standard printf() and sprintf() functions in the compiler C library with low resource version with a limited set of features and output types.

Features of miniprintf include:
 • %c character
 • %d,%i signed integer (- sign added if needed)
 • %s character string
 • %u unsigned integer as decimal
 • %x or %X unsigned integer as hexadecimal (uppercase)
 • %% to write %
 • Padding with 0 or space, up to 9 digits
 • #define to allow for additional padding characters ._?
 • #define to allow %f floating point representation up to 2 decimals

Developer must implement: int _fwrite(char *str, int len) to send data out to the correct location

Example implementation of _fwrite:


int _fwrite(const void *buf, size_t count)
{
char * p = (char*)buf;
while(*p && count){
uart1_putchar((uint8_t)*p);
p++;
count-=1;
}
return 0;
}


We compiled a test program for miniprintf using ARM GCC for STM32, and a few different configurations.

Results with -O0
 • Test code without printf() - 8,800 bytes
 • Test code with standard printf() - 35,072 bytes (+26,272 bytes)
 • Test code with miniprintf() - 10,384 bytes (+1,584)
 • Test code with miniprintf() and float support - 10,704 bytes (+1,904 bytes; +320 bytes vs without float)

Results with -Os
 • Test code without printf() - 4,352 bytes
 • Test code with standard printf() - 32,064 bytes (+27,712 bytes)
 • Test code with miniprintf() - 5,024 bytes (+672 bytes !)
 • Test code with miniprintf() and float support - 6,936 bytes (+2,584 bytes; +1,912 bytes vs without float)

Sign Up For Full Access
Members gain full access to the Prosource library with dozens of source files for ARM Cortex, ATMega, PIC processors as well as desktop and embedded Linux systems. Solutions for bootloaders, Buildroot and Yocto targets as well as guided how-tos on embedded development to gain critical knowledge in new systems and speed development.
Statistics:
Language:C
Files:2
Lines:462
Files size: 12.34 KB
 
RAM usage:0 bytes, uses only stack
Avg Flash:1584 bytes (1904 bytes with float) (-O0)
Code:
miniprintf.c
File size: 8378 bytes
test.c
File size: 4019 bytes