aboutsummaryrefslogtreecommitdiff
path: root/embassy-usb-dfu/src/lib.rs
blob: eaa4b6e33346c4f436b2bf7f8241d319a75c82d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#![no_std]
#![doc = include_str!("../README.md")]
#![warn(missing_docs)]
mod fmt;

pub mod consts;

#[cfg(feature = "dfu")]
mod dfu;
#[cfg(feature = "dfu")]
pub use self::dfu::*;

#[cfg(feature = "application")]
mod application;
#[cfg(feature = "application")]
pub use self::application::*;

#[cfg(any(
    all(feature = "dfu", feature = "application"),
    not(any(feature = "dfu", feature = "application"))
))]
compile_error!("usb-dfu must be compiled with exactly one of `dfu`, or `application` features");

/// Provides a platform-agnostic interface for initiating a system reset.
///
/// This crate exposes `ResetImmediate` when compiled with cortex-m or esp32c3 support, which immediately issues a
/// reset request without interfacing with any other peripherals.
///
/// If alternate behaviour is desired, a custom implementation of Reset can be provided as a type argument to the usb_dfu function.
pub trait Reset {
    /// Reset the device.
    fn sys_reset() -> !;
}

/// Reset immediately.
#[cfg(feature = "esp32c3-hal")]
pub struct ResetImmediate;

#[cfg(feature = "esp32c3-hal")]
impl Reset for ResetImmediate {
    fn sys_reset() -> ! {
        esp32c3_hal::reset::software_reset();
        loop {}
    }
}

/// Reset immediately.
#[cfg(feature = "cortex-m")]
pub struct ResetImmediate;

#[cfg(feature = "cortex-m")]
impl Reset for ResetImmediate {
    fn sys_reset() -> ! {
        cortex_m::peripheral::SCB::sys_reset()
    }
}