1 package com.atmel.fpslic;
6 * Exposes the FpslicRaw interface of an FPSLIC wired to an FTDI USB-UART.
8 public class FpslicRawUsb implements FpslicRaw {
10 private FtdiUart ftdiuart;
11 private FpslicPinsUsb pins;
23 public FpslicRawUsb(FtdiUart ftdiuart) throws IOException {
24 this.ftdiuart = ftdiuart;
25 //this.pins = new FpslicPinsUsb(ftdiuart);
29 public void reset() throws IOException {
40 try { Thread.sleep(500); } catch (Exception e) { }
41 if (initPin()) throw new RuntimeException("INIT was still high after pulling RESET low");
44 try { Thread.sleep(500); } catch (Exception e) { }
45 if (!initPin()) throw new RuntimeException("INIT was still low after releasing RESET");
51 public OutputStream getConfigStream() throws IOException {
53 return new OutputStream() {
56 public void write(int in) throws IOException {
57 for(int i=7; i>=0; i--) {
59 sendConfigBits((((in & 0xff) & (1<<i))!=0)?1:0, 1);
62 public void write(byte[] b, int off, int len) throws IOException {
63 for(int i=off; i<off+len; i++)
66 public void flush() throws IOException {
67 FpslicRawUsb.this.flush();
69 public void close() throws IOException {
73 // turn off the CON pin we've been pulling low...
77 throw new RuntimeException("initialization failed at " + bytes);
78 for(int i=0; i<100; i++) {
81 throw new RuntimeException("initialization failed at " + bytes);
82 try { Thread.sleep(20); } catch (Exception e) { }
86 // switching to uart mode will implicitly release AVRRST
89 ftdiuart.uart_and_cbus_mode(1<<1, 1<<1);
94 public OutputStream getOutputStream() { return ftdiuart.getOutputStream(); }
95 public InputStream getInputStream() { return ftdiuart.getInputStream(); }
97 public void selfTest() throws Exception {
99 System.out.print("smoke check: ");
103 sendConfigBits(Integer.parseInt("00000000", 2), 8);
104 sendConfigBits(Integer.parseInt("10110111", 2), 8);
107 try { Thread.sleep(100); } catch (Exception e) { }
109 System.out.print((pin ? green(" [pass]") : red(" [FAIL]")));
111 // preamble shifted one bit earlier than it should be
113 sendConfigBits(Integer.parseInt("0000000", 2), 7);
114 sendConfigBits(Integer.parseInt("10110111", 2), 8);
115 sendConfigBits(0, 2);
117 try { Thread.sleep(100); } catch (Exception e) { }
119 System.out.print((pin ? red(" [FAIL]") : green(" [pass]")));
121 // preamble shifted one bit later than it should be
123 sendConfigBits(Integer.parseInt("000000000", 2), 9);
124 sendConfigBits(Integer.parseInt("10110111", 2), 8);
125 //sendConfigBits(0, 1);
127 try { Thread.sleep(100); } catch (Exception e) { }
129 System.out.print((pin ? red(" [FAIL]") : green(" [pass]")));
131 // plain 'ol bogus preamble
133 sendConfigBits(Integer.parseInt("00000000", 2), 8);
134 sendConfigBits(Integer.parseInt("11110111", 2), 8);
135 sendConfigBits(0, 1);
137 try { Thread.sleep(100); } catch (Exception e) { }
139 System.out.print((pin ? red(" [FAIL]") : green(" [pass]")));
141 System.out.println();
144 // Private //////////////////////////////////////////////////////////////////////////////
146 private void flush() throws IOException { ftdiuart.getOutputStream().flush(); }
148 private int dbits = 0;
149 private void setDBusLine() throws IOException {
150 ftdiuart.getOutputStream().write((byte)dbits);
152 private void clearDBusLines() throws IOException {
156 private void setDBusLine(int bit, boolean val) throws IOException {
157 dbits = val ? (dbits | (1 << bit)) : (dbits & (~(1 << bit)));
161 private void sendConfigBits(int dat, int numbits) throws IOException {
162 for(int i=(numbits-1); i>=0; i--) {
163 boolean bit = (dat & (1<<i)) != 0;
166 dbits &= ~(1<<6); // let the clock fall with the next data bit, whenever it goes out
170 // tricky: RESET has a weak pull-up, and is wired to a CBUS line. So,
171 // we can pull it down (assert reset) from uart-mode, or we can
172 // let it float upward from either mode.
173 private void resetPin(boolean on) throws IOException {
174 ftdiuart.uart_and_cbus_mode(1<<1, on ? (1<<1) : 0);
177 ftdiuart.dbus_mode(dmask);
182 private void releaseConPin() throws IOException {
184 ftdiuart.dbus_mode(dmask);
188 private void conPin(boolean on) throws IOException {
190 ftdiuart.dbus_mode(dmask);
195 private void avrrstPin(boolean on) throws IOException { setDBusLine(7, on); }
196 private void cclkPin(boolean on) throws IOException { setDBusLine(6, on); }
197 private void configDataPin(boolean on) throws IOException { setDBusLine(5, on); }
198 private boolean initPin() throws IOException { flush(); return (ftdiuart.readPins() & (1<<4))!=0; }
200 private static String red(Object o) { return "\033[31m"+o+"\033[0m"; }
201 private static String green(Object o) { return "\033[32m"+o+"\033[0m"; }