Very lean Serial.printf()
I am tired to write again and again Serial.print() commands to display some values. Unfortunately the typical printf function is very big, the libc version can take almost the half or more memory of a F103C8. It is not only the function itself, is the number of functions that the linker attaches when this library is called (floating point, helpers, stdio stuff, etc)
I remember, years ago there were very optimised versions of printf() for use with 8051s taking no more than 2KB. For this reason I opened my old notebooks and I found some versions of these functions. I tried to adopt them in STM32STM32Microcontrollers and processors…Map magic glue code, ethernet compiles, just WARNING: library LwIP claims to run on [ stm32 ] architecture(s) and may be incompatible with your current board which runs on [STM32]…View in hardware catalog environment, inside Print class. I tested over 6-8 functions and I end-up with the version of sdcc compiler. I removed all mcu specific stuff and after some changes the code is running on STM32STM32Microcontrollers and processors…Map magic glue code, ethernet compiles, just WARNING: library LwIP claims to run on [ stm32 ] architecture(s) and may be incompatible with your current board which runs on [STM32]…View in hardware catalog .
What I have until now:
– A printf function that supports integer (long, short, byte), string, character and pointer variables.
– ‘ ‘, -, +, b, l, c, s, p, d, i, o, u, and x format specifiers
– No static variables, no dynamic allocation, no globals, just plain functions running with local variables.
– Print.printf member function
– Very small size : 1080 bytes (for the 3 core functions) and 64 bytes for Print.printf() encapsulation.
– Until now I cant rid off the declaration of a temporary buffer inside Print.printf(), but I am sure that the buffer is not needed (WIP…). For this reason the output of the function is limited by the size of this buffer, now is 64 bytes (no test for overflow!!!).
– No floating point but it is possible to include this functionality, I have to examine the impact of this.
I think that code needs some cleanup, but it is a good start.
The code is included in one file called print_format.c, this includes the main function and some helpers.
Two files of our core files must be changed.
First the Print.h for the declaration of the function, Roger has already the function with header guards, so I wrote a new line
int printf(const char * format, ...);