Does not work yet but getting closer?

This commit is contained in:
rusefi 2020-06-27 14:58:45 -04:00
parent 991e83c506
commit 32d17463fc
7 changed files with 77 additions and 4 deletions

View File

@ -7,7 +7,12 @@ public enum DfuCommmand {
GETSTATUS(3),
CLRSTATUS(4),
GETSTATE(5),
ABORT(6);
ABORT(6),
/**
* http://dfu-util.sourceforge.net/dfuse.html
*/
SE_SET_ADDRESS(0x21);
private final byte value;

View File

@ -0,0 +1,13 @@
package com.rusefi.dfu.commands;
import com.rusefi.dfu.DfuCommmand;
import com.rusefi.dfu.usb4java.USBDfuConnection;
import java.nio.ByteBuffer;
public class DfuCommandAbort {
public static void execute(USBDfuConnection session) {
ByteBuffer buffer = ByteBuffer.allocateDirect(0);
session.sendData(DfuCommmand.ABORT, (short) 0, buffer);
}
}

View File

@ -6,11 +6,8 @@ import com.rusefi.dfu.usb4java.USBDfuConnection;
import java.nio.ByteBuffer;
public class DfuCommandClearStatus {
public static void execute(USBDfuConnection session) {
ByteBuffer buffer = ByteBuffer.allocateDirect(0);
session.sendData(DfuCommmand.CLRSTATUS, (short) 0, buffer);
}
}

View File

@ -0,0 +1,14 @@
package com.rusefi.dfu.commands;
import com.rusefi.dfu.DfuCommmand;
import com.rusefi.dfu.usb4java.USBDfuConnection;
import java.nio.ByteBuffer;
public class DfuSeCommandSetAddress {
public static void execute(USBDfuConnection session, int address) {
ByteBuffer buffer = ByteBuffer.allocateDirect(4);
buffer.putInt(address);
session.sendData(DfuCommmand.CLRSTATUS, (short) 0, buffer);
}
}

View File

@ -2,6 +2,7 @@ package com.rusefi.dfu.usb4java;
import com.rusefi.dfu.DfuSeFlashDescriptor;
import com.rusefi.dfu.FlashRange;
import com.rusefi.dfu.commands.DfuCommandAbort;
import com.rusefi.dfu.commands.DfuCommandClearStatus;
import com.rusefi.dfu.commands.DfuCommandGetStatus;
import org.apache.commons.logging.Log;
@ -150,6 +151,14 @@ public class DfuDeviceLocator {
case DFU_ERROR:
DfuCommandClearStatus.execute(session);
break;
case DFU_DOWNLOAD_SYNC:
case DFU_DOWNLOAD_IDLE:
case DFU_UPLOAD_IDLE:
case DFU_MANIFEST_SYNC:
case DFU_DOWNLOAD_BUSY:
case DFU_MANIFEST:
DfuCommandAbort.execute(session);
break;
default:
throw new IllegalStateException("Unexpected state " + state);
}

View File

@ -15,6 +15,8 @@ public class USBDfuConnection implements DfuConnection {
private final FlashRange flashRange;
public USBDfuConnection(DeviceHandle deviceHandle, byte interfaceNumber, int transferSize, FlashRange flashRange) {
if (transferSize == 0)
throw new IllegalArgumentException("transfer size not detected");
this.deviceHandle = deviceHandle;
this.interfaceNumber = interfaceNumber;
this.transferSize = transferSize;

View File

@ -1,5 +1,7 @@
package com.rusefi.dfu;
import com.rusefi.dfu.commands.DfuCommandGetStatus;
import com.rusefi.dfu.commands.DfuSeCommandSetAddress;
import com.rusefi.dfu.usb4java.DfuDeviceLocator;
import com.rusefi.dfu.usb4java.LogUtil;
import com.rusefi.dfu.usb4java.USBDfuConnection;
@ -8,6 +10,7 @@ import org.apache.commons.logging.Log;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
public class Sandbox {
private static final Log log = LogUtil.getLog(Sandbox.class);
@ -23,7 +26,37 @@ public class Sandbox {
HexImage image = HexImage.loadHexToBuffer(new FileInputStream("rusefi.hex"), device.getFlashRange());
for (int offset = 0; offset < device.getFlashRange().getTotalLength(); offset += device.getTransferSize()) {
System.out.println("Handing offset " + offset);
DfuSeCommandSetAddress.execute(device, device.getFlashRange().getBaseAddress() + offset);
waitStatus(device);
ByteBuffer buffer = ByteBuffer.allocateDirect(device.getTransferSize());
buffer.put(image.getImage(), offset, device.getTransferSize());
device.sendData(DfuCommmand.DNLOAD, (short) 2, buffer);
waitStatus(device);
}
log.info("STM32 DFU " + device);
}
private static void waitStatus(USBDfuConnection device) {
DfuCommandGetStatus.State state = DfuCommandGetStatus.read(device);
System.out.println(" state " + state);
while (state == DfuCommandGetStatus.State.DFU_DOWNLOAD_BUSY) {
sleep(106);
state = DfuCommandGetStatus.read(device);
System.out.println(" state " + state);
}
}
private static void sleep(int millis) {
System.out.println("Sleep " + millis);
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
}
}