9e7da7e3175654bf3c73149a60ef4784a703b36b
[slipway.git] / src / edu / berkeley / obits / device / atmel / AvrDrone.java
1 package edu.berkeley.obits.device.atmel;
2
3 import edu.berkeley.obits.*;
4 import org.ibex.util.Log;
5 import java.io.*;
6 import java.util.*;
7 import gnu.io.*;
8
9 /** the "host" side of the AVR Drone; see AvrDrone.c for the other side */
10 public class AvrDrone extends AtmelDevice {
11
12     final DataInputStream in;
13
14     final DataOutputStream out;
15
16     final SerialPort sp;
17
18     public AvrDrone(SerialPort sp) throws IOException, UnsupportedCommOperationException, InterruptedException, DeviceException {
19         this.sp = sp;
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();
27         reset();
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");
36     }
37
38     public synchronized void scanFPGA() throws DeviceException {
39         try {
40             out.writeByte(3);
41             out.flush();
42         } catch (IOException e) { throw new DeviceException(e); }
43     }
44
45     public synchronized byte readBus() throws DeviceException {
46         try {
47             out.writeByte(2);
48             out.flush();
49             return in.readByte();
50         } catch (IOException e) { throw new DeviceException(e); }
51     }
52
53     public synchronized void reset() throws DeviceException {
54         try {
55             Log.info(this, "resetting device");
56             sp.setDTR(true);
57             sp.setRTS(true);
58             Thread.sleep(500);
59             Log.info(this, "deasserting reset signal");
60             sp.setDTR(false);
61             sp.setRTS(false);
62             Thread.sleep(100);
63         } catch (InterruptedException e) { throw new DeviceException(e); }
64     }
65
66     private byte[][][] cache = new byte[24][][];
67     public synchronized byte mode4(int z, int y, int x) throws DeviceException {
68         if (cache[x]==null) return 0;
69         if (cache[x][y]==null) return 0;
70         return cache[x][y][z];
71     }
72     public synchronized void mode4(int z, int y, int x, int d) throws DeviceException {
73         try {
74             /*
75             Log.info(this, "writing configuration frame [zyxd]: " +
76                       pad(1, Integer.toString(z&0xff, 16)) + " " +
77                       pad(1, Integer.toString(y&0xff, 16)) + " " +
78                       pad(1, Integer.toString(x&0xff, 16)) + " " +
79                       pad(1, Integer.toString(d&0xff, 16))
80                       );
81             */
82             out.writeByte(1);
83             out.writeByte(z);
84             out.writeByte(y);
85             out.writeByte(x);
86             out.writeByte(d);
87             if (cache[x & 0xff]==null) cache[x & 0xff] = new byte[24][];
88             if (cache[x & 0xff][y & 0xff]==null) cache[x & 0xff][y & 0xff] = new byte[256];
89             cache[x & 0xff][y & 0xff][z & 0xff] = (byte)(d & 0xff);
90         } catch (IOException e) { throw new DeviceException(e); }
91     }
92
93     public synchronized void flush() throws DeviceException {
94         try {
95             out.flush();
96         } catch (IOException e) { throw new DeviceException(e); }
97     }
98
99     private String pad(int i, String s) { if (s.length()>i) return s; return "0"+pad((i-1),s); }
100
101 }