checkpoint
[slipway.git] / src / com / atmel / fpslic / ChipImpl.java
1 package com.atmel.fpslic;
2 import com.ftdi.usb.*;
3 import java.io.*;
4
5 public class ChipImpl extends FtdiUart implements Chip {
6
7     private int dmask =
8         (1<<0) |
9         (1<<1) |
10         (1<<2) |
11         //(1<<3) |
12         //(1<<4) |
13         (1<<5) |
14         (1<<6) |
15         (1<<7);
16
17     public ChipImpl() throws IOException {
18         super(0x6666, 0x3133, 1500 * 1000);
19         doReset();
20     }
21
22     public void flush() throws IOException { getOutputStream().flush(); }
23
24     protected int dbits = 0;
25     protected synchronized void dbang(int bit, boolean val) throws IOException {
26         dbits = val ? (dbits | (1 << bit)) : (dbits & (~(1 << bit)));
27         try {
28             getOutputStream().write((byte)dbits);
29         } catch (IOException e) { throw new RuntimeException(e); }
30     }
31
32     public void doReset() throws IOException {
33
34         dmask =
35             (1<<0) |
36             (1<<1) |
37             (1<<2) |
38             //(1<<3) |
39             //(1<<4) |
40             (1<<5) |
41             (1<<6) |
42             (1<<7);
43         avrrst(false);
44
45         flush();
46         //purge();
47
48         dbus_mode(dmask);
49         flush();
50
51         clk(false);
52         data(false);
53         con(false);
54         flush();
55         //try { Thread.sleep(500); } catch (Exception e) { }
56
57         reset(false);
58         flush();
59         try { Thread.sleep(500); } catch (Exception e) { }
60         if (initErr()) throw new RuntimeException("INIT was still high after pulling RESET low");
61
62         reset(true);
63         flush();
64         try { Thread.sleep(500); } catch (Exception e) { }
65         if (!initErr()) throw new RuntimeException("INIT was still low after releasing RESET");
66
67         con(false);
68     }
69
70     public void config(boolean bit) throws IOException { config(bit?1:0, 1); }
71     public void config(int dat) throws IOException { config(dat, 8); }
72     public void config(int dat, int numbits) throws IOException {
73         for(int i=(numbits-1); i>=0; i--) {
74             boolean bit = (dat & (1<<i)) != 0;
75             data(bit);
76             clk(true);
77             clk(false);
78         }
79     }
80
81     // tricky: RESET has a weak pull-up, and is wired to a CBUS line.  So,
82     //         we can pull it down (assert reset) from uart-mode, or we can
83     //         let it float upward from either mode.
84     public void reset(boolean on) throws IOException {
85         uart_and_cbus_mode(1<<1, on ? (1<<1) : 0);
86         flush();
87         if (on) {
88             dbus_mode(dmask);
89             flush();
90         }
91     }
92
93     public void avrrst(boolean on) throws IOException { dbang(7, on); }
94     public void clk(boolean on)    throws IOException { dbang(6, on); }
95     public void data(boolean on)   throws IOException { dbang(5, on); }
96
97     public boolean initErr()       throws IOException { flush(); return (readPins() & (1<<4))!=0; }
98
99     public boolean con() throws IOException {
100         flush();
101         //dmask &= ~(1<<0);
102         dbus_mode(dmask);
103         return (readPins() & (1<<0)) != 0;
104     }
105     public boolean rcon() throws IOException {
106         flush();
107         dmask &= ~(1<<0);
108         dbus_mode(dmask);
109         return (readPins() & (1<<0)) != 0;
110     }
111     public void con(boolean on) throws IOException {
112         flush();
113         dmask |= (1<<0);
114         dbang(0, on);
115         dbus_mode(dmask);
116     }
117
118     public static String red(Object o) { return "\033[31m"+o+"\033[0m"; }
119     public static String green(Object o) { return "\033[32m"+o+"\033[0m"; }
120     public void selfTest() throws Exception {
121         ChipImpl d = this;
122         boolean pin;
123         d.doReset();
124         d.config(0,3);
125         d.con();
126         d.config(0,7);
127         d.flush();
128         //d.flush();
129         d.config(Integer.parseInt("10110111", 2), 8);
130         d.config(0,1);
131         d.flush();
132         try { Thread.sleep(100); } catch (Exception e) { }
133         pin = d.initErr();
134         System.out.println("good preamble   => " + pin + " " + (pin ? green("good") : red("BAD")));
135
136         d.doReset();
137         try { Thread.sleep(100); } catch (Exception e) { }
138         d.config(0,3);
139         d.con();
140         d.config(0,6);
141         d.flush();
142         //d.flush();
143         d.config(Integer.parseInt("10110111", 2), 8);
144         d.config(0, 2);
145         d.flush();
146         try { Thread.sleep(100); } catch (Exception e) { }
147         pin = d.initErr();
148         System.out.println("bad preamble #2 => " + pin + " " + (pin ? red("BAD") : green("good")));
149
150         d.doReset();
151         try { Thread.sleep(100); } catch (Exception e) { }
152         d.config(0,3);
153         d.con();
154         d.config(0,7);
155         d.flush();
156         //d.flush();
157         d.config(Integer.parseInt("11110111", 2), 8);
158         d.config(0, 1);
159         d.flush();
160         try { Thread.sleep(100); } catch (Exception e) { }
161         pin = d.initErr();
162         System.out.println("bad preamble #1 => " + pin + " " + (pin ? red("BAD") : green("good")));
163     }
164 }