1 package edu.berkeley.obits.device.atmel;
3 import edu.berkeley.obits.*;
4 import org.ibex.util.Log;
9 public class FtdiBoard extends Board {
12 System.load(new File("build/"+System.mapLibraryName("Ftdi")).getAbsolutePath());
15 private final ChipImpl chip;
16 private final InputStream in;
17 private final OutputStream out;
19 public InputStream getInputStream() { return in; }
20 public OutputStream getOutputStream() { return out; }
22 public FtdiBoard() throws Exception {
23 chip = new ChipImpl();
24 boot(new InputStreamReader(new FileInputStream("bitstreams/usbdrone.bst")));
25 in = chip.getInputStream();
26 out = chip.getOutputStream();
27 for(int i=0; i<255; i++) out.write(0);
35 public void boot(Reader r) throws Exception {
40 for(int i=0; i<1000; i++) {
47 d.config(Integer.parseInt("10110111", 2), 8);
50 try { Thread.sleep(100); } catch (Exception e) { }
52 System.out.println("good preamble => " + pin + " " + (pin ? green("good") : red("BAD")));
55 try { Thread.sleep(100); } catch (Exception e) { }
61 d.config(Integer.parseInt("10110111", 2), 8);
64 try { Thread.sleep(100); } catch (Exception e) { }
66 System.out.println("bad preamble #2 => " + pin + " " + (pin ? red("BAD") : green("good")));
69 try { Thread.sleep(100); } catch (Exception e) { }
75 d.config(Integer.parseInt("11110111", 2), 8);
78 try { Thread.sleep(100); } catch (Exception e) { }
80 System.out.println("bad preamble #1 => " + pin + " " + (pin ? red("BAD") : green("good")));
86 //d.config(Integer.parseInt("10110111", 2));
89 BufferedReader br = new BufferedReader(r);
92 //System.out.println("cts="+""+" pins=" + pad(Integer.toString(d.readPins()&0xff,2),8));
94 String s = br.readLine();
96 int in = Integer.parseInt(s, 2);
98 for(int i=7; i>=0; i--) {
99 d.config((((in & 0xff) & (1<<i))!=0)?1:0, 1);
100 boolean init = true; // d.initErr()
101 if (bytes < 100 || (bytes % 1000)==0) {
104 System.out.print("wrote " + bytes + " bytes, init="+init+" \r");
107 throw new RuntimeException("initialization failed at byte " + bytes + ", bit " + i);
114 throw new RuntimeException("initialization failed at " + bytes);
115 //System.out.println("cts="+""+" pins=" + pad(Integer.toString(d.readPins()&0xff,2),8));
118 for(int i=0; i<100; i++) {
121 throw new RuntimeException("initialization failed at " + bytes);
122 try { Thread.sleep(20); } catch (Exception e) { }
126 System.out.println();
127 System.out.println("avr reset => false");
129 try { Thread.sleep(500); } catch (Exception e) { }
130 //System.out.println("cts="+""+" pins=" + pad(Integer.toString(d.readPins()&0xff,2),8));
132 //((ChipImpl)d).avr();
134 //System.out.println("avr reset => true");
139 //try { Thread.sleep(500); } catch (Exception e) { }
140 //System.out.println("cts="+""+" pins=" + pad(Integer.toString(d.readPins()&0xff,2),8));
143 public static String pad(String s, int i) {
144 if (s.length() >= i) return s;
145 return "0"+pad(s, i-1);
147 public static String red(Object o) { return "\033[31m"+o+"\033[0m"; }
148 public static String green(Object o) { return "\033[32m"+o+"\033[0m"; }