Raise errors instead of logging them
This commit is contained in:
parent
49c3278f2f
commit
782c16df7f
44
ESPLoader.js
44
ESPLoader.js
@ -1,3 +1,5 @@
|
|||||||
|
import {ESPError, TimeoutError} from "./error.js";
|
||||||
|
|
||||||
const MAGIC_TO_CHIP = {
|
const MAGIC_TO_CHIP = {
|
||||||
[0x00f01d83]: () => import('./targets/esp32.js'),
|
[0x00f01d83]: () => import('./targets/esp32.js'),
|
||||||
[0x6921506f]: () => import('./targets/esp32c3.js'), // ESP32C3 eco 1+2
|
[0x6921506f]: () => import('./targets/esp32c3.js'), // ESP32C3 eco 1+2
|
||||||
@ -160,10 +162,10 @@ class ESPLoader {
|
|||||||
if (op == null || op_ret == op) {
|
if (op == null || op_ret == op) {
|
||||||
return [val, data];
|
return [val, data];
|
||||||
} else {
|
} else {
|
||||||
throw("invalid response");
|
throw new ESPError("invalid response");
|
||||||
}
|
}
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
if (e === "timeout") {
|
if (e instanceof TimeoutError) {
|
||||||
throw(e);
|
throw(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -209,7 +211,7 @@ class ESPLoader {
|
|||||||
return resp;
|
return resp;
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
console.log("Sync err " + e);
|
console.log("Sync err " + e);
|
||||||
throw(e);
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -240,7 +242,7 @@ class ESPLoader {
|
|||||||
//var str = new TextDecoder().decode(res);
|
//var str = new TextDecoder().decode(res);
|
||||||
//this.log(str);
|
//this.log(str);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e === "timeout") {
|
if (e instanceof TimeoutError) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -253,7 +255,7 @@ class ESPLoader {
|
|||||||
var resp = await this.sync();
|
var resp = await this.sync();
|
||||||
return "success";
|
return "success";
|
||||||
} catch(error) {
|
} catch(error) {
|
||||||
if (error === "timeout") {
|
if (error instanceof TimeoutError) {
|
||||||
if (esp32r0_delay) {
|
if (esp32r0_delay) {
|
||||||
this.write_char('_');
|
this.write_char('_');
|
||||||
} else {
|
} else {
|
||||||
@ -283,8 +285,7 @@ class ESPLoader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (resp !== "success") {
|
if (resp !== "success") {
|
||||||
this.log("Failed to connect with the device");
|
throw new ESPError("Failed to connect with the device");
|
||||||
return "error";
|
|
||||||
}
|
}
|
||||||
this.write_char('\n');
|
this.write_char('\n');
|
||||||
this.write_char('\r');
|
this.write_char('\r');
|
||||||
@ -297,7 +298,7 @@ class ESPLoader {
|
|||||||
if (chip_magic_value in MAGIC_TO_CHIP) {
|
if (chip_magic_value in MAGIC_TO_CHIP) {
|
||||||
this.chip = (await MAGIC_TO_CHIP[chip_magic_value]()).default;
|
this.chip = (await MAGIC_TO_CHIP[chip_magic_value]()).default;
|
||||||
} else {
|
} else {
|
||||||
console.log("Unknown chip magic")
|
throw new ESPError(`Unexpected CHIP magic value ${chip_magic_value}. Failed to autodetect chip type.`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -510,10 +511,10 @@ class ESPLoader {
|
|||||||
var SPI_CMD_USR = (1 << 18);
|
var SPI_CMD_USR = (1 << 18);
|
||||||
var SPI_USR2_COMMAND_LEN_SHIFT = 28;
|
var SPI_USR2_COMMAND_LEN_SHIFT = 28;
|
||||||
if(read_bits > 32) {
|
if(read_bits > 32) {
|
||||||
throw "Reading more than 32 bits back from a SPI flash operation is unsupported";
|
throw new ESPError("Reading more than 32 bits back from a SPI flash operation is unsupported");
|
||||||
}
|
}
|
||||||
if (data.length > 64) {
|
if (data.length > 64) {
|
||||||
throw "Writing more than 64 bytes of data with one SPI command is unsupported";
|
throw new ESPError("Writing more than 64 bytes of data with one SPI command is unsupported");
|
||||||
}
|
}
|
||||||
|
|
||||||
var data_bits = data.length * 8;
|
var data_bits = data.length * 8;
|
||||||
@ -553,7 +554,7 @@ class ESPLoader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (i === 10) {
|
if (i === 10) {
|
||||||
throw "SPI command did not complete in time";
|
throw new ESPError("SPI command did not complete in time");
|
||||||
}
|
}
|
||||||
var stat = await this.read_reg({addr:SPI_W0_REG});
|
var stat = await this.read_reg({addr:SPI_W0_REG});
|
||||||
await this.write_reg({addr:SPI_USR_REG, value:old_spi_usr});
|
await this.write_reg({addr:SPI_USR_REG, value:old_spi_usr});
|
||||||
@ -636,14 +637,12 @@ class ESPLoader {
|
|||||||
this.FLASH_WRITE_SIZE = 0x4000;
|
this.FLASH_WRITE_SIZE = 0x4000;
|
||||||
return this.chip;
|
return this.chip;
|
||||||
} else {
|
} else {
|
||||||
this.log("Failed to start stub. Unexpected response");
|
throw new ESPError("Failed to start stub. Unexpected response");
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
change_baud = async() => {
|
change_baud = async() => {
|
||||||
this.log("Changing baudrate to " + this.baudrate);
|
this.log("Changing baudrate to " + this.baudrate);
|
||||||
console.log("Changing baudrate to " + this.baudrate);
|
|
||||||
let second_arg = this.IS_STUB ? this.transport.baudrate : 0;
|
let second_arg = this.IS_STUB ? this.transport.baudrate : 0;
|
||||||
let pkt = this._appendArray(this._int_to_bytearray(this.baudrate), this._int_to_bytearray(second_arg));
|
let pkt = this._appendArray(this._int_to_bytearray(this.baudrate), this._int_to_bytearray(second_arg));
|
||||||
let resp = await this.command({op:this.ESP_CHANGE_BAUDRATE, data:pkt});
|
let resp = await this.command({op:this.ESP_CHANGE_BAUDRATE, data:pkt});
|
||||||
@ -658,11 +657,7 @@ class ESPLoader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
main_fn = async ({mode='default_reset'} = {}) => {
|
main_fn = async ({mode='default_reset'} = {}) => {
|
||||||
await this.detect_chip({mode:mode});
|
await this.detect_chip();
|
||||||
if (this.chip == null) {
|
|
||||||
this.log("Error in connecting to board");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var chip = await this.chip.get_chip_description(this);
|
var chip = await this.chip.get_chip_description(this);
|
||||||
this.log("Chip is " + chip);
|
this.log("Chip is " + chip);
|
||||||
@ -698,8 +693,7 @@ class ESPLoader {
|
|||||||
|
|
||||||
parse_flash_size_arg = function(flsz) {
|
parse_flash_size_arg = function(flsz) {
|
||||||
if (typeof this.chip.FLASH_SIZES[flsz] === 'undefined') {
|
if (typeof this.chip.FLASH_SIZES[flsz] === 'undefined') {
|
||||||
this.log("Flash size " + flsz + " is not supported by this chip type. Supported sizes: " + this.chip.FLASH_SIZES);
|
throw new ESPError("Flash size " + flsz + " is not supported by this chip type. Supported sizes: " + this.chip.FLASH_SIZES);
|
||||||
throw "Invalid flash size";
|
|
||||||
}
|
}
|
||||||
return this.chip.FLASH_SIZES[flsz];
|
return this.chip.FLASH_SIZES[flsz];
|
||||||
}
|
}
|
||||||
@ -758,8 +752,7 @@ class ESPLoader {
|
|||||||
let flash_end = this.flash_size_bytes(flash_size);
|
let flash_end = this.flash_size_bytes(flash_size);
|
||||||
for (var i = 0; i < fileArray.length; i++) {
|
for (var i = 0; i < fileArray.length; i++) {
|
||||||
if ((fileArray[i].data.length + fileArray[i].address) > flash_end) {
|
if ((fileArray[i].data.length + fileArray[i].address) > flash_end) {
|
||||||
this.log("Specified file doesn't fit in the available flash");
|
throw new ESPError(`File ${i + 1} doesn't fit in the available flash`);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -824,7 +817,7 @@ class ESPLoader {
|
|||||||
timeout = block_timeout;
|
timeout = block_timeout;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
this.log("Yet to handle Non Compressed writes");
|
throw new ESPError("Yet to handle Non Compressed writes");
|
||||||
}
|
}
|
||||||
bytes_sent += block.length;
|
bytes_sent += block.length;
|
||||||
image = image.slice(this.FLASH_WRITE_SIZE, image.length);
|
image = image.slice(this.FLASH_WRITE_SIZE, image.length);
|
||||||
@ -842,6 +835,7 @@ class ESPLoader {
|
|||||||
if (new String(res).valueOf() != new String(calcmd5).valueOf()) {
|
if (new String(res).valueOf() != new String(calcmd5).valueOf()) {
|
||||||
this.log("File md5: " + calcmd5);
|
this.log("File md5: " + calcmd5);
|
||||||
this.log("Flash md5: " + res);
|
this.log("Flash md5: " + res);
|
||||||
|
throw new ESPError("MD5 of file does not match data in flash!")
|
||||||
} else {
|
} else {
|
||||||
this.log("Hash of data verified.");
|
this.log("Hash of data verified.");
|
||||||
}
|
}
|
||||||
@ -869,4 +863,4 @@ class ESPLoader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export { ESPLoader };
|
export { ESPLoader };
|
||||||
|
3
error.js
Normal file
3
error.js
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
export class ESPError extends Error {}
|
||||||
|
|
||||||
|
export class TimeoutError extends ESPError {}
|
24
index.js
24
index.js
@ -19,6 +19,7 @@ const alertDiv = document.getElementById('alertDiv');
|
|||||||
//import { Transport } from './cp210x-webusb.js'
|
//import { Transport } from './cp210x-webusb.js'
|
||||||
import { Transport } from './webserial.js'
|
import { Transport } from './webserial.js'
|
||||||
import { ESPLoader } from './ESPLoader.js'
|
import { ESPLoader } from './ESPLoader.js'
|
||||||
|
import { ESPError } from './error.js'
|
||||||
|
|
||||||
let term = new Terminal({cols:120, rows:40});
|
let term = new Terminal({cols:120, rows:40});
|
||||||
term.open(terminal);
|
term.open(terminal);
|
||||||
@ -91,8 +92,11 @@ connectButton.onclick = async () => {
|
|||||||
|
|
||||||
chip = await esploader.main_fn();
|
chip = await esploader.main_fn();
|
||||||
|
|
||||||
await esploader.flash_id();
|
// Temporarily broken
|
||||||
|
// await esploader.flash_id();
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
|
console.error(e);
|
||||||
|
term.writeln(`Error: ${e.message}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("Settings done for :" + chip);
|
console.log("Settings done for :" + chip);
|
||||||
@ -121,8 +125,14 @@ resetButton.onclick = async () => {
|
|||||||
|
|
||||||
eraseButton.onclick = async () => {
|
eraseButton.onclick = async () => {
|
||||||
eraseButton.disabled = true;
|
eraseButton.disabled = true;
|
||||||
await esploader.erase_flash();
|
try{
|
||||||
eraseButton.disabled = false;
|
await esploader.erase_flash();
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
term.writeln(`Error: ${e.message}`);
|
||||||
|
} finally {
|
||||||
|
eraseButton.disabled = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
addFile.onclick = async () => {
|
addFile.onclick = async () => {
|
||||||
@ -283,6 +293,10 @@ programButton.onclick = async () => {
|
|||||||
|
|
||||||
fileArr.push({data:fileObj.data, address:offset});
|
fileArr.push({data:fileObj.data, address:offset});
|
||||||
}
|
}
|
||||||
esploader.write_flash({fileArray: fileArr, flash_size: 'keep'});
|
try {
|
||||||
|
esploader.write_flash({fileArray: fileArr, flash_size: 'keep'});
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
term.writeln(`Error: ${e.message}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
'use strict';
|
import {TimeoutError} from './error.js';
|
||||||
|
|
||||||
class Transport {
|
class Transport {
|
||||||
constructor(device) {
|
constructor(device) {
|
||||||
@ -131,7 +131,7 @@ class Transport {
|
|||||||
|
|
||||||
if (done) {
|
if (done) {
|
||||||
console.log("timed out");
|
console.log("timed out");
|
||||||
throw("timeout");
|
throw new TimeoutError("Timeout");
|
||||||
} else {
|
} else {
|
||||||
if (timeout > 0) {
|
if (timeout > 0) {
|
||||||
clearTimeout(t);
|
clearTimeout(t);
|
||||||
@ -164,7 +164,7 @@ class Transport {
|
|||||||
done = o.done;
|
done = o.done;
|
||||||
|
|
||||||
if (done) {
|
if (done) {
|
||||||
throw("timeout");
|
throw new TimeoutError("Timeout");
|
||||||
} else {
|
} else {
|
||||||
if (timeout > 0) {
|
if (timeout > 0) {
|
||||||
clearTimeout(t);
|
clearTimeout(t);
|
||||||
|
Loading…
Reference in New Issue
Block a user