AnsweredAssumed Answered

printf redirection to UART using newlib

Question asked by mcheah on Jul 26, 2016
Latest reply on Oct 4, 2016 by LPCX presso support

I'm racking my brain to find a way to redirect DEBUGOUT and PRINTF to the UART using newlib on LPCXpresso 7.9.2. I'm using LPCOpen for the lpc_board_ea_devkit_4088 platform from embedded artists Quickstart board.

I'm running code that has .cpp files so I cannot use redlib, so I've switched to newlib, but I for the life of me cannot find a way to override the printf function to be directed to the UART.

I'm running newlib (NoHost).

Examples given say to override the _write function, but they don't seem to work in any meaningful way.  I've altered the retarget.h header in my board library as follows, but I'm not sure it takes any effect.  All the examples seem to be directed towards redlib in particular (which does work), but don't seem to work with newlib.  I'm not entirely convinced that the documentation for the printf stuff is up to date for newlib (referring to this:Using printf() ).  Anyone have suggestions?  Am I missing something basic here?  Seems like something so simple shouldn't be that hard.


#if defined( __NEWLIB__ )

/* Include stdio.h to pull in __REDLIB_INTERFACE_VERSION__ */

#include <stdio.h>


#if defined(DEBUG_ENABLE)


/* Do nothing, semihosting is enabled by default in LPCXpresso */

#endif /* defined(DEBUG_SEMIHOSTING) */

#endif /* defined(DEBUG_ENABLE) */



int _write(int iFileHandle, char *pcBuffer, int iLength)


#if defined(DEBUG_ENABLE)

  unsigned int i;

  for (i = 0; i < iLength; i++) {




  return iLength;



/* Called by bottom level of scanf routine within RedLib C library to read

   a character. With the default semihosting stub, this would read the character

   from the debugger console window (which acts as stdin). But this version reads

   the character from the LPC1768/RDB1768 UART. */

int _read(void)


#if defined(DEBUG_ENABLE)

  char c = Board_UARTGetChar();

  return (int) c;




  return (int) -1;





#endif /* !defined(DEBUG_SEMIHOSTING) */

#endif /* defined ( __NEWLIB__ ) */