From bc9f2a656d3bc86097a7deb0c307327e2e62284f Mon Sep 17 00:00:00 2001 From: Jakub Kocka Date: Tue, 30 Jan 2024 13:32:23 +0100 Subject: [PATCH] fix: ROM doesn't attach in-package flash chips --- esptool/__init__.py | 30 ++++++++++++++++++++++++------ esptool/targets/esp32.py | 17 +++++++++++++++++ 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/esptool/__init__.py b/esptool/__init__.py index 2227f5762..021ce08f5 100644 --- a/esptool/__init__.py +++ b/esptool/__init__.py @@ -779,6 +779,13 @@ def add_spi_flash_subparsers( "Keeping initial baud rate %d" % initial_baud ) + def _define_spi_conn(spi_connection): + """Prepare SPI configuration string and value for flash_spi_attach()""" + clk, q, d, hd, cs = spi_connection + spi_config_txt = f"CLK:{clk}, Q:{q}, D:{d}, HD:{hd}, CS:{cs}" + value = (hd << 24) | (cs << 18) | (d << 12) | (q << 6) | clk + return spi_config_txt, value + # Override the common SPI flash parameter stuff if configured to do so if hasattr(args, "spi_connection") and args.spi_connection is not None: spi_config = args.spi_connection @@ -790,15 +797,26 @@ def add_spi_flash_subparsers( esp.check_spi_connection(args.spi_connection) # Encode the pin numbers as a 32-bit integer with packed 6-bit values, # the same way the ESP ROM takes them - clk, q, d, hd, cs = args.spi_connection - spi_config = f"CLK:{clk}, Q:{q}, D:{d}, HD:{hd}, CS:{cs}" - value = (hd << 24) | (cs << 18) | (d << 12) | (q << 6) | clk + spi_config, value = _define_spi_conn(args.spi_connection) print(f"Configuring SPI flash mode ({spi_config})...") esp.flash_spi_attach(value) elif args.no_stub: - print("Enabling default SPI flash mode...") - # ROM loader doesn't enable flash unless we explicitly do it - esp.flash_spi_attach(0) + if esp.CHIP_NAME != "ESP32" or esp.secure_download_mode: + print("Enabling default SPI flash mode...") + # ROM loader doesn't enable flash unless we explicitly do it + esp.flash_spi_attach(0) + else: + # ROM doesn't attach in-package flash chips + spi_chip_pads = esp.get_chip_spi_pads() + spi_config_txt, value = _define_spi_conn(spi_chip_pads) + if spi_chip_pads != (0, 0, 0, 0, 0): + print( + "Attaching flash from eFuses' SPI pads configuration" + f"({spi_config_txt})..." + ) + else: + print("Enabling default SPI flash mode...") + esp.flash_spi_attach(value) # XMC chip startup sequence XMC_VENDOR_ID = 0x20 diff --git a/esptool/targets/esp32.py b/esptool/targets/esp32.py index 690fdbb91..d3ae70bb8 100644 --- a/esptool/targets/esp32.py +++ b/esptool/targets/esp32.py @@ -36,6 +36,9 @@ class ESP32ROM(ESPLoader): SPI_MISO_DLEN_OFFS = 0x2C EFUSE_RD_REG_BASE = 0x3FF5A000 + EFUSE_BLK0_RDATA3_REG_OFFS = EFUSE_RD_REG_BASE + 0x00C + EFUSE_BLK0_RDATA5_REG_OFFS = EFUSE_RD_REG_BASE + 0x014 + EFUSE_DIS_DOWNLOAD_MANUAL_ENCRYPT_REG = EFUSE_RD_REG_BASE + 0x18 EFUSE_DIS_DOWNLOAD_MANUAL_ENCRYPT = 1 << 7 # EFUSE_RD_DISABLE_DL_ENCRYPT @@ -276,6 +279,20 @@ def get_chip_features(self): return features + def get_chip_spi_pads(self): + """Read chip spi pad config + return: clk, q, d, hd, cd + """ + efuse_blk0_rdata5 = self.read_reg(self.EFUSE_BLK0_RDATA5_REG_OFFS) + spi_pad_clk = efuse_blk0_rdata5 & 0x1F + spi_pad_q = (efuse_blk0_rdata5 >> 5) & 0x1F + spi_pad_d = (efuse_blk0_rdata5 >> 10) & 0x1F + spi_pad_cs = (efuse_blk0_rdata5 >> 15) & 0x1F + + efuse_blk0_rdata3_reg = self.read_reg(self.EFUSE_BLK0_RDATA3_REG_OFFS) + spi_pad_hd = (efuse_blk0_rdata3_reg >> 4) & 0x1F + return spi_pad_clk, spi_pad_q, spi_pad_d, spi_pad_hd, spi_pad_cs + def read_efuse(self, n): """Read the nth word of the ESP3x EFUSE region.""" return self.read_reg(self.EFUSE_RD_REG_BASE + (4 * n))