-
Notifications
You must be signed in to change notification settings - Fork 80
/
RigolOscilloscope.cpp
1276 lines (1122 loc) · 36.4 KB
/
RigolOscilloscope.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
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
/***********************************************************************************************************************
* *
* libscopehal v0.1 *
* *
* Copyright (c) 2012-2023 Andrew D. Zonenberg and contributors *
* 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 "RigolOscilloscope.h"
#include "EdgeTrigger.h"
#include <cinttypes>
#ifdef _WIN32
#include <chrono>
#include <thread>
#endif
using namespace std;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Construction / destruction
RigolOscilloscope::RigolOscilloscope(SCPITransport* transport)
: SCPIDevice(transport)
, SCPIInstrument(transport)
, m_triggerArmed(false)
, m_triggerWasLive(false)
, m_triggerOneShot(false)
{
//Last digit of the model number is the number of channels
if(1 != sscanf(m_model.c_str(), "DS%d", &m_modelNumber))
{
if(1 != sscanf(m_model.c_str(), "MSO%d", &m_modelNumber))
{
LogError("Bad model number\n");
return;
}
else
{
m_protocol = MSO5;
// Hacky workaround since :SYST:OPT:STAT doesn't work properly on some scopes
// Only enable chan 1
m_transport->SendCommandQueued("CHAN1:DISP 1\n");
m_transport->SendCommandQueued("CHAN2:DISP 0\n");
if(m_modelNumber % 10 > 2)
{
m_transport->SendCommandQueued("CHAN3:DISP 0\n");
m_transport->SendCommandQueued("CHAN4:DISP 0\n");
}
// Set in run mode to be able to set memory depth
m_transport->SendCommandQueued("RUN\n");
m_transport->SendCommandQueued("ACQ:MDEP 200M\n");
auto reply = Trim(m_transport->SendCommandQueuedWithReply("ACQ:MDEP?\n"));
m_opt200M = reply == "2.0000E+08" ?
true :
false; // Yes, it actually returns a stringified float, manual says "scientific notation"
// Reset memory depth
m_transport->SendCommandQueued("ACQ:MDEP 1M\n");
string originalBandwidthLimit = m_transport->SendCommandQueuedWithReply("CHAN1:BWL?");
// Figure out its actual bandwidth since :SYST:OPT:STAT is practically useless
m_transport->SendCommandQueued("CHAN1:BWL 200M\n");
reply = Trim(m_transport->SendCommandQueuedWithReply("CHAN1:BWL?\n"));
// A bit of a tree, maybe write more beautiful code
if(reply == "200M")
m_bandwidth = 350;
else
{
m_transport->SendCommandQueued("CHAN1:BWL 100M\n");
reply = Trim(m_transport->SendCommandQueuedWithReply("CHAN1:BWL?\n"));
if(reply == "100M")
m_bandwidth = 200;
else
{
if(m_modelNumber % 1000 - m_modelNumber % 10 == 100)
m_bandwidth = 100;
else
m_bandwidth = 70;
}
}
m_transport->SendCommandQueued("CHAN1:BWL " + originalBandwidthLimit);
}
}
else
{
if(m_model.size() >= 7 && (m_model[6] == 'D' || m_model[6] == 'E'))
m_protocol = DS_OLD;
else
m_protocol = DS;
}
// Maybe fix this in a similar manner to bandwidth
int nchans = m_modelNumber % 10;
if(m_protocol != MSO5)
m_bandwidth = m_modelNumber % 1000 - nchans;
for(int i = 0; i < nchans; i++)
{
//Hardware name of the channel
string chname = string("CHAN") + to_string(i + 1);
//Color the channels based on Rigol's standard color sequence (yellow-cyan-red-blue)
string color = "#ffffff";
switch(i)
{
case 0:
color = "#ffff00";
break;
case 1:
color = "#00ffff";
break;
case 2:
color = "#ff00ff";
break;
case 3:
color = "#336699";
break;
}
//Create the channel
auto chan = new OscilloscopeChannel(
this,
chname,
color,
Unit(Unit::UNIT_FS),
Unit(Unit::UNIT_VOLTS),
Stream::STREAM_TYPE_ANALOG,
i);
m_channels.push_back(chan);
chan->SetDefaultDisplayName();
}
m_analogChannelCount = nchans;
//Add the external trigger input
m_extTrigChannel =
new OscilloscopeChannel(
this,
"EX",
"",
Unit(Unit::UNIT_FS),
Unit(Unit::UNIT_VOLTS),
Stream::STREAM_TYPE_TRIGGER,
m_channels.size());
m_channels.push_back(m_extTrigChannel);
m_extTrigChannel->SetDefaultDisplayName();
//Configure acquisition modes
if(m_protocol == DS_OLD)
m_transport->SendCommandQueued(":WAV:POIN:MODE RAW");
else
{
m_transport->SendCommandQueued(":WAV:FORM BYTE");
m_transport->SendCommandQueued(":WAV:MODE RAW");
}
if(m_protocol == MSO5 || m_protocol == DS_OLD)
{
for(size_t i = 0; i < m_analogChannelCount; i++)
m_transport->SendCommandQueued(":" + m_channels[i]->GetHwname() + ":VERN ON");
}
if(m_protocol == MSO5 || m_protocol == DS)
m_transport->SendCommandQueued(":TIM:VERN ON");
FlushConfigCache();
//make sure all setup commands finish before we proceed
m_transport->FlushCommandQueue();
}
RigolOscilloscope::~RigolOscilloscope()
{
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Accessors
unsigned int RigolOscilloscope::GetInstrumentTypes() const
{
return Instrument::INST_OSCILLOSCOPE;
}
uint32_t RigolOscilloscope::GetInstrumentTypesForChannel(size_t /*i*/) const
{
return Instrument::INST_OSCILLOSCOPE;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Device interface functions
string RigolOscilloscope::GetDriverNameInternal()
{
return "rigol";
}
void RigolOscilloscope::FlushConfigCache()
{
lock_guard<recursive_mutex> lock(m_cacheMutex);
m_channelAttenuations.clear();
m_channelCouplings.clear();
m_channelOffsets.clear();
m_channelVoltageRanges.clear();
m_channelsEnabled.clear();
m_channelBandwidthLimits.clear();
m_srateValid = false;
m_mdepthValid = false;
m_triggerOffsetValid = false;
delete m_trigger;
m_trigger = NULL;
}
bool RigolOscilloscope::IsChannelEnabled(size_t i)
{
//ext trigger should never be displayed
if(i == m_extTrigChannel->GetIndex())
return false;
//TODO: handle digital channels, for now just claim they're off
if(i >= m_analogChannelCount)
return false;
{
lock_guard<recursive_mutex> lock(m_cacheMutex);
if(m_channelsEnabled.find(i) != m_channelsEnabled.end())
return m_channelsEnabled[i];
}
auto reply = Trim(m_transport->SendCommandQueuedWithReply(":" + m_channels[i]->GetHwname() + ":DISP?"));
if(reply == "0")
{
lock_guard<recursive_mutex> lock(m_cacheMutex);
m_channelsEnabled[i] = false;
return false;
}
else
{
lock_guard<recursive_mutex> lock(m_cacheMutex);
m_channelsEnabled[i] = true;
return true;
}
}
void RigolOscilloscope::EnableChannel(size_t i)
{
m_transport->SendCommandQueued(":" + m_channels[i]->GetHwname() + ":DISP ON");
// invalidate channel enable cache until confirmed on next IsChannelEnabled
m_channelsEnabled.erase(i);
}
void RigolOscilloscope::DisableChannel(size_t i)
{
m_transport->SendCommandQueued(":" + m_channels[i]->GetHwname() + ":DISP OFF");
// invalidate channel enable cache until confirmed on next IsChannelEnabled
m_channelsEnabled.erase(i);
}
vector<OscilloscopeChannel::CouplingType> RigolOscilloscope::GetAvailableCouplings(size_t /*i*/)
{
vector<OscilloscopeChannel::CouplingType> ret;
ret.push_back(OscilloscopeChannel::COUPLE_DC_1M);
ret.push_back(OscilloscopeChannel::COUPLE_AC_1M);
//TODO: some higher end models do have 50 ohm inputs... which ones?
//ret.push_back(OscilloscopeChannel::COUPLE_DC_50);
ret.push_back(OscilloscopeChannel::COUPLE_GND);
return ret;
}
OscilloscopeChannel::CouplingType RigolOscilloscope::GetChannelCoupling(size_t i)
{
{
lock_guard<recursive_mutex> lock(m_cacheMutex);
if(m_channelCouplings.find(i) != m_channelCouplings.end())
return m_channelCouplings[i];
}
auto reply = Trim(m_transport->SendCommandQueuedWithReply(":" + m_channels[i]->GetHwname() + ":COUP?"));
lock_guard<recursive_mutex> lock(m_cacheMutex);
if(reply == "AC")
m_channelCouplings[i] = OscilloscopeChannel::COUPLE_AC_1M;
else if(reply == "DC")
m_channelCouplings[i] = OscilloscopeChannel::COUPLE_DC_1M;
else /* if(reply == "GND") */
m_channelCouplings[i] = OscilloscopeChannel::COUPLE_GND;
return m_channelCouplings[i];
}
void RigolOscilloscope::SetChannelCoupling(size_t i, OscilloscopeChannel::CouplingType type)
{
bool valid = true;
switch(type)
{
case OscilloscopeChannel::COUPLE_AC_1M:
m_transport->SendCommandQueued(":" + m_channels[i]->GetHwname() + ":COUP AC");
break;
case OscilloscopeChannel::COUPLE_DC_1M:
m_transport->SendCommandQueued(":" + m_channels[i]->GetHwname() + ":COUP DC");
break;
case OscilloscopeChannel::COUPLE_GND:
m_transport->SendCommandQueued(":" + m_channels[i]->GetHwname() + ":COUP GND");
break;
default:
LogError("Invalid coupling for channel\n");
valid = false;
}
if(valid)
{
lock_guard<recursive_mutex> lock2(m_cacheMutex);
m_channelCouplings[i] = type;
}
}
double RigolOscilloscope::GetChannelAttenuation(size_t i)
{
{
lock_guard<recursive_mutex> lock(m_cacheMutex);
if(m_channelAttenuations.find(i) != m_channelAttenuations.end())
return m_channelAttenuations[i];
}
auto reply = Trim(m_transport->SendCommandQueuedWithReply(":" + m_channels[i]->GetHwname() + ":PROB?"));
double atten;
sscanf(reply.c_str(), "%lf", &atten);
lock_guard<recursive_mutex> lock(m_cacheMutex);
m_channelAttenuations[i] = atten;
return atten;
}
void RigolOscilloscope::SetChannelAttenuation(size_t i, double atten)
{
bool valid = true;
switch((int)(atten * 10000 +
0.1)) //+ 0.1 in case atten is for example 0.049999 or so, to round it to 0.05 which turns to an int of 500
{
case 1:
m_transport->SendCommandQueued(m_channels[i]->GetHwname() + ":PROB 0.0001");
break;
case 2:
m_transport->SendCommandQueued(m_channels[i]->GetHwname() + ":PROB 0.0002");
break;
case 5:
m_transport->SendCommandQueued(m_channels[i]->GetHwname() + ":PROB 0.0005");
break;
case 10:
m_transport->SendCommandQueued(m_channels[i]->GetHwname() + ":PROB 0.001");
break;
case 20:
m_transport->SendCommandQueued(m_channels[i]->GetHwname() + ":PROB 0.002");
break;
case 50:
m_transport->SendCommandQueued(m_channels[i]->GetHwname() + ":PROB 0.005");
break;
case 100:
m_transport->SendCommandQueued(m_channels[i]->GetHwname() + ":PROB 0.01");
break;
case 200:
m_transport->SendCommandQueued(m_channels[i]->GetHwname() + ":PROB 0.02");
break;
case 500:
m_transport->SendCommandQueued(m_channels[i]->GetHwname() + ":PROB 0.05");
break;
case 1000:
m_transport->SendCommandQueued(m_channels[i]->GetHwname() + ":PROB 0.1");
break;
case 2000:
m_transport->SendCommandQueued(m_channels[i]->GetHwname() + ":PROB 0.2");
break;
case 5000:
m_transport->SendCommandQueued(m_channels[i]->GetHwname() + ":PROB 0.5");
break;
case 10000:
m_transport->SendCommandQueued(m_channels[i]->GetHwname() + ":PROB 1");
break;
case 20000:
m_transport->SendCommandQueued(m_channels[i]->GetHwname() + ":PROB 2");
break;
case 50000:
m_transport->SendCommandQueued(m_channels[i]->GetHwname() + ":PROB 5");
break;
case 100000:
m_transport->SendCommandQueued(m_channels[i]->GetHwname() + ":PROB 10");
break;
case 200000:
m_transport->SendCommandQueued(m_channels[i]->GetHwname() + ":PROB 20");
break;
case 500000:
m_transport->SendCommandQueued(m_channels[i]->GetHwname() + ":PROB 50");
break;
case 1000000:
m_transport->SendCommandQueued(m_channels[i]->GetHwname() + ":PROB 100");
break;
case 2000000:
m_transport->SendCommandQueued(m_channels[i]->GetHwname() + ":PROB 200");
break;
case 5000000:
m_transport->SendCommandQueued(m_channels[i]->GetHwname() + ":PROB 500");
break;
case 10000000:
m_transport->SendCommandQueued(m_channels[i]->GetHwname() + ":PROB 1000");
break;
case 20000000:
m_transport->SendCommandQueued(m_channels[i]->GetHwname() + ":PROB 2000");
break;
case 50000000:
m_transport->SendCommandQueued(m_channels[i]->GetHwname() + ":PROB 5000");
break;
case 100000000:
m_transport->SendCommandQueued(m_channels[i]->GetHwname() + ":PROB 10000");
break;
case 200000000:
m_transport->SendCommandQueued(m_channels[i]->GetHwname() + ":PROB 20000");
break;
case 500000000:
m_transport->SendCommandQueued(m_channels[i]->GetHwname() + ":PROB 50000");
break;
default:
LogError("Invalid attenuation for channel\n");
valid = false;
}
if(valid)
{
lock_guard<recursive_mutex> lock2(m_cacheMutex);
m_channelAttenuations[i] = (int)(atten * 10000 + 0.1) * 0.0001;
}
}
vector<unsigned int> RigolOscilloscope::GetChannelBandwidthLimiters(size_t /*i*/)
{
vector<unsigned int> ret;
if(m_protocol == MSO5)
{
switch(m_bandwidth)
{
case 70:
case 100:
ret = {20, 0};
break;
case 200:
ret = {20, 100, 0};
break;
case 350:
ret = {20, 100, 200, 0};
break;
default:
LogError("Invalid model bandwidth\n");
}
}
//For now, all known DS series models only support 20 MHz or off
else if(m_protocol == DS)
{
ret = {20, 0};
}
return ret;
}
unsigned int RigolOscilloscope::GetChannelBandwidthLimit(size_t i)
{
{
lock_guard<recursive_mutex> lock(m_cacheMutex);
if(m_channelBandwidthLimits.find(i) != m_channelBandwidthLimits.end())
return m_channelBandwidthLimits[i];
}
auto reply = Trim(m_transport->SendCommandQueuedWithReply(m_channels[i]->GetHwname() + ":BWL?"));
lock_guard<recursive_mutex> lock2(m_cacheMutex);
if(reply == "20M")
m_channelBandwidthLimits[i] = 20;
if(reply == "100M")
m_channelBandwidthLimits[i] = 100;
if(reply == "200M")
m_channelBandwidthLimits[i] = 200;
else
m_channelBandwidthLimits[i] = m_bandwidth;
return m_channelBandwidthLimits[i];
}
void RigolOscilloscope::SetChannelBandwidthLimit(size_t i, unsigned int limit_mhz)
{
bool valid = true;
if(m_protocol == MSO5)
{
switch(m_bandwidth)
{
case 70:
case 100:
if((limit_mhz <= 20) & (limit_mhz != 0))
m_transport->SendCommandQueued(m_channels[i]->GetHwname() + ":BWL 20M");
else
m_transport->SendCommandQueued(m_channels[i]->GetHwname() + ":BWL OFF");
break;
case 200:
if((limit_mhz <= 20) & (limit_mhz != 0))
m_transport->SendCommandQueued(m_channels[i]->GetHwname() + ":BWL 20M");
else if((limit_mhz <= 100) & (limit_mhz != 0))
m_transport->SendCommandQueued(m_channels[i]->GetHwname() + ":BWL 100M");
else
m_transport->SendCommandQueued(m_channels[i]->GetHwname() + ":BWL OFF");
break;
case 350:
if((limit_mhz <= 20) & (limit_mhz != 0))
m_transport->SendCommandQueued(m_channels[i]->GetHwname() + ":BWL 20M");
else if((limit_mhz <= 100) & (limit_mhz != 0))
m_transport->SendCommandQueued(m_channels[i]->GetHwname() + ":BWL 100M");
else if((limit_mhz <= 200) & (limit_mhz != 0))
m_transport->SendCommandQueued(m_channels[i]->GetHwname() + ":BWL 200M");
else
m_transport->SendCommandQueued(m_channels[i]->GetHwname() + ":BWL OFF");
break;
default:
LogError("Invalid model number\n");
valid = false;
}
}
else if(m_protocol == DS)
{
if((limit_mhz <= 20) & (limit_mhz != 0))
m_transport->SendCommandQueued(m_channels[i]->GetHwname() + ":BWL 20M");
else
m_transport->SendCommandQueued(m_channels[i]->GetHwname() + ":BWL OFF");
}
else
LogError("unimplemented SetChannelBandwidth for this model\n");
if(valid)
{
lock_guard<recursive_mutex> lock2(m_cacheMutex);
if(limit_mhz == 0)
m_channelBandwidthLimits[i] = m_bandwidth; // max
else if(limit_mhz <= 20)
m_channelBandwidthLimits[i] = 20;
else if(m_bandwidth == 70)
m_channelBandwidthLimits[i] = 70;
else if((limit_mhz <= 100) | (m_bandwidth == 100))
m_channelBandwidthLimits[i] = 100;
else if((limit_mhz <= 200) | (m_bandwidth == 200))
m_channelBandwidthLimits[i] = 200;
else
m_channelBandwidthLimits[i] = m_bandwidth; // 350 MHz
}
}
float RigolOscilloscope::GetChannelVoltageRange(size_t i, size_t /*stream*/)
{
{
lock_guard<recursive_mutex> lock(m_cacheMutex);
if(m_channelVoltageRanges.find(i) != m_channelVoltageRanges.end())
return m_channelVoltageRanges[i];
}
string reply;
if(m_protocol == DS)
reply = Trim(m_transport->SendCommandQueuedWithReply(":" + m_channels[i]->GetHwname() + ":RANGE?"));
else if(m_protocol == MSO5 || m_protocol == DS_OLD)
reply = Trim(m_transport->SendCommandQueuedWithReply(":" + m_channels[i]->GetHwname() + ":SCALE?"));
float range;
sscanf(reply.c_str(), "%f", &range);
lock_guard<recursive_mutex> lock(m_cacheMutex);
if(m_protocol == MSO5)
range = 8 * range;
if(m_protocol == DS_OLD)
range = 10 * range;
m_channelVoltageRanges[i] = range;
return range;
}
void RigolOscilloscope::SetChannelVoltageRange(size_t i, size_t /*stream*/, float range)
{
{
lock_guard<recursive_mutex> lock2(m_cacheMutex);
m_channelVoltageRanges[i] = range;
}
if(m_protocol == DS)
m_transport->SendCommandQueued(":" + m_channels[i]->GetHwname() + ":RANGE " + to_string(range));
else if(m_protocol == MSO5 || m_protocol == DS_OLD)
m_transport->SendCommandQueued(":" + m_channels[i]->GetHwname() + ":SCALE " + to_string(range/8));
}
OscilloscopeChannel* RigolOscilloscope::GetExternalTrigger()
{
//FIXME
return nullptr;
}
float RigolOscilloscope::GetChannelOffset(size_t i, size_t /*stream*/)
{
{
lock_guard<recursive_mutex> lock(m_cacheMutex);
if(m_channelOffsets.find(i) != m_channelOffsets.end())
return m_channelOffsets[i];
}
auto reply = Trim(m_transport->SendCommandQueuedWithReply(":" + m_channels[i]->GetHwname() + ":OFFS?"));
float offset;
sscanf(reply.c_str(), "%f", &offset);
lock_guard<recursive_mutex> lock(m_cacheMutex);
m_channelOffsets[i] = offset;
return offset;
}
void RigolOscilloscope::SetChannelOffset(size_t i, size_t /*stream*/, float offset)
{
m_transport->SendCommandQueued(":" + m_channels[i]->GetHwname() + ":OFFS " + to_string(offset));
lock_guard<recursive_mutex> lock(m_cacheMutex);
m_channelOffsets[i] = offset;
}
Oscilloscope::TriggerMode RigolOscilloscope::PollTrigger()
{
auto stat = Trim(m_transport->SendCommandQueuedWithReply(":TRIG:STAT?"));
if(stat != "STOP")
m_triggerWasLive = true;
if(stat == "TD")
return TRIGGER_MODE_TRIGGERED;
else if(stat == "RUN")
return TRIGGER_MODE_RUN;
else if(stat == "WAIT")
return TRIGGER_MODE_WAIT;
else if(stat == "AUTO")
return TRIGGER_MODE_AUTO;
else
{
//The "TD" state is not sticky on Rigol scopes, unlike the equivalent LeCroy status register bit.
//The scope will go from "run" to "stop" state on trigger with only a momentary pass through "TD".
//If we armed the trigger recently and we're now stopped, this means we must have triggered.
if(m_triggerArmed && (m_protocol != DS_OLD || m_triggerWasLive))
{
m_triggerArmed = false;
m_triggerWasLive = false;
return TRIGGER_MODE_TRIGGERED;
}
//Nope, we're actually stopped
return TRIGGER_MODE_STOP;
}
}
bool RigolOscilloscope::AcquireData()
{
//LogDebug("Acquiring data\n");
lock_guard<recursive_mutex> lock(m_transport->GetMutex());
LogIndenter li;
//Grab the analog waveform data
int unused1;
int unused2;
size_t npoints;
int unused3;
double sec_per_sample;
double xorigin;
double xreference;
double yincrement;
double yorigin;
double yreference;
size_t maxpoints = 250 * 1000;
if(m_protocol == DS)
maxpoints = 250 * 1000;
else if(m_protocol == DS_OLD)
maxpoints = 8192; // FIXME
else if(m_protocol == MSO5)
maxpoints = GetSampleDepth(); //You can use 250E6 points too, but it is very slow
unsigned char* temp_buf = new unsigned char[maxpoints + 1];
map<int, vector<UniformAnalogWaveform*>> pending_waveforms;
for(size_t i = 0; i < m_analogChannelCount; i++)
{
if(!IsChannelEnabled(i))
continue;
//LogDebug("Channel %zu\n", i);
int64_t fs_per_sample = 0;
if(m_protocol == DS_OLD)
{
yreference = 0;
npoints = maxpoints;
yincrement = GetChannelVoltageRange(i, 0) / 256.0f;
yorigin = GetChannelOffset(i, 0);
auto reply = Trim(m_transport->SendCommandQueuedWithReply(":" + m_channels[i]->GetHwname() + ":OFFS?"));
sscanf(reply.c_str(), "%lf", &yorigin);
/* for these scopes, this is seconds per div */
reply = Trim(m_transport->SendCommandQueuedWithReply(":TIM:SCAL?"));
sscanf(reply.c_str(), "%lf", &sec_per_sample);
fs_per_sample = (sec_per_sample * 12 * FS_PER_SECOND) / npoints;
}
else
{
m_transport->SendCommandQueued(string("WAV:SOUR ") + m_channels[i]->GetHwname());
//This is basically the same function as a LeCroy WAVEDESC, but much less detailed
auto reply = Trim(m_transport->SendCommandQueuedWithReply("WAV:PRE?"));
//LogDebug("Preamble = %s\n", reply.c_str());
sscanf(reply.c_str(),
"%d,%d,%zu,%d,%lf,%lf,%lf,%lf,%lf,%lf",
&unused1,
&unused2,
&npoints,
&unused3,
&sec_per_sample,
&xorigin,
&xreference,
&yincrement,
&yorigin,
&yreference);
fs_per_sample = round(sec_per_sample * FS_PER_SECOND);
//LogDebug("X: %d points, %f origin, ref %f fs/sample %ld\n", npoints, xorigin, xreference, fs_per_sample);
//LogDebug("Y: %f inc, %f origin, %f ref\n", yincrement, yorigin, yreference);
}
//If we have zero points in the reply, skip reading data from this channel
if(npoints == 0)
continue;
//Set up the capture we're going to store our data into
auto cap = AllocateAnalogWaveform(m_nickname + "." + GetChannel(i)->GetHwname());
cap->Resize(0);
cap->m_timescale = fs_per_sample;
cap->m_triggerPhase = 0;
cap->m_startTimestamp = time(NULL);
double t = GetTime();
cap->m_startFemtoseconds = (t - floor(t)) * FS_PER_SECOND;
//Downloading the waveform is a pain in the butt, because we can only pull 250K points at a time! (Unless you have a MSO5)
for(size_t npoint = 0; npoint < npoints;)
{
if(m_protocol == MSO5)
{
//Ask for the data block
m_transport->SendCommandQueued("*WAI");
m_transport->SendCommandQueued("WAV:DATA?");
}
else if(m_protocol == DS_OLD)
{
m_transport->SendCommandQueued(string(":WAV:DATA? ") + m_channels[i]->GetHwname());
}
else
{
//Ask for the data
m_transport->SendCommandQueued(string("WAV:STAR ") + to_string(npoint+1)); //ONE based indexing WTF
size_t end = npoint + maxpoints;
if(end > npoints)
end = npoints;
m_transport->SendCommandQueued(string("WAV:STOP ") + to_string(end)); //Here it is zero based, so it gets from 1-1000
//Ask for the data block
m_transport->SendCommandQueued("WAV:DATA?");
}
m_transport->FlushCommandQueue();
//Read block header, should be maximally 11 long on MSO5 scope with >= 100 MPoints
unsigned char header[12] = {0};
unsigned char header_size;
m_transport->ReadRawData(2, header);
//LogWarning("Time %f\n", (GetTime() - start));
sscanf((char*)header, "#%c", &header_size);
header_size = header_size - '0';
if (header_size > 12)
{
header_size = 12;
}
m_transport->ReadRawData(header_size, header);
//Look up the block size
//size_t blocksize = end - npoints;
//LogDebug("Block size = %zu\n", blocksize);
size_t header_blocksize;
sscanf((char*)header, "%zu", &header_blocksize);
//LogDebug("Header block size = %zu\n", header_blocksize);
if(header_blocksize == 0)
{
LogWarning("Ran out of data after %zu points\n", npoint);
m_transport->ReadRawData(1, temp_buf); //discard the trailing newline
//If this happened after zero samples, free the waveform so it doesn't leak
if(npoint == 0)
{
AddWaveformToAnalogPool(cap);
cap = nullptr;
}
break;
}
if (header_blocksize > maxpoints)
{
header_blocksize = maxpoints;
}
//Read actual block content and decode it
//Scale: (value - Yorigin - Yref) * Yinc
m_transport->ReadRawData(header_blocksize + 1, temp_buf); //trailing newline after data block
double ydelta = yorigin + yreference;
cap->Resize(cap->m_samples.size() + header_blocksize);
cap->PrepareForCpuAccess();
for(size_t j = 0; j < header_blocksize; j++)
{
float v = (static_cast<float>(temp_buf[j]) - ydelta) * yincrement;
if(m_protocol == DS_OLD)
v = (128 - static_cast<float>(temp_buf[j])) * yincrement - ydelta;
//LogDebug("V = %.3f, temp=%d, delta=%f, inc=%f\n", v, temp_buf[j], ydelta, yincrement);
cap->m_samples[npoint + j] = v;
}
cap->MarkSamplesModifiedFromCpu();
npoint += header_blocksize;
}
//Done, update the data
if(cap)
pending_waveforms[i].push_back(cap);
}
//Now that we have all of the pending waveforms, save them in sets across all channels
m_pendingWaveformsMutex.lock();
size_t num_pending = 1; //TODO: segmented capture support
for(size_t i = 0; i < num_pending; i++)
{
SequenceSet s;
for(size_t j = 0; j < m_analogChannelCount; j++)
{
if(pending_waveforms.count(j) > 0)
s[GetOscilloscopeChannel(j)] = pending_waveforms[j][i];
}
m_pendingWaveforms.push_back(s);
}
m_pendingWaveformsMutex.unlock();
//Clean up
delete[] temp_buf;
//TODO: support digital channels
//Re-arm the trigger if not in one-shot mode
if(!m_triggerOneShot)
{
if(m_protocol == DS_OLD)
{
m_transport->SendCommandQueued(":STOP");
m_transport->SendCommandQueued(":TRIG:EDGE:SWE SING");
m_transport->SendCommandQueued(":RUN");
}
else
{
m_transport->SendCommandQueued(":SING");
m_transport->SendCommandQueued("*WAI");
}
m_triggerArmed = true;
}
//LogDebug("Acquisition done\n");
return true;
}
void RigolOscilloscope::Start()
{
//LogDebug("Start single trigger\n");
if(m_protocol == DS_OLD)
{
m_transport->SendCommandQueued(":TRIG:EDGE:SWE SING");
m_transport->SendCommandQueued(":RUN");
}
else
{
m_transport->SendCommandQueued(":SING");
m_transport->SendCommandQueued("*WAI");
}
m_triggerArmed = true;
m_triggerOneShot = false;
}
void RigolOscilloscope::StartSingleTrigger()
{
if(m_protocol == DS_OLD)
{
m_transport->SendCommandQueued(":TRIG:EDGE:SWE SING");
m_transport->SendCommandQueued(":RUN");
}
else
{
m_transport->SendCommandQueued(":SING");
m_transport->SendCommandQueued("*WAI");
}
m_triggerArmed = true;
m_triggerOneShot = true;
}
void RigolOscilloscope::Stop()
{
m_transport->SendCommandQueued(":STOP");
m_triggerArmed = false;
m_triggerOneShot = true;
}
void RigolOscilloscope::ForceTrigger()
{
if(m_protocol == DS)
m_transport->SendCommandQueued(":TFOR");
else
LogError("RigolOscilloscope::ForceTrigger not implemented for this model\n");
}
bool RigolOscilloscope::IsTriggerArmed()
{
return m_triggerArmed;
}
vector<uint64_t> RigolOscilloscope::GetSampleRatesNonInterleaved()
{
//FIXME
vector<uint64_t> ret;
if(m_protocol == MSO5)
{
ret =
{
100,
200,
500,
1000,
2000,
5000,
10 * 1000,
20 * 1000,
50 * 1000,
100 * 1000,
200 * 1000,
500 * 1000,
1 * 1000 * 1000,
2 * 1000 * 1000,
5 * 1000 * 1000,
10 * 1000 * 1000,
20 * 1000 * 1000,
50 * 1000 * 1000,
100 * 1000 * 1000,
200 * 1000 * 1000,
500 * 1000 * 1000,
1 * 1000 * 1000 * 1000,
2 * 1000 * 1000 * 1000,
};
}