checkpoint
[slipway.git] / src / edu / berkeley / slipway / FtdiBoard.java
1 package edu.berkeley.slipway;
2
3 import com.atmel.fpslic.*;
4 import edu.berkeley.obits.*;
5 import org.ibex.util.Log;
6 import java.io.*;
7 import java.util.*;
8 import gnu.io.*;
9
10 public class FtdiBoard extends Board {
11
12     static {
13         System.load(new File("build/"+System.mapLibraryName("Ftdi")).getAbsolutePath());
14     }
15
16     private final ChipImpl chip;
17     private final InputStream in;
18     private final OutputStream out;
19
20     public InputStream getInputStream() { return in; }
21     public OutputStream getOutputStream() { return out; }
22
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);
29         out.flush();
30     }
31
32     public void reset() throws IOException {
33         chip.doReset();
34     }
35
36     public void boot(Reader r) throws Exception {
37         boolean pin;
38         Chip d = chip;
39
40         //d.buffered(false);
41
42         d.doReset();
43         d.config(0,3);
44         d.con();
45         d.config(0,7);
46         d.flush();
47         //d.flush();
48         d.config(Integer.parseInt("10110111", 2), 8);
49         d.config(0,1);
50         d.flush();
51         try { Thread.sleep(100); } catch (Exception e) { }
52         pin = d.initErr();
53         System.out.println("good preamble   => " + pin + " " + (pin ? green("good") : red("BAD")));
54
55         d.doReset();
56         try { Thread.sleep(100); } catch (Exception e) { }
57         d.config(0,3);
58         d.con();
59         d.config(0,6);
60         d.flush();
61         //d.flush();
62         d.config(Integer.parseInt("10110111", 2), 8);
63         d.config(0, 2);
64         d.flush();
65         try { Thread.sleep(100); } catch (Exception e) { }
66         pin = d.initErr();
67         System.out.println("bad preamble #2 => " + pin + " " + (pin ? red("BAD") : green("good")));
68
69         d.doReset();
70         try { Thread.sleep(100); } catch (Exception e) { }
71         d.config(0,3);
72         d.con();
73         d.config(0,7);
74         d.flush();
75         //d.flush();
76         d.config(Integer.parseInt("11110111", 2), 8);
77         d.config(0, 1);
78         d.flush();
79         try { Thread.sleep(100); } catch (Exception e) { }
80         pin = d.initErr();
81         System.out.println("bad preamble #1 => " + pin + " " + (pin ? red("BAD") : green("good")));
82
83         d.doReset();
84
85         d.config(0,10);
86         d.con();
87         //d.config(Integer.parseInt("10110111", 2));
88         //d.config(0);
89
90         BufferedReader br = new BufferedReader(r);
91         br.readLine();
92         int bytes = 0;
93         //System.out.println("cts="+""+"  pins=" + pad(Integer.toString(d.readPins()&0xff,2),8));
94         while(true) {
95             String s = br.readLine();
96             if (s==null) break;
97             int in = Integer.parseInt(s, 2);
98             bytes++;
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) {
103                     d.flush();
104                     init = d.initErr();
105                     System.out.print("wrote " + bytes + " bytes, init="+init+"      \r");
106                     d.rcon();
107                 }
108                 if (!init)
109                     throw new RuntimeException("initialization failed at byte " + bytes + ", bit " + i);
110             }
111         }
112
113
114         d.flush();
115         if (!d.initErr())
116             throw new RuntimeException("initialization failed at " + bytes);
117         //System.out.println("cts="+""+"  pins=" + pad(Integer.toString(d.readPins()&0xff,2),8));
118
119
120         for(int i=0; i<100; i++) {
121             d.flush();
122             if (!d.initErr())
123                 throw new RuntimeException("initialization failed at " + bytes);
124             try { Thread.sleep(20); } catch (Exception e) { }
125             d.config(0,1);
126         }
127
128         System.out.println();
129         System.out.println("avr reset => false");
130         d.avrrst(false);
131         try { Thread.sleep(500); } catch (Exception e) { }
132         //System.out.println("cts="+""+"  pins=" + pad(Integer.toString(d.readPins()&0xff,2),8));
133
134         //((ChipImpl)d).avr();
135
136         //System.out.println("avr reset => true");
137         chip.purge();
138         chip.uart_and_cbus_mode(1<<1, 1<<1);
139         
140         //d.avrrst(true);
141         //try { Thread.sleep(500); } catch (Exception e) { }
142         //System.out.println("cts="+""+"  pins=" + pad(Integer.toString(d.readPins()&0xff,2),8));
143     }
144
145     public static String pad(String s, int i) {
146         if (s.length() >= i) return s;
147         return "0"+pad(s, i-1);
148     }
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"; }
151 }