2021-07-27 03:46:23 -04:00
|
|
|
/*
|
2024-04-07 03:24:42 -04:00
|
|
|
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
|
2021-07-27 03:46:23 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Unlicense OR CC0-1.0
|
|
|
|
*/
|
2018-09-04 01:53:26 -04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
2022-01-11 22:03:38 -05:00
|
|
|
#include "driver/uart.h"
|
2018-09-04 01:53:26 -04:00
|
|
|
#include "esp_sleep.h"
|
|
|
|
#include "esp_log.h"
|
2022-04-16 05:32:22 -04:00
|
|
|
#include "esp_timer.h"
|
2022-01-11 22:03:38 -05:00
|
|
|
#include "light_sleep_example.h"
|
2018-09-04 01:53:26 -04:00
|
|
|
|
2022-01-11 22:03:38 -05:00
|
|
|
static void light_sleep_task(void *args)
|
2021-07-27 03:46:23 -04:00
|
|
|
{
|
2022-01-11 22:03:38 -05:00
|
|
|
while (true) {
|
|
|
|
printf("Entering light sleep\n");
|
|
|
|
/* To make sure the complete line is printed before entering sleep mode,
|
|
|
|
* need to wait until UART TX FIFO is empty:
|
|
|
|
*/
|
|
|
|
uart_wait_tx_idle_polling(CONFIG_ESP_CONSOLE_UART_NUM);
|
|
|
|
|
|
|
|
/* Get timestamp before entering sleep */
|
|
|
|
int64_t t_before_us = esp_timer_get_time();
|
|
|
|
|
|
|
|
/* Enter sleep mode */
|
|
|
|
esp_light_sleep_start();
|
|
|
|
|
|
|
|
/* Get timestamp after waking up from sleep */
|
|
|
|
int64_t t_after_us = esp_timer_get_time();
|
|
|
|
|
|
|
|
/* Determine wake up reason */
|
|
|
|
const char* wakeup_reason;
|
|
|
|
switch (esp_sleep_get_wakeup_cause()) {
|
|
|
|
case ESP_SLEEP_WAKEUP_TIMER:
|
|
|
|
wakeup_reason = "timer";
|
|
|
|
break;
|
|
|
|
case ESP_SLEEP_WAKEUP_GPIO:
|
|
|
|
wakeup_reason = "pin";
|
|
|
|
break;
|
|
|
|
case ESP_SLEEP_WAKEUP_UART:
|
|
|
|
wakeup_reason = "uart";
|
2024-04-07 03:24:42 -04:00
|
|
|
/* Hang-up for a while to switch and execute the uart task
|
2022-01-11 22:03:38 -05:00
|
|
|
* Otherwise the chip may fall sleep again before running uart task */
|
|
|
|
vTaskDelay(1);
|
|
|
|
break;
|
2024-06-07 05:25:35 -04:00
|
|
|
#if EXAMPLE_TOUCH_LSLEEP_WAKEUP_SUPPORT
|
2021-11-26 05:09:24 -05:00
|
|
|
case ESP_SLEEP_WAKEUP_TOUCHPAD:
|
|
|
|
wakeup_reason = "touch";
|
|
|
|
break;
|
|
|
|
#endif
|
2022-01-11 22:03:38 -05:00
|
|
|
default:
|
|
|
|
wakeup_reason = "other";
|
|
|
|
break;
|
|
|
|
}
|
2023-02-01 03:37:04 -05:00
|
|
|
#if CONFIG_NEWLIB_NANO_FORMAT
|
|
|
|
/* printf in newlib-nano does not support %ll format, causing example test fail */
|
|
|
|
printf("Returned from light sleep, reason: %s, t=%d ms, slept for %d ms\n",
|
|
|
|
wakeup_reason, (int) (t_after_us / 1000), (int) ((t_after_us - t_before_us) / 1000));
|
|
|
|
#else
|
2022-01-11 22:03:38 -05:00
|
|
|
printf("Returned from light sleep, reason: %s, t=%lld ms, slept for %lld ms\n",
|
|
|
|
wakeup_reason, t_after_us / 1000, (t_after_us - t_before_us) / 1000);
|
2023-02-01 03:37:04 -05:00
|
|
|
#endif
|
2022-01-11 22:03:38 -05:00
|
|
|
if (esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_GPIO) {
|
2024-04-07 03:24:42 -04:00
|
|
|
/* Waiting for the gpio inactive, or the chip will continuously trigger wakeup*/
|
2022-01-11 22:03:38 -05:00
|
|
|
example_wait_gpio_inactive();
|
2018-09-04 01:53:26 -04:00
|
|
|
}
|
|
|
|
}
|
2021-07-27 00:18:56 -04:00
|
|
|
vTaskDelete(NULL);
|
|
|
|
}
|
2018-09-04 01:53:26 -04:00
|
|
|
|
2021-07-27 00:18:56 -04:00
|
|
|
void app_main(void)
|
|
|
|
{
|
2022-01-11 22:03:38 -05:00
|
|
|
/* Enable wakeup from light sleep by gpio */
|
|
|
|
example_register_gpio_wakeup();
|
|
|
|
/* Enable wakeup from light sleep by timer */
|
|
|
|
example_register_timer_wakeup();
|
|
|
|
/* Enable wakeup from light sleep by uart */
|
|
|
|
example_register_uart_wakeup();
|
2024-06-07 05:25:35 -04:00
|
|
|
#if EXAMPLE_TOUCH_LSLEEP_WAKEUP_SUPPORT
|
2021-11-26 05:09:24 -05:00
|
|
|
/* Enable wakeup from light sleep by touch element */
|
|
|
|
example_register_touch_wakeup();
|
|
|
|
#endif
|
2022-01-11 22:03:38 -05:00
|
|
|
|
|
|
|
xTaskCreate(light_sleep_task, "light_sleep_task", 4096, NULL, 6, NULL);
|
2018-09-04 01:53:26 -04:00
|
|
|
}
|