-
Notifications
You must be signed in to change notification settings - Fork 0
/
k8000.c
1286 lines (1090 loc) · 30.5 KB
/
k8000.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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
mod_k8000.c - description
-------------------
begin : Mon Jan 07 2002
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
/* CVS information :
*
* $Id: k8000.c,v 1.5 2005/07/30 10:53:23 js Exp $
*
*/
#ifdef __KERNEL__
#include <linux/kernel.h>
#include <linux/module.h>
MODULE_AUTHOR("Joris Struyve <[email protected]>");
MODULE_DESCRIPTION("libk8000 kernel module");
MODULE_LICENSE("GPL");
#ifdef __MPC860__
#include <asm/commproc.h>
#define SCL 0x00000020
#define SDA 0x00000010
#else
#include <asm/io.h>
#endif
#else
#include <time.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/io.h>
#include <errno.h>
/* Includes for I2C Kernel Device */
#include <unistd.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/i2c-dev.h>
#define MUTEX_LPT0 4567
#define MUTEX_LPT1 4568
#define MUTEX_LPT2 4569
#define MUTEX_LPT3 4570
#endif
#define HIGH 1
#define LOW 0
#include "k8000.h"
#ifndef __KERNEL__
int deviceDescriptor;
char DeviceName[100]="/dev/velleman";
/* mutex stuff */
int mut_id[4];
int *current_mut_id;
int use_mutexlock=0;
#endif
/*********************************************\
* Pin assignment for 5 chains *
***********************************************
* Chain 0 (default chain) *
* pin 17 1-Select in SCL D3 (controlport) *
* pin 13 Select SDA in D4 (statusport) *
* pin 14 1-Auto Feed SDA out D1 (controlport) *
***********************************************
* Chain 1 *
* pin 2 Data0 SCL D0 (dataport) *
* pin 10 1-ACK SDA in D6 (statusport) *
* pin 1 1-Strobe SDA out D0 (controlport) *
***********************************************
* Chain 2 *
* pin 4 Data2 SCL D2 (dataport) *
* pin 11 Busy SDA in D7 (statusport) *
* pin 3 Data1 SDA out D1 (dataport) *
***********************************************
* Chain 3 *
* pin 6 Data4 SCL D4 (dataport) *
* pin 12 Paper Empty SDA in D5 (statusport) *
* pin 5 Data3 SDA out D3 (dataport) *
***********************************************
* Chain 4 *
* pin 8 Data6 SCL D6 (dataport) *
* pin 15 1-Error SDA in D3 (statusport) *
* pin 7 Data5 SDA out D5 (dataport) *
\*********************************************/
typedef struct
{
short *scl_port_addr;
short *sda_in_port_addr;
short *sda_out_port_addr;
byte scl_mask;
byte sda_in_mask;
byte sda_out_mask;
byte scl_reverse;
byte sda_in_reverse;
byte sda_out_reverse;
} chain_config;
short ControlPort;
short StatusPort;
short DataPort;
/* declare and init the chains array */
chain_config chains[5] = { {&ControlPort ,&StatusPort ,&ControlPort,
0x08 /* 0000.1000 */,0x10 /* 0001.0000 */,0x02 /* 0000.0010 */,1,0,1},
{&DataPort ,&StatusPort ,&ControlPort,
0x01 /* 0000.0001 */,0x40 /* 0100.0000 */,0x01 /* 0000.0001 */,0,1,1},
{&DataPort ,&StatusPort , &DataPort,
0x04 /* 0000.0100 */,0x80 /* 1000.0000 */,0x02 /* 0000.0010 */,0,0,0},
{&DataPort ,&StatusPort ,&DataPort,
0x10 /* 0001.0000 */,0x20 /* 0010.0000 */,0x08 /* 0000.1000 */,0,0,0},
{&DataPort ,&StatusPort ,&DataPort,
0x40 /* 0100.0000 */,0x08 /* 0000.1000 */,0x20 /* 0010.0000 */,0,1,0}};
/* active_chain points to a chain_config struct in the chains array */
chain_config *active_chain = chains;
/* IC addresses ( 7 LSB's are meaningfull ) */
byte IOchipcode[] = {0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F};
byte DACchipcode[] = {0x20, 0x21, 0x22, 0x23};
byte ADDAchipcode[] = {0x48, 0x49, 0x4A, 0x4B};
/* configuration and data matrix*/
short IOconfig_matrix[MaxChains][MaxIOchip+1];
short IOdata_matrix [MaxChains][MaxIOchip+1];
short IO_matrix [MaxChains][MaxIOchannel+1];
short DAC_matrix [MaxChains][MaxDACchannel+1];
short AD_matrix [MaxChains][MaxADchannel+1];
short DA_matrix [MaxChains][MaxDAchannel+1];
/* old configuration and data arrays */
/* they just point to the first chain */
short *IOconfig = IOconfig_matrix[0];
short *IOdata = IOdata_matrix[0];
short *IO = IO_matrix[0];
short *DAC = DAC_matrix[0];
short *AD = AD_matrix[0];
short *DA = DA_matrix[0];
/* Global vars */
int debug=0;
int printerror=0;
int I2CbusDelay = 70;
int pre_I2CbusDelay = 35;
int post_I2CbusDelay = 35;
int autoioperm=1;
int activechainnumber=0;
/* Function pointer declarations */
void (*I2cSendData)(byte addr, byte *data, int len)=NULL;
void (*I2cReadData)(byte addr,byte *data,int len)=NULL;
/**************************************************************\
* Prints message if debug is on *
\**************************************************************/
void Debug(char *fmt,...)
{
va_list ap;
char buf[200];
if(debug)
{
va_start(ap,fmt);
vsprintf(buf,fmt,ap);
va_end(ap);
buf[200]=0;
#ifdef __KERNEL__
printk(buf);
#else
printf(buf);fflush(stdout);
#endif
}
}
/**************************************************************\
* Prints error message to stderr or kernel ring buffer *
\**************************************************************/
void PrintError(char *fmt,...)
{
va_list ap;
char buf[200];
if(printerror)
{
va_start(ap,fmt);
vsprintf(buf,fmt,ap);
va_end(ap);
buf[200]=0;
#ifdef __KERNEL__
printk(buf);
#else
fprintf(stderr,"LibK8000 : ");
fprintf(stderr,buf);fflush(stderr);
#endif
}
}
/**************************************************************\
* Delay functions. *
\**************************************************************/
inline void I2cDelay(void)
{
int i,j;
for (i=0;i<I2CbusDelay;i++) {j++;};
}
inline void I2cPreDelay(void)
{
int i,j;
for (i=0;i<pre_I2CbusDelay;i++) {j++;};
}
inline void I2cPostDelay(void)
{
int i,j;
for (i=0;i<post_I2CbusDelay;i++) {j++;};
}
/**************************************************************\
* IC basic functions (platform/interface specific) *
* *
* This is the platform/interface specific part. *
* This is for user space progs over the parallel port. *
* *
\**************************************************************/
#ifdef __MPC860__
inline byte GetScl(void)
{
byte tmp;
volatile cpm8xx_t *cp = cpmp;
tmp = (byte) cp->cp_pbdat & SCL;
return tmp;
}
inline void SetScl(short state)
{
volatile cpm8xx_t *cp = cpmp;
int i=0;
I2cPreDelay();
if(state==HIGH)
{
cp->cp_pbdat |= SCL;
cp->cp_pbdir &= 0xFFFFFFDF;
// wait some time for scl to be released by slave
for(i=0;i<I2CbusDelay && !GetScl();i++);
cp->cp_pbdir |= 0x00000020;
}
else
cp->cp_pbdat &= ~SCL;
I2cPostDelay();
}
inline void SetSda(short state)
{
volatile cpm8xx_t *cp = cpmp;
if(state==HIGH)
cp->cp_pbdat |= SDA;
else
cp->cp_pbdat &= ~SDA;
}
inline byte GetSda(void)
{
byte tmp;
volatile cpm8xx_t *cp = cpmp;
tmp = (byte) cp->cp_pbdat & SDA;
return tmp;
}
inline void SdaOut(void)
{
volatile cpm8xx_t *cp = cpmp;
cp->cp_pbdir |= 0x00000010;
}
inline void SdaIn(void)
{
volatile cpm8xx_t *cp = cpmp;
cp->cp_pbdir &= 0xFFFFFFEF;
}
#else // __MPC860__
inline byte GetScl(void)
{
// SCL is uni-directional for a k8000 connected on the i386 // port
return 1;
}
inline void SetScl(short state)
{
I2cPreDelay();
if ( (active_chain->scl_reverse && state) || (!active_chain->scl_reverse && !state) )
outb(inb(*(active_chain->scl_port_addr)) & (~active_chain->scl_mask),*(active_chain->scl_port_addr) );
else
outb(inb(*(active_chain->scl_port_addr)) | active_chain->scl_mask,*(active_chain->scl_port_addr) );
I2cPostDelay();
}
inline void SetSda(short state)
{
if ( (active_chain->sda_out_reverse && state) || (!active_chain->sda_out_reverse && !state) )
outb(inb(*(active_chain->sda_out_port_addr)) & (~active_chain->sda_out_mask) ,*(active_chain->sda_out_port_addr));
else
outb(inb(*(active_chain->sda_out_port_addr)) | active_chain->sda_out_mask,*(active_chain->sda_out_port_addr));
}
inline byte GetSda(void)
{
if(active_chain->sda_in_reverse)
return (byte) !((inb(*(active_chain->sda_in_port_addr))) & active_chain->sda_in_mask);
else
return (byte) ((inb(*(active_chain->sda_in_port_addr))) & active_chain->sda_in_mask);
}
// No need to set the sda direction on a i386 // port
inline void SdaOut(void) {}
inline void SdaIn(void) {}
#endif // __MPC860__
/**************************************************************\
* IC standard protocol conditions *
\**************************************************************/
inline void StartCond(void)
{
/* Assert : SCL and SDA are high */
SetSda(LOW);
SetScl(LOW);
}
inline void I2CBusNotBusy(void)
{
SetScl(HIGH);
SetSda(HIGH);
}
inline void StopCond(void)
{
SetSda(LOW);
SetScl(HIGH);
SetSda(HIGH);
}
inline byte GetAck(void)
{
int i;
byte val;
SetSda(HIGH);
SdaIn();
SetScl(HIGH);
// wait some time for sda to be pulled low by slave
val = GetSda();
for(i=0;i<I2CbusDelay && val;i++)
val = GetSda();
SetScl(LOW);
SdaOut();
return val;
}
inline void MasterAck(void)
{
SetSda(LOW);
SetScl(HIGH);
SetScl(LOW);
SetSda(HIGH);
}
inline void MasterNack(void)
{
SetSda(HIGH);
SetScl(HIGH);
SetScl(LOW);
}
/**************************************************************\
* Generic IC input/output functions *
\**************************************************************/
byte I2cSendAddressByte(byte addr)
{
int i;
int sb;
/* clock out the slave the address byte */
Debug("I2cSendAddressByte ADDR : ");
for (i=7;i>=0;i--)
{
sb = addr & ( 1 << i );
Debug("%d",sb>0?1:0);
if(sb)
SetSda(HIGH);
else
SetSda(LOW);
SetScl(HIGH);
SetScl(LOW);
}
// return the slave ack state
return GetAck();
}
void I2cSendData_direct(byte addr,byte *data,int len)
{
int i,j;
byte sb;
/* set the write lsb (0) */
addr = (addr<<1);
StartCond();
if( I2cSendAddressByte(addr) )
PrintError("(W) NACK addr 0x%X\n",addr>>1);
/* send the data */
Debug(" (DATA ");
for (j=0;j<len;j++) /* for all data bytes */
{
Debug(":");
for (i=7;i>=0;i--)
{
sb = (*(data +j)) & ( 1 << i );
Debug("%d",sb>0?1:0);
if(sb)
SetSda(HIGH);
else
SetSda(LOW);
SetScl(HIGH);
SetScl(LOW);
}
// read byte ack from slave
if ( GetAck() )
PrintError("(W) NACK byte %d for 0x%X\n",j+1,addr>>1);
}
StopCond();
Debug(")\n");
}
void I2cReadData_direct(byte addr,byte *data,int len)
{
int i,j;
byte sb;
/* set the read lsb (1) */
addr = (addr<<1)|1;
StartCond();
if( I2cSendAddressByte(addr) )
PrintError("(R) NACK addr 0x%X\n",addr>>1);
/* read the data */
Debug(" (DATA ");
for (j=0;j<len;j++) /* for all data bytes */
{
Debug(":");
*(data+j) = 0;
SdaIn();
for (i=7;i>=0;i--)
{
SetScl(HIGH);
sb = GetSda();
SetScl(LOW);
Debug("%d",sb>0?1:0);
*(data+j) |= ((sb>0?1:0)<<i);
}
SdaOut();
// ack byte, except last byte
if(j!=len-1)
MasterAck();
else
MasterNack();
}
Debug(")\n");
StopCond();
}
#ifndef __KERNEL__
void I2cSendData_device(byte addr,byte *data,int len)
{
int result=0;
if(ioctl(deviceDescriptor,I2C_SLAVE, addr))
PrintError("LIBK8000 I2cSendData_device : IOCTL Problem",strerror(errno));
result=write(deviceDescriptor,data,len);
}
void I2cReadData_device(byte addr,byte *data,int len)
{
int result=0;
if(ioctl(deviceDescriptor,I2C_SLAVE, addr))
PrintError("LIBK8000 I2cReadData_device : IOCTL Problem %s",strerror(errno));
result=read(deviceDescriptor,data,len);
}
#endif
/**************************************************************\
* IO input procedures *
\**************************************************************/
void ReadIOchip(int chip_no)
{
short start_channel, channel;
byte data;
I2cReadData(IOchipcode[chip_no],&data,1);
IOdata_matrix[activechainnumber][chip_no] = ~data;
start_channel = chip_no * 8 + 1;
for (channel = 0; channel <= 7; channel++)
IO_matrix[activechainnumber][start_channel+channel] = ((IOdata_matrix[activechainnumber][chip_no] & (0x01 << channel)) != 0);
}
void ReadAllIO(void)
{
int chip_no;
for (chip_no = 0; chip_no <= MaxIOchip; chip_no++)
ReadIOchip(chip_no);
}
void ReadIOchannel(int channel_no)
{
int chip_no;
chip_no = (channel_no - 1) / 8;
ReadIOchip(chip_no);
}
/**************************************************************\
* IO output procedures *
\**************************************************************/
void IOoutput(int chip_no, short data)
{
byte start_channel, channel;
byte conv;
conv = (byte) ((~data) | IOconfig_matrix[activechainnumber][chip_no]);
I2cSendData((byte)IOchipcode[chip_no],&conv,1);
IOdata_matrix[activechainnumber][chip_no] = (IOdata_matrix[activechainnumber][chip_no] & IOconfig_matrix[activechainnumber][chip_no]) | (~conv);
start_channel = chip_no * 8 + 1;
for (channel = 0; channel <= 7; channel++)
IO_matrix[activechainnumber][start_channel+channel] = ((IOdata_matrix[activechainnumber][chip_no] & (0x01 << channel)) != 0);
}
void ClearIOchip(int chip_no)
{
IOoutput(chip_no, 0);
}
void ClearIOchannel(int channel_no)
{
int chip_no, channel;
byte data;
chip_no = (channel_no - 1) / 8;
channel = (channel_no - 1) % 8;
data = IOdata_matrix[activechainnumber][chip_no] & (~(0x01 << channel));
IOoutput(chip_no, data);
}
void SetIOchip(int chip_no)
{
IOoutput(chip_no, 0xFF);
}
void SetIOchannel(int channel_no)
{
int chip_no, channel;
byte data;
chip_no = (channel_no - 1) / 8;
channel = (channel_no - 1) % 8;
data = IOdata_matrix[activechainnumber][chip_no] | (0x01 << channel);
IOoutput(chip_no, data);
}
void SetAllIO(void)
{
int chip_no;
for (chip_no = 0; chip_no <= MaxIOchip; chip_no++)
IOoutput(chip_no, 0xFF);
}
void ClearAllIO(void)
{
int chip_no;
for (chip_no = 0; chip_no <= MaxIOchip; chip_no++)
IOoutput(chip_no, 0);
}
/**************************************************************\
* IO data update procedures *
\**************************************************************/
void UpdateIOdataArray(int chip_no, short data)
{
int start_channel, channel;
IOdata_matrix[activechainnumber][chip_no] = (IOdata_matrix[activechainnumber][chip_no] & IOconfig_matrix[activechainnumber][chip_no]);
IOdata_matrix[activechainnumber][chip_no] = IOdata_matrix[activechainnumber][chip_no] | (data & (~IOconfig_matrix[activechainnumber][chip_no]));
start_channel = chip_no * 8 + 1;
for (channel = 0; channel <= 7; channel++)
IO_matrix[activechainnumber][start_channel+channel] = ((IOdata_matrix[activechainnumber][chip_no] & (0x01 << channel)) != 0);
}
void ClearIOdataArray(int chip_no)
{
int start_channel, channel;
IOdata_matrix[activechainnumber][chip_no] = IOdata_matrix[activechainnumber][chip_no] & IOconfig_matrix[activechainnumber][chip_no];
start_channel = chip_no * 8 + 1;
for (channel = 0; channel <= 7; channel++)
IO_matrix[activechainnumber][start_channel+channel] = ((IOdata_matrix[activechainnumber][chip_no] & (0x01 << channel)) != 0);
}
void SetIOdataArray(int chip_no)
{
int start_channel, channel;
IOdata_matrix[activechainnumber][chip_no] = IOdata_matrix[activechainnumber][chip_no] | ( ~IOconfig_matrix[activechainnumber][chip_no]);
start_channel = chip_no * 8 + 1;
for (channel = 0; channel <= 7; channel++)
IO_matrix[activechainnumber][start_channel+channel] = ((IOdata_matrix[activechainnumber][chip_no] & (0x01 << channel)) != 0);
}
void SetIOchArray(int channel_no)
{
int chip_no, channel;
byte data;
chip_no = (channel_no - 1) / 8;
channel = (channel_no - 1) % 8;
data = IOdata_matrix[activechainnumber][chip_no] | (0x01 << channel);
UpdateIOdataArray(chip_no, data);
}
void ClearIOchArray(int channel_no)
{
int chip_no, channel;
byte data;
chip_no = (channel_no - 1) / 8;
channel = (channel_no - 1) % 8;
data = IOdata_matrix[activechainnumber][chip_no] & (~(0x01 << channel));
UpdateIOdataArray(chip_no, data);
}
void UpdateAllIO(void)
{
int chip_no;
for (chip_no = 0; chip_no <= MaxIOchip; chip_no++)
IOoutput(chip_no, IOdata_matrix[activechainnumber][chip_no]);
}
void UpdateIOchip(int chip_no)
{
IOoutput(chip_no, IOdata_matrix[activechainnumber][chip_no]);
}
/**************************************************************\
* IO config procedures *
\**************************************************************/
void ConfigAllIOasInput(void)
{
int chip_no;
for (chip_no = 0; chip_no <= MaxIOchip; chip_no++)
{
IOconfig_matrix[activechainnumber][chip_no] = 0;
ClearIOchip(chip_no);
IOconfig_matrix[activechainnumber][chip_no] = 0xFF;
ReadIOchip(chip_no);
}
}
void ConfigAllIOasOutput(void)
{
int chip_no;
for (chip_no = 0; chip_no <= MaxIOchip; chip_no++)
IOconfig_matrix[activechainnumber][chip_no] = 0x00;
ClearAllIO();
}
void ConfigIOchipAsInput(int chip_no)
{
IOconfig_matrix[activechainnumber][chip_no] = 0;
ClearIOchip(chip_no);
IOconfig_matrix[activechainnumber][chip_no] = 0xFF;
ReadIOchip(chip_no);
}
void ConfigIOchipAsOutput(int chip_no)
{
IOconfig_matrix[activechainnumber][chip_no] = 0x00;
ClearIOchip(chip_no);
}
void ConfigIOchannelAsInput(int channel_no)
{
int chip_no, channel;
chip_no = (channel_no - 1) / 8;
channel = (channel_no - 1) % 8;
IOconfig_matrix[activechainnumber][chip_no] = IOconfig_matrix[activechainnumber][chip_no] & (~(0x01 << channel));
ClearIOchannel(channel_no);
IOconfig_matrix[activechainnumber][chip_no] = IOconfig_matrix[activechainnumber][chip_no] | (0x01 << channel);
ReadIOchannel(channel_no);
}
void ConfigIOchannelAsOutput(int channel_no)
{
int chip_no, channel;
chip_no = (channel_no - 1) / 8;
channel = (channel_no - 1) % 8;
IOconfig_matrix[activechainnumber][chip_no] = IOconfig_matrix[activechainnumber][chip_no] & (~(0x01 << channel));
ClearIOchannel(channel_no);
}
/**************************************************************\
* DAC output procedures *
\**************************************************************/
void OutputDACchannel(int channel_no, int data)
{
byte total_data[2];
total_data[0] = (byte) (0xF0 | ((channel_no - 1) % 8));
total_data[1] = (byte) (data > 63) ? 63: data;
DAC_matrix[activechainnumber][channel_no] = total_data[1];
I2cSendData(DACchipcode[(channel_no-1)/8],total_data,2);
}
void UpdateDACchip(int chip_no)
{
byte total_data[9];
int i,channel;
total_data[0] = 0x00;
channel = chip_no * 8;
for(i=1;i<=8;i++)
total_data[i] = DAC_matrix[activechainnumber][channel + i] = (DAC_matrix[activechainnumber][channel+i] > 63) ? 63 : DAC_matrix[activechainnumber][channel+i];
I2cSendData(DACchipcode[chip_no],total_data,9);
}
void UpdateDACchannel(int channel_no)
{
OutputDACchannel(channel_no, DAC_matrix[activechainnumber][channel_no]);
}
void ClearDACchannel(int channel_no)
{
OutputDACchannel(channel_no, 0);
}
void SetDACchannel(int channel_no)
{
OutputDACchannel(channel_no,63);
}
void ClearDACchip(int chip_no)
{
int i, channel;
channel = chip_no * 8;
for (i = 1; i <= 8; i++)
DAC_matrix[activechainnumber][channel + i] = 0;
UpdateDACchip(chip_no);
}
void SetDACchip(int chip_no)
{
int i, channel;
channel = chip_no * 8;
for (i = 1; i <= 8; i++)
DAC_matrix[activechainnumber][channel + i] = 63;
UpdateDACchip(chip_no);
}
void UpdateAllDAC(void)
{
int chip_no;
for (chip_no = 0; chip_no <= MaxIOcard; chip_no++)
UpdateDACchip(chip_no);
}
void ClearAllDAC(void)
{
int channel_no;
for (channel_no = 1; channel_no <= MaxDACchannel; channel_no++)
DAC_matrix[activechainnumber][channel_no] = 0x00;
UpdateAllDAC();
}
void SetAllDAC(void)
{
int channel_no;
for (channel_no = 1; channel_no <= MaxDACchannel; channel_no++)
DAC_matrix[activechainnumber][channel_no] = 63;
UpdateAllDAC();
}
/**************************************************************\
* DA output procedures *
\**************************************************************/
void OutputDAchannel(int channel_no, int data)
{
byte total_data[2];
total_data[0] = (0x40);
total_data[1] = (byte) data;
DA_matrix[activechainnumber][channel_no] = (byte) data;
I2cSendData(ADDAchipcode[channel_no-1],total_data,2);
}
void UpdateDAchannel(int channel_no)
{
OutputDAchannel(channel_no, DA_matrix[activechainnumber][channel_no]);
}
void ClearDAchannel(int channel_no)
{
OutputDAchannel(channel_no, 0);
}
void SetDAchannel(int channel_no)
{
OutputDAchannel(channel_no, 255);
}
void UpdateAllDA(void)
{
int channel_no;
for (channel_no = 1; channel_no <= MaxDAchannel; channel_no++)
OutputDAchannel(channel_no, DA_matrix[activechainnumber][channel_no]);
}
void ClearAllDA(void)
{
int channel_no;
for (channel_no = 1; channel_no <= MaxDAchannel; channel_no++)
OutputDAchannel(channel_no, 0);
}
void SetAllDA(void)
{
int channel_no;
for (channel_no = 1; channel_no <= MaxDAchannel; channel_no++ )
OutputDAchannel(channel_no, 255);
}
/**************************************************************\
* AD input procedures *
\**************************************************************/
void ReadADchannel(int channel_no)
{
byte total_data[2];
total_data[0] = (byte) (0x40 | ((channel_no - 1) % 4));
I2cSendData(ADDAchipcode[(channel_no-1)/4],total_data,1);
I2cReadData(ADDAchipcode[(channel_no-1)/4],total_data,2);
AD_matrix[activechainnumber][channel_no] = (short) total_data[1];
}
void ReadADchip(int chip_no)
{
byte total_data[5];
int channel;
total_data[0] = (0x44);
I2cSendData(ADDAchipcode[chip_no],total_data,1);
I2cReadData(ADDAchipcode[chip_no],total_data,5);
channel = chip_no * 4 + 1;
AD_matrix[activechainnumber][channel] = (short) total_data[1];
AD_matrix[activechainnumber][channel + 1] = (short) total_data[2];
AD_matrix[activechainnumber][channel + 2] = (short) total_data[3];
AD_matrix[activechainnumber][channel + 3] = (short) total_data[4];
}
void ReadAllAD(void)
{
int chip_no;
for (chip_no = 0; chip_no <= MaxIOcard; chip_no++)
ReadADchip(chip_no);
}
/**************************************************************\
* Complete card and general procedures *
\**************************************************************/
void ReadAll(void)
{
ReadAllIO();
ReadAllAD();
}
void ReadCard(int card_no)
{
int chip_no;
chip_no = card_no * 2;
ReadIOchip(chip_no);
ReadIOchip(chip_no + 1);
ReadADchip(card_no);
}
void UpdateAll(void)
{
UpdateAllIO();
UpdateAllDAC();
UpdateAllDA();
}
void UpdateCard(int card_no)
{
int chip_no;
chip_no = card_no * 2;
UpdateIOchip(chip_no);
UpdateIOchip(chip_no + 1);
UpdateDACchip(card_no);
UpdateDAchannel(card_no + 1);
}
int SetPortPerm(unsigned long from, unsigned long num, int turn_on)
{
#ifdef __KERNEL__
return 0;
#else
if (ioperm(from,num,turn_on))
{
PrintError("Error setting port permissions '%s'\n",strerror(errno));
return 1;
}
else
return 0;
#endif
}
int SelectI2CprinterPort(int Printer_no)
{
switch (Printer_no)
{
#ifndef __KERNEL__
case I2C_DEV: /* Use I2C Kernel Device ( -1 = I2C_DEV) */
I2cSendData=I2cSendData_device;
I2cReadData=I2cReadData_device;
deviceDescriptor=open(DeviceName, O_RDWR);
if (deviceDescriptor == -1)
{
PrintError("Error opening device '%s'\n",strerror(errno));
return -1;