checkpoint
[slipway.git] / src / com / atmel / fpslic / FpslicRawUsb.java
1 package com.atmel.fpslic;
2 import com.ftdi.usb.*;
3 import java.io.*;
4
5 /**
6  * Exposes the FpslicRaw interface of an FPSLIC wired to an FTDI USB-UART.
7  */
8 public class FpslicRawUsb implements FpslicRaw {
9
10     private FtdiUart ftdiuart;
11
12     private int dmask =
13         (1<<0) |
14         (1<<1) |
15         (1<<2) |
16         //(1<<3) |
17         //(1<<4) |
18         (1<<5) |
19         (1<<6) |
20         (1<<7);
21
22     public FpslicRawUsb(FtdiUart ftdiuart) throws IOException {
23         this.ftdiuart = ftdiuart;
24         reset();
25     }
26
27     public void reset() throws IOException {
28
29         dmask =
30             (1<<0) |
31             (1<<1) |
32             (1<<2) |
33             //(1<<3) |
34             //(1<<4) |
35             (1<<5) |
36             (1<<6) |
37             (1<<7);
38         avrrst(false);
39
40         flush();
41         //purge();
42
43         ftdiuart.dbus_mode(dmask);
44         flush();
45
46         clk(false);
47         data(false);
48         con(false);
49         flush();
50         //try { Thread.sleep(500); } catch (Exception e) { }
51
52         reset(false);
53         flush();
54         try { Thread.sleep(500); } catch (Exception e) { }
55         if (initErr()) throw new RuntimeException("INIT was still high after pulling RESET low");
56
57         reset(true);
58         flush();
59         try { Thread.sleep(500); } catch (Exception e) { }
60         if (!initErr()) throw new RuntimeException("INIT was still low after releasing RESET");
61
62         con(false);
63     }
64
65     public OutputStream getConfigStream() throws IOException {
66         reset();
67         config(0,2);
68         flush();
69         return new OutputStream() {
70                 int bytes = 0;
71                 int bits = 0;
72                 public void write(int in) throws IOException {
73                     for(int i=7; i>=0; i--) {
74                         bits++;
75                         config((((in & 0xff) & (1<<i))!=0)?1:0, 1);
76                         if (bits==1) con();
77                     }
78                 }
79                 public void write(byte[] b, int off, int len) throws IOException {
80                     for(int i=off; i<off+len; i++)
81                         write(b[i]);
82                 }
83                 public void flush() throws IOException {
84                     FpslicRawUsb.this.flush();
85                     rcon();
86                 }
87                 public void close() throws IOException {
88                     flush();
89                     if (!initErr())
90                         throw new RuntimeException("initialization failed at " + bytes);
91                     for(int i=0; i<100; i++) {
92                         flush();
93                         if (!initErr())
94                             throw new RuntimeException("initialization failed at " + bytes);
95                         try { Thread.sleep(20); } catch (Exception e) { }
96                         config(0,1);
97                     }
98                     avrrst(false);
99                     try { Thread.sleep(100); } catch (Exception e) { }
100                     ftdiuart.purge();
101                     ftdiuart.uart_and_cbus_mode(1<<1, 1<<1);
102                 }
103             };
104     }
105
106     public OutputStream getOutputStream() { return ftdiuart.getOutputStream(); }
107     public InputStream  getInputStream() { return ftdiuart.getInputStream(); }
108
109     private void preamble() throws IOException {
110         config(0,7);
111         flush();
112     }
113     public void selfTest() throws Exception {
114         boolean pin;
115
116         getConfigStream();
117         config(0,1);
118         con();
119         config(0,7);
120         config(Integer.parseInt("10110111", 2), 8);
121         config(0,1);
122         flush();
123         try { Thread.sleep(100); } catch (Exception e) { }
124         pin = initErr();
125         System.out.println("good preamble   => " + pin + " " + (pin ? green("good") : red("BAD")));
126
127         reset();
128         try { Thread.sleep(100); } catch (Exception e) { }
129         config(0,3);
130         con();
131         config(0,6);
132         flush();
133         // one too many
134         config(Integer.parseInt("10110111", 2), 8);
135         config(0, 2);
136         flush();
137         try { Thread.sleep(100); } catch (Exception e) { }
138         pin = initErr();
139         System.out.println("bad preamble #2 => " + pin + " " + (pin ? red("BAD") : green("good")));
140
141         reset();
142         try { Thread.sleep(100); } catch (Exception e) { }
143         getConfigStream();
144         config(0,1);
145         con();
146         config(0,7);
147         config(Integer.parseInt("11110111", 2), 8);
148         config(0, 1);
149         flush();
150         try { Thread.sleep(100); } catch (Exception e) { }
151         pin = initErr();
152         System.out.println("bad preamble #1 => " + pin + " " + (pin ? red("BAD") : green("good")));
153     }
154
155     // Private //////////////////////////////////////////////////////////////////////////////
156
157     private void flush() throws IOException { ftdiuart.getOutputStream().flush(); }
158
159     private int dbits = 0;
160     private void dbang(int bit, boolean val) throws IOException {
161         dbits = val ? (dbits | (1 << bit)) : (dbits & (~(1 << bit)));
162         ftdiuart.getOutputStream().write((byte)dbits);
163     }
164
165     private void config(boolean bit) throws IOException { config(bit?1:0, 1); }
166     private void config(int dat) throws IOException { config(dat, 8); }
167     private void config(int dat, int numbits) throws IOException {
168         for(int i=(numbits-1); i>=0; i--) {
169             boolean bit = (dat & (1<<i)) != 0;
170             data(bit);
171             clk(true);
172             clk(false);
173         }
174     }
175
176     // tricky: RESET has a weak pull-up, and is wired to a CBUS line.  So,
177     //         we can pull it down (assert reset) from uart-mode, or we can
178     //         let it float upward from either mode.
179     private void reset(boolean on) throws IOException {
180         ftdiuart.uart_and_cbus_mode(1<<1, on ? (1<<1) : 0);
181         flush();
182         if (on) {
183             ftdiuart.dbus_mode(dmask);
184             flush();
185         }
186     }
187
188     private void avrrst(boolean on) throws IOException { dbang(7, on); }
189     private void clk(boolean on)    throws IOException { dbang(6, on); }
190     private void data(boolean on)   throws IOException { dbang(5, on); }
191     private boolean initErr()       throws IOException { flush(); return (ftdiuart.readPins() & (1<<4))!=0; }
192
193     private boolean con() throws IOException {
194         flush();
195         //dmask &= ~(1<<0);
196         ftdiuart.dbus_mode(dmask);
197         return (ftdiuart.readPins() & (1<<0)) != 0;
198     }
199
200     private boolean rcon() throws IOException {
201         flush();
202         dmask &= ~(1<<0);
203         ftdiuart.dbus_mode(dmask);
204         return (ftdiuart.readPins() & (1<<0)) != 0;
205     }
206     private void con(boolean on) throws IOException {
207         flush();
208         dmask |= (1<<0);
209         dbang(0, on);
210         ftdiuart.dbus_mode(dmask);
211     }
212
213     private static String red(Object o) { return "\033[31m"+o+"\033[0m"; }
214     private static String green(Object o) { return "\033[32m"+o+"\033[0m"; }
215 }