-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgpib_conv_nano.ino
1158 lines (1001 loc) · 27 KB
/
gpib_conv_nano.ino
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
/* GPIB to USB converter
Arduino nano with ATmega328P
*/
/*
GPIB Connector pinout
Pin | Name | Description | Source | Atmega pin
----+-------+--------------------+-------------------+--------------------
1 | DIO1 | Data bit 1 (LSB) | Talker | PC0 A0
2 | DIO2 | Data bit 2 | Talker | PC1 A1
3 | DIO3 | Data bit 3 | Talker | PC2 A2
4 | DIO4 | Data bit 4 | Talker | PC3 A3
5 | EOI | End Or Indentity | Talker/Controller | PD2 D2
6 | DAV | Data Valid | Controller | PD3 D3
7 | NRFD | Not Ready For Data | Listener | PD4 D4
8 | NDAC | No Data Accepted | Listener | PD5 D5
9 | IFC | Interface Clear | Controller | PD6 D6
10 | SRQ | Service Request | Talker | PD7 D7
11 | ATN | Attention | Controller | PB2 D10
12 | | Shield | |
13 | DIO5 | Data bit 5 | Talker | PC4 A4
14 | DIO6 | Data bit 6 | Talker | PC5 A5
15 | DIO7 | Data Bit 7 | Talker | PB0 D8
16 | DIO8 | Data bit 8 (MSB) | Talker | PB1 D9
17 | REN | Remote Enabled | Controller | PB3 D11
18 | | GND DAV | |
19 | | GND NRFD | |
20 | | GND NDAC | |
21 | | GND IFC | |
22 | | GND SRQ | |
23 | | GND ATN | |
24 | | GND data | |
*/
#define EOI (1<<2) //PD2 - D2, pin 5 GPIB
#define DAV (1<<3) //PD3 - D3, pin 6 GPIB
#define NRFD (1<<4) //PD4 - D4, pin 7 GPIB, output
#define NDAC (1<<5) //PD5 - D5, pin 8 GPIB, output
#define IFC (1<<6) //PD6 - D6, pin 9 GPIB
#define SRQ (1<<7) //PD7 - D7, pin 10 GPIB
#define ATN (1<<2) //PB2 - D10, pin 11 GPIB
#define REN (1<<3) //PB3 - D11, pin 17 GPIB
#define SetLed(x) ( PORTB = x?(PORTB | (1<<5)) : (PORTB & ~(1<<5)) )
#define SetEOI(x) ( PORTD = (x)? (PORTD | EOI) : (PORTD & ~EOI) )
#define SetDAV(x) ( PORTD = (x)? (PORTD | DAV) : (PORTD & ~DAV) )
#define SetNRFD(x) ( PORTD = (x)? (PORTD | NRFD) : (PORTD & ~NRFD) )
#define SetNDAC(x) ( PORTD = (x)? (PORTD | NDAC) : (PORTD & ~NDAC) )
#define SetIFC(x) ( PORTD = (x)? (PORTD | IFC) : (PORTD & ~IFC) )
#define SetSRQ(x) ( PORTD = (x)? (PORTD | SRQ) : (PORTD & ~SRQ) )
#define SetATN(x) ( PORTB = (x)? (PORTB | ATN) : (PORTB & ~ATN) )
#define SetREN(x) ( PORTB = (x)? (PORTB | REN) : (PORTB & ~REN) )
#define ESC_KEY_UP 0x41
#define ESC_KEY_DOWN 0x42
#define ESC_KEY_RIGHT 0x43
#define ESC_KEY_LEFT 0x44
#define MAX_COMMANDS 15
#define BUF_SIZE 64
#define GPIB_BUF_SIZE 128
#define GPIB_MAX_RECEIVE_TIMEOUT 50000
#define GPIB_MAX_TRANSMIT_TIMEOUT 50000
#define EMPTY_LINE 1
#define DEFAULT_ADDRESS 21
#define HELP_LINES 18
#define HELP_STRING_LEN 64
const char helpStrings[HELP_LINES][HELP_STRING_LEN] PROGMEM = {
"GPIB to USB converter v4\r\n\r\n",
"Transmit commands, OK/TIMEOUT/ERROR\r\n",
" <D> Data (ATN false), <M> Data without EOI\r\n",
" <C> Command (ATN true)\r\n",
" <T> Hex transmit (0C - command, 0D - data)\r\n",
"Receive commands (receives until EOI,max 127 bytes)\r\n",
" <X> ASCII, <payload> or TIMEOUT\r\n",
" <Y> BINARY, <length><payload>\r\n",
" <Z> HEX, <length><payload>\r\n",
" <P> Continous read (plotter mode)\r\n",
"General commands\r\n",
" <A> Set/get converter talk address\r\n",
" <S> Get REQ/SRQ/LISTEN state (1 if true)\r\n",
" <R> Set REMOTE mode (REN true)\r\n",
" <L> Set LOCAL mode (REN false)\r\n",
" <I> Generate IFC pulse\r\n",
" <E> Get/set echo on(E1)/off(E0)\r\n",
" <H> Commands history\r\n"
};
typedef enum {OFF = 0, SLOW, FAST} ledBlinking_t;
ledBlinking_t ledBlinking = OFF;
unsigned char listenAddress = DEFAULT_ADDRESS;
unsigned char msgEndSeq = 0;
unsigned char remoteState = 0;
#define FOSC 16000000UL // Clock Speed
#define BAUD 115200UL
#define MYUBRR FOSC/8/BAUD-1 //U2X set to 1
// The UDRE Flag indicates if the transmit buffer (UDR) is ready to receive new data
#define UARTTransmitBufferEmpty() ( UCSR0A & (1<<UDRE0))
// This flag bit is set when there are unread data in the receive buffer and cleared after UDR read
#define UARTDataAvailable() (UCSR0A & (1<<RXC0))
/* USART on */
void UART_init (void) {
unsigned int ubrr = MYUBRR;
UBRR0H = (unsigned char)(ubrr>>8);
UBRR0L = (unsigned char)ubrr;
UCSR0A = (1<<U2X0);
/* Enable receiver and transmitter */
UCSR0B = (1<<RXEN0) | (1<<TXEN0);
/* Set frame format: 8data, 1 stop bit, no parity */
UCSR0C = (3<<UCSZ00);
}
void UART_transmit( unsigned char data ) {
/* Wait for empty transmit buffer */
while ( !UARTTransmitBufferEmpty() );
/* Put data into buffer, sends the data */
UDR0 = data;
}
unsigned char UART_receive( void ) {
/* Wait for data to be received */
while ( !UARTDataAvailable() );
/* Get and return received data from buffer */
return UDR0;
}
unsigned char DataRead()
{
unsigned char val;
val = PINC & 0x3f;
val |= (PINB & 0x03) << 6;
return val;
}
void DataWrite(unsigned char val)
{
PORTC = val&0x3f;
PORTB = (PORTB&0xfc) | (val>>6);
}
/* ======================================================= */
void GPIO_init() {
// DDR = 1 output
// DDR = 0 input
DDRB = (1<<5); // PB5 output, PB4 input
PORTB = (1<<4);
}
int uart_putchar(char ch, FILE* file)
{
UART_transmit(ch);
return ch;
}
void ReconfigureGPIO_GPIBReceiveMode()
{
DDRB = (1<<5) | ATN | REN; // these lines are outputs, other as inputs
PORTB = (1<<4) | 0x03 | ATN | (remoteState?0:REN); // pullup on
DDRC = 0x00; // PC0-PA5 inputs
PORTC = 0x3f; // pullup on
DDRD = IFC | REN | NRFD | NDAC; // these lines are outputs, other as inputs
PORTD = IFC | (remoteState?0:REN) | EOI | DAV | SRQ; // pullup on
}
void ReconfigureGPIO_GPIBNormalMode()
{
DDRC = 0x3f; //outputs
DDRB = (1<<5) | ATN | REN | 0x03; // these lines are outputs
PORTB = (1<<4) | ATN | (remoteState?0:REN); // pullup on
DataWrite(0x00); // output level 0
DDRD = IFC | EOI | DAV; // these lines are outputs
PORTD = IFC | EOI | DAV | SRQ | NRFD | NDAC; // pullup on
}
int GPIB_Receive(unsigned char * buf, unsigned char bufLength, unsigned char * receivedLength)
{
unsigned char index = 0;
unsigned char c;
unsigned int timeout;
do
{
SetNRFD(1); //ready for receiving data
//-1 & 5
timeout = 0;
while (PIND & DAV) // waiting for falling edge
{
timeout++;
if (timeout > GPIB_MAX_RECEIVE_TIMEOUT)
{
*receivedLength = index;
SetNRFD(0);
return 0;
}
}
// 0
SetNRFD(0); //not ready for receiving data
// 1
c = ~DataRead(); //read data
buf[index++] = c;
SetNDAC(1); //data accepted
//2
while (!(PIND & DAV)) // waiting for rising edge
{
//delay(1);
timeout++;
if (timeout > GPIB_MAX_RECEIVE_TIMEOUT)
{
*receivedLength = index;
SetNDAC(0);
return 0;
}
}
//3
SetNDAC(0);
//4
} while ((index < bufLength) /*&& (c != 13)*/);
*receivedLength = index;
return 255;
}
int GPIB_Receive_till_eoi(unsigned char * buf, unsigned char bufLength, unsigned char * receivedLength)
{
unsigned char index = 0;
unsigned char c;
unsigned char eoi = 0;
unsigned int timeout;
do
{
SetNRFD(1); //ready for receiving data
//-1 & 5
timeout = 0;
while (PIND & DAV) // waiting for falling edge
{
timeout++;
if (timeout > GPIB_MAX_RECEIVE_TIMEOUT)
{
*receivedLength = index;
SetNRFD(0);
return 0;
}
}
// 0
if ((PIND & EOI) == 0)
eoi = 1;
SetNRFD(0); //not ready for receiving data
// 1
c = ~DataRead(); //read data
buf[index++] = c;
SetNDAC(1); //data accepted
//2
while (!(PIND & DAV)) // waiting for rising edge
{
timeout++;
if (timeout > GPIB_MAX_RECEIVE_TIMEOUT)
{
*receivedLength = index;
SetNDAC(0);
return 0;
}
}
//3
SetNDAC(0);
//4
} while ((index < bufLength) && (eoi == 0));
*receivedLength = index;
return 255;
}
int GPIB_Receive_till_lf(unsigned char * buf, unsigned char bufLength, unsigned char * receivedLength)
{
unsigned char index = 0;
unsigned char c;
unsigned int timeout;
//SetNDAC(0);
//SetNRFD(0);
do
{
SetNRFD(1); //ready for receiving data
//-1 & 5
timeout = 0;
while (PIND & DAV) // waiting for falling edge
{
timeout++;
if (timeout > GPIB_MAX_RECEIVE_TIMEOUT)
{
*receivedLength = index;
SetNRFD(0);
return 0;
}
}
// 0
SetNRFD(0); //not ready for receiving data
// 1
c = ~DataRead(); //read data
buf[index++] = c;
SetNDAC(1); //data accepted
//2
while (!(PIND & DAV)) // waiting for rising edge
{
timeout++;
if (timeout > GPIB_MAX_RECEIVE_TIMEOUT)
{
*receivedLength = index;
SetNDAC(0);
return 0;
}
}
//3
SetNDAC(0);
//4
} while ((index < bufLength) && (c != 10));
*receivedLength = index;
return 255;
}
int GPIB_Transmit(unsigned char * buf, unsigned char bufLength, unsigned char eoi)
{
unsigned char index = 0;
unsigned int timeout;
if ((0 == bufLength) || ((PIND & NRFD) && (PIND & NDAC)))
return 0;
do
{
if ((index+1 == bufLength) && eoi)
SetEOI(0); // last byte
//transmit debug
//printf("%02x ", buf[index]);
DataWrite(~buf[index]);
index++;
delayMicroseconds(100);
timeout = 0;
while (!(PIND & NRFD)) // waiting for high on NRFD
{
timeout++;
if (timeout > GPIB_MAX_TRANSMIT_TIMEOUT)
{
SetEOI(1);
return 0;
}
}
SetDAV(0);
delayMicroseconds(100);
while (!(PIND & NDAC)) // waiting for high on NDAC
{
timeout++;
if (timeout > GPIB_MAX_TRANSMIT_TIMEOUT)
{
SetEOI(1);
SetDAV(1);
return 0;
}
}
SetEOI(1);
SetDAV(1);
//4
} while ((index < bufLength));
//printf("\r\n");
return 255;
}
void ShowHelp()
{
char buf[64];
unsigned char i;
for (i=0; i<HELP_LINES; i++)
{
memcpy_P(buf, helpStrings[i], HELP_STRING_LEN);
printf("%s", buf);
}
}
char UART_RcvEscapeSeq()
{
while (!UARTDataAvailable());
if (UART_receive() != 0x5B)
return 0;
while (!UARTDataAvailable());
return UART_receive();
}
#define T1_INIT 64910 // preload timer 65536-16MHz/256/100Hz
ISR(TIMER1_OVF_vect)
{
static unsigned char timCnt = 0;
static unsigned char led = 0;
TCNT1 = T1_INIT;
if (OFF == ledBlinking)
return;
timCnt++;
if (timCnt >= ((ledBlinking==SLOW)?25:5))
{
timCnt = 0;
led = !led;
SetLed(led);
}
}
#define ishexdigit(x) \
(((x >= '0') && (x <= '9')) || \
((x >= 'A') && (x <= 'F')))
unsigned char hex2dec(unsigned char x)
{
if ((x >= '0') && (x <= '9'))
return (x-'0');
else
return (10+x-'A');
}
unsigned char CheckHexMsg(unsigned char * buf, unsigned char len, unsigned char *outputMsg, unsigned char *outputLen, unsigned char *eoi)
{
unsigned char i;
*eoi = 1; //default
if (('D'==toupper(buf[1])) && (';'==buf[len-1]))
{
--len;
*eoi = 0;
}
if ((len & 0x01) || (len < 4))
return 0; //msg length is not even
if (('0'!=buf[0]) || (('C'!=toupper(buf[1])) && ('D'!=toupper(buf[1]))))
return 0;
for (i=2; i<len; i++)
{
if (!ishexdigit(toupper(buf[i])))
return 0;
}
*outputLen = 0;
for (i=2; i<len; i=i+2)
{
*outputMsg = hex2dec(toupper(buf[i])) << 4;
*outputMsg += hex2dec(toupper(buf[i+1]));
//printf("%d ", *outputMsg);
outputMsg++;
*outputLen += 1;
}
//printf("\r\n");
return 1;
}
char commandsHistory[BUF_SIZE*MAX_COMMANDS];
char savedCommands = 0;
char selectedCommand = 0;
unsigned char listenMode = 0;
unsigned char listenMode_prev = 0;
unsigned char buf[BUF_SIZE+4];
unsigned char msgBuf[BUF_SIZE+4];
unsigned char gpibBuf[GPIB_BUF_SIZE];
void setup()
{
}
void loop()
{
unsigned char bufPos = 0;
unsigned char cursorPos = 0;
unsigned char localEcho = 1;
unsigned char c;
int i;
unsigned char gpibIndex = 0;
unsigned char command = 0;
int result = 0;
unsigned char msgLen = 0;
unsigned char msgEOI = 1;
GPIO_init();
#if 1
// initialize Timer1
noInterrupts(); // disable all interrupts
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 64910; // preload timer 65536-16MHz/256/100Hz
TCCR1B |= (1 << CS12); // 256 prescaler
TIMSK1 |= (1 << TOIE1); // enable timer overflow interrupt
interrupts(); // enable all interrupts
#endif
ReconfigureGPIO_GPIBNormalMode();
UART_init();
fdevopen(uart_putchar, NULL);
#if 1
if (0 == (PINB & (1<<4))) // printer mode
{
ledBlinking = SLOW;
ReconfigureGPIO_GPIBReceiveMode();
delay(1);
while (1)
{
result = GPIB_Receive(gpibBuf, GPIB_BUF_SIZE-2, &gpibIndex);
if (gpibIndex != 0)
{
for (i=0; i<gpibIndex; i++)
UART_transmit(gpibBuf[i]);
}
else
{
delay(10);
}
}
}
//localEcho = (PINB & _BV(PB7))?1:0;
#endif
while (1) //main loop
{
selectedCommand = savedCommands;
if (localEcho && !bufPos)
printf("<GPIB> ");
do
{
while (!UARTDataAvailable());
c = UART_receive();
if (0x08 == c) //backspace
{
if ((bufPos > 0) && (cursorPos == bufPos))
{
--bufPos;
--cursorPos;
if (localEcho)
{
UART_transmit(0x08);
UART_transmit(' ');
UART_transmit(0x08);
}
}
else if ((bufPos > 0) && (cursorPos > 0))
{
--bufPos;
--cursorPos;
memmove(&buf[cursorPos], &buf[cursorPos+1], bufPos-cursorPos);
if (localEcho)
{
UART_transmit(0x08);
buf[bufPos] = 0;
printf("%s ", &buf[cursorPos]);
for (i=cursorPos; i<(bufPos+1); i++)
UART_transmit(0x08);
}
}
}
else if (10 == c) //ignore LF
{
}
/* else if (9 == c) //tab key
{
printf("<bufPos=%d cursorPos=%d>", bufPos, cursorPos);
}
*/ else if (0x1b == c) //escape character
{
switch (UART_RcvEscapeSeq())
{
case ESC_KEY_UP:
selectedCommand = selectedCommand?selectedCommand-1:0;
memmove(&buf[0], &commandsHistory[selectedCommand*BUF_SIZE], BUF_SIZE);
if (localEcho)
{
while (cursorPos < bufPos)
{
UART_transmit(' ');
cursorPos++;
}
while (bufPos--)
{
UART_transmit(0x08);
UART_transmit(' ');
UART_transmit(0x08);
}
printf("%s", &buf[0]);
}
bufPos = strlen((char*)&buf[0]);
cursorPos = bufPos;
break;
case ESC_KEY_DOWN:
if ((selectedCommand+1) == savedCommands) //current command is last command in buffer
{
selectedCommand = savedCommands;
if (localEcho)
{
while (cursorPos < bufPos)
{
UART_transmit(' ');
cursorPos++;
}
while (bufPos--)
{
UART_transmit(0x08);
UART_transmit(' ');
UART_transmit(0x08);
}
}
bufPos = 0;
cursorPos = 0;
}
else if ((selectedCommand+1) < savedCommands) // <MAX_COMMANDS
{
selectedCommand++;
memmove(&buf[0], &commandsHistory[selectedCommand*BUF_SIZE], BUF_SIZE);
if (localEcho)
{
while (cursorPos < bufPos)
{
UART_transmit(' ');
cursorPos++;
}
while (bufPos--)
{
UART_transmit(0x08);
UART_transmit(' ');
UART_transmit(0x08);
}
printf("%s", &buf[0]);
}
bufPos = strlen((char*)&buf[0]);
cursorPos = bufPos;
}
break;
case ESC_KEY_LEFT:
if (cursorPos)
{
--cursorPos;
if (localEcho)
{
UART_transmit(0x1B);
UART_transmit(0x5B);
UART_transmit('D');
}
}
break;
case ESC_KEY_RIGHT:
if (cursorPos < bufPos)
{
cursorPos++;
if (localEcho)
{
UART_transmit(0x1B);
UART_transmit(0x5B);
UART_transmit('C');
}
}
break;
default:
break;
}
}
else if (13 == c)
{
if (localEcho)
{
UART_transmit(13); //CR
UART_transmit(10); //LF
}
if (bufPos)
command = toupper(buf[0]);
else
command = EMPTY_LINE;
}
else
{
if (bufPos < BUF_SIZE-1)
{
if (cursorPos == bufPos)
{
buf[bufPos++] = c;
cursorPos++;
if (localEcho)
UART_transmit(c); //local echo
}
else
{
memmove(&buf[cursorPos+1], &buf[cursorPos], bufPos-cursorPos);
buf[cursorPos++] = c;
bufPos++;
buf[bufPos] = 0;
if (localEcho)
{
UART_transmit(c); //local echo
printf("%s", &buf[cursorPos]);
for (i=cursorPos; i<bufPos; i++)
UART_transmit(0x08);
}
}
}
}
} while (!command);
if ('D' == command) //send data
{
if (!listenMode)
{
if (1 == msgEndSeq)
buf[bufPos++] = 13; //CR
else if (2==msgEndSeq)
buf[bufPos++] = 10; //LF
else if (3==msgEndSeq)
{
buf[bufPos++] = 13; //CR
buf[bufPos++] = 10; //LF
}
result = GPIB_Transmit(buf+1, bufPos-1, 1);
if (result == 255) // transmit ok
printf("OK\r\n");
else //timeout
printf("TIMEOUT\r\n");
if ((1==msgEndSeq) || (2==msgEndSeq))
--bufPos;
else if (3==msgEndSeq)
bufPos -= 2;
}
else
printf("ERROR\r\n");
}
else if ('M' == command) //send data without EOI
{
if (!listenMode)
{
if (1 == msgEndSeq)
buf[bufPos++] = 13; //CR
else if (2==msgEndSeq)
buf[bufPos++] = 10; //LF
else if (3==msgEndSeq)
{
buf[bufPos++] = 13; //CR
buf[bufPos++] = 10; //LF
}
result = GPIB_Transmit(buf+1, bufPos-1, 0);
if (result == 255) // transmit ok
printf("OK\r\n");
else //timeout
printf("TIMEOUT\r\n");
if ((1==msgEndSeq) || (2==msgEndSeq))
--bufPos;
else if (3==msgEndSeq)
bufPos -= 2;
}
else
printf("ERROR\r\n");
}
else if ('C' == command) //send command
{
for (i=1; i<bufPos; i++)
{
if ((buf[i] == '?') || (buf[i] == (64+listenAddress)))//unlisten
{
listenMode = 0;
ledBlinking = OFF;
SetLed(1);
}
else if (buf[i] == (32+listenAddress))
{
listenMode = 1;
ledBlinking = FAST;
}
}
if (1 == msgEndSeq)
buf[bufPos++] = 13; //CR
else if (2==msgEndSeq)
buf[bufPos++] = 10; //LF
else if (3==msgEndSeq)
{
buf[bufPos++] = 13; //CR
buf[bufPos++] = 10; //LF
}
ReconfigureGPIO_GPIBNormalMode();
SetATN(0);
delayMicroseconds(100);
result = GPIB_Transmit(buf+1, bufPos-1, 1);
if (result == 255) // transmit ok
printf("OK\r\n");
else //timeout
printf("TIMEOUT\r\n");
SetATN(1);
if ((1==msgEndSeq) || (2==msgEndSeq))
--bufPos;
else if (3==msgEndSeq)
bufPos -= 2;
if (listenMode)
ReconfigureGPIO_GPIBReceiveMode();
else
ReconfigureGPIO_GPIBNormalMode();
listenMode_prev = listenMode;
}
else if ('R' == command)
{
SetREN(0);
remoteState = 1;
printf("OK\r\n");
}
else if ('L' == command)
{
SetREN(1);
remoteState = 0;
printf("OK\r\n");
}
else if ('I' == command)
{
SetIFC(0);
delay(1);
SetIFC(1);
printf("OK\r\n");
}
else if ('S' == command)
{
UART_transmit(remoteState?'1':'0');
UART_transmit((0 == (PINC & SRQ))?'1':'0');
UART_transmit(listenMode?'1':'0');
UART_transmit(13);
UART_transmit(10);
}
else if ('P' == command)
{
listenMode_prev = 0; //cancel listen mode
listenMode = 0; //cancel listen mode
ledBlinking = SLOW;
ReconfigureGPIO_GPIBReceiveMode();
// if (localEcho)
// printf("PRINTER MODE, send <ESC> to return to normal mode\r\n");
delay(1);
while (c != 27)
{
if (UARTDataAvailable())
c = UART_receive();
result = GPIB_Receive(gpibBuf, GPIB_BUF_SIZE-2, &gpibIndex);
if (gpibIndex != 0)
{
for (i=0; i<gpibIndex; i++)
UART_transmit(gpibBuf[i]);
}
else
{
delay(10);
}
}
c = 0;
ReconfigureGPIO_GPIBNormalMode();
ledBlinking = OFF;
SetLed(1);
}
else if ('X' == command) //ascii receive
{
if (!listenMode)
{
ReconfigureGPIO_GPIBReceiveMode();
delay(1);
}
result = GPIB_Receive_till_eoi(gpibBuf, GPIB_BUF_SIZE-2, &gpibIndex);
if (gpibIndex != 0)
{
gpibBuf[gpibIndex] = 0;
printf("%s",gpibBuf);
}
else
printf("TIMEOUT\r\n");
if (!listenMode)
ReconfigureGPIO_GPIBNormalMode();
}
else if ('Y' == command) //binary receive
{
if (!listenMode)
{
ReconfigureGPIO_GPIBReceiveMode();
delay(1);
}
result = GPIB_Receive_till_eoi(gpibBuf, GPIB_BUF_SIZE-2, &gpibIndex);
UART_transmit(gpibIndex);
for (i=0; i<gpibIndex; i++)
{
UART_transmit(gpibBuf[i]);
}
if (!listenMode)
ReconfigureGPIO_GPIBNormalMode();
}
else if ('Z' == command) //hex receive
{
if (!listenMode)
{
ReconfigureGPIO_GPIBReceiveMode();
delay(1);
}
result = GPIB_Receive_till_eoi(gpibBuf, GPIB_BUF_SIZE-2, &gpibIndex);
printf("%02x", gpibIndex);
for (i=0; i<gpibIndex; i++)
printf("%02x",gpibBuf[i]);
printf("\r\n");
if (!listenMode)
ReconfigureGPIO_GPIBNormalMode();