Supporting multiple boards

This commit is contained in:
Eric Ratliff
2026-02-19 10:12:33 -06:00
parent 2739d83b99
commit b909da298e
16 changed files with 1554 additions and 94 deletions

View File

@@ -7,6 +7,7 @@
# Usage:
# ./upload.sh Auto-detect port, compile + upload
# ./upload.sh -p /dev/ttyUSB0 Specify port
# ./upload.sh --board mega Use a named board
# ./upload.sh --monitor Open serial monitor after upload
# ./upload.sh --clean Clean build cache first
# ./upload.sh --verbose Full compiler + avrdude output
@@ -42,15 +43,26 @@ toml_array() {
| sed 's/.*\[//; s/\].*//; s/"//g; s/,/ /g' | tr -s ' '
}
toml_section_get() {
local section="$1" key="$2"
awk -v section="[$section]" -v key="$key" '
$0 == section { found=1; next }
/^\[/ { found=0 }
found && $1 == key && /=/ {
sub(/^[^=]*= *"?/, ""); sub(/"? *$/, ""); print; exit
}
' "$CONFIG"
}
SKETCH_NAME="$(toml_get 'name')"
FQBN="$(toml_get 'fqbn')"
DEFAULT_BOARD="$(toml_get 'default')"
WARNINGS="$(toml_get 'warnings')"
INCLUDE_DIRS="$(toml_array 'include_dirs')"
EXTRA_FLAGS="$(toml_array 'extra_flags')"
BAUD="$(toml_get 'baud')"
[[ -n "$SKETCH_NAME" ]] || die "Could not read project name from .anvil.toml"
[[ -n "$FQBN" ]] || die "Could not read fqbn from .anvil.toml"
[[ -n "$SKETCH_NAME" ]] || die "Could not read project name from .anvil.toml"
[[ -n "$DEFAULT_BOARD" ]] || die "Could not read default board from .anvil.toml"
BAUD="${BAUD:-115200}"
LOCAL_CONFIG="$SCRIPT_DIR/.anvil.local"
@@ -68,22 +80,42 @@ PORT=""
DO_MONITOR=0
DO_CLEAN=0
VERBOSE=""
BOARD_NAME=""
while [[ $# -gt 0 ]]; do
case "$1" in
-p|--port) PORT="$2"; shift 2 ;;
--board) BOARD_NAME="$2"; shift 2 ;;
--monitor) DO_MONITOR=1; shift ;;
--clean) DO_CLEAN=1; shift ;;
--verbose) VERBOSE="--verbose"; shift ;;
-h|--help)
echo "Usage: ./upload.sh [-p PORT] [--monitor] [--clean] [--verbose]"
echo "Usage: ./upload.sh [-p PORT] [--board NAME] [--monitor] [--clean] [--verbose]"
echo " Compiles and uploads the sketch. Settings from .anvil.toml."
echo " --board NAME selects a board from [boards.NAME]."
exit 0
;;
*) die "Unknown option: $1" ;;
esac
done
# -- Resolve board ---------------------------------------------------------
ACTIVE_BOARD="${BOARD_NAME:-$DEFAULT_BOARD}"
FQBN="$(toml_section_get "boards.$ACTIVE_BOARD" "fqbn")"
if [[ -z "$FQBN" ]]; then
die "No board '$ACTIVE_BOARD' in .anvil.toml.\n Add it: anvil board --add $ACTIVE_BOARD"
fi
BOARD_BAUD="$(toml_section_get "boards.$ACTIVE_BOARD" "baud")"
if [[ -n "$BOARD_BAUD" ]]; then
BAUD="$BOARD_BAUD"
fi
if [[ -n "$BOARD_NAME" ]]; then
ok "Using board: $BOARD_NAME ($FQBN)"
fi
# -- Preflight -------------------------------------------------------------
command -v arduino-cli &>/dev/null \
|| die "arduino-cli not found in PATH."
@@ -94,13 +126,11 @@ command -v arduino-cli &>/dev/null \
# -- Resolve port ----------------------------------------------------------
# Priority: -p flag > VID:PID resolve > saved port > auto-detect
# resolve_vid_pid VID:PID -- search arduino-cli JSON for matching device
resolve_vid_pid() {
local target_vid target_pid json
target_vid="$(echo "$1" | cut -d: -f1 | tr '[:upper:]' '[:lower:]')"
target_pid="$(echo "$1" | cut -d: -f2 | tr '[:upper:]' '[:lower:]')"
json="$(arduino-cli board list --format json 2>/dev/null)" || return
# Walk through JSON looking for matching vid/pid on serial ports
echo "$json" | python3 -c "
import sys, json
try:
@@ -120,7 +150,6 @@ except: pass
}
if [[ -z "$PORT" ]]; then
# Try VID:PID resolution first
if [[ -n "$LOCAL_VID_PID" ]]; then
PORT="$(resolve_vid_pid "$LOCAL_VID_PID")"
if [[ -n "$PORT" ]]; then
@@ -132,13 +161,11 @@ if [[ -z "$PORT" ]]; then
fi
fi
# Fall back to saved port
if [[ -z "$PORT" ]] && [[ -n "$LOCAL_PORT" ]]; then
PORT="$LOCAL_PORT"
warn "Using port $PORT (from .anvil.local)"
fi
# Fall back to auto-detect
if [[ -z "$PORT" ]]; then
PORT=$(arduino-cli board list 2>/dev/null \
| grep -i "serial" \
@@ -191,7 +218,8 @@ COMPILE_ARGS=(
)
if [[ -n "$BUILD_FLAGS" ]]; then
COMPILE_ARGS+=(--build-property "build.extra_flags=$BUILD_FLAGS")
COMPILE_ARGS+=(--build-property "compiler.cpp.extra_flags=$BUILD_FLAGS")
COMPILE_ARGS+=(--build-property "compiler.c.extra_flags=$BUILD_FLAGS")
fi
[[ -n "$VERBOSE" ]] && COMPILE_ARGS+=("$VERBOSE")