knob_os/tools/flash_slave.sh

53 lines
2.4 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/bin/bash
# flash_slave.sh —— 备份原厂固件并把自研双音源固件烧进副控
#
# 用法:./tools/flash_slave.sh [串口]
# 串口省略时自动找 cu.usbserial-*(副控经 CH340C,需把 Type-C 反插/换口)
#
# 三步:找串口 → 备份原厂 4MB(已备份则跳过)→ 烧录
# ⚠ 烧录会覆盖原厂固件!备份文件丢失等于放弃原厂蓝牙功能的退路,别跳过。
set -e
HERE="$(cd "$(dirname "$0")/.." && pwd)"
# arduino-cli 装的 esptool 是个可执行文件(esptool_py/<ver>/esptool),不是 .py
ESPTOOL="$(ls "$HOME/Library/Arduino15/packages/esp32/tools/esptool_py"/*/esptool 2>/dev/null | sort -V | tail -1)"
if [ -z "$ESPTOOL" ] && command -v esptool >/dev/null; then ESPTOOL="esptool"; fi
if [ -z "$ESPTOOL" ]; then
echo "!! 找不到 esptool(Arduino15 包目录下)"; exit 1
fi
PORT="${1:-$(ls /dev/cu.usbserial-* 2>/dev/null | head -1)}"
if [ -z "$PORT" ]; then
echo "!! 没找到 cu.usbserial-* 串口。副控要用 CH340C 那个口(Type-C 反插方向),插好后重跑。"
ls /dev/cu.* 2>/dev/null | head -10
exit 1
fi
echo ">> port: $PORT"
# ---------- 1. 备份原厂 4MB ----------
# 2026-11-03 已备份整片:00-固件库/设备原机备份/2026-11-03_ESP32副控_4MB整片.bin
# 脚本只认自己的命名(copro-factory-*);已有 11-03 整片备份时这两条都不会执行。
BK="/Volumes/DATA/资料/esp32/00-固件库/设备原机备份"
mkdir -p "$BK"
SNAP="$(ls "$BK"/copro-factory-*.bin 2>/dev/null | tail -1)"
if [ -z "$SNAP" ] && ls "$BK"/*副控*4MB*.bin >/dev/null 2>&1; then
echo ">> 已有备份,跳过: $(ls "$BK"/*副控*4MB*.bin)"
elif [ -z "$SNAP" ]; then
SNAP="$BK/copro-factory-$(date +%Y%m%d-%H%M%S).bin"
echo ">> 备份原厂 4MB -> $SNAP (约 1 分钟,别拔线)"
"$ESPTOOL" --chip esp32 --port "$PORT" --baud 921600 \
read_flash 0x0 0x400000 "$SNAP"
echo ">> 备份完成,务必保留这个文件"
fi
# ---------- 2. 烧录自研固件 ----------
APP="$HERE/build_slave"
echo ">> flashing slave_audio"
"$ESPTOOL" --chip esp32 --port "$PORT" --baud 921600 \
write_flash 0x1000 "$APP/slave_audio.ino.bootloader.bin" \
0x8000 "$APP/slave_audio.ino.partitions.bin" \
0x10000 "$APP/slave_audio.ino.bin"
echo ">> 完成。验证:打开串口监视器 115200,应看到"
echo " '=== slave_audio (A2DP sink + uart mp3 + PCM5100) ===' 和 'ready'"