THIS WORKS RELIABLY
[slipway.git] / src / edu / berkeley / obits / device / atmel / ChipImpl.java
index 0b58130..c2c7752 100644 (file)
@@ -4,40 +4,58 @@ import java.io.*;
 
 public class ChipImpl extends FtdiChip implements Chip {
 
+    private int dmask =
+        (1<<0) |
+        (1<<1) |
+        (1<<2) |
+        //(1<<3) |
+        //(1<<4) |
+        (1<<5) |
+        (1<<6) |
+        (1<<7);
+
     public ChipImpl() {
         super();
         doReset();
     }
 
     public void doReset() {
-        flush();
 
-        buffered(false);
+        dmask =
+            (1<<0) |
+            (1<<1) |
+            (1<<2) |
+            //(1<<3) |
+            //(1<<4) |
+            (1<<5) |
+            (1<<6) |
+            (1<<7);
+        avrrst(false);
+
         flush();
+        //purge();
 
         dbangmode(dmask);
         flush();
 
         clk(false);
-        flush();
-
         data(false);
-        flush();
-
         con(false);
         flush();
+        //try { Thread.sleep(500); } catch (Exception e) { }
 
         reset(false);
         flush();
-        try { Thread.sleep(200); } catch (Exception e) { }
+        try { Thread.sleep(500); } catch (Exception e) { }
+        if (initErr()) throw new RuntimeException("INIT was still high after pulling RESET low");
+        //System.out.println("0 con() = " + con());
 
         reset(true);
         flush();
-        try { Thread.sleep(200); } catch (Exception e) { }
-
-        dmask &= ~(1<<7);
-        dbangmode(dmask);
-        flush();
+        try { Thread.sleep(500); } catch (Exception e) { }
+        if (!initErr()) throw new RuntimeException("INIT was still low after releasing RESET");
+        //System.out.println("1 con() = " + con());
+        con(false);
     }
 
     int porte = 0;
@@ -60,35 +78,39 @@ public class ChipImpl extends FtdiChip implements Chip {
         }
     }
 
-    protected int dmask =
-        //(1<<0) |
-        (1<<1) |
-        (1<<2) |
-        //(1<<3) |
-        //(1<<4) |
-        (1<<5) |
-        (1<<6) |
-        (1<<7);
-
+    // tricky: RESET has a weak pull-up, and is wired to a CBUS line.  So,
+    //         we can pull it down (assert reset) from uart-mode, or we can
+    //         let it float upward from either mode.
     public void reset(boolean on) {
         bits = on ? (1<<1) : 0;
+        mask = (1<<1);
         uart();
+        flush();
+        if (on) {
+            //mask = 0;
+            //uart();
+            //flush();
+            dbangmode(dmask);
+            flush();
+        }
     }
+
     public void avrrst(boolean on) { dbang(7, on); }
     public void clk(boolean on)    { dbang(6, on); }
     public void data(boolean on)   { dbang(5, on); }
 
     public boolean initErr()       { flush(); return (readPins() & (1<<4))!=0; }
+
     public boolean con() {
         flush();
-        dmask &= ~(1<<0);
+        //dmask &= ~(1<<0);
         dbangmode(dmask);
         return (readPins() & (1<<0)) != 0;
     }
     public void con(boolean on) {
         flush();
         dmask |= (1<<0);
-        dbangmode(dmask);
         dbang(0, on);
+        dbangmode(dmask);
     }
 }