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