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("FtdiUartNative")).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 String bstFile = this.getClass().getName();
26 bstFile = bstFile.substring(0, bstFile.lastIndexOf('.'));
27 bstFile = bstFile.replace('.', '/')+"/slipway_drone.bst";
28 boot(new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream(bstFile)));
29 in = chip.getInputStream();
30 out = chip.getOutputStream();
31 for(int i=0; i<255; i++) out.write(0);
35 public void reset() throws IOException {
39 public void boot(Reader r) throws Exception {
51 d.config(Integer.parseInt("10110111", 2), 8);
54 try { Thread.sleep(100); } catch (Exception e) { }
56 System.out.println("good preamble => " + pin + " " + (pin ? green("good") : red("BAD")));
59 try { Thread.sleep(100); } catch (Exception e) { }
65 d.config(Integer.parseInt("10110111", 2), 8);
68 try { Thread.sleep(100); } catch (Exception e) { }
70 System.out.println("bad preamble #2 => " + pin + " " + (pin ? red("BAD") : green("good")));
73 try { Thread.sleep(100); } catch (Exception e) { }
79 d.config(Integer.parseInt("11110111", 2), 8);
82 try { Thread.sleep(100); } catch (Exception e) { }
84 System.out.println("bad preamble #1 => " + pin + " " + (pin ? red("BAD") : green("good")));
90 //d.config(Integer.parseInt("10110111", 2));
93 BufferedReader br = new BufferedReader(r);
96 //System.out.println("cts="+""+" pins=" + pad(Integer.toString(d.readPins()&0xff,2),8));
98 String s = br.readLine();
100 int in = Integer.parseInt(s, 2);
102 for(int i=7; i>=0; i--) {
103 d.config((((in & 0xff) & (1<<i))!=0)?1:0, 1);
104 boolean init = true; // d.initErr()
105 if (bytes < 100 || (bytes % 1000)==0) {
108 System.out.print("wrote " + bytes + " bytes, init="+init+" \r");
112 throw new RuntimeException("initialization failed at byte " + bytes + ", bit " + i);
119 throw new RuntimeException("initialization failed at " + bytes);
120 //System.out.println("cts="+""+" pins=" + pad(Integer.toString(d.readPins()&0xff,2),8));
123 for(int i=0; i<100; i++) {
126 throw new RuntimeException("initialization failed at " + bytes);
127 try { Thread.sleep(20); } catch (Exception e) { }
131 System.out.println();
132 System.out.println("avr reset => false");
134 try { Thread.sleep(500); } catch (Exception e) { }
135 //System.out.println("cts="+""+" pins=" + pad(Integer.toString(d.readPins()&0xff,2),8));
137 //((ChipImpl)d).avr();
139 //System.out.println("avr reset => true");
141 chip.uart_and_cbus_mode(1<<1, 1<<1);
144 //try { Thread.sleep(500); } catch (Exception e) { }
145 //System.out.println("cts="+""+" pins=" + pad(Integer.toString(d.readPins()&0xff,2),8));
148 public static String pad(String s, int i) {
149 if (s.length() >= i) return s;
150 return "0"+pad(s, i-1);
152 public static String red(Object o) { return "\033[31m"+o+"\033[0m"; }
153 public static String green(Object o) { return "\033[32m"+o+"\033[0m"; }