@@ -9,8 +9,16 @@ import { IEspLoaderTerminal } from "./types/loaderTerminal.js";
99import { LoaderOptions } from "./types/loaderOptions.js" ;
1010import { FlashOptions } from "./types/flashOptions.js" ;
1111import { After , Before } from "./types/resetModes.js" ;
12+ import { ROM_LIST } from "./targets/index.js" ;
1213
13- type FlashReadCallback = ( ( packet : Uint8Array , progress : number , totalSize : number ) => void ) | null ;
14+ /**
15+ * Callback function type for handling packets received during flash memory read operations.
16+ * @callback FlashReadCallback
17+ * @param {Uint8Array } packet - The data packet received from the flash memory.
18+ * @param {number } progress - The current progress of the read operation in bytes.
19+ * @param {number } totalSize - The total size of the data to be read in bytes.
20+ */
21+ export type FlashReadCallback = ( ( packet : Uint8Array , progress : number , totalSize : number ) => void ) | null ;
1422
1523/**
1624 * Return the chip ROM based on the given magic number
@@ -96,6 +104,8 @@ export class ESPLoader {
96104 ESP_FLASH_DEFL_END = 0x12 ;
97105 ESP_SPI_FLASH_MD5 = 0x13 ;
98106
107+ ESP_GET_SECURITY_INFO = 0x14 ;
108+
99109 // Only Stub supported commands
100110 ESP_ERASE_FLASH = 0xd0 ;
101111 ESP_ERASE_REGION = 0xd1 ;
@@ -389,8 +399,9 @@ export class ESPLoader {
389399 if ( op == null || opRet == op ) {
390400 return [ val , data ] ;
391401 } else if ( data [ 0 ] != 0 && data [ 1 ] == this . ROM_INVALID_RECV_MSG ) {
392- await this . flushInput ( ) ;
393- throw new ESPError ( "unsupported command error" ) ;
402+ this . debug ( "read_packet unsupported command error " + op ) ;
403+ // await this.flushInput();
404+ throw new ESPError ( "unsupported command error " + op ) ;
394405 }
395406 }
396407 }
@@ -629,7 +640,7 @@ export class ESPLoader {
629640 this . debug ( "Connect attempt successful." ) ;
630641 this . info ( "\n\r" , false ) ;
631642
632- if ( detecting ) {
643+ if ( ! detecting ) {
633644 const chipMagicValue = ( await this . readReg ( this . CHIP_DETECT_MAGIC_REG_ADDR ) ) >>> 0 ;
634645 this . debug ( "Chip Magic " + chipMagicValue . toString ( 16 ) ) ;
635646 const chip = await magic2Chip ( chipMagicValue ) ;
@@ -641,17 +652,49 @@ export class ESPLoader {
641652 }
642653 }
643654
655+ /**
656+ * Get the CHIP ID with ESP_GET_SECURITY_INFO check command.
657+ * @returns {number } Chip ID number
658+ */
659+ async getChipId ( ) : Promise < number | undefined > {
660+ const response = await this . checkCommand ( "get security info" , this . ESP_GET_SECURITY_INFO , new Uint8Array ( 0 ) ) ;
661+
662+ this . debug ( "get_chip_id " + response . toString ( 16 ) ) ;
663+
664+ if ( response instanceof Uint8Array && response . length > 16 ) {
665+ const chipId = response [ 12 ] | ( response [ 13 ] << 8 ) | ( response [ 14 ] << 16 ) | ( response [ 15 ] << 24 ) ;
666+ return chipId ;
667+ }
668+ return ;
669+ }
670+
644671 /**
645672 * Connect and detect the existing chip.
646673 * @param {string } mode Reset mode to use for connection.
674+ * @param {number } attempts - Number of connection attempts
647675 */
648- async detectChip ( mode : Before = "default_reset" ) {
676+ async detectChip ( mode : Before = "default_reset" , attempts = 7 ) {
649677 await this . connect ( mode ) ;
650- this . info ( "Detecting chip type... " , false ) ;
678+ try {
679+ this . info ( "Detecting chip type... " ) ;
680+ const chipID = await this . getChipId ( ) ;
681+ const filteredROMList = ROM_LIST . filter (
682+ ( n ) => n . CHIP_NAME !== "ESP8266" && n . CHIP_NAME !== "ESP32" && n . CHIP_NAME !== "ESP32-S2" ,
683+ ) ;
684+ for ( const cls of filteredROMList ) {
685+ if ( chipID === cls . IMAGE_CHIP_ID ) {
686+ this . chip = cls ;
687+ break ;
688+ }
689+ }
690+ } catch ( error ) {
691+ await this . transport . disconnect ( ) ;
692+ await this . connect ( mode , attempts , false ) ;
693+ }
651694 if ( this . chip != null ) {
652695 this . info ( this . chip . CHIP_NAME ) ;
653696 } else {
654- this . info ( "unknown! " ) ;
697+ this . info ( "unknown chip! detectchip has failed. " ) ;
655698 }
656699 }
657700
@@ -1091,6 +1134,16 @@ export class ESPLoader {
10911134 return strmd5 ;
10921135 }
10931136
1137+ /**
1138+ * Read data from flash memory of the chip.
1139+ * This function reads a specified amount of data from the flash memory starting at a given address.
1140+ * It sends a read command to the chip and processes the response packets until the requested size is read.
1141+ * @param {number } addr - The starting address in flash memory to read from.
1142+ * @param {number } size - The number of bytes to read from flash memory.
1143+ * @param {FlashReadCallback } onPacketReceived - Optional callback function to handle each received packet.
1144+ * @returns {Promise<Uint8Array> } A promise that resolves to the data read from flash memory as a Uint8Array.
1145+ * @throws {ESPError } If the read operation fails or an unexpected response is received.
1146+ */
10941147 async readFlash ( addr : number , size : number , onPacketReceived : FlashReadCallback = null ) {
10951148 let pkt = this . _appendArray ( this . _intToByteArray ( addr ) , this . _intToByteArray ( size ) ) ;
10961149 pkt = this . _appendArray ( pkt , this . _intToByteArray ( 0x1000 ) ) ;
0 commit comments