Skip to content

Commit 494c635

Browse files
committed
add detect chip with get security info
1 parent ed69ccb commit 494c635

File tree

5 files changed

+88
-23
lines changed

5 files changed

+88
-23
lines changed

src/esploader.ts

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ import { FlashOptions } from "./types/flashOptions.js";
1111
import { After, Before } from "./types/resetModes.js";
1212
import { FlashFreqValues, FlashModeValues, FlashSizeValues } from "./types/arguments.js";
1313
import { loadFirmwareImage } from "./image/index.js";
14+
import { ROM_LIST } from "./targets/index.js";
1415

1516
/**
16-
* Flash read callback function type
17-
* @param {Uint8Array} packet - Packet data
18-
* @param {number} progress - Progress number
19-
* @param {number} totalSize - Total size number
17+
* Callback function type for handling packets received during flash memory read operations.
18+
* @callback FlashReadCallback
19+
* @param {Uint8Array} packet - The data packet received from the flash memory.
20+
* @param {number} progress - The current progress of the read operation in bytes.
21+
* @param {number} totalSize - The total size of the data to be read in bytes.
2022
*/
2123
export type FlashReadCallback = ((packet: Uint8Array, progress: number, totalSize: number) => void) | null;
2224

@@ -106,6 +108,8 @@ export class ESPLoader {
106108
ESP_FLASH_DEFL_END = 0x12;
107109
ESP_SPI_FLASH_MD5 = 0x13;
108110

111+
ESP_GET_SECURITY_INFO = 0x14;
112+
109113
// Only Stub supported commands
110114
ESP_ERASE_FLASH = 0xd0;
111115
ESP_ERASE_REGION = 0xd1;
@@ -409,8 +413,9 @@ export class ESPLoader {
409413
if (op == null || opRet == op) {
410414
return [val, data];
411415
} else if (data[0] != 0 && data[1] == this.ROM_INVALID_RECV_MSG) {
412-
await this.flushInput();
413-
throw new ESPError("unsupported command error");
416+
this.debug("read_packet unsupported command error " + op);
417+
// await this.flushInput();
418+
throw new ESPError("unsupported command error " + op);
414419
}
415420
}
416421
}
@@ -649,7 +654,7 @@ export class ESPLoader {
649654
this.debug("Connect attempt successful.");
650655
this.info("\n\r", false);
651656

652-
if (detecting) {
657+
if (!detecting) {
653658
const chipMagicValue = (await this.readReg(this.CHIP_DETECT_MAGIC_REG_ADDR)) >>> 0;
654659
this.debug("Chip Magic " + chipMagicValue.toString(16));
655660
const chip = await magic2Chip(chipMagicValue);
@@ -661,17 +666,49 @@ export class ESPLoader {
661666
}
662667
}
663668

669+
/**
670+
* Get the CHIP ID with ESP_GET_SECURITY_INFO check command.
671+
* @returns {number} Chip ID number
672+
*/
673+
async getChipId(): Promise<number | undefined> {
674+
const response = await this.checkCommand("get security info", this.ESP_GET_SECURITY_INFO, new Uint8Array(0));
675+
676+
this.debug("get_chip_id " + response.toString(16));
677+
678+
if (response instanceof Uint8Array && response.length > 16) {
679+
const chipId = response[12] | (response[13] << 8) | (response[14] << 16) | (response[15] << 24);
680+
return chipId;
681+
}
682+
return;
683+
}
684+
664685
/**
665686
* Connect and detect the existing chip.
666687
* @param {string} mode Reset mode to use for connection.
688+
* @param {number} attempts - Number of connection attempts
667689
*/
668-
async detectChip(mode: Before = "default_reset") {
690+
async detectChip(mode: Before = "default_reset", attempts = 7) {
669691
await this.connect(mode);
670-
this.info("Detecting chip type... ", false);
692+
try {
693+
this.info("Detecting chip type... ");
694+
const chipID = await this.getChipId();
695+
const filteredROMList = ROM_LIST.filter(
696+
(n) => n.CHIP_NAME !== "ESP8266" && n.CHIP_NAME !== "ESP32" && n.CHIP_NAME !== "ESP32-S2",
697+
);
698+
for (const cls of filteredROMList) {
699+
if (chipID === cls.IMAGE_CHIP_ID) {
700+
this.chip = cls;
701+
break;
702+
}
703+
}
704+
} catch (error) {
705+
await this.transport.disconnect();
706+
await this.connect(mode, attempts, false);
707+
}
671708
if (this.chip != null) {
672709
this.info(this.chip.CHIP_NAME);
673710
} else {
674-
this.info("unknown!");
711+
this.info("unknown chip! detectchip has failed.");
675712
}
676713
}
677714

@@ -1156,11 +1193,14 @@ export class ESPLoader {
11561193
}
11571194

11581195
/**
1159-
* Read flash memory from the chip.
1160-
* @param {number} addr Address number
1161-
* @param {number} size Package size
1162-
* @param {FlashReadCallback} onPacketReceived Callback function to call when packet is received
1163-
* @returns {Uint8Array} Flash read data
1196+
* Read data from flash memory of the chip.
1197+
* This function reads a specified amount of data from the flash memory starting at a given address.
1198+
* It sends a read command to the chip and processes the response packets until the requested size is read.
1199+
* @param {number} addr - The starting address in flash memory to read from.
1200+
* @param {number} size - The number of bytes to read from flash memory.
1201+
* @param {FlashReadCallback} onPacketReceived - Optional callback function to handle each received packet.
1202+
* @returns {Promise<Uint8Array>} A promise that resolves to the data read from flash memory as a Uint8Array.
1203+
* @throws {ESPError} If the read operation fails or an unexpected response is received.
11641204
*/
11651205
async readFlash(addr: number, size: number, onPacketReceived: FlashReadCallback = null) {
11661206
let pkt = this._appendArray(this._intToByteArray(addr), this._intToByteArray(size));

src/targets/esp8266.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export class ESP8266ROM extends ROM {
5050
public SPI_USR2_OFFS = 0x24;
5151
public SPI_MOSI_DLEN_OFFS = 0; // not in esp8266
5252
public SPI_MISO_DLEN_OFFS = 0; // not in esp8266
53+
public IMAGE_CHIP_ID = 9999; // not in esp8266
5354
public SPI_W0_OFFS = 0x40;
5455

5556
public async readEfuse(loader: ESPLoader, offset: number): Promise<number> {

src/targets/index.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { ESP32ROM } from "./esp32";
2+
import { ESP32C2ROM } from "./esp32c2";
3+
import { ESP32C3ROM } from "./esp32c3";
4+
import { ESP32C5ROM } from "./esp32c5";
5+
import { ESP32C6ROM } from "./esp32c6";
6+
import { ESP32C61ROM } from "./esp32c61";
7+
import { ESP32H2ROM } from "./esp32h2";
8+
import { ESP32P4ROM } from "./esp32p4";
9+
import { ESP32S2ROM } from "./esp32s2";
10+
import { ESP32S3ROM } from "./esp32s3";
11+
import { ESP8266ROM } from "./esp8266";
12+
13+
export const CHIP_DEFS = {
14+
esp8266: new ESP8266ROM(),
15+
esp32: new ESP32ROM(),
16+
esp32s2: new ESP32S2ROM(),
17+
esp32s3: new ESP32S3ROM(),
18+
esp32c3: new ESP32C3ROM(),
19+
esp32c2: new ESP32C2ROM(),
20+
esp32c6: new ESP32C6ROM(),
21+
esp32c61: new ESP32C61ROM(),
22+
esp32c5: new ESP32C5ROM(),
23+
esp32h2: new ESP32H2ROM(),
24+
esp32p4: new ESP32P4ROM(),
25+
};
26+
27+
export const CHIP_LIST = Object.keys(CHIP_DEFS) as Array<keyof typeof CHIP_DEFS>;
28+
export const ROM_LIST = Object.values(CHIP_DEFS);

src/targets/rom.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export abstract class ROM {
101101
// abstract EFUSE_RD_REG_BASE: number; //esp32
102102

103103
abstract FLASH_WRITE_SIZE: number;
104-
// abstract IMAGE_CHIP_ID: number; // not in esp8266
104+
abstract IMAGE_CHIP_ID: number; // not in esp8266
105105
abstract SPI_MOSI_DLEN_OFFS: number; // not in esp8266
106106
abstract SPI_MISO_DLEN_OFFS: number; // not in esp8266
107107
abstract SPI_REG_BASE: number;

src/webserial.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -244,14 +244,10 @@ class Transport {
244244
}
245245

246246
async flushInput() {
247-
try {
248-
if (!this.reader) {
249-
this.reader = this.device.readable?.getReader();
250-
}
251-
await this.reader?.cancel();
247+
if (this.reader) {
248+
await this.reader.cancel();
249+
this.reader.releaseLock();
252250
this.reader = this.device.readable?.getReader();
253-
} catch (error) {
254-
this.trace(`Error while flushing input: ${error}`);
255251
}
256252
this.buffer = new Uint8Array(0);
257253
}

0 commit comments

Comments
 (0)