esp-idf/examples/system/light_sleep/main/light_sleep_example_main.c

93 lines
3.1 KiB
C
Raw Normal View History

2021-07-27 03:46:23 -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"
#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";
/* 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;
#if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
case ESP_SLEEP_WAKEUP_TOUCHPAD:
wakeup_reason = "touch";
break;
#endif
2022-01-11 22:03:38 -05:00
default:
wakeup_reason = "other";
break;
}
#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);
#endif
2022-01-11 22:03:38 -05:00
if (esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_GPIO) {
/* 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();
#if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
/* 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
}