diff options
| author | Kaitlyn Kenwell <[email protected]> | 2023-12-14 13:29:26 -0500 |
|---|---|---|
| committer | Kaitlyn Kenwell <[email protected]> | 2023-12-14 13:29:26 -0500 |
| commit | 9f9f6e75bb3ef6d285ebed88a20ab57fb55f3d07 (patch) | |
| tree | 2a40c219cee9f20cd3b4e3d2b0067b0ba89c4a63 /embassy-usb-dfu/src/bootloader.rs | |
| parent | cbc8ccc51e8e747fab51ac377225495cd24eb447 (diff) | |
Abstract chip reset logic, add Reset impls for cortex-m and esp32c3
Diffstat (limited to 'embassy-usb-dfu/src/bootloader.rs')
| -rw-r--r-- | embassy-usb-dfu/src/bootloader.rs | 19 |
1 files changed, 13 insertions, 6 deletions
diff --git a/embassy-usb-dfu/src/bootloader.rs b/embassy-usb-dfu/src/bootloader.rs index 99384d961..d41e6280d 100644 --- a/embassy-usb-dfu/src/bootloader.rs +++ b/embassy-usb-dfu/src/bootloader.rs | |||
| @@ -1,3 +1,5 @@ | |||
| 1 | use core::marker::PhantomData; | ||
| 2 | |||
| 1 | use embassy_boot::{AlignedBuffer, BlockingFirmwareUpdater}; | 3 | use embassy_boot::{AlignedBuffer, BlockingFirmwareUpdater}; |
| 2 | use embassy_usb::control::{InResponse, OutResponse, Recipient, RequestType}; | 4 | use embassy_usb::control::{InResponse, OutResponse, Recipient, RequestType}; |
| 3 | use embassy_usb::driver::Driver; | 5 | use embassy_usb::driver::Driver; |
| @@ -8,17 +10,19 @@ use crate::consts::{ | |||
| 8 | DfuAttributes, Request, State, Status, APPN_SPEC_SUBCLASS_DFU, DESC_DFU_FUNCTIONAL, DFU_PROTOCOL_DFU, | 10 | DfuAttributes, Request, State, Status, APPN_SPEC_SUBCLASS_DFU, DESC_DFU_FUNCTIONAL, DFU_PROTOCOL_DFU, |
| 9 | USB_CLASS_APPN_SPEC, | 11 | USB_CLASS_APPN_SPEC, |
| 10 | }; | 12 | }; |
| 13 | use crate::Reset; | ||
| 11 | 14 | ||
| 12 | /// Internal state for USB DFU | 15 | /// Internal state for USB DFU |
| 13 | pub struct Control<'d, DFU: NorFlash, STATE: NorFlash, const BLOCK_SIZE: usize> { | 16 | pub struct Control<'d, DFU: NorFlash, STATE: NorFlash, RST: Reset, const BLOCK_SIZE: usize> { |
| 14 | updater: BlockingFirmwareUpdater<'d, DFU, STATE>, | 17 | updater: BlockingFirmwareUpdater<'d, DFU, STATE>, |
| 15 | attrs: DfuAttributes, | 18 | attrs: DfuAttributes, |
| 16 | state: State, | 19 | state: State, |
| 17 | status: Status, | 20 | status: Status, |
| 18 | offset: usize, | 21 | offset: usize, |
| 22 | _rst: PhantomData<RST>, | ||
| 19 | } | 23 | } |
| 20 | 24 | ||
| 21 | impl<'d, DFU: NorFlash, STATE: NorFlash, const BLOCK_SIZE: usize> Control<'d, DFU, STATE, BLOCK_SIZE> { | 25 | impl<'d, DFU: NorFlash, STATE: NorFlash, RST: Reset, const BLOCK_SIZE: usize> Control<'d, DFU, STATE, RST, BLOCK_SIZE> { |
| 22 | pub fn new(updater: BlockingFirmwareUpdater<'d, DFU, STATE>, attrs: DfuAttributes) -> Self { | 26 | pub fn new(updater: BlockingFirmwareUpdater<'d, DFU, STATE>, attrs: DfuAttributes) -> Self { |
| 23 | Self { | 27 | Self { |
| 24 | updater, | 28 | updater, |
| @@ -26,6 +30,7 @@ impl<'d, DFU: NorFlash, STATE: NorFlash, const BLOCK_SIZE: usize> Control<'d, DF | |||
| 26 | state: State::DfuIdle, | 30 | state: State::DfuIdle, |
| 27 | status: Status::Ok, | 31 | status: Status::Ok, |
| 28 | offset: 0, | 32 | offset: 0, |
| 33 | _rst: PhantomData, | ||
| 29 | } | 34 | } |
| 30 | } | 35 | } |
| 31 | 36 | ||
| @@ -36,7 +41,9 @@ impl<'d, DFU: NorFlash, STATE: NorFlash, const BLOCK_SIZE: usize> Control<'d, DF | |||
| 36 | } | 41 | } |
| 37 | } | 42 | } |
| 38 | 43 | ||
| 39 | impl<'d, DFU: NorFlash, STATE: NorFlash, const BLOCK_SIZE: usize> Handler for Control<'d, DFU, STATE, BLOCK_SIZE> { | 44 | impl<'d, DFU: NorFlash, STATE: NorFlash, RST: Reset, const BLOCK_SIZE: usize> Handler |
| 45 | for Control<'d, DFU, STATE, RST, BLOCK_SIZE> | ||
| 46 | { | ||
| 40 | fn control_out( | 47 | fn control_out( |
| 41 | &mut self, | 48 | &mut self, |
| 42 | req: embassy_usb::control::Request, | 49 | req: embassy_usb::control::Request, |
| @@ -131,7 +138,7 @@ impl<'d, DFU: NorFlash, STATE: NorFlash, const BLOCK_SIZE: usize> Handler for Co | |||
| 131 | buf[0..6].copy_from_slice(&[self.status as u8, 0x32, 0x00, 0x00, self.state as u8, 0x00]); | 138 | buf[0..6].copy_from_slice(&[self.status as u8, 0x32, 0x00, 0x00, self.state as u8, 0x00]); |
| 132 | match self.state { | 139 | match self.state { |
| 133 | State::DlSync => self.state = State::Download, | 140 | State::DlSync => self.state = State::Download, |
| 134 | State::ManifestSync => cortex_m::peripheral::SCB::sys_reset(), | 141 | State::ManifestSync => RST::sys_reset(), |
| 135 | _ => {} | 142 | _ => {} |
| 136 | } | 143 | } |
| 137 | 144 | ||
| @@ -157,9 +164,9 @@ impl<'d, DFU: NorFlash, STATE: NorFlash, const BLOCK_SIZE: usize> Handler for Co | |||
| 157 | /// | 164 | /// |
| 158 | /// Once the host has initiated a DFU download operation, the chunks sent by the host will be written to the DFU partition. | 165 | /// Once the host has initiated a DFU download operation, the chunks sent by the host will be written to the DFU partition. |
| 159 | /// Once the final sync in the manifestation phase has been received, the handler will trigger a system reset to swap the new firmware. | 166 | /// Once the final sync in the manifestation phase has been received, the handler will trigger a system reset to swap the new firmware. |
| 160 | pub fn usb_dfu<'d, D: Driver<'d>, DFU: NorFlash, STATE: NorFlash, const BLOCK_SIZE: usize>( | 167 | pub fn usb_dfu<'d, D: Driver<'d>, DFU: NorFlash, STATE: NorFlash, RST: Reset, const BLOCK_SIZE: usize>( |
| 161 | builder: &mut Builder<'d, D>, | 168 | builder: &mut Builder<'d, D>, |
| 162 | handler: &'d mut Control<'d, DFU, STATE, BLOCK_SIZE>, | 169 | handler: &'d mut Control<'d, DFU, STATE, RST, BLOCK_SIZE>, |
| 163 | ) { | 170 | ) { |
| 164 | let mut func = builder.function(0x00, 0x00, 0x00); | 171 | let mut func = builder.function(0x00, 0x00, 0x00); |
| 165 | let mut iface = func.interface(); | 172 | let mut iface = func.interface(); |
