Skip to content

Commit b5d9016

Browse files
committed
feat: unified maple_upload and dfu_util scripts
shellcheck and shfmt applied Signed-off-by: Frederic Pillon <[email protected]>
1 parent d702d73 commit b5d9016

File tree

6 files changed

+142
-169
lines changed

6 files changed

+142
-169
lines changed

dfu-util.sh

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/sh -
2+
#
3+
# Use the correct dfu-util program based on the architecture
4+
#
5+
6+
# Get the directory where the script is running.
7+
DIR=$(cd "$(dirname "$0")" && pwd)
8+
UNAME_OS="$(uname -s)"
9+
case "${UNAME_OS}" in
10+
Linux*)
11+
# Choose dfu program by arch
12+
if [ "$(uname -m)" = "x86_64" ]; then
13+
DFU_UTIL=${DIR}/linux/dfu-util_x86_64/dfu-util
14+
else
15+
DFU_UTIL=${DIR}/linux/dfu-util/dfu-util
16+
fi
17+
;;
18+
Darwin*)
19+
DFU_UTIL=${DIR}/macosx/dfu-util/dfu-util
20+
if [ ! -x "${DFU_UTIL}" ]; then
21+
DFU_UTIL=/opt/local/bin/dfu-util
22+
fi
23+
;;
24+
Windows*)
25+
DFU_UTIL=${DIR}/win/dfu-util.exe
26+
;;
27+
*)
28+
echo "Unknown host OS: ${UNAME_OS}."
29+
exit 1
30+
;;
31+
esac
32+
33+
# Not found!
34+
if [ ! -x "${DFU_UTIL}" ]; then
35+
echo "$0: error: cannot find ${DFU_UTIL}" >&2
36+
exit 2
37+
fi
38+
39+
# Pass all parameters through
40+
"${DFU_UTIL}" "$@"

linux/dfu-util.sh

-23
This file was deleted.

linux/maple_upload.sh

-64
This file was deleted.

macosx/dfu-util.sh

-18
This file was deleted.

macosx/maple_upload.sh

-64
This file was deleted.

maple_upload.sh

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/bin/sh -
2+
3+
set -e
4+
5+
if [ $# -lt 4 ]; then
6+
echo "Usage: $0 <dummy_port> <altID> <usbID> <binfile>" >&2
7+
exit 1
8+
fi
9+
altID="$2"
10+
usbID="$3"
11+
binfile="$4"
12+
EXT=""
13+
14+
UNAME_OS="$(uname -s)"
15+
case "${UNAME_OS}" in
16+
Linux*)
17+
dummy_port_fullpath="/dev/$1"
18+
OS_DIR="linux"
19+
;;
20+
Darwin*)
21+
dummy_port_fullpath="/dev/$1"
22+
OS_DIR="macosx"
23+
;;
24+
Windows*)
25+
dummy_port_fullpath="$1"
26+
OS_DIR="win"
27+
EXT=".exe"
28+
;;
29+
*)
30+
echo "Unknown host OS: ${UNAME_OS}."
31+
exit 1
32+
;;
33+
esac
34+
35+
# Get the directory where the script is running.
36+
DIR=$(cd "$(dirname "$0")" && pwd)
37+
38+
# ----------------- IMPORTANT -----------------
39+
# The 2nd parameter to upload_reset is the delay after resetting
40+
# before it exits. This value is in milliseonds.
41+
# You may need to tune this to your system
42+
# 750ms to 1500ms seems to work on my Mac
43+
# This is less critical now that we automatically retry dfu-util
44+
45+
if ! "${DIR}/${OS_DIR}/upload_reset${EXT}" "${dummy_port_fullpath}" 750; then
46+
echo "****************************************" >&2
47+
echo "* Could not automatically reset device *" >&2
48+
echo "* Please manually reset device! *" >&2
49+
echo "****************************************" >&2
50+
sleep 2 # Wait for user to see message.
51+
fi
52+
53+
COUNTER=5
54+
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
60+
ret=$?
61+
do
62+
if [ $ret -eq 0 ]; then
63+
break
64+
else
65+
COUNTER=$((COUNTER - 1))
66+
if [ $ret -eq 74 ] && [ $COUNTER -gt 0 ]; then
67+
# I/O error, probably because no DFU device was found
68+
echo "Trying ${COUNTER} more time(s)" >&2
69+
sleep 1
70+
else
71+
exit $ret
72+
fi
73+
fi
74+
done
75+
76+
printf "Waiting for %s serial..." "${dummy_port_fullpath}" >&2
77+
COUNTER=40
78+
if [ ${OS_DIR} = "win" ]; then
79+
while [ $COUNTER -gt 0 ]; do
80+
if ! "${DIR}/${OS_DIR}/check_port${EXT}" "${dummy_port_fullpath}"; then
81+
COUNTER=$((COUNTER - 1))
82+
printf "." >&2
83+
sleep 0.1
84+
else
85+
break
86+
fi
87+
done
88+
89+
else
90+
while [ ! -r "${dummy_port_fullpath}" ] && [ $COUNTER -gt 0 ]; do
91+
COUNTER=$((COUNTER - 1))
92+
printf "." >&2
93+
sleep 0.1
94+
done
95+
fi
96+
97+
if [ $COUNTER -eq -0 ]; then
98+
echo " Timed out." >&2
99+
exit 1
100+
else
101+
echo " Done." >&2
102+
fi

0 commit comments

Comments
 (0)