Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/i2c.h"
#include "esp_log.h"
#include "string.h"
#include "driver/rtc_io.h"
#include "esp_sleep.h"
#include "time.h"
#include "sys/time.h"
#include "driver/uart.h"
#define pin GPIO_NUM_15
RTC_DATA_ATTR int timesWokenUp = 0;
void app_main(void)
{
/* 一般 GPIO config and wakeup enable */
bool re = esp_sleep_is_valid_wakeup_gpio(pin);
printf("pin %d is %s\n", pin, re == 1 ? "RTC_GPIO" : "NORMAL_GRIO");
gpio_pad_select_gpio(pin);
gpio_pulldown_en(pin);
gpio_pullup_dis(pin);
gpio_set_direction(pin, GPIO_MODE_INPUT);
printf("pin stat = %d\n ", gpio_get_level(pin));
gpio_wakeup_enable(pin, GPIO_INTR_HIGH_LEVEL);
esp_sleep_enable_gpio_wakeup();
/*使timer唤醒睡眠功能用*/
esp_sleep_enable_timer_wakeup(10000000);
while (1)
{
printf("start to sleep\n");
uart_wait_tx_idle_polling(CONFIG_ESP_CONSOLE_UART_NUM); //等待发送缓冲区的数据全部发送完成。
esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause();
switch (cause)
{
case ESP_SLEEP_WAKEUP_TIMER:
printf("wakeup from timer\n");
break;
case ESP_SLEEP_WAKEUP_EXT0:
printf("wakeup from EXT0\n");
break;
case ESP_SLEEP_WAKEUP_GPIO:
printf("wakeup from GPIO\n");
break;
case ESP_SLEEP_WAKEUP_EXT1:
printf("wakeup from EXT1\n");
break;
default:
printf("NO REASON\n");
}
// int64_t start = esp_timer_get_time();
printf("going to sleep. woken up %d\n", timesWokenUp++);
esp_deep_sleep_start();
printf("wakeup ,wakeup\n");
}
}