checkpoing
[eltron.git] / src / edu / berkeley / cs / obits / device / atmel / AvrDrone.java
1 package edu.berkeley.cs.obits.device.atmel;
2
3 import edu.berkeley.cs.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         this.out = new DataOutputStream(sp.getOutputStream());
23         this.in = new DataInputStream(sp.getInputStream());
24         Log.debug(this, "consuming any leftover data on the serial port");
25         while(in.available() > 0) in.read();
26         reset();
27         Log.debug(this, "waiting for device to identify itself");
28         if (in.readByte() != (byte)'O')  throw new RuntimeException("didn't get the proper signature");
29         if (in.readByte() != (byte)'B')  throw new RuntimeException("didn't get the proper signature");
30         if (in.readByte() != (byte)'I')  throw new RuntimeException("didn't get the proper signature");
31         if (in.readByte() != (byte)'T')  throw new RuntimeException("didn't get the proper signature");
32         if (in.readByte() != (byte)'S')  throw new RuntimeException("didn't get the proper signature");
33         if (in.readByte() != (byte)'\n') throw new RuntimeException("didn't get the proper signature");
34         Log.info(this, "device correctly identified itself; ready for operation");
35     }
36
37     public void reset() throws DeviceException {
38         try {
39             Log.info(this, "resetting device");
40             sp.setDTR(true);
41             Thread.sleep(500);
42             sp.setDTR(false);
43             Thread.sleep(3000);
44         } catch (InterruptedException e) { throw new DeviceException(e); }
45     }
46
47     public void mode4(int z, int y, int x, int d) throws DeviceException {
48         try {
49             Log.debug(this, "writing configuration frame [zyxd]: " +
50                       pad(2, Integer.toString(z, 16)) + " " +
51                       pad(2, Integer.toString(y, 16)) + " " +
52                       pad(2, Integer.toString(x, 16)) + " " +
53                       pad(2, Integer.toString(d, 16))
54                       );
55             out.writeByte(1);
56             out.writeByte(z);
57             out.writeByte(y);
58             out.writeByte(x);
59             out.writeByte(d);
60         } catch (IOException e) { throw new DeviceException(e); }
61     }
62
63     public void flush() throws DeviceException {
64         try {
65             out.flush();
66         } catch (IOException e) { throw new DeviceException(e); }
67     }
68
69     private String pad(int i, String s) { if (s.length()>i) return s; return "0"+pad((i-1),s); }
70
71 }