checkpoint
[slipway.git] / src / edu / berkeley / slipway / FtdiBoard.java
1 package edu.berkeley.slipway;
2
3 import com.ftdi.usb.*;
4 import com.atmel.fpslic.*;
5 import edu.berkeley.obits.*;
6 import org.ibex.util.*;
7 import java.io.*;
8 import java.util.*;
9 import gnu.io.*;
10
11 public class FtdiBoard extends Board {
12
13     static {
14         System.load(new File("build/"+System.mapLibraryName("FtdiUartNative")).getAbsolutePath());
15     }
16
17     private final FpslicRaw chip;
18     private final InputStream in;
19     private final OutputStream out;
20
21     public InputStream getInputStream() { return in; }
22     public OutputStream getOutputStream() { return out; }
23
24     public FtdiBoard() throws Exception {
25         chip = new FpslicRaw(new FpslicPinsUsb(new FtdiUart(0x6666, 0x3133, 1500 * 1000)));
26         String bstFile = this.getClass().getName();
27         bstFile = bstFile.substring(0, bstFile.lastIndexOf('.'));
28         bstFile = bstFile.replace('.', '/')+"/slipway_drone.bst";
29         boot(new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream(bstFile)));
30         in = chip.getInputStream();
31         out = chip.getOutputStream();
32         for(int i=0; i<255; i++) out.write(0);
33         out.flush();
34     }
35
36     public void reset() throws IOException {
37         chip.reset();
38     }
39
40     public void boot(Reader r) throws Exception {
41
42         chip.selfTest();
43
44         int total = 75090/9;
45         OutputStream os = new ProgressOutputStream("bootstrap bitstream:", chip.getConfigStream(), total);
46         BufferedReader br = new BufferedReader(r);
47
48         int bytes = 0;
49         while(true) {
50             String s = br.readLine();
51             if (s==null) break;
52             bytes++;
53             os.write((byte)Integer.parseInt(s, 2));
54             if ((bytes % 1000)==0) os.flush();
55         }
56         os.close();
57     }
58
59     public static String pad(String s, int i) {
60         if (s.length() >= i) return s;
61         return "0"+pad(s, i-1);
62     }
63 }