updates that were lying around but never got checked in; includes reorg of gui
[slipway.git] / src / com / atmel / fpslic / FpslicBoard.java
1 package com.atmel.fpslic;
2
3 import edu.berkeley.abits.*;
4 import java.io.*;
5
6 /**
7  * Implementation of <tt>Board</tt> for Fpslic devices; subclass must
8  * implement methods to wiggle pins on the chip.
9  */
10 public abstract class FpslicBoard implements Board {
11
12     public FpslicBoard() throws IOException { }
13
14     public void reset() throws IOException {
15         avrrstPin(false);
16         configDataPin(false);
17         resetPin(false);
18         cclkPin(false);
19         
20         conPin(false);
21         flush();
22
23         resetPin(false);
24         try { Thread.sleep(500); } catch (Exception e) { }
25         if (initPin()) throw new IOException("INIT was still high after pulling RESET low");
26
27         resetPin(true);
28         try { Thread.sleep(500); } catch (Exception e) { }
29         if (!initPin()) throw new IOException("INIT was still low after releasing RESET");
30
31         sendConfigBits(0,2);
32         flush();
33     }
34
35     public OutputStream getConfigStream() throws IOException {
36         reset();
37         return new OutputStream() {
38                 int bytes = 0;
39                 int bits = 0;
40                 public void write(int in) throws IOException {
41                     for(int i=7; i>=0; i--) {
42                         bits++;
43                         sendConfigBits((((in & 0xff) & (1<<i))!=0)?1:0, 1);
44                     }
45                 }
46                 public void write(byte[] b, int off, int len) throws IOException {
47                     for(int i=off; i<off+len; i++)
48                         write(b[i]);
49                 }
50                 public void flush() throws IOException {
51                     FpslicBoard.this.flush();
52                 }
53                 public void close() throws IOException {
54                     flush();
55                     releaseConPin();
56                     if (!initPin())
57                         throw new IOException("initialization failed at " + bytes);
58                     for(int i=0; i<100; i++) {
59                         flush();
60                         if (!initPin())
61                             throw new IOException("initialization failed at " + bytes);
62                         try { Thread.sleep(20); } catch (Exception e) { }
63                         sendConfigBits(0,1);
64                     }
65                     FpslicBoard.this.close();
66                 }
67             };
68     }
69
70     public void selfTest(SelfTestResultListener resultListener) throws IOException {
71         boolean pin;
72
73         // correct preamble
74         getConfigStream();
75         sendConfigBits(Integer.parseInt("00000000", 2), 8);
76         sendConfigBits(Integer.parseInt("10110111", 2), 8);
77         sendConfigBits(0,1);
78         flush();
79         try { Thread.sleep(100); } catch (Exception e) { }
80         pin = initPin();
81         resultListener.reportTestResult(0, 4, pin);
82
83         // preamble shifted one bit earlier than it should be
84         getConfigStream();
85         sendConfigBits(Integer.parseInt("0000000",  2), 7);
86         sendConfigBits(Integer.parseInt("10110111", 2), 8);
87         sendConfigBits(0, 2);
88         flush();
89         try { Thread.sleep(100); } catch (Exception e) { }
90         pin = initPin();
91         resultListener.reportTestResult(1, 4, !pin);
92
93         // preamble shifted one bit later than it should be
94         getConfigStream();
95         sendConfigBits(Integer.parseInt("000000000", 2), 9);
96         sendConfigBits(Integer.parseInt("10110111",  2), 8);
97         //sendConfigBits(0, 1);
98         flush();
99         try { Thread.sleep(100); } catch (Exception e) { }
100         pin = initPin();
101         resultListener.reportTestResult(2, 4, !pin);
102
103         // plain 'ol bogus preamble
104         getConfigStream();
105         sendConfigBits(Integer.parseInt("00000000", 2), 8);
106         sendConfigBits(Integer.parseInt("11110111", 2), 8);
107         sendConfigBits(0, 1);
108         flush();
109         try { Thread.sleep(100); } catch (Exception e) { }
110         pin = initPin();
111         resultListener.reportTestResult(3, 4, !pin);
112     }
113
114     ////////////////////////////////////////////////////////////////////////////////
115
116     private void sendConfigBits(int bits, int numbits) throws IOException {
117         for(int i=(numbits-1); i>=0; i--) {
118             boolean bit = (bits & (1<<i)) != 0;
119             configDataPin(bit);
120             cclkPin(true);
121             cclkPin(false);
122         }
123     }
124
125     ////////////////////////////////////////////////////////////////////////////////
126
127     protected abstract void    avrrstPin(boolean on)      throws IOException;
128     protected abstract void    cclkPin(boolean on)        throws IOException;
129     protected abstract void    configDataPin(boolean on)  throws IOException;
130     protected abstract void    resetPin(boolean on)       throws IOException;
131     protected abstract boolean initPin()                  throws IOException;
132     protected abstract void    releaseConPin()            throws IOException;
133     protected abstract void    conPin(boolean on)         throws IOException;
134
135     protected abstract void    purge()                    throws IOException;
136     protected abstract void    flush()                    throws IOException;
137     protected abstract void    close()                    throws IOException;
138
139 }