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