-
Notifications
You must be signed in to change notification settings - Fork 2
/
parallel.c
521 lines (434 loc) · 14.7 KB
/
parallel.c
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/*
Marko Pinteric 2020
General purpose C library for parallel communications on Raspberry Pi
- supports 6800 and 8080 protocols, both 4 bits and 8 bits
- supports arbitrary GPIO pins from 0 to 27
- supports writing and reading, reading is optional
- supports objective oriented programming, initialisation returns the pointer to chip instance
- all RPi data lines by default in read/input mode in order to avoid possible conflict and destruction of GPIO pins
GPIO communication based on Tiny GPIO Access on http://abyz.me.uk/rpi/pigpio/examples.html
for more information see: https://github.com/marko-pi/parallel, http://www.pinteric.com/displays.html
Create shared object with: gcc -o parallel.so -shared -fPIC parallel.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <fcntl.h>
#include <stdbool.h>
#include <time.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>
/* TINY GPIO VARIABLES */
#define GPSET0 7
#define GPSET1 8
#define GPCLR0 10
#define GPCLR1 11
#define GPLEV0 13
#define GPLEV1 14
#define GPPUD 37
#define GPPUDCLK0 38
#define GPPUDCLK1 39
/* GPIO address */
volatile static uint32_t *gpioReg = MAP_FAILED;
#define PI_BANK (gpio>>5)
#define PI_BIT (1<<(gpio&0x1F))
/* gpio modes */
#define PI_INPUT 0
#define PI_OUTPUT 1
#define PI_ALT0 4
#define PI_ALT1 5
#define PI_ALT2 6
#define PI_ALT3 7
#define PI_ALT4 3
#define PI_ALT5 2
/* values for pull-ups/downs off, pull-down and pull-up */
#define PI_PUD_OFF 0
#define PI_PUD_DOWN 1
#define PI_PUD_UP 2
/* INTERNAL VARIABLES */
#define UNDEFINED 0xFFFF
/* 8 data lines, RS/CD EN/WR RW/RD control lines, protocol, 5 wait times */
struct chipdata
{
unsigned d7, d6, d5, d4, d3, d2, d1, d0, rscd, enwr, rwrd, protocol, tsetup, tclock, tread, tproc, thold;
};
union chip
{
struct chipdata data;
unsigned pins[11];
};
union chip *curchip;
/* time of the last execution */
struct timespec ttime;
/* time to wait for the next execution */
uint32_t timing;
/* GPIO status buffer */
uint32_t gpioBuf[3];
/* GPIO clear/set buffers */
uint32_t clr, set;
/* TINY GPIO METHODS */
void gpioSetMode(unsigned gpio, unsigned mode)
{
int reg, shift;
reg = gpio/10;
shift = (gpio%10) * 3;
gpioReg[reg] = (gpioReg[reg] & ~(7<<shift)) | (mode<<shift);
}
int gpioGetMode(unsigned gpio)
{
int reg, shift;
reg = gpio/10;
shift = (gpio%10) * 3;
return (*(gpioReg + reg) >> shift) & 7;
}
void gpioSetPullUpDown(unsigned gpio, unsigned pud)
{
*(gpioReg + GPPUD) = pud;
usleep(20);
*(gpioReg + GPPUDCLK0 + PI_BANK) = PI_BIT;
usleep(20);
*(gpioReg + GPPUD) = 0;
*(gpioReg + GPPUDCLK0 + PI_BANK) = 0;
}
int gpioRead(unsigned gpio)
{
if ((*(gpioReg + GPLEV0 + PI_BANK) & PI_BIT) != 0) return 1;
else return 0;
}
void gpioWrite(unsigned gpio, unsigned level)
{
if (level == 0) *(gpioReg + GPCLR0 + PI_BANK) = PI_BIT;
else *(gpioReg + GPSET0 + PI_BANK) = PI_BIT;
}
int gpioInitialise(void)
{
int fd;
fd = open("/dev/gpiomem", O_RDWR | O_SYNC) ;
if (fd < 0)
{
fprintf(stderr, "Failed to open /dev/gpiomem\n");
return -1;
}
gpioReg = (uint32_t *)mmap(NULL, 0xB4, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
close(fd);
if (gpioReg == MAP_FAILED)
{
fprintf(stderr, "Bad, mmap failed\n");
return -1;
}
return 0;
}
/* INTERNAL METHODS */
#define WAIT() ({\
ntime = ttime.tv_sec * (uint64_t)1000000000L + ttime.tv_nsec + timing;\
while(1)\
{\
clock_gettime(CLOCK_MONOTONIC,&ctime);\
if (ctime.tv_sec * (uint64_t)1000000000L + ctime.tv_nsec >= ntime) break;\
}\
})
/* read multiple data or a register */
/* clr/set must be set to command/data beforehand */
void readparallel(unsigned char *datapos, int datanum)
{
int i, j, k;
unsigned char value;
uint32_t readings;
uint32_t clk; /* bit for the clock line */
int bpc; /* bits per cycle */
struct timespec ctime;
uint64_t ntime;
WAIT();
*(gpioReg + GPCLR0) = clr;
*(gpioReg + GPSET0) = set;
clock_gettime(CLOCK_MONOTONIC,&ttime);
timing = curchip->data.tsetup;
if(curchip->data.protocol == 6800) clk = 1 << curchip->data.enwr;
if(curchip->data.protocol == 8080) clk = 1 << curchip->data.rwrd;
if(curchip->data.d0 == UNDEFINED) bpc=4;
else bpc=8;
for (i=0; i<datanum; i++)
{
value = 0;
for (j=8/bpc; j>0; j--)
{
WAIT();
if(curchip->data.protocol == 6800) *(gpioReg + GPSET0) = clk;
if(curchip->data.protocol == 8080) *(gpioReg + GPCLR0) = clk;
clock_gettime(CLOCK_MONOTONIC,&ttime);
timing = curchip->data.tread;
WAIT();
readings = *(gpioReg + GPLEV0);
/* not refreshing time */
timing = curchip->data.tclock;
for (k=0; k<bpc; k++)
{
value = value << 1;
if ((readings & (1 << curchip->pins[k])) > 0) value = (value | 0x01);
}
WAIT();
if(curchip->data.protocol == 6800) *(gpioReg + GPCLR0) = clk;
if(curchip->data.protocol == 8080) *(gpioReg + GPSET0) = clk;
clock_gettime(CLOCK_MONOTONIC,&ttime);
if (j==1) timing = curchip->data.tproc;
else timing = curchip->data.tclock;
if (curchip->data.thold > timing) timing = curchip->data.thold;
}
datapos[i]=value;
}
}
/* write multiple data or a command */
/* clr/set must be set to command/data beforehand */
void writeparallel(unsigned char *datapos, int datanum)
{
int i, j, k;
uint32_t clk; /* bit for the clock line */
int bpc; /* bits per cycle */
unsigned char datum; /* datum to be sent */
struct timespec ctime;
uint64_t ntime;
*(gpioReg + GPCLR0) = clr;
*(gpioReg + GPSET0) = set;
clock_gettime(CLOCK_MONOTONIC,&ttime);
timing = curchip->data.tsetup;
clk = 1 << curchip->data.enwr;
if(curchip->data.d0 == UNDEFINED) bpc=4;
else bpc=8;
for (i=0; i<datanum; i++)
{
datum = datapos[i];
for (j=8/bpc; j>0; j--)
{
clr = 0;
set = 0;
if(curchip->data.protocol == 6800) set = clk;
if(curchip->data.protocol == 8080) clr = clk;
for (k=0; k<bpc; k++)
{
if ((datum & 0x80) > 0) set = set | (1 << curchip->pins[k]);
else clr = clr | (1 << curchip->pins[k]);
datum = datum << 1;
}
WAIT();
/* make sure that the clock line setting goes last */
if(curchip->data.protocol == 6800)
{
*(gpioReg + GPCLR0) = clr;
*(gpioReg + GPSET0) = set;
}
if(curchip->data.protocol == 8080)
{
*(gpioReg + GPSET0) = set;
*(gpioReg + GPCLR0) = clr;
}
clock_gettime(CLOCK_MONOTONIC,&ttime);
timing = curchip->data.tclock;
WAIT();
if(curchip->data.protocol == 6800) *(gpioReg + GPCLR0) = clk;
if(curchip->data.protocol == 8080) *(gpioReg + GPSET0) = clk;
clock_gettime(CLOCK_MONOTONIC,&ttime);
if (j==1) timing = curchip->data.tproc;
else timing = curchip->data.tclock;
}
}
}
/* EXTERNAL METHODS */
/* gets: 8 data lines, RS/CD EN/WR RW/RD control lines, protocol, 5 wait times */
/* returns: the pointer to chip instance */
/* GPIO number out of range -> undefined line; D3/D2/D1/D0 undefined -> 4 bit communication; RWRD undefined -> write to chip only */
/* initialise communications */
union chip *initialise(int d7, int d6, int d5, int d4, int d3, int d2, int d1, int d0, int rscd, int enwr, int rwrd, int protocol, int tsetup, int tclock, int tread, int tproc, int thold)
{
int i;
int reg, shift;
union chip *tempchip = malloc(sizeof(union chip));
if (gpioInitialise() < 0) return(NULL);
gpioBuf[0] = gpioReg[0];
gpioBuf[1] = gpioReg[1];
gpioBuf[2] = gpioReg[2];
tempchip->data.d7 = (unsigned)d7;
tempchip->data.d6 = (unsigned)d6;
tempchip->data.d5 = (unsigned)d5;
tempchip->data.d4 = (unsigned)d4;
if((d3>27) || (d3<0)) tempchip->data.d3 = UNDEFINED;
else tempchip->data.d3 = (unsigned)d3;
if((d2>27) || (d2<0)) tempchip->data.d2 = UNDEFINED;
else tempchip->data.d2 = (unsigned)d2;
if((d1>27) || (d1<0)) tempchip->data.d1 = UNDEFINED;
else tempchip->data.d1 = (unsigned)d1;
if((d0>27) || (d0<0)) tempchip->data.d0 = UNDEFINED;
else tempchip->data.d0 = (unsigned)d0;
tempchip->data.rscd = (unsigned)rscd;
tempchip->data.enwr = (unsigned)enwr;
if((rwrd>27) || (rwrd<0)) tempchip->data.rwrd = UNDEFINED;
else tempchip->data.rwrd = (unsigned)rwrd;
tempchip->data.protocol = (unsigned)protocol;
tempchip->data.tsetup = (unsigned)tsetup;
tempchip->data.tclock = (unsigned)tclock;
tempchip->data.tread = (unsigned)tread;
tempchip->data.tproc = (unsigned)tproc;
tempchip->data.thold = (unsigned)thold;
/* chip by default in write mode */
if((tempchip->data.protocol == 6800) && (tempchip->data.rwrd != UNDEFINED)) *(gpioReg + GPCLR0) = (1 << tempchip->data.rwrd);
/* RPi control lines default states */
if(tempchip->data.protocol == 6800) *(gpioReg + GPCLR0) = (1 << tempchip->data.enwr);
if((tempchip->data.protocol == 8080) && (tempchip->data.rwrd != UNDEFINED)) *(gpioReg + GPSET0) = (1 << tempchip->data.rwrd);
if(tempchip->data.protocol == 8080) *(gpioReg + GPSET0) = (1 << tempchip->data.enwr);
/* RPi data lines by default in read/input mode */
for (i=0; i<8; i++) if (tempchip->pins[i] != UNDEFINED)
{
reg = tempchip->pins[i]/10;
shift = (tempchip->pins[i]%10) * 3;
gpioBuf[reg] = (gpioBuf[reg] & ~(7<<shift)) | (PI_INPUT<<shift);
}
/* RPi control lines in write/output mode */
for (i=8; i<11; i++) if (tempchip->pins[i] != UNDEFINED)
{
reg = tempchip->pins[i]/10;
shift = (tempchip->pins[i]%10) * 3;
gpioBuf[reg] = (gpioBuf[reg] & ~(7<<shift)) | (PI_OUTPUT<<shift);
}
gpioReg[0] = gpioBuf[0];
gpioReg[1] = gpioBuf[1];
gpioReg[2] = gpioBuf[2];
return(tempchip);
}
/* gets: the pointer to chip instance */
/* deinitialise communications */
int deinitialise(union chip *tempchip)
{
free(tempchip);
return(0);
}
/* gets: the pointer to chip instance, the pointer to data array, the number of data to read */
/* read multiple data */
int readdata(union chip *tempchip, unsigned char *datapos, int datanum)
{
curchip=tempchip;
if (tempchip->data.rwrd == UNDEFINED) return(-1);
clr = 0;
set = 0;
/* chip in data mode */
if(tempchip->data.protocol == 6800) set = set | (1 << curchip->data.rscd);
if(tempchip->data.protocol == 8080) clr = clr | (1 << curchip->data.rscd);
/* chip in read mode */
if(tempchip->data.protocol == 6800) set = set | (1 << curchip->data.rwrd);
readparallel(datapos, datanum);
clr = 0;
set = 0;
/* chip in write mode */
if(tempchip->data.protocol == 6800) clr = clr | (1 << curchip->data.rwrd);
*(gpioReg + GPCLR0) = clr;
*(gpioReg + GPSET0) = set;
return(0);
}
/* gets: the pointer to chip instance */
/* returns: register value */
/* read register */
int readregister(union chip *tempchip)
{
unsigned char datareg;
curchip=tempchip;
if (tempchip->data.rwrd == UNDEFINED) return(-1);
clr = 0;
set = 0;
/* chip in command mode */
if(tempchip->data.protocol == 6800) clr = clr | (1 << curchip->data.rscd);
if(tempchip->data.protocol == 8080) set = set | (1 << curchip->data.rscd);
/* chip in read mode */
if(tempchip->data.protocol == 6800) set = set | (1 << curchip->data.rwrd);
readparallel(&datareg, 1);
clr = 0;
set = 0;
/* chip in write mode */
if(tempchip->data.protocol == 6800) clr = clr | (1 << curchip->data.rwrd);
*(gpioReg + GPCLR0) = clr;
*(gpioReg + GPSET0) = set;
return((int)datareg);
}
/* gets: the pointer to chip instance, the pointer to data array, the number of data to write */
/* write multiple data */
void writedata(union chip *tempchip, unsigned char *datapos, int datanum)
{
int i;
int reg, shift;
int bpc; /* bits per cycle */
struct timespec ctime;
uint64_t ntime;
curchip=tempchip;
if(curchip->data.d0 == UNDEFINED) bpc=4;
else bpc=8;
clr = 0;
set = 0;
if(tempchip->data.protocol == 6800) set = set | (1 << curchip->data.rscd);
if(tempchip->data.protocol == 8080) clr = clr | (1 << curchip->data.rscd);
/* RPi data lines in write/output mode */
gpioBuf[0] = gpioReg[0];
gpioBuf[1] = gpioReg[1];
gpioBuf[2] = gpioReg[2];
for (i=0; i<bpc; i++)
{
reg = curchip->pins[i]/10;
shift = (curchip->pins[i]%10) * 3;
gpioBuf[reg] = (gpioBuf[reg] & ~(7<<shift)) | (PI_OUTPUT<<shift);
}
WAIT();
gpioReg[0] = gpioBuf[0];
gpioReg[1] = gpioBuf[1];
gpioReg[2] = gpioBuf[2];
writeparallel(datapos, datanum);
/* RPi data pins in read/input mode */
for (i=0; i<bpc; i++)
{
reg = curchip->pins[i]/10;
shift = (curchip->pins[i]%10) * 3;
gpioBuf[reg] = (gpioBuf[reg] & ~(7<<shift)) | (PI_INPUT<<shift);
}
gpioReg[0] = gpioBuf[0];
gpioReg[1] = gpioBuf[1];
gpioReg[2] = gpioBuf[2];
}
/* gets: the pointer to chip instance, command value */
/* write command */
void writecommand(union chip *tempchip, unsigned char datacom)
{
int i;
int reg, shift;
int bpc; /* bits per cycle */
struct timespec ctime;
uint64_t ntime;
curchip=tempchip;
if(curchip->data.d0 == UNDEFINED) bpc=4;
else bpc=8;
clr = 0;
set = 0;
if(tempchip->data.protocol == 6800) clr = clr | (1 << curchip->data.rscd);
if(tempchip->data.protocol == 8080) set = set | (1 << curchip->data.rscd);
/* RPi data lines in write/output mode */
gpioBuf[0] = gpioReg[0];
gpioBuf[1] = gpioReg[1];
gpioBuf[2] = gpioReg[2];
for (i=0; i<bpc; i++)
{
reg = curchip->pins[i]/10;
shift = (curchip->pins[i]%10) * 3;
gpioBuf[reg] = (gpioBuf[reg] & ~(7<<shift)) | (PI_OUTPUT<<shift);
}
WAIT();
gpioReg[0] = gpioBuf[0];
gpioReg[1] = gpioBuf[1];
gpioReg[2] = gpioBuf[2];
writeparallel(&datacom, 1);
/* RPi data lines in read/input mode */
for (i=0; i<bpc; i++) if (tempchip->pins[i] != UNDEFINED)
{
reg = curchip->pins[i]/10;
shift = (curchip->pins[i]%10) * 3;
gpioBuf[reg] = (gpioBuf[reg] & ~(7<<shift)) | (PI_INPUT<<shift);
}
gpioReg[0] = gpioBuf[0];
gpioReg[1] = gpioBuf[1];
gpioReg[2] = gpioBuf[2];
}