aboutsummaryrefslogtreecommitdiff
path: root/embassy-usb-dfu/src/bootloader.rs
diff options
context:
space:
mode:
authorKaitlyn Kenwell <[email protected]>2023-12-13 14:53:49 -0500
committerKaitlyn Kenwell <[email protected]>2023-12-13 14:53:49 -0500
commitc2942f2727739d8972ad211721b1bb1804fb7b4a (patch)
tree34ad607895f6cea10ee4f5437e4b8b9f42fabd31 /embassy-usb-dfu/src/bootloader.rs
parent2afec225e3eddb5738bbc995baf04e13dd1df9e7 (diff)
fmt
Diffstat (limited to 'embassy-usb-dfu/src/bootloader.rs')
-rw-r--r--embassy-usb-dfu/src/bootloader.rs56
1 files changed, 21 insertions, 35 deletions
diff --git a/embassy-usb-dfu/src/bootloader.rs b/embassy-usb-dfu/src/bootloader.rs
index 7bcb0b258..215058932 100644
--- a/embassy-usb-dfu/src/bootloader.rs
+++ b/embassy-usb-dfu/src/bootloader.rs
@@ -1,12 +1,13 @@
1use embassy_boot::BlockingFirmwareUpdater; 1use embassy_boot::BlockingFirmwareUpdater;
2use embassy_usb::{ 2use embassy_usb::control::{InResponse, OutResponse, Recipient, RequestType};
3 control::{InResponse, OutResponse, Recipient, RequestType}, 3use embassy_usb::driver::Driver;
4 driver::Driver, 4use embassy_usb::{Builder, Handler};
5 Builder, Handler, 5use embedded_storage::nor_flash::{NorFlash, NorFlashErrorKind};
6};
7use embedded_storage::nor_flash::{NorFlashErrorKind, NorFlash};
8 6
9use crate::consts::{DfuAttributes, Request, State, Status, USB_CLASS_APPN_SPEC, APPN_SPEC_SUBCLASS_DFU, DFU_PROTOCOL_DFU, DESC_DFU_FUNCTIONAL}; 7use crate::consts::{
8 DfuAttributes, Request, State, Status, APPN_SPEC_SUBCLASS_DFU, DESC_DFU_FUNCTIONAL, DFU_PROTOCOL_DFU,
9 USB_CLASS_APPN_SPEC,
10};
10 11
11/// Internal state for USB DFU 12/// Internal state for USB DFU
12pub struct Control<'d, DFU: NorFlash, STATE: NorFlash, const BLOCK_SIZE: usize> { 13pub struct Control<'d, DFU: NorFlash, STATE: NorFlash, const BLOCK_SIZE: usize> {
@@ -69,17 +70,11 @@ impl<'d, DFU: NorFlash, STATE: NorFlash, const BLOCK_SIZE: usize> Handler for Co
69 match e { 70 match e {
70 embassy_boot::FirmwareUpdaterError::Flash(e) => match e { 71 embassy_boot::FirmwareUpdaterError::Flash(e) => match e {
71 NorFlashErrorKind::NotAligned => self.status = Status::ErrWrite, 72 NorFlashErrorKind::NotAligned => self.status = Status::ErrWrite,
72 NorFlashErrorKind::OutOfBounds => { 73 NorFlashErrorKind::OutOfBounds => self.status = Status::ErrAddress,
73 self.status = Status::ErrAddress
74 }
75 _ => self.status = Status::ErrUnknown, 74 _ => self.status = Status::ErrUnknown,
76 }, 75 },
77 embassy_boot::FirmwareUpdaterError::Signature(_) => { 76 embassy_boot::FirmwareUpdaterError::Signature(_) => self.status = Status::ErrVerify,
78 self.status = Status::ErrVerify 77 embassy_boot::FirmwareUpdaterError::BadState => self.status = Status::ErrUnknown,
79 }
80 embassy_boot::FirmwareUpdaterError::BadState => {
81 self.status = Status::ErrUnknown
82 }
83 } 78 }
84 } 79 }
85 } 80 }
@@ -101,17 +96,11 @@ impl<'d, DFU: NorFlash, STATE: NorFlash, const BLOCK_SIZE: usize> Handler for Co
101 match e { 96 match e {
102 embassy_boot::FirmwareUpdaterError::Flash(e) => match e { 97 embassy_boot::FirmwareUpdaterError::Flash(e) => match e {
103 NorFlashErrorKind::NotAligned => self.status = Status::ErrWrite, 98 NorFlashErrorKind::NotAligned => self.status = Status::ErrWrite,
104 NorFlashErrorKind::OutOfBounds => { 99 NorFlashErrorKind::OutOfBounds => self.status = Status::ErrAddress,
105 self.status = Status::ErrAddress
106 }
107 _ => self.status = Status::ErrUnknown, 100 _ => self.status = Status::ErrUnknown,
108 }, 101 },
109 embassy_boot::FirmwareUpdaterError::Signature(_) => { 102 embassy_boot::FirmwareUpdaterError::Signature(_) => self.status = Status::ErrVerify,
110 self.status = Status::ErrVerify 103 embassy_boot::FirmwareUpdaterError::BadState => self.status = Status::ErrUnknown,
111 }
112 embassy_boot::FirmwareUpdaterError::BadState => {
113 self.status = Status::ErrUnknown
114 }
115 } 104 }
116 } 105 }
117 } 106 }
@@ -162,10 +151,10 @@ impl<'d, DFU: NorFlash, STATE: NorFlash, const BLOCK_SIZE: usize> Handler for Co
162} 151}
163 152
164/// An implementation of the USB DFU 1.1 protocol 153/// An implementation of the USB DFU 1.1 protocol
165/// 154///
166/// This function will add a DFU interface descriptor to the provided Builder, and register the provided Control as a handler for the USB device 155/// This function will add a DFU interface descriptor to the provided Builder, and register the provided Control as a handler for the USB device
167/// The handler is responsive to DFU GetState, GetStatus, Abort, and ClrStatus commands, as well as Download if configured by the user. 156/// The handler is responsive to DFU GetState, GetStatus, Abort, and ClrStatus commands, as well as Download if configured by the user.
168/// 157///
169/// Once the host has initiated a DFU download operation, the chunks sent by the host will be written to the DFU partition. 158/// Once the host has initiated a DFU download operation, the chunks sent by the host will be written to the DFU partition.
170/// Once the final sync in the manifestation phase has been received, the handler will trigger a system reset to swap the new firmware. 159/// Once the final sync in the manifestation phase has been received, the handler will trigger a system reset to swap the new firmware.
171pub fn usb_dfu<'d, D: Driver<'d>, DFU: NorFlash, STATE: NorFlash, const BLOCK_SIZE: usize>( 160pub fn usb_dfu<'d, D: Driver<'d>, DFU: NorFlash, STATE: NorFlash, const BLOCK_SIZE: usize>(
@@ -174,20 +163,17 @@ pub fn usb_dfu<'d, D: Driver<'d>, DFU: NorFlash, STATE: NorFlash, const BLOCK_SI
174) { 163) {
175 let mut func = builder.function(0x00, 0x00, 0x00); 164 let mut func = builder.function(0x00, 0x00, 0x00);
176 let mut iface = func.interface(); 165 let mut iface = func.interface();
177 let mut alt = iface.alt_setting( 166 let mut alt = iface.alt_setting(USB_CLASS_APPN_SPEC, APPN_SPEC_SUBCLASS_DFU, DFU_PROTOCOL_DFU, None);
178 USB_CLASS_APPN_SPEC,
179 APPN_SPEC_SUBCLASS_DFU,
180 DFU_PROTOCOL_DFU,
181 None,
182 );
183 alt.descriptor( 167 alt.descriptor(
184 DESC_DFU_FUNCTIONAL, 168 DESC_DFU_FUNCTIONAL,
185 &[ 169 &[
186 handler.attrs.bits(), 170 handler.attrs.bits(),
187 0xc4, 0x09, // 2500ms timeout, doesn't affect operation as DETACH not necessary in bootloader code 171 0xc4,
172 0x09, // 2500ms timeout, doesn't affect operation as DETACH not necessary in bootloader code
188 (BLOCK_SIZE & 0xff) as u8, 173 (BLOCK_SIZE & 0xff) as u8,
189 ((BLOCK_SIZE & 0xff00) >> 8) as u8, 174 ((BLOCK_SIZE & 0xff00) >> 8) as u8,
190 0x10, 0x01, // DFU 1.1 175 0x10,
176 0x01, // DFU 1.1
191 ], 177 ],
192 ); 178 );
193 179