Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getMicros() and getMillis() strange behavior with RTC external wake #36

Open
W4KRL opened this issue Oct 16, 2023 · 0 comments
Open

getMicros() and getMillis() strange behavior with RTC external wake #36

W4KRL opened this issue Oct 16, 2023 · 0 comments

Comments

@W4KRL
Copy link

W4KRL commented Oct 16, 2023

getMicros() and getMillis() return values that are not monotonically increasing.

When code senses a high on pin 39 it flips the sense to low, records getMillis(), and goes to sleep. Now when code senses a low on 39 it wakes the ESP32 from sleep, records getMillis(), flips sense, and goes back to sleep. getMillis should be forever increasing but it does not. getTime works fine.

Here is sample output with discrepancy noted. Code follows.

15:24:23.739 -> ets Jul 29 2019 12:21:46
15:24:23.739 ->
15:24:23.739 -> rst:0x5 (DEEPSLEEP_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
15:24:23.739 -> configsip: 0, SPIWP:0xee
15:24:23.739 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
15:24:23.739 -> mode:DIO, clock div:1
15:24:23.739 -> load:0x3fff0030,len:1344
15:24:23.739 -> load:0x40078000,len:13964
15:24:23.739 -> load:0x40080400,len:3600
15:24:23.739 -> entry 0x400805f0
15:24:23.784 -> getMillis(): 827 <========= big number
15:24:24.771 -> Boot number: 11
15:24:24.771 -> Wakeup external RTC_IO
15:24:24.771 -> Sunday, January 17 2021 15:25:28 <==== time for boot 11
15:24:24.771 -> Going to sleep now with sense = 0
15:24:24.771 -> getMillis(): 827
16:54:08.992 -> ets Jul 29 2019 12:21:46
16:54:08.992 ->
16:54:08.992 -> rst:0x5 (DEEPSLEEP_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
16:54:08.992 -> configsip: 0, SPIWP:0xee
16:54:08.992 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
16:54:08.992 -> mode:DIO, clock div:1
16:54:08.992 -> load:0x3fff0030,len:1344
16:54:08.992 -> load:0x40078000,len:13964
16:54:08.992 -> load:0x40080400,len:3600
16:54:08.992 -> entry 0x400805f0
16:54:09.029 -> getMillis(): 490 <======== smaller number!!!. it should be higher than boot 11
16:54:10.046 -> Boot number: 12
16:54:10.046 -> Wakeup external RTC_IO
16:54:10.046 -> Sunday, January 17 2021 16:55:59 < ====== time for boot 12 - 31 minutes later - agrees with time stamp
16:54:10.046 -> Going to sleep now with sense = 1
16:54:10.046 -> getMillis(): 490

CODE:

#include <ESP32Time.h> // https://github.com/fbiego/ESP32Time
ESP32Time rtc;

RTC_DATA_ATTR int bootCount = 0;
RTC_DATA_ATTR int sense = 1;

/*
Method to print the reason by which ESP32
has been awaken from sleep
*/
void print_wakeup_reason() {
esp_sleep_wakeup_cause_t wakeup_reason; // sets type for wakeup_reason

wakeup_reason = esp_sleep_get_wakeup_cause();

switch (wakeup_reason) {
case ESP_SLEEP_WAKEUP_EXT0: Serial.println("Wakeup external RTC_IO"); break;
case ESP_SLEEP_WAKEUP_EXT1: Serial.println("Wakeup external RTC_CNTL"); break;
case ESP_SLEEP_WAKEUP_TIMER: Serial.println("Wakeup caused by timer"); break;
case ESP_SLEEP_WAKEUP_TOUCHPAD: Serial.println("Wakeup caused by touchpad"); break;
case ESP_SLEEP_WAKEUP_ULP: Serial.println("Wakeup caused by ULP program"); break;
default:
Serial.printf("Wakeup was not caused by deep sleep: %d\n", wakeup_reason);
rtc.setTime(30, 24, 15, 17, 1, 2021); // 17th Jan 2021 15:24:30
}
}

void setup() {
Serial.begin(115200);
Serial.printf("getMillis(): %d\n", rtc.getMillis());

delay(1000); //Take some time to open up the Serial Monitor

//Increment boot number and print it every reboot
++bootCount;
Serial.printf("Boot number: %d\n", bootCount);

//Print the wakeup reason for ESP32
print_wakeup_reason();
Serial.println(rtc.getTime("%A, %B %d %Y %H:%M:%S")); // (String) returns time with specified format

// change sense of wakeup pin
esp_sleep_enable_ext0_wakeup(GPIO_NUM_39, sense); // 1 = High, 0 = Low
sense = (sense == 1 ? 0 : 1);

//Go to sleep now
Serial.printf("Going to sleep now with sense = %d\n", sense);
Serial.printf("getMillis(): %d\n", rtc.getMillis());
esp_deep_sleep_start();
Serial.println("This will never be printed");
}

void loop() {
//This is not going to be called
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant