-
Notifications
You must be signed in to change notification settings - Fork 0
/
GPIB.cpp
1187 lines (774 loc) · 29.4 KB
/
GPIB.cpp
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
#include "gpib.h"
#include "Arduino.h"
//*******************************************************************************
//*
//* Test for Instrument on the GPIB Bus
//*
//* returns TRUE if a device is on the bus, FALSE if nothing is there
//*
//*******************************************************************************
int GPIBCheck(void)
{
uint8_t DDR_set, PORT_set, retval;
//when devices are not addressed, they let the bus float high
//assert ATN and that places devices into listen mode
//in listen mode a device will pull NDAC low
//
DDR_set = NDAC_DDR & NDAC_BIT; //record if the DDR bit is set so it can be restored
PORT_set = NDAC_PORT & NDAC_BIT; //record if the PORT bit is set so it can be restored
NDAC_DDR &= ~NDAC_BIT; //make NDAC input
//NDAC_PORT &= ~NDAC_BIT; //make tristate TODO: MAYBE make it weakly pulled up?
NDAC_PORT |= NDAC_BIT; //enable weak pull-up
GPIBInterfaceClear(); //reset the interface if it is in the middle of something
GPIBAssert(&ATN_PORT, &ATN_DDR, ATN_BIT); //assert ATN
delay(1); //delay a moment just to let things get processed: not absolutely required
if (NDAC_PIN & NDAC_BIT) retval = 0; //if the pin is high (not asserted then no device is on the bus: return false
else retval = 1; //if the pin is low, then a device is on the bus: return true
if (DDR_set) NDAC_DDR |= NDAC_BIT;
else NDAC_DDR &= ~NDAC_BIT;
if (PORT_set) NDAC_PORT |= NDAC_BIT;
else NDAC_PORT &= ~NDAC_BIT;
GPIBInterfaceClear(); //reset the interface
return retval;
}
//******************************************************************************
//******************************************************************************
void GPIBInit(void)
{
//lets try setting it up to be a listener by default
//NDAC and NRFD need to be actively pulled
GPIBAssert(&NDAC_PORT,&NDAC_DDR, NDAC_BIT); // Active data not accepted: pulled low
GPIBRelease(&NRFD_PORT, &NRFD_DDR, NRFD_BIT); // Active ready for data: pulled high
GPIBClear(&DAV_PORT, &DAV_DDR, DAV_BIT); //passively let the bus float, maybe with weak pull-ups
GPIBClear(&EOI_PORT, &EOI_DDR, EOI_BIT);
GPIBClear(&ATN_PORT, &ATN_DDR, ATN_BIT);
GPIBClear(&SRQ_PORT, &SRQ_DDR, SRQ_BIT); //weak pull up on the SRQ, so other devices can signal
GPIBRelease(&IFC_PORT, &IFC_DDR, IFC_BIT); //actively pull the pins because only controller uses these
GPIBRelease(&REN_PORT, &REN_DDR, REN_BIT);
GPIBClearData(); //set data bits to input-tristate
}
//******************************************************************************
// Initialize the GIB bus for "SNIFFING"
// be as passive as possible
// "clear" all pins except DAV which is input-pullup (weakly pulled up)
//******************************************************************************
void GPIBInitSniff(void)
{
//weak pull-up on signals that are monitored for changes
//or else they will have noise in the absence of an active bus
GPIBClear(&DAV_PORT, &DAV_DDR, DAV_BIT); //input-pullup
//tristate the pins which are unused or sure to be driven by the bus when data is available
//consider using GPIBClear() to weakly pull up the pins
GPIBFloat(&NDAC_PORT,&NDAC_DDR, NDAC_BIT);
GPIBFloat(&NRFD_PORT, &NRFD_DDR, NRFD_BIT);
GPIBFloat(&EOI_PORT, &EOI_DDR, EOI_BIT);
GPIBFloat(&ATN_PORT, &ATN_DDR, ATN_BIT);
GPIBFloat(&SRQ_PORT, &SRQ_DDR, SRQ_BIT);
GPIBFloat(&IFC_PORT, &IFC_DDR, IFC_BIT);
GPIBFloat(&REN_PORT, &REN_DDR, REN_BIT);
GPIBClearData(); //set data bits to input-tristate because they are read only if there is bus activity
}
//********************************************************************************************************
//*
//* High level function to write a block of data
//* Receives a pointer to a block of bytes, the length of the block, and whether to use EOI signalling
//* Returns the number of bytes sent or zero if there is an error
//*
//********************************************************************************************************
uint16_t GPIBWriteData(char* data, uint16_t count, bool useEOI)
{
//writes an entire array of data to the GPIB bus
uint16_t counter;
uint8_t error = 0;
bool transEOI = false;
uint16_t last = count-1;
uint16_t retval;
//Serial.print(F(" Write Data "));
for (counter = 0; counter < count; counter++)
{
if ((counter == last) && useEOI) transEOI= true;
error = GPIBWriteByte(data[counter], transEOI);
}
if (error) retval = 0;
else retval = count;
//Serial.print(F(" EXIT Write Data "));
return retval;
}
//***********************************************
//*
//* Send a null terminated string to the bus
//*
//***********************************************
uint16_t GPIBWriteString (char* data, bool useEOI)
{
uint16_t counter;
uint8_t error = 0;
bool transEOI = false;
uint16_t retval;
while(!transEOI)
{
if (data[counter+1] == 0) transEOI = true; //if the next character is the null terminator, then this is the last
error = GPIBWriteByte(data[counter], transEOI);
counter++;
}
if (error) retval = 0;
else retval = counter;
return retval;
}
//*********************************************************************
//*
//* A wrapper of writedata to/ assert/release ATN to form a command
//*
//*********************************************************************
void GPIBWriteCmd(char* data, uint16_t _length, bool useEOI)
{
//Serial.print(F(" Write Cmd "));
GPIBAssert(&ATN_PORT, &ATN_DDR, ATN_BIT);
GPIBWriteData(data, _length, useEOI);
GPIBRelease(&ATN_PORT, &ATN_DDR, ATN_BIT);
}
//***********************************************
// Release a pin: Output-High
// GPIB uses negative logic: High = not asserted
//***********************************************
void GPIBRelease(volatile uint8_t *gpibPORT, volatile uint8_t *gpibDDR, uint8_t gpibBIT)
{
//make the line OUTPUT-HIGH
//set DDR to output (Set the bit)
*gpibDDR |= gpibBIT;
//Set the PORT to HIGH (set the bit)
*gpibPORT |= gpibBIT;
}
//***********************************************
// Assert a pin: Output-LOW
// GPIB uses negative logic: low = asserted
//***********************************************
void GPIBAssert(volatile uint8_t *gpibPORT, volatile uint8_t *gpibDDR, uint8_t gpibBIT)
{
//make the line OUTPUT-LOW
//Set DDR to OUTPUT (Set the bit)
*gpibDDR |= gpibBIT;
//Set PORT to LOW (clear the bit)
*gpibPORT &= ~gpibBIT;
//PORTA &= ~gpibBIT;
/*if (*gpibPORT == NDAC_PORT)
{
Serial.print (F("Asserting NDAC "));
Serial.print((uint8_t)gpibPORT);
Serial.print(" ");
Serial.print((uint8_t)gpibDDR);
Serial.print(" ");
Serial.println(gpibBIT,BIN);
}*/
}
//***********************************************
// Clear a pin i.e. "De-assert", "input-weak pullup".
// Use pullup to prevent noise
//***********************************************
void GPIBClear(volatile uint8_t *gpibPORT, volatile uint8_t *gpibDDR, uint8_t gpibBIT)
{
//make the line INPUT-PULLUP
//Set DDR to INPUT (Clear the bit)
*gpibDDR &= ~gpibBIT;
//Set PORT to HIGH (set the bit)
*gpibPORT |= gpibBIT;
}
//***********************************************
// Float a pin: "Input-High"
//***********************************************
void GPIBFloat(volatile uint8_t *gpibPORT, volatile uint8_t *gpibDDR, uint8_t gpibBIT)
{
//make the line INPUT-TRISTATE
//Set DDR to INPUT (Clear the bit)
*gpibDDR &= ~gpibBIT;
//Set PORT to TRISTATE (set the bit)
*gpibPORT &= ~gpibBIT;
}
//***********************************************
//***********************************************
void GPIBClearData(void)
{
//Do it the hard slow way but universally acceptable: set individual pins.
//do it in this order: When turning a pin OUTPUT-HIGH/LOW, first set the value, then set the direction
// When turning a pin INPUT-HIGH/LOW, first set the direction, then set the value
//Clear DDR AND PORT for each data pin (input-tristate)
DATA0_DDR &= ~DATA0_BIT;
DATA0_PORT &= ~DATA0_BIT;
DATA1_DDR &= ~DATA1_BIT;
DATA1_PORT &= ~DATA1_BIT;
DATA2_DDR &= ~DATA2_BIT;
DATA2_PORT &= ~DATA2_BIT;
DATA3_DDR &= ~DATA3_BIT;
DATA3_PORT &= ~DATA3_BIT;
DATA4_DDR &= ~DATA4_BIT;
DATA4_PORT &= ~DATA4_BIT;
DATA5_DDR &= ~DATA5_BIT;
DATA5_PORT &= ~DATA5_BIT;
DATA6_DDR &= ~DATA6_BIT;
DATA6_PORT &= ~DATA6_BIT;
DATA7_DDR &= ~DATA7_BIT;
DATA7_PORT &= ~DATA7_BIT;
}
//***********************************************
//***********************************************
void GPIBInitListen(void)
{
GPIBAssert(&NDAC_PORT, &NDAC_DDR, NDAC_BIT); // Active data not accepted
GPIBRelease(&NRFD_PORT, &NRFD_DDR, NRFD_BIT); // active ready for data
GPIBClearData(); //set data bits to input-tristate
GPIBClear(&DAV_PORT, &DAV_DDR, DAV_BIT); // listener does not control
GPIBClear(&EOI_PORT, &EOI_DDR, EOI_BIT); // listener does not control
GPIBClear(&ATN_PORT, &ATN_DDR, ATN_BIT); // listener does not control
}
//***********************************************
//***********************************************
void GPIBInitTalk()
{
GPIBRelease(&DAV_PORT, &DAV_DDR, DAV_BIT); // active pull up
GPIBRelease(&EOI_PORT, &EOI_DDR, EOI_BIT); // active pull up
GPIBRelease(&ATN_PORT, &ATN_DDR, ATN_BIT); // active pull up
GPIBClearData(); //set data bits to input-tristate
GPIBClear(&NDAC_PORT, &NDAC_DDR, NDAC_BIT); // passive
GPIBClear(&NRFD_PORT, &NRFD_DDR, NRFD_BIT); // Passive
}
//***********************************************
//***********************************************
void GPIBInterfaceClear(void)
{
//need to hold IFC asserted for at least 150uS - do it for 300us just to be sure
GPIBAssert(&IFC_PORT, &IFC_DDR, IFC_BIT);
delayMicroseconds(300);
GPIBRelease(&IFC_PORT, &IFC_DDR, IFC_BIT);
}
//***********************************************
//***********************************************
void GPIBRemoteEnable(bool state)
{
if(state) GPIBAssert(&REN_PORT, &REN_DDR, REN_BIT);
else GPIBRelease(&REN_PORT, &REN_DDR, REN_BIT);
}
//***********************************************
//***********************************************
uint8_t WaitForDAV(uint8_t state, uint16_t timeLimit)
{
//wait for DAV to change to a specificed state with a timeout
uint16_t timeOut = 0;
uint8_t retval = 0;
uint8_t reading = 0;
if (DAV_PIN & DAV_BIT) reading = 1;
while ((reading != state) && (timeOut < timeLimit))
{
delay(1);
if (DAV_PIN & DAV_BIT) reading = 1;
else reading = 0;
timeOut += 1;
}
if (timeOut == timeLimit) retval = 1; //ONE indicates timeout error
return retval;
}
//***********************************************
//***********************************************
uint8_t WaitForNRFD(uint8_t state, uint16_t timeLimit)
{
//wait for NRFD to change to a specificed state with a timeout
uint16_t timeOut = 0;
uint8_t retval = 0;
uint8_t reading = 0;
if (NRFD_PIN & NRFD_BIT) reading = 1;
while ((reading != state) && (timeOut < timeLimit))
{
delay(1);
if (NRFD_PIN & NRFD_BIT) reading = 1;
else reading = 0;
timeOut += 1;
}
if (timeOut == timeLimit) retval = 1; //ONE indicates timeout error
return retval;
}
//***********************************************
//***********************************************
uint8_t WaitForNDAC(uint8_t state, uint16_t timeLimit)
{
//wait for NDAC to change to a specificed state with a timeout
uint16_t timeOut = 0;
uint8_t retval = 0;
uint8_t reading = 0;
if (NDAC_PIN & NDAC_BIT) reading = 1;
while ((reading != state) && (timeOut < timeLimit))
{
delay(1);
if (NDAC_PIN & NDAC_BIT) reading = 1;
else reading = 0;
timeOut += 1;
}
if (timeOut == timeLimit) retval = 1; //ONE indicates timeout error
return retval;
}
//***********************************************
//***********************************************
uint16_t GPIBGetData(char* buffer, uint16_t len, uint16_t timeOut)
{
//Listener function
//reads data off the bus and transmits it immediately by serial
//returns an indicator of success (0) or error (1)
/*
Sequence of events:
Listener: Assert NDAC, Release NRFD (ready for data)
Talker: Asserts DAV after placing new data
Listener: Asserts NRFD (not ready for new data: tells talker to hold the current data)
Listener: Read data
Listener: Release NDAC (data is accepted)
Talker: Release DAV
Listener: Assert NDAC, Release NRFD
*/
uint16_t retval = 0;
bool last = false;
//GPIBInitListen();
//Should ALREADY be in "listen mode so the bus is correct for listening but set the handshaking anyway
GPIBAssert(&NDAC_PORT, &NDAC_DDR, NDAC_BIT); //indicate data is no longer accepted
GPIBRelease(&NRFD_PORT, &NRFD_DDR, NRFD_BIT); //indicate ready for new data
while (last == false)
{
if (WaitForDAV(_LOW, timeOut)) //wait for Data to be AVailable (LOW), with a timeout
{
retval = 0;
//Serial.println(F("DAV never asserted!"));
break;
}
GPIBAssert(&NRFD_PORT, &NRFD_DDR, NRFD_BIT); //indicate Not Ready For new Data
if ((EOI_PIN & EOI_BIT) == 0) last = true; //if the bit is low (asserted) by the client, then this is the last byte of the transmission
//read the data
buffer[retval] = GPIBReadByte();
//Serial.write(buffer[retval]); //debug print the data
retval++;
if(retval > len) break;
GPIBRelease(&NDAC_PORT, &NDAC_DDR, NDAC_BIT); //release NDAC to indicate that data is accepted
//try releasing NRFD and say we are ready for new data
GPIBRelease(&NRFD_PORT, &NRFD_DDR, NRFD_BIT); //indicate ready for new data
if (WaitForDAV(_HIGH, timeOut)) //wait for data availability to be de-asserted (high) by the client, with a timeout
{
retval = 0;
//Serial.println(F("DAV never released!"));
//Serial.print(F("DAV: "));
// if (DAV_PIN & DAV_BIT) Serial.print(F("Signal is RELEASED (high) "));
// else Serial.print(F("Signal is ASSERTED (low), "));
// if (DAV_PORT & DAV_BIT) Serial.print(F("PORT is RELEASED (high) "));
// else Serial.print(F("PORT is ASSERTED (low), "));
// if(DAV_DDR & DAV_BIT) Serial.println(F("DDR: OUTPUT "));
// else Serial.println(F("DDR: INPUT "));
break;
}
GPIBAssert(&NDAC_PORT, &NDAC_DDR, NDAC_BIT); //indicate data is no longer accepted
GPIBRelease(&NRFD_PORT, &NRFD_DDR, NRFD_BIT); //indicate ready for new data
} //end while
//Serial.print(_CR);
//Serial.print(_LF);
//GPIBAssert(NDAC_PORT, NDAC_DDR, NDAC_BIT);
//GPIBAssert(NRFD_PORT, NRFD_DDR, NRFD_BIT);
GPIBInitListen();
//finish with both NDAC and NRFD asserted to show
//a listener is on the bus, but is not ready for data
return retval;
}
//***********************************************
// GPIB Monitor
// Continuously listens and prints a byte
// NO TIMEOUT for starting to receive data
// Assumes ARDUINO platform in printing
//
//***********************************************
void GPIBMonitor(void) {
//GPIB BUS MONITOR FUNCTION
//bool last = false;
uint8_t data;
//Should ALREADY be in "listen" mode
//wait infinitely for DAV to be asserted
while (WaitForDAV(_LOW, 10000)) { //wait for Data to be AVailable (LOW), with a timeout, Return of 1 means timed out
Serial.println(F("waiting..."));
}
GPIBAssert(&NRFD_PORT, &NRFD_DDR, NRFD_BIT); //indicate Not Ready For new Data
//read the data
Serial.write(GPIBReadByte()); //print the data
if((EOI_PIN & EOI_BIT) == 0) Serial.println(); //if the bit is low (asserted), then this is the last byte of the message: send a new line
GPIBRelease(&NDAC_PORT, &NDAC_DDR, NDAC_BIT); //release NDAC to indicate that data is accepted
GPIBRelease(&NRFD_PORT, &NRFD_DDR, NRFD_BIT); //indicate ready for new data
if (WaitForDAV(_HIGH, 10000)) //wait for data availability to be de-asserted (high) by the client, with a timeout
{
Serial.println(F("DAV never released!"));
Serial.print(F("DAV: "));
if (DAV_PIN & DAV_BIT) Serial.print(F("DAV Signal is RELEASED (high) "));
else Serial.print(F("DAV Signal is ASSERTED (low), "));
if (DAV_PORT & DAV_BIT) Serial.print(F("DAV PORT is RELEASED (high) "));
else Serial.print(F("DAV PORT is ASSERTED (low), "));
if(DAV_DDR & DAV_BIT) Serial.println(F("DAV DDR: OUTPUT "));
else Serial.println(F("DAV DDR: INPUT "));
}
GPIBAssert(&NDAC_PORT, &NDAC_DDR, NDAC_BIT); //indicate data is no longer accepted
GPIBRelease(&NRFD_PORT, &NRFD_DDR, NRFD_BIT); //indicate ready for new data
}
//***********************************************
//***********************************************
uint8_t GPIBReadByte(void)
{
uint8_t a =0;
//do it the hard slow but universally functional way.
//low is a "1" and high is a "0" bit
//Turn on set bits (set low on the bus)
if (!(DATA7_PIN & DATA7_BIT)) a |= 0x80;
if (!(DATA6_PIN & DATA6_BIT)) a |= 0x40;
if (!(DATA5_PIN & DATA5_BIT)) a |= 0x20;
if (!(DATA4_PIN & DATA4_BIT)) a |= 0x10;
if (!(DATA3_PIN & DATA3_BIT)) a |= 0x08;
if (!(DATA2_PIN & DATA2_BIT)) a |= 0x04;
if (!(DATA1_PIN & DATA1_BIT)) a |= 0x02;
if (!(DATA0_PIN & DATA0_BIT)) a |= 0x01;
return a;
}
//***********************************************
//*
//* Check to see if data is available on the bus
//* Revieves: VOID
//* Returns: uint_8 zero if none ready, 1 if the bus has data
//*
//***********************************************
uint8_t GPIBDataReady(void)
{
//Returns TRUE if it is asserted(LOW)
//Returns FALSE if it is not asserted (HIGH)
if (DAV_PIN & DAV_BIT) return 0;
else return 1;
}
//***********************************************
//*
//* Set the data pins on the GPIB bus
//*
//***********************************************
void _WriteByte(uint8_t data)
{
// _SHOULD_ not need to change the PORT values as we only need to drive pins low or tristate them
// in both cases PORT values are zero.
//WRITE DATA
//Call SerialSendStr("send >")
//Call SerialSendByte(data)
//Call SerialSendStr("<")
//Call SerialSendbyte(10)
//set the pins individually bit by bit
// check bits in data byte. If a bit is set, then drive the data pin low otherwise, drive it high
DATA7_DDR |= DATA7_BIT;
if (data & 0x80) DATA7_PORT &= ~DATA7_BIT; //clear the pin if the bit is set
else DATA7_PORT |= DATA7_BIT; //set the pin if the bit is clear
DATA6_DDR |= DATA6_BIT;
if (data & 0x40) DATA6_PORT &= ~DATA6_BIT; //clear the pin if the bit is set
else DATA6_PORT |= DATA6_BIT; //set the pin if the bit is clear
DATA5_DDR |= DATA5_BIT;
if (data & 0x20) DATA5_PORT &= ~DATA5_BIT; //clear the pin if the bit is set
else DATA5_PORT |= DATA5_BIT; //set the pin if the bit is clear
DATA4_DDR |= DATA4_BIT;
if (data & 0x10) DATA4_PORT &= ~DATA4_BIT; //clear the pin if the bit is set
else DATA4_PORT |= DATA4_BIT; //set the pin if the bit is clear
DATA3_DDR |= DATA3_BIT;
if (data & 0x08) DATA3_PORT &= ~DATA3_BIT; //clear the pin if the bit is set
else DATA3_PORT |= DATA3_BIT; //set the pin if the bit is clear
DATA2_DDR |= DATA2_BIT;
if (data & 0x04) DATA2_PORT &= ~DATA2_BIT; //clear the pin if the bit is set
else DATA2_PORT |= DATA2_BIT; //set the pin if the bit is clear
DATA1_DDR |= DATA1_BIT;
if (data & 0x02) DATA1_PORT &= ~DATA1_BIT; //clear the pin if the bit is set
else DATA1_PORT |= DATA1_BIT; //set the pin if the bit is clear
DATA0_DDR |= DATA0_BIT;
if (data & 0x01) DATA0_PORT &= ~DATA0_BIT; //clear the pin if the bit is set
else DATA0_PORT |= DATA0_BIT; //set the pin if the bit is clear
}
//******************************************************************************
//******************************************************************************
//* THIS IS THE LOW-LEVEL FUNCTION TO COMMUNICATE OVER THE GPIB BUS
//* IT WILL RETURN AN ERROR IF THRE IS A TIMEOUT.
//* TIMEOUT IS PERFORMED BY THE WAITFOR() FUNCTION WHICH RETURNS TRUE
//* IF THERE WAS A TIMEOUT ERROR
//******************************************************************************
//******************************************************************************
uint8_t GPIBWriteByte(uint8_t datum, bool useEOI)
{
uint8_t retval =0;
//place a byte onto the GPIB Bus with proper handshaking
//returns FALSE if no error, TRUE if there is an error
//debug.print "writebyte: >"+chr(datum)+"< = "+ Cstr(datum)
//debug.print "writebyte >"+chr(datum)+"< = "+ Cstr(datum)
//Serial.print (F(" Write Byte "));
//Call SerialSendStr("NDAC? ")
retval = WaitForNDAC(_ASSERTED, 10000);
if (retval)
{
//Serial.print(F(" TIMEOUT NDAC ASSERT "));
goto quit; //break;
}
//Call SerialSendStr("NDAC OK, ")
_WriteByte(datum);
//Call SerialSendStr("NRFD? ")
retval = WaitForNRFD(_RELEASED, 10000);
if (retval)
{
//Serial.print(F(" TIMEOUT NRFD RELEASE "));
goto quit; //break;
}
//Call SerialSendStr("NRFD OK, Set DAV, ")
if(useEOI)
{
//Call SerialSendStr("Set EOI, ")
GPIBAssert(&EOI_PORT, &EOI_DDR, EOI_BIT);
}
GPIBAssert(&DAV_PORT, &DAV_DDR, DAV_BIT);
//Call SerialSendStr("NDAC? ")
retval = WaitForNDAC(_RELEASED, 10000);
if (retval)
{
//Serial.print(F(" TIMEOUT NDAC RELEASE "));
goto quit; //break;
}
quit:
//Call SerialSendStr("NDAC OK, BYTE DONE" +Chr(10)+chr(13))
GPIBRelease(&DAV_PORT, &DAV_DDR, DAV_BIT);
if(useEOI) GPIBRelease(&EOI_PORT, &EOI_DDR, EOI_BIT); //maybe simple ALWAYS release EOI?
return retval;
}
void GPIBSendState(void){
uint8_t x = 0;
uint8_t cnt = 0 ;
Serial.println();
Serial.print(F("DAV: "));
if (DAV_PIN & DAV_BIT) Serial.print(F("RELEASED "));
else Serial.print(F("ASSERTED, "));
if(DAV_DDR & DAV_BIT) Serial.println(F("OUTPUT"));
else Serial.println(F("INPUT"));
Serial.print(F("NRFD: "));
if (NRFD_PIN & NRFD_BIT) Serial.print(F("RELEASED "));
else Serial.print(F("ASSERTED, "));
if(NRFD_DDR & NRFD_BIT) Serial.println(F("OUTPUT"));
else Serial.println(F("INPUT"));
Serial.print(F("NDAC: "));
if (NDAC_PIN & NDAC_BIT) Serial.print(F("RELEASED "));
else Serial.print(F("ASSERTED, "));
if(NDAC_DDR & NDAC_BIT) Serial.println(F("OUTPUT"));
else Serial.println(F("INPUT"));
Serial.print(F("EOI: "));
if (EOI_PIN & EOI_BIT) Serial.print(F("RELEASED "));
else Serial.print(F("ASSERTED, "));
if(EOI_DDR & EOI_BIT) Serial.println(F("OUTPUT"));
else Serial.println(F("INPUT"));
Serial.print(F("DATA0: "));
if (DATA0_PIN & DATA0_BIT) Serial.print(F("RELEASED "));
else Serial.print(F("ASSERTED, "));
if(DATA0_DDR & DATA0_BIT) Serial.println(F("OUTPUT"));
else Serial.println(F("INPUT"));
Serial.print(F("DATA1: "));
if (DATA1_PIN & DATA1_BIT) Serial.print(F("RELEASED "));
else Serial.print(F("ASSERTED, "));
if(DATA1_DDR & DATA1_BIT) Serial.println(F("OUTPUT"));
else Serial.println(F("INPUT"));
Serial.print(F("DATA2: "));
if (DATA2_PIN & DATA2_BIT) Serial.print(F("RELEASED "));
else Serial.print(F("ASSERTED, "));
if(DATA2_DDR & DATA2_BIT) Serial.println(F("OUTPUT"));
else Serial.println(F("INPUT"));
Serial.print(F("DATA3: "));
if (DATA3_PIN & DATA3_BIT) Serial.print(F("RELEASED "));
else Serial.print(F("ASSERTED, "));
if(DATA3_DDR & DATA3_BIT) Serial.println(F("OUTPUT"));
else Serial.println(F("INPUT"));
Serial.print(F("DATA4: "));
if (DATA4_PIN & DATA4_BIT) Serial.print(F("RELEASED "));
else Serial.print(F("ASSERTED, "));
if(DATA4_DDR & DATA4_BIT) Serial.println(F("OUTPUT"));
else Serial.println(F("INPUT"));
Serial.print(F("DATA5: "));
if (DATA5_PIN & DATA5_BIT) Serial.print(F("RELEASED "));
else Serial.print(F("ASSERTED, "));
if(DATA5_DDR & DATA5_BIT) Serial.println(F("OUTPUT"));
else Serial.println(F("INPUT"));
Serial.print(F("DATA6: "));
if (DATA6_PIN & DATA6_BIT) Serial.print(F("RELEASED "));
else Serial.print(F("ASSERTED, "));
if(DATA6_DDR & DATA6_BIT) Serial.println(F("OUTPUT"));
else Serial.println(F("INPUT"));
Serial.print(F("DATA7: "));
if (DATA7_PIN & DATA7_BIT) Serial.print(F("RELEASED "));
else Serial.print(F("ASSERTED, "));
if(DATA7_DDR & DATA7_BIT) Serial.println(F("OUTPUT"));
else Serial.println(F("INPUT"));
}
/*
'***********************************************
'***********************************************
Sub ReadGPIB()
' proceeds through a receive cycle as a state machine
' TODO: Add loop counter for timeout purposes
Dim last as boolean = false 'true = no error
Select Case receiveState
Case 0
#ifdef DEBUG
Call SerialSendStr("RCV ")
#endif
Call Clear(DAV) ' listener does not control DAV
Call Assert(NDAC) ' data not accepted
Call Release(NRFD) ' ready for data
if CheckState(DAV) then receiveState = 2' next state if DAV is already asserted
receiveState = 1
Case 1
if CheckState(DAV) then receiveState = 2 'next state when DAV is asserted
Case 2
receivingData = true
Call Assert(NRFD) 'indicate Not Ready For new Data
if (CheckState(EOI)) then last = true 'check if last byte of transmission
SerialSendByte(ReadByte()) 'read the data and send the data
Call Release(NDAC)
receiveState = 3
Case 3
if (Not CheckState(DAV)) then receiveState = 4 'next state when DAV is released
Case 4
Call Assert(NDAC)
Call Release(NRFD) 'leaves line idle if this is the last byte
if (last) then
SerialSendByte(_CR)
SerialSendByte(_LF)
Call Release(NDAC) 'leaves line idle
End IF
ReceiveState = 1
receivingData = false
End Select
end Sub
'***********************************************
'***********************************************
'Function WaitFor(byval line as Byte, byval state as Byte) as Byte
' Dim TimeOut as Long = 0
' Waitfor = 0 'ZERO indicates sucess
' PinRead() returns zero for low, and NON-ZERO for high... Not simply a 1
' While ((IIF(PinRead(line)<>0,1,0) <> state) AND TimeOut < ctlTMOLoops)
' TimeOut = TimeOut + 1
' Wend
'
' if TimeOut = ctlTMOLoops then Waitfor = 1 'ONE indicates error
'End Function
'*****************************************************
'*****************************************************
function CheckState(byval line as Byte) as Boolean
' WARNING
' USING THIS FUNCTION ASSUMES THE PIN BEING CHECKED IS SET TO INPUT!!