aboutsummaryrefslogtreecommitdiff
path: root/embassy-usb-dfu/src
diff options
context:
space:
mode:
Diffstat (limited to 'embassy-usb-dfu/src')
-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
3 files changed, 14 insertions, 18 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}