fix: allow connecting to KL520 in USB Boot mode (is_connectable=False)

KL520 in USB Boot mode reports is_connectable=False, which is normal -
firmware must be loaded first. The previous code rejected the connection
attempt early. Now we use connect_devices_without_check() for devices
that are not yet connectable, then proceed with firmware loading.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
jim800121chen 2026-03-09 21:15:53 +08:00
parent f2596511e0
commit b07e47c517

View File

@ -611,8 +611,11 @@ def handle_connect(params):
if target_dev is None:
target_dev = descs.device_descriptor_list[0]
if not target_dev.is_connectable:
return {"error": "device is not connectable"}
# Note: KL520 in USB Boot mode has is_connectable=False, which is
# normal — it becomes connectable after firmware is loaded. KL720 KDP
# legacy (pid=0x0200) is also not connectable until firmware load.
# So we do NOT reject is_connectable=False here; instead we attempt
# connection and firmware load as appropriate.
# Determine chip type from device_type param or product_id
pid = target_dev.product_id
@ -689,12 +692,14 @@ def handle_connect(params):
}
# ── Normal connection (KL520 or KL720 KDP2) ──
# KL720 KDP2: connect_devices() often fails with Error 28. Using it
# before connect_devices_without_check() corrupts SDK internal state
# and causes SIGSEGV. Go directly to connect_devices_without_check()
# for KL720 to avoid the crash.
if _device_chip == "KL720":
_log(f"KL720: Using connect_devices_without_check(usb_port_id={target_dev.usb_port_id})...")
# Use connect_devices_without_check when:
# - KL720 KDP2: connect_devices() often fails with Error 28
# - KL520 USB Boot: is_connectable=False, connect_devices() rejects it
# In these cases, connect_devices_without_check() works and we can
# still load firmware afterwards.
use_without_check = (_device_chip == "KL720") or (not target_dev.is_connectable)
if use_without_check:
_log(f"{_device_chip}: Using connect_devices_without_check(usb_port_id={target_dev.usb_port_id}, connectable={target_dev.is_connectable})...")
_device_group = kp.core.connect_devices_without_check(
usb_port_ids=[target_dev.usb_port_id]
)