fix detect chiptype
This commit is contained in:
parent
811ef6829b
commit
1a05445931
|
|
@ -7,41 +7,65 @@ Detects and lists ESP32 devices connected via USB
|
|||
import serial.tools.list_ports
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
import time
|
||||
import serial
|
||||
|
||||
def detect_chip_type(port):
|
||||
"""
|
||||
Try to detect the ESP32 chip type using esptool.py
|
||||
Returns chip type string or 'Unknown'
|
||||
"""
|
||||
try:
|
||||
result = subprocess.run(
|
||||
['esptool.py', '--port', port, 'chip_id'],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=10
|
||||
)
|
||||
# Try to enter ROM bootloader via DTR/RTS before probing
|
||||
try_enter_bootloader(port)
|
||||
|
||||
output = result.stdout + result.stderr
|
||||
cmd = ['esptool.py', '--port', port, '--chip', 'auto', '--before', 'default_reset',
|
||||
'--after', 'no_reset', 'chip_id']
|
||||
try :
|
||||
result = subprocess.run(cmd, capture_output=True, text=True, timeout=20)
|
||||
output = (result.stdout or '') + (result.stderr or '')
|
||||
|
||||
if 'ESP32-S3' in output:
|
||||
return 'ESP32-S3'
|
||||
elif 'ESP32-S2' in output:
|
||||
return 'ESP32-S2'
|
||||
elif 'ESP32-C3' in output:
|
||||
return 'ESP32-C3'
|
||||
elif 'ESP32-C6' in output:
|
||||
return 'ESP32-C6'
|
||||
elif 'ESP32-H2' in output:
|
||||
return 'ESP32-H2'
|
||||
elif 'ESP32' in output:
|
||||
return 'ESP32'
|
||||
if result.returncode != 0:
|
||||
last = output.strip().splitlines()[-1] if output.strip().splitlines() else 'esptool failed'
|
||||
print(f" esptool on {port} failed: {last}")
|
||||
return 'Unknown'
|
||||
|
||||
except Exception:
|
||||
pass
|
||||
# Prefer exact "Chip is ..." line (e.g., ESP32-D0WD-V3)
|
||||
for line in output.splitlines():
|
||||
if line.startswith('Chip is '):
|
||||
return line.replace('Chip is ', '').split(' (', 1)[0].strip()
|
||||
|
||||
# Fallback loose matches
|
||||
for name in ('ESP32-S3','ESP32-S2','ESP32-C6','ESP32-C3','ESP32-H2','ESP32'):
|
||||
if name in output:
|
||||
return name
|
||||
|
||||
except FileNotFoundError:
|
||||
print(" esptool.py not found (pip install esptool)")
|
||||
except subprocess.TimeoutExpired:
|
||||
print(f" esptool on {port} timed out (check EN/IO0 wiring)")
|
||||
except Exception as e:
|
||||
print(f" Error running esptool on {port}: {e}")
|
||||
|
||||
return 'Unknown'
|
||||
|
||||
def try_enter_bootloader(port):
|
||||
"""
|
||||
Toggle DTR/RTS to enter download mode.
|
||||
Assumes RTS->EN (active-low) and DTR->IO0 (active-low on many adapters).
|
||||
Safe no-op if control lines aren't available.
|
||||
"""
|
||||
try:
|
||||
with serial.Serial(port, 115200, timeout=0.1) as ser:
|
||||
ser.rts = True # EN low
|
||||
ser.dtr = True # IO0 low
|
||||
time.sleep(0.05)
|
||||
ser.rts = False # EN high
|
||||
time.sleep(0.05)
|
||||
ser.dtr = False # IO0 high
|
||||
time.sleep(0.05)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def guess_chip_type_from_usb(vid, pid):
|
||||
"""
|
||||
|
|
|
|||
Loading…
Reference in New Issue