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++) {
46 d.config(Integer.parseInt("10110111", 2), 8);
49 try { Thread.sleep(100); } catch (Exception e) { }
51 System.out.println("good preamble => " + pin + " " + (pin ? green("good") : red("BAD")));
54 try { Thread.sleep(100); } catch (Exception e) { }
59 d.config(Integer.parseInt("10110111", 2), 8);
62 try { Thread.sleep(100); } catch (Exception e) { }
64 System.out.println("bad preamble #2 => " + pin + " " + (pin ? red("BAD") : green("good")));
67 try { Thread.sleep(100); } catch (Exception e) { }
72 d.config(Integer.parseInt("11110111", 2), 8);
75 try { Thread.sleep(100); } catch (Exception e) { }
77 System.out.println("bad preamble #1 => " + pin + " " + (pin ? red("BAD") : green("good")));
83 //d.config(Integer.parseInt("10110111", 2));
86 BufferedReader br = new BufferedReader(r);
89 //System.out.println("cts="+""+" pins=" + pad(Integer.toString(d.readPins()&0xff,2),8));
91 String s = br.readLine();
93 int in = Integer.parseInt(s, 2);
95 for(int i=7; i>=0; i--) {
96 d.config((((in & 0xff) & (1<<i))!=0)?1:0, 1);
97 boolean init = true; // d.initErr()
98 if (bytes < 100 || (bytes % 1000)==0) {
101 System.out.print("wrote " + bytes + " bytes, init="+init+" \r");
104 throw new RuntimeException("initialization failed at byte " + bytes + ", bit " + i);
111 throw new RuntimeException("initialization failed at " + bytes);
112 //System.out.println("cts="+""+" pins=" + pad(Integer.toString(d.readPins()&0xff,2),8));
115 for(int i=0; i<100; i++) {
118 throw new RuntimeException("initialization failed at " + bytes);
119 try { Thread.sleep(20); } catch (Exception e) { }
123 System.out.println();
124 System.out.println("avr reset => false");
126 try { Thread.sleep(500); } catch (Exception e) { }
127 //System.out.println("cts="+""+" pins=" + pad(Integer.toString(d.readPins()&0xff,2),8));
129 //((ChipImpl)d).avr();
131 //System.out.println("avr reset => true");
136 //try { Thread.sleep(500); } catch (Exception e) { }
137 //System.out.println("cts="+""+" pins=" + pad(Integer.toString(d.readPins()&0xff,2),8));
140 public static String pad(String s, int i) {
141 if (s.length() >= i) return s;
142 return "0"+pad(s, i-1);
144 public static String red(Object o) { return "\033[31m"+o+"\033[0m"; }
145 public static String green(Object o) { return "\033[32m"+o+"\033[0m"; }