checkpoint
authoradam <adam@megacz.com>
Mon, 11 Sep 2006 12:21:47 +0000 (13:21 +0100)
committeradam <adam@megacz.com>
Mon, 11 Sep 2006 12:21:47 +0000 (13:21 +0100)
src/edu/berkeley/obits/device/atmel/Chip.java
src/edu/berkeley/obits/device/atmel/ChipImpl.java
src/edu/berkeley/obits/device/atmel/FtdiChip.java

index bbb0870..82035df 100644 (file)
@@ -5,17 +5,12 @@ import java.io.*;
 public interface Chip {
     
     public void doReset();
-
-
     public void reset(boolean on);
     public void avrrst(boolean on);
-
     public void config(boolean bit);
     public void config(int data, int numbits);
-
     public boolean initErr();
     public void    porte(int pin, boolean b);
-
     public void    con(boolean b);
     public boolean con();
 
index a3033db..9e7cd7f 100644 (file)
@@ -10,7 +10,7 @@ public class ChipImpl extends FtdiChip implements Chip {
     }
 
     public void doReset() {
-        dbangmode();
+        dbangmode(dmask);
         clk(false);
         data(false);
 
@@ -25,7 +25,7 @@ public class ChipImpl extends FtdiChip implements Chip {
         try { Thread.sleep(200); } catch (Exception e) { }
 
         dmask &= ~(1<<7);
-        dbangmode();
+        dbangmode(dmask);
     }
 
     int porte = 0;
@@ -53,10 +53,19 @@ public class ChipImpl extends FtdiChip implements Chip {
         }
     }
 
+    protected static int dmask =
+        //(1<<0) |
+        (1<<1) |
+        (1<<2) |
+        //(1<<3) |
+        //(1<<4) |
+        (1<<5) |
+        (1<<6) |
+        (1<<7);
+
     public void reset(boolean on) {
         bits = on ? (1<<1) : 0;
-        cbangmode();
-        //dbang(0, on);
+        uart();
     }
     public void avrrst(boolean on) { dbang(7, on); }
     public boolean initErr()       { return (readPins() & (1<<4))!=0; }
@@ -64,35 +73,14 @@ public class ChipImpl extends FtdiChip implements Chip {
     public void data(boolean on)   { dbang(5, on); }
 
     public boolean con() {
-
-        /*
-        mask &= ~(1<<0);
-        cbangmode();
-        boolean ret = (readPins() & (1<<0)) != 0;
-        dbangmode();
-        return ret;
-        */
-
-
-
         dmask &= ~(1<<0);
-        dbangmode();
+        dbangmode(dmask);
         return (readPins() & (1<<0)) != 0;
-
     }
     public void con(boolean on) {
-
-        /*
-        mask |= (1<<0);
-        bits = on ? (1<<0) : 0;
-        cbangmode();
-        */
-
-
         dmask |= (1<<0);
-        dbangmode();
+        dbangmode(dmask);
         dbang(0, on);
-
     }
 
 }
index d1734e5..828c1a2 100644 (file)
@@ -7,6 +7,9 @@ public class FtdiChip {
     protected int bits = 0;
     protected SWIGTYPE_p_ftdi_context context = example.new_ftdi_context();
 
+    public OutputStream getOutputStream() { return out; }
+    public InputStream  getInputStream() { return in; }
+
     public FtdiChip() {
         example.ftdi_init(context);
         example.ftdi_usb_open(context, 0x6666, 0x3133);
@@ -19,11 +22,15 @@ public class FtdiChip {
         example.ftdi_read_pins(context, b);
         return b[0];
     }
+
     ByteArrayOutputStream baos = new ByteArrayOutputStream();
     public void flush() {
-        byte[] bytes = baos.toByteArray();
-        baos = new ByteArrayOutputStream();
-        dbang(bytes, bytes.length);
+        try {
+            byte[] bytes = baos.toByteArray();
+            baos = new ByteArrayOutputStream();
+            out.write(bytes, 0, bytes.length);
+            out.flush();
+        } catch (IOException e) { throw new RuntimeException(e); }
     }
 
     public boolean buffered = false;
@@ -34,31 +41,16 @@ public class FtdiChip {
         //(1<<3)
         ;
 
-    protected static int dmask =
-        //(1<<0) |
-        (1<<1) |
-        (1<<2) |
-        //(1<<3) |
-        //(1<<4) |
-        (1<<5) |
-        (1<<6) |
-        (1<<7);
-
     public synchronized void purge() {
         example.ftdi_usb_purge_buffers(context);
-        example.ftdi_setflowctrl(context, (1 << 8));
     }
     public synchronized void uart() {
-        cbangmode();
-    }
-    public synchronized void dbangmode() {
-        example.ftdi_set_bitmode(context, (short)dmask, (short)0x01);
-    }
-
-    protected synchronized void cbangmode() {
         example.ftdi_set_bitmode(context, (short)((mask << 4) | bits), (short)0x20);
         example.ftdi_setflowctrl(context, (1 << 8));
     }
+    public synchronized void dbangmode(int dmask) {
+        example.ftdi_set_bitmode(context, (short)dmask, (short)0x01);
+    }
 
     protected int dbits = 0;
     protected synchronized void dbang(int bit, boolean val) {
@@ -66,45 +58,30 @@ public class FtdiChip {
         if (buffered) {
             baos.write((byte)dbits);
         } else {
-            dbang((byte)dbits);
+            try {
+                out.write((byte)dbits);
+                out.flush();
+            } catch (IOException e) { throw new RuntimeException(e); }
         }
     }
 
-    protected synchronized void dbang(byte by) {
-        byte[] b = new byte[1];
-        b[0] = by;
-        example.ftdi_write_data(context, b, 1);
-    }
-    protected synchronized void dbang(byte[] b, int len) {
-        example.ftdi_write_data(context, b, len);
-    }
-
-    private OutputStream os = new ChipOutputStream();
-    private InputStream  is = new ChipInputStream();
-    public OutputStream getOutputStream() { return os; }
-    public InputStream  getInputStream() { return is; }
-    
-    public class ChipInputStream extends InputStream {
-        public int available() throws IOException {
-            // FIXME
-            return 0;
-        }
-        public long skip(long l) throws IOException {
-            throw new RuntimeException("not supported");
-        }
-        public int read() throws IOException {
-            System.out.println("read()");
-            byte[] b = new byte[1];
-            int result = 0;
-            while(result==0)
-                result = read(b, 0, 1);
-            return b[0] & 0xff;
-        }
-        public int read(byte[] b, int off, int len) throws IOException {
-            // FIXME: blocking reads?
-            int result = 0;
-            while(true) {
-                if (len==0) return 0;
+    private final InputStream in = new InputStream() {
+            public int available() throws IOException {
+                // FIXME
+                return 0;
+            }
+            public int read() throws IOException {
+                byte[] b = new byte[1];
+                int result = 0;
+                while(result==0) result = read(b, 0, 1);
+                return b[0] & 0xff;
+            }
+            public int read(byte[] b, int off, int len) throws IOException {
+                // FIXME: blocking reads?
+                int result = 0;
+                while(true) {
+                    if (len==0) return 0;
                     byte[] b0 = new byte[len];
                     synchronized(FtdiChip.this) {
                         result = example.ftdi_read_data(context, b0, len);
@@ -113,27 +90,28 @@ public class FtdiChip {
                         System.arraycopy(b0, 0, b, off, result);
                         return result;
                     }
-                try { Thread.sleep(50); } catch (Exception e) { e.printStackTrace(); } 
+                    try { Thread.sleep(50); } catch (Exception e) { e.printStackTrace(); } 
+                }
             }
-        }
-    }
+        };
 
-    public class ChipOutputStream extends OutputStream {
-        public void write(int b) throws IOException {
-            byte[] d = new byte[1];
-            d[0] = (byte)b;
-            write(d, 0, 1);
-        }
-        public void write(byte[] b, int off, int len) throws IOException {
-            byte[] b2 = new byte[64];
-            while(len > 0) {
-                System.arraycopy(b, off, b2, 0, Math.min(b2.length, len));
-                synchronized(FtdiChip.this) {
-                    int result = example.ftdi_write_data(context, b2, Math.min(b2.length, len));
+    private final OutputStream out = new OutputStream() {
+            public void write(int b) throws IOException {
+                byte[] d = new byte[1];
+                d[0] = (byte)b;
+                write(d, 0, 1);
+            }
+            public void write(byte[] b, int off, int len) throws IOException {
+                byte[] b2 = new byte[64];
+                while(len > 0) {
+                    System.arraycopy(b, off, b2, 0, Math.min(b2.length, len));
+                    int result;
+                    synchronized(FtdiChip.this) {
+                        result = example.ftdi_write_data(context, b2, Math.min(b2.length, len));
+                    }
                     off += result;
                     len -= result;
                 }
             }
-        }
-    }
+        };
 }