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