|
| 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