From 660c8a0a1c0fdd92aa6e42fea6444e774493868f Mon Sep 17 00:00:00 2001 From: adam Date: Mon, 5 Sep 2005 22:20:52 -0700 Subject: [PATCH] checkpoint darcs-hash:20050906052052-5007d-55d70d3e5084d96c540c366f4a1a84e8eba31cce.gz --- src/edu/berkeley/cs/obits/AtmelSerial.java | 48 ++----------- src/edu/berkeley/cs/obits/Device.java | 19 ++++++ .../cs/obits/device/atmel/AtmelDevice.java | 7 ++ .../berkeley/cs/obits/device/atmel/AvrDrone.java | 72 ++++++++++++++++++++ 4 files changed, 103 insertions(+), 43 deletions(-) create mode 100644 src/edu/berkeley/cs/obits/Device.java create mode 100644 src/edu/berkeley/cs/obits/device/atmel/AtmelDevice.java create mode 100644 src/edu/berkeley/cs/obits/device/atmel/AvrDrone.java diff --git a/src/edu/berkeley/cs/obits/AtmelSerial.java b/src/edu/berkeley/cs/obits/AtmelSerial.java index 08b4e48..5550225 100644 --- a/src/edu/berkeley/cs/obits/AtmelSerial.java +++ b/src/edu/berkeley/cs/obits/AtmelSerial.java @@ -1,5 +1,7 @@ package edu.berkeley.cs.obits; +import edu.berkeley.cs.obits.device.atmel.*; +import org.ibex.util.Log; import java.io.*; import java.util.*; import gnu.io.*; @@ -10,51 +12,11 @@ public class AtmelSerial { Enumeration e = CommPortIdentifier.getPortIdentifiers(); while(e.hasMoreElements()) { CommPortIdentifier cpi = (CommPortIdentifier)e.nextElement(); - System.err.println("trying " + cpi.getName()); + Log.info(AtmelSerial.class, "trying " + cpi.getName()); } return new RXTXPort("/dev/cu.usbserial-FTBUODP4"); } - public static class AvrDrone { - final DataInputStream in; - final DataOutputStream out; - final SerialPort sp; - public AvrDrone(SerialPort sp) throws IOException, UnsupportedCommOperationException, InterruptedException { - this.sp = sp; - sp.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); - sp.setFlowControlMode(sp.FLOWCONTROL_RTSCTS_OUT); - this.out = new DataOutputStream(sp.getOutputStream()); - this.in = new DataInputStream(sp.getInputStream()); - while(in.available() > 0) in.read(); - reset(); - System.err.println("waiting..."); - 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"); - System.err.println("ready."); - } - public void reset() throws InterruptedException { - sp.setDTR(true); - Thread.sleep(500); - sp.setDTR(false); - Thread.sleep(3000); - } - public void mode4(int z, int y, int x, int d) throws IOException { - out.writeByte(1); - out.writeByte(z); - out.writeByte(y); - out.writeByte(x); - out.writeByte(d); - } - public void flush() throws IOException { - out.flush(); - } - } - - public static void main(String[] s) throws Exception { AvrDrone device = new AvrDrone(detectObitsPort()); int count = 0; @@ -65,11 +27,11 @@ public class AtmelSerial { long foo = Long.parseLong(str, 16); device.mode4((int)(foo >> 24), (int)(foo >> 16), (int)(foo >> 8), (int)(foo >> 0)); count++; - if (count % 100 == 0) System.err.println("wrote " + count + " configuration octets"); + if (count % 100 == 0) Log.info(AtmelSerial.class, "wrote " + count + " configuration octets"); } device.flush(); long end = System.currentTimeMillis(); - System.err.println("finished in " + ((end-begin)/1000) + "s"); + Log.info(AtmelSerial.class, "finished in " + ((end-begin)/1000) + "s"); System.exit(0); } catch (Exception e) { e.printStackTrace(); } } diff --git a/src/edu/berkeley/cs/obits/Device.java b/src/edu/berkeley/cs/obits/Device.java new file mode 100644 index 0000000..8f26d34 --- /dev/null +++ b/src/edu/berkeley/cs/obits/Device.java @@ -0,0 +1,19 @@ +package edu.berkeley.cs.obits; + +import java.util.*; + +/** a physical or virtual reconfigurable device */ +public interface Device { + + /** reset the device */ + public void reset() throws DeviceException; + + /** flush any commands issued so far, blocking until they have taken effect */ + public void flush() throws DeviceException; + + public static class DeviceException extends Exception { + public DeviceException(String s) { super(s); } + public DeviceException(Throwable t) { super(t); } + } + +} diff --git a/src/edu/berkeley/cs/obits/device/atmel/AtmelDevice.java b/src/edu/berkeley/cs/obits/device/atmel/AtmelDevice.java new file mode 100644 index 0000000..515e9b0 --- /dev/null +++ b/src/edu/berkeley/cs/obits/device/atmel/AtmelDevice.java @@ -0,0 +1,7 @@ +package edu.berkeley.cs.obits.device.atmel; + +import edu.berkeley.cs.obits.*; + +public interface AtmelDevice extends Device { + public void mode4(int z, int y, int x, int d) throws DeviceException; +} diff --git a/src/edu/berkeley/cs/obits/device/atmel/AvrDrone.java b/src/edu/berkeley/cs/obits/device/atmel/AvrDrone.java new file mode 100644 index 0000000..c77f05c --- /dev/null +++ b/src/edu/berkeley/cs/obits/device/atmel/AvrDrone.java @@ -0,0 +1,72 @@ +package edu.berkeley.cs.obits.device.atmel; + +import edu.berkeley.cs.obits.*; +import org.ibex.util.Log; +import java.io.*; +import java.util.*; +import gnu.io.*; + +/** the "host" side of the AVR Drone; see AvrDrone.c for the other side */ +public class AvrDrone implements AtmelDevice { + + final DataInputStream in; + + final DataOutputStream out; + + final SerialPort sp; + + 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.setFlowControlMode(sp.FLOWCONTROL_RTSCTS_OUT); + 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"); + 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"); + } + + public void reset() throws DeviceException { + try { + Log.info(this, "resetting device"); + sp.setDTR(true); + Thread.sleep(500); + sp.setDTR(false); + Thread.sleep(3000); + } catch (InterruptedException e) { throw new DeviceException(e); } + } + + /** issue a command to the device in Mode4 format; see Gosset's documentation for further details */ + public void mode4(int z, int y, int x, int d) throws DeviceException { + try { + Log.debug(this, "writing configuration frame [zyxd]: " + + pad(2, Integer.toString(z, 16)) + " " + + pad(2, Integer.toString(y, 16)) + " " + + pad(2, Integer.toString(x, 16)) + " " + + pad(2, Integer.toString(d, 16)) + ); + out.writeByte(1); + out.writeByte(z); + out.writeByte(y); + out.writeByte(x); + out.writeByte(d); + } catch (IOException e) { throw new DeviceException(e); } + } + + public void flush() throws DeviceException { + try { + out.flush(); + } catch (IOException e) { throw new DeviceException(e); } + } + + private String pad(int i, String s) { if (s.length()>i) return s; return "0"+pad((i-1),s); } + +} -- 1.7.10.4