2026-09-14 15:05:17 +08:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
"""RTT 传输:薄封装 rtt_pitch_reg_test 的 J-Link/RTT 实现(单一事实来源,避免复制)."""
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
|
|
from contextlib import contextmanager
|
|
|
|
|
|
|
|
|
|
|
|
from ..paths import TOOLS_DIR # noqa: F401 (确保 sys.path 已注入)
|
|
|
|
|
|
|
|
|
|
|
|
import rtt_pitch_reg_test as _rtt
|
|
|
|
|
|
|
|
|
|
|
|
# 直接复用,保持零行为变更
|
|
|
|
|
|
connect_jlink = _rtt.connect_jlink
|
|
|
|
|
|
find_rtt_control_block = _rtt.find_rtt_control_block
|
|
|
|
|
|
wait_rtt_ready = _rtt.wait_rtt_ready
|
|
|
|
|
|
RttSession = _rtt.RttSession
|
|
|
|
|
|
analyze_lines = _rtt.analyze_lines
|
|
|
|
|
|
CheckResult = _rtt.CheckResult
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@contextmanager
|
2026-09-14 21:49:37 +08:00
|
|
|
|
def open_session(device: str = "AT32F403AC", *, reset: bool = True):
|
|
|
|
|
|
"""连接 J-Link + 启动 RTT,yield RttSession;退出时清理.
|
|
|
|
|
|
|
|
|
|
|
|
reset=True:先硬件复位并等待开机,避免前序 LCD/烧写把主循环卡死导致无 ACK。
|
|
|
|
|
|
"""
|
|
|
|
|
|
import time
|
|
|
|
|
|
|
2026-09-14 15:05:17 +08:00
|
|
|
|
_rtt.import_deps()
|
|
|
|
|
|
jlink = connect_jlink(device)
|
|
|
|
|
|
try:
|
2026-09-14 21:49:37 +08:00
|
|
|
|
if reset:
|
|
|
|
|
|
try:
|
|
|
|
|
|
jlink.reset(halt=False)
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
pass
|
|
|
|
|
|
time.sleep(5.0)
|
2026-09-14 15:05:17 +08:00
|
|
|
|
cb = find_rtt_control_block(jlink)
|
|
|
|
|
|
if cb is None:
|
|
|
|
|
|
raise SystemExit("SEGGER RTT control block 未找到(固件未运行?)")
|
|
|
|
|
|
print(f"RTT CB @ 0x{cb:08X}", flush=True)
|
|
|
|
|
|
jlink.rtt_start(cb)
|
|
|
|
|
|
wait_rtt_ready(jlink)
|
|
|
|
|
|
sess = RttSession(jlink)
|
|
|
|
|
|
sess.pump(0.3)
|
|
|
|
|
|
yield sess
|
|
|
|
|
|
finally:
|
|
|
|
|
|
try:
|
|
|
|
|
|
jlink.rtt_stop()
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
pass
|
|
|
|
|
|
jlink.close()
|