Skip to content

[maple_upload] Improve script on linux and macosx #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions linux/maple_upload.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

#set -e
set -e

if [ $# -lt 4 ]; then
echo "Usage: $0 $# <dummy_port> <altID> <usbID> <binfile>" >&2
Expand All @@ -24,16 +24,41 @@ DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
# This value is in milliseonds
# You may need to tune this to your system
# 750ms to 1500ms seems to work on my Mac
# This is less critical now that we automatically retry dfu-util

"${DIR}/upload-reset" "${dummy_port_fullpath}" 750
if ! "${DIR}/upload-reset" "${dummy_port_fullpath}" 750; then
echo "****************************************" >&2
echo "* Could not automatically reset device *" >&2
echo "* Please manually reset device! *" >&2
echo "****************************************" >&2
sleep 2 # Wait for user to see message.
fi

"${DIR}/dfu-util.sh" -d "${usbID}" -a "${altID}" -D "${binfile}" ${dfuse_addr} -R
COUNTER=5
while
"${DIR}/dfu-util.sh" -d "${usbID}" -a "${altID}" -D "${binfile}" ${dfuse_addr} -R
((ret = $?))
do
if [ $ret -eq 74 ] && [ $((--COUNTER)) -gt 0 ]; then
# I/O error, probably because no DFU device was found
echo "Trying ${COUNTER} more time(s)" >&2
sleep 1
else
exit $ret
fi
done

echo -n Waiting for "${dummy_port_fullpath}" serial...
echo -n "Waiting for ${dummy_port_fullpath} serial..." >&2

COUNTER=0
while [ ! -r "${dummy_port_fullpath}" ] && ((COUNTER++ < 40)); do
COUNTER=40
while [ ! -r "${dummy_port_fullpath}" ] && ((COUNTER--)); do
echo -n "." >&2
sleep 0.1
done

echo Done
if [ $COUNTER -eq -1 ]; then
echo " Timed out." >&2
exit 1
else
echo " Done." >&2
fi
18 changes: 18 additions & 0 deletions macosx/dfu-util.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

# Get the directory where the script is running.
DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)

DFU_UTIL=${DIR}/dfu-util/dfu-util
if [ ! -x "${DFU_UTIL}" ]; then
DFU_UTIL=/opt/local/bin/dfu-util
fi

# Not found!
if [ ! -x "${DFU_UTIL}" ]; then
echo "$0: error: cannot find ${DFU_UTIL}" >&2
exit 2
fi

# Pass all parameters through
"${DFU_UTIL}" "$@"
75 changes: 38 additions & 37 deletions macosx/maple_upload.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,58 +6,59 @@ if [ $# -lt 4 ]; then
echo "Usage: $0 $# <dummy_port> <altID> <usbID> <binfile>" >&2
exit 1
fi
altID=$2
usbID=$3
binfile=$4
altID="$2"
usbID="$3"
binfile="$4"
dummy_port_fullpath="/dev/$1"
if [ $# -eq 5 ]; then
dfuse_addr="--dfuse-address $5"
else
dfuse_addr=""
fi

# Get the directory where the script is running.
DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)

# ----------------- Old code to reset the USB - which doesn't seem to work --------
#
#if we can find the Serial device try resetting it and then sleeping for 1 sec while the board reboots
#if [ -e $dummy_port_fullpath ]; then
# echo "resetting " $dummy_port_fullpath
# stty -f $dummy_port_fullpath 1200
# sleep 1
## stty -f $dummy_port_fullpath 1200
## sleep 1
#fi
# ------------------ End of old code -----------------

# ----------------- IMPORTANT -----------------
# The 2nd parameter to upload-reset is the delay after resetting before it exits
# This value is in milliseonds
# You may need to tune this to your system
# 750ms to 1500ms seems to work on my Mac
# This is less critical now that we automatically retry dfu-util

"${DIR}"/upload-reset "${dummy_port_fullpath}" 750

if [ $# -eq 5 ]; then
dfuse_addr="--dfuse-address $5"
else
dfuse_addr=""
fi

#DFU_UTIL=/usr/local/bin/dfu-util
DFU_UTIL=${DIR}/dfu-util/dfu-util
if [ ! -x "${DFU_UTIL}" ]; then
DFU_UTIL=/opt/local/bin/dfu-util
fi

if [ ! -x ${DFU_UTIL} ]; then
echo "$0: error: cannot find ${DFU_UTIL}" >&2
exit 2
if ! "${DIR}/upload-reset" "${dummy_port_fullpath}" 750; then
echo "****************************************" >&2
echo "* Could not automatically reset device *" >&2
echo "* Please manually reset device! *" >&2
echo "****************************************" >&2
sleep 2 # Wait for user to see message.
fi

${DFU_UTIL} -d "${usbID}" -a "${altID}" -D "${binfile}" -R ${dfuse_addr} -R
COUNTER=5
while
"${DIR}/dfu-util.sh" -d "${usbID}" -a "${altID}" -D "${binfile}" ${dfuse_addr} -R
((ret = $?))
do
if [ $ret -eq 74 ] && [ $((--COUNTER)) -gt 0 ]; then
# I/O error, probably because no DFU device was found
echo "Trying ${COUNTER} more time(s)" >&2
sleep 1
else
exit $ret
fi
done

echo -n Waiting for "${dummy_port_fullpath}" serial...
echo -n "Waiting for ${dummy_port_fullpath} serial..." >&2

COUNTER=0
while [ ! -c "${dummy_port_fullpath}" ] && ((COUNTER++ < 40)); do
COUNTER=40
while [ ! -r "${dummy_port_fullpath}" ] && ((COUNTER--)); do
echo -n "." >&2
sleep 0.1
done

echo Done
if [ $COUNTER -eq -1 ]; then
echo " Timed out." >&2
exit 1
else
echo " Done." >&2
fi