Files
CelloEffects/gcc/flash.sh
T
2026-10-18 23:39:44 +02:00

86 lines
3.0 KiB
Bash
Executable File

#!/usr/bin/env bash
# Flash / probe helper for the STM32H7S78-DK dual-boot project (macOS/Linux).
#
# Usage: gcc/flash.sh <boot|appli|all|probe|reset|verify>
#
# boot Flash Boot (internal flash 0x08000000)
# appli Flash Appli (external OSPI flash 0x70000000, needs the external loader)
# all Flash Boot then Appli (required order)
# probe Connect to the board and print device info (no write)
# reset Hardware reset of the board
# verify Dump 4 KB of internal and external flash to /tmp for inspection
#
# The STM32H7RS core sits behind access port 1 and the running firmware can
# prevent a hot-plug connection, so every command uses "ap=1 mode=UR".
#
# STM32_Programmer_CLI is looked up in: $STM32_PROGRAMMER_CLI, $PATH,
# the standalone STM32CubeProgrammer app, then the STM32CubeIDE plugin.
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
BOOT_HEX="$ROOT/Boot/TouchGFX/build/bin/target.hex"
APPLI_HEX="$ROOT/Appli/TouchGFX/build/bin/target.hex"
LOADER="$ROOT/gcc/MX66UW1G45G_STM32H7S78-DK.stldr"
find_cli() {
if [[ -n "${STM32_PROGRAMMER_CLI:-}" && -x "$STM32_PROGRAMMER_CLI" ]]; then
echo "$STM32_PROGRAMMER_CLI"; return
fi
if command -v STM32_Programmer_CLI >/dev/null 2>&1; then
command -v STM32_Programmer_CLI; return
fi
local candidates=(
"/Applications/STMicroelectronics/programmer/STM32CubeProgrammer.app/Contents/MacOs/bin/STM32_Programmer_CLI"
"/Applications/STMicroelectronics/STM32Cube/STM32CubeProgrammer/STM32CubeProgrammer.app/Contents/MacOs/bin/STM32_Programmer_CLI"
"/opt/st/stm32cubeclt/STM32CubeProgrammer/bin/STM32_Programmer_CLI"
)
# STM32CubeIDE bundles its own copy; the plugin folder name changes with each update
local ide
for ide in /Applications/STMicroelectronics/STM32CubeIDE.app/Contents/Eclipse/plugins/com.st.stm32cube.ide.mcu.externaltools.cubeprogrammer.*/tools/bin/STM32_Programmer_CLI; do
candidates+=("$ide")
done
local c
for c in "${candidates[@]}"; do
[[ -x "$c" ]] && { echo "$c"; return; }
done
echo "error: STM32_Programmer_CLI not found (set STM32_PROGRAMMER_CLI)" >&2
exit 1
}
require() {
[[ -f "$1" ]] || { echo "error: $1 not found - build first (make -C gcc)" >&2; exit 1; }
}
CLI="$(find_cli)"
CONNECT=(-c port=SWD ap=1 mode=UR)
flash_boot() {
require "$BOOT_HEX"
echo ">>> Flashing Boot: $BOOT_HEX"
"$CLI" "${CONNECT[@]}" -d "$BOOT_HEX" -v -hardRst
}
flash_appli() {
require "$APPLI_HEX"
require "$LOADER"
echo ">>> Flashing Appli: $APPLI_HEX (external loader)"
"$CLI" "${CONNECT[@]}" -el "$LOADER" -d "$APPLI_HEX" -hardRst
}
case "${1:-}" in
boot) flash_boot ;;
appli) flash_appli ;;
all) flash_boot; flash_appli ;;
probe) "$CLI" -l st; "$CLI" "${CONNECT[@]}" ;;
reset) "$CLI" "${CONNECT[@]}" -hardRst ;;
verify)
"$CLI" "${CONNECT[@]}" -u 0x08000000 0x1000 /tmp/read_int.bin
"$CLI" "${CONNECT[@]}" -el "$LOADER" -u 0x70000000 0x1000 /tmp/read_ext.bin
;;
*)
sed -n '4,12p' "$0" | sed 's/^# \{0,1\}//'
exit 1
;;
esac