deep_sleep_example_main.c 9.7 KB

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