checkpoint
[slipway.git] / src / edu / berkeley / obits / device / atmel / AvrDrone.java
1 package edu.berkeley.obits.device.atmel;
2
3 import edu.berkeley.obits.*;
4 import org.ibex.util.Log;
5 import java.io.*;
6 import java.util.*;
7 import gnu.io.*;
8
9 /** the "host" side of the AVR Drone; see AvrDrone.c for the other side */
10 public class AvrDrone extends AtmelDevice {
11
12     final DataInputStream in;
13
14     final DataOutputStream out;
15
16     final SerialPort sp;
17
18     public AvrDrone(SerialPort sp) throws IOException, UnsupportedCommOperationException, InterruptedException, DeviceException {
19         this.sp = sp;
20         sp.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
21         sp.setFlowControlMode(sp.FLOWCONTROL_RTSCTS_OUT);
22         sp.setInputBufferSize(1024);
23         //sp.setFlowControlMode(sp.FLOWCONTROL_NONE);
24         this.out = new DataOutputStream(sp.getOutputStream());
25         this.in = new DataInputStream(sp.getInputStream());
26         Log.debug(this, "consuming any leftover data on the serial port");
27         while(in.available() > 0) in.read();
28         reset();
29         Log.debug(this, "waiting for device to identify itself");
30         if (in.readByte() != (byte)'O')  throw new RuntimeException("didn't get the proper signature");
31         if (in.readByte() != (byte)'B')  throw new RuntimeException("didn't get the proper signature");
32         if (in.readByte() != (byte)'I')  throw new RuntimeException("didn't get the proper signature");
33         if (in.readByte() != (byte)'T')  throw new RuntimeException("didn't get the proper signature");
34         if (in.readByte() != (byte)'S')  throw new RuntimeException("didn't get the proper signature");
35         if (in.readByte() != (byte)'\n') throw new RuntimeException("didn't get the proper signature");
36         Log.info(this, "device correctly identified itself; ready for operation");
37     }
38
39     public synchronized void scanFPGA(boolean on) throws DeviceException {
40         try {
41             if (on) {
42                 out.writeByte(3);
43                 out.flush();
44             } else {
45                 // FIXME
46             }
47         } catch (IOException e) { throw new DeviceException(e); }
48     }
49
50     public static interface ByteCallback {
51         public void call(byte b) throws Exception;
52     }
53
54     private Vector callbacks = new Vector();
55
56     private Thread reader = new Thread() {
57             public void run() {
58                 while(true) {
59                     try {
60                         byte b = in.readByte();
61                         ByteCallback bc = (ByteCallback)callbacks.remove(0);
62                         bc.call(b);
63                     } catch (Exception e) {
64                         e.printStackTrace();
65                     }
66                 }
67             }
68         };
69
70     public synchronized void readBus(ByteCallback bc) throws DeviceException {
71         try {
72             System.out.println("capacity: " + callbacks.size());
73             callbacks.add(bc);
74             out.writeByte(2);
75             out.flush();
76             if (reader != null) {
77                 reader.start();
78                 reader = null;
79             }
80         } catch (IOException e) { throw new DeviceException(e); }
81     }
82
83     public synchronized void reset() throws DeviceException {
84         try {
85             Log.info(this, "resetting device");
86             sp.setDTR(true);
87             sp.setRTS(true);
88             Thread.sleep(500);
89             Log.info(this, "deasserting reset signal");
90             sp.setDTR(false);
91             sp.setRTS(false);
92             Thread.sleep(100);
93         } catch (InterruptedException e) { throw new DeviceException(e); }
94     }
95
96     private byte[][][] cache = new byte[24][][];
97     public synchronized byte mode4(int z, int y, int x) throws DeviceException {
98         if (cache[x]==null) return 0;
99         if (cache[x][y]==null) return 0;
100         return cache[x][y][z];
101     }
102     public synchronized void mode4(int z, int y, int x, int d) throws DeviceException {
103         try {
104             /*
105             Log.info(this, "writing configuration frame [zyxd]: " +
106                       pad(1, Integer.toString(z&0xff, 16)) + " " +
107                       pad(1, Integer.toString(y&0xff, 16)) + " " +
108                       pad(1, Integer.toString(x&0xff, 16)) + " " +
109                       pad(1, Integer.toString(d&0xff, 16))
110                       );
111             */
112             out.writeByte(1);
113             out.writeByte(z);
114             out.writeByte(y);
115             out.writeByte(x);
116             out.writeByte(d);
117             if (cache[x & 0xff]==null) cache[x & 0xff] = new byte[24][];
118             if (cache[x & 0xff][y & 0xff]==null) cache[x & 0xff][y & 0xff] = new byte[256];
119             cache[x & 0xff][y & 0xff][z & 0xff] = (byte)(d & 0xff);
120         } catch (IOException e) { throw new DeviceException(e); }
121     }
122
123     public synchronized void flush() throws DeviceException {
124         try {
125             out.flush();
126         } catch (IOException e) { throw new DeviceException(e); }
127     }
128
129     private String pad(int i, String s) { if (s.length()>i) return s; return "0"+pad((i-1),s); }
130
131 }