cancel
Showing results for 
Search instead for 
Did you mean: 

How to redirect printf?

anonymous.8
Senior II

I have generated a project in CubeIDE and I wish to redirect printf's output. Alas, neither of the methods I've found by googling work: writing a custom _write() - function does nothing, writing a custom __io_putchar() does nothing. These are the methods that many posts here on the forums have told one to use and they're also what the examples do and yet they don't work.

Does the code generated by CubeIDE require some extra steps or what the hell is going on?

1 ACCEPTED SOLUTION

Accepted Solutions

No, I didn't have a newline in there and your question made me think "If newline causes printf to work, that'd mean the output is getting buffered, so..what if I disable the buffering instead?" I didn't even bother to try with a newline, I just went straight for the buffering and it works:

  setvbuf(stdout, NULL, _IONBF, 0);

So, for anyone else who stumbles upon this, all you need to do is implement your own __io_putchar() and one or more of the following: 1) have a newline in all your printf-statements 2) set stdout-buffer to null as above 3) call fflush() after printf

View solution in original post

7 REPLIES 7

The USART needs to be up and functional. You'll need to ensure there is code that initializes the clocks, pins and peripheral, and that you call that function.

You should test __io_putchar(), and inspect peripheral registers in the debugger. You might also want to push data via ITM_SendChar() so they can be seen via an SWV Viewer.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

I am actually putting text on to a display and yes, the display works and the function that I am using to put characters on it works.

Adding a breakpoint on both _write() and __io_putchar() reveals that neither of them gets called.

Suggests you need to check options for printf or tinyprintf library inclusion, or have it pull the full library. Perhaps buried in project setting/metadata.

I normally use Keil, and GNU/GCC via a makefile, which pulls in the full library and we have less of a circus.

ST could do with spending some time on usability and fit/finish of their software so people should not need to ask on the forum about things that "just work" in every other professional software environment...

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Doesn't help at all that I'm new to Eclipse/CubeIDE, either :downcast_face_with_sweat: I guess I'll try poking around some more, if I just so happened to stumble upon the required setting..

Ethan HUANG
ST Employee

Just curious did you put newline (\n) in your printf? In my test, __io_putchar was indeed NOT hit when I simply used printf("test");. And I did see __io_putchar hit after adding newline such as printf("test\n");

No, I didn't have a newline in there and your question made me think "If newline causes printf to work, that'd mean the output is getting buffered, so..what if I disable the buffering instead?" I didn't even bother to try with a newline, I just went straight for the buffering and it works:

  setvbuf(stdout, NULL, _IONBF, 0);

So, for anyone else who stumbles upon this, all you need to do is implement your own __io_putchar() and one or more of the following: 1) have a newline in all your printf-statements 2) set stdout-buffer to null as above 3) call fflush() after printf