-
Notifications
You must be signed in to change notification settings - Fork 26
/
UploadFirmwareV2Unit.cpp
1073 lines (848 loc) · 27.3 KB
/
UploadFirmwareV2Unit.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
// (c) OneOfEleven 2020
//
// This code can be used on terms of WTFPL Version 2 (http://www.wtfpl.net)
#include <vcl.h>
#include <stdio.h>
#pragma hdrstop
#include "UploadFirmwareV2Unit.h"
#include "DataUnit.h"
#include "Unit1.h"
#include "common.h"
#include "Settings.h"
#pragma package(smart_init)
#pragma link "cgauges"
#pragma resource "*.dfm"
// *********************************************
#define DEFAULT_FIRMWARE_BASE_ADDR 0x08000000
#define DEFAULT_FIRMWARE_V2_BASE_ADDR (DEFAULT_FIRMWARE_BASE_ADDR + 0x00004000)
#pragma pack(push, 1)
typedef struct
{
char sz_signature[5]; // "DfuSe"
uint8_t b_version; // 0x01
uint32_t dfu_image_size; //
uint8_t b_targets; //
} t_dfuse_prefix;
typedef struct
{
char sz_signature[6]; // "Target"
uint8_t b_alternate_setting; //
uint32_t b_target_named; //
char sz_target_name[255]; //
uint32_t target_size; //
uint32_t nb_elements; //
} t_dfuse_target_prefix;
typedef struct
{
uint32_t address;
uint32_t size;
} t_dfuse_element;
typedef struct
{
uint8_t bcd_device_lo;
uint8_t bcd_device_hi;
uint8_t id_product_lo;
uint8_t id_product_hi;
uint8_t id_vendor_lo;
uint8_t id_vendor_hi;
uint8_t bcd_dfu_lo; // 0x1a
uint8_t bcd_dfu_hi; // 0x01
uint8_t uc_dfu_signature[3]; // "UFD"
uint8_t b_length; // 16
uint32_t dw_crc; // zlib crc32 over entire file bar this crc
} t_dfuse_suffix;
#pragma pack(pop)
// *********************************************
TUploadFirmwareV2Form *UploadFirmwareV2Form = NULL;
__fastcall TUploadFirmwareV2Form::TUploadFirmwareV2Form(TComponent* Owner)
: TForm(Owner)
{
}
void __fastcall TUploadFirmwareV2Form::FormCreate(TObject *Sender)
{
AnsiString s;
m_initialised = false;
Caption = Application->Title + " " + this->Caption;
// stop flicker
Memo1->ControlStyle = Memo1->ControlStyle << csOpaque;
Memo1->Clear();
Memo1->Lines->Add("");
Memo1->Lines->Add("You will need to ensure your NanoVNA V2 is in firmware upload mode before uploading new firmware.");
Memo1->Lines->Add("");
Memo1->Lines->Add("Ensure you're disconnected from the VNA (disconnect button top/left main window).");
Memo1->Lines->Add("Turn the VNA off.");
Memo1->Lines->Add("Ensure the VNA is plugged into your PC using the USB cable.");
Memo1->Lines->Add("Press and hold down the left user button on the VNA whilst turning the VNA back on.");
Memo1->Lines->Add("The VNA's screen should then be all white/blank (firmware upload mode).");
Memo1->Lines->Add("Reconnect to the VNA (connect button top/left main window).");
Memo1->Lines->Add("");
Memo1->Lines->Add("Press the \"open folder\" button above this memo to find the correct V2 firmware file.");
#ifdef _DEBUG
Memo1->Lines->Add("Or select one of the built-in firmwares to upload to your VNA (the above top row of buttons).");
#endif
Memo1->Lines->Add("");
Memo1->Lines->Add("If you uploaded the wrong firmware or the upload fails for whatever reason, don't");
Memo1->Lines->Add("worry, just repeat the above process with the correct firmware file.");
OpenDialog1->InitialDir = ExtractFilePath(Application->ExeName);
// CGauge1->DoubleBuffered = true;
CGauge1->Progress = 0;
m_flash_addr = DEFAULT_FIRMWARE_V2_BASE_ADDR;
m_flash_size = 0;
m_firmware.reserve(1024 * 1024 * 4); // 4MB
// LeaveDFUButton->Enabled = false;
// #ifdef _DEBUG
// GridPanel1->Visible = true;
// GridPanel1->Enabled = true;
// #else
// GridPanel1->Visible = false;
// GridPanel1->Enabled = false;
// #endif
// move to the saved position
this->Top = settings.firmwareV2WindowPos.top;
this->Left = settings.firmwareV2WindowPos.left;
this->Width = settings.firmwareV2WindowPos.width;
this->Height = settings.firmwareV2WindowPos.height;
}
void __fastcall TUploadFirmwareV2Form::FormDestroy(TObject *Sender)
{
//
}
void __fastcall TUploadFirmwareV2Form::FormClose(TObject *Sender, TCloseAction &Action)
{
// rebootUnit();
}
void __fastcall TUploadFirmwareV2Form::WMWindowPosChanging(TWMWindowPosChanging &msg)
{
const int thresh = WINDOW_SNAP;
if (msg.WindowPos->flags & SWP_STATECHANGED)
{
if (msg.WindowPos->flags & SWP_FRAMECHANGED)
{
if (msg.WindowPos->x < 0 && msg.WindowPos->y < 0)
{ // Window state is about to change to MAXIMIZED
if ((msg.WindowPos->flags & (SWP_SHOWWINDOW | SWP_NOACTIVATE)) == (SWP_SHOWWINDOW | SWP_NOACTIVATE))
{ // about to minimize
return;
}
else
{ // about to maximize
return;
}
}
}
}
if (msg.WindowPos->hwnd != this->Handle || Screen == NULL)
return;
const int dtLeft = Screen->DesktopRect.left;
//const int dtRight = Screen->DesktopRect.right;
const int dtTop = Screen->DesktopRect.top;
const int dtBottom = Screen->DesktopRect.bottom;
const int dtWidth = Screen->DesktopRect.Width();
const int dtHeight = Screen->DesktopRect.Height();
//const int waLeft = Screen->WorkAreaRect.left;
//const int waRight = Screen->WorkAreaRect.right;
//const int waTop = Screen->WorkAreaRect.top;
//const int waBottom = Screen->WorkAreaRect.bottom;
//const int waWidth = Screen->WorkAreaRect.Width();
//const int waHeight = Screen->WorkAreaRect.Height();
int x = msg.WindowPos->x;
int y = msg.WindowPos->y;
int w = msg.WindowPos->cx;
int h = msg.WindowPos->cy;
for (int i = 0; i < Screen->MonitorCount; i++)
{ // sticky screen edges
const int mLeft = Screen->Monitors[i]->WorkareaRect.left;
const int mRight = Screen->Monitors[i]->WorkareaRect.right;
const int mTop = Screen->Monitors[i]->WorkareaRect.top;
const int mBottom = Screen->Monitors[i]->WorkareaRect.bottom;
const int mWidth = Screen->Monitors[i]->WorkareaRect.Width();
const int mHeight = Screen->Monitors[i]->WorkareaRect.Height();
if (ABS(x - mLeft) < thresh)
x = mLeft; // stick left to left side
else
if (ABS((x + w) - mRight) < thresh)
x = mRight - w; // stick right to right side
if (ABS(y - mTop) < thresh)
y = mTop; // stick top to top side
else
if (ABS((y + h) - mBottom) < thresh)
y = mBottom - h; // stick bottom to bottm side
// stick the right side to the right side of the screen if the left side is stuck to the left side of the screen
if (x == mLeft)
if ((w >= (mWidth - thresh)) && (w <= (mWidth + thresh)))
w = mWidth;
// stick the bottom to the bottom of the screen if the top is stuck to the top of the screen
if (y == mTop)
if ((h >= (mHeight - thresh)) && (h <= (mHeight + thresh)))
h = mHeight;
}
/*
{ // sticky screen edges
if (ABS(x - waLeft) < thresh)
x = waLeft; // stick left to left side
else
if (ABS((x + w) - waRight) < thresh)
x = waRight - w; // stick right to right side
if (ABS(y - waTop) < thresh)
y = waTop; // stick top to top side
else
if (ABS((y + h) - waBottom) < thresh)
y = waBottom - h; // stick bottom to bottm side
// stick the right side to the right side of the screen if the left side is stuck to the left side of the screen
if (x == waLeft)
if ((w >= (waWidth - thresh)) && (w <= (waWidth + thresh)))
w = waWidth;
// stick the bottom to the bottom of the screen if the top is stuck to the top of the screen
if (y == waTop)
if ((h >= (waHeight - thresh)) && (h <= (waHeight + thresh)))
h = waHeight;
}
*/
// limit minimum size
if (w < Constraints->MinWidth)
w = Constraints->MinWidth;
if (h < Constraints->MinHeight)
h = Constraints->MinHeight;
// limit maximum size
if (w > Constraints->MaxWidth && Constraints->MaxWidth > Constraints->MinWidth)
w = Constraints->MaxWidth;
if (h > Constraints->MaxHeight && Constraints->MaxHeight > Constraints->MinHeight)
h = Constraints->MaxHeight;
// limit maximum size
if (w > dtWidth)
w = dtWidth;
if (h > dtHeight)
h = dtHeight;
/*
if (Application->MainForm && this != Application->MainForm)
{ // stick to our main form sides
const TRect rect = Application->MainForm->BoundsRect;
if (ABS(x - rect.left) < thresh)
x = rect.left; // stick to left to left side
else
if (ABS((x + w) - rect.left) < thresh)
x = rect.left - w; // stick right to left side
else
if (ABS(x - rect.right) < thresh)
x = rect.right; // stick to left to right side
else
if (ABS((x + w) - rect.right) < thresh)
x = rect.right - w; // stick to right to right side
if (ABS(y - rect.top) < thresh)
y = rect.top; // stick top to top side
else
if (ABS((y + h) - rect.top) < thresh)
y = rect.top - h; // stick bottom to top side
else
if (ABS(y - rect.bottom) < thresh)
y = rect.bottom; // stick top to bottom side
else
if (ABS((y + h) - rect.bottom) < thresh)
y = rect.bottom - h; // stick bottom to bottom side
}
*/
// stop it completely leaving the desktop area
if (x < (dtLeft - Width + (dtWidth / 15)))
x = dtLeft - Width + (dtWidth / 15);
if (x > (dtWidth - (Screen->Width / 15)))
x = dtWidth - (Screen->Width / 15);
if (y < dtTop)
y = dtTop;
if (y > (dtBottom - (dtHeight / 10)))
y = dtBottom - (dtHeight / 10);
msg.WindowPos->x = x;
msg.WindowPos->y = y;
msg.WindowPos->cx = w;
msg.WindowPos->cy = h;
}
void __fastcall TUploadFirmwareV2Form::show()
{
this->Show();
this->BringToFront();
this->Update();
}
void __fastcall TUploadFirmwareV2Form::ClearMemoButtonClick(TObject *Sender)
{
Memo1->Clear();
}
void __fastcall TUploadFirmwareV2Form::FormKeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
switch (Key)
{
case VK_ESCAPE:
Key = 0;
// rebootUnit();
this->Hide();
break;
}
}
void __fastcall TUploadFirmwareV2Form::uploadFirmwareFile(std::vector <uint8_t> &buffer, AnsiString file_ext)
{
String s;
if (!Form1)
return;
if (!Form1->connected() || !nanovna2_comms.inDFUMode())
return;
Memo1->Lines->Add("");
/*
if (unit_type == UNIT_TYPE_NONE)
{
String s = L"Invalid unit type";
Memo1->Lines->Add(s);
Application->NormalizeTopMosts();
Application->MessageBox(s.w_str(), L"Error", MB_ICONERROR | MB_OK);
Application->RestoreTopMosts();
return;
}
*/
if (buffer.size() <= 128)
{
s = L"File is too small to be valid";
Memo1->Lines->Add(s);
Application->NormalizeTopMosts();
Application->MessageBox(s.w_str(), L"Error", MB_ICONERROR | MB_OK);
Application->RestoreTopMosts();
return;
}
CGauge1->Progress = 0;
CGauge1->Update();
// ******************************
m_firmware.resize(0);
if (file_ext != ".hex" && file_ext != ".dfu" && file_ext != ".bin")
{
s = "Unknown file type, must be .bin, .dfu or .hex";
Memo1->Lines->Add(s);
Application->NormalizeTopMosts();
Application->MessageBox(s.w_str(), L"Error", MB_ICONERROR | MB_OK);
Application->RestoreTopMosts();
return;
}
if (file_ext == ".dfu")
{
// **********
const t_dfuse_prefix *prefix = (t_dfuse_prefix *)&buffer[0];
AnsiString prefix_sz_signature;
for (unsigned int i = 0; i < sizeof(prefix->sz_signature); i++)
prefix_sz_signature += prefix->sz_signature[i];
if (prefix_sz_signature != "DfuSe")
{
s.printf(L"Invalid DFU prefix signature");
Memo1->Lines->Add(s);
Application->NormalizeTopMosts();
Application->MessageBox(s.w_str(), L"Error", MB_ICONERROR | MB_OK);
Application->RestoreTopMosts();
return;
}
if (prefix->b_version != 1)
{
s.printf(L"Only DFU file format v1 currently supported [%u]", prefix->b_version);
Memo1->Lines->Add(s);
Application->NormalizeTopMosts();
Application->MessageBox(s.w_str(), L"Error", MB_ICONERROR | MB_OK);
Application->RestoreTopMosts();
return;
}
if ((prefix->dfu_image_size + sizeof(t_dfuse_suffix)) > buffer.size())
{
s.printf(L"Invalid DFU image size [dfu_image_size: %u actual dfu_image_size: %u]",
prefix->dfu_image_size,
buffer.size() - sizeof(t_dfuse_suffix));
Memo1->Lines->Add(s);
Application->NormalizeTopMosts();
Application->MessageBox(s.w_str(), L"Error", MB_ICONERROR | MB_OK);
Application->RestoreTopMosts();
return;
}
if (prefix->b_targets != 1)
{
s.printf(L"Invalid DFU number of targets [b_targets: %u]", prefix->b_targets);
Memo1->Lines->Add(s);
Application->NormalizeTopMosts();
Application->MessageBox(s.w_str(), L"Error", MB_ICONERROR | MB_OK);
Application->RestoreTopMosts();
return;
}
// **********
const t_dfuse_target_prefix *target_prefix = (t_dfuse_target_prefix *)&buffer[sizeof(t_dfuse_prefix)];
AnsiString target_sz_signature;
for (unsigned int i = 0; i < sizeof(target_prefix->sz_signature); i++)
target_sz_signature += target_prefix->sz_signature[i];
if (target_sz_signature != "Target")
{
s.printf(L"Invalid DFU target prefix [target sz_signature: %s]", target_sz_signature.c_str());
Memo1->Lines->Add(s);
Application->NormalizeTopMosts();
Application->MessageBox(s.w_str(), L"Error", MB_ICONERROR | MB_OK);
Application->RestoreTopMosts();
return;
}
// **********
const t_dfuse_suffix *suffix = (t_dfuse_suffix *)&buffer[prefix->dfu_image_size];
const uint32_t crc = common.updateCRC32(0xffffffff, &buffer[0], prefix->dfu_image_size + sizeof(t_dfuse_suffix) - sizeof(suffix->dw_crc));
if (crc != suffix->dw_crc)
{
s.printf(L"Invalid DFU crc [%08x actual: %08x]", suffix->dw_crc, crc);
Memo1->Lines->Add(s);
Application->NormalizeTopMosts();
Application->MessageBox(s.w_str(), L"Error", MB_ICONERROR | MB_OK);
Application->RestoreTopMosts();
return;
}
if (suffix->b_length != sizeof(t_dfuse_suffix))
{
s.printf(L"Invalid DFU suffix length [%u %u]", suffix->b_length, sizeof(t_dfuse_suffix));
Memo1->Lines->Add(s);
Application->NormalizeTopMosts();
Application->MessageBox(s.c_str(), L"Error", MB_ICONERROR | MB_OK);
Application->RestoreTopMosts();
return;
}
// **********
t_dfuse_element *element = (t_dfuse_element *)&buffer[sizeof(t_dfuse_prefix) + sizeof(t_dfuse_target_prefix)];
uint32_t element_data = sizeof(t_dfuse_prefix) + sizeof(t_dfuse_target_prefix) + sizeof(t_dfuse_element);
//m_flash_addr = element->address;
// Memo1->Lines->Add("");
for (unsigned int i = 0; i < target_prefix->nb_elements; i++)
{
{
s.printf(L"DFU element address [%u]: 0x%08x", i, element->address);
Memo1->Lines->Add(s);
s.printf(L"DFU element size [%u]: %u", i, element->size);
Memo1->Lines->Add(s);
}
if (element->size <= 128 || element->size >= (1024 * 1024 * 4))
{
s.printf(L"DFU firmware size too small");
Memo1->Lines->Add(s);
Application->NormalizeTopMosts();
Application->MessageBox(s.w_str(), L"Error", MB_ICONERROR | MB_OK);
Application->RestoreTopMosts();
return;
}
if (element->address < DEFAULT_FIRMWARE_BASE_ADDR || element->address >= (DEFAULT_FIRMWARE_BASE_ADDR + (1024 * 1024)))
{
s.printf(L"DFU firmware address out of range");
Memo1->Lines->Add(s);
Application->NormalizeTopMosts();
Application->MessageBox(s.c_str(), L"Error", MB_ICONERROR | MB_OK);
Application->RestoreTopMosts();
return;
}
const uint32_t addr = element->address - DEFAULT_FIRMWARE_BASE_ADDR;
const uint32_t old_size = m_firmware.size();
const uint32_t new_size = addr + element->size;
if (old_size < new_size)
{
m_firmware.resize(new_size);
memset(&m_firmware[old_size], 0xff, new_size - old_size);
}
memcpy(&m_firmware[addr], &buffer[element_data], element->size);
element_data += element->size + sizeof(t_dfuse_element);
element = (t_dfuse_element *)((uint8_t *)element + sizeof(t_dfuse_element) + element->size);
}
}
/*
// TEST ONLY
{ // save the firmware
const int file_handle = FileCreate("saved.bin");
if (file_handle > 0)
{
FileWrite(file_handle, &m_firmware[0], m_firmware.size());
FileClose(file_handle);
}
return;
}
*/
if (file_ext == ".hex")
{
char tmp[5];
uint8_t chksum;
uint8_t rec_length;
uint8_t rec_type;
uint8_t rec_chksum;
uint16_t rec_addr;
uint32_t size = 0;
uint32_t flash_addr = 0;
bool end_of_file_record = false;
std::vector <uint8_t> data(64);
m_firmware.resize(1024 * 1024 * 4); // allow for up to 4MB
memset(&m_firmware[0], 0xff, m_firmware.size());
Memo1->Lines->BeginUpdate();
//Memo1->Lines->Add("");
int line_num = 0;
unsigned int in_file_pos = 0;
while (in_file_pos < buffer.size())
{
String line;
line_num++;
data.resize(0);
// remove any invalid characters
unsigned int i = 0;
while (in_file_pos < buffer.size())
{
const char c = (char)buffer[in_file_pos];
if (i == 0)
{
if (c == ':')
i = 1; // found start of a line
}
else
{
if (c == ':')
break; // start of next line
if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'))
line += c;
}
in_file_pos++;
}
if (line.IsEmpty())
continue;
//Memo1->Lines->Add(line);
if (line.Length() < (2 + 4 + 2))
{
Memo1->Lines->EndUpdate();
m_firmware.resize(0);
s.printf(L"Invalid HEX file on line %d", line_num);
Memo1->Lines->Add(s);
Application->NormalizeTopMosts();
Application->MessageBox(s.w_str(), L"Error", MB_ICONERROR | MB_OK);
Application->RestoreTopMosts();
return;
}
int j = 1;
int k;
chksum = 0;
k = 0;
while (k < 2)
tmp[k++] = line[j++];
tmp[k] = '\0';
rec_length = (uint8_t)strtol(tmp, NULL, 16);
chksum += rec_length;
k = 0;
while (k < 4)
tmp[k++] = line[j++];
tmp[k] = '\0';
rec_addr = (uint16_t)strtol(tmp, NULL, 16);
chksum += (rec_addr >> 8) & 0xff;
chksum += (rec_addr >> 0) & 0xff;
k = 0;
while (k < 2)
tmp[k++] = line[j++];
tmp[k] = '\0';
rec_type = (uint8_t)strtol(tmp, NULL, 16);
chksum += rec_type;
if (rec_length != ((line.Length() - j - 1) / 2))
{
Memo1->Lines->EndUpdate();
m_firmware.resize(0);
s.printf(L"Invalid HEX file on line %d", line_num);
Memo1->Lines->Add(s);
Application->NormalizeTopMosts();
Application->MessageBox(s.w_str(), L"Error", MB_ICONERROR | MB_OK);
Application->RestoreTopMosts();
return;
}
for (i = 0; i < (unsigned int)rec_length; i++)
{
int k = 0;
while (k < 2)
tmp[k++] = line[j++];
tmp[k] = '\0';
const uint8_t b = (uint8_t)strtol(tmp, NULL, 16);
chksum += b;
data.push_back(b);
}
k = 0;
while (k < 2)
tmp[k++] = line[j++];
tmp[k] = '\0';
chksum += (uint8_t)strtol(tmp, NULL, 16);
if (chksum != 0)
{
Memo1->Lines->EndUpdate();
m_firmware.resize(0);
s.printf(L"Bad checksum in HEX file on line %d", line_num);
Memo1->Lines->Add(s);
Application->NormalizeTopMosts();
Application->MessageBox(s.w_str(), L"Error", MB_ICONERROR | MB_OK);
Application->RestoreTopMosts();
return;
}
// rec_type
// 00 - data record
// 01 - end-of-file record
// 02 - extended segment address record
// 04 - extended linear address record
// 05 - start linear address record (MDK-ARM only)
if (rec_type == 0)
{ // data record
uint32_t addr = flash_addr + rec_addr;
if (addr < DEFAULT_FIRMWARE_BASE_ADDR)
{
s.printf(L"Address out of range in HEX file on line %d", line_num);
Memo1->Lines->Add(s);
Application->NormalizeTopMosts();
Application->MessageBox(s.w_str(), L"Error", MB_ICONERROR | MB_OK);
Application->RestoreTopMosts();
return;
}
addr -= DEFAULT_FIRMWARE_BASE_ADDR;
if (size < (addr + data.size()))
size = addr + data.size();
if (size > m_firmware.capacity())
{
s.printf(L"Address out of range in HEX file on line %d", line_num);
Memo1->Lines->Add(s);
Application->NormalizeTopMosts();
Application->MessageBox(s.w_str(), L"Error", MB_ICONERROR | MB_OK);
Application->RestoreTopMosts();
return;
}
memcpy(&m_firmware[addr], &data[0], data.size());
}
if (rec_type == 1)
{ // end of file record
end_of_file_record = true;
break;
}
if (rec_type == 2)
{ // extended segment address record
uint32_t addr = 0;
for (unsigned int k = 0; k < data.size(); k++)
addr = (addr << 8) | data[k];
flash_addr = addr << 4;
}
if (rec_type == 3)
{ // unknown
}
if (rec_type == 4)
{ // 04 - extended linear address record
uint32_t addr = 0;
for (unsigned int k = 0; k < data.size(); k++)
addr = (addr << 8) | data[k];
flash_addr = addr << 16;
}
if (rec_type == 5)
{ // 05 - start linear address record (MDK-ARM only)
uint32_t addr = 0;
for (unsigned int k = 0; k < data.size(); k++)
addr = (addr << 8) | data[k];
flash_addr = addr;
}
}
Memo1->Lines->EndUpdate();
if (!end_of_file_record)
{
/* s.printf(L"End of file record not found");
Memo1->Lines->Add(s);
Application->NormalizeTopMosts();
Application->MessageBox(s.w_str(), L"Error", MB_ICONERROR | MB_OK);
Application->RestoreTopMosts();
m_firmware.resize(0);
return;
*/ }
m_firmware.resize(size);
}
if (file_ext == ".bin")
{
m_firmware.resize(buffer.size());
memcpy(&m_firmware[0], &buffer[0], m_firmware.size());
}
Memo1->Lines->Add("");
s.printf(L"Firmware size: %u bytes", m_firmware.size());
Memo1->Lines->Add(s);
// ******************************
if (data_unit.m_vna_data.type != UNIT_TYPE_NANOVNA_V2 || !nanovna2_comms.inDFUMode())
{
Application->NormalizeTopMosts();
Application->MessageBox(L"Your unit does not appear to be in DFU mode", L"Error", MB_ICONERROR | MB_OK);
Application->RestoreTopMosts();
m_firmware.resize(0);
return;
}
// **********************************************
// write the new firmware
nanovna2_comms.mode = MODE_DFU_MODE;
CGauge1->Progress = 0;
CGauge1->Update();
Memo1->Lines->Add("");
Memo1->Lines->Add("writing new firmware .. ");
nanovna2_comms.addTxNulls(true, 64);
nanovna2_comms.addTxFlashWriteAddr(DEFAULT_FIRMWARE_V2_BASE_ADDR);
nanovna2_comms.sendData();
int64_t addr_error = -1;
int block_size = 128;
m_block_buf.resize(block_size);
for (uint32_t addr = 0; addr < m_firmware.size() && Form1->connected(); addr += block_size)
{
uint32_t block_size = m_block_buf.size();
if (block_size > (m_firmware.size() - addr))
block_size = m_firmware.size() - addr;
memset(&m_block_buf[0], 0xff, m_block_buf.size());
memcpy(&m_block_buf[0], &m_firmware[addr], block_size);
nanovna2_comms.addTxWriteFlashData(true, m_block_buf, true);
nanovna2_comms.sendData();
uint8_t ack_byte = ~CMD_V2_INDICATE_ACK;
// wait for the VNA to finish writing the data to flash - wait for up to 2 seconds for the reply/ACK
Form1->m_comms.rx_timer.mark();
while (Form1->connected() && Form1->m_comms.rx_timer.millisecs(false) < 2000 && ack_byte != CMD_V2_INDICATE_ACK)
{
Sleep(1);
Application->ProcessMessages();
const int bytes_read = Form1->fetchCommsData(true);
if (bytes_read < 0)
break; // error
if (bytes_read <= 0)
continue; // no data received from the V2
Form1->m_comms.rx_timer.mark();
// display all the received bytes (as hex)
s = "";
for (int k = 0, i = 0; k < bytes_read; k++)
{
String s2;
const uint8_t b = (int)Form1->m_comms.rx.buffer[k]; // fetch an RX byte
s2.printf(L" %02x", b);
s += s2;
if (++i >= 16)
{
i = 0;
Form1->pushCommMessage("rx:" + s);
s = "";
}
}
if (!s.IsEmpty())
{
Form1->pushCommMessage("rx:" + s);
s = "";
}
// look for the ACK byte
for (int k = 0; k < bytes_read && ack_byte != CMD_V2_INDICATE_ACK; k++)
ack_byte = (int)Form1->m_comms.rx.buffer[k];
}
if (ack_byte != CMD_V2_INDICATE_ACK)
{ // flash write ACK not received
addr_error = (int64_t)addr;
break;
}
CGauge1->Progress = ((uint64_t)(addr + block_size) * CGauge1->MaxValue) / m_firmware.size();
CGauge1->Update();
}
Form1->m_comms.rx.buffer_wr = 0;
if (addr_error < 0)
{
Memo1->Lines->Add(" done ");
}
else
{ // error
s.printf(L"error writing to flash address 0x%08X", (uint32_t)(DEFAULT_FIRMWARE_V2_BASE_ADDR + addr_error));
Memo1->Lines->Add(s);
}
// Re-boot the unit
if (addr_error < 0)
{
LeaveDFUButton->Enabled = true;
rebootUnit();
Hide();
/*
Memo1->Lines->Add("");
Memo1->Lines->Add("The VNA now needs to be power cycled (turned off then on again).");
Memo1->Lines->Add("");
*/
}
if (Form1->connected())
nanovna2_comms.mode = MODE_IDLE;
}
void __fastcall TUploadFirmwareV2Form::rebootUnit()
{
nanovna2_comms.softReboot();
if (Form1)
Form1->disconnect();
// LeaveDFUButton->Enabled = false;
}
void __fastcall TUploadFirmwareV2Form::OpenFileBitBtnClick(TObject *Sender)
{
CCriticalSection cs(readWrite_cs, false);
if (!cs.tryEnter())
return;
Application->NormalizeTopMosts();
const bool ok = OpenDialog1->Execute();
Application->RestoreTopMosts();
if (!ok)
return;
Memo1->Lines->Clear();
Memo1->Lines->Add("");
this->Update();
String name = OpenDialog1->FileName;
String ext = ExtractFileExt(name).LowerCase();
std::vector <uint8_t> buffer;
{
String s;
s = "Loading \"" + name + "\" ..";
Memo1->Lines->Add(s);
const int size = common.loadFile(name, buffer);
if (size <= 0)
{
switch (size)
{
case -1: