light_sleep_example_main.c 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <time.h>
  10. #include <sys/time.h>
  11. #include "freertos/FreeRTOS.h"
  12. #include "freertos/task.h"
  13. #include "driver/uart.h"
  14. #include "esp_sleep.h"
  15. #include "esp_log.h"
  16. #include "esp_timer.h"
  17. #include "light_sleep_example.h"
  18. static void light_sleep_task(void *args)
  19. {
  20. while (true) {
  21. printf("Entering light sleep\n");
  22. /* To make sure the complete line is printed before entering sleep mode,
  23. * need to wait until UART TX FIFO is empty:
  24. */
  25. uart_wait_tx_idle_polling(CONFIG_ESP_CONSOLE_UART_NUM);
  26. /* Get timestamp before entering sleep */
  27. int64_t t_before_us = esp_timer_get_time();
  28. /* Enter sleep mode */
  29. esp_light_sleep_start();
  30. /* Get timestamp after waking up from sleep */
  31. int64_t t_after_us = esp_timer_get_time();
  32. /* Determine wake up reason */
  33. const char* wakeup_reason;
  34. switch (esp_sleep_get_wakeup_cause()) {
  35. case ESP_SLEEP_WAKEUP_TIMER:
  36. wakeup_reason = "timer";
  37. break;
  38. case ESP_SLEEP_WAKEUP_GPIO:
  39. wakeup_reason = "pin";
  40. break;
  41. case ESP_SLEEP_WAKEUP_UART:
  42. wakeup_reason = "uart";
  43. /* Hang-up for a while to switch and execuse the uart task
  44. * Otherwise the chip may fall sleep again before running uart task */
  45. vTaskDelay(1);
  46. break;
  47. #if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
  48. case ESP_SLEEP_WAKEUP_TOUCHPAD:
  49. wakeup_reason = "touch";
  50. break;
  51. #endif
  52. default:
  53. wakeup_reason = "other";
  54. break;
  55. }
  56. #if CONFIG_NEWLIB_NANO_FORMAT
  57. /* printf in newlib-nano does not support %ll format, causing example test fail */
  58. printf("Returned from light sleep, reason: %s, t=%d ms, slept for %d ms\n",
  59. wakeup_reason, (int) (t_after_us / 1000), (int) ((t_after_us - t_before_us) / 1000));
  60. #else
  61. printf("Returned from light sleep, reason: %s, t=%lld ms, slept for %lld ms\n",
  62. wakeup_reason, t_after_us / 1000, (t_after_us - t_before_us) / 1000);
  63. #endif
  64. if (esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_GPIO) {
  65. /* Waiting for the gpio inactive, or the chip will continously trigger wakeup*/
  66. example_wait_gpio_inactive();
  67. }
  68. }
  69. vTaskDelete(NULL);
  70. }
  71. void app_main(void)
  72. {
  73. /* Enable wakeup from light sleep by gpio */
  74. example_register_gpio_wakeup();
  75. /* Enable wakeup from light sleep by timer */
  76. example_register_timer_wakeup();
  77. /* Enable wakeup from light sleep by uart */
  78. example_register_uart_wakeup();
  79. #if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
  80. /* Enable wakeup from light sleep by touch element */
  81. example_register_touch_wakeup();
  82. #endif
  83. xTaskCreate(light_sleep_task, "light_sleep_task", 4096, NULL, 6, NULL);
  84. }