Specifications

The HAL file infrastructure also allows you to manipulate character mode devices with UNIX-style path
names. The HAL registers character mode devices as nodes in the HAL file system. By convention,
system.h defines the name of a device node as the prefix /dev/ plus the name assigned to the hardware
component at system generation time. For example, a UART peripheral that appears as uart1 in Qsys or
SOPC builder is named /dev/uart1 in system.h.
Note: The standard header files stdio.h, stddef.h, and stdlib.h are installed with the HAL.
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#define BUF_SIZE (10)
int main(void)
{
FILE* fp;
char buffer[BUF_SIZE];
fp = fopen ("/mount/rozipfs/test", "r"); if (fp == NULL)
{
printf ("Cannot open file.\n");
exit (1);
}
fread (buffer, BUF_SIZE, 1, fp);
fclose (fp);
return 0;
}
For more information about the use of these functions, refer to the newlib C library documentation
installed with the Nios II EDS. On the Windows Start menu, click Programs > Altera > Nios II > Nios II
Documentation.
Related Information
HAL API Reference on page 14-1
Read-Only Zip File System on page 12-1
Using Character-Mode Devices
A character-mode device is a hardware peripheral that sends and/or receives characters serially. A
common example is the UART. Character mode devices are registered as nodes in the HAL file system. In
general, a program associates a file descriptor to a device’s name, and then writes and reads characters to
or from the file using the ANSI C file operations defined in file.h. The HAL also supports the concept of
standard input, standard output, and standard error, allowing programs to call the stdio.h I/O functions.
Standard Input, Standard Output and Standard Error
Using standard input (stdin), standard output (stdout), and standard error (stderr) is the easiest way
to implement simple console I/O. The HAL manages stdin, stdout, and stderr behind the scenes,
which allows you to send and receive characters through these channels without explicitly managing file
descriptors. For example, the HAL directs the output of printf() to standard out, and perror() to
standard error. You associate each channel to a specific hardware device by manipulating BSP settings.
Note:
This program sends characters to whatever device is associated with stdout when the program is
compiled.
6-6
Using Character-Mode Devices
NII5V2
2015.05.14
Altera Corporation
Developing Programs Using the Hardware Abstraction Layer
Send Feedback