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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//! Reset reasons
use esp_idf_sys::*;

/// Reset reasons
#[doc(alias = "esp_reset_reason_t")]
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum ResetReason {
    /// Software restart via `esp_restart()`
    Software,
    /// Reset by external pin
    ExternalPin,
    /// Reset due to other watchdogs
    Watchdog,
    /// Reset over SDIO
    Sdio,
    /// Software reset due to exception/panic
    Panic,
    /// Reset (software or hardware) due to interrupt watchdog
    InterruptWatchdog,
    /// Reset due to power-on event
    PowerOn,
    /// Reset reason can not be determined
    Unknown,
    /// Brownout reset (software or hardware)
    Brownout,
    /// Reset due to task watchdog
    TaskWatchdog,
    /// Reset after exiting deep sleep mode
    DeepSleep,
    /// Reset by USB peripheral (introduced in IDF v5.1.4)
    #[cfg(not(any(
        esp_idf_version_major = "4",
        all(esp_idf_version_major = "5", esp_idf_version_minor = "0"),
        esp_idf_version_full = "5.1.0",
        esp_idf_version_full = "5.1.1",
        esp_idf_version_full = "5.1.2",
        esp_idf_version_full = "5.1.3",
    )))]
    USBPeripheral,
    /// Reset by JTAG (introduced in IDF v5.1.4)
    #[cfg(not(any(
        esp_idf_version_major = "4",
        all(esp_idf_version_major = "5", esp_idf_version_minor = "0"),
        esp_idf_version_full = "5.1.0",
        esp_idf_version_full = "5.1.1",
        esp_idf_version_full = "5.1.2",
        esp_idf_version_full = "5.1.3",
    )))]
    JTAG,
    /// Reset due to efuse error (introduced in IDF v5.2.2)
    #[cfg(not(any(
        esp_idf_version_major = "4",
        all(esp_idf_version_major = "5", esp_idf_version_minor = "0"),
        all(esp_idf_version_major = "5", esp_idf_version_minor = "1"),
        esp_idf_version_full = "5.2.0",
        esp_idf_version_full = "5.2.1",
    )))]
    EfuseError,
    /// Reset due to power glitch detected (introduced in IDF v5.2.2)
    #[cfg(not(any(
        esp_idf_version_major = "4",
        all(esp_idf_version_major = "5", esp_idf_version_minor = "0"),
        all(esp_idf_version_major = "5", esp_idf_version_minor = "1"),
        esp_idf_version_full = "5.2.0",
        esp_idf_version_full = "5.2.1",
    )))]
    PowerGlitch,
    /// Reset due to CPU lock up (introduced in IDF v5.2.2)
    #[cfg(not(any(
        esp_idf_version_major = "4",
        all(esp_idf_version_major = "5", esp_idf_version_minor = "0"),
        all(esp_idf_version_major = "5", esp_idf_version_minor = "1"),
        esp_idf_version_full = "5.2.0",
        esp_idf_version_full = "5.2.1",
    )))]
    CPULockup,
}

