Skip to content

Commit a0aeba9

Browse files
committed
[STM32CubeProg] Add support to request reset to bootloader mode
Signed-off-by: Frederic Pillon <[email protected]>
1 parent a0a4804 commit a0aeba9

File tree

1 file changed

+81
-13
lines changed

1 file changed

+81
-13
lines changed

linux/stm32CubeProg.sh

+81-13
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
#!/bin/bash
22
set -o nounset # Treat unset variables as an error
33
#set -x
4+
5+
# STM32 Cube programmer variables
46
STM32CP_CLI=STM32_Programmer.sh
57
ADDRESS=0x8000000
68
ERASE=
79
MODE=
810
PORT=
911
OPTS=
1012

13+
# Script variables
14+
SERPORT=
15+
STATUS=
16+
1117
###############################################################################
1218
## Help function
1319
usage()
@@ -24,8 +30,10 @@ usage()
2430
echo "## Ex: 10 erase all sectors using SWD interface."
2531
echo "## file_path: file path name to be downloaded: (bin, hex)"
2632
echo "## Options:"
27-
echo "## For SWD and DFU: no mandatory options"
28-
echo "## For Serial: <com_port>"
33+
echo "## For SWD: no mandatory options"
34+
echo "## For DFU: no mandatory options"
35+
echo "## Use '-serport=<com_port>' to request reset to bootloader mode"
36+
echo "## For Serial: 'serport=<com_port>'"
2937
echo "## com_port: serial identifier (mandatory). Ex: /dev/ttyS0"
3038
echo "##"
3139
echo "## Note: all trailing arguments will be passed to the $STM32CP_CLI"
@@ -37,7 +45,6 @@ usage()
3745
exit $1
3846
}
3947

40-
4148
check_tool() {
4249
command -v $STM32CP_CLI >/dev/null 2>&1
4350
if [ $? != 0 ]; then
@@ -53,6 +60,38 @@ check_tool() {
5360
fi
5461
}
5562

63+
bootloaderMode() {
64+
if [ ! -z $SERPORT ]; then
65+
# Try to configure it at 1200 to restart
66+
# in Bootloader mode
67+
if [ -c $SERPORT ]; then
68+
count=0
69+
res=1
70+
while [ $res -ne 0 ] && ((count++ < 5)); do
71+
# echo "Try to set $SERPORT at 1200"
72+
stty -F $SERPORT 1200 > /dev/null 2>&1
73+
res=$?
74+
sleep 0.1
75+
done
76+
if [ $res -eq 0 ]; then
77+
sleep 0.5
78+
fi
79+
fi
80+
fi
81+
}
82+
83+
upload() {
84+
count=0
85+
STATUS=1
86+
while [ $STATUS -ne 0 ] && ((count++ < 5)); do
87+
# echo "Try upload $count "
88+
${STM32CP_CLI} -c port=${PORT} ${MODE} ${ERASE} -q -d ${FILEPATH} ${ADDRESS} ${OPTS}
89+
STATUS=$?
90+
sleep 0.5
91+
done
92+
}
93+
94+
# Main
5695
check_tool
5796

5897
if [ $# -lt 2 ]; then
@@ -69,35 +108,64 @@ if [ $1 -ge 10 ]; then
69108
ERASE='-e all'
70109
PROTOCOL=$(($1 - 10))
71110
fi
111+
112+
# Check if serial port option available
113+
if [ $# -gt 2 ] && [[ $3 == "-serport="* ]]; then
114+
SERPORT=`echo $3 | cut -d'=' -f2`
115+
if [ ! -z $SERPORT ] && [[ $SERPORT != "/dev/"* ]]; then
116+
SERPORT="/dev/"${SERPORT}
117+
fi
118+
fi
119+
72120
# Protocol $1
73121
# 0: SWD
74122
# 1: Serial
75123
# 2: DFU
76124
case $PROTOCOL in
77125
0)
78126
PORT='SWD'
79-
MODE='mode=UR'
80-
shift 2;;
127+
MODE='mode=UR';;
81128
1)
82-
if [ $# -lt 3 ]; then
129+
if [ -z $SERPORT ]; then
130+
echo "Missing Serial port!"
83131
usage 3
84-
else
85-
PORT=$3
86-
shift 3
87-
fi;;
132+
fi
133+
PORT=$SERPORT;;
88134
2)
89135
PORT='USB1'
90-
shift 2;;
136+
bootloaderMode;;
91137
*)
92138
echo "Protocol unknown!"
93139
usage 4;;
94140
esac
95141

142+
if [ -z $SERPORT ]; then
143+
shift 2
144+
else
145+
shift 3
146+
fi
147+
96148
if [ $# -gt 0 ]; then
97149
OPTS="$@"
98150
fi
99151

100-
${STM32CP_CLI} -c port=${PORT} ${MODE} ${ERASE} -q -d ${FILEPATH} ${ADDRESS} ${OPTS}
152+
upload
153+
154+
if [ ! -z $SERPORT ] && [ $STATUS -eq 0 ]; then
155+
echo -n "Waiting for $SERPORT serial..."
156+
count=0
157+
while [ ! -c $SERPORT ] && ((count++ < 40)); do
158+
sleep 0.1
159+
done
160+
count=0
161+
res=1
162+
while [ $res -ne 0 ] && ((count++ < 20)); do
163+
stty -F $SERPORT > /dev/null 2>&1
164+
res=$?
165+
sleep 1
166+
done
167+
echo "done"
168+
fi
101169

102-
exit 0
170+
exit $STATUS
103171

0 commit comments

Comments
 (0)