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

analogRead seems to speed up system clock #120

Closed
JeroenVanOort opened this issue Jan 8, 2017 · 6 comments
Closed

analogRead seems to speed up system clock #120

JeroenVanOort opened this issue Jan 8, 2017 · 6 comments

Comments

@JeroenVanOort
Copy link

I'm building an energy monitor using EmonLib and I've got most things working now. Since I'll need to add a time stamp to the measurements I send to my web application, I'd like to sync the ESP32's system clock to an NTP server. Sync to NTP doesn't seem to be my problem so far, but keeping time is. I've observed that the system clock on my device seems to go faster when doing a lot of analogRead's, which is necessary for monitoring energy usage.

After a measurement that takes a second, I print the time to serial. I've seen 3 to 4 seconds jumps and sometimes it's just the one second like I'd expect it to. When I comment out the measurement function and put delay(1000); in it's place, time runs normal.

Do you have any clue where this behavior comes from and how we could fix this?

@JeroenVanOort
Copy link
Author

I've made a simple program to test this. I observe that after 30 seconds according to my laptop's clock, about 50 seconds have passed according to the ESP32's clock. When I comment out the for loop, the ESP32's clock runs just fine.

What can we do to fix this?

#include <time.h>

time_t lcd_time;

void setup()
{
    Serial.begin(115200);
}

void loop()
{
    int i;
    for (i = 0; i < 100000; i++) {
      analogRead(36);
    }
    
    time(&lcd_time);
    Serial.println(lcd_time);
    
    delay(1000);
}

@igrr
Copy link
Member

igrr commented Jan 13, 2017

Thanks for the example. I don't have a workaround for you, need to investigate this. The issue is in the esp-idf, not in the Arduino libraries.

@igrr
Copy link
Member

igrr commented Jan 16, 2017

We were able to reproduce this, and this looks like a hardware bug, i.e. a write to FRC_TIMER_INT_REG(0) gets lost, so the timer interrupt doesn't get cleared... and for some reason this happens when ADC is in use. We will try to reproduce this in the simulation environment.

For now, the workaround is to add a write to some random memory address into the frc_timer_isr function (components/newlib/time.c:85), for example

WRITE_PERI_REG(DR_REG_FRC_TIMER_BASE + 0x60, 0xabababab);

It can go before or after WRITE_PERI_REG(FRC_TIMER_INT_REG(0), FRC_TIMER_INT_CLR); (both ways seem to fix the problem).

We'll update ESP-IDF with this workaround while we are trying to narrow down the root cause.

@tolson2000
Copy link

This workaround does seem to work using the esp-idf with arduino as component.
I don't know how to move this to the arduino-esp32 though to use with Arduino IDE.

@igrr
Copy link
Member

igrr commented Jan 17, 2017

The fix is staged for merging into the ESP-IDF. Once it is merged, @me-no-dev can update libraries in this repository to make the fix available in Arduino IDE.

igrr added a commit to espressif/esp-idf that referenced this issue Jan 17, 2017
Fix issues with time syscalls

- fix compilation error when no time source is selected (#238)
- work around a bug that time speeds up when RTC registers are read on the APP CPU (espressif/arduino-esp32#120)


See merge request !427
@g01d10x
Copy link

g01d10x commented Mar 25, 2018

Hello @igrr and @me-no-dev .. I am really loving the ESP32 devices and community :)

Does anyone know if this 'time warp' was ever addressed or made it through to the Arduino side of things? I'm doing some ADC related work and noticed that time seems to accelerate for the ESP32 vs the rest of the world. I followed the full installation instructions as of the github page about 1 month ago, and it seems that this bug is still present.

Looking forward to info on how I can address this in my Apps :)

Cheers from Canada !

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

4 participants