dfu_java/src/main/java/com/rusefi/dfu/DfuLogic.java

42 lines
1.9 KiB
Java

package com.rusefi.dfu;
import com.rusefi.dfu.commands.DfuSeCommandErasePage;
import com.rusefi.dfu.commands.DfuSeCommandSetAddress;
import java.nio.ByteBuffer;
import java.util.List;
public class DfuLogic {
public static final short ST_VENDOR = 0x0483;
public static final int ST_DFU_PRODUCT = 0xdf11;
public static final int USB_CLASS_APP_SPECIFIC = 0xfe;
public static final byte DFU_SUBCLASS = 0x01;
public static final byte USB_DT_DFU = 0x21;
public static final String FLASH_TAG = "Flash";
public static void uploadImage(DfuConnection device, BinaryImage image, FlashRange range) {
List<Integer> erasePages = range.pagesForSize(image.getImageSize());
// todo: smarted start address logic
int eraseAddress = 0x08000000;
for (Integer erasePage : erasePages) {
DfuSeCommandErasePage.execute(device, eraseAddress);
eraseAddress += erasePage;
}
System.out.println(String.format("Erased up to %x", eraseAddress));
for (int offset = 0; offset < image.getImage().length; offset += device.getTransferSize()) {
DfuSeCommandSetAddress.execute(device, device.getFlashRange().getBaseAddress() + offset);
DfuConnectionUtil.waitStatus(device);
ByteBuffer buffer = ByteBuffer.allocateDirect(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);
device.sendData(DfuCommmand.DNLOAD, DfuSeCommand.W_DNLOAD, buffer);
// AN3156 USB DFU protocol used in the STM32 bootloader
// "The Write memory operation is effectively executed only when a DFU_GETSTATUS request is issued by the host. "
DfuConnectionUtil.waitStatus(device);
}
}
}