checkpoint
[slipway.git] / src / com / atmel / fpslic / FpslicPinsUsb.java
1 package com.atmel.fpslic;
2 import com.ftdi.usb.*;
3 import java.io.*;
4
5 /**
6  * Exposes the raw pin-level configuration interface to the FPSLIC
7  */
8 public class FpslicPinsUsb implements FpslicPins {
9
10     private FtdiUart ftdiuart;
11
12     public FpslicPinsUsb(FtdiUart ftdiuart) {
13         this.ftdiuart = ftdiuart;
14     }
15
16     public void avrrstPin(boolean on)     throws IOException { setDBusLine(7, on); }
17     public void cclkPin(boolean on)       throws IOException { setDBusLine(6, on); }
18     public void configDataPin(boolean on) throws IOException { setDBusLine(5, on); }
19     public boolean initPin()              throws IOException { flush(); return (ftdiuart.readPins() & (1<<4))!=0; }
20
21     // tricky: RESET has a weak pull-up, and is wired to a CBUS line.  So,
22     //         we can pull it down (assert reset) from uart-mode, or we can
23     //         let it float upward from either mode.
24     public void resetPin(boolean on) throws IOException {
25         ftdiuart.uart_and_cbus_mode(1<<1, on ? (1<<1) : 0);
26         flush();
27         if (on) {
28             ftdiuart.dbus_mode(dmask);
29             flush();
30         }
31     }
32
33     public void flush() throws IOException { ftdiuart.getOutputStream().flush(); }
34
35     private int dbits = 0;
36     public void setDBusLine() throws IOException {
37         ftdiuart.getOutputStream().write((byte)dbits);
38     }
39     public void clearDBusLines() throws IOException {
40         dbits = 0;
41         setDBusLine();
42     }
43     public void setDBusLine(int bit, boolean val) throws IOException {
44         dbits = val ? (dbits | (1 << bit)) : (dbits & (~(1 << bit)));
45         setDBusLine();
46     }
47     
48     public void releaseConPin() throws IOException {
49         dmask &= ~(1<<0);
50         ftdiuart.dbus_mode(dmask);
51         flush();
52     }
53
54     public void conPin(boolean on) throws IOException {
55         dmask |= (1<<0);
56         ftdiuart.dbus_mode(dmask);
57         setDBusLine(0, on);
58         flush();
59     }
60
61     public void close() throws IOException {
62         // switching to uart mode will implicitly release AVRRST
63         avrrstPin(false);
64         ftdiuart.purge();
65         ftdiuart.uart_and_cbus_mode(1<<1, 1<<1);
66     }
67     
68     private int dmask =
69         (1<<0) |
70         (1<<1) |
71         (1<<2) |
72         //(1<<3) |
73         //(1<<4) |
74         (1<<5) |
75         (1<<6) |
76         (1<<7);
77
78     public InputStream  getUartInputStream() throws IOException { return ftdiuart.getInputStream(); }
79     public OutputStream getUartOutputStream() throws IOException { return ftdiuart.getOutputStream(); }
80 }
81