Skip to content

Commit 43e5896

Browse files
authored
Merge pull request #97 from fpistm/Arduino_compatibility
refactor(stm32CubeProg): use getopt and extend options
2 parents 0cbc4fc + ed7bc74 commit 43e5896

File tree

2 files changed

+164
-101
lines changed

2 files changed

+164
-101
lines changed

maple_upload.sh

+12-13
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
set -e
44

5-
if [ $# -lt 4 ]; then
6-
echo "Usage: $0 <dummy_port> <altID> <usbID> <binfile>" >&2
5+
if [ $# -lt 5 ]; then
6+
echo "Usage: $0 <dummy_port> <altID> <usbVID> <usbPID> <binfile>" >&2
77
exit 1
88
fi
99
altID="$2"
10-
usbID="$3"
11-
binfile="$4"
10+
usbVID=${3#"0x"}
11+
usbPID=${4#"0x"}
12+
binfile="$5"
1213
EXT=""
1314

1415
UNAME_OS="$(uname -s)"
@@ -52,11 +53,7 @@ fi
5253

5354
COUNTER=5
5455
while
55-
if [ $# -eq 5 ]; then
56-
"${DIR}/dfu-util.sh" -d "${usbID}" -a "${altID}" -D "${binfile}" "--dfuse-address $5" -R
57-
else
58-
"${DIR}/dfu-util.sh" -d "${usbID}" -a "${altID}" -D "${binfile}" -R
59-
fi
56+
"${DIR}/dfu-util.sh" -d "${usbVID}:${usbPID}" -a "${altID}" -D "${binfile}" -R
6057
ret=$?
6158
do
6259
if [ $ret -eq 0 ]; then
@@ -73,13 +70,15 @@ do
7370
fi
7471
done
7572

76-
printf "Waiting for %s serial..." "${dummy_port_fullpath}" >&2
73+
sleep 1
74+
75+
printf "Waiting for %s serial..." "${dummy_port_fullpath}"
7776
COUNTER=40
7877
if [ ${OS_DIR} = "win" ]; then
7978
while [ $COUNTER -gt 0 ]; do
8079
if ! "${DIR}/${OS_DIR}/check_port${EXT}" "${dummy_port_fullpath}"; then
8180
COUNTER=$((COUNTER - 1))
82-
printf "." >&2
81+
printf "."
8382
sleep 0.1
8483
else
8584
break
@@ -89,7 +88,7 @@ if [ ${OS_DIR} = "win" ]; then
8988
else
9089
while [ ! -r "${dummy_port_fullpath}" ] && [ $COUNTER -gt 0 ]; do
9190
COUNTER=$((COUNTER - 1))
92-
printf "." >&2
91+
printf "."
9392
sleep 0.1
9493
done
9594
fi
@@ -98,5 +97,5 @@ if [ $COUNTER -eq -0 ]; then
9897
echo " Timed out." >&2
9998
exit 1
10099
else
101-
echo " Done." >&2
100+
echo " Done."
102101
fi

stm32CubeProg.sh

+152-88
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,143 @@
11
#!/bin/sh -
22
set -o nounset # Treat unset variables as an error
3-
# set -o xtrace # Print command traces before executing command.
3+
# set -o xtrace # Print command traces before executing command.
44

55
STM32CP_CLI=
6+
INTERFACE=
7+
PORT=
8+
FILEPATH=
69
ADDRESS=0x8000000
7-
ERASE=""
8-
MODE=""
9-
PORT=""
10-
OPTS=""
10+
OFFSET=0x0
11+
# Optional
12+
ERASE=
13+
# Optional for Serial
14+
RTS=
15+
DTR=
16+
# Mandatory for DFU
17+
VID=
18+
PID=
1119

1220
###############################################################################
1321
## Help function
1422
usage() {
15-
echo "############################################################"
16-
echo "##"
17-
echo "## $(basename "$0") <protocol> <file_path> <offset> [OPTIONS]"
18-
echo "##"
19-
echo "## protocol:"
20-
echo "## 0: SWD"
21-
echo "## 1: Serial"
22-
echo "## 2: DFU"
23-
echo "## Note: prefix it by 1 to erase all sectors."
24-
echo "## Ex: 10 erase all sectors using SWD interface."
25-
echo "## file_path: file path name to be downloaded: (bin, hex)"
26-
echo "## offset: offset to add to $ADDRESS"
27-
echo "## Options:"
28-
echo "## For SWD and DFU: no mandatory options"
29-
echo "## For Serial: <com_port>"
30-
echo "## com_port: serial identifier (mandatory). Ex: /dev/ttyS0 or COM1"
31-
echo "##"
32-
echo "## Note: all trailing arguments will be passed to the $STM32CP_CLI"
33-
echo "## They have to be valid commands for STM32CubeProgrammer cli"
34-
echo "## Ex: -rst: Reset system"
35-
echo "############################################################"
23+
echo "Usage: $(basename "$0") [OPTIONS]...
24+
25+
Mandatory options:
26+
-i, --interface <'swd'/'dfu'/'serial'> interface identifier: 'swd', 'dfu' or 'serial'
27+
-f, --file <path> file path to be downloaded: bin or hex
28+
Optional options:
29+
-e, --erase erase all sectors before flashing
30+
-o, --offset <hex value> offset from flash base ($ADDRESS) where flashing should start
31+
32+
Specific options for Serial protocol:
33+
Mandatory:
34+
-c, --com <name> serial identifier, ex: COM1 or /dev/ttyS0,...
35+
Optional:
36+
-r, --rts <low/high> polarity of RTS signal ('low' by default)
37+
-d, --dtr <low/high> polarity of DTR signal
38+
39+
Specific options for DFU protocol:
40+
Mandatory:
41+
-v, --vid <hex value> vendor id, ex: 0x0483
42+
-p, --pid <hex value> product id, ex: 0xdf11
43+
44+
" >&2
3645
exit "$1"
3746
}
3847

48+
aborting() {
49+
echo "STM32CubeProgrammer not found ($STM32CP_CLI).
50+
Please install it or add '<STM32CubeProgrammer path>/bin' to your PATH environment:
51+
https://www.st.com/en/development-tools/stm32cubeprog.html
52+
Aborting!" >&2
53+
exit 1
54+
}
55+
56+
# parse command line arguments
57+
# options may be followed by one colon to indicate they have a required arg
58+
if ! options=$(getopt -a -o hi:ef:o:c:r:d:v:p: --long help,interface:,erase,file:,offset:,com:,rts:,dtr:,vid:,pid: -- "$@"); then
59+
echo "Terminating..." >&2
60+
exit 1
61+
fi
62+
63+
eval set -- "$options"
64+
65+
while true; do
66+
case "$1" in
67+
-h | --help | -\?)
68+
usage 0
69+
;;
70+
-i | --interface)
71+
INTERFACE=$(echo "$2" | tr '[:upper:]' '[:lower:]')
72+
echo "Selected interface: $INTERFACE"
73+
shift 2
74+
;;
75+
-e | --erase)
76+
ERASE="--erase all"
77+
shift 1
78+
;;
79+
-f | --file)
80+
FILEPATH=$2
81+
shift 2
82+
;;
83+
-o | --offset)
84+
OFFSET=$2
85+
ADDRESS=$(printf "0x%x" $((ADDRESS + OFFSET)))
86+
shift 2
87+
;;
88+
-c | --com)
89+
PORT=$2
90+
shift 2
91+
;;
92+
-r | --rts)
93+
RTS=$(echo "rts=$2" | tr '[:upper:]' '[:lower:]')
94+
shift 2
95+
;;
96+
-d | --dtr)
97+
DTR=$(echo "dtr=$2" | tr '[:upper:]' '[:lower:]')
98+
shift 2
99+
;;
100+
-v | --vid)
101+
VID=$2
102+
shift 2
103+
;;
104+
-p | --pid)
105+
PID=$2
106+
shift 2
107+
;;
108+
--)
109+
shift
110+
break
111+
;;
112+
esac
113+
done
114+
# Check STM32CubeProgrammer cli availability, fallback to dfu-util if protocol dfu
39115
UNAME_OS="$(uname -s)"
40116
case "${UNAME_OS}" in
41117
Linux*)
42118
STM32CP_CLI=STM32_Programmer.sh
43-
if ! command -v $STM32CP_CLI > /dev/null 2>&1; then
119+
if ! command -v $STM32CP_CLI >/dev/null 2>&1; then
44120
export PATH="$HOME/STMicroelectronics/STM32Cube/STM32CubeProgrammer/bin":"$PATH"
45121
fi
46-
if ! command -v $STM32CP_CLI > /dev/null 2>&1; then
122+
if ! command -v $STM32CP_CLI >/dev/null 2>&1; then
47123
export PATH="/opt/stm32cubeprog/bin":"$PATH"
48124
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
125+
if ! command -v $STM32CP_CLI >/dev/null 2>&1; then
126+
aborting
55127
fi
56128
;;
57129
Darwin*)
58130
STM32CP_CLI=STM32_Programmer_CLI
59-
if ! command -v $STM32CP_CLI > /dev/null 2>&1; then
131+
if ! command -v $STM32CP_CLI >/dev/null 2>&1; then
60132
export PATH="/Applications/STMicroelectronics/STM32Cube/STM32CubeProgrammer/STM32CubeProgrammer.app/Contents/MacOs/bin":"$PATH"
61133
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
134+
if ! command -v $STM32CP_CLI >/dev/null 2>&1; then
135+
aborting
68136
fi
69137
;;
70138
Windows*)
71139
STM32CP_CLI=STM32_Programmer_CLI.exe
72-
if ! command -v $STM32CP_CLI > /dev/null 2>&1; then
140+
if ! command -v $STM32CP_CLI >/dev/null 2>&1; then
73141
if [ -n "${PROGRAMFILES+x}" ]; then
74142
STM32CP86=${PROGRAMFILES}/STMicroelectronics/STM32Cube/STM32CubeProgrammer/bin
75143
export PATH="${STM32CP86}":"$PATH"
@@ -78,69 +146,65 @@ case "${UNAME_OS}" in
78146
STM32CP=${PROGRAMW6432}/STMicroelectronics/STM32Cube/STM32CubeProgrammer/bin
79147
export PATH="${STM32CP}":"$PATH"
80148
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!"
149+
if ! command -v $STM32CP_CLI >/dev/null 2>&1; then
150+
aborting
86151
fi
87152
fi
88153
;;
89154
*)
90-
echo "Unknown host OS: ${UNAME_OS}."
155+
echo "Unknown host OS: ${UNAME_OS}." >&2
91156
exit 1
92157
;;
93158
esac
94159

