If I enable the pullup before going to sleep, then everything works fine however if my switch (rain tipping bucket) remains closed during the sleep, which it has a 50% chance of doing, then I am using power through the pull-up continuously which I am trying to avoid.
So, the question is, is it not allowed to toggle the rtc_gpio_pullup (that is, do a rtc_gpio_pullup_en, read the port, then do a rtc_gpio_pullup_dis) while in a wake_stub and then return to sleeping?
Here is a code example.
THANKS!
- // this is an example
- // GPIO-17 has a switch to ground
- #define RAIN_CNT_RTC_GPIO_NUM 17
- #define RAIN_CNT_GPIO_NUM GPIO_NUM_27
- static void RTC_IRAM_ATTR wake_stub(void)
- {
- #define PULSE_CNT_IS_LOW() \
- ((REG_GET_FIELD(RTC_GPIO_IN_REG, RTC_GPIO_IN_NEXT) & BIT(RAIN_CNT_RTC_GPIO_NUM)) == 0)
- /
- rtc_gpio_pullup_en(RAIN_CNT_GPIO_NUM); // start using power
- if (PULSE_CNT_IS_LOW())
- {
- // do something
- }
- else
- {
- // do something else
- }
- rtc_gpio_pullup_dis(RAIN_CNT_GPIO_NUM); // stop using power
- // Set wakeup time in stub
- uint64_t time_in_us = WAKE_EVERY_X_SECONDS * uS_TO_S_FACTOR;
- deepsleep_for_us(time_in_us);
- // Print status.
- ESP_RTC_LOGI("wake stub: going to deep sleep");
- // clear out the UART buffer before going to sleep
- while (REG_GET_FIELD(UART_STATUS_REG(0), UART_ST_UTX_OUT))
- {
- }
- // Set the pointer of the wake stub function.
- REG_WRITE(RTC_ENTRY_ADDR_REG, (uint32_t)&wake_stub);
- // Go to sleep again
- CLEAR_PERI_REG_MASK(RTC_CNTL_STATE0_REG, RTC_CNTL_SLEEP_EN);
- SET_PERI_REG_MASK(RTC_CNTL_STATE0_REG, RTC_CNTL_SLEEP_EN);
- } // end of wake_stub example