UART redirection with C++ on the FRDM-K64F

Showing results for 
Search instead for 
Did you mean: 

UART redirection with C++ on the FRDM-K64F

Contributor II

In trying to create a test project in C++ for the FRDM-K64F with MCUXpresso, I found that UART redirection of printf and std::count calls was not working.  After some investigation, I found that fsl_debug_console.c does not define the necessary redirection functions for Newlib (_write and _read, see Using printf()) in C++ projects.  For Redlib in C projects, the equivalent functions (__sys_write, __sys_readc) are correctly defined but for some reason the Newlib functions are missing.

The attached fsl_debug_console.c adds the missing functions and successfully redirects to/from the UART with printf, scanf, std::cout, and std::cin.

Here are the versions that I am using:

- MCUXpresso IDE v10.0.2 [Build 411] [2017-07-11] 

- MXUXpresso SDK 2.2.0

Labels (1)
3 Replies

Contributor III

Hello Kyle,

I'm having the same problem, but the solution doesn't help.

I'm using a FRDM-KL26Z on MCUXpresso 11.3.0 and SDK version 2.2.0; a very similar setup to what you're using. The file compiles seamlessly—there's only one change, but _write never gets called. (Not testing _read)

To reproduce the problem:

Make a new C++ project for the FRDM-KL26Z using the wizard, use all default settings. Replace the fsl_debug_console.c with the one provided by Kyle above. Change thePRINTF("Hello World\n"); line in main() to:

    PRINTF("PRINTF Hello World\n");

    printf("printf Hello World\n");

    PRINTF("look for \"printf Hello World\" above!\n");

Put a breakpoint somewhere early on in:

int _write(int handle, char *buffer, size_t size)

But it never gets called.

What am I doing wrong?


Thank you very kindly for your help,


0 Kudos

Contributor III

Hi Everyone,

I figured it out. It's in fact calling _write_r() instead of _write, so adding a function that does the same as _write in Kyle's file above, but is defined as:

int _write_r(struct _reent *reent, int handle, const void *buffer, size_t size) {



will get the job done somewhat crudely. Better to get Newlib to use _write() in the first place. I think that's accomplished by turning off threads? 


0 Kudos

Contributor III

Ran into the same issue described above for std::cout redirection.

Modified for MCUXpresso v10.1.0 (build 606) and SDK 2.3.0 (below) and std::cout displays to UART.

Note: A separate CR (\r) is necessary in addition to std::endl.


MCUXpresso SDK team, would you be able to promote this change into the SDK sources for future projects?

0 Kudos