Specifications
For more information about the GCC small newlib C library, refer to the newlib documentation installed
with the Nios II EDS. On the Windows Start menu, click Programs > Altera > Nios II > Nios II
Documentation.
Note: The Nios II implementation of the small newlib C library differs slightly from GCC.
For more information about the differences, refer to the "Limitations of the Nios II Small newlib C
Library" table (Table 6-9).
Use UNIX-Style File I/O
If you need to reduce the code footprint further, you can omit the newlib C library, and use the UNIX-
style API.
For more information, refer to the “UNIX-Style Interface” chapter.
The Nios II EDS provides ANSI C file I/O, in the newlib C library, because there is a per-access perform‐
ance overhead associated with accessing devices and files using the UNIX-style file I/O functions. The
ANSI C file I/O provides buffered access, thereby reducing the total number of hardware I/O accesses
performed. Also the ANSI C API is more flexible and therefore easier to use. However, these benefits are
gained at the expense of code footprint.
Related Information
UNIX-Style Interface on page 6-4
Emulate ANSI C Functions
If you choose to omit the full implementation of newlib, but you need a limited number of ANSI-style
functions, you can implement them easily using UNIX-style functions.
Example 6–15. Unbuffered getchar()
/* getchar: unbuffered single character input */
int getchar ( void )
{
char c;
return ( read ( 0, &c, 1 ) == 1 ) ? ( unsigned char ) c : EOF;
}
This example is from the book:
The C Programming Language, Second Edition, by Brian W. Kernighan
and Dennis M. Ritchie. This standard textbook contains many other useful functions.
Use the Lightweight Device Driver API
The lightweight device driver API allows you to minimize the overhead of accessing device drivers. It has
no direct effect on the size of the drivers themselves, but lets you eliminate driver API features which you
might not need, reducing the overall size of the HAL code.
The lightweight device driver API is available for character-mode devices. The following device drivers
support the lightweight device driver API:
• JTAG UART
• UART
• Optrex 16207 LCD
For these devices, the lightweight device driver API conserves code space by eliminating the dynamic file
descriptor table and replacing it with three static file descriptors, corresponding to stdin, stdout, and
stderr. Library functions related to opening, closing, and manipulating file descriptors are unavailable,
NII5V2
2015.05.14
Use UNIX-Style File I/O
6-31
Developing Programs Using the Hardware Abstraction Layer
Altera Corporation
Send Feedback