diff options
| author | James Munns <[email protected]> | 2025-12-04 18:46:39 +0100 |
|---|---|---|
| committer | James Munns <[email protected]> | 2025-12-04 18:47:31 +0100 |
| commit | c3bc8fe8c0db112e5f2f66580104fc49b02890d2 (patch) | |
| tree | eea7adc15021697ea980289edd6235daaa12d6ee /embassy-mcxa/run.sh | |
| parent | 5182b2ed54f991b40b45fd2de75f597358ff9c3e (diff) | |
| parent | 277ab0d2e8714edf37a0ee84cda1059d9944ecef (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.sh | 93 |
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 | ||
| 2 | set -euo pipefail | ||
| 3 | |||
| 4 | ELF="${1:-}" | ||
| 5 | if [[ -z "${ELF}" ]]; then | ||
| 6 | echo "Usage: $0 <elf_file>" | ||
| 7 | exit 1 | ||
| 8 | fi | ||
| 9 | if [[ ! -f "${ELF}" ]]; then | ||
| 10 | echo "ELF not found: ${ELF}" | ||
| 11 | exit 1 | ||
| 12 | fi | ||
| 13 | |||
| 14 | # Configurable via env | ||
| 15 | CHIP="${CHIP:-MCXA276}" | ||
| 16 | SPEED="${PROBE_SPEED:-1000}" # kHz | ||
| 17 | # Default to J-Link if PROBE not provided | ||
| 18 | PROBE_OPT=(--probe "${PROBE:-1366:0101:000600110607}") | ||
| 19 | PORT="${PROBE_RS_GDB_PORT:-1337}" | ||
| 20 | |||
| 21 | cleanup() { | ||
| 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 | } | ||
| 26 | trap cleanup EXIT | ||
| 27 | |||
| 28 | if ! command -v probe-rs >/dev/null 2>&1; then | ||
| 29 | echo "probe-rs not found (cargo install probe-rs --features cli)" | ||
| 30 | exit 1 | ||
| 31 | fi | ||
| 32 | if ! 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 | ||
| 35 | fi | ||
| 36 | |||
| 37 | # Start probe-rs GDB server and capture its output to a log (do not hide errors) | ||
| 38 | SERVER_LOG=$(mktemp) | ||
| 39 | set +e | ||
| 40 | probe-rs gdb --chip "${CHIP}" --protocol swd --speed "${SPEED}" --non-interactive "${ELF}" "${PROBE_OPT[@]}" \ | ||
| 41 | >"${SERVER_LOG}" 2>&1 & | ||
| 42 | GDB_SERVER_PID=$! | ||
| 43 | set -e | ||
| 44 | |||
| 45 | # Wait for server readiness without touching the TCP port to avoid corrupting the GDB protocol | ||
| 46 | ready="" | ||
| 47 | for _ 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 | ||
| 56 | done | ||
| 57 | if [[ -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 | ||
| 62 | fi | ||
| 63 | |||
| 64 | # GDB script: load to RAM and run, no reset | ||
| 65 | GDB_SCRIPT=$(mktemp) | ||
| 66 | cat >"${GDB_SCRIPT}" <<EOF | ||
| 67 | set pagination off | ||
| 68 | set confirm off | ||
| 69 | set mem inaccessible-by-default off | ||
| 70 | |||
| 71 | # Connect and load without reset | ||
| 72 | target remote :${PORT} | ||
| 73 | monitor halt | ||
| 74 | load | ||
| 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 | ||
| 77 | set *0xE000ED08 = 0x20000000 | ||
| 78 | # Now read SP and PC from our vector table and set them | ||
| 79 | set \$sp = *(unsigned int*)0x20000000 | ||
| 80 | set \$pc = *(unsigned int*)0x20000004 | ||
| 81 | # Run target (blocks here until you Ctrl+C, like before) | ||
| 82 | continue | ||
| 83 | EOF | ||
| 84 | |||
| 85 | # Run gdb against the server | ||
| 86 | if ! 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 | ||
| 91 | fi | ||
| 92 | |||
| 93 | echo "Program loaded and started (no reset)" | ||
