-
Notifications
You must be signed in to change notification settings - Fork 0
/
mouth.c
280 lines (253 loc) · 7.05 KB
/
mouth.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
#include "dna.h"
#include "mouth.h"
#include <gpib.h>
//
//This is the MOUTH of Word Generator 2.
//The purpose of this source code is to wrap the GPIB interface functions
//into a seperate file (and eventually function panel).
//The BRAIN uses these commands to communicate with the GPIB
//instruments: SRS DS345.
#include "toolbox.h"
static const char gpibDS345Name[] = "DEV2"; // setup in driver panel
static const int gpibDS345Addr = 19; // set on front panel of SRS
static int gpibDS345=-1; // Handle (-1 is error value)
// Sort of a magic number
#define NoValue 577.5698e-4;
// SMART CACHE While adding buffer commands:
double LastAmp=NoValue;
double LastFreq=NoValue;
int CVIFUNC mouth_InitRF (void)
// Same for all the GPIB modes
{
int err=0;
// We only need to do this once
if (-1==gpibDS345)
gpibDS345 = ibdev (0, gpibDS345Addr, NO_SAD, T3s, 1, 0);
errChk(mouth_Error("mouth_InitRF{ibdev call failed}",0,0));
LastAmp=NoValue;
LastFreq=NoValue;
Error:
return (err?-1:0);
}
int CVIFUNC mouth_Error (const char message[], dnaByte abortQ,
dnaByte forceErrorQ)
{
int status,err;
long count;
char mouthMessage[80]={0};
#if VERBOSE > 0
if (forceErrorQ) {
FmtOut ("%s\n", message);
Beep();
Breakpoint();
ScanIn ("%s", mouthMessage); // Wait for enter to be pressed
if (abortQ) abort();
return -1;
}
status = ThreadIbsta ();
if (status & ERR) { // ERR is defined in gpib.h
err=ThreadIberr();
count = ThreadIbcntl();
FmtOut ("%s\n", message);
FmtOut ("status %d error %d count %d\n", status,err,count);
FmtOut ("Library error so Abort always requested.\n");
FmtOut ("Reminder: Did you turn on the SRS? to GPIB ID #19?\n");
Beep();
Breakpoint();
ScanIn ("%s", mouthMessage); // Wait for enter to be pressed
if (abortQ) abort();
return -1;
}
#else
if (forceErrorQ) {
Beep();
MessagePopup ("Mouth Error", message);
Breakpoint();
if (abortQ) abort();
return -1;
}
status = ThreadIbsta ();
if (status & ERR) {
err=ThreadIberr();
count = ThreadIbcntl();
Fmt (mouthMessage, "%s<%s\n", message);
Fmt (mouthMessage, "%s[a]<status %d error %d count %d\n", status,err,count);
if (abortQ)
Fmt (mouthMessage, "%s[a]<Software Error and Abort has been requested.\n");
Fmt (mouthMessage, "%s[a]<Reminder: Did you turn on the SRS? to GPIB ID #19?\n");
Beep();
MessagePopup ("Mouth Error", mouthMessage);
Breakpoint();
// if (abortQ) abort();
return -1;
}
#endif
return 0;
}
int CVIFUNC mouth_WriteDS345 (const char command[], int numberofBytes)
{
int err=0;
// ibwrt (gpibDS345, command, numberofBytes);
ibwait (gpibDS345, 0); // re-synch
errChk(mouth_Error("mouth_WriteDS345{ibwait call failed}",0,0));
ibwrta (gpibDS345, command, numberofBytes);
errChk(mouth_Error("mouth_WriteDS345{ibwrta call failed}",0,0));
Error:
return (err?-1:0);
}
int CVIFUNC mouth_ZeroDS345 (void)
{
int err=0,l;
char *command;
mouth_MakeIdleCommand(&command,0.0,0.0,0.0);
l=strlen(command);
// ibwrt (gpibDS345, "OFFS, numberofBytes);
ibwait (gpibDS345, 0); // re-synch
errChk(mouth_Error("mouth_ZeroDS345{ibwait call failed}",0,0));
ibwrta (gpibDS345, command, l);
errChk(mouth_Error("mouth_ZeroDS345{ibwrta call failed}",0,0));
Error:
return (err?-1:0);
}
int CVIFUNC mouth_MakeIdleCommand (char ** command, double amp, double DCOffset,
double freq)
// Takes frequency in MHz, Offset V, Ampl Vpp
{
int err=0;
char buf[255];
int pos=0;
freq*=1000000.0; // Convert to Hertz
FillBytes (buf, 0, 255, 0);
pos=Fmt(buf,"%s<OFFS %f;AMPL %fVP;FREQ %f",DCOffset,amp,freq);
if (pos<0) {
err=-1;
command=0;
goto Error;
}
(*command)=StrDup((char *)&buf);
Error:
return (err?-1:0);
}
int CVIFUNC mouth_AddBufferCommand (dnaBufferRF*buffer, double amp, double freq)
// Takes frequency in MHz, Ampl Vpp
{
int err=0;
if (freq!=LastFreq)
if (amp!=LastAmp) {
LastAmp=amp;
LastFreq=freq;
freq*=1000000.0; // Convert to Hz
Fmt(&buffer->Buffer[buffer->PosByte],"%s<AMPL %fVP;FREQ %f",amp,freq);
#if VERBOSE > 1
printf("%s\n",&buffer->Buffer[buffer->PosByte]);
#endif
// Order of the next two commands is important
buffer->NumberOfCommands++;
errChk(dna_AdvanceBufferRF (buffer));
}
else
errChk(mouth_AddBufferFreq(buffer,freq));
else
if (amp!=LastAmp)
errChk(mouth_AddBufferAmp(buffer,amp));
else {
buffer->Buffer[buffer->PosByte]=0;
// Order of the next two commands is important
buffer->NumberOfCommands++;
errChk(dna_AdvanceBufferRF (buffer));
}
Error:
return (err?-1:0);
}
int CVIFUNC mouth_AddBufferZero (dnaBufferRF*buffer)
{
int err=0;
LastAmp=0.0;
LastFreq=0.0;
Fmt(&buffer->Buffer[buffer->PosByte],"%s<AMPL %fVP;FREQ %f",0.0,0.0);
#if VERBOSE > 1
printf("%s\n",&buffer->Buffer[buffer->PosByte]);
#endif
// Order of the next two commands is important
buffer->NumberOfCommands++;
errChk(dna_AdvanceBufferRF (buffer));
Error:
return (err?-1:0);
}
int CVIFUNC mouth_AddBufferFreq (dnaBufferRF*buffer, double freq)
{
int err=0;
LastFreq=freq;
freq*=1000000.0; // Convert to Hz
Fmt(&buffer->Buffer[buffer->PosByte],"%s<FREQ %f",freq);
#if VERBOSE > 1
printf("%s\n",&buffer->Buffer[buffer->PosByte]);
#endif
// Order of the next two commands is important
buffer->NumberOfCommands++;
errChk(dna_AdvanceBufferRF (buffer));
Error:
return (err?-1:0);
}
int CVIFUNC mouth_AddBufferAmp (dnaBufferRF*buffer, double amp)
{
int err=0;
Fmt(&buffer->Buffer[buffer->PosByte],"%s<AMPL %fVP",amp);
#if VERBOSE > 1
printf("%s\n",&buffer->Buffer[buffer->PosByte]);
#endif
// Order of the next two commands is important
buffer->NumberOfCommands++;
errChk(dna_AdvanceBufferRF (buffer));
LastAmp=amp;
Error:
return (err?-1:0);
}
int CVIFUNC mouth_ResetBuffer (dnaBufferRF*buffer)
// Gets buffer ready for building or output
{
LastAmp=NoValue;
LastFreq=NoValue;
buffer->CurrentStage=0;
buffer->TargetTime=buffer->StartTimes[0];
buffer->CommandCounter=buffer->Commands[0];
buffer->CallTimebase=buffer->RFTimebase[0];
buffer->PosByte=0;
buffer->PosCommand=0;
return 0;
}
int CVIFUNC mouth_OutBufferCommand (dnaBufferRF*buffer)
{
int l;
int err=0;
require(0<buffer->CommandCounter);
l = dna_LengthBufferRF (buffer);
if (l) {
// ibwrt (gpibDS345, buffer->Buffer+buffer->PosByte, l);
ibwait (gpibDS345, 0); // re-synch
if (ThreadIbsta() & ERR)
return -2; // TODO BUG GOTCHA : make more verbose
// errChk(mouth_Error("mouth_WriteDS345{ibwait call failed}",0,0));
ibwrta (gpibDS345, buffer->Buffer+buffer->PosByte, l);
if (ThreadIbsta() & ERR)
return -3;
// err = mouth_Error("mouth_WriteDS345{ibwrta call failed}",0,0);
#if VERBOSE > 1
printf("%s\n",&buffer->Buffer[buffer->PosByte]);
if (err)
printf("mouth_OutBufferCommand{ibwrt call failed} %d %d %d %d %d\n",
buffer->PosByte,buffer->PosCommand,ERR,ThreadIbsta(),ThreadIberr());
#endif
if (err) goto Error;
errChk(dna_AdvanceRunBufferRF (buffer));
}
else {
errChk(dna_AdvanceRunBufferRF (buffer));
#if VERBOSE > 0
printf("mouth_OutBufferCommand{Length was zero} %d %d\n",
buffer->PosByte,buffer->PosCommand);
#endif
}
Error:
return (err?-1:0);
}