Digital I/O Functions

The low level functions used to manipulate the digital I/O points are the system calls open, lseek, read, write, close, and ioctl. The uses of these functions are demonstrated in two simple sample programs,

xor.c and even_parity.c.

open gives access to the digital I/O character device. It takes two parameters, the node for your device in / dev and the mode you wish to open it in. For the digital I/O device, you will want to use the node /dev/io1 and the mode for read and write O_RDWR. This function will return the handle to your device:

handle = open(“/dev/io1”, O_RDWR)

lseek will choose the current I/O point and takes three parameters including the handle, the offset, and a descriptor for how the offset is used (SEEK_SET, SEEK_CUR, SEEK_END). SEEK_SET will position the file pointer to an absolute position in the array of active pins as specified by the value in offset. SEEK_CUR will move relative to the current position by the number in offset. SEEK_END will move the file pointer to the end of the array of active pins. lseek should be used to position the file pointer prior to any read or write operation.

lseek(handle, pin->offset, SEEK_SET)

read will read the value of the current I/O point. It takes three parameters, the handle, the byte(s) for the read information to be stored to, and the number of bytes to be read. This function will only allow you to read from the active pins. Reads can be done as a single pin or in a group of contiguous pins. The result of the read will be 0xFF (high) or 0x00 (low). If the read operation was not successful, then an error will be returned. The example below reads one byte into the state field of the dimm_io structure.

error = read(handle, &(pin->state), 1)

write sets the value of the current I/O point and it also takes three parameters, the handle, the byte(s) that will be written to the output pins, and the number of bytes to be written. Note that only output pins can be written to, and attempting to write to input pins will generate an error. Similar to reading, write operations can be done for a single I/O point or as a contiguous group of pins. Write values are non-zero for high, and zero for low. The example demonstrates writing one byte from the state field of the dimm_io structure. Prior to using this write function, the state (0 or 1) of the pin was specified in the state field.

error = write(handle, &(pin->state), 1)

close will close the digital I/O device and only takes one parameter, the handle for the device. This should be done once the device is no longer needed.

close(handle)

ioctl will configure an I/O point and it will pass control information to the device driver. Each ioctl call takes three parameters, the handle, a descriptor of the desired command, and a pointer to a dimm_io structure:

error = ioctl(handle, DIMM_IO_IOCTL_SET_CONFIG_BY_PORT_BIT, pin) Each of the command descriptors are listed below. If any of them are unsuccessful executing the function, an error will return.

www.amctechcorp.com

69

Page 69
Image 69
AMC 68VZ328 software manual Digital I/O Functions