2018-09-04 01:53:26 -04:00
|
|
|
/* Light sleep example
|
|
|
|
|
|
|
|
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, this
|
|
|
|
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
|
CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "esp_sleep.h"
|
|
|
|
#include "esp_log.h"
|
2019-05-22 08:21:11 -04:00
|
|
|
#include "driver/uart.h"
|
2020-04-23 00:39:07 -04:00
|
|
|
#include "driver/gpio.h"
|
2021-01-20 09:17:24 -05:00
|
|
|
#include "esp_timer.h"
|
2018-09-04 01:53:26 -04:00
|
|
|
|
|
|
|
/* Most development boards have "boot" button attached to GPIO0.
|
|
|
|
* You can also change this to another pin.
|
|
|
|
*/
|
|
|
|
#define BUTTON_GPIO_NUM_DEFAULT 0
|
|
|
|
|
|
|
|
/* "Boot" button on GPIO0 is active low */
|
|
|
|
#define BUTTON_WAKEUP_LEVEL_DEFAULT 0
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
void app_main(void)
|
2018-09-04 01:53:26 -04:00
|
|
|
{
|
|
|
|
/* Configure the button GPIO as input, enable wakeup */
|
|
|
|
const int button_gpio_num = BUTTON_GPIO_NUM_DEFAULT;
|
|
|
|
const int wakeup_level = BUTTON_WAKEUP_LEVEL_DEFAULT;
|
|
|
|
gpio_config_t config = {
|
|
|
|
.pin_bit_mask = BIT64(button_gpio_num),
|
|
|
|
.mode = GPIO_MODE_INPUT
|
|
|
|
};
|
|
|
|
ESP_ERROR_CHECK(gpio_config(&config));
|
|
|
|
gpio_wakeup_enable(button_gpio_num,
|
|
|
|
wakeup_level == 0 ? GPIO_INTR_LOW_LEVEL : GPIO_INTR_HIGH_LEVEL);
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
/* Wake up in 2 seconds, or when button is pressed */
|
|
|
|
esp_sleep_enable_timer_wakeup(2000000);
|
|
|
|
esp_sleep_enable_gpio_wakeup();
|
|
|
|
|
|
|
|
/* Wait until GPIO goes high */
|
2020-04-27 08:30:23 -04:00
|
|
|
if (gpio_get_level(button_gpio_num) == wakeup_level) {
|
2018-09-04 01:53:26 -04:00
|
|
|
printf("Waiting for GPIO%d to go high...\n", button_gpio_num);
|
|
|
|
do {
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(10));
|
2020-04-27 08:30:23 -04:00
|
|
|
} while (gpio_get_level(button_gpio_num) == wakeup_level);
|
2018-09-04 01:53:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
|
|
|
*/
|
2019-11-27 08:32:52 -05:00
|
|
|
uart_wait_tx_idle_polling(CONFIG_ESP_CONSOLE_UART_NUM);
|
2018-09-04 01:53:26 -04:00
|
|
|
|
|
|
|
/* Get timestamp before entering sleep */
|
|
|
|
int64_t t_before_us = esp_timer_get_time();
|
|
|
|
|
|
|
|
/* Enter sleep mode */
|
|
|
|
esp_light_sleep_start();
|
|
|
|
/* Execution continues here after wakeup */
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
default:
|
|
|
|
wakeup_reason = "other";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|