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                 int bytes = 0;
124                 public void write(int in) throws IOException {
125                     bytes++;
126                     for(int i=7; i>=0; i--) {
127                         config((((in & 0xff) & (1<<i))!=0)?1:0, 1);
128                     }
129                 }
130                 public void write(byte[] b, int off, int len) throws IOException {
131                     for(int i=off; i<off+len; i++)
132                         write(b[i]);
133                 }
134                 public void flush() throws IOException {
135                     ChipImpl.this.flush();
136                     rcon();
137                 }
138                 public void close() throws IOException {
139                     flush();
140                     if (!initErr())
141                         throw new RuntimeException("initialization failed at " + bytes);
142                     for(int i=0; i<100; i++) {
143                         flush();
144                         if (!initErr())
145                             throw new RuntimeException("initialization failed at " + bytes);
146                         try { Thread.sleep(20); } catch (Exception e) { }
147                         config(0,1);
148                     }
149                     avrrst(false);
150                     try { Thread.sleep(100); } catch (Exception e) { }
151                     purge();
152                     uart_and_cbus_mode(1<<1, 1<<1);
153                 }
154             };
155     }
156
157     public static String red(Object o) { return "\033[31m"+o+"\033[0m"; }
158     public static String green(Object o) { return "\033[32m"+o+"\033[0m"; }
159     public void selfTest() throws Exception {
160         ChipImpl d = this;
161         boolean pin;
162         d.doReset();
163         d.config(0,3);
164         d.con();
165         d.config(0,7);
166         d.flush();
167         //d.flush();
168         d.config(Integer.parseInt("10110111", 2), 8);
169         d.config(0,1);
170         d.flush();
171         try { Thread.sleep(100); } catch (Exception e) { }
172         pin = d.initErr();
173         System.out.println("good preamble   => " + pin + " " + (pin ? green("good") : red("BAD")));
174
175         d.doReset();
176         try { Thread.sleep(100); } catch (Exception e) { }
177         d.config(0,3);
178         d.con();
179         d.config(0,6);
180         d.flush();
181         //d.flush();
182         d.config(Integer.parseInt("10110111", 2), 8);
183         d.config(0, 2);
184         d.flush();
185         try { Thread.sleep(100); } catch (Exception e) { }
186         pin = d.initErr();
187         System.out.println("bad preamble #2 => " + pin + " " + (pin ? red("BAD") : green("good")));
188
189         d.doReset();
190         try { Thread.sleep(100); } catch (Exception e) { }
191         d.config(0,3);
192         d.con();
193         d.config(0,7);
194         d.flush();
195         //d.flush();
196         d.config(Integer.parseInt("11110111", 2), 8);
197         d.config(0, 1);
198         d.flush();
199         try { Thread.sleep(100); } catch (Exception e) { }
200         pin = d.initErr();
201         System.out.println("bad preamble #1 => " + pin + " " + (pin ? red("BAD") : green("good")));
202     }
203 }