-
Notifications
You must be signed in to change notification settings - Fork 26
/
UploadFirmwareUnit.h
162 lines (125 loc) · 4.8 KB
/
UploadFirmwareUnit.h
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
// (c) OneOfEleven 2020
//
// This code can be used on terms of WTFPL Version 2 (http://www.wtfpl.net)
#ifndef UploadFirmwareUnitH
#define UploadFirmwareUnitH
#define VC_EXTRALEAN
#define WIN32_EXTRA_LEAN
#define WIN32_LEAN_AND_MEAN
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Dialogs.hpp>
#include <ComCtrls.hpp>
#include <Buttons.hpp>
#include <Vcl.ExtCtrls.hpp>
#include <setupapi.h> // You must Manually add the setupapi.lib to the project library link tab!
#include <winioctl.h>
#include <initguid.h>
#include <vector>
#include "types.h"
#include "CriticalSection.h"
#include "HighResolutionTick.h"
#include "st_dfu.h"
#include "libusb.h"
// STM32F in bootloader mode (not DFU mode)
// shows up in libusb device list as ..
//
// list_devices: index 0
// list_devices: vid/pid 0483 DF11
// list_devices: manufacturer STMicroelectronics
// list_devices: product STM32 BOOTLOADER
// list_devices: serial number FFFFFFFEFFFF
//
// DfuUtil uses that mode
class TUploadFirmwareForm : public TForm
{
__published: // IDE-managed Components
TOpenDialog *OpenDialog1;
TMemo *Memo1;
TButton *ClearMemoButton;
TProgressBar *ProgressBar1;
TComboBox *USBDeviceComboBox;
TLabel *Label1;
TLabel *Label3;
TBitBtn *OpenFileBitBtn;
TButton *LeaveDFUButton;
TGridPanel *GridPanel1;
TButton *OneOfElevenFirmwareButton;
TButton *Button2;
TButton *Button1;
TBitBtn *CloseBitBtn;
TBitBtn *SaveFlashBitBtn;
TSaveDialog *SaveDialog1;
TLabel *Label2;
void __fastcall FormCreate(TObject *Sender);
void __fastcall FormDestroy(TObject *Sender);
void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
void __fastcall ClearMemoButtonClick(TObject *Sender);
void __fastcall USBDeviceComboBoxDropDown(TObject *Sender);
void __fastcall USBDeviceComboBoxSelect(TObject *Sender);
void __fastcall USBDeviceComboBoxChange(TObject *Sender);
void __fastcall FormKeyDown(TObject *Sender, WORD &Key,
TShiftState Shift);
void __fastcall OneOfElevenFirmwareButtonClick(TObject *Sender);
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
void __fastcall OpenFileBitBtnClick(TObject *Sender);
void __fastcall FormHide(TObject *Sender);
void __fastcall LeaveDFUButtonClick(TObject *Sender);
void __fastcall FormShow(TObject *Sender);
void __fastcall CloseBitBtnClick(TObject *Sender);
void __fastcall FormMouseEnter(TObject *Sender);
void __fastcall SaveFlashBitBtnClick(TObject *Sender);
private: // User declarations
bool m_initialised;
std::vector <HDEVNOTIFY> m_notification_handle;
AnsiString m_dfu_name;
std::vector <t_usb_device> m_usb_devices;
std::vector <uint8_t> m_firmware;
std::vector <uint8_t> m_block_buf;
std::vector <AnsiString> m_desc_string;
uint32_t m_flash_addr;
uint32_t m_flash_size;
CHighResolutionTick m_tick;
TSTDFU m_stdfu;
DFU_FUNCTIONAL_DESCRIPTOR m_DfuDesc;
UINT m_DfuInterfaceIdx;
UINT m_NbOfAlternates;
USB_DEVICE_DESCRIPTOR m_DeviceDesc;
// CRITICAL_SECTION readWriteCriticalSection;
CCriticalSectionObj readWrite_cs;
void __fastcall OnDeviceChange(TMessage &msg);
void __fastcall WMWindowPosChanging(TWMWindowPosChanging &msg);
void __fastcall saveData(std::vector <uint8_t> &data, String ID);
void __fastcall saveFlashToFile();
void __fastcall uploadFirmwareFile(std::vector <uint8_t> &buffer, AnsiString file_ext, int unit_type = UNIT_TYPE_NONE);
bool __fastcall updateUSBDeviceComboBox();
void __fastcall closeDFUDevice(bool lock_critical = true);
bool __fastcall openDFUDevice(String device_path);
bool __fastcall waitUntilState(int state, float timeout_secs);
bool __fastcall waitUntilIdle(float timeout_secs);
bool __fastcall download(void *buffer, int size, int block);
bool __fastcall upload(void *buffer, int size, int block);
bool __fastcall getStringDiscrip(int index, void *buffer, int size);
bool __fastcall selectCurrentConfig(int config_index, int interface_index, int alt_set_index);
bool __fastcall sendCommand(uint8_t a, uint8_t b);
bool __fastcall eraseBlock(uint32_t address);
bool __fastcall setAddressPointer(uint32_t address);
bool __fastcall getVidPid(AnsiString s, int &vid, int &pid);
void __fastcall rebootUnit();
void __fastcall usbProcess();
protected:
#pragma option push -vi-
BEGIN_MESSAGE_MAP
VCL_MESSAGE_HANDLER(WM_WINDOWPOSCHANGING, TWMWindowPosMsg, WMWindowPosChanging);
VCL_MESSAGE_HANDLER(WM_DEVICECHANGE, TMessage, OnDeviceChange);
END_MESSAGE_MAP(TForm)
#pragma option pop
public: // User declarations
__fastcall TUploadFirmwareForm(TComponent* Owner);
void __fastcall show();
};
extern PACKAGE TUploadFirmwareForm *UploadFirmwareForm;
#endif