Search code examples
c++cserial-portprintfuart

Is there a format processor to write my own printf-like function and keep the %d style arguments, without using sprintf?


I'm writing a serial interface for an MCU, and I want to know how one would create a printf-like function to write to the serial UART. I can write to the UART, but to save memory and stack space, and avoid temp string buffers, I would prefer to do that write directly instead of doing sprintf() to a string and then writing the string via serial. There is no kernel and no file handling, so FILE* writes like those from fprintf() won't work (but sprintf() does).

Is there something that processes formatted strings for each char, so I can print char-by-char as it parses the format string, and applies the related arguments?

We are using newlib as part of the efm32-base project.

UPDATE

I would like to note that ultimately we implemented the _write() function because thats all newlib needs to light up printf.


Solution

  • depending on your standard library implementation you need to write your own versions of fputc or _write functions.