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