several changes..
2 * GraphLCD driver library
4 * port.c - parallel port class with low level routines
6 * This file is released under the GNU General Public License. Refer
7 * to the COPYING file distributed with this package.
9 * (c) 2004 Andreas Regel <andreas.regel AT powarman.de>
20 #include <sys/ioctl.h>
21 #include <linux/ppdev.h>
22 #include <linux/parport.h>
28 static inline int port_in(int port)
31 __asm__ volatile ("inb %1,%0"
33 : "d" ((unsigned short) port));
37 static inline void port_out(unsigned short int port, unsigned char val)
39 __asm__ volatile ("outb %0,%1\n"
41 : "a" (val), "d" (port));
44 cParallelPort::cParallelPort()
51 cParallelPort::~cParallelPort()
55 int cParallelPort::Open(int portIO)
62 if (ioperm(port, 3, 255) == -1)
64 syslog(LOG_ERR, "glcd drivers: ERROR ioperm(0x%X) failed! Err:%s (cParallelPort::Open)\n",
65 port, strerror(errno));
73 syslog(LOG_ERR, "glcd drivers: ERROR iopl failed! Err:%s (cParallelPort::Init)\n",
81 int cParallelPort::Open(const char * device)
85 fd = open(device, O_RDWR);
88 syslog(LOG_ERR, "glcd drivers: ERROR cannot open %s. Err:%s (cParallelPort::Init)\n",
89 device, strerror(errno));
93 if (ioctl(fd, PPCLAIM, NULL) == -1)
95 syslog(LOG_ERR, "glcd drivers: ERROR cannot claim %s. Err:%s (cParallelPort::Init)\n",
96 device, strerror(errno));
101 int mode = PARPORT_MODE_PCSPP;
102 if (ioctl(fd, PPSETMODE, &mode) == -1)
104 syslog(LOG_ERR, "glcd drivers: ERROR cannot setmode %s. Err:%s (cParallelPort::Init)\n",
105 device, strerror(errno));
113 int cParallelPort::Close()
119 ioctl(fd, PPRELEASE);
132 if (ioperm(port, 3, 0) == -1)
148 void cParallelPort::Claim()
154 void cParallelPort::Release()
157 ioctl(fd, PPRELEASE);
160 void cParallelPort::SetDirection(int direction)
164 if (ioctl(fd, PPDATADIR, &direction) == -1)
166 perror("ioctl(PPDATADIR)");
172 if (direction == kForward)
173 port_out(port + 2, port_in(port + 2) & 0xdf);
175 port_out(port + 2, port_in(port + 2) | 0x20);
179 unsigned char cParallelPort::ReadControl()
185 if (ioctl(fd, PPRCONTROL, &value) == -1)
187 perror("ioctl(PPRCONTROL)");
193 value = port_in(port + 2);
199 void cParallelPort::WriteControl(unsigned char value)
203 if (ioctl(fd, PPWCONTROL, &value) == -1)
205 perror("ioctl(PPWCONTROL)");
211 port_out(port + 2, value);
215 unsigned char cParallelPort::ReadStatus()
221 if (ioctl(fd, PPRSTATUS, &value) == -1)
223 perror("ioctl(PPRSTATUS)");
229 value = port_in(port + 1);
235 unsigned char cParallelPort::ReadData()
241 if (ioctl(fd, PPRDATA, &data) == -1)
243 perror("ioctl(PPRDATA)");
249 data = port_in(port);
255 void cParallelPort::WriteData(unsigned char data)
259 if (ioctl(fd, PPWDATA, &data) == -1)
261 perror("ioctl(PPWDATA)");
267 port_out(port, data);
273 cSerialPort::cSerialPort()
278 cSerialPort::~cSerialPort()
282 int cSerialPort::Open(const char * device)
284 struct termios options;
286 fd = open(device, O_RDWR | O_NOCTTY | O_NONBLOCK);
289 printf("error opening port\n");
293 tcgetattr(fd, &options);
295 cfsetispeed(&options, B921600);
296 cfsetospeed(&options, B921600);
298 options.c_cflag &= ~PARENB;
299 options.c_cflag &= ~CSTOPB;
300 options.c_cflag &= ~CSIZE;
301 options.c_cflag |= CS8;
303 options.c_cflag &= ~CRTSCTS;
305 options.c_cflag |= (CLOCAL | CREAD);
307 options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
309 options.c_iflag &= ~(IXON | IXOFF | IXANY);
311 options.c_oflag &= ~OPOST;
313 tcsetattr(fd, TCSANOW, &options);
318 int cSerialPort::Close()
326 int cSerialPort::ReadData(unsigned char * data)
330 return read(fd, data, 1);
333 void cSerialPort::WriteData(unsigned char data)
338 void cSerialPort::WriteData(unsigned char * data, unsigned short length)
342 write(fd, data, length);
346 } // end of namespace