Skip to content

Commit aee68fa

Browse files
committed
fix(write_flash): Make write flash mem independent
* Remove reading of magic register to avoid issues with different memory layouts.
1 parent 529ff4e commit aee68fa

File tree

2 files changed

+16
-22
lines changed

2 files changed

+16
-22
lines changed

esptool/cmds.py

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -708,10 +708,18 @@ def write_flash(esp, args):
708708
raise # Reconnect limit reached
709709

710710
if esp.IS_STUB:
711+
# Get the "encrypted" flag for the last file flashed
712+
# Note: all_files list contains quadruplets like:
713+
# (address: int, filename: str | None, data: bytes, encrypted: bool)
714+
last_file_encrypted = all_files[-1][2]
715+
711716
# Stub only writes each block to flash after 'ack'ing the receive,
712-
# so do a final dummy operation which will not be 'ack'ed
717+
# so do a final operation which will not be 'ack'ed
713718
# until the last block has actually been written out to flash
714-
esp.read_reg(ESPLoader.CHIP_DETECT_MAGIC_REG_ADDR, timeout=timeout)
719+
if args.compress and not last_file_encrypted:
720+
esp.flash_defl_finish(reboot=False, timeout=timeout)
721+
else:
722+
esp.flash_finish(reboot=False, timeout=timeout)
715723

716724
t = time.time() - t
717725
speed_msg = ""
@@ -750,22 +758,6 @@ def write_flash(esp, args):
750758

751759
print("\nLeaving...")
752760

753-
if esp.IS_STUB:
754-
# skip sending flash_finish to ROM loader here,
755-
# as it causes the loader to exit and run user code
756-
esp.flash_begin(0, 0)
757-
758-
# Get the "encrypted" flag for the last file flashed
759-
# Note: all_files list contains triplets like:
760-
# (address: Integer, filename: String, encrypted: Boolean)
761-
last_file_encrypted = all_files[-1][2]
762-
763-
# Check whether the last file flashed was compressed or not
764-
if args.compress and not last_file_encrypted:
765-
esp.flash_defl_finish(False)
766-
else:
767-
esp.flash_finish(False)
768-
769761

770762
def _parse_app_info(app_info_segment):
771763
"""

esptool/loader.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,11 +1040,11 @@ def flash_encrypt_block(self, data, seq, timeout=DEFAULT_TIMEOUT):
10401040
else:
10411041
raise
10421042

1043-
def flash_finish(self, reboot=False):
1043+
def flash_finish(self, reboot=False, timeout=DEFAULT_TIMEOUT):
10441044
"""Leave flash mode and run/reboot"""
10451045
pkt = struct.pack("<I", int(not reboot))
10461046
# stub sends a reply to this command
1047-
self.check_command("leave Flash mode", self.ESP_FLASH_END, pkt)
1047+
self.check_command("leave Flash mode", self.ESP_FLASH_END, pkt, timeout=timeout)
10481048

10491049
def run(self, reboot=False):
10501050
"""Run application code in flash"""
@@ -1329,14 +1329,16 @@ def flash_defl_block(self, data, seq, timeout=DEFAULT_TIMEOUT):
13291329
raise
13301330

13311331
@stub_and_esp32_function_only
1332-
def flash_defl_finish(self, reboot=False):
1332+
def flash_defl_finish(self, reboot=False, timeout=DEFAULT_TIMEOUT):
13331333
"""Leave compressed flash mode and run/reboot"""
13341334
if not reboot and not self.IS_STUB:
13351335
# skip sending flash_finish to ROM loader, as this
13361336
# exits the bootloader. Stub doesn't do this.
13371337
return
13381338
pkt = struct.pack("<I", int(not reboot))
1339-
self.check_command("leave compressed flash mode", self.ESP_FLASH_DEFL_END, pkt)
1339+
self.check_command(
1340+
"leave compressed flash mode", self.ESP_FLASH_DEFL_END, pkt, timeout=timeout
1341+
)
13401342
self.in_bootloader = False
13411343

13421344
@stub_and_esp32_function_only

0 commit comments

Comments
 (0)