Added trigger to scope

This commit is contained in:
2026-03-05 16:13:26 +01:00
parent e0a276cd18
commit cfdec1aec7
2 changed files with 96 additions and 18 deletions

View File

@@ -10,6 +10,7 @@ MEM_BASE = 0x00000000
REG_BASE = 0x80000000
REG_CONTROL = REG_BASE + 0x00
REG_STATUS = REG_BASE + 0x04
REG_TRIG_VAL = REG_BASE + 0x08
def _add_bridge_module_path() -> None:
@@ -46,6 +47,12 @@ def parse_args() -> argparse.Namespace:
default=0.05,
help="Seconds to wait after arm/dearm before reading",
)
parser.add_argument(
"--trigger-value",
type=lambda x: int(x, 0),
default=None,
help="Optional trigger threshold (16-bit, signal_a rising crossing). If omitted, triggering is disabled.",
)
parser.add_argument(
"--unsigned",
action="store_true",
@@ -63,14 +70,38 @@ def parse_args() -> argparse.Namespace:
action="store_true",
help="Keep running: press Enter to recapture/replot in the same window",
)
parser.add_argument(
"--continuous",
action="store_true",
help="Keep running and recapture continuously without waiting for Enter",
)
return parser.parse_args()
def capture_once(bridge, args: argparse.Namespace) -> list[tuple[int, int, int, int]]:
samples = []
frame_count = args.depth
print("[signal_scope] Arming scope (write REG_CONTROL bit0=1)...")
bridge.write32(REG_CONTROL, 0x1)
if args.trigger_value is None:
print("[signal_scope] Arming scope with trigger disabled...")
bridge.write32(REG_CONTROL, 0x1) # bit0: arm pulse, bit1: trigger enable=0
else:
trig_val = args.trigger_value & 0xFFFF
print(f"[signal_scope] Config trigger: trig_val=0x{trig_val:04x}, source=signal_a rising")
bridge.write32(REG_TRIG_VAL, trig_val)
print("[signal_scope] Arming scope with trigger enabled...")
bridge.write32(REG_CONTROL, 0x3) # bit0: arm pulse, bit1: trigger enable
# Wait until the new arm command is active, then wait for its trigger event.
while (bridge.read32(REG_STATUS) & 0x4) == 0:
time.sleep(0.001)
print("[signal_scope] Waiting for trigger...")
while True:
status = bridge.read32(REG_STATUS)
if status & 0x1:
break
time.sleep(0.001)
if args.wait_s > 0:
print(f"[signal_scope] Waiting {args.wait_s:.3f}s for capture to complete...")
time.sleep(args.wait_s)
@@ -93,7 +124,6 @@ def capture_once(bridge, args: argparse.Namespace) -> list[tuple[int, int, int,
samples.append((ch_a, ch_b, ch_c, ch_d))
if idx and (idx % max(1, frame_count // 10) == 0):
pct = (100 * idx) // frame_count
print(f"[signal_scope] Read progress: {pct}% ({idx}/{frame_count})")
print(f"[signal_scope] Read complete: {len(samples)} frames")
return samples
@@ -116,10 +146,10 @@ def plot_samples(ax, samples: list[tuple[int, int, int, int]], args: argparse.Na
series[3].append(ch_d)
ax.cla()
ax.plot(series[0], linewidth=1, label="ch_a")
ax.plot(series[1], linewidth=1, label="ch_b")
ax.plot(series[2], linewidth=1, label="ch_c")
ax.plot(series[3], linewidth=1, label="ch_d")
ax.plot(series[0], linewidth=1, label="ch_d")
ax.plot(series[1], linewidth=1, label="ch_c")
ax.plot(series[2], linewidth=1, label="ch_b")
ax.plot(series[3], linewidth=1, label="ch_a")
ax.set_title(f"signal_scope_q15 capture #{capture_idx} (depth={args.depth}, chain={args.chain})")
ax.set_xlabel("Sample")
ax.set_ylabel("Value")
@@ -178,10 +208,13 @@ def main() -> int:
fig.savefig(out_path, dpi=150)
print(f"Wrote plot: {out_path}")
if not args.interactive:
if not args.interactive and not args.continuous:
break
plt.show(block=False)
if args.continuous:
capture_idx += 1
continue
answer = input("[signal_scope] Press Enter to recapture, or 'q' + Enter to quit: ")
if answer.strip().lower().startswith("q"):
break