Skip to content

Commit 1cc2f20

Browse files
committed
retry using intervals
1 parent e311cef commit 1cc2f20

File tree

1 file changed

+94
-60
lines changed

1 file changed

+94
-60
lines changed

src/webstlink.js

+94-60
Original file line numberDiff line numberDiff line change
@@ -219,73 +219,107 @@ export default class WebStlink {
219219
}
220220

221221
async find_mcus_by_core() {
222-
let err = "";
223-
224-
for (let retry = 0; retry < this._max_retry; retry++) {
225-
console.log(`find_mcus_by_core: ${retry+1}/${this._max_retry}`)
226-
227-
let cpuid = await this._stlink.get_debugreg32(CPUID_REG);
228-
if (cpuid == 0) {
229-
err = "Not connected to CPU";
230-
continue;
231-
}
232-
this._dbg.verbose("CPUID: " + H32(cpuid));
233-
let partno = (0xfff & (cpuid >> 4));
234-
let mcus = libstlink.DEVICES.find(mcus => (mcus.part_no == partno));
235-
if (mcus) {
236-
this._mcus_by_core = mcus;
237-
return;
238-
}
239-
240-
err = `PART_NO: 0x${H24(partno)} is not supported`;
241-
}
222+
return new Promise((resolve, reject) => {
223+
let err = "";
224+
let retry = 0;
225+
226+
const retryInterval = setInterval(async () => {
227+
retry++;
228+
console.log(`find_mcus_by_core attempt: ${retry}`)
229+
230+
let cpuid = await this._stlink.get_debugreg32(CPUID_REG);
231+
if (cpuid == 0) {
232+
err = "Not connected to CPU";
233+
return;
234+
}
235+
this._dbg.verbose("CPUID: " + H32(cpuid));
236+
let partno = (0xfff & (cpuid >> 4));
237+
let mcus = libstlink.DEVICES.find(mcus => (mcus.part_no == partno));
238+
if (mcus) {
239+
this._mcus_by_core = mcus;
240+
clearInterval(retryInterval);
241+
resolve();
242+
}
243+
244+
err = `PART_NO: 0x${H24(partno)} is not supported`;
245+
}, 100)
246+
247+
setTimeout(() => {
248+
if (retryInterval) {
249+
clearInterval(retryInterval);
250+
}
251+
if (!this._mcus_by_core) {
252+
reject(new libstlink.exceptions.Exception(err));
253+
}
254+
}, 5000);
255+
})
242256

243-
244-
throw new libstlink.exceptions.Exception(err);
245257
}
246258

247259
async find_mcus_by_devid() {
248-
let err = "";
249-
250-
for (let retry = 0; retry < this._max_retry; retry++) {
251-
console.log(`find_mcus_by_devid: ${retry+1}/${this._max_retry}`)
252-
253-
let idcode = await this._stlink.get_debugreg32(this._mcus_by_core.idcode_reg);
254-
this._dbg.verbose("IDCODE: " + H32(idcode));
255-
let devid = (0xfff & idcode);
256-
let mcus = this._mcus_by_core.devices.find(mcus => (mcus.dev_id == devid));
257-
if (mcus) {
258-
this._mcus_by_devid = mcus;
259-
return;
260-
}
261-
262-
err = `DEV_ID: 0x${H24(devid)} is not supported`;
263-
}
264-
265-
266-
throw new libstlink.exceptions.Exception(err);
260+
return new Promise((resolve, reject) => {
261+
let err = "";
262+
let retry = 0;
263+
264+
const retryInterval = setInterval(async () => {
265+
retry++;
266+
console.log(`find_mcus_by_devid attempt: ${retry}`)
267+
268+
let idcode = await this._stlink.get_debugreg32(this._mcus_by_core.idcode_reg);
269+
this._dbg.verbose("IDCODE: " + H32(idcode));
270+
let devid = (0xfff & idcode);
271+
let mcus = this._mcus_by_core.devices.find(mcus => (mcus.dev_id == devid));
272+
if (mcus) {
273+
this._mcus_by_devid = mcus;
274+
clearInterval(retryInterval);
275+
resolve();
276+
}
277+
278+
err = `DEV_ID: 0x${H24(devid)} is not supported`;
279+
}, 100)
280+
281+
setTimeout(() => {
282+
if (retryInterval) {
283+
clearInterval(retryInterval);
284+
}
285+
if (!this._mcus_by_devid) {
286+
reject(new libstlink.exceptions.Exception(err));
287+
}
288+
}, 5000);
289+
})
267290
}
268291

269292
async find_mcus_by_flash_size() {
270-
let err = "";
271-
272-
for (let retry = 0; retry < this._max_retry; retry++) {
273-
console.log(`find_mcus_by_flash_size: ${retry+1}/${this._max_retry}`)
274-
275-
this._flash_size = await this._stlink.get_debugreg16(this._mcus_by_devid.flash_size_reg);
276-
this._mcus = this._mcus_by_devid.devices.filter(
277-
mcu => (mcu.flash_size == this._flash_size)
278-
);
279-
280-
if (this._mcus.length !== 0) {
281-
return;
282-
}
283-
284-
err = `Connected CPU with DEV_ID: 0x${H24(this._mcus_by_devid.dev_id)} and FLASH size: ${this._flash_size}KB is not supported`;
285-
}
286-
287-
288-
throw new libstlink.exceptions.Exception(err);
293+
return new Promise((resolve, reject) => {
294+
let err = "";
295+
let retry = 0;
296+
297+
const retryInterval = setInterval(async () => {
298+
retry++;
299+
console.log(`find_mcus_by_flash_size attempt: ${retry}`)
300+
301+
this._flash_size = await this._stlink.get_debugreg16(this._mcus_by_devid.flash_size_reg);
302+
this._mcus = this._mcus_by_devid.devices.filter(
303+
mcu => (mcu.flash_size == this._flash_size)
304+
);
305+
306+
if (this._mcus.length !== 0) {
307+
clearInterval(retryInterval);
308+
resolve()
309+
}
310+
311+
err = `Connected CPU with DEV_ID: 0x${H24(this._mcus_by_devid.dev_id)} and FLASH size: ${this._flash_size}KB is not supported`;
312+
}, 100)
313+
314+
setTimeout(() => {
315+
if (retryInterval) {
316+
clearInterval(retryInterval);
317+
}
318+
if (!this._mcus.length === 0) {
319+
reject(new libstlink.exceptions.Exception(err));
320+
}
321+
}, 5000);
322+
})
289323
}
290324

291325
fix_cpu_type(cpu_type) {

0 commit comments

Comments
 (0)