@@ -11,12 +11,14 @@ import { FlashOptions } from "./types/flashOptions.js";
1111import { After , Before } from "./types/resetModes.js" ;
1212import { FlashFreqValues , FlashModeValues , FlashSizeValues } from "./types/arguments.js" ;
1313import { 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 */
2123export 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 ) ) ;
0 commit comments