and now one step backwards :)

This commit is contained in:
rusefi 2020-07-05 19:22:01 -04:00
parent 74a938fd8f
commit 86c5b76487
12 changed files with 57 additions and 25 deletions

View File

@ -13,4 +13,6 @@ public interface DfuConnection {
int receiveData(DfuCommmand command, short wValue, ByteBuffer data);
int sendData(DfuCommmand command, short wValue, ByteBuffer data);
ByteBuffer allocateBuffer(int capacity);
}

View File

@ -20,6 +20,7 @@ public class DfuLogic {
}
public static void actuallyUploadImage(Logger logger, DfuConnection device, BinaryImage image, FlashRange range) {
long enter = System.currentTimeMillis();
List<Integer> erasePages = range.pagesForSize(image.getImageSize());
// todo: smarted start address logic
int eraseAddress = 0x08000000;
@ -33,7 +34,7 @@ public class DfuLogic {
DfuSeCommandSetAddress.execute(logger, device, device.getFlashRange().getBaseAddress() + offset);
DfuConnectionUtil.waitStatus(logger, device);
ByteBuffer buffer = ByteBuffer.allocate(device.getTransferSize());
ByteBuffer buffer = device.allocateBuffer(device.getTransferSize());
// last transfer would usually be smaller than transfer size
int size = Math.min(device.getTransferSize(), image.getImage().length - offset);
buffer.put(image.getImage(), offset, size);
@ -42,13 +43,16 @@ public class DfuLogic {
// "The Write memory operation is effectively executed only when a DFU_GETSTATUS request is issued by the host. "
DfuConnectionUtil.waitStatus(logger, device);
}
logger.info("Uploaded " + image.getImage().length + " bytes in " + (System.currentTimeMillis() - enter) + " ms");
}
public static void leaveDFU(Logger logger, DfuConnection device) {
device.sendData(DfuCommmand.DNLOAD, DfuSeCommand.W_DNLOAD, ByteBuffer.allocate(0));
logger.info("Leaving DFU");
device.sendData(DfuCommmand.DNLOAD, DfuSeCommand.W_DNLOAD, device.allocateBuffer(0));
// The DFU Leave operation is effectively executed only when a DFU_GETSTATUS request is
// issued by the host.
DfuConnectionUtil.waitStatus(logger, device);
logger.info("DONE");
}
public static void startup(Logger logger, DfuConnection device) {

View File

@ -12,14 +12,14 @@ import static android.hardware.usb.UsbConstants.USB_DIR_OUT;
public class AndroidDfuConnection implements DfuConnection {
private final UsbDeviceConnection usbDeviceConnection;
private final byte interfaceNumber;
private final int interfaceNumber;
private final int transferSize;
private final FlashRange flashRange;
private static final byte REQUEST_TYPE_CLASS = 32;
private static final byte RECIPIENT_INTERFACE = 0x01;
public AndroidDfuConnection(UsbDeviceConnection usbDeviceConnection, byte interfaceNumber, int transferSize, FlashRange flashRange) {
public AndroidDfuConnection(UsbDeviceConnection usbDeviceConnection, int interfaceNumber, int transferSize, FlashRange flashRange) {
this.usbDeviceConnection = usbDeviceConnection;
this.interfaceNumber = interfaceNumber;
this.transferSize = transferSize;
@ -46,7 +46,15 @@ public class AndroidDfuConnection implements DfuConnection {
return transfer(usbDeviceConnection, USB_DIR_OUT, command.getValue(), wValue, data);
}
@Override
public ByteBuffer allocateBuffer(int capacity) {
// on Android direct buffer comes with arrayOffset which is just too much trouble
return ByteBuffer.allocate(capacity);
}
private int transfer(UsbDeviceConnection connection, int direction, int request, short wValue, ByteBuffer byteBuffer) {
if (!byteBuffer.hasArray() || byteBuffer.arrayOffset() != 0)
throw new IllegalArgumentException("Need a simpler ByteArray");
return connection.controlTransfer(REQUEST_TYPE_CLASS | RECIPIENT_INTERFACE | direction, request,
wValue, interfaceNumber, byteBuffer.array(), byteBuffer.limit(), DFU_TIMEOUT);
}

View File

@ -42,7 +42,7 @@ public class DfuDeviceLocator {
throw new IllegalStateException("Unexpected USB_DT_DFU");
int transferSize = rawDescs[69] * 256 + rawDescs[68];
return new Result(connection, flashRange, transferSize);
return new Result(connection, interfaceIndex, flashRange, transferSize);
}
}
return null;
@ -50,11 +50,13 @@ public class DfuDeviceLocator {
public static class Result {
private final UsbDeviceConnection connection;
private final int interfaceIndex;
private final FlashRange flashRange;
private final int transferSize;
public Result(UsbDeviceConnection connection, FlashRange flashRange, int transferSize) {
public Result(UsbDeviceConnection connection, int interfaceIndex, FlashRange flashRange, int transferSize) {
this.connection = connection;
this.interfaceIndex = interfaceIndex;
this.flashRange = flashRange;
this.transferSize = transferSize;
}
@ -67,6 +69,10 @@ public class DfuDeviceLocator {
return flashRange;
}
public int getInterfaceIndex() {
return interfaceIndex;
}
public int getTransferSize() {
return transferSize;
}

View File

@ -6,8 +6,8 @@ import com.rusefi.dfu.DfuConnection;
import java.nio.ByteBuffer;
public class DfuCommandAbort {
public static void execute(DfuConnection session) {
ByteBuffer buffer = ByteBuffer.allocate(0);
session.sendData(DfuCommmand.ABORT, (short) 0, buffer);
public static void execute(DfuConnection connection) {
ByteBuffer buffer = connection.allocateBuffer(0);
connection.sendData(DfuCommmand.ABORT, (short) 0, buffer);
}
}

View File

@ -7,7 +7,7 @@ import java.nio.ByteBuffer;
public class DfuCommandClearStatus {
public static void execute(DfuConnection session) {
ByteBuffer buffer = ByteBuffer.allocate(0);
ByteBuffer buffer = session.allocateBuffer(0);
session.sendData(DfuCommmand.CLRSTATUS, (short) 0, buffer);
}
}

View File

@ -13,9 +13,9 @@ public class DfuCommandGetStatus {
private final static AtomicInteger STATUS_COUNTER = new AtomicInteger();
public static DeviceStatus read(DfuLogic.Logger logger, DfuConnection session) {
ByteBuffer buffer = ByteBuffer.allocate(PACKET_SIZE);
int count = session.receiveData(DfuCommmand.GETSTATUS, (short) 0, buffer);
public static DeviceStatus read(DfuLogic.Logger logger, DfuConnection connection) {
ByteBuffer buffer = connection.allocateBuffer(PACKET_SIZE);
int count = connection.receiveData(DfuCommmand.GETSTATUS, (short) 0, buffer);
if (count != PACKET_SIZE)
return new DeviceStatus(null, State.DFU_ERROR);
buffer.rewind();

View File

@ -5,10 +5,10 @@ import com.rusefi.dfu.*;
import java.nio.ByteBuffer;
public class DfuSeCommandErasePage {
public static void execute(DfuLogic.Logger logger, DfuConnection session, int address) {
public static void execute(DfuLogic.Logger logger, DfuConnection connection, int address) {
logger.info(String.format("SetAddress %x", address));
ByteBuffer buffer = DfuSeCommandSetAddress.createSpecialCommandBuffer(DfuSeCommand.SE_ERASE_PAGE, address);
session.sendData(DfuCommmand.DNLOAD, DfuSeCommand.W_SPECIAL, buffer);
DfuConnectionUtil.waitStatus(logger, session);
ByteBuffer buffer = DfuSeCommandSetAddress.createSpecialCommandBuffer(connection, DfuSeCommand.SE_ERASE_PAGE, address);
connection.sendData(DfuCommmand.DNLOAD, DfuSeCommand.W_SPECIAL, buffer);
DfuConnectionUtil.waitStatus(logger, connection);
}
}

View File

@ -9,14 +9,14 @@ import java.nio.ByteBuffer;
import java.nio.ByteOrder;
public class DfuSeCommandSetAddress {
public static void execute(DfuLogic.Logger logger, DfuConnection session, int address) {
public static void execute(DfuLogic.Logger logger, DfuConnection connection, int address) {
logger.info(String.format("SetAddress %x", address));
ByteBuffer buffer = createSpecialCommandBuffer(DfuSeCommand.SE_SET_ADDRESS, address);
session.sendData(DfuCommmand.DNLOAD, DfuSeCommand.W_SPECIAL, buffer);
ByteBuffer buffer = createSpecialCommandBuffer(connection, DfuSeCommand.SE_SET_ADDRESS, address);
connection.sendData(DfuCommmand.DNLOAD, DfuSeCommand.W_SPECIAL, buffer);
}
protected static ByteBuffer createSpecialCommandBuffer(byte command, int address) {
ByteBuffer buffer = createBuffer(5);
protected static ByteBuffer createSpecialCommandBuffer(DfuConnection connection, byte command, int address) {
ByteBuffer buffer = createBuffer(connection, 5);
buffer.put(command);
// buffer.rewind();
// byte[] t = new byte[4];
@ -25,7 +25,7 @@ public class DfuSeCommandSetAddress {
return buffer;
}
protected static ByteBuffer createBuffer(int capacity) {
return ByteBuffer.allocate(capacity).order(ByteOrder.LITTLE_ENDIAN);
protected static ByteBuffer createBuffer(DfuConnection connection, int capacity) {
return connection.allocateBuffer(capacity).order(ByteOrder.LITTLE_ENDIAN);
}
}

View File

@ -45,6 +45,12 @@ public class USBDfuConnection implements DfuConnection {
return transfer(command, wValue, data, LibUsb.ENDPOINT_OUT);
}
@Override
public ByteBuffer allocateBuffer(int capacity) {
// usb4java requires direct buffer
return ByteBuffer.allocateDirect(capacity);
}
private int transfer(DfuCommmand command, short wValue, ByteBuffer data, byte mode) {
return LibUsb.controlTransfer(
deviceHandle,

View File

@ -27,6 +27,11 @@ public class DfuLogicTest {
return data.limit();
}
@Override
public ByteBuffer allocateBuffer(int capacity) {
return ByteBuffer.allocate(capacity);
}
@Override
public int sendData(DfuCommmand command, short wValue, ByteBuffer data) {
return data.limit();

View File

@ -21,8 +21,9 @@ public class Sandbox {
return;
}
HexImage image = HexImage.loadHexToBuffer(new FileInputStream("rusefi.hex"), device.getFlashRange());
//HexImage image = HexImage.loadHexToBuffer(new FileInputStream("rusefi.hex"), device.getFlashRange());
DfuImage image = new DfuImage().read("rusefi_disco.dfu");
DfuLogic.uploadImage(logger, device, image, device.getFlashRange());
log.info("STM32 DFU " + device);