package edu.berkeley.obits.device.atmel;
+import edu.berkeley.slipway.*;
import edu.berkeley.obits.*;
import org.ibex.util.Log;
import java.io.*;
/** the "host" side of the AVR Drone; see AvrDrone.c for the other side */
public class AvrDrone extends AtmelDevice {
- final DataInputStream in;
-
- final DataOutputStream out;
-
- final SerialPort sp;
-
- public AvrDrone() { sp = null; in = null; out = null; }
- public AvrDrone(SerialPort sp) throws IOException, UnsupportedCommOperationException, InterruptedException, DeviceException {
- this.sp = sp;
- //sp.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
- sp.setSerialPortParams(38400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
- sp.setFlowControlMode(sp.FLOWCONTROL_RTSCTS_OUT);
- sp.setInputBufferSize(1024);
- //sp.setFlowControlMode(sp.FLOWCONTROL_NONE);
- this.out = new DataOutputStream(sp.getOutputStream());
- this.in = new DataInputStream(sp.getInputStream());
- Log.debug(this, "consuming any leftover data on the serial port");
- while(in.available() > 0) in.read();
- reset();
- Log.debug(this, "waiting for device to identify itself");
+ private final DataInputStream in;
+ private final DataOutputStream out;
+ private final Board board;
+
+ public AvrDrone(Board b) throws IOException {
+ this.board = b;
+ this.out = new DataOutputStream(b.getOutputStream());
+ this.in = new DataInputStream(b.getInputStream());
+ init();
+ }
+
+ public void reset() throws DeviceException {
+ try {
+ board.reset();
+ } catch (IOException e) {
+ throw new DeviceException(e);
+ }
+ }
+
+ private void init() throws IOException {
+ //Log.debug(this, "waiting for device to identify itself");
+ /*
if (in.readByte() != (byte)'O') throw new RuntimeException("didn't get the proper signature");
if (in.readByte() != (byte)'B') throw new RuntimeException("didn't get the proper signature");
if (in.readByte() != (byte)'I') throw new RuntimeException("didn't get the proper signature");
if (in.readByte() != (byte)'T') throw new RuntimeException("didn't get the proper signature");
if (in.readByte() != (byte)'S') throw new RuntimeException("didn't get the proper signature");
if (in.readByte() != (byte)'\n') throw new RuntimeException("didn't get the proper signature");
- Log.info(this, "device correctly identified itself; ready for operation");
+ */
+ byte[] bytes = new byte[6];
+ int i=0;
+
+ out.write(0);
+ out.flush();
+
+ // read any crap that might be left in the buffer
+ while(true) {
+ System.arraycopy(bytes, 1, bytes, 0, 5);
+ bytes[5] = in.readByte();
+ i++;
+ System.out.println("got " + new String(bytes));
+ if (bytes[0] == (byte)'O' &&
+ bytes[1] == (byte)'B' &&
+ bytes[2] == (byte)'I' &&
+ bytes[3] == (byte)'T' &&
+ bytes[4] == (byte)'S' &&
+ bytes[5] == (byte)'\n') {
+ System.out.println("got proper signature");
+ break;
+ }
+ }
+ //Log.info(this, "device correctly identified itself; ready for operation");
}
public synchronized void scanFPGA(boolean on) throws DeviceException {
- if (sp==null) return;
try {
if (on) {
out.writeByte(3);
// fixme!
public static int retval = 0;
public synchronized int readCount() throws DeviceException {
- if (sp==null) return 0;
try {
if (reader != null) {
reader.start();
System.out.println("*** reader thread begun");
while(true) {
try {
- byte b = sp==null ? 0 : in.readByte();
+ byte b = in.readByte();
ByteCallback bc = (ByteCallback)callbacks.remove(0);
bc.call(b);
} catch (Exception e) {
public synchronized void readBus(ByteCallback bc) throws DeviceException {
try {
callbacks.add(bc);
- if (sp!=null) {
- out.writeByte(2);
- out.flush();
- }
+ out.writeByte(2);
+ out.flush();
if (reader != null) {
reader.start();
reader = null;
public synchronized void readInterrupts(ByteCallback bc) throws DeviceException {
try {
callbacks.add(bc);
- if (sp!=null) {
- out.writeByte(6);
- out.flush();
- }
+ out.writeByte(6);
+ out.flush();
if (reader != null) {
reader.start();
reader = null;
} catch (IOException e) { throw new DeviceException(e); }
}
- public synchronized void reset() throws DeviceException {
- if (sp==null) return;
- try {
- Log.info(this, "resetting device");
- sp.setDTR(true);
- sp.setRTS(true);
- Thread.sleep(500);
- Log.info(this, "deasserting reset signal");
- sp.setDTR(false);
- sp.setRTS(false);
- Thread.sleep(100);
- } catch (InterruptedException e) { throw new DeviceException(e); }
- }
-
private byte[][][] cache = new byte[24][][];
- public synchronized byte mode4(int z, int y, int x) throws DeviceException {
+ public /*synchronized*/ byte mode4(int z, int y, int x) throws DeviceException {
if (cache[x]==null) return 0;
if (cache[x][y]==null) return 0;
return cache[x][y][z];
int lasty = 0;
public static int save = 0;
public static int saveof = 0;
- public synchronized void mode4(int z, int y, int x, int d) throws DeviceException {
+ public /*synchronized*/ void mode4(int z, int y, int x, int d) throws DeviceException {
try {
/*
Log.info(this, "writing configuration frame [zyxd]: " +
boolean ydec = y==lasty-1;
boolean xdec = x==lastx-1;
- //System.out.println(zchange + " " + ychange + " " + xchange);
- if (sp!=null) {
- out.writeByte(0x80
- | (zinc?0x40:zdec?0x04:zchange?0x44:0x00)
- | (yinc?0x20:ydec?0x02:ychange?0x22:0x00)
- | (xinc?0x10:xdec?0x01:xchange?0x11:0x00));
- if (!zinc && !zdec && zchange) out.writeByte(z); else save++;
- if (!yinc && !ydec && ychange) out.writeByte(y); else save++;
- if (!xinc && !xdec && xchange) out.writeByte(x); else save++;
- saveof++;
- lastz = z;
- lastx = x;
- lasty = y;
- out.writeByte(d);
- }
+ out.writeByte(1);
+ out.writeByte(z);
+ out.writeByte(y);
+ out.writeByte(x);
+ saveof++;
+ lastz = z;
+ lastx = x;
+ lasty = y;
+ out.writeByte(d);
+
if (cache[x & 0xff]==null) cache[x & 0xff] = new byte[24][];
if (cache[x & 0xff][y & 0xff]==null) cache[x & 0xff][y & 0xff] = new byte[256];
cache[x & 0xff][y & 0xff][z & 0xff] = (byte)(d & 0xff);
} catch (IOException e) { throw new DeviceException(e); }
}
- public synchronized void flush() throws DeviceException {
- if (sp==null) return;
+ public /*synchronized*/ void flush() throws DeviceException {
try {
out.flush();
} catch (IOException e) { throw new DeviceException(e); }