deep_sleep_example_main.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /* Deep sleep wake up example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <time.h>
  11. #include <sys/time.h>
  12. #include "sdkconfig.h"
  13. #include "soc/soc_caps.h"
  14. #include "freertos/FreeRTOS.h"
  15. #include "freertos/task.h"
  16. #include "esp_sleep.h"
  17. #include "esp_log.h"
  18. #include "driver/rtc_io.h"
  19. #include "soc/rtc.h"
  20. #if SOC_TOUCH_SENSOR_SUPPORTED
  21. #include "soc/sens_periph.h"
  22. #include "driver/touch_pad.h"
  23. #endif
  24. #if SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
  25. #define DEFAULT_WAKEUP_PIN CONFIG_EXAMPLE_GPIO_WAKEUP_PIN
  26. #ifdef CONFIG_EXAMPLE_GPIO_WAKEUP_HIGH_LEVEL
  27. #define DEFAULT_WAKEUP_LEVEL ESP_GPIO_WAKEUP_GPIO_HIGH
  28. #else
  29. #define DEFAULT_WAKEUP_LEVEL ESP_GPIO_WAKEUP_GPIO_LOW
  30. #endif
  31. #endif
  32. static RTC_DATA_ATTR struct timeval sleep_enter_time;
  33. #ifdef CONFIG_EXAMPLE_TOUCH_WAKEUP
  34. #if CONFIG_IDF_TARGET_ESP32
  35. #define TOUCH_THRESH_NO_USE 0
  36. static void calibrate_touch_pad(touch_pad_t pad);
  37. #endif
  38. #endif
  39. void app_main(void)
  40. {
  41. struct timeval now;
  42. gettimeofday(&now, NULL);
  43. int sleep_time_ms = (now.tv_sec - sleep_enter_time.tv_sec) * 1000 + (now.tv_usec - sleep_enter_time.tv_usec) / 1000;
  44. switch (esp_sleep_get_wakeup_cause()) {
  45. #if CONFIG_EXAMPLE_EXT0_WAKEUP
  46. case ESP_SLEEP_WAKEUP_EXT0: {
  47. printf("Wake up from ext0\n");
  48. break;
  49. }
  50. #endif // CONFIG_EXAMPLE_EXT0_WAKEUP
  51. #ifdef CONFIG_EXAMPLE_EXT1_WAKEUP
  52. case ESP_SLEEP_WAKEUP_EXT1: {
  53. uint64_t wakeup_pin_mask = esp_sleep_get_ext1_wakeup_status();
  54. if (wakeup_pin_mask != 0) {
  55. int pin = __builtin_ffsll(wakeup_pin_mask) - 1;
  56. printf("Wake up from GPIO %d\n", pin);
  57. } else {
  58. printf("Wake up from GPIO\n");
  59. }
  60. break;
  61. }
  62. #endif // CONFIG_EXAMPLE_EXT1_WAKEUP
  63. #if SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
  64. case ESP_SLEEP_WAKEUP_GPIO: {
  65. uint64_t wakeup_pin_mask = esp_sleep_get_gpio_wakeup_status();
  66. if (wakeup_pin_mask != 0) {
  67. int pin = __builtin_ffsll(wakeup_pin_mask) - 1;
  68. printf("Wake up from GPIO %d\n", pin);
  69. } else {
  70. printf("Wake up from GPIO\n");
  71. }
  72. break;
  73. }
  74. #endif //SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
  75. case ESP_SLEEP_WAKEUP_TIMER: {
  76. printf("Wake up from timer. Time spent in deep sleep: %dms\n", sleep_time_ms);
  77. break;
  78. }
  79. #ifdef CONFIG_EXAMPLE_TOUCH_WAKEUP
  80. case ESP_SLEEP_WAKEUP_TOUCHPAD: {
  81. printf("Wake up from touch on pad %d\n", esp_sleep_get_touchpad_wakeup_status());
  82. break;
  83. }
  84. #endif // CONFIG_EXAMPLE_TOUCH_WAKEUP
  85. case ESP_SLEEP_WAKEUP_UNDEFINED:
  86. default:
  87. printf("Not a deep sleep reset\n");
  88. }
  89. vTaskDelay(1000 / portTICK_PERIOD_MS);
  90. const int wakeup_time_sec = 20;
  91. printf("Enabling timer wakeup, %ds\n", wakeup_time_sec);
  92. esp_sleep_enable_timer_wakeup(wakeup_time_sec * 1000000);
  93. #if CONFIG_EXAMPLE_EXT0_WAKEUP
  94. const int ext_wakeup_pin_0 = 3;
  95. printf("Enabling EXT0 wakeup on pin GPIO%d\n", ext_wakeup_pin_0);
  96. esp_sleep_enable_ext0_wakeup(ext_wakeup_pin_0, 1);
  97. // Configure pullup/downs via RTCIO to tie wakeup pins to inactive level during deepsleep.
  98. // EXT0 resides in the same power domain (RTC_PERIPH) as the RTC IO pullup/downs.
  99. // No need to keep that power domain explicitly, unlike EXT1.
  100. rtc_gpio_pullup_dis(ext_wakeup_pin_0);
  101. rtc_gpio_pulldown_en(ext_wakeup_pin_0);
  102. #endif // CONFIG_EXAMPLE_EXT0_WAKEUP
  103. #ifdef CONFIG_EXAMPLE_EXT1_WAKEUP
  104. const int ext_wakeup_pin_1 = 2;
  105. const uint64_t ext_wakeup_pin_1_mask = 1ULL << ext_wakeup_pin_1;
  106. const int ext_wakeup_pin_2 = 4;
  107. const uint64_t ext_wakeup_pin_2_mask = 1ULL << ext_wakeup_pin_2;
  108. printf("Enabling EXT1 wakeup on pins GPIO%d, GPIO%d\n", ext_wakeup_pin_1, ext_wakeup_pin_2);
  109. esp_sleep_enable_ext1_wakeup(ext_wakeup_pin_1_mask | ext_wakeup_pin_2_mask, ESP_EXT1_WAKEUP_ANY_HIGH);
  110. /* If there are no external pull-up/downs, tie wakeup pins to inactive level with internal pull-up/downs via RTC IO
  111. * during deepsleep. However, RTC IO relies on the RTC_PERIPH power domain. Keeping this power domain on will
  112. * increase some power comsumption. */
  113. # if CONFIG_EXAMPLE_EXT1_USE_INTERNAL_PULLUPS
  114. esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
  115. rtc_gpio_pullup_dis(ext_wakeup_pin_1);
  116. rtc_gpio_pulldown_en(ext_wakeup_pin_1);
  117. rtc_gpio_pullup_dis(ext_wakeup_pin_2);
  118. rtc_gpio_pulldown_en(ext_wakeup_pin_2);
  119. # endif //CONFIG_EXAMPLE_EXT1_USE_INTERNAL_PULLUPS
  120. #endif // CONFIG_EXAMPLE_EXT1_WAKEUP
  121. #ifdef CONFIG_EXAMPLE_GPIO_WAKEUP
  122. const gpio_config_t config = {
  123. .pin_bit_mask = BIT(DEFAULT_WAKEUP_PIN),
  124. .mode = GPIO_MODE_INPUT,
  125. };
  126. ESP_ERROR_CHECK(gpio_config(&config));
  127. ESP_ERROR_CHECK(esp_deep_sleep_enable_gpio_wakeup(BIT(DEFAULT_WAKEUP_PIN), DEFAULT_WAKEUP_LEVEL));
  128. printf("Enabling GPIO wakeup on pins GPIO%d\n", DEFAULT_WAKEUP_PIN);
  129. #endif
  130. #ifdef CONFIG_EXAMPLE_TOUCH_WAKEUP
  131. #if CONFIG_IDF_TARGET_ESP32
  132. // Initialize touch pad peripheral.
  133. // The default fsm mode is software trigger mode.
  134. ESP_ERROR_CHECK(touch_pad_init());
  135. // If use touch pad wake up, should set touch sensor FSM mode at 'TOUCH_FSM_MODE_TIMER'.
  136. touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
  137. // Set reference voltage for charging/discharging
  138. // In this case, the high reference valtage will be 2.4V - 1V = 1.4V
  139. // The low reference voltage will be 0.5
  140. // The larger the range, the larger the pulse count value.
  141. touch_pad_set_voltage(TOUCH_HVOLT_2V4, TOUCH_LVOLT_0V5, TOUCH_HVOLT_ATTEN_1V);
  142. //init RTC IO and mode for touch pad.
  143. touch_pad_config(TOUCH_PAD_NUM8, TOUCH_THRESH_NO_USE);
  144. touch_pad_config(TOUCH_PAD_NUM9, TOUCH_THRESH_NO_USE);
  145. calibrate_touch_pad(TOUCH_PAD_NUM8);
  146. calibrate_touch_pad(TOUCH_PAD_NUM9);
  147. #elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
  148. /* Initialize touch pad peripheral. */
  149. touch_pad_init();
  150. /* Only support one touch channel in sleep mode. */
  151. touch_pad_config(TOUCH_PAD_NUM9);
  152. /* Denoise setting at TouchSensor 0. */
  153. touch_pad_denoise_t denoise = {
  154. /* The bits to be cancelled are determined according to the noise level. */
  155. .grade = TOUCH_PAD_DENOISE_BIT4,
  156. .cap_level = TOUCH_PAD_DENOISE_CAP_L4,
  157. };
  158. touch_pad_denoise_set_config(&denoise);
  159. touch_pad_denoise_enable();
  160. printf("Denoise function init\n");
  161. /* Filter setting */
  162. touch_filter_config_t filter_info = {
  163. .mode = TOUCH_PAD_FILTER_IIR_16,
  164. .debounce_cnt = 1, // 1 time count.
  165. .noise_thr = 0, // 50%
  166. .jitter_step = 4, // use for jitter mode.
  167. .smh_lvl = TOUCH_PAD_SMOOTH_IIR_2,
  168. };
  169. touch_pad_filter_set_config(&filter_info);
  170. touch_pad_filter_enable();
  171. printf("touch pad filter init %d\n", TOUCH_PAD_FILTER_IIR_8);
  172. /* Set sleep touch pad. */
  173. touch_pad_sleep_channel_enable(TOUCH_PAD_NUM9, true);
  174. touch_pad_sleep_channel_enable_proximity(TOUCH_PAD_NUM9, false);
  175. /* Reducing the operating frequency can effectively reduce power consumption. */
  176. touch_pad_sleep_channel_set_work_time(1000, TOUCH_PAD_MEASURE_CYCLE_DEFAULT);
  177. /* Enable touch sensor clock. Work mode is "timer trigger". */
  178. touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
  179. touch_pad_fsm_start();
  180. vTaskDelay(100 / portTICK_PERIOD_MS);
  181. /* set touchpad wakeup threshold */
  182. uint32_t touch_value, wake_threshold;
  183. touch_pad_sleep_channel_read_smooth(TOUCH_PAD_NUM9, &touch_value);
  184. wake_threshold = touch_value * 0.1; // wakeup when touch sensor crosses 10% of background level
  185. touch_pad_sleep_set_threshold(TOUCH_PAD_NUM9, wake_threshold);
  186. printf("Touch pad #%d average: %d, wakeup threshold set to %d\n",
  187. TOUCH_PAD_NUM9, touch_value, (uint32_t)(touch_value * 0.1));
  188. #endif
  189. printf("Enabling touch pad wakeup\n");
  190. esp_sleep_enable_touchpad_wakeup();
  191. esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
  192. #endif // CONFIG_EXAMPLE_TOUCH_WAKEUP
  193. #if CONFIG_IDF_TARGET_ESP32
  194. // Isolate GPIO12 pin from external circuits. This is needed for modules
  195. // which have an external pull-up resistor on GPIO12 (such as ESP32-WROVER)
  196. // to minimize current consumption.
  197. rtc_gpio_isolate(GPIO_NUM_12);
  198. #endif
  199. printf("Entering deep sleep\n");
  200. gettimeofday(&sleep_enter_time, NULL);
  201. esp_deep_sleep_start();
  202. }
  203. #ifdef CONFIG_EXAMPLE_TOUCH_WAKEUP
  204. #if CONFIG_IDF_TARGET_ESP32
  205. static void calibrate_touch_pad(touch_pad_t pad)
  206. {
  207. int avg = 0;
  208. const size_t calibration_count = 128;
  209. for (int i = 0; i < calibration_count; ++i) {
  210. uint16_t val;
  211. touch_pad_read(pad, &val);
  212. avg += val;
  213. }
  214. avg /= calibration_count;
  215. const int min_reading = 300;
  216. if (avg < min_reading) {
  217. printf("Touch pad #%d average reading is too low: %d (expecting at least %d). "
  218. "Not using for deep sleep wakeup.\n", pad, avg, min_reading);
  219. touch_pad_config(pad, 0);
  220. } else {
  221. int threshold = avg - 100;
  222. printf("Touch pad #%d average: %d, wakeup threshold set to %d.\n", pad, avg, threshold);
  223. touch_pad_config(pad, threshold);
  224. }
  225. }
  226. #endif
  227. #endif // CONFIG_EXAMPLE_TOUCH_WAKEUP