5d1aec534d9040955499dc82e0fb31501119120a
[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.doReset();
40         d.config(0,10);
41         d.con();
42         d.flush();
43         d.config(Integer.parseInt("10110111", 2), 8);
44         d.config(0,1);
45         d.flush();
46         pin = d.initErr();
47         System.out.println("good preamble   => " + pin + " " + (pin ? green("good") : red("BAD")));
48
49         d.doReset();
50         d.config(0,9);
51         d.con();
52         d.flush();
53         d.config(Integer.parseInt("10110111", 2), 8);
54         d.config(0, 2);
55         d.flush();
56         pin = d.initErr();
57         System.out.println("bad preamble #2 => " + pin + " " + (pin ? red("BAD") : green("good")));
58
59         d.doReset();
60         d.config(0,10);
61         d.con();
62         d.flush();
63         d.config(Integer.parseInt("11110111", 2), 8);
64         d.config(0, 1);
65         d.flush();
66         pin = d.initErr();
67         System.out.println("bad preamble #1 => " + pin + " " + (pin ? red("BAD") : green("good")));
68
69         d.doReset();
70
71         d.config(0,10);
72         d.con();
73         //d.config(Integer.parseInt("10110111", 2));
74         //d.config(0);
75
76         BufferedReader br = new BufferedReader(r);
77         br.readLine();
78         int bytes = 0;
79         d.buffered();
80         //System.out.println("cts="+""+"  pins=" + pad(Integer.toString(d.readPins()&0xff,2),8));
81         while(true) {
82             String s = br.readLine();
83             if (s==null) break;
84             int in = Integer.parseInt(s, 2);
85             bytes++;
86             for(int i=7; i>=0; i--) {
87                 d.config((((in & 0xff) & (1<<i))!=0)?1:0, 1);
88                 boolean init = true; // d.initErr()
89                 if (bytes < 100 || (bytes % 1000)==0) {
90                     d.flush();
91                     init = d.initErr();
92                     System.out.print("wrote " + bytes + " bytes, init="+init+"      \r");
93                 }
94                 if (!init)
95                     throw new RuntimeException("initialization failed at byte " + bytes + ", bit " + i);
96             }
97         }
98
99         d.flush();
100         if (!d.initErr())
101             throw new RuntimeException("initialization failed at " + bytes);
102         //System.out.println("cts="+""+"  pins=" + pad(Integer.toString(d.readPins()&0xff,2),8));
103
104
105         for(int i=0; i<100; i++) {
106             d.flush();
107             if (!d.initErr())
108                 throw new RuntimeException("initialization failed at " + bytes);
109             try { Thread.sleep(20); } catch (Exception e) { }
110             d.config(0,1);
111         }
112
113         System.out.println();
114         System.out.println("avr reset => false");
115         d.avrrst(false);
116         try { Thread.sleep(500); } catch (Exception e) { }
117         //System.out.println("cts="+""+"  pins=" + pad(Integer.toString(d.readPins()&0xff,2),8));
118
119         //((ChipImpl)d).avr();
120
121         //System.out.println("avr reset => true");
122         chip.uart();
123         chip.purge();
124         
125         //d.avrrst(true);
126         //try { Thread.sleep(500); } catch (Exception e) { }
127         //System.out.println("cts="+""+"  pins=" + pad(Integer.toString(d.readPins()&0xff,2),8));
128     }
129
130     public static String pad(String s, int i) {
131         if (s.length() >= i) return s;
132         return "0"+pad(s, i-1);
133     }
134     public static String red(Object o) { return "\033[31m"+o+"\033[0m"; }
135     public static String green(Object o) { return "\033[32m"+o+"\033[0m"; }
136 }