checkpoint
authoradam <adam@megacz.com>
Thu, 21 Sep 2006 14:25:10 +0000 (15:25 +0100)
committeradam <adam@megacz.com>
Thu, 21 Sep 2006 14:25:10 +0000 (15:25 +0100)
src/com/atmel/fpslic/FpslicRawUsb.java
src/edu/berkeley/slipway/FtdiBoard.java

index 276b4e5..ca3737e 100644 (file)
@@ -19,22 +19,11 @@ public class FpslicRawUsb implements FpslicRaw {
         (1<<6) |
         (1<<7);
 
-    public FpslicRawUsb() throws IOException {
-        this(new FtdiUart(0x6666, 0x3133, 1500 * 1000));
-    }
     public FpslicRawUsb(FtdiUart ftdiuart) throws IOException {
         this.ftdiuart = ftdiuart;
         reset();
     }
 
-    void flush() throws IOException { ftdiuart.getOutputStream().flush(); }
-
-    protected int dbits = 0;
-    protected synchronized void dbang(int bit, boolean val) throws IOException {
-        dbits = val ? (dbits | (1 << bit)) : (dbits & (~(1 << bit)));
-        ftdiuart.getOutputStream().write((byte)dbits);
-    }
-
     public void reset() throws IOException {
 
         dmask =
@@ -73,62 +62,16 @@ public class FpslicRawUsb implements FpslicRaw {
         con(false);
     }
 
-    void config(boolean bit) throws IOException { config(bit?1:0, 1); }
-    void config(int dat) throws IOException { config(dat, 8); }
-    void config(int dat, int numbits) throws IOException {
-        for(int i=(numbits-1); i>=0; i--) {
-            boolean bit = (dat & (1<<i)) != 0;
-            data(bit);
-            clk(true);
-            clk(false);
-        }
-    }
-
-    // tricky: RESET has a weak pull-up, and is wired to a CBUS line.  So,
-    //         we can pull it down (assert reset) from uart-mode, or we can
-    //         let it float upward from either mode.
-    void reset(boolean on) throws IOException {
-        ftdiuart.uart_and_cbus_mode(1<<1, on ? (1<<1) : 0);
-        flush();
-        if (on) {
-            ftdiuart.dbus_mode(dmask);
-            flush();
-        }
-    }
-
-    void avrrst(boolean on) throws IOException { dbang(7, on); }
-    void clk(boolean on)    throws IOException { dbang(6, on); }
-    void data(boolean on)   throws IOException { dbang(5, on); }
-
-    boolean initErr()       throws IOException { flush(); return (ftdiuart.readPins() & (1<<4))!=0; }
-
-    boolean con() throws IOException {
-        flush();
-        //dmask &= ~(1<<0);
-        ftdiuart.dbus_mode(dmask);
-        return (ftdiuart.readPins() & (1<<0)) != 0;
-    }
-    boolean rcon() throws IOException {
-        flush();
-        dmask &= ~(1<<0);
-        ftdiuart.dbus_mode(dmask);
-        return (ftdiuart.readPins() & (1<<0)) != 0;
-    }
-    void con(boolean on) throws IOException {
-        flush();
-        dmask |= (1<<0);
-        dbang(0, on);
-        ftdiuart.dbus_mode(dmask);
-    }
-
     public OutputStream getConfigStream() throws IOException {
         reset();
-        config(0,10);
+        config(0,3);
         con();
+        config(0,7);
+        flush();
         return new OutputStream() {
                 int bytes = 0;
+                int bits = 0;
                 public void write(int in) throws IOException {
-                    bytes++;
                     for(int i=7; i>=0; i--) {
                         config((((in & 0xff) & (1<<i))!=0)?1:0, 1);
                     }
@@ -163,17 +106,14 @@ public class FpslicRawUsb implements FpslicRaw {
     public OutputStream getOutputStream() { return ftdiuart.getOutputStream(); }
     public InputStream  getInputStream() { return ftdiuart.getInputStream(); }
 
-
-    static String red(Object o) { return "\033[31m"+o+"\033[0m"; }
-    static String green(Object o) { return "\033[32m"+o+"\033[0m"; }
-    public void selfTest() throws Exception {
-        boolean pin;
-        reset();
-        config(0,3);
-        con();
+    private void preamble() throws IOException {
         config(0,7);
         flush();
+    }
+    public void selfTest() throws Exception {
+        boolean pin;
 
+        getConfigStream();
         config(Integer.parseInt("10110111", 2), 8);
         config(0,1);
         flush();
@@ -187,7 +127,7 @@ public class FpslicRawUsb implements FpslicRaw {
         con();
         config(0,6);
         flush();
-        //flush();
+        // one too many
         config(Integer.parseInt("10110111", 2), 8);
         config(0, 2);
         flush();
@@ -197,11 +137,7 @@ public class FpslicRawUsb implements FpslicRaw {
 
         reset();
         try { Thread.sleep(100); } catch (Exception e) { }
-        config(0,3);
-        con();
-        config(0,7);
-        flush();
-        //flush();
+        getConfigStream();
         config(Integer.parseInt("11110111", 2), 8);
         config(0, 1);
         flush();
@@ -209,4 +145,65 @@ public class FpslicRawUsb implements FpslicRaw {
         pin = initErr();
         System.out.println("bad preamble #1 => " + pin + " " + (pin ? red("BAD") : green("good")));
     }
+
+    // Private //////////////////////////////////////////////////////////////////////////////
+
+    private void flush() throws IOException { ftdiuart.getOutputStream().flush(); }
+
+    private int dbits = 0;
+    private void dbang(int bit, boolean val) throws IOException {
+        dbits = val ? (dbits | (1 << bit)) : (dbits & (~(1 << bit)));
+        ftdiuart.getOutputStream().write((byte)dbits);
+    }
+
+    private void config(boolean bit) throws IOException { config(bit?1:0, 1); }
+    private void config(int dat) throws IOException { config(dat, 8); }
+    private void config(int dat, int numbits) throws IOException {
+        for(int i=(numbits-1); i>=0; i--) {
+            boolean bit = (dat & (1<<i)) != 0;
+            data(bit);
+            clk(true);
+            clk(false);
+        }
+    }
+
+    // tricky: RESET has a weak pull-up, and is wired to a CBUS line.  So,
+    //         we can pull it down (assert reset) from uart-mode, or we can
+    //         let it float upward from either mode.
+    private void reset(boolean on) throws IOException {
+        ftdiuart.uart_and_cbus_mode(1<<1, on ? (1<<1) : 0);
+        flush();
+        if (on) {
+            ftdiuart.dbus_mode(dmask);
+            flush();
+        }
+    }
+
+    private void avrrst(boolean on) throws IOException { dbang(7, on); }
+    private void clk(boolean on)    throws IOException { dbang(6, on); }
+    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();
+        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"; }
+    private static String green(Object o) { return "\033[32m"+o+"\033[0m"; }
 }
index 4b6088f..27c9440 100644 (file)
@@ -1,5 +1,6 @@
 package edu.berkeley.slipway;
 
+import com.ftdi.usb.*;
 import com.atmel.fpslic.*;
 import edu.berkeley.obits.*;
 import org.ibex.util.Log;
@@ -21,7 +22,7 @@ public class FtdiBoard extends Board {
     public OutputStream getOutputStream() { return out; }
 
     public FtdiBoard() throws Exception {
-        chip = new FpslicRawUsb();
+        chip = new FpslicRawUsb(new FtdiUart(0x6666, 0x3133, 1500 * 1000));
         String bstFile = this.getClass().getName();
         bstFile = bstFile.substring(0, bstFile.lastIndexOf('.'));
         bstFile = bstFile.replace('.', '/')+"/slipway_drone.bst";