forked from uos3/obc-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflash
More file actions
executable file
·218 lines (190 loc) · 4.98 KB
/
Copy pathflash
File metadata and controls
executable file
·218 lines (190 loc) · 4.98 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
#!/usr/bin/env bash
## Cross-Platform Embedded Toolchain
## Copyright (c) 2016 Phil Crump <phil@philcrump.co.uk>
## Freely distributable under the MIT license.
source_dir="$(cd $(dirname ${BASH_SOURCE[0]}) && pwd)"
cd $source_dir
host_platform='unknown'
unamestr=`uname`
if [[ "$unamestr" == 'Linux' ]]; then
host_platform='linux'
elif [[ "$unamestr" == 'Darwin' ]]; then
host_platform='mac'
else
host_platform='windows'
fi
COLOUR_GREEN='\033[0;32m'
COLOUR_YELLOW='\033[1;33m'
COLOUR_PURPLE='\033[0;35m'
COLOUR_RED='\033[0;31m'
COLOUR_OFF='\033[0;00m'
CLEAR_LINE='\033[2K'
_ERROR_="$COLOUR_RED[ERROR]$COLOUR_OFF"
_INFO_="$COLOUR_PURPLE[INFO]$COLOUR_OFF"
_DEBUG_="$COLOUR_YELLOW[DEBUG]$COLOUR_OFF"
opt_verbose=false
opt_quiet=false
opt_sound=false
opt_power=false
stdin="/proc/$$/fd/0"
stdout="/proc/$$/fd/1"
stderr="/proc/$$/fd/2"
stdnull="/dev/null"
spinner() {
case $spinner_char in
'/') echo '-';;
'|') echo '/';;
'\\') echo '|';;
'-') echo '\\';;
*) echo '-';;
esac
return 0
}
playsound() {
if $opt_sound
then
if hash aplay 2>/dev/null
then
aplay -q $@
elif hash play 2>/dev/null
then
play -q $@
else
echo -e "$_ERROR_ No .wav player detected" >&$stderr
fi
fi
}
locate_black_magic() {
probe="`ls 2>/dev/null -Ub1 -- /dev/serial/by-id/usb-Black_Sphere_Technologies_Black_Magic_Probe_*-if00 | head -n 1`"
if ! [ -z "$probe" ]
then
echo "$probe"
return 0
fi
return 1
}
#
wait_for_black_magic() {
until programmer="`locate_black_magic`"; do
spinner_char=`spinner`
echo -ne "$_INFO_$COLOUR_YELLOW Looking for programmer... $spinner_char$COLOUR_OFF\r" >&$stdout
sleep 0.2
done
echo "$programmer"
}
flash_device() {
local flashtool="$1"
local programmer="$2"
local binary="$3"
programmer_power="disable"
if $opt_power; then
programmer_power="enable";
fi
flash_cmd="$flashtool -q --batch \
-ex 'file '$binary \
-ex 'target extended-remote '$programmer \
-ex 'monitor tpwr $programmer_power' \
-ex 'monitor swdp_scan' \
-ex 'attach 1' \
-ex 'load '$binary \
-ex 'compare-sections'"
flash_success=false
while ! ${flash_success}
do
spinner_char=`spinner`
echo -ne "$_INFO_$COLOUR_YELLOW Scanning for board.. $spinner_char$COLOUR_OFF\r";
flash_out=`eval $flash_cmd 2>&1`;
echo ${flash_out} | grep -vqE 'warning|fail|Error|MIS-MATCHED'
if [[ $? -eq 0 ]]; then
flash_success=true
echo -e "\n${flash_out}"
else
if $opt_verbose; then
echo ${flash_out};
fi
sleep 0.2
fi
done
}
# For Linux - check user has permissions to the serial port (required for Black Magic Probe)
if [ ${host_platform} == "linux" ]; then
groups $USER | grep -q 'dialout'
if [[ ! $? -eq 0 ]]; then
echo -e "$_ERROR_ Current user is not in dialout group, you'll need this to use the programmer!" >&$stderr
echo -e " * eg. for Ubuntu/Debian: 'sudo gpasswd --add "$USER" dialout'" >&$stderr
exit 1
fi
fi
## Read Flags
OPTIND=1
while getopts ":qvSP" opt; do
case "$opt" in
q) # Quiet
opt_quiet=true
;;
v) # Verbose
opt_verbose=true
;;
S) # Sound
opt_sound=true
;;
P) # Sound
opt_power=true
;;
?) # Illegal Option
echo -e "$_ERROR_ Illegal option '$OPTARG'"
exit 3
;;
esac
done
for i in `seq 2 $OPTIND`; do shift; done
binary="$1"
flashtool="arm-none-eabi-gdb"
if [ -z "$binary" ]; then
binary="builds/$(ls -t -I "*.*" builds/ | head -1)"
echo -e "$_INFO_ Most recent build selected: ${binary}" >&$stdout
fi
if [[ ! -f $binary ]]; then
echo -e "$_ERROR_ Binary $binary not found!" >&$stderr
exit 1
fi
if ! $opt_quiet; then
echo -e "$_INFO_ Binary: ${binary}" >&$stdout
fi
if ! hash "$flashtool" 2>/dev/null; then
echo -e "$_ERROR_ $flashtool not found!" >&$stderr
exit 1
fi
if ! $opt_quiet; then
echo -e "$_INFO_ Flash Tool: arm-none-eabi-gdb" >&$stdout
fi
if [ ${host_platform} == "linux" ]; then
if ! programmer="`wait_for_black_magic`"; then
exit 1
fi
elif [ ${host_platform} == "windows" ]; then
if ! [ -f "COM_PORT" ]; then
echo -e "$_ERROR_ Windows users need to set the BMP COM port in a file called 'COM_PORT'."
echo -e " eg. 'COM8'"
return 1
fi
programmer=$(<COM_PORT)
if [ -z "$programmer" ]; then
echo -e "$_ERROR_ BMP COM port not found in 'COM_PORT' file."
echo -e " eg. 'COM8'"
return 1
fi
elif [ ${host_platform} == "mac" ]; then
echo -e "$_ERROR_ Not implemented for mac yet.."
return 1
fi
echo -e "$_INFO_$COLOUR_GREEN Found Blackmagic$COLOUR_OFF" >&$stdout
if ! $opt_quiet; then
echo -e "$_INFO_ Programmer: $programmer" >&$stdout
fi
if $opt_power; then
echo -e "$_INFO_ Programmer Power:$COLOUR_RED ON $COLOUR_OFF"
fi
flash_device "$flashtool" "$programmer" "$binary"
echo -e "$CLEAR_LINE$_INFO_$COLOUR_GREEN Board Flashed$COLOUR_OFF" >&$stdout
playsound tools/flash.wav