[embassy-usb-dfu] accept closure to customise DFU function

This provides a more generic interface for users to customise the DFU function instead of restricting customisation to DFU headers.
This commit is contained in:
Gerhard de Clercq 2025-05-14 09:52:46 +02:00
parent 46e25cbc5f
commit d4d10bad0b
4 changed files with 30 additions and 40 deletions

View File

@ -2,7 +2,7 @@ use embassy_boot::BlockingFirmwareState;
use embassy_time::{Duration, Instant}; use embassy_time::{Duration, Instant};
use embassy_usb::control::{InResponse, OutResponse, Recipient, RequestType}; use embassy_usb::control::{InResponse, OutResponse, Recipient, RequestType};
use embassy_usb::driver::Driver; use embassy_usb::driver::Driver;
use embassy_usb::{msos, Builder, Handler}; use embassy_usb::{Builder, FunctionBuilder, Handler};
use embedded_storage::nor_flash::NorFlash; use embedded_storage::nor_flash::NorFlash;
use crate::consts::{ use crate::consts::{
@ -130,22 +130,13 @@ pub fn usb_dfu<'d, D: Driver<'d>, MARK: DfuMarker, RST: Reset>(
builder: &mut Builder<'d, D>, builder: &mut Builder<'d, D>,
handler: &'d mut Control<MARK, RST>, handler: &'d mut Control<MARK, RST>,
timeout: Duration, timeout: Duration,
winusb_guids: Option<&'d [&str]>, func_modifier: impl Fn(&mut FunctionBuilder<'_, 'd, D>),
) { ) {
let mut func = builder.function(0x00, 0x00, 0x00); let mut func = builder.function(0x00, 0x00, 0x00);
if let Some(winusb_guids) = winusb_guids {
// We add MSOS headers so that the device automatically gets assigned the WinUSB driver on Windows. // Here we give users the opportunity to add their own function level MSOS headers for instance.
// Otherwise users need to do this manually using a tool like Zadig. // This is useful when DFU functionality is part of a composite USB device.
// func_modifier(&mut func);
// Adding them here on the function level appears to only be needed for compositive devices.
// In addition to being on the function level, they should also be added to the device level.
//
func.msos_feature(msos::CompatibleIdFeatureDescriptor::new("WINUSB", ""));
func.msos_feature(msos::RegistryPropertyFeatureDescriptor::new(
"DeviceInterfaceGUIDs",
msos::PropertyData::RegMultiSz(winusb_guids),
));
}
let mut iface = func.interface(); let mut iface = func.interface();
let mut alt = iface.alt_setting(USB_CLASS_APPN_SPEC, APPN_SPEC_SUBCLASS_DFU, DFU_PROTOCOL_RT, None); let mut alt = iface.alt_setting(USB_CLASS_APPN_SPEC, APPN_SPEC_SUBCLASS_DFU, DFU_PROTOCOL_RT, None);

View File

@ -1,7 +1,7 @@
use embassy_boot::{AlignedBuffer, BlockingFirmwareUpdater, FirmwareUpdaterError}; use embassy_boot::{AlignedBuffer, BlockingFirmwareUpdater, FirmwareUpdaterError};
use embassy_usb::control::{InResponse, OutResponse, Recipient, RequestType}; use embassy_usb::control::{InResponse, OutResponse, Recipient, RequestType};
use embassy_usb::driver::Driver; use embassy_usb::driver::Driver;
use embassy_usb::{msos, Builder, Handler}; use embassy_usb::{Builder, FunctionBuilder, Handler};
use embedded_storage::nor_flash::{NorFlash, NorFlashErrorKind}; use embedded_storage::nor_flash::{NorFlash, NorFlashErrorKind};
use crate::consts::{ use crate::consts::{
@ -186,22 +186,13 @@ impl<'d, DFU: NorFlash, STATE: NorFlash, RST: Reset, const BLOCK_SIZE: usize> Ha
pub fn usb_dfu<'d, D: Driver<'d>, DFU: NorFlash, STATE: NorFlash, RST: Reset, const BLOCK_SIZE: usize>( pub fn usb_dfu<'d, D: Driver<'d>, DFU: NorFlash, STATE: NorFlash, RST: Reset, const BLOCK_SIZE: usize>(
builder: &mut Builder<'d, D>, builder: &mut Builder<'d, D>,
handler: &'d mut Control<'d, DFU, STATE, RST, BLOCK_SIZE>, handler: &'d mut Control<'d, DFU, STATE, RST, BLOCK_SIZE>,
winusb_guids: Option<&'d [&str]>, func_modifier: impl Fn(&mut FunctionBuilder<'_, 'd, D>),
) { ) {
let mut func = builder.function(USB_CLASS_APPN_SPEC, APPN_SPEC_SUBCLASS_DFU, DFU_PROTOCOL_DFU); let mut func = builder.function(USB_CLASS_APPN_SPEC, APPN_SPEC_SUBCLASS_DFU, DFU_PROTOCOL_DFU);
if let Some(winusb_guids) = winusb_guids {
// We add MSOS headers so that the device automatically gets assigned the WinUSB driver on Windows. // Here we give users the opportunity to add their own function level MSOS headers for instance.
// Otherwise users need to do this manually using a tool like Zadig. // This is useful when DFU functionality is part of a composite USB device.
// func_modifier(&mut func);
// Adding them here on the function level appears to only be needed for compositive devices.
// In addition to being on the function level, they should also be added to the device level.
//
func.msos_feature(msos::CompatibleIdFeatureDescriptor::new("WINUSB", ""));
func.msos_feature(msos::RegistryPropertyFeatureDescriptor::new(
"DeviceInterfaceGUIDs",
msos::PropertyData::RegMultiSz(winusb_guids),
));
}
let mut iface = func.interface(); let mut iface = func.interface();
let mut alt = iface.alt_setting(USB_CLASS_APPN_SPEC, APPN_SPEC_SUBCLASS_DFU, DFU_PROTOCOL_DFU, None); let mut alt = iface.alt_setting(USB_CLASS_APPN_SPEC, APPN_SPEC_SUBCLASS_DFU, DFU_PROTOCOL_DFU, None);

View File

@ -70,11 +70,15 @@ async fn main(_spawner: Spawner) {
msos::PropertyData::RegMultiSz(DEVICE_INTERFACE_GUIDS), msos::PropertyData::RegMultiSz(DEVICE_INTERFACE_GUIDS),
)); ));
// For non-composite devices: usb_dfu(&mut builder, &mut state, Duration::from_millis(2500), |func| {
usb_dfu(&mut builder, &mut state, Duration::from_millis(2500), None); // You likely don't have to add these function level headers if your USB device is not composite
// (i.e. if your device does not expose another interface in addition to DFU)
// Or for composite devices: func.msos_feature(msos::CompatibleIdFeatureDescriptor::new("WINUSB", ""));
// usb_dfu(&mut builder, &mut state, Duration::from_millis(2500), Some(DEVICE_INTERFACE_GUIDS)); func.msos_feature(msos::RegistryPropertyFeatureDescriptor::new(
"DeviceInterfaceGUIDs",
msos::PropertyData::RegMultiSz(DEVICE_INTERFACE_GUIDS),
));
});
let mut dev = builder.build(); let mut dev = builder.build();
dev.run().await dev.run().await

View File

@ -78,11 +78,15 @@ fn main() -> ! {
msos::PropertyData::RegMultiSz(DEVICE_INTERFACE_GUIDS), msos::PropertyData::RegMultiSz(DEVICE_INTERFACE_GUIDS),
)); ));
// For non-composite devices: usb_dfu::<_, _, _, _, 4096>(&mut builder, &mut state, |func| {
usb_dfu::<_, _, _, _, 4096>(&mut builder, &mut state, None); // You likely don't have to add these function level headers if your USB device is not composite
// (i.e. if your device does not expose another interface in addition to DFU)
// Or for composite devices: func.msos_feature(msos::CompatibleIdFeatureDescriptor::new("WINUSB", ""));
// usb_dfu::<_, _, _, _, 4096>(&mut builder, &mut state, Some(DEVICE_INTERFACE_GUIDS)); func.msos_feature(msos::RegistryPropertyFeatureDescriptor::new(
"DeviceInterfaceGUIDs",
msos::PropertyData::RegMultiSz(DEVICE_INTERFACE_GUIDS),
));
});
let mut dev = builder.build(); let mut dev = builder.build();
embassy_futures::block_on(dev.run()); embassy_futures::block_on(dev.run());