-
-
Notifications
You must be signed in to change notification settings - Fork 86
micros()
sometimes returns incorrect value
#49
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
Comments
i think the begin() frequency is wrong for a 1 ms tick shouldn't it be 1000.0f |
for interrupts, maybe try noInterrupts() and interrupts(), or stronger rollover test?? I don't have an R4 to test yet |
It has a high probability of being an interrupt problem with an occasional extra tick in I found it because of a problem with the I changed the program:
Then you see both cases:
|
noInterrupts() and interrupts() is not an answer. In Uno R3 you can call micros() from an ISR. Here is the code for R3:
|
This comment was marked as off-topic.
This comment was marked as off-topic.
The issue happens in this scenario:
The Uno R3 code micros() reads the state of the interrupt request flag to handle this situation. |
Maybe the TUNDF bit of the AGTCR register ? |
I would propose to configure the timer with a clock divider of 8 (24 MHz / 8 = 3000 ticks per ms):
The micros implementation will be simplified to:
The test cnt > 1500 is necessary to check if the underflow happened before reading the counter. This implementation passes all my tests ... Any other people that can confirm this works fine ? |
the simple test for above implementation is:
This should yield no output, except when agt_time_ms wraps after 1 hr and 10 minutes. |
@Kees-van-der-Oord
|
|
@Kees-van-der-Oord |
It takes 1 hour and 10 minutes before
So it seems it handles the wrap scenario correctly as well ... |
Great test. I actually thought about that since I have users who run my libraries long enough for that to happen. |
A slightly modified version of your test:
shows that the time for the
Note that I set the baud rate to 2000000 bps (I'm not sure the speed is applicable at all ...) |
I have noticed that Serial is really high overhead. The Serial transmit interrupt causes pauses in SPI send. What happens if you put a delay after each print? |
I get unexpected variations in the output of the tests.
I guess this is due to compiler optimization side effects - leave that for now. |
There is one difference with the R3: I use GPT 0 timer to do some tasks, and the interrupt priority of the GPT timer seems to be higher than the AGT timer used for the millis() implementation. In stress, the agt_time_ms variable doesn't get updated anymore :-). Any suggestions to promote the AGT interrupt ? |
this looks like it' s requesting AGT 1 -- should it be requesting AGT 0? |
thanks: I corrected it. |
OK i modified time.cpp with your updates. micros() seems to be working. Good job. |
Good to hear the good news.
I guess we should go for the extra 3% speed and not for the nice solution .... |
I tried to push the changes to a new branch in 'https://github.com/arduino/ArduinoCore-renesas/', but it says:
What is the correct way to submit this solution ? |
I was hoping someone else would answer. In the past things I worked on were submitted by a person with Arduino contacts. |
Hi @Kees-van-der-Oord and @greiman. Thanks for your interest in contributing! Proposals for changes to the codebase hosted in this repository should be submitted via "pull requests" (PRs). An overview of the contribution workflow:
ⓘ It is also possible to edit files and make commits via the GitHub interface, eliminating the need to make a local clone. But contributors often find that it is more convenient to work with the files on their own computer for any significant development work and I see you are already working with a local clone. There is a detailed guide to contributing via the pull request system here: |
1. Fix for anomaly in micros() whwn the counter wrapped just before it was read 2. Specify timer configuration as constants to make micros() considerably faster
I submitted my first pull request: ! |
micros()
sometimes returns incorrect value
Fixed by merging #52 . |
Here is the problem - in this program bgn is often larger than end.
Here is the print out for the first few cases with bgn > end.
Wonder if the interrupt protection is correct. 1000 is a clue.
The text was updated successfully, but these errors were encountered: