aboutsummaryrefslogtreecommitdiff
path: root/embassy-mcxa/run.sh
diff options
context:
space:
mode:
authorJames Munns <[email protected]>2025-12-04 18:46:39 +0100
committerJames Munns <[email protected]>2025-12-04 18:47:31 +0100
commitc3bc8fe8c0db112e5f2f66580104fc49b02890d2 (patch)
treeeea7adc15021697ea980289edd6235daaa12d6ee /embassy-mcxa/run.sh
parent5182b2ed54f991b40b45fd2de75f597358ff9c3e (diff)
parent277ab0d2e8714edf37a0ee84cda1059d9944ecef (diff)
Import embassy-mcxa repo
Merge remote-tracking branch 'james-e-mcxa/james/upstream' into james/upstream-mcxa
Diffstat (limited to 'embassy-mcxa/run.sh')
-rw-r--r--embassy-mcxa/run.sh93
1 files changed, 93 insertions, 0 deletions
diff --git a/embassy-mcxa/run.sh b/embassy-mcxa/run.sh
new file mode 100644
index 000000000..418dc8a24
--- /dev/null
+++ b/embassy-mcxa/run.sh
@@ -0,0 +1,93 @@
1#!/usr/bin/env bash
2set -euo pipefail
3
4ELF="${1:-}"
5if [[ -z "${ELF}" ]]; then
6 echo "Usage: $0 <elf_file>"
7 exit 1
8fi
9if [[ ! -f "${ELF}" ]]; then
10 echo "ELF not found: ${ELF}"
11 exit 1
12fi
13
14# Configurable via env
15CHIP="${CHIP:-MCXA276}"
16SPEED="${PROBE_SPEED:-1000}" # kHz
17# Default to J-Link if PROBE not provided
18PROBE_OPT=(--probe "${PROBE:-1366:0101:000600110607}")
19PORT="${PROBE_RS_GDB_PORT:-1337}"
20
21cleanup() {
22 if [[ -n "${GDB_SERVER_PID:-}" ]]; then kill "${GDB_SERVER_PID}" 2>/dev/null || true; fi
23 [[ -n "${GDB_SCRIPT:-}" ]] && rm -f "${GDB_SCRIPT}" || true
24 [[ -n "${SERVER_LOG:-}" ]] && rm -f "${SERVER_LOG}" || true
25}
26trap cleanup EXIT
27
28if ! command -v probe-rs >/dev/null 2>&1; then
29 echo "probe-rs not found (cargo install probe-rs --features cli)"
30 exit 1
31fi
32if ! command -v gdb-multiarch >/dev/null 2>&1; then
33 echo "gdb-multiarch not found; install it (e.g., sudo apt install gdb-multiarch)."
34 exit 1
35fi
36
37# Start probe-rs GDB server and capture its output to a log (do not hide errors)
38SERVER_LOG=$(mktemp)
39set +e
40probe-rs gdb --chip "${CHIP}" --protocol swd --speed "${SPEED}" --non-interactive "${ELF}" "${PROBE_OPT[@]}" \
41 >"${SERVER_LOG}" 2>&1 &
42GDB_SERVER_PID=$!
43set -e
44
45# Wait for server readiness without touching the TCP port to avoid corrupting the GDB protocol
46ready=""
47for _ in {1..50}; do
48 if grep -q "Firing up GDB stub" "${SERVER_LOG}"; then ready=1; break; fi
49 if grep -q "Connecting to the chip was unsuccessful" "${SERVER_LOG}"; then
50 echo "probe-rs gdb server failed to connect to target. Log:" >&2
51 echo "----- probe-rs gdb log -----" >&2
52 sed -e 's/^/ /' "${SERVER_LOG}" >&2 || true
53 exit 1
54 fi
55 sleep 0.1
56done
57if [[ -z "${ready}" ]]; then
58 echo "probe-rs gdb server did not report readiness. Log:" >&2
59 echo "----- probe-rs gdb log -----" >&2
60 sed -e 's/^/ /' "${SERVER_LOG}" >&2 || true
61 exit 1
62fi
63
64# GDB script: load to RAM and run, no reset
65GDB_SCRIPT=$(mktemp)
66cat >"${GDB_SCRIPT}" <<EOF
67set pagination off
68set confirm off
69set mem inaccessible-by-default off
70
71# Connect and load without reset
72target remote :${PORT}
73monitor halt
74load
75# Set VTOR to point to our RAM vector table at 0x20000000
76# This ensures the CPU uses the correct initial SP and Reset vector
77set *0xE000ED08 = 0x20000000
78# Now read SP and PC from our vector table and set them
79set \$sp = *(unsigned int*)0x20000000
80set \$pc = *(unsigned int*)0x20000004
81# Run target (blocks here until you Ctrl+C, like before)
82continue
83EOF
84
85# Run gdb against the server
86if ! gdb-multiarch -q -batch -x "${GDB_SCRIPT}" "${ELF}"; then
87 echo "GDB failed to load/run. probe-rs gdb server log:" >&2
88 echo "----- probe-rs gdb log -----" >&2
89 sed -e 's/^/ /' "${SERVER_LOG}" >&2 || true
90 exit 1
91fi
92
93echo "Program loaded and started (no reset)"