X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fslipway%2FFtdiBoard.java;h=ba84988ab84a2aad074b4528907807956b21dde6;hb=05bfa73d5415e19943cf36c094dbdb50304a341c;hp=d9bc8c1f4c45d71dc2719537bdcc906c2c76cda5;hpb=86a9f741672ac1ca31965c008151c13b490d2949;p=slipway.git diff --git a/src/edu/berkeley/slipway/FtdiBoard.java b/src/edu/berkeley/slipway/FtdiBoard.java index d9bc8c1..ba84988 100644 --- a/src/edu/berkeley/slipway/FtdiBoard.java +++ b/src/edu/berkeley/slipway/FtdiBoard.java @@ -23,7 +23,7 @@ public class FtdiBoard extends Fpslic implements Board { public FtdiBoard() throws Exception { super(24, 24); - chip = new FpslicBoot(new FpslicBootPinsUsb(new FtdiUart(0x6666, 0x3133, 1500 * 1000))); + chip = new FpslicBoot(new FpslicBootPinsUsb(new FtdiUart(0x6666, 0x3133, 1500 * 1000/2))); String bstFile = this.getClass().getName(); bstFile = bstFile.substring(0, bstFile.lastIndexOf('.')); bstFile = bstFile.replace('.', '/')+"/slipway_drone.bst"; @@ -35,12 +35,9 @@ public class FtdiBoard extends Fpslic implements Board { init(); } - public void reset() throws IOException { - chip.reset(); - } + public void reset() throws IOException { chip.reset(); } public void boot(Reader r) throws Exception { - chip.selfTest(); int total = 75090/9; @@ -58,11 +55,6 @@ public class FtdiBoard extends Fpslic implements Board { os.close(); } - public static String pad(String s, int i) { - if (s.length() >= i) return s; - return "0"+pad(s, i-1); - } - // AvrDrone leftovers ////////////////////////////////////////////////////////////////////////////// @@ -85,6 +77,7 @@ public class FtdiBoard extends Fpslic implements Board { bytes[3] == (byte)'T' && bytes[4] == (byte)'S') { System.out.println("\rsignature: got proper signature "); + chip.purge(); break; } } @@ -94,9 +87,14 @@ public class FtdiBoard extends Fpslic implements Board { public void run() { while(true) { try { + while(callbacks.size() == 0) Thread.sleep(50); byte b = in.readByte(); ByteCallback bc = (ByteCallback)callbacks.remove(0); + //System.out.println("readback " + b + " in " + (System.currentTimeMillis()-bc.time)); bc.call(b); + synchronized(lock) { + lock.notifyAll(); + } } catch (Exception e) { e.printStackTrace(); } @@ -105,96 +103,112 @@ public class FtdiBoard extends Fpslic implements Board { }.start(); } - public synchronized void scanFPGA(boolean on) throws IOException { - if (on) { - out.writeByte(3); - out.flush(); - } else { - // FIXME + + // Programming /////////////////////////////////////////////////////////////////////////////// + + private byte[][][] cache = new byte[24][][]; + public byte mode4(int z, int y, int x) { + if (cache[x]==null) return 0; + if (cache[x][y]==null) return 0; + return cache[x][y][z]; + } + + public synchronized void mode4(int z, int y, int x, int d) { + try { + if (cache[x & 0xff]==null) cache[x & 0xff] = new byte[24][]; + if (cache[x & 0xff][y & 0xff]==null) cache[x & 0xff][y & 0xff] = new byte[256]; + cache[x & 0xff][y & 0xff][z & 0xff] = (byte)(d & 0xff); + + out.writeByte(1); + out.writeByte(z); + out.writeByte(y); + out.writeByte(x); + out.writeByte(d); + } catch (IOException e) { + throw new RuntimeException(e); } } - // fixme! - public static int retval = 0; + + public synchronized void flush() { + try { out.flush(); } catch (IOException e) { throw new RuntimeException(e); } } + + + // Callbacks ////////////////////////////////////////////////////////////////////////////// + + private Vector callbacks = new Vector(); + + private Object lock = new Object(); + private static final int limit = 40; + + private void enqueue(ByteCallback bcb) { + synchronized(lock) { + try { + while (callbacks.size() >= limit) { + lock.wait(100); + } + } catch (Exception e) { + throw new RuntimeException(e); + } + } + bcb.time = System.currentTimeMillis(); + callbacks.add(bcb); + } + + public static abstract class ByteCallback { + public int result; + public long time; + public abstract void call(byte b) throws Exception; + } + public synchronized int readCount() { try { ByteCallback bc = new ByteCallback() { public synchronized void call(byte b) throws Exception { - retval = + result = ((b & 0xff) << 24) | ((in.read() & 0xff) << 16) | ((in.read() & 0xff) << 8) | ((in.read() & 0xff) << 0); + timer = + ((in.read() & 0xff) << 24) | + ((in.read() & 0xff) << 16) | + ((in.read() & 0xff) << 8) | + ((in.read() & 0xff) << 0); + //System.out.println("timer => " + Integer.toString(timer, 16)); this.notify(); } }; synchronized(bc) { - callbacks.add(bc); - out.writeByte(6); + enqueue(bc); + out.writeByte(3); out.flush(); bc.wait(); } - return retval; + return bc.result; } catch (Exception e) { throw new RuntimeException(e); } } - public static interface ByteCallback { - public void call(byte b) throws Exception; - } - - private Vector callbacks = new Vector(); - + public int timer = 0; public synchronized void readBus(ByteCallback bc) throws IOException { - callbacks.add(bc); + enqueue(bc); out.writeByte(2); out.flush(); } + /* public synchronized void readInterrupts(ByteCallback bc) throws IOException { - callbacks.add(bc); - out.writeByte(6); + enqueue(bc); + out.writeByte(3); out.flush(); } + */ - private byte[][][] cache = new byte[24][][]; - public /*synchronized*/ byte mode4(int z, int y, int x) { - if (cache[x]==null) return 0; - if (cache[x][y]==null) return 0; - return cache[x][y][z]; - } - - int lastz = 0; - int lastx = 0; - int lasty = 0; - public static int save = 0; - public static int saveof = 0; - public /*synchronized*/ void mode4(int z, int y, int x, int d) { - try { - out.writeByte(1); - out.writeByte(z); - out.writeByte(y); - out.writeByte(x); - saveof++; - lastz = z; - lastx = x; - lasty = y; - out.writeByte(d); - - if (cache[x & 0xff]==null) cache[x & 0xff] = new byte[24][]; - if (cache[x & 0xff][y & 0xff]==null) cache[x & 0xff][y & 0xff] = new byte[256]; - cache[x & 0xff][y & 0xff][z & 0xff] = (byte)(d & 0xff); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - public /*synchronized*/ void flush() { - try { - out.flush(); - } catch (IOException e) { - throw new RuntimeException(e); - } - } + // Util ////////////////////////////////////////////////////////////////////////////// private String pad(int i, String s) { if (s.length()>i) return s; return "0"+pad((i-1),s); } + public static String pad(String s, int i) { + if (s.length() >= i) return s; + return "0"+pad(s, i-1); + } }