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(SerialPort sp) throws IOException, UnsupportedCommOperationException, InterruptedException, DeviceException {
20 sp.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
21 sp.setFlowControlMode(sp.FLOWCONTROL_RTSCTS_OUT);
22 //sp.setFlowControlMode(sp.FLOWCONTROL_NONE);
23 this.out = new DataOutputStream(sp.getOutputStream());
24 this.in = new DataInputStream(sp.getInputStream());
25 Log.debug(this, "consuming any leftover data on the serial port");
26 while(in.available() > 0) in.read();
28 Log.debug(this, "waiting for device to identify itself");
29 if (in.readByte() != (byte)'O') throw new RuntimeException("didn't get the proper signature");
30 if (in.readByte() != (byte)'B') throw new RuntimeException("didn't get the proper signature");
31 if (in.readByte() != (byte)'I') throw new RuntimeException("didn't get the proper signature");
32 if (in.readByte() != (byte)'T') throw new RuntimeException("didn't get the proper signature");
33 if (in.readByte() != (byte)'S') throw new RuntimeException("didn't get the proper signature");
34 if (in.readByte() != (byte)'\n') throw new RuntimeException("didn't get the proper signature");
35 Log.info(this, "device correctly identified itself; ready for operation");
38 public synchronized void scanFPGA() throws DeviceException {
42 } catch (IOException e) { throw new DeviceException(e); }
44 public synchronized byte readBus() throws DeviceException {
49 } catch (IOException e) { throw new DeviceException(e); }
52 public synchronized void reset() throws DeviceException {
54 Log.info(this, "resetting device");
58 Log.info(this, "deasserting reset signal");
62 } catch (InterruptedException e) { throw new DeviceException(e); }
65 private byte[][][] cache = new byte[24][][];
66 public synchronized byte mode4(int z, int y, int x) throws DeviceException {
67 if (cache[x]==null) return 0;
68 if (cache[x][y]==null) return 0;
69 return cache[x][y][z];
71 public synchronized void mode4(int z, int y, int x, int d) throws DeviceException {
74 Log.info(this, "writing configuration frame [zyxd]: " +
75 pad(1, Integer.toString(z&0xff, 16)) + " " +
76 pad(1, Integer.toString(y&0xff, 16)) + " " +
77 pad(1, Integer.toString(x&0xff, 16)) + " " +
78 pad(1, Integer.toString(d&0xff, 16))
86 if (cache[x & 0xff]==null) cache[x & 0xff] = new byte[24][];
87 if (cache[x & 0xff][y & 0xff]==null) cache[x & 0xff][y & 0xff] = new byte[256];
88 cache[x & 0xff][y & 0xff][z & 0xff] = (byte)(d & 0xff);
89 } catch (IOException e) { throw new DeviceException(e); }
92 public synchronized void flush() throws DeviceException {
95 } catch (IOException e) { throw new DeviceException(e); }
98 private String pad(int i, String s) { if (s.length()>i) return s; return "0"+pad((i-1),s); }