X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fslipway%2FFtdiBoard.java;h=9758540a5cdbf48a24c2232826f098a9e846f92b;hb=304b5fd54ac37294709cf761e5d69b8731b878a8;hp=962ddce5a9e3a89e18e93b1ef90c86a5229fa1f3;hpb=37015141a62a639e7d5f67729866277f76387916;p=slipway.git diff --git a/src/edu/berkeley/slipway/FtdiBoard.java b/src/edu/berkeley/slipway/FtdiBoard.java index 962ddce..9758540 100644 --- a/src/edu/berkeley/slipway/FtdiBoard.java +++ b/src/edu/berkeley/slipway/FtdiBoard.java @@ -1,151 +1,212 @@ 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.*; -public class FtdiBoard extends Board { +public class FtdiBoard extends Fpslic implements Board { static { - System.load(new File("build/"+System.mapLibraryName("Ftdi")).getAbsolutePath()); + System.load(new File("build/"+System.mapLibraryName("FtdiUartNative")).getAbsolutePath()); } - private final ChipImpl chip; - private final InputStream in; - private final OutputStream out; + private final FpslicBoot chip; + private final DataInputStream in; + private final DataOutputStream out; public InputStream getInputStream() { return in; } public OutputStream getOutputStream() { return out; } public FtdiBoard() throws Exception { - chip = new ChipImpl(); - boot(new InputStreamReader(new FileInputStream("bitstreams/usbdrone.bst"))); - in = chip.getInputStream(); - out = chip.getOutputStream(); + super(24, 24); + chip = new FpslicBoot(new FpslicBootPinsUsb(new FtdiUart(0x6666, 0x3133, 1500 * 1000))); + String bstFile = this.getClass().getName(); + bstFile = bstFile.substring(0, bstFile.lastIndexOf('.')); + bstFile = bstFile.replace('.', '/')+"/slipway_drone.bst"; + boot(new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream(bstFile))); + in = new DataInputStream(chip.getInputStream()); + out = new DataOutputStream(chip.getOutputStream()); for(int i=0; i<255; i++) out.write(0); out.flush(); } - public void reset() { - chip.doReset(); + public void reset() throws IOException { + chip.reset(); } public void boot(Reader r) throws Exception { - boolean pin; - Chip d = chip; - - //d.buffered(false); - - d.doReset(); - d.config(0,3); - d.con(); - d.config(0,7); - d.flush(); - //d.flush(); - d.config(Integer.parseInt("10110111", 2), 8); - d.config(0,1); - d.flush(); - try { Thread.sleep(100); } catch (Exception e) { } - pin = d.initErr(); - System.out.println("good preamble => " + pin + " " + (pin ? green("good") : red("BAD"))); - - d.doReset(); - try { Thread.sleep(100); } catch (Exception e) { } - d.config(0,3); - d.con(); - d.config(0,6); - d.flush(); - //d.flush(); - d.config(Integer.parseInt("10110111", 2), 8); - d.config(0, 2); - d.flush(); - try { Thread.sleep(100); } catch (Exception e) { } - pin = d.initErr(); - System.out.println("bad preamble #2 => " + pin + " " + (pin ? red("BAD") : green("good"))); - - d.doReset(); - try { Thread.sleep(100); } catch (Exception e) { } - d.config(0,3); - d.con(); - d.config(0,7); - d.flush(); - //d.flush(); - d.config(Integer.parseInt("11110111", 2), 8); - d.config(0, 1); - d.flush(); - try { Thread.sleep(100); } catch (Exception e) { } - pin = d.initErr(); - System.out.println("bad preamble #1 => " + pin + " " + (pin ? red("BAD") : green("good"))); - - d.doReset(); - - d.config(0,10); - d.con(); - //d.config(Integer.parseInt("10110111", 2)); - //d.config(0); + chip.selfTest(); + + int total = 75090/9; + OutputStream os = new ProgressOutputStream("bootstrap bitstream:", chip.getConfigStream(), total); BufferedReader br = new BufferedReader(r); - br.readLine(); + int bytes = 0; - //System.out.println("cts="+""+" pins=" + pad(Integer.toString(d.readPins()&0xff,2),8)); while(true) { String s = br.readLine(); if (s==null) break; - int in = Integer.parseInt(s, 2); bytes++; - for(int i=7; i>=0; i--) { - d.config((((in & 0xff) & (1<= i) return s; + return "0"+pad(s, i-1); + } + + + // AvrDrone leftovers ////////////////////////////////////////////////////////////////////////////// + + private void init() throws IOException { + byte[] bytes = new byte[6]; + int i=0; + + out.write(0); + out.flush(); + + // read any crap that might be left in the buffer + while(true) { + System.arraycopy(bytes, 1, bytes, 0, 5); + bytes[5] = in.readByte(); + i++; + System.out.print("\rsignature: read \"" + new String(bytes) + "\" "); + if (bytes[0] == (byte)'O' && + bytes[1] == (byte)'B' && + bytes[2] == (byte)'I' && + bytes[3] == (byte)'T' && + bytes[4] == (byte)'S') { + System.out.println("\rsignature: got proper signature "); + break; } } + } + + public synchronized void scanFPGA(boolean on) throws IOException { + if (on) { + out.writeByte(3); + out.flush(); + } else { + // FIXME + } + } + // fixme! + public static int retval = 0; + public synchronized int readCount() { + try { + if (reader != null) { + reader.start(); + reader = null; + } + ByteCallback bc = new ByteCallback() { + public synchronized void call(byte b) throws Exception { + retval = + ((b & 0xff) << 24) | + ((in.read() & 0xff) << 16) | + ((in.read() & 0xff) << 8) | + ((in.read() & 0xff) << 0); + this.notify(); + } + }; + synchronized(bc) { + callbacks.add(bc); + out.writeByte(6); + out.flush(); + bc.wait(); + } + return retval; + } catch (Exception e) { throw new RuntimeException(e); } + } + + public static interface ByteCallback { + public void call(byte b) throws Exception; + } - d.flush(); - if (!d.initErr()) - throw new RuntimeException("initialization failed at " + bytes); - //System.out.println("cts="+""+" pins=" + pad(Integer.toString(d.readPins()&0xff,2),8)); + private Vector callbacks = new Vector(); + + private Thread reader = new Thread() { + public void run() { + System.out.println("*** reader thread begun"); + while(true) { + try { + byte b = in.readByte(); + ByteCallback bc = (ByteCallback)callbacks.remove(0); + bc.call(b); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + }; + public synchronized void readBus(ByteCallback bc) throws IOException { + callbacks.add(bc); + out.writeByte(2); + out.flush(); + if (reader != null) { + reader.start(); + reader = null; + } + } - for(int i=0; i<100; i++) { - d.flush(); - if (!d.initErr()) - throw new RuntimeException("initialization failed at " + bytes); - try { Thread.sleep(20); } catch (Exception e) { } - d.config(0,1); + public synchronized void readInterrupts(ByteCallback bc) throws IOException { + callbacks.add(bc); + out.writeByte(6); + out.flush(); + if (reader != null) { + reader.start(); + reader = null; } + } - System.out.println(); - System.out.println("avr reset => false"); - d.avrrst(false); - try { Thread.sleep(500); } catch (Exception e) { } - //System.out.println("cts="+""+" pins=" + pad(Integer.toString(d.readPins()&0xff,2),8)); - - //((ChipImpl)d).avr(); - - //System.out.println("avr reset => true"); - chip.purge(); - chip.uart(); - - //d.avrrst(true); - //try { Thread.sleep(500); } catch (Exception e) { } - //System.out.println("cts="+""+" pins=" + pad(Integer.toString(d.readPins()&0xff,2),8)); + 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]; } - public static String pad(String s, int i) { - if (s.length() >= i) return s; - return "0"+pad(s, i-1); + 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 static String red(Object o) { return "\033[31m"+o+"\033[0m"; } - public static String green(Object o) { return "\033[32m"+o+"\033[0m"; } + + public /*synchronized*/ void flush() { + try { + out.flush(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private String pad(int i, String s) { if (s.length()>i) return s; return "0"+pad((i-1),s); } + }