|
5 | 5 | #include "freertos/FreeRTOS.h" |
6 | 6 | #include "freertos/task.h" |
7 | 7 |
|
| 8 | +#include "soc/rtc_cntl_reg.h" // for read rtc registers directly (cause) |
| 9 | +#include "soc/soc.h" |
| 10 | + |
8 | 11 | #define ESP_EXT0_WAKEUP_LEVEL_LOW 0 |
9 | 12 | #define ESP_EXT0_WAKEUP_LEVEL_HIGH 1 |
10 | 13 |
|
| 14 | +// These are flags for wakeup cause to get it directly from RTC |
| 15 | +#define RTC_EXT0_TRIG_EN BIT(0) //!< EXT0 GPIO wakeup |
| 16 | +#define RTC_EXT1_TRIG_EN BIT(1) //!< EXT1 GPIO wakeup |
| 17 | +#define RTC_GPIO_TRIG_EN BIT(2) //!< GPIO wakeup (light sleep only) |
| 18 | +#define RTC_TIMER_TRIG_EN BIT(3) //!< Timer wakeup |
| 19 | +#define RTC_SDIO_TRIG_EN BIT(4) //!< SDIO wakeup (light sleep only) |
| 20 | +#define RTC_MAC_TRIG_EN BIT(5) //!< MAC wakeup (light sleep only) |
| 21 | +#define RTC_UART0_TRIG_EN BIT(6) //!< UART0 wakeup (light sleep only) |
| 22 | +#define RTC_UART1_TRIG_EN BIT(7) //!< UART1 wakeup (light sleep only) |
| 23 | +#define RTC_TOUCH_TRIG_EN BIT(8) //!< Touch wakeup |
| 24 | +#define RTC_ULP_TRIG_EN BIT(9) //!< ULP wakeup |
| 25 | +#define RTC_BT_TRIG_EN BIT(10) //!< BT wakeup (light sleep only) |
| 26 | + |
| 27 | +static struct timeval tv_start, tv_stop; |
| 28 | + |
11 | 29 | TEST_CASE("esp_deepsleep works", "[deepsleep][reset=DEEPSLEEP_RESET]") |
12 | 30 | { |
13 | 31 | esp_deep_sleep(2000000); |
@@ -48,44 +66,6 @@ TEST_CASE("wake up from light sleep using timer", "[deepsleep]") |
48 | 66 | TEST_ASSERT_INT32_WITHIN(500, 2000, (int) dt); |
49 | 67 | } |
50 | 68 |
|
51 | | -TEST_CASE("wake up disable timer for ext0 wakeup (13 low)", "[deepsleep][ignore]") |
52 | | -{ |
53 | | - // Setup timer to wakeup with timeout |
54 | | - esp_sleep_enable_timer_wakeup(2000000); |
55 | | - struct timeval tv_start, tv_stop; |
56 | | - gettimeofday(&tv_start, NULL); |
57 | | - esp_light_sleep_start(); |
58 | | - gettimeofday(&tv_stop, NULL); |
59 | | - float dt = (tv_stop.tv_sec - tv_start.tv_sec) * 1e3f + |
60 | | - (tv_stop.tv_usec - tv_start.tv_usec) * 1e-3f; |
61 | | - printf("Timer sleep time = %d\r\n", (int)dt); |
62 | | - |
63 | | - TEST_ASSERT_INT32_WITHIN(500, 2000, (int) dt); |
64 | | - |
65 | | - // Setup ext0 configuration to wake up |
66 | | - ESP_ERROR_CHECK(rtc_gpio_init(GPIO_NUM_13)); |
67 | | - ESP_ERROR_CHECK(gpio_pullup_en(GPIO_NUM_13)); |
68 | | - ESP_ERROR_CHECK(gpio_pulldown_dis(GPIO_NUM_13)); |
69 | | - ESP_ERROR_CHECK(esp_sleep_enable_ext0_wakeup(GPIO_NUM_13, ESP_EXT0_WAKEUP_LEVEL_LOW)); |
70 | | - |
71 | | - // Disable timer wakeup trigger to wakeup from ext0 source |
72 | | - // instead of timer wakeup |
73 | | - ESP_ERROR_CHECK(esp_sleep_disable_timer_wakeup()); |
74 | | - printf("Waiting low level on GPIO_13\r\n"); |
75 | | - |
76 | | - gettimeofday(&tv_start, NULL); |
77 | | - esp_light_sleep_start(); |
78 | | - gettimeofday(&tv_stop, NULL); |
79 | | - |
80 | | - dt = (tv_stop.tv_sec - tv_start.tv_sec) * 1e3f + |
81 | | - (tv_stop.tv_usec - tv_start.tv_usec) * 1e-3f; |
82 | | - printf("Ext0 sleep time = %d\r\n", (int)dt); |
83 | | - |
84 | | - // Check error message |
85 | | - esp_err_t err_code = esp_sleep_disable_timer_wakeup(); |
86 | | - TEST_ASSERT(err_code == ESP_ERR_INVALID_STATE); |
87 | | -} |
88 | | - |
89 | 69 | #ifndef CONFIG_FREERTOS_UNICORE |
90 | 70 | TEST_CASE("enter deep sleep on APP CPU and wake up using timer", "[deepsleep][reset=DEEPSLEEP_RESET]") |
91 | 71 | { |
@@ -148,3 +128,90 @@ TEST_CASE("wake up using ext1 when RTC_PERIPH is on (13 low)", "[deepsleep][igno |
148 | 128 | ESP_ERROR_CHECK(esp_sleep_enable_ext1_wakeup(BIT(GPIO_NUM_13), ESP_EXT1_WAKEUP_ALL_LOW)); |
149 | 129 | esp_deep_sleep_start(); |
150 | 130 | } |
| 131 | + |
| 132 | +static float get_time(void) |
| 133 | +{ |
| 134 | + gettimeofday(&tv_stop, NULL); |
| 135 | + |
| 136 | + float dt = (tv_stop.tv_sec - tv_start.tv_sec) * 1e3f + |
| 137 | + (tv_stop.tv_usec - tv_start.tv_usec) * 1e-3f; |
| 138 | + return abs(dt); |
| 139 | +} |
| 140 | + |
| 141 | +static uint32_t get_cause() |
| 142 | +{ |
| 143 | + uint32_t wakeup_cause = REG_GET_FIELD(RTC_CNTL_WAKEUP_STATE_REG, \ |
| 144 | + RTC_CNTL_WAKEUP_CAUSE); |
| 145 | + return wakeup_cause; |
| 146 | +} |
| 147 | + |
| 148 | +// This test case verifies deactivation of trigger for wake up sources |
| 149 | +TEST_CASE("disable source behavior", "[deepsleep][ignore]") |
| 150 | +{ |
| 151 | + float dt = 0; |
| 152 | + |
| 153 | + printf("Setup timer and ext0 to wakeup imediately from GPIO_13 \n"); |
| 154 | + |
| 155 | + // Setup ext0 configuration to wake up almost immediately |
| 156 | + // The wakeup time is proportional to input capacitance * pullup resistance |
| 157 | + ESP_ERROR_CHECK(rtc_gpio_init(GPIO_NUM_13)); |
| 158 | + ESP_ERROR_CHECK(gpio_pullup_en(GPIO_NUM_13)); |
| 159 | + ESP_ERROR_CHECK(gpio_pulldown_dis(GPIO_NUM_13)); |
| 160 | + ESP_ERROR_CHECK(esp_sleep_enable_ext0_wakeup(GPIO_NUM_13, ESP_EXT0_WAKEUP_LEVEL_HIGH)); |
| 161 | + |
| 162 | + // Setup timer to wakeup with timeout |
| 163 | + esp_sleep_enable_timer_wakeup(2000000); |
| 164 | + |
| 165 | + // Save start time |
| 166 | + gettimeofday(&tv_start, NULL); |
| 167 | + esp_light_sleep_start(); |
| 168 | + |
| 169 | + dt = get_time(); |
| 170 | + printf("Ext0 sleep time = %d \n", (int) dt); |
| 171 | + |
| 172 | + // Check wakeup from Ext0 using time measurement because wakeup cause is |
| 173 | + // not available in light sleep mode |
| 174 | + TEST_ASSERT_INT32_WITHIN(297, 300, (int) dt); |
| 175 | + |
| 176 | + TEST_ASSERT((get_cause() & RTC_EXT0_TRIG_EN) != 0); |
| 177 | + |
| 178 | + // Disable Ext0 source. Timer source should be triggered |
| 179 | + ESP_ERROR_CHECK(esp_sleep_disable_wakeup_source(ESP_SLEEP_SOURCE_EXT0)); |
| 180 | + printf("Disable ext0 source leave timer active.\n"); |
| 181 | + |
| 182 | + gettimeofday(&tv_start, NULL); |
| 183 | + esp_light_sleep_start(); |
| 184 | + |
| 185 | + dt = get_time(); |
| 186 | + printf("Timer sleep time = %d \n", (int) dt); |
| 187 | + |
| 188 | + TEST_ASSERT_INT32_WITHIN(500, 2000, (int) dt); |
| 189 | + |
| 190 | + // Additionaly check wakeup cause |
| 191 | + TEST_ASSERT((get_cause() & RTC_TIMER_TRIG_EN) != 0); |
| 192 | + |
| 193 | + // Disable timer source. |
| 194 | + ESP_ERROR_CHECK(esp_sleep_disable_wakeup_source(ESP_SLEEP_SOURCE_TIMER)); |
| 195 | + |
| 196 | + // Setup ext0 configuration to wake up immediately |
| 197 | + ESP_ERROR_CHECK(rtc_gpio_init(GPIO_NUM_13)); |
| 198 | + ESP_ERROR_CHECK(gpio_pullup_en(GPIO_NUM_13)); |
| 199 | + ESP_ERROR_CHECK(gpio_pulldown_dis(GPIO_NUM_13)); |
| 200 | + ESP_ERROR_CHECK(esp_sleep_enable_ext0_wakeup(GPIO_NUM_13, ESP_EXT0_WAKEUP_LEVEL_HIGH)); |
| 201 | + |
| 202 | + printf("Disable timer and wake up from ext0 source.\n"); |
| 203 | + |
| 204 | + gettimeofday(&tv_start, NULL); |
| 205 | + esp_light_sleep_start(); |
| 206 | + |
| 207 | + dt = get_time(); |
| 208 | + printf("Ext0 sleep time = %d \n", (int) dt); |
| 209 | + |
| 210 | + TEST_ASSERT_INT32_WITHIN(198, 200, (int) dt); |
| 211 | + TEST_ASSERT((get_cause() & RTC_EXT0_TRIG_EN) != 0); |
| 212 | + |
| 213 | + // Check error message when source is already disabled |
| 214 | + esp_err_t err_code = esp_sleep_disable_wakeup_source(ESP_SLEEP_SOURCE_TIMER); |
| 215 | + TEST_ASSERT(err_code == ESP_ERR_INVALID_STATE); |
| 216 | +} |
| 217 | + |
0 commit comments