1 package com.atmel.fpslic;
7 * Bitstream level access to the bootstrap interface (protocol wrapper around FpslicPins)
9 public class FpslicBoot {
11 private FpslicBootPins pins;
13 public FpslicBoot(FpslicBootPins pins) throws IOException {
18 public void reset() throws IOException {
20 pins.avrrstPin(false);
21 pins.configDataPin(false);
29 try { Thread.sleep(500); } catch (Exception e) { }
30 if (pins.initPin()) throw new RuntimeException("INIT was still high after pulling RESET low");
33 try { Thread.sleep(500); } catch (Exception e) { }
34 if (!pins.initPin()) throw new RuntimeException("INIT was still low after releasing RESET");
40 public OutputStream getConfigStream() throws IOException {
42 return new OutputStream() {
45 public void write(int in) throws IOException {
46 for(int i=7; i>=0; i--) {
48 sendConfigBits((((in & 0xff) & (1<<i))!=0)?1:0, 1);
51 public void write(byte[] b, int off, int len) throws IOException {
52 for(int i=off; i<off+len; i++)
55 public void flush() throws IOException {
58 public void close() throws IOException {
62 // turn off the CON pin we've been pulling low...
66 throw new RuntimeException("initialization failed at " + bytes);
67 for(int i=0; i<100; i++) {
70 throw new RuntimeException("initialization failed at " + bytes);
71 try { Thread.sleep(20); } catch (Exception e) { }
80 public OutputStream getOutputStream() throws IOException { return pins.getUartOutputStream(); }
81 public InputStream getInputStream() throws IOException { return pins.getUartInputStream(); }
82 public void purge() throws IOException { pins.purge(); }
84 public void selfTest() throws Exception {
86 System.out.print("smoke check: ");
90 sendConfigBits(Integer.parseInt("00000000", 2), 8);
91 sendConfigBits(Integer.parseInt("10110111", 2), 8);
94 try { Thread.sleep(100); } catch (Exception e) { }
96 System.out.print((pin ? green(" [pass]") : red(" [FAIL]")));
98 // preamble shifted one bit earlier than it should be
100 sendConfigBits(Integer.parseInt("0000000", 2), 7);
101 sendConfigBits(Integer.parseInt("10110111", 2), 8);
102 sendConfigBits(0, 2);
104 try { Thread.sleep(100); } catch (Exception e) { }
105 pin = pins.initPin();
106 System.out.print((pin ? red(" [FAIL]") : green(" [pass]")));
108 // preamble shifted one bit later than it should be
110 sendConfigBits(Integer.parseInt("000000000", 2), 9);
111 sendConfigBits(Integer.parseInt("10110111", 2), 8);
112 //sendConfigBits(0, 1);
114 try { Thread.sleep(100); } catch (Exception e) { }
115 pin = pins.initPin();
116 System.out.print((pin ? red(" [FAIL]") : green(" [pass]")));
118 // plain 'ol bogus preamble
120 sendConfigBits(Integer.parseInt("00000000", 2), 8);
121 sendConfigBits(Integer.parseInt("11110111", 2), 8);
122 sendConfigBits(0, 1);
124 try { Thread.sleep(100); } catch (Exception e) { }
125 pin = pins.initPin();
126 System.out.print((pin ? red(" [FAIL]") : green(" [pass]")));
128 System.out.println();
131 // Private //////////////////////////////////////////////////////////////////////////////
133 private void sendConfigBits(int dat, int numbits) throws IOException {
134 for(int i=(numbits-1); i>=0; i--) {
135 boolean bit = (dat & (1<<i)) != 0;
136 pins.configDataPin(bit);
139 //dbits &= ~(1<<6); // let the clock fall with the next data bit, whenever it goes out
143 private static String red(Object o) { return "\033[31m"+o+"\033[0m"; }
144 private static String green(Object o) { return "\033[32m"+o+"\033[0m"; }