-
Notifications
You must be signed in to change notification settings - Fork 24
/
usb_to_gpib.c
1250 lines (1134 loc) · 35.4 KB
/
usb_to_gpib.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
/*
* GPIBUSB Adapter
* usb_to_gpib.c
**
* © 2013-2014 Steven Casagrande ([email protected]).
*
* This file is a part of the GPIBUSB Adapter project.
* Licensed under the AGPL version 3.
**
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
**
*
* This code requires the CCS compiler from ccsinfo.com to compile.
* A precompiled hex file is included at github.com/Galvant/gpibusb-firmware
*/
#include <18F4520.h>
#fuses HS, NOPROTECT, NOLVP, WDT, WDT4096
#use delay(clock=18432000)
#use rs232(baud=460800,uart1)
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "usb_to_gpib.h"
const unsigned int version = 5;
const unsigned int buf_size = 235;
char cmd_buf[10], buf[buf_size+20];
unsigned int buf_out = 0;
unsigned int buf_in = 0;
int partnerAddress = 1;
int myAddress;
char eos = 10; // Default end of string character.
char eos_string[3] = "";
char eos_code = 3;
char eoiUse = 1; // By default, we are using EOI to signal end of
// msg from instrument
char debug = 0; // enable or disable read&write error messages
byte strip = 0;
char autoread = 1;
char eot_enable = 1;
char eot_char = 13; // default CR
char listen_only = 0;
char mode = 1;
char save_cfg = 1;
unsigned int status_byte = 0;
unsigned int32 timeout = 1000;
unsigned int32 seconds = 0;
// Variables for device mode
boolean device_talk = false;
boolean device_listen = false;
boolean device_srq = false;
// EEPROM variables
const char VALID_EEPROM_CODE = 0xAA;
#define WITH_TIMEOUT
#define WITH_WDT
//#define VERBOSE_DEBUG
#int_timer2
void clock_isr() {
++seconds;
}
#int_rda
RDA_isr()
{
char c;
BOOLEAN add_null = false;
do {
c=getc();
if ((c>=32)&&(c<=126)) { // if human readable ascii char
buf[buf_in++] = c;
add_null = true;
}
} while((c!=10)&&(c!=13)); //both LF and CR are now valid termination chars
while(kbhit()){
buf[buf_in] = getc();
}
if (add_null)
buf[buf_in++] = 0x00;
if (buf_in >= buf_size)
buf_in = 0;
}
char buf_get(char *pnt) {
pnt = &(buf[buf_out]);
buf_out += (strlen(&(buf[buf_out])) + 1);
if (buf_out >= buf_size)
buf_out = 0;
if (buf_out == buf_in) {
buf_out = 0;
buf_in = 0;
}
return pnt;
}
// Puts all the GPIB pins into their correct initial states.
void prep_gpib_pins() {
output_low(TE); // Disables talking on data and handshake lines
output_low(PE);
if (mode) {
output_high(SC); // Allows transmit on REN and IFC
output_low(DC); // Transmit ATN and receive SRQ
}
else {
output_low(SC);
output_high(DC);
}
output_float(DIO1);
output_float(DIO2);
output_float(DIO3);
output_float(DIO4);
output_float(DIO5);
output_float(DIO6);
output_float(DIO7);
output_float(DIO8);
if (mode) {
output_high(ATN);
output_float(EOI);
output_float(DAV);
output_low(NRFD);
output_low(NDAC);
output_high(IFC);
output_float(SRQ);
output_low(REN);
}
else {
output_float(ATN);
output_float(EOI);
output_float(DAV);
output_float(NRFD);
output_float(NDAC);
output_float(IFC);
output_float(SRQ);
output_float(REN);
}
}
void gpib_init() {
prep_gpib_pins(); // Put all the pins into high-impedance mode
//output_low(NRFD); // ?? Needed ??
output_high(NDAC); // ?? Needed ??
}
char gpib_controller_assign(int address) {
myAddress = address;
output_low(IFC); // Assert interface clear. Resets bus and makes it
// controller in charge.
delay_ms(200);
output_float(IFC); // Finishing clearing interface
output_low(REN); // Put all connected devices into "remote" mode
cmd_buf[0] = CMD_DCL;
return gpib_cmd(cmd_buf, 1); // Send GPIB DCL cmd, clear all devices on bus
}
char gpib_cmd(char *bytes, int length) {
// Write a GPIB CMD byte to the bus
return _gpib_write(bytes, length, 1, 0);
}
char gpib_write(char *bytes, int length, useEOI) {
// Write a GPIB data string to the bus
return _gpib_write(bytes, length, 0, useEOI);
}
char _gpib_write(char *bytes, int length, BOOLEAN attention, BOOLEAN useEOI) {
/*
* Write a string of bytes to the bus
* bytes: array containing characters to be written
* length: number of bytes to write, 0 if not known.
* attention: 1 if this is a gpib command, 0 for data
*/
char a; // Storage variable for the current character
int i; // Loop counter variable
output_high(PE);
if(attention) // If byte is a gpib bus command
{
output_low(ATN); // Assert the ATN line, informing all
// this is a cmd byte.
}
if(length==0) // If the length was unknown
{
length = strlen((char*)bytes); // Calculate the number of bytes to
// be sent
}
output_high(TE); // Enable talking
output_high(EOI);
output_high(DAV);
output_float(NRFD);
output_float(NDAC);
// Before we start transfering, we have to make sure that NRFD is high
// and NDAC is low
#ifdef WITH_TIMEOUT
seconds = 0;
enable_interrupts(INT_TIMER2);
while((input(NDAC) || !(input(NRFD))) && (seconds <= timeout)) {
restart_wdt();
if(seconds >= timeout) {
if (debug == 1) {
printf("Timeout: Before writing %c %x ", bytes[0], bytes[0]);
}
device_talk = false;
device_srq = false;
prep_gpib_pins();
return 1;
}
}
disable_interrupts(INT_TIMER2);
#else
while(input(NDAC)){}
#endif
for(i = 0;i < length;i++) { //Loop through each character, write to bus
a = bytes[i]; // So I don't have to keep typing bytes[i]
#ifdef VERBOSE_DEBUG
printf("Writing byte: %c %x %c", a, a, eot_char);
#endif
// Wait for NDAC to go low, indicating previous bit is now done with
#ifdef WITH_TIMEOUT
seconds = 0;
enable_interrupts(INT_TIMER2);
while(input(NDAC) && (seconds <= timeout)) {
restart_wdt();
if(seconds >= timeout) {
if (debug == 1) {
printf("Timeout: Waiting for NDAC to go low while writing%c", eot_char);
}
device_talk = false;
device_srq = false;
prep_gpib_pins();
return 1;
}
}
disable_interrupts(INT_TIMER2);
#else
while(input(NDAC)){}
#endif
// Put the byte on the data lines
a = a^0xff;
output_b(a);
output_float(NRFD);
// Wait for listeners to be ready for data (NRFD should be high)
#ifdef WITH_TIMEOUT
seconds = 0;
enable_interrupts(INT_TIMER2);
while(!(input(NRFD)) && (seconds <= timeout)) {
restart_wdt();
if(seconds >= timeout) {
if (debug == 1) {
printf("Timeout: Waiting for NRFD to go high while writing%c", eot_char);
}
device_talk = false;
device_srq = false;
prep_gpib_pins();
return 1;
}
}
disable_interrupts(INT_TIMER2);
#else
while(!(input(NRFD))){}
#endif
if((i==length-1) && (useEOI)) { // If last byte in string
output_low(EOI); // Assert EOI
}
output_low(DAV); // Inform listeners that the data is ready to be read
// Wait for NDAC to go high, all listeners have accepted the byte
#ifdef WITH_TIMEOUT
seconds = 0;
enable_interrupts(INT_TIMER2);
while(!(input(NDAC)) && (seconds <= timeout)) {
restart_wdt();
if(seconds >= timeout) {
if (debug == 1) {
printf("Timeout: Waiting for NDAC to go high while writing%c", eot_char);
}
device_talk = false;
device_srq = false;
prep_gpib_pins();
return 1;
}
}
disable_interrupts(INT_TIMER2);
#else
while(!(input(NDAC))){}
#endif
output_high(DAV); // Byte has been accepted by all, indicate
// byte is no longer valid
} // Finished outputing all bytes to listeners
output_low(TE); // Disable talking on datalines
// Float all data lines
output_float(DIO1);
output_float(DIO2);
output_float(DIO3);
output_float(DIO4);
output_float(DIO5);
output_float(DIO6);
output_float(DIO7);
output_float(DIO8);
if(attention) { // If byte was a gpib cmd byte
output_high(ATN); // Release ATN line
}
output_float(DAV);
output_float(EOI);
output_high(NDAC);
output_high(NRFD);
output_low(PE);
return 0;
}
char gpib_receive(char *byt) {
char a = 0; // Storage for received character
char eoiStatus; // Returns 0x00 or 0x01 depending on status of EOI line
// Raise NRFD, telling the talker we are ready for the byte
output_high(NRFD);
// Assert NDAC informing the talker we have not accepted the byte yet
output_low(NDAC);
output_float(DAV);
// Wait for DAV to go low (talker informing us the byte is ready)
#ifdef WITH_TIMEOUT
seconds = 0;
enable_interrupts(INT_TIMER2);
while(input(DAV) && (seconds <= timeout)) {
restart_wdt();
if(seconds >= timeout) {
if (debug == 1) {
printf("Timeout: Waiting for DAV to go low while reading%c", eot_char);
}
device_listen = false;
prep_gpib_pins();
return 0xff;
}
}
disable_interrupts(INT_TIMER2);
#else
while(input(DAV)) {}
#endif
// Assert NRFD, informing talker to not change the data lines
output_low(NRFD);
// Read port B, where the data lines are connected
a = input_b();
a = a^0xff; // Flip all bits since GPIB uses negative logic.
eoiStatus = input(EOI);
#ifdef VERBOSE_DEBUG
printf("Got byte: %c %x ", a, a);
#endif
// Un-assert NDAC, informing talker that we have accepted the byte
output_float(NDAC);
// Wait for DAV to go high (talker knows that we have read the byte)
#ifdef WITH_TIMEOUT
seconds = 0;
enable_interrupts(INT_TIMER2);
while(!(input(DAV)) && (seconds<=timeout) ) {
restart_wdt();
if(seconds >= timeout) {
if (debug == 1){
printf("Timeout: Waiting for DAV to go high while reading%c", eot_char);
}
device_listen = false;
prep_gpib_pins();
return 0xff;
}
}
disable_interrupts(INT_TIMER2);
#else
while(!(input(DAV))) {}
#endif
// Prep for next byte, we have not accepted anything
output_low(NDAC);
#ifdef VERBOSE_DEBUG
printf("EOI: %c%c", eoiStatus, eot_char);
#endif
*byt = a;
return eoiStatus;
}
char gpib_read(boolean read_until_eoi) {
char readCharacter,eoiStatus;
char readBuf[100];
char i = 0, j=0;
char errorFound = 0;
boolean reading_done = false;
char *bufPnt;
bufPnt = &readBuf[0];
#ifdef VERBOSE_DEBUG
printf("gpib_read start\n\r");
#endif
if (mode) {
// Command all talkers and listeners to stop
cmd_buf[0] = CMD_UNT;
errorFound = errorFound || gpib_cmd(cmd_buf, 1);
cmd_buf[0] = CMD_UNL;
errorFound = errorFound || gpib_cmd(cmd_buf, 1);
if(errorFound){return 1;}
// Set the controller into listener mode
cmd_buf[0] = myAddress + 0x20;
errorFound = errorFound || gpib_cmd(cmd_buf, 1);
if(errorFound){return 1;}
// Set target device into talker mode
cmd_buf[0] = partnerAddress + 0x40;
errorFound = gpib_cmd(cmd_buf, 1);
if(errorFound){return 1;}
}
i = 0;
bufPnt = &readBuf[0];
/*
* In this section you will notice that I buffer the received characters,
* then manually iterate the pointer through the buffer, writing them to
* UART. If I instead just tried to printf the entire 'string' it would
* fail. (even if I add a null char at the end). This is because when
* transfering binary data, some actual data points can be 0x00.
*
* The other option of going putc(readBuf[x]);x++; Is for some reason slower
* than getting a pointer on the first element, then iterating that pointer
* through the buffer (as I have done here).
*/
#ifdef VERBOSE_DEBUG
printf("gpib_read loop start\n\r");
#endif
if(read_until_eoi == 1){
do {
eoiStatus = gpib_receive(&readCharacter); // eoiStatus is line lvl
if(eoiStatus==0xff){return 1;}
if (eos_code != 0) {
if((readCharacter != eos_string[0]) || (eoiStatus)){ // Check for EOM char
readBuf[i] = readCharacter; //Copy the read char into the buffer
i++;
}
}
else {
if((readCharacter == eos_string[1]) && (eoiStatus == 0)) {
if (readBuf[i-1] == eos_string[0]) {
i--;
}
}
else {
readBuf[i] = readCharacter;
i++;
}
}
if(i == 100){
for(j=0;j<100;++j){
putc(*bufPnt);
++bufPnt;
}
i = 0;
bufPnt = &readBuf[0];
#ifdef WITH_WDT
restart_wdt();
#endif
}
} while (eoiStatus);
for(j=0;j<i-strip;++j){
putc(*bufPnt);
++bufPnt;
}
} else {
do {
eoiStatus = gpib_receive(&readCharacter);
if(eoiStatus==0xff){return 1;}
if (eos_code != 0) {
if(readCharacter != eos_string[0]){ // Check for EOM char
readBuf[i] = readCharacter; //Copy the read char into the buffer
i++;
}
else {
reading_done = true;
}
}
else {
if(readCharacter == eos_string[1]) {
if (readBuf[i-1] == eos_string[0]) {
i--;
reading_done = true;
}
}
else {
readBuf[i] = readCharacter;
i++;
}
}
if(i == 100){
for(j=0;j<100;++j){
putc(*bufPnt);
++bufPnt;
}
i = 0;
bufPnt = &readBuf[0];
#ifdef WITH_WDT
restart_wdt();
#endif
}
} while (reading_done == false);
reading_done = false;
for(j=0;j<i-strip;++j){
putc(*bufPnt);
++bufPnt;
}
}
if (eot_enable == 1) {
printf("%c", eot_char);
}
#ifdef VERBOSE_DEBUG
printf("gpib_read loop end\n\r");
#endif
if (mode) {
errorFound = 0;
// Command all talkers and listeners to stop
cmd_buf[0] = CMD_UNT;
errorFound = errorFound || gpib_cmd(cmd_buf, 1);
cmd_buf[0] = CMD_UNL;
errorFound = errorFound || gpib_cmd(cmd_buf, 1);
}
#ifdef VERBOSE_DEBUG
printf("gpib_read end\n\r");
#endif
return errorFound;
}
char addressTarget(int address) {
/*
* Address the currently specified GPIB address (as set by the ++addr cmd)
* to listen
*/
char writeError = 0;
cmd_buf[0] = CMD_UNT;
writeError = writeError || gpib_cmd(cmd_buf, 1);
cmd_buf[0] = CMD_UNL; // Everyone stop listening
writeError = writeError || gpib_cmd(cmd_buf, 1);
cmd_buf[0] = address + 0x20;
writeError = writeError || gpib_cmd(cmd_buf, 1);
return writeError;
}
boolean srq_state(void) {
return !((boolean)input(SRQ));
}
void serial_poll(int address) {
char error = 0;
char status_byte;
cmd_buf[0] = CMD_SPE; // enable serial poll
error = error || gpib_cmd(cmd_buf, 1);
cmd_buf[0] = address + 0x40;
error = error || gpib_cmd(cmd_buf, 1);
if (error) return;
error = gpib_receive(&status_byte);
if (error == 1) error = 0; // gpib_receive returns EOI lvl and 0xFF on errors
if (error == 0xFF) error = 1;
cmd_buf[0] = CMD_SPD; // disable serial poll
gpib_cmd(cmd_buf, 1);
if (!error)
printf("%c%c", status_byte, eot_char);
}
void main(void) {
char writeError = 0;
char *buf_pnt = &buf[0];
// Original Command Set
char addressBuf[4] = "+a:";
char timeoutBuf[4] = "+t:";
char eosBuf[6] = "+eos:";
char eoiBuf[6] = "+eoi:";
char testBuf[6] = "+test";
char readCmdBuf[6] = "+read";
char getCmdBuf[5] = "+get";
char stripBuf[8] = "+strip:";
char versionBuf[5] = "+ver";
char autoReadBuf[11] = "+autoread:";
char resetBuf[7] = "+reset";
char debugBuf[8] = "+debug:";
// Prologix Compatible Command Set
char addrBuf[7] = "++addr";
char autoBuf[7] = "++auto";
char clrBuf[6] = "++clr";
char eotEnableBuf[13] = "++eot_enable";
char eotCharBuf[11] = "++eot_char";
char ifcBuf[6] = "++ifc";
char lloBuf[6] = "++llo";
char locBuf[6] = "++loc";
char lonBuf[6] = "++lon"; //TODO: Listen mode
char modeBuf[7] = "++mode";
char readTimeoutBuf[14] = "++read_tmo_ms";
char rstBuf[6] = "++rst";
char savecfgBuf[10] = "++savecfg";
char spollBuf[8] = "++spoll";
char srqBuf[6] = "++srq";
char statusBuf[9] = "++status";
char trgBuf[6] = "++trg";
char verBuf[6] = "++ver";
char helpBuf[7] = "++help"; //TODO
output_high(LED_ERROR); // Turn on the error LED
// Setup the Watchdog Timer
#ifdef WITH_WDT
setup_wdt(WDT_ON);
#endif
#ifdef WITH_TIMEOUT
// Setup the timer
set_rtcc(0);
setup_timer_2(T2_DIV_BY_16,144,2); // 1ms interupt
enable_interrupts(GLOBAL);
disable_interrupts(INT_TIMER2);
#endif
// Handle the EEPROM stuff
if (read_eeprom(0x00) == VALID_EEPROM_CODE) {
mode = read_eeprom(0x01);
partnerAddress = read_eeprom(0x02);
eot_char = read_eeprom(0x03);
eot_enable = read_eeprom(0x04);
eos_code = read_eeprom(0x05);
switch (eos_code) {
case 0:
eos_code = 0;
eos_string[0] = 13;
eos_string[1] = 10;
eos_string[2] = 0x00;
eos = 10;
break;
case 1:
eos_code = 1;
eos_string[0] = 13;
eos_string[1] = 0x00;
eos = 13;
break;
case 2:
eos_code = 2;
eos_string[0] = 10;
eos_string[1] = 0x00;
eos = 10;
break;
default:
eos_code = 3;
eos_string[0] = 0x00;
eos = 0;
break;
}
eoiUse = read_eeprom(0x06);
autoread = read_eeprom(0x07);
listen_only = read_eeprom(0x08);
save_cfg = read_eeprom(0x09);
}
else {
write_eeprom(0x00, VALID_EEPROM_CODE);
write_eeprom(0x01, 1); // mode
write_eeprom(0x02, 1); // partnerAddress
write_eeprom(0x03, 13); // eot_char
write_eeprom(0x04, 1); // eot_enable
write_eeprom(0x05, 3); // eos_code
write_eeprom(0x06, 1); // eoiUse
write_eeprom(0x07, 1); // autoread
write_eeprom(0x08, 0); // listen_only
write_eeprom(0x09, 1); // save_cfg
}
// Start all the GPIB related stuff
gpib_init(); // Initialize the GPIB Bus
if (mode) {
gpib_controller_assign(0x00);
}
/*
* The following little block helps provide some visual feedback as to which
* stage of the startup process the microcontroller is in. This is because
* during testing I found that enabling the RDA interrupt would cause issues
* on my dev system (ubuntu gnome edition 13.10 64bit) when first plugged
* into the computer. This, in combination with the fact that the serial
* port is unaccessable to my user account for approx 30sec after initial
* enumeration (but is able to be opened by root) leads me to believe that
* some update to ubuntu or the linux kernel or something is probing newly
* connected usb->serial adapters. Whatever it is that my PC is sending is
* causing the adapater to have a fit. This is probably due to a high volume
* of RDA interrupts and the system is unable to process them before the
* next. I imagine maybe that in the end, "buf" is getting messed up, but
* in the end the WDT solves the lockup issue.
*
* Note this problem is only on initial USB connection and not when pushing
* the reset button.
*
* UPDATE: It turns out this is due to the software package "modemmanager".
* The easiest solution is just to remove it. On Debian-based distros run
* apt-get purge modemmanager
*/
output_low(LED_ERROR); // Turn off the error LED
restart_wdt();
delay_ms(100);
restart_wdt();
output_high(LED_ERROR);
restart_wdt();
delay_ms(100);
restart_wdt();
enable_interrupts(INT_RDA);
restart_wdt();
output_low(LED_ERROR);
#ifdef VERBOSE_DEBUG
switch (restart_cause())
{
case WDT_TIMEOUT:
{
printf("WDT restart\r\n");
break;
}
case NORMAL_POWER_UP:
{
printf("Normal power up\r\n");
break;
}
}
#endif
// Main execution loop
for(;;) {
#ifdef WITH_WDT
restart_wdt();
#endif
if(buf_in != buf_out) {
buf_pnt = buf_get(buf_pnt);
if(*buf_pnt == '+') { // Controller commands start with a +
// +a:N
if(strncmp((char*)buf_pnt,(char*)addressBuf,3)==0) {
partnerAddress = atoi((char*)(buf_pnt+3)); // Parse out the GPIB address
}
// ++addr N
else if(strncmp((char*)buf_pnt,(char*)addrBuf,6)==0) {
if (*(buf_pnt+6) == 0x00) {
printf("%i%c", partnerAddress, eot_char);
}
else if (*(buf_pnt+6) == 32) {
partnerAddress = atoi((char*)(buf_pnt+7));
}
}
// +t:N
else if(strncmp((char*)buf_pnt,(char*)timeoutBuf,3)==0) {
timeout = atoi32((char*)(buf_pnt+3)); // Parse out the timeout period
}
// ++read_tmo_ms N
else if(strncmp((char*)buf_pnt,(char*)readTimeoutBuf,13)==0) {
if (*(buf_pnt+13) == 0x00) {
printf("%Lu%c", timeout, eot_char);
}
else if (*(buf_pnt+13) == 32) {
timeout = atoi32((char*)(buf_pnt+14));
}
}
// +read
else if((strncmp((char*)buf_pnt,(char*)readCmdBuf,5)==0) && (mode)) {
if(gpib_read(eoiUse)){
if (debug == 1) {printf("Read error occured.%c", eot_char);}
//delay_ms(1);
//reset_cpu();
}
}
// ++read
else if((strncmp((char*)buf_pnt+1,(char*)readCmdBuf,5)==0) && (mode)) {
if (*(buf_pnt+6) == 0x00) {
gpib_read(false); // read until EOS condition
}
else if (*(buf_pnt+7) == 101) {
gpib_read(true); // read until EOI flagged
}
/*else if (*(buf_pnt+6) == 32) {
// read until specified character
}*/
}
// +test
else if(strncmp((char*)buf_pnt,(char*)testBuf,5)==0) {
printf("testing%c", eot_char);
}
// +eos:N
else if(strncmp((char*)buf_pnt,(char*)eosBuf,5)==0) {
eos = atoi((char*)(buf_pnt+5)); // Parse out the end of string byte
eos_string[0] = eos;
eos_string[1] = 0x00;
eos_code = 4;
}
// ++eos {0|1|2|3}
else if(strncmp((char*)buf_pnt+1,(char*)eosBuf,4)==0) {
if (*(buf_pnt+5) == 0x00) {
printf("%i%c", eos_code, eot_char);
}
else if (*(buf_pnt+5) == 32) {
eos_code = atoi((char*)(buf_pnt+6));
switch (eos_code) {
case 0:
eos_code = 0;
eos_string[0] = 13;
eos_string[1] = 10;
eos_string[2] = 0x00;
eos = 10;
break;
case 1:
eos_code = 1;
eos_string[0] = 13;
eos_string[1] = 0x00;
eos = 13;
break;
case 2:
eos_code = 2;
eos_string[0] = 10;
eos_string[1] = 0x00;
eos = 10;
break;
default:
eos_code = 3;
eos_string[0] = 0x00;
eos = 0;
break;
}
}
}
// +eoi:{0|1}
else if(strncmp((char*)buf_pnt,(char*)eoiBuf,5)==0) {
eoiUse = atoi((char*)(buf_pnt+5)); // Parse out the end of string byte
}
// ++eoi {0|1}
else if(strncmp((char*)buf_pnt+1,(char*)eoiBuf,4)==0) {
if (*(buf_pnt+5) == 0x00) {
printf("%i%c", eoiUse, eot_char);
}
else if (*(buf_pnt+5) == 32) {
eoiUse = atoi((char*)(buf_pnt+6));
}
}
// +strip:{0|1}
else if(strncmp((char*)buf_pnt,(char*)stripBuf,7)==0) {
strip = atoi((char*)(buf_pnt+7)); // Parse out the end of string byte
}
// +ver
else if(strncmp((char*)buf_pnt,(char*)versionBuf,4)==0) {
printf("%i%c", version, eot_char);
}
// ++ver
else if(strncmp((char*)buf_pnt+1,(char*)versionBuf,4)==0) {
printf("Version %i.0%c", version, eot_char);
}
// +get
else if((strncmp((char*)buf_pnt,(char*)getCmdBuf,4)==0) && (mode)) {
if (*(buf_pnt+5) == 0x00) {
writeError = writeError || addressTarget(partnerAddress);
cmd_buf[0] = CMD_GET;
gpib_cmd(cmd_buf, 1);
}
/*else if (*(buf_pnt+5) == 32) {
TODO: Add support for specified addresses
}*/
}
// ++trg
else if((strncmp((char*)buf_pnt,(char*)trgBuf,5)==0) && (mode)) {
if (*(buf_pnt+5) == 0x00) {
writeError = writeError || addressTarget(partnerAddress);
cmd_buf[0] = CMD_GET;
gpib_cmd(cmd_buf, 1);
}
/*else if (*(buf_pnt+5) == 32) {
TODO: Add support for specified addresses
}*/
}
// +autoread:{0|1}
else if(strncmp((char*)buf_pnt,(char*)autoReadBuf,10)==0) {
autoread = atoi((char*)(buf_pnt+10));
}
// ++auto {0|1}
else if(strncmp((char*)buf_pnt,(char*)autoBuf,6)==0) {
if (*(buf_pnt+6) == 0x00) {
printf("%i%c", autoRead, eot_char);
}
else if (*(buf_pnt+6) == 32) {
autoread = atoi((char*)(buf_pnt+7));
if ((autoread != 0) && (autoread != 1)) {
autoread = 1; // If non-bool sent, set to enable
}
}
}
// +reset
else if(strncmp((char*)buf_pnt,(char*)resetBuf,6)==0) {
delay_ms(1);
reset_cpu();
}
// ++rst
else if(strncmp((char*)buf_pnt,(char*)rstBuf,5)==0) {
delay_ms(1);
reset_cpu();
}
// +debug:{0|1}
else if(strncmp((char*)buf_pnt,(char*)debugBuf,7)==0) {
debug = atoi((char*)(buf_pnt+7));
}
// ++debug {0|1}
else if(strncmp((char*)buf_pnt+1,(char*)debugBuf,6)==0) {
if (*(buf_pnt+7) == 0x00) {
printf("%i%c", debug, eot_char);
}
else if (*(buf_pnt+7) == 32) {
debug = atoi((char*)(buf_pnt+8));
if ((debug != 0) && (debug != 1)) {
debug = 0; // If non-bool sent, set to disabled
}
}
}
// ++clr
else if((strncmp((char*)buf_pnt,(char*)clrBuf,5)==0) && (mode)) {
// This command is special in that we must
// address a specific instrument.
writeError = writeError || addressTarget(partnerAddress);