2016-12-08 09:22:10 -05:00
|
|
|
#include "unity.h"
|
2017-04-21 00:32:50 -04:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include "esp_sleep.h"
|
2016-12-08 09:22:10 -05:00
|
|
|
#include "driver/rtc_io.h"
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
|
2018-03-20 02:43:48 -04:00
|
|
|
#include "soc/rtc.h" // for wakeup trigger defines
|
2018-03-16 02:57:35 -04:00
|
|
|
#include "soc/rtc_cntl_reg.h" // for read rtc registers directly (cause)
|
2018-03-20 02:43:48 -04:00
|
|
|
#include "soc/soc.h" // for direct register read macros
|
2018-03-16 02:57:35 -04:00
|
|
|
|
2018-03-14 01:54:45 -04:00
|
|
|
#define ESP_EXT0_WAKEUP_LEVEL_LOW 0
|
|
|
|
#define ESP_EXT0_WAKEUP_LEVEL_HIGH 1
|
|
|
|
|
2018-03-16 02:57:35 -04:00
|
|
|
static struct timeval tv_start, tv_stop;
|
|
|
|
|
2017-09-20 05:17:51 -04:00
|
|
|
TEST_CASE("esp_deepsleep works", "[deepsleep][reset=DEEPSLEEP_RESET]")
|
2016-12-08 09:22:10 -05:00
|
|
|
{
|
|
|
|
esp_deep_sleep(2000000);
|
|
|
|
}
|
|
|
|
|
2017-09-20 05:17:51 -04:00
|
|
|
static void deep_sleep_task(void *arg)
|
2016-12-08 09:22:10 -05:00
|
|
|
{
|
|
|
|
esp_deep_sleep_start();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void do_deep_sleep_from_app_cpu()
|
|
|
|
{
|
|
|
|
xTaskCreatePinnedToCore(&deep_sleep_task, "ds", 2048, NULL, 5, NULL, 1);
|
|
|
|
|
|
|
|
// keep running some non-IRAM code
|
|
|
|
vTaskSuspendAll();
|
2017-09-20 05:17:51 -04:00
|
|
|
|
|
|
|
while (true) {
|
2016-12-08 09:22:10 -05:00
|
|
|
;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-20 05:17:51 -04:00
|
|
|
TEST_CASE("wake up using timer", "[deepsleep][reset=DEEPSLEEP_RESET]")
|
2016-12-08 09:22:10 -05:00
|
|
|
{
|
2017-04-21 00:32:50 -04:00
|
|
|
esp_sleep_enable_timer_wakeup(2000000);
|
2016-12-08 09:22:10 -05:00
|
|
|
esp_deep_sleep_start();
|
|
|
|
}
|
|
|
|
|
2017-04-21 00:32:50 -04:00
|
|
|
TEST_CASE("wake up from light sleep using timer", "[deepsleep]")
|
|
|
|
{
|
|
|
|
esp_sleep_enable_timer_wakeup(2000000);
|
|
|
|
struct timeval tv_start, tv_stop;
|
|
|
|
gettimeofday(&tv_start, NULL);
|
|
|
|
esp_light_sleep_start();
|
|
|
|
gettimeofday(&tv_stop, NULL);
|
|
|
|
float dt = (tv_stop.tv_sec - tv_start.tv_sec) * 1e3f +
|
2017-09-20 05:17:51 -04:00
|
|
|
(tv_stop.tv_usec - tv_start.tv_usec) * 1e-3f;
|
2017-04-21 00:32:50 -04:00
|
|
|
TEST_ASSERT_INT32_WITHIN(500, 2000, (int) dt);
|
|
|
|
}
|
|
|
|
|
2017-11-10 00:26:11 -05:00
|
|
|
#ifndef CONFIG_FREERTOS_UNICORE
|
2017-09-20 05:17:51 -04:00
|
|
|
TEST_CASE("enter deep sleep on APP CPU and wake up using timer", "[deepsleep][reset=DEEPSLEEP_RESET]")
|
2016-12-08 09:22:10 -05:00
|
|
|
{
|
2017-04-21 00:32:50 -04:00
|
|
|
esp_sleep_enable_timer_wakeup(2000000);
|
2016-12-08 09:22:10 -05:00
|
|
|
do_deep_sleep_from_app_cpu();
|
|
|
|
}
|
2017-11-10 00:26:11 -05:00
|
|
|
#endif
|
2016-12-08 09:22:10 -05:00
|
|
|
|
|
|
|
|
2016-12-14 03:38:45 -05:00
|
|
|
TEST_CASE("wake up using ext0 (13 high)", "[deepsleep][ignore]")
|
2016-12-08 09:22:10 -05:00
|
|
|
{
|
|
|
|
ESP_ERROR_CHECK(rtc_gpio_init(GPIO_NUM_13));
|
|
|
|
ESP_ERROR_CHECK(gpio_pullup_dis(GPIO_NUM_13));
|
|
|
|
ESP_ERROR_CHECK(gpio_pulldown_en(GPIO_NUM_13));
|
2018-03-14 01:54:45 -04:00
|
|
|
ESP_ERROR_CHECK(esp_sleep_enable_ext0_wakeup(GPIO_NUM_13, ESP_EXT0_WAKEUP_LEVEL_HIGH));
|
2016-12-08 09:22:10 -05:00
|
|
|
esp_deep_sleep_start();
|
|
|
|
}
|
|
|
|
|
2016-12-14 03:38:45 -05:00
|
|
|
TEST_CASE("wake up using ext0 (13 low)", "[deepsleep][ignore]")
|
2016-12-08 09:22:10 -05:00
|
|
|
{
|
|
|
|
ESP_ERROR_CHECK(rtc_gpio_init(GPIO_NUM_13));
|
|
|
|
ESP_ERROR_CHECK(gpio_pullup_en(GPIO_NUM_13));
|
|
|
|
ESP_ERROR_CHECK(gpio_pulldown_dis(GPIO_NUM_13));
|
2018-03-14 01:54:45 -04:00
|
|
|
ESP_ERROR_CHECK(esp_sleep_enable_ext0_wakeup(GPIO_NUM_13, ESP_EXT0_WAKEUP_LEVEL_LOW));
|
2016-12-08 09:22:10 -05:00
|
|
|
esp_deep_sleep_start();
|
|
|
|
}
|
|
|
|
|
2016-12-14 03:38:45 -05:00
|
|
|
TEST_CASE("wake up using ext1 when RTC_PERIPH is off (13 high)", "[deepsleep][ignore]")
|
2016-12-16 01:10:07 -05:00
|
|
|
{
|
|
|
|
// This test needs external pulldown
|
|
|
|
ESP_ERROR_CHECK(rtc_gpio_init(GPIO_NUM_13));
|
2017-04-21 00:32:50 -04:00
|
|
|
ESP_ERROR_CHECK(esp_sleep_enable_ext1_wakeup(BIT(GPIO_NUM_13), ESP_EXT1_WAKEUP_ANY_HIGH));
|
2016-12-16 01:10:07 -05:00
|
|
|
esp_deep_sleep_start();
|
|
|
|
}
|
|
|
|
|
2016-12-14 03:38:45 -05:00
|
|
|
TEST_CASE("wake up using ext1 when RTC_PERIPH is off (13 low)", "[deepsleep][ignore]")
|
2016-12-16 01:10:07 -05:00
|
|
|
{
|
|
|
|
// This test needs external pullup
|
|
|
|
ESP_ERROR_CHECK(rtc_gpio_init(GPIO_NUM_13));
|
2017-04-21 00:32:50 -04:00
|
|
|
ESP_ERROR_CHECK(esp_sleep_enable_ext1_wakeup(BIT(GPIO_NUM_13), ESP_EXT1_WAKEUP_ALL_LOW));
|
2016-12-16 01:10:07 -05:00
|
|
|
esp_deep_sleep_start();
|
|
|
|
}
|
|
|
|
|
2016-12-14 03:38:45 -05:00
|
|
|
TEST_CASE("wake up using ext1 when RTC_PERIPH is on (13 high)", "[deepsleep][ignore]")
|
2016-12-08 09:22:10 -05:00
|
|
|
{
|
|
|
|
ESP_ERROR_CHECK(rtc_gpio_init(GPIO_NUM_13));
|
|
|
|
ESP_ERROR_CHECK(gpio_pullup_dis(GPIO_NUM_13));
|
|
|
|
ESP_ERROR_CHECK(gpio_pulldown_en(GPIO_NUM_13));
|
2017-04-21 00:32:50 -04:00
|
|
|
ESP_ERROR_CHECK(esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON));
|
|
|
|
ESP_ERROR_CHECK(esp_sleep_enable_ext1_wakeup(BIT(GPIO_NUM_13), ESP_EXT1_WAKEUP_ANY_HIGH));
|
2016-12-08 09:22:10 -05:00
|
|
|
esp_deep_sleep_start();
|
|
|
|
}
|
|
|
|
|
2016-12-14 03:38:45 -05:00
|
|
|
TEST_CASE("wake up using ext1 when RTC_PERIPH is on (13 low)", "[deepsleep][ignore]")
|
2016-12-08 09:22:10 -05:00
|
|
|
{
|
|
|
|
ESP_ERROR_CHECK(rtc_gpio_init(GPIO_NUM_13));
|
|
|
|
ESP_ERROR_CHECK(gpio_pullup_en(GPIO_NUM_13));
|
|
|
|
ESP_ERROR_CHECK(gpio_pulldown_dis(GPIO_NUM_13));
|
2017-04-21 00:32:50 -04:00
|
|
|
ESP_ERROR_CHECK(esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON));
|
|
|
|
ESP_ERROR_CHECK(esp_sleep_enable_ext1_wakeup(BIT(GPIO_NUM_13), ESP_EXT1_WAKEUP_ALL_LOW));
|
2016-12-08 09:22:10 -05:00
|
|
|
esp_deep_sleep_start();
|
|
|
|
}
|
2018-03-16 02:57:35 -04:00
|
|
|
|
2018-03-20 02:43:48 -04:00
|
|
|
static float get_time_ms(void)
|
2018-03-16 02:57:35 -04:00
|
|
|
{
|
|
|
|
gettimeofday(&tv_stop, NULL);
|
|
|
|
|
|
|
|
float dt = (tv_stop.tv_sec - tv_start.tv_sec) * 1e3f +
|
|
|
|
(tv_stop.tv_usec - tv_start.tv_usec) * 1e-3f;
|
2018-03-29 02:24:59 -04:00
|
|
|
return fabs(dt);
|
2018-03-16 02:57:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t get_cause()
|
|
|
|
{
|
|
|
|
uint32_t wakeup_cause = REG_GET_FIELD(RTC_CNTL_WAKEUP_STATE_REG, \
|
|
|
|
RTC_CNTL_WAKEUP_CAUSE);
|
|
|
|
return wakeup_cause;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This test case verifies deactivation of trigger for wake up sources
|
2018-03-20 02:43:48 -04:00
|
|
|
TEST_CASE("disable source trigger behavior", "[deepsleep]")
|
2018-03-16 02:57:35 -04:00
|
|
|
{
|
|
|
|
float dt = 0;
|
|
|
|
|
|
|
|
printf("Setup timer and ext0 to wakeup imediately from GPIO_13 \n");
|
|
|
|
|
|
|
|
// Setup ext0 configuration to wake up almost immediately
|
|
|
|
// The wakeup time is proportional to input capacitance * pullup resistance
|
|
|
|
ESP_ERROR_CHECK(rtc_gpio_init(GPIO_NUM_13));
|
|
|
|
ESP_ERROR_CHECK(gpio_pullup_en(GPIO_NUM_13));
|
|
|
|
ESP_ERROR_CHECK(gpio_pulldown_dis(GPIO_NUM_13));
|
|
|
|
ESP_ERROR_CHECK(esp_sleep_enable_ext0_wakeup(GPIO_NUM_13, ESP_EXT0_WAKEUP_LEVEL_HIGH));
|
|
|
|
|
|
|
|
// Setup timer to wakeup with timeout
|
|
|
|
esp_sleep_enable_timer_wakeup(2000000);
|
|
|
|
|
|
|
|
// Save start time
|
|
|
|
gettimeofday(&tv_start, NULL);
|
|
|
|
esp_light_sleep_start();
|
|
|
|
|
2018-03-20 02:43:48 -04:00
|
|
|
dt = get_time_ms();
|
2018-03-16 02:57:35 -04:00
|
|
|
printf("Ext0 sleep time = %d \n", (int) dt);
|
|
|
|
|
|
|
|
// Check wakeup from Ext0 using time measurement because wakeup cause is
|
|
|
|
// not available in light sleep mode
|
2018-03-20 02:43:48 -04:00
|
|
|
TEST_ASSERT_INT32_WITHIN(299, 300, (int) dt);
|
2018-03-16 02:57:35 -04:00
|
|
|
|
|
|
|
TEST_ASSERT((get_cause() & RTC_EXT0_TRIG_EN) != 0);
|
|
|
|
|
|
|
|
// Disable Ext0 source. Timer source should be triggered
|
2018-03-20 02:43:48 -04:00
|
|
|
ESP_ERROR_CHECK(esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_EXT0));
|
|
|
|
printf("Disable ext0 trigger and leave timer active.\n");
|
2018-03-16 02:57:35 -04:00
|
|
|
|
|
|
|
gettimeofday(&tv_start, NULL);
|
|
|
|
esp_light_sleep_start();
|
|
|
|
|
2018-03-20 02:43:48 -04:00
|
|
|
dt = get_time_ms();
|
2018-03-16 02:57:35 -04:00
|
|
|
printf("Timer sleep time = %d \n", (int) dt);
|
|
|
|
|
|
|
|
TEST_ASSERT_INT32_WITHIN(500, 2000, (int) dt);
|
|
|
|
|
|
|
|
// Additionaly check wakeup cause
|
|
|
|
TEST_ASSERT((get_cause() & RTC_TIMER_TRIG_EN) != 0);
|
|
|
|
|
|
|
|
// Disable timer source.
|
2018-03-20 02:43:48 -04:00
|
|
|
ESP_ERROR_CHECK(esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_TIMER));
|
2018-03-16 02:57:35 -04:00
|
|
|
|
|
|
|
// Setup ext0 configuration to wake up immediately
|
|
|
|
ESP_ERROR_CHECK(rtc_gpio_init(GPIO_NUM_13));
|
|
|
|
ESP_ERROR_CHECK(gpio_pullup_en(GPIO_NUM_13));
|
|
|
|
ESP_ERROR_CHECK(gpio_pulldown_dis(GPIO_NUM_13));
|
|
|
|
ESP_ERROR_CHECK(esp_sleep_enable_ext0_wakeup(GPIO_NUM_13, ESP_EXT0_WAKEUP_LEVEL_HIGH));
|
|
|
|
|
2018-03-20 02:43:48 -04:00
|
|
|
printf("Disable timer trigger to wake up from ext0 source.\n");
|
2018-03-16 02:57:35 -04:00
|
|
|
|
|
|
|
gettimeofday(&tv_start, NULL);
|
|
|
|
esp_light_sleep_start();
|
|
|
|
|
2018-03-20 02:43:48 -04:00
|
|
|
dt = get_time_ms();
|
2018-03-16 02:57:35 -04:00
|
|
|
printf("Ext0 sleep time = %d \n", (int) dt);
|
|
|
|
|
2018-03-20 02:43:48 -04:00
|
|
|
TEST_ASSERT_INT32_WITHIN(199, 200, (int) dt);
|
2018-03-16 02:57:35 -04:00
|
|
|
TEST_ASSERT((get_cause() & RTC_EXT0_TRIG_EN) != 0);
|
|
|
|
|
|
|
|
// Check error message when source is already disabled
|
2018-03-20 02:43:48 -04:00
|
|
|
esp_err_t err_code = esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_TIMER);
|
2018-03-16 02:57:35 -04:00
|
|
|
TEST_ASSERT(err_code == ESP_ERR_INVALID_STATE);
|
2018-03-20 02:43:48 -04:00
|
|
|
printf("Test case completed successfully.");
|
2018-03-16 02:57:35 -04:00
|
|
|
}
|
|
|
|
|