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