checkpoint
[slipway.git] / src / edu / berkeley / slipway / FtdiBoard.java
1 package edu.berkeley.slipway;
2
3 import com.atmel.fpslic.*;
4 import edu.berkeley.obits.*;
5 import org.ibex.util.Log;
6 import java.io.*;
7 import java.util.*;
8 import gnu.io.*;
9
10 public class FtdiBoard extends Board {
11
12     static {
13         System.load(new File("build/"+System.mapLibraryName("FtdiUartNative")).getAbsolutePath());
14     }
15
16     private final Chip chip;
17     private final InputStream in;
18     private final OutputStream out;
19
20     public InputStream getInputStream() { return in; }
21     public OutputStream getOutputStream() { return out; }
22
23     public FtdiBoard() throws Exception {
24         chip = new ChipImpl();
25         String bstFile = this.getClass().getName();
26         bstFile = bstFile.substring(0, bstFile.lastIndexOf('.'));
27         bstFile = bstFile.replace('.', '/')+"/slipway_drone.bst";
28         boot(new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream(bstFile)));
29         in = chip.getInputStream();
30         out = chip.getOutputStream();
31         for(int i=0; i<255; i++) out.write(0);
32         out.flush();
33     }
34
35     public void reset() throws IOException {
36         chip.doReset();
37     }
38
39     public void boot(Reader r) throws Exception {
40         Chip d = chip;
41
42         //d.buffered(false);
43
44         d.selfTest();
45
46         d.doReset();
47
48         d.config(0,10);
49         d.con();
50         //d.config(Integer.parseInt("10110111", 2));
51         //d.config(0);
52
53         BufferedReader br = new BufferedReader(r);
54         br.readLine();
55         int bytes = 0;
56         //System.out.println("cts="+""+"  pins=" + pad(Integer.toString(d.readPins()&0xff,2),8));
57         while(true) {
58             String s = br.readLine();
59             if (s==null) break;
60             int in = Integer.parseInt(s, 2);
61             bytes++;
62             for(int i=7; i>=0; i--) {
63                 d.config((((in & 0xff) & (1<<i))!=0)?1:0, 1);
64                 boolean init = true; // d.initErr()
65                 if (bytes < 100 || (bytes % 1000)==0) {
66                     d.flush();
67                     init = d.initErr();
68                     System.out.print("wrote " + bytes + " bytes, init="+init+"      \r");
69                     d.rcon();
70                 }
71                 if (!init)
72                     throw new RuntimeException("initialization failed at byte " + bytes + ", bit " + i);
73             }
74         }
75
76
77         d.flush();
78         if (!d.initErr())
79             throw new RuntimeException("initialization failed at " + bytes);
80         //System.out.println("cts="+""+"  pins=" + pad(Integer.toString(d.readPins()&0xff,2),8));
81
82
83         for(int i=0; i<100; i++) {
84             d.flush();
85             if (!d.initErr())
86                 throw new RuntimeException("initialization failed at " + bytes);
87             try { Thread.sleep(20); } catch (Exception e) { }
88             d.config(0,1);
89         }
90
91         System.out.println();
92         System.out.println("avr reset => false");
93         d.avrrst(false);
94         try { Thread.sleep(500); } catch (Exception e) { }
95         //System.out.println("cts="+""+"  pins=" + pad(Integer.toString(d.readPins()&0xff,2),8));
96
97         //((Chip)d).avr();
98
99         //System.out.println("avr reset => true");
100         ((ChipImpl)chip).purge();
101         ((ChipImpl)chip).uart_and_cbus_mode(1<<1, 1<<1);
102         
103         //d.avrrst(true);
104         //try { Thread.sleep(500); } catch (Exception e) { }
105         //System.out.println("cts="+""+"  pins=" + pad(Integer.toString(d.readPins()&0xff,2),8));
106     }
107
108     public static String pad(String s, int i) {
109         if (s.length() >= i) return s;
110         return "0"+pad(s, i-1);
111     }
112 }