checkpoint
[slipway.git] / src / com / atmel / fpslic / FpslicRawUsb.java
index ca3737e..cbc6e80 100644 (file)
@@ -35,44 +35,37 @@ public class FpslicRawUsb implements FpslicRaw {
             (1<<5) |
             (1<<6) |
             (1<<7);
-        avrrst(false);
-
-        flush();
-        //purge();
-
         ftdiuart.dbus_mode(dmask);
-        flush();
 
+        avrrst(false);
         clk(false);
         data(false);
-        con(false);
+
         flush();
-        //try { Thread.sleep(500); } catch (Exception e) { }
+        dmask |= (1<<0);
+        dbang(0, false);
+        ftdiuart.dbus_mode(dmask);
 
         reset(false);
-        flush();
         try { Thread.sleep(500); } catch (Exception e) { }
         if (initErr()) throw new RuntimeException("INIT was still high after pulling RESET low");
 
         reset(true);
-        flush();
         try { Thread.sleep(500); } catch (Exception e) { }
         if (!initErr()) throw new RuntimeException("INIT was still low after releasing RESET");
 
-        con(false);
+        config(0,2);
+        flush();
     }
 
     public OutputStream getConfigStream() throws IOException {
         reset();
-        config(0,3);
-        con();
-        config(0,7);
-        flush();
         return new OutputStream() {
                 int bytes = 0;
                 int bits = 0;
                 public void write(int in) throws IOException {
                     for(int i=7; i>=0; i--) {
+                        bits++;
                         config((((in & 0xff) & (1<<i))!=0)?1:0, 1);
                     }
                 }
@@ -82,9 +75,9 @@ public class FpslicRawUsb implements FpslicRaw {
                 }
                 public void flush() throws IOException {
                     FpslicRawUsb.this.flush();
-                    rcon();
                 }
                 public void close() throws IOException {
+                    rcon();
                     flush();
                     if (!initErr())
                         throw new RuntimeException("initialization failed at " + bytes);
@@ -106,14 +99,11 @@ public class FpslicRawUsb implements FpslicRaw {
     public OutputStream getOutputStream() { return ftdiuart.getOutputStream(); }
     public InputStream  getInputStream() { return ftdiuart.getInputStream(); }
 
-    private void preamble() throws IOException {
-        config(0,7);
-        flush();
-    }
     public void selfTest() throws Exception {
         boolean pin;
 
         getConfigStream();
+        config(Integer.parseInt("00000000", 2), 8);
         config(Integer.parseInt("10110111", 2), 8);
         config(0,1);
         flush();
@@ -121,13 +111,8 @@ public class FpslicRawUsb implements FpslicRaw {
         pin = initErr();
         System.out.println("good preamble   => " + pin + " " + (pin ? green("good") : red("BAD")));
 
-        reset();
-        try { Thread.sleep(100); } catch (Exception e) { }
-        config(0,3);
-        con();
-        config(0,6);
-        flush();
-        // one too many
+        getConfigStream();
+        config(Integer.parseInt("0000000",  2), 7);
         config(Integer.parseInt("10110111", 2), 8);
         config(0, 2);
         flush();
@@ -135,9 +120,8 @@ public class FpslicRawUsb implements FpslicRaw {
         pin = initErr();
         System.out.println("bad preamble #2 => " + pin + " " + (pin ? red("BAD") : green("good")));
 
-        reset();
-        try { Thread.sleep(100); } catch (Exception e) { }
         getConfigStream();
+        config(Integer.parseInt("00000000", 2), 8);
         config(Integer.parseInt("11110111", 2), 8);
         config(0, 1);
         flush();
@@ -163,7 +147,7 @@ public class FpslicRawUsb implements FpslicRaw {
             boolean bit = (dat & (1<<i)) != 0;
             data(bit);
             clk(true);
-            clk(false);
+            dbits &= ~(1<<6);  // let the clock fall with the next data bit, whenever it goes out
         }
     }
 
@@ -184,24 +168,9 @@ public class FpslicRawUsb implements FpslicRaw {
     private void data(boolean on)   throws IOException { dbang(5, on); }
     private boolean initErr()       throws IOException { flush(); return (ftdiuart.readPins() & (1<<4))!=0; }
 
-    private boolean con() throws IOException {
-        flush();
-        //dmask &= ~(1<<0);
-        ftdiuart.dbus_mode(dmask);
-        return (ftdiuart.readPins() & (1<<0)) != 0;
-    }
-
-    private boolean rcon() throws IOException {
-        flush();
+    private void rcon() throws IOException {
         dmask &= ~(1<<0);
         ftdiuart.dbus_mode(dmask);
-        return (ftdiuart.readPins() & (1<<0)) != 0;
-    }
-    private void con(boolean on) throws IOException {
-        flush();
-        dmask |= (1<<0);
-        dbang(0, on);
-        ftdiuart.dbus_mode(dmask);
     }
 
     private static String red(Object o) { return "\033[31m"+o+"\033[0m"; }