tree: 3e087ff8ebe8fd416f8edec361fba71ca813f377
  1. BUILD.bazel
  2. lib.rs
  3. README.md
protocol/usb/dfu/README.md

USB DFU 1.1 Library

This crate provides a USB DFU 1.1 class implementation for OpenPRoT.

1. Overview

The USB DFU 1.1 protocol allows for firmware upgrades over USB. This implementation is designed for limited-memory devices and supports multiple alternate settings to address different memory regions (e.g., internal flash, external SPI flash, bootloader).

2. USB Descriptors

2.1 DFU Functional Descriptor

The DFU functional descriptor follows the USB DFU 1.1 specification.

OffsetFieldSizeValueDescription
0bLength10x09Size of this descriptor in bytes.
1bDescriptorType10x21DFU FUNCTIONAL descriptor type.
2bmAttributes10x07bitCanDnload, bitCanUpload, bitManifestationTolerant.
3wDetachTimeOut20x0000Time in ms to wait after DFU_DETACH.
5wTransferSize2ConfigurableMaximum number of bytes the device can accept per control-write.
7bcdDFUVersion20x0110DFU specification release number in BCD.

2.2 Interface Descriptors

Multiple alternate settings are supported. Each altsetting typically represents a different memory partition.

OffsetFieldSizeValueDescription
0bLength10x09Size of this descriptor in bytes.
1bDescriptorType10x04INTERFACE descriptor type.
2bInterfaceNumber1VariableNumber of this interface.
3bAlternateSetting1VariableValue used to select this alternate setting.
4bNumEndpoints10x00No endpoints used (control only).
5bInterfaceClass10xFEApplication Specific Class.
6bInterfaceSubClass10x01Device Firmware Upgrade.
7bInterfaceProtocol10x02DFU Mode.
8iInterface1VariableIndex of string descriptor describing this partition.

3. DFU Requests

The following DFU-specific requests are supported on the control endpoint (0):

RequestCodeDirectionDescription
DFU_DETACH0Host to DeviceRequests the device to leave its current mode and enter DFU mode.
DFU_DNLOAD1Host to DeviceData packets sent from the host to the device.
DFU_UPLOAD2Device to HostData packets sent from the device to the host.
DFU_GETSTATUS3Device to HostReturns the current state and status of the device.
DFU_CLRSTATUS4Host to DeviceClears error status and returns to dfuIDLE.
DFU_GETSTATE5Device to HostReturns the current state of the device.
DFU_ABORT6Host to DeviceAborts the current operation and returns to dfuIDLE.

4. State Machine

This implementation follows the DFU 1.1 state machine. For simplicity, all operations (flash erase/write) are performed synchronously within the DFU_DNLOAD and DFU_UPLOAD request handling, or immediately transition from SYNC to IDLE.

5. Application Interface (DfuHandler)

The application must provide an implementation of the DfuHandler trait.

pub trait DfuHandler {
    /// Handle a DNLOAD block.
    fn dnload(&mut self, alt: u8, block_num: u16, data: &[u8]) -> Result<(), DfuStatus>;
    /// Handle an UPLOAD block.
    fn upload(&mut self, alt: u8, block_num: u16, data: &mut [u8]) -> Result<usize, DfuStatus>;
    /// Perform manifestation.
    fn manifest(&mut self) -> Result<(), DfuStatus>;
    /// Abort current operation.
    fn abort(&mut self);
}