To use the standard output functions putchar, puts, and printf, you need to customize the way that characters are written to the standard output device. These output functions rely on a function __putchar that outputs a character and returns an indication of whether it was successfully written.
The prototype for __putchar is
int __putchar(int ch);
Sending all output to the CrossStudio virtual terminal
You can send all output to the CrossStudio virtual terminal by supplying the following implementation of the__putchar function in your code:
#include <__cross_studio_io.h>
int __putchar(int ch)
{
return debug_putchar(ch);
}
This hands off output of the character ch to the low-level debug output routine, debug_putchar.
Whilst this is an adequate implementation of __putchar, it does consume stack space for an unnecessary nested call and associated register saving. A better way of achieving the same result is to define the low-level symbol for __putchar to be equivalent to the low-level symbol for debug_putchar. To do this, we need to instruct the linker to make the symbols equivalent.
To do this using the HCC environment:
- Select the project node in the Project Explorer.
- Display the Properties Window.
- Enter the text “-D___putchar=_debug_putchar” into the Additional Options property of the Linker Options group. Note that there are three leading underscores in ___putchar and a single leading underscore in _debug_putchar because the C compiler automatically prepends an underscore to all global symbols.
To do this using the GCC environment:
- Select the project node in the Project Explorer.
- Display the Properties Window.
- Enter the text “__putchar=debug_putchar” into the Linker > Linker Symbol Definitions property of the Linker Options group.
Sending all output to another device
If you need to output to a physical device, such as a UART, the following notes will help you:
- If the character cannot be written for any reason, putchar must return EOF.
- The higher layers of the library do not translate C's end of line character '\n' before passing it to putchar. If you are directing output to a serial line connected to a terminal, for instance, you will most likely need to output a carriage return and line feed when given the character '\n' (ASCII code 10).