Visible to Intel only — GUID: iga1401317337443
Ixiasoft
Visible to Intel only — GUID: iga1401317337443
Ixiasoft
11.4.1. HAL System Library Support
The Intel-provided driver implements a HAL character-mode device driver that integrates into the HAL system library for Nios® II and Nios® V processors systems. HAL users should access the UART via the familiar HAL API and the ANSI C standard library, rather than accessing the UART registers. ioctl() requests are defined that allow HAL users to control the hardware-dependent aspects of the UART.
For Nios® II and Nios® V processors users, the HAL system library API provides complete access to the UART core's features. Nios® II and Nios® V processors programs treat the UART core as a character mode device, and send and receive data using the ANSI C standard library functions.
The driver supports the CTS/RTS control signals when they are enabled in Platform Designer. Refer to Driver Options: Fast Versus Small Implementations section.
The following code demonstrates the simplest possible usage, printing a message to stdout using printf(). In this example, the system contains a UART core, and the HAL system library has been configured to use this device for stdout.
Example: Printing Characters to a UART Core as stdout
#include <stdio.h>
int main ()
{
printf("Hello world.\n");
return 0;
}
The following code demonstrates reading characters from and sending messages to a UART device using the C standard library. In this example, the system contains a UART core named uart1 that is not necessarily configured as the stdout device. In this case, the program treats the device like any other node in the HAL file system.
For more information about the HAL system library, refer to the Nios® II Software Developer's Handbook.
Example: Sending and Receiving Characters
/* A simple program that recognizes the characters 't' and 'v' */
#include <stdio.h>
#include <string.h>
int main ()
{
char* msg = "Detected the character 't'.\n";
FILE* fp;
char prompt = 0;
fp = fopen ("/dev/uart1", "r+"); //Open file for reading and writing
if (fp)
{
while (prompt != 'v')
{ // Loop until we receive a 'v'.
prompt = getc(fp); // Get a character from the UART.
if (prompt == 't')
{ // Print a message if character is 't'.
fwrite (msg, strlen (msg), 1, fp);
}
}
fprintf(fp, "Closing the UART file.\n");
fclose (fp);
}
return 0;
}