@@ -6,58 +6,59 @@ if [ $# -lt 4 ]; then
66  echo  " Usage: $0  $#  <dummy_port> <altID> <usbID> <binfile>" >&2 
77  exit  1
88fi 
9- altID=$2 
10- usbID=$3 
11- binfile=$4 
9+ altID=" $2 " 
10+ usbID=" $3 " 
11+ binfile=" $4 " 
1212dummy_port_fullpath=" /dev/$1 " 
13+ if  [ $#  -eq  5 ];  then 
14+   dfuse_addr=" --dfuse-address $5 " 
15+ else 
16+   dfuse_addr=" " 
17+ fi 
1318
1419#  Get the directory where the script is running.
1520DIR=$( cd " $( dirname " ${BASH_SOURCE[0]} " ) " &&  pwd) 
1621
17- #  -----------------  Old code to reset the USB - which doesn't seem to work --------
18- # 
19- # if we can find the Serial device try resetting it and then sleeping for 1 sec while the board reboots
20- # if [ -e $dummy_port_fullpath ]; then
21- # 	echo "resetting " $dummy_port_fullpath
22- # 	stty -f $dummy_port_fullpath 1200
23- # 	sleep 1
24- # #	stty -f $dummy_port_fullpath 1200
25- # #	sleep 1
26- # fi
27- #  ------------------  End of old code -----------------
28- 
2922#   ----------------- IMPORTANT -----------------
3023#  The 2nd parameter to upload-reset is the delay after resetting before it exits
3124#  This value is in milliseonds
3225#  You may need to tune this to your system
3326#  750ms to 1500ms seems to work on my Mac
27+ #  This is less critical now that we automatically retry dfu-util
3428
35- " ${DIR} " " ${dummy_port_fullpath} " 
36- 
37- if  [ $#  -eq  5 ];  then 
38-   dfuse_addr=" --dfuse-address $5 " 
39- else 
40-   dfuse_addr=" " 
41- fi 
42- 
43- # DFU_UTIL=/usr/local/bin/dfu-util
44- DFU_UTIL=${DIR} /dfu-util/dfu-util
45- if  [ !  -x  " ${DFU_UTIL} " ;  then 
46-   DFU_UTIL=/opt/local/bin/dfu-util
47- fi 
48- 
49- if  [ !  -x  ${DFU_UTIL}  ];  then 
50-   echo  " $0 : error: cannot find ${DFU_UTIL} " >&2 
51-   exit  2
29+ if  !  " ${DIR} /upload-reset" " ${dummy_port_fullpath} " ;  then 
30+   echo  " ****************************************" >&2 
31+   echo  " * Could not automatically reset device *" >&2 
32+   echo  " * Please manually reset device!        *" >&2 
33+   echo  " ****************************************" >&2 
34+   sleep 2 #  Wait for user to see message.
5235fi 
5336
54- ${DFU_UTIL}  -d " ${usbID} " " ${altID} " " ${binfile} " ${dfuse_addr}  -R
37+ COUNTER=5
38+ while 
39+   " ${DIR} /dfu-util.sh" " ${usbID} " " ${altID} " " ${binfile} " ${dfuse_addr}  -R
40+   (( ret =  $? )) 
41+ do 
42+   if  [ $ret  -eq  74 ] &&  [ $(( -- COUNTER)) -gt  0 ];  then 
43+     #  I/O error, probably because no DFU device was found
44+     echo  " Trying ${COUNTER}  more time(s)" >&2 
45+     sleep 1
46+   else 
47+     exit  $ret 
48+   fi 
49+ done 
5550
56- echo  -n Waiting for  " ${dummy_port_fullpath} " 
51+ echo  -n " Waiting for ${dummy_port_fullpath}  serial..."   >&2 
5752
58- COUNTER=0
59- while  [ !  -c  " ${dummy_port_fullpath} " &&  (( COUNTER++  <  40 )) ;  do 
53+ COUNTER=40
54+ while  [ !  -r  " ${dummy_port_fullpath} " &&  (( COUNTER-- )) ;  do 
55+   echo  -n " ." >&2 
6056  sleep 0.1
6157done 
6258
63- echo  Done
59+ if  [ $COUNTER  -eq  -1 ];  then 
60+   echo  "  Timed out." >&2 
61+   exit  1
62+ else 
63+   echo  "  Done." >&2 
64+ fi 
0 commit comments