#!/usr/bin/env bash # 编译 + 烧录装甲板固件,然后抓一段串口日志(UART0 = RXD GPIO20 / TXD GPIO21)。 # # scripts/flash_armor.sh # 自动找串口,烧完看 20 秒日志 # ESP_PORT=/dev/cu.usbmodem1101 scripts/flash_armor.sh # scripts/flash_armor.sh --monitor 60 # 看 60 秒 # scripts/flash_armor.sh --no-monitor # 只烧不抓 # # 板子上的调试口就是 UART0,所以烧完之后日志会直接从这里出来,不需要另开串口工具。 set -euo pipefail cd "$(dirname "$0")/.." MONITOR_SECS=20 while [ $# -gt 0 ]; do case "$1" in --monitor) MONITOR_SECS="${2:-20}" shift 2 ;; --no-monitor) MONITOR_SECS=0 shift ;; *) break ;; esac done port="${ESP_PORT:-}" if [ -z "$port" ]; then port=$(ls /dev/cu.usbmodem* /dev/cu.wchusbserial* /dev/cu.SLAB* /dev/cu.usbserial* 2>/dev/null | head -1 || true) fi if [ -z "$port" ]; then echo "no serial port found; set ESP_PORT=/dev/cu.xxx" >&2 exit 1 fi echo "==> build" ./scripts/build_armor.sh "$@" elf="armor/target/riscv32imc-esp-espidf/release/armor" if [ ! -f "$elf" ]; then echo "expected $elf; run scripts/build_armor.sh first" >&2 exit 1 fi echo "==> flash $port" # espflash 直接吃 ELF:分区表与 bootloader 都从 ELF 里读,不用手工拼地址。 espflash flash --chip esp32c3 --port "$port" --non-interactive "$elf" if [ "$MONITOR_SECS" -gt 0 ]; then echo "==> serial log (${MONITOR_SECS}s, 115200 8N1)" python3 - "$port" "$MONITOR_SECS" <<'PY' import os, sys, termios, time fd = os.open(sys.argv[1], os.O_RDONLY | os.O_NOCTTY) a = termios.tcgetattr(fd) a[0] = a[1] = a[3] = 0 a[2] = termios.CS8 | termios.CREAD | termios.CLOCAL a[4] = a[5] = termios.B115200 a[6][termios.VMIN] = 0 a[6][termios.VTIME] = 1 termios.tcsetattr(fd, termios.TCSANOW, a) end = time.time() + float(sys.argv[2]) while time.time() < end: try: data = os.read(fd, 4096) except OSError: break if data: sys.stdout.write(data.decode("utf-8", "replace")) sys.stdout.flush() PY fi