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.selfTest();
43
44         OutputStream os = d.getConfigStream();
45
46         BufferedReader br = new BufferedReader(r);
47         br.readLine();
48
49         int bytes = 0;
50         while(true) {
51             String s = br.readLine();
52             if (s==null) break;
53             int in = Integer.parseInt(s, 2);
54             bytes++;
55             os.write((byte)in);
56             if ((bytes % 1000)==0) {
57                 os.flush();
58                 System.out.print("wrote " + bytes + " bytes\r");
59                 d.rcon();
60             }
61         }
62
63         d.flush();
64         if (!d.initErr())
65             throw new RuntimeException("initialization failed at " + bytes);
66
67         for(int i=0; i<100; i++) {
68             d.flush();
69             if (!d.initErr())
70                 throw new RuntimeException("initialization failed at " + bytes);
71             try { Thread.sleep(20); } catch (Exception e) { }
72             d.config(0,1);
73         }
74
75         System.out.println();
76         System.out.println("avr reset => false");
77         d.avrrst(false);
78         try { Thread.sleep(500); } catch (Exception e) { }
79         //System.out.println("cts="+""+"  pins=" + pad(Integer.toString(d.readPins()&0xff,2),8));
80
81         //((Chip)d).avr();
82
83         //System.out.println("avr reset => true");
84         ((ChipImpl)chip).purge();
85         ((ChipImpl)chip).uart_and_cbus_mode(1<<1, 1<<1);
86         
87         //d.avrrst(true);
88         //try { Thread.sleep(500); } catch (Exception e) { }
89         //System.out.println("cts="+""+"  pins=" + pad(Integer.toString(d.readPins()&0xff,2),8));
90     }
91
92     public static String pad(String s, int i) {
93         if (s.length() >= i) return s;
94         return "0"+pad(s, i-1);
95     }
96 }