Okay.
I tried this example too and it still does not work. Did the following: Created the attached .c file for overriding the _write and _read functions. Copied it into the source folder of my project (not the board library). Commented out #include retarget.h in board.c. Uncommented #define DEBUG_SEMIHOSTING.
Switched library between newlib (semihosting), newlib (nohost), newlib (none) and get the following results
newlib (semihosting) - complains there are multiple definition of _read and _write, line 290 external location E:\jenkins-slave\workspace\LibrariesFresco\Lewlib_hostings_newlib_stub_semihost\srt\syscalls.c and does not build
newlib (nohost) - compiles and runs, but any calls to printf disappear into the air and are not put out on the UART or the debug window or the LPCXpresso Console
newlib (none) - does not compile with an unspecified error.
It seems like the linker is still overriding my _write function with something else. It should be noted that for whatever reason I cannot change my library headers from the Quick Settings toolbar. The options are all grayed out. I'm manually changing the project settings for MCU C Compiler ->Miscellaneous, MCU Assembler -> Architecture & Headers, and MCU Linker -> Managed Linker Script.
retarget_uart.c
//*****************************************************************************
// retarget_itm.c - Provides retargeting of C library printf/scanf
// functions via ITM / SWO Trace
//*****************************************************************************
// Copyright(C) NXP Semiconductors, 2015
// All rights reserved.
//
// Software that is described herein is for illustrative purposes only
// which provides customers with programming information regarding the
// LPC products. This software is supplied "AS IS" without any warranties of
// any kind, and NXP Semiconductors and its licensor disclaim any and
// all warranties, express or implied, including all implied warranties of
// merchantability, fitness for a particular purpose and non-infringement of
// intellectual property rights. NXP Semiconductors assumes no responsibility
// or liability for the use of the software, conveys no license or rights under
// any patent, copyright, mask work right, or any other intellectual property
// rights in or to any products. NXP Semiconductors reserves the right to make
// changes in the software without notification. NXP Semiconductors also makes
// no representation or warranty that such application will be suitable for the
// specified use without further testing or modification.
//
// Permission to use, copy, modify, and distribute this software and its
// documentation is hereby granted, under NXP Semiconductors' and its
// licensor's relevant copyrights in the software, without fee, provided that it
// is used in conjunction with NXP Semiconductors microcontrollers. This
// copyright, permission, and disclaimer notice must appear in all copies of
// this code.
//*****************************************************************************
#include <stdint.h>
// ******************************************************************
// Cortex-M SWO Trace / Debug registers used for accessing ITM
// ******************************************************************
// CoreDebug - Debug Exception and Monitor Control Register
#define DEMCR (*((volatile uint32_t *) (0xE000EDFC)))
// DEMCR Trace Enable Bit
#define TRCENA (1UL << 24)
// ITM Stimulus Port Access Registers
#define ITM_Port8(n) (*((volatile uint8_t *) (0xE0000000 + 4 * n)))
#define ITM_Port16(n) (*((volatile uint16_t *) (0xE0000000 + 4 * n)))
#define ITM_Port32(n) (*((volatile uint32_t *) (0xE0000000 + 4 * n)))
// ITM Trace Control Register
#define ITM_TCR (*((volatile uint32_t *) (0xE0000000 + 0xE80)))
// ITM TCR: ITM Enable bit
#define ITM_TCR_ITMENA (1UL << 0)
// ITM Trace Enable Register
#define ITM_TER (*((volatile uint32_t *) (0xE0000000 + 0xE00)))
// ITM Stimulus Port #0 Enable bit
#define ITM_TER_PORT0ENA (1UL << 0)
// ******************************************************************
// Buffer used for pseudo-ITM reads from the host debugger
// ******************************************************************
// Value identifying ITM_RxBuffer is ready for next character
#define ITM_RXBUFFER_EMPTY 0x5AA55AA5
// variable to receive ITM input characters
volatile int32_t ITM_RxBuffer = ITM_RXBUFFER_EMPTY;
// ******************************************************************
// Redlib C Library function : __sys_write
// Newlib C library function : _write
//
// Function called by bottom level of printf routine within C library.
// With the default semihosting stub, this would write the
// character(s) to the debugger console window (which acts as
// stdout). But this version writes the character(s) from the Cortex
// M3/M4 SWO / ITM interface for display in the ITM Console.
// ******************************************************************
#if defined (__REDLIB__)
int __sys_write(int iFileHandle, char *pcBuffer, int iLength) {
#elif defined (__NEWLIB__)
int _write(int iFileHandle, char *pcBuffer, int iLength) {
#endif
//int _write(int iFileHandle, char *pcBuffer, int iLength)
//{
#if defined(DEBUG_ENABLE)
unsigned int i;
for (i = 0; i < iLength; i++) {
Board_UARTPutChar(pcBuffer[i]);
}
#endif
return iLength;
}
#if defined (__REDLIB__)
// ******************************************************************
// Redlib C Library function : __sys_readc
//
// 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 a buffer
// which acts as a pseudo-interface to the Cortex-M3/M4 ITM.
// ******************************************************************
int __sys_readc(void) {
int32_t c = -1;
// check if debugger connected and ITM channel enabled for tracing
if ((DEMCR & TRCENA) &&
// ITM enabled
(ITM_TCR & ITM_TCR_ITMENA) &&
// ITM Port #0 enabled
(ITM_TER & ITM_TER_PORT0ENA)) {
do {
if (ITM_RxBuffer != ITM_RXBUFFER_EMPTY) {
// Read from buffer written to by tools
c = ITM_RxBuffer;
// Flag ready for next character
ITM_RxBuffer = ITM_RXBUFFER_EMPTY;
}
}while (c == -1);
}
return c;
}
// #endif REDLIB __sys_readc()
#elif defined (__NEWLIB__)
// ******************************************************************
// Function _read
//
// Called by bottom level of scanf routine within Newlib C library
// to read multiple characters. With the default semihosting stub, this
// would read characters from the debugger console window (which
// acts as stdin). But this version reads the characters from a buffer
// which acts as a pseudo-interface to the Cortex-M3/M4 ITM.
// ******************************************************************
int _read(void)
{
#if defined(DEBUG_ENABLE)
char c = Board_UARTGetChar();
return (int) c;
#else
return (int) -1;
#endif
}
#endif // NEWLIB _read()