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