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