-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathstm32CubeProg.sh
More file actions
348 lines (331 loc) · 9.9 KB
/
Copy pathstm32CubeProg.sh
File metadata and controls
348 lines (331 loc) · 9.9 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#!/bin/sh -
set -o nounset # Treat unset variables as an error
# set -o xtrace # Print command traces before executing command.
UNAME_OS="$(uname -s)"
GNU_GETOPT="n" # "y" if GNU getopt is available
STM32CP_CLI=
INTERFACE=
PORT=
FILEPATH=
OFFSET=0x0
# Optional
ADDRESS=0x8000000
START=0x8000000
ERASE=
# Optional for SWD
FREQ=
MODE=UR
SERIAL_NUMBER=
# Optional for Serial
RTS=
DTR=
PARITY=EVEN
# Mandatory for DFU
VID=
PID=
###############################################################################
## Help function
usage() {
echo "Usage: $(basename "$0") [OPTIONS]...
Mandatory options:
-i, --interface <'swd'/'dfu'/'serial'/'jlink'> interface identifier: 'swd', 'dfu', 'serial' or 'jlink'
-b, --bin <file_path> binary file path to be downloaded: bin or hex
Optional options:
-a, --address <hex value> flash base address. Default: $ADDRESS
-e, --erase erase all sectors before flashing
-s, --start <hex value> start address after flashing. Default: $START
-o, --offset <hex value> offset from flash base ($ADDRESS) where flashing should start
Specific options for SWD protocol:
Optional:
-f, --freq <value> SWD frequency in KHz.
-m, --mode connection mode: UR (default), HOTPLUG, POWERDOWN or hwRstPulse
-n, --snum <serial number> serial number of the ST-Link device (if multiple ST-Link devices are connected)
Specific options for Serial protocol:
Mandatory:
-c, --com <name> serial identifier, ex: COM1 or /dev/ttyS0,...
Optional:
-d, --dtr <low/high> polarity of DTR signal
-p, --parity <none/even/odd> parity bit configuration ('even' by default)
-r, --rts <low/high> polarity of RTS signal ('low' by default)
Specific options for DFU protocol:
Mandatory:
-v, --vid <hex value> vendor id, ex: 0x0483
-p, --pid <hex value> product id, ex: 0xdf11
" >&2
exit "$1"
}
aborting() {
echo "STM32CubeProgrammer not found ($STM32CP_CLI).
Please install it or add '<STM32CubeProgrammer path>/bin' to your PATH environment:
https://www.st.com/en/development-tools/stm32cubeprog.html
Aborting!" >&2
exit 1
}
check_getopt() {
case "${UNAME_OS}" in
Linux* | Windows*)
GNU_GETOPT="y"
;;
Darwin*)
# Check if getopt is in the path and if it is gnu or BSD version in the path
if command -v getopt >/dev/null 2>&1; then
getopt --test >/dev/null 2>&1
if [ $? -eq 4 ]; then
GNU_GETOPT="y"
fi
fi
# If getopt is not gnu version, check if it is installed via brew
if [ "${GNU_GETOPT}" != "y" ]; then
if command -v brew >/dev/null 2>&1; then
BREW_PREFIX=$(brew --prefix)
if command -v "${BREW_PREFIX}/bin/getopt" >/dev/null 2>&1; then
"${BREW_PREFIX}"/bin/getopt --test >/dev/null 2>&1
if [ $? -eq 4 ]; then
export PATH="${BREW_PREFIX}/bin:$PATH"
GNU_GETOPT="y"
fi
fi
if [ "${GNU_GETOPT}" != "y" ]; then
if command -v "${BREW_PREFIX}/opt/gnu-getopt/bin/getopt" >/dev/null 2>&1; then
export PATH="${BREW_PREFIX}/opt/gnu-getopt/bin":"$PATH"
GNU_GETOPT="y"
fi
fi
fi
# Check for MacPorts getopt
if [ "${GNU_GETOPT}" != "y" ]; then
if command -v /opt/local/bin/getopt >/dev/null 2>&1; then
/opt/local/bin/getopt --test >/dev/null 2>&1
if [ $? -eq 4 ]; then
export PATH="/opt/local/bin:$PATH"
GNU_GETOPT="y"
fi
fi
fi
# Fallback to previous check
if [ "${GNU_GETOPT}" != "y" ]; then
if ! command -v /usr/local/opt/gnu-getopt/bin/getopt >/dev/null 2>&1; then
if command -v /opt/homebrew/opt/gnu-getopt/bin/getopt >/dev/null 2>&1; then
export PATH="/opt/homebrew/opt/gnu-getopt/bin":"$PATH"
GNU_GETOPT="y"
fi
else
export PATH="/usr/local/opt/gnu-getopt/bin":"$PATH"
GNU_GETOPT="y"
fi
fi
fi
;;
*)
echo "Unknown host OS: ${UNAME_OS}." >&2
exit 1
;;
esac
}
check_getopt
# Check STM32CubeProgrammer cli availability
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
aborting
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
aborting
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
aborting
fi
fi
;;
*)
echo "Unknown host OS: ${UNAME_OS}." >&2
exit 1
;;
esac
# parse command line arguments
# options may be followed by one colon to indicate they have a required arg
if [ "${GNU_GETOPT}" = "y" ]; then
if ! options=$(getopt -a -o hi:b:a:es:o:f:m:n:c:d:p:r:v: --long help,interface:,bin:,address:,erase,start:,offset:,freq:,mode:,snum:,com:,dtr:,parity:,rts:,pid:,vid: -- "$@"); then
echo "Terminating..." >&2
exit 1
fi
else
echo "Warning: long options not supported due to getopt from FreeBSD usage."
echo " Space in arguments is not supported."
if ! options=$(getopt hi:b:a:es:o:f:m:n:c:d:p:r:v: "$@"); then
echo "Terminating..." >&2
exit 1
fi
fi
eval set -- "$options"
while true; do
case "$1" in
-h | --help | -\?)
usage 0
;;
-i | --interface)
INTERFACE=$(echo "$2" | tr '[:upper:]' '[:lower:]')
echo "Selected interface: $INTERFACE"
shift 2
;;
-b | --bin)
FILEPATH=$2
shift 2
;;
-a | --address)
ADDRESS=$2
shift 2
;;
-e | --erase)
ERASE="--erase all"
shift 1
;;
-s | --start)
START=$2
shift 2
;;
-o | --offset)
OFFSET=$2
shift 2
;;
-f | --freq)
FREQ=$2
shift 2
;;
-m | --mode)
MODE=$2
shift 2
;;
-n | --snum)
SERIAL_NUMBER="$2"
shift 2
;;
-c | --com)
PORT=$2
shift 2
;;
-d | --dtr)
DTR=$(echo "dtr=$2" | tr '[:upper:]' '[:lower:]')
shift 2
;;
-p | --pid | --parity)
PID=$2
PARITY=$(echo "$2" | tr '[:lower:]' '[:upper:]')
shift 2
;;
-r | --rts)
RTS=$(echo "rts=$2" | tr '[:upper:]' '[:lower:]')
shift 2
;;
-v | --vid)
VID=$2
shift 2
;;
--)
shift
break
;;
*)
echo "Unknown option $1"
usage 1
;;
esac
done
ADDRESS=$(printf "0x%x" $((ADDRESS + OFFSET)))
# Check mandatory options
if [ -z "${INTERFACE}" ]; then
echo "Error missing interface!" >&2
usage 1
fi
if [ -z "${FILEPATH}" ]; then
echo "Error missing binary file argument!" >&2
usage 1
fi
if [ ! -r "${FILEPATH}" ]; then
echo "Error ${FILEPATH} does not exist!" >&2
usage 1
fi
case "${INTERFACE}" in
swd)
PORT_PARAM="port=SWD"
if [ -n "${FREQ}" ]; then
case "${FREQ}" in
0) ;;
*[!0-9]*)
echo "Invalid SWD frequency (expected integer KHz): ${FREQ}" >&2
exit 1
;;
*)
PORT_PARAM="${PORT_PARAM} freq=${FREQ}"
;;
esac
fi
if [ -n "${SERIAL_NUMBER}" ] && [ "${SERIAL_NUMBER}" != "0" ]; then
PORT_PARAM="${PORT_PARAM} sn=${SERIAL_NUMBER}"
fi
${STM32CP_CLI} --connect "${PORT_PARAM}" mode="${MODE}" "${ERASE}" --quietMode --download "${FILEPATH}" "${ADDRESS}" --start "${START}"
;;
dfu)
if [ -z "${VID}" ] || [ -z "${PID}" ]; then
echo "Missing mandatory arguments for DFU mode (VID/PID)!" >&2
exit 1
fi
${STM32CP_CLI} --connect port=usb1 VID="${VID}" PID="${PID}" "${ERASE}" --quietMode --download "${FILEPATH}" "${ADDRESS}" --start "${START}"
;;
serial)
if [ -z "${PORT}" ]; then
echo "Missing mandatory arguments for serial mode: serial identifier!" >&2
exit 1
fi
if [ -n "${RTS}" ]; then
if [ "${RTS}" != "rts=low" ] && [ "${RTS}" != "rts=high" ]; then
echo "Wrong rts value waiting high or low instead of ${RTS}" >&2
exit 1
fi
fi
if [ -n "${DTR}" ]; then
if [ "${DTR}" != "dtr=low" ] && [ "${DTR}" != "dtr=high" ]; then
echo "Wrong dtr value waiting high or low instead of ${DTR}" >&2
exit 1
fi
fi
if [ "$PARITY" != "NONE" ] && [ "$PARITY" != "EVEN" ] && [ "$PARITY" != "ODD" ]; then
echo "Wrong parity value waiting none, even or odd instead of ${PARITY}" >&2
exit 1
fi
${STM32CP_CLI} --connect port="${PORT}" P="${PARITY}" "${RTS}" "${DTR}" "${ERASE}" --quietMode --download "${FILEPATH}" "${ADDRESS}" --start "${START}"
;;
jlink)
${STM32CP_CLI} --connect port=JLINK ap=0 "${ERASE}" --quietMode --download "${FILEPATH}" "${ADDRESS}" --start "${START}"
;;
*)
echo "Protocol unknown!" >&2
usage 4
;;
esac
exit $?