making things reusable by Android

This commit is contained in:
rusefi 2020-06-29 00:48:52 -04:00
parent 3395172a36
commit 49600d0303
4 changed files with 120 additions and 13 deletions

View File

@ -17,4 +17,6 @@ Zadig is a Windows application that installs generic USB drivers, such as WinUSB
Uses precompiled https://github.com/j123b567/java-intelhex-parser
This implementation would not happen without http://dfu-util.sourceforge.net/dfuse.html and https://github.com/kairyu/flop
This implementation would not happen without http://dfu-util.sourceforge.net/dfuse.html and https://github.com/kairyu/flop
https://github.com/UmbrelaSmart/android-stm32-dfu-programmer is also nice

81
reference/F407usb.txt Normal file
View File

@ -0,0 +1,81 @@
1 configuration(s)
Config: Configuration Descriptor:
bLength 9
bDescriptorType 2
wTotalLength 54
bNumInterfaces 1
bConfigurationValue 1
iConfiguration 0
bmAttributes 0xc0
Self Powered
bMaxPower 100mA
extralen 0
extra:
Interface:
numAltsetting 4
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 0
bAlternateSetting 0
bNumEndpoints 0
bInterfaceClass 254 Application
bInterfaceSubClass 1
bInterfaceProtocol 2
iInterface 4
extralen 0
extra:
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 0
bAlternateSetting 1
bNumEndpoints 0
bInterfaceClass 254 Application
bInterfaceSubClass 1
bInterfaceProtocol 2
iInterface 5
extralen 0
extra:
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 0
bAlternateSetting 2
bNumEndpoints 0
bInterfaceClass 254 Application
bInterfaceSubClass 1
bInterfaceProtocol 2
iInterface 6
extralen 0
extra:
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 0
bAlternateSetting 3
bNumEndpoints 0
bInterfaceClass 254 Application
bInterfaceSubClass 1
bInterfaceProtocol 2
iInterface 7
extralen 9
extra:
09 21 0b ff 00 00 08 1a 01
1 interface(s)
Interface #0 setting #0:
Setting 0: 0 4 class fe, subclass 1, protocol: 2
Descriptor @Internal Flash /0x08000000/04*016Kg,01*064Kg,07*128Kg
Interface #0 setting #1:
Setting 1: 0 5 class fe, subclass 1, protocol: 2
Descriptor @Option Bytes /0x1FFFC000/01*016 e
Interface #0 setting #2:
Setting 2: 0 6 class fe, subclass 1, protocol: 2
Descriptor @OTP Memory /0x1FFF7800/01*512 e,01*016 e
Interface #0 setting #3:
Setting 3: 0 7 class fe, subclass 1, protocol: 2
Descriptor @Device Feature/0xFFFF0000/01*004 e

View File

@ -9,7 +9,7 @@ import java.nio.ByteBuffer;
public class DfuLogic {
public static final short ST_VENDOR = 0x0483;
public static final int ST_DFU_PRODUCT = 0xdf11;
public static final byte USB_CLASS_APP_SPECIFIC = (byte) 0xfe;
public static final int USB_CLASS_APP_SPECIFIC = 0xfe;
public static final byte DFU_SUBCLASS = 0x01;
public static final byte USB_DT_DFU = 0x21;

View File

@ -16,6 +16,8 @@ public class DfuDeviceLocator {
private static final Log log = LogUtil.getLog(DfuDeviceLocator.class);
private static StringBuilder usbInfo = new StringBuilder();
public static Context openContext() {
Context context = new Context();
int result = LibUsb.init(context);
@ -64,7 +66,8 @@ public class DfuDeviceLocator {
public static USBDfuConnection findDfuInterface(Device device, DeviceDescriptor deviceDescriptor) {
byte numConfigurations = deviceDescriptor.bNumConfigurations();
log.info(numConfigurations + " configuration(s)");
appendInfo(numConfigurations + " configuration(s)");
DeviceHandle deviceHandle = open(device);
int transferSize = 0;
@ -77,11 +80,10 @@ public class DfuDeviceLocator {
throw new LibUsbException("getConfigDescriptor", result);
}
System.out.println("Config " + config);
System.out.println("Config Done");
appendInfo("Config: " + config);
byte numInterfaces = config.bNumInterfaces();
log.info(numInterfaces + " interface(s)");
appendInfo(numInterfaces + " interface(s)");
for (int interfaceIndex = 0; interfaceIndex < numInterfaces; interfaceIndex++) {
Interface iface = config.iface()[interfaceIndex];
@ -107,6 +109,26 @@ public class DfuDeviceLocator {
}
}
for (int interfaceIndex = 0; interfaceIndex < numInterfaces; interfaceIndex++) {
Interface iface = config.iface()[interfaceIndex];
for (int s = 0; s < iface.numAltsetting(); s++) {
InterfaceDescriptor setting = iface.altsetting()[s];
appendInfo("Interface #" + interfaceIndex + " setting #" + s + ":");
byte interfaceNumber = setting.bInterfaceNumber();
appendInfo(String.format("Setting %d: %x %x class %x, subclass %x, protocol: %x", s,
interfaceNumber,
setting.iInterface(),
setting.bInterfaceClass(),
setting.bInterfaceSubClass(),
setting.bInterfaceProtocol()
));
String stringDescriptor = LibUsb.getStringDescriptor(deviceHandle, setting.iInterface());
appendInfo("Descriptor " + stringDescriptor);
}
}
for (int interfaceIndex = 0; interfaceIndex < numInterfaces; interfaceIndex++) {
Interface iface = config.iface()[interfaceIndex];
@ -115,14 +137,8 @@ public class DfuDeviceLocator {
log.info("Settings " + setting);
byte interfaceNumber = setting.bInterfaceNumber();
log.info(String.format("Setting %d: %x %x class %x, subclass %x, protocol: %x", s,
interfaceNumber,
setting.iInterface(),
setting.bInterfaceClass(), setting.bInterfaceSubClass(),
setting.bInterfaceProtocol()
));
if (setting.bInterfaceClass() == DfuLogic.USB_CLASS_APP_SPECIFIC &&
if (setting.bInterfaceClass() == (byte) DfuLogic.USB_CLASS_APP_SPECIFIC &&
setting.bInterfaceSubClass() == DfuLogic.DFU_SUBCLASS) {
log.debug(String.format("Found DFU interface: %d", interfaceNumber));
@ -162,6 +178,9 @@ public class DfuDeviceLocator {
if (state != DfuCommandGetStatus.State.DFU_IDLE)
throw new IllegalStateException("Not idle");
System.out.printf("info:\n" + usbInfo);
return session;
}
}
@ -170,6 +189,11 @@ public class DfuDeviceLocator {
return null;
}
private static void appendInfo(String message) {
log.info(message);
usbInfo.append(message).append("\n");
}
private static DeviceHandle open(Device device) {
DeviceHandle deviceHandle = new DeviceHandle();
int result = LibUsb.open(device, deviceHandle);