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 FpslicRaw 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 FpslicRawUsb();
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.reset();
37     }
38
39     public void boot(Reader r) throws Exception {
40
41         chip.selfTest();
42
43         OutputStream os = chip.getConfigStream();
44         BufferedReader br = new BufferedReader(r);
45         br.readLine();
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 }