Permalink
Browse files

o Trying to create a proper fx2 programmer CLI.

  • Loading branch information...
trygvis committed Aug 29, 2010
1 parent 25250d6 commit 25c81f813394eeedf6b0144fcd42801bda081612
@@ -1,17 +1,22 @@
package javax.usb.extra;
import javax.usb.*;
+import java.util.*;
public class ExtraUsbUtil {
- public static UsbDevice findDevice(UsbHub usbHub, short idVendor, short idProduct) {
+ public static boolean isUsbDevice(UsbDeviceDescriptor descriptor, short idVendor, short idProduct) {
+ return descriptor.idVendor() == idVendor && descriptor.idProduct() == idProduct;
+ }
+
+ public static UsbDevice findUsbDevice(UsbHub usbHub, short idVendor, short idProduct) {
for (UsbDevice device : usbHub.getAttachedUsbDevices()) {
if (device.isUsbHub()) {
continue;
}
UsbDeviceDescriptor deviceDescriptor = device.getUsbDeviceDescriptor();
- if (deviceDescriptor.idVendor() == idVendor && deviceDescriptor.idProduct() == idProduct) {
+ if (isUsbDevice(deviceDescriptor, idVendor, idProduct)) {
return device;
}
}
@@ -21,7 +26,7 @@ public static UsbDevice findDevice(UsbHub usbHub, short idVendor, short idProduc
continue;
}
- UsbDevice foundDevice = findDevice((UsbHub) device, idVendor, idProduct);
+ UsbDevice foundDevice = findUsbDevice((UsbHub) device, idVendor, idProduct);
if (foundDevice != null) {
return foundDevice;
@@ -30,4 +35,54 @@ public static UsbDevice findDevice(UsbHub usbHub, short idVendor, short idProduc
return null;
}
+
+ public static List<UsbDevice> findUsbDevices(UsbHub usbHub, short idVendor, short idProduct) {
+ List<UsbDevice> devices = new ArrayList<UsbDevice>();
+
+ for (UsbDevice device : usbHub.getAttachedUsbDevices()) {
+ if (device.isUsbHub()) {
+ continue;
+ }
+
+ UsbDeviceDescriptor deviceDescriptor = device.getUsbDeviceDescriptor();
+
+ if (deviceDescriptor.idVendor() == idVendor && deviceDescriptor.idProduct() == idProduct) {
+ devices.add(device);
+ }
+ }
+
+ for (UsbDevice device : usbHub.getAttachedUsbDevices()) {
+ if (!device.isUsbHub()) {
+ continue;
+ }
+
+ List<UsbDevice> foundDevices = findUsbDevices((UsbHub) device, idVendor, idProduct);
+
+ devices.addAll(foundDevices);
+ }
+
+ return devices;
+ }
+
+ public static List<UsbDevice> listUsbDevices(UsbHub usbHub) {
+ List<UsbDevice> devices = new ArrayList<UsbDevice>();
+
+ for (UsbDevice device : usbHub.getAttachedUsbDevices()) {
+ if (device.isUsbHub()) {
+ continue;
+ }
+
+ devices.add(device);
+ }
+
+ for (UsbDevice device : usbHub.getAttachedUsbDevices()) {
+ if (!device.isUsbHub()) {
+ continue;
+ }
+
+ devices.addAll(listUsbDevices(usbHub));
+ }
+
+ return devices;
+ }
}
View
@@ -21,6 +21,12 @@
<artifactId>javax.usb-extra</artifactId>
<version>${project.version}</version>
</dependency>
+ <dependency>
+ <groupId>commons-io</groupId>
+ <artifactId>commons-io</artifactId>
+ <version>1.4</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
<plugins>
@@ -83,6 +89,10 @@
<name>lsusb</name>
<mainClass>no.hackaton.usbtools.lsusb</mainClass>
</program>
+ <program>
+ <name>fx2programmer</name>
+ <mainClass>no.hackaton.usbtools.Fx2Programmer</mainClass>
+ </program>
</programs>
</configuration>
</plugin>
@@ -1,7 +1,6 @@
package no.hackaton.usbtools;
-import static java.lang.Long.toHexString;
-import static javax.usb.util.UsbUtil.unsignedInt;
+import static java.lang.Long.*;
import javax.usb.*;
import javax.usb.extra.*;
@@ -13,37 +12,26 @@
*/
public class Fx2Device implements Closeable {
public static final short CPUCS = (short) 0xE600;
+ public static final short FX2_ID_VENDOR = 0x04b4;
+ public static final short FX2_ID_PRODUCT = (short) 0x8613;
private final UsbDevice device;
-// private final UsbInterface usbInterface;
public static Fx2Device findFx2Device(UsbHub hub) throws UsbException {
- short idVendor = 0x04b4;
- short idProduct = (short) 0x8613;
-
- UsbDevice device = ExtraUsbUtil.findDevice(hub, idVendor, idProduct);
+ UsbDevice device = ExtraUsbUtil.findUsbDevice(hub, FX2_ID_VENDOR, FX2_ID_PRODUCT);
if (device == null) {
return null;
}
-// UsbInterface usbInterface = device.getActiveUsbConfiguration().getUsbInterface((byte) 0);
-// usbInterface.claim();
-
return new Fx2Device(device/*, null*/);
}
public Fx2Device(UsbDevice device/*, UsbInterface usbInterface*/) {
this.device = device;
-// this.usbInterface = usbInterface;
}
public void close() throws IOException {
-// try {
-// usbInterface.release();
-// } catch (UsbException e) {
-// throw new IOException(e.getMessage());
-// }
}
public void setReset() throws UsbException {
@@ -57,26 +45,58 @@ public void releaseReset() throws UsbException {
write(CPUCS, new byte[]{0x00});
}
- public void write(short address, byte[] bytes) throws UsbException {
- System.err.println("Fx2Device.write: address=" + toHexString(unsignedInt(address)) + ", bytes=" + bytes.length);
+ public void write(int address, byte[] bytes) throws UsbException {
+ write(address, bytes, 0, bytes.length);
+ }
+
+ public void write(int address, byte[] bytes, int offset, int length) throws UsbException {
+ System.err.println("Fx2Device.write: address=" + toHexString(address) + ", bytes=" + bytes.length);
byte bmRequestType = 0x40; // Vendor request, IN
byte bRequest = (byte) 0xa0;
- short wValue = address;
+ short wValue = (short) address;
short wIndex = 0;
UsbControlIrp irp = device.createUsbControlIrp(bmRequestType, bRequest, wValue, wIndex);
irp.setData(bytes);
+ irp.setOffset(offset);
+ irp.setLength(length);
device.syncSubmit(irp);
}
- public byte[] read(short address, int count) throws UsbException {
- System.err.println("Fx2Device.read: address=" + toHexString(unsignedInt(address)) + ", count=" + count);
+ public byte[] read(int address, int count) throws UsbException {
+ System.err.println("Fx2Device.read: address=" + toHexString(address) + ", count=" + count);
byte bmRequestType = (byte) 0xc0; // Vendor request, OUT
byte bRequest = (byte) 0xa0;
- short wValue = address;
+ short wValue = (short) address;
short wIndex = 0;
byte[] bytes = new byte[count];
device.syncSubmit(new DefaultUsbControlIrp(bytes, 0, count, true, bmRequestType, bRequest, wValue, wIndex));
return bytes;
}
+
+ public OutputStream toOutputStream() {
+ return new OutputStream() {
+ int address = 0;
+
+ @Override
+ public void write(byte[] bytes, int offset, int length) throws IOException {
+ try {
+ Fx2Device.this.write(address, bytes, offset, length);
+ address += length;
+ } catch (UsbException e) {
+ throw new IOException(e.getMessage());
+ }
+ }
+
+ @Override
+ public void write(int b) throws IOException {
+ try {
+ Fx2Device.this.write(address, new byte[]{(byte) b});
+ address++;
+ } catch (UsbException e) {
+ throw new IOException(e.getMessage());
+ }
+ }
+ };
+ }
}
@@ -1,41 +1,111 @@
package no.hackaton.usbtools;
+import static java.lang.Thread.sleep;
+import static java.util.Arrays.*;
import javalibusb1.*;
+import static javax.usb.extra.ExtraUsbUtil.*;
+import static javax.usb.util.UsbUtil.*;
import static no.hackaton.usbtools.Fx2Device.*;
+import no.hackaton.usbtools.IntelHex.*;
+import static no.hackaton.usbtools.IntelHex.RecordType.*;
+import static no.hackaton.usbtools.IntelHex.*;
import javax.usb.*;
import java.io.*;
+import java.util.ArrayList;
+import java.util.*;
public class Fx2Programmer {
- public static void main(String[] args) throws UsbException {
+ public static void main(String[] a) throws Exception {
System.setProperty(Libusb1UsbServices.JAVAX_USB_LIBUSB_TRACE_PROPERTY, "true");
UsbServices usbServices = UsbHostManager.getUsbServices();
+ UsbHub hub = usbServices.getRootUsbHub();
- Fx2Device device = findFx2Device(usbServices.getRootUsbHub());
+ short idVendor = FX2_ID_VENDOR;
+ short idProduct = FX2_ID_PRODUCT;
- if (device == null) {
- System.err.println("No FX2 attached.");
- return;
+ List<String> args = new ArrayList<String>();
+ args.addAll(asList(a));
+
+ Iterator<String> it = args.iterator();
+ while (it.hasNext()) {
+ String arg = it.next();
+ if (arg.equals("--list")) {
+ doList("0", hub, idVendor, idProduct);
+ break;
+ }
+ // TODO: Parse out --id=VVVV.PPPP[.N] and --device=[device path]
+ else {
+ commandPhase(args, hub, idVendor, idProduct);
+ }
}
+ }
+
+ private static void commandPhase(List<String> args, UsbHub hub, short idVendor, short idProduct) throws Exception {
+ Fx2Device device = new Fx2Device(findUsbDevice(hub, idVendor, idProduct));
- try {
- device.setReset();
+ for (String arg : args) {
+ if (arg.equals("reset")) {
+ doReset(device);
+ } else if (arg.equals("run")) {
+ doRun(device);
+ } else if (arg.startsWith("prg:")) {
+ File file = new File(arg.substring(4));
+
+ if (!file.canRead()) {
+ System.out.println("Can't read file: " + file);
+ break;
+ }
+
+ doProgramDevice(file, device);
+ } else if (arg.startsWith("delay:")) {
+ long delay = Long.parseLong(arg.substring(6));
+ doDelay(delay);
+ } else {
+ System.out.println("Unknown command: " + arg);
+ break;
+ }
+ }
+ }
- byte[] data = device.read((short)0xe000, 16);
- HexFormatter.writeBytes(System.out, data);
+ private static void doReset(Fx2Device device) throws UsbException {
+ device.setReset();
+ }
+
+ private static void doRun(Fx2Device device) throws UsbException {
+ device.releaseReset();
+ }
+
+ private static void doProgramDevice(File file, Fx2Device device) throws IOException, UsbException {
+ device.setReset(); // I think this is required
+ for (IntelHexPacket packet : openIntelHexFile(file)) {
+ if (packet.recordType.equals(DATA)) {
+ device.write(packet.address, packet.data);
+ } else {
+ System.out.println("Unknown packet type: " + packet.recordType + ".");
+ }
+ }
+ }
+
+ private static void doDelay(long delay) throws InterruptedException {
+ sleep(delay);
+ }
- data = new byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
- device.write((short)0xe000, data);
+ private static void doList(String prefix, UsbHub hub, short idVendor, short idProduct) {
+ List<UsbDevice> list = hub.getAttachedUsbDevices();
+ for (int i = 0; i < list.size(); i++) {
+ UsbDevice device = list.get(i);
+ if (device.isUsbHub()) {
+ doList(prefix + "." + i, (UsbHub) device, idVendor, idProduct);
+ } else {
+ UsbDeviceDescriptor deviceDescriptor = device.getUsbDeviceDescriptor();
- data = device.read((short)0xe000, 16);
- HexFormatter.writeBytes(System.out, data);
+ System.err.print(prefix + "." + i + " " + toHexString(deviceDescriptor.idVendor()) + ":" + toHexString(deviceDescriptor.idProduct()));
- device.releaseReset();
- } finally {
- try {
- device.close();
- } catch (IOException e) {
- // ignore
+ if (isUsbDevice(deviceDescriptor, idVendor, idProduct)) {
+ System.err.print(" (Unconfigured FX2)");
+ }
+ System.err.println();
}
}
}
Oops, something went wrong.

0 comments on commit 25c81f8

Please sign in to comment.