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