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;
19 public AvrDrone() { sp = null; in = null; out = null; isFake = true; }
21 public AvrDrone(InputStream is, OutputStream os) throws IOException {
22 this.out = new DataOutputStream(os);
23 this.in = new DataInputStream(is);
29 public AvrDrone(SerialPort sp) throws IOException, UnsupportedCommOperationException, InterruptedException, DeviceException {
31 //sp.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
32 sp.setSerialPortParams(38400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
33 sp.setFlowControlMode(sp.FLOWCONTROL_RTSCTS_OUT);
34 sp.setInputBufferSize(1024);
35 //sp.setFlowControlMode(sp.FLOWCONTROL_NONE);
36 this.out = new DataOutputStream(sp.getOutputStream());
37 this.in = new DataInputStream(sp.getInputStream());
38 Log.debug(this, "consuming any leftover data on the serial port");
39 while(in.available() > 0) in.read();
45 private void init() throws IOException {
46 Log.debug(this, "waiting for device to identify itself");
47 if (in.readByte() != (byte)'O') throw new RuntimeException("didn't get the proper signature");
48 if (in.readByte() != (byte)'B') throw new RuntimeException("didn't get the proper signature");
49 if (in.readByte() != (byte)'I') throw new RuntimeException("didn't get the proper signature");
50 if (in.readByte() != (byte)'T') throw new RuntimeException("didn't get the proper signature");
51 if (in.readByte() != (byte)'S') throw new RuntimeException("didn't get the proper signature");
52 if (in.readByte() != (byte)'\n') throw new RuntimeException("didn't get the proper signature");
53 Log.info(this, "device correctly identified itself; ready for operation");
56 public synchronized void scanFPGA(boolean on) throws DeviceException {
65 } catch (IOException e) { throw new DeviceException(e); }
68 public static int retval = 0;
69 public synchronized int readCount() throws DeviceException {
76 ByteCallback bc = new ByteCallback() {
77 public synchronized void call(byte b) throws Exception {
80 ((in.read() & 0xff) << 16) |
81 ((in.read() & 0xff) << 8) |
82 ((in.read() & 0xff) << 0);
93 } catch (Exception e) { throw new DeviceException(e); }
96 public static interface ByteCallback {
97 public void call(byte b) throws Exception;
100 private Vector callbacks = new Vector();
102 private Thread reader = new Thread() {
104 System.out.println("*** reader thread begun");
107 byte b = isFake ? 0 : in.readByte();
108 ByteCallback bc = (ByteCallback)callbacks.remove(0);
110 } catch (Exception e) {
117 public synchronized void readBus(ByteCallback bc) throws DeviceException {
124 if (reader != null) {
128 } catch (IOException e) { throw new DeviceException(e); }
131 public synchronized void readInterrupts(ByteCallback bc) throws DeviceException {
138 if (reader != null) {
142 } catch (IOException e) { throw new DeviceException(e); }
145 public synchronized void reset() throws DeviceException {
146 if (sp==null) return;
148 Log.info(this, "resetting device");
152 Log.info(this, "deasserting reset signal");
156 } catch (InterruptedException e) { throw new DeviceException(e); }
159 private byte[][][] cache = new byte[24][][];
160 public synchronized byte mode4(int z, int y, int x) throws DeviceException {
161 if (cache[x]==null) return 0;
162 if (cache[x][y]==null) return 0;
163 return cache[x][y][z];
169 public static int save = 0;
170 public static int saveof = 0;
171 public synchronized void mode4(int z, int y, int x, int d) throws DeviceException {
174 Log.info(this, "writing configuration frame [zyxd]: " +
175 pad(1, Integer.toString(z&0xff, 16)) + " " +
176 pad(1, Integer.toString(y&0xff, 16)) + " " +
177 pad(1, Integer.toString(x&0xff, 16)) + " " +
178 pad(1, Integer.toString(d&0xff, 16))
181 boolean zchange = z!=lastz;
182 boolean ychange = y!=lasty;
183 boolean xchange = x!=lastx;
184 boolean zinc = z==lastz+1;
185 boolean yinc = y==lasty+1;
186 boolean xinc = x==lastx+1;
187 boolean zdec = z==lastz-1;
188 boolean ydec = y==lasty-1;
189 boolean xdec = x==lastx-1;
191 //System.out.println(zchange + " " + ychange + " " + xchange);
195 | (zinc?0x40:zdec?0x04:zchange?0x44:0x00)
196 | (yinc?0x20:ydec?0x02:ychange?0x22:0x00)
197 | (xinc?0x10:xdec?0x01:xchange?0x11:0x00));
198 if (!zinc && !zdec && zchange) out.writeByte(z); else save++;
199 if (!yinc && !ydec && ychange) out.writeByte(y); else save++;
200 if (!xinc && !xdec && xchange) out.writeByte(x); else save++;
212 if (cache[x & 0xff]==null) cache[x & 0xff] = new byte[24][];
213 if (cache[x & 0xff][y & 0xff]==null) cache[x & 0xff][y & 0xff] = new byte[256];
214 cache[x & 0xff][y & 0xff][z & 0xff] = (byte)(d & 0xff);
215 } catch (IOException e) { throw new DeviceException(e); }
218 public synchronized void flush() throws DeviceException {
222 } catch (IOException e) { throw new DeviceException(e); }
225 private String pad(int i, String s) { if (s.length()>i) return s; return "0"+pad((i-1),s); }