95-
if [ $# -lt 3 ]; then
96-
echo "Not enough arguments!"
97-
usage 2
160+
# Check mandatory options
161+
if [ -z "${INTERFACE}" ]; then
162+
echo "Error missing interface!" >&2
163+
usage 1
98164
fi
99-
100-
# Parse options
101-
PROTOCOL=$1
102-
FILEPATH=$2
103-
OFFSET=$3
104-
ADDRESS=$(printf "0x%x" $((ADDRESS + OFFSET)))
105-
106-
# Protocol $1
107-
# 1x: Erase all sectors
108-
if [ "$1" -ge 10 ]; then
109-
ERASE="yes"
110-
PROTOCOL=$(($1 - 10))
165+
if [ -z "${FILEPATH}" ]; then
166+
echo "Error missing file argmument!" >&2
167+
usage 1
111168
fi
112-
# Protocol $1
113-
# 0: SWD
114-
# 1: Serial
115-
# 2: DFU
116-
case $PROTOCOL in
117-
0)
118-
PORT="SWD"
119-
MODE="mode=UR"
120-
shift 3
169+
if [ ! -r "${FILEPATH}" ]; then
170+
echo "Error ${FILEPATH} does not exist!" >&2
171+
usage 1
172+
fi
173+
174+
case "${INTERFACE}" in
175+
swd)
176+
${STM32CP_CLI} --connect port=SWD mode=UR "${ERASE}" --quietMode --download "${FILEPATH}" "${ADDRESS}" --start "${ADDRESS}"
121177
;;
122-
1)
123-
if [ $# -lt 4 ]; then
124-
usage 3
125-
else
126-
PORT=$4
127-
shift 4
178+
dfu)
179+
if [ -z "${VID}" ] || [ -z "${PID}" ]; then
180+
echo "Missing mandatory arguments for DFU mode (VID/PID)!" >&2
181+
exit 1
128182
fi
183+
${STM32CP_CLI} --connect port=usb1 VID="${VID}" PID="${PID}" "${ERASE}" --quietMode --download "${FILEPATH}" "${ADDRESS}" --start "${ADDRESS}"
129184
;;
130-
2)
131-
PORT="USB1"
132-
shift 3
185+
serial)
186+
if [ -z "${PORT}" ]; then
187+
echo "Missing mandatory arguments for serial mode: serial identifier!" >&2
188+
exit 1
189+
fi
190+
if [ -n "${RTS}" ]; then
191+
if [ "${RTS}" != "rts=low" ] && [ "${RTS}" != "rts=high" ]; then
192+
echo "Wrong rts value waiting high or low instead of ${RTS}" >&2
193+
exit 1
194+
fi
195+
fi
196+
if [ -n "${DTR}" ]; then
197+
if [ "${DTR}" != "dtr=low" ] && [ "${DTR}" != "dtr=high" ]; then
198+
echo "Wrong dtr value waiting high or low instead of ${DTR}" >&2
199+
exit 1
200+
fi
201+
fi
202+
${STM32CP_CLI} --connect port="${PORT}" "${RTS}" "${DTR}" "${ERASE}" --quietMode --download "${FILEPATH}" "${ADDRESS}" --start "${ADDRESS}"
133203
;;
134204
*)
135-
echo "Protocol unknown!"
205+
echo "Protocol unknown!" >&2
136206
usage 4
137207
;;
138208
esac
139209

140-
if [ $# -gt 0 ]; then
141-
OPTS="$*"
142-
fi
143-
144-
${STM32CP_CLI} -c port=${PORT} ${MODE} ${ERASE:+"-e all"} -q -d "${FILEPATH}" "${ADDRESS}" -s "${ADDRESS}" "${OPTS}"
145-
146210
exit $?

0 commit comments

Comments
 (0)