use bitflags to decode wakeup cause (#473)

* get_wakeup_cause: use bitflags instead of cast, &

* get_wakeup_cause: bitflags for other cpus
This commit is contained in:
liebman 2023-04-11 03:10:17 -07:00 committed by GitHub
parent 696b21bd92
commit ba078ef547
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 50 deletions

View File

@ -12,6 +12,7 @@ repository = "https://github.com/esp-rs/esp-hal"
license = "MIT OR Apache-2.0" license = "MIT OR Apache-2.0"
[dependencies] [dependencies]
bitflags = "2.1.0"
cfg-if = "1.0.0" cfg-if = "1.0.0"
critical-section = "1.1.1" critical-section = "1.1.1"
embedded-can = { version = "0.4.1", optional = true } embedded-can = { version = "0.4.1", optional = true }

View File

@ -30,39 +30,41 @@ pub enum SleepSource {
BT, BT,
} }
#[allow(unused)] bitflags::bitflags! {
pub(crate) enum WakeupReason { #[allow(unused)]
NoSleep = 0, pub(crate) struct WakeupReason: u32 {
#[cfg(pm_support_ext0_wakeup)] const NoSleep = 0;
/// EXT0 GPIO wakeup #[cfg(pm_support_ext0_wakeup)]
ExtEvent0Trig = 1 << 0, /// EXT0 GPIO wakeup
#[cfg(pm_support_ext1_wakeup)] const ExtEvent0Trig = 1 << 0;
/// EXT1 GPIO wakeup #[cfg(pm_support_ext1_wakeup)]
ExtEvent1Trig = 1 << 1, /// EXT1 GPIO wakeup
/// GPIO wakeup (light sleep only) const ExtEvent1Trig = 1 << 1;
GpioTrigEn = 1 << 2, /// GPIO wakeup (light sleep only)
/// Timer wakeup const GpioTrigEn = 1 << 2;
TimerTrigEn = 1 << 3, /// Timer wakeup
#[cfg(pm_support_wifi_wakeup)] const TimerTrigEn = 1 << 3;
/// MAC wakeup (light sleep only) #[cfg(pm_support_wifi_wakeup)]
WifiTrigEn = 1 << 5, /// MAC wakeup (light sleep only)
/// UART0 wakeup (light sleep only) const WifiTrigEn = 1 << 5;
Uart0TrigEn = 1 << 6, /// UART0 wakeup (light sleep only)
/// UART1 wakeup (light sleep only) const Uart0TrigEn = 1 << 6;
Uart1TrigEn = 1 << 7, /// UART1 wakeup (light sleep only)
#[cfg(pm_support_touch_sensor_wakeup)] const Uart1TrigEn = 1 << 7;
/// Touch wakeup #[cfg(pm_support_touch_sensor_wakeup)]
TouchTrigEn = 1 << 8, /// Touch wakeup
#[cfg(ulp_supported)] const TouchTrigEn = 1 << 8;
/// ULP wakeup #[cfg(ulp_supported)]
UlpTrigEn = 1 << 9, /// ULP wakeup
#[cfg(pm_support_bt_wakeup)] const UlpTrigEn = 1 << 9;
/// BT wakeup (light sleep only) #[cfg(pm_support_bt_wakeup)]
BtTrigEn = 1 << 10, /// BT wakeup (light sleep only)
#[cfg(riscv_coproc_supported)] const BtTrigEn = 1 << 10;
CocpuTrigEn = 1 << 11, #[cfg(riscv_coproc_supported)]
#[cfg(riscv_coproc_supported)] const CocpuTrigEn = 1 << 11;
CocpuTrapTrigEn = 1 << 13, #[cfg(riscv_coproc_supported)]
const CocpuTrapTrigEn = 1 << 13;
}
} }
pub fn software_reset() { pub fn software_reset() {

View File

@ -760,68 +760,69 @@ pub fn get_wakeup_cause() -> SleepSource {
} }
#[cfg(esp32c6)] #[cfg(esp32c6)]
let wakeup_cause = unsafe { let wakeup_cause = WakeupReason::from_bits_retain(unsafe {
(&*crate::peripherals::PMU::PTR) (&*crate::peripherals::PMU::PTR)
.slp_wakeup_status0 .slp_wakeup_status0
.read() .read()
.wakeup_cause() .wakeup_cause()
.bits() .bits()
}; });
#[cfg(not(any(esp32, esp32c6)))] #[cfg(not(any(esp32, esp32c6)))]
let wakeup_cause = unsafe { let wakeup_cause = WakeupReason::from_bits_retain(unsafe {
(&*RTC_CNTL::PTR) (&*RTC_CNTL::PTR)
.slp_wakeup_cause .slp_wakeup_cause
.read() .read()
.wakeup_cause() .wakeup_cause()
.bits() .bits()
}; });
#[cfg(esp32)] #[cfg(esp32)]
let wakeup_cause = let wakeup_cause = WakeupReason::from_bits_retain(unsafe {
unsafe { (&*RTC_CNTL::PTR).wakeup_state.read().wakeup_cause().bits() as u32 }; (&*RTC_CNTL::PTR).wakeup_state.read().wakeup_cause().bits() as u32
});
if (wakeup_cause & WakeupReason::TimerTrigEn as u32) != 0 { if wakeup_cause.contains(WakeupReason::TimerTrigEn) {
return SleepSource::Timer; return SleepSource::Timer;
} }
if (wakeup_cause & WakeupReason::GpioTrigEn as u32) != 0 { if wakeup_cause.contains(WakeupReason::GpioTrigEn) {
return SleepSource::Gpio; return SleepSource::Gpio;
} }
if (wakeup_cause & (WakeupReason::Uart0TrigEn as u32 | WakeupReason::Uart1TrigEn as u32)) != 0 { if wakeup_cause.intersects(WakeupReason::Uart0TrigEn | WakeupReason::Uart1TrigEn) {
return SleepSource::Uart; return SleepSource::Uart;
} }
#[cfg(pm_support_ext0_wakeup)] #[cfg(pm_support_ext0_wakeup)]
if (wakeup_cause & WakeupReason::ExtEvent0Trig as u32) != 0 { if wakeup_cause.contains(WakeupReason::ExtEvent0Trig) {
return SleepSource::Ext0; return SleepSource::Ext0;
} }
#[cfg(pm_support_ext1_wakeup)] #[cfg(pm_support_ext1_wakeup)]
if (wakeup_cause & WakeupReason::ExtEvent1Trig as u32) != 0 { if wakeup_cause.contains(WakeupReason::ExtEvent1Trig) {
return SleepSource::Ext1; return SleepSource::Ext1;
} }
#[cfg(pm_support_touch_sensor_wakeup)] #[cfg(pm_support_touch_sensor_wakeup)]
if (wakeup_cause & WakeupReason::TouchTrigEn as u32) != 0 { if wakeup_cause.contains(WakeupReason::TouchTrigEn) {
return SleepSource::TouchPad; return SleepSource::TouchPad;
} }
#[cfg(ulp_supported)] #[cfg(ulp_supported)]
if (wakeup_cause & WakeupReason::UlpTrigEn as u32) != 0 { if wakeup_cause.contains(WakeupReason::UlpTrigEn) {
return SleepSource::Ulp; return SleepSource::Ulp;
} }
#[cfg(pm_support_wifi_wakeup)] #[cfg(pm_support_wifi_wakeup)]
if (wakeup_cause & WakeupReason::WifiTrigEn as u32) != 0 { if wakeup_cause.contains(WakeupReason::WifiTrigEn) {
return SleepSource::Wifi; return SleepSource::Wifi;
} }
#[cfg(pm_support_bt_wakeup)] #[cfg(pm_support_bt_wakeup)]
if (wakeup_cause & WakeupReason::BtTrigEn as u32) != 0 { if wakeup_cause.contains(WakeupReason::BtTrigEn) {
return SleepSource::BT; return SleepSource::BT;
} }
#[cfg(riscv_coproc_supported)] #[cfg(riscv_coproc_supported)]
if (wakeup_cause & WakeupReason::CocpuTrigEn as u32) != 0 { if wakeup_cause.contains(WakeupReason::CocpuTrigEn) {
return SleepSource::Ulp; return SleepSource::Ulp;
} else if (wakeup_cause & WakeupReason::CocpuTrapTrigEn as u32) != 0 { } else if wakeup_cause.contains(WakeupReason::CocpuTrapTrigEn) {
return SleepSource::CocpuTrapTrig; return SleepSource::CocpuTrapTrig;
} }