checkpoint
authoradam <adam@megacz.com>
Fri, 22 Sep 2006 02:48:52 +0000 (03:48 +0100)
committeradam <adam@megacz.com>
Fri, 22 Sep 2006 02:48:52 +0000 (03:48 +0100)
src/com/atmel/fpslic/FpslicRawUsb.java
src/edu/berkeley/obits/AtmelSerial.java
src/edu/berkeley/obits/device/atmel/AtmelDevice.java
src/edu/berkeley/obits/device/atmel/AvrDrone.java
src/edu/berkeley/slipway/FtdiBoard.java
src/org/ibex/util/ProgressInputStream.java [new file with mode: 0644]
src/org/ibex/util/ProgressOutputStream.java [new file with mode: 0644]

index 9bc3393..63842ae 100644 (file)
@@ -37,24 +37,19 @@ public class FpslicRawUsb implements FpslicRaw {
             (1<<7);
         ftdiuart.dbus_mode(dmask);
 
-        avrrst(false);
-        clk(false);
-        data(false);
-
+        clearDBusLines();
         flush();
-        dmask |= (1<<0);
-        dbang(0, false);
         ftdiuart.dbus_mode(dmask);
 
-        reset(false);
+        resetPin(false);
         try { Thread.sleep(500); } catch (Exception e) { }
-        if (initErr()) throw new RuntimeException("INIT was still high after pulling RESET low");
+        if (initPin()) throw new RuntimeException("INIT was still high after pulling RESET low");
 
-        reset(true);
+        resetPin(true);
         try { Thread.sleep(500); } catch (Exception e) { }
-        if (!initErr()) throw new RuntimeException("INIT was still low after releasing RESET");
+        if (!initPin()) throw new RuntimeException("INIT was still low after releasing RESET");
 
-        config(0,2);
+        sendConfigBits(0,2);
         flush();
     }
 
