From 262dba0010e0714bdba91bb6dd0264e0915991ba Mon Sep 17 00:00:00 2001 From: Jan Kratochvil Date: Wed, 26 Jun 2013 13:21:26 +0200 Subject: [PATCH] +parport --- .gitignore | 1 + Makefile | 4 ++-- parport.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 parport.c diff --git a/.gitignore b/.gitignore index 55271f9..15f6995 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ controler device sniff terminal +parport diff --git a/Makefile b/Makefile index 3dcd102..6f50f1c 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CC=gcc -CFLAGS=-g3 -Wall -Werror +CFLAGS=-g3 -Wall -Werror -std=c99 -D_GNU_SOURCE PROGS=controler device sniff terminal -all: $(PROGS) +all: $(PROGS) parport $(PROGS): lptgpib.o clean: rm -f $(PROGS) *.o diff --git a/parport.c b/parport.c new file mode 100644 index 0000000..7167477 --- /dev/null +++ b/parport.c @@ -0,0 +1,75 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +static const int port=0x37a; + +#define LENGTH(x) (sizeof(x)/sizeof(*(x))) + +static volatile struct termios tios; + +static void cleanup(void) { + tcsetattr(0,TCSANOW,(struct termios *)&tios); +} + +int main(void) { + uint8_t out=0x24; + if (ioperm(port,1,1)!=0) { + fprintf(stderr,"ioperm 0x%x: %m\n",port); + exit(EXIT_FAILURE); + } + int i=tcgetattr(0,(struct termios *)&tios); + assert(i==0); + atexit(cleanup); + struct termios tios_raw=tios; + cfmakeraw(&tios_raw); + i=tcsetattr(0,TCSANOW,&tios_raw); + assert(i==0); + for (;;) { + static const struct item { + const char *name; + uint8_t mask; + char key; + } items[]={ + {"DAV=pin1" ,0x01,'1'}, + {"NRFD=pin14",0x02,'2'}, + {"NDAC=pin16",0x04,'4'}, + {"ATN=pin17" ,0x08,'8'}, + {"bidir" ,0x20,'b'}, + }; + outb(out,port); + usleep(1000000/10); + uint8_t in=inb(port); + printf("out=0x%02x in=0x%02x",out,in); + for (const struct item *item=items;itemname,!!(out&item->mask),!!(in&item->mask),item->key); + putchar('\r'); + fflush(stdout); + struct pollfd pollfd; + pollfd.fd=STDIN_FILENO; + pollfd.events=POLLIN; + switch (poll(&pollfd,1,0)) { + case 1: { + char c; + ssize_t got=read(STDIN_FILENO,&c,1); + assert(got==1); + if (c==27||c==3||c=='\r') { + putchar('\n'); + exit(EXIT_FAILURE); + } + for (const struct item *item=items;itemkey) + out^=item->mask; + } break; + case 0: + break; + case -1: + abort(); + } + } +} -- 1.8.3.1