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