|
| 1 | +#!/bin/sh - |
| 2 | +set -o nounset # Treat unset variables as an error |
| 3 | + |
| 4 | +STM32CP_CLI= |
| 5 | +ADDRESS=0x8000000 |
| 6 | +ERASE="" |
| 7 | +MODE="" |
| 8 | +PORT="" |
| 9 | +OPTS="" |
| 10 | + |
| 11 | +############################################################################### |
| 12 | +## Help function |
| 13 | +usage() { |
| 14 | + echo "############################################################" |
| 15 | + echo "##" |
| 16 | + echo "## $(basename "$0") <protocol> <file_path> [OPTIONS]" |
| 17 | + echo "##" |
| 18 | + echo "## protocol:" |
| 19 | + echo "## 0: SWD" |
| 20 | + echo "## 1: Serial" |
| 21 | + echo "## 2: DFU" |
| 22 | + echo "## Note: prefix it by 1 to erase all sectors." |
| 23 | + echo "## Ex: 10 erase all sectors using SWD interface." |
| 24 | + echo "## file_path: file path name to be downloaded: (bin, hex)" |
| 25 | + echo "## Options:" |
| 26 | + echo "## For SWD and DFU: no mandatory options" |
| 27 | + echo "## For Serial: <com_port>" |
| 28 | + echo "## com_port: serial identifier (mandatory). Ex: /dev/ttyS0 or COM1" |
| 29 | + echo "##" |
| 30 | + echo "## Note: all trailing arguments will be passed to the $STM32CP_CLI" |
| 31 | + echo "## They have to be valid commands for STM32 MCU" |
| 32 | + echo "## Ex: -g: Run the code at the specified address" |
| 33 | + echo "## -rst: Reset system" |
| 34 | + echo "## -s: start automatically (optional)" |
| 35 | + echo "############################################################" |
| 36 | + exit "$1" |
| 37 | +} |
| 38 | + |
| 39 | +UNAME_OS="$(uname -s)" |
| 40 | +case "${UNAME_OS}" in |
| 41 | + Linux*) |
| 42 | + STM32CP_CLI=STM32_Programmer.sh |
| 43 | + if ! command -v $STM32CP_CLI >/dev/null 2>&1; then |
| 44 | + export PATH="$HOME/STMicroelectronics/STM32Cube/STM32CubeProgrammer/bin":$PATH |
| 45 | + fi |
| 46 | + if ! command -v $STM32CP_CLI >/dev/null 2>&1; then |
| 47 | + export PATH="/opt/stm32cubeprog/bin":$PATH |
| 48 | + fi |
| 49 | + if ! command -v $STM32CP_CLI >/dev/null 2>&1; then |
| 50 | + echo "STM32CubeProgrammer not found ($STM32CP_CLI)." |
| 51 | + echo "Please install it or add '<STM32CubeProgrammer path>/bin' to your PATH environment:" |
| 52 | + echo "https://www.st.com/en/development-tools/stm32cubeprog.html" |
| 53 | + echo "Aborting!" |
| 54 | + exit 1 |
| 55 | + fi |
| 56 | + ;; |
| 57 | + Darwin*) |
| 58 | + STM32CP_CLI=STM32_Programmer_CLI |
| 59 | + if ! command -v $STM32CP_CLI >/dev/null 2>&1; then |
| 60 | + export PATH="/Applications/STMicroelectronics/STM32Cube/STM32CubeProgrammer/STM32CubeProgrammer.app/Contents/MacOs/bin":$PATH |
| 61 | + fi |
| 62 | + if ! command -v $STM32CP_CLI >/dev/null 2>&1; then |
| 63 | + echo "STM32CubeProgrammer not found ($STM32CP_CLI)." |
| 64 | + echo "Please install it or add '<STM32CubeProgrammer path>/bin' to your PATH environment:" |
| 65 | + echo "https://www.st.com/en/development-tools/stm32cubeprog.html" |
| 66 | + echo "Aborting!" |
| 67 | + exit 1 |
| 68 | + fi |
| 69 | + ;; |
| 70 | + Windows*) |
| 71 | + STM32CP_CLI=STM32_Programmer_CLI.exe |
| 72 | + if ! command -v $STM32CP_CLI >/dev/null 2>&1; then |
| 73 | + if [ -n "${PROGRAMFILES+x}" ]; then |
| 74 | + STM32CP86=${PROGRAMFILES}/STMicroelectronics/STM32Cube/STM32CubeProgrammer/bin |
| 75 | + export PATH=${STM32CP86}:$PATH |
| 76 | + fi |
| 77 | + if [ -n "${PROGRAMW6432+x}" ]; then |
| 78 | + STM32CP=${PROGRAMW6432}/STMicroelectronics/STM32Cube/STM32CubeProgrammer/bin |
| 79 | + export PATH=${STM32CP}:$PATH |
| 80 | + fi |
| 81 | + if ! command -v $STM32CP_CLI >/dev/null 2>&1; then |
| 82 | + echo "STM32CubeProgrammer not found ($STM32CP_CLI)." |
| 83 | + echo "Please install it or add '<STM32CubeProgrammer path>\bin' to your PATH environment:" |
| 84 | + echo "https://www.st.com/en/development-tools/stm32cubeprog.html" |
| 85 | + echo "Aborting!" |
| 86 | + fi |
| 87 | + fi |
| 88 | + ;; |
| 89 | + *) |
| 90 | + echo "Unknown host OS: ${UNAME_OS}." |
| 91 | + exit 1 |
| 92 | + ;; |
| 93 | +esac |
| 94 | + |
| 95 | +if [ $# -lt 2 ]; then |
| 96 | + echo "Not enough arguments!" |
| 97 | + usage 2 |
| 98 | +fi |
| 99 | + |
| 100 | +# Parse options |
| 101 | +PROTOCOL=$1 |
| 102 | +FILEPATH=$2 |
| 103 | +# Protocol $1 |
| 104 | +# 1x: Erase all sectors |
| 105 | +if [ "$1" -ge 10 ]; then |
| 106 | + ERASE="yes" |
| 107 | + PROTOCOL=$(($1 - 10)) |
| 108 | +fi |
| 109 | +# Protocol $1 |
| 110 | +# 0: SWD |
| 111 | +# 1: Serial |
| 112 | +# 2: DFU |
| 113 | +case $PROTOCOL in |
| 114 | + 0) |
| 115 | + PORT="SWD" |
| 116 | + MODE="mode=UR" |
| 117 | + shift 2 |
| 118 | + ;; |
| 119 | + 1) |
| 120 | + if [ $# -lt 3 ]; then |
| 121 | + usage 3 |
| 122 | + else |
| 123 | + PORT=$3 |
| 124 | + shift 3 |
| 125 | + fi |
| 126 | + ;; |
| 127 | + 2) |
| 128 | + PORT="USB1" |
| 129 | + shift 2 |
| 130 | + ;; |
| 131 | + *) |
| 132 | + echo "Protocol unknown!" |
| 133 | + usage 4 |
| 134 | + ;; |
| 135 | +esac |
| 136 | + |
| 137 | +if [ $# -gt 0 ]; then |
| 138 | + OPTS="$*" |
| 139 | +fi |
| 140 | + |
| 141 | +${STM32CP_CLI} -c port=${PORT} ${MODE} ${ERASE:+"-e all"} -q -d "${FILEPATH}" ${ADDRESS} "${OPTS}" |
| 142 | + |
| 143 | +exit $? |
0 commit comments