Visible to Intel only — GUID: ohi1661905528025
Ixiasoft
Visible to Intel only — GUID: ohi1661905528025
Ixiasoft
57.5.1. HAL System Library Support
The driver supports the CTS/RTS control signals when they are enabled in Platform Designer. Refer to Driver Options: Fast vs. Small Implementations.
The following code demonstrates the simplest possible usage, printing a message to stdout using printf(), sending data, receiving data, using ioctl, using EOP, using EH and set TRBK.
- In the first example, the system contains a Lighweight UART core, and the HAL system library has been configured to use this device for stdout.
- Except the first example, all of the examples use the JTAG UART as stdout for debugging purpose.
Example: Printing Characters to a Lightweight UART Core as stdout
#include <stdio.h> int main () { printf("Hello world.\n"); return 0; }
The following three example codes demonstrate reading characters from and sending messages to another Lightweight UART device using the C standard library. In these three examples, the system contains a Lightweight UART core, 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 Data via File Pointer
#include <stdio.h> #include "system.h" #define ELEMENT_SIZE 1 /* this is a simple program to send data via lw uart.*/ int main() { FILE* fp; int ret_val; /* open file pointer with device name, that can be found in system.h */ fp = fopen (INTEL_LW_UART_0_NAME, "r+"); if(NULL==fp) { printf("Device not found\n"); } else { ret_val = fprintf(fp,"Hello LW UART.\n"); /* using fprintf*/ if (0>ret_val) { printf("write error!\n"); } char msg[] = "Hello again.\n"; int msg_size = sizeof(msg)-1; /* -1 because of null terminator in c string*/ ret_val = fwrite (msg, ELEMENT_SIZE, msg_size, fp); /* using fwrite*/ if (msg_size>ret_val) { printf("write error!\n"); } } ret_val = fclose(fp); if( 0 != ret_val) { printf(“unable to close\n”); } return 0; }
Example: Sending Data via File Descriptor
#include <stdio.h> #include "system.h" #include <fcntl.h> #include <unistd.h> /* this is a simple program to send data via lw uart.*/ int main() { int ret_val; int fd; char msg[] = "Hello LW UART.\n"; int msg_size = sizeof(msg)-1; /* -1 because of null terminator in c string*/ /* open file descriptor with device name, that can be found in system.h */ fd = open(INTEL_LW_UART_0_NAME, O_RDWR); if(0==fd) { printf("Device not found\n"); } else { ret_val = write(fd,msg, msg_size); if (msg_size>ret_val) { printf("write error!\n"); } } ret_val = close(fd); if (0!=ret_val) { printf("unable to close!\n"); } return 0; }
Example: Receiving Data
#include <stdio.h> #include "system.h" #define ELEMENT_SIZE 1 #define READ_SIZE 15 #define BUFF_SIZE 100 /* this is a simple program to read data via lw uart.*/ int main() { FILE* fp; int ret_val; char msg_buf[BUFF_SIZE]; char * buf_ptr; /* open file pointer with device name, that can be found in system.h */ fp = fopen (INTEL_LW_UART_1_NAME, "r+"); if(NULL==fp) { printf("Device not found\n"); } else { buf_ptr = msg_buf; ret_val = fread (buf_ptr, ELEMENT_SIZE, READ_SIZE, fp); if(READ_SIZE>ret_val) { printf("read error!\n"); } else { printf("Read: %*.*s", ret_val,ret_val, msg_buf); } } ret_val = fclose(fp); if (0!=ret_val) { printf("unable to close!\n"); } return 0; }
- This example is only for receiving data. A complete system is required to send data.
- If rx timeout is disabled, you must ensure that there are at least READ_SIZE byte of data available in the driver rx buffer.
Example: Using IOCTL
#include <stdio.h> #include "system.h" #include <fcntl.h> #include "sys/alt_errno.h" #include <sys/ioctl.h> #include <sys/termios.h> #include <unistd.h> /*this is a simple test to test IOCTL*/ int main() { int fd; FILE * fp=NULL; int err_save, ret_val; struct termios term; fd = open(INTEL_LW_UART_0_NAME, O_RDWR); if(0==fd) { printf("Device not found\n"); } else { /* lock access */ ret_val = ioctl (fd, TIOCEXCL, NULL); if(0 == ret_val) { ALT_ERRNO = 0; fp = fopen (INTEL_LW_UART_0_NAME, "r+"); err_save = ALT_ERRNO; if(NULL == fp) { printf("EC %d! File is locked!\n", err_save); } /* unlock access */ ret_val = ioctl (fd, TIOCNXCL, NULL); if(0 == ret_val) { ALT_ERRNO = 0; fp = fopen (INTEL_LW_UART_0_NAME, "r+"); err_save = ALT_ERRNO; if(NULL != fp) { printf("EC %d! File unlocked.\n", err_save); } /* change baudrate */ ret_val = ioctl (fd, TIOCMGET, &term); if(0!=ret_val) { /* should not happened in this example*/ printf("EC=%d, TIOCMGET failed!\n", ret_val); } printf("orignal baudrate %ld\n", term.c_ispeed); term.c_ispeed = 57600; term.c_ospeed = term.c_ispeed; ret_val = ioctl (fd, TIOCMSET, &term); if(0!=ret_val) { /* should not happened in this example*/ printf("EC=%d, TIOCMSET failed!\n", ret_val); } else { ret_val = ioctl (fd, TIOCMGET, &term); if(0!=ret_val) { /* should not happened in this example*/ printf("EC=%d, TIOCMGET failed!\n", ret_val); } printf("changed baudrate %ld\n", term.c_ispeed); } ret_val = fclose(fp); if (0>ret_val) { printf("unable to close!\n"); } } } } ret_val = close(fd); if (0!=ret_val) { printf("unable to close!\n"); } return 0; }
Example: Using EOP
#include <stdio.h> #include "system.h" #include "intel_lw_uart.h" void eop_user_cb_func(void) { printf("eop detected!\n"); } int main() { int fd; FILE* fp; int ret_val; fp = fopen (INTEL_LW_UART_1_NAME, "r+"); if(NULL==fp) { printf("Device not found\n"); } else { fd = fileno(fp); /*fd can be obtained via fp*/ /*configure eop feature.*/ ret_val = intel_lw_uart_init_eop(fd, eop_user_cb_func,'A'); if(0!=ret_val) { printf("eop configuration failed!\n"); } } ret_val = fclose(fp); if (0!=ret_val) { printf("unable to close!\n"); } return 0; }
Example: Using EH
#include <stdio.h> #include <unistd.h> #include <fcntl.h> #include "system.h" #include "intel_lw_uart.h" #include "intel_lw_uart_regs.h" void eh_user_cb_func(void* base, alt_u32 status) { if(status &INTEL_LW_UART_CONTROL_ROE_MSK) { printf("Base 0x%p rx over run error\n", base); } if(status &INTEL_LW_UART_CONTROL_TOE_MSK) { printf("Base 0x%p tx over run error\n", base); } if(status &INTEL_LW_UART_CONTROL_RUE_MSK) { printf("Base 0x%p rx under run error\n", base); } if(status &INTEL_LW_UART_CONTROL_FE_MSK) { printf("Base 0x%p frame error\n", base); } if(status &INTEL_LW_UART_CONTROL_PE_MSK) { printf("Base 0x%p frame error\n", base); } if(status &INTEL_LW_UART_CONTROL_BRK_MSK) { printf("Base 0x%p rx break error\n", base); } } int main() { int fd; int ret_val; fd = open (INTEL_LW_UART_0_NAME, O_RDWR); if(0==fd) { printf("Device not found\n"); } else { /*configure eh feature.*/ ret_val = intel_lw_uart_init_eh(fd, eh_user_cb_func); if(0!=ret_val) { printf("eh configuration failed!\n"); } } /* trigger rx underrun error */ (void)IORD_INTEL_LW_UART_RXDATA(INTEL_LW_UART_0_BASE); ret_val = close(fd); if (0!=ret_val) { printf("unable to close!\n"); } return 0; }
Example: Setting TRBK
#include <stdio.h> #include "system.h" #include "intel_lw_uart.h" #include <fcntl.h> #include <unistd.h> #include "intel_lw_uart_regs.h" int main() { int fd; int ret_val; fd = open(INTEL_LW_UART_0_NAME, O_RDWR); if(0==fd) { printf("Device not found\n"); } else { ret_val=intel_lw_uart_set_trbk(fd, INTEL_LW_UART_TRBK_ENABLE); if(0!=ret_val) { printf("trbk configuration failed!\n"); } usleep(100); ret_val=intel_lw_uart_set_trbk(fd, INTEL_LW_UART_TRBK_DISABLE); if(0!=ret_val) { printf("trbk configuration failed!\n"); } } ret_val = close(fd); if (0!=ret_val) { printf("unable to close!\n"); } return 0; }
Example: Assert RTS for Small Driver
#include <stdio.h> #include "system.h" #include "intel_lw_uart_regs.h" #define ELEMENT_SIZE 1 /*This is a simple program to send data via lw uart.*/ int main() { FILE* fp; int ret_val; alt_u32 control; /*open file pointer with device name, that can be found in system.h*/ fp = fopen(INTEL_LW_UART_0_NAME,"r+"); if(NULL==fp) { printf("Device not found\n"); } else { /* Assert RTS to transmit */ control = IORD_INTEL_LW_UART_CONTROL(INTEL_LW_UART_0_BASE); CONTROL |= INTEL_LW_UART_CONTROL_RTS_MASK; IOWR_INTEL_LW_UART_CONTROL(INTEL_LW_UART_0_BASE, control); ret_val=fprintf(fp,"Hello LW UART.\n"; /* using fprintf*/ if(0!=ret_val) { printf("write error!\n"); } } ret_val=fclose(fp); if(0!=ret_val) { printf("unable to close!\n"); } /* Deassert RTS */ control&=INTEL_LW_UART_CONTROL_RTS_MSK; IOWR_INTEL_LW_UART_CONTROL(INTEL_LW_UART_0_BASE, control); return 0; }