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