let's located STM DFU

This commit is contained in:
rusefi 2020-06-26 21:55:43 -04:00
parent 453cb91507
commit 511e27d519
5 changed files with 104 additions and 1 deletions

15
.idea/jarRepositories.xml Normal file
View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="MavenRepo" />
<option name="name" value="MavenRepo" />
<option name="url" value="https://repo.maven.apache.org/maven2/" />
</remote-repository>
</component>
</project>

View File

@ -1 +1,10 @@
apply plugin: 'java'
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
implementation group: 'org.usb4java', name: 'usb4java', version: '1.3.0'
implementation group: 'commons-logging', name: 'commons-logging', version: '1.2'
}

View File

@ -0,0 +1,52 @@
package com.rusefi.dfu;
import org.apache.commons.logging.Log;
import org.usb4java.*;
public class DfuDeviceLocator {
private static final short ST_VENDOR = 0x0483;
private static final short ST_DFU_PRODUCT = (short) 0xdf11;
private static final Log log = LogUtil.getLog(DfuDeviceLocator.class);
public static Device findDevice() {
return findDevice(openContext(), ST_VENDOR, ST_DFU_PRODUCT);
}
public static Device findDevice(Context context, short vendorId, short productId) {
// Read the USB device list
DeviceList list = new DeviceList();
int result = LibUsb.getDeviceList(context, list);
if (result < 0)
throw new LibUsbException("Unable to get device list", result);
log.info(list.getSize() + " device(s) found");
try {
// Iterate over all devices and scan for the right one
for (Device device : list) {
DeviceDescriptor descriptor = new DeviceDescriptor();
result = LibUsb.getDeviceDescriptor(device, descriptor);
// System.out.println("I see " + descriptor);
if (result != LibUsb.SUCCESS)
throw new LibUsbException("Unable to read device descriptor", result);
if (descriptor.idVendor() == vendorId && descriptor.idProduct() == productId)
return device;
}
} finally {
// Ensure the allocated device list is freed
LibUsb.freeDeviceList(list, true);
}
// Device not found
return null;
}
public static Context openContext() {
Context context = new Context();
int result = LibUsb.init(context);
if (result != LibUsb.SUCCESS)
throw new LibUsbException("Unable to initialize libusb.", result);
log.info("Welcome " + context);
return context;
}
}

View File

@ -0,0 +1,12 @@
package com.rusefi.dfu;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class LogUtil {
// todo: one day I would love to learn how to enable trace level with commons logging + java logging
static Log getLog(Class<?> clazz) {
return LogFactory.getLog(clazz);
}
}

View File

@ -0,0 +1,15 @@
package com.rusefi.dfu;
import org.apache.commons.logging.Log;
import org.usb4java.Device;
public class Sandbox {
private static final Log log = LogUtil.getLog(Sandbox.class);
public static void main(String[] args) {
log.info("Hello sandbox");
Device device = DfuDeviceLocator.findDevice();
log.info("STM32 DFU " + device);
}
}