-
Notifications
You must be signed in to change notification settings - Fork 79
/
SiglentSCPIOscilloscope.cpp
251 lines (212 loc) · 10 KB
/
SiglentSCPIOscilloscope.cpp
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
/***********************************************************************************************************************
* *
* ANTIKERNEL v0.1 *
* *
* Copyright (c) 2012-2020 Andrew D. Zonenberg, Galen Schretlen *
* 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. *
* *
* * Neither the name of the author nor the names of any contributors may be used to endorse or promote products *
* derived from this software without specific prior written permission. *
* *
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS "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 AUTHORS BE HELD 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. *
* *
***********************************************************************************************************************/
#include "scopehal.h"
#include "SiglentSCPIOscilloscope.h"
#include "ProtocolDecoder.h"
#include "base64.h"
using namespace std;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Construction / destruction
SiglentSCPIOscilloscope::SiglentSCPIOscilloscope(SCPITransport* transport)
: LeCroyOscilloscope(transport)
{
}
SiglentSCPIOscilloscope::~SiglentSCPIOscilloscope()
{
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// SCPI protocol logic
string SiglentSCPIOscilloscope::GetDriverNameInternal()
{
return "siglent";
}
// parses out length, does no other validation. requires 17 bytes at header.
uint32_t SiglentSCPIOscilloscope::ReadWaveHeader(char *header)
{
m_transport->ReadRawData(16, (unsigned char*)header);
if (strlen(header) != 16)
{
LogError("Unexpected descriptor header %s\n", header);
return 0;
}
LogDebug("got header: %s\n", header);
return atoi(&header[8]);
}
void SiglentSCPIOscilloscope::ReadWaveDescriptorBlock(SiglentWaveformDesc_t *descriptor, unsigned int /*channel*/)
{
char header[17] = {0};
uint32_t headerLength = 0;
headerLength = ReadWaveHeader(header);
if(headerLength != sizeof(struct SiglentWaveformDesc_t))
{
LogError("Unexpected header length: %u\n", headerLength);
}
m_transport->ReadRawData(sizeof(struct SiglentWaveformDesc_t), (unsigned char*)descriptor);
// grab the \n
m_transport->ReadReply();
}
bool SiglentSCPIOscilloscope::AcquireData(bool toQueue)
{
lock_guard<recursive_mutex> lock(m_mutex);
LogDebug("Acquire data\n");
double start = GetTime();
//Read the wavedesc for every enabled channel in batch mode first
vector<struct SiglentWaveformDesc_t*> wavedescs;
char tmp[128];
string cmd;
bool enabled[4] = {false};
BulkCheckChannelEnableState();
for(unsigned int i=0; i<m_analogChannelCount; i++)
enabled[i] = IsChannelEnabled(i);
for(unsigned int i=0; i<m_analogChannelCount; i++)
{
wavedescs.push_back(new struct SiglentWaveformDesc_t);
if(enabled[i])
{
m_transport->SendCommand(m_channels[i]->GetHwname() + ":WF? DESC");
ReadWaveDescriptorBlock(wavedescs[i], i);
LogDebug("name %s, number: %u\n",wavedescs[i]->InstrumentName,
wavedescs[i]->InstrumentNumber);
}
}
// grab the actual waveforms
//TODO: WFSU in outer loop and WF in inner loop
unsigned int num_sequences = 1;
for(unsigned int i=0; i<m_analogChannelCount; i++)
{
//If the channel is invisible, don't waste time capturing data
struct SiglentWaveformDesc_t *wavedesc = wavedescs[i];
if(string(wavedesc->DescName).empty())
{
m_channels[i]->SetData(NULL);
continue;
}
//Set up the capture we're going to store our data into
AnalogWaveform* cap = new AnalogWaveform;
//TODO: get sequence count from wavedesc
//TODO: sequence mode should be multiple captures, one per sequence, with some kind of fifo or something?
//Parse the wavedesc headers
LogDebug(" Wavedesc len: %d\n", wavedesc->WaveDescLen);
LogDebug(" Usertext len: %d\n", wavedesc->UserTextLen);
LogDebug(" Trigtime len: %d\n", wavedesc->TriggerTimeArrayLen);
if(wavedesc->TriggerTimeArrayLen != 0)
num_sequences = wavedesc->TriggerTimeArrayLen;
float v_gain = wavedesc->VerticalGain;
float v_off = wavedesc->VerticalOffset;
float interval = wavedesc->HorizontalInterval * 1e12f;
double h_off = wavedesc->HorizontalOffset * 1e12f; //ps from start of waveform to trigger
double h_off_frac = fmodf(h_off, interval); //fractional sample position, in ps
if(h_off_frac < 0)
h_off_frac = interval + h_off_frac;
cap->m_triggerPhase = h_off_frac; //TODO: handle this properly in segmented mode?
//We might have multiple offsets
//double h_unit = *reinterpret_cast<double*>(pdesc + 244);
//Timestamp is a somewhat complex format that needs some shuffling around.
double fseconds = wavedesc->Timestamp.Seconds;
uint8_t seconds = floor(wavedesc->Timestamp.Seconds);
cap->m_startPicoseconds = static_cast<int64_t>( (fseconds - seconds) * 1e12f );
time_t tnow = time(NULL);
struct tm* now = localtime(&tnow);
struct tm tstruc;
tstruc.tm_sec = seconds;
tstruc.tm_min = wavedesc->Timestamp.Minutes;
tstruc.tm_hour = wavedesc->Timestamp.Hours;
tstruc.tm_mday = wavedesc->Timestamp.Days;
tstruc.tm_mon = wavedesc->Timestamp.Months;
tstruc.tm_year = wavedesc->Timestamp.Years;
tstruc.tm_wday = now->tm_wday;
tstruc.tm_yday = now->tm_yday;
tstruc.tm_isdst = now->tm_isdst;
cap->m_startTimestamp = mktime(&tstruc);
cap->m_timescale = round(interval);
for(unsigned int j=0; j<num_sequences; j++)
{
LogDebug("Channel %u block %u\n", i, j);
//Ask for the segment of interest
//(segment number is ignored for non-segmented waveforms)
cmd = "WAVEFORM_SETUP SP,0,NP,0,FP,0,SN,";
if(num_sequences > 1)
{
snprintf(tmp, sizeof(tmp), "%u", j + 1); //segment 0 = "all", 1 = first part of capture
cmd += tmp;
m_transport->SendCommand(cmd);
}
//Read the actual waveform data
cmd = "C1:WF? DAT2";
cmd[1] += i;
m_transport->SendCommand(cmd);
char header[17] = {0};
size_t wavesize = ReadWaveHeader(header);
uint8_t *data = new uint8_t[wavesize];
m_transport->ReadRawData(wavesize, data);
// two \n...
m_transport->ReadReply();
m_transport->ReadReply();
double trigtime = 0;
if( (num_sequences > 1) && (j > 0) )
{
//If a multi-segment capture, ask for the trigger time data
cmd = "C1:WF? TIME";
cmd[1] += i;
m_transport->SendCommand(cmd);
trigtime = ReadWaveHeader(header);
// \n
m_transport->ReadReply();
//double trigoff = ptrigtime[1]; //offset to point 0 from trigger time
}
int64_t trigtime_samples = trigtime * 1e12f / interval;
//int64_t trigoff_samples = trigoff * 1e12f / interval;
//LogDebug(" Trigger time: %.3f sec (%lu samples)\n", trigtime, trigtime_samples);
//LogDebug(" Trigger offset: %.3f sec (%lu samples)\n", trigoff, trigoff_samples);
//If we have samples already in the capture, stretch the final one to our trigger offset
/*
if(cap->m_samples.size())
{
auto& last_sample = cap->m_samples[cap->m_samples.size()-1];
last_sample.m_duration = trigtime_samples - last_sample.m_offset;
}
*/
//Decode the samples
unsigned int num_samples = wavesize;
LogDebug("Got %u samples\n", num_samples);
cap->Resize(num_samples);
for(unsigned int i=0; i<num_samples; i++)
{
cap->m_offsets[i] = i+trigtime_samples;
cap->m_durations[i] = 1;
cap->m_samples[i] = data[i] * v_gain - v_off;
}
}
//Done, update the data
m_channels[i]->SetData(cap);
}
double dt = GetTime() - start;
LogTrace("Waveform download took %.3f ms\n", dt * 1000);
return true;
}