-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathUSB_EZ_interface.cpp
1017 lines (892 loc) · 27.8 KB
/
USB_EZ_interface.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
//
// Copyright 2004-2007, Thomas C. McDermott, N5EG
// This file is part of VNAR - the Vector Network Analyzer program.
//
// VNAR is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// VNAR is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with VNAR, if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
/// \author Thomas C. McDermott
#pragma once
// Encapsulate the EZUSBSYS device as a .NET managed object
//
// Updated: 4-4-03 to use pinned pointer for code download
//
// Updated: 7-8-03 for the .NET 2003 compiler to avoid exposing
// any methods or variables to the caller via the header file.
// It ends up being messy as a result...
//
// 2-22-04 MS VC++ posting says that using command switch
// /d1PrivateNativeTypes will revert to 2002 behavior of not making
// the native types (unmanaged types) public. This might avoid the
// problem. KB article #822330
//
// Updated: 3-10-04 to use Helper class and forward reference in the .H
// file to sidestep the problem, pin the entire Helper object with one pointer
//
#include "stdafx.h"
#include "objbase.h"
#include "DataDisplay.h"
#include "Mockup.h"
#include "Constants.h"
#include "AudioInput.h"
#using <mscorlib.dll>
#include <math.h>
#include <cstdio>
#include <complex>
using namespace System;
using namespace System::Windows::Forms;
//volatile float magSig;
//volatile float phaseSig;
extern "C"
{
// Declare USB constants and structures
#include "winioctl.h" /// IOCTL definitions
// #include "c:\ntddk\inc\usb100.h" /// general USB
#pragma warning(disable: 4200) // Supress C4200 warning in Microsoft-supplied headers
// #include "c:\ntddk\inc\usbdi.h" // USB Device Interface
#pragma warning(default: 4200)
// #include "c:\ntddk\inc\devioctl.h" /// Device IOCTL definitions
// #include "C:\Cypress\USB\Drivers\ezusbdrv\ezusbsys.h" /// Ezusb IOCTL codes
}
#include "USB_EZ_interface.h"
#define USB_STRING char[256]
#define USB_CONFIG_DESCR unsigned short[1024]
#define DDSToFreq(X) (long)(((double)(X) * 288000000.0 / (4294967296.0 * 65536.0 ) + 0.5 ) )
/// Holds the USB device state while hiding it from other code
/// Helper is not on the managed heap, and requires explicit lifetime control.
/// Neither it nor it's contents get moved in memory by the garbage collector
/*__nogc*/ class Helper
#define BULK_TRANSFER_CONTROL int
#define USB_DEVICE_DESCRIPTOR int
#define EZUSB_DRIVER_VERSION int
#define GET_STRING_DESCRIPTOR_IN char
#define VENDOR_REQUEST_IN int
{
public:
BULK_TRANSFER_CONTROL * pInPipe; ///< USB Bulk Transfer Input Pipe
BULK_TRANSFER_CONTROL * pOutPipe; ///< USB Bulk Transfer Output Pipe
USB_DEVICE_DESCRIPTOR * pDevDescr; ///< USB Device Descriptor
EZUSB_DRIVER_VERSION * pDrvVers; ///< EZUSB Driver Version
GET_STRING_DESCRIPTOR_IN * pStrDescr; ///< VNA Device String Descriptor
UCHAR * pInterfaceInfo; ///< Interface Information Structure
unsigned short * pConfigDescr; ///< Configuration Descriptor
char * pDevString; ///< USB Device Identifier, string version
HANDLE DevDrvHandle; ///< Device Handle
LPDWORD pBytesReturned; ///< Size of return buffer from EzUSB calls
Helper()
{
pInPipe = new BULK_TRANSFER_CONTROL;
pOutPipe = new BULK_TRANSFER_CONTROL;
pDevDescr = new USB_DEVICE_DESCRIPTOR;
pDrvVers = new EZUSB_DRIVER_VERSION;
pStrDescr = new GET_STRING_DESCRIPTOR_IN;
pInterfaceInfo = new UCHAR[1024];
pDevString = new USB_STRING;
pConfigDescr = new USB_CONFIG_DESCR;
pBytesReturned = new DWORD;
}
~Helper()
{
delete pInPipe;
delete pOutPipe;
delete pDevDescr;
delete pDrvVers;
delete pStrDescr;
delete pInterfaceInfo;
delete pDevString;
delete pConfigDescr;
delete pBytesReturned;
}
};
/// Find USB device, acquire handle
void VNADevice::GetHandle(void)
{
// HANDLE hDevice;
// char __pin * usbdev0 = "\\\\.\\ezusb-0"; // device 0
// char __pin * usbdev1 = "\\\\.\\ezusb-1"; // device 1
pin_ptr<char> usbdev0 = "\\\\.\\ezusb-0"; // device 0
pin_ptr<char> usbdev1 = "\\\\.\\ezusb-1"; // device 1
#if 0
hDevice = CreateFile(usbdev0, // try device 0
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
if (hDevice==INVALID_HANDLE_VALUE)
{
hDevice = CreateFile(usbdev1, // try device 1
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
}
if (hDevice==INVALID_HANDLE_VALUE)
state = -1; // open failed
else
#endif
state = 1; // open succeded
// d->DevDrvHandle = hDevice;
};
/// Release handle to USB device
void VNADevice::ReleaseHandle(void)
{
#if 0
if (state == 1)
CloseHandle(d->DevDrvHandle);
#endif
};
/// Set or Release RESET on the Ez-USB processor
bool VNADevice::ToggleReset(bool hold)
{
// use the vendor request type to set/release the reset register in the 8051
// VENDOR_REQUEST_IN __pin * pRequest = new VENDOR_REQUEST_IN;
pin_ptr<VENDOR_REQUEST_IN> pRequest = new VENDOR_REQUEST_IN;
#if 0
pRequest->bRequest = 0xA0; // Anchorchips Vendor Request Type
pRequest->wValue = CPUCS_REG_EZUSB; // 8051 Control / Status Register
pRequest->wIndex = 0x00;
pRequest->wLength = 0x01;
pRequest->bData = (hold) ? 1 : 0; // 1 holds 8051 in reset, 0 starts 8051 (at 0x0000)
pRequest->direction = 0x00;
GetHandle();
Result = (DeviceIoControl((HANDLE)d->DevDrvHandle,
IOCTL_Ezusb_VENDOR_REQUEST,
pRequest,
sizeof(VENDOR_REQUEST_IN),
NULL,
0,
d->pBytesReturned,
NULL) !=0); // casts BOOL (which is typedef'd to int) to bool
ReleaseHandle();
#endif
// return(Result);
return(true);
};
/// Construct the VNADevice
VNADevice::VNADevice(System::IO::Ports::SerialPort^ port)
{
d = new Helper; // Allocate a Helper to the VNADevice
GetHandle();
serialPort = port;
ReleaseHandle();
mode = 0;
lastFreq = 0;
lastDir = -1;
hardware = HW_MOCKUP;
hasScanCommand = false;
minHWFreq = 500000;
maxHWFreq = 900000000;
audioRefLevel = 0.0;
S11Real = gcnew array<Double>(1200);
S11Imag = gcnew array<Double>(1200);
S21Real = gcnew array<Double>(1200);
S21Imag = gcnew array<Double>(1200);
};
/// Destructor for VNADevice
VNADevice::~VNADevice()
{
delete d; // since d is on the unmanaged heap
}
/// Build Device Descriptors and Pipes for USB device
bool VNADevice::Init(void)
{
GetHandle();
// this->SetFreq(50000000L, true);
// Code between these two markers is new experimental code
//Result = DeviceIoControl((HANDLE)d->DevDrvHandle,
// IOCTL_Ezusb_GET_DEVICE_DESCRIPTOR,
// NULL,
// 0,
// d->pDevDescr,
// sizeof(USB_DEVICE_DESCRIPTOR),
// d->pBytesReturned,
// NULL);
//Result = DeviceIoControl((HANDLE)d->DevDrvHandle,
// IOCTL_EZUSB_GET_DRIVER_VERSION,
// NULL,
// 0,
// d->pDrvVers,
// sizeof(EZUSB_DRIVER_VERSION),
// d->pBytesReturned,
// NULL);
//Result = DeviceIoControl((HANDLE)d->DevDrvHandle,
// IOCTL_Ezusb_GET_CONFIGURATION_DESCRIPTOR,
// NULL,
// 0,
// d->pConfigDescr,
// sizeof(USB_CONFIG_DESCR),
// d->pBytesReturned,
// NULL);
//d->pStrDescr->Index = 1;
//d->pStrDescr->LanguageId = 27;
//Result = DeviceIoControl((HANDLE)d->DevDrvHandle, // gets a UNICODE string
// IOCTL_Ezusb_GET_STRING_DESCRIPTOR,
// d->pStrDescr,
// sizeof(GET_STRING_DESCRIPTOR_IN),
// d->pDevString,
// sizeof(USB_STRING),
// d->pBytesReturned,
// NULL);
//Result = DeviceIoControl((HANDLE)d->DevDrvHandle,
// IOCTL_Ezusb_GET_PIPE_INFO,
// NULL,
// 0,
// d->pInterfaceInfo,
// sizeof(d->pInterfaceInfo),
// d->pBytesReturned,
// NULL);
// BUGBUG Something before this point corrupts memory and crashes Windows - thus it's removed
//// The following code has not been modified to the pinned object style of coding...
////pPipeInfo = (PUSBD_INTERFACE_INFORMATION) pInterfaceInfo;
////pInPipe->pipeNum = 0; // most likely
////pOutPipe->pipeNum = 1; // most likely
////for (int i=0; i<static_cast<int>(pPipeInfo->NumberOfPipes); i++) // Identify pipes we need
////{
//// if (pPipeInfo->Pipes[i].EndpointAddress == 0x02) //EP2-Out
//// pOutPipe->pipeNum = i;
//// if (pPipeInfo->Pipes[i].EndpointAddress == 0x82) //EP2-In
//// pInPipe->pipeNum = i;
////}
// End experimental code marker
ReleaseHandle();
//return(i!=0); // success if #pipes > zero
return(true); // guarantee success
};
/// State of USB device
int VNADevice::State() {return state;};
/// Release reset on the 8051 processor
bool VNADevice::Start() { return(ToggleReset(0)); };
/// Halt the 8051 processor
bool VNADevice::Stop() { return(ToggleReset(1)); };
/// Download code to 8051 at Address
bool VNADevice::Download(array<System::Byte> ^Codebuffer, int CodeSize, unsigned short Address )
{
// ANCHOR_DOWNLOAD_CONTROL __pin * pDownAddr = new ANCHOR_DOWNLOAD_CONTROL;
// unsigned char __pin *pB = &Codebuffer[0]; // pB is a pinned pointer (doesn't get moved in memory).
// It's needed when passing a pointer to an unmanaged external DLL.
// Codebuffer gets unpinned when pB goes out of scope, or
// gets assigned a value of zero.
#if 0
pin_ptr<ANCHOR_DOWNLOAD_CONTROL> pDownAddr = new ANCHOR_DOWNLOAD_CONTROL;
pin_ptr<System::Byte> pB = &Codebuffer[0];
pDownAddr->Offset = Address;
GetHandle();
Result = (DeviceIoControl((HANDLE)d->DevDrvHandle,
IOCTL_EZUSB_ANCHOR_DOWNLOAD,
pDownAddr,
sizeof(ANCHOR_DOWNLOAD_CONTROL),
(LPVOID)pB,
CodeSize,
d->pBytesReturned,
NULL) !=0); // casts BOOL (which is typedef'd to int) to bool
ReleaseHandle();
#endif
return(true);
// return(Result);
};
/// Read data from BULK endpoint
bool VNADevice::Read(VNA_RXBUFFER * readbuf)
{
// void __pin * rb = readbuf; // pin the readbuf in memory
#if 0
pin_ptr<VNA_RXBUFFER> rb = readbuf; // pin the readbuf in memory
d->pInPipe->pipeNum = 0; // most likely
GetHandle();
Result = (DeviceIoControl((HANDLE)d->DevDrvHandle,
IOCTL_EZUSB_BULK_READ,
d->pInPipe,
sizeof(BULK_TRANSFER_CONTROL),
rb, // readbuf
64,
d->pBytesReturned,
NULL) !=0); // casts BOOL (which is typedef'd to int) to bool
ReleaseHandle();
#endif
// return(Result);
return(true);
};
/// Write data to BULK endpoint
bool VNADevice::Write(VNA_TXBUFFER * writebuf)
{
// void __pin * wb = writebuf; // pin the writebuf in memory
#if 0
pin_ptr<VNA_TXBUFFER> wb = writebuf; // pin the writebuf in memory
d->pOutPipe->pipeNum = 1; // most likely
GetHandle();
Result = (DeviceIoControl((HANDLE)d->DevDrvHandle,
IOCTL_EZUSB_BULK_WRITE,
d->pOutPipe,
sizeof(BULK_TRANSFER_CONTROL),
wb, // writebuf
64,
d->pBytesReturned,
NULL) !=0); // casts BOOL (which is typedef'd to int) to bool
ReleaseHandle();
#endif
// return(Result);
return(true);
};
void VNADevice::Sweep(__int64 startF, __int64 stepF, int numPoints, int duration, System::Windows::Forms::ProgressBar^ SweepProgressBar)
{
Sweep(startF, stepF, numPoints, duration,SweepProgressBar, false);
}
static __int64 oldstartF=0;
static __int64 oldstepF=0;
static __int64 oldnumPoints=0;
bool VNADevice::Sweep(__int64 startF, __int64 stepF, int numPoints, int duration, System::Windows::Forms::ProgressBar^ SweepProgressBar, int power)
{
String ^s;
array<String ^>^ sa, ^ss;
// String ^ t;
if (hardware != HW_NANOVNA && hardware != HW_NANOV2 ) {
SetAudioPower(power);
ArmAudio(numPoints,serialPort);
}
dur = duration;
if (hardware == HW_MOCKUP) {
// if (!power) dur += 2; // add 2 duration for lead in and out
StartAudioSimulation(mode, numPoints + 10, dur, startF, stepF, cable_before, cable_after, 0, resistance, capacitance, sourcecapacitance, inductance, noise);
} else {
try {
// t = String::Format("0 {0} {1} {2} ", startF, numPoints+5, stepF);
// serialPort->WriteLine(String::Format("0 1000000 1 0 ", startF, numPoints, stepF));
// Sleep(200);
if (!serialPort->IsOpen) {
if (!FindVNA())
return false;
}
if (hardware == HW_NANOVNA) {
// serialPort->WriteLine(String::Format("resume"));
// Sleep(20);
// s = serialPort->ReadLine();
// s = serialPort->ReadExisting();
if (hasScanCommand) {
// Perform(String::Format("scan {0} {1} {2}",startF, stepF, numPoints));
#if 1
// sa = Perform(String::Format("offset {0}",IFREQ));
s = serialPort->ReadExisting(); // In case previous scan was aborted
serialPort->WriteLine(String::Format("scanraw 2 {0} {1} {2} {3}",startF, stepF, numPoints, duration));
// serialPort->WriteLine(String::Format("scanraw {0} {1} {2} {3}",startF, stepF, numPoints, duration));
s = serialPort->ReadLine();
// Sleep(20);
// s = serialPort->ReadExisting();
#endif
} else {
int base = 0;
while (numPoints > 0) {
int startIndex;
// sa = Perform(String::Format("freq {0}",startF)); // Stop sweeping
sa = Perform(String::Format("sweep {0} {1}",startF, startF+100*stepF, 101));
// sa = Perform("resume");
Sleep(800);
// sa = Perform("pause");
again_1:
sa = Perform("data 0");
startIndex = 1;
if (sa->Length == 102) {
startIndex = 0;
} else if (sa->Length != 103)
goto again_1;
for (int i = 0; i < sa->Length-1-startIndex; i++) {
s = sa[i+startIndex];
ss = s->Split(' ');
S11Real[i+base] = Convert::ToDouble(ss[0])*10.0;
S11Imag[i+base] = Convert::ToDouble(ss[1])*10.0;
}
if (S11Real[base] > 10000.0) {
S11Real[base] = S11Real[base+1];
S11Imag[base] = S11Imag[base+1];
}
again_2:
sa = Perform("data 1");
startIndex = 1;
if (sa->Length == 102) {
startIndex = 0;
} else if (sa->Length != 103)
goto again_2;
for (int i = 0; i < sa->Length-1-startIndex; i++) {
s = sa[i+startIndex];
ss = s->Split(' ');
S21Real[i+base] = Convert::ToDouble(ss[0])*3.0;
S21Imag[i+base] = Convert::ToDouble(ss[1])*3.0;
}
if (S21Real[base] > 10000.0) {
S21Real[base] = S21Real[base+1];
S21Imag[base] = S21Imag[base+1];
}
base += 101;
numPoints -= 101;
startF += 101*stepF;
}
}
index = 0;
} else if (hardware == HW_NANOV2) {
array <unsigned char> ^ buf; // Start autoread
#define SENDV2(...) { array <unsigned char> ^ _buf = { __VA_ARGS__}; serialPort->Write(_buf, 0 , _buf->Length);}
#define FLUSHV2 Sleep(100); serialPort->ReadExisting()
buf = gcnew array<unsigned char>(500);
// if (startF != oldstartF || stepF != oldstepF || numPoints != oldnumPoints) {
FLUSHV2;
buf[0] = (unsigned char) 0x23;
buf[1] = (unsigned char) 0x0;
buf[2] = (unsigned char)(((unsigned __int64)startF));
buf[3] = (unsigned char)(((unsigned __int64)startF)>>8);
buf[4] = (unsigned char)(((unsigned __int64)startF)>>16);
buf[5] = (unsigned char)(((unsigned __int64)startF)>>24);
buf[6] = (unsigned char)(((unsigned __int64)startF)>>32);
buf[7] = (unsigned char)(((unsigned __int64)startF)>>40);
buf[8] = (unsigned char)(((unsigned __int64)startF)>>48);
buf[9] = (unsigned char)(((unsigned __int64)startF)>>56);
serialPort->Write(buf, 0 , 10);
buf[0] = 0x23;
buf[1] = 0x10;
buf[2] = (unsigned char)(((unsigned __int64)stepF));
buf[3] = (unsigned char)(((unsigned __int64)stepF)>>8);
buf[4] = (unsigned char)(((unsigned __int64)stepF)>>16);
buf[5] = (unsigned char)(((unsigned __int64)stepF)>>24);
buf[6] = (unsigned char)(((unsigned __int64)stepF)>>32);
buf[7] = (unsigned char)(((unsigned __int64)stepF)>>40);
buf[8] = (unsigned char)(((unsigned __int64)stepF)>>48);
buf[9] = (unsigned char)(((unsigned __int64)stepF)>>56);
serialPort->Write(buf, 0 , 10);
FLUSHV2;
buf[0] = 0x21;
buf[1] = 0x20;
buf[2] = (unsigned char)(((unsigned short)numPoints));
buf[3] = (unsigned char)(((unsigned short)numPoints)>>8);
serialPort->Write(buf, 0 , 4);
oldstartF = startF;
oldstepF = stepF;
oldnumPoints = numPoints;
// Sleep(4000);
// }
FLUSHV2;
// SENDV2(0,0,0,0,0,0,0,0); // Reset protocol
// FLUSHV2;
// SENDV2(0,0,0,0,0,0,0,0);
// FLUSHV2;
SENDV2(0x20, 0x30, 0x00); // Clear FIFO
SweepProgressBar->Value = 0;
int base = 0;
while (numPoints > 0) {
int getPoints = 101;
if (getPoints > numPoints)
getPoints = numPoints;
FLUSHV2;
SENDV2(0x18, 0x30, (unsigned char)getPoints); // Read FIFO
int bytes = numPoints * 32;
buf = gcnew array<unsigned char>(bytes);
int data[7];
for(int i=0; i<getPoints; i++) {
int r = serialPort->Read(buf, 0 ,32);
while (r != 32) {
r += serialPort->Read(buf, r ,32-r);
}
for (int k=0; k<6; k++) {
data[k] = (int)( (buf[0*32+k*4+0]) + (buf[0*32+k*4+1]<<8) + (buf[0*32+k*4+2]<<16) + (buf[0*32+k*4+3]<<24) );
}
data[6] = (int)( (buf[0*32+24+0]) + (buf[0*32+24+1]<<8) );
std::complex<double> fwd((double)data[0], (double)data[1]);
std::complex<double> refl((double)data[2], (double)data[3]);
std::complex<double> thru((double)data[4], (double)data[5]);
std::complex<double> zero((double)0.0, (double)0.0);
if (fwd != zero) {
std::complex<double> S11 = refl/fwd;
std::complex<double> S21 = thru/fwd;
double t;
int j = data[6];
t = real(S11);
S11Real[j] = t;
S11Imag[j] = imag(S11);
S21Real[j] = real(S21);
S21Imag[j] = imag(S21);
}
else
MessageBox::Show("Zero thru", "Error");
if (((base+i) % 10) == 0) SweepProgressBar->Value = (base+i+1);
}
numPoints -= getPoints;
base += getPoints;
}
index = 0; // Start from ReadWrite from0
} else {
//serialPort->ReadExisting();
if (power)
serialPort->WriteLine(String::Format("F2 {0} {1} {2} {3} {4} {5}", startF, numPoints + 10, stepF, dur, IFREQ, hardware-2));
else
serialPort->WriteLine(String::Format("F0 {0} {1} {2} {3} {4} {5}", startF, numPoints + 10, stepF, dur+2, IFREQ, hardware-2)); // add 2 duration for lead in and out
Sleep(20);
}
}
catch (System::Exception^ e) {
MessageBox::Show(e->Message, "Error");
return(false);
}
}
return(true);
// mp = 0;
}
void VNADevice::SetFreq(__int64 startF, int direction)
{
if (hardware != HW_MOCKUP){
try {
if (!serialPort->IsOpen) {
if (!FindVNA())
return;
}
if (serialPort->IsOpen) {
if (hardware == HW_NANOVNA) {
// Perform(String::Format("freq {0}", startF));
#if 0
String ^s = serialPort->ReadExisting();
serialPort->WriteLine(String::Format("freq {0}", startF));
s = serialPort->ReadLine();
// s = serialPort->ReadExisting();
#endif
} else if (hardware == HW_NANOV2) {
// not implemented yet
} else {
SetAudioPower((direction == 2?true:false));
serialPort->WriteLine(String::Format("F{1} {0} 1 0 5 {2} {3}", startF, direction, IFREQ, hardware-2));
}
}
}
catch (System::Exception^ e) {
MessageBox::Show(e->Message, "Error");
// serialPort->Close();
// Sleep(1000);
// serialPort->Open();
// Sleep(1000);
//serialPort->ReadExisting();
return;
}
} else {
StartAudioSimulation(mode, 1, 5, startF, 0, cable_before, cable_after, direction, resistance, capacitance, sourcecapacitance, inductance, noise);
}
}
array<String ^>^VNADevice::Perform(String ^command)
{
array<String ^>^ sa;
String ^s = serialPort->ReadExisting(); // Discard what is available
System::Threading::Thread::Sleep(10);
serialPort->WriteLine(command);
System::Threading::Thread::Sleep(100);
s = serialPort->ReadExisting();
int count = 0;
while (!s->Contains("ch>")) { // Wait for all output
System::Threading::Thread::Sleep(100);
s = s->Concat(serialPort->ReadExisting());
count++;
if (count > 30) { // longest command takes 2 seconds
MessageBox::Show("Timeout when communicating with NanoVNA", "Error");
throw;
}
}
sa = s->Split('\n');
return(sa);
}
bool VNADevice::FindVNA()
{
int step=0;
try
{
if (hardware == HW_MOCKUP)
return true;
// serialPort->PortName = SetPortName(_serialPort->PortName);
// serialPort->BaudRate = SetPortBaudRate(_serialPort->BaudRate);
serialPort->Parity =(System::IO::Ports::Parity) 0;
serialPort->DataBits = 8;
serialPort->StopBits = (System::IO::Ports::StopBits)1;
serialPort->Handshake = (System::IO::Ports::Handshake)0;
// Set the read/write timeouts
serialPort->ReadTimeout = 5000;
serialPort->WriteTimeout = 1000;
if (!serialPort->IsOpen) {
serialPort->ReadBufferSize = 40000;
serialPort->Open();
Sleep(200);
}
if (hardware == HW_NANOV2) {
array<unsigned char>^ buf = gcnew array<unsigned char>(1); // read data
buf[0] = (unsigned char) 0x0D;
serialPort->Write(buf, 0 ,1);
Sleep(500);
String ^s = serialPort->ReadExisting();
if (s == "2")
return true;
else
return false;
} else if (hardware == HW_NANOVNA) {
String ^s;
again1:
s = serialPort->ReadExisting();
if (s->Length != 0) {
Sleep(100);
goto again1;
}
serialPort->NewLine = "\r\n";
array<String ^>^ sa;
sa = Perform("scanraw");
step=1;
if (sa->Length > 2 && sa[1]->StartsWith("usage: scanraw {channel(0|1|2)}")) {
hasScanCommand = true;
Perform("pause");
} else {
hasScanCommand = false;
Perform("resume");
Perform("cal off");
}
// serialPort->WriteLine("cal off");
// s = serialPort->ReadLine(); // cal off
// s = serialPort->ReadExisting(); // ch>
// System::Threading::Thread::Sleep(100);
// serialPort->WriteLine("pause");
// s = serialPort->ReadLine();
// s = serialPort->ReadExisting();
return true;
} else {
String ^s;
again2:
s = serialPort->ReadExisting(); // Throw away whatever was send before
if (s->Length != 0) {
Sleep(100);
goto again2;
}
serialPort->Write("f3");
System::Threading::Thread::Sleep(200);
s = serialPort->ReadLine();
if (s->StartsWith("TAPR VNA v4"))
return true;
}
}
catch( Exception^ e)
{
MessageBox::Show(e->Message, "Error");
return false;
}
return false;
}
#define PhaseToQ(X) (short)( sin((X) * DEGR2RAD) * 1800 + 1850 )
#define PhaseToI(X) (short)( cos((X) * DEGR2RAD) * 1800 + 1850 )
#define Noise(X, Y) (short)( X * (1.0 + Y * ((1.0 * rand() / RAND_MAX) - 0.5) ));
#define NoiseLevel 0.005
#define MaxLevel +0
#define MinLevel -57
#define MaxDAC 3500
#define MagDac(X) ((short)(MaxDAC * (( X ) - MinLevel) / (MaxLevel - MinLevel) ) )
#define MAXAVERAGE 300
static float sumreflphase[MAXAVERAGE];
static float sumtranphase[MAXAVERAGE];
static float sumreflmag[MAXAVERAGE];
static float sumtranmag[MAXAVERAGE];
static float sumreflevel[MAXAVERAGE];
static float Median(float *data, int size)
{
int i, j;
float swap;
// bubble sort the elements
for (i=size-1; i>=0; i--) {
for (j=1; j<=i; j++) {
if (data[j-1] > data[j])
{
swap = data[j-1];
data[j-1] = data[j];
data[j] = swap;
}
}
}
return(data[size/2]); // the median value
}
/// Write TxBuffer to VNA (command), readback the result to RxBuffer (response)
bool VNADevice::WriteRead(VNA_TXBUFFER * TxBuffer, VNA_RXBUFFER * RxBuffer, int direction)
{
// modifications to test Proto8 target changes
bool rxsuccess = true;
// long freq;
float reflphase,tranphase;
float reflmag,tranmag;
float reflevel;
unsigned long freq;
int i;
if (hardware == HW_NANOVNA) {
array<String^>^ sa;
String ^s;
double x;
double y;
reflevel = 0.0; // Always 0.0 for NanoVNA
try {
if (hasScanCommand) {
again:
s = serialPort->ReadLine();
// if (s->StartsWith("done"))
// return false;
// if (s->StartsWith("start"))
// goto again;
if (s->StartsWith("scanraw"))
goto again;
if (s->StartsWith("ch>"))
return false;
if (s->StartsWith("0 "))
goto again;
if (s->StartsWith("1 "))
goto again;
sa = s->Split('\t');
freq = Convert::ToInt32(sa[0]);
x = Convert::ToDouble(sa[1]);
y = Convert::ToDouble(sa[2]);
reflphase = (float) (atan2(y, x) * RAD2DEGR);
reflmag = (float)todb(sqrt(x*x + y*y));
x = Convert::ToDouble(sa[3]);
y = Convert::ToDouble(sa[4]);
tranphase = (float) (atan2(y, x) * RAD2DEGR);
tranmag = (float)todb(sqrt(x*x + y*y));
} else {
x = S11Real[index];
y = S11Imag[index];
reflphase = (float) (atan2(y, x) * RAD2DEGR);
reflmag = (float)todb(sqrt(x*x + y*y));
x = S21Real[index];
y = S21Imag[index];
tranphase = (float) (atan2(y, x) * RAD2DEGR);
tranmag = (float)todb(sqrt(x*x + y*y));
}
index++;
}
catch( Exception^ e )
{
MessageBox::Show(e->Message, "Error");
return false;
}
} else if (hardware == HW_NANOV2) {
// String ^s;
double x;
double y;
reflevel = 0.0; // Always 0.0 for NanoVNA
try {
x = S11Real[index];
y = S11Imag[index];
reflphase = (float) (atan2(y, x) * RAD2DEGR);
reflmag = (float)todb(sqrt(x*x + y*y));
x = S21Real[index];
y = S21Imag[index];
tranphase = (float) (atan2(y, x) * RAD2DEGR);
tranmag = (float)todb(sqrt(x*x + y*y));
index++;
}
catch( Exception^ /* e */ )
{
return false;
}
} else {
//unsigned int level;
int availableSamples = ((dur+2) * SAMPPERMS - 2);
// int reply = TxBuffer->ReplyType;
int retries=0;
// return true;
#define MAX_RETRIES 100
// i = 0;
for (i=0; i<availableSamples; i++)
{
while (!RetreiveData((int)TxBuffer->TxAccum, dur, sumreflmag[i], sumreflphase[i], sumtranmag[i], sumtranphase[i], sumreflevel[i], freq, availableSamples) && retries < MAX_RETRIES) {
Sleep(2);
retries++;
}
if (retries >= MAX_RETRIES)
return (false);
}
#if 1
reflmag = Median(sumreflmag,availableSamples);
tranmag = Median(sumtranmag,availableSamples);
reflphase = Median(sumreflphase,availableSamples);
tranphase = Median(sumtranphase,availableSamples);
reflevel = Median(sumreflevel,availableSamples);
#else
reflmag = sumreflmag[0];
tranmag = sumtranmag[0];
reflphase = sumreflphase[0];
tranphase = sumtranphase[0];
reflevel = sumreflevel[0];
#endif
// mp++;
}
RxBuffer->Vref1 = DB2SHORT(reflevel);
// RxBuffer->Vref2 = level;
NormalizePhase(reflphase);
RxBuffer->ReflPQ = PHASE2SHORT(reflphase) ;
RxBuffer->ReflMQ = DB2SHORT(reflmag);
NormalizePhase(tranphase);
RxBuffer->TranPQ = PHASE2SHORT(tranphase) ;
RxBuffer->TranMQ = DB2SHORT(tranmag);
RxBuffer->Freq = freq;
return(rxsuccess);
}
void VNADevice::SetMode(int m) {
mode = m;
}
void VNADevice::SetBefore(int l) {
cable_before = l;
}
void VNADevice::SetAfter(int l) {
cable_after = l;
}
void VNADevice::SetResistance(int l) {
resistance = l;
}
void VNADevice::SetCapacitance(int l) {
capacitance = l;
}
void VNADevice::SetSourceCapacitance(int l) {
sourcecapacitance = l;
}
void VNADevice::SetInductance(int l) {
inductance = l;
}
void VNADevice::SetNoise(float c) {
noise = c;
}
void VNADevice::SetMinFreq(__int64 f) {
minHWFreq = f;
}
void VNADevice::SelectHardware(int h) {
hardware = h;
}
int VNADevice::GetHardware() {
return(hardware);
}
void VNADevice::SetMaxFreq(__int64 f) {
maxHWFreq = f;
}
__int64 VNADevice::GetMaxFreq() {
return(maxHWFreq);
}
__int64 VNADevice::GetMinFreq() {
return(minHWFreq);
}