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