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