127 lines
3.6 KiB
Python
127 lines
3.6 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Probe: RTT log + BLE uartw write to see if MCU gets F0 60."""
|
|
import asyncio
|
|
import threading
|
|
import time
|
|
import pylink
|
|
from bleak import BleakScanner, BleakClient
|
|
|
|
UARTW = "e49a25e0-f69a-11e8-8eb2-f2801f1b9fd1"
|
|
UARTN = "e49a28e1-f69a-11e8-8eb2-f2801f1b9fd1"
|
|
MIDI = "7772e5db-3868-4112-a1a9-f2669d106bf3"
|
|
|
|
|
|
def open_jlink():
|
|
j = pylink.JLink()
|
|
j.open()
|
|
try:
|
|
j.exec_command("HideDeviceSelection = 1")
|
|
except Exception:
|
|
pass
|
|
j.set_tif(pylink.enums.JLinkInterfaces.SWD)
|
|
last = None
|
|
for dev in ("Cortex-M4", "AT32F403AC", "AT32F403A"):
|
|
try:
|
|
try:
|
|
j.exec_command(f"Device = {dev}")
|
|
except Exception:
|
|
pass
|
|
j.connect(dev)
|
|
print("JLink connected", dev, flush=True)
|
|
try:
|
|
j.restart(halt=False)
|
|
except Exception:
|
|
try:
|
|
j.go()
|
|
except Exception:
|
|
pass
|
|
return j
|
|
except Exception as e:
|
|
last = e
|
|
raise SystemExit(f"JLink connect failed: {last}")
|
|
|
|
|
|
async def find_dev(timeout=60):
|
|
t0 = time.time()
|
|
while time.time() - t0 < timeout:
|
|
d = await BleakScanner.find_device_by_filter(
|
|
lambda d, a: d.name and "Smart Guitar" in d.name, timeout=8
|
|
)
|
|
if d:
|
|
return d
|
|
print("waiting BLE...", flush=True)
|
|
return None
|
|
|
|
|
|
async def main():
|
|
j = open_jlink()
|
|
j.rtt_start()
|
|
stop = False
|
|
lines = []
|
|
|
|
def reader():
|
|
while not stop:
|
|
try:
|
|
data = j.rtt_read(0, 4096)
|
|
if data:
|
|
s = bytes(data).decode("utf-8", "replace")
|
|
lines.append(s)
|
|
print("RTT>", s, end="" if s.endswith("\n") else "\n", flush=True)
|
|
except Exception as e:
|
|
print("rtt err", e, flush=True)
|
|
break
|
|
time.sleep(0.03)
|
|
|
|
th = threading.Thread(target=reader, daemon=True)
|
|
th.start()
|
|
print("collect boot logs 3s...", flush=True)
|
|
await asyncio.sleep(3)
|
|
|
|
d = await find_dev(90)
|
|
if not d:
|
|
print("NO BLE DEVICE", flush=True)
|
|
stop = True
|
|
j.close()
|
|
return
|
|
print("BLE", d.name, d.address, flush=True)
|
|
|
|
try:
|
|
async with BleakClient(d, timeout=30) as c:
|
|
print("connected", flush=True)
|
|
notifs = []
|
|
|
|
def cb(_s, data):
|
|
notifs.append(bytes(data))
|
|
print("NOTIFY", data.hex(), flush=True)
|
|
|
|
for u in (UARTN, MIDI):
|
|
try:
|
|
await c.start_notify(u, cb)
|
|
print("notify ok", u[:8], flush=True)
|
|
except Exception as e:
|
|
print("notify fail", u[:8], e, flush=True)
|
|
|
|
await asyncio.sleep(0.5)
|
|
print("WRITE uartw F0 60 01 01 F7", flush=True)
|
|
try:
|
|
await c.write_gatt_char(UARTW, bytes.fromhex("F0600101F7"), response=False)
|
|
print("write returned", flush=True)
|
|
except Exception as e:
|
|
print("write err", e, flush=True)
|
|
await asyncio.sleep(4)
|
|
print("notifs", len(notifs), "conn", c.is_connected, flush=True)
|
|
except Exception as e:
|
|
print("BLE session err", e, flush=True)
|
|
|
|
await asyncio.sleep(1)
|
|
stop = True
|
|
time.sleep(0.3)
|
|
j.close()
|
|
print("RTT total chunks", len(lines), flush=True)
|
|
ble_hits = [x for x in lines if "BLE" in x or "sysex" in x]
|
|
print("BLE-related RTT lines:", ble_hits, flush=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|