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 {
23         try {
24             getOutputStream().flush();
25         } catch (Exception e) { throw new RuntimeException(e); }
26     }
27
28     protected int dbits = 0;
29     protected synchronized void dbang(int bit, boolean val) throws IOException {
30         dbits = val ? (dbits | (1 << bit)) : (dbits & (~(1 << bit)));
31         try {
32             getOutputStream().write((byte)dbits);
33         } catch (IOException e) { throw new RuntimeException(e); }
34     }
35
36     public void doReset() throws IOException {
37
38         dmask =
39             (1<<0) |
40             (1<<1) |
41             (1<<2) |
42             //(1<<3) |
43             //(1<<4) |
44             (1<<5) |
45             (1<<6) |
46             (1<<7);
47         avrrst(false);
48
49         flush();
50         //purge();
51
52         dbus_mode(dmask);
53         flush();
54
55         clk(false);
56         data(false);
57         con(false);
58         flush();
59         //try { Thread.sleep(500); } catch (Exception e) { }
60
61         reset(false);
62         flush();
63         try { Thread.sleep(500); } catch (Exception e) { }
64         if (initErr()) throw new RuntimeException("INIT was still high after pulling RESET low");
65         //System.out.println("0 con() = " + con());
66
67         reset(true);
68         flush();
69         try { Thread.sleep(500); } catch (Exception e) { }
70         if (!initErr()) throw new RuntimeException("INIT was still low after releasing RESET");
71         //System.out.println("1 con() = " + con());
72         con(false);
73     }
74
75     public void config(boolean bit) throws IOException { config(bit?1:0, 1); }
76     public void config(int dat) throws IOException { config(dat, 8); }
77     public void config(int dat, int numbits) throws IOException {
78         for(int i=(numbits-1); i>=0; i--) {
79             boolean bit = (dat & (1<<i)) != 0;
80             data(bit);
81             clk(true);
82             clk(false);
83         }
84     }
85
86     // tricky: RESET has a weak pull-up, and is wired to a CBUS line.  So,
87     //         we can pull it down (assert reset) from uart-mode, or we can
88     //         let it float upward from either mode.
89     public void reset(boolean on) throws IOException {
90         uart_and_cbus_mode(1<<1, on ? (1<<1) : 0);
91         flush();
92         if (on) {
93             dbus_mode(dmask);
94             flush();
95         }
96     }
97
98     public void avrrst(boolean on) throws IOException { dbang(7, on); }
99     public void clk(boolean on)    throws IOException { dbang(6, on); }
100     public void data(boolean on)   throws IOException { dbang(5, on); }
101
102     public boolean initErr()       throws IOException { flush(); return (readPins() & (1<<4))!=0; }
103
104     public boolean con() throws IOException {
105         flush();
106         //dmask &= ~(1<<0);
107         dbus_mode(dmask);
108         return (readPins() & (1<<0)) != 0;
109     }
110     public boolean rcon() throws IOException {
111         flush();
112         dmask &= ~(1<<0);
113         dbus_mode(dmask);
114         return (readPins() & (1<<0)) != 0;
115     }
116     public void con(boolean on) throws IOException {
117         flush();
118         dmask |= (1<<0);
119         dbang(0, on);
120         dbus_mode(dmask);
121     }
122 }