2019-06-04 08:06 AM
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?
Solved! Go to Solution.
2019-06-04 10:22 PM
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
2019-06-04 08:15 AM
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.
2019-06-04 08:19 AM
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.
2019-06-04 08:24 AM
Adding a breakpoint on both _write() and __io_putchar() reveals that neither of them gets called.
2019-06-04 09:50 AM
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...
2019-06-04 09:53 AM
Doesn't help at all that I'm new to Eclipse/CubeIDE, either I guess I'll try poking around some more, if I just so happened to stumble upon the required setting..
2019-06-04 08:47 PM
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");
2019-06-04 10:22 PM
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