Skip to content

Instantly share code, notes, and snippets.

@boarchuz
Created September 9, 2020 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boarchuz/f9cfb476fb85ede0ffc88d88c8d500f3 to your computer and use it in GitHub Desktop.
Save boarchuz/f9cfb476fb85ede0ffc88d88c8d500f3 to your computer and use it in GitHub Desktop.
ESP32 Deep Sleep Interrupt Detect Without Wake
#include "esp_system.h"
#include "esp_sleep.h"
#include "esp_log.h"
#include "driver/rtc_io.h"
#include "soc/rtc_io_struct.h"
static const char *TAG = "INT_DETECT";
#define INT_PIN GPIO_NUM_0
RTC_DATA_ATTR int counter;
static bool is_interrupt_triggered()
{
int rtcio_num = rtc_io_number_get(INT_PIN);
bool triggered = (RTCIO.status.status & BIT(rtcio_num));
if (triggered) {
// Clear
RTCIO.status_w1tc.w1tc = BIT(rtcio_num);
}
return triggered;
}
void app_main(void)
{
if (esp_reset_reason() == ESP_RST_DEEPSLEEP)
{
++counter;
if(is_interrupt_triggered())
{
ESP_LOGI(TAG, "Interrupt was triggered!");
counter = 0;
}
}
else
{
ESP_LOGI(TAG, "Starting...");
ESP_ERROR_CHECK( rtc_gpio_init(INT_PIN) );
ESP_ERROR_CHECK( rtc_gpio_set_direction(INT_PIN, RTC_GPIO_MODE_INPUT_ONLY) );
ESP_ERROR_CHECK( rtc_gpio_pulldown_dis(INT_PIN) );
ESP_ERROR_CHECK( rtc_gpio_pullup_en(INT_PIN) );
int rtcio_num = rtc_io_number_get(INT_PIN);
RTCIO.pin[rtcio_num].wakeup_enable = 0;
RTCIO.pin[rtcio_num].int_type = GPIO_INTR_NEGEDGE;
counter = 0;
}
ESP_LOGI(TAG, "Counter: %d", counter);
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
esp_deep_sleep(2ULL * 1000 * 1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment