This commit makes it possible to provide function level msos GUIDs to usb_dfu. This helps to ensure that composite DFU devices automatically get assigned the WinUSB driver on Windows.
114 lines
4.1 KiB
Rust
114 lines
4.1 KiB
Rust
#![no_std]
|
|
#![no_main]
|
|
|
|
use core::cell::RefCell;
|
|
|
|
use cortex_m_rt::{entry, exception};
|
|
#[cfg(feature = "defmt")]
|
|
use defmt_rtt as _;
|
|
use embassy_boot_stm32::*;
|
|
use embassy_stm32::flash::{Flash, BANK1_REGION, WRITE_SIZE};
|
|
use embassy_stm32::rcc::WPAN_DEFAULT;
|
|
use embassy_stm32::usb::Driver;
|
|
use embassy_stm32::{bind_interrupts, peripherals, usb};
|
|
use embassy_sync::blocking_mutex::Mutex;
|
|
use embassy_usb::{msos, Builder};
|
|
use embassy_usb_dfu::consts::DfuAttributes;
|
|
use embassy_usb_dfu::{usb_dfu, Control, ResetImmediate};
|
|
|
|
bind_interrupts!(struct Irqs {
|
|
USB_LP => usb::InterruptHandler<peripherals::USB>;
|
|
});
|
|
|
|
// This is a randomly generated GUID to allow clients on Windows to find our device
|
|
const DEVICE_INTERFACE_GUIDS: &[&str] = &["{EAA9A5DC-30BA-44BC-9232-606CDC875321}"];
|
|
|
|
#[entry]
|
|
fn main() -> ! {
|
|
let mut config = embassy_stm32::Config::default();
|
|
config.rcc = WPAN_DEFAULT;
|
|
let p = embassy_stm32::init(config);
|
|
|
|
// Prevent a hard fault when accessing flash 'too early' after boot.
|
|
#[cfg(feature = "defmt")]
|
|
for _ in 0..10000000 {
|
|
cortex_m::asm::nop();
|
|
}
|
|
|
|
let layout = Flash::new_blocking(p.FLASH).into_blocking_regions();
|
|
let flash = Mutex::new(RefCell::new(layout.bank1_region));
|
|
|
|
let config = BootLoaderConfig::from_linkerfile_blocking(&flash, &flash, &flash);
|
|
let active_offset = config.active.offset();
|
|
let bl = BootLoader::prepare::<_, _, _, 2048>(config);
|
|
if bl.state == State::DfuDetach {
|
|
let driver = Driver::new(p.USB, Irqs, p.PA12, p.PA11);
|
|
let mut config = embassy_usb::Config::new(0xc0de, 0xcafe);
|
|
config.manufacturer = Some("Embassy");
|
|
config.product = Some("USB-DFU Bootloader example");
|
|
config.serial_number = Some("1235678");
|
|
|
|
let fw_config = FirmwareUpdaterConfig::from_linkerfile_blocking(&flash, &flash);
|
|
let mut buffer = AlignedBuffer([0; WRITE_SIZE]);
|
|
let updater = BlockingFirmwareUpdater::new(fw_config, &mut buffer.0[..]);
|
|
|
|
let mut config_descriptor = [0; 256];
|
|
let mut bos_descriptor = [0; 256];
|
|
let mut control_buf = [0; 4096];
|
|
let mut state = Control::new(updater, DfuAttributes::CAN_DOWNLOAD, ResetImmediate);
|
|
let mut builder = Builder::new(
|
|
driver,
|
|
config,
|
|
&mut config_descriptor,
|
|
&mut bos_descriptor,
|
|
&mut [],
|
|
&mut control_buf,
|
|
);
|
|
|
|
// We add MSOS headers so that the device automatically gets assigned the WinUSB driver on Windows.
|
|
// Otherwise users need to do this manually using a tool like Zadig.
|
|
builder.msos_descriptor(msos::windows_version::WIN8_1, 2);
|
|
|
|
// In the case of non-composite devices, it seems that feature headers need to be on the device level.
|
|
// (As is implemented here)
|
|
//
|
|
// For composite devices however, they should be on the function level instead.
|
|
// (This is achieved by passing a GUID to the "usb_dfu" function)
|
|
builder.msos_feature(msos::CompatibleIdFeatureDescriptor::new("WINUSB", ""));
|
|
builder.msos_feature(msos::RegistryPropertyFeatureDescriptor::new(
|
|
"DeviceInterfaceGUIDs",
|
|
msos::PropertyData::RegMultiSz(DEVICE_INTERFACE_GUIDS),
|
|
));
|
|
|
|
// For non-composite devices:
|
|
usb_dfu::<_, _, _, _, 4096>(&mut builder, &mut state, None);
|
|
|
|
// Or for composite devices:
|
|
// usb_dfu::<_, _, _, _, 4096>(&mut builder, &mut state, Some(DEVICE_INTERFACE_GUIDS));
|
|
|
|
let mut dev = builder.build();
|
|
embassy_futures::block_on(dev.run());
|
|
}
|
|
|
|
unsafe { bl.load(BANK1_REGION.base + active_offset) }
|
|
}
|
|
|
|
#[no_mangle]
|
|
#[cfg_attr(target_os = "none", link_section = ".HardFault.user")]
|
|
unsafe extern "C" fn HardFault() {
|
|
cortex_m::peripheral::SCB::sys_reset();
|
|
}
|
|
|
|
#[exception]
|
|
unsafe fn DefaultHandler(_: i16) -> ! {
|
|
const SCB_ICSR: *const u32 = 0xE000_ED04 as *const u32;
|
|
let irqn = core::ptr::read_volatile(SCB_ICSR) as u8 as i16 - 16;
|
|
|
|
panic!("DefaultHandler #{:?}", irqn);
|
|
}
|
|
|
|
#[panic_handler]
|
|
fn panic(_info: &core::panic::PanicInfo) -> ! {
|
|
cortex_m::asm::udf();
|
|
}
|