aboutsummaryrefslogtreecommitdiff
path: root/examples/boot/application/stm32wba-dfu/src/main.rs
diff options
context:
space:
mode:
authorGerzain Mata <[email protected]>2025-07-29 01:19:22 -0700
committerGerzain Mata <[email protected]>2025-07-29 08:11:49 -0700
commit1b4ea556c0e99622a18c48126cfc6899ffa102b1 (patch)
tree158edb71711a270b00e634bbab55194cf35c4935 /examples/boot/application/stm32wba-dfu/src/main.rs
parent7cf7f8b0ef344cdbef76703d4d700fbfddda23c9 (diff)
STM32WBA usb-dfu example
- Added sample application - Added sample bootloader Removed trace import
Diffstat (limited to 'examples/boot/application/stm32wba-dfu/src/main.rs')
-rw-r--r--examples/boot/application/stm32wba-dfu/src/main.rs114
1 files changed, 114 insertions, 0 deletions
diff --git a/examples/boot/application/stm32wba-dfu/src/main.rs b/examples/boot/application/stm32wba-dfu/src/main.rs
new file mode 100644
index 000000000..bf17a7150
--- /dev/null
+++ b/examples/boot/application/stm32wba-dfu/src/main.rs
@@ -0,0 +1,114 @@
1#![no_std]
2#![no_main]
3
4use core::cell::RefCell;
5
6#[cfg(feature = "defmt")]
7use defmt_rtt as _;
8use embassy_boot_stm32::{AlignedBuffer, BlockingFirmwareState, FirmwareUpdaterConfig};
9use embassy_executor::Spawner;
10use embassy_stm32::flash::{Flash, WRITE_SIZE};
11use embassy_stm32::usb::{self, Driver};
12use embassy_stm32::{bind_interrupts, peripherals};
13use embassy_sync::blocking_mutex::Mutex;
14use embassy_time::Duration;
15use embassy_usb::{msos, Builder};
16use embassy_usb_dfu::consts::DfuAttributes;
17use embassy_usb_dfu::{usb_dfu, Control, ResetImmediate};
18use panic_reset as _;
19
20bind_interrupts!(struct Irqs {
21 USB_OTG_HS => usb::InterruptHandler<peripherals::USB_OTG_HS>;
22});
23
24// This is a randomly generated GUID to allow clients on Windows to find your device.
25//
26// N.B. update to a custom GUID for your own device!
27const DEVICE_INTERFACE_GUIDS: &[&str] = &["{EAA9A5DC-30BA-44BC-9232-606CDC875321}"];
28
29#[embassy_executor::main]
30async fn main(_spawner: Spawner) {
31 let mut config = embassy_stm32::Config::default();
32
33 {
34 use embassy_stm32::rcc::*;
35 config.rcc.pll1 = Some(Pll {
36 source: PllSource::HSI,
37 prediv: PllPreDiv::DIV1, // PLLM = 1 → HSI / 1 = 16 MHz
38 mul: PllMul::MUL30, // PLLN = 30 → 16 MHz * 30 = 480 MHz VCO
39 divr: Some(PllDiv::DIV5), // PLLR = 5 → 96 MHz (Sysclk)
40 divq: Some(PllDiv::DIV10), // PLLQ = 10 → 48 MHz
41 divp: Some(PllDiv::DIV30), // PLLP = 30 → 16 MHz (USB_OTG_HS)
42 frac: Some(0), // Fractional part (disabled)
43 });
44
45 config.rcc.ahb_pre = AHBPrescaler::DIV1;
46 config.rcc.apb1_pre = APBPrescaler::DIV1;
47 config.rcc.apb2_pre = APBPrescaler::DIV1;
48 config.rcc.apb7_pre = APBPrescaler::DIV1;
49 config.rcc.ahb5_pre = AHB5Prescaler::DIV4;
50
51 config.rcc.voltage_scale = VoltageScale::RANGE1;
52 config.rcc.mux.otghssel = mux::Otghssel::PLL1_P;
53 config.rcc.sys = Sysclk::PLL1_R;
54 }
55
56 let p = embassy_stm32::init(config);
57 let flash = Flash::new_blocking(p.FLASH);
58 let flash = Mutex::new(RefCell::new(flash));
59
60 let config = FirmwareUpdaterConfig::from_linkerfile_blocking(&flash, &flash);
61 let mut magic = AlignedBuffer([0; WRITE_SIZE]);
62 let mut firmware_state = BlockingFirmwareState::from_config(config, &mut magic.0);
63 firmware_state.mark_booted().expect("Failed to mark booted");
64
65 // Create the driver, from the HAL.
66 let mut ep_out_buffer = [0u8; 256];
67 let mut config = embassy_stm32::usb::Config::default();
68 config.vbus_detection = false;
69
70 let driver = Driver::new_hs(p.USB_OTG_HS, Irqs, p.PD6, p.PD7, &mut ep_out_buffer, config);
71 let mut config = embassy_usb::Config::new(0xc0de, 0xcafe);
72 config.manufacturer = Some("Embassy");
73 config.product = Some("USB-DFU Runtime example");
74 config.serial_number = Some("1235678");
75
76 let mut config_descriptor = [0; 256];
77 let mut bos_descriptor = [0; 256];
78 let mut control_buf = [0; 64];
79 let mut state = Control::new(firmware_state, DfuAttributes::CAN_DOWNLOAD, ResetImmediate);
80 let mut builder = Builder::new(
81 driver,
82 config,
83 &mut config_descriptor,
84 &mut bos_descriptor,
85 &mut [],
86 &mut control_buf,
87 );
88
89 // We add MSOS headers so that the device automatically gets assigned the WinUSB driver on Windows.
90 // Otherwise users need to do this manually using a tool like Zadig.
91 //
92 // It seems these always need to be at added at the device level for this to work and for
93 // composite devices they also need to be added on the function level (as shown later).
94
95 builder.msos_descriptor(msos::windows_version::WIN8_1, 2);
96 builder.msos_feature(msos::CompatibleIdFeatureDescriptor::new("WINUSB", ""));
97 builder.msos_feature(msos::RegistryPropertyFeatureDescriptor::new(
98 "DeviceInterfaceGUIDs",
99 msos::PropertyData::RegMultiSz(DEVICE_INTERFACE_GUIDS),
100 ));
101
102 usb_dfu(&mut builder, &mut state, Duration::from_millis(1000), |func| {
103 // You likely don't have to add these function level headers if your USB device is not composite
104 // (i.e. if your device does not expose another interface in addition to DFU)
105 func.msos_feature(msos::CompatibleIdFeatureDescriptor::new("WINUSB", ""));
106 func.msos_feature(msos::RegistryPropertyFeatureDescriptor::new(
107 "DeviceInterfaceGUIDs",
108 msos::PropertyData::RegMultiSz(DEVICE_INTERFACE_GUIDS),
109 ));
110 });
111
112 let mut dev = builder.build();
113 dev.run().await
114}