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();
26 boot(new InputStreamReader(new FileInputStream("bitstreams/usbdrone.bst")));
28 in = chip.getInputStream();
29 out = chip.getOutputStream();
30 for(int i=0; i<255; i++) out.write(0);
38 public void boot(Reader r) throws Exception {
44 d.config(Integer.parseInt("10110111", 2), 8);
47 System.out.println("good preamble => " + pin + " " + (pin ? green("good") : red("BAD")));
52 d.config(Integer.parseInt("10110111", 2), 8);
55 System.out.println("bad preamble #2 => " + pin + " " + (pin ? red("BAD") : green("good")));
60 d.config(Integer.parseInt("11110111", 2), 8);
63 System.out.println("bad preamble #1 => " + pin + " " + (pin ? red("BAD") : green("good")));
70 //d.config(Integer.parseInt("10110111", 2));
73 BufferedReader br = new BufferedReader(r);
77 //System.out.println("cts="+""+" pins=" + pad(Integer.toString(d.readPins()&0xff,2),8));
79 String s = br.readLine();
81 int in = Integer.parseInt(s, 2);
83 for(int i=7; i>=0; i--) {
84 d.config((((in & 0xff) & (1<<i))!=0)?1:0, 1);
85 boolean init = true; // d.initErr()
86 if (bytes < 100 || (bytes % 1000)==0) {
89 System.out.print("wrote " + bytes + " bytes, init="+init+" \r");
92 throw new RuntimeException("initialization failed at byte " + bytes + ", bit " + i);
98 throw new RuntimeException("initialization failed at " + bytes);
99 //System.out.println("cts="+""+" pins=" + pad(Integer.toString(d.readPins()&0xff,2),8));
102 for(int i=0; i<100; i++) {
105 throw new RuntimeException("initialization failed at " + bytes);
106 try { Thread.sleep(20); } catch (Exception e) { }
110 System.out.println();
111 System.out.println("avr reset => false");
113 try { Thread.sleep(500); } catch (Exception e) { }
114 //System.out.println("cts="+""+" pins=" + pad(Integer.toString(d.readPins()&0xff,2),8));
116 //((ChipImpl)d).avr();
118 //System.out.println("avr reset => true");
123 //try { Thread.sleep(500); } catch (Exception e) { }
124 //System.out.println("cts="+""+" pins=" + pad(Integer.toString(d.readPins()&0xff,2),8));
127 public static String pad(String s, int i) {
128 if (s.length() >= i) return s;
129 return "0"+pad(s, i-1);
131 public static String red(Object o) { return "\033[31m"+o+"\033[0m"; }
132 public static String green(Object o) { return "\033[32m"+o+"\033[0m"; }