@@ -49,6 +49,7 @@ class UsbConnection implements IOIOConnection {
4949 private static final String TAG = UsbConnection .class .getSimpleName ();
5050 private static final int SOFT_RESET = 0x01 ;
5151 private static final int HARD_RESET = 0x00 ;
52+ private static final int SYNC = 0x23 ;
5253 private static final int MAX_RETRIES = 10 ;
5354 private ConnectionState state ;
5455 private FixedReadBufferedInputStream inputStream ;
@@ -60,7 +61,7 @@ private enum ConnectionState {
6061 }
6162
6263 UsbConnection () {
63- Log .i (TAG , "creating UsbConnection" );
64+ Log .d (TAG , "creating UsbConnection" );
6465 this .state = ConnectionState .INIT ;
6566 this .inputStream = null ;
6667 this .outputStream = null ;
@@ -74,8 +75,8 @@ public boolean canClose() {
7475
7576 @ Override
7677 public synchronized void disconnect () {
77- Log .i (TAG , "disconnect entered" );
78- if (this . state != ConnectionState .DISCONNECTED ) {
78+ Log .d (TAG , "disconnect entered: " + state );
79+ if (state != ConnectionState .DISCONNECTED ) {
7980 IOUtil .setError ("USB disconnected" );
8081 close ();
8182 }
@@ -110,6 +111,7 @@ protected void finalize() {
110111 }
111112
112113 private void close () {
114+ Log .d (TAG , "close streams" );
113115 state = ConnectionState .DISCONNECTED ;
114116 try {
115117 if (inputStream != null ) {
@@ -131,20 +133,20 @@ private void close() {
131133 }
132134
133135 private void handleResetResponse (int attempt ) throws IOException {
134- if (attempt > 0 ) {
136+ if (attempt < MAX_RETRIES ) {
135137 int response = inputStream .read ();
136- Log .e (TAG , "Response:" + response + " available:" + inputStream .available ());
138+ Log .d (TAG , "Response:" + response + " available:" + inputStream .available () + " attempt:" + attempt );
137139 if (response != SOFT_RESET ) {
138- // fail
139- if (inputStream .available () == 0 ) {
140+ // unexpected
141+ if (inputStream .available () < 1 ) {
140142 try {
141143 Thread .sleep (100 );
142144 }
143145 catch (InterruptedException e ) {
144146 throw new IOIOException (e );
145147 }
146148 }
147- handleResetResponse (attempt - 1 );
149+ handleResetResponse (attempt + 1 );
148150 }
149151 } else {
150152 throw new IOIOException ("USB connection failure" );
@@ -153,12 +155,12 @@ private void handleResetResponse(int attempt) throws IOException {
153155
154156 private boolean open () {
155157 boolean result = false ;
156- Log .i (TAG , "open() entered" );
158+ Log .d (TAG , "open() entered" );
157159
158160 try {
159161 openStreams ();
160162 resetBoard ();
161- handleResetResponse (MAX_RETRIES );
163+ handleResetResponse (0 );
162164 result = true ;
163165 } catch (java .io .IOException e ) {
164166 IOUtil .setError ("Failed to open streams: " + e );
@@ -182,13 +184,15 @@ private void openStreams() {
182184
183185 private void resetBoard () throws IOException {
184186 if (IOUtil .getHardReset ()) {
187+ Log .d (TAG , "hard reset" );
185188 outputStream .write (HARD_RESET );
186189 outputStream .write ('I' );
187190 outputStream .write ('O' );
188191 outputStream .write ('I' );
189192 outputStream .write ('O' );
190193 } else {
191- outputStream .write (SOFT_RESET );
194+ Log .d (TAG , "soft reset" );
195+ outputStream .write (SYNC );
192196 }
193197 outputStream .flush ();
194198 }
0 commit comments