forked from MDSplus/mdsplus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hpe1564.c
10323 lines (8931 loc) · 359 KB
/
hpe1564.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
/*
Copyright (c) 2017, Massachusetts Institute of Technology All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* ONLY one of these can be defined */
#define REGB_DRIVER
#if defined(REGB_DRIVER) && defined(MSGB_DRIVER)
#error Must define only ONE of REGB_DRIVER or MSGB_DRIVER
#endif
#if !defined(REGB_DRIVER) && !defined(MSGB_DRIVER)
#error MUST have either REGB_DRIVER or MSGB_DRIVER defined!
#endif
/* _HPUX_SOURCE and _XOPEN_SOURCE need to be defined before any standard
include if they are to work correctly
They provide the path through the #ifdefs in standard include files
that extends the ANSI C ifdefs as needed for driver writing.
*/
#ifdef __hpux
#define _HPUX_SOURCE
#define _XOPEN_SOURCE
#endif
#include <stdlib.h> /* prototype for malloc() */
#include <string.h> /* prototype for strcpy() */
#include <stdio.h> /* prototype for sprintf() */
#include <stdarg.h> /* variable arg list for tracing */
#include <sys/timeb.h> /* prototype for ftime() */
#include "visa.h"
#if defined _WIN32 || defined __WIN32__
#include <windows.h>
#define hpe1564_DECLARE_TIME_LOCALS SYSTEMTIME st;
#define hpe1564_GET_TIME GetLocalTime(&st);
#define hpe1564_TIME_FIELDS st.wMonth, st.wDay, st.wHour, \
st.wMinute, st.wSecond, st.wMilliseconds
#else /* not win32 */
#include <time.h> /* standard time functions */
#ifdef __hpux
#define DWORD int
#include <sys/time.h>
#define hpe1564_DECLARE_TIME_LOCALS struct timeval tv; \
struct timezone tz; \
struct tm *tmp;
#define hpe1564_GET_TIME gettimeofday(&tv, &tz); \
tmp = localtime((time_t*)&tv.tv_sec);
#define hpe1564_TIME_FIELDS tmp->tm_mon+1, tmp->tm_mday, tmp->tm_hour, \
tmp->tm_min, tmp->tm_sec, tv.tv_usec/1000
#else /* not unix, use ANSI time function */
#define hpe1564_DECLARE_TIME_LOCALS struct tm *tmp; time_t seconds;
#define hpe1564_GET_TIME time(&seconds); \
tmp = localtime(&seconds);
#define hpe1564_TIME_FIELDS tmp->tm_mon+1, tmp->tm_mday, tmp->tm_hour, \
tmp->tm_min, tmp->tm_sec, 0
#endif /* ifdef __hpux */
#endif /* ifdef _WIN32 */
#define INSTR_CALLBACKS /* needed for handler prototypes in hpe1564.h */
#undef _VI_FUNC
#define _VI_FUNC
#include "hpe1564.h"
#include "core.h"
#define hpe1564_MODEL2_CODE 614 /* 2 channel DAC */
#define hpe1564_MODEL4_CODE 615 /* 4 channel DAC */
#define hpe1564_MANF_ID 4095
#define hpe1564_IDN_STRING4 "HEWLETT-PACKARD,E1564A"
#define hpe1564_IDN_STRING2 "HEWLETT-PACKARD,E1563A"
#define hpe1564_REV_CODE "A.02.02" /* Driver Revision */
#define hpe1564_ERR_MSG_LENGTH 256 /* size of error message buffer */
#define hpe1564_MAX_STAT_REG 4 /* number of IEEE 488.2 status registers */
/* this has to match the index of the ESR register in hpe1564_accessInfo[] */
#define hpe1564_ESR_REG_IDX 2
/******************************************************
* Callback events
******************************************************
*/
ViInt32 hpe1564_statusHap[hpe1564_MAX_STAT_HAP] = {
hpe1564_USER_ERROR_HANDLER,
hpe1564_INSTR_ERROR_HANDLER,
hpe1564_VOLT_OVLD,
hpe1564_LIM1_FAIL,
hpe1564_LIM2_FAIL,
hpe1564_LIM3_FAIL,
hpe1564_LIM4_FAIL,
hpe1564_GOT_TRIG,
hpe1564_PRETRIG_DONE,
hpe1564_MEAS_DONE
};
/* Assumes we have driver copies of the event register. This is needed
* because in IEEE 488.2, the event register are cleared after they are
* read. Since the event register contains several events, we need to
* keep this information around to pass back to the user.
*/
struct hpe1564_statusAccess {
ViInt32 registerIdx;
ViString condQry;
ViString eventQry;
ViString enableCmd;
};
const struct hpe1564_statusAccess hpe1564_accessInfo[hpe1564_MAX_STAT_REG] = {
{0, "", "*STB?", "*SRE"},
{400, "STAT:QUES:COND?", "STAT:QUES:EVEN?", "STAT:QUES:ENAB"},
{600, "", "*ESR?", "*ESE"},
{800, "STAT:OPER:COND?", "STAT:OPER:EVEN?", "STAT:OPER:ENAB"}
};
/* this will return the index associated with the happening */
ViBoolean hpe1564_findHappeningIdx(ViInt32 happening, ViPInt32 pIdx)
{
/* Note: this is a linear search, for faster access this
* could be done as a binary search since the data is arrange
* in order numerically.
*/
for (*pIdx = 0; *pIdx < hpe1564_MAX_STAT_HAP; *pIdx = *pIdx + 1) {
if (hpe1564_statusHap[*pIdx] == happening) {
return VI_TRUE;
}
}
return VI_FALSE;
}
/* this will return the index that corresponds with regNum */
static ViBoolean hpe1564_findAccessIdx(ViInt32 regNum, ViPInt32 pIdx)
{
for (*pIdx = 0; *pIdx < hpe1564_MAX_STAT_REG; *pIdx = *pIdx + 1) {
if (regNum == hpe1564_accessInfo[*pIdx].registerIdx) {
return VI_TRUE;
}
}
return VI_FALSE;
}
/* you can remove the SWAP's below if you do not have block IO (arbitrary block)
if you do then you must also remove the routines that use them (_cmd_arr functions)
*/
#define SWAP_FLOAT64(dest) \
{ unsigned char src[8]; \
*((double *)src) = *((double *)dest); \
((unsigned char *)(dest))[0] = ((unsigned char*)(src))[7]; \
((unsigned char *)(dest))[1] = ((unsigned char*)(src))[6]; \
((unsigned char *)(dest))[2] = ((unsigned char*)(src))[5]; \
((unsigned char *)(dest))[3] = ((unsigned char*)(src))[4]; \
((unsigned char *)(dest))[4] = ((unsigned char*)(src))[3]; \
((unsigned char *)(dest))[5] = ((unsigned char*)(src))[2]; \
((unsigned char *)(dest))[6] = ((unsigned char*)(src))[1]; \
((unsigned char *)(dest))[7] = ((unsigned char*)(src))[0]; \
}
#define SWAP_32(dest) \
{ unsigned char src[4]; \
*((long *)src) = *((long *)dest); \
((unsigned char *)(dest))[0] = ((unsigned char*)(src))[3]; \
((unsigned char *)(dest))[1] = ((unsigned char*)(src))[2]; \
((unsigned char *)(dest))[2] = ((unsigned char*)(src))[1]; \
((unsigned char *)(dest))[3] = ((unsigned char*)(src))[0]; \
}
#define SWAP_16(dest) \
{ unsigned char src[2]; \
*((ViInt16 *)src) = *((ViInt16 *)dest); \
((unsigned char *)(dest))[0] = ((unsigned char*)(src))[1]; \
((unsigned char *)(dest))[1] = ((unsigned char*)(src))[0]; \
}
/*===============================================================
*
* All messages are stored in this area to aid in localization
*
*===============================================================
*/
#define hpe1564_MSG_VI_OPEN_ERR \
"vi was zero. Was the hpe1564_init() successful?"
#define hpe1564_MSG_CONDITION \
"condition"
/* hpe1564_statCond_Q() */
#define hpe1564_MSG_EVENT \
"event"
/* hpe1564_statEvent_Q() */
#define hpe1564_MSG_EVENT_HDLR_INSTALLED \
"event handler is already installed for event happening"
/* hpe1564_statEvent_Q() */
#define hpe1564_MSG_EVENT_HDLR_INST2 \
"Only 1 handler can be installed at a time."
/* hpe1564_statEvent_Q() */
#define hpe1564_MSG_INVALID_HAPPENING \
"is not a valid happening."
/* hpe1564_statCond_Q() */
/* hpe1564_statEven_Q() */
/* hpe1564_statEvenHdlr() */
/* hpe1564_statEvenHdlr_Q() */
#define hpe1564_MSG_NOT_QUERIABLE \
"is not queriable."
/* hpe1564_statCond_Q() */
/* hpe1564_statEven_Q() */
#define hpe1564_MSG_IN_FUNCTION \
"in function"
/* hpe1564_error_message() */
#define hpe1564_MSG_INVALID_STATUS \
"Parameter 2 is invalid" \
"in function hpe1564_error_message()."
/* hpe1564_error_message() */
#define hpe1564_MSG_INVALID_STATUS_VALUE \
"is not a valid viStatus value."
/* hpe1564_error_message() */
#define hpe1564_MSG_INVALID_VI \
"Parameter 1 is invalid" \
" in function hpe1564_error_message()" \
". Using an inactive ViSession may cause this error." \
" Was the instrument driver closed prematurely?"
/* hpe1564_message_query() */
#define hpe1564_MSG_NO_ERRORS \
"No Errors"
/* hpe1564_error_message() */
#define hpe1564_MSG_SELF_TEST_FAILED \
"Self test failed."
/* hpe1564_self_test() */
#define hpe1564_MSG_SELF_TEST_PASSED \
"Self test passed."
/* hpe1564_self_test() */
#define hpe1564_MSG_WARN_SELF_TEST \
"Power-on self test failed, run full self test."
/* hpe1564_self_test() */
/* the following messages are used by the functions to check parameters */
#define hpe1564_MSG_BOOLEAN "Expected 0 or 1; Got %d"
#define hpe1564_MSG_REAL "Expected %lg to %lg; Got %lg"
#define hpe1564_MSG_INT "Expected %hd to %hd; Got %hd"
#define hpe1564_MSG_LONG "Expected %ld to %ld; Got %ld"
#define hpe1564_MSG_LOOKUP "Error converting string response to integer"
#define hpe1564_MSG_NO_MATCH "Could not match string %s"
/*
* static error message
*/
#define VI_ERROR_PARAMETER1_MSG \
"Parameter 1 is invalid"
#define VI_ERROR_PARAMETER2_MSG \
"Parameter 2 is invalid"
#define VI_ERROR_PARAMETER3_MSG \
"Parameter 3 is invalid"
#define VI_ERROR_PARAMETER4_MSG \
"Parameter 4 is invalid"
#define VI_ERROR_PARAMETER5_MSG \
"Parameter 5 is invalid"
#define VI_ERROR_PARAMETER6_MSG \
"Parameter 6 is invalid"
#define VI_ERROR_PARAMETER7_MSG \
"Parameter 7 is invalid"
#define VI_ERROR_PARAMETER8_MSG \
"Parameter 8 is invalid"
#define VI_ERROR_PARAMETER9_MSG \
"Parameter 9 is invalid"
#define VI_ERROR_PARAMETER10_MSG \
"Parameter 10 is invalid"
#define VI_ERROR_PARAMETER11_MSG \
"Parameter 11 is invalid"
#define VI_ERROR_PARAMETER12_MSG \
"Parameter 12 is invalid"
#define VI_ERROR_PARAMETER13_MSG \
"Parameter 13 is invalid"
#define VI_ERROR_PARAMETER14_MSG \
"Parameter 14 is invalid"
#define VI_ERROR_PARAMETER15_MSG \
"Parameter 15 is invalid"
#define VI_ERROR_PARAMETER16_MSG \
"Parameter 16 is invalid"
#define VI_ERROR_PARAMETER17_MSG \
"Parameter 17 is invalid"
#define VI_ERROR_PARAMETER18_MSG \
"Parameter 18 is invalid"
#define VI_ERROR_FAIL_ID_QUERY_MSG \
"Instrument IDN does not match."
#define INSTR_ERROR_INV_SESSION_MSG \
"ViSession (parmeter 1) was not created by this driver"
#define INSTR_ERROR_NULL_PTR_MSG \
"NULL pointer detected"
#define INSTR_ERROR_RESET_FAILED_MSG \
"reset failed"
#define INSTR_ERROR_UNEXPECTED_MSG \
"An unexpected error occurred"
#define INSTR_ERROR_DETECTED_MSG \
"Instrument Error Detected, call hpe1564_error_query()."
#define INSTR_ERROR_NOTFOUND_MSG \
"Instrument was not found."
#define INSTR_ERROR_MULTIPLE_MSG \
"Multiple Instruments were found, none were opened"
#define INSTR_ERROR_A16MAP_FAILED_MSG \
"reset failed"
#define INSTR_ERROR_LOOKUP_MSG \
"String not found in table"
#define INSTR_ERROR_SAMPTTL_MSG \
"output of SAMP disabled, sample source now using TTL trig line "
#define INSTR_ERROR_MASTER_TRIGTTL_MSG \
"Trig source 2 conflicts with MASTER|SLAVE mode; trig source 2 set to HOLD"
#define INSTR_ERROR_MASTER_SAMPTTL_MSG \
"Samp src TTLT<n> conflicts with trig mode MASTER<n>; setting samp src TIMer"
#define INSTR_ERROR_SLAVE_SAMPLE_MSG \
"Sample source changes not allowed while trig mode is SLAVE<n>"
#define INSTR_ERROR_MASTER_OUTP_MSG \
"Output state ON not allowed while trig mode is MASTER<n>|SLAVE<n>"
#define INSTR_ERROR_TRIGTTL_MSG \
"output of TRIG disabled, trig source now using TTL trig line "
#define INSTR_ERROR_LEVMIN_MSG \
"Trig or Limit level < MIN, setting to MIN."
#define INSTR_ERROR_LEVMAX_MSG \
"Trig or Limit level > MAX, setting to MAX."
#define INSTR_ERROR_TRIGLIM_MSG \
"Trig level/Limit check conflict, limit check disabled"
#define INSTR_ERROR_LIMTRIG_MSG \
"Trig level/Limit check conflict, trig source set to IMMediate"
#define INSTR_ERROR_PRETRIG_MSG \
"Pretrigger count >= total count; setting pretrigger to total - 1"
#define INSTR_ERROR_TRIG_IGN_MSG \
"Trigger ignored."
#define INSTR_ERROR_SAMP_IGN_MSG \
"Sample trigger ignored."
#define INSTR_ERROR_INIT_IGN_MSG \
"Illegal while initiated"
#define INSTR_ERROR_CAL_OFF_MSG \
"Calibration not enabled, see hpe1564_calState"
#define INSTR_ERROR_BAD_CAL_VAL_MSG \
"Calibration value not between 85% and 98% of full scale"
#define INSTR_ERROR_FLASH_PROG_MSG \
"Flash programming error; switches set incorrectly?"
#define INSTR_ERROR_FLASH_VPP_MSG \
"Flash VPP low error; switches set incorrectly?"
#define INSTR_ERROR_FLASH_ERASE_MSG \
"Error erasing flash; store aborted"
#define INSTR_ERROR_CAL_STALE_MSG \
"Calibration constants not modified; store aborted"
#define INSTR_ERROR_CAL_STORE_MSG \
"Calibration store unsuccessful; stored value incorrect"
#define INSTR_ERROR_CAL_NOT_STORED_MSG \
"Must STORE data before exiting Calibration state"
#define INSTR_ERROR_CAL_NO_CONVERGE_MSG \
"Calibration not converging; exiting"
#define INSTR_ERROR_BIG_VOLT_MSG \
"Overloads occurring, reduce signal level."
#define INSTR_ERROR_TRIG_DEAD_MSG \
"Trigger source deadlock detected, data fetch aborted"
#define INSTR_ERROR_SAMP_DEAD_MSG \
"Sample source deadlock detected, data fetch aborted"
#define INSTR_ERROR_CAL_GAIN_AUTO_MSG \
"Error generating gain constant"
#define INSTR_ERROR_CAL_OFFS_AUTO_MSG \
"Error generating filter offset"
#define INSTR_ERROR_OVLD_MSG \
"Overload detected; Attempting re-connect of input relays"
#define INSTR_ERROR_OVLD_DATA_MSG \
"Overload detected; data is questionable"
/*================================================================*/
/* don't check the debug pointer all the time!*/
#ifdef DEBUG
#define hpe1564_DEBUG_CHK_THIS( vi, thisPtr) \
/* check for NULL user data */ \
if( 0 == thisPtr) \
{ \
hpe1564_LOG_STATUS( \
vi, NULL, hpe1564_INSTR_ERROR_INV_SESSION ); \
} \
{ \
ViSession defRM; \
\
/* This should never fail */ \
errStatus = viGetAttribute( vi, \
VI_ATTR_RM_SESSION, &defRM); \
if( VI_SUCCESS > errStatus ) \
{ \
hpe1564_LOG_STATUS( \
vi, NULL, hpe1564_INSTR_ERROR_UNEXPECTED ); \
} \
if( defRM != thisPtr->defRMSession) \
{ \
hpe1564_LOG_STATUS( \
vi, NULL, hpe1564_INSTR_ERROR_INV_SESSION ); \
} \
}
#else
#define hpe1564_DEBUG_CHK_THIS( vi, thisPtr)
#endif
/* add the following to the globals data structure */
/* Note: for debugging, you can add __FILE__, __LINE__ as parameters
* to hpe1564_LOG_STATUS, and ViString filename, and ViInt32 lineNumber to
* hpe1564_statusUpdate() in order to determine exactly where an error
* occured in a driver.
*/
#define hpe1564_LOG_STATUS( vi, thisPtr, status ) \
return hpe1564_statusUpdate( vi, thisPtr, status)
/* declare this here since it is called by statusUpdate */
static void hpe1564_srqTraverse(ViSession vi, ViInt32 eventReg);
ViStatus hpe1564_statusUpdate(ViSession vi, hpe1564_globals * thisPtr, ViStatus s)
{
ViInt32 eventQ;
ViUInt32 rc;
char lc[20];
ViStatus errStatus;
if (!thisPtr)
return s;
/* This is only done, if the vi is valid and
* no other errors have occured.
*/
if (s == VI_ERROR_TMO) {
thisPtr->errNumber = s;
return s;
}
/* Check if user wants to query the instrument state. */
if (thisPtr && thisPtr->errQueryDetect) {
/* assume IEEE 488.2 Instrument and query standard
* status event register for a parser error
*/
if (thisPtr->e1406) {
errStatus = viWrite(vi, (ViBuf) "*ESR?", 5, &rc);
if (errStatus < VI_SUCCESS)
return VI_ERROR_SYSTEM_ERROR;
errStatus = viRead(vi, (ViPBuf) lc, 20, &rc);
if (errStatus < VI_SUCCESS)
return VI_ERROR_SYSTEM_ERROR;
eventQ = strtol(lc,NULL,0);
if ((0x04 /* Query Error */
| 0x08 /* Device Dependent Error */
| 0x10 /* Execution Error */
| 0x20 /* Command Error */
) & eventQ) {
/* See if the user has an instr error handler enabled */
if (thisPtr->eventHandlerArray[hpe1564_INSTR_ERROR_HANDLER_IDX].eventHandler) {
/* call the users handler */
thisPtr->eventHandlerArray[hpe1564_INSTR_ERROR_HANDLER_IDX].eventHandler(vi, (ViInt32) s,
thisPtr->
eventHandlerArray
[hpe1564_INSTR_ERROR_HANDLER_IDX].
userData);
}
return hpe1564_INSTR_ERROR_DETECTED;
}
}
}
/* if a plug and play error occurs, see if the user has a handler enabled */
if (s != hpe1564_INSTR_ERROR_DETECTED &&
VI_SUCCESS > s && thisPtr->eventHandlerArray[hpe1564_USER_ERROR_HANDLER_IDX].eventHandler) {
/* call the users handler */
thisPtr->eventHandlerArray[hpe1564_USER_ERROR_HANDLER_IDX].eventHandler(vi, (ViInt32) s,
thisPtr->
eventHandlerArray
[hpe1564_USER_ERROR_HANDLER_IDX].
userData);
}
thisPtr->errNumber = s;
return s;
}
/*
* Error Message Structures
*/
struct instrErrStruct {
ViStatus errStatus;
ViString errMessage;
};
const static struct instrErrStruct instrErrMsgTable[] = {
{VI_ERROR_PARAMETER1, VI_ERROR_PARAMETER1_MSG},
{VI_ERROR_PARAMETER2, VI_ERROR_PARAMETER2_MSG},
{VI_ERROR_PARAMETER3, VI_ERROR_PARAMETER3_MSG},
{VI_ERROR_PARAMETER4, VI_ERROR_PARAMETER4_MSG},
{VI_ERROR_PARAMETER5, VI_ERROR_PARAMETER5_MSG},
{VI_ERROR_PARAMETER6, VI_ERROR_PARAMETER6_MSG},
{VI_ERROR_PARAMETER7, VI_ERROR_PARAMETER7_MSG},
{VI_ERROR_PARAMETER8, VI_ERROR_PARAMETER8_MSG},
{hpe1564_INSTR_ERROR_PARAMETER9, VI_ERROR_PARAMETER9_MSG},
{hpe1564_INSTR_ERROR_PARAMETER10, VI_ERROR_PARAMETER10_MSG},
{hpe1564_INSTR_ERROR_PARAMETER11, VI_ERROR_PARAMETER11_MSG},
{hpe1564_INSTR_ERROR_PARAMETER12, VI_ERROR_PARAMETER12_MSG},
{hpe1564_INSTR_ERROR_PARAMETER13, VI_ERROR_PARAMETER13_MSG},
{hpe1564_INSTR_ERROR_PARAMETER14, VI_ERROR_PARAMETER14_MSG},
{hpe1564_INSTR_ERROR_PARAMETER15, VI_ERROR_PARAMETER15_MSG},
{hpe1564_INSTR_ERROR_PARAMETER16, VI_ERROR_PARAMETER16_MSG},
{hpe1564_INSTR_ERROR_PARAMETER17, VI_ERROR_PARAMETER17_MSG},
{hpe1564_INSTR_ERROR_PARAMETER18, VI_ERROR_PARAMETER18_MSG},
{VI_ERROR_FAIL_ID_QUERY, VI_ERROR_FAIL_ID_QUERY_MSG},
{hpe1564_INSTR_ERROR_INV_SESSION, INSTR_ERROR_INV_SESSION_MSG},
{hpe1564_INSTR_ERROR_NULL_PTR, INSTR_ERROR_NULL_PTR_MSG},
{hpe1564_INSTR_ERROR_RESET_FAILED, INSTR_ERROR_RESET_FAILED_MSG},
{hpe1564_INSTR_ERROR_UNEXPECTED, INSTR_ERROR_UNEXPECTED_MSG},
{hpe1564_INSTR_ERROR_DETECTED, INSTR_ERROR_DETECTED_MSG},
{hpe1564_ERROR_NOTFOUND, INSTR_ERROR_NOTFOUND_MSG},
{hpe1564_ERROR_MULTIPLE, INSTR_ERROR_MULTIPLE_MSG},
{hpe1564_INSTR_ERROR_A16MAP_FAILED, INSTR_ERROR_A16MAP_FAILED_MSG},
{hpe1564_INSTR_ERROR_LOOKUP, INSTR_ERROR_LOOKUP_MSG},
{hpe1564_ERROR_SAMPTTL, INSTR_ERROR_SAMPTTL_MSG},
{hpe1564_ERROR_TRIGTTL, INSTR_ERROR_TRIGTTL_MSG},
{hpe1564_ERROR_LEVMIN, INSTR_ERROR_LEVMIN_MSG},
{hpe1564_ERROR_LEVMAX, INSTR_ERROR_LEVMAX_MSG},
{hpe1564_ERROR_TRIGLIM, INSTR_ERROR_TRIGLIM_MSG},
{hpe1564_ERROR_PRETRIG, INSTR_ERROR_PRETRIG_MSG},
{hpe1564_ERROR_TRIG_IGN, INSTR_ERROR_TRIG_IGN_MSG},
{hpe1564_ERROR_SAMP_IGN, INSTR_ERROR_SAMP_IGN_MSG},
{hpe1564_ERROR_INIT_IGN, INSTR_ERROR_INIT_IGN_MSG},
{hpe1564_ERROR_CAL_OFF, INSTR_ERROR_CAL_OFF_MSG},
{hpe1564_ERROR_BAD_CAL_VAL, INSTR_ERROR_BAD_CAL_VAL_MSG},
{hpe1564_ERROR_FLASH_PROG, INSTR_ERROR_FLASH_PROG_MSG},
{hpe1564_ERROR_FLASH_VPP, INSTR_ERROR_FLASH_VPP_MSG},
{hpe1564_ERROR_FLASH_ERASE, INSTR_ERROR_FLASH_ERASE_MSG},
{hpe1564_ERROR_CAL_STALE, INSTR_ERROR_CAL_STALE_MSG},
{hpe1564_ERROR_CAL_STORE, INSTR_ERROR_CAL_STORE_MSG},
{hpe1564_ERROR_CAL_NOT_STORED, INSTR_ERROR_CAL_NOT_STORED_MSG},
{hpe1564_ERROR_CAL_NO_CONVERGE, INSTR_ERROR_CAL_NO_CONVERGE_MSG},
{hpe1564_ERROR_BIG_VOLT, INSTR_ERROR_BIG_VOLT_MSG},
{hpe1564_ERROR_LIMTRIG, INSTR_ERROR_LIMTRIG_MSG},
{hpe1564_ERROR_TRIG_DEAD, INSTR_ERROR_TRIG_DEAD_MSG},
{hpe1564_ERROR_SAMP_DEAD, INSTR_ERROR_SAMP_DEAD_MSG},
{hpe1564_ERROR_SELF_TEST, hpe1564_MSG_SELF_TEST_FAILED},
{hpe1564_ERROR_CAL_GAIN_AUTO, INSTR_ERROR_CAL_GAIN_AUTO_MSG},
{hpe1564_ERROR_CAL_OFFS_AUTO, INSTR_ERROR_CAL_OFFS_AUTO_MSG},
{hpe1564_ERROR_OVLD, INSTR_ERROR_OVLD_MSG},
{hpe1564_ERROR_OVLD_DATA, INSTR_ERROR_OVLD_DATA_MSG},
{hpe1564_ERROR_MASTER_TRIGTTL, INSTR_ERROR_MASTER_TRIGTTL_MSG},
{hpe1564_ERROR_MASTER_SAMPTTL, INSTR_ERROR_MASTER_SAMPTTL_MSG},
{hpe1564_ERROR_SLAVE_SAMPLE, INSTR_ERROR_SLAVE_SAMPLE_MSG},
{hpe1564_ERROR_MASTER_OUTP, INSTR_ERROR_MASTER_OUTP_MSG},
{hpe1564_WARN_SELF_TEST, hpe1564_MSG_WARN_SELF_TEST},
};
/* macros for testing parameters */
#define hpe1564_CHK_BOOLEAN( my_val, err ) if( hpe1564_chk_boolean( thisPtr, my_val) ) hpe1564_LOG_STATUS( vi, thisPtr, err);
static ViBoolean hpe1564_chk_boolean(hpe1564_globals * thisPtr, ViBoolean my_val)
{
char message[hpe1564_ERR_MSG_LENGTH];
if ((my_val != VI_TRUE) && (my_val != VI_FALSE)) {
/* true = parameter is invalid */
sprintf(message, hpe1564_MSG_BOOLEAN, my_val);
hpe1564_CDE_MESSAGE(message);
/* true = parameter is invalid */
return VI_TRUE;
}
/* false = okay */
return VI_FALSE;
}
#define hpe1564_CHK_REAL_RANGE( my_val, min, max, err ) if( hpe1564_chk_real_range( thisPtr, my_val, min, max) ) hpe1564_LOG_STATUS( vi, thisPtr, err);
static ViBoolean hpe1564_chk_real_range(hpe1564_globals * thisPtr,
ViReal64 my_val, ViReal64 min, ViReal64 max)
{
char message[hpe1564_ERR_MSG_LENGTH];
if ((my_val < min) || (my_val > max)) {
sprintf(message, hpe1564_MSG_REAL, min, max, my_val);
hpe1564_CDE_MESSAGE(message);
/* true = parameter is invalid */
return VI_TRUE;
}
return VI_FALSE;
}
#define hpe1564_CHK_INT_RANGE( my_val, min, max, err ) if( hpe1564_chk_int_range( thisPtr, my_val, min, max) ) hpe1564_LOG_STATUS( vi, thisPtr, err);
static ViBoolean hpe1564_chk_int_range(hpe1564_globals * thisPtr,
ViInt16 my_val, ViInt16 min, ViInt16 max)
{
char message[hpe1564_ERR_MSG_LENGTH];
if ((my_val < min) || (my_val > max)) {
sprintf(message, hpe1564_MSG_INT, min, max, my_val);
hpe1564_CDE_MESSAGE(message);
/* true = parameter is invalid */
return VI_TRUE;
}
return VI_FALSE;
}
#define hpe1564_CHK_LONG_RANGE( my_val, min, max, err ) if( hpe1564_chk_long_range( thisPtr, my_val, min, max) ) hpe1564_LOG_STATUS( vi, thisPtr, err);
static ViBoolean hpe1564_chk_long_range(hpe1564_globals * thisPtr,
ViInt32 my_val, ViInt32 min, ViInt32 max)
{
char message[hpe1564_ERR_MSG_LENGTH];
if ((my_val < min) || (my_val > max)) {
sprintf(message, hpe1564_MSG_LONG, min, max, my_val);
hpe1564_CDE_MESSAGE(message);
/* true = parameter is invalid */
return VI_TRUE;
}
return VI_FALSE;
}
#define hpe1564_CHK_ENUM( my_val, limit, err ) if( hpe1564_chk_enum( thisPtr, my_val, limit) ) hpe1564_LOG_STATUS( vi, thisPtr, err);
/* utility routine which searches for a string in an array of strings. */
/* This is used by the CHK_ENUM macro */
static ViBoolean hpe1564_chk_enum(hpe1564_globals * thisPtr, ViInt16 my_val, ViInt16 limit)
{
char message[hpe1564_ERR_MSG_LENGTH];
if ((my_val < 0) || (my_val > limit)) {
sprintf(message, hpe1564_MSG_INT, 0, limit, my_val);
hpe1564_CDE_MESSAGE(message);
/* true = parameter is invalid */
return VI_TRUE;
}
return VI_FALSE;
}
/* ==========================================================================
This function searches an array of strings for a specific string and
returns its index. If successful, a VI_SUCCESS is returned,
else hpe1564_INSTR_ERROR_LOOKUP is returned.
======================================================================== */
ViStatus hpe1564_findIndex(hpe1564_globals * thisPtr, const char *const array_of_strings[],
/*last entry in array must be 0 */
const char *string, /* string read from instrument */
ViPInt16 index)
{ /* result index */
ViInt16 i;
ViInt16 my_len;
char search_str[20];
char message[80];
strcpy(search_str, string);
/* get rid of newline if present in string */
/* needed because %s includes newline in some VTL's */
my_len = strlen(search_str);
if (search_str[my_len - 1] == '\n')
search_str[my_len - 1] = '\0';
for (i = 0; array_of_strings[i]; i++) {
if (!strcmp(array_of_strings[i], search_str)) {
*index = i;
return VI_SUCCESS;
}
}
/* if we got here, we did not find it */
sprintf(message, hpe1564_MSG_NO_MATCH, search_str);
hpe1564_CDE_MESSAGE(message);
return hpe1564_INSTR_ERROR_LOOKUP;
}
/* returns the globals pointer */
#define GetGlobals(vi,thisPtr)\
{\
errStatus = viGetAttribute( vi, VI_ATTR_USER_DATA, (ViAddr) &thisPtr);\
if( VI_SUCCESS > errStatus)\
hpe1564_LOG_STATUS( vi, NULL, errStatus);\
}
void hpe1564_delay(long delayTime)
{
struct timeb timebuffer;
long t0;
long t1;
ftime(&timebuffer);
t0 = timebuffer.time;
t1 = (long)timebuffer.millitm;
ftime(&timebuffer);
while (((timebuffer.time - t0) * 1000 + ((long)timebuffer.millitm - t1)) < delayTime) {
ftime(&timebuffer);
}
}
#ifndef __hpux
long setDelay(double val)
{
double slice;
_int64_t count;
if (!QueryPerformanceFrequency((LARGE_INTEGER *) & count)) {
//hdw doesn't have high perfomance count so use getickcount
slice = 1e-3; //units for gettick count
} else {
slice = 1.0 / count; //Seconds per tick
}
return (long)(val / slice) + 1;
}
void hpe1564_doDelay(long ticks)
{
_int64_t startval, tmp;
if (!QueryPerformanceCounter((LARGE_INTEGER *) & startval)) {
DWORD sval;
sval = GetTickCount();
while (GetTickCount() - sval < (DWORD) ticks) ;
return;
}
tmp = startval;
while (tmp - startval < (DWORD) ticks) {
QueryPerformanceCounter((LARGE_INTEGER *) & tmp);
}
}
#else
long setDelay(double val)
{
return val * 1E6;
}
void doDelay(long ticks)
{
struct timeval t0, t1;
gettimeofday(&t0, NULL);
t0.tv_usec += ticks;
t0.tv_sec += t0.tv_usec / 1000000;
t0.tv_usec = t0.tv_usec % 1000000;
gettimeofday(&t1, NULL);
while (t1.tv_sec < t0.tv_sec)
gettimeofday(&t1, NULL);
if (t1.tv_sec >= t0.tv_sec)
return; /* get out quick if past delay time */
while (t1.tv_usec < t0.tv_usec)
gettimeofday(&t1, NULL);
return;
}
#endif
/****************************************************************************
hpe1564_init
*****************************************************************************
Parameter Name Type Direction
------------------------------------------------------------------------
| InstrDesc ViRsrc IN
| ---------------------------------------------------------------------
| | The Instrument Description.
| |
| | Examples: VXI::5, GPIB-VXI::128::INSTR
------------------------------------------------------------------------
| id_query ViBoolean IN
| ---------------------------------------------------------------------
| | if( VI_TRUE) Perform In-System Verification.
| | if(VI_FALSE) Do not perform In-System Verification
------------------------------------------------------------------------
| do_reset ViBoolean IN
| ---------------------------------------------------------------------
| | IF( VI_TRUE) Perform Reset Operation.
| | if(VI_FALSE) Do not perform Reset operation
------------------------------------------------------------------------
| vi ViPSession OUT
| ---------------------------------------------------------------------
| | Instrument Handle. This is VI_NULL if an error occurred
| | during the init.
*****************************************************************************/
ViStatus _VI_FUNC hpe1564_init(ViRsrc InstrDesc, ViBoolean id_query, ViBoolean do_reset,
ViPSession vi)
{
hpe1564_globals *thisPtr = NULL;
ViSession defRM = 0;
ViStatus errStatus;
ViUInt16 intf, num, primary, secondary;
char gpibdesc[64];
ViSession vi1406;
char idn_buf[256];
ViUInt16 manfId;
ViUInt16 modelCode;
ViAddr addr; /* A16 base address */
ViInt16 result; /* power on self test result */
ViUInt32 tempBlock[4]; /* used to check for D32 capability */
*vi = VI_NULL;
/* Find the Default Resource Manager */
errStatus = viOpenDefaultRM(&defRM);
if (VI_SUCCESS > errStatus) {
/* Errors: VI_ERROR_SYSTEM_ERROR
* VI_ERROR_ALLOC
*/
hpe1564_LOG_STATUS(*vi, NULL, errStatus);
}
errStatus = viOpen(defRM, InstrDesc, VI_NULL, VI_NULL, vi);
if (VI_SUCCESS > errStatus) {
viClose(defRM);
/* Errors: VI_ERROR_NSUP_OPER
* VI_ERROR_INV_RSRC_NAME
* VI_ERROR_INV_ACC_MODE
* VI_ERROR_RSRC_NFOUND
* VI_ERROR_ALLOC
*/
*vi = VI_NULL;
hpe1564_LOG_STATUS(*vi, NULL, errStatus);
}
/* get memory for instance specific globals */
thisPtr = (hpe1564_globals *) malloc(sizeof(hpe1564_globals));
if (0 == thisPtr) {
viClose(defRM); /* also closes vi session */
*vi = VI_NULL;
hpe1564_LOG_STATUS(*vi, NULL, VI_ERROR_ALLOC);
}
/* initialize memory area to 0 */
{
int i;
char *p;
p = (char *)thisPtr;
for (i = 0; i < sizeof(hpe1564_globals); i++)
p[i] = 0;
}
if (errStatus = viGetAttribute(*vi, VI_ATTR_INTF_TYPE, &intf)) {
viClose(*vi);
viClose(defRM);
*vi = VI_NULL;
hpe1564_LOG_STATUS(*vi, NULL, errStatus);
}
/* check for 1406 internal driver by reading *IDN? of 0 and
*IDN? of secondary for this address*/
if (intf == VI_INTF_GPIB_VXI) {
viGetAttribute(*vi, VI_ATTR_INTF_NUM, &num);
viGetAttribute(*vi, VI_ATTR_GPIB_PRIMARY_ADDR, &primary);
sprintf(gpibdesc, "GPIB%hd::%hd::0", num, primary);
errStatus = viOpen(defRM, gpibdesc, VI_NULL, VI_NULL, &vi1406);
if (errStatus < VI_SUCCESS)
goto gpib_check_done;
/* try sending an IDN? to 1406 */
viSetAttribute(vi1406, VI_ATTR_TMO_VALUE, 1000);
errStatus = viClear(vi1406);
if (errStatus < VI_SUCCESS) {
viClose(vi1406);
goto gpib_check_done;
}
errStatus = viPrintf(vi1406, "*IDN?\n");
if (errStatus < VI_SUCCESS) {
viClose(vi1406);
goto gpib_check_done;
}
/* it worked, try getting response and check it */
errStatus = viScanf(vi1406, "%t", idn_buf);
hpe1564_delay(10L); /* delay 10 ms */
viClose(vi1406);
if (errStatus < VI_SUCCESS || memcmp(idn_buf, "HEWLETT-PACKARD,E140", 20))
goto gpib_check_done;
/* Since we got here, we ARE an E140x */
/* try sending an IDN? to the card drivers */
/* these next two calls fail for GPIB-VXI mode. They were
in the original template, but I am commenting them out
here for now.
viSetAttribute(*vi, VI_ATTR_TMO_VALUE, 1000);