ARM Technical Support Knowledge Articles

How do I retarget C++ streamed I/O?

Applies to: ARM Developer Suite (ADS), DS-5, RealView Developer Kit (RVDK) for OKI, RealView Developer Kit (RVDK) for ST, RealView Developer Kit for XScale (RVXDK), RealView Development Suite (RVDS)

Answer

The C++ libraries depend on the C library for target-specific support. There are no target dependencies in the C++ libraries.

To retarget your C++ streamed I/O, we recommend that you retarget the _sys_xxxx driver level functions. These are the functions used by the C and C++ library and are responsible for target specific I/O. The default implementation uses Semihosting to handle I/O on the host machine.

The ARM Compiler toolchain's C and C++ libraries documentation discusses which functions have to be re-implemented. A summary is shown below:

_sys_open()
_sys_close()
_sys_read()
_sys_write()
_sys_ensure()
_sys_flen()
_sys_seek()
_sys_istty()
_sys_tmpnam()
_sys_exit()
_ttywrch() 
For an example file (retarget.c), used for redirecting C++ streamed I/O to an UART (connected to e.g. a Terminal Emulator), click here.

To retarget standard input/output functionality, low level library functions such as fgetc() and fputc()can be re-implemented instead.

In ADS 1.2, RVDS 3.x, RVDS 4.x and DS-5, only functions such as fgetc() and fputc() need to be reimplemented. With RVDS 2.x an additional library function (used by cin) __fread_bytes_avail() must also be reimplemented.

The default version of __fread_bytes_avail() fills a buffer by calling fgetc() until either the buffer is filled or EOF is encountered.  Since the retargeted version of fgetc() in the example code simply returns characters from a UART (and never returns EOF), __fread_bytes_avail() must be re-implemented to read a single character by calling fgetc():
 
#ifdef __cplusplus
extern "C"
#endif
size_t __fread_bytes_avail(void *buf, size_t nbytes, FILE *stream)
/* Read a single character into buf[] using fgetc() */
{
  *(char *)buf = (char)fgetc(stream);
  return 1;
}

This function should be added to retarget.c in the Application Note 107 code \embeddedsw\Source example.

For ADS users, general retargeting of I/O functions is described in Application Note 107, "Embedded Software Development".  For RVDS users, general retargeting of I/O functions can be found in the RealView Compilation Tools documentation. For DS-5 users, see ARM Compiler toolchain documentation.

Download

 TEXT Icon  retarget.c 

Attachments: retarget.c

Article last edited on: 2011-09-20 16:35:49

Rate this article

[Bad]
|
|
[Good]
Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential