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;
22 public FpslicRawUsb(FtdiUart ftdiuart) throws IOException {
23 this.ftdiuart = ftdiuart;
27 public void reset() throws IOException {
38 ftdiuart.dbus_mode(dmask);
47 try { Thread.sleep(500); } catch (Exception e) { }
48 if (initErr()) throw new RuntimeException("INIT was still high after pulling RESET low");
52 try { Thread.sleep(500); } catch (Exception e) { }
53 if (!initErr()) throw new RuntimeException("INIT was still low after releasing RESET");
58 public OutputStream getConfigStream() throws IOException {
62 return new OutputStream() {
65 public void write(int in) throws IOException {
66 for(int i=7; i>=0; i--) {
68 config((((in & 0xff) & (1<<i))!=0)?1:0, 1);
72 public void write(byte[] b, int off, int len) throws IOException {
73 for(int i=off; i<off+len; i++)
76 public void flush() throws IOException {
77 FpslicRawUsb.this.flush();
80 public void close() throws IOException {
83 throw new RuntimeException("initialization failed at " + bytes);
84 for(int i=0; i<100; i++) {
87 throw new RuntimeException("initialization failed at " + bytes);
88 try { Thread.sleep(20); } catch (Exception e) { }
92 try { Thread.sleep(100); } catch (Exception e) { }
94 ftdiuart.uart_and_cbus_mode(1<<1, 1<<1);
99 public OutputStream getOutputStream() { return ftdiuart.getOutputStream(); }
100 public InputStream getInputStream() { return ftdiuart.getInputStream(); }
102 private void preamble() throws IOException {
106 public void selfTest() throws Exception {
113 config(Integer.parseInt("10110111", 2), 8);
116 try { Thread.sleep(100); } catch (Exception e) { }
118 System.out.println("good preamble => " + pin + " " + (pin ? green("good") : red("BAD")));
125 config(Integer.parseInt("10110111", 2), 8);
128 try { Thread.sleep(100); } catch (Exception e) { }
130 System.out.println("bad preamble #2 => " + pin + " " + (pin ? red("BAD") : green("good")));
136 config(Integer.parseInt("11110111", 2), 8);
139 try { Thread.sleep(100); } catch (Exception e) { }
141 System.out.println("bad preamble #1 => " + pin + " " + (pin ? red("BAD") : green("good")));
144 // Private //////////////////////////////////////////////////////////////////////////////
146 private void flush() throws IOException { ftdiuart.getOutputStream().flush(); }
148 private int dbits = 0;
149 private void dbang(int bit, boolean val) throws IOException {
150 dbits = val ? (dbits | (1 << bit)) : (dbits & (~(1 << bit)));
151 ftdiuart.getOutputStream().write((byte)dbits);
154 private void config(boolean bit) throws IOException { config(bit?1:0, 1); }
155 private void config(int dat) throws IOException { config(dat, 8); }
156 private void config(int dat, int numbits) throws IOException {
157 for(int i=(numbits-1); i>=0; i--) {
158 boolean bit = (dat & (1<<i)) != 0;
161 dbits &= ~(1<<6); // let the clock fall with the next data bit, whenever it goes out
165 // tricky: RESET has a weak pull-up, and is wired to a CBUS line. So,
166 // we can pull it down (assert reset) from uart-mode, or we can
167 // let it float upward from either mode.
168 private void reset(boolean on) throws IOException {
169 ftdiuart.uart_and_cbus_mode(1<<1, on ? (1<<1) : 0);
172 ftdiuart.dbus_mode(dmask);
177 private void avrrst(boolean on) throws IOException { dbang(7, on); }
178 private void clk(boolean on) throws IOException { dbang(6, on); }
179 private void data(boolean on) throws IOException { dbang(5, on); }
180 private boolean initErr() throws IOException { flush(); return (ftdiuart.readPins() & (1<<4))!=0; }
182 private boolean con() throws IOException {
185 ftdiuart.dbus_mode(dmask);
186 return (ftdiuart.readPins() & (1<<0)) != 0;
189 private boolean rcon() throws IOException {
192 ftdiuart.dbus_mode(dmask);
193 return (ftdiuart.readPins() & (1<<0)) != 0;
195 private void con(boolean on) throws IOException {
199 ftdiuart.dbus_mode(dmask);
202 private static String red(Object o) { return "\033[31m"+o+"\033[0m"; }
203 private static String green(Object o) { return "\033[32m"+o+"\033[0m"; }