Skip to content

timerReadMicros returns older value #4160

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

Closed
ummarbhutta opened this issue Jul 12, 2020 · 5 comments
Closed

timerReadMicros returns older value #4160

ummarbhutta opened this issue Jul 12, 2020 · 5 comments
Labels
Status: Stale Issue is stale stage (outdated/stuck)

Comments

@ummarbhutta
Copy link

Hardware:

Board: ESP32 Dev Module
IDE name: Arduino IDE
Upload Speed: 115200
Computer OS: Windows 10

Description:

I am working on an application where I have GPIO interrupt defined, and on each interrupt I want to record timer value for which I am calling timerReadMicros but the problem I am facing is timerReadMicros always return older time when it was last called, and if I call it second time it seems to be close to real value. It looks like timerReadMicros is storing and returning older values. I have tried timerRead , timerReadSeconds . Every function have same behavior. Following is the code snippet

void IRAM_ATTR entry_isr()
{
    value = timerReadMicros(timer); // will always return value it was called the last time
}

if I change the code like this

void IRAM_ATTR entry_isr()
{
    timerReadMicros(timer); // will always return value it was called the last time
    value = timerReadMicros(timer); // kind of hack to get correct value
}

timer configuration is like this

  timer = timerBegin(0,40,true); //Input clock 80MHz, Prescaler = 40, Upward counting ==> 0.5microsecond per counter increment
  timerWrite(timer, 0); //reset timer to 0

I assume I am missing something very basic here.

Any help is really appreciated.

@atanisoft
Copy link
Collaborator

duplicate of #3434. There needs to be a small delay inside timerRead(), you can try delayMicroseconds(1); instead of delay(1); which was mentioned in the other issue (a 1ms delay inside the ISR WILL cause problems).

@atanisoft
Copy link
Collaborator

also if you just need an incremental timestamp you can call esp_timer_get_time(); which will return the number of microseconds since boot.

@ummarbhutta
Copy link
Author

Hi @atanisoft, I wouldn't go for updating the library code and adding delay after update. Not sure why a delay is required, isn't it preventing us from full control on hardware timers? e.g. If I am using timerRead with scaling less than 80 than I can get resolution of less than 1 microseconds.. But putting delay will make it hard.

Anyways thanks for second suggestion, for now I read resolution in microseconds so will give it a try.

@stale
Copy link

stale bot commented Sep 11, 2020

[STALE_SET] This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.

@stale stale bot added the Status: Stale Issue is stale stage (outdated/stuck) label Sep 11, 2020
@stale
Copy link

stale bot commented Sep 26, 2020

[STALE_DEL] This stale issue has been automatically closed. Thank you for your contributions.

@stale stale bot closed this as completed Sep 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Stale Issue is stale stage (outdated/stuck)
Projects
None yet
Development

No branches or pull requests

2 participants