1 package edu.berkeley.obits.device.atmel;
3 import edu.berkeley.obits.*;
4 import org.ibex.util.Log;
9 /** the "host" side of the AVR Drone; see AvrDrone.c for the other side */
10 public class AvrDrone extends AtmelDevice {
12 final DataInputStream in;
14 final DataOutputStream out;
18 public AvrDrone() { sp = null; in = null; out = null; }
19 public AvrDrone(SerialPort sp) throws IOException, UnsupportedCommOperationException, InterruptedException, DeviceException {
21 //sp.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
22 sp.setSerialPortParams(38400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
23 sp.setFlowControlMode(sp.FLOWCONTROL_RTSCTS_OUT);
24 sp.setInputBufferSize(1024);
25 //sp.setFlowControlMode(sp.FLOWCONTROL_NONE);
26 this.out = new DataOutputStream(sp.getOutputStream());
27 this.in = new DataInputStream(sp.getInputStream());
28 Log.debug(this, "consuming any leftover data on the serial port");
29 while(in.available() > 0) in.read();
31 Log.debug(this, "waiting for device to identify itself");
32 if (in.readByte() != (byte)'O') throw new RuntimeException("didn't get the proper signature");
33 if (in.readByte() != (byte)'B') throw new RuntimeException("didn't get the proper signature");
34 if (in.readByte() != (byte)'I') throw new RuntimeException("didn't get the proper signature");
35 if (in.readByte() != (byte)'T') throw new RuntimeException("didn't get the proper signature");
36 if (in.readByte() != (byte)'S') throw new RuntimeException("didn't get the proper signature");
37 if (in.readByte() != (byte)'\n') throw new RuntimeException("didn't get the proper signature");
38 Log.info(this, "device correctly identified itself; ready for operation");
41 public synchronized void scanFPGA(boolean on) throws DeviceException {
50 } catch (IOException e) { throw new DeviceException(e); }
53 public static int retval = 0;
54 public synchronized int readCount() throws DeviceException {
55 if (sp==null) return 0;
61 ByteCallback bc = new ByteCallback() {
62 public synchronized void call(byte b) throws Exception {
65 ((in.read() & 0xff) << 16) |
66 ((in.read() & 0xff) << 8) |
67 ((in.read() & 0xff) << 0);
78 } catch (Exception e) { throw new DeviceException(e); }
81 public static interface ByteCallback {
82 public void call(byte b) throws Exception;
85 private Vector callbacks = new Vector();
87 private Thread reader = new Thread() {
91 byte b = sp==null ? 0 : in.readByte();
92 ByteCallback bc = (ByteCallback)callbacks.remove(0);
94 } catch (Exception e) {
101 public synchronized void readBus(ByteCallback bc) throws DeviceException {
108 if (reader != null) {
112 } catch (IOException e) { throw new DeviceException(e); }
115 public synchronized void readInterrupts(ByteCallback bc) throws DeviceException {
122 if (reader != null) {
126 } catch (IOException e) { throw new DeviceException(e); }
129 public synchronized void reset() throws DeviceException {
130 if (sp==null) return;
132 Log.info(this, "resetting device");
136 Log.info(this, "deasserting reset signal");
140 } catch (InterruptedException e) { throw new DeviceException(e); }
143 private byte[][][] cache = new byte[24][][];
144 public synchronized byte mode4(int z, int y, int x) throws DeviceException {
145 if (cache[x]==null) return 0;
146 if (cache[x][y]==null) return 0;
147 return cache[x][y][z];
153 public static int save = 0;
154 public static int saveof = 0;
155 public synchronized void mode4(int z, int y, int x, int d) throws DeviceException {
158 Log.info(this, "writing configuration frame [zyxd]: " +
159 pad(1, Integer.toString(z&0xff, 16)) + " " +
160 pad(1, Integer.toString(y&0xff, 16)) + " " +
161 pad(1, Integer.toString(x&0xff, 16)) + " " +
162 pad(1, Integer.toString(d&0xff, 16))
165 boolean zchange = z!=lastz;
166 boolean ychange = y!=lasty;
167 boolean xchange = x!=lastx;
168 boolean zinc = z==lastz+1;
169 boolean yinc = y==lasty+1;
170 boolean xinc = x==lastx+1;
171 boolean zdec = z==lastz-1;
172 boolean ydec = y==lasty-1;
173 boolean xdec = x==lastx-1;
175 //System.out.println(zchange + " " + ychange + " " + xchange);
178 | (zinc?0x40:zdec?0x04:zchange?0x44:0x00)
179 | (yinc?0x20:ydec?0x02:ychange?0x22:0x00)
180 | (xinc?0x10:xdec?0x01:xchange?0x11:0x00));
181 if (!zinc && !zdec && zchange) out.writeByte(z); else save++;
182 if (!yinc && !ydec && ychange) out.writeByte(y); else save++;
183 if (!xinc && !xdec && xchange) out.writeByte(x); else save++;
190 if (cache[x & 0xff]==null) cache[x & 0xff] = new byte[24][];
191 if (cache[x & 0xff][y & 0xff]==null) cache[x & 0xff][y & 0xff] = new byte[256];
192 cache[x & 0xff][y & 0xff][z & 0xff] = (byte)(d & 0xff);
193 } catch (IOException e) { throw new DeviceException(e); }
196 public synchronized void flush() throws DeviceException {
197 if (sp==null) return;
200 } catch (IOException e) { throw new DeviceException(e); }
203 private String pad(int i, String s) { if (s.length()>i) return s; return "0"+pad((i-1),s); }