50
50
sanitize_string ,
51
51
)
52
52
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
+
53
67
DETECTED_FLASH_SIZES = {
54
68
0x12 : "256KB" ,
55
69
0x13 : "512KB" ,
@@ -313,6 +327,34 @@ def dump_mem(
313
327
return data .getvalue ()
314
328
315
329
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
+
316
358
def detect_flash_size (esp : ESPLoader ) -> str | None :
317
359
"""
318
360
Detect the flash size of the connected ESP device.
@@ -328,9 +370,7 @@ def detect_flash_size(esp: ESPLoader) -> str | None:
328
370
"Detecting flash size is not supported in secure download mode. "
329
371
"Need to manually specify flash size."
330
372
)
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 )
334
374
return flash_size
335
375
336
376
@@ -1018,21 +1058,15 @@ def _define_spi_conn(spi_connection):
1018
1058
log .print ("Enabling default SPI flash mode..." )
1019
1059
esp .flash_spi_attach (value )
1020
1060
1021
- # XMC chip startup sequence
1022
- XMC_VENDOR_ID = 0x20
1023
-
1024
1061
def is_xmc_chip_strict ():
1025
1062
# 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 )
1033
1064
if vendor_id != XMC_VENDOR_ID :
1034
1065
return False
1035
1066
1067
+ mfid = (device_id >> 8 ) & 0xFF
1068
+ cpid = device_id & 0xFF
1069
+
1036
1070
matched = False
1037
1071
if mfid == 0x40 :
1038
1072
if cpid >= 0x13 and cpid <= 0x20 :
@@ -1245,13 +1279,10 @@ def print_flash_id(esp: ESPLoader) -> None:
1245
1279
Args:
1246
1280
esp: Initiated esp object connected to a real device.
1247
1281
"""
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' } " )
1255
1286
1256
1287
1257
1288
def flash_id (esp : ESPLoader ) -> None :
0 commit comments