@@ -66,7 +61,7 @@ public class FpslicRawUsb implements FpslicRaw {
                 public void write(int in) throws IOException {
                     for(int i=7; i>=0; i--) {
                         bits++;
-                        config((((in & 0xff) & (1<<i))!=0)?1:0, 1);
+                        sendConfigBits((((in & 0xff) & (1<<i))!=0)?1:0, 1);
                     }
                 }
                 public void write(byte[] b, int off, int len) throws IOException {
@@ -79,23 +74,24 @@ public class FpslicRawUsb implements FpslicRaw {
                 public void close() throws IOException {
 
                     flush();
-                    if (!initErr())
+
+                    // turn off the CON pin we've been pulling low...
+                    dmask &= ~(1<<0);
+                    ftdiuart.dbus_mode(dmask);
+                    flush();
+
+                    if (!initPin())
                         throw new RuntimeException("initialization failed at " + bytes);
                     for(int i=0; i<100; i++) {
                         flush();
-                        if (!initErr())
+                        if (!initPin())
                             throw new RuntimeException("initialization failed at " + bytes);
                         try { Thread.sleep(20); } catch (Exception e) { }
-                        config(0,1);
+                        sendConfigBits(0,1);
                     }
 
-                    // turn off the CON pin we've been pulling low...
-                    dmask &= ~(1<<0);
-                    ftdiuart.dbus_mode(dmask);
-
-
-                    avrrst(false);
-                    try { Thread.sleep(100); } catch (Exception e) { }
+                    // switching to uart mode will implicitly release AVRRST
+                    avrrstPin(false);
                     ftdiuart.purge();
                     ftdiuart.uart_and_cbus_mode(1<<1, 1<<1);
                 }
@@ -107,33 +103,49 @@ public class FpslicRawUsb implements FpslicRaw {
 
     public void selfTest() throws Exception {
         boolean pin;
+        System.out.print("smoke check: ");
+
+        // correct preamble
+        getConfigStream();
+        sendConfigBits(Integer.parseInt("00000000", 2), 8);
+        sendConfigBits(Integer.parseInt("10110111", 2), 8);
+        sendConfigBits(0,1);
+        flush();
+        try { Thread.sleep(100); } catch (Exception e) { }
+        pin = initPin();
+        System.out.print((pin ? green(" [pass]") : red(" [FAIL]")));
 
+        // preamble shifted one bit earlier than it should be
         getConfigStream();
-        config(Integer.parseInt("00000000", 2), 8);
-        config(Integer.parseInt("10110111", 2), 8);
-        config(0,1);
+        sendConfigBits(Integer.parseInt("0000000",  2), 7);
+        sendConfigBits(Integer.parseInt("10110111", 2), 8);
+        sendConfigBits(0, 2);
         flush();
         try { Thread.sleep(100); } catch (Exception e) { }
-        pin = initErr();
-        System.out.println("good preamble   => " + pin + " " + (pin ? green("good") : red("BAD")));
+        pin = initPin();
+        System.out.print((pin ? red(" [FAIL]") : green(" [pass]")));
 
+        // preamble shifted one bit later than it should be
         getConfigStream();
-        config(Integer.parseInt("0000000",  2), 7);
-        config(Integer.parseInt("10110111", 2), 8);
-        config(0, 2);
+        sendConfigBits(Integer.parseInt("000000000", 2), 9);
+        sendConfigBits(Integer.parseInt("10110111",  2), 8);
+        //sendConfigBits(0, 1);
         flush();
         try { Thread.sleep(100); } catch (Exception e) { }
-        pin = initErr();
-        System.out.println("bad preamble #2 => " + pin + " " + (pin ? red("BAD") : green("good")));
+        pin = initPin();
+        System.out.print((pin ? red(" [FAIL]") : green(" [pass]")));
 
+        // plain 'ol bogus preamble
         getConfigStream();
-        config(Integer.parseInt("00000000", 2), 8);
-        config(Integer.parseInt("11110111", 2), 8);
-        config(0, 1);
+        sendConfigBits(Integer.parseInt("00000000", 2), 8);
+        sendConfigBits(Integer.parseInt("11110111", 2), 8);
+        sendConfigBits(0, 1);
         flush();
         try { Thread.sleep(100); } catch (Exception e) { }
-        pin = initErr();
-        System.out.println("bad preamble #1 => " + pin + " " + (pin ? red("BAD") : green("good")));
+        pin = initPin();
+        System.out.print((pin ? red(" [FAIL]") : green(" [pass]")));
+
+        System.out.println();
     }
 
     // Private //////////////////////////////////////////////////////////////////////////////
@@ -141,18 +153,23 @@ public class FpslicRawUsb implements FpslicRaw {
     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)));
+    private void setDBusLine() throws IOException {
         ftdiuart.getOutputStream().write((byte)dbits);
     }
+    private void clearDBusLines() throws IOException {
+        dbits = 0;
+        setDBusLine();
+    }
+    private void setDBusLine(int bit, boolean val) throws IOException {
+        dbits = val ? (dbits | (1 << bit)) : (dbits & (~(1 << bit)));
+        setDBusLine();
+    }
 
-    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 {
+    private void sendConfigBits(int dat, int numbits) throws IOException {
         for(int i=(numbits-1); i>=0; i--) {
             boolean bit = (dat & (1<<i)) != 0;
-            data(bit);
-            clk(true);
+            configDataPin(bit);
+            cclkPin(true);
             dbits &= ~(1<<6);  // let the clock fall with the next data bit, whenever it goes out
         }
     }
@@ -160,7 +177,7 @@ public class FpslicRawUsb implements FpslicRaw {
     // 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 {
+    private void resetPin(boolean on) throws IOException {
         ftdiuart.uart_and_cbus_mode(1<<1, on ? (1<<1) : 0);
         flush();
         if (on) {
@@ -169,10 +186,10 @@ public class FpslicRawUsb implements FpslicRaw {
         }
     }
 
-    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 void avrrstPin(boolean on) throws IOException { setDBusLine(7, on); }
+    private void cclkPin(boolean on)    throws IOException { setDBusLine(6, on); }
+    private void configDataPin(boolean on)   throws IOException { setDBusLine(5, on); }
+    private boolean initPin()       throws IOException { flush(); return (ftdiuart.readPins() & (1<<4))!=0; }
 
     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 d8deb7d..aab369b 100644 (file)
@@ -39,7 +39,7 @@ public class AtmelSerial {
         At40k at40k = new At40k.At40k10(device);
         try {
             long begin = System.currentTimeMillis();
-            device.readMode4(System.in);
+            device.readMode4(new ProgressInputStream("configuring fabric", System.in, 111740));
             long end = System.currentTimeMillis();
             Log.info(AtmelSerial.class, "finished in " + ((end-begin)/1000) + "s");
             Thread.sleep(1000);
index ae77df1..ffad1f4 100644 (file)
@@ -86,9 +86,9 @@ public abstract class AtmelDevice extends Bits implements Device {
             long foo = Long.parseLong(str, 16);
             mode4((int)(foo >> 24), (int)(foo >> 16), (int)(foo >>  8), (int)(foo >>  0));
             count++;
-            if (count % 100 == 0) Log.info(AtmelSerial.class, "wrote " + count + " configuration octets");
         }
         flush();
+        in.close();
     }
 
     public void writeMode4(Writer w) throws IOException {
index 6776b8b..5e5489b 100644 (file)
@@ -30,7 +30,7 @@ public class AvrDrone extends AtmelDevice {
     }
 
     private void init() throws IOException {
-        Log.debug(this, "waiting for device to identify itself");
+        //Log.debug(this, "waiting for device to identify itself");
         /*
         if (in.readByte() != (byte)'O')  throw new RuntimeException("didn't get the proper signature");
         if (in.readByte() != (byte)'B')  throw new RuntimeException("didn't get the proper signature");
@@ -43,10 +43,10 @@ public class AvrDrone extends AtmelDevice {
         byte[] bytes = new byte[6];
         for(int i=0; i<6; i++) {
             bytes[i] = in.readByte();
-            System.out.println("got " + (i+1) + " header bytes: " + (bytes[i] & 0xff) + " '" + ((char)bytes[i]) + "'");
+            //System.out.println("got " + (i+1) + " header bytes: " + (bytes[i] & 0xff) + " '" + ((char)bytes[i]) + "'");
             // FIXME
         }
-        Log.info(this, "device correctly identified itself; ready for operation");
+        //Log.info(this, "device correctly identified itself; ready for operation");
     }
 
     public synchronized void scanFPGA(boolean on) throws DeviceException {
index d84afa1..ee29066 100644 (file)
@@ -3,7 +3,7 @@ package edu.berkeley.slipway;
 import com.ftdi.usb.*;
 import com.atmel.fpslic.*;
 import edu.berkeley.obits.*;
-import org.ibex.util.Log;
+import org.ibex.util.*;
 import java.io.*;
 import java.util.*;
 import gnu.io.*;
@@ -41,7 +41,8 @@ public class FtdiBoard extends Board {
 
         chip.selfTest();
 
-        OutputStream os = chip.getConfigStream();
+        int total = 75090/9;
+        OutputStream os = new ProgressOutputStream("bootstrap bitstream:", chip.getConfigStream(), total);
         BufferedReader br = new BufferedReader(r);
 
         int bytes = 0;
@@ -50,10 +51,7 @@ public class FtdiBoard extends Board {
             if (s==null) break;
             bytes++;
             os.write((byte)Integer.parseInt(s, 2));
-            if ((bytes % 1000)==0) {
-                os.flush();
-                System.out.print("wrote " + bytes + " bytes\r");
-            }
+            if ((bytes % 1000)==0) os.flush();
         }
         os.close();
     }
diff --git a/src/org/ibex/util/ProgressInputStream.java b/src/org/ibex/util/ProgressInputStream.java
new file mode 100644 (file)
index 0000000..9908071
--- /dev/null
@@ -0,0 +1,54 @@
+package org.ibex.util;
+import java.io.*;
+
+public class ProgressInputStream extends FilterInputStream {
+
+    private int size = -1;
+    private int bytes = 0;
+    private String title;
+
+    public ProgressInputStream(String title, InputStream o) { this(title, o, -1); }
+
+    public ProgressInputStream(String title, InputStream o, int size) {
+        super(o);
+        this.size = size;
+        this.title = title;
+    }
+
+    public int read() throws IOException {
+        int ret = super.read();
+        if (ret != -1) bytes++;
+        return ret;
+    }
+
+    public int read(byte[] b, int off, int len) throws IOException {
+        int ret = super.read(b, off, len);
+        if (ret != -1) bytes += ret;
+        update();
+        return ret;
+    }
+
+    private void update() {
+        System.out.print("\r                                                              \r");
+        System.out.print(title);
+        if (size != -1) {
+            int frac = (100 * bytes) / size;
+            String fracs = frac+"";
+            while(fracs.length()<3) fracs = " "+fracs;
+            System.out.print(" ");
+            System.out.print("\033[32m");
+            System.out.print(fracs+"%");
+            System.out.print("\033[0m");
+        }
+        System.out.print(" ");
+        System.out.print(bytes);
+        System.out.print(" bytes ");
+    }
+
+    public void close() throws IOException {
+        super.close();
+        bytes = size;
+        update();
+        System.out.println();
+    }
+}
diff --git a/src/org/ibex/util/ProgressOutputStream.java b/src/org/ibex/util/ProgressOutputStream.java
new file mode 100644 (file)
index 0000000..a671f3a
--- /dev/null
@@ -0,0 +1,53 @@
+package org.ibex.util;
+import java.io.*;
+
+public class ProgressOutputStream extends FilterOutputStream {
+
+    private int size = -1;
+    private int bytes = 0;
+    private String title;
+
+    public ProgressOutputStream(String title, OutputStream o) { this(title, o, -1); }
+
+    public ProgressOutputStream(String title, OutputStream o, int size) {
+        super(o);
+        this.size = size;
+        this.title = title;
+    }
+
+    public void write(int i) throws IOException {
+        super.write(i);
+        bytes++;
+        update();
+    }
+
+    public void write(byte[] b, int off, int len) throws IOException {
+        super.write(b, off, len);
+        bytes += len;
+        update();
+    }
+
+    private void update() {
+        System.out.print("\r                                                              \r");
+        System.out.print(title);
+        if (size != -1) {
+            int frac = (100 * bytes) / size;
+            String fracs = frac+"";
+            while(fracs.length()<3) fracs = " "+fracs;
+            System.out.print(" ");
+            System.out.print("\033[32m");
+            System.out.print(fracs+"%");
+            System.out.print("\033[0m");
+        }
+        System.out.print(" ");
+        System.out.print(bytes);
+        System.out.print(" bytes ");
+    }
+
+    public void close() throws IOException {
+        super.close();
+        bytes = size;
+        update();
+        System.out.println();
+    }
+}