major overhaul of FPGA code to support both ML509 and Bee2 at the same time
[fleet.git] / src / edu / berkeley / fleet / fpga / Server.java
index 03b0442..3122b32 100644 (file)
@@ -4,15 +4,18 @@ import gnu.io.*;
 import java.io.*;
 import java.net.*;
 import java.util.*;
+import java.util.concurrent.Semaphore;
 
+// FIXME: accept connections, but stall, during programming
 public class Server {
+    static boolean sign = false;
 
     static long jarFileTime = 0;
     static long bitFileTime = 0;
     static {
         try {
             jarFileTime = new File("fleet.jar").lastModified();
-            bitFileTime = new File("build/fpga/main.bit").lastModified();
+            bitFileTime = new File("./main.bit").lastModified();
         } catch (Exception e) { throw new RuntimeException(e); }
     }
 
@@ -20,8 +23,8 @@ public class Server {
     public static void main(String[] args) throws Exception {
         System.err.println("programming...");
         Process proc = Runtime.getRuntime().exec(new String[] {
-                "misc/program.sh",
-                "build/fpga/main.bit"
+                "./misc/program.sh",
+                "./build/ml509.small/main.bit"
             });
         BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
         String str = null;
@@ -43,7 +46,7 @@ public class Server {
                             System.err.println("jarfile modified; exiting...");
                             System.exit(0);
                         }
-                        if (bitFileTime != new File("build/fpga/main.bit").lastModified()) {
+                        if (bitFileTime != new File("./main.bit").lastModified()) {
                             System.err.println("bitfile modified; exiting...");
                             System.exit(0);
                         }
@@ -55,6 +58,7 @@ public class Server {
         while(true) {
             try {
                 Socket s = ss.accept();
+                System.out.println("accept!");
                 new Handler(s).start();
             } catch (Exception e) {
                 e.printStackTrace();
@@ -63,17 +67,26 @@ public class Server {
     }
 
     public static String pass_string = "password=security_is_for_wimps ";
-
     static class Handler extends Thread {
         private Socket socket;
         boolean closed = false;
         private SerialPort sp;
         public Handler(Socket s) { this.socket = s; this.sp = sp; }
         public void run() {
+            System.err.println("waiting for Server.class lock...");
             synchronized(Server.class) {
+                System.err.println("   (got it)");
                 try {
-                    this.sp = new RXTXPort("/dev/ttyS0");
-                    sp.sendBreak(500);
+                    if (new File("/dev/cu.usbserial-FTCBWI2P").exists())
+                        this.sp = new RXTXPort("/dev/cu.usbserial-FTCBWI2P");
+                    else
+                        this.sp = new RXTXPort("/dev/ttyS0");
+                    sp.setInputBufferSize(0);
+                    sp.setOutputBufferSize(0);
+                    //sp.setSerialPortParams(38400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
+                    sp.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
+                    //sp.setFlowControlMode(sp.FLOWCONTROL_RTSCTS_IN | sp.FLOWCONTROL_RTSCTS_OUT);
+                    sp.setFlowControlMode(0);
                     try {
                         _run();
                     } finally {
@@ -84,7 +97,14 @@ public class Server {
         }
         public void _run() {
             try {
-                final InputStream is = socket.getInputStream();
+                final InputStream is = new BufferedInputStream(socket.getInputStream());
+                final OutputStream os = socket.getOutputStream();
+                final OutputStream fos = sp.getOutputStream();
+                final InputStream fis = new BufferedInputStream(sp.getInputStream());
+
+                sp.sendBreak(100);
+
+                // read login string
                 byte[] buf = new byte[1024];
                 StringBuffer sb = new StringBuffer();
                 while(true) {
@@ -96,23 +116,20 @@ public class Server {
                 System.err.println("login string: " + sb.toString());
                 if (!sb.toString().startsWith(pass_string)) return;
 
-                //try { Thread.sleep(2000); } catch(Exception e) { }
-                final OutputStream os = socket.getOutputStream();
                 System.err.println("sending instructions...");
-
-                sp.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
-                sp.setFlowControlMode(sp.FLOWCONTROL_RTSCTS_IN | sp.FLOWCONTROL_RTSCTS_OUT);
-                final OutputStream fos = sp.getOutputStream();
-                final InputStream fis = sp.getInputStream();
-
                 new Thread() {
                     public void run() {
                         try {
-                            byte[] buf = new byte[1024];
                             while(true) {
-                                int numread = is.read(buf, 0, buf.length);
-                                if (numread == -1) break;
-                                fos.write(buf, 0, numread);
+                                int r = is.read();
+                                if (r == -1) break;
+                                System.err.println("write: 0x"+Integer.toString(r & 0xff, 16));
+                                synchronized(fos) {
+                                    fos.write(r);
+                                    if (is.available()==0) fos.flush();
+                                }
+                            }
+                            synchronized(fos) {
                                 fos.flush();
                             }
                         } catch (Exception e) { throw new RuntimeException(e);
@@ -126,46 +143,35 @@ public class Server {
 
                 System.err.println("reading back...");
                 while(true) {
-                    long result = 0;
                     int val = 0;
                     for(int i=0; i<6; i++) {
                         int k = 0;
-                        while(!closed && fis.available()==0) {
+                        while (fis.available()==0) {
                             if (closed) return;
-                            k++;
-                            if (k >= 100) {
-                                System.err.println("sleep");
-                                k = 0;
-                            }
                             Thread.sleep(10);
                         }
-                        if (closed) return;
                         val = fis.read();
                         if (val==-1) break;
-                        System.err.println("byte: 0x"+Integer.toString(val & 0xff, 16));
+                        if ((val & (3<<6)) == 0) {
+                            synchronized(fos) {
+                                fos.write( (1<<6) | 1);
+                            }
+                        }
+                        System.err.println("read: 0x"+Integer.toString(val & 0xff, 16));
                         os.write((byte)val);
-                        os.flush();
-                        result |= ((long)val) << (i * 8);
                     }
                     if (val==-1) break;
-                    System.err.print(result);
-                    System.err.print(" 0x");
-                    System.err.print(Long.toString(result, 16));
-                    System.err.println();
                 }
+                os.flush();
                 System.err.println("done.");
 
             } catch (Exception e) { throw new RuntimeException(e);
             } finally {
                 try {
                     System.err.println("closing...");
-                    /*
-                    fis.close();
-                    if (fis != null) fis.close();
-                    */
                 } catch (Throwable t) { t.printStackTrace(); }
             }
         }
     }
 
-}
\ No newline at end of file
+}