aboutsummaryrefslogtreecommitdiff
path: root/examples/boot/bootloader/stm32wba-dfu/src
diff options
context:
space:
mode:
Diffstat (limited to 'examples/boot/bootloader/stm32wba-dfu/src')
-rw-r--r--examples/boot/bootloader/stm32wba-dfu/src/main.rs158
1 files changed, 158 insertions, 0 deletions
diff --git a/examples/boot/bootloader/stm32wba-dfu/src/main.rs b/examples/boot/bootloader/stm32wba-dfu/src/main.rs
new file mode 100644
index 000000000..75d8d4199
--- /dev/null
+++ b/examples/boot/bootloader/stm32wba-dfu/src/main.rs
@@ -0,0 +1,158 @@
1#![no_std]
2#![no_main]
3
4use core::cell::RefCell;
5
6use cortex_m_rt::{entry, exception};
7#[cfg(feature = "defmt")]
8use defmt_rtt as _;
9use embassy_boot_stm32::*;
10use embassy_stm32::flash::{Flash, BANK1_REGION, WRITE_SIZE};
11use embassy_stm32::usb::Driver;
12use embassy_stm32::{bind_interrupts, peripherals, usb, Config};
13use embassy_sync::blocking_mutex::Mutex;
14use embassy_usb::{msos, Builder};
15use embassy_usb_dfu::consts::DfuAttributes;
16use embassy_usb_dfu::{usb_dfu, Control, ResetImmediate};
17
18bind_interrupts!(struct Irqs {
19 USB_OTG_HS => usb::InterruptHandler<peripherals::USB_OTG_HS>;
20});
21
22// This is a randomly generated GUID to allow clients on Windows to find your device.
23//
24// N.B. update to a custom GUID for your own device!
25const DEVICE_INTERFACE_GUIDS: &[&str] = &["{EAA9A5DC-30BA-44BC-9232-606CDC875321}"];
26
27// This is a randomly generated example key.
28//
29// N.B. Please replace with your own!
30#[cfg(feature = "verify")]
31static PUBLIC_SIGNING_KEY: &[u8; 32] = include_bytes!("../secrets/key.pub.short");
32
33#[entry]
34fn main() -> ! {
35 let mut config = Config::default();
36
37 {
38 use embassy_stm32::rcc::*;
39 config.rcc.pll1 = Some(Pll {
40 source: PllSource::HSI,
41 prediv: PllPreDiv::DIV1, // PLLM = 1 → HSI / 1 = 16 MHz
42 mul: PllMul::MUL30, // PLLN = 30 → 16 MHz * 30 = 480 MHz VCO
43 divr: Some(PllDiv::DIV5), // PLLR = 5 → 96 MHz (Sysclk)
44 divq: Some(PllDiv::DIV10), // PLLQ = 10 → 48 MHz
45 divp: Some(PllDiv::DIV30), // PLLP = 30 → 16 MHz (USB_OTG_HS)
46 frac: Some(0), // Fractional part (disabled)
47 });
48
49 config.rcc.ahb_pre = AHBPrescaler::DIV1;
50 config.rcc.apb1_pre = APBPrescaler::DIV1;
51 config.rcc.apb2_pre = APBPrescaler::DIV1;
52 config.rcc.apb7_pre = APBPrescaler::DIV1;
53 config.rcc.ahb5_pre = AHB5Prescaler::DIV4;
54
55 config.rcc.voltage_scale = VoltageScale::RANGE1;
56 config.rcc.mux.otghssel = mux::Otghssel::PLL1_P;
57 config.rcc.sys = Sysclk::PLL1_R;
58 }
59
60 let p = embassy_stm32::init(config);
61
62 // Prevent a hard fault when accessing flash 'too early' after boot.
63 #[cfg(feature = "defmt")]
64 for _ in 0..10000000 {
65 cortex_m::asm::nop();
66 }
67
68 let layout = Flash::new_blocking(p.FLASH).into_blocking_regions();
69 let flash = Mutex::new(RefCell::new(layout.bank1_region));
70
71 let config = BootLoaderConfig::from_linkerfile_blocking(&flash, &flash, &flash);
72 let active_offset = config.active.offset();
73 let bl = BootLoader::prepare::<_, _, _, 2048>(config);
74
75 // Create the driver, from the HAL.
76 let mut ep_out_buffer = [0u8; 256];
77 let mut config = embassy_stm32::usb::Config::default();
78
79 config.vbus_detection = false;
80
81 if bl.state == State::DfuDetach {
82 let driver = Driver::new_hs(p.USB_OTG_HS, Irqs, p.PD6, p.PD7, &mut ep_out_buffer, config);
83 let mut config = embassy_usb::Config::new(0xc0de, 0xcafe);
84 config.manufacturer = Some("Embassy");
85 config.product = Some("USB-DFU Bootloader example");
86 config.serial_number = Some("1235678");
87
88 let fw_config = FirmwareUpdaterConfig::from_linkerfile_blocking(&flash, &flash);
89 let mut buffer = AlignedBuffer([0; WRITE_SIZE]);
90 let updater = BlockingFirmwareUpdater::new(fw_config, &mut buffer.0[..]);
91
92 let mut config_descriptor = [0; 256];
93 let mut bos_descriptor = [0; 256];
94 let mut control_buf = [0; 4096];
95
96 #[cfg(not(feature = "verify"))]
97 let mut state = Control::new(updater, DfuAttributes::CAN_DOWNLOAD, ResetImmediate);
98
99 #[cfg(feature = "verify")]
100 let mut state = Control::new(updater, DfuAttributes::CAN_DOWNLOAD, ResetImmediate, PUBLIC_SIGNING_KEY);
101
102 let mut builder = Builder::new(
103 driver,
104 config,
105 &mut config_descriptor,
106 &mut bos_descriptor,
107 &mut [],
108 &mut control_buf,
109 );
110
111 // We add MSOS headers so that the device automatically gets assigned the WinUSB driver on Windows.
112 // Otherwise users need to do this manually using a tool like Zadig.
113 //
114 // It seems these always need to be at added at the device level for this to work and for
115 // composite devices they also need to be added on the function level (as shown later).
116
117 builder.msos_descriptor(msos::windows_version::WIN8_1, 2);
118 builder.msos_feature(msos::CompatibleIdFeatureDescriptor::new("WINUSB", ""));
119 builder.msos_feature(msos::RegistryPropertyFeatureDescriptor::new(
120 "DeviceInterfaceGUIDs",
121 msos::PropertyData::RegMultiSz(DEVICE_INTERFACE_GUIDS),
122 ));
123
124 usb_dfu::<_, _, _, _, 4096>(&mut builder, &mut state, |func| {
125 // You likely don't have to add these function level headers if your USB device is not composite
126 // (i.e. if your device does not expose another interface in addition to DFU)
127 func.msos_feature(msos::CompatibleIdFeatureDescriptor::new("WINUSB", ""));
128 func.msos_feature(msos::RegistryPropertyFeatureDescriptor::new(
129 "DeviceInterfaceGUIDs",
130 msos::PropertyData::RegMultiSz(DEVICE_INTERFACE_GUIDS),
131 ));
132 });
133
134 let mut dev = builder.build();
135 embassy_futures::block_on(dev.run());
136 }
137
138 unsafe { bl.load(BANK1_REGION.base + active_offset) }
139}
140
141#[no_mangle]
142#[cfg_attr(target_os = "none", link_section = ".HardFault.user")]
143unsafe extern "C" fn HardFault() {
144 cortex_m::peripheral::SCB::sys_reset();
145}
146
147#[exception]
148unsafe fn DefaultHandler(_: i16) -> ! {
149 const SCB_ICSR: *const u32 = 0xE000_ED04 as *const u32;
150 let irqn = core::ptr::read_volatile(SCB_ICSR) as u8 as i16 - 16;
151
152 panic!("DefaultHandler #{:?}", irqn);
153}
154
155#[panic_handler]
156fn panic(_info: &core::panic::PanicInfo) -> ! {
157 cortex_m::asm::udf();
158}