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