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         uart(1<<1, on ? (1<<1) : 0);
77         flush();
78         if (on) {
79             dbangmode(dmask);
80             flush();
81         }
82     }
83
84     public void avrrst(boolean on) { dbang(7, on); }
85     public void clk(boolean on)    { dbang(6, on); }
86     public void data(boolean on)   { dbang(5, on); }
87
88     public boolean initErr()       { flush(); return (readPins() & (1<<4))!=0; }
89
90     public boolean con() {
91         flush();
92         //dmask &= ~(1<<0);
93         dbangmode(dmask);
94         return (readPins() & (1<<0)) != 0;
95     }
96     public boolean rcon() {
97         flush();
98         dmask &= ~(1<<0);
99         dbangmode(dmask);
100         return (readPins() & (1<<0)) != 0;
101     }
102     public void con(boolean on) {
103         flush();
104         dmask |= (1<<0);
105         dbang(0, on);
106         dbangmode(dmask);
107     }
108 }