5050 sanitize_string ,
5151)
5252
53+
54+ # Vendors with different detection logic
55+ ADESTO_VENDOR_ID = 0x1F
56+ XMC_VENDOR_ID = 0x20
57+
58+ DETECTED_FLASH_SIZES_ADESTO = {
59+ 0x04 : "512KB" ,
60+ 0x05 : "1MB" ,
61+ 0x06 : "2MB" ,
62+ 0x07 : "4MB" ,
63+ 0x08 : "8MB" ,
64+ 0x09 : "16MB" ,
65+ }
66+
5367DETECTED_FLASH_SIZES = {
5468 0x12 : "256KB" ,
5569 0x13 : "512KB" ,
@@ -313,6 +327,34 @@ def dump_mem(
313327 return data .getvalue ()
314328
315329
330+ def _get_flash_info (esp : ESPLoader , cache : bool = True ) -> tuple [int , int , str | None ]:
331+ """
332+ Get the flash memory chip information including vendor ID, device ID, and
333+ flash size.
334+
335+ Args:
336+ esp: Initiated esp object connected to a real device.
337+ cache: Whether to use cached flash ID (default: True).
338+
339+ Returns:
340+ Tuple containing (vendor_id, device_id, flash_size)
341+ """
342+ flash_id = esp .flash_id (cache = cache )
343+ vendor_id = flash_id & 0xFF
344+ # Swap the bytes of the device ID by taking the high byte first, then the low byte
345+ device_id = ((flash_id >> 16 ) & 0xFF ) | ((flash_id >> 8 ) & 0xFF ) << 8
346+
347+ if vendor_id == ADESTO_VENDOR_ID :
348+ # Lower 5 bits of second byte of flash_id is size_id
349+ size_id = (flash_id >> 8 ) & 0x1F
350+ flash_size = DETECTED_FLASH_SIZES_ADESTO .get (size_id )
351+ else :
352+ size_id = flash_id >> 16
353+ flash_size = DETECTED_FLASH_SIZES .get (size_id )
354+
355+ return vendor_id , device_id , flash_size
356+
357+
316358def detect_flash_size (esp : ESPLoader ) -> str | None :
317359 """
318360 Detect the flash size of the connected ESP device.
@@ -328,9 +370,7 @@ def detect_flash_size(esp: ESPLoader) -> str | None:
328370 "Detecting flash size is not supported in secure download mode. "
329371 "Need to manually specify flash size."
330372 )
331- flash_id = esp .flash_id ()
332- size_id = flash_id >> 16
333- flash_size = DETECTED_FLASH_SIZES .get (size_id )
373+ _ , _ , flash_size = _get_flash_info (esp )
334374 return flash_size
335375
336376
@@ -1018,21 +1058,15 @@ def _define_spi_conn(spi_connection):
10181058 log .print ("Enabling default SPI flash mode..." )
10191059 esp .flash_spi_attach (value )
10201060
1021- # XMC chip startup sequence
1022- XMC_VENDOR_ID = 0x20
1023-
10241061 def is_xmc_chip_strict ():
10251062 # Read ID without cache, because it should be different after the XMC startup
1026- id = esp .flash_id (cache = False )
1027- rdid = ((id & 0xFF ) << 16 ) | ((id >> 16 ) & 0xFF ) | (id & 0xFF00 )
1028-
1029- vendor_id = (rdid >> 16 ) & 0xFF
1030- mfid = (rdid >> 8 ) & 0xFF
1031- cpid = rdid & 0xFF
1032-
1063+ vendor_id , device_id , _ = _get_flash_info (esp , False )
10331064 if vendor_id != XMC_VENDOR_ID :
10341065 return False
10351066
1067+ mfid = (device_id >> 8 ) & 0xFF
1068+ cpid = device_id & 0xFF
1069+
10361070 matched = False
10371071 if mfid == 0x40 :
10381072 if cpid >= 0x13 and cpid <= 0x20 :
@@ -1245,13 +1279,10 @@ def print_flash_id(esp: ESPLoader) -> None:
12451279 Args:
12461280 esp: Initiated esp object connected to a real device.
12471281 """
1248- flash_id = esp .flash_id ()
1249- log .print (f"Manufacturer: { flash_id & 0xFF :02x} " )
1250- flid_lowbyte = (flash_id >> 16 ) & 0xFF
1251- log .print (f"Device: { (flash_id >> 8 ) & 0xFF :02x} { flid_lowbyte :02x} " )
1252- log .print (
1253- f"Detected flash size: { DETECTED_FLASH_SIZES .get (flid_lowbyte , 'Unknown' )} "
1254- )
1282+ manufacturer_id , device_id , flash_size = _get_flash_info (esp )
1283+ log .print (f"Manufacturer: { manufacturer_id :02x} " )
1284+ log .print (f"Device: { device_id :04x} " )
1285+ log .print (f"Detected flash size: { flash_size or 'Unknown' } " )
12551286
12561287
12571288def flash_id (esp : ESPLoader ) -> None :
0 commit comments