Remaping printf on custom boards in Mbed 6

What is the appropriate way to remap printf to a custom serial port in Mbed 6? When moving from a FRDM k64f board to a custom board, I removed the USB port and would like to know the “correct” way to redirect printf to a different UART.

I have tried to rename the change the STDIO_UART_TX , STDIO_UART_RX, and STDIO_UART macros to the different pins, but that doesn’t seem to have had an effect.

Hello Kyle,

I don’t know whether this is the “correct” way to do it but you can try the mbed_override_console hook:

/** Applications may implement this to change stdin, stdout, stderr.
 *
 * This hook gives the application a chance to specify a custom FileHandle
 * for the console.
...
 */
FileHandle *mbed_override_console(int fd);

For example, to remap printf to an USART port available on pins D0, D1:

#include "mbed.h"

...

FileHandle *mbed::mbed_override_console(int)
{
    static BufferedSerial custom_target_console(D0, D1);
    return &custom_target_console;
}

...

int main()
{
...
}

NOTE: The mbed_override_console is declared in the mbed-os/platform/mbed_retarget.h file.

Best regards, Zoltan

4 Likes