aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--embassy-usb-dfu/src/application.rs10
-rw-r--r--embassy-usb-dfu/src/dfu.rs14
-rw-r--r--embassy-usb-dfu/src/lib.rs8
-rw-r--r--examples/boot/application/stm32wb-dfu/src/main.rs4
-rw-r--r--examples/boot/bootloader/stm32wb-dfu/src/main.rs4
5 files changed, 18 insertions, 22 deletions
diff --git a/embassy-usb-dfu/src/application.rs b/embassy-usb-dfu/src/application.rs
index e93c241ad..3c1e8b2cc 100644
--- a/embassy-usb-dfu/src/application.rs
+++ b/embassy-usb-dfu/src/application.rs
@@ -1,5 +1,3 @@
1use core::marker::PhantomData;
2
3use embassy_boot::BlockingFirmwareState; 1use embassy_boot::BlockingFirmwareState;
4use embassy_time::{Duration, Instant}; 2use embassy_time::{Duration, Instant};
5use embassy_usb::control::{InResponse, OutResponse, Recipient, RequestType}; 3use embassy_usb::control::{InResponse, OutResponse, Recipient, RequestType};
@@ -36,19 +34,19 @@ pub struct Control<MARK: DfuMarker, RST: Reset> {
36 state: State, 34 state: State,
37 timeout: Option<Duration>, 35 timeout: Option<Duration>,
38 detach_start: Option<Instant>, 36 detach_start: Option<Instant>,
39 _rst: PhantomData<RST>, 37 reset: RST,
40} 38}
41 39
42impl<MARK: DfuMarker, RST: Reset> Control<MARK, RST> { 40impl<MARK: DfuMarker, RST: Reset> Control<MARK, RST> {
43 /// Create a new DFU instance to expose a DFU interface. 41 /// Create a new DFU instance to expose a DFU interface.
44 pub fn new(dfu_marker: MARK, attrs: DfuAttributes) -> Self { 42 pub fn new(dfu_marker: MARK, attrs: DfuAttributes, reset: RST) -> Self {
45 Control { 43 Control {
46 dfu_marker, 44 dfu_marker,
47 attrs, 45 attrs,
48 state: State::AppIdle, 46 state: State::AppIdle,
49 detach_start: None, 47 detach_start: None,
50 timeout: None, 48 timeout: None,
51 _rst: PhantomData, 49 reset,
52 } 50 }
53 } 51 }
54} 52}
@@ -65,7 +63,7 @@ impl<MARK: DfuMarker, RST: Reset> Handler for Control<MARK, RST> {
65 ); 63 );
66 if delta < timeout { 64 if delta < timeout {
67 self.dfu_marker.mark_dfu(); 65 self.dfu_marker.mark_dfu();
68 RST::sys_reset() 66 self.reset.sys_reset()
69 } 67 }
70 } 68 }
71 } 69 }
diff --git a/embassy-usb-dfu/src/dfu.rs b/embassy-usb-dfu/src/dfu.rs
index c23286cf5..a98d6ab40 100644
--- a/embassy-usb-dfu/src/dfu.rs
+++ b/embassy-usb-dfu/src/dfu.rs
@@ -1,5 +1,3 @@
1use core::marker::PhantomData;
2
3use embassy_boot::{AlignedBuffer, BlockingFirmwareUpdater, FirmwareUpdaterError}; 1use embassy_boot::{AlignedBuffer, BlockingFirmwareUpdater, FirmwareUpdaterError};
4use embassy_usb::control::{InResponse, OutResponse, Recipient, RequestType}; 2use embassy_usb::control::{InResponse, OutResponse, Recipient, RequestType};
5use embassy_usb::driver::Driver; 3use embassy_usb::driver::Driver;
@@ -20,12 +18,12 @@ pub struct Control<'d, DFU: NorFlash, STATE: NorFlash, RST: Reset, const BLOCK_S
20 status: Status, 18 status: Status,
21 offset: usize, 19 offset: usize,
22 buf: AlignedBuffer<BLOCK_SIZE>, 20 buf: AlignedBuffer<BLOCK_SIZE>,
23 _rst: PhantomData<RST>, 21 reset: RST,
24} 22}
25 23
26impl<'d, DFU: NorFlash, STATE: NorFlash, RST: Reset, const BLOCK_SIZE: usize> Control<'d, DFU, STATE, RST, BLOCK_SIZE> { 24impl<'d, DFU: NorFlash, STATE: NorFlash, RST: Reset, const BLOCK_SIZE: usize> Control<'d, DFU, STATE, RST, BLOCK_SIZE> {
27 /// Create a new DFU instance to handle DFU transfers. 25 /// Create a new DFU instance to handle DFU transfers.
28 pub fn new(updater: BlockingFirmwareUpdater<'d, DFU, STATE>, attrs: DfuAttributes) -> Self { 26 pub fn new(updater: BlockingFirmwareUpdater<'d, DFU, STATE>, attrs: DfuAttributes, reset: RST) -> Self {
29 Self { 27 Self {
30 updater, 28 updater,
31 attrs, 29 attrs,
@@ -33,7 +31,7 @@ impl<'d, DFU: NorFlash, STATE: NorFlash, RST: Reset, const BLOCK_SIZE: usize> Co
33 status: Status::Ok, 31 status: Status::Ok,
34 offset: 0, 32 offset: 0,
35 buf: AlignedBuffer([0; BLOCK_SIZE]), 33 buf: AlignedBuffer([0; BLOCK_SIZE]),
36 _rst: PhantomData, 34 reset,
37 } 35 }
38 } 36 }
39 37
@@ -155,14 +153,14 @@ impl<'d, DFU: NorFlash, STATE: NorFlash, RST: Reset, const BLOCK_SIZE: usize> Ha
155 } 153 }
156 match Request::try_from(req.request) { 154 match Request::try_from(req.request) {
157 Ok(Request::GetStatus) => { 155 Ok(Request::GetStatus) => {
158 //TODO: Configurable poll timeout, ability to add string for Vendor error
159 buf[0..6].copy_from_slice(&[self.status as u8, 0x32, 0x00, 0x00, self.state as u8, 0x00]);
160 match self.state { 156 match self.state {
161 State::DlSync => self.state = State::Download, 157 State::DlSync => self.state = State::Download,
162 State::ManifestSync => RST::sys_reset(), 158 State::ManifestSync => self.reset.sys_reset(),
163 _ => {} 159 _ => {}
164 } 160 }
165 161
162 //TODO: Configurable poll timeout, ability to add string for Vendor error
163 buf[0..6].copy_from_slice(&[self.status as u8, 0x32, 0x00, 0x00, self.state as u8, 0x00]);
166 Some(InResponse::Accepted(&buf[0..6])) 164 Some(InResponse::Accepted(&buf[0..6]))
167 } 165 }
168 Ok(Request::GetState) => { 166 Ok(Request::GetState) => {
diff --git a/embassy-usb-dfu/src/lib.rs b/embassy-usb-dfu/src/lib.rs
index eaa4b6e33..54ffa7276 100644
--- a/embassy-usb-dfu/src/lib.rs
+++ b/embassy-usb-dfu/src/lib.rs
@@ -26,10 +26,10 @@ compile_error!("usb-dfu must be compiled with exactly one of `dfu`, or `applicat
26/// This crate exposes `ResetImmediate` when compiled with cortex-m or esp32c3 support, which immediately issues a 26/// This crate exposes `ResetImmediate` when compiled with cortex-m or esp32c3 support, which immediately issues a
27/// reset request without interfacing with any other peripherals. 27/// reset request without interfacing with any other peripherals.
28/// 28///
29/// If alternate behaviour is desired, a custom implementation of Reset can be provided as a type argument to the usb_dfu function. 29/// If alternate behaviour is desired, a custom implementation of Reset can be provided as an argument to the usb_dfu function.
30pub trait Reset { 30pub trait Reset {
31 /// Reset the device. 31 /// Reset the device.
32 fn sys_reset() -> !; 32 fn sys_reset(&self);
33} 33}
34 34
35/// Reset immediately. 35/// Reset immediately.
@@ -38,7 +38,7 @@ pub struct ResetImmediate;
38 38
39#[cfg(feature = "esp32c3-hal")] 39#[cfg(feature = "esp32c3-hal")]
40impl Reset for ResetImmediate { 40impl Reset for ResetImmediate {
41 fn sys_reset() -> ! { 41 fn sys_reset(&self) {
42 esp32c3_hal::reset::software_reset(); 42 esp32c3_hal::reset::software_reset();
43 loop {} 43 loop {}
44 } 44 }
@@ -50,7 +50,7 @@ pub struct ResetImmediate;
50 50
51#[cfg(feature = "cortex-m")] 51#[cfg(feature = "cortex-m")]
52impl Reset for ResetImmediate { 52impl Reset for ResetImmediate {
53 fn sys_reset() -> ! { 53 fn sys_reset(&self) {
54 cortex_m::peripheral::SCB::sys_reset() 54 cortex_m::peripheral::SCB::sys_reset()
55 } 55 }
56} 56}
diff --git a/examples/boot/application/stm32wb-dfu/src/main.rs b/examples/boot/application/stm32wb-dfu/src/main.rs
index 0ab99ff90..dda2b795b 100644
--- a/examples/boot/application/stm32wb-dfu/src/main.rs
+++ b/examples/boot/application/stm32wb-dfu/src/main.rs
@@ -44,7 +44,7 @@ async fn main(_spawner: Spawner) {
44 let mut config_descriptor = [0; 256]; 44 let mut config_descriptor = [0; 256];
45 let mut bos_descriptor = [0; 256]; 45 let mut bos_descriptor = [0; 256];
46 let mut control_buf = [0; 64]; 46 let mut control_buf = [0; 64];
47 let mut state = Control::new(firmware_state, DfuAttributes::CAN_DOWNLOAD); 47 let mut state = Control::new(firmware_state, DfuAttributes::CAN_DOWNLOAD, ResetImmediate);
48 let mut builder = Builder::new( 48 let mut builder = Builder::new(
49 driver, 49 driver,
50 config, 50 config,
@@ -54,7 +54,7 @@ async fn main(_spawner: Spawner) {
54 &mut control_buf, 54 &mut control_buf,
55 ); 55 );
56 56
57 usb_dfu::<_, _, ResetImmediate>(&mut builder, &mut state, Duration::from_millis(2500)); 57 usb_dfu(&mut builder, &mut state, Duration::from_millis(2500));
58 58
59 let mut dev = builder.build(); 59 let mut dev = builder.build();
60 dev.run().await 60 dev.run().await
diff --git a/examples/boot/bootloader/stm32wb-dfu/src/main.rs b/examples/boot/bootloader/stm32wb-dfu/src/main.rs
index b09d53cf0..28216806e 100644
--- a/examples/boot/bootloader/stm32wb-dfu/src/main.rs
+++ b/examples/boot/bootloader/stm32wb-dfu/src/main.rs
@@ -55,7 +55,7 @@ fn main() -> ! {
55 let mut config_descriptor = [0; 256]; 55 let mut config_descriptor = [0; 256];
56 let mut bos_descriptor = [0; 256]; 56 let mut bos_descriptor = [0; 256];
57 let mut control_buf = [0; 4096]; 57 let mut control_buf = [0; 4096];
58 let mut state = Control::new(updater, DfuAttributes::CAN_DOWNLOAD); 58 let mut state = Control::new(updater, DfuAttributes::CAN_DOWNLOAD, ResetImmediate);
59 let mut builder = Builder::new( 59 let mut builder = Builder::new(
60 driver, 60 driver,
61 config, 61 config,
@@ -77,7 +77,7 @@ fn main() -> ! {
77 msos::PropertyData::RegMultiSz(DEVICE_INTERFACE_GUIDS), 77 msos::PropertyData::RegMultiSz(DEVICE_INTERFACE_GUIDS),
78 )); 78 ));
79 79
80 usb_dfu::<_, _, _, ResetImmediate, 4096>(&mut builder, &mut state); 80 usb_dfu::<_, _, _, _, 4096>(&mut builder, &mut state);
81 81
82 let mut dev = builder.build(); 82 let mut dev = builder.build();
83 embassy_futures::block_on(dev.run()); 83 embassy_futures::block_on(dev.run());