-
Notifications
You must be signed in to change notification settings - Fork 26
/
UploadFirmwareUnit.cpp
2635 lines (2103 loc) · 64.7 KB
/
UploadFirmwareUnit.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>
//#include <intrin.h>
#pragma hdrstop
#include "UploadFirmwareUnit.h"
#include "DataUnit.h"
#include "Unit1.h"
#include "common.h"
#include "Settings.h"
//#include <initguid.h>
#include "Dbt.h"
#pragma package(smart_init)
#pragma resource "*.dfm"
#define DEFAULT_FIRMWARE_BASE_ADDR 0x08000000
// **************
// NanoVNA-H
#define NANOVNA_H_SAVEAREA_MAX 5
#define NANOVNA_H_SAVE_CONFIG_SIZE 0x00000800
#define NANOVNA_H_SAVE_PROP_CONFIG_SIZE 0x00001800
#define NANOVNA_H_SAVE_CONFIG_ADDR 0x08018000
#define NANOVNA_H_SAVE_PROP_CONFIG_ADDR (NANOVNA_H_SAVE_CONFIG_ADDR + NANOVNA_H_SAVE_CONFIG_SIZE)
#define NANOVNA_H_SAVE_FULL_AREA_SIZE (NANOVNA_H_SAVE_CONFIG_SIZE + NANOVNA_H_SAVEAREA_MAX * NANOVNA_H_SAVE_PROP_CONFIG_SIZE)
// **************
// NanoVNA-H4
#define NANOVNA_H4_SAVEAREA_MAX 5
#define NANOVNA_H4_SAVE_CONFIG_SIZE 0x00000800
#define NANOVNA_H4_SAVE_PROP_CONFIG_SIZE 0x00004000
#define NANOVNA_H4_SAVE_CONFIG_ADDR 0x0802B800
#define NANOVNA_H4_SAVE_PROP_CONFIG_ADDR (NANOVNA_H4_SAVE_CONFIG_ADDR + NANOVNA_H4_SAVE_CONFIG_SIZE)
#define NANOVNA_H4_SAVE_FULL_AREA_SIZE (NANOVNA_H4_SAVE_CONFIG_SIZE + NANOVNA_H4_SAVEAREA_MAX * NANOVNA_H4_SAVE_PROP_CONFIG_SIZE)
// **************
const GUID GUID_DEVINTERFACE_COMPORT = {0x86e0d1e0, 0x8089, 0x11d0, {0x9c, 0xe4, 0x08, 0x00, 0x3e, 0x30, 0x1f, 0x73}};
const GUID GUID_DEVINTERFACE_USB_DEVICE = {0xA5DCBF10, 0x6530, 0x11D2, {0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED}};
//const GUID GUID_DFU = {0x3FE809AB, 0xFB91, 0x4CB5, {0xA6, 0x43, 0x69, 0x67, 0x0D, 0x52, 0x36, 0x6E}};
//const GUID GUID_APP = {0xcb979912, 0x5029, 0x420a, {0xae, 0xb1, 0x34, 0xfc, 0x0a, 0x7d, 0x57, 0x26}};
// VID/PID for STM32F bootloader
#define MD_VID 0x0483
#define MD_PID 0xDF11
// VID's/PID's for GD32 NanoVNA V2's
#define V2a_VID 0x0483 // this when no bootloader and no firmware present ?
#define V2a_PID 0x5740 //
#define V2b_VID 0x04b4 // this when a NanoVNA V2 is present - either DFU bootloader mode or normal mode
#define V2b_PID 0x0008 // " "
#define CONTROL_BLOCK 0
#define SPI_BLOCK 1
#define FLASH_BLOCK 2
#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)
TUploadFirmwareForm *UploadFirmwareForm = NULL;
__fastcall TUploadFirmwareForm::TUploadFirmwareForm(TComponent* Owner)
: TForm(Owner)
{
}
void __fastcall TUploadFirmwareForm::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 need to ensure your NanoVNA V1 is in DFU mode before uploading new firmware.");
Memo1->Lines->Add("");
Memo1->Lines->Add("NanoVNA-H ... go into the CONFIG/DFU menu and enable DFU mode.");
Memo1->Lines->Add("NanoVNA-H4 .. turn VNA off, press & hold down the joy button whilst turning the unit back on.");
Memo1->Lines->Add("");
Memo1->Lines->Add("Then press the \"open folder\" button above this memo to find the correct firmware file.");
Memo1->Lines->Add("Or select one of the built-in firmwares shown in the above row of buttons.");
Memo1->Lines->Add("");
Memo1->Lines->Add("If you uploaded the wrong firmware or the upload fails for whatever reason, don't worry, you");
Memo1->Lines->Add("can easily recover the unit by shorting the 'BOOT0' and 'VDD' pin's/pads together on the VNA's");
Memo1->Lines->Add("PCB whilst turning the unit back on if need be.");
OpenDialog1->InitialDir = ExtractFilePath(Application->ExeName);
SaveDialog1->InitialDir = ExtractFilePath(Application->ExeName);
ProgressBar1->DoubleBuffered = true;
ProgressBar1->Position = 0;
m_flash_addr = DEFAULT_FIRMWARE_BASE_ADDR;
m_flash_size = 0;
m_firmware.reserve(1024 * 1024 * 4); // 4MB
memset(&m_DeviceDesc, 0, sizeof(USB_DEVICE_DESCRIPTOR));
memset(&m_DfuDesc, 0, sizeof(DFU_FUNCTIONAL_DESCRIPTOR));
m_DfuInterfaceIdx = 0;
m_NbOfAlternates = 0;
#if 0
{ // USB serial comport
DEV_BROADCAST_DEVICEINTERFACE dbch;
memset(&dbch, 0, sizeof(dbch));
dbch.dbcc_size = sizeof(dbch);
dbch.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
dbch.dbcc_classguid = GUID_DEVINTERFACE_COMPORT;
HDEVNOTIFY handle = ::RegisterDeviceNotification(this->Handle, &dbch, DEVICE_NOTIFY_WINDOW_HANDLE);
if (handle != NULL)
m_notification_handle.push_back(handle);
}
#endif
#if 1
{ // DFU device
DEV_BROADCAST_DEVICEINTERFACE dbch;
memset(&dbch, 0, sizeof(dbch));
dbch.dbcc_size = sizeof(dbch);
dbch.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
dbch.dbcc_classguid = GUID_DEVINTERFACE_USB_DEVICE;
HDEVNOTIFY handle = ::RegisterDeviceNotification(this->Handle, &dbch, DEVICE_NOTIFY_WINDOW_HANDLE);
if (handle != NULL)
m_notification_handle.push_back(handle);
}
#endif
// LeaveDFUButton->Enabled = false;
// move to the saved position
this->Top = settings.firmwareWindowPos.top;
this->Left = settings.firmwareWindowPos.left;
this->Width = settings.firmwareWindowPos.width;
this->Height = settings.firmwareWindowPos.height;
}
void __fastcall TUploadFirmwareForm::FormDestroy(TObject *Sender)
{
for (int i = (int)m_notification_handle.size() - 1; i >= 0; i--)
{
if (m_notification_handle[i])
::UnregisterDeviceNotification(m_notification_handle[i]);
m_notification_handle[i] = NULL;
}
m_notification_handle.resize(0);
closeDFUDevice();
}
void __fastcall TUploadFirmwareForm::FormClose(TObject *Sender, TCloseAction &Action)
{
// rebootUnit();
}
void __fastcall TUploadFirmwareForm::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 TUploadFirmwareForm::OnDeviceChange(TMessage &msg)
{
const DWORD wparam = msg.WParam;
const DWORD lparam = msg.LParam;
const int event_type = wparam;
String s;
String Msg = "OnDeviceChange .. ";
String name_str;
Msg += " event type: ";
switch (event_type)
{
case DBT_APPYBEGIN:
Msg += "DBT_APPYBEGIN";
break;
case DBT_APPYEND:
Msg += "DBT_CONFIGCHANGECANCELED";
break;
case DBT_MONITORCHANGE:
Msg += "DBT_MONITORCHANGE";
break;
case DBT_SHELLLOGGEDON:
Msg += "DBT_SHELLLOGGEDON";
break;
case DBT_CONFIGMGAPI32:
Msg += "DBT_CONFIGMGAPI32";
break;
case DBT_VXDINITCOMPLETE:
Msg += "DBT_VXDINITCOMPLETE";
break;
case DBT_VOLLOCKQUERYLOCK:
Msg += "VOLLOCKQUERYLOCK";
break;
case DBT_VOLLOCKLOCKTAKEN:
Msg += "DBT_VOLLOCKLOCKTAKEN";
break;
case DBT_VOLLOCKLOCKFAILED:
Msg += "DBT_VOLLOCKLOCKFAILED";
break;
case DBT_VOLLOCKQUERYUNLOCK:
Msg += "DBT_VOLLOCKQUERYUNLOCK";
break;
case DBT_VOLLOCKLOCKRELEASED:
Msg += "DBT_VOLLOCKLOCKRELEASED";
break;
case DBT_VOLLOCKUNLOCKFAILED:
Msg += "DBT_VOLLOCKUNLOCKFAILED";
break;
case DBT_CONFIGCHANGED:
Msg += "DBT_CONFIGCHANGED";
break;
case DBT_CUSTOMEVENT:
Msg += "DBT_CUSTOMEVENT";
break;
case DBT_DEVICEARRIVAL:
Msg += "DBT_DEVICEARRIVAL";
break;
case DBT_DEVICEQUERYREMOVE:
Msg += "DBT_DEVICEQUERYREMOVE";
break;
case DBT_DEVICEQUERYREMOVEFAILED:
Msg += "DBT_DEVICEQUERYREMOVEFAILED";
break;
case DBT_DEVICEREMOVEPENDING:
Msg += "DBT_DEVICEREMOVEPENDING";
break;
case DBT_DEVICEREMOVECOMPLETE:
Msg += "DBT_DEVICEREMOVECOMPLETE";
break;
case DBT_DEVICETYPESPECIFIC:
Msg += "DBT_DEVICETYPESPECIFIC";
break;
case DBT_QUERYCHANGECONFIG:
Msg += "DBT_QUERYCHANGECONFIG";
break;
case DBT_DEVNODES_CHANGED:
Msg += "DBT_DEVNODES_CHANGED";
break;
case DBT_USERDEFINED:
Msg += "DBT_USERDEFINED";
break;
default:
s.printf(L"unknown [%d]", wparam);
Msg += s;
break;
}
PDEV_BROADCAST_DEVICEINTERFACE pdbch = (PDEV_BROADCAST_DEVICEINTERFACE)lparam;
if (pdbch == NULL)
{
Msg += ", device type: none";
// Memo1->Lines->Add(Msg);
return;
}
const DWORD device_type = pdbch->dbcc_devicetype;
const GUID guid = pdbch->dbcc_classguid;
s.printf(L", guid %08X %04X %04X %02X %02X %02X %02X %02X %02X %02X %02X",
guid.Data1, guid.Data2, guid.Data3, guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
Msg += s;
if (pdbch->dbcc_classguid == GUID_DEVINTERFACE_USB_DEVICE)
{
}
if (pdbch->dbcc_classguid == GUID_DEVINTERFACE_COMPORT)
{
}
Msg += ", device type: ";
switch (device_type)
{
case DBT_DEVTYP_OEM: // oem-defined device type
Msg += "DBT_DEVTYP_OEM";
break;
case DBT_DEVTYP_DEVNODE: // devnode number
Msg += "DBT_DEVTYP_DEVNODE";
break;
case DBT_DEVTYP_VOLUME: // logical volume
Msg += "DBT_DEVTYP_VOLUME";
break;
case DBT_DEVTYP_PORT: // serial, parallel
Msg += "DBT_DEVTYP_PORT";
if (event_type == DBT_DEVICEARRIVAL)
{ // added
}
else
if (event_type == DBT_DEVICEREMOVECOMPLETE)
{ // removed
}
break;
case DBT_DEVTYP_NET: // network resource
Msg += "DBT_DEVTYP_NET";
break;
case DBT_DEVTYP_DEVICEINTERFACE: // device interface class
Msg += "DBT_DEVTYP_DEVICEINTERFACE";
#if defined(__BORLANDC__)
name_str = String((const wchar_t *)pdbch->dbcc_name);
if (!name_str.IsEmpty())
Msg += ", name: " + name_str + " ";
#endif
// NanoVNA DFU
// name: \\?\USB#VID_0483&PID_DF11#FFFFFFFEFFFF#{a5dcbf10-6530-11d2-901f-00c04fb951ed}
if (name_str.UpperCase().Pos("USB#VID_0483&PID_DF11") > 0)
{
if (event_type == DBT_DEVICEARRIVAL)
{ // added
//m_dfu_name = name_str;
Memo1->Lines->Add("");
Memo1->Lines->Add(Msg);
if (this->Visible)
Beep(440, 80);
}
else
if (event_type == DBT_DEVICEREMOVECOMPLETE)
{ // removed
if (m_dfu_name.UpperCase() == name_str.UpperCase())
{
m_dfu_name = "";
Memo1->Lines->Add(Msg);
if (this->Visible)
Beep(349.23, 80);
}
}
usbProcess();
}
break;
case DBT_DEVTYP_HANDLE: // file system handle
Msg += "DBT_DEVTYP_HANDLE";
break;
// #if(WINVER >= _WIN32_WINNT_WIN7)
// case DBT_DEVTYP_DEVINST: // device instance
// Msg += "DBT_DEVTYP_DEVINST";
// break;
// #endif
default:
s.printf(L"unknown device type [%u]", device_type);
Msg += s;
#if defined(__BORLANDC__)
Msg += ", name: " + String((const wchar_t *)pdbch->dbcc_name) + " ";
#endif
break;
}
// Memo1->Lines->Add(Msg);
}
void __fastcall TUploadFirmwareForm::show()
{
this->Show();
this->BringToFront();
this->Update();
usbProcess();
}
void __fastcall TUploadFirmwareForm::saveData(std::vector <uint8_t> &data, String ID)
{
if (data.empty())
return;
String filename;
ID = ID.Trim();
if (!ID.IsEmpty())
filename += ID;
if (!filename.IsEmpty())
filename += "_";
filename += FormatDateTime("yyyy-mm-dd_hh-nn-ss", Now());
// remove/replace any invalid filename characters
filename = common.cleanFilename(filename, false, true);
SaveDialog1->Title = "Save the flash image to ..";
SaveDialog1->FileName = filename;
Application->NormalizeTopMosts();
const bool ok = SaveDialog1->Execute();
Application->RestoreTopMosts();
if (!ok)
return;
filename = SaveDialog1->FileName;
AnsiString ext = ExtractFileExt(filename).LowerCase();
if (ext == ".bin")
{
common.saveFile(filename, data);
return;
}
if (ext == ".hex")
{
// https://en.wikipedia.org/wiki/Intel_HEX
//
// rec_type
// 00 - data record
// 01 - end-of-file record
// 02 - extended segment address record
// 03 - start segment address
// 04 - extended linear address record
// 05 - start linear address record (MDK-ARM only)
//
// :02 0000 04 0800 F2
//
// :10 0000 00 00020020010100086134000851D40008 FA
// :10 0010 00 E1620008E1620008E1620008E1620008 B4
// "" ""
// :10 FFE0 00 083D0020000000000000000000000000 AC
// :10 FFF0 00 70B50225054C0134290020006419FFF7 73
//
// :02 0000 04 0801 F1
//
// :10 0000 00 27FE2578002DF6D170BDC046A0630108 FB
// :10 0010 00 084A0300D16910B50020994206D8581A 41
// "" ""
// :10 77B0 00 71727466000000003863ED3EDA0F493F D5
// :10 77C0 00 5E987B3FDA0FC93F6937AC3168212233 BD
//
// :08 77D0 00 B40F14336821A23349
// :08 77D8 00 488FFE7F0100000054
//
// :10 77E0 00 0096000000000800206B4D475450455A 99
// :10 77F0 00 590000006D1D6E7066617A7900010000 0D
// "" ""
// :10 7940 00 00000000000000000000000000000000 37
// :10 7950 00 00000000000000000000000000000000 27
//
// :04 0000 05 08000101 ED
// :00 0000 01 FF
std::vector <String> lines;
int rec_type = 4; // start with extended linear address record
const uint32_t base_addr = 0x08000000;
uint32_t addr = 0;
while (true)
{
String s = ":";
String s2;
uint8_t chksum = 0;
int size = 0;
switch (rec_type)
{
case 0: // data record
size = 16;
break;
case 1: // end-of-file record
size = 0;
break;
case 2: // extended segment address record
size = 2;
break;
case 3: // start segment address
size = 4;
break;
case 4: // extended linear address record
size = 2;
break;
case 5: // start linear address record (MDK-ARM only)
size = 4;
break;
}
// size of data to follow (8-bit)
s2.printf(L"%02X", size);
s += s2;
chksum += (uint8_t)size;
{ // address
uint16_t adr = 0x0000;
if (rec_type == 0) // data record
adr = (uint16_t)(addr & 0xffff);
/* else
if (rec_type == 1) // end-of-file record
adr = 0x0000;
else
if (rec_type == 2) // extended segment address record
adr = 0x0000;
else
if (rec_type == 3) // start segment address
adr = 0x0000;
else
if (rec_type == 4) // extended linear address record
adr = 0x0000;
else
if (rec_type == 5) // start linear address record (MDK-ARM only)
adr = 0x0000;
*/
s2.printf(L"%04X", adr);
s += s2;
chksum += (uint8_t)(adr >> 8);
chksum += (uint8_t)(adr >> 0);
}
// record type (8-bit)
s2.printf(L"%02X", rec_type);
s += s2;
chksum += rec_type;
// data
if (rec_type == 0)
{ // data record
for (int i = 0; i < size; i++)
{
const uint8_t b = data[addr++];
s2.printf(L"%02X", b);
s += s2;
chksum += b;
}
}
else
if (rec_type == 1)
{ // end-of-file record
}
else
if (rec_type == 2)
{ // extended segment address record
}
else
if (rec_type == 3)
{ // start segment address
}
else
if (rec_type == 4)
{ // extended linear address record
const uint16_t value = (uint16_t)((base_addr + addr) >> 16);
s2.printf(L"%04X", value);
s += s2;
chksum += (uint8_t)(value >> 8);
chksum += (uint8_t)(value >> 0);
}
else
if (rec_type == 5)
{ // start linear address record (MDK-ARM only)
const uint32_t value = base_addr + addr;
s2.printf(L"%08X", value);
s += s2;
chksum += (uint8_t)(value >> 24);
chksum += (uint8_t)(value >> 16);
chksum += (uint8_t)(value >> 8);
chksum += (uint8_t)(value >> 0);
}
// checksum (8-bit)
s2.printf(L"%02X", (uint8_t)(0 - (int)chksum));
s += s2;
// line is complete
lines.push_back(s);
// decide on the next stage
if (rec_type == 0) // data record
{
if (addr >= data.size())
rec_type = 5;
else
if ((addr & 0xffff) == 0)
rec_type = 4;
}
else
if (rec_type == 1) // end-of-file record
{
break;
}
else
if (rec_type == 2) // extended segment address record
{
}
else
if (rec_type == 3) // start segment address
{
}
else
if (rec_type == 4) // extended linear address record
{
rec_type = 0;
}
else
if (rec_type == 5) // start linear address record (MDK-ARM only)
{
rec_type = 1;
}
else
{ // error
break;
}
}
common.saveFile(filename, lines);
return;
}
Application->NormalizeTopMosts();
Application->MessageBox(L"No saved - unknown file extension", L"Error", MB_ICONERROR | MB_OK);
Application->RestoreTopMosts();
}
void __fastcall TUploadFirmwareForm::ClearMemoButtonClick(TObject *Sender)
{
Memo1->Clear();
}
bool __fastcall TUploadFirmwareForm::openDFUDevice(String device_path)
{
AnsiString s;
DFUSTATUS status;
if (m_stdfu.isOpen())
return false;
m_flash_addr = 0;
m_flash_size = 0;
m_desc_string.resize(0);
if (device_path.IsEmpty())
return false;
/*
int i = USBDeviceComboBox->ItemIndex;
if (i >= 0)
i = (int)USBDeviceComboBox->Items->Objects[i];
if (i < 0)
return false;
if (i < 0 || i >= (int)m_usb_devices.size())
return false;
device_path = m_usb_devices[i].path;
*/
Memo1->Lines->Add("");
Memo1->Lines->Add("Opening " + device_path + " ");
if (m_stdfu.open(AnsiString(device_path).c_str()) != STDFU_NOERROR)
{
Memo1->Lines->Add(" open failed ");
return false;
}
// **********************************
memset(&m_DfuDesc, 0, sizeof(DFU_FUNCTIONAL_DESCRIPTOR));
m_DfuInterfaceIdx = 0;
m_NbOfAlternates = 0;
if (m_stdfu.getDFUDescriptor(&m_DfuInterfaceIdx, &m_NbOfAlternates, &m_DfuDesc) != STDFU_NOERROR)
{
Memo1->Lines->Add(" get dfu descriptor failed ");
return false;
}
if (m_DfuDesc.wTransfertSize <= 0)
{
Memo1->Lines->Add(" invalid block size ");
m_DfuDesc.wTransfertSize = 1024;
// return false;
}
m_block_buf.resize(m_DfuDesc.wTransfertSize);
Memo1->Lines->Add("");
s.printf("block size: %d ", m_DfuDesc.wTransfertSize);
Memo1->Lines->Add(s);
// **********************************
memset(&m_DeviceDesc, 0, sizeof(USB_DEVICE_DESCRIPTOR));
if (m_stdfu.getDeviceDescriptor(&m_DeviceDesc) == STDFU_NOERROR)
{
AnsiString s;
Memo1->Lines->Add("");
if ((m_DfuDesc.bcdDFUVersion < 0x011A) || (m_DfuDesc.bcdDFUVersion >= 0x0120))
{
if (m_DfuDesc.bcdDFUVersion != 0)
Memo1->Lines->Add("Bad DFU protocol version. Should be 1.1A ");
}
else
{
s.printf("vid: 0x%04X ", m_DeviceDesc.idVendor);
Memo1->Lines->Add(s);
s.printf("pid: 0x%04X ", m_DeviceDesc.idProduct);
Memo1->Lines->Add(s);
s.printf("ver: %d.%02X ", m_DeviceDesc.bcdDevice >> 8, m_DeviceDesc.bcdDevice & 0xff);
Memo1->Lines->Add(s);
}
}
// **********************************
// STM32F072
//
// vid: 0x0483
// pid: 0xDF11
// ver: 34.00
//
// desc 1:STMicroelectronics
// desc 2:STM32 BOOTLOADER
// desc 3:FFFFFFFEFFFF
// desc 4:@Internal Flash /0x08000000/064*0002Kg
// desc 5:@Option Bytes /0x1FFFF800/01*016 e
//
// Flash addr: 0x08000000
// Flash size: 131072
// STM32F303
//
// vid: 0x0483
// pid: 0xDF11
// ver: 34.00
//
// desc 1:STMicroelectronics
// desc 2:STM32 BOOTLOADER
// desc 3:205C366B2039
// desc 4:@Internal Flash /0x08000000/128*0002Kg
// desc 5:@Option Bytes /0x1FFFF800/01*016 e
//
// Flash addr: 0x08000000
// Flash size: 262144
if (!selectCurrentConfig(0, 0, 0))
{
Memo1->Lines->Add(" error: select current config 1");
return false;
}
Memo1->Lines->Add("");
for (int i = 1; i < 8; i++)
{
AnsiString s1;
AnsiString s3;
char str[256];
s1.printf("desc %d:", i);
if (!getStringDiscrip(i, str, sizeof(str)))
{