mirror of
https://github.com/espressif/esptool.git
synced 2025-10-20 22:09:59 +08:00
refactor: Optimize unnecessary chip interrogations
This commit is contained in:
@@ -24,7 +24,7 @@ from .loader import (
|
|||||||
ESPLoader,
|
ESPLoader,
|
||||||
timeout_per_mb,
|
timeout_per_mb,
|
||||||
)
|
)
|
||||||
from .targets import CHIP_DEFS, CHIP_LIST, ESP8266ROM, ROM_LIST
|
from .targets import CHIP_DEFS, CHIP_LIST, ROM_LIST
|
||||||
from .util import (
|
from .util import (
|
||||||
FatalError,
|
FatalError,
|
||||||
NotImplementedInROMError,
|
NotImplementedInROMError,
|
||||||
@@ -93,14 +93,10 @@ def detect_chip(
|
|||||||
detect_port.connect(connect_mode, connect_attempts, detecting=True)
|
detect_port.connect(connect_mode, connect_attempts, detecting=True)
|
||||||
try:
|
try:
|
||||||
print("Detecting chip type...", end="")
|
print("Detecting chip type...", end="")
|
||||||
res = detect_port.check_command(
|
chip_id = detect_port.get_chip_id()
|
||||||
"get security info", ESPLoader.ESP_GET_SECURITY_INFO, b""
|
for cls in [
|
||||||
)
|
n for n in ROM_LIST if n.CHIP_NAME not in ("ESP8266", "ESP32", "ESP32S2")
|
||||||
# 4b flags, 1b flash_crypt_cnt, 7*1b key_purposes, 4b chip_id
|
]:
|
||||||
res = struct.unpack("<IBBBBBBBBI", res[:16])
|
|
||||||
chip_id = res[9] # 2/4 status bytes invariant
|
|
||||||
|
|
||||||
for cls in ROM_LIST[3:]:
|
|
||||||
# cmd not supported on ESP8266 and ESP32 + ESP32-S2 doesn't return chip_id
|
# cmd not supported on ESP8266 and ESP32 + ESP32-S2 doesn't return chip_id
|
||||||
if chip_id == cls.IMAGE_CHIP_ID:
|
if chip_id == cls.IMAGE_CHIP_ID:
|
||||||
inst = cls(detect_port._port, baud, trace_enabled=trace_enabled)
|
inst = cls(detect_port._port, baud, trace_enabled=trace_enabled)
|
||||||
@@ -831,7 +827,7 @@ def image_info(args):
|
|||||||
raise FatalError("Append digest field not 0 or 1")
|
raise FatalError("Append digest field not 0 or 1")
|
||||||
|
|
||||||
chip_id = int.from_bytes(extended_header[4:5], "little")
|
chip_id = int.from_bytes(extended_header[4:5], "little")
|
||||||
for rom in [n for n in ROM_LIST if n != ESP8266ROM]:
|
for rom in [n for n in ROM_LIST if n.CHIP_NAME != "ESP8266"]:
|
||||||
if chip_id == rom.IMAGE_CHIP_ID:
|
if chip_id == rom.IMAGE_CHIP_ID:
|
||||||
args.chip = rom.CHIP_NAME
|
args.chip = rom.CHIP_NAME
|
||||||
break
|
break
|
||||||
|
@@ -871,10 +871,12 @@ class ESPLoader(object):
|
|||||||
self.flash_begin(0, 0)
|
self.flash_begin(0, 0)
|
||||||
self.flash_finish(reboot)
|
self.flash_finish(reboot)
|
||||||
|
|
||||||
def flash_id(self):
|
def flash_id(self, _cache=[]):
|
||||||
"""Read SPI flash manufacturer and device id"""
|
"""Read SPI flash manufacturer and device id"""
|
||||||
|
if not _cache:
|
||||||
SPIFLASH_RDID = 0x9F
|
SPIFLASH_RDID = 0x9F
|
||||||
return self.run_spiflash_command(SPIFLASH_RDID, b"", 24)
|
_cache.append(self.run_spiflash_command(SPIFLASH_RDID, b"", 24))
|
||||||
|
return _cache[0]
|
||||||
|
|
||||||
def flash_type(self):
|
def flash_type(self):
|
||||||
"""Read flash type bit field from eFuse. Returns 0, 1, None (not present)"""
|
"""Read flash type bit field from eFuse. Returns 0, 1, None (not present)"""
|
||||||
@@ -893,13 +895,17 @@ class ESPLoader(object):
|
|||||||
}
|
}
|
||||||
|
|
||||||
@esp32s3_or_newer_function_only
|
@esp32s3_or_newer_function_only
|
||||||
def get_chip_id(self):
|
def get_chip_id(self, _cache=[]):
|
||||||
res = self.check_command("get security info", self.ESP_GET_SECURITY_INFO, b"")
|
if not _cache:
|
||||||
|
res = self.check_command(
|
||||||
|
"get security info", self.ESP_GET_SECURITY_INFO, b""
|
||||||
|
)
|
||||||
res = struct.unpack(
|
res = struct.unpack(
|
||||||
"<IBBBBBBBBI", res[:16]
|
"<IBBBBBBBBI", res[:16]
|
||||||
) # 4b flags, 1b flash_crypt_cnt, 7*1b key_purposes, 4b chip_id
|
) # 4b flags, 1b flash_crypt_cnt, 7*1b key_purposes, 4b chip_id
|
||||||
chip_id = res[9] # 2/4 status bytes invariant
|
chip_id = res[9] # 2/4 status bytes invariant
|
||||||
return chip_id
|
_cache.append(chip_id)
|
||||||
|
return _cache[0]
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def parse_flash_size_arg(cls, arg):
|
def parse_flash_size_arg(cls, arg):
|
||||||
|
@@ -153,13 +153,10 @@ class ESP32ROM(ESPLoader):
|
|||||||
return 0xF
|
return 0xF
|
||||||
|
|
||||||
def get_encrypted_download_disabled(self):
|
def get_encrypted_download_disabled(self):
|
||||||
if (
|
return (
|
||||||
self.read_reg(self.EFUSE_DIS_DOWNLOAD_MANUAL_ENCRYPT_REG)
|
self.read_reg(self.EFUSE_DIS_DOWNLOAD_MANUAL_ENCRYPT_REG)
|
||||||
& self.EFUSE_DIS_DOWNLOAD_MANUAL_ENCRYPT
|
& self.EFUSE_DIS_DOWNLOAD_MANUAL_ENCRYPT
|
||||||
):
|
)
|
||||||
return True
|
|
||||||
else:
|
|
||||||
return False
|
|
||||||
|
|
||||||
def get_flash_encryption_enabled(self):
|
def get_flash_encryption_enabled(self):
|
||||||
flash_crypt_cnt = (
|
flash_crypt_cnt = (
|
||||||
|
Reference in New Issue
Block a user