Nios® II Software Developer Handbook

ID 683525
Date 8/28/2023
Public
Document Table of Contents

8.7.1.1. Create a Device Instance

For a device to be made available as a character mode device, it must provide an instance of the alt_dev structure. The alt_dev structure, defined in < Nios® II EDS install path>/components/altera_hal/HAL/inc/sys/alt_dev.h, is essentially a collection of function pointers. These functions are called in response to application accesses to the HAL file system. For example, if you call the function open() with a file name that corresponds to this device, the result is a call to the open() function provided in this structure.

Example 7–1. alt_dev Structure

typedef struct {

 alt_llist llist; /* for internal use */

 const char* name; 

 int (*open) (alt_fd* fd, const char* name, int flags, int mode);

 int (*close) (alt_fd* fd);

 int (*read) (alt_fd* fd, char* ptr, int len);

 int (*write) (alt_fd* fd, const char* ptr, int len); 

 int (*lseek) (alt_fd* fd, int ptr, int dir);

 int (*fstat) (alt_fd* fd, struct stat* buf);

 int (*ioctl) (alt_fd* fd, int req, void* arg);

} alt_dev;

For more information about open(), close(), read(), write(), lseek(), fstat(), and ioctl(), refer to the "HAL API Reference" section.