3a99c03f05bb3e58d9b5541014a49b56da226330
[slipway.git] / src / edu / berkeley / slipway / SerialBoard.java
1 package edu.berkeley.slipway;
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 public class SerialBoard implements Board {
10
11     private final SerialPort sp;
12     private final DataInputStream in;
13     private final DataOutputStream out;
14
15     public SerialBoard(SerialPort sp) throws IOException, UnsupportedCommOperationException, InterruptedException {
16         this.sp = sp;
17         sp.setSerialPortParams(38400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
18         sp.setFlowControlMode(sp.FLOWCONTROL_RTSCTS_OUT);
19         sp.setInputBufferSize(1024);
20         this.out = new DataOutputStream(sp.getOutputStream());
21         this.in = new DataInputStream(sp.getInputStream());
22         Log.debug(this, "consuming any leftover data on the serial port");
23         while(in.available() > 0) in.read();
24         reset();
25     }
26
27     public void reset() {
28         try {
29             Log.info(this, "resetting device");
30             sp.setDTR(true);
31             sp.setRTS(true);
32             Thread.sleep(500);
33             Log.info(this, "deasserting reset signal");
34             sp.setDTR(false);
35             sp.setRTS(false);
36             Thread.sleep(100);
37         } catch (InterruptedException e) { throw new RuntimeException(e); }
38     }
39
40     public void boot(Reader r) throws Exception {
41         throw new Error("not implemented");
42     }
43
44     public InputStream getInputStream() { return in; }
45     public OutputStream getOutputStream() { return out; }
46 }