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