forked from erikkaashoek/NanoVNA-App
-
Notifications
You must be signed in to change notification settings - Fork 9
/
NanoVNA_v2_comms.cpp
1294 lines (1083 loc) · 35 KB
/
NanoVNA_v2_comms.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
#pragma hdrstop
#include "NanoVNA_v2_comms.h"
#include "CommsUnit.h"
#include "DataUnit.h"
#include "Unit1.h"
#include "SettingsUnit.h"
#pragma package(smart_init)
// *******************************
// Normal mode
// tx: 00 00 00 00 00 00 00 00 20 26 02 0D
// rx: 32 ACK
// tx: 10 F0
// rx: 02 DEVICE VARIANT
// tx: 10 F1
// rx: 01 PROTOCOL VERSION
// tx: 10 F2
// rx: 02 HARDWARE REVISION
// tx: 10 F3 10 F4
// rx: 01 02 FIRMWARE VERSION
// DFU mode
// tx: 00 00 00 00 00 00 00 00 20 26 02 0D
// rx: 32 ACK
// tx: 10 F0
// rx: 02 DEVICE VARIANT
// tx: 10 F1
// rx: 01 PROTOCOL VERSION
// tx: 10 F2
// rx: 00 HARDWARE REVISION
// tx: 10 F3 10 F4
// rx: FF 00 FIRMWARE VERSION
// *******************************
CNanoVNA2Comms nanovna2_comms;
CNanoVNA2Comms::CNanoVNA2Comms()
{
m_capture_bm = NULL;
reset(false);
m_mode = MODE_NONE;
}
CNanoVNA2Comms::~CNanoVNA2Comms()
{
if (m_capture_bm)
{
delete m_capture_bm;
m_capture_bm = NULL;
}
}
void __fastcall CNanoVNA2Comms::resetScan()
{
m_start = -1;
m_step = -1;
m_points_per_segment = 0;
m_data_points_per_frequency = 0;
}
void __fastcall CNanoVNA2Comms::reset(const bool reset_vna)
{
if (m_capture_bm)
{
delete m_capture_bm;
m_capture_bm = NULL;
}
m_tx_cmd.resize(0);
m_mode = MODE_INIT1;
m_new_mode = MODE_INIT1;
m_retries = 0;
m_get_screen_capture = false;
m_pause_comms = false;
m_usb_data_mode = false;
m_poll_ms = DEFAULT_POLL_V2_MS;
m_state_timeout_ms = DEFAULT_STATE_TIME_OUT_V2_MS;
resetScan();
// if (reset_vna)
// leaveUSBDataMode(true);
}
void __fastcall CNanoVNA2Comms::setMode(const t_mode mode)
{
// request a new mode
m_new_mode = mode;
const t_mode prev_mode = m_mode;
m_mode = mode;
if (mode != prev_mode)
{
m_retries = 0;
if (prev_mode > MODE_IDLE)
{
if (mode == MODE_IDLE)
{
if (prev_mode == MODE_SINGLE_SCAN || prev_mode == MODE_SCAN || prev_mode == MODE_GENERATOR)
{
Form1->pushCommMessage("tx: clearing FIFO buffer");
addTxNulls();
//addTxNulls(true, 0);
//addTxWrite1(REG_V2_VALUES_FIFO, 0);
sendData();
m_tx_cmd.resize(0);
leaveUSBDataMode(false);
}
if (prev_mode != MODE_POLL)
{
m_mode = MODE_POLL;
poll();
}
}
}
}
}
void __fastcall CNanoVNA2Comms::sendData(void *data, int size)
{
if (data == NULL && size < 0)
{
data = &m_tx_cmd[0];
size = m_tx_cmd.size();
}
if (!data || size <= 0 || !Form1)
return;
String s;
{
const uint8_t *ptr = (const uint8_t *)data;
for (int i = 0; i < size; i++)
{
String s2;
s2.printf(L" %02X", ptr[i]);
s += s2;
}
s = s.Trim();
}
if (Form1->m_comms.serial.connected)
{ // send the new command through the serial link
Form1->pushCommMessage("tx: " + s);
if (Form1->m_comms.serial.TxBytes(data, size) < size)
{ // failed
Form1->pushCommMessage("tx: error .. " + s);
//Form1->m_comms.serial.Disconnect();
//::PostMessage(Form1->Handle, WM_DISCONNECT, 0, 0);
return;
}
Form1->m_comms.rx_timer.mark();
}
#ifdef TCPIPH
if (Form1->m_comms.tcpip.connected)
{ // send the new command through the tcpip link
Form1->pushCommMessage("tx: " + s);
if (m_comms.tcpip.TxBytes(data, size) < size)
{ // failed
Form1->pushCommMessage("tx: " + String("error ") + m_comms.tcpip.lastErrorStr + " .. " + s);
//m_comms.tcpip.Disconnect();
//::PostMessage(Form1->Handle, WM_DISCONNECT, 0, 0);
return;
}
Form1->m_comms.rx_timer.mark();
}
#else
if (Form1->m_comms.tcpip)
{
try
{
if (Form1->m_comms.tcpip->Connected())
{ // send the new command through the tcpip link
Form1->pushCommMessage("tx: " + s);
TMemoryStream *stream = new TMemoryStream();
if (stream)
{
stream->Position = 0;
stream->Write(data, size);
stream->Position = 0;
Form1->m_comms.tcpip->IOHandler->Write(stream, 0, false);
delete stream;
Form1->m_comms.rx_timer.mark();
}
}
}
catch (Exception &exception)
{
//Application->ShowException(&exception);
Form1->pushCommMessage("tx: tcpip error " + exception.ToString());
}
}
#endif
}
bool __fastcall CNanoVNA2Comms::inDFUMode()
{
return (data_unit.m_vna_data.hardware_revision == REG_DFU_V2_HARDWARE_REVISION_ACK && data_unit.m_vna_data.firmware_major == REG_DFU_V2_FIRMWARE_MAJOR_ACK) ? true : false;
}
void __fastcall CNanoVNA2Comms::clearTxCommands()
{
m_tx_cmd.resize(0);
}
void __fastcall CNanoVNA2Comms::addTxNulls(const bool reset_buf, const int num)
{
if (reset_buf)
m_tx_cmd.resize(0);
for (int i = 0; i < num; i++)
m_tx_cmd.push_back(0x00);
}
void __fastcall CNanoVNA2Comms::addTxRead1(const uint8_t reg_addr)
{
m_tx_cmd.push_back(CMD_V2_READ_1);
m_tx_cmd.push_back(reg_addr);
}
void __fastcall CNanoVNA2Comms::addTxRead2(const uint8_t reg_addr)
{
m_tx_cmd.push_back(CMD_V2_READ_2);
m_tx_cmd.push_back(reg_addr);
}
void __fastcall CNanoVNA2Comms::addTxRead4(const uint8_t reg_addr)
{
m_tx_cmd.push_back(CMD_V2_READ_4);
m_tx_cmd.push_back(reg_addr);
}
void __fastcall CNanoVNA2Comms::addTxRead8(const uint8_t reg_addr)
{
m_tx_cmd.push_back(CMD_V2_READ_8);
m_tx_cmd.push_back(reg_addr);
}
//void __fastcall CNanoVNA2Comms::addTxRead8(const uint8_t reg_addr)
//{
// m_tx_cmd.push_back(CMD_V2_READ_8);
// m_tx_cmd.push_back(reg_addr);
//}
void __fastcall CNanoVNA2Comms::addTxWrite1(const uint8_t reg_addr, uint8_t value)
{
m_tx_cmd.push_back(CMD_V2_WRITE_1);
m_tx_cmd.push_back(reg_addr);
m_tx_cmd.push_back(value);
}
void __fastcall CNanoVNA2Comms::addTxWrite2(const uint8_t reg_addr, uint16_t value)
{
m_tx_cmd.push_back(CMD_V2_WRITE_2);
m_tx_cmd.push_back(reg_addr);
m_tx_cmd.push_back((uint8_t)(value & 0xff)); value >>= 8;
m_tx_cmd.push_back((uint8_t)(value & 0xff));
}
void __fastcall CNanoVNA2Comms::addTxWrite4(const uint8_t reg_addr, uint32_t value)
{
m_tx_cmd.push_back(CMD_V2_WRITE_4);
m_tx_cmd.push_back(reg_addr);
m_tx_cmd.push_back((uint8_t)(value & 0xff)); value >>= 8;
m_tx_cmd.push_back((uint8_t)(value & 0xff)); value >>= 8;
m_tx_cmd.push_back((uint8_t)(value & 0xff)); value >>= 8;
m_tx_cmd.push_back((uint8_t)(value & 0xff));
}
void __fastcall CNanoVNA2Comms::addTxWrite8(const uint8_t reg_addr, uint64_t value)
{
m_tx_cmd.push_back(CMD_V2_WRITE_8);
m_tx_cmd.push_back(reg_addr);
m_tx_cmd.push_back((uint8_t)(value & 0xff)); value >>= 8;
m_tx_cmd.push_back((uint8_t)(value & 0xff)); value >>= 8;
m_tx_cmd.push_back((uint8_t)(value & 0xff)); value >>= 8;
m_tx_cmd.push_back((uint8_t)(value & 0xff)); value >>= 8;
m_tx_cmd.push_back((uint8_t)(value & 0xff)); value >>= 8;
m_tx_cmd.push_back((uint8_t)(value & 0xff)); value >>= 8;
m_tx_cmd.push_back((uint8_t)(value & 0xff)); value >>= 8;
m_tx_cmd.push_back((uint8_t)(value & 0xff));
}
void __fastcall CNanoVNA2Comms::addTxUserArgument(const uint32_t arg)
{
// addTxNulls(); // reset the protocol to known state
addTxWrite4(REG_DFU_V2_USER_ARGUMENT, arg); // argument value
}
void __fastcall CNanoVNA2Comms::addTxFlashWriteAddr(const uint32_t addr)
{
Form1->printfCommMessage("tx:flash write address %0X08", addr);
// addTxNulls(); // reset the protocol to known state
addTxWrite4(REG_DFU_V2_FLASH_WRITE_START, addr); // flash address
}
void __fastcall CNanoVNA2Comms::addTxWriteFlashData(const bool reset_buf, std::vector <uint8_t> &data, const bool ack)
{
if (reset_buf)
m_tx_cmd.resize(0);
if (!data.empty() && data.size() <= 255)
{
Form1->printfCommMessage("tx:flash write data [%u]", data.size());
// add the flash data
m_tx_cmd.push_back(CMD_V2_WRITE_FIFO);
m_tx_cmd.push_back(REG_DFU_V2_FLASH_FIFO);
m_tx_cmd.push_back(data.size());
for (unsigned int i = 0; i < data.size(); i++)
m_tx_cmd.push_back(data[i]);
// we want an ACK back after it's written the data
if (ack)
m_tx_cmd.push_back(CMD_V2_INDICATE);
}
}
void __fastcall CNanoVNA2Comms::enterUSBDataMode(const bool send_nulls)
{
Form1->pushCommMessage("tx: enter USB data mode");
if (send_nulls)
addTxNulls(); // reset the protocol to known state
else
addTxNulls(true, 0);
addTxWrite1(REG_V2_RAW_SAMPLES_MODE, 0x01); // enter USB raw mode
sendData();
m_tx_cmd.resize(0);
m_usb_data_mode = true;
}
void __fastcall CNanoVNA2Comms::leaveUSBDataMode(const bool send_nulls)
{
Form1->pushCommMessage("tx: leave USB data mode");
if (send_nulls)
addTxNulls(); // reset the protocol to known state
else
addTxNulls(true, 0);
addTxWrite1(REG_V2_RAW_SAMPLES_MODE, 0x02); // leave USB raw mode
sendData();
m_tx_cmd.resize(0);
m_usb_data_mode = false;
}
void __fastcall CNanoVNA2Comms::setAverageSetting(int value)
{
if (value < REG_V2_AVERAGE_SETTING_MIN) value = REG_V2_AVERAGE_SETTING_MIN;
else
if (value > REG_V2_AVERAGE_SETTING_MAX) value = REG_V2_AVERAGE_SETTING_MAX;
Form1->printfCommMessage("tx: set average setting %d", value);
addTxNulls(true, 0);
addTxWrite1(REG_V2_AVERAGE_SETTING, value);
sendData();
m_tx_cmd.resize(0);
}
void __fastcall CNanoVNA2Comms::setSI5351OutputPower(int value)
{
if (value < REG_V2_SI5351_POWER_MIN || value > REG_V2_SI5351_POWER_MAX)
value = REG_V2_SI5351_POWER_MAX;
Form1->printfCommMessage("tx: set Si5351 output power %d", value);
addTxNulls(true, 0);
addTxWrite1(REG_V2_SI5351_POWER, value);
sendData();
m_tx_cmd.resize(0);
}
void __fastcall CNanoVNA2Comms::setADF4350OutputPower(int value)
{
if (value < REG_V2_ADF4350_POWER_MIN || value > REG_V2_ADF4350_POWER_MAX)
value = REG_V2_ADF4350_POWER_MAX;
Form1->printfCommMessage("tx: set ADF4350 output power %d", value);
addTxNulls(true, 0);
addTxWrite1(REG_V2_ADF4350_POWER, value);
sendData();
m_tx_cmd.resize(0);
}
void __fastcall CNanoVNA2Comms::setTime(int year, int month, int day, int hour, int min, int sec)
{
uint32_t a=((14-month)/12);
uint32_t y=year+4800-a;
uint32_t m=month+(12*a)-3;
uint32_t Uday=(day+((153*m+2)/5)+365*y+(y/4)-(y/100)+(y/400)-32045)-2440588;
uint32_t time = Uday*60*60*24 + sec + min*60 + hour*3600;
Form1->printfCommMessage("tx: set UNIX time %u", time);
nanovna2_comms.addTxWrite4(REG_V2_UNIX_TIME, time);
sendData();
m_tx_cmd.resize(0);
}
void __fastcall CNanoVNA2Comms::softReboot()
{
Form1->pushCommMessage("tx: request soft reboot");
addTxNulls(); // reset the protocol to known state
addTxWrite1(REG_DFU_V2_DO_REBOOT, 0x5E); // soft reboot the VNA
sendData();
m_tx_cmd.resize(0);
}
void __fastcall CNanoVNA2Comms::pauseComms(bool pause)
{
const bool was_paused = m_pause_comms;
m_pause_comms = pause;
// reset the link timer so as not to immediately timeout the link when it gets unpaused
if (Form1)
{
Form1->m_comms.rx_timer.mark();
Form1->m_comms.link_timer.mark();
}
if (pause)
return;
// un-paused
if (!was_paused)
return;
m_retries = 0;
// was paused, but no more
// do stuff that wasn't being done whilst we were paused
// poll();
if (m_mode != MODE_SCAN)
return;
// if (data_unit.m_vna_data.cmd_pause)
// enterUSBDataMode();
requestScan();
}
bool __fastcall CNanoVNA2Comms::requestCapture()
{ // request a screen capture from the VNA
if (!Form1)
return false;
if (m_mode != MODE_IDLE)
return false;
if (!m_get_screen_capture)
return false;
m_get_screen_capture = false;
memset(&m_cap_header, 0, sizeof(m_cap_header));
setMode(MODE_CAPTURE);
poll();
return true;
}
void __fastcall CNanoVNA2Comms::requestScan()
{
if (!Form1)
return;
const bool scanning = (m_mode == MODE_SINGLE_SCAN || m_mode == MODE_SCAN) ? true : false;
const bool generating = (m_mode == MODE_GENERATOR) ? true : false;
const int segments = data_unit.m_segments;
const int segment = data_unit.m_segment;
const int points_per_segment = data_unit.m_points_per_segment;
const int num_points = data_unit.m_points;
const uint64_t span = scanning ? data_unit.m_freq_span_Hz / segments : 0;
const uint64_t start = scanning ? data_unit.m_freq_start_Hz + (span * segment) : data_unit.m_freq_cw_Hz;
if (segments < 1 || points_per_segment < 8)
return;
const uint64_t step = span / (points_per_segment - 1);
const int points_per_frequency = 1;
const bool new_start = (m_start != start ||
m_step != step ||
m_points_per_segment != points_per_segment ||
m_data_points_per_frequency != points_per_frequency) ? true : false;
// global buffer
Form1->m_freq_data_list.resize(points_per_segment); // buffer size to the number of points we are to gather from the VNA
{
t_data_point fp;
for (unsigned int i = 0; i < Form1->m_freq_data_list.size(); i++)
Form1->m_freq_data_list[i] = fp;
}
// clear the incoming buffer
if (segment <= 0)
data_unit.m_point_incoming.resize(0);
if (new_start)
{
if (Form1)
Form1->pushCommMessage("tx: setting scan parameters");
// reset protocol to known state
addTxNulls();
if (settings.calibrationSelection == CAL_SELECT_VNA)
addTxWrite1(REG_V2_RAW_SAMPLES_MODE, 0x03); // enter USB calibrated mode
else
addTxWrite1(REG_V2_RAW_SAMPLES_MODE, 0x00); // enter USB mode
// clear the FIFO
//addTxWrite1(REG_V2_VALUES_FIFO, 0);
// start frequency
m_start = start;
addTxWrite8(REG_V2_SWEEP_START_HZ, start);
// step frequency
m_step = step;
addTxWrite8(REG_V2_SWEEP_STEP_HZ, step);
// number of points
m_points_per_segment = points_per_segment;
addTxWrite2(REG_V2_SWEEP_POINTS, points_per_segment);
// data points per frequency
m_data_points_per_frequency = points_per_frequency;
addTxWrite2(REG_V2_VALUES_PER_FREQUENCY, points_per_frequency);
sendData();
m_tx_cmd.resize(0);
}
m_points_requested = requestPoints(points_per_segment);
}
int __fastcall CNanoVNA2Comms::requestPoints(int num_points)
{
// request a number of frequency scanned points
m_tx_cmd.resize(0);
if (num_points <= 0)
return 0;
if (Form1)
Form1->printfCommMessage("tx: requesting %d points ..", num_points);
// reset protocol to known state
// addTxNulls();
addTxNulls(true, 0);
// clear the FIFO
// addTxWrite1(REG_V2_VALUES_FIFO, 0);
m_tx_cmd.push_back(CMD_V2_READ_FIFO);
m_tx_cmd.push_back(REG_V2_VALUES_FIFO);
m_tx_cmd.push_back(0); // tell it to send ALL the scan points in one go
/*
int points = num_points;
while (points > 0)
{
// we can only request up to a maximum of 255 points at a time
int n = points;
if (n > 255)
n = 255;
// if (n > 128)
// n = 128;
// request num_points
m_tx_cmd.push_back(CMD_V2_READ_FIFO);
m_tx_cmd.push_back(REG_V2_VALUES_FIFO);
m_tx_cmd.push_back(n);
points -= n;
}
*/
sendData();
m_tx_cmd.resize(0);
return num_points;
}
void __fastcall CNanoVNA2Comms::poll()
{
m_poll_timer.mark();
if (!Form1)
return;
switch (m_mode)
{
case MODE_NONE:
if (m_new_mode != m_mode)
{
m_tx_cmd.resize(0);
m_retries = 0;
m_mode = m_new_mode;
}
break;
case MODE_INIT1: // request a simple ACK to see if it's a V2 we're connected too
/* if (m_new_mode != m_mode)
{
m_tx_cmd.resize(0);
m_retries = 0;
m_mode = m_new_mode;
}
else
*/ {
// leave USB raw mode
leaveUSBDataMode(true);
// request an simple ACK
Form1->pushCommMessage("tx: are you a V2 ?");
addTxNulls(true, 0);
m_tx_cmd.push_back(CMD_V2_INDICATE);
sendData();
m_tx_cmd.resize(0);
m_retries++;
}
break;
case MODE_INIT2: // get the type of device
/* if (m_new_mode != m_mode)
{
m_tx_cmd.resize(0);
m_retries = 0;
m_mode = m_new_mode;
}
else
*/ {
Form1->pushCommMessage("tx: requesting device variant");
m_tx_cmd.resize(0);
addTxRead1(REG_V2_DEVICE_VARIANT);
sendData();
m_tx_cmd.resize(0);
m_retries++;
}
break;
case MODE_INIT3: // get the serial protocol version
/* if (m_new_mode != m_mode)
{
m_tx_cmd.resize(0);
m_retries = 0;
m_mode = m_new_mode;
}
else
*/ {
Form1->pushCommMessage("tx: requesting serial protocol version");
m_tx_cmd.resize(0);
addTxRead1(REG_V2_PROTCOL_VERSION);
sendData();
m_tx_cmd.resize(0);
m_retries++;
}
break;
case MODE_INIT4: // get the hardware revision and the firmware major and minor version
/* if (m_new_mode != m_mode)
{
m_tx_cmd.resize(0);
m_retries = 0;
m_mode = m_new_mode;
}
else
*/ {
Form1->pushCommMessage("tx: requesting hw-revision and fw-version");
m_tx_cmd.resize(0);
addTxRead1(REG_V2_HARDWARE_REVISION);
addTxRead1(REG_V2_FIRMWARE_MAJOR);
addTxRead1(REG_V2_FIRMWARE_MINOR);
addTxRead1(REG_V2_AVERAGE_SETTING);
sendData();
m_tx_cmd.resize(0);
m_retries++;
}
break;
case MODE_IDLE:
/* if (m_new_mode != m_mode)
{
m_tx_cmd.resize(0);
m_retries = 0;
m_mode = m_new_mode;
}
else
*/ {
}
break;
case MODE_POLL:
/* if (m_new_mode != m_mode)
{
m_tx_cmd.resize(0);
m_retries = 0;
m_mode = m_new_mode;
}
else
*/ if (!m_pause_comms)
{
Form1->pushCommMessage("tx: polling - requesting hw-revision and fw-version");
// addTxNulls();
addTxNulls(true, 0);
addTxRead1(REG_V2_HARDWARE_REVISION);
addTxRead1(REG_V2_FIRMWARE_MAJOR);
addTxRead1(REG_V2_FIRMWARE_MINOR);
addTxRead2(REG_V2_BATTERY);
sendData();
m_tx_cmd.resize(0);
m_retries++;
}
break;
case MODE_SINGLE_SCAN:
case MODE_SCAN:
case MODE_GENERATOR:
/* if (m_new_mode != m_mode)
{
m_tx_cmd.resize(0);
m_retries = 0;
m_mode = m_new_mode;
}
else
*/ {
}
break;
case MODE_CAPTURE:
/* if (m_new_mode != m_mode)
{
m_tx_cmd.resize(0);
m_retries = 0;
m_mode = m_new_mode;
}
else
*/ {
Form1->pushCommMessage("tx: requesting a screen capture");
memset(&m_cap_header, 0, sizeof(m_cap_header));
// addTxNulls();
addTxNulls(true, 0);
addTxWrite1(REG_V2_CAPTURE_SCREEN, settings.scrFormat);
sendData();
m_tx_cmd.resize(0);
m_retries++;
}
break;
case MODE_DFU_MODE: // don't do anything while the other window is uploading flash firmware
break;
default:
setMode(MODE_IDLE);
break;
}
}
void __fastcall CNanoVNA2Comms::newUnit()
{
String s;
if (Form1)
{
s = IntToHex((int)data_unit.m_vna_data.hardware_revision, 2) + " ..... HARDWARE REVISION";
Form1->pushCommMessage("rx: " + s);
s = IntToHex((int)data_unit.m_vna_data.firmware_major, 2) + " " + IntToHex((int)data_unit.m_vna_data.firmware_minor, 2) + " .. FIRMWARE VERSION";
Form1->pushCommMessage("rx: " + s);
Form1->pushCommMessage("rx: [connected to a V2]");
}
// ****************
data_unit.m_vna_data.type = UNIT_TYPE_NANOVNA_V2;
data_unit.m_vna_data.name = "NanoVNA-";
switch (data_unit.m_vna_data.hardware_revision)
{
case REG_V2_HARDWARE_REVISION_ACK_2_2:
if (data_unit.m_vna_data.firmware_major == 2) {
data_unit.m_vna_data.name += "LiteVNA";
data_unit.m_vna_data.max_points = 65535;
// data_unit.m_vna_data.hardware_revision = REG_V2_HARDWARE_REVISION_ACK_2_4;
} else {
data_unit.m_vna_data.name += "V2.2";
data_unit.m_vna_data.max_points = 1024;
}
break;
case REG_V2_HARDWARE_REVISION_ACK_2_3:
data_unit.m_vna_data.name += "V2Plus";
data_unit.m_vna_data.max_points = 1024;
break;
case REG_V2_HARDWARE_REVISION_ACK_2_4:
data_unit.m_vna_data.name += "V2Plus4";
data_unit.m_vna_data.max_points = 65535;
break;
case REG_DFU_V2_HARDWARE_REVISION_ACK:
data_unit.m_vna_data.name += "V2-DFU";
data_unit.m_vna_data.max_points = 201;
break;
default:
data_unit.m_vna_data.name += "?";
data_unit.m_vna_data.max_points = 1024;
break;
}
data_unit.m_vna_data.version.printf(L"v%u.%u", data_unit.m_vna_data.firmware_major, data_unit.m_vna_data.firmware_minor);
data_unit.m_vna_data.info.resize(0);
data_unit.m_vna_data.info.push_back(data_unit.m_vna_data.name);
s.printf(L"protocol v%u", data_unit.m_vna_data.protool_version);
data_unit.m_vna_data.info.push_back(s);
s.printf(L"hardware r%u", data_unit.m_vna_data.hardware_revision);
data_unit.m_vna_data.info.push_back(s);
s.printf(L"firmware v%u.%u", data_unit.m_vna_data.firmware_major, data_unit.m_vna_data.firmware_minor);
data_unit.m_vna_data.info.push_back(s);
s.printf(L"max points %d", data_unit.m_vna_data.max_points);
data_unit.m_vna_data.info.push_back(s);
// tell the exec we have found a VNA
if (Application->MainForm)
::PostMessage(Application->MainForm->Handle, WM_NEW_UNIT_TYPE, 0, 0);
// ****************
if (inDFUMode() && Form1)
Form1->pushCommMessage("rx: the unit is in DFU mode");
setMode(MODE_IDLE); // onto the next stage
}
int __fastcall CNanoVNA2Comms::processRx(t_serial_buffer &serial_buffer)
{
String s;
if (m_mode == MODE_NONE)
return 0;
if (!Form1)
return 0; // if the main form (window) is not there we can't use any of it's functions
const int size = serial_buffer.buffer_wr; // number of available RX bytes
int k = 0;
if (size <= 0)
return 0; // no data received
// process any received data
if (m_mode == MODE_INIT1)
{ // just want a simple reply
const uint8_t b = serial_buffer.buffer[k++]; // fetch an RX byte
s = IntToHex((int)b, 2);
if (b == CMD_V2_INDICATE_ACK)
s += " .. ACK OK";
Form1->pushCommMessage("rx: " + s);
if (b == CMD_V2_INDICATE_ACK)
{ // received the correct response
setMode(MODE_INIT2); // onto the next stage
poll();
m_tx_cmd.resize(0);
}
}
else
if (m_mode == MODE_INIT2)
{ // fetching the device variant
const uint8_t b = serial_buffer.buffer[k++]; // fetch an RX byte
s = IntToHex((int)b, 2);
if (b == REG_V2_DEVICE_VARIANT_ACK)
s += " .. DEVICE VARIANT OK";
Form1->pushCommMessage("rx: " + s);
if (b == REG_V2_DEVICE_VARIANT_ACK)
{ // received the correct response
setMode(MODE_INIT3); // onto the next stage
poll();
m_tx_cmd.resize(0);
}
}
else
if (m_mode == MODE_INIT3)
{ // fetching the serial protocol version
const uint8_t b = serial_buffer.buffer[k++]; // fetch an RX byte
data_unit.m_vna_data.protool_version = b;
s = IntToHex((int)b, 2);
if (b == REG_V2_PROTOCOL_VERSION_ACK)
s += " .. PROTOCOL VERSION OK";
Form1->pushCommMessage("rx: " + s);
if (b == REG_V2_PROTOCOL_VERSION_ACK)
{ // received the correct response
setMode(MODE_INIT4); // onto the next stage
poll();
m_tx_cmd.resize(0);
}
}
else
if (m_mode == MODE_INIT4)
{ // fetching the hardware revision
if (size >= 4)
{ // we have received enough bytes
// fetch them
const uint8_t b1 = serial_buffer.buffer[k++];
const uint8_t b2 = serial_buffer.buffer[k++];
const uint8_t b3 = serial_buffer.buffer[k++];
const uint8_t b4 = serial_buffer.buffer[k++];
data_unit.m_vna_data.hardware_revision = b1;
data_unit.m_vna_data.firmware_major = b2;
data_unit.m_vna_data.firmware_minor = b3;
data_unit.m_bandwidth_Hz = b4;
newUnit();
}
}
else
if (m_mode == MODE_IDLE)
{ // we're not doing anything with the VNA, just idling along
#if 1
// display all the received bytes (as hex)
s = "";
int i = 0;
while (k < (int)serial_buffer.buffer_wr)
{
String s2;
s2.printf(L" %02x", serial_buffer.buffer[k++]);
s += s2;
if (++i >= (int)sizeof(t_v2_fifo_sparam))
{
i = 0;
Form1->pushCommMessage("rx: *" + s);
s = "";
}
}
if (!s.IsEmpty())
{
Form1->pushCommMessage("rx: *" + s);
s = "";
}
#endif
}
else
if (m_mode == MODE_POLL)
{ // we are polling the VNA to see if it's still there and working OK
if (size >= 5)
{ // we have received enough bytes
// fetch them
const uint8_t b1 = serial_buffer.buffer[k++];
const uint8_t b2 = serial_buffer.buffer[k++];
const uint8_t b3 = serial_buffer.buffer[k++];
int value = serial_buffer.buffer[k++]|(serial_buffer.buffer[k++]<<8);
if (value > 0)
{