-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathdfu.js
210 lines (185 loc) · 5.71 KB
/
dfu.js
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
Vue.use(VueMaterial.default);
new Vue({
el: "#app",
data: {
statusText: "init",
connected: false,
firmwareFile: null,
dfuDesc: null,
device: null,
log : [],
builds: [],
firmwareFileArrayBuffer: null,
progress: {
total: 0,
done: 0
},
},
methods: {
loadLatestBuilds: async function () {
const res = await fetch('https://circleci.com/api/v1.1/project/github/ttrftech/NanoVNA/tree/master?shallow=true&offset=0&limit=5&mine=false');
if (!res.ok) {
alert('failed to fetchlatest build information');
}
this.builds = await res.json();
},
downloadFirmware: async function (buildNum) {
const res = await fetch(`https://circleci.com/api/v1.1/project/github/ttrftech/NanoVNA/${buildNum}/artifacts`);
if (!res.ok) {
alert('failed to fetchlatest build information');
}
const artifacts = await res.json();
const binInfo = artifacts.find( i => /ch\.bin/.test(i.path) );
console.log({binInfo});
const a = document.createElement('a');
a.download = `ch-${buildNum}.bin`;
a.href = binInfo.url;
a.click();
},
select: async function () {
this.log.length = 0;
this.log.push("requestDevice...");
const filters = [
{ vendorId: 0x0483, productId: 0xdf11 }
];
let device;
try {
device = await navigator.usb.requestDevice({ filters });
} catch (e) {
alert(e);
this.statusText = e;
return;
}
if (!device) {
this.statusText = "no device selected";
return;
}
this.log.push("findDeviceDfuInterfaces");
const interfaces = await dfu.findDeviceDfuInterfaces(device);
if (interfaces.length === 0) {
this.statusText = "selected device does not have any dfu interface";
return;
}
try {
const d = new dfu.Device(device, interfaces[0]);
await device.open();
console.log(device);
await device.selectConfiguration(1);
const mapping = await d.readInterfaceNames();
for (let intf of interfaces) {
if (intf.name === null) {
let configIndex = intf.configuration.configurationValue;
let intfNumber = intf["interface"].interfaceNumber;
let alt = intf.alternate.alternateSetting;
intf.name = mapping[configIndex][intfNumber][alt];
}
}
} finally {
await device.close();
}
this.log.push("connecting...");
await this.connect(new dfu.Device(device, interfaces.find( i => /Internal Flash/.test(i.name) )));
},
fileChange: function (e) {
e.preventDefault();
const file = e.target.files[0];
if (/\.bin$/.test(file.name)) {
this.firmwareFile = file;
const reader = new FileReader();
reader.onload = () => {
this.firmwareFileArrayBuffer = reader.result;
};
reader.readAsArrayBuffer(file);
} else {
alert('require .bin file');
}
},
connect: async function (device) {
this.log.push("device.open");
try {
await device.open();
} catch (e) {
this.statusText = e;
return;
}
this.log.push("readConfigurationDescriptor");
const configDesc = dfu.parseConfigurationDescriptor(await device.readConfigurationDescriptor(0));
let funcDesc = null;
if (configDesc.bConfigurationValue === device.settings.configuration.configurationValue) {
for (let desc of configDesc.descriptors) {
if (desc.bDescriptorType == 0x21 && desc.hasOwnProperty("bcdDFUVersion")) {
funcDesc = desc;
break;
}
}
}
if (!funcDesc) {
this.statusText = "funcDesc is not found";
return;
}
const dfuDesc = {
WillDetach: ((funcDesc.bmAttributes & 0x08) != 0),
ManifestationTolerant: ((funcDesc.bmAttributes & 0x04) != 0),
CanUpload: ((funcDesc.bmAttributes & 0x02) != 0),
CanDnload: ((funcDesc.bmAttributes & 0x01) != 0),
TransferSize: funcDesc.wTransferSize,
DetachTimeOut: funcDesc.wDetachTimeOut,
DFUVersion: funcDesc.bcdDFUVersion
};
this.log.push(`dfuDesc: ${JSON.stringify(dfuDesc, null, 2)}`);
if (!dfuDesc.CanDnload) {
this.statusText = "download is disabled... give up";
return;
}
this.dfuDesc = dfuDesc;
if (dfuDesc.DFUVersion === 0x011a && device.settings.alternate.interfaceProtocol == 0x02) {
this.log.push("this is dfuse device");
device = new dfuse.Device(device.device_, device.settings);
console.log(device);
this.log.push(`memoryInfo: ${JSON.stringify(device.memoryInfo, null, 2)}`);
} else {
this.statusText = "unsupported device";
return;
}
device.logDebug = (m) => { this.log.push(`[DEBUG] ${m}`) };
device.logInfo = (m) => { this.log.push(`[INFO] ${m}`); this.statusText = m };
device.logWarning = (m) => { this.log.push(`[WARNING] ${m}`); this.statusText = m };
device.logError = (m) => { this.log.push(`[ERROR] ${m}`); this.statusText = m };
device.logProgress = (done, total) => {
this.progress.done = done;
this.progress.total = total;
};
this.connected = true;
this.device = device;
},
write: async function () {
const { device, firmwareFileArrayBuffer } = this;
try {
let status = await device.getStatus();
if (status.state == dfu.dfuERROR) {
await device.clearStatus();
}
} catch (error) {
this.statusText = "Failed to clear status";
}
device.startAddress = 0x8000000;
await device.do_download(this.dfuDesc.TransferSize, firmwareFileArrayBuffer, this.dfuDesc.ManifestationTolerant);
await device.waitDisconnected(5000);
this.connected = false;
this.device = null;
},
disconnect: async function () {
this.connected = false;
this.device.close();
this.device = null;
}
},
mounted: async function () {
console.log('mounted');
this.loadLatestBuilds();
this.$watch("log", () => {
const log = this.$refs.log;
log.scrollTop = log.scrollHeight;
});
}
});