-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpib_usb.c
1981 lines (1619 loc) · 61.6 KB
/
gpib_usb.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
/*****************************************************************************
Firmware for Galvant Industries GPIBUSB Adapter Revision 3 & 4
Copyright (C) 2019 Steve Matos
GPIBUSB adapter hardware designed by Steven Casagrande ([email protected])
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 <https://www.gnu.org/licenses/>.
This code requires the CCS compiler from <https://www.ccsinfo.com/> to compile.
A pre-compiled hex file is included at
<https://github.com/steve1515/gpibusb-firmware>
*****************************************************************************/
#include <18F4520.h>
#fuses HS, NOPROTECT, NOLVP, WDT, WDT4096
#use standard_io(all)
#use delay(clock=18432000)
#use rs232(baud=460800,uart1)
#case
#ignore_warnings 204 // Ignore 'Condition always FALSE' warning for debug_printf() and eot_printf()
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include "gpib_usb.h"
//#define VERBOSE_DEBUG
#define VERSION_MAJOR 6
#define VERSION_MINOR_A 0
#define VERSION_MINOR_B 0
// The EEPROM version code occupies the first byte in EEPROM. If the code in
// EEPROM differs from the string defined below, then the default values for
// all EEPROM configuration values along with the defined version string will
// be written to EEPROM on startup.
#define EEPROM_VERSION_CODE 0xA1
#define CR 0x0d // Carriage Return
#define LF 0x0a // Line Feed
#define ESC 0x1b // Escape
#define TAB 0x09 // Tab
#define SP 0x20 // Space
// GPIB Command Bytes (See IEEE 488.1 and IEEE 488.2)
#define GPIB_CMD_GTL 0x01 // Go To Local
#define GPIB_CMD_SDC 0x04 // Selected Device Clear
#define GPIB_CMD_PPC 0x05 // PPC Parallel Poll Configure
#define GPIB_CMD_GET 0x08 // Group Execute Trigger
#define GPIB_CMD_TCT 0x09 // TCT Take Control
#define GPIB_CMD_LLO 0x11 // Local Lockout
#define GPIB_CMD_DCL 0x14 // Device Clear
#define GPIB_CMD_PPU 0x15 // PPU Parallel Poll Unconfigure
#define GPIB_CMD_SPE 0x18 // Serial Poll Enable
#define GPIB_CMD_SPD 0x19 // Serial Poll Disable
#define GPIB_CMD_MLA 0x20 // Device Listen Address (MLA)
#define GPIB_CMD_MTA 0x40 // Device Talk Address (MTA)
#define GPIB_CMD_UNL 0x3f // Unlisten
#define GPIB_CMD_UNT 0x5f // Untalk
#define GPIB_CMD_PPE 0x60 // PPE Parallel Poll Enable
#define GPIB_CMD_PPD 0x70 // PPD Parallel Poll Disable
#define CONTROLLER_ADDR 0 // Controller GPIB address (always zero)
#define MODE_DEVICE 0
#define MODE_CONTROLLER 1
#define EOS_CR_LF 0
#define EOS_CR 1
#define EOS_LF 2
#define EOS_NONE 3
#define READ_TO_TIMEOUT 0
#define READ_TO_EOI 1
#define READ_TO_CHAR 2
// Note: UART receive ring buffer length of 256 allows for easy rollover of indexes.
// Do not change buffer length!
#define BUFFER_LEN 256
uint8_t _ringBuffer[BUFFER_LEN];
uint8_t _recvBuffer[BUFFER_LEN];
volatile uint8_t _ringBufferRead = 0;
volatile uint8_t _ringBufferWrite = 0;
bool _debugMode = false; // True = display user-level debugging messages
uint8_t _gpibMode = MODE_CONTROLLER;
// Address variables represent either the target address in controller mode
// or this device's address in device mode.
// Note: SAD has no effect in device mode.
uint8_t _devicePad = 1; // Device primary address (PAD)
uint8_t _deviceSad = 0; // Device secondary address (SAD)
bool _useDeviceSad = false; // True if device has a secondary address (SAD)
bool _autoRead = true;
bool _useEoi = true;
uint8_t _eosMode = EOS_CR_LF;
bool _eotEnable = true;
char _eotChar = LF;
bool _listenOnlyMode = false;
uint8_t _deviceStatusByte = 0x00;
bool _saveCfgEnable = false;
uint16_t _gpibTimeout = 1000;
uint16_t _mSecTimer = 0;
char _eosBuffer[] = "\r\n";
// Device Mode State Variables
bool _deviceTalk = false; // True = device addressed as talker
bool _deviceListen = false; // True = device addressed as listener
bool _deviceSerialPoll = false; // True = serial poll mode enabled
// Prologix Compatible Command Set
char _cmdAddr[] = "addr"; // ++addr [<PAD> [<SAD>]]
char _cmdAuto[] = "auto"; // ++auto [0|1]
char _cmdClr[] = "clr"; // ++clr
char _cmdEoi[] = "eoi"; // ++eoi [0|1]
char _cmdEos[] = "eos"; // ++eos [0|1|2|3]
char _cmdEotEnable[] = "eot_enable"; // ++eot_enable [0|1]
char _cmdEotChar[] = "eot_char"; // ++eot_char [<char>]
char _cmdIfc[] = "ifc"; // ++ifc
char _cmdLlo[] = "llo"; // ++llo
char _cmdLoc[] = "loc"; // ++loc
char _cmdLon[] = "lon"; // ++lon [0|1]
char _cmdMode[] = "mode"; // ++mode [0|1]
char _cmdReadTmoMs[] = "read_tmo_ms"; // ++read_tmo_ms <time>
char _cmdRead[] = "read"; // ++read [eoi|<char>]
char _cmdRst[] = "rst"; // ++rst
char _cmdSavecfg[] = "savecfg"; // ++savecfg [0|1]
char _cmdSpoll[] = "spoll"; // ++spoll [<PAD> [<SAD>]]
char _cmdSrq[] = "srq"; // ++srq
char _cmdStatus[] = "status"; // ++status [0-255]
char _cmdTrg[] = "trg"; // ++trg [[<PAD1> [<SAD1>]] [<PAD2> [<SAD2>]] ... [<PAD15> [<SAD15>]]]
char _cmdVer[] = "ver"; // ++ver
char _cmdHelp[] = "help"; // ++help
// Additional Commands
char _cmdDebug[] = "debug"; // ++debug [0|1]
#define debug_printf(fmt, ...) do {\
if (_debugMode)\
{\
printf((fmt), ##__VA_ARGS__);\
if (_eotEnable) printf("%c", _eotChar);\
}\
} while (0)
#define eot_printf(fmt, ...) do {\
printf((fmt), ##__VA_ARGS__);\
if (_eotEnable) printf("%c", _eotChar);\
} while (0)
bool buffer_get(uint8_t *buffer);
char* trim_right(char *str);
char* get_address(char *buffer, uint8_t *pad, uint8_t *sad, uint8_t *validSad);
void handle_command(uint8_t *buffer);
void handle_device_mode();
void handle_listen_only_mode();
#inline void update_eeprom(int8_t address, int8_t value);
void eeprom_read_cfg();
void eeprom_write_cfg();
void gpib_init_pins(uint8_t mode);
#inline void gpib_send_ifc();
bool gpib_read_status_byte(uint8_t *statusByte, uint8_t pad, uint8_t sad, bool useSad);
#inline bool gpib_send_command(uint8_t command);
#inline bool gpib_send_data(uint8_t *buffer, uint8_t length, bool useEoi);
bool gpib_send_setup(uint8_t pad, uint8_t sad, bool useSad);
bool gpib_send(uint8_t *buffer, uint8_t length, bool isCommand, bool useEoi);
bool gpib_receive_setup(uint8_t pad, uint8_t sad, bool useSad);
bool gpib_receive_byte(char *buffer, uint8_t *eoiStatus);
void gpib_receive_data(uint8_t readMode, char readToChar);
void main()
{
#ifdef VERBOSE_DEBUG
// Get microcontroller restart cause.
// Note: This must be done before any other registers are modified.
uint8_t restartCause = restart_cause();
switch (restartCause)
{
case WDT_TIMEOUT:
eot_printf("Restart Cause: Watchdog Timeout");
break;
case NORMAL_POWER_UP:
eot_printf("Restart Cause: Normal Power Up");
break;
case MCLR_FROM_RUN:
eot_printf("Restart Cause: Reset Push-button");
break;
case RESET_INSTRUCTION:
eot_printf("Restart Cause: Reset Instruction");
break;
default:
eot_printf("Restart Cause: Other (%u)", restartCause);
break;
}
#endif
// Turn on error LED
output_high(LED_ERROR);
// Setup watchdog timer
setup_wdt(WDT_ON);
// Setup timeout timer
set_rtcc(0);
setup_timer_2(T2_DIV_BY_16, 144, 2); // 1 mSec interrupt
enable_interrupts(GLOBAL);
disable_interrupts(INT_TIMER2);
// Read EEPROM configuration values
eeprom_read_cfg();
// Initialize GPIB bus lines
gpib_init_pins(_gpibMode);
if (_gpibMode == MODE_CONTROLLER)
gpib_send_ifc();
// Delay before enabling RDA interrupt.
// Note: Delaying the enable of the RDA interrupt solves some issues
// on Linux operating systems where the "modemmanager" package
// is installed. The "modemmanager" package appears to cause
// a ~30 second delay where the serial port is unaccessible.
// Blink LED during delay
output_low(LED_ERROR);
restart_wdt(); delay_ms(100);
output_high(LED_ERROR);
restart_wdt(); delay_ms(100);
enable_interrupts(INT_RDA);
restart_wdt();
output_low(LED_ERROR);
// Main Loop
for (;;)
{
restart_wdt();
// Check for data in UART receive buffer and process as required
if (buffer_get(_recvBuffer))
{
// Check if the received data is a controller command sequence (++ command)
// Note: First byte in receive buffer is the control
// command flag (CCF). If CCF == 1, then data is a command.
if (_recvBuffer[0])
{
handle_command(_recvBuffer);
}
else // Not an internal controller command sequence
{
uint8_t dataLen = _recvBuffer[1];
uint8_t *pBuf = _recvBuffer+2;
if (_gpibMode == MODE_CONTROLLER)
{
bool errorStatus = false;
// Address target device and send data
errorStatus = errorStatus || gpib_send_setup(_devicePad, _deviceSad, _useDeviceSad);
errorStatus = errorStatus || gpib_send_data(pBuf, dataLen, _useEoi);
// Automatically read after sending data if auto read mode is enabled
if (_autoRead)
{
errorStatus = errorStatus || gpib_receive_setup(_devicePad, _deviceSad, _useDeviceSad);
if (!errorStatus)
gpib_receive_data(READ_TO_EOI, NULL);
}
}
else // Device mode
{
// Sending data is only allowed when addressed to talk,
// serial poll mode disabled, and ATN deasserted.
// Reference: IEEE 488.1-1987 - Section 2.5.2 T Function State Diagrams
if (_deviceTalk && !_deviceSerialPoll && input(ATN))
gpib_send_data(pBuf, dataLen, _useEoi);
}
}
}
// Handle device mode processing
if (_gpibMode == MODE_DEVICE)
{
if (_listenOnlyMode)
handle_listen_only_mode();
else
handle_device_mode();
}
}
}
#int_timer2
void clock_isr()
{
_mSecTimer++;
}
#int_rda
void RDA_isr()
{
// This interrupt handler takes incoming UART data and fills a ring buffer
// further processing in the main loop.
// Ring Buffer Format
// ==================
// - Read index points to buffer index of next read.
//
// - Write index points to buffer index of next free byte.
//
// - Buffer empty is indicated by Read Index == Write Index.
//
// - Buffer size is 256 bytes and index pointers are unsigned 8-bit values,
// so wrap arounds are automatically handled by binary 8-bit arithmetic.
//
// - When data is available in the ring buffer, the read index points
// to a controller command flag, followed by a data byte length,
// followed by data bytes.
//
// Example (where read pointer points to byte 0):
// | Byte 0 | Byte 1 | Byte 2 | Byte 3 | ... | Byte N |
// | CCF | DLEN | D1 | D2 | ... | DN |
// where...
// CCF = Control Command Flag (1 = Controller Command; 0 = Device Data)
// DLEN = Data Length in Bytes
// D1..DN = Data of size DLEN bytes
// UART Data Notes
// ===============
// - All un-escaped LF (0x0a), CR (0x0d), ESC (0x1b), and '+' characters
// are discarded.
//
// - Any UART input that starts with an un-escaped '++' character sequence
// is interpreted as a controller command and not transmitted over GPIB.
// Do nothing if no data is ready
if (!kbhit())
return;
uint8_t startIndex = _ringBufferWrite;
uint8_t readNum = 0;
uint8_t byteLen = 0;
bool escapeNext = false;
char c;
char c1 = '\0';
char c2 = '\0';
for (;;)
{
// Get character from UART
c = getc();
readNum++;
// Save 1st and 2nd characters received.
// Note: These characters will be used later to determine if the received
// string is a controller command.
if (readNum == 1)
c1 = c;
if (readNum == 2)
c2 = c;
// If the escape flag is not set and an escape character is
// received, set the escape flag for the next character.
// Note: Checking that the escape flag is not set,
// allows escaping of the escape character.
if (!escapeNext && c == ESC)
{
escapeNext = true;
continue;
}
// Discard un-escaped '+' characters
if (!escapeNext && c == '+')
continue;
// Exit loop if un-escaped termination character (CR or LF) is received
if (!escapeNext && (c == CR || c == LF))
break;
// Before adding the first character to the buffer below,
// advance the ring buffer write pointer 2 positions.
// Note: The 1st and 2nd bytes are used for the controller command flag
// and data length size respectively.
if (byteLen == 0)
_ringBufferWrite += 2;
// Add character to buffer (if escaped or a character other then ESC, '+', CR, LF)
_ringBuffer[_ringBufferWrite] = c;
_ringBufferWrite++;
byteLen++;
escapeNext = false;
// If data added to the ring buffer has caused the pointers to become
// equal, then this means the buffer is full and will overflow if more
// bytes are added. We also don't allow the pointers to be equal unless
// the buffer is empty, so this data must be discarded and the write
// pointer must be reset to where it was before data was added to the
// ring buffer.
// Simply put, pointers being equal means empty not full,
// so we must discard the incoming data.
if (_ringBufferRead == _ringBufferWrite)
{
byteLen = 0;
_ringBufferWrite = startIndex;
break;
}
}
// Consume any additional bytes (flush receive buffer)
while (kbhit())
getc();
// Do nothing if no bytes were added to the buffer
if (byteLen == 0)
return;
// Set controller command flag if first two characters received were '++'
_ringBuffer[startIndex] = (c1 == '+' && c2 == '+') ? 0x01 : 0x00;
// Set data byte length
_ringBuffer[(uint8_t)(startIndex + 1)] = byteLen;
}
bool buffer_get(uint8_t *buffer)
{
// This function gets an item from the ring buffer and places it into
// the buffer given.
// It returns true if successful, false otherwise.
// Note: It is assumed that the destination buffer given is the same
// size as the ring buffer. This prevents the data from filling
// the destination buffer completely due to the fact that the
// pointers will only allow the ring buffer to be filled to
// its size minus one (e.g. pointers can never be equal if data
// is in the ring buffer, so there is always at least one free byte).
// This ensures that there will always be enough space to add a null
// terminator on the end of the received data.
// Return false if buffer is empty
if (_ringBufferRead == _ringBufferWrite)
return false;
// Get byte length of data to copy in input buffer (CCF + DLEN + Data)
uint8_t byteLen = _ringBuffer[(uint8_t)(_ringBufferRead + 1)] + 2;
// Zero destination buffer
// Note: This allows string read functions to work since any data
// copied into the destination buffer will be null terminated.
memset(buffer, 0, BUFFER_LEN);
// Check if the data to be read will wrap around the
// ring buffer, and perform two copies if required.
if (((uint16_t)_ringBufferRead + byteLen) > BUFFER_LEN)
{
uint8_t readLen1 = BUFFER_LEN - _ringBufferRead;
uint8_t readLen2 = byteLen - readLen1;
memcpy(buffer, &_ringBuffer[_ringBufferRead], readLen1);
memcpy(&buffer[readLen1], _ringBuffer, readLen2);
}
else
{
memcpy(buffer, &_ringBuffer[_ringBufferRead], byteLen);
}
// Update the read pointer
_ringBufferRead += byteLen;
return true;
}
char* trim_right(char *str)
{
// This function trims whitespace from the right side of the given string.
// Note: The given string is modified in place.
uint8_t len, i;
len = strlen(str);
if (len < 1)
return str;
// Trim trailing spaces and tabs
i = strlen(str) - 1;
while (i >= 0 && (str[i] == SP || str[i] == TAB))
{
str[i] = '\0';
i--;
}
return str;
}
char* get_address(char *buffer, uint8_t *pad, uint8_t *sad, uint8_t *validSad)
{
// This function returns the next PAD and SAD if available from the given string.
//
// Parameters:
// [in] buffer: Buffer containing string to search for PAD and SAD
// [out] pad: Primary address (PAD) of device [Valid Range = 1-30]
// [out] sad: Secondary address (SAD) of device [Valid Range = 0-30]
// [out] validSad: 1 = SAD was found
//
// Return Value: Pointer to next PAD in buffer, otherwise NULL (See Notes)
//
// Notes:
// 1. The input buffer must be NULL terminated.
// 2. The input buffer may contain multiple PADs or PAD/SAD combinations.
// 3. If the input buffer contains multiple PAD/SADs, each value must be
// separated by one or more space characters (0x20).
// 4. If an invalid PAD is encountered, NULL will be returned and
// pad will be set to zero.
// 5. If an invalid SAD is encountered, NULL will be returned and
// validSad will be set to 0.
// Initialize output values
*pad = 0;
*sad = 0;
*validSad = 0;
char *pBuf = buffer;
uint8_t value;
// Consume any leading spaces
while (*pBuf == SP)
pBuf++;
// If no following characters are found (end of string), return NULL
if (pBuf == NULL)
return NULL;
// Get PAD
value = atoi(pBuf);
// If PAD is not valid, return NULL
if (value < 1 || value > 30)
return NULL;
// Valid PAD found
*pad = value;
// Search for next space character
pBuf = strchr(pBuf, SP);
// If no following space character is found, return NULL
if (pBuf == NULL)
return NULL;
// Consume any leading spaces
while (*pBuf == SP)
pBuf++;
// If no following characters are found (end of string), return NULL
if (pBuf == NULL)
return NULL;
// Get next value
// Note: This could be a PAD or a SAD
value = atoi(pBuf);
// If value is PAD (1-30), return pointer (no SAD found)
if (value >=1 && value <= 30)
return pBuf;
// If value is not a valid SAD, return NULL
if (value < 96 || value > 126)
return NULL;
// Valid SAD found
// Note: User enters 96-126 to indicate SAD of 0-30, so 0x60 is subtracted
// before internally storing the value as 0-30.
*sad = value - 0x60;
*validSad = 1;
// Search for next space character
pBuf = strchr(pBuf, SP);
// If no following space character is found, return NULL
if (pBuf == NULL)
return NULL;
// Consume any leading spaces
while (*pBuf == SP)
pBuf++;
// Return pointer
// Note: This may be a NULL if end of string was reached.
return pBuf;
}
void handle_command(uint8_t *buffer)
{
// This function handles a controller command sequence (++ command).
//
// Parameters:
// [in] buffer: Byte buffer containing a command sequence
// Verify that the CCF flag is set and data length > 0
if (!buffer[0] || buffer[1] < 1)
return;
// Get a pointer to the data section of the buffer
char *pBuf = trim_right(&buffer[2]);
#ifdef VERBOSE_DEBUG
eot_printf("Trimmed Command String: '%s'", pBuf);
#endif
// ++addr [<PAD> [<SAD>]]
if (!strncmp(pBuf, _cmdAddr, 4))
{
if (*(pBuf+4) == '\0') // Query current address
{
if (_useDeviceSad)
eot_printf("%u %u", _devicePad, _deviceSad + 0x60);
else
eot_printf("%u", _devicePad);
}
else if (*(pBuf+4) == SP) // Set address
{
uint8_t pad, sad, validSad;
get_address(pBuf+5, &pad, &sad, &validSad);
// If PAD was found valid, update address variables
if (pad > 0)
{
_devicePad = pad;
_deviceSad = sad;
_useDeviceSad = validSad;
if (_saveCfgEnable)
eeprom_write_cfg();
}
}
}
// ++auto [0|1]
else if (_gpibMode == MODE_CONTROLLER && !strncmp(pBuf, _cmdAuto, 4))
{
if (*(pBuf+4) == '\0') // Query current auto read mode
{
eot_printf("%u", _autoRead);
}
else if (*(pBuf+4) == SP) // Set auto read mode
{
_autoRead = atoi(pBuf+5) > 0;
if (_saveCfgEnable)
eeprom_write_cfg();
}
}
// ++clr
else if (_gpibMode == MODE_CONTROLLER && !strncmp(pBuf, _cmdClr, 3))
{
bool errorStatus = false;
errorStatus = errorStatus || gpib_send_setup(_devicePad, _deviceSad, _useDeviceSad);
errorStatus = errorStatus || gpib_send_command(GPIB_CMD_SDC);
}
// ++eoi [0|1]
else if (!strncmp(pBuf, _cmdEoi, 3))
{
if (*(pBuf+3) == '\0') // Query current EOI mode
{
eot_printf("%u", _useEoi);
}
else if (*(pBuf+3) == SP) // Set EOI mode
{
_useEoi = atoi(pBuf+4) > 0;
if (_saveCfgEnable)
eeprom_write_cfg();
}
}
// ++eos [0|1|2|3]
else if (!strncmp(pBuf, _cmdEos, 3))
{
if (*(pBuf+3) == '\0') // Query current EOS mode
{
eot_printf("%u", _eosMode);
}
else if (*(pBuf+3) == SP) // Set EOS mode
{
uint8_t value = atoi(pBuf+4);
// Only accept valid values
if (value >= 0 && value <= 3)
{
_eosMode = value;
if (_saveCfgEnable)
eeprom_write_cfg();
}
}
}
// ++eot_enable [0|1]
else if (!strncmp(pBuf, _cmdEotEnable, 10))
{
if (*(pBuf+10) == '\0') // Query current EOT mode
{
eot_printf("%u", _eotEnable);
}
else if (*(pBuf+10) == SP) // Set EOT mode
{
_eotEnable = atoi(pBuf+11) > 0;
if (_saveCfgEnable)
eeprom_write_cfg();
}
}
// ++eot_char [<char>]
else if (!strncmp(pBuf, _cmdEotChar, 8))
{
if (*(pBuf+8) == '\0') // Query current EOT character
{
eot_printf("%u", _eotChar);
}
else if (*(pBuf+8) == SP) // Set EOT character
{
_eotChar = atoi(pBuf+9);
if (_saveCfgEnable)
eeprom_write_cfg();
}
}
// ++ifc
else if (_gpibMode == MODE_CONTROLLER && !strncmp(pBuf, _cmdIfc, 3))
{
gpib_send_ifc();
}
// ++llo
else if (_gpibMode == MODE_CONTROLLER && !strncmp(pBuf, _cmdLlo, 3))
{
bool errorStatus = false;
errorStatus = errorStatus || gpib_send_setup(_devicePad, _deviceSad, _useDeviceSad);
errorStatus = errorStatus || gpib_send_command(GPIB_CMD_LLO);
}
// ++loc
else if (_gpibMode == MODE_CONTROLLER && !strncmp(pBuf, _cmdLoc, 3))
{
bool errorStatus = false;
errorStatus = errorStatus || gpib_send_setup(_devicePad, _deviceSad, _useDeviceSad);
errorStatus = errorStatus || gpib_send_command(GPIB_CMD_GTL);
}
// ++lon [0|1]
else if (_gpibMode == MODE_DEVICE && !strncmp(pBuf, _cmdLon, 3))
{
if (*(pBuf+3) == '\0') // Query current listen only mode
eot_printf("%u", _listenOnlyMode);
else if (*(pBuf+3) == SP) // Set listen only mode
_listenOnlyMode = atoi(pBuf+4) > 0;
}
// ++mode [0|1]
else if (!strncmp(pBuf, _cmdMode, 4))
{
if (*(pBuf+4) == '\0') // Query current mode
{
eot_printf("%u", _gpibMode);
}
else if (*(pBuf+4) == SP) // Set mode
{
uint8_t value = atoi(pBuf+5);
// Set new mode only if mode is changed and in valid range
if (_gpibMode != value && value >= 0 && value <= 1)
{
_gpibMode = value;
gpib_init_pins(_gpibMode);
_listenOnlyMode = false;
_deviceTalk = false;
_deviceListen = false;
_deviceSerialPoll = false;
_deviceStatusByte = 0x00;
if (_gpibMode == MODE_CONTROLLER)
gpib_send_ifc();
if (_saveCfgEnable)
eeprom_write_cfg();
}
}
}
// Note: The processing of '++read_tmo_ms' must come before '++read' or
// else it will never get processed.
// ++read_tmo_ms <time>
else if (!strncmp(pBuf, _cmdReadTmoMs, 11))
{
if (*(pBuf+11) == '\0') // Query current timeout
{
eot_printf("%lu", _gpibTimeout);
}
else if (*(pBuf+11) == SP) // Set timeout
{
uint32_t value = atoi32(pBuf+12);
// Only accept valid values
if (value >= 0 && value <= 3000)
{
_gpibTimeout = (uint16_t)value;
if (_saveCfgEnable)
eeprom_write_cfg();
}
}
}
// ++read [eoi|<char>]
else if (_gpibMode == MODE_CONTROLLER && !strncmp(pBuf, _cmdRead, 4))
{
if (*(pBuf+4) == '\0') // Read until timeout
{
if (!gpib_receive_setup(_devicePad, _deviceSad, _useDeviceSad))
gpib_receive_data(READ_TO_TIMEOUT, NULL);
}
else if (*(pBuf+4) == SP
&& *(pBuf+5) == 'e' && *(pBuf+6) == 'o' && *(pBuf+7) == 'i') // Read until EOI (or timeout)
{
if (!gpib_receive_setup(_devicePad, _deviceSad, _useDeviceSad))
gpib_receive_data(READ_TO_EOI, NULL);
}
else if (*(pBuf+4) == SP) // Read until character (or timeout)
{
char c = atoi(pBuf+5);
if (!gpib_receive_setup(_devicePad, _deviceSad, _useDeviceSad))
gpib_receive_data(READ_TO_CHAR, c);
}
}
// ++rst
else if (!strncmp(pBuf, _cmdRst, 3))
{
delay_ms(1);
reset_cpu();
}
// ++savecfg [0|1]
else if (!strncmp(pBuf, _cmdSavecfg, 7))
{
if (*(pBuf+7) == '\0') // Query current save configuration mode
{
eot_printf("%u", _saveCfgEnable);
}
else if (*(pBuf+7) == SP) // Set save configuration mode
{
_saveCfgEnable = atoi(pBuf+8) > 0;
// Save immediately when "++savecfg 1" is received
if (_saveCfgEnable)
eeprom_write_cfg();
}
}
// ++spoll [<PAD> [<SAD>]]
else if (_gpibMode == MODE_CONTROLLER && !strncmp(pBuf, _cmdSpoll, 5))
{
if (*(pBuf+5) == '\0') // Serial poll currently addressed device
{
uint8_t statusByte = 0x00;
if (!gpib_read_status_byte(&statusByte, _devicePad, _deviceSad, _useDeviceSad))
putc(statusByte);
}
else if (*(pBuf+5) == SP) // Serial poll specified device address
{
uint8_t pad, sad, validSad;
uint8_t statusByte = 0x00;
get_address(pBuf+6, &pad, &sad, &validSad);
if (pad > 0 && !gpib_read_status_byte(&statusByte, pad, sad, validSad))
putc(statusByte);
}
}
// ++srq
else if (_gpibMode == MODE_CONTROLLER && !strncmp(pBuf, _cmdSrq, 3))
{
eot_printf("%u", !input(SRQ));
}
// ++status [0-255]
else if (_gpibMode == MODE_DEVICE && !strncmp(pBuf, _cmdStatus, 6))
{
if (*(pBuf+6) == '\0') // Query current status byte
{
eot_printf("%u", _deviceStatusByte);
}
else if (*(pBuf+6) == SP) // Set status byte
{
_deviceStatusByte = atoi(pBuf+7);
// When RQS (bit 6) is set, assert SRQ
if (_deviceStatusByte & 0x40)
output_low(SRQ);
else
output_high(SRQ);
}
}
// ++trg [[<PAD1> [<SAD1>]] [<PAD2> [<SAD2>]] ... [<PAD15> [<SAD15>]]]
else if (_gpibMode == MODE_CONTROLLER && !strncmp(pBuf, _cmdTrg, 3))
{
if (*(pBuf+3) == '\0') // Send GPIB GET to currently addressed device
{
bool errorStatus = false;
errorStatus = errorStatus || gpib_send_setup(_devicePad, _deviceSad, _useDeviceSad);
errorStatus = errorStatus || gpib_send_command(GPIB_CMD_GET);
}
else if (*(pBuf+3) == SP) // Send GPIB GET to specified device addresses
{
uint8_t pad, sad, validSad;
bool errorStatus = false;
pBuf = pBuf+4;
// Send to a maximum of 15 device addresses
for (uint8_t i = 0; i < 15; i++)
{
restart_wdt();
pBuf = get_address(pBuf, &pad, &sad, &validSad);
// Exit loop if invalid PAD was found
if (pad < 1)
break;
errorStatus = false;
errorStatus = errorStatus || gpib_send_setup(pad, sad, validSad);
errorStatus = errorStatus || gpib_send_command(GPIB_CMD_GET);
// Exit loop if no more addresses were given
if (pBuf == NULL)
break;
}
}
}
// ++ver
else if (!strncmp(pBuf, _cmdVer, 3))
{
eot_printf("GPIB-USB Version %u.%u%u",
VERSION_MAJOR, VERSION_MINOR_A, VERSION_MINOR_B);
}
// ++help
else if (!strncmp(pBuf, _cmdHelp, 4))
{
eot_printf("Documentation: https://github.com/steve1515/gpibusb-firmware");
}