05ed4f2f9972c4fbb3ab8757cafcc7685cfc891c
[slipway.git] / src / edu / berkeley / obits / device / atmel / ChipImpl.java
1 package edu.berkeley.obits.device.atmel;
2 import com.ftdi.usb.*;
3 import java.io.*;
4
5 public class ChipImpl extends FtdiUart implements Chip {
6
7     private int dmask =
8         (1<<0) |
9         (1<<1) |
10         (1<<2) |
11         //(1<<3) |
12         //(1<<4) |
13         (1<<5) |
14         (1<<6) |
15         (1<<7);
16
17     public ChipImpl() {
18         super();
19         doReset();
20     }
21
22     public void doReset() {
23
24         dmask =
25             (1<<0) |
26             (1<<1) |
27             (1<<2) |
28             //(1<<3) |
29             //(1<<4) |
30             (1<<5) |
31             (1<<6) |
32             (1<<7);
33         avrrst(false);
34
35         flush();
36         //purge();
37
38         dbangmode(dmask);
39         flush();
40
41         clk(false);
42         data(false);
43         con(false);
44         flush();
45         //try { Thread.sleep(500); } catch (Exception e) { }
46
47         reset(false);
48         flush();
49         try { Thread.sleep(500); } catch (Exception e) { }
50         if (initErr()) throw new RuntimeException("INIT was still high after pulling RESET low");
51         //System.out.println("0 con() = " + con());
52
53         reset(true);
54         flush();
55         try { Thread.sleep(500); } catch (Exception e) { }
56         if (!initErr()) throw new RuntimeException("INIT was still low after releasing RESET");
57         //System.out.println("1 con() = " + con());
58         con(false);
59     }
60
61     int porte = 0;
62     public void porte(int pin, boolean b) {
63         porte = (~(1<<pin)) | (b ? (1<<pin) : 0);
64         if (pin==4) {
65             dbang(2, b);
66             flush();
67         }
68     }
69
70     public void config(boolean bit) { config(bit?1:0, 1); }
71     public void config(int dat) { config(dat, 8); }
72     public void config(int dat, int numbits) {
73         for(int i=(numbits-1); i>=0; i--) {
74             boolean bit = (dat & (1<<i)) != 0;
75             data(bit);
76             clk(true);
77             clk(false);
78         }
79     }
80
81     // tricky: RESET has a weak pull-up, and is wired to a CBUS line.  So,
82     //         we can pull it down (assert reset) from uart-mode, or we can
83     //         let it float upward from either mode.
84     public void reset(boolean on) {
85         bits = on ? (1<<1) : 0;
86         mask = (1<<1);
87         uart();
88         flush();
89         if (on) {
90             //mask = 0;
91             //uart();
92             //flush();
93             dbangmode(dmask);
94             flush();
95         }
96     }
97
98     public void avrrst(boolean on) { dbang(7, on); }
99     public void clk(boolean on)    { dbang(6, on); }
100     public void data(boolean on)   { dbang(5, on); }
101
102     public boolean initErr()       { flush(); return (readPins() & (1<<4))!=0; }
103
104     public boolean con() {
105         flush();
106         //dmask &= ~(1<<0);
107         dbangmode(dmask);
108         return (readPins() & (1<<0)) != 0;
109     }
110     public boolean rcon() {
111         flush();
112         dmask &= ~(1<<0);
113         dbangmode(dmask);
114         return (readPins() & (1<<0)) != 0;
115     }
116     public void con(boolean on) {
117         flush();
118         dmask |= (1<<0);
119         dbang(0, on);
120         dbangmode(dmask);
121     }
122 }