1
1
#! /bin/bash
2
2
set -o nounset # Treat unset variables as an error
3
3
# set -x
4
+
5
+ # STM32 Cube programmer variables
4
6
STM32CP_CLI=STM32_Programmer.sh
5
7
ADDRESS=0x8000000
6
8
ERASE=
7
9
MODE=
8
10
PORT=
9
11
OPTS=
10
12
13
+ # Script variables
14
+ SERPORT=
15
+ STATUS=
16
+
11
17
# ##############################################################################
12
18
# # Help function
13
19
usage ()
@@ -24,8 +30,10 @@ usage()
24
30
echo " ## Ex: 10 erase all sectors using SWD interface."
25
31
echo " ## file_path: file path name to be downloaded: (bin, hex)"
26
32
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>'"
29
37
echo " ## com_port: serial identifier (mandatory). Ex: /dev/ttyS0"
30
38
echo " ##"
31
39
echo " ## Note: all trailing arguments will be passed to the $STM32CP_CLI "
@@ -37,7 +45,6 @@ usage()
37
45
exit $1
38
46
}
39
47
40
-
41
48
check_tool () {
42
49
command -v $STM32CP_CLI > /dev/null 2>&1
43
50
if [ $? != 0 ]; then
@@ -53,6 +60,38 @@ check_tool() {
53
60
fi
54
61
}
55
62
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
56
95
check_tool
57
96
58
97
if [ $# -lt 2 ]; then
@@ -69,35 +108,64 @@ if [ $1 -ge 10 ]; then
69
108
ERASE=' -e all'
70
109
PROTOCOL=$(( $1 - 10 ))
71
110
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
+
72
120
# Protocol $1
73
121
# 0: SWD
74
122
# 1: Serial
75
123
# 2: DFU
76
124
case $PROTOCOL in
77
125
0)
78
126
PORT=' SWD'
79
- MODE=' mode=UR'
80
- shift 2;;
127
+ MODE=' mode=UR' ;;
81
128
1)
82
- if [ $# -lt 3 ]; then
129
+ if [ -z $SERPORT ]; then
130
+ echo " Missing Serial port!"
83
131
usage 3
84
- else
85
- PORT=$3
86
- shift 3
87
- fi ;;
132
+ fi
133
+ PORT=$SERPORT ;;
88
134
2)
89
135
PORT=' USB1'
90
- shift 2 ;;
136
+ bootloaderMode ;;
91
137
* )
92
138
echo " Protocol unknown!"
93
139
usage 4;;
94
140
esac
95
141
142
+ if [ -z $SERPORT ]; then
143
+ shift 2
144
+ else
145
+ shift 3
146
+ fi
147
+
96
148
if [ $# -gt 0 ]; then
97
149
OPTS=" $@ "
98
150
fi
99
151
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
101
169
102
- exit 0
170
+ exit $STATUS
103
171
0 commit comments