|
| 1 | +/* |
| 2 | +* This sketch tests the deep sleep functionality of the ESP32 |
| 3 | +*/ |
| 4 | + |
| 5 | +#include <Arduino.h> |
| 6 | +#include "driver/rtc_io.h" |
| 7 | +#include "driver/uart.h" |
| 8 | + |
| 9 | +// Timer |
| 10 | +#define uS_TO_S_FACTOR 1000000ULL /* Conversion factor for micro seconds to seconds */ |
| 11 | +#define TIME_TO_SLEEP 1 /* Time ESP32 will go to sleep (in seconds) */ |
| 12 | + |
| 13 | +// Touchpad |
| 14 | +#if CONFIG_IDF_TARGET_ESP32 |
| 15 | +#define THRESHOLD 1000 /* Greater the value, more the sensitivity */ |
| 16 | +#else |
| 17 | +#define THRESHOLD 0 /* Lower the value, more the sensitivity */ |
| 18 | +#endif |
| 19 | + |
| 20 | +// External wakeup |
| 21 | +#define BUTTON_PIN_BITMASK(GPIO) (1ULL << GPIO) // 2 ^ GPIO_NUMBER in hex |
| 22 | + |
| 23 | +#if CONFIG_IDF_TARGET_ESP32H2 |
| 24 | +#define WAKEUP_GPIO GPIO_NUM_7 // Only RTC IO are allowed |
| 25 | +#else |
| 26 | +#define WAKEUP_GPIO GPIO_NUM_4 // Only RTC IO are allowed |
| 27 | +#endif |
| 28 | + |
| 29 | +RTC_DATA_ATTR int bootCount = 0; |
| 30 | + |
| 31 | +void print_wakeup_reason() { |
| 32 | +esp_sleep_wakeup_cause_t wakeup_reason; |
| 33 | + |
| 34 | +wakeup_reason = esp_sleep_get_wakeup_cause(); |
| 35 | + |
| 36 | +Serial.print("Wakeup reason: "); |
| 37 | + |
| 38 | +switch (wakeup_reason) { |
| 39 | +case ESP_SLEEP_WAKEUP_EXT0: Serial.println("rtc_io"); break; |
| 40 | +case ESP_SLEEP_WAKEUP_EXT1: Serial.println("rtc_cntl"); break; |
| 41 | +case ESP_SLEEP_WAKEUP_TIMER: Serial.println("timer"); break; |
| 42 | +case ESP_SLEEP_WAKEUP_TOUCHPAD: Serial.println("touchpad"); break; |
| 43 | +case ESP_SLEEP_WAKEUP_ULP: Serial.println("ulp"); break; |
| 44 | +case ESP_SLEEP_WAKEUP_GPIO: Serial.println("gpio"); break; |
| 45 | +case ESP_SLEEP_WAKEUP_UART: Serial.println("uart"); break; |
| 46 | +default: Serial.println("power_up"); break; |
| 47 | +} |
| 48 | +} |
| 49 | + |
| 50 | +void setup_timer() { |
| 51 | +esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); |
| 52 | +} |
| 53 | + |
| 54 | +void setup_touchpad() { |
| 55 | +touchSleepWakeUpEnable(T1, THRESHOLD); |
| 56 | +} |
| 57 | + |
| 58 | +void setup_rtc_io() { |
| 59 | +esp_sleep_enable_ext0_wakeup(WAKEUP_GPIO, 1); |
| 60 | +rtc_gpio_pullup_en(WAKEUP_GPIO); |
| 61 | +rtc_gpio_pulldown_dis(WAKEUP_GPIO); |
| 62 | +} |
| 63 | + |
| 64 | +void setup_rtc_cntl() { |
| 65 | +esp_sleep_enable_ext1_wakeup_io(BUTTON_PIN_BITMASK(WAKEUP_GPIO), ESP_EXT1_WAKEUP_ANY_HIGH); |
| 66 | +rtc_gpio_pulldown_dis(WAKEUP_GPIO); |
| 67 | +rtc_gpio_pullup_en(WAKEUP_GPIO); |
| 68 | +} |
| 69 | + |
| 70 | +void setup_gpio() { |
| 71 | +esp_sleep_pd_config(ESP_PD_DOMAIN_VDDSDIO, ESP_PD_OPTION_ON); |
| 72 | +rtc_gpio_pullup_dis(WAKEUP_GPIO); |
| 73 | +rtc_gpio_pulldown_en(WAKEUP_GPIO); |
| 74 | +gpio_wakeup_enable(WAKEUP_GPIO, GPIO_INTR_HIGH_LEVEL); |
| 75 | +esp_sleep_enable_gpio_wakeup(); |
| 76 | +} |
| 77 | + |
| 78 | +void setup_uart() { |
| 79 | +uart_set_wakeup_threshold(UART_NUM_0, 9); // wake up with "aaa" string (9 positive edges) |
| 80 | +esp_sleep_enable_uart_wakeup(UART_NUM_0); |
| 81 | +} |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +void setup() { |
| 86 | +Serial.begin(115200); |
| 87 | +while (!Serial) { |
| 88 | +delay(10); |
| 89 | +} |
| 90 | + |
| 91 | +//Increment boot number and print it every reboot |
| 92 | +++bootCount; |
| 93 | +Serial.println("Boot number: " + String(bootCount)); |
| 94 | + |
| 95 | +//Print the wakeup reason for ESP32 |
| 96 | +print_wakeup_reason(); |
| 97 | +Serial.flush(); |
| 98 | +} |
| 99 | + |
| 100 | +void loop() { |
| 101 | +// Disable all configured wakeup sources |
| 102 | +esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_ALL); |
| 103 | + |
| 104 | +if(Serial.available() > 0) { |
| 105 | +String command = Serial.readString(); |
| 106 | +command.trim(); |
| 107 | + |
| 108 | +if (command == "timer_deep") { |
| 109 | +// Test timer wakeup from deep sleep |
| 110 | +setup_timer(); |
| 111 | +esp_deep_sleep_start(); |
| 112 | +Serial.println("FAIL"); |
| 113 | +while(1); |
| 114 | +} else if (command == "touchpad_deep") { |
| 115 | +// Test touchpad wakeup from deep sleep |
| 116 | +setup_touchpad(); |
| 117 | +esp_deep_sleep_start(); |
| 118 | +Serial.println("FAIL"); |
| 119 | +while(1); |
| 120 | +} else if (command == "rtc_io_deep") { |
| 121 | +// Test external wakeup from deep sleep using RTC IO |
| 122 | +setup_rtc_io(); |
| 123 | +esp_deep_sleep_start(); |
| 124 | +Serial.println("FAIL"); |
| 125 | +while(1); |
| 126 | +} else if (command == "rtc_cntl_deep") { |
| 127 | +// Test external wakeup from deep sleep using RTC controller |
| 128 | +setup_rtc_cntl(); |
| 129 | +esp_deep_sleep_start(); |
| 130 | +Serial.println("FAIL"); |
| 131 | +while(1); |
| 132 | +} else if (command == "timer_light") { |
| 133 | +// Test timer wakeup from light sleep |
| 134 | +setup_timer(); |
| 135 | +} else if (command == "touchpad_light") { |
| 136 | +// Test touchpad wakeup from light sleep |
| 137 | +setup_touchpad(); |
| 138 | +} else if (command == "rtc_io_light") { |
| 139 | +// Test external wakeup from light sleep using RTC IO |
| 140 | +setup_rtc_io(); |
| 141 | +} else if (command == "rtc_cntl_light") { |
| 142 | +// Test external wakeup from light sleep using RTC controller |
| 143 | +setup_rtc_cntl(); |
| 144 | +} else if (command == "gpio_light") { |
| 145 | +// Test external wakeup from light sleep using GPIO |
| 146 | +setup_gpio(); |
| 147 | +} else if (command == "uart_light") { |
| 148 | +// Test external wakeup from light sleep using UART |
| 149 | +setup_uart(); |
| 150 | +} else { |
| 151 | +Serial.println("FAIL"); |
| 152 | +while(1); |
| 153 | +} |
| 154 | + |
| 155 | +esp_light_sleep_start(); |
| 156 | +Serial.println("Woke up from light sleep"); |
| 157 | +print_wakeup_reason(); |
| 158 | +Serial.flush(); |
| 159 | +rtc_gpio_hold_dis(WAKEUP_GPIO); |
| 160 | +} |
| 161 | +} |
0 commit comments