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 FpslicPins pins;
12 public FpslicRawUsb(FtdiUart ftdiuart) throws IOException {
13 this.pins = new FpslicPinsUsb(ftdiuart);
17 public void reset() throws IOException {
19 pins.avrrstPin(false);
20 pins.configDataPin(false);
28 try { Thread.sleep(500); } catch (Exception e) { }
29 if (pins.initPin()) throw new RuntimeException("INIT was still high after pulling RESET low");
32 try { Thread.sleep(500); } catch (Exception e) { }
33 if (!pins.initPin()) throw new RuntimeException("INIT was still low after releasing RESET");
39 public OutputStream getConfigStream() throws IOException {
41 return new OutputStream() {
44 public void write(int in) throws IOException {
45 for(int i=7; i>=0; i--) {
47 sendConfigBits((((in & 0xff) & (1<<i))!=0)?1:0, 1);
50 public void write(byte[] b, int off, int len) throws IOException {
51 for(int i=off; i<off+len; i++)
54 public void flush() throws IOException {
57 public void close() throws IOException {
61 // turn off the CON pin we've been pulling low...
65 throw new RuntimeException("initialization failed at " + bytes);
66 for(int i=0; i<100; i++) {
69 throw new RuntimeException("initialization failed at " + bytes);
70 try { Thread.sleep(20); } catch (Exception e) { }
79 public OutputStream getOutputStream() throws IOException { return pins.getUartOutputStream(); }
80 public InputStream getInputStream() throws IOException { return pins.getUartInputStream(); }
82 public void selfTest() throws Exception {
84 System.out.print("smoke check: ");
88 sendConfigBits(Integer.parseInt("00000000", 2), 8);
89 sendConfigBits(Integer.parseInt("10110111", 2), 8);
92 try { Thread.sleep(100); } catch (Exception e) { }
94 System.out.print((pin ? green(" [pass]") : red(" [FAIL]")));
96 // preamble shifted one bit earlier than it should be
98 sendConfigBits(Integer.parseInt("0000000", 2), 7);
99 sendConfigBits(Integer.parseInt("10110111", 2), 8);
100 sendConfigBits(0, 2);
102 try { Thread.sleep(100); } catch (Exception e) { }
103 pin = pins.initPin();
104 System.out.print((pin ? red(" [FAIL]") : green(" [pass]")));
106 // preamble shifted one bit later than it should be
108 sendConfigBits(Integer.parseInt("000000000", 2), 9);
109 sendConfigBits(Integer.parseInt("10110111", 2), 8);
110 //sendConfigBits(0, 1);
112 try { Thread.sleep(100); } catch (Exception e) { }
113 pin = pins.initPin();
114 System.out.print((pin ? red(" [FAIL]") : green(" [pass]")));
116 // plain 'ol bogus preamble
118 sendConfigBits(Integer.parseInt("00000000", 2), 8);
119 sendConfigBits(Integer.parseInt("11110111", 2), 8);
120 sendConfigBits(0, 1);
122 try { Thread.sleep(100); } catch (Exception e) { }
123 pin = pins.initPin();
124 System.out.print((pin ? red(" [FAIL]") : green(" [pass]")));
126 System.out.println();
129 // Private //////////////////////////////////////////////////////////////////////////////
131 private void sendConfigBits(int dat, int numbits) throws IOException {
132 for(int i=(numbits-1); i>=0; i--) {
133 boolean bit = (dat & (1<<i)) != 0;
134 pins.configDataPin(bit);
137 //dbits &= ~(1<<6); // let the clock fall with the next data bit, whenever it goes out
141 private static String red(Object o) { return "\033[31m"+o+"\033[0m"; }
142 private static String green(Object o) { return "\033[32m"+o+"\033[0m"; }