mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-20 09:03:28 +00:00
file creation and structure for options
This commit is contained in:
parent
201c143762
commit
38d3c17ee8
12 changed files with 226 additions and 0 deletions
7
tools-bao/README.md
Normal file
7
tools-bao/README.md
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
# Bao CLI (temporary home)
|
||||||
|
|
||||||
|
This is the Baochip CLI used by the VS Code extension. It will be moved under `tools/` after repo cleanup.
|
||||||
|
|
||||||
|
## Install
|
||||||
|
```bash
|
||||||
|
python -m pip install -r tools-bao/requirements.txt
|
||||||
69
tools-bao/bao.py
Normal file
69
tools-bao/bao.py
Normal file
|
|
@ -0,0 +1,69 @@
|
||||||
|
"""
|
||||||
|
Baochip CLI — entry point for host-side utilities.
|
||||||
|
|
||||||
|
Usage examples:
|
||||||
|
python tools-bao/bao.py ports
|
||||||
|
python tools-bao/bao.py monitor -p COM3 -b 115200 --ts
|
||||||
|
python tools-bao/bao.py build --target dabao
|
||||||
|
"""
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import sys
|
||||||
|
|
||||||
|
# Subcommand imports
|
||||||
|
from commands.ports import cmd_ports
|
||||||
|
from commands.monitor import cmd_monitor
|
||||||
|
from commands.flash import cmd_flash
|
||||||
|
from commands.doctor import cmd_doctor
|
||||||
|
from commands.build import cmd_build
|
||||||
|
|
||||||
|
def main():
|
||||||
|
ap = argparse.ArgumentParser(prog="bao.py")
|
||||||
|
sub = ap.add_subparsers(dest="cmd", required=True)
|
||||||
|
|
||||||
|
# ports
|
||||||
|
s = sub.add_parser("ports", help="List serial ports")
|
||||||
|
s.add_argument("-v", "--verbose", action="store_true")
|
||||||
|
s.set_defaults(func=cmd_ports)
|
||||||
|
|
||||||
|
# monitor
|
||||||
|
m = sub.add_parser("monitor", help="Open a serial monitor")
|
||||||
|
m.add_argument("-p", "--port", required=True)
|
||||||
|
m.add_argument("-b", "--baud", type=int, default=115200)
|
||||||
|
m.add_argument("--ts", action="store_true", help="Show timestamps")
|
||||||
|
m.add_argument("--save", help="Append output to a file")
|
||||||
|
m.add_argument("--reset", action="store_true", help="Toggle DTR/RTS on open")
|
||||||
|
m.set_defaults(func=cmd_monitor)
|
||||||
|
|
||||||
|
# build (Xous via cargo xtask)
|
||||||
|
b = sub.add_parser("build", help="Build Xous image for Baochip via cargo xtask")
|
||||||
|
b.add_argument("--target", required=True, help="Bao target (e.g. dabao, baosec)")
|
||||||
|
b.add_argument("--release", action="store_true", help="Use release build")
|
||||||
|
b.add_argument("--extra-args", help="Extra args to pass to cargo xtask")
|
||||||
|
b.set_defaults(func=cmd_build)
|
||||||
|
|
||||||
|
# doctor
|
||||||
|
d = sub.add_parser("doctor", help="Check Python environment and ports")
|
||||||
|
d.set_defaults(func=cmd_doctor)
|
||||||
|
|
||||||
|
# flash (stub)
|
||||||
|
f = sub.add_parser("flash", help="Flash a built image (stub)")
|
||||||
|
f.add_argument("-p", "--port", required=True)
|
||||||
|
f.add_argument("-b", "--baud", type=int, default=115200)
|
||||||
|
f.add_argument("--addr", type=lambda x: int(x, 0), default=0x0)
|
||||||
|
f.add_argument("--file", help="Binary to flash")
|
||||||
|
f.set_defaults(func=cmd_flash)
|
||||||
|
|
||||||
|
args = ap.parse_args()
|
||||||
|
try:
|
||||||
|
args.func(args)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print("\n[bao] aborted by user.")
|
||||||
|
sys.exit(1)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"[bao] error: {e}", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
0
tools-bao/commands/__init__.py
Normal file
0
tools-bao/commands/__init__.py
Normal file
22
tools-bao/commands/build.py
Normal file
22
tools-bao/commands/build.py
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
ROOT = Path(__file__).resolve().parents[2] # xous-core root (tools-bao -> xous-core)
|
||||||
|
|
||||||
|
def cmd_build(args):
|
||||||
|
"""Run cargo xtask <target> to build a Xous image for Baochip."""
|
||||||
|
target = args.target
|
||||||
|
cargo_cmd = ["cargo", "xtask", target]
|
||||||
|
if args.release:
|
||||||
|
cargo_cmd.append("--release")
|
||||||
|
if args.extra_args:
|
||||||
|
cargo_cmd += args.extra_args.split()
|
||||||
|
|
||||||
|
print(f"[bao] Running: {' '.join(cargo_cmd)}")
|
||||||
|
try:
|
||||||
|
subprocess.run(cargo_cmd, cwd=ROOT, check=True)
|
||||||
|
print(f"[bao] Build for target '{target}' complete.")
|
||||||
|
except subprocess.CalledProcessError as e:
|
||||||
|
print(f"[bao] Build failed with exit code {e.returncode}", file=sys.stderr)
|
||||||
|
sys.exit(e.returncode)
|
||||||
11
tools-bao/commands/doctor.py
Normal file
11
tools-bao/commands/doctor.py
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
from serial.tools import list_ports
|
||||||
|
|
||||||
|
def cmd_doctor(_args):
|
||||||
|
"""Quick environment check."""
|
||||||
|
try:
|
||||||
|
ports = list(list_ports.comports())
|
||||||
|
print(f"[bao] Python OK; pyserial OK; ports found: {len(ports)}")
|
||||||
|
for p in ports:
|
||||||
|
print(f" - {p.device} {p.description}")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"[bao] Doctor failed: {e}")
|
||||||
2
tools-bao/commands/flash.py
Normal file
2
tools-bao/commands/flash.py
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
def cmd_flash(args):
|
||||||
|
print(f"[bao] (stub) would flash '{args.file}' to addr {hex(args.addr)} on {args.port} @ {args.baud}")
|
||||||
37
tools-bao/commands/monitor.py
Normal file
37
tools-bao/commands/monitor.py
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
from serial.serialutil import SerialException
|
||||||
|
from utils.serial_utils import open_serial, safe_close
|
||||||
|
|
||||||
|
|
||||||
|
def cmd_monitor(args):
|
||||||
|
"""Simple serial monitor with optional timestamps."""
|
||||||
|
ser = open_serial(args.port, args.baud, timeout=0.1, reset=getattr(args, "reset", False))
|
||||||
|
|
||||||
|
print(f"[bao] Monitor {args.port} @ {args.baud} (Ctrl+C to exit)")
|
||||||
|
consecutive_errors = 0
|
||||||
|
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
data = ser.read(4096)
|
||||||
|
if data:
|
||||||
|
s = data.decode(errors="replace")
|
||||||
|
if args.ts:
|
||||||
|
ts = time.strftime("%H:%M:%S")
|
||||||
|
s = "".join(f"[{ts}] {line}\n" for line in s.splitlines())
|
||||||
|
sys.stdout.write(s)
|
||||||
|
sys.stdout.flush()
|
||||||
|
consecutive_errors = 0
|
||||||
|
time.sleep(0.01)
|
||||||
|
except SerialException as e:
|
||||||
|
consecutive_errors += 1
|
||||||
|
print(f"\n[bao] Serial error: {e}. Retrying ({consecutive_errors}/3)...", file=sys.stderr)
|
||||||
|
time.sleep(0.25)
|
||||||
|
if consecutive_errors >= 3:
|
||||||
|
print("[bao] Giving up. Check that no other program is using the port.", file=sys.stderr)
|
||||||
|
break
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
pass
|
||||||
|
finally:
|
||||||
|
safe_close(ser)
|
||||||
15
tools-bao/commands/ports.py
Normal file
15
tools-bao/commands/ports.py
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
from serial.tools import list_ports
|
||||||
|
|
||||||
|
def cmd_ports(args):
|
||||||
|
ports = list(list_ports.comports())
|
||||||
|
if args.verbose:
|
||||||
|
print(f"[bao] pyserial found {len(ports)} port(s)")
|
||||||
|
if not ports:
|
||||||
|
print("[bao] No serial ports found.")
|
||||||
|
print(" Try: python -m serial.tools.list_ports -v")
|
||||||
|
return
|
||||||
|
for p in ports:
|
||||||
|
vidpid = ""
|
||||||
|
if p.vid is not None and p.pid is not None:
|
||||||
|
vidpid = f" (VID:PID={p.vid:04x}:{p.pid:04x})"
|
||||||
|
print(f"{p.device}\t{p.description}{vidpid}")
|
||||||
1
tools-bao/requirements.txt
Normal file
1
tools-bao/requirements.txt
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
pyserial>=3.5
|
||||||
0
tools-bao/utils/__init__.py
Normal file
0
tools-bao/utils/__init__.py
Normal file
4
tools-bao/utils/common.py
Normal file
4
tools-bao/utils/common.py
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
def project_root() -> Path:
|
||||||
|
return Path(__file__).resolve().parents[2]
|
||||||
58
tools-bao/utils/serial_utils.py
Normal file
58
tools-bao/utils/serial_utils.py
Normal file
|
|
@ -0,0 +1,58 @@
|
||||||
|
"""
|
||||||
|
Shared serial helpers for Bao tools.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
import serial
|
||||||
|
from serial.serialutil import SerialException
|
||||||
|
|
||||||
|
|
||||||
|
def open_serial(port: str, baud: int, *, timeout: float = 0.1, reset: bool = False) -> serial.Serial:
|
||||||
|
"""
|
||||||
|
Open a serial port with consistent options and optional short reset pulse.
|
||||||
|
- Sets DTR/RTS low by default (helps avoid unwanted resets/power cuts).
|
||||||
|
- If reset=True, briefly toggles DTR/RTS to nudge bootloaders.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
ser = serial.Serial(port, baud, timeout=timeout)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"[bao] cannot open {port}: {e}", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
# default: release control lines
|
||||||
|
try:
|
||||||
|
ser.dtr = False
|
||||||
|
ser.rts = False
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
if reset:
|
||||||
|
try:
|
||||||
|
ser.dtr = True
|
||||||
|
ser.rts = True
|
||||||
|
time.sleep(0.05)
|
||||||
|
ser.dtr = False
|
||||||
|
ser.rts = False
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
# clear any pending bytes
|
||||||
|
try:
|
||||||
|
ser.reset_input_buffer()
|
||||||
|
ser.reset_output_buffer()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
return ser
|
||||||
|
|
||||||
|
|
||||||
|
def safe_close(ser: serial.Serial | None) -> None:
|
||||||
|
"""Close a serial port, ignoring errors."""
|
||||||
|
if ser is None:
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
ser.close()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
Loading…
Reference in a new issue