Skip to content

Commit 5ba8b5e

Browse files
committed
feat(esp_hw_support): add new API to get all wakeup sources
1 parent 7e570d2 commit 5ba8b5e

File tree

2 files changed

+116
-13
lines changed

2 files changed

+116
-13
lines changed

components/esp_hw_support/include/esp_sleep.h

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -105,21 +105,23 @@ typedef enum {
105105
* @brief Sleep wakeup cause
106106
*/
107107
typedef enum {
108-
ESP_SLEEP_WAKEUP_UNDEFINED, //!< In case of deep sleep, reset was not caused by exit from deep sleep
109-
ESP_SLEEP_WAKEUP_ALL, //!< Not a wakeup cause, used to disable all wakeup sources with esp_sleep_disable_wakeup_source
110-
ESP_SLEEP_WAKEUP_EXT0, //!< Wakeup caused by external signal using RTC_IO
111-
ESP_SLEEP_WAKEUP_EXT1, //!< Wakeup caused by external signal using RTC_CNTL
112-
ESP_SLEEP_WAKEUP_TIMER, //!< Wakeup caused by timer
113-
ESP_SLEEP_WAKEUP_TOUCHPAD, //!< Wakeup caused by touchpad
114-
ESP_SLEEP_WAKEUP_ULP, //!< Wakeup caused by ULP program
115-
ESP_SLEEP_WAKEUP_GPIO, //!< Wakeup caused by GPIO (light sleep only on ESP32, S2 and S3)
116-
ESP_SLEEP_WAKEUP_UART, //!< Wakeup caused by UART (light sleep only)
108+
ESP_SLEEP_WAKEUP_UNDEFINED, //!< In case of deep sleep, reset was not caused by exit from deep sleep
109+
ESP_SLEEP_WAKEUP_ALL, //!< Not a wakeup cause, used to disable all wakeup sources with esp_sleep_disable_wakeup_source
110+
ESP_SLEEP_WAKEUP_EXT0, //!< Wakeup caused by external signal using RTC_IO
111+
ESP_SLEEP_WAKEUP_EXT1, //!< Wakeup caused by external signal using RTC_CNTL
112+
ESP_SLEEP_WAKEUP_TIMER, //!< Wakeup caused by timer
113+
ESP_SLEEP_WAKEUP_TOUCHPAD, //!< Wakeup caused by touchpad
114+
ESP_SLEEP_WAKEUP_ULP, //!< Wakeup caused by ULP program
115+
ESP_SLEEP_WAKEUP_GPIO, //!< Wakeup caused by GPIO (light sleep only on ESP32, S2 and S3)
116+
ESP_SLEEP_WAKEUP_UART, //!< Wakeup caused by UART0 (light sleep only)
117+
ESP_SLEEP_WAKEUP_UART1, //!< Wakeup caused by UART1 (light sleep only)
118+
ESP_SLEEP_WAKEUP_UART2, //!< Wakeup caused by UART2 (light sleep only)
117119
ESP_SLEEP_WAKEUP_WIFI, //!< Wakeup caused by WIFI (light sleep only)
118120
ESP_SLEEP_WAKEUP_COCPU, //!< Wakeup caused by COCPU int
119121
ESP_SLEEP_WAKEUP_COCPU_TRAP_TRIG, //!< Wakeup caused by COCPU crash
120-
ESP_SLEEP_WAKEUP_BT, //!< Wakeup caused by BT (light sleep only)
121-
ESP_SLEEP_WAKEUP_VAD, //!< Wakeup caused by VAD
122-
ESP_SLEEP_WAKEUP_VBAT_UNDER_VOLT, //!< Wakeup caused by VDD_BAT under voltage.
122+
ESP_SLEEP_WAKEUP_BT, //!< Wakeup caused by BT (light sleep only)
123+
ESP_SLEEP_WAKEUP_VAD, //!< Wakeup caused by VAD
124+
ESP_SLEEP_WAKEUP_VBAT_UNDER_VOLT, //!< Wakeup caused by VDD_BAT under voltage.
123125
} esp_sleep_source_t;
124126

125127
/**
@@ -691,10 +693,20 @@ void esp_deep_sleep_deregister_hook(esp_deep_sleep_cb_t old_dslp_cb);
691693
/**
692694
* @brief Get the wakeup source which caused wakeup from sleep
693695
*
696+
* @note !!! This API will only return one wakeup source. If multiple wakeup sources
697+
* wake up at the same time, the wakeup source information may be lost.
698+
*
694699
* @return cause of wake up from last sleep (deep sleep or light sleep)
695700
*/
696-
esp_sleep_wakeup_cause_t esp_sleep_get_wakeup_cause(void);
701+
esp_sleep_wakeup_cause_t esp_sleep_get_wakeup_cause(void)
702+
__attribute__((deprecated("use esp_sleep_get_wakeup_causes instead")));
697703

704+
/**
705+
* @brief Get all wakeup sources bitmap which caused wakeup from sleep.
706+
*
707+
* @return The bitmap of the wakeup sources of the last wakeup from sleep. (deep sleep or light sleep)
708+
*/
709+
uint32_t esp_sleep_get_wakeup_causes(void);
698710

699711
/**
700712
* @brief Default stub to run on wake from deep sleep.

components/esp_hw_support/sleep_modes.c

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2339,6 +2339,97 @@ esp_sleep_wakeup_cause_t esp_sleep_get_wakeup_cause(void)
23392339
}
23402340
}
23412341

2342+
uint32_t esp_sleep_get_wakeup_causes(void)
2343+
{
2344+
uint32_t wakeup_cause = 0;
2345+
2346+
if (esp_rom_get_reset_reason(0) != RESET_REASON_CORE_DEEP_SLEEP && !s_light_sleep_wakeup) {
2347+
wakeup_cause |= BIT(ESP_SLEEP_WAKEUP_UNDEFINED);
2348+
return wakeup_cause;
2349+
}
2350+
2351+
#if SOC_PMU_SUPPORTED
2352+
uint32_t wakeup_cause_raw = pmu_ll_hp_get_wakeup_cause(&PMU);
2353+
#else
2354+
uint32_t wakeup_cause_raw = rtc_cntl_ll_get_wakeup_cause();
2355+
#endif
2356+
2357+
if (wakeup_cause_raw & RTC_TIMER_TRIG_EN) {
2358+
wakeup_cause |= BIT(ESP_SLEEP_WAKEUP_TIMER);
2359+
}
2360+
if (wakeup_cause_raw & RTC_GPIO_TRIG_EN) {
2361+
wakeup_cause |= BIT(ESP_SLEEP_WAKEUP_GPIO);
2362+
}
2363+
if (wakeup_cause_raw & RTC_UART0_TRIG_EN) {
2364+
wakeup_cause |= BIT(ESP_SLEEP_WAKEUP_UART);
2365+
}
2366+
if (wakeup_cause_raw & RTC_UART1_TRIG_EN) {
2367+
wakeup_cause |= BIT(ESP_SLEEP_WAKEUP_UART1);
2368+
}
2369+
#if SOC_PMU_SUPPORTED && (SOC_UART_HP_NUM > 2)
2370+
if (wakeup_cause_raw & RTC_UART2_TRIG_EN) {
2371+
wakeup_cause |= BIT(ESP_SLEEP_WAKEUP_UART2);
2372+
}
2373+
#endif
2374+
#if SOC_PM_SUPPORT_EXT0_WAKEUP
2375+
if (wakeup_cause_raw & RTC_EXT0_TRIG_EN) {
2376+
wakeup_cause |= BIT(ESP_SLEEP_WAKEUP_EXT0);
2377+
}
2378+
#endif
2379+
#if SOC_PM_SUPPORT_EXT1_WAKEUP
2380+
if (wakeup_cause_raw & RTC_EXT1_TRIG_EN) {
2381+
wakeup_cause |= BIT(ESP_SLEEP_WAKEUP_EXT1);
2382+
}
2383+
#endif
2384+
#if SOC_PM_SUPPORT_TOUCH_SENSOR_WAKEUP
2385+
if (wakeup_cause_raw & RTC_TOUCH_TRIG_EN) {
2386+
wakeup_cause |= BIT(ESP_SLEEP_WAKEUP_TOUCHPAD);
2387+
}
2388+
#endif
2389+
#if SOC_ULP_FSM_SUPPORTED
2390+
if (wakeup_cause_raw & RTC_ULP_TRIG_EN) {
2391+
wakeup_cause |= BIT(ESP_SLEEP_WAKEUP_ULP);
2392+
}
2393+
#endif
2394+
#if SOC_PM_SUPPORT_WIFI_WAKEUP
2395+
if (wakeup_cause_raw & RTC_WIFI_TRIG_EN) {
2396+
wakeup_cause |= BIT(ESP_SLEEP_WAKEUP_WIFI);
2397+
}
2398+
#endif
2399+
#if SOC_PM_SUPPORT_BT_WAKEUP
2400+
if (wakeup_cause_raw & RTC_BT_TRIG_EN) {
2401+
wakeup_cause |= BIT(ESP_SLEEP_WAKEUP_BT);
2402+
}
2403+
#endif
2404+
#if SOC_RISCV_COPROC_SUPPORTED
2405+
if (wakeup_cause_raw & RTC_COCPU_TRIG_EN) {
2406+
wakeup_cause |= BIT(ESP_SLEEP_WAKEUP_ULP);
2407+
}
2408+
if (wakeup_cause_raw & RTC_COCPU_TRAP_TRIG_EN) {
2409+
wakeup_cause |= BIT(ESP_SLEEP_WAKEUP_COCPU_TRAP_TRIG);
2410+
}
2411+
#endif
2412+
#if SOC_LP_CORE_SUPPORTED
2413+
if (wakeup_cause_raw & RTC_LP_CORE_TRIG_EN) {
2414+
wakeup_cause |= BIT(ESP_SLEEP_WAKEUP_ULP);
2415+
}
2416+
#endif
2417+
#if SOC_LP_VAD_SUPPORTED
2418+
if (wakeup_cause_raw & RTC_LP_VAD_TRIG_EN) {
2419+
wakeup_cause |= BIT(ESP_SLEEP_WAKEUP_VAD);
2420+
}
2421+
#endif
2422+
#if SOC_VBAT_SUPPORTED
2423+
if (wakeup_cause_raw & RTC_VBAT_UNDER_VOLT_TRIG_EN) {
2424+
wakeup_cause |= BIT(ESP_SLEEP_WAKEUP_VBAT_UNDER_VOLT);
2425+
}
2426+
#endif
2427+
if (wakeup_cause == 0) {
2428+
wakeup_cause |= BIT(ESP_SLEEP_WAKEUP_UNDEFINED);
2429+
}
2430+
return wakeup_cause;
2431+
}
2432+
23422433
esp_err_t esp_sleep_pd_config(esp_sleep_pd_domain_t domain, esp_sleep_pd_option_t option)
23432434
{
23442435
if (domain >= ESP_PD_DOMAIN_MAX || option > ESP_PD_OPTION_AUTO) {

0 commit comments

Comments
 (0)