Skip to content

Commit 775cb47

Browse files
committed
[STM32CubeProg] Wait for serial port availability
Signed-off-by: Frederic Pillon <[email protected]>
1 parent 31f3d43 commit 775cb47

File tree

1 file changed

+61
-14
lines changed

1 file changed

+61
-14
lines changed

linux/stm32CubeProg.sh

+61-14
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,10 +30,12 @@ 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 "## For Serial: 'serport=<com_port>'"
2936
echo "## com_port: serial identifier (mandatory). Ex: /dev/ttyS0"
3037
echo "##"
38+
echo "## '-serport=<com_port>' is also used to wait the serial port availability"
3139
echo "## Note: all trailing arguments will be passed to the $STM32CP_CLI"
3240
echo "## They have to be valid commands for STM32 MCU"
3341
echo "## Ex: -g: Run the code at the specified address"
@@ -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,18 @@ check_tool() {
5360
fi
5461
}
5562

63+
upload() {
64+
count=0
65+
STATUS=1
66+
while [ $STATUS -ne 0 ] && ((count++ < 5)); do
67+
# echo "Try upload $count "
68+
${STM32CP_CLI} -c port=${PORT} ${MODE} ${ERASE} -q -d ${FILEPATH} ${ADDRESS} ${OPTS}
69+
STATUS=$?
70+
sleep 0.5
71+
done
72+
}
73+
74+
# Main
5675
check_tool
5776

5877
if [ $# -lt 2 ]; then
@@ -69,35 +88,63 @@ if [ $1 -ge 10 ]; then
6988
ERASE='-e all'
7089
PROTOCOL=$(($1 - 10))
7190
fi
91+
92+
# Check if serial port option available
93+
if [ $# -gt 2 ] && [[ $3 == "-serport="* ]]; then
94+
SERPORT=`echo $3 | cut -d'=' -f2`
95+
if [ ! -z $SERPORT ] && [[ $SERPORT != "/dev/"* ]]; then
96+
SERPORT="/dev/"${SERPORT}
97+
fi
98+
fi
99+
72100
# Protocol $1
73101
# 0: SWD
74102
# 1: Serial
75103
# 2: DFU
76104
case $PROTOCOL in
77105
0)
78106
PORT='SWD'
79-
MODE='mode=UR'
80-
shift 2;;
107+
MODE='mode=UR';;
81108
1)
82-
if [ $# -lt 3 ]; then
109+
if [ -z $SERPORT ]; then
110+
echo "Missing Serial port!"
83111
usage 3
84-
else
85-
PORT=$3
86-
shift 3
87-
fi;;
112+
fi
113+
PORT=$SERPORT;;
88114
2)
89-
PORT='USB1'
90-
shift 2;;
115+
PORT='USB1';;
91116
*)
92117
echo "Protocol unknown!"
93118
usage 4;;
94119
esac
95120

121+
if [ -z $SERPORT ]; then
122+
shift 2
123+
else
124+
shift 3
125+
fi
126+
96127
if [ $# -gt 0 ]; then
97128
OPTS="$@"
98129
fi
99130

100-
${STM32CP_CLI} -c port=${PORT} ${MODE} ${ERASE} -q -d ${FILEPATH} ${ADDRESS} ${OPTS}
131+
upload
132+
133+
if [ ! -z $SERPORT ] && [ $STATUS -eq 0 ]; then
134+
echo -n "Waiting for $SERPORT serial..."
135+
count=0
136+
while [ ! -c $SERPORT ] && ((count++ < 40)); do
137+
sleep 0.1
138+
done
139+
count=0
140+
res=1
141+
while [ $res -ne 0 ] && ((count++ < 20)); do
142+
stty -F $SERPORT > /dev/null 2>&1
143+
res=$?
144+
sleep 1
145+
done
146+
echo "done"
147+
fi
101148

102-
exit 0
149+
exit $STATUS
103150

0 commit comments

Comments
 (0)