checkpoint
[slipway.git] / src / com / atmel / fpslic / ChipImpl.java
1 package com.atmel.fpslic;
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() throws IOException {
18         super(0x6666, 0x3133, 1500 * 1000);
19         doReset();
20     }
21
22     public void flush() throws IOException { getOutputStream().flush(); }
23
24     protected int dbits = 0;
25     protected synchronized void dbang(int bit, boolean val) throws IOException {
26         dbits = val ? (dbits | (1 << bit)) : (dbits & (~(1 << bit)));
27         try {
28             getOutputStream().write((byte)dbits);
29         } catch (IOException e) { throw new RuntimeException(e); }
30     }
31
32     public void doReset() throws IOException {
33
34         dmask =
35             (1<<0) |
36             (1<<1) |
37             (1<<2) |
38             //(1<<3) |
39             //(1<<4) |
40             (1<<5) |
41             (1<<6) |
42             (1<<7);
43         avrrst(false);
44
45         flush();
46         //purge();
47
48         dbus_mode(dmask);
49         flush();
50
51         clk(false);
52         data(false);
53         con(false);
54         flush();
55         //try { Thread.sleep(500); } catch (Exception e) { }
56
57         reset(false);
58         flush();
59         try { Thread.sleep(500); } catch (Exception e) { }
60         if (initErr()) throw new RuntimeException("INIT was still high after pulling RESET low");
61
62         reset(true);
63         flush();
64         try { Thread.sleep(500); } catch (Exception e) { }
65         if (!initErr()) throw new RuntimeException("INIT was still low after releasing RESET");
66
67         con(false);
68     }
69
70     public void config(boolean bit) throws IOException { config(bit?1:0, 1); }
71     public void config(int dat) throws IOException { config(dat, 8); }
72     public void config(int dat, int numbits) throws IOException {
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) throws IOException {
85         uart_and_cbus_mode(1<<1, on ? (1<<1) : 0);
86         flush();
87         if (on) {
88             dbus_mode(dmask);
89             flush();
90         }
91     }
92
93     public void avrrst(boolean on) throws IOException { dbang(7, on); }
94     public void clk(boolean on)    throws IOException { dbang(6, on); }
95     public void data(boolean on)   throws IOException { dbang(5, on); }
96
97     public boolean initErr()       throws IOException { flush(); return (readPins() & (1<<4))!=0; }
98
99     public boolean con() throws IOException {
100         flush();
101         //dmask &= ~(1<<0);
102         dbus_mode(dmask);
103         return (readPins() & (1<<0)) != 0;
104     }
105     public boolean rcon() throws IOException {
106         flush();
107         dmask &= ~(1<<0);
108         dbus_mode(dmask);
109         return (readPins() & (1<<0)) != 0;
110     }
111     public void con(boolean on) throws IOException {
112         flush();
113         dmask |= (1<<0);
114         dbang(0, on);
115         dbus_mode(dmask);
116     }
117
118     public OutputStream getConfigStream() throws IOException {
119         doReset();
120         config(0,10);
121         con();
122         return new OutputStream() {
123                 public void write(int in) throws IOException {
124                     for(int i=7; i>=0; i--) {
125                         config((((in & 0xff) & (1<<i))!=0)?1:0, 1);
126                     }
127                 }
128                 public void write(byte[] b, int off, int len) throws IOException {
129                     for(int i=off; i<off+len; i++)
130                         write(b[i]);
131                 }
132                 public void flush() throws IOException {
133                     ChipImpl.this.flush();
134                 }
135             };
136     }
137
138     public static String red(Object o) { return "\033[31m"+o+"\033[0m"; }
139     public static String green(Object o) { return "\033[32m"+o+"\033[0m"; }
140     public void selfTest() throws Exception {
141         ChipImpl d = this;
142         boolean pin;
143         d.doReset();
144         d.config(0,3);
145         d.con();
146         d.config(0,7);
147         d.flush();
148         //d.flush();
149         d.config(Integer.parseInt("10110111", 2), 8);
150         d.config(0,1);
151         d.flush();
152         try { Thread.sleep(100); } catch (Exception e) { }
153         pin = d.initErr();
154         System.out.println("good preamble   => " + pin + " " + (pin ? green("good") : red("BAD")));
155
156         d.doReset();
157         try { Thread.sleep(100); } catch (Exception e) { }
158         d.config(0,3);
159         d.con();
160         d.config(0,6);
161         d.flush();
162         //d.flush();
163         d.config(Integer.parseInt("10110111", 2), 8);
164         d.config(0, 2);
165         d.flush();
166         try { Thread.sleep(100); } catch (Exception e) { }
167         pin = d.initErr();
168         System.out.println("bad preamble #2 => " + pin + " " + (pin ? red("BAD") : green("good")));
169
170         d.doReset();
171         try { Thread.sleep(100); } catch (Exception e) { }
172         d.config(0,3);
173         d.con();
174         d.config(0,7);
175         d.flush();
176         //d.flush();
177         d.config(Integer.parseInt("11110111", 2), 8);
178         d.config(0, 1);
179         d.flush();
180         try { Thread.sleep(100); } catch (Exception e) { }
181         pin = d.initErr();
182         System.out.println("bad preamble #1 => " + pin + " " + (pin ? red("BAD") : green("good")));
183     }
184 }