impl From<esp_reset_reason_t> for ResetReason {
    #[allow(non_upper_case_globals)]
    fn from(value: esp_reset_reason_t) -> Self {
        match value {
            esp_reset_reason_t_ESP_RST_SW => Self::Software,
            esp_reset_reason_t_ESP_RST_EXT => Self::ExternalPin,
            esp_reset_reason_t_ESP_RST_WDT => Self::Watchdog,
            esp_reset_reason_t_ESP_RST_SDIO => Self::Sdio,
            esp_reset_reason_t_ESP_RST_PANIC => Self::Panic,
            esp_reset_reason_t_ESP_RST_INT_WDT => Self::InterruptWatchdog,
            esp_reset_reason_t_ESP_RST_POWERON => Self::PowerOn,
            esp_reset_reason_t_ESP_RST_UNKNOWN => Self::Unknown,
            esp_reset_reason_t_ESP_RST_BROWNOUT => Self::Brownout,
            esp_reset_reason_t_ESP_RST_TASK_WDT => Self::TaskWatchdog,
            esp_reset_reason_t_ESP_RST_DEEPSLEEP => Self::DeepSleep,
            #[cfg(not(any(
                esp_idf_version_major = "4",
                all(esp_idf_version_major = "5", esp_idf_version_minor = "0"),
                esp_idf_version_full = "5.1.0",
                esp_idf_version_full = "5.1.1",
                esp_idf_version_full = "5.1.2",
                esp_idf_version_full = "5.1.3",
            )))]
            esp_reset_reason_t_ESP_RST_USB => Self::USBPeripheral,
            #[cfg(not(any(
                esp_idf_version_major = "4",
                all(esp_idf_version_major = "5", esp_idf_version_minor = "0"),
                esp_idf_version_full = "5.1.0",
                esp_idf_version_full = "5.1.1",
                esp_idf_version_full = "5.1.2",
                esp_idf_version_full = "5.1.3",
            )))]
            esp_reset_reason_t_ESP_RST_JTAG => Self::JTAG,
            #[cfg(not(any(
                esp_idf_version_major = "4",
                all(esp_idf_version_major = "5", esp_idf_version_minor = "0"),
                all(esp_idf_version_major = "5", esp_idf_version_minor = "1"),
                esp_idf_version_full = "5.2.0",
                esp_idf_version_full = "5.2.1",
            )))]
            esp_reset_reason_t_ESP_RST_EFUSE => Self::EfuseError,
            #[cfg(not(any(
                esp_idf_version_major = "4",
                all(esp_idf_version_major = "5", esp_idf_version_minor = "0"),
                all(esp_idf_version_major = "5", esp_idf_version_minor = "1"),
                esp_idf_version_full = "5.2.0",
                esp_idf_version_full = "5.2.1",
            )))]
            esp_reset_reason_t_ESP_RST_PG => Self::PowerGlitch,
            #[cfg(not(any(
                esp_idf_version_major = "4",
                all(esp_idf_version_major = "5", esp_idf_version_minor = "0"),
                all(esp_idf_version_major = "5", esp_idf_version_minor = "1"),
                esp_idf_version_full = "5.2.0",
                esp_idf_version_full = "5.2.1",
            )))]
            esp_reset_reason_t_ESP_RST_CPU => Self::CPULockup,
            _ => unreachable!(),
        }
    }
}

impl ResetReason {
    /// Get the reason for the last reset
    #[doc(alias = "esp_reset_reason")]
    pub fn get() -> Self {
        // SAFETY: `esp_reset_reason()` reads from static memory that is initialized when
        // constructors run and never modified after that point. We would only see
        // uninitialized/partially initialized data if we run before main() (and before
        // constructors running).
        let rr = unsafe { esp_reset_reason() };
        rr.into()
    }
}

/// Wakeup reasons
#[doc(alias = "esp_sleep_source_t")]
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum WakeupReason {
    Unknown,
    ULP,
    Button,
    Timer,
    Other(u32),
}

impl From<esp_sleep_source_t> for WakeupReason {
    #[allow(non_upper_case_globals)]
    fn from(value: esp_sleep_source_t) -> Self {
        match value {
            esp_sleep_source_t_ESP_SLEEP_WAKEUP_UNDEFINED => Self::Unknown,
            esp_sleep_source_t_ESP_SLEEP_WAKEUP_EXT0 => Self::Button,
            esp_sleep_source_t_ESP_SLEEP_WAKEUP_EXT1 => Self::Button,
            esp_sleep_source_t_ESP_SLEEP_WAKEUP_ULP => Self::ULP,
            esp_sleep_source_t_ESP_SLEEP_WAKEUP_COCPU => Self::ULP,
            esp_sleep_source_t_ESP_SLEEP_WAKEUP_TIMER => Self::Timer,
            other => Self::Other(other),
        }
    }
}

impl WakeupReason {
    /// Get the reason for the wakeup
    #[doc(alias = "esp_sleep_get_wakeup_cause")]
    pub fn get() -> Self {
        let wr = unsafe { esp_sleep_get_wakeup_cause() };
        wr.into()
    }
}

pub fn restart() -> ! {
    unsafe { esp_restart() }
}