For an example, here is read program.
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdio.h>
#include "ppci.h"
// This method prints contents of all registers
// in P-PCI-LV.
void dump_reg(struct ppci *ppci, int minor ) {
printf("Output data = %x\n", ppci->data);
printf("LED = %x\n", ppci->led);
printf("Switch Register = %x\n", ppci->sw);
printf("Transfer Address = %x\n", ppci->address);
printf("Transfer Count = %x\n", ppci->length);
printf("Mode = %x\n", ppci->mode);
printf("Status = %x\n", ppci->status);
printf("RetLength = %x\n", ppci->retlen);
printf("FIFOLength = %x\n", ppci->fifolen);
}
// This is a main program.
main(int argc, char **argv) {
int fd;
unsigned int i, j, k;
int minor, direction, control=0;
int length, retlen;
int *w_buffer, *r_buffer;
struct ppci ppci;
int loop, count;
if( argc != 4 ) {
printf("usage : read minor_number loop count\n");
exit(0);
}
minor = atoi(argv[1]);
loop = atoi(argv[2]);
count = atoi(argv[3]);
// The minor number is used as the identification of
// P-PCI-LV.
if( minor == 0 )
fd = open("/dev/ppci0", O_RDWR);
else if( minor == 1 )
fd = open("/dev/ppci1", O_RDWR);
else {
printf("Illegal minor number! 0 or 1\n");
exit(0);
}
// Allocate buffers for read and predefined data.
r_buffer = (int *)malloc(count*sizeof(int));
w_buffer = (int *)malloc(count*sizeof(int));
// Fill predefined data.
// The bit patter in data is good for checking
// crosstalk on a cable.
for(i = 0; i< count; i++) {
if((i%2) == 0 )
w_buffer[i] = 0xaaaaaaaa;
if((i%2) == 1 )
w_buffer[i] = 0x55555555;
r_buffer[i] = 0;
}
// The direction means "read".
// The function should be called first for determining
// the direction of data transfer.
direction = 0;
if( ioctl(fd, PPCIIOC_RESET, direction) == -1 ) {
printf("ioctl PPCIIOC_RESET error...\n");
exit(0);
}
// Read contents of all regsiters in P-PCI-LV and the
// prints it out.
if( ioctl(fd, PPCIIOC_DUMP_REGISTERS, &ppci) == -1 ) {
printf("ioctl PPCIIOC_GET_CONTROL error...\n");
exit(0);
}
dump_reg(&ppci, minor);
length = count*sizeof(int);
for(k = 0; k < loop; k++) {
// Read data. retlen will contains actual length transferred.
// if error occurrs, exit with error message.
// The error may "timeout".
// The value of timeout is 5 sec. as default.
retlen = read(fd, r_buffer, length);
if( retlen < 0 || retlen > length) {
printf("read error occurred...\n");
exit(0);
}
// Compare read data with predefined data(writen data).
// If data error detects, exit with error message.
// Otherwise, continue.
for(i = 0; i < count; i++) {
if( w_buffer[i] != r_buffer[i] ) {
printf("data error : loop = %d, i = %d\n", loop, i);
printf("written data read data\n");
for(j = i; j < 10; j++)
printf("%x %x\n", w_buffer[j], r_buffer[j]);
free(r_buffer);
free(w_buffer);
close(fd);
exit(0);
}
r_buffer[i] = 0;
}
if(!(k % 10000))
printf("loop = %d\n", k);
}
// If all data transfers finish, close the device.
close(fd);
}