1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
/*
* GraphLCD driver library
*
* port.c - parallel port class with low level routines
*
* This file is released under the GNU General Public License. Refer
* to the COPYING file distributed with this package.
*
* (c) 2004 Andreas Regel <andreas.regel AT powarman.de>
* (c) 2011-2012 Wolfgang Astleitner <mrwastl AT users.sourceforge.net>
*/
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <termios.h>
#include <pthread.h>
#include <sys/io.h>
#include <sys/ioctl.h>
#include <linux/ppdev.h>
#include <linux/parport.h>
#include "port.h"
#if defined(__linux__) && (defined(__i386__) || defined(__x86_64__))
#define __HAS_DIRECTIO__ 1
#endif
namespace GLCD
{
static pthread_mutex_t claimport_mutex;
static inline int port_in(int port)
{
#ifdef __HAS_DIRECTIO__
unsigned char value;
__asm__ volatile ("inb %1,%0"
: "=a" (value)
: "d" ((unsigned short) port));
return value;
#else
return 0;
#endif
}
static inline void port_out(unsigned short int port, unsigned char val)
{
#ifdef __HAS_DIRECTIO__
__asm__ volatile ("outb %0,%1\n"
:
: "a" (val), "d" (port));
#endif
}
cParallelPort::cParallelPort()
: fd(-1),
port(0),
usePPDev(false),
portClaimed(false)
{
}
cParallelPort::~cParallelPort()
{
}
int cParallelPort::Open(int portIO)
{
#ifdef __HAS_DIRECTIO__
usePPDev = false;
port = portIO;
if (port < 0x400)
{
if (ioperm(port, 3, 255) == -1)
{
syslog(LOG_ERR, "glcd drivers: ERROR ioperm(0x%X) failed! Err:%s (cParallelPort::Open)\n",
port, strerror(errno));
return -1;
}
}
else
{
if (iopl(3) == -1)
{
syslog(LOG_ERR, "glcd drivers: ERROR iopl failed! Err:%s (cParallelPort::Init)\n",
strerror(errno));
return -1;
}
}
return 0;
#else
syslog(LOG_ERR, "glcd drivers: ERROR: direct IO/parport is not available on this architecture / operating system\n");
return -1;
#endif
}
int cParallelPort::Open(const char * device)
{
usePPDev = true;
fd = open(device, O_RDWR);
if (fd == -1)
{
syslog(LOG_ERR, "glcd drivers: ERROR cannot open %s. Err:%s (cParallelPort::Init)\n",
device, strerror(errno));
return -1;
}
if (!Claim())
{
syslog(LOG_ERR, "glcd drivers: ERROR cannot claim %s. Err:%s (cParallelPort::Init)\n",
device, strerror(errno));
close(fd);
return -1;
}
int mode = PARPORT_MODE_PCSPP;
if (ioctl(fd, PPSETMODE, &mode) != 0)
{
syslog(LOG_ERR, "glcd drivers: ERROR cannot setmode %s. Err:%s (cParallelPort::Init)\n",
device, strerror(errno));
close(fd);
return -1;
}
return 0;
}
int cParallelPort::Close()
{
if (usePPDev)
{
if (fd != -1)
{
ioctl(fd, PPRELEASE);
close(fd);
fd = -1;
}
else
{
return -1;
}
}
else
{
#ifdef __HAS_DIRECTIO__
if (port < 0x400)
{
if (ioperm(port, 3, 0) == -1)
{
return -1;
}
}
else
{
if (iopl(0) == -1)
{
return -1;
}
}
#else
return -1; // should never make it until here ...
#endif
}
return 0;
}
bool cParallelPort::Claim()
{
if (!IsPortClaimed())
{
if (usePPDev)
portClaimed = (ioctl(fd, PPCLAIM) == 0);
else
portClaimed = (pthread_mutex_lock(&claimport_mutex) == 0);
}
return IsPortClaimed();
}
void cParallelPort::Release()
{
if (IsPortClaimed())
{
if (usePPDev)
portClaimed = !(ioctl(fd, PPRELEASE) == 0);
else
portClaimed = !(pthread_mutex_unlock(&claimport_mutex) == 0);
}
}
void cParallelPort::SetDirection(int direction)
{
if (usePPDev)
{
if (ioctl(fd, PPDATADIR, &direction) == -1)
{
perror("ioctl(PPDATADIR)");
//exit(1);
}
}
else
{
if (direction == kForward)
port_out(port + 2, port_in(port + 2) & 0xdf);
else
port_out(port + 2, port_in(port + 2) | 0x20);
}
}
unsigned char cParallelPort::ReadControl()
{
unsigned char value;
if (usePPDev)
{
if (ioctl(fd, PPRCONTROL, &value) == -1)
{
perror("ioctl(PPRCONTROL)");
//exit(1);
}
}
else
{
value = port_in(port + 2);
}
return value;
}
void cParallelPort::WriteControl(unsigned char value)
{
if (usePPDev)
{
if (ioctl(fd, PPWCONTROL, &value) == -1)
{
perror("ioctl(PPWCONTROL)");
//exit(1);
}
}
else
{
port_out(port + 2, value);
}
}
unsigned char cParallelPort::ReadStatus()
{
unsigned char value;
if (usePPDev)
{
if (ioctl(fd, PPRSTATUS, &value) == -1)
{
perror("ioctl(PPRSTATUS)");
//exit(1);
}
}
else
{
value = port_in(port + 1);
}
return value;
}
unsigned char cParallelPort::ReadData()
{
unsigned char data;
if (usePPDev)
{
if (ioctl(fd, PPRDATA, &data) == -1)
{
perror("ioctl(PPRDATA)");
//exit(1);
}
}
else
{
data = port_in(port);
}
return data;
}
void cParallelPort::WriteData(unsigned char data)
{
if (usePPDev)
{
if (ioctl(fd, PPWDATA, &data) == -1)
{
perror("ioctl(PPWDATA)");
//exit(1);
}
}
else
{
port_out(port, data);
}
}
cSerialPort::cSerialPort()
: fd(-1)
{
}
cSerialPort::~cSerialPort()
{
}
int cSerialPort::Open(const char * device)
{
struct termios options;
fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
printf("error opening port\n");
return -1;
}
//fcntl(fd, F_SETFL, FNDELAY);
fcntl(fd, F_SETFL, 0);
tcgetattr(fd, &options);
cfsetispeed(&options, B921600);
cfsetospeed(&options, B921600);
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_cflag &= ~CRTSCTS;
options.c_cflag |= (CLOCAL | CREAD);
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_iflag &= ~(IXON | IXOFF | IXANY);
options.c_oflag &= ~OPOST;
tcsetattr(fd, TCSANOW, &options);
return 0;
}
int cSerialPort::Close()
{
if (fd == -1)
return -1;
close(fd);
return 0;
}
int cSerialPort::ReadData(unsigned char * data)
{
if (fd == -1)
return 0;
return read(fd, data, 1);
}
void cSerialPort::WriteData(unsigned char data)
{
WriteData(&data, 1);
}
void cSerialPort::WriteData(unsigned char * data, unsigned short length)
{
if (fd == -1)
return;
write(fd, data, length);
}
} // end of namespace
|