1 package edu.berkeley.slipway;
3 import com.atmel.fpslic.*;
4 import edu.berkeley.obits.*;
5 import org.ibex.util.Log;
10 public class FtdiBoard extends Board {
13 System.load(new File("build/"+System.mapLibraryName("Ftdi")).getAbsolutePath());
16 private final ChipImpl chip;
17 private final InputStream in;
18 private final OutputStream out;
20 public InputStream getInputStream() { return in; }
21 public OutputStream getOutputStream() { return out; }
23 public FtdiBoard() throws Exception {
24 chip = new ChipImpl();
25 boot(new InputStreamReader(new FileInputStream("bitstreams/usbdrone.bst")));
26 in = chip.getInputStream();
27 out = chip.getOutputStream();
28 for(int i=0; i<255; i++) out.write(0);
36 public void boot(Reader r) throws Exception {
48 d.config(Integer.parseInt("10110111", 2), 8);
51 try { Thread.sleep(100); } catch (Exception e) { }
53 System.out.println("good preamble => " + pin + " " + (pin ? green("good") : red("BAD")));
56 try { Thread.sleep(100); } catch (Exception e) { }
62 d.config(Integer.parseInt("10110111", 2), 8);
65 try { Thread.sleep(100); } catch (Exception e) { }
67 System.out.println("bad preamble #2 => " + pin + " " + (pin ? red("BAD") : green("good")));
70 try { Thread.sleep(100); } catch (Exception e) { }
76 d.config(Integer.parseInt("11110111", 2), 8);
79 try { Thread.sleep(100); } catch (Exception e) { }
81 System.out.println("bad preamble #1 => " + pin + " " + (pin ? red("BAD") : green("good")));
87 //d.config(Integer.parseInt("10110111", 2));
90 BufferedReader br = new BufferedReader(r);
93 //System.out.println("cts="+""+" pins=" + pad(Integer.toString(d.readPins()&0xff,2),8));
95 String s = br.readLine();
97 int in = Integer.parseInt(s, 2);
99 for(int i=7; i>=0; i--) {
100 d.config((((in & 0xff) & (1<<i))!=0)?1:0, 1);
101 boolean init = true; // d.initErr()
102 if (bytes < 100 || (bytes % 1000)==0) {
105 System.out.print("wrote " + bytes + " bytes, init="+init+" \r");
109 throw new RuntimeException("initialization failed at byte " + bytes + ", bit " + i);
116 throw new RuntimeException("initialization failed at " + bytes);
117 //System.out.println("cts="+""+" pins=" + pad(Integer.toString(d.readPins()&0xff,2),8));
120 for(int i=0; i<100; i++) {
123 throw new RuntimeException("initialization failed at " + bytes);
124 try { Thread.sleep(20); } catch (Exception e) { }
128 System.out.println();
129 System.out.println("avr reset => false");
131 try { Thread.sleep(500); } catch (Exception e) { }
132 //System.out.println("cts="+""+" pins=" + pad(Integer.toString(d.readPins()&0xff,2),8));
134 //((ChipImpl)d).avr();
136 //System.out.println("avr reset => true");
138 chip.uart_and_cbus_mode(1<<1, 1<<1);
141 //try { Thread.sleep(500); } catch (Exception e) { }
142 //System.out.println("cts="+""+" pins=" + pad(Integer.toString(d.readPins()&0xff,2),8));
145 public static String pad(String s, int i) {
146 if (s.length() >= i) return s;
147 return "0"+pad(s, i-1);
149 public static String red(Object o) { return "\033[31m"+o+"\033[0m"; }
150 public static String green(Object o) { return "\033[32m"+o+"\033[0m"; }