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.Log;
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 FpslicRawUsb(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         OutputStream os = chip.getConfigStream();
45         BufferedReader br = new BufferedReader(r);
46
47         int bytes = 0;
48         while(true) {
49             String s = br.readLine();
50             if (s==null) break;
51             bytes++;
52             os.write((byte)Integer.parseInt(s, 2));
53             if ((bytes % 1000)==0) {
54                 os.flush();
55                 System.out.print("wrote " + bytes + " bytes\r");
56             }
57         }
58         os.close();
59     }
60
61     public static String pad(String s, int i) {
62         if (s.length() >= i) return s;
63         return "0"+pad(s, i-1);
64     }
65 }