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("FtdiUartNative")).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         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);
32         out.flush();
33     }
34
35     public void reset() throws IOException {
36         chip.doReset();
37     }
38
39     public void boot(Reader r) throws Exception {
40         boolean pin;
41         Chip d = chip;
42
43         //d.buffered(false);
44
45         d.doReset();
46         d.config(0,3);
47         d.con();
48         d.config(0,7);
49         d.flush();
50         //d.flush();
51         d.config(Integer.parseInt("10110111", 2), 8);
52         d.config(0,1);
53         d.flush();
54         try { Thread.sleep(100); } catch (Exception e) { }
55         pin = d.initErr();
56         System.out.println("good preamble   => " + pin + " " + (pin ? green("good") : red("BAD")));
57
58         d.doReset();
59         try { Thread.sleep(100); } catch (Exception e) { }
60         d.config(0,3);
61         d.con();
62         d.config(0,6);
63         d.flush();
64         //d.flush();
65         d.config(Integer.parseInt("10110111", 2), 8);
66         d.config(0, 2);
67         d.flush();
68         try { Thread.sleep(100); } catch (Exception e) { }
69         pin = d.initErr();
70         System.out.println("bad preamble #2 => " + pin + " " + (pin ? red("BAD") : green("good")));
71
72         d.doReset();
73         try { Thread.sleep(100); } catch (Exception e) { }
74         d.config(0,3);
75         d.con();
76         d.config(0,7);
77         d.flush();
78         //d.flush();
79         d.config(Integer.parseInt("11110111", 2), 8);
80         d.config(0, 1);
81         d.flush();
82         try { Thread.sleep(100); } catch (Exception e) { }
83         pin = d.initErr();
84         System.out.println("bad preamble #1 => " + pin + " " + (pin ? red("BAD") : green("good")));
85
86         d.doReset();
87
88         d.config(0,10);
89         d.con();
90         //d.config(Integer.parseInt("10110111", 2));
91         //d.config(0);
92
93         BufferedReader br = new BufferedReader(r);
94         br.readLine();
95         int bytes = 0;
96         //System.out.println("cts="+""+"  pins=" + pad(Integer.toString(d.readPins()&0xff,2),8));
97         while(true) {
98             String s = br.readLine();
99             if (s==null) break;
100             int in = Integer.parseInt(s, 2);
101             bytes++;
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) {
106                     d.flush();
107                     init = d.initErr();
108                     System.out.print("wrote " + bytes + " bytes, init="+init+"      \r");
109                     d.rcon();
110                 }
111                 if (!init)
112                     throw new RuntimeException("initialization failed at byte " + bytes + ", bit " + i);
113             }
114         }
115
116
117         d.flush();
118         if (!d.initErr())
119             throw new RuntimeException("initialization failed at " + bytes);
120         //System.out.println("cts="+""+"  pins=" + pad(Integer.toString(d.readPins()&0xff,2),8));
121
122
123         for(int i=0; i<100; i++) {
124             d.flush();
125             if (!d.initErr())
126                 throw new RuntimeException("initialization failed at " + bytes);
127             try { Thread.sleep(20); } catch (Exception e) { }
128             d.config(0,1);
129         }
130
131         System.out.println();
132         System.out.println("avr reset => false");
133         d.avrrst(false);
134         try { Thread.sleep(500); } catch (Exception e) { }
135         //System.out.println("cts="+""+"  pins=" + pad(Integer.toString(d.readPins()&0xff,2),8));
136
137         //((ChipImpl)d).avr();
138
139         //System.out.println("avr reset => true");
140         chip.purge();
141         chip.uart_and_cbus_mode(1<<1, 1<<1);
142         
143         //d.avrrst(true);
144         //try { Thread.sleep(500); } catch (Exception e) { }
145         //System.out.println("cts="+""+"  pins=" + pad(Integer.toString(d.readPins()&0xff,2),8));
146     }
147
148     public static String pad(String s, int i) {
149         if (s.length() >= i) return s;
150         return "0"+pad(s, i-1);
151     }
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"; }
154 }