checkpoint
[slipway.git] / src / edu / berkeley / obits / device / atmel / ChipImpl.java
1 package edu.berkeley.obits.device.atmel;
2 import com.ftdi.usb.*;
3 import java.io.*;
4
5 public class ChipImpl extends FtdiChip implements Chip {
6
7     protected 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();
19         doReset();
20     }
21
22     public boolean buffered = true;
23     public void buffered() { buffered = true; }
24     public void buffered(boolean buf) {
25         if (!buf) flush();
26         buffered = buf;
27     }
28
29     public void doReset() {
30
31         flush();
32
33         dbangmode(dmask);
34         flush();
35
36         clk(false);
37         data(false);
38         con(false);
39         flush();
40
41         reset(false);
42         flush();
43         try { Thread.sleep(500); } catch (Exception e) { }
44
45         reset(true);
46         flush();
47         try { Thread.sleep(500); } catch (Exception e) { }
48
49 System.out.println("\ndisagree:"+(dmask ^ (        //(1<<0) |
50         (1<<1) |
51         (1<<2) |
52         //(1<<3) |
53         //(1<<4) |
54         (1<<5) |
55         (1<<6) |
56         (1<<7)))+"\n");
57
58 //dmask &= ~(1<<7);
59         dbangmode(dmask);
60         flush();
61     }
62
63     int porte = 0;
64     public void porte(int pin, boolean b) {
65         porte = (~(1<<pin)) | (b ? (1<<pin) : 0);
66         if (pin==4) {
67             dbang(2, b);
68             flush();
69         }
70     }
71
72     public void config(boolean bit) { config(bit?1:0, 1); }
73     public void config(int dat) { config(dat, 8); }
74     public void config(int dat, int numbits) {
75         for(int i=(numbits-1); i>=0; i--) {
76             boolean bit = (dat & (1<<i)) != 0;
77             data(bit);
78             clk(true);
79             clk(false);
80         }
81     }
82
83     public void reset(boolean on) {
84         bits = on ? (1<<1) : 0;
85         uart();
86     }
87     public void avrrst(boolean on) { dbang(7, on); }
88     public void clk(boolean on)    { dbang(6, on); }
89     public void data(boolean on)   { dbang(5, on); }
90
91     public boolean initErr()       { flush(); return (readPins() & (1<<4))!=0; }
92     public boolean con() {
93         flush();
94         dmask &= ~(1<<0);
95         dbangmode(dmask);
96         return (readPins() & (1<<0)) != 0;
97     }
98     public void con(boolean on) {
99         flush();
100         dmask |= (1<<0);
101         dbangmode(dmask);
102         dbang(0, on);
103     }
104 }