forked from fenugrec/tekfwtool
/
getcaldata.c
285 lines (227 loc) · 7.1 KB
/
getcaldata.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
/*
*
* CAL EEPROM backup - for TDS520B,TDS540B,TDS724A,TDS744A,TDS754A,TDS782A,TDS784A
*
* compiled with MinGW gcc on Windows Vista / Win7
* tested with Agilent I/O 16.x and S82357 from http://bmjd.biz/index.php
*
* Original code by ostapip (forum.tek.com somewhere).
* Also includes some mods from Ragges (EEVblog), https://github.com/ragges/tektools/
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
#include <conio.h>
#include <windows.h>
/* for Agilent adapters uncomment following line */
#include "ni488.h"
#elif defined(__linux__)
/* linux with linux-gpib */
#include <gpib/ib.h>
#elif defined(__APPLE__)
/* MacOS with NI GPIB drivers */
#include <ni4882.h>
#elif defined(__MSDOS__)
#include "dosdefs.h"
#else
#error "Unknown compiler environment!"
#endif
#if !defined (PROG_NAME)
#define PROG_NAME __FILE__
#endif
#if !defined (GIT_VERSION)
#define GIT_VERSION "unknown"
#endif
#if !defined (BUILD_TIME)
#define BUILD_TIME __DATE__ " " __TIME__
#endif
#define ARRAYSIZE 100 // Size of read buffer
int Dev; // Device handle
char ReadBuffer[ARRAYSIZE + 1]; // Read data buffer
char ErrorMnemonic[21][5] = {"EDVR", "ECIC", "ENOL", "EADR", "EARG",
"ESAC", "EABO", "ENEB", "EDMA", "",
"EOIP", "ECAP", "EFSO", "", "EBUS",
"ESTB", "ESRQ", "", "", "", "ETAB"};
void GPIBCleanup(int Dev, char* ErrorMsg);
static int debug;
static char* ident = PROG_NAME " Version: " GIT_VERSION " Build time: " BUILD_TIME;
static void print_version(void)
{
fprintf(stderr, "# %s\n", ident);
}
/*
* ========================================================================
*
* INITIALIZATION SECTION
*
* ========================================================================
*/
/*
* Assign a unique identifier to the device and store in the variable
* Dev. If the ERR bit is set in ibsta, call GPIBCleanup with an
* error message. Otherwise, the device handle, Dev, is returned and
* is used in all subsequent calls to the device.
*/
#define BDINDEX 0 // Board Index
#define PRIMARY_ADDR_OF_DMM 1 // Primary address of device
#define NO_SECONDARY_ADDR 0 // Secondary address of device
#define TIMEOUT T10s // Timeout value = 10 seconds
#define EOTMODE 1 // Enable the END message
#define EOSMODE 0 // Disable the EOS mode
int main(void) {
FILE *outfile;
int f;
#if 0
unsigned long addr;
// addr = 0x00040000L; /* CAL data base address on TDS5xxB,6xxA,7xxA*/
addr = 262144; /* CAL data base address on TDS5xxB,6xxA,7xxA*/
#endif
unsigned char calbuf[255];
int caldata;
print_version();
Dev = ibdev (BDINDEX, PRIMARY_ADDR_OF_DMM, NO_SECONDARY_ADDR,
TIMEOUT, EOTMODE, EOSMODE);
if (ibsta & ERR)
{
GPIBCleanup(Dev, "Unable to open device");
return 1;
}
/*
* Clear the internal or device functions of the device. If the error
* bit ERR is set in ibsta, call GPIBCleanup with an error message.
*/
ibclr (Dev);
if (ibsta & ERR)
{
GPIBCleanup(Dev, "Unable to clear device");
return 1;
}
/*
* ========================================================================
*
* MAIN BODY SECTION
*
* In this application, the Main Body communicates with the instrument
* by writing a command to it and reading its response.
*
* ========================================================================
*/
ibwrt (Dev, "*IDN?", 5L);
if (ibsta & ERR)
{
GPIBCleanup(Dev, "Unable to write to device");
return 1;
}
ibrd (Dev, ReadBuffer, ARRAYSIZE);
if (ibsta & ERR)
{
GPIBCleanup(Dev, "Unable to read data from device");
return 1;
}
ReadBuffer[ibcntl] = '\0';
printf("DSO IDN: %s\n", ReadBuffer);
outfile = fopen("U1052.bin","wb");
printf("dumping U1052.bin\n");
printf("\nPlease wait ...\n");
/* the first 8 bytes of the U1052 EEPROM are not mapped and empty (0x00h)
so we write as well 8 time 0x00h to dump file */
for(f = 0; f < 8; f++)
{
fputc(0x00, outfile);
}
/* send first TEKTRONIX Password fr TDS5xxB/7xxA models to allow memory dump */
ibwrt (Dev, "PASSWORD PITBULL", 17L);
if (ibsta & ERR)
{
GPIBCleanup(Dev, "Unable to write to device");
return 1;
}
for(f = 0; f < 124; f++)
{
char cmdbuf[64] = "WORDCONSTANT:ATOFFSET? 262144,";
char cmdnr[16];
unsigned char* abuffer;
sprintf(cmdnr,"%d",f);
strcat(cmdbuf, cmdnr);
if (debug > 1) printf("DSO IDN: %s\n", cmdbuf);
ibwrt (Dev, cmdbuf, sizeof(cmdbuf));
if (ibsta & ERR)
{
GPIBCleanup(Dev, "Unable to write to device");
return 1;
}
ibrd(Dev, calbuf, 6);
if (ibsta & ERR)
{
GPIBCleanup(Dev, "Unable to write to device");
return 1;
}
caldata = atoi((char *) calbuf);
if (debug > 1) printf("CAL DATA: %X\n", caldata);
abuffer = (unsigned char *)&caldata;
fputc(abuffer[1], outfile);
fputc(abuffer[0], outfile);
}
fclose(outfile);
outfile = fopen("U1055.bin","wb");
printf("dumping U1055.bin\n");
printf("\nPlease wait ...\n");
for(f = 0; f < 128; f++)
{
char cmdbuf[64] = "WORDCONSTANT:ATOFFSET? 262144,";
char cmdnr[16];
unsigned char* abuffer;
sprintf(cmdnr,"%d",f+124);
strcat(cmdbuf, cmdnr);
if (debug > 1) printf("DSO IDN: %s\n", cmdbuf);
ibwrt (Dev, cmdbuf, sizeof(cmdbuf));
if (ibsta & ERR)
{
GPIBCleanup(Dev, "Unable to write to device");
return 1;
}
ibrd(Dev, calbuf, 6);
if (ibsta & ERR)
{
GPIBCleanup(Dev, "Unable to write to device");
return 1;
}
caldata = atoi((char *) calbuf);
if (debug > 1) printf("CAL DATA: %X\n", caldata);
abuffer = (unsigned char *)&caldata;
fputc(abuffer[1], outfile);
fputc(abuffer[0], outfile);
}
fclose(outfile);
printf("\nCalibration data has been successful dumped\n");
/*
* ========================================================================
*
* CLEANUP SECTION
*
* ========================================================================
*/
/* Take the device offline. */
ibonl (Dev, 0);
return 0;
}
/*
* After each GPIB call, the application checks whether the call
* succeeded. If an NI-488.2 call fails, the GPIB driver sets the
* corresponding bit in the global status variable. If the call
* failed, this procedure prints an error message, takes the
* device offline and exits.
*/
void GPIBCleanup(int ud, char* ErrorMsg)
{
printf("Error : %s\nibsta = 0x%x iberr = %d (%s)\n",
ErrorMsg, ibsta, iberr, ErrorMnemonic[iberr]);
if (ud != -1)
{
printf("Cleanup: Taking device offline\n");
ibonl (ud, 0);
